decompiled_code:
__int64 __fastcall get_addr_and_class(__int64 *a1, int a2, unsigned int a3, _DWORD *a4)
{
__int64 v4; // rax
int v5; // er8
__int64 v6; // rdx
unsigned __int64 v7; // rsi
__int64 result; // rax

if ( !a1 )
return 0LL;
v4 = *(_QWORD *)(a1[7] + 8);
if ( *(_DWORD *)(v4 + 16) == 3 )
{
v5 = *(_DWORD *)(v4 + 144);
if ( (unsigned int)(v5 - 1) > 1 )
{
elf_errno = 19;
result = 0LL;
}
else if ( *((_DWORD *)a1 + 2) == a3 )
{
v6 = elf_fmsize[34 * (unsigned int)(v5 - 1)
+ 34 * (unsigned __int64)(unsigned int)(*((_DWORD *)a1 + 10) - 1)
+ 2 * a3];
if ( v6 )
{
if ( a2 < 0 || (v7 = v6 * (a2 + 1), a1[2] < v7) )
{
elf_errno = 70;
result = 0LL;
}
else
{
result = *a1;
if ( *a1 )
{
if ( a4 )
*a4 = v5;
result = *a1 + v7 - v6;
}
else
{
elf_errno = 18;
}
}
}
else
{
elf_errno = 3;
result = 0LL;
}
}
else
{
elf_errno = 71;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, int a2, unsigned int a3, _DWORD *a4)
{
__int64 v4; // rax
int v5; // er8
__int64 v6; // rdx
unsigned __int64 v7; // rsi
__int64 result; // rax

if ( !a1 )
return 0LL;
v4 = *(_QWORD *)(a1[7] + 8);
if ( *(_DWORD *)(v4 + 16) == 3 )
{
v5 = *(_DWORD *)(v4 + 144);
if ( (unsigned int)(v5 - 1) > 1 )
{
elf_errno = 19;
result = 0LL;
}
else if ( *((_DWORD *)a1 + 2) == a3 )
{
v6 = elf_fmsize[34 * (unsigned int)(v5 - 1)
+ 34 * (unsigned __int64)(unsigned int)(*((_DWORD *)a1 + 10) - 1)
+ 2 * a3];
if ( v6 )
{
if ( a2 < 0 || (v7 = v6 * (a2 + 1), a1[2] < v7) )
{
elf_errno = 70;
result = 0LL;
}
else
{
result = *a1;
if ( *a1 )
{
if ( a4 )
*a4 = v5;
result = *a1 + v7 - v6;
}
else
{
elf_errno = 18;
}
}
}
else
{
elf_errno = 3;
result = 0LL;
}
}
else
{
elf_errno = 71;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
return result;
}

original_code:
static char*
get_addr_and_class(const Elf_Data *data, int ndx, Elf_Type type, unsigned *cls) {
    Scn_Data *sd = (Scn_Data*)data;
    Elf_Scn *scn;
    Elf *elf;
    size_t n;
    if (!sd) {
 return ((void *)0);
    }
    do{}while(0);
    scn = sd->sd_scn;
    do{}while(0);
    do{}while(0);
    elf = scn->s_elf;
    do{}while(0);
    do{}while(0);
    if (elf->e_kind != ELF_K_ELF) {
 (_elf_errno = (ERROR_NOTELF));
 return ((void *)0);
    }
    if (!((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return ((void *)0);
    }
    if (data->d_type != type) {
 (_elf_errno = (ERROR_BADTYPE));
 return ((void *)0);
    }
    n = (_elf_fmsize[((elf->e_class))-1][((data->d_version))-0 -1][((type))-ELF_T_BYTE][(0)]);
    if (n == 0) {
 (_elf_errno = (ERROR_UNIMPLEMENTED));
 return ((void *)0);
    }
    if (ndx < 0 || data->d_size < (ndx + 1) * n) {
 (_elf_errno = (ERROR_BADINDEX));
 return ((void *)0);
    }
    if (!data->d_buf) {
 (_elf_errno = (ERROR_NULLBUF));
 return ((void *)0);
    }
    if (cls) {
 *cls = elf->e_class;
    }
    return (char*)data->d_buf + n * ndx;
}

canonical_original_code:
static char *get_addr_and_class(struct Elf_Data *data, int ndx, enum Elf_Type type, unsigned int *cls) {
    struct Scn_Data *sd = (Scn_Data*)data;
    struct Elf_Scn *scn;
    struct Elf *elf;
    unsigned long n;
    if (!sd) {
 return ((void *)0);
    }
    do{}while(0);
    scn = sd->sd_scn;
    do{}while(0);
    do{}while(0);
    elf = scn->s_elf;
    do{}while(0);
    do{}while(0);
    if (elf->e_kind != ELF_K_ELF) {
 (_elf_errno = (ERROR_NOTELF));
 return ((void *)0);
    }
    if (!((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return ((void *)0);
    }
    if (data->d_type != type) {
 (_elf_errno = (ERROR_BADTYPE));
 return ((void *)0);
    }
    n = (_elf_fmsize[((elf->e_class))-1][((data->d_version))-0 -1][((type))-ELF_T_BYTE][(0)]);
    if (n == 0) {
 (_elf_errno = (ERROR_UNIMPLEMENTED));
 return ((void *)0);
    }
    if (ndx < 0 || data->d_size < (ndx + 1) * n) {
 (_elf_errno = (ERROR_BADINDEX));
 return ((void *)0);
    }
    if (!data->d_buf) {
 (_elf_errno = (ERROR_NULLBUF));
 return ((void *)0);
    }
    if (cls) {
 *cls = elf->e_class;
    }
    return (char *)data->d_buf + n * ndx;
}

variable_types:
cls
unsigned int *

variable_types: struct Scn_Data { struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } sd_data; struct Scn_Data *sd_link; struct Elf_Scn { struct Elf_Scn *s_link; struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 } e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; } *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; } *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } u_shdr64; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } u_shdr32; } s_uhdr; long s_magic; } *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; } *

variable_types: struct Elf_Scn { struct Elf_Scn *s_link; struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 } e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; } *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; } *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data { struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } sd_data; struct Scn_Data *sd_link; struct Elf_Scn *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; } *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } u_shdr64; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } u_shdr32; } s_uhdr; long s_magic; } *


repo: aodinokov/expos

decompiled_code:
__int64 __fastcall elf32_fsize(unsigned int a1, __int64 a2, int a3)
{
return a2 * elf_fsize(1, a1, a3);
}

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

original_code:
size_t
elf32_fsize(Elf_Type type, size_t count, unsigned ver) {
    return count * _elf_fsize(1, type, ver);
}

canonical_original_code:
unsigned long elf32_fsize(enum Elf_Type type, unsigned long count, unsigned int ver) {
    return count * _elf_fsize(1, type, ver);
}

variable_types: enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 }

variable_types:
count
unsigned long

variable_types:
ver
unsigned int

return_type: unsigned long


decompiled_code:
__int64 __fastcall elf64_fsize(unsigned int a1, __int64 a2, int a3)
{
return a2 * elf_fsize(2, a1, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2, int a3)
{
return a2 * func0(2, a1, a3);
}

original_code:
size_t
elf64_fsize(Elf_Type type, size_t count, unsigned ver) {
    return count * _elf_fsize(2, type, ver);
}

canonical_original_code:
unsigned long elf64_fsize(enum Elf_Type type, unsigned long count, unsigned int ver) {
    return count * _elf_fsize(2, type, ver);
}

user_defined_types: enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 }


decompiled_code:
__int64 __fastcall gelf_fsize(__int64 a1, unsigned int a2, __int64 a3, int a4)
{
__int64 result; // rax
int v5; // edi

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 16) == 3 )
{
v5 = *(_DWORD *)(a1 + 144);
if ( (unsigned int)(v5 - 1) <= 1 )
{
result = a3 * elf_fsize(v5, a2, a4);
}
else
{
elf_errno = 19;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned int a2, __int64 a3, int a4)
{
__int64 result; // rax
int v5; // edi

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 16) == 3 )
{
v5 = *(_DWORD *)(a1 + 144);
if ( (unsigned int)(v5 - 1) <= 1 )
{
result = a3 * func0(v5, a2, a4);
}
else
{
elf_errno = 19;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
}
return result;
}

original_code:
size_t
gelf_fsize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
    if (elf) {
 if (elf->e_kind != ELF_K_ELF) {
     (_elf_errno = (ERROR_NOTELF));
 }
 else if (((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
     return count * _elf_fsize(elf->e_class, type, ver);
 }
 else {
     (_elf_errno = (ERROR_UNKNOWN_CLASS));
 }
    }
    return 0;
}

canonical_original_code:
unsigned long gelf_fsize(struct Elf *elf, enum Elf_Type type, unsigned long count, unsigned int ver) {
    if (elf) {
 if (elf->e_kind != ELF_K_ELF) {
     (_elf_errno = (ERROR_NOTELF));
 }
 else if (((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
     return count * _elf_fsize(elf->e_class, type, ver);
 }
 else {
     (_elf_errno = (ERROR_UNKNOWN_CLASS));
 }
    }
    return 0;
}

variable_types: struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 } e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; } *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn { struct Elf_Scn *s_link; struct Elf *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data { struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } sd_data; struct Scn_Data *sd_link; struct Elf_Scn *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; } *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } u_shdr64; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } u_shdr32; } s_uhdr; long s_magic; } *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; } *

user_defined_types: struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; }

user_defined_types: struct Elf_Scn { struct Elf_Scn *s_link; struct Elf *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr u_shdr64; struct Elf32_Shdr u_shdr32; } s_uhdr; long s_magic; }

user_defined_types: struct Scn_Data { struct Elf_Data sd_data; struct Scn_Data *sd_link; struct Elf_Scn *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; }

user_defined_types: struct Elf_Data { void *d_buf; enum Elf_Type d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; }


decompiled_code:
__int64 __fastcall gelf_msize(__int64 a1, unsigned int a2, __int64 a3, int a4)
{
__int64 result; // rax
unsigned int v5; // eax

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 16) == 3 )
{
v5 = *(_DWORD *)(a1 + 144) - 1;
if ( v5 <= 1 )
{
if ( a4 == 1 )
{
if ( a2 <= 0x10 )
{
result = elf_fmsize[34 * v5 + 2 * a2];
if ( result )
result *= a3;
else
elf_errno = 15;
}
else
{
elf_errno = 15;
result = 0LL;
}
}
else
{
elf_errno = 9;
result = 0LL;
}
}
else
{
elf_errno = 19;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned int a2, __int64 a3, int a4)
{
__int64 result; // rax
unsigned int v5; // eax

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 16) == 3 )
{
v5 = *(_DWORD *)(a1 + 144) - 1;
if ( v5 <= 1 )
{
if ( a4 == 1 )
{
if ( a2 <= 0x10 )
{
result = elf_fmsize[34 * v5 + 2 * a2];
if ( result )
result *= a3;
else
elf_errno = 15;
}
else
{
elf_errno = 15;
result = 0LL;
}
}
else
{
elf_errno = 9;
result = 0LL;
}
}
else
{
elf_errno = 19;
result = 0LL;
}
}
else
{
elf_errno = 13;
result = 0LL;
}
}
return result;
}

original_code:
size_t
gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
    size_t n;
    if (elf) {
 if (elf->e_kind != ELF_K_ELF) {
     (_elf_errno = (ERROR_NOTELF));
 }
 else if (!((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
     (_elf_errno = (ERROR_UNKNOWN_CLASS));
 }
 else if (!((ver) > 0 && (ver) <= 1)) {
     (_elf_errno = (ERROR_UNKNOWN_VERSION));
 }
 else if (!((unsigned)(type) < ELF_T_NUM)) {
     (_elf_errno = (ERROR_UNKNOWN_TYPE));
 }
 else if (!(n = (_elf_fmsize[((elf->e_class))-1][((ver))-0 -1][((type))-ELF_T_BYTE][(0)]))) {
     (_elf_errno = (ERROR_UNKNOWN_TYPE));
 }
 else {
     return count * n;
 }
    }
    return 0;
}

canonical_original_code:
unsigned long gelf_msize(struct Elf *elf, enum Elf_Type type, unsigned long count, unsigned int ver) {
    unsigned long n;
    if (elf) {
 if (elf->e_kind != ELF_K_ELF) {
     (_elf_errno = (ERROR_NOTELF));
 }
 else if (!((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
     (_elf_errno = (ERROR_UNKNOWN_CLASS));
 }
 else if (!((ver) > 0 && (ver) <= 1)) {
     (_elf_errno = (ERROR_UNKNOWN_VERSION));
 }
 else if (!((unsigned int)(type) < ELF_T_NUM)) {
     (_elf_errno = (ERROR_UNKNOWN_TYPE));
 }
 else if (!(n = (_elf_fmsize[((elf->e_class))-1][((ver))-0 -1][((type))-ELF_T_BYTE][(0)]))) {
     (_elf_errno = (ERROR_UNKNOWN_TYPE));
 }
 else {
     return count * n;
 }
    }
    return 0;
}

variable_types:
n
unsigned long

user_defined_types: enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 }

user_defined_types: struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; }

user_defined_types: struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; }

