original_code:
t_bool sort_item(t_item **tab)
{
  int i;
  i = 0;
  while (tab[i + 1] != ((void *)0))
    {
      if (my_strcmp_case(tab[i]->name, tab[i + 1]->name) > 0)
 {
   my_swap(tab + i, tab + i + 1);
   i = 0;
 }
      else
 i++;
    }
  return (1);
}

canonical_original_code:
unsigned char sort_item(struct s_item **tab)
{
  int i;
  i = 0;
  while (tab[i + 1] != ((void *)0))
    {
      if (my_strcmp_case(tab[i]->name, tab[i + 1]->name) > 0)
 {
   my_swap(tab + i, tab + i + 1);
   i = 0;
 }
      else
 i++;
    }
  return (1);
}

variable_types: i int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
void *__fastcall creat_path(_QWORD *a1)
{
__int64 v1; // r12
void *i; // r13
void *v3; // rbx
void *v4; // rbp
int v5; // ebx

if ( !*a1 )
return &unk_17C;
v1 = 1LL;
for ( i = 0LL; ; i = v4 )
{
v3 = (void *)my_stradd(&unk_17C);
if ( !v3 )
return 0LL;
v4 = (void *)my_stradd(i);
if ( !v4 )
return v4;
free(v3);
v5 = v1;
if ( i )
free(i);
if ( !a1[v1++] )
break;
}
if ( !v5 )
v4 = &unk_17C;
return v4;
}

canonical_decompiled_code:
void *__fastcall func1(_QWORD *a1)
{
__int64 v1; // r12
void *i; // r13
void *v3; // rbx
void *v4; // rbp
int v5; // ebx

if ( !*a1 )
return &unk_17C;
v1 = 1LL;
for ( i = 0LL; ; i = v4 )
{
v3 = (void *)my_stradd(&unk_17C);
if ( !v3 )
return 0LL;
v4 = (void *)my_stradd(i);
if ( !v4 )
return v4;
free(v3);
v5 = v1;
if ( i )
free(i);
if ( !a1[v1++] )
break;
}
if ( !v5 )
v4 = &unk_17C;
return v4;
}

original_code:
char *creat_path(char **path)
{
  char *ret;
  char *temp;
  char *temp2;
  int i;
  i = 0;
  ret = ((void *)0);
  while (path[i] != ((void *)0))
    {
      if ((temp = my_stradd("/", path[i])) == ((void *)0))
 return (((void *)0));
      if ((temp2 = my_stradd(ret, temp)) == ((void *)0))
 return (((void *)0));
      free(temp);
      i++;
      if (ret != ((void *)0))
 free(ret);
      ret = temp2;
    }
  if (i == 0)
    return ("/");
  return (ret);
}

variable_types:
path
char **

variable_types:
ret
char *

variable_types:
temp
char *

return_type: char *


decompiled_code:
__int64 __fastcall my_strdup(char *a1)
{
int v1; // eax
__int64 v2; // rcx
char v3; // dl
__int64 v4; // rax

if ( !a1 )
return 0LL;
v1 = my_strlen();
v2 = my_malloc((unsigned int)(v1 + 1));
if ( v2 )
{
v3 = *a1;
if ( *a1 )
{
v4 = 0LL;
do
{
*(_BYTE *)(v2 + v4++) = v3;
v3 = a1[v4];
}
while ( v3 );
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
int v1; // eax
__int64 v2; // rcx
char v3; // dl
__int64 v4; // rax

if ( !a1 )
return 0LL;
v1 = my_strlen();
v2 = my_malloc((unsigned int)(v1 + 1));
if ( v2 )
{
v3 = *a1;
if ( *a1 )
{
v4 = 0LL;
do
{
*(_BYTE *)(v2 + v4++) = v3;
v3 = a1[v4];
}
while ( v3 );
}
}
return v2;
}

original_code:
char *my_strdup(char *str)
{
  char *ret;
  int i;
  if (!str)
    return (str);
  i = 0;
  if ((ret = my_malloc(sizeof(*ret) * (my_strlen(str) + 1))) == ((void *)0))
    return (((void *)0));
  while (str[i] != '\0')
    {
      ret[i] = str[i];
      i++;
    }
  return (ret);
}

variable_types:
str
char *


DefaultFreeEntry

repo: emeryberger/eon

decompiled_code:
void __fastcall DefaultFreeEntry(__int64 a1, void *a2, int a3)
{
if ( a3 == 1 )
free(a2);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, void *a2, int a3)
{
if ( a3 == 1 )
free(a2);
}

original_code:
static void
DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
{
    if (flag == 1)
        free(he);
}

canonical_original_code:
static void DefaultFreeEntry(void *pool, struct PLHashEntry *he, unsigned int flag)
{
    if (flag == 1)
        free(he);
}

variable_types:
pool
void *

variable_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *

variable_types:
flag
unsigned int

return_type: void


DefaultFreeTable

decompiled_code:
void __fastcall DefaultFreeTable(__int64 a1, void *a2)
{
free(a2);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, void *a2)
{
free(a2);
}

original_code:
static void
DefaultFreeTable(void *pool, void *item)
{
    free(item);
}

variable_types:
item
void *


DefaultAllocEntry

decompiled_code:
void *DefaultAllocEntry()
{
return malloc(0x20uLL);
}

canonical_decompiled_code:
void *func2()
{
return malloc(0x20uLL);
}

original_code:
static PLHashEntry *
DefaultAllocEntry(void *pool, const void *key)
{
    return malloc(sizeof(PLHashEntry));
}

canonical_original_code:
static struct PLHashEntry *DefaultAllocEntry(void *pool, void *key)
{
    return malloc(sizeof(PLHashEntry));
}

variable_types:
key
void *

return_type: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *


DefaultAllocTable

decompiled_code:
void *__fastcall DefaultAllocTable(__int64 a1, size_t a2)
{
return malloc(a2);
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, size_t a2)
{
return malloc(a2);
}

original_code:
static void *
DefaultAllocTable(void *pool, PRSize size)
{
    return malloc(size);
}

canonical_original_code:
static void *DefaultAllocTable(void *pool, long size)
{
    return malloc(size);
}

variable_types: size long

return_type: void *


original_code:
void
PL_HashTableDestroy(PLHashTable *ht)
{
    PRUint32 i, n;
    PLHashEntry *he, *next;
    const PLHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;
    n = (1 << (32 - (ht)->shift));
    for (i = 0; i < n; i++) {
        for (he = ht->buckets[i]; he; he = next) {
            next = he->next;
            (*allocOps->freeEntry)(allocPriv, he, 1);
        }
    }
    (*allocOps->freeTable)(allocPriv, ht->buckets);
    (*allocOps->freeTable)(allocPriv, ht);
}

canonical_original_code:
void PL_HashTableDestroy(struct PLHashTable *ht)
{
    unsigned int i; unsigned int n;
    struct PLHashEntry *he; struct PLHashEntry *next;
    struct PLHashAllocOps *allocOps = ht->allocOps;
    void *allocPriv = ht->allocPriv;
    n = (1 << (32 - (ht)->shift));
    for (i = 0; i < n; i++) {
        for (he = ht->buckets[i]; he; he = next) {
            next = he->next;
            (*allocOps->freeEntry)(allocPriv, he, 1);
        }
    }
    (*allocOps->freeTable)(allocPriv, ht->buckets);
    (*allocOps->freeTable)(allocPriv, ht);
}

variable_types: struct PLHashTable { struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } **buckets; unsigned int nentries; unsigned int shift; unsigned int (*keyHash)(void *key); int (*keyCompare)(void *v1, void *v2); int (*valueCompare)(void *v1, void *v2); struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); } *allocOps; void *allocPriv; } *

variable_types:
i
unsigned int

variable_types:
n
unsigned int

variable_types: struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); } *

variable_types:
allocPriv
void *

user_defined_types: struct PLHashTable { struct PLHashEntry **buckets; unsigned int nentries; unsigned int shift; unsigned int (*keyHash)(void *key); int (*keyCompare)(void *v1, void *v2); int (*valueCompare)(void *v1, void *v2); struct PLHashAllocOps *allocOps; void *allocPriv; }


original_code:
void
PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he)
{
    PRUint32 i, n;
    PLHashEntry *next, **oldbuckets;
    PRSize nb;
    *hep = he->next;
    (*ht->allocOps->freeEntry)(ht->allocPriv, he, 1);
    n = (1 << (32 - (ht)->shift));
    if (--ht->nentries < (((n) > (1 << 4)) ? ((n) >> 2) : 0)) {
        oldbuckets = ht->buckets;
        nb = n * sizeof(PLHashEntry*) / 2;
        ht->buckets = (PLHashEntry**)(
            (*ht->allocOps->allocTable)(ht->allocPriv, nb));
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return;
        }
        memset(ht->buckets, 0, nb);
        ht->shift++;
        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
                do {} while(0);
                he->next = 0;
                *hep = he;
            }
        }
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
    }
}

canonical_original_code:
void PL_HashTableRawRemove(struct PLHashTable *ht, struct PLHashEntry **hep, struct PLHashEntry *he)
{
    unsigned int i; unsigned int n;
    struct PLHashEntry *next; struct PLHashEntry **oldbuckets;
    long nb;
    *hep = he->next;
    (*ht->allocOps->freeEntry)(ht->allocPriv, he, 1);
    n = (1 << (32 - (ht)->shift));
    if (--ht->nentries < (((n) > (1 << 4)) ? ((n) >> 2) : 0)) {
        oldbuckets = ht->buckets;
        nb = n * sizeof(struct PLHashEntry *) / 2;
        ht->buckets = (struct PLHashEntry **)(
            (*ht->allocOps->allocTable)(ht->allocPriv, nb));
        if (!ht->buckets) {
            ht->buckets = oldbuckets;
            return;
        }
        memset(ht->buckets, 0, nb);
        ht->shift++;
        for (i = 0; i < n; i++) {
            for (he = oldbuckets[i]; he; he = next) {
                next = he->next;
                hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
                do {} while(0);
                he->next = 0;
                *hep = he;
            }
        }
        (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
    }
}

variable_types:
hep
struct PLHashEntry **

variable_types:
he
struct PLHashEntry *

variable_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; } **

variable_types: nb long

user_defined_types: struct PLHashAllocOps { void *(*allocTable)(void *pool, long size); void (*freeTable)(void *pool, void *item); struct PLHashEntry *(*allocEntry)(void *pool, void *key); void (*freeEntry)(void *pool, struct PLHashEntry *he, unsigned int flag); }


decompiled_code:
__int64 __fastcall PL_HashTableRemove(__int64 a1, __int64 a2)
{
int v2; // eax
_QWORD *v3; // rsi
__int64 result; // rax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
v3 = (_QWORD *)PL_HashTableRawLookup(a1, v2, a2);
result = 0LL;
if ( *v3 )
{
PL_HashTableRawRemove(a1, v3, (_QWORD *)*v3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
int v2; // eax
_QWORD *v3; // rsi
__int64 result; // rax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
v3 = (_QWORD *)func6(a1, v2, a2);
result = 0LL;
if ( *v3 )
{
func7(a1, v3, (_QWORD *)*v3);
result = 1LL;
}
return result;
}

original_code:
PRBool
PL_HashTableRemove(PLHashTable *ht, const void *key)
{
    PLHashNumber keyHash;
    PLHashEntry *he, **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) == 0)
        return 0;
    PL_HashTableRawRemove(ht, hep, he);
    return 1;
}

canonical_original_code:
int PL_HashTableRemove(struct PLHashTable *ht, void *key)
{
    unsigned int keyHash;
    struct PLHashEntry *he; struct PLHashEntry **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) == 0)
        return 0;
    PL_HashTableRawRemove(ht, hep, he);
    return 1;
}

variable_types:
keyHash
unsigned int

return_type: int

user_defined_types: struct PLHashEntry { struct PLHashEntry *next; unsigned int keyHash; void *key; void *value; }


decompiled_code:
__int64 __fastcall PL_HashTableLookup(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
result = *(_QWORD *)PL_HashTableRawLookup(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 24);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax

v2 = (*(__int64 (__fastcall **)(__int64))(a1 + 16))(a2);
result = *(_QWORD *)func6(a1, v2, a2);
if ( result )
result = *(_QWORD *)(result + 24);
return result;
}

original_code:
void *
PL_HashTableLookup(PLHashTable *ht, const void *key)
{
    PLHashNumber keyHash;
    PLHashEntry *he, **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != 0) {
        return he->value;
    }
    return 0;
}

canonical_original_code:
void *PL_HashTableLookup(struct PLHashTable *ht, void *key)
{
    unsigned int keyHash;
    struct PLHashEntry *he; struct PLHashEntry **hep;
    keyHash = (*ht->keyHash)(key);
    hep = PL_HashTableRawLookup(ht, keyHash, key);
    if ((he = *hep) != 0) {
        return he->value;
    }
    return 0;
}


original_code:
int
PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg)
{
    PLHashEntry *he, **hep;
    PRUint32 i, nbuckets;
    int rv, n = 0;
    PLHashEntry *todo = 0;
    nbuckets = (1 << (32 - (ht)->shift));
    for (i = 0; i < nbuckets; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != 0) {
            rv = (*f)(he, n, arg);
            n++;
            if (rv & (2 | 4)) {
                *hep = he->next;
                if (rv & 2) {
                    he->next = todo;
                    todo = he;
                }
            } else {
                hep = &he->next;
            }
            if (rv & 1) {
                goto out;
            }
        }
    }
out:
    hep = &todo;
    while ((he = *hep) != 0) {
        PL_HashTableRawRemove(ht, hep, he);
    }
    return n;
}

canonical_original_code:
int PL_HashTableEnumerateEntries(struct PLHashTable *ht, int (*f)(struct PLHashEntry *he, int i, void *arg), void *arg)
{
    struct PLHashEntry *he; struct PLHashEntry **hep;
    unsigned int i; unsigned int nbuckets;
    int rv; int n = 0;
    struct PLHashEntry *todo = 0;
    nbuckets = (1 << (32 - (ht)->shift));
    for (i = 0; i < nbuckets; i++) {
        hep = &ht->buckets[i];
        while ((he = *hep) != 0) {
            rv = (*f)(he, n, arg);
            n++;
            if (rv & (2 | 4)) {
                *hep = he->next;
                if (rv & 2) {
                    he->next = todo;
                    todo = he;
                }
            } else {
                hep = &he->next;
            }
            if (rv & 1) {
                goto out;
            }
        }
    }
out:
    hep = &todo;
    while ((he = *hep) != 0) {
        PL_HashTableRawRemove(ht, hep, he);
    }
    return n;
}