user_defined_types: struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; }


decompiled_code:
void *__fastcall elf32_newehdr(__int64 a1)
{
return elf_newehdr(a1, 1);
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
return func0(a1, 1);
}

original_code:
Elf32_Ehdr*
elf32_newehdr(Elf *elf) {
    return (Elf32_Ehdr*)_elf_newehdr(elf, 1);
}

canonical_original_code:
struct Elf32_Ehdr *elf32_newehdr(struct Elf *elf) {
    return (struct Elf32_Ehdr *)_elf_newehdr(elf, 1);
}

return_type: struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *


decompiled_code:
void *__fastcall elf64_newehdr(__int64 a1)
{
return elf_newehdr(a1, 2);
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1)
{
return func0(a1, 2);
}

original_code:
Elf64_Ehdr*
elf64_newehdr(Elf *elf) {
    return (Elf64_Ehdr*)_elf_newehdr(elf, 2);
}

canonical_original_code:
struct Elf64_Ehdr *elf64_newehdr(struct Elf *elf) {
    return (struct Elf64_Ehdr *)_elf_newehdr(elf, 2);
}

return_type: struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *


decompiled_code:
void *__fastcall gelf_newehdr(__int64 a1, int a2)
{
if ( (unsigned int)(a2 - 1) <= 1 && elf_fmsize[34 * (unsigned int)(elf_version - 1) + 6 + 34 * (__int64)(a2 - 1)] )
return elf_newehdr(a1, a2);
elf_errno = 19;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, int a2)
{
if ( (unsigned int)(a2 - 1) <= 1 && elf_fmsize[34 * (unsigned int)(elf_version - 1) + 6 + 34 * (__int64)(a2 - 1)] )
return func0(a1, a2);
elf_errno = 19;
return 0LL;
}

original_code:
unsigned long
gelf_newehdr(Elf *elf, int cls) {
    if (!((cls) >= 1 && (cls) <= 2) || !(_elf_fmsize[((cls))-1][((_elf_version))-0 -1][((ELF_T_EHDR))-ELF_T_BYTE][(0)])) {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return 0;
    }
    return (unsigned long)_elf_newehdr(elf, cls);
}

canonical_original_code:
unsigned long gelf_newehdr(struct Elf *elf, int cls) {
    if (!((cls) >= 1 && (cls) <= 2) || !(_elf_fmsize[((cls))-1][((_elf_version))-0 -1][((ELF_T_EHDR))-ELF_T_BYTE][(0)])) {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return 0;
    }
    return (unsigned long)_elf_newehdr(elf, cls);
}

variable_types: cls int


decompiled_code:
unsigned __int64 __fastcall elf_rand(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 result; // rax

if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 16) == 1 )
{
if ( a2 && *(_QWORD *)a1 >= a2 )
{
*(_QWORD *)(a1 + 96) = a2;
result = a2;
}
else
{
elf_errno = 8;
result = 0LL;
}
}
else
{
elf_errno = 7;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 result; // rax

if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 16) == 1 )
{
if ( a2 && *(_QWORD *)a1 >= a2 )
{
*(_QWORD *)(a1 + 96) = a2;
result = a2;
}
else
{
elf_errno = 8;
result = 0LL;
}
}
else
{
elf_errno = 7;
result = 0LL;
}
return result;
}

original_code:
size_t
elf_rand(Elf *elf, size_t offset) {
    if (!elf) {
 return 0;
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_AR) {
 (_elf_errno = (ERROR_NOTARCHIVE));
    }
    else if (offset <= 0 || offset > elf->e_size) {
 (_elf_errno = (ERROR_BADOFF));
    }
    else {
 elf->e_off = offset;
 return offset;
    }
    return 0;
}

canonical_original_code:
unsigned long elf_rand(struct Elf *elf, unsigned long offset) {
    if (!elf) {
 return 0;
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_AR) {
 (_elf_errno = (ERROR_NOTARCHIVE));
    }
    else if (offset <= 0 || offset > elf->e_size) {
 (_elf_errno = (ERROR_BADOFF));
    }
    else {
 elf->e_off = offset;
 return offset;
    }
    return 0;
}

variable_types:
offset
unsigned long


nlist

decompiled_code:
__int64 __fastcall nlist(char *file, const char **a2)
{
const char **v2; // rbx
unsigned int v3; // eax
unsigned int v4; // er13
const char *v5; // rax
unsigned int v7; // ebp
unsigned int v8; // eax
int v9; // er12
__int64 v10; // rax
__int64 v11; // r14

v2 = a2;
v3 = elf_version(1LL);
v4 = -1;
if ( v3 )
{
v7 = v3;
v8 = open(file, 0);
v9 = v8;
if ( v8 == -1 )
{
elf_version(v7);
v4 = -1;
}
else
{
v10 = elf_begin(v8, 1LL, 0LL);
v11 = v10;
if ( v10 )
{
v4 = elf_nlist(v10, a2);
elf_end(v11);
close(v9);
elf_version(v7);
if ( !v4 )
return v4;
}
else
{
close(v9);
elf_version(v7);
v4 = -1;
}
}
}
v5 = *a2;
if ( *a2 )
{
do
{
if ( !*v5 )
break;
v2[1] = 0LL;
v2 += 3;
v5 = *v2;
}
while ( *v2 );
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *file, const char **a2)
{
const char **v2; // rbx
unsigned int v3; // eax
unsigned int v4; // er13
const char *v5; // rax
unsigned int v7; // ebp
unsigned int v8; // eax
int v9; // er12
__int64 v10; // rax
__int64 v11; // r14

v2 = a2;
v3 = elf_version(1LL);
v4 = -1;
if ( v3 )
{
v7 = v3;
v8 = open(file, 0);
v9 = v8;
if ( v8 == -1 )
{
elf_version(v7);
v4 = -1;
}
else
{
v10 = elf_begin(v8, 1LL, 0LL);
v11 = v10;
if ( v10 )
{
v4 = func2(v10, a2);
elf_end(v11);
close(v9);
elf_version(v7);
if ( !v4 )
return v4;
}
else
{
close(v9);
elf_version(v7);
v4 = -1;
}
}
}
v5 = *a2;
if ( *a2 )
{
do
{
if ( !*v5 )
break;
v2[1] = 0LL;
v2 += 3;
v5 = *v2;
}
while ( *v2 );
}
return v4;
}

original_code:
int
nlist(const char *filename, struct nlist *nl) {
    int result = -1;
    unsigned oldver;
    Elf *elf;
    int fd;
    if ((oldver = elf_version(1)) != 0) {
 if ((fd = open(filename, (0 | 0))) != -1) {
     if ((elf = elf_begin(fd, ELF_C_READ, ((void *)0)))) {
  result = _elf_nlist(elf, nl);
  elf_end(elf);
     }
     close(fd);
 }
 elf_version(oldver);
    }
    if (result) {
 while (nl->n_name && *nl->n_name) {
     nl->n_value = 0;
     nl++;
 }
    }
    return result;
}

canonical_original_code:
int nlist(char *filename, struct nlist *nl) {
    int result = -1;
    unsigned int oldver;
    struct Elf *elf;
    int fd;
    if ((oldver = elf_version(1)) != 0) {
 if ((fd = open(filename, (0 | 0))) != -1) {
     if ((elf = elf_begin(fd, ELF_C_READ, ((void *)0)))) {
  result = _elf_nlist(elf, nl);
  elf_end(elf);
     }
     close(fd);
 }
 elf_version(oldver);
    }
    if (result) {
 while (nl->n_name && *nl->n_name) {
     nl->n_value = 0;
     nl++;
 }
    }
    return result;
}

variable_types:
filename
char *

variable_types: struct nlist { char *n_name; long n_value; short n_scnum; unsigned short n_type; char n_sclass; char n_numaux; } *

variable_types: result int

variable_types:
oldver
unsigned int

variable_types: fd int

return_type: int


decompiled_code:
__int64 __fastcall elf_ndxscn(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = *(_QWORD *)(a1 + 16);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = *(_QWORD *)(a1 + 16);
else
result = 0LL;
return result;
}

original_code:
size_t
elf_ndxscn(Elf_Scn *scn) {
    if (!scn) {
 return 0;
    }
    do{}while(0);
    return scn->s_index;
}

canonical_original_code:
unsigned long elf_ndxscn(struct Elf_Scn *scn) {
    if (!scn) {
 return 0;
    }
    do{}while(0);
    return scn->s_index;
}


decompiled_code:
unsigned int **__fastcall elf_getarsym(__int64 a1, char *a2)
{
char *v3; // r13
unsigned int *v4; // rbp
unsigned __int64 v5; // rbx
unsigned int *v6; // rdi
unsigned int *v7; // rbp
unsigned int **v8; // r14
__int64 v9; // r15
unsigned int *v10; // rbx
unsigned int **v11; // rax
unsigned int **ptr; // [rsp+8h] [rbp-70h]
__int64 v14; // [rsp+18h] [rbp-60h]
__int64 v15; // [rsp+20h] [rbp-58h]
char v16; // [rsp+38h] [rbp-40h] BYREF

v3 = a2;
if ( !a2 )
v3 = &v16;
*(_QWORD *)v3 = 0LL;
if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
{
elf_errno = 7;
return 0LL;
}
v4 = *(unsigned int **)(a1 + 112);
if ( !v4 || (*(_BYTE *)(a1 + 212) & 0x10) != 0 )
{
LABEL_24:
*(_QWORD *)v3 = *(_QWORD *)(a1 + 120);
return *(unsigned int ***)(a1 + 112);
}
v5 = *(_QWORD *)(a1 + 120);
if ( v5 <= 3 )
{
elf_errno = 40;
return 0LL;
}
v14 = _byteswap_ulong(*v4);
v15 = (unsigned int)v14 + 1LL;
if ( v5 < 4 * v15 )
{
elf_errno = 40;
return 0LL;
}
ptr = (unsigned int **)malloc(24 * v15);
if ( !ptr )
{
elf_errno = 60;
return ptr;
}
v6 = &v4[v15];
v7 = (unsigned int *)((char *)v4 + v5);
if ( !v14 )
{
LABEL_23:
v11 = &ptr[3 * v15 - 3];
*v11 = 0LL;
v11[2] = (unsigned int *)-1LL;
v11[1] = 0LL;
*(_QWORD *)(a1 + 112) = ptr;
*(_QWORD *)(a1 + 120) = v15;
*(_BYTE *)(a1 + 212) |= 0x10u;
goto LABEL_24;
}
v8 = ptr;
v9 = 0LL;
while ( 1 )
{
*v8 = v6;
if ( v7 <= v6 )
break;
v10 = v6;
while ( *(_BYTE *)v10 )
{
v10 = (unsigned int *)((char *)v10 + 1);
if ( v7 == v10 )
goto LABEL_22;
}
if ( v7 <= v10 )
break;
v8[2] = (unsigned int *)elf_hash();
++v9;
v8[1] = (unsigned int *)_byteswap_ulong(*(_DWORD *)(*(_QWORD *)(a1 + 112) + 4 * v9));
v6 = (unsigned int *)((char *)v10 + 1);
v8 += 3;
if ( v9 == v14 )
goto LABEL_23;
}
LABEL_22:
elf_errno = 40;
free(ptr);
return 0LL;
}

canonical_decompiled_code:
unsigned int **__fastcall func0(__int64 a1, char *a2)
{
char *v3; // r13
unsigned int *v4; // rbp
unsigned __int64 v5; // rbx
unsigned int *v6; // rdi
unsigned int *v7; // rbp
unsigned int **v8; // r14
__int64 v9; // r15
unsigned int *v10; // rbx
unsigned int **v11; // rax
unsigned int **ptr; // [rsp+8h] [rbp-70h]
__int64 v14; // [rsp+18h] [rbp-60h]
__int64 v15; // [rsp+20h] [rbp-58h]
char v16; // [rsp+38h] [rbp-40h] BYREF

v3 = a2;
if ( !a2 )
v3 = &v16;
*(_QWORD *)v3 = 0LL;
if ( !a1 )
return 0LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
{
elf_errno = 7;
return 0LL;
}
v4 = *(unsigned int **)(a1 + 112);
if ( !v4 || (*(_BYTE *)(a1 + 212) & 0x10) != 0 )
{
LABEL_24:
*(_QWORD *)v3 = *(_QWORD *)(a1 + 120);
return *(unsigned int ***)(a1 + 112);
}
v5 = *(_QWORD *)(a1 + 120);
if ( v5 <= 3 )
{
elf_errno = 40;
return 0LL;
}
v14 = _byteswap_ulong(*v4);
v15 = (unsigned int)v14 + 1LL;
if ( v5 < 4 * v15 )
{
elf_errno = 40;
return 0LL;
}
ptr = (unsigned int **)malloc(24 * v15);
if ( !ptr )
{
elf_errno = 60;
return ptr;
}
v6 = &v4[v15];
v7 = (unsigned int *)((char *)v4 + v5);
if ( !v14 )
{
LABEL_23:
v11 = &ptr[3 * v15 - 3];
*v11 = 0LL;
v11[2] = (unsigned int *)-1LL;
v11[1] = 0LL;
*(_QWORD *)(a1 + 112) = ptr;
*(_QWORD *)(a1 + 120) = v15;
*(_BYTE *)(a1 + 212) |= 0x10u;
goto LABEL_24;
}
v8 = ptr;
v9 = 0LL;
while ( 1 )
{
*v8 = v6;
if ( v7 <= v6 )
break;
v10 = v6;
while ( *(_BYTE *)v10 )
{
v10 = (unsigned int *)((char *)v10 + 1);
if ( v7 == v10 )
goto LABEL_22;
}
if ( v7 <= v10 )
break;
v8[2] = (unsigned int *)elf_hash();
++v9;
v8[1] = (unsigned int *)_byteswap_ulong(*(_DWORD *)(*(_QWORD *)(a1 + 112) + 4 * v9));
v6 = (unsigned int *)((char *)v10 + 1);
v8 += 3;
if ( v9 == v14 )
goto LABEL_23;
}
LABEL_22:
elf_errno = 40;
free(ptr);
return 0LL;
}

original_code:
Elf_Arsym*
elf_getarsym(Elf *elf, size_t *ptr) {
    Elf_Arsym *syms;
    size_t count;
    size_t tmp;
    size_t i;
    char *s;
    char *e;
    if (!ptr) {
 ptr = &tmp;
    }
    *ptr = 0;
    if (!elf) {
 return ((void *)0);
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_AR) {
 (_elf_errno = (ERROR_NOTARCHIVE));
 return ((void *)0);
    }
    if (elf->e_symtab && !elf->e_free_syms) {
 if (elf->e_symlen < 4) {
     (_elf_errno = (ERROR_SIZE_ARSYMTAB));
     return ((void *)0);
 }
 count = ((unsigned int) ((((unsigned int)((unsigned char*)(elf->e_symtab))[0])<<(24)) | (((unsigned int)((unsigned char*)(elf->e_symtab))[1])<<(16)) | (((unsigned int)((unsigned char*)(elf->e_symtab))[2])<<(8)) | (((unsigned int)((unsigned char*)(elf->e_symtab))[3])<<(0))));
 if (elf->e_symlen < 4 * (count + 1)) {
     (_elf_errno = (ERROR_SIZE_ARSYMTAB));
     return ((void *)0);
 }
 if (!(syms = (Elf_Arsym*)malloc((count + 1) * sizeof(*syms)))) {
     (_elf_errno = (ERROR_MEM_ARSYMTAB));
     return ((void *)0);
 }
 s = elf->e_symtab + 4 * (count + 1);
 e = elf->e_symtab + elf->e_symlen;
 for (i = 0; i < count; i++, s++) {
     syms[i].as_name = s;
     while (s < e && *s) {
  s++;
     }
     if (s >= e) {
  (_elf_errno = (ERROR_SIZE_ARSYMTAB));
  free(syms);
  return ((void *)0);
     }
     do{}while(0);
     syms[i].as_hash = elf_hash((unsigned char*)syms[i].as_name);
     syms[i].as_off = ((unsigned int) ((((unsigned int)((unsigned char*)(elf->e_symtab + 4 * (i + 1)))[0])<<(24)) | (((unsigned int)((unsigned char*)(elf->e_symtab + 4 * (i + 1)))[1])<<(16)) | (((unsigned int)((unsigned char*)(elf->e_symtab + 4 * (i + 1)))[2])<<(8)) | (((unsigned int)((unsigned char*)(elf->e_symtab + 4 * (i + 1)))[3])<<(0))));
 }
 syms[count].as_name = ((void *)0);
 syms[count].as_hash = ~0UL;
 syms[count].as_off = 0;
 elf->e_symtab = (char*)syms;
 elf->e_symlen = count + 1;
 elf->e_free_syms = 1;
    }
    *ptr = elf->e_symlen;
    return (Elf_Arsym*)elf->e_symtab;
}

canonical_original_code:
struct Elf_Arsym *elf_getarsym(struct Elf *elf, unsigned long *ptr) {
    struct Elf_Arsym *syms;
    unsigned long count;
    unsigned long tmp;
    unsigned long i;
    char *s;
    char *e;
    if (!ptr) {
 ptr = &tmp;
    }
    *ptr = 0;
    if (!elf) {
 return ((void *)0);
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_AR) {
 (_elf_errno = (ERROR_NOTARCHIVE));
 return ((void *)0);
    }
    if (elf->e_symtab && !elf->e_free_syms) {
 if (elf->e_symlen < 4) {
     (_elf_errno = (ERROR_SIZE_ARSYMTAB));
     return ((void *)0);
 }
 count = ((unsigned int) ((((unsigned int)((unsigned char *)(elf->e_symtab))[0])<<(24)) | (((unsigned int)((unsigned char *)(elf->e_symtab))[1])<<(16)) | (((unsigned int)((unsigned char *)(elf->e_symtab))[2])<<(8)) | (((unsigned int)((unsigned char *)(elf->e_symtab))[3])<<(0))));
 if (elf->e_symlen < 4 * (count + 1)) {
     (_elf_errno = (ERROR_SIZE_ARSYMTAB));
     return ((void *)0);
 }
 if (!(syms = (struct Elf_Arsym *)malloc((count + 1) * sizeof(*syms)))) {
     (_elf_errno = (ERROR_MEM_ARSYMTAB));
     return ((void *)0);
 }
 s = elf->e_symtab + 4 * (count + 1);
 e = elf->e_symtab + elf->e_symlen;
 for (i = 0; i < count; i++, s++) {
     syms[i].as_name = s;
     while (s < e && *s) {
  s++;
     }
     if (s >= e) {
  (_elf_errno = (ERROR_SIZE_ARSYMTAB));
  free(syms);
  return ((void *)0);
     }
     do{}while(0);
     syms[i].as_hash = elf_hash((unsigned char *)syms[i].as_name);
     syms[i].as_off = ((unsigned int) ((((unsigned int)((unsigned char *)(elf->e_symtab + 4 * (i + 1)))[0])<<(24)) | (((unsigned int)((unsigned char *)(elf->e_symtab + 4 * (i + 1)))[1])<<(16)) | (((unsigned int)((unsigned char *)(elf->e_symtab + 4 * (i + 1)))[2])<<(8)) | (((unsigned int)((unsigned char *)(elf->e_symtab + 4 * (i + 1)))[3])<<(0))));
 }
 syms[count].as_name = ((void *)0);
 syms[count].as_hash = ~0UL;
 syms[count].as_off = 0;
 elf->e_symtab = (char *)syms;
 elf->e_symlen = count + 1;
 elf->e_free_syms = 1;
    }
    *ptr = elf->e_symlen;
    return (struct Elf_Arsym *)elf->e_symtab;
}

variable_types:
ptr
unsigned long *

variable_types: struct Elf_Arsym { char *as_name; unsigned long as_off; unsigned long as_hash; } *

variable_types:
tmp
unsigned long

variable_types:
i
unsigned long

variable_types:
s
char *

variable_types:
e
char *


original_code:
char*
elf_strptr(Elf *elf, size_t section, size_t offset) {
    Elf_Data *data;
    Elf_Scn *scn;
    size_t n;
    char *s;
    if (!elf) {
 return ((void *)0);
    }
    do{}while(0);
    if (!(scn = elf_getscn(elf, section))) {
 return ((void *)0);
    }
    if (scn->s_index == 0) {
 (_elf_errno = (ERROR_NOSTRTAB));
 return ((void *)0);
    }
    if (elf->e_class == 1) {
 if (scn->s_uhdr.u_shdr32.sh_type != 3) {
     (_elf_errno = (ERROR_NOSTRTAB));
     return ((void *)0);
 }
    }
    else if (elf->e_class == 2) {
 if (scn->s_uhdr.u_shdr64.sh_type != 3) {
     (_elf_errno = (ERROR_NOSTRTAB));
     return ((void *)0);
 }
    }
    else if (((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
 (_elf_errno = (ERROR_UNIMPLEMENTED));
 return ((void *)0);
    }
    else {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return ((void *)0);
    }
    n = 0;
    data = ((void *)0);
    if (elf->e_elf_flags & 0x4) {
 while ((data = elf_getdata(scn, data))) {
     n = data->d_off;
     if (offset >= n && offset - n < data->d_size) {
  break;
     }
 }
    }
    else {
 while ((data = elf_getdata(scn, data))) {
     if (data->d_align > 1) {
  n += data->d_align - 1;
  n -= n % data->d_align;
     }
     if (offset < n) {
  (_elf_errno = (ERROR_BADSTROFF));
  return ((void *)0);
     }
     if (offset - n < data->d_size) {
  break;
     }
     n += data->d_size;
 }
    }
    if (data == ((void *)0)) {
 (_elf_errno = (ERROR_BADSTROFF));
 return ((void *)0);
    }
    if (data->d_buf == ((void *)0)) {
 (_elf_errno = (ERROR_NULLBUF));
 return ((void *)0);
    }
    offset -= n;
    s = (char*)data->d_buf;
    if (!(_elf_sanity_checks & (1u << 0))) {
 return s + offset;
    }
    for (n = offset; n < data->d_size; n++) {
 if (s[n] == '\0') {
     return s + offset;
 }
    }
    (_elf_errno = (ERROR_UNTERM));
    return ((void *)0);
}

canonical_original_code:
char *elf_strptr(struct Elf *elf, unsigned long section, unsigned long offset) {
    struct Elf_Data *data;
    struct Elf_Scn *scn;
    unsigned long n;
    char *s;
    if (!elf) {
 return ((void *)0);
    }
    do{}while(0);
    if (!(scn = elf_getscn(elf, section))) {
 return ((void *)0);
    }
    if (scn->s_index == 0) {
 (_elf_errno = (ERROR_NOSTRTAB));
 return ((void *)0);
    }
    if (elf->e_class == 1) {
 if (scn->s_uhdr.u_shdr32.sh_type != 3) {
     (_elf_errno = (ERROR_NOSTRTAB));
     return ((void *)0);
 }
    }
    else if (elf->e_class == 2) {
 if (scn->s_uhdr.u_shdr64.sh_type != 3) {
     (_elf_errno = (ERROR_NOSTRTAB));
     return ((void *)0);
 }
    }
    else if (((elf->e_class) >= 1 && (elf->e_class) <= 2)) {
 (_elf_errno = (ERROR_UNIMPLEMENTED));
 return ((void *)0);
    }
    else {
 (_elf_errno = (ERROR_UNKNOWN_CLASS));
 return ((void *)0);
    }
    n = 0;
    data = ((void *)0);
    if (elf->e_elf_flags & 0x4) {
 while ((data = elf_getdata(scn, data))) {
     n = data->d_off;
     if (offset >= n && offset - n < data->d_size) {
  break;
     }
 }
    }
    else {
 while ((data = elf_getdata(scn, data))) {
     if (data->d_align > 1) {
  n += data->d_align - 1;
  n -= n % data->d_align;
     }
     if (offset < n) {
  (_elf_errno = (ERROR_BADSTROFF));
  return ((void *)0);
     }
     if (offset - n < data->d_size) {
  break;
     }
     n += data->d_size;
 }
    }
    if (data == ((void *)0)) {
 (_elf_errno = (ERROR_BADSTROFF));
 return ((void *)0);
    }
    if (data->d_buf == ((void *)0)) {
 (_elf_errno = (ERROR_NULLBUF));
 return ((void *)0);
    }
    offset -= n;
    s = (char *)data->d_buf;
    if (!(_elf_sanity_checks & (1u << 0))) {
 return s + offset;
    }
    for (n = offset; n < data->d_size; n++) {
 if (s[n] == '\0') {
     return s + offset;
 }
    }
    (_elf_errno = (ERROR_UNTERM));
    return ((void *)0);
}

variable_types:
section
unsigned long

variable_types: struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } *

variable_types: struct Elf_Scn { struct Elf_Scn *s_link; struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 } e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; } *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; } *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data { struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } sd_data; struct Scn_Data *sd_link; struct Elf_Scn *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; } *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } u_shdr64; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } u_shdr32; } s_uhdr; long s_magic; } *

return_type: char *