variable_types: int (*)(struct PLHashEntry *he, int i, void *arg)

variable_types:
arg
void *

variable_types:
nbuckets
unsigned int

variable_types: rv int

variable_types: n int


decompiled_code:
__int64 __fastcall PL_HashString(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v2 = v1 ^ __ROL4__(v2, 4);
v1 = *++a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned __int8 *a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
v2 = v1 ^ __ROL4__(v2, 4);
v1 = *++a1;
}
while ( *a1 );
return v2;
}

original_code:
PLHashNumber
PL_HashString(const void *key)
{
    PLHashNumber h;
    const PRUint8 *s;
    h = 0;
    for (s = (const PRUint8*)key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ *s;
    return h;
}

canonical_original_code:
unsigned int PL_HashString(void *key)
{
    unsigned int h;
    unsigned char *s;
    h = 0;
    for (s = (unsigned char *)key; *s; s++)
        h = (h >> 28) ^ (h << 4) ^ *s;
    return h;
}

variable_types:
key
void *

variable_types:
h
unsigned int

variable_types:
s
unsigned char *

return_type: unsigned int


decompiled_code:
_BOOL8 __fastcall PL_CompareStrings(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(const char *a1, const char *a2)
{
return strcmp(a1, a2) == 0;
}

original_code:
int
PL_CompareStrings(const void *v1, const void *v2)
{
  return strcmp((const char*)v1, (const char*)v2) == 0;
}

canonical_original_code:
int PL_CompareStrings(void *v1, void *v2)
{
  return strcmp((char *)v1, (char *)v2) == 0;
}


decompiled_code:
_BOOL8 __fastcall PL_CompareValues(__int64 a1, __int64 a2)
{
return a2 == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(__int64 a1, __int64 a2)
{
return a2 == a1;
}

original_code:
int
PL_CompareValues(const void *v1, const void *v2)
{
    return v1 == v2;
}

canonical_original_code:
int PL_CompareValues(void *v1, void *v2)
{
    return v1 == v2;
}


saferead

decompiled_code:
__int64 __fastcall saferead(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er14

if ( a3 > 0 )
{
v4 = a3;
v5 = 0;
while ( 1 )
{
v6 = read(fd, buf, v4);
v7 = v6;
if ( v6 == -1 )
{
if ( *_errno_location() != 4 )
return v7;
}
else
{
if ( !v6 )
return v5;
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
if ( v4 <= 0 )
return v5;
}
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er14

if ( a3 > 0 )
{
v4 = a3;
v5 = 0;
while ( 1 )
{
v6 = read(fd, buf, v4);
v7 = v6;
if ( v6 == -1 )
{
if ( *_errno_location() != 4 )
return v7;
}
else
{
if ( !v6 )
return v5;
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
if ( v4 <= 0 )
return v5;
}
}
return 0;
}

original_code:
int saferead(int fd, void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = read(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      if (n == 0)
 return actual;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}

variable_types: fd int

variable_types:
buffer
void *

variable_types: count int

variable_types: actual int


safewrite

decompiled_code:
__int64 __fastcall safewrite(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er14

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 == -1 )
{
if ( *_errno_location() != 4 )
return v7;
}
else
{
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
}
while ( v4 > 0 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, int a3)
{
int v4; // ebx
unsigned int v5; // er12
unsigned int v6; // eax
unsigned int v7; // er14

if ( a3 <= 0 )
return 0;
v4 = a3;
v5 = 0;
do
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 == -1 )
{
if ( *_errno_location() != 4 )
return v7;
}
else
{
v4 -= v6;
v5 += v6;
buf = (char *)buf + (int)v6;
}
}
while ( v4 > 0 );
return v5;
}

original_code:
int safewrite(int fd, const void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = write(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}

canonical_original_code:
int safewrite(int fd, void *buffer, int count)
{
  int actual = 0;
  while (count > 0)
    {
      int n = write(fd, buffer, count);
      if (n == -1 && (*__errno_location ()) == 4)
 continue;
      if (n == -1)
 return -1;
      count -= n;
      actual += n;
      buffer += n;
    }
  return actual;
}


decompiled_code:
__int64 __fastcall init_sf_source(int fd)
{
char v1; // al
unsigned int v2; // edx
char v4[4]; // [rsp+8h] [rbp-10h] BYREF
char v5; // [rsp+Ch] [rbp-Ch] BYREF
char v6; // [rsp+Dh] [rbp-Bh]
char buf; // [rsp+Eh] [rbp-Ah] BYREF
char v8; // [rsp+Fh] [rbp-9h]

v5 = 84;
v6 = 33;
if ( (unsigned int)safewrite(fd, &v5, 2) != 2 )
return (unsigned int)-1;
if ( (unsigned int)saferead(fd, &buf, 2) != 2 )
return (unsigned int)-1;
if ( buf != 84 )
return (unsigned int)-1;
v1 = v8;
if ( v8 <= 31 )
return (unsigned int)-1;
if ( v6 <= v8 )
v1 = v6;
v2 = 0;
if ( v1 == 33 )
{
v4[0] = platform;
v4[1] = unk_34D;
v4[2] = unk_34E;
v4[3] = unk_34F;
if ( (unsigned int)safewrite(fd, v4, 4) == 4 )
v2 = -((unsigned int)saferead(fd, v4, 4) != 4);
else
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd)
{
char v1; // al
unsigned int v2; // edx
char v4[4]; // [rsp+8h] [rbp-10h] BYREF
char v5; // [rsp+Ch] [rbp-Ch] BYREF
char v6; // [rsp+Dh] [rbp-Bh]
char buf; // [rsp+Eh] [rbp-Ah] BYREF
char v8; // [rsp+Fh] [rbp-9h]

v5 = 84;
v6 = 33;
if ( (unsigned int)func2(fd, &v5, 2) != 2 )
return (unsigned int)-1;
if ( (unsigned int)func1(fd, &buf, 2) != 2 )
return (unsigned int)-1;
if ( buf != 84 )
return (unsigned int)-1;
v1 = v8;
if ( v8 <= 31 )
return (unsigned int)-1;
if ( v6 <= v8 )
v1 = v6;
v2 = 0;
if ( v1 == 33 )
{
v4[0] = platform;
v4[1] = unk_34D;
v4[2] = unk_34E;
v4[3] = unk_34F;
if ( (unsigned int)func2(fd, v4, 4) == 4 )
v2 = -((unsigned int)func1(fd, v4, 4) != 4);
else
v2 = -1;
}
return v2;
}

original_code:
int init_sf_source(int fd)
{
  char check[2], us[2];
  int version;
  unsigned char nonce[4];
  us[0] = 'T'; us[1] = '!';
  if (safewrite(fd, us, 2) != 2 ||
      saferead(fd, check, 2) != 2 ||
      check[0] != 'T' || check[1] < ' ')
    return -1;
  version = check[1];
  if (us[1] < version)
    version = us[1];
  switch (version)
    {
    case ' ': break;
    case '!':
      nonce[0] = platform;
      nonce[1] = platform >> 8;
      nonce[2] = platform >> 16;
      nonce[3] = platform >> 24;
      if (safewrite(fd, nonce, 4) != 4)
 return -1;
      if (saferead(fd, nonce, 4) != 4)
 return -1;
      break;
    }
  return 0;
}

canonical_original_code:
int init_sf_source(int fd)
{
  char check[2]; char us[2];
  int version;
  unsigned char nonce[4];
  us[0] = 'T'; us[1] = '!';
  if (safewrite(fd, us, 2) != 2 ||
      saferead(fd, check, 2) != 2 ||
      check[0] != 'T' || check[1] < ' ')
    return -1;
  version = check[1];
  if (us[1] < version)
    version = us[1];
  switch (version)
    {
    case ' ': break;
    case '!':
      nonce[0] = platform;
      nonce[1] = platform >> 8;
      nonce[2] = platform >> 16;
      nonce[3] = platform >> 24;
      if (safewrite(fd, nonce, 4) != 4)
 return -1;
      if (saferead(fd, nonce, 4) != 4)
 return -1;
      break;
    }
  return 0;
}

variable_types:
check
char [2]

variable_types:
us
char [2]

variable_types: version int

variable_types:
nonce
unsigned char [4]


decompiled_code:
__int64 __fastcall open_sf_source(char *name, __int16 a2)
{
int v2; // ebx
struct hostent *v3; // rax
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 >= 0 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v5[0].sa_family = v3->h_addrtype;
memcpy(&v5[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v5[0].sa_data = _bswap_16(a2);
if ( connect(v2, v5, 0x10u) < 0 || (int)init_sf_source(v2) < 0 )
{
close(v2);
v2 = -1;
}
}
else
{
close(v2);
v2 = -1;
}
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *name, __int16 a2)
{
int v2; // ebx
struct hostent *v3; // rax
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 >= 0 )
{
v3 = gethostbyname(name);
if ( v3 )
{
v5[0].sa_family = v3->h_addrtype;
memcpy(&v5[0].sa_data[2], *(const void **)v3->h_addr_list, v3->h_length);
*(_WORD *)v5[0].sa_data = func0(a2);
if ( connect(v2, v5, 0x10u) < 0 || (int)func3(v2) < 0 )
{
close(v2);
v2 = -1;
}
}
else
{
close(v2);
v2 = -1;
}
}
return (unsigned int)v2;
}

original_code:
int open_sf_source(const char *host, int port)
{
  int fd = socket(2, SOCK_STREAM, 0);
  struct hostent *entry;
  struct sockaddr_in addr;
  if (fd < 0)
    return fd;
  entry = gethostbyname(host);
  if (!entry)
    {
      close(fd);
      return -1;
    }
  addr.sin_family = entry->h_addrtype;
  memcpy(&addr.sin_addr, entry->h_addr_list[0], entry->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0)
    {
      close(fd);
      return -1;
    }
  if (init_sf_source(fd) < 0)
    {
      close(fd);
      return -1;
    }
  return fd;
}

canonical_original_code:
int open_sf_source(char *host, int port)
{
  int fd = socket(2, SOCK_STREAM, 0);
  struct hostent *entry;
  struct sockaddr_in addr;
  if (fd < 0)
    return fd;
  entry = gethostbyname(host);
  if (!entry)
    {
      close(fd);
      return -1;
    }
  addr.sin_family = entry->h_addrtype;
  memcpy(&addr.sin_addr, entry->h_addr_list[0], entry->h_length);
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0)
    {
      close(fd);
      return -1;
    }
  if (init_sf_source(fd) < 0)
    {
      close(fd);
      return -1;
    }
  return fd;
}

variable_types:
host
char *

variable_types: port int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
void *__fastcall read_sf_packet(int fd, _DWORD *a2)
{
void *v2; // rbx
unsigned __int8 v3; // r13
void *v4; // rax
int v5; // edx
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v2 = 0LL;
if ( (unsigned int)saferead(fd, buf, 1) == 1 )
{
v3 = buf[0];
v4 = malloc((unsigned __int8)buf[0]);
v2 = v4;
if ( v4 )
{
v5 = saferead(fd, v4, v3);
if ( v5 == (unsigned __int8)buf[0] )
{
*a2 = (unsigned __int8)buf[0];
}
else
{
free(v2);
v2 = 0LL;
}
}
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func5(int fd, _DWORD *a2)
{
void *v2; // rbx
unsigned __int8 v3; // r13
void *v4; // rax
int v5; // edx
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

v2 = 0LL;
if ( (unsigned int)func1(fd, buf, 1) == 1 )
{
v3 = buf[0];
v4 = malloc((unsigned __int8)buf[0]);
v2 = v4;
if ( v4 )
{
v5 = func1(fd, v4, v3);
if ( v5 == (unsigned __int8)buf[0] )
{
*a2 = (unsigned __int8)buf[0];
}
else
{
free(v2);
v2 = 0LL;
}
}
}
return v2;
}

original_code:
void *read_sf_packet(int fd, int *len)
{
  unsigned char l;
  void *packet;
  if (saferead(fd, &l, 1) != 1)
    return ((void *)0);
  packet = malloc(l);
  if (!packet)
    return ((void *)0);
  if (saferead(fd, packet, l) != l)
    {
      free(packet);
      return ((void *)0);
    }
  *len = l;
  return packet;
}

variable_types:
len
int *

variable_types:
l
unsigned char

variable_types:
packet
void *


decompiled_code:
__int64 __fastcall write_sf_packet(int fd, void *buf, unsigned __int8 a3)
{
int v3; // eax
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

bufa[0] = a3;
if ( (unsigned int)safewrite(fd, bufa, 1) != 1 )
return 0xFFFFFFFFLL;
v3 = safewrite(fd, buf, (unsigned __int8)bufa[0]);
return (unsigned int)-((unsigned __int8)bufa[0] != v3);
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, void *buf, unsigned __int8 a3)
{
int v3; // eax
char bufa[25]; // [rsp+Fh] [rbp-19h] BYREF

bufa[0] = a3;
if ( (unsigned int)func2(fd, bufa, 1) != 1 )
return 0xFFFFFFFFLL;
v3 = func2(fd, buf, (unsigned __int8)bufa[0]);
return (unsigned int)-((unsigned __int8)bufa[0] != v3);
}

original_code:
int write_sf_packet(int fd, const void *packet, int len)
{
  unsigned char l = len;
  if (safewrite(fd, &l, 1) != 1 ||
      safewrite(fd, packet, l) != l)
    return -1;
  return 0;
}

canonical_original_code:
int write_sf_packet(int fd, void *packet, int len)
{
  unsigned char l = len;
  if (safewrite(fd, &l, 1) != 1 ||
      safewrite(fd, packet, l) != l)
    return -1;
  return 0;
}

variable_types: len int


decompiled_code:
__int64 __fastcall hton_int16(_WORD *a1, __int16 a2)
{
*a1 = a2;
return HIBYTE(a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_WORD *a1, __int16 a2)
{
*a1 = a2;
return HIBYTE(a2);
}

original_code:
void hton_int16(uint8_t *buf, int16_t data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
}

canonical_original_code:
void hton_int16(unsigned char *buf, short data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
}

variable_types:
buf
unsigned char *

variable_types: data short


decompiled_code:
__int64 __fastcall hton_int32(unsigned int *a1, unsigned int a2)
{
*a1 = a2;
return HIBYTE(a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, unsigned int a2)
{
*a1 = a2;
return HIBYTE(a2);
}

original_code:
void hton_int32(uint8_t *buf, int32_t data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
  buf[2] = (data >> 16) & 0x00ff;
  buf[3] = (data >> 24) & 0x00ff;
}

canonical_original_code:
void hton_int32(unsigned char *buf, int data)
{
  buf[0] = data & 0x00ff;
  buf[1] = (data >> 8) & 0x00ff;
  buf[2] = (data >> 16) & 0x00ff;
  buf[3] = (data >> 24) & 0x00ff;
}

variable_types: data int


decompiled_code:
__int64 __fastcall hton_uint16(_WORD *a1, __int16 a2)
{
return hton_int16(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(_WORD *a1, __int16 a2)
{
return func0(a1, a2);
}

original_code:
void hton_uint16(uint8_t *buf, uint16_t data)
{
  hton_int16(buf,data);
}

canonical_original_code:
void hton_uint16(unsigned char *buf, unsigned short data)
{
  hton_int16(buf,data);
}

variable_types:
data
unsigned short


decompiled_code:
__int64 __fastcall hton_uint32(unsigned int *a1, unsigned int a2)
{
return hton_int32(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1, unsigned int a2)
{
return func1(a1, a2);
}

original_code:
void hton_uint32(uint8_t *buf, uint32_t data)
{
  hton_int32(buf,data);
}

canonical_original_code:
void hton_uint32(unsigned char *buf, unsigned int data)
{
  hton_int32(buf,data);
}

variable_types:
data
unsigned int


decompiled_code:
__int64 __fastcall ntoh_int32(unsigned __int8 *a1, _DWORD *a2)
{
__int64 result; // rax

result = (a1[3] << 24) | *a1 | (a1[2] << 16) | (a1[1] << 8);
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 *a1, _DWORD *a2)
{
__int64 result; // rax

result = (a1[3] << 24) | *a1 | (a1[2] << 16) | (a1[1] << 8);
*a2 = result;
return result;
}

canonical_original_code:
void ntoh_int32(unsigned char *buf, int *data)
{
 *data = ((int)buf[0]) |
   (((int)buf[1]) << 8) |
   (((int)buf[2]) << 16) |
   (((int)buf[3]) << 24);
}

variable_types:
data
int *


decompiled_code:
__int64 __fastcall ntoh_uint32(unsigned __int8 *a1, _DWORD *a2)
{
return ntoh_int32(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 *a1, _DWORD *a2)
{
return func5(a1, a2);
}

original_code:
void ntoh_uint32(uint8_t *buf, uint32_t *data)
{
 ntoh_int32(buf,(int32_t*)data);
}

canonical_original_code:
void ntoh_uint32(unsigned char *buf, unsigned int *data)
{
 ntoh_int32(buf,(int *)data);
}

variable_types:
data
unsigned int *


dbg

decompiled_code:
__int64 dbg(__int64 a1, const char *a2, ...)
{
unsigned int v2; // ebx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
pthread_mutex_lock(&mutex);
v2 = vprintf(a2, arg);
pthread_mutex_unlock(&mutex);
return v2;
}

canonical_decompiled_code:
__int64 func8(__int64 a1, const char *a2, ...)
{
unsigned int v2; // ebx
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
pthread_mutex_lock(&mutex);
v2 = vprintf(a2, arg);
pthread_mutex_unlock(&mutex);
return v2;
}

original_code:
int dbg(int level, const char* format, ...)
{
 int val = 0;
 va_list ap;
 pthread_mutex_lock(&mutex);
 __builtin_va_start(ap,format);
 val = vprintf(format, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&mutex);
 return val;
}

canonical_original_code:
int dbg(int level, char *format, ...)
{
 int val = 0;
 __builtin_va_list ap;
 pthread_mutex_lock(&mutex);
 __builtin_va_start(ap,format);
 val = vprintf(format, ap);
 __builtin_va_end(ap);
 pthread_mutex_unlock(&mutex);
 return val;
}

variable_types: level int

variable_types:
format
char *

variable_types: val int


decompiled_code:
__int64 __fastcall open_telos_source(__int64 a1, unsigned int a2)
{
unsigned int v2; // ebx

pthread_mutex_lock(&sfwritemutex);
v2 = open_sf_source(a1, a2);
pthread_mutex_unlock(&sfwritemutex);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
unsigned int v2; // ebx

pthread_mutex_lock(&sfwritemutex);
v2 = open_sf_source(a1, a2);
pthread_mutex_unlock(&sfwritemutex);
return v2;
}

original_code:
int open_telos_source(const char *host, int port)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = open_sf_source(host,port);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

canonical_original_code:
int open_telos_source(char *host, int port)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = open_sf_source(host,port);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

variable_types:
host
char *

variable_types: port int

variable_types: result int


decompiled_code:
__int64 __fastcall init_telos_source(unsigned int a1)
{
unsigned int v1; // ebx

pthread_mutex_lock(&sfwritemutex);
v1 = init_sf_source(a1);
pthread_mutex_unlock(&sfwritemutex);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v1; // ebx

pthread_mutex_lock(&sfwritemutex);
v1 = init_sf_source(a1);
pthread_mutex_unlock(&sfwritemutex);
return v1;
}

original_code:
int init_telos_source(int fd)
{
  int result;
  pthread_mutex_lock(&sfwritemutex);
  result = init_sf_source(fd);
  pthread_mutex_unlock(&sfwritemutex);
  return result;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall free_telos_packet(void **a1)
{
free(*((void **)*a1 + 1));
free(*a1);
*a1 = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void **a1)
{
free(*((void **)*a1 + 1));
free(*a1);
*a1 = 0LL;
return 0LL;
}

original_code:
int free_telos_packet(telospacket **pkt)
{
 free((*pkt)->data);
 free(*pkt);
 *pkt = ((void *)0);
 return 0;
}

canonical_original_code:
int free_telos_packet(struct telospacket **pkt)
{
 free((*pkt)->data);
 free(*pkt);
 *pkt = ((void *)0);
 return 0;
}

variable_types: struct telospacket { unsigned char length; unsigned char dsn; unsigned short addr; unsigned char type; unsigned char group; unsigned char *data; } **


decompiled_code:
__int64 __fastcall write_telos_packet(unsigned int a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // bl
char *v4; // rbp
char v5; // al
__int16 v6; // ax
char *v7; // rcx
__int64 v8; // rax
char *v9; // rsi
unsigned int v10; // ebx
__int64 v11; // rbx

v2 = *a2;
if ( *a2 > 0x3Cu )
{
dbg(0LL, "packet too long! (%i bytes)\n", v2);
v10 = -1;
}
else
{
v4 = (char *)malloc(v2 + 10LL);
*v4 = v2;
v4[1] = 1;
v4[2] = 8;
v5 = telos_dsn;
v4[3] = telos_dsn;
telos_dsn = v5 + 1;
v4[4] = -1;
v4[5] = -1;
v6 = *((_WORD *)a2 + 1);
v4[6] = v6 & 0xF;
v4[7] = HIBYTE(v6) & 0xF;
v4[8] = a2[4];
v4[9] = a2[5];
v7 = v4 + 10;
v8 = *a2;
v9 = (char *)*((_QWORD *)a2 + 1);
if ( (unsigned int)v8 >= 8 )
{
*(_QWORD *)(v4 + 10) = *(_QWORD *)v9;
*(_QWORD *)&v7[v8 - 8] = *(_QWORD *)&v9[v8 - 8];
qmemcpy(
(void *)((unsigned __int64)(v4 + 18) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v9 - &v7[-((unsigned __int64)(v4 + 18) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v8 + (_DWORD)v7 - (((_DWORD)v4 + 18) & 0xFFFFFFF8)) >> 3));
}
else if ( (v8 & 4) != 0 )
{
*(_DWORD *)(v4 + 10) = *(_DWORD *)v9;
*(_DWORD *)&v7[v8 - 4] = *(_DWORD *)&v9[v8 - 4];
}
else if ( (_DWORD)v8 )
{
v4[10] = *v9;
if ( (v8 & 2) != 0 )
*(_WORD *)&v7[v8 - 2] = *(_WORD *)&v9[v8 - 2];
}
puts("Sending data...");
v11 = 0LL;
do
printf("%X ", (unsigned __int8)v4[v11++]);
while ( *a2 + 9 >= (int)v11 );
puts("\nSend done.");
pthread_mutex_lock(&sfwritemutex);
v10 = write_sf_packet(a1, v4, (unsigned int)*a2 + 10);
pthread_mutex_unlock(&sfwritemutex);
free(v4);
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // bl
char *v4; // rbp
char v5; // al
__int16 v6; // ax
char *v7; // rcx
__int64 v8; // rax
char *v9; // rsi
unsigned int v10; // ebx
__int64 v11; // rbx

v2 = *a2;
if ( *a2 > 0x3Cu )
{
dbg(0LL, "packet too long! (%i bytes)\n", v2);
v10 = -1;
}
else
{
v4 = (char *)malloc(v2 + 10LL);
*v4 = v2;
v4[1] = 1;
v4[2] = 8;
v5 = telos_dsn;
v4[3] = telos_dsn;
telos_dsn = v5 + 1;
v4[4] = -1;
v4[5] = -1;
v6 = *((_WORD *)a2 + 1);
v4[6] = v6 & 0xF;
v4[7] = HIBYTE(v6) & 0xF;
v4[8] = a2[4];
v4[9] = a2[5];
v7 = v4 + 10;
v8 = *a2;
v9 = (char *)*((_QWORD *)a2 + 1);
if ( (unsigned int)v8 >= 8 )
{
*(_QWORD *)(v4 + 10) = *(_QWORD *)v9;
*(_QWORD *)&v7[v8 - 8] = *(_QWORD *)&v9[v8 - 8];
qmemcpy(
(void *)((unsigned __int64)(v4 + 18) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v9 - &v7[-((unsigned __int64)(v4 + 18) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v8 + (_DWORD)v7 - (((_DWORD)v4 + 18) & 0xFFFFFFF8)) >> 3));
}
else if ( (v8 & 4) != 0 )
{
*(_DWORD *)(v4 + 10) = *(_DWORD *)v9;
*(_DWORD *)&v7[v8 - 4] = *(_DWORD *)&v9[v8 - 4];
}
else if ( (_DWORD)v8 )
{
v4[10] = *v9;
if ( (v8 & 2) != 0 )
*(_WORD *)&v7[v8 - 2] = *(_WORD *)&v9[v8 - 2];
}
puts("Sending data...");
v11 = 0LL;
do
printf("%X ", (unsigned __int8)v4[v11++]);
while ( *a2 + 9 >= (int)v11 );
puts("\nSend done.");
pthread_mutex_lock(&sfwritemutex);
v10 = write_sf_packet(a1, v4, (unsigned int)*a2 + 10);
pthread_mutex_unlock(&sfwritemutex);
free(v4);
}
return v10;
}

original_code:
int write_telos_packet(int fd, const telospacket *packet)
{
  unsigned char l;
  uint8_t *buffer;
  int result;
  int i;
  if (packet->length > 60)
  {
   dbg(TSRC,"packet too long! (%i bytes)\n",packet->length);
   return -1;
  }
  buffer = malloc(packet->length + 10);
  buffer[0] = packet->length;
  buffer[1] = 0x01;
  buffer[2] = 0x08;
  buffer[3] = telos_dsn;
  telos_dsn++;
  buffer[4] = 0xff;
  buffer[5] = 0xff;
  buffer[6] = packet->addr & 0x0F;
  buffer[7] = (packet->addr >> 8) & 0x0F;
  buffer[8] = packet->type;
  buffer[9] = packet->group;
  memcpy(buffer+10, packet->data, packet->length);
  printf("Sending data...\n");
  for (i=0; i < packet->length + 10; i++)
    printf("%X ", buffer[i]);
  printf("\nSend done.\n");
  l = packet->length + 10;
  pthread_mutex_lock(&sfwritemutex);
  result = write_sf_packet(fd, (void*)buffer, packet->length+10);
  pthread_mutex_unlock(&sfwritemutex);
  free((void*)buffer);
  return result;
}

canonical_original_code:
int write_telos_packet(int fd, struct telospacket *packet)
{
  unsigned char l;
  unsigned char *buffer;
  int result;
  int i;
  if (packet->length > 60)
  {
   dbg(TSRC,"packet too long! (%i bytes)\n",packet->length);
   return -1;
  }
  buffer = malloc(packet->length + 10);
  buffer[0] = packet->length;
  buffer[1] = 0x01;
  buffer[2] = 0x08;
  buffer[3] = telos_dsn;
  telos_dsn++;
  buffer[4] = 0xff;
  buffer[5] = 0xff;
  buffer[6] = packet->addr & 0x0F;
  buffer[7] = (packet->addr >> 8) & 0x0F;
  buffer[8] = packet->type;
  buffer[9] = packet->group;
  memcpy(buffer+10, packet->data, packet->length);
  printf("Sending data...\n");
  for (i=0; i < packet->length + 10; i++)
    printf("%X ", buffer[i]);
  printf("\nSend done.\n");
  l = packet->length + 10;
  pthread_mutex_lock(&sfwritemutex);
  result = write_sf_packet(fd, (void *)buffer, packet->length+10);
  pthread_mutex_unlock(&sfwritemutex);
  free((void *)buffer);
  return result;
}

variable_types: struct telospacket { unsigned char length; unsigned char dsn; unsigned short addr; unsigned char type; unsigned char group; unsigned char *data; } *

variable_types:
l
unsigned char

variable_types:
buffer
unsigned char *

variable_types: i int


repo: emestee/bk-emulator

decompiled_code:
void __fastcall disk_open(unsigned int *a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er13
void *v5; // rax
char *v6; // rax
char *v7; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*((_BYTE *)a1 + 26) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*a1 = v3;
if ( v3 == -1 )
perror("seek");
v4 = *a1;
if ( (*a1 & 0x1FF) != 0 )
{
v6 = dcgettext(0LL, "%s is not an integer number of blocks: %d bytes\n", 5);
fprintf(stderr, v6, a2, v4);
close(v2);
}
else
{
v5 = mmap(0LL, v4, *((_BYTE *)a1 + 26) == 0 ? 3 : 1, 1, v2, 0LL);
*((_QWORD *)a1 + 1) = v5;
if ( v5 == (void *)-1LL )
{
*((_QWORD *)a1 + 1) = 0LL;
perror(a2);
}
if ( *((_BYTE *)a1 + 26) )
{
v7 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v7, a2);
}
}
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned int *a1, const char *a2)
{
int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // er13
void *v5; // rax
char *v6; // rax
char *v7; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*((_BYTE *)a1 + 26) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*a1 = v3;
if ( v3 == -1 )
perror("seek");
v4 = *a1;
if ( (*a1 & 0x1FF) != 0 )
{
v6 = dcgettext(0LL, "%s is not an integer number of blocks: %d bytes\n", 5);
fprintf(stderr, v6, a2, v4);
close(v2);
}
else
{
v5 = mmap(0LL, v4, *((_BYTE *)a1 + 26) == 0 ? 3 : 1, 1, v2, 0LL);
*((_QWORD *)a1 + 1) = v5;
if ( v5 == (void *)-1LL )
{
*((_QWORD *)a1 + 1) = 0LL;
perror(a2);
}
if ( *((_BYTE *)a1 + 26) )
{
v7 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v7, a2);
}
}
}
}

original_code:
void disk_open(disk_t * pdt, char * name) {
 int fd = open(name, 02|0);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00|0);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length == -1) perror("seek");
 if (pdt->length % 512) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks: %d bytes\n"), name, pdt->length);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

canonical_original_code:
void disk_open(struct disk_t *pdt, char *name) {
 int fd = open(name, 02|0);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00|0);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length == -1) perror("seek");
 if (pdt->length % 512) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks: %d bytes\n"), name, pdt->length);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

variable_types: struct disk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; unsigned char side; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char need_sidetrk; unsigned char need_sectsize; unsigned char cursec; } *

variable_types:
name
char *


decompiled_code:
__int64 disk_init()
{
__int64 *v0; // rax

if ( !init_done_1 )
{
disk_open((unsigned int *)disks, floppyA);
disk_open((unsigned int *)(unsigned int)((_DWORD)disks + 40), floppyB);
disk_open((unsigned int *)(unsigned int)((_DWORD)disks + 80), floppyC);
disk_open((unsigned int *)(unsigned int)((_DWORD)disks + 120), floppyD);
init_done_1 = 1;
}
v0 = disks;
do
{
v0[2] = 0LL;
*((_BYTE *)v0 + 28) = 0;
*((_BYTE *)v0 + 27) = 0;
*((_BYTE *)v0 + 25) = 0;
*((_BYTE *)v0 + 24) = 0;
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)disks + 160) );
selected = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 *v0; // rax

if ( !init_done_1 )
{
func0((unsigned int *)disks, floppyA);
func0((unsigned int *)(unsigned int)((_DWORD)disks + 40), floppyB);
func0((unsigned int *)(unsigned int)((_DWORD)disks + 80), floppyC);
func0((unsigned int *)(unsigned int)((_DWORD)disks + 120), floppyD);
init_done_1 = 1;
}
v0 = disks;
do
{
v0[2] = 0LL;
*((_BYTE *)v0 + 28) = 0;
*((_BYTE *)v0 + 27) = 0;
*((_BYTE *)v0 + 25) = 0;
*((_BYTE *)v0 + 24) = 0;
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)disks + 160) );
selected = -1;
return 0LL;
}

original_code:
int disk_init() {
 static char init_done = 0;
 int i;
 if (!init_done) {
  disk_open(&disks[0], floppyA);
  disk_open(&disks[1], floppyB);
  disk_open(&disks[2], floppyC);
  disk_open(&disks[3], floppyD);
  init_done = 1;
 }
 for (i = 0; i < 4; i++) {
  disks[i].ptr = ((void *)0);
  disks[i].track = disks[i].side =
  disks[i].motor = disks[i].inprogress = 0;
 }
 selected = -1;
 return 0;
}


decompiled_code:
int disk_finish()
{
__int64 *v0; // rbx
int result; // eax
void *v2; // rdi

v0 = disks;
do
{
v2 = (void *)v0[1];
if ( v2 )
result = munmap(v2, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)disks + 160) );
return result;
}

canonical_decompiled_code:
int func2()
{
__int64 *v0; // rbx
int result; // eax
void *v2; // rdi

v0 = disks;
do
{
v2 = (void *)v0[1];
if ( v2 )
result = munmap(v2, *(unsigned int *)v0);
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)disks + 160) );
return result;
}

original_code:
void disk_finish() {
 int i;
 for (i = 0; i < 4; i++) {
  if (!disks[i].image)
   continue;
  munmap(disks[i].image, disks[i].length);
 }
}


decompiled_code:
_BOOL8 index_flag()
{
return 100 * ((int)(ticks / (double)(TICK_RATE / 1000)) / 0x64u) == (int)(ticks / (double)(TICK_RATE / 1000));
}

canonical_decompiled_code:
_BOOL8 func3()
{
return 100 * ((int)(ticks / (double)(TICK_RATE / 1000)) / 0x64u) == (int)(ticks / (double)(TICK_RATE / 1000));
}

original_code:
int index_flag() {
 extern double ticks;
 unsigned msec = ticks / (TICK_RATE/1000);
 return (msec % 100 == 0);
}

canonical_original_code:
int index_flag() {
 extern double ticks;
 unsigned int msec = ticks / (TICK_RATE/1000);
 return (msec % 100 == 0);
}

variable_types: ticks double

variable_types:
msec
unsigned int


original_code:
unsigned short
disk_read_word(disk_t * pdt) {
 unsigned short ret;
 if (pdt->need_sidetrk) {
  ret = (pdt->side << 8) | pdt->track;
  pdt->need_sidetrk = 0;
  pdt->need_sectsize = 1;
 } else if (pdt->need_sectsize) {
  ret = ((pdt->cursec+1) << 8) | 2;
  pdt->need_sectsize = 0;
  pdt->ptr = data_marker;
 } else {
  ret = *pdt->ptr++;
  if (pdt->ptr == index_marker + (sizeof(index_marker)/sizeof(*index_marker))) {
   pdt->need_sidetrk = 1;
  } else if (pdt->ptr == data_marker + (sizeof(data_marker)/sizeof(*data_marker))) {
   pdt->ptr = pdt->image +
   (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec) * 512/2;
  } else if (pdt->ptr ==
   pdt->image + (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec+1) * 512/2) {
   pdt->ptr = end_marker;
  } else if (pdt->ptr == end_marker + (sizeof(end_marker)/sizeof(*end_marker))) {
   if (++pdt->cursec == 10) pdt->inprogress = 0;
   pdt->ptr = index_marker;
  }
 }
 return ret;
}

canonical_original_code:
unsigned short disk_read_word(struct disk_t *pdt) {
 unsigned short ret;
 if (pdt->need_sidetrk) {
  ret = (pdt->side << 8) | pdt->track;
  pdt->need_sidetrk = 0;
  pdt->need_sectsize = 1;
 } else if (pdt->need_sectsize) {
  ret = ((pdt->cursec+1) << 8) | 2;
  pdt->need_sectsize = 0;
  pdt->ptr = data_marker;
 } else {
  ret = *pdt->ptr++;
  if (pdt->ptr == index_marker + (sizeof(index_marker)/sizeof(*index_marker))) {
   pdt->need_sidetrk = 1;
  } else if (pdt->ptr == data_marker + (sizeof(data_marker)/sizeof(*data_marker))) {
   pdt->ptr = pdt->image +
   (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec) * 512/2;
  } else if (pdt->ptr ==
   pdt->image + (((pdt->track * 2 + pdt->side) * 10) + pdt->cursec+1) * 512/2) {
   pdt->ptr = end_marker;
  } else if (pdt->ptr == end_marker + (sizeof(end_marker)/sizeof(*end_marker))) {
   if (++pdt->cursec == 10) pdt->inprogress = 0;
   pdt->ptr = index_marker;
  }
 }
 return ret;
}

variable_types:
ret
unsigned short

return_type: unsigned short

user_defined_types: struct disk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; unsigned char side; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char need_sidetrk; unsigned char need_sectsize; unsigned char cursec; }


decompiled_code:
__int64 __fastcall disk_write(__int16 a1, unsigned __int16 a2)
{
char v2; // bh
__int16 v3; // di
char *v5; // rax
unsigned int v6; // esi
char v7; // bl
const char *v8; // rcx
char *v9; // rax

v2 = HIBYTE(a2);
v3 = a1 + 424;
if ( v3 )
{
if ( v3 == 2 )
{
v9 = dcgettext(0LL, "Writing 177132, data %06o\n", 5);
fprintf(stderr, v9, a2);
}
}
else if ( a2 )
{
v5 = dcgettext(0LL, "Writing 177130, data %06o\n", 5);
fprintf(stderr, v5, a2);
switch ( a2 & 0xF )
{
case 0:
goto LABEL_6;
case 2:
case 6:
case 0xA:
case 0xE:
selected = 1;
break;
case 4:
case 0xC:
selected = 2;
break;
case 8:
selected = 3;
break;
default:
selected = 0;
break;
}
v6 = selected;
v7 = BYTE4(disks[5 * selected + 3]) | v2 & 1;
BYTE4(disks[5 * selected + 3]) = v7;
v8 = "ON";
if ( !v7 )
v8 = "OFF";
fprintf(stderr, "Drive %d i/o %s\n", v6, v8);
}
else
{
LABEL_6:
selected = -1;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, unsigned __int16 a2)
{
char v2; // bh
__int16 v3; // di
char *v5; // rax
unsigned int v6; // esi
char v7; // bl
const char *v8; // rcx
char *v9; // rax

v2 = HIBYTE(a2);
v3 = a1 + 424;
if ( v3 )
{
if ( v3 == 2 )
{
v9 = dcgettext(0LL, "Writing 177132, data %06o\n", 5);
fprintf(stderr, v9, a2);
}
}
else if ( a2 )
{
v5 = dcgettext(0LL, "Writing 177130, data %06o\n", 5);
fprintf(stderr, v5, a2);
switch ( a2 & 0xF )
{
case 0:
goto LABEL_6;
case 2:
case 6:
case 0xA:
case 0xE:
selected = 1;
break;
case 4:
case 0xC:
selected = 2;
break;
case 8:
selected = 3;
break;
default:
selected = 0;
break;
}
v6 = selected;
v7 = BYTE4(disks[5 * selected + 3]) | v2 & 1;
BYTE4(disks[5 * selected + 3]) = v7;
v8 = "ON";
if ( !v7 )
v8 = "OFF";
fprintf(stderr, "Drive %d i/o %s\n", v6, v8);
}
else
{
LABEL_6:
selected = -1;
}
return 0LL;
}

original_code:
int
disk_write(c_addr addr, d_word word) {
 d_word offset = addr - 0177130;
 disk_t * pdt;
 switch (offset) {
 case 0:
  if (word) {
   fprintf(stderr, gettext ("Writing 177130, data %06o\n"), word);
  }
  switch (word & 0xf) {
  case 0: selected = -1; break;
  case 1: default: selected = 0; break;
  case 2: case 6: case 10: case 14: selected = 1; break;
  case 4: case 12: selected = 2; break;
  case 8: selected = 3; break;
  }
  if (selected >= 0) {
   pdt = &disks[selected];
   pdt->inprogress |= !! (word & 0400);
   fprintf(stderr, "Drive %d i/o %s\n", selected,
    pdt->inprogress ? "ON" : "OFF");
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing 177132, data %06o\n"), word);
  break;
 }
 return 0;
}

canonical_original_code:
int disk_write(unsigned long addr, unsigned short word) {
 unsigned short offset = addr - 0177130;
 struct disk_t *pdt;
 switch (offset) {
 case 0:
  if (word) {
   fprintf(stderr, gettext ("Writing 177130, data %06o\n"), word);
  }
  switch (word & 0xf) {
  case 0: selected = -1; break;
  case 1: default: selected = 0; break;
  case 2: case 6: case 10: case 14: selected = 1; break;
  case 4: case 12: selected = 2; break;
  case 8: selected = 3; break;
  }
  if (selected >= 0) {
   pdt = &disks[selected];
   pdt->inprogress |= !! (word & 0400);
   fprintf(stderr, "Drive %d i/o %s\n", selected,
    pdt->inprogress ? "ON" : "OFF");
  }
  break;
 case 2:
  fprintf(stderr, gettext ("Writing 177132, data %06o\n"), word);
  break;
 }
 return 0;
}

variable_types:
addr
unsigned long

variable_types:
word
unsigned short

variable_types:
offset
unsigned short


decompiled_code:
__int64 __fastcall disk_bwrite(__int16 a1, unsigned __int8 a2)
{
char *v2; // rax
char *v3; // rax
char *v5; // rax

if ( a1 == -422 )
{
v5 = dcgettext(0LL, "Writing byte 177132, data %03o\n", 5);
fprintf(stderr, v5, a2);
}
else if ( (unsigned __int16)(a1 + 424) > 2u )
{
if ( a1 == -421 )
{
v3 = dcgettext(0LL, "Writing byte 177133, data %03o\n", 5);
fprintf(stderr, v3, a2);
}
}
else
{
if ( a1 == -424 )
v2 = dcgettext(0LL, "Writing byte 177130, data %03o\n", 5);
else
v2 = dcgettext(0LL, "Writing byte 177131, data %03o\n", 5);
fprintf(stderr, v2, a2);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1, unsigned __int8 a2)
{
char *v2; // rax
char *v3; // rax
char *v5; // rax

if ( a1 == -422 )
{
v5 = dcgettext(0LL, "Writing byte 177132, data %03o\n", 5);
fprintf(stderr, v5, a2);
}
else if ( (unsigned __int16)(a1 + 424) > 2u )
{
if ( a1 == -421 )
{
v3 = dcgettext(0LL, "Writing byte 177133, data %03o\n", 5);
fprintf(stderr, v3, a2);
}
}
else
{
if ( a1 == -424 )
v2 = dcgettext(0LL, "Writing byte 177130, data %03o\n", 5);
else
v2 = dcgettext(0LL, "Writing byte 177131, data %03o\n", 5);
fprintf(stderr, v2, a2);
}
return 0LL;
}

original_code:
int
disk_bwrite(c_addr addr, d_byte byte) {
 d_word offset = addr - 0177130;
 switch (offset) {
 case 0:
  fprintf(stderr, gettext ("Writing byte 177130, data %03o\n"), byte);
  break;
 case 1:
  fprintf(stderr, gettext ("Writing byte 177131, data %03o\n"), byte);
  break;
 case 2:
  fprintf(stderr, gettext ("Writing byte 177132, data %03o\n"), byte);
  break;
 case 3:
  fprintf(stderr, gettext ("Writing byte 177133, data %03o\n"), byte);
  break;
 }
 return 0;
}

canonical_original_code:
int disk_bwrite(unsigned long addr, unsigned char byte) {
 unsigned short offset = addr - 0177130;
 switch (offset) {
 case 0:
  fprintf(stderr, gettext ("Writing byte 177130, data %03o\n"), byte);
  break;
 case 1:
  fprintf(stderr, gettext ("Writing byte 177131, data %03o\n"), byte);
  break;
 case 2:
  fprintf(stderr, gettext ("Writing byte 177132, data %03o\n"), byte);
  break;
 case 3:
  fprintf(stderr, gettext ("Writing byte 177133, data %03o\n"), byte);
  break;
 }
 return 0;
}

variable_types:
byte
unsigned char


decompiled_code:
int __fastcall do_disk_io(unsigned int a1, unsigned int a2, int a3, unsigned int a4)
{
__int64 v5; // rbx
unsigned int v6; // er15
char *v7; // r14
char *v8; // rax
unsigned int v9; // er14
__int64 v10; // r12
int v11; // er15
int v12; // er14
char *v13; // rax
char *v14; // rax
char *v15; // rax
char *v17; // rax
int v18; // ebp
__int64 v19; // r14
int v20; // ebx
unsigned int v21; // er15
char *v22; // rax
char *v23; // rax
int v24; // [rsp+Ch] [rbp-4Ch]
__int16 v25[29]; // [rsp+1Eh] [rbp-3Ah] BYREF

v5 = a4;
if ( (a3 & 0x8000) == 0 )
{
v7 = dcgettext(0LL, "Reading", 5);
v6 = a3;
}
else
{
v6 = (unsigned __int16)-(__int16)a3;
v7 = dcgettext(0LL, "Writing", 5);
}
v8 = dcgettext(0LL, "%s block %d (%d words) from drive %d @ addr %06o\n", 5);
fprintf(stderr, v8, v7, a2, v6, a1, v5);
pdp[16] |= 1u;
sl_byte(pdp, 42LL, 0LL);
if ( disks[5 * (int)a1 + 1] )
{
v9 = disks[5 * (int)a1];
if ( v9 >> 9 <= a2 )
{
v14 = dcgettext(0LL, "Block number %d too large for image size %d\n", 5);
fprintf(stderr, v14, a2, v9);
sl_byte(pdp, 42LL, 5LL);
}
else if ( a3 > 0x7FFF )
{
if ( BYTE2(disks[5 * (int)a1 + 3]) )
{
v23 = dcgettext(0LL, "Disk is read-only\n", 5);
fprintf(stderr, v23);
sl_byte(pdp, 42LL, 1LL);
}
else
{
v18 = (unsigned __int16)-(__int16)a3;
if ( v18 )
{
v19 = 2LL * (int)(a2 << 8);
v24 = v5 - (a2 << 9);
v20 = 0;
while ( 1 )
{
v21 = v24 + v19;
if ( LODWORD(disks[5 * (int)a1]) >> 1 <= (a2 << 8) + v20 )
break;
if ( (unsigned int)lc_word(v21, v25) )
{
v22 = dcgettext(0LL, "Write failure @ %06o\n", 5);
fprintf(stderr, v22, v21);
sl_byte(pdp, 42LL, 7LL);
break;
}
*(_WORD *)(disks[5 * (int)a1 + 1] + v19) = v25[0];
++v20;
v19 += 2LL;
if ( v18 == v20 )
goto LABEL_25;
}
}
else
{
v20 = 0;
}
if ( v18 == v20 )
LABEL_25:
pdp[16] &= 0xFEu;
}
}
else
{
if ( a3 <= 0 )
{
v12 = 0;
}
else
{
v10 = 2LL * (int)(a2 << 8);
v11 = v5 + 2 * a3;
v12 = 0;
while ( LODWORD(disks[5 * (int)a1]) >> 1 > (a2 << 8) + v12 )
{
if ( (unsigned int)sc_word((unsigned int)v5, *(unsigned __int16 *)(disks[5 * (int)a1 + 1] + v10)) )
{
v17 = dcgettext(0LL, "Read failure @ %06o\n", 5);
fprintf(stderr, v17, (unsigned int)v5);
sl_byte(pdp, 42LL, 7LL);
break;
}
++v12;
LODWORD(v5) = v5 + 2;
v10 += 2LL;
if ( (_DWORD)v5 == v11 )
break;
}
}
if ( a3 == v12 )
pdp[16] &= 0xFEu;
}
}
else
{
v13 = dcgettext(0LL, "Disk not ready\n", 5);
fprintf(stderr, v13);
sl_byte(pdp, 42LL, 6LL);
}
ticks = (double)(TICK_RATE / 100) + ticks;
pop(pdp, (unsigned int)((_DWORD)pdp + 14));
v15 = dcgettext(0LL, "Done\n", 5);
return fprintf(stderr, v15);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, unsigned int a2, int a3, unsigned int a4)
{
__int64 v5; // rbx
unsigned int v6; // er15
char *v7; // r14
char *v8; // rax
unsigned int v9; // er14
__int64 v10; // r12
int v11; // er15
int v12; // er14
char *v13; // rax
char *v14; // rax
char *v15; // rax
char *v17; // rax
int v18; // ebp
__int64 v19; // r14
int v20; // ebx
unsigned int v21; // er15
char *v22; // rax
char *v23; // rax
int v24; // [rsp+Ch] [rbp-4Ch]
__int16 v25[29]; // [rsp+1Eh] [rbp-3Ah] BYREF

v5 = a4;
if ( (a3 & 0x8000) == 0 )
{
v7 = dcgettext(0LL, "Reading", 5);
v6 = a3;
}
else
{
v6 = (unsigned __int16)-(__int16)a3;
v7 = dcgettext(0LL, "Writing", 5);
}
v8 = dcgettext(0LL, "%s block %d (%d words) from drive %d @ addr %06o\n", 5);
fprintf(stderr, v8, v7, a2, v6, a1, v5);
pdp[16] |= 1u;
sl_byte(pdp, 42LL, 0LL);
if ( disks[5 * (int)a1 + 1] )
{
v9 = disks[5 * (int)a1];
if ( v9 >> 9 <= a2 )
{
v14 = dcgettext(0LL, "Block number %d too large for image size %d\n", 5);
fprintf(stderr, v14, a2, v9);
sl_byte(pdp, 42LL, 5LL);
}
else if ( a3 > 0x7FFF )
{
if ( BYTE2(disks[5 * (int)a1 + 3]) )
{
v23 = dcgettext(0LL, "Disk is read-only\n", 5);
fprintf(stderr, v23);
sl_byte(pdp, 42LL, 1LL);
}
else
{
v18 = (unsigned __int16)-(__int16)a3;
if ( v18 )
{
v19 = 2LL * (int)(a2 << 8);
v24 = v5 - (a2 << 9);
v20 = 0;
while ( 1 )
{
v21 = v24 + v19;
if ( LODWORD(disks[5 * (int)a1]) >> 1 <= (a2 << 8) + v20 )
break;
if ( (unsigned int)lc_word(v21, v25) )
{
v22 = dcgettext(0LL, "Write failure @ %06o\n", 5);
fprintf(stderr, v22, v21);
sl_byte(pdp, 42LL, 7LL);
break;
}
*(_WORD *)(disks[5 * (int)a1 + 1] + v19) = v25[0];
++v20;
v19 += 2LL;
if ( v18 == v20 )
goto LABEL_25;
}
}
else
{
v20 = 0;
}
if ( v18 == v20 )
LABEL_25:
pdp[16] &= 0xFEu;
}
}
else
{
if ( a3 <= 0 )
{
v12 = 0;
}
else
{
v10 = 2LL * (int)(a2 << 8);
v11 = v5 + 2 * a3;
v12 = 0;
while ( LODWORD(disks[5 * (int)a1]) >> 1 > (a2 << 8) + v12 )
{
if ( (unsigned int)sc_word((unsigned int)v5, *(unsigned __int16 *)(disks[5 * (int)a1 + 1] + v10)) )
{
v17 = dcgettext(0LL, "Read failure @ %06o\n", 5);
fprintf(stderr, v17, (unsigned int)v5);
sl_byte(pdp, 42LL, 7LL);
break;
}
++v12;
LODWORD(v5) = v5 + 2;
v10 += 2LL;
if ( (_DWORD)v5 == v11 )
break;
}
}
if ( a3 == v12 )
pdp[16] &= 0xFEu;
}
}
else
{
v13 = dcgettext(0LL, "Disk not ready\n", 5);
fprintf(stderr, v13);
sl_byte(pdp, 42LL, 6LL);
}
ticks = (double)(TICK_RATE / 100) + ticks;
pop(pdp, (unsigned int)((_DWORD)pdp + 14));
v15 = dcgettext(0LL, "Done\n", 5);
return fprintf(stderr, v15);
}

original_code:
void do_disk_io(int drive, int blkno, int nwords, int ioaddr) {
 fprintf(stderr, gettext ("%s block %d (%d words) from drive %d @ addr %06o\n"),
  nwords & 0100000 ? gettext ("Writing") : gettext ("Reading"), blkno,
  nwords & 0100000 ? -nwords & 0xffff : nwords, drive, ioaddr);
 pdp.psw |= 01;
 sl_byte(&pdp, 052, 0);
 if (disks[drive].image == 0) {
  fprintf(stderr, gettext ("Disk not ready\n"));
  sl_byte(&pdp, 052, 6);
 } else if (blkno >= disks[drive].length / 512) {
  fprintf(stderr, gettext ("Block number %d too large for image size %d\n"),
   blkno, disks[drive].length);
  sl_byte(&pdp, 052, 5);
 } else if (nwords < 0100000) {
  int i;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   if (0 != sc_word(ioaddr, disks[drive].image[256 * blkno + i])) {
    fprintf(stderr, gettext ("Read failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
    break;
   }
  }
  if (i == nwords) pdp.psw &= ~01;
 } else if (!disks[drive].ro) {
  int i;
  nwords = -nwords & 0xffff;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   d_word word;
                        if (0 != lc_word(ioaddr, &word)) {
                                fprintf(stderr, gettext ("Write failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
                                break;
                        }
   disks[drive].image[256 * blkno + i] = word;
                }
                if (i == nwords) pdp.psw &= ~01;
 } else {
  fprintf(stderr, gettext ("Disk is read-only\n"));
  sl_byte(&pdp, 052, 1);
 }
 ticks += TICK_RATE/100;
        pop( &pdp, &pdp.regs[7] );
 fprintf(stderr, gettext ("Done\n"));
}

canonical_original_code:
void do_disk_io(int drive, int blkno, int nwords, int ioaddr) {
 fprintf(stderr, gettext ("%s block %d (%d words) from drive %d @ addr %06o\n"),
  nwords & 0100000 ? gettext ("Writing") : gettext ("Reading"), blkno,
  nwords & 0100000 ? -nwords & 0xffff : nwords, drive, ioaddr);
 pdp.psw |= 01;
 sl_byte(&pdp, 052, 0);
 if (disks[drive].image == 0) {
  fprintf(stderr, gettext ("Disk not ready\n"));
  sl_byte(&pdp, 052, 6);
 } else if (blkno >= disks[drive].length / 512) {
  fprintf(stderr, gettext ("Block number %d too large for image size %d\n"),
   blkno, disks[drive].length);
  sl_byte(&pdp, 052, 5);
 } else if (nwords < 0100000) {
  int i;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   if (0 != sc_word(ioaddr, disks[drive].image[256 * blkno + i])) {
    fprintf(stderr, gettext ("Read failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
    break;
   }
  }
  if (i == nwords) pdp.psw &= ~01;
 } else if (!disks[drive].ro) {
  int i;
  nwords = -nwords & 0xffff;
  for (i = 0;
  i < nwords && 256 * blkno + i < disks[drive].length/2;
  i++, ioaddr += 2) {
   unsigned short word;
                        if (0 != lc_word(ioaddr, &word)) {
                                fprintf(stderr, gettext ("Write failure @ %06o\n"), ioaddr);
    sl_byte(&pdp, 052, 7);
                                break;
                        }
   disks[drive].image[256 * blkno + i] = word;
                }
                if (i == nwords) pdp.psw &= ~01;
 } else {
  fprintf(stderr, gettext ("Disk is read-only\n"));
  sl_byte(&pdp, 052, 1);
 }
 ticks += TICK_RATE/100;
        pop( &pdp, &pdp.regs[7] );
 fprintf(stderr, gettext ("Done\n"));
}

variable_types: drive int

variable_types: blkno int

variable_types: nwords int

variable_types: ioaddr int


decompiled_code:
int fake_disk_io()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
unsigned __int16 v2; // r12
__int16 v4[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(_WORD *)pdp;
v1 = *(_WORD *)&pdp[2];
v2 = *(_WORD *)&pdp[4];
lc_word(*(unsigned __int16 *)&pdp[6] + 28LL, v4);
return do_disk_io(v4[0] & 3, v0, v1, v2);
}

canonical_decompiled_code:
int func9()
{
unsigned __int16 v0; // bx
unsigned __int16 v1; // bp
unsigned __int16 v2; // r12
__int16 v4[13]; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(_WORD *)pdp;
v1 = *(_WORD *)&pdp[2];
v2 = *(_WORD *)&pdp[4];
lc_word(*(unsigned __int16 *)&pdp[6] + 28LL, v4);
return func8(v4[0] & 3, v0, v1, v2);
}

original_code:
void fake_disk_io() {
 d_word blkno = pdp.regs[0];
 d_word nwords = pdp.regs[1];
 c_addr ioaddr = pdp.regs[2];
 c_addr parms = pdp.regs[3];
 d_word drive;
 lc_word(parms+034, &drive);
 drive &= 3;
 do_disk_io(drive, blkno, nwords, ioaddr);
}

canonical_original_code:
void fake_disk_io() {
 unsigned short blkno = pdp.regs[0];
 unsigned short nwords = pdp.regs[1];
 unsigned long ioaddr = pdp.regs[2];
 unsigned long parms = pdp.regs[3];
 unsigned short drive;
 lc_word(parms+034, &drive);
 drive &= 3;
 do_disk_io(drive, blkno, nwords, ioaddr);
}

variable_types:
blkno
unsigned short

variable_types:
nwords
unsigned short

variable_types:
ioaddr
unsigned long

variable_types:
parms
unsigned long

variable_types:
drive
unsigned short


decompiled_code:
int fake_sector_io()
{
__int64 v0; // rbx
unsigned __int8 v1; // bp^1
__int16 v3; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v5; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int16 v6; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(unsigned __int16 *)&pdp[6];
lc_word(v0 + 28, &v6);
lc_word(v0 + 22, &v4);
lc_word(v0 + 24, &v5);
v1 = HIBYTE(v6);
v6 &= 3u;
lc_word(v0 + 26, &v3);
return do_disk_io(v6, v1 + 10 * ((v3 & 1) + 2 * HIBYTE(v3)) - 1, v5, v4);
}

canonical_decompiled_code:
int func10()
{
__int64 v0; // rbx
unsigned __int8 v1; // bp^1
__int16 v3; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v4; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v5; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int16 v6; // [rsp+Eh] [rbp-1Ah] BYREF

v0 = *(unsigned __int16 *)&pdp[6];
lc_word(v0 + 28, &v6);
lc_word(v0 + 22, &v4);
lc_word(v0 + 24, &v5);
v1 = HIBYTE(v6);
v6 &= 3u;
lc_word(v0 + 26, &v3);
return func8(v6, v1 + 10 * ((v3 & 1) + 2 * HIBYTE(v3)) - 1, v5, v4);
}

original_code:
void fake_sector_io() {
 c_addr parms = pdp.regs[3];
 d_word drive;
 d_word nwords, ioaddr, side, trk, sector;
 int blkno;
        lc_word(parms+034, &drive);
 lc_word(parms+026, &ioaddr);
 lc_word(parms+030, &nwords);
 sector = drive >> 8;
 drive &= 3;
 lc_word(parms+032, &side);
 trk = side >> 8;
 side &= 1;
 blkno = sector-1 + 10 * (side + 2 * trk);
 do_disk_io(drive, blkno, nwords, ioaddr);
}

canonical_original_code:
void fake_sector_io() {
 unsigned long parms = pdp.regs[3];
 unsigned short drive;
 unsigned short nwords; unsigned short ioaddr; unsigned short side; unsigned short trk; unsigned short sector;
 int blkno;
        lc_word(parms+034, &drive);
 lc_word(parms+026, &ioaddr);
 lc_word(parms+030, &nwords);
 sector = drive >> 8;
 drive &= 3;
 lc_word(parms+032, &side);
 trk = side >> 8;
 side &= 1;
 blkno = sector-1 + 10 * (side + 2 * trk);
 do_disk_io(drive, blkno, nwords, ioaddr);
}

variable_types:
ioaddr
unsigned short

variable_types:
side
unsigned short

variable_types:
trk
unsigned short

variable_types:
sector
unsigned short


decompiled_code:
void tape_init()
{
char *v0; // rax

if ( tape_read_file )
{
if ( fake_tape )
{
fclose(tape_read_file);
fake_state = 0;
}
else
{
pclose(tape_read_file);
}
tape_read_file = 0LL;
}
if ( fake_tape )
{
if ( tape_write_file )
{
fclose(tape_write_file);
tape_write_file = 0LL;
}
}
else if ( !tape_write_file )
{
tape_write_file = popen("readtape", "w");
if ( tape_write_file )
{
v0 = dcgettext(0LL, "readtape open successful\n", 5);
fprintf(stderr, v0);
}
else
{
perror("readtape");
}
}
}

canonical_decompiled_code:
void func0()
{
char *v0; // rax

if ( tape_read_file )
{
if ( fake_tape )
{
fclose(tape_read_file);
fake_state = 0;
}
else
{
pclose(tape_read_file);
}
tape_read_file = 0LL;
}
if ( fake_tape )
{
if ( tape_write_file )
{
fclose(tape_write_file);
tape_write_file = 0LL;
}
}
else if ( !tape_write_file )
{
tape_write_file = popen("readtape", "w");
if ( tape_write_file )
{
v0 = dcgettext(0LL, "readtape open successful\n", 5);
fprintf(stderr, v0);
}
else
{
perror("readtape");
}
}
}

original_code:
void tape_init() {
 if (tape_read_file) {
     if (fake_tape) {
  fclose(tape_read_file);
  fake_state = Idle;
     } else {
  pclose(tape_read_file);
     }
     tape_read_file = ((void *)0);
 }
 if (fake_tape) {
     if (tape_write_file) {
  fclose(tape_write_file);
  tape_write_file = 0;
     }
 } else if (tape_write_file == ((void *)0)) {
  tape_write_file = popen("readtape", "w");
  if (tape_write_file) {
   fprintf(stderr, gettext ("readtape open successful\n"));
  } else perror("readtape");
 }
}


decompiled_code:
__int64 tape_read()
{
FILE *v0; // rdi
__int64 result; // rax
unsigned int v2; // ebx
int v3; // ebp
int v4; // ebx
char *v5; // rax

if ( fake_tape )
{
tape_read_val = (int)(ticks / 1001.0) & 1;
result = (int)(ticks / 1001.0) & 1;
}
else if ( tape_status || (v0 = tape_read_file) == 0LL )
{
tape_read_val = tape_read_val == 0;
result = (unsigned __int8)tape_read_val;
}
else
{
do
{
if ( ticks <= *(double *)&tape_read_ticks )
break;
v3 = fgetc(v0);
v4 = fgetc(tape_read_file);
if ( v4 == -1 )
{
v5 = dcgettext(0LL, "End of tape\n", 5);
fprintf(stderr, v5);
pclose(tape_read_file);
tape_read_file = 0LL;
}
v2 = (v3 << 8) | v4;
tape_read_val = v2 >> 15;
*(double *)&tape_read_ticks = (double)(v2 & 0x7FFF) + *(double *)&tape_read_ticks;
v0 = tape_read_file;
}
while ( tape_read_file );
result = (unsigned __int8)tape_read_val;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
FILE *v0; // rdi
__int64 result; // rax
unsigned int v2; // ebx
int v3; // ebp
int v4; // ebx
char *v5; // rax

if ( fake_tape )
{
tape_read_val = (int)(ticks / 1001.0) & 1;
result = (int)(ticks / 1001.0) & 1;
}
else if ( tape_status || (v0 = tape_read_file) == 0LL )
{
tape_read_val = tape_read_val == 0;
result = (unsigned __int8)tape_read_val;
}
else
{
do
{
if ( ticks <= *(double *)&tape_read_ticks )
break;
v3 = fgetc(v0);
v4 = fgetc(tape_read_file);
if ( v4 == -1 )
{
v5 = dcgettext(0LL, "End of tape\n", 5);
fprintf(stderr, v5);
pclose(tape_read_file);
tape_read_file = 0LL;
}
v2 = (v3 << 8) | v4;
tape_read_val = v2 >> 15;
*(double *)&tape_read_ticks = (double)(v2 & 0x7FFF) + *(double *)&tape_read_ticks;
v0 = tape_read_file;
}
while ( tape_read_file );
result = (unsigned __int8)tape_read_val;
}
return result;
}

original_code:
int
tape_read() {
    unsigned delta;
    if (fake_tape) {
 tape_read_val = ((unsigned) (ticks / 1001.0)) & 1;
 return tape_read_val;
    }
    if (tape_status || tape_read_file == ((void *)0)) {
 return tape_read_val = !tape_read_val;
    }
    while (tape_read_file && ticks > tape_read_ticks) {
 int c1 = fgetc(tape_read_file);
 int c2 = fgetc(tape_read_file);
 if (c2 == (-1)) {
  fprintf(stderr, gettext ("End of tape\n"));
  pclose(tape_read_file);
  tape_read_file = 0;
 }
 delta = c1 << 8;
 delta |= c2;
 tape_read_val = delta >> 15;
 delta &= 0x7FFF;
 tape_read_ticks += delta;
    }
    return tape_read_val;
}

canonical_original_code:
int tape_read() {
    unsigned int delta;
    if (fake_tape) {
 tape_read_val = ((unsigned int) (ticks / 1001.0)) & 1;
 return tape_read_val;
    }
    if (tape_status || tape_read_file == ((void *)0)) {
 return tape_read_val = !tape_read_val;
    }
    while (tape_read_file && ticks > tape_read_ticks) {
 int c1 = fgetc(tape_read_file);
 int c2 = fgetc(tape_read_file);
 if (c2 == (-1)) {
  fprintf(stderr, gettext ("End of tape\n"));
  pclose(tape_read_file);
  tape_read_file = 0;
 }
 delta = c1 << 8;
 delta |= c2;
 tape_read_val = delta >> 15;
 delta &= 0x7FFF;
 tape_read_ticks += delta;
    }
    return tape_read_val;
}

variable_types:
delta
unsigned int


decompiled_code:
__int64 get_emt36_filename()
{
__int64 i; // rbx
char v1; // ah
__int64 v2; // rdx
__int64 result; // rax
__int16 v4; // [rsp+Ch] [rbp-Ch] BYREF
unsigned __int16 v5[5]; // [rsp+Eh] [rbp-Ah] BYREF

lc_word(198LL, v5);
for ( i = 0LL; i != 16; i += 2LL )
{
lc_word(v5[0] + (unsigned int)i + 6, &v4);
v1 = HIBYTE(v4);
bk_filename[i] = v4;
byte_BB1[i] = v1;
}
byte_BC0 = 0;
v2 = 15LL;
while ( 1 )
{
result = (unsigned int)v2;
if ( bk_filename[v2] != 32 )
break;
unix_filename[v2--] = 0;
if ( v2 == -1 )
return result;
}
if ( (int)v2 >= 0 )
{
result = (int)v2;
do
{
unix_filename[result] = bk_filename[result];
--result;
}
while ( (int)result >= 0 );
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 i; // rbx
char v1; // ah
__int64 v2; // rdx
__int64 result; // rax
__int16 v4; // [rsp+Ch] [rbp-Ch] BYREF
unsigned __int16 v5[5]; // [rsp+Eh] [rbp-Ah] BYREF

lc_word(198LL, v5);
for ( i = 0LL; i != 16; i += 2LL )
{
lc_word(v5[0] + (unsigned int)i + 6, &v4);
v1 = HIBYTE(v4);
bk_filename[i] = v4;
byte_BB1[i] = v1;
}
byte_BC0 = 0;
v2 = 15LL;
while ( 1 )
{
result = (unsigned int)v2;
if ( bk_filename[v2] != 32 )
break;
unix_filename[v2--] = 0;
if ( v2 == -1 )
return result;
}
if ( (int)v2 >= 0 )
{
result = (int)v2;
do
{
unix_filename[result] = bk_filename[result];
--result;
}
while ( (int)result >= 0 );
}
return result;
}

original_code:
void
get_emt36_filename() {
 int i;
 d_word base;
 lc_word(0306, &base);
 for (i = 0; i < 8; i++) {
  d_word d;
  lc_word(base + 6 + 2*i, &d);
  bk_filename[2*i] = d & 0xff;
  bk_filename[2*i+1] = d >> 8;
 }
 bk_filename[16] = '\0';
 for (i = 15; i >= 0 && bk_filename[i] == ' '; unix_filename[i--]='\0');
 for (; i >= 0; i--) unix_filename[i] = bk_filename[i];
}

canonical_original_code:
void get_emt36_filename() {
 int i;
 unsigned short base;
 lc_word(0306, &base);
 for (i = 0; i < 8; i++) {
  unsigned short d;
  lc_word(base + 6 + 2*i, &d);
  bk_filename[2*i] = d & 0xff;
  bk_filename[2*i+1] = d >> 8;
 }
 bk_filename[16] = '\0';
 for (i = 15; i >= 0 && bk_filename[i] == ' '; unix_filename[i--]='\0');
 for (; i >= 0; i--) unix_filename[i] = bk_filename[i];
}

variable_types: i int

variable_types:
base
unsigned short

variable_types:
d
unsigned short


decompiled_code:
void tape_read_start()
{
if ( !tapeflag )
{
get_emt36_filename();
sprintf(buf_5, "maketape '%s' '%s'", bk_filename, unix_filename);
tape_read_file = popen(buf_5, "r");
if ( tape_read_file )
tape_read_ticks = ticks;
else
perror(unix_filename);
}
}

canonical_decompiled_code:
void func3()
{
if ( !tapeflag )
{
func2();
sprintf(buf_5, "maketape '%s' '%s'", bk_filename, unix_filename);
tape_read_file = popen(buf_5, "r");
if ( tape_read_file )
tape_read_ticks = ticks;
else
perror(unix_filename);
}
}

original_code:
void
tape_read_start() {
 static char buf[80];
 if (tapeflag) {
  return;
 }
 get_emt36_filename();
 sprintf(buf, "maketape '%s' '%s'", bk_filename, unix_filename);
 tape_read_file = popen(buf, "r");
 if (tape_read_file) {
  tape_read_ticks = ticks;
 } else perror(unix_filename);
}

variable_types:
buf
char [80]


decompiled_code:
int tape_read_finish()
{
int result; // eax

if ( tape_read_file )
{
result = pclose(tape_read_file);
tape_read_file = 0LL;
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax

if ( tape_read_file )
{
result = pclose(tape_read_file);
tape_read_file = 0LL;
}
return result;
}

original_code:
void
tape_read_finish() {
 if (!tape_read_file) return;
 pclose(tape_read_file);
 tape_read_file = 0;
}


decompiled_code:
void __fastcall tape_write(int a1, int a2)
{
char *v2; // rbx
char *v3; // rax
unsigned int i; // ebx
unsigned int v5; // ebp

if ( fake_tape )
{
if ( (unsigned __int8)tape_status != a1 )
{
ticks = ticks + 10000.0;
tape_status = a1;
}
if ( tape_read_file && tape_status )
{
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
}
else
{
if ( (unsigned __int8)tape_status != a1 )
{
ticks = ticks + 10000.0;
tape_status = a1;
if ( (_BYTE)a1 )
v2 = dcgettext(0LL, "OFF", 5);
else
v2 = dcgettext(0LL, "ON", 5);
v3 = dcgettext(0LL, "Tape %s\n", 5);
fprintf(stderr, v3, v2);
if ( tape_status )
{
fflush(tape_write_file);
if ( tape_read_file )
tape_read_finish();
}
else
{
tape_write_ticks = ticks;
tape_read_ticks = ticks;
tape_read_start();
}
}
if ( !tape_status && (unsigned __int8)tape_write_val != a2 )
{
for ( i = (int)(ticks - *(double *)&tape_write_ticks); i; i -= v5 )
{
v5 = 0x7FFF;
if ( i <= 0x7FFF )
v5 = i;
fputc((v5 >> 8) | ((unsigned __int8)tape_write_val << 7), tape_write_file);
fputc((unsigned __int8)v5, tape_write_file);
}
tape_write_ticks = ticks;
tape_write_val = a2;
}
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2)
{
char *v2; // rbx
char *v3; // rax
unsigned int i; // ebx
unsigned int v5; // ebp

if ( fake_tape )
{
if ( (unsigned __int8)tape_status != a1 )
{
ticks = ticks + 10000.0;
tape_status = a1;
}
if ( tape_read_file && tape_status )
{
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
}
else
{
if ( (unsigned __int8)tape_status != a1 )
{
ticks = ticks + 10000.0;
tape_status = a1;
if ( (_BYTE)a1 )
v2 = dcgettext(0LL, "OFF", 5);
else
v2 = dcgettext(0LL, "ON", 5);
v3 = dcgettext(0LL, "Tape %s\n", 5);
fprintf(stderr, v3, v2);
if ( tape_status )
{
fflush(tape_write_file);
if ( tape_read_file )
func4();
}
else
{
tape_write_ticks = ticks;
tape_read_ticks = ticks;
func3();
}
}
if ( !tape_status && (unsigned __int8)tape_write_val != a2 )
{
for ( i = (int)(ticks - *(double *)&tape_write_ticks); i; i -= v5 )
{
v5 = 0x7FFF;
if ( i <= 0x7FFF )
v5 = i;
fputc((v5 >> 8) | ((unsigned __int8)tape_write_val << 7), tape_write_file);
fputc((unsigned __int8)v5, tape_write_file);
}
tape_write_ticks = ticks;
tape_write_val = a2;
}
}
}

original_code:
void tape_write(unsigned status, unsigned val) {
    if (fake_tape) {
 if (status != tape_status) {
  ticks += 10000;
  tape_status = status;
 }
 if (tape_read_file && tape_status) {
  fclose(tape_read_file);
  tape_read_file = 0;
  fake_state = Idle;
 }
 return;
    }
    if (status != tape_status) {
 ticks += 10000;
 tape_status = status;
 fprintf(stderr, gettext ("Tape %s\n"), tape_status ? gettext ("OFF") : gettext ("ON"));
 if (!tape_status) {
     tape_read_ticks = tape_write_ticks = ticks;
     tape_read_start();
 } else {
     fflush(tape_write_file);
     if(tape_read_file) tape_read_finish();
 }
    }
    if (!tape_status && val != tape_write_val) {
     unsigned delta = ticks - tape_write_ticks;
     while (delta) {
  unsigned delta2 = (delta <= 32767) ? delta : 32767;
  fputc((delta2 >> 8) | (tape_write_val << 7), tape_write_file);
  fputc(delta2 & 0xFF, tape_write_file);
  delta -= delta2;
     }
     tape_write_ticks = ticks;
 tape_write_val = val;
    }
}

canonical_original_code:
void tape_write(unsigned int status, unsigned int val) {
    if (fake_tape) {
 if (status != tape_status) {
  ticks += 10000;
  tape_status = status;
 }
 if (tape_read_file && tape_status) {
  fclose(tape_read_file);
  tape_read_file = 0;
  fake_state = Idle;
 }
 return;
    }
    if (status != tape_status) {
 ticks += 10000;
 tape_status = status;
 fprintf(stderr, gettext ("Tape %s\n"), tape_status ? gettext ("OFF") : gettext ("ON"));
 if (!tape_status) {
     tape_read_ticks = tape_write_ticks = ticks;
     tape_read_start();
 } else {
     fflush(tape_write_file);
     if(tape_read_file) tape_read_finish();
 }
    }
    if (!tape_status && val != tape_write_val) {
     unsigned int delta = ticks - tape_write_ticks;
     while (delta) {
  unsigned int delta2 = (delta <= 32767) ? delta : 32767;
  fputc((delta2 >> 8) | (tape_write_val << 7), tape_write_file);
  fputc(delta2 & 0xFF, tape_write_file);
  delta -= delta2;
     }
     tape_write_ticks = ticks;
 tape_write_val = val;
    }
}

variable_types:
status
unsigned int

variable_types:
val
unsigned int


decompiled_code:
__int64 fake_tuneup_sequence()
{
sc_word(204LL, 10LL);
return pop(&pdp, (unsigned int)((_DWORD)&pdp + 14));
}

canonical_decompiled_code:
__int64 func6()
{
sc_word(204LL, 10LL);
return pop(&pdp, (unsigned int)((_DWORD)&pdp + 14));
}

original_code:
void fake_tuneup_sequence() {
 sc_word(0314, 10);
        pop( &pdp, &pdp.regs[7] );
}


decompiled_code:
void fake_array_with_tuneup()
{
pdp[7] = -24866;
}

original_code:
void fake_array_with_tuneup() {
 pdp.regs[7] = 0117336;
}


decompiled_code:
__int64 __fastcall fake_read_strobe()
{
unsigned int v0; // ebx
char *v1; // rax
__int16 v2; // ax
char *v3; // rax
unsigned __int8 v4; // al
char v5; // cl
unsigned int v7; // ebp
char *v8; // rax
unsigned __int8 v9; // al
char v10; // cl
unsigned int v11; // ebp
char *v12; // rax
int v13; // eax
char v14; // cl
unsigned int v15; // ebp
char *v16; // rax
char v17; // cl
__int16 v18[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( fake_state )
{
switch ( fake_state )
{
case 0:
goto LABEL_3;
case 1:
if ( (bitnum_4 & 7) != 0 )
goto LABEL_10;
goto LABEL_5;
case 2:
if ( (bitnum_4 & 7) == 0 )
{
if ( tape_read_file )
v9 = fgetc(tape_read_file);
else
v9 = bitnum_4 == 0;
curbyte_0 = v9;
curlen_1 |= v9 << bitnum_4;
}
v10 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v10 & 7)) & 1;
if ( bitnum_4 == 16 )
{
v11 = curlen_1;
v12 = dcgettext(0LL, "File length will be %o\n", 5);
fprintf(stderr, v12, v11);
fake_state = 3;
bitnum_4 = 0;
}
goto LABEL_12;
case 3:
v0 = (bk_filename[bitnum_4 >> 3] >> (bitnum_4 & 7)) & 1;
if ( ++bitnum_4 == 128 )
{
fake_state = 4;
bitnum_4 = 0;
}
goto LABEL_12;
case 4:
if ( (bitnum_4 & 7) == 0 )
{
if ( tape_read_file )
{
curbyte_0 = fgetc(tape_read_file);
}
else
{
lc_word(0LL, v18);
curbyte_0 = v18[0];
}
v13 = checksum_3 + (unsigned __int8)curbyte_0;
if ( (v13 & 0x10000) != 0 )
v13 = (unsigned __int16)(checksum_3 + (unsigned __int8)curbyte_0) + 1;
checksum_3 = v13;
}
v14 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v14 & 7)) & 1;
if ( 8 * curlen_1 == bitnum_4 )
{
if ( !tape_read_file )
++checksum_3;
v15 = checksum_3;
v16 = dcgettext(0LL, "Checksum will be %06o\n", 5);
fprintf(stderr, v16, v15);
fake_state = 5;
bitnum_4 = 0;
}
goto LABEL_12;
case 5:
v17 = bitnum_4++;
v0 = ((unsigned int)checksum_3 >> v17) & 1;
if ( bitnum_4 == 16 )
{
if ( tape_read_file )
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
goto LABEL_12;
default:
goto LABEL_12;
}
}
if ( tape_read_file )
{
LABEL_3:
v1 = dcgettext(0LL, "Asked for strobe in Idle state?\n", 5);
fprintf(stderr, v1);
fclose(tape_read_file);
v2 = 5;
}
else
{
get_emt36_filename();
tape_read_file = fopen(unix_filename, "r");
v3 = dcgettext(0LL, "Will read unix file <%s> under BK name <%s>\n", 5);
fprintf(stderr, v3, unix_filename, bk_filename);
fake_state = 1;
bitnum_4 = 0;
checksum_3 = 0;
curaddr_2 = 0;
curlen_1 = 0;
LABEL_5:
v4 = 0;
if ( tape_read_file )
v4 = fgetc(tape_read_file);
curbyte_0 = v4;
curaddr_2 |= v4 << bitnum_4;
LABEL_10:
v5 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v5 & 7)) & 1;
if ( bitnum_4 == 16 )
{
v7 = curaddr_2;
v8 = dcgettext(0LL, "File address will be %o\n", 5);
fprintf(stderr, v8, v7);
fake_state = 2;
bitnum_4 = 0;
}
LABEL_12:
v2 = v0 == 0 ? 5 : 15;
}
pdp[4] = v2;
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

canonical_decompiled_code:
__int64 __fastcall func8()
{
unsigned int v0; // ebx
char *v1; // rax
__int16 v2; // ax
char *v3; // rax
unsigned __int8 v4; // al
char v5; // cl
unsigned int v7; // ebp
char *v8; // rax
unsigned __int8 v9; // al
char v10; // cl
unsigned int v11; // ebp
char *v12; // rax
int v13; // eax
char v14; // cl
unsigned int v15; // ebp
char *v16; // rax
char v17; // cl
__int16 v18[13]; // [rsp+Eh] [rbp-1Ah] BYREF

if ( fake_state )
{
switch ( fake_state )
{
case 0:
goto LABEL_3;
case 1:
if ( (bitnum_4 & 7) != 0 )
goto LABEL_10;
goto LABEL_5;
case 2:
if ( (bitnum_4 & 7) == 0 )
{
if ( tape_read_file )
v9 = fgetc(tape_read_file);
else
v9 = bitnum_4 == 0;
curbyte_0 = v9;
curlen_1 |= v9 << bitnum_4;
}
v10 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v10 & 7)) & 1;
if ( bitnum_4 == 16 )
{
v11 = curlen_1;
v12 = dcgettext(0LL, "File length will be %o\n", 5);
fprintf(stderr, v12, v11);
fake_state = 3;
bitnum_4 = 0;
}
goto LABEL_12;
case 3:
v0 = (bk_filename[bitnum_4 >> 3] >> (bitnum_4 & 7)) & 1;
if ( ++bitnum_4 == 128 )
{
fake_state = 4;
bitnum_4 = 0;
}
goto LABEL_12;
case 4:
if ( (bitnum_4 & 7) == 0 )
{
if ( tape_read_file )
{
curbyte_0 = fgetc(tape_read_file);
}
else
{
lc_word(0LL, v18);
curbyte_0 = v18[0];
}
v13 = checksum_3 + (unsigned __int8)curbyte_0;
if ( (v13 & 0x10000) != 0 )
v13 = (unsigned __int16)(checksum_3 + (unsigned __int8)curbyte_0) + 1;
checksum_3 = v13;
}
v14 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v14 & 7)) & 1;
if ( 8 * curlen_1 == bitnum_4 )
{
if ( !tape_read_file )
++checksum_3;
v15 = checksum_3;
v16 = dcgettext(0LL, "Checksum will be %06o\n", 5);
fprintf(stderr, v16, v15);
fake_state = 5;
bitnum_4 = 0;
}
goto LABEL_12;
case 5:
v17 = bitnum_4++;
v0 = ((unsigned int)checksum_3 >> v17) & 1;
if ( bitnum_4 == 16 )
{
if ( tape_read_file )
fclose(tape_read_file);
tape_read_file = 0LL;
fake_state = 0;
}
goto LABEL_12;
default:
goto LABEL_12;
}
}
if ( tape_read_file )
{
LABEL_3:
v1 = dcgettext(0LL, "Asked for strobe in Idle state?\n", 5);
fprintf(stderr, v1);
fclose(tape_read_file);
v2 = 5;
}
else
{
func2();
tape_read_file = fopen(unix_filename, "r");
v3 = dcgettext(0LL, "Will read unix file <%s> under BK name <%s>\n", 5);
fprintf(stderr, v3, unix_filename, bk_filename);
fake_state = 1;
bitnum_4 = 0;
checksum_3 = 0;
curaddr_2 = 0;
curlen_1 = 0;
LABEL_5:
v4 = 0;
if ( tape_read_file )
v4 = fgetc(tape_read_file);
curbyte_0 = v4;
curaddr_2 |= v4 << bitnum_4;
LABEL_10:
v5 = bitnum_4++;
v0 = ((int)(unsigned __int8)curbyte_0 >> (v5 & 7)) & 1;
if ( bitnum_4 == 16 )
{
v7 = curaddr_2;
v8 = dcgettext(0LL, "File address will be %o\n", 5);
fprintf(stderr, v8, v7);
fake_state = 2;
bitnum_4 = 0;
}
LABEL_12:
v2 = v0 == 0 ? 5 : 15;
}
pdp[4] = v2;
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

original_code:
void fake_read_strobe() {
 static int bitnum;
 static unsigned checksum;
 static unsigned char curbyte;
 static unsigned curlen;
 static unsigned curaddr;
 int bit;
 if (fake_state == Idle && !tape_read_file) {
  get_emt36_filename();
  tape_read_file = fopen(unix_filename, "r");
  fprintf(stderr, gettext ("Will read unix file <%s> under BK name <%s>\n"),
   unix_filename, bk_filename);
  fake_state = Addr;
  bitnum = 0;
  checksum = 0;
  curlen = curaddr = 0;
 }
 switch (fake_state) {
 case Idle:
  fprintf(stderr, gettext ("Asked for strobe in Idle state?\n"));
  fclose(tape_read_file);
  bit = 0;
  break;
 case Addr:
  if (!(bitnum & 7)) {
   curbyte = tape_read_file ? fgetc(tape_read_file) : 0;
   curaddr |= curbyte << bitnum;
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File address will be %o\n"), curaddr);
   fake_state = Len;
   bitnum = 0;
  }
                break;
 case Len:
  if (!(bitnum & 7)) {
                        curbyte = tape_read_file ? fgetc(tape_read_file) :
    bitnum ? 0 : 1;
   curlen |= curbyte << bitnum;
                }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File length will be %o\n"), curlen);
                        fake_state = Name;
                        bitnum = 0;
                }
                break;
 case Name:
  bit = (bk_filename[bitnum >> 3] >> (bitnum & 7)) & 1;
  bitnum++;
  if (bitnum == 16 * 8) {
   fake_state = Data;
   bitnum = 0;
  }
  break;
 case Data:
  if (!(bitnum & 7)) {
   if (tape_read_file) {
    curbyte = fgetc(tape_read_file);
   } else {
    d_word w;
    lc_word(0, &w);
    curbyte = w & 0xff;
   }
   checksum += curbyte;
   if (checksum & 0200000) {
    checksum = (checksum & 0xFFFF) + 1;
   }
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == curlen * 8) {
   if (!tape_read_file) checksum++;
   fprintf(stderr, gettext ("Checksum will be %06o\n"), checksum);
   fake_state = Checksum;
   bitnum = 0;
  }
  break;
 case Checksum:
  bit = (checksum >> bitnum++) & 1;
  if (bitnum == 16) {
   if (tape_read_file) fclose(tape_read_file);
   tape_read_file = 0;
   fake_state = Idle;
  }
  break;
 }
        pdp.regs[4] = bit ? 15 : 5;
        pop( &pdp, &pdp.regs[7] );
}

canonical_original_code:
void fake_read_strobe() {
 static int bitnum;
 static unsigned int checksum;
 static unsigned char curbyte;
 static unsigned int curlen;
 static unsigned int curaddr;
 int bit;
 if (fake_state == Idle && !tape_read_file) {
  get_emt36_filename();
  tape_read_file = fopen(unix_filename, "r");
  fprintf(stderr, gettext ("Will read unix file <%s> under BK name <%s>\n"),
   unix_filename, bk_filename);
  fake_state = Addr;
  bitnum = 0;
  checksum = 0;
  curlen = curaddr = 0;
 }
 switch (fake_state) {
 case Idle:
  fprintf(stderr, gettext ("Asked for strobe in Idle state?\n"));
  fclose(tape_read_file);
  bit = 0;
  break;
 case Addr:
  if (!(bitnum & 7)) {
   curbyte = tape_read_file ? fgetc(tape_read_file) : 0;
   curaddr |= curbyte << bitnum;
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File address will be %o\n"), curaddr);
   fake_state = Len;
   bitnum = 0;
  }
                break;
 case Len:
  if (!(bitnum & 7)) {
                        curbyte = tape_read_file ? fgetc(tape_read_file) :
    bitnum ? 0 : 1;
   curlen |= curbyte << bitnum;
                }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == 16) {
   fprintf(stderr, gettext ("File length will be %o\n"), curlen);
                        fake_state = Name;
                        bitnum = 0;
                }
                break;
 case Name:
  bit = (bk_filename[bitnum >> 3] >> (bitnum & 7)) & 1;
  bitnum++;
  if (bitnum == 16 * 8) {
   fake_state = Data;
   bitnum = 0;
  }
  break;
 case Data:
  if (!(bitnum & 7)) {
   if (tape_read_file) {
    curbyte = fgetc(tape_read_file);
   } else {
    unsigned short w;
    lc_word(0, &w);
    curbyte = w & 0xff;
   }
   checksum += curbyte;
   if (checksum & 0200000) {
    checksum = (checksum & 0xFFFF) + 1;
   }
  }
  bit = (curbyte >> ((bitnum++) & 7)) & 1;
  if (bitnum == curlen * 8) {
   if (!tape_read_file) checksum++;
   fprintf(stderr, gettext ("Checksum will be %06o\n"), checksum);
   fake_state = Checksum;
   bitnum = 0;
  }
  break;
 case Checksum:
  bit = (checksum >> bitnum++) & 1;
  if (bitnum == 16) {
   if (tape_read_file) fclose(tape_read_file);
   tape_read_file = 0;
   fake_state = Idle;
  }
  break;
 }
        pdp.regs[4] = bit ? 15 : 5;
        pop( &pdp, &pdp.regs[7] );
}

variable_types: bitnum int

variable_types:
checksum
unsigned int

variable_types:
curbyte
unsigned char

variable_types:
curlen
unsigned int

variable_types:
curaddr
unsigned int

variable_types: bit int

variable_types:
w
unsigned short


decompiled_code:
__int64 fake_write_file()
{
const char *v0; // rdx
unsigned __int16 v2; // [rsp+2h] [rbp-16h] BYREF
__int16 v3; // [rsp+4h] [rbp-14h] BYREF
unsigned __int16 v4; // [rsp+6h] [rbp-12h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

lc_word(198LL, &v5);
get_emt36_filename();
tape_write_file = fopen(unix_filename, "w");
v0 = " ";
if ( !tape_write_file )
v0 = " NOT ";
fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n", v0, bk_filename, unix_filename);
if ( tape_write_file )
{
lc_word(v5 + 2, &v2);
fputc((unsigned __int8)v2, tape_write_file);
fputc(HIBYTE(v2), tape_write_file);
lc_word(v5 + 4, &v3);
fputc((unsigned __int8)v3, tape_write_file);
fputc(HIBYTE(v3), tape_write_file);
for ( ; v3; --v3 )
{
lc_word(v2, &v4);
if ( (v2 & 1) != 0 )
v4 >>= 8;
fputc((unsigned __int8)v4, tape_write_file);
++v2;
}
fclose(tape_write_file);
tape_write_file = 0LL;
sl_byte(pdp, v5 + 1, 0LL);
}
else
{
perror(unix_filename);
sl_byte(pdp, v5 + 1, 1LL);
}
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

canonical_decompiled_code:
__int64 func9()
{
const char *v0; // rdx
unsigned __int16 v2; // [rsp+2h] [rbp-16h] BYREF
__int16 v3; // [rsp+4h] [rbp-14h] BYREF
unsigned __int16 v4; // [rsp+6h] [rbp-12h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

lc_word(198LL, &v5);
func2();
tape_write_file = fopen(unix_filename, "w");
v0 = " ";
if ( !tape_write_file )
v0 = " NOT ";
fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n", v0, bk_filename, unix_filename);
if ( tape_write_file )
{
lc_word(v5 + 2, &v2);
fputc((unsigned __int8)v2, tape_write_file);
fputc(HIBYTE(v2), tape_write_file);
lc_word(v5 + 4, &v3);
fputc((unsigned __int8)v3, tape_write_file);
fputc(HIBYTE(v3), tape_write_file);
for ( ; v3; --v3 )
{
lc_word(v2, &v4);
if ( (v2 & 1) != 0 )
v4 >>= 8;
fputc((unsigned __int8)v4, tape_write_file);
++v2;
}
fclose(tape_write_file);
tape_write_file = 0LL;
sl_byte(pdp, v5 + 1, 0LL);
}
else
{
perror(unix_filename);
sl_byte(pdp, v5 + 1, 1LL);
}
return pop(pdp, (unsigned int)((_DWORD)pdp + 14));
}

original_code:
void
fake_write_file() {
 c_addr base;
 lc_word(0306, &base);
 get_emt36_filename();
 tape_write_file = fopen(unix_filename, "w");
 fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n",
  tape_write_file ? " " : " NOT ", bk_filename, unix_filename);
 if (tape_write_file) {
  d_word addr;
         d_word length;
  lc_word(base + 2, &addr);
  fputc(addr & 0xff, tape_write_file);
  fputc(addr >> 8, tape_write_file);
  lc_word(base + 4, &length);
  fputc(length & 0xff, tape_write_file);
  fputc(length >> 8, tape_write_file);
  for (; length; addr++, length--) {
   d_word w;
   lc_word(addr, &w);
   if (addr & 1) w >>= 8;
   fputc(w & 0xff, tape_write_file);
  }
  fclose(tape_write_file);
  tape_write_file = 0;
  sl_byte(&pdp, base+1, 0);
 } else {
  perror(unix_filename);
  sl_byte(&pdp, base+1, 1);
 }
 pop( &pdp, &pdp.regs[7] );
}

canonical_original_code:
void fake_write_file() {
 unsigned long base;
 lc_word(0306, &base);
 get_emt36_filename();
 tape_write_file = fopen(unix_filename, "w");
 fprintf(stderr, "Will%swrite BK file <%s> under unix file name <%s>\n",
  tape_write_file ? " " : " NOT ", bk_filename, unix_filename);
 if (tape_write_file) {
  unsigned short addr;
         unsigned short length;
  lc_word(base + 2, &addr);
  fputc(addr & 0xff, tape_write_file);
  fputc(addr >> 8, tape_write_file);
  lc_word(base + 4, &length);
  fputc(length & 0xff, tape_write_file);
  fputc(length >> 8, tape_write_file);
  for (; length; addr++, length--) {
   unsigned short w;
   lc_word(addr, &w);
   if (addr & 1) w >>= 8;
   fputc(w & 0xff, tape_write_file);
  }
  fclose(tape_write_file);
  tape_write_file = 0;
  sl_byte(&pdp, base+1, 0);
 } else {
  perror(unix_filename);
  sl_byte(&pdp, base+1, 1);
 }
 pop( &pdp, &pdp.regs[7] );
}

variable_types:
base
unsigned long

variable_types:
addr
unsigned short

variable_types:
length
unsigned short


unit

decompiled_code:
__int64 __fastcall unit(unsigned __int16 a1)
{
return HIBYTE(a1) & 3;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1)
{
return HIBYTE(a1) & 3;
}

original_code:
static inline unsigned unit(d_word word) {
 return (word >> 8) & 3;
}

canonical_original_code:
static inline unsigned int unit(unsigned short word) {
 return (word >> 8) & 3;
}

variable_types:
word
unsigned short


cmd

decompiled_code:
__int64 __fastcall cmd(unsigned __int16 a1)
{
return (a1 >> 1) & 7;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
return (a1 >> 1) & 7;
}

original_code:
static inline disk_cmd cmd(d_word word) {
 return (word >> 1) & 7;
}

canonical_original_code:
static inline enum disk_cmd cmd(unsigned short word) {
 return (word >> 1) & 7;
}

return_type: enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 }


decompiled_code:
void __fastcall tdisk_open(__int64 a1, const char *a2)
{
int v2; // ebx
unsigned int v3; // eax
void *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*(_BYTE *)(a1 + 32) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*(_DWORD *)a1 = v3;
if ( (v3 & 0x3F) != 0 )
{
v5 = dcgettext(0LL, "%s is not an integer number of blocks\n", 5);
fprintf(stderr, v5, a2);
close(v2);
}
else
{
v4 = mmap(0LL, v3, *(_BYTE *)(a1 + 32) == 0 ? 3 : 1, 1, v2, 0LL);
*(_QWORD *)(a1 + 8) = v4;
if ( v4 == (void *)-1LL )
{
*(_QWORD *)(a1 + 8) = 0LL;
perror(a2);
}
if ( *(_BYTE *)(a1 + 32) )
{
v6 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v6, a2);
}
}
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, const char *a2)
{
int v2; // ebx
unsigned int v3; // eax
void *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = open(a2, 2);
if ( v2 == -1 && (*(_BYTE *)(a1 + 32) = 1, v2 = open(a2, 0), v2 == -1) )
{
perror(a2);
}
else
{
v3 = lseek(v2, 0LL, 2);
*(_DWORD *)a1 = v3;
if ( (v3 & 0x3F) != 0 )
{
v5 = dcgettext(0LL, "%s is not an integer number of blocks\n", 5);
fprintf(stderr, v5, a2);
close(v2);
}
else
{
v4 = mmap(0LL, v3, *(_BYTE *)(a1 + 32) == 0 ? 3 : 1, 1, v2, 0LL);
*(_QWORD *)(a1 + 8) = v4;
if ( v4 == (void *)-1LL )
{
*(_QWORD *)(a1 + 8) = 0LL;
perror(a2);
}
if ( *(_BYTE *)(a1 + 32) )
{
v6 = dcgettext(0LL, "%s will be read only\n", 5);
fprintf(stderr, v6, a2);
}
}
}
}

original_code:
void tdisk_open(tdisk_t * pdt, char * name) {
 int fd = open(name, 02);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length % 64) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks\n"), name);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

canonical_original_code:
void tdisk_open(struct tdisk_t *pdt, char *name) {
 int fd = open(name, 02);
 if (fd == -1) {
  pdt->ro = 1;
  fd = open(name, 00);
 }
 if (fd == -1) {
  perror(name);
  return;
 }
 pdt->length = lseek(fd, 0, 2);
 if (pdt->length % 64) {
  fprintf(stderr, gettext ("%s is not an integer number of blocks\n"), name);
  close(fd);
  return;
 }
 pdt->image = mmap(0, pdt->length, 0x1 | (pdt->ro ? 0 : 0x2), 0x01, fd, 0);
 if (pdt->image == ((void *) -1)) {
  pdt->image = 0;
  perror(name);
 }
 if (pdt->ro) {
  fprintf(stderr, gettext ("%s will be read only\n"), name);
 }
}

variable_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd { nopD=0, rtcD=1, stepinD=2, stepoutD=3, readtsD=4, readD=5, writeD=6, delD=7 } cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; } *

variable_types:
name
char *

variable_types: fd int

user_defined_types: struct tdisk_t { unsigned int length; unsigned short *image; unsigned short *ptr; unsigned char track; enum disk_cmd cmd; unsigned char ro; unsigned char motor; unsigned char inprogress; unsigned char crc; unsigned char cursec; }


decompiled_code:
__int64 tdisk_init()
{
__int64 *v0; // rax

if ( !init_done_0 )
{
disk_open(&tdisks, floppyA);
disk_open((unsigned int)((_DWORD)&tdisks + 40), floppyB);
disk_open((unsigned int)((_DWORD)&tdisks + 80), floppyC);
disk_open((unsigned int)((_DWORD)&tdisks + 120), floppyD);
init_done_0 = 1;
}
v0 = &tdisks;
do
{
v0[2] = 0LL;
*((_BYTE *)v0 + 34) = 0;
*((_BYTE *)v0 + 33) = 0;
*((_BYTE *)v0 + 24) = 0;
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&tdisks + 160) );
selected = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 *v0; // rax

if ( !init_done_0 )
{
disk_open(&tdisks, floppyA);
disk_open((unsigned int)((_DWORD)&tdisks + 40), floppyB);
disk_open((unsigned int)((_DWORD)&tdisks + 80), floppyC);
disk_open((unsigned int)((_DWORD)&tdisks + 120), floppyD);
init_done_0 = 1;
}
v0 = &tdisks;
do
{
v0[2] = 0LL;
*((_BYTE *)v0 + 34) = 0;
*((_BYTE *)v0 + 33) = 0;
*((_BYTE *)v0 + 24) = 0;
v0 += 5;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&tdisks + 160) );
selected = -1;
return 0LL;
}

original_code:
int tdisk_init() {
 static char init_done = 0;
 int i;
 if (!init_done) {
  disk_open(&tdisks[0], floppyA);
  disk_open(&tdisks[1], floppyB);
  disk_open(&tdisks[2], floppyC);
  disk_open(&tdisks[3], floppyD);
  init_done = 1;
 }
 for (i = 0; i < 4; i++) {
  tdisks[i].ptr = ((void *)0);
  tdisks[i].track =
  tdisks[i].motor =
  tdisks[i].inprogress = 0;
 }
 selected = -1;
 return 0;
}

variable_types: i int