decompiled_code:
__int64 __fastcall elf_version(int a1)
{
signed int v1; // eax
__int64 result; // rax

v1 = (unsigned int)getenv("LIBELF_SANITY_CHECKS");
if ( v1 )
elf_sanity_checks = strtol((const char *)v1, 0LL, 0);
result = 1LL;
if ( a1 )
{
if ( a1 == 1 )
{
result = 1LL;
if ( elf_version )
result = elf_version;
elf_version = 1;
}
else
{
elf_errno = 9;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
signed int v1; // eax
__int64 result; // rax

v1 = (unsigned int)getenv("LIBELF_SANITY_CHECKS");
if ( v1 )
elf_sanity_checks = strtol((const char *)v1, 0LL, 0);
result = 1LL;
if ( a1 )
{
if ( a1 == 1 )
{
result = 1LL;
if ( elf_version )
result = elf_version;
elf_version = 1;
}
else
{
elf_errno = 9;
result = 0LL;
}
}
return result;
}

original_code:
unsigned
elf_version(unsigned ver) {
    const char *s;
    unsigned tmp;
    if ((s = getenv("LIBELF_SANITY_CHECKS"))) {
 _elf_sanity_checks = (int)strtol(s, (char**)((void *)0), 0);
    }
    if (ver == 0) {
 return 1;
    }
    if (!((ver) > 0 && (ver) <= 1)) {
 (_elf_errno = (ERROR_UNKNOWN_VERSION));
 return 0;
    }
    tmp = _elf_version == 0 ? 1 : _elf_version;
    _elf_version = ver;
    return tmp;
}

canonical_original_code:
unsigned int elf_version(unsigned int ver) {
    char *s;
    unsigned int tmp;
    if ((s = getenv("LIBELF_SANITY_CHECKS"))) {
 _elf_sanity_checks = (int)strtol(s, (char **)((void *)0), 0);
    }
    if (ver == 0) {
 return 1;
    }
    if (!((ver) > 0 && (ver) <= 1)) {
 (_elf_errno = (ERROR_UNKNOWN_VERSION));
 return 0;
    }
    tmp = _elf_version == 0 ? 1 : _elf_version;
    _elf_version = ver;
    return tmp;
}

variable_types:
ver
unsigned int

variable_types:
tmp
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall elf32_getehdr(__int64 a1)
{
return elf_getehdr(a1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return func0(a1, 1);
}

original_code:
Elf32_Ehdr*
elf32_getehdr(Elf *elf) {
    return (Elf32_Ehdr*)_elf_getehdr(elf, 1);
}

canonical_original_code:
struct Elf32_Ehdr *elf32_getehdr(struct Elf *elf) {
    return (struct Elf32_Ehdr *)_elf_getehdr(elf, 1);
}

variable_types: struct Elf { unsigned long e_size; unsigned long e_dsize; enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 } e_kind; char *e_data; char *e_rawdata; unsigned long e_idlen; int e_fd; unsigned int e_count; struct Elf *e_parent; unsigned long e_next; unsigned long e_base; struct Elf *e_link; struct Elf_Arhdr { char *ar_name; long ar_date; long ar_uid; long ar_gid; unsigned long ar_mode; long ar_size; char *ar_rawname; } *e_arhdr; unsigned long e_off; struct Elf *e_members; char *e_symtab; unsigned long e_symlen; char *e_strtab; unsigned long e_strlen; unsigned int e_class; unsigned int e_encoding; unsigned int e_version; char *e_ehdr; char *e_phdr; unsigned long e_phnum; struct Elf_Scn { struct Elf_Scn *s_link; struct Elf *s_elf; unsigned long s_index; unsigned int s_scn_flags; unsigned int s_shdr_flags; struct Scn_Data { struct Elf_Data { void *d_buf; enum Elf_Type { ELF_T_BYTE=0, ELF_T_ADDR=1, ELF_T_DYN=2, ELF_T_EHDR=3, ELF_T_HALF=4, ELF_T_OFF=5, ELF_T_PHDR=6, ELF_T_RELA=7, ELF_T_REL=8, ELF_T_SHDR=9, ELF_T_SWORD=10, ELF_T_SYM=11, ELF_T_WORD=12, ELF_T_SXWORD=13, ELF_T_XWORD=14, ELF_T_VDEF=15, ELF_T_VNEED=16, ELF_T_NUM=17 } d_type; unsigned long d_size; long d_off; unsigned long d_align; unsigned int d_version; } sd_data; struct Scn_Data *sd_link; struct Elf_Scn *sd_scn; char *sd_memdata; unsigned int sd_data_flags; unsigned int sd_freeme; unsigned int sd_free_data; long sd_magic; } *s_data_1; struct Scn_Data *s_data_n; struct Scn_Data *s_rawdata; unsigned int s_type; unsigned long s_offset; unsigned long s_size; unsigned int s_freeme; union { struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } u_shdr64; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } u_shdr32; } s_uhdr; long s_magic; } *e_scn_1; struct Elf_Scn *e_scn_n; unsigned int e_elf_flags; unsigned int e_ehdr_flags; unsigned int e_phdr_flags; unsigned int e_readable; unsigned int e_writable; unsigned int e_disabled; unsigned int e_cooked; unsigned int e_free_syms; unsigned int e_unmap_data; unsigned int e_memory; long e_magic; } *

user_defined_types: struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }


decompiled_code:
__int64 __fastcall elf64_getehdr(__int64 a1)
{
return elf_getehdr(a1, 2);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return func0(a1, 2);
}

original_code:
Elf64_Ehdr*
elf64_getehdr(Elf *elf) {
    return (Elf64_Ehdr*)_elf_getehdr(elf, 2);
}

canonical_original_code:
struct Elf64_Ehdr *elf64_getehdr(struct Elf *elf) {
    return (struct Elf64_Ehdr *)_elf_getehdr(elf, 2);
}

user_defined_types: struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }


decompiled_code:
__int64 __fastcall elf_kind(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = *(unsigned int *)(a1 + 16);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax

if ( a1 )
result = *(unsigned int *)(a1 + 16);
else
result = 0LL;
return result;
}

original_code:
Elf_Kind
elf_kind(Elf *elf) {
    if (!elf) {
 return ELF_K_NONE;
    }
    do{}while(0);
    return elf->e_kind;
}

canonical_original_code:
enum Elf_Kind elf_kind(struct Elf *elf) {
    if (!elf) {
 return ELF_K_NONE;
    }
    do{}while(0);
    return elf->e_kind;
}

return_type: enum Elf_Kind { ELF_K_NONE=0, ELF_K_AR=1, ELF_K_COFF=2, ELF_K_ELF=3, ELF_K_NUM=4 }


original_code:
size_t
elfx_movscn(Elf *elf, Elf_Scn *scn, Elf_Scn *after) {
    Elf_Scn *prev;
    Elf_Scn *tmp;
    int off;
    if (!elf || !scn || !after) {
 return 0;
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_ELF) {
 (_elf_errno = (ERROR_NOTELF));
 return 0;
    }
    do{}while(0);
    do{}while(0);
    if (scn->s_elf != elf || after->s_elf != elf) {
 (_elf_errno = (ERROR_ELFSCNMISMATCH));
 return 0;
    }
    do{}while(0);
    if (scn == elf->e_scn_1) {
 (_elf_errno = (ERROR_NULLSCN));
 return 0;
    }
    if (scn == after || scn == after->s_link) {
 return scn->s_index;
    }
    prev = ((void *)0);
    for (tmp = elf->e_scn_1; tmp->s_link; tmp = tmp->s_link) {
 if (tmp->s_link == scn) {
     prev = tmp;
     break;
 }
    }
    do{}while(0);
    off = 0;
    for (tmp = elf->e_scn_1; tmp; tmp = tmp->s_link) {
 if (off) {
     tmp->s_index += off;
 }
 if (tmp == after) {
     off++;
 }
 else if (tmp == scn) {
     off--;
 }
    }
    do{}while(0);
    prev->s_link = scn->s_link;
    scn->s_link = after->s_link;
    after->s_link = scn;
    scn->s_index = after->s_index + 1;
    if (elf->e_scn_n == scn) {
 elf->e_scn_n = prev;
    }
    else if (elf->e_scn_n == after) {
 elf->e_scn_n = scn;
    }
    return scn->s_index;
}

canonical_original_code:
unsigned long elfx_movscn(struct Elf *elf, struct Elf_Scn *scn, struct Elf_Scn *after) {
    struct Elf_Scn *prev;
    struct Elf_Scn *tmp;
    int off;
    if (!elf || !scn || !after) {
 return 0;
    }
    do{}while(0);
    if (elf->e_kind != ELF_K_ELF) {
 (_elf_errno = (ERROR_NOTELF));
 return 0;
    }
    do{}while(0);
    do{}while(0);
    if (scn->s_elf != elf || after->s_elf != elf) {
 (_elf_errno = (ERROR_ELFSCNMISMATCH));
 return 0;
    }
    do{}while(0);
    if (scn == elf->e_scn_1) {
 (_elf_errno = (ERROR_NULLSCN));
 return 0;
    }
    if (scn == after || scn == after->s_link) {
 return scn->s_index;
    }
    prev = ((void *)0);
    for (tmp = elf->e_scn_1; tmp->s_link; tmp = tmp->s_link) {
 if (tmp->s_link == scn) {
     prev = tmp;
     break;
 }
    }
    do{}while(0);
    off = 0;
    for (tmp = elf->e_scn_1; tmp; tmp = tmp->s_link) {
 if (off) {
     tmp->s_index += off;
 }
 if (tmp == after) {
     off++;
 }
 else if (tmp == scn) {
     off--;
 }
    }
    do{}while(0);
    prev->s_link = scn->s_link;
    scn->s_link = after->s_link;
    after->s_link = scn;
    scn->s_index = after->s_index + 1;
    if (elf->e_scn_n == scn) {
 elf->e_scn_n = prev;
    }
    else if (elf->e_scn_n == after) {
 elf->e_scn_n = scn;
    }
    return scn->s_index;
}

variable_types:
scn
struct Elf_Scn *

variable_types:
after
struct Elf_Scn *

variable_types: off int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int i; // ebx
int v4; // ebx
unsigned int v5; // er12
int v6; // ebx
char s[6]; // [rsp+7h] [rbp-21h] BYREF
char ptr[27]; // [rsp+Dh] [rbp-1Bh] BYREF

if ( argc <= 0 )
{
puts("ERROR: USAGE: COMMAND <input file> <output file(*.coe)>\n");
exit(1);
}
fi = fopen(argv[1], "rb");
if ( !fi )
{
fwrite("ERROR: cannot open input file\n\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
fp = fopen("/Users/toku1938/Work/Convert2Coe/src/header.txt", "rb");
if ( !fp )
{
fwrite("ERROR: cannot find header file\n\n", 1uLL, 0x20uLL, stderr);
exit(1);
}
ft = fopen("test.coe", "wb");
if ( !ft )
{
fwrite("ERROR: cannot open input file\n\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
for ( i = 0; i != 3; i += buf == 10 )
{
if ( !fread(&buf, 1uLL, 1uLL, fi) )
break;
printf("%02x", (unsigned __int8)buf);
}
while ( fread(&data, 1uLL, 1uLL, fp) == 1 )
{
if ( fwrite(&data, 1uLL, 1uLL, ft) != 1 )
{
fwrite("ERROR: cannot write header file to output file\n\n", 1uLL, 0x30uLL, stderr);
exit(1);
}
}
fclose(fp);
while ( fread(ptr, 1uLL, 3uLL, fi) == 3 )
{
sprintf(s, "%x%x%x", (unsigned __int8)ptr[0] >> 4, (unsigned __int8)ptr[1] >> 4, (unsigned __int8)ptr[2] >> 4);
v4 = strlen(s);
v5 = fwrite(s, 1uLL, v4, ft);
if ( v4 != v5 )
{
fwrite("ERROR: cannot write the data to output file\n\n", 1uLL, 0x2DuLL, stderr);
printf("fwrite returns %d\n", v5);
exit(1);
}
fwrite(",\n", 1uLL, 2uLL, ft);
}
fseek(ft, -2LL, 2);
fwrite(";\n", 1uLL, 2uLL, ft);
v6 = ftell(ft);
fclose(ft);
ft = fopen("test.coe", "rb");
if ( !ft )
{
fwrite("ERROR: cannot open output file\n\n", 1uLL, 0x20uLL, stderr);
exit(1);
}
fo = fopen(argv[2], "wb");
if ( !fo )
{
fwrite("ERROR: cannot open output file\n\n", 1uLL, 0x20uLL, stderr);
exit(1);
}
do
{
if ( fread(&data, 1uLL, 1uLL, ft) != 1 )
break;
if ( fwrite(&data, 1uLL, 1uLL, fo) != 1 )
{
fwrite("ERROR: cannot write the data to output file\n\n", 1uLL, 0x2DuLL, stderr);
exit(1);
}
}
while ( v6 != (unsigned int)ftell(fo) );
fclose(fi);
fclose(ft);
remove("test.coe");
fclose(fo);
return 0;
}

original_code:
int main(int argc, char** argv)
{
    int i,index;
    char *p,foo[]="\n";
    index = 0;
    i = 0;
    if(argc < 1){
 printf("ERROR: USAGE: COMMAND <input file> <output file(*.coe)>\n\n");
 exit(1);
    }
    if((fi = fopen(argv[1],"rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open input file\n\n");
 exit(1);
    }
    if((fp = fopen("/Users/toku1938/Work/Convert2Coe/src/header.txt","rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot find header file\n\n");
 exit(1);
    }
    if((ft = fopen("test.coe","wb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open input file\n\n");
 exit(1);
    }
    while(i= fread(buf,sizeof(unsigned char),1,fi) != 0){
 p = buf;
 do{
     printf("%02x", *p & 0xff);
     if(memcmp(p,foo,1)==0) index++;
     *p++;
 } while(--i);
 if(index==3)break;
    }
    while(fread(&data,sizeof(unsigned char),1,fp) == 1){
 if(fwrite(&data,sizeof(unsigned char),1,ft) != 1){
     fprintf(stderr,"ERROR: cannot write header file to output file\n\n");
     exit(1);
 }
    }
    fclose(fp);
    unsigned char data1[3];
    while(fread(&data1,sizeof(unsigned char),3,fi)==3){
 char a[6];
 int t,len;
 sprintf(a,"%x%x%x", data1[0]/16,data1[1]/16,data1[2]/16);
 len = strlen(a);
 if((t=fwrite(&a,sizeof(char),len,ft))!=len){
     fprintf(stderr,"ERROR: cannot write the data to output file\n\n");
     printf("fwrite returns %d\n",t);
     exit(1);
 }
 fputs(",\n",ft);
    }
    fseek(ft,-2L,2);
    fputs(";\n",ft);
    int loc = ftell(ft);
    fclose(ft);
    if((ft = fopen("test.coe","rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open output file\n\n");
 exit(1);
    }
    if((fo = fopen(argv[2],"wb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open output file\n\n");
 exit(1);
    }
    while(fread(&data,sizeof(unsigned char),1,ft) == 1){
 int loc_t;
 if(fwrite(&data,sizeof(unsigned char),1,fo)!=1){
     fprintf(stderr,"ERROR: cannot write the data to output file\n\n");
     exit(1);
 }
 if((loc_t=ftell(fo)) == loc) break;
    }
    fclose(fi);
    fclose(ft);
    remove("test.coe");
    fclose(fo);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int index;
    char *p; char foo[] = "\n";
    index = 0;
    i = 0;
    if(argc < 1){
 printf("ERROR: USAGE: COMMAND <input file> <output file(*.coe)>\n\n");
 exit(1);
    }
    if((fi = fopen(argv[1],"rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open input file\n\n");
 exit(1);
    }
    if((fp = fopen("/Users/toku1938/Work/Convert2Coe/src/header.txt","rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot find header file\n\n");
 exit(1);
    }
    if((ft = fopen("test.coe","wb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open input file\n\n");
 exit(1);
    }
    while(i= fread(buf,sizeof(unsigned char),1,fi) != 0){
 p = buf;
 do{
     printf("%02x", *p & 0xff);
     if(memcmp(p,foo,1)==0) index++;
     *p++;
 } while(--i);
 if(index==3)break;
    }
    while(fread(&data,sizeof(unsigned char),1,fp) == 1){
 if(fwrite(&data,sizeof(unsigned char),1,ft) != 1){
     fprintf(stderr,"ERROR: cannot write header file to output file\n\n");
     exit(1);
 }
    }
    fclose(fp);
    unsigned char data1[3];
    while(fread(&data1,sizeof(unsigned char),3,fi)==3){
 char a[6];
 int t; int len;
 sprintf(a,"%x%x%x", data1[0]/16,data1[1]/16,data1[2]/16);
 len = strlen(a);
 if((t=fwrite(&a,sizeof(char),len,ft))!=len){
     fprintf(stderr,"ERROR: cannot write the data to output file\n\n");
     printf("fwrite returns %d\n",t);
     exit(1);
 }
 fputs(",\n",ft);
    }
    fseek(ft,-2L,2);
    fputs(";\n",ft);
    int loc = ftell(ft);
    fclose(ft);
    if((ft = fopen("test.coe","rb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open output file\n\n");
 exit(1);
    }
    if((fo = fopen(argv[2],"wb")) == ((void *)0)){
 fprintf(stderr,"ERROR: cannot open output file\n\n");
 exit(1);
    }
    while(fread(&data,sizeof(unsigned char),1,ft) == 1){
 int loc_t;
 if(fwrite(&data,sizeof(unsigned char),1,fo)!=1){
     fprintf(stderr,"ERROR: cannot write the data to output file\n\n");
     exit(1);
 }
 if((loc_t=ftell(fo)) == loc) break;
    }
    fclose(fi);
    fclose(ft);
    remove("test.coe");
    fclose(fo);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: index int

variable_types:
p
char *

variable_types:
foo
char []

variable_types:
a
char [6]

variable_types: t int

variable_types: len int

variable_types: loc int


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
int v4; // ebx

if ( argc <= 1 )
{
v4 = 4000;
}
else
{
if ( !(unsigned int)is_number(argv[1]) )
{
log(v3);
exit(1);
}
v4 = atoi(argv[1]);
if ( v4 <= 1024 )
{
log(v3);
exit(1);
}
}
gettimeofday(&last_time, 0LL);
current_time = last_time.tv_sec;
create_host((unsigned int)v4);
while ( 1 )
{
mudsleep(1LL);
gettimeofday(&last_time, 0LL);
current_time = last_time.tv_sec;
check_connections();
}
}

original_code:
int main(int argc, char **argv) {
 int port = 4000;
 if( argc > 1 )
 {
  if( !is_number(argv[1]) )
  {
   log("Invalid port number!");
   exit(1);
  }
  else if( (port = atoi(argv[1])) <= 1024 )
  {
   log("Port must be above 1024!");
   exit(1);
  }
 }
 gettimeofday( &last_time, 0 );
 current_time = (time_t)last_time.tv_sec;
 create_host(port);
 while(1) {
  mudsleep(1);
  gettimeofday( &last_time, 0 );
  current_time = (time_t)last_time.tv_sec;
  check_connections();
 }
 return 1;
}

canonical_original_code:
int main(int argc, char **argv) {
 int port = 4000;
 if( argc > 1 )
 {
  if( !is_number(argv[1]) )
  {
   log("Invalid port number!");
   exit(1);
  }
  else if( (port = atoi(argv[1])) <= 1024 )
  {
   log("Port must be above 1024!");
   exit(1);
  }
 }
 gettimeofday( &last_time, 0 );
 current_time = (long)last_time.tv_sec;
 create_host(port);
 while(1) {
  mudsleep(1);
  gettimeofday( &last_time, 0 );
  current_time = (long)last_time.tv_sec;
  check_connections();
 }
 return 1;
}

variable_types: port int


original_code:
void init_socket(SOCKET_T *sock) {
 sock->inbuf[0] = '\0';
 sock->outbuf[0] = '\0';
 sock->connected = 2;
}

canonical_original_code:
void init_socket(struct socket_t *sock) {
 sock->inbuf[0] = '\0';
 sock->outbuf[0] = '\0';
 sock->connected = 2;
}

variable_types: struct socket_t { struct socket_t *next; struct socket_t *prev; struct socket_t *reply; char inbuf[4096]; char outbuf[4096]; char *name; char *password; char *host; int desc; int connected; } *

return_type: void


decompiled_code:
__int64 __fastcall free_socket(__int64 a1)
{
_QWORD *v1; // rax
__int64 result; // rax
__int64 v3; // rax

*(_DWORD *)(a1 + 8244) = 0;
close(*(_DWORD *)(a1 + 8240));
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
{
*v1 = *(_QWORD *)a1;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
v3 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
{
socket_list = *(_QWORD *)a1;
*(_QWORD *)(v3 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
socket_list = 0LL;
}
}
result = socket_free;
if ( socket_free )
{
*(_QWORD *)(socket_free + 8) = a1;
*(_QWORD *)a1 = result;
}
socket_free = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
_QWORD *v1; // rax
__int64 result; // rax
__int64 v3; // rax

*(_DWORD *)(a1 + 8244) = 0;
close(*(_DWORD *)(a1 + 8240));
v1 = *(_QWORD **)(a1 + 8);
if ( v1 )
{
*v1 = *(_QWORD *)a1;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
v3 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 )
{
socket_list = *(_QWORD *)a1;
*(_QWORD *)(v3 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
socket_list = 0LL;
}
}
result = socket_free;
if ( socket_free )
{
*(_QWORD *)(socket_free + 8) = a1;
*(_QWORD *)a1 = result;
}
socket_free = a1;
return result;
}

original_code:
void free_socket(SOCKET_T *sock) {
 sock->connected = 0;
 close(sock->desc);
 if( sock->prev == 0 ) { if( sock->next == 0 ) { socket_list = 0; } else { socket_list = sock->next; socket_list->prev = 0; sock->next = 0; } } else { sock->prev->next = sock->next; sock->next = sock->prev = 0; };
 if( socket_free != 0 ) { socket_free->prev = sock; sock->next = socket_free; socket_free = sock; } else { socket_free = sock; };
}

canonical_original_code:
void free_socket(struct socket_t *sock) {
 sock->connected = 0;
 close(sock->desc);
 if( sock->prev == 0 ) { if( sock->next == 0 ) { socket_list = 0; } else { socket_list = sock->next; socket_list->prev = 0; sock->next = 0; } } else { sock->prev->next = sock->next; sock->next = sock->prev = 0; };
 if( socket_free != 0 ) { socket_free->prev = sock; sock->next = socket_free; socket_free = sock; } else { socket_free = sock; };
}

user_defined_types: struct socket_t { struct socket_t *next; struct socket_t *prev; struct socket_t *reply; char inbuf[4096]; char outbuf[4096]; char *name; char *password; char *host; int desc; int connected; }


decompiled_code:
double __fastcall get_address(__int64 a1, __int64 a2, double a3)
{
unsigned int v3; // eax
struct hostent *v4; // rax
socklen_t len; // [rsp+Ch] [rbp-101Ch] BYREF
char s[4120]; // [rsp+10h] [rbp-1018h] BYREF

len = 16;
getpeername(*(_DWORD *)(a1 + 8240), (struct sockaddr *)a2, &len);
v3 = _bswap_32(*(_DWORD *)(a2 + 4));
sprintf(s, "%d.%d.%d.%d", HIBYTE(v3), BYTE2(v3), BYTE1(v3), (unsigned __int8)v3);
v4 = gethostbyaddr((const void *)(a2 + 4), 4u, 2);
if ( v4 )
str_dup(a1 + 8232, v4->h_name);
else
str_dup(a1 + 8232, s);
sprintf(s, "Socket connected from %s", *(const char **)(a1 + 8232));
return log(a3);
}

canonical_decompiled_code:
double __fastcall func5(__int64 a1, __int64 a2, double a3)
{
unsigned int v3; // eax
struct hostent *v4; // rax
socklen_t len; // [rsp+Ch] [rbp-101Ch] BYREF
char s[4120]; // [rsp+10h] [rbp-1018h] BYREF

len = 16;
getpeername(*(_DWORD *)(a1 + 8240), (struct sockaddr *)a2, &len);
v3 = func0(*(_DWORD *)(a2 + 4));
sprintf(s, "%d.%d.%d.%d", HIBYTE(v3), BYTE2(v3), BYTE1(v3), (unsigned __int8)v3);
v4 = gethostbyaddr((const void *)(a2 + 4), 4u, 2);
if ( v4 )
str_dup(a1 + 8232, v4->h_name);
else
str_dup(a1 + 8232, s);
sprintf(s, "Socket connected from %s", *(const char **)(a1 + 8232));
return log(a3);
}

original_code:
void get_address(SOCKET_T *sock, struct sockaddr_in *sockad) {
 char buf[4096];
 struct hostent *from;
 int addr;
 int size = sizeof(struct sockaddr_in);
 getpeername(sock->desc, (struct sockaddr*)sockad, &size);
 addr = ntohl(sockad->sin_addr.s_addr);
 sprintf( buf, "%d.%d.%d.%d",
  (addr >> 24) & 0xFF, (addr >> 16) & 0xFF,
  (addr >> 8) & 0xFF, (addr) & 0xFF );
 from = gethostbyaddr((char*)&sockad->sin_addr,sizeof(sockad->sin_addr), 2);
 if(from)
  str_dup(&sock->host,from->h_name);
 else
  str_dup(&sock->host,buf);
 sprintf(buf,"Socket connected from %s",sock->host);
 log(buf);
}

canonical_original_code:
void get_address(struct socket_t *sock, struct sockaddr_in *sockad) {
 char buf[4096];
 struct hostent *from;
 int addr;
 int size = sizeof(struct sockaddr_in);
 getpeername(sock->desc, (struct sockaddr *)sockad, &size);
 addr = ntohl(sockad->sin_addr.s_addr);
 sprintf( buf, "%d.%d.%d.%d",
  (addr >> 24) & 0xFF, (addr >> 16) & 0xFF,
  (addr >> 8) & 0xFF, (addr) & 0xFF );
 from = gethostbyaddr((char *)&sockad->sin_addr,sizeof(sockad->sin_addr), 2);
 if(from)
  str_dup(&sock->host,from->h_name);
 else
  str_dup(&sock->host,buf);
 sprintf(buf,"Socket connected from %s",sock->host);
 log(buf);
}

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]; } *

variable_types:
buf
char [4096]

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

variable_types: addr int

variable_types: size int

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:
__int64 __fastcall create_host(unsigned int a1, double a2)
{
int v2; // eax
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-128h] BYREF
char s[280]; // [rsp+10h] [rbp-118h] BYREF

v2 = socket(2, 1, 0);
host = v2;
if ( v2 < 0 )
{
perror("create_host: failed to create host socket");
exit(1);
}
v3 = v2;
v5.sa_family = 2;
*(_WORD *)v5.sa_data = _bswap_16(a1);
*(_DWORD *)&v5.sa_data[2] = _bswap_32(0);
if ( bind(v3, &v5, 0x10u) < 0 )
{
perror("create_host: bind failed");
exit(1);
}
if ( listen(host, 5) < 0 )
{
perror("create_host: listen failed");
exit(1);
}
sprintf(s, "Host socket listening on port %d, file descriptor %d.\n", a1, (unsigned int)host);
log(a2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, double a2)
{
int v2; // eax
int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-128h] BYREF
char s[280]; // [rsp+10h] [rbp-118h] BYREF

v2 = socket(2, 1, 0);
host = v2;
if ( v2 < 0 )
{
perror("create_host: failed to create host socket");
exit(1);
}
v3 = v2;
v5.sa_family = 2;
*(_WORD *)v5.sa_data = func1(a1);
*(_DWORD *)&v5.sa_data[2] = func0(0);
if ( bind(v3, &v5, 0x10u) < 0 )
{
perror("create_host: bind failed");
exit(1);
}
if ( listen(host, 5) < 0 )
{
perror("create_host: listen failed");
exit(1);
}
sprintf(s, "Host socket listening on port %d, file descriptor %d.\n", a1, (unsigned int)host);
log(a2);
return 1LL;
}

original_code:
int create_host(int port) {
 char buf[256];
 struct sockaddr_in sa;
 if((host = socket(2, SOCK_STREAM, 0)) < 0) {
  perror("create_host: failed to create host socket");
  exit(1);
 }
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 sa.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 if(bind(host, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
  perror("create_host: bind failed");
  exit(1);
 }
 if(listen(host, 5) < 0) {
  perror("create_host: listen failed");
  exit(1);
 }
 sprintf(buf,"Host socket listening on port %d, file descriptor %d.\n",port,host);
 log(buf);
 return 1;
}

canonical_original_code:
int create_host(int port) {
 char buf[256];
 struct sockaddr_in sa;
 if((host = socket(2, SOCK_STREAM, 0)) < 0) {
  perror("create_host: failed to create host socket");
  exit(1);
 }
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 sa.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 if(bind(host, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  perror("create_host: bind failed");
  exit(1);
 }
 if(listen(host, 5) < 0) {
  perror("create_host: listen failed");
  exit(1);
 }
 sprintf(buf,"Host socket listening on port %d, file descriptor %d.\n",port,host);
 log(buf);
 return 1;
}

variable_types:
buf
char [256]

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]; }


decompiled_code:
__int64 __fastcall new_socket(double a1)
{
int v1; // edi
__int64 result; // rax
int v3; // edx
_DWORD *v4; // rbx
__int64 v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF
socklen_t addr_len; // [rsp+18h] [rbp-10h] BYREF
socklen_t len[3]; // [rsp+1Ch] [rbp-Ch] BYREF

len[0] = 16;
getsockname(host, &v6, len);
v1 = accept(host, &v6, &addr_len);
len[0] = v1;
result = 0LL;
if ( v1 >= 0 )
{
v3 = fcntl(v1, 4, 2048LL);
result = 0LL;
if ( v3 != -1 )
{
v4 = (_DWORD *)socket_free;
if ( socket_free )
{
socket_free = *(_QWORD *)socket_free;
*(_QWORD *)v4 = 0LL;
}
else
{
v4 = malloc(0x2038uLL);
memset(v4, 0, 0x2038uLL);
}
init_socket((__int64)v4);
v4[2060] = len[0];
v5 = socket_list;
if ( socket_list )
{
*(_QWORD *)(socket_list + 8) = v4;
*(_QWORD *)v4 = v5;
}
socket_list = (__int64)v4;
send_to((__int64)v4, welcome_message);
send_to((__int64)v4, "What is your name? ");
get_address((__int64)v4, (__int64)&v6, a1);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(double a1)
{
int v1; // edi
__int64 result; // rax
int v3; // edx
_DWORD *v4; // rbx
__int64 v5; // rax
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF
socklen_t addr_len; // [rsp+18h] [rbp-10h] BYREF
socklen_t len[3]; // [rsp+1Ch] [rbp-Ch] BYREF

len[0] = 16;
getsockname(host, &v6, len);
v1 = accept(host, &v6, &addr_len);
len[0] = v1;
result = 0LL;
if ( v1 >= 0 )
{
v3 = fcntl(v1, 4, 2048LL);
result = 0LL;
if ( v3 != -1 )
{
v4 = (_DWORD *)socket_free;
if ( socket_free )
{
socket_free = *(_QWORD *)socket_free;
*(_QWORD *)v4 = 0LL;
}
else
{
v4 = malloc(0x2038uLL);
memset(v4, 0, 0x2038uLL);
}
func2((__int64)v4);
v4[2060] = len[0];
v5 = socket_list;
if ( socket_list )
{
*(_QWORD *)(socket_list + 8) = v4;
*(_QWORD *)v4 = v5;
}
socket_list = (__int64)v4;
func4((__int64)v4, welcome_message);
func4((__int64)v4, "What is your name? ");
func5((__int64)v4, (__int64)&v6, a1);
result = 1LL;
}
}
return result;
}

original_code:
int new_socket(void) {
 SOCKET_T *sock = 0;
 int desc = sizeof(struct sockaddr_in);
 unsigned int len;
 struct sockaddr_in sockad;
 getsockname(host, (struct sockaddr*)&sockad, &desc);
 if((desc = accept(host, &sockad, &len)) < 0)
  return 0;
 if(fcntl(desc, 4, 04000) == -1)
  return 0;
 if( socket_free == 0 ) { sock = malloc(sizeof(*sock)); memset(sock,0,sizeof(*sock)); } else { sock = socket_free; socket_free = socket_free->next; sock->next = ((void *)0); };
 init_socket(sock);
 sock->desc = desc;
 if( socket_list != 0 ) { socket_list->prev = sock; sock->next = socket_list; socket_list = sock; } else { socket_list = sock; };
 send_to(sock, welcome_message);
 send_to(sock, "What is your name? ");
 get_address(sock,&sockad);
 return 1;
}

canonical_original_code:
int new_socket() {
 struct socket_t *sock = 0;
 int desc = sizeof(struct sockaddr_in);
 unsigned int len;
 struct sockaddr_in sockad;
 getsockname(host, (struct sockaddr *)&sockad, &desc);
 if((desc = accept(host, &sockad, &len)) < 0)
  return 0;
 if(fcntl(desc, 4, 04000) == -1)
  return 0;
 if( socket_free == 0 ) { sock = malloc(sizeof(*sock)); memset(sock,0,sizeof(*sock)); } else { sock = socket_free; socket_free = socket_free->next; sock->next = ((void *)0); };
 init_socket(sock);
 sock->desc = desc;
 if( socket_list != 0 ) { socket_list->prev = sock; sock->next = socket_list; socket_list = sock; } else { socket_list = sock; };
 send_to(sock, welcome_message);
 send_to(sock, "What is your name? ");
 get_address(sock,&sockad);
 return 1;
}

variable_types: desc int

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall process_output(__int64 a1)
{
__int64 result; // rax
unsigned int i; // ebx
size_t v3; // rax
int v4; // eax

result = 1LL;
if ( *(_BYTE *)(a1 + 4120) )
{
for ( i = 0; ; i += v4 )
{
v3 = strlen((const char *)(a1 + 4120));
if ( i >= v3 )
{
*(_BYTE *)(a1 + 4120) = 0;
return 1LL;
}
v4 = write(*(_DWORD *)(a1 + 8240), (const void *)(a1 + i + 4120), (int)(v3 - i));
if ( v4 < 0 )
break;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
unsigned int i; // ebx
size_t v3; // rax
int v4; // eax

result = 1LL;
if ( *(_BYTE *)(a1 + 4120) )
{
for ( i = 0; ; i += v4 )
{
v3 = strlen((const char *)(a1 + 4120));
if ( i >= v3 )
{
*(_BYTE *)(a1 + 4120) = 0;
return 1LL;
}
v4 = write(*(_DWORD *)(a1 + 8240), (const void *)(a1 + i + 4120), (int)(v3 - i));
if ( v4 < 0 )
break;
}
result = 0LL;
}
return result;
}

original_code:
int process_output(SOCKET_T *sock) {
 int i = 0;
 unsigned int total = 0;
 int block;
 if(sock->outbuf[0] == '\0')
  return 1;
 while(total < strlen(sock->outbuf))
 {
  block = strlen(sock->outbuf) - total;
  if((i = write(sock->desc, &sock->outbuf[total], block)) < 0)
   return 0;
  total += i;
 }
 sock->outbuf[0] = '\0';
 return 1;
}

canonical_original_code:
int process_output(struct socket_t *sock) {
 int i = 0;
 unsigned int total = 0;
 int block;
 if(sock->outbuf[0] == '\0')
  return 1;
 while(total < strlen(sock->outbuf))
 {
  block = strlen(sock->outbuf) - total;
  if((i = write(sock->desc, &sock->outbuf[total], block)) < 0)
   return 0;
  total += i;
 }
 sock->outbuf[0] = '\0';
 return 1;
}

variable_types: i int

variable_types:
total
unsigned int

variable_types: block int


original_code:
int process_input(SOCKET_T *sock) {
 unsigned int len = 0;
 unsigned int result;
 for(;;) {
  len += result = read(sock->desc, sock->inbuf + len, sizeof(sock->inbuf) - len - 10);
  if(sock->inbuf[len-1] == '\n' || sock->inbuf[len-1] == '\r') {
   sock->inbuf[len-1] = '\0';
   for( result = 0; result < strlen(sock->inbuf); result++ ) {
    if(sock->inbuf[result] == '\n' || sock->inbuf[result] == '\r')
     sock->inbuf[result] = '\0';
   }
   break;
  }
  else if(result <= 0) {
   return 0;
  }
 }
 nanny(sock,sock->inbuf);
 sock->inbuf[0] = '\0';
 return 1;
}

canonical_original_code:
int process_input(struct socket_t *sock) {
 unsigned int len = 0;
 unsigned int result;
 for(;;) {
  len += result = read(sock->desc, sock->inbuf + len, sizeof(sock->inbuf) - len - 10);
  if(sock->inbuf[len-1] == '\n' || sock->inbuf[len-1] == '\r') {
   sock->inbuf[len-1] = '\0';
   for( result = 0; result < strlen(sock->inbuf); result++ ) {
    if(sock->inbuf[result] == '\n' || sock->inbuf[result] == '\r')
     sock->inbuf[result] = '\0';
   }
   break;
  }
  else if(result <= 0) {
   return 0;
  }
 }
 nanny(sock,sock->inbuf);
 sock->inbuf[0] = '\0';
 return 1;
}

variable_types:
result
unsigned int


decompiled_code:
__int64 __fastcall check_connections(double a1)
{
int v1; // er8
__int64 i; // rsi
int v3; // ecx
__int64 v4; // rdx
__int64 v5; // rax
int v6; // edx
__int64 result; // rax
_QWORD *v8; // rbp
__int64 v9; // rbx
int v10; // ecx
__int64 v11; // rdx

v1 = host;
memset(&fd_read, 0, sizeof(fd_read));
memset(&fd_write, 0, sizeof(fd_write));
memset(&fd_exc, 0, sizeof(fd_exc));
fd_read.fds_bits[host / 64] |= 1LL << (host % 64);
for ( i = socket_list; i; i = *(_QWORD *)i )
{
v3 = *(_DWORD *)(i + 8240);
if ( v1 < v3 )
v1 = *(_DWORD *)(i + 8240);
v4 = 1LL << (v3 % 64);
v5 = v3 / 64;
fd_read.fds_bits[v5] |= v4;
fd_write.fds_bits[v5] |= v4;
fd_exc.fds_bits[v5] |= v4;
}
v6 = select(v1 + 1, &fd_read, &fd_write, &fd_exc, 0LL);
result = 0LL;
if ( v6 >= 0 )
{
if ( (fd_read.fds_bits[host / 64] & (1LL << (host % 64))) != 0 )
new_socket(a1);
v8 = (_QWORD *)socket_list;
if ( socket_list )
{
do
{
v9 = (__int64)v8;
v8 = (_QWORD *)*v8;
v10 = *(_DWORD *)(v9 + 8240);
v11 = 1LL << (v10 % 64);
if ( (fd_exc.fds_bits[v10 / 64] & v11) == 0 )
{
if ( (fd_read.fds_bits[v10 / 64] & v11) != 0 && !(unsigned int)process_input(v9) )
free_socket(v9);
if ( (fd_write.fds_bits[*(_DWORD *)(v9 + 8240) / 64] & (1LL << (*(_DWORD *)(v9 + 8240) % 64))) != 0
&& !(unsigned int)process_output(v9) )
{
free_socket(v9);
}
if ( *(_DWORD *)(v9 + 8244) )
continue;
}
free_socket(v9);
}
while ( v8 );
result = 1LL;
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(double a1)
{
int v1; // er8
__int64 i; // rsi
int v3; // ecx
__int64 v4; // rdx
__int64 v5; // rax
int v6; // edx
__int64 result; // rax
_QWORD *v8; // rbp
__int64 v9; // rbx
int v10; // ecx
__int64 v11; // rdx

v1 = host;
memset(&fd_read, 0, sizeof(fd_read));
memset(&fd_write, 0, sizeof(fd_write));
memset(&fd_exc, 0, sizeof(fd_exc));
fd_read.fds_bits[host / 64] |= 1LL << (host % 64);
for ( i = socket_list; i; i = *(_QWORD *)i )
{
v3 = *(_DWORD *)(i + 8240);
if ( v1 < v3 )
v1 = *(_DWORD *)(i + 8240);
v4 = 1LL << (v3 % 64);
v5 = v3 / 64;
fd_read.fds_bits[v5] |= v4;
fd_write.fds_bits[v5] |= v4;
fd_exc.fds_bits[v5] |= v4;
}
v6 = select(v1 + 1, &fd_read, &fd_write, &fd_exc, 0LL);
result = 0LL;
if ( v6 >= 0 )
{
if ( (fd_read.fds_bits[host / 64] & (1LL << (host % 64))) != 0 )
func7(a1);
v8 = (_QWORD *)socket_list;
if ( socket_list )
{
do
{
v9 = (__int64)v8;
v8 = (_QWORD *)*v8;
v10 = *(_DWORD *)(v9 + 8240);
v11 = 1LL << (v10 % 64);
if ( (fd_exc.fds_bits[v10 / 64] & v11) == 0 )
{
if ( (fd_read.fds_bits[v10 / 64] & v11) != 0 && !(unsigned int)func11(v9) )
func3(v9);
if ( (fd_write.fds_bits[*(_DWORD *)(v9 + 8240) / 64] & (1LL << (*(_DWORD *)(v9 + 8240) % 64))) != 0
&& !(unsigned int)func8(v9) )
{
func3(v9);
}
if ( *(_DWORD *)(v9 + 8244) )
continue;
}
func3(v9);
}
while ( v8 );
result = 1LL;
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
int check_connections(void) {
 SOCKET_T *sock;
 SOCKET_T *sock_next;
 int high = host;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_read)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_write)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_exc)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fd_read)->__fds_bits)[((host) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((host) % (8 * (int) sizeof (__fd_mask)))))));
 for(sock = socket_list; sock; sock = sock->next) {
  if(sock->desc > high)
   high = sock->desc;
  ((void) (((&fd_read)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fd_write)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fd_exc)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
 }
 if(select(high+1, &fd_read, &fd_write, &fd_exc, 0) < 0)
  return 0;
 if(((((&fd_read)->__fds_bits)[((host) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((host) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  new_socket();
 for(sock = socket_list; sock; sock = sock_next) {
  sock_next = sock->next;
  if(((((&fd_exc)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   free_socket(sock);
   continue;
  }
  if(((((&fd_read)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   if(!process_input(sock))
    free_socket(sock);
  }
  if(((((&fd_write)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   if(!process_output(sock))
    free_socket(sock);
  }
  if(sock->connected == 0) {
   free_socket(sock);
   continue;
  }
 }
 return 1;
}

canonical_original_code:
int check_connections() {
 struct socket_t *sock;
 struct socket_t *sock_next;
 int high = host;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_read)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_write)->__fds_bits)[0]) : "memory"); } while (0);
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fd_exc)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fd_read)->__fds_bits)[((host) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((host) % (8 * (int) sizeof (__fd_mask)))))));
 for(sock = socket_list; sock; sock = sock->next) {
  if(sock->desc > high)
   high = sock->desc;
  ((void) (((&fd_read)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fd_write)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&fd_exc)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))));
 }
 if(select(high+1, &fd_read, &fd_write, &fd_exc, 0) < 0)
  return 0;
 if(((((&fd_read)->__fds_bits)[((host) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((host) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  new_socket();
 for(sock = socket_list; sock; sock = sock_next) {
  sock_next = sock->next;
  if(((((&fd_exc)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   free_socket(sock);
   continue;
  }
  if(((((&fd_read)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   if(!process_input(sock))
    free_socket(sock);
  }
  if(((((&fd_write)->__fds_bits)[((sock->desc) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock->desc) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   if(!process_output(sock))
    free_socket(sock);
  }
  if(sock->connected == 0) {
   free_socket(sock);
   continue;
  }
 }
 return 1;
}

variable_types: high int


decompiled_code:
char *__fastcall strip_argument(char *a1, char *a2)
{
const unsigned __int16 **v4; // rsi
char v5; // dl
char v6; // al
char *result; // rax

v4 = _ctype_b_loc();
while ( 1 )
{
v5 = *a1;
if ( ((*v4)[*a1] & 0x2000) == 0 )
break;
++a1;
}
if ( v5 == 39 || v5 == 34 )
++a1;
else
v5 = 32;
v6 = *a1;
if ( *a1 )
{
while ( v5 != v6 )
{
*a2++ = v6;
v6 = *++a1;
if ( !*a1 )
{
result = a1;
goto LABEL_13;
}
}
result = a1 + 1;
}
else
{
result = a1;
}
LABEL_13:
for ( *a2 = 0; ((*v4)[*result] & 0x2000) != 0; ++result )
;
return result;
}

canonical_decompiled_code:
char *__fastcall func0(char *a1, char *a2)
{
const unsigned __int16 **v4; // rsi
char v5; // dl
char v6; // al
char *result; // rax

v4 = _ctype_b_loc();
while ( 1 )
{
v5 = *a1;
if ( ((*v4)[*a1] & 0x2000) == 0 )
break;
++a1;
}
if ( v5 == 39 || v5 == 34 )
++a1;
else
v5 = 32;
v6 = *a1;
if ( *a1 )
{
while ( v5 != v6 )
{
*a2++ = v6;
v6 = *++a1;
if ( !*a1 )
{
result = a1;
goto LABEL_13;
}
}
result = a1 + 1;
}
else
{
result = a1;
}
LABEL_13:
for ( *a2 = 0; ((*v4)[*result] & 0x2000) != 0; ++result )
;
return result;
}

original_code:
char *strip_argument(char *argument, char *arg_first) {
 char deli;
 while( ((*__ctype_b_loc ())[(int) ((*argument))] & (unsigned short int) _ISspace) )
  argument++;
 deli = ' ';
 if( *argument == '\'' || *argument == '"' )
  deli = *argument++;
 while( *argument != '\0' ) {
  if(*argument == deli) {
   argument++;
   break;
  }
  *arg_first = *argument;
  arg_first++;
  argument++;
 }
 *arg_first = '\0';
 while(((*__ctype_b_loc ())[(int) ((*argument))] & (unsigned short int) _ISspace))
  argument++;
 return argument;
}

canonical_original_code:
char *strip_argument(char *argument, char *arg_first) {
 char deli;
 while( ((*__ctype_b_loc ())[(int) ((*argument))] & (unsigned short) _ISspace) )
  argument++;
 deli = ' ';
 if( *argument == '\'' || *argument == '"' )
  deli = *argument++;
 while( *argument != '\0' ) {
  if(*argument == deli) {
   argument++;
   break;
  }
  *arg_first = *argument;
  arg_first++;
  argument++;
 }
 *arg_first = '\0';
 while(((*__ctype_b_loc ())[(int) ((*argument))] & (unsigned short) _ISspace))
  argument++;
 return argument;
}

variable_types:
argument
char *

variable_types: deli char


mudsleep

decompiled_code:
int __fastcall mudsleep(int a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_usec = 0LL;
v2.tv_sec = a1;
result = select(0, 0LL, 0LL, 0LL, &v2);
if ( result < 0 )
{
perror("mudsleep: select is not responding");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int a1)
{
int result; // eax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_usec = 0LL;
v2.tv_sec = a1;
result = select(0, 0LL, 0LL, 0LL, &v2);
if ( result < 0 )
{
perror("mudsleep: select is not responding");
exit(1);
}
return result;
}

original_code:
void mudsleep(int seconds)
{
 struct timeval wait_time;
 wait_time.tv_usec = 0;
 wait_time.tv_sec = seconds;
 if(select(0, 0, 0, 0, &wait_time) < 0) {
  perror("mudsleep: select is not responding");
  exit(1);
 }
}

variable_types: seconds int

variable_types: struct timeval { long tv_sec; long tv_usec; }


log

decompiled_code:
void __fastcall log(double x, const char *a2)
{
char *v2; // rbx

v2 = ctime(&current_time);
v2[strlen(v2) - 1] = 0;
fprintf(stderr, "%s :: %s\n", v2, a2);
}

canonical_decompiled_code:
void __fastcall func0(double x, const char *a2)
{
char *v2; // rbx

v2 = ctime(&current_time);
v2[strlen(v2) - 1] = 0;
fprintf(stderr, "%s :: %s\n", v2, a2);
}

original_code:
void log( const char *str ) {
 char *logstr;
 logstr = ctime( &current_time );
 logstr[strlen(logstr)-1] = '\0';
 fprintf( stderr, "%s :: %s\n", logstr, str );
}

canonical_original_code:
void log(char *str) {
 char *logstr;
 logstr = ctime( &current_time );
 logstr[strlen(logstr)-1] = '\0';
 fprintf( stderr, "%s :: %s\n", logstr, str );
}

variable_types:
str
char *

variable_types:
logstr
char *


chatroom

decompiled_code:
int __fastcall chatroom(__int64 a1, const char *a2)
{
int result; // eax
__int64 *v3; // rbx
const char *v4; // rbp
__int64 v5; // rax
__int64 **v6; // rbp
size_t v7; // rax
__int64 *v8; // rbx
const char *v9; // rbp
__int64 *v10; // rbp
char v11; // [rsp+0h] [rbp-2018h] BYREF
char s1[4095]; // [rsp+1h] [rbp-2017h] BYREF
char s[4120]; // [rsp+1000h] [rbp-1018h] BYREF

if ( *a2 == 47 )
{
v4 = (const char *)strip_argument(a2, &v11);
if ( !strcasecmp(s1, "who") )
{
s[0] = 0;
v6 = (__int64 **)socket_list;
if ( socket_list )
{
do
{
v7 = strlen(s);
sprintf(&s[v7], "%-12s\n\r", (const char *)v6[1027]);
v6 = (__int64 **)*v6;
}
while ( v6 );
}
result = send_to(a1, s);
}
else if ( !strcasecmp(s1, "quit") )
{
result = send_to(a1, "Okay, press enter.\n\r");
*(_DWORD *)(a1 + 8244) = 7;
}
else if ( !strcasecmp(s1, "emote") )
{
result = sprintf(s, "@%s %s\n\r", *(const char **)(a1 + 8216), v4);
v8 = (__int64 *)socket_list;
if ( socket_list )
{
do
{
if ( *((_DWORD *)v8 + 2061) == 8 )
result = send_to(v8, s);
v8 = (__int64 *)*v8;
}
while ( v8 );
}
}
else if ( !strcasecmp(s1, "tell") )
{
v9 = (const char *)strip_argument(v4, &v11);
sprintf(s, "You tell %s> %s\n\r", *(const char **)(a1 + 8216), v9);
send_to(a1, s);
result = sprintf(s, "%s tells you> %s\n\r", *(const char **)(a1 + 8216), v9);
v10 = (__int64 *)socket_list;
if ( socket_list )
{
while ( 1 )
{
if ( *((_DWORD *)v10 + 2061) == 8 )
{
result = strcasecmp((const char *)v10[1027], &v11);
if ( !result )
break;
}
v10 = (__int64 *)*v10;
if ( !v10 )
return result;
}
result = send_to(v10, s);
v10[2] = a1;
}
}
else if ( !strcasecmp(s1, "reply") )
{
v5 = *(_QWORD *)(a1 + 16);
if ( v5 && *(_DWORD *)(v5 + 8244) == 8 )
{
sprintf(s, "You reply to %s> %s\n\r", *(const char **)(v5 + 8216), v4);
send_to(a1, s);
sprintf(s, "%s replies> %s\n\r", *(const char **)(a1 + 8216), v4);
result = send_to(*(_QWORD *)(a1 + 16), s);
}
else
{
result = send_to(a1, "They aren't here.\n\r");
}
}
else
{
result = send_to(a1, "Invalid command.\n\r");
}
}
else
{
result = sprintf(s, "%s> %s\n\r", *(const char **)(a1 + 8216), a2);
v3 = (__int64 *)socket_list;
if ( socket_list )
{
do
{
if ( *((_DWORD *)v3 + 2061) == 8 )
result = send_to(v3, s);
v3 = (__int64 *)*v3;
}
while ( v3 );
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, const char *a2)
{
int result; // eax
__int64 *v3; // rbx
const char *v4; // rbp
__int64 v5; // rax
__int64 **v6; // rbp
size_t v7; // rax
__int64 *v8; // rbx
const char *v9; // rbp
__int64 *v10; // rbp
char v11; // [rsp+0h] [rbp-2018h] BYREF
char s1[4095]; // [rsp+1h] [rbp-2017h] BYREF
char s[4120]; // [rsp+1000h] [rbp-1018h] BYREF

if ( *a2 == 47 )
{
v4 = (const char *)strip_argument(a2, &v11);
if ( !strcasecmp(s1, "who") )
{
s[0] = 0;
v6 = (__int64 **)socket_list;
if ( socket_list )
{
do
{
v7 = strlen(s);
sprintf(&s[v7], "%-12s\n\r", (const char *)v6[1027]);
v6 = (__int64 **)*v6;
}
while ( v6 );
}
result = send_to(a1, s);
}
else if ( !strcasecmp(s1, "quit") )
{
result = send_to(a1, "Okay, press enter.\n\r");
*(_DWORD *)(a1 + 8244) = 7;
}
else if ( !strcasecmp(s1, "emote") )
{
result = sprintf(s, "@%s %s\n\r", *(const char **)(a1 + 8216), v4);
v8 = (__int64 *)socket_list;
if ( socket_list )
{
do
{
if ( *((_DWORD *)v8 + 2061) == 8 )
result = send_to(v8, s);
v8 = (__int64 *)*v8;
}
while ( v8 );
}
}
else if ( !strcasecmp(s1, "tell") )
{
v9 = (const char *)strip_argument(v4, &v11);
sprintf(s, "You tell %s> %s\n\r", *(const char **)(a1 + 8216), v9);
send_to(a1, s);
result = sprintf(s, "%s tells you> %s\n\r", *(const char **)(a1 + 8216), v9);
v10 = (__int64 *)socket_list;
if ( socket_list )
{
while ( 1 )
{
if ( *((_DWORD *)v10 + 2061) == 8 )
{
result = strcasecmp((const char *)v10[1027], &v11);
if ( !result )
break;
}
v10 = (__int64 *)*v10;
if ( !v10 )
return result;
}
result = send_to(v10, s);
v10[2] = a1;
}
}
else if ( !strcasecmp(s1, "reply") )
{
v5 = *(_QWORD *)(a1 + 16);
if ( v5 && *(_DWORD *)(v5 + 8244) == 8 )
{
sprintf(s, "You reply to %s> %s\n\r", *(const char **)(v5 + 8216), v4);
send_to(a1, s);
sprintf(s, "%s replies> %s\n\r", *(const char **)(a1 + 8216), v4);
result = send_to(*(_QWORD *)(a1 + 16), s);
}
else
{
result = send_to(a1, "They aren't here.\n\r");
}
}
else
{
result = send_to(a1, "Invalid command.\n\r");
}
}
else
{
result = sprintf(s, "%s> %s\n\r", *(const char **)(a1 + 8216), a2);
v3 = (__int64 *)socket_list;
if ( socket_list )
{
do
{
if ( *((_DWORD *)v3 + 2061) == 8 )
result = send_to(v3, s);
v3 = (__int64 *)*v3;
}
while ( v3 );
}
}
return result;
}

original_code:
void chatroom(SOCKET_T *sock, char *argument) {
 char buf[4096];
 char arg[4096];
 SOCKET_T *slist;
 if(argument[0] == '/') {
  argument = strip_argument(argument,arg);
  if(!strcasecmp(arg+1,"who")) {
   buf[0] = '\0';
   for(slist = socket_list; slist; slist = slist->next) {
    sprintf(buf+strlen(buf),"%-12s\n\r", slist->name );
   }
   send_to(sock,buf);
   return;
  }
  if(!strcasecmp(arg+1,"quit")) {
   send_to(sock,"Okay, press enter.\n\r");
   sock->connected = 7;
   return;
  }
  if(!strcasecmp(arg+1,"emote")) {
   sprintf(buf,"@%s %s\n\r",sock->name,argument);
   for(slist = socket_list; slist; slist = slist->next) {
    if(slist->connected == 8)
     send_to(slist,buf);
   }
   return;
  }
  if(!strcasecmp(arg+1,"tell")) {
   argument = strip_argument(argument,arg);
   sprintf(buf,"You tell %s> %s\n\r",sock->name,argument);
   send_to(sock,buf);
   sprintf(buf,"%s tells you> %s\n\r",sock->name,argument);
   for(slist = socket_list; slist; slist = slist->next) {
    if(slist->connected == 8 && !strcasecmp(slist->name,arg) ) {
     send_to(slist,buf);
     slist->reply = sock;
     break;
    }
   }
   return;
  }
  if(!strcasecmp(arg+1,"reply")) {
   if( !sock->reply || sock->reply->connected != 8 ) {
    send_to(sock,"They aren't here.\n\r");
    return;
   }
   sprintf(buf,"You reply to %s> %s\n\r",sock->reply->name,argument);
   send_to(sock,buf);
   sprintf(buf,"%s replies> %s\n\r",sock->name,argument);
   send_to(sock->reply,buf);
   return;
  }
  send_to(sock,"Invalid command.\n\r");
  return;
 }
 sprintf(buf,"%s> %s\n\r",sock->name,argument);
 for(slist = socket_list; slist; slist = slist->next) {
  if(slist->connected == 8)
   send_to(slist,buf);
 }
 return;
}

canonical_original_code:
void chatroom(struct socket_t *sock, char *argument) {
 char buf[4096];
 char arg[4096];
 struct socket_t *slist;
 if(argument[0] == '/') {
  argument = strip_argument(argument,arg);
  if(!strcasecmp(arg+1,"who")) {
   buf[0] = '\0';
   for(slist = socket_list; slist; slist = slist->next) {
    sprintf(buf+strlen(buf),"%-12s\n\r", slist->name );
   }
   send_to(sock,buf);
   return;
  }
  if(!strcasecmp(arg+1,"quit")) {
   send_to(sock,"Okay, press enter.\n\r");
   sock->connected = 7;
   return;
  }
  if(!strcasecmp(arg+1,"emote")) {
   sprintf(buf,"@%s %s\n\r",sock->name,argument);
   for(slist = socket_list; slist; slist = slist->next) {
    if(slist->connected == 8)
     send_to(slist,buf);
   }
   return;
  }
  if(!strcasecmp(arg+1,"tell")) {
   argument = strip_argument(argument,arg);
   sprintf(buf,"You tell %s> %s\n\r",sock->name,argument);
   send_to(sock,buf);
   sprintf(buf,"%s tells you> %s\n\r",sock->name,argument);
   for(slist = socket_list; slist; slist = slist->next) {
    if(slist->connected == 8 && !strcasecmp(slist->name,arg) ) {
     send_to(slist,buf);
     slist->reply = sock;
     break;
    }
   }
   return;
  }
  if(!strcasecmp(arg+1,"reply")) {
   if( !sock->reply || sock->reply->connected != 8 ) {
    send_to(sock,"They aren't here.\n\r");
    return;
   }
   sprintf(buf,"You reply to %s> %s\n\r",sock->reply->name,argument);
   send_to(sock,buf);
   sprintf(buf,"%s replies> %s\n\r",sock->name,argument);
   send_to(sock->reply,buf);
   return;
  }
  send_to(sock,"Invalid command.\n\r");
  return;
 }
 sprintf(buf,"%s> %s\n\r",sock->name,argument);
 for(slist = socket_list; slist; slist = slist->next) {
  if(slist->connected == 8)
   send_to(slist,buf);
 }
 return;
}

variable_types: struct socket_t { struct socket_t *next; struct socket_t *prev; struct socket_t *reply; char inbuf[4096]; char outbuf[4096]; char *name; char *password; char *host; int desc; int connected; } *

variable_types:
buf
char [4096]

variable_types:
arg
char [4096]


main

repo: apaznikov/qdmd

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 3 )
{
md_initialize(argv[1], argv[2], envp);
}
else if ( argc == 2 )
{
md_initialize(argv[1], "md.input", envp);
}
else
{
md_initialize("mdf.cls", "md.input", envp);
}
md_run();
md_finalize();
return 0;
}

original_code:
int main(int argc, char **argv)
{
    if (argc == 3) {
        md_initialize(argv[1], argv[2]);
    } else if (argc == 2) {
        md_initialize(argv[1], "md.input");
    } else {
        md_initialize("mdf.cls", "md.input");
    }
    md_run();
    md_finalize();
    return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
_BOOL8 __fastcall t1_iszero(double a1)
{
return a1 > 0.000001;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(double a1)
{
return a1 > 0.000001;
}

original_code:
static inline int t1_iszero(double x)
{
    return x > 1e-6;
}

variable_types: x double


decompiled_code:
double __fastcall t1_distance(int a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm2_8
double v5; // xmm0_8
double result; // xmm0_8

v2 = *(double *)(x + 8LL * a1) - *(double *)(x + 8LL * a2);
v3 = *(double *)(y + 8LL * a1) - *(double *)(y + 8LL * a2);
v4 = *(double *)(z + 8LL * a1) - *(double *)(z + 8LL * a2);
v5 = v2 * v2 + v3 * v3 + v4 * v4;
if ( v5 < 0.0 )
result = sqrt(v5);
else
result = sqrt(v5);
return result;
}

canonical_decompiled_code:
double __fastcall func1(int a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm2_8
double v5; // xmm0_8
double result; // xmm0_8

v2 = *(double *)(x + 8LL * a1) - *(double *)(x + 8LL * a2);
v3 = *(double *)(y + 8LL * a1) - *(double *)(y + 8LL * a2);
v4 = *(double *)(z + 8LL * a1) - *(double *)(z + 8LL * a2);
v5 = v2 * v2 + v3 * v3 + v4 * v4;
if ( v5 < 0.0 )
result = sqrt(v5);
else
result = sqrt(v5);
return result;
}

original_code:
static inline double t1_distance(int i, int j)
{
    return sqrt(pow(x[i] - x[j], 2) +
                pow(y[i] - y[j], 2) +
                pow(z[i] - z[j], 2));
}

variable_types: i int

variable_types: j int

return_type: double


decompiled_code:
double __fastcall t1_eta(int a1, int a2, double a3)
{
int v3; // ebx
double v4; // r15
double v5; // r13
double v6; // r12
double v7; // xmm0_8

if ( natoms <= 0 )
return *(double *)&LC1;
v3 = 0;
v4 = *(double *)&LC1;
do
{
if ( a1 != v3 && a2 != v3 )
{
v5 = t1_distance(a1, v3);
v6 = t1_f_cutoff(v5);
if ( v6 > 0.000001 )
{
v7 = pow(a3 - v5, 3.0);
v4 = v4 + exp(v7 * 2.330419169512) * v6;
}
}
++v3;
}
while ( natoms > v3 );
return v4;
}

canonical_decompiled_code:
double __fastcall func5(int a1, int a2, double a3)
{
int v3; // ebx
double v4; // r15
double v5; // r13
double v6; // r12
double v7; // xmm0_8

if ( natoms <= 0 )
return *(double *)&LC1;
v3 = 0;
v4 = *(double *)&LC1;
do
{
if ( a1 != v3 && a2 != v3 )
{
v5 = func1(a1, v3);
v6 = func4(v5);
if ( v6 > 0.000001 )
{
v7 = pow(a3 - v5, 3.0);
v4 = v4 + exp(v7 * 2.330419169512) * v6;
}
}
++v3;
}
while ( natoms > v3 );
return v4;
}

original_code:
inline double t1_eta(int i, int j, double rij)
{
    double rik, f_cutoff_val, sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t1_distance(i, k);
            f_cutoff_val = t1_f_cutoff(rik);
            if (f_cutoff_val > 1e-6) {
                sum += f_cutoff_val * exp(pow(1.3258, 3) *
                                          pow(rij - rik, 3));
            }
        }
    }
    return sum;
}

canonical_original_code:
inline double t1_eta(int i, int j, double rij)
{
    double rik; double f_cutoff_val; double sum = 0;
    int k;
    for (k = 0; k < natoms; k++) {
        if ((k != i) && (k != j)) {
            rik = t1_distance(i, k);
            f_cutoff_val = t1_f_cutoff(rik);
            if (f_cutoff_val > 1e-6) {
                sum += f_cutoff_val * exp(pow(1.3258, 3) *
                                          pow(rij - rik, 3));
            }
        }
    }
    return sum;
}

variable_types: rij double

variable_types: rik double

variable_types: sum double

variable_types: k int