decompiled_code:
__int64 __fastcall libc_start_main_hijack_32(__int64 a1, int a2, _DWORD *a3, __int64 a4, __int64 a5, int a6)
{
__int64 v6; // r10
__int64 v8; // rdx
_DWORD *v9; // rax
_DWORD *v10; // rdx
__int64 v11; // rax
__int64 v12; // rax
int v13; // ecx
char v14; // dl
int *v15; // rax
int v16; // edx
__int64 result; // rax

v6 = *(_QWORD *)(a1 + 8);
v8 = *(unsigned __int16 *)(v6 + 44);
v9 = (_DWORD *)(v6 + *(unsigned int *)(v6 + 28));
if ( (_WORD)v8 )
{
v10 = &v9[8 * v8];
while ( *v9 != 1 )
{
v9 += 8;
if ( v10 == v9 )
goto LABEL_6;
}
a5 = v6 + (unsigned int)v9[1] + (unsigned __int64)(unsigned int)(*(_DWORD *)(v6 + 24) - v9[2]);
}
LABEL_6:
v11 = (unsigned int)(a2 - 1);
if ( (unsigned int)v11 <= 2 )
a6 = CSWTCH_11[v11];
v12 = 0LL;
v13 = 0;
do
{
v14 = *(_BYTE *)(a5 + v12);
if ( (unsigned __int8)(v14 - 80) <= 9u )
goto LABEL_9;
if ( v14 == 104 )
{
if ( a6 == v13 )
{
v15 = (int *)(a5 + (int)v12 + 1);
v16 = *v15;
result = (__int64)v15 - v6;
*a3 = v16;
return result;
}
LABEL_9:
++v13;
}
++v12;
}
while ( v12 != 256 );
puts("[-] correct push instruction could not be located");
return -1LL;
}

original_code:
off_t libc_start_main_hijack_32(Elfit_t *host, int mode, uint32_t *patch_addr)
{
    Elf32_Ehdr *ehdr;
    Elf32_Phdr *phdr;
    Elf32_Addr startaddr;
    unsigned char *startoff;
    uint32_t hltoff;
    int ofd;
    int i, c, pushes, pushfound, pushestil;
    ehdr = (Elf32_Ehdr *) host->mem;
    phdr = (Elf32_Phdr *) (host->mem + ehdr->e_phoff);
    startaddr = ehdr->e_entry;
    for(i = 0; i < ehdr->e_phnum; i++, phdr++)
    {
        if (phdr->p_type == 1 && phdr->p_flags == (1 << 2) | (1 << 0))
        {
            startoff = host->mem + phdr->p_offset + (startaddr - phdr->p_vaddr);
            break;
        }
    }
    pushes = 0;
    pushfound = 0;
    switch(mode)
    {
        case 1: pushestil = 4; break;
        case 2: pushestil = 3; break;
        case 3: pushestil = 7; break;
    }
    for(i = 0; i < 0x100; i++)
    {
        if (startoff[i] >= 0x50 && startoff[i] <= 0x59)
        {
            pushes++;
        }
        if (startoff[i] == 0x68)
        {
            if (pushes == pushestil)
            {
                *patch_addr = *(uint32_t *)&startoff[i+1];
                pushfound = 1;
                break;
            }
            pushes++;
        }
    }
    if (!pushfound)
    {
        printf("[-] correct push instruction could not be located\n");
        return -1;
    }
    return &startoff[i+1] - host->mem;
}

canonical_original_code:
long libc_start_main_hijack_32(struct Elfit_t *host, int mode, unsigned int *patch_addr)
{
    struct Elf32_Ehdr *ehdr;
    struct Elf32_Phdr *phdr;
    unsigned int startaddr;
    unsigned char *startoff;
    unsigned int hltoff;
    int ofd;
    int i; int c; int pushes; int pushfound; int pushestil;
    ehdr = (struct Elf32_Ehdr *) host->mem;
    phdr = (struct Elf32_Phdr *) (host->mem + ehdr->e_phoff);
    startaddr = ehdr->e_entry;
    for(i = 0; i < ehdr->e_phnum; i++, phdr++)
    {
        if (phdr->p_type == 1 && phdr->p_flags == (1 << 2) | (1 << 0))
        {
            startoff = host->mem + phdr->p_offset + (startaddr - phdr->p_vaddr);
            break;
        }
    }
    pushes = 0;
    pushfound = 0;
    switch(mode)
    {
        case 1: pushestil = 4; break;
        case 2: pushestil = 3; break;
        case 3: pushestil = 7; break;
    }
    for(i = 0; i < 0x100; i++)
    {
        if (startoff[i] >= 0x50 && startoff[i] <= 0x59)
        {
            pushes++;
        }
        if (startoff[i] == 0x68)
        {
            if (pushes == pushestil)
            {
                *patch_addr = *(unsigned int *)&startoff[i+1];
                pushfound = 1;
                break;
            }
            pushes++;
        }
    }
    if (!pushfound)
    {
        printf("[-] correct push instruction could not be located\n");
        return -1;
    }
    return &startoff[i+1] - host->mem;
}

variable_types: mode int

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

variable_types: struct Elf32_Phdr { unsigned int p_type; unsigned int p_offset; unsigned int p_vaddr; unsigned int p_paddr; unsigned int p_filesz; unsigned int p_memsz; unsigned int p_flags; unsigned int p_align; } *

variable_types:
startaddr
unsigned int

variable_types:
startoff
unsigned char *

variable_types:
hltoff
unsigned int

variable_types: c int

variable_types: pushes int

variable_types: pushfound int

variable_types: pushestil int


repo: NickStephens/elfit

decompiled_code:
char *__fastcall libc_start_main_hijack_64(__int64 a1, int a2, _QWORD *a3, unsigned int *a4)
{
__int64 v4; // rdi
__int64 v6; // rdx
__int64 v7; // rax
__int64 v8; // rdx
unsigned int *v9; // rax
__int64 v10; // rcx
char *result; // rax

v4 = *(_QWORD *)(a1 + 8);
v6 = *(unsigned __int16 *)(v4 + 56);
v7 = v4 + *(_QWORD *)(v4 + 32);
if ( (_WORD)v6 )
{
v8 = v7 + 56 * v6;
while ( *(_DWORD *)v7 != 1 )
{
v7 += 56LL;
if ( v7 == v8 )
goto LABEL_6;
}
a4 = (unsigned int *)(v4 + *(_QWORD *)(v7 + 8) + *(_QWORD *)(v4 + 24) - *(_QWORD *)(v7 + 16));
}
LABEL_6:
if ( (unsigned int)(a2 - 1) <= 2 )
{
v9 = (unsigned int *)((char *)a4 + 7 * a2 + 11);
v10 = *v9;
}
else
{
v9 = a4;
v10 = *a4;
}
result = (char *)v9 - v4;
*a3 = v10;
return result;
}

original_code:
off_t libc_start_main_hijack_64(Elfit_t *host, int mode, uint64_t *patch_addr)
{
    Elf64_Ehdr *ehdr;
    Elf64_Phdr *phdr;
    Elf64_Addr startaddr;
    unsigned char *startoff;
    off_t offset;
    int ofd;
    int i, c;
    ehdr = (Elf64_Ehdr *) host->mem;
    phdr = (Elf64_Phdr *) (host->mem + ehdr->e_phoff);
    startaddr = ehdr->e_entry;
    for(i = 0; i < ehdr->e_phnum; i++, phdr++)
    {
        if (phdr->p_type == 1 && phdr->p_flags == (1 << 2) | (1 << 0))
        {
            startoff = host->mem + phdr->p_offset + (startaddr - phdr->p_vaddr);
            break;
        }
    }
    switch(mode)
    {
        case 1: offset = 18; break;
        case 2: offset = 25; break;
        case 3: offset = 32; break;
    }
    *patch_addr = *(uint32_t *)&startoff[offset];
    return &startoff[offset] - host->mem;
}

canonical_original_code:
long libc_start_main_hijack_64(struct Elfit_t *host, int mode, unsigned long *patch_addr)
{
    struct Elf64_Ehdr *ehdr;
    struct Elf64_Phdr *phdr;
    unsigned long startaddr;
    unsigned char *startoff;
    long offset;
    int ofd;
    int i; int c;
    ehdr = (struct Elf64_Ehdr *) host->mem;
    phdr = (struct Elf64_Phdr *) (host->mem + ehdr->e_phoff);
    startaddr = ehdr->e_entry;
    for(i = 0; i < ehdr->e_phnum; i++, phdr++)
    {
        if (phdr->p_type == 1 && phdr->p_flags == (1 << 2) | (1 << 0))
        {
            startoff = host->mem + phdr->p_offset + (startaddr - phdr->p_vaddr);
            break;
        }
    }
    switch(mode)
    {
        case 1: offset = 18; break;
        case 2: offset = 25; break;
        case 3: offset = 32; break;
    }
    *patch_addr = *(unsigned int *)&startoff[offset];
    return &startoff[offset] - host->mem;
}

variable_types: struct Elfit_t { char *name; unsigned char *mem; struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *file; } *

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

variable_types: struct Elf64_Phdr { unsigned int p_type; unsigned int p_flags; unsigned long p_offset; unsigned long p_vaddr; unsigned long p_paddr; unsigned long p_filesz; unsigned long p_memsz; unsigned long p_align; } *

variable_types:
startaddr
unsigned long

variable_types: offset long

variable_types: ofd int

variable_types: i int

return_type: long

user_defined_types: struct Elfit_t { char *name; unsigned char *mem; struct stat *file; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
void __fastcall __noreturn print_help(char a1, const char *a2)
{
if ( a1 )
printf("unrecognized option: %c\n", (unsigned int)a1);
printf(
"usage: %s [options] <host>\n"
"options:\n"
"-p, --parasite <parasite> specify parasite file\n"
"\n"
"INJECTION TECHNIQUES:\n"
"\t-t, --text text padding infection\n"
"\t-r, --reverse reverse text padding infection\n"
"\t-w, --data data padding infection\n"
"\t-s, --shared <sharedobj> inject shared object\n"
"\t-a, --relinjex relocatable injection\n"
"\t-n, --note note injection\n"
"\n"
"REDIRECTION TECHNIQUES:\n"
"\t-g, --got <symbol> hijack symbol's got entry\n"
"\t-e, --entrypoint use entry point redirection\n"
"\t-c, --ctor use ctor redirection\n"
"\t-d, --dtor use dtor redirection\n"
"\t-m, --start-main init|fini|main hijack chosen __libc_start_main arg\n"
"\n"
"PARASITE MODIFICATION:\n"
"\t-v, --patch-addr <addr> explicitly patch parasite with addr for jmp point\n"
"\t-q, --patch-position <position> byte index into parasite with which to patch with return addr\n"
"\t-z, --in-segment-poly <key> mutates the parasite with key, may mark injection segment writable\n"
"-x cross architecture infection, infect executables on i386 if on x64 or infect executables of x64 if on i368\n",
a2);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(char a1, const char *a2)
{
if ( a1 )
printf("unrecognized option: %c\n", (unsigned int)a1);
printf(
"usage: %s [options] <host>\n"
"options:\n"
"-p, --parasite <parasite> specify parasite file\n"
"\n"
"INJECTION TECHNIQUES:\n"
"\t-t, --text text padding infection\n"
"\t-r, --reverse reverse text padding infection\n"
"\t-w, --data data padding infection\n"
"\t-s, --shared <sharedobj> inject shared object\n"
"\t-a, --relinjex relocatable injection\n"
"\t-n, --note note injection\n"
"\n"
"REDIRECTION TECHNIQUES:\n"
"\t-g, --got <symbol> hijack symbol's got entry\n"
"\t-e, --entrypoint use entry point redirection\n"
"\t-c, --ctor use ctor redirection\n"
"\t-d, --dtor use dtor redirection\n"
"\t-m, --start-main init|fini|main hijack chosen __libc_start_main arg\n"
"\n"
"PARASITE MODIFICATION:\n"
"\t-v, --patch-addr <addr> explicitly patch parasite with addr for jmp point\n"
"\t-q, --patch-position <position> byte index into parasite with which to patch with return addr\n"
"\t-z, --in-segment-poly <key> mutates the parasite with key, may mark injection segment writable\n"
"-x cross architecture infection, infect executables on i386 if on x64 or infect executables of x64 if on i368\n",
a2);
exit(-1);
}

original_code:
static void print_help(char opt, char *prog)
{
    if (opt != 0x00)
        printf("unrecognized option: %c\n", opt);
    printf(
    "usage: %s [options] <host>\n"
    "options:\n"
    "-p, --parasite <parasite>  specify parasite file\n"
    "\n"
    "INJECTION TECHNIQUES:\n"
    "\t-t, --text               text padding infection\n"
    "\t-r, --reverse            reverse text padding infection\n"
    "\t-w, --data               data padding infection\n"
    "\t-s, --shared <sharedobj> inject shared object\n"
    "\t-a, --relinjex           relocatable injection\n"
    "\t-n, --note               note injection\n"
    "\n"
    "REDIRECTION TECHNIQUES:\n"
    "\t-g, --got <symbol>               hijack symbol's got entry\n"
    "\t-e, --entrypoint                 use entry point redirection\n"
    "\t-c, --ctor                       use ctor redirection\n"
    "\t-d, --dtor                       use dtor redirection\n"
    "\t-m, --start-main init|fini|main  hijack chosen __libc_start_main arg\n"
    "\n"
    "PARASITE MODIFICATION:\n"
    "\t-v, --patch-addr <addr>         explicitly patch parasite with addr for jmp point\n"
    "\t-q, --patch-position <position> byte index into parasite with which to patch with return addr\n"
    "\t-z, --in-segment-poly <key>     mutates the parasite with key, may mark injection segment writable\n"
    "-x                 cross architecture infection, infect executables on i386 if on x64 or infect executables of x64 if on i368\n",
    prog);
    exit(-1);
}

variable_types: opt char

variable_types:
prog
char *

return_type: void


usage

decompiled_code:
int __fastcall usage(int argc, char **argv)
{
char *v2; // rax
char *v3; // rbx
char *v4; // r13
int result; // eax
int v6; // eax

v2 = (char *)calloc(0x150uLL, 1uLL);
if ( argc <= 2 )
LABEL_23:
print_help(0, *argv);
v3 = v2;
v4 = v2 + 224;
while ( 2 )
{
result = getopt_long(argc, argv, "z:p:trsnaeg:cdm:v:q:xh", &long_options, &option_index);
if ( result != -1 )
{
switch ( result )
{
case 'a':
*((_DWORD *)v3 + 82) = 5;
continue;
case 'c':
*((_DWORD *)v3 + 55) = 3;
continue;
case 'd':
*((_DWORD *)v3 + 55) = 4;
continue;
case 'e':
*((_DWORD *)v3 + 55) = 1;
continue;
case 'g':
strncpy(v4, optarg, 0x63uLL);
*((_DWORD *)v3 + 55) = 2;
continue;
case 'h':
goto LABEL_23;
case 'm':
v6 = str_to_mode(optarg);
*((_DWORD *)v3 + 55) = 6;
*((_DWORD *)v3 + 81) = v6;
continue;
case 'n':
*((_DWORD *)v3 + 82) = 6;
continue;
case 'p':
strncpy(v3 + 100, optarg, 0x63uLL);
continue;
case 'q':
*((_DWORD *)v3 + 54) = atoi(optarg);
continue;
case 'r':
*((_DWORD *)v3 + 82) = 2;
continue;
case 's':
*((_DWORD *)v3 + 82) = 4;
continue;
case 't':
*((_DWORD *)v3 + 82) = 1;
continue;
case 'v':
*((_QWORD *)v3 + 26) = strtoul(optarg, 0LL, 16);
continue;
case 'w':
*((_DWORD *)v3 + 82) = 3;
continue;
case 'x':
++*((_DWORD *)v3 + 83);
continue;
case 'z':
v3[200] = *optarg;
continue;
default:
print_help(result, *argv);
}
}
return result;
}
}

canonical_decompiled_code:
int __fastcall func1(int argc, char **argv)
{
char *v2; // rax
char *v3; // rbx
char *v4; // r13
int result; // eax
int v6; // eax

v2 = (char *)calloc(0x150uLL, 1uLL);
if ( argc <= 2 )
LABEL_23:
func0(0, *argv);
v3 = v2;
v4 = v2 + 224;
while ( 2 )
{
result = getopt_long(argc, argv, "z:p:trsnaeg:cdm:v:q:xh", &long_options, &option_index);
if ( result != -1 )
{
switch ( result )
{
case 'a':
*((_DWORD *)v3 + 82) = 5;
continue;
case 'c':
*((_DWORD *)v3 + 55) = 3;
continue;
case 'd':
*((_DWORD *)v3 + 55) = 4;
continue;
case 'e':
*((_DWORD *)v3 + 55) = 1;
continue;
case 'g':
strncpy(v4, optarg, 0x63uLL);
*((_DWORD *)v3 + 55) = 2;
continue;
case 'h':
goto LABEL_23;
case 'm':
v6 = str_to_mode(optarg);
*((_DWORD *)v3 + 55) = 6;
*((_DWORD *)v3 + 81) = v6;
continue;
case 'n':
*((_DWORD *)v3 + 82) = 6;
continue;
case 'p':
strncpy(v3 + 100, optarg, 0x63uLL);
continue;
case 'q':
*((_DWORD *)v3 + 54) = atoi(optarg);
continue;
case 'r':
*((_DWORD *)v3 + 82) = 2;
continue;
case 's':
*((_DWORD *)v3 + 82) = 4;
continue;
case 't':
*((_DWORD *)v3 + 82) = 1;
continue;
case 'v':
*((_QWORD *)v3 + 26) = strtoul(optarg, 0LL, 16);
continue;
case 'w':
*((_DWORD *)v3 + 82) = 3;
continue;
case 'x':
++*((_DWORD *)v3 + 83);
continue;
case 'z':
v3[200] = *optarg;
continue;
default:
func0(result, *argv);
}
}
return result;
}
}

original_code:
opts_t * usage(int argc, char *argv[])
{
    opts_t *opts;
    int c;
    opts = malloc(sizeof(opts_t));
    if (argc < 3)
    {
        print_help(0, argv[0]);
        return ((void *)0);
    }
    memset(opts, 0, sizeof(opts_t));
    while((c = getopt_long(argc, argv, "z:p:trsnaeg:cdm:v:q:xh", long_options, &option_index)) != -1)
    {
        switch(c)
        {
            case 'z': opts->polymorphic_key = optarg[0]; break;
            case 'p': strncpy(opts->parasite, optarg, 100 -1); break;
            case 't':
                opts->injection_method = 1; break;
            case 'r':
                opts->injection_method = 2; break;
            case 's':
                opts->injection_method = 4; break;
            case 'w':
                opts->injection_method = 3; break;
            case 'a':
                opts->injection_method = 5; break;
            case 'n':
                opts->injection_method = 6; break;
            case 'e': opts->redirection_method = 1; break;
            case 'g': strncpy(opts->pltsymbol, optarg, 100 -1);
                opts->redirection_method = 2; break;
            case 'c': opts->redirection_method = 3; break;
            case 'd': opts->redirection_method = 4; break;
            case 'm': opts->startmain_mode = str_to_mode(optarg);
            opts->redirection_method = 6; break;
            case 'v': opts->patch_addr = strtoul(optarg, ((void *)0), 16); break;
            case 'q': opts->patch_pos = atoi(optarg); break;
            case 'x': opts->cross_infect++; break;
            case 'h': print_help(0x00, argv[0]); break;
            default: print_help((char) c, argv[0]); break;
        }
    }
    strncpy(opts->host, argv[argc-1], 100 -1);
}

canonical_original_code:
struct opts_t *usage(int argc, char *argv[])
{
    struct opts_t *opts;
    int c;
    opts = malloc(sizeof(opts_t));
    if (argc < 3)
    {
        print_help(0, argv[0]);
        return ((void *)0);
    }
    memset(opts, 0, sizeof(opts_t));
    while((c = getopt_long(argc, argv, "z:p:trsnaeg:cdm:v:q:xh", long_options, &option_index)) != -1)
    {
        switch(c)
        {
            case 'z': opts->polymorphic_key = optarg[0]; break;
            case 'p': strncpy(opts->parasite, optarg, 100 -1); break;
            case 't':
                opts->injection_method = 1; break;
            case 'r':
                opts->injection_method = 2; break;
            case 's':
                opts->injection_method = 4; break;
            case 'w':
                opts->injection_method = 3; break;
            case 'a':
                opts->injection_method = 5; break;
            case 'n':
                opts->injection_method = 6; break;
            case 'e': opts->redirection_method = 1; break;
            case 'g': strncpy(opts->pltsymbol, optarg, 100 -1);
                opts->redirection_method = 2; break;
            case 'c': opts->redirection_method = 3; break;
            case 'd': opts->redirection_method = 4; break;
            case 'm': opts->startmain_mode = str_to_mode(optarg);
            opts->redirection_method = 6; break;
            case 'v': opts->patch_addr = strtoul(optarg, ((void *)0), 16); break;
            case 'q': opts->patch_pos = atoi(optarg); break;
            case 'x': opts->cross_infect++; break;
            case 'h': print_help(0x00, argv[0]); break;
            default: print_help((char) c, argv[0]); break;
        }
    }
    strncpy(opts->host, argv[argc-1], 100 -1);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct opts_t { char host[100]; char parasite[100]; char polymorphic_key; unsigned long patch_addr; unsigned int patch_pos; unsigned int redirection_method; char pltsymbol[100]; unsigned int startmain_mode; unsigned int injection_method; unsigned int cross_infect; } *


decompiled_code:
__int64 fdc_getmainstatus()
{
__int64 result; // rax

result = (unsigned __int8)fdc_status_main;
if ( fdc_status_main >= 0 )
{
result = (unsigned __int8)fdc_status_main | 0xFFFFFF80;
fdc_status_main |= 0x80u;
}
return result;
}

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

result = (unsigned __int8)fdc_status_main;
if ( fdc_status_main >= 0 )
{
result = (unsigned __int8)fdc_status_main | 0xFFFFFF80;
fdc_status_main |= 0x80u;
}
return result;
}

original_code:
unsigned char fdc_getmainstatus(void) {
 if (!(fdc_status_main&0x80)) {
  fdc_status_main|=0x80;
 }
 return fdc_status_main;
}

canonical_original_code:
unsigned char fdc_getmainstatus() {
 if (!(fdc_status_main&0x80)) {
  fdc_status_main|=0x80;
 }
 return fdc_status_main;
}

return_type: unsigned char


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

result = puts("*** TC set");
if ( phase == 1 )
{
fdc_status_main &= 0xDFu;
phase = 2;
fdc_interrupt_pending = 1;
}
return result;
}

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

result = puts("*** TC set");
if ( phase == 1 )
{
fdc_status_main &= 0xDFu;
phase = 2;
fdc_interrupt_pending = 1;
}
return result;
}

original_code:
void fdc_terminalcount(void) {
 printf("*** TC set\n");
 if (phase == 1) {
  phase = 2;
  fdc_status_main&=~0x20;
  fdc_interrupt_pending = 1;
 }
}

canonical_original_code:
void fdc_terminalcount() {
 printf("*** TC set\n");
 if (phase == 1) {
  phase = 2;
  fdc_status_main&=~0x20;
  fdc_interrupt_pending = 1;
 }
}


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

result = (unsigned int)fdc_interrupt_pending;
if ( fdc_interrupt_pending )
{
result = 1LL;
if ( a1 )
fdc_interrupt_pending = 0;
}
return result;
}

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

result = (unsigned int)fdc_interrupt_pending;
if ( fdc_interrupt_pending )
{
result = 1LL;
if ( a1 )
fdc_interrupt_pending = 0;
}
return result;
}

original_code:
int fdc_interrupt(int clear) {
 if(fdc_interrupt_pending) {
  if (clear) fdc_interrupt_pending = 0;
  return(1);
 } else return (0);
}

variable_types: clear int

return_type: int


decompiled_code:
__int64 fdc_senseint()
{
__int64 result; // rax

resultlen = 2;
ResByte = 0;
fdcresult = fdc_status;
fdc_status = 0;
*(&fdcresult + 1) = fdc_track;
phase = 2;
result = fdc_status_main & 0xF0 | 0x40u;
fdc_status_main = fdc_status_main & 0xF0 | 0x40;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

resultlen = 2;
ResByte = 0;
fdcresult = fdc_status;
fdc_status = 0;
*(&fdcresult + 1) = fdc_track;
phase = 2;
result = fdc_status_main & 0xF0 | 0x40u;
fdc_status_main = fdc_status_main & 0xF0 | 0x40;
return result;
}

original_code:
void fdc_senseint(void) {
 fdcresult[0] = fdc_status[0];
 fdcresult[1] = fdc_track;
 resultlen = 2;
 ResByte = 0;
 fdc_status_main&=~(0x01|0x02|0x04|0x08);
 fdc_status[0] = 0;
 fdc_status_main|=0x40;
 phase = 2;
}

canonical_original_code:
void fdc_senseint() {
 fdcresult[0] = fdc_status[0];
 fdcresult[1] = fdc_track;
 resultlen = 2;
 ResByte = 0;
 fdc_status_main&=~(0x01|0x02|0x04|0x08);
 fdc_status[0] = 0;
 fdc_status_main|=0x40;
 phase = 2;
}


decompiled_code:
__int64 __fastcall fdc_seek(unsigned int a1)
{
int v1; // ebx
char v2; // al
int v3; // ebx
char v4; // al
char v5; // al
char v6; // dl
__int64 result; // rax

v1 = (unsigned __int8)fdccommand[1];
printf("Seeking to Track %i\n", a1);
fdc_track = a1;
v2 = fdc_status[3];
v3 = (v1 >> 2) & 1;
if ( a1 )
v4 = v2 & 0xEF;
else
v4 = v2 | 0x10;
fdc_status[3] = v4;
v5 = fdc_status[0] | 4;
if ( !v3 )
v5 = fdc_status[0] & 0xFB;
v6 = fdccommand[1] & 3;
fdc_status[0] = v5 | 0x20;
if ( v6 == 2 )
{
result = (unsigned __int8)fdc_status_main | 4u;
fdc_status_main |= 4u;
}
else if ( v6 == 3 )
{
result = (unsigned __int8)fdc_status_main | 8u;
fdc_status_main |= 8u;
}
else
{
result = (unsigned __int8)fdc_status_main | 1u;
if ( v6 == 1 )
result = (unsigned __int8)fdc_status_main | 2u;
fdc_status_main = result;
}
fdc_interrupt_pending = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
int v1; // ebx
char v2; // al
int v3; // ebx
char v4; // al
char v5; // al
char v6; // dl
__int64 result; // rax

v1 = (unsigned __int8)fdccommand[1];
printf("Seeking to Track %i\n", a1);
fdc_track = a1;
v2 = fdc_status[3];
v3 = (v1 >> 2) & 1;
if ( a1 )
v4 = v2 & 0xEF;
else
v4 = v2 | 0x10;
fdc_status[3] = v4;
v5 = fdc_status[0] | 4;
if ( !v3 )
v5 = fdc_status[0] & 0xFB;
v6 = fdccommand[1] & 3;
fdc_status[0] = v5 | 0x20;
if ( v6 == 2 )
{
result = (unsigned __int8)fdc_status_main | 4u;
fdc_status_main |= 4u;
}
else if ( v6 == 3 )
{
result = (unsigned __int8)fdc_status_main | 8u;
fdc_status_main |= 8u;
}
else
{
result = (unsigned __int8)fdc_status_main | 1u;
if ( v6 == 1 )
result = (unsigned __int8)fdc_status_main | 2u;
fdc_status_main = result;
}
fdc_interrupt_pending = 1;
return result;
}

original_code:
void fdc_seek(int track) {
 int head = (fdccommand[1]&0x04)>>2;
 printf("Seeking to Track %i\n", track);
 fdc_track = track;
 if (track == 0)
  fdc_status[3]|=0x10;
 else
  fdc_status[3]&=~0x10;
 if (head == 0)
  fdc_status[0]&=~0x04;
 else
  fdc_status[0]|=0x04;
 fdc_status[0]|=0x20;
 switch(fdccommand[1]&0x03) {
  case 0:
   fdc_status_main|=0x01; break;
  case 1:
   fdc_status_main|=0x02; break;
  case 2:
   fdc_status_main|=0x04; break;
  case 3:
   fdc_status_main|=0x08; break;
 }
 fdc_interrupt_pending = 1;
}

variable_types: track int

variable_types: head int


decompiled_code:
int __fastcall fdc_setresult(unsigned int a1, unsigned int a2, unsigned int a3)
{
__int64 v3; // rax
__int64 v4; // r9
__int64 v5; // rdx
__int64 v6; // rsi
__int64 v7; // rcx

v3 = a3;
v4 = a2;
v5 = (unsigned __int8)fdc_status[1];
v6 = (unsigned __int8)fdc_status[0];
v7 = (unsigned __int8)fdc_status[2];
*(&fdcresult + 3) = a1;
*(&fdcresult + 5) = v3;
fdcresult = v6;
*(&fdcresult + 1) = v5;
*(&fdcresult + 2) = v7;
*(&fdcresult + 4) = v4;
*(&fdcresult + 6) = 2;
resultlen = 7;
ResByte = 0;
return printf("Setting command result: R0:%02X R1:%02X R2:%02X TRACK:%i HEAD:%i SECTOR:%i\n", v6, v5, v7, a1, v4, v3);
}

canonical_decompiled_code:
int __fastcall func6(unsigned int a1, unsigned int a2, unsigned int a3)
{
__int64 v3; // rax
__int64 v4; // r9
__int64 v5; // rdx
__int64 v6; // rsi
__int64 v7; // rcx

v3 = a3;
v4 = a2;
v5 = (unsigned __int8)fdc_status[1];
v6 = (unsigned __int8)fdc_status[0];
v7 = (unsigned __int8)fdc_status[2];
*(&fdcresult + 3) = a1;
*(&fdcresult + 5) = v3;
fdcresult = v6;
*(&fdcresult + 1) = v5;
*(&fdcresult + 2) = v7;
*(&fdcresult + 4) = v4;
*(&fdcresult + 6) = 2;
resultlen = 7;
ResByte = 0;
return printf("Setting command result: R0:%02X R1:%02X R2:%02X TRACK:%i HEAD:%i SECTOR:%i\n", v6, v5, v7, a1, v4, v3);
}

original_code:
void fdc_setresult(int track, int head, int sector) {
 fdcresult[0] = fdc_status[0];
 fdcresult[1] = fdc_status[1];
 fdcresult[2] = fdc_status[2];
 fdcresult[3] = track;
 fdcresult[4] = head;
 fdcresult[5] = sector;
 fdcresult[6] = 2;
 resultlen = 7;
 ResByte = 0;
 printf("Setting command result: R0:%02X R1:%02X R2:%02X TRACK:%i HEAD:%i SECTOR:%i\n", fdc_status[0], fdc_status[1], fdc_status[2], track, head, sector);
}

variable_types: sector int


decompiled_code:
__int64 fdc_read_data()
{
__int64 result; // rax

printf(
"Reading Sector %i at Track %i, Head %i\n",
(unsigned __int8)fdccommand[4],
(unsigned int)fdc_track,
(unsigned __int8)fdccommand[3]);
fdc_setresult(fdc_track, (unsigned __int8)fdccommand[3], (unsigned __int8)fdccommand[4]);
result = (unsigned __int8)fdccommand[4];
fdc_status_main |= 0x60u;
fdc_interrupt_pending = 1;
fdc_sector = result;
fdc_bytepos = 0;
phase = 1;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax

printf(
"Reading Sector %i at Track %i, Head %i\n",
(unsigned __int8)fdccommand[4],
(unsigned int)fdc_track,
(unsigned __int8)fdccommand[3]);
func6(fdc_track, (unsigned __int8)fdccommand[3], (unsigned __int8)fdccommand[4]);
result = (unsigned __int8)fdccommand[4];
fdc_status_main |= 0x60u;
fdc_interrupt_pending = 1;
fdc_sector = result;
fdc_bytepos = 0;
phase = 1;
return result;
}

original_code:
void fdc_read_data(void) {
 printf("Reading Sector %i at Track %i, Head %i\n", fdccommand[4], fdc_track, fdccommand[3]);
 fdc_setresult(fdc_track, fdccommand[3], fdccommand[4]);
 fdc_interrupt_pending = 1;
 fdc_sector=fdccommand[4];
 fdc_bytepos=0;
 fdc_status_main|=0x40;
 fdc_status_main|=0x20;
 phase = 1;
}

canonical_original_code:
void fdc_read_data() {
 printf("Reading Sector %i at Track %i, Head %i\n", fdccommand[4], fdc_track, fdccommand[3]);
 fdc_setresult(fdc_track, fdccommand[3], fdccommand[4]);
 fdc_interrupt_pending = 1;
 fdc_sector=fdccommand[4];
 fdc_bytepos=0;
 fdc_status_main|=0x40;
 fdc_status_main|=0x20;
 phase = 1;
}


decompiled_code:
int fdc_read_id()
{
int v0; // ebx
int result; // eax

v0 = (int)(unsigned __int8)fdccommand[1] >> 2;
printf("Reading Sector ID at Track %i, Head %i\n", (unsigned int)fdc_track, (unsigned __int8)fdccommand[1]);
result = fdc_setresult(fdc_track, v0 & 1, 1u);
fdc_status_main |= 0x40u;
fdc_interrupt_pending = 1;
phase = 2;
return result;
}

canonical_decompiled_code:
int func8()
{
int v0; // ebx
int result; // eax

v0 = (int)(unsigned __int8)fdccommand[1] >> 2;
printf("Reading Sector ID at Track %i, Head %i\n", (unsigned int)fdc_track, (unsigned __int8)fdccommand[1]);
result = func6(fdc_track, v0 & 1, 1u);
fdc_status_main |= 0x40u;
fdc_interrupt_pending = 1;
phase = 2;
return result;
}

original_code:
void fdc_read_id(void) {
 int head = (fdccommand[1]&0x04)>>2;
 printf("Reading Sector ID at Track %i, Head %i\n", fdc_track, fdccommand[1]);
 fdc_setresult(fdc_track, head, 1);
 fdc_interrupt_pending = 1;
 fdc_status_main|=0x40;
 phase = 2;
}

canonical_original_code:
void fdc_read_id() {
 int head = (fdccommand[1]&0x04)>>2;
 printf("Reading Sector ID at Track %i, Head %i\n", fdc_track, fdccommand[1]);
 fdc_setresult(fdc_track, head, 1);
 fdc_interrupt_pending = 1;
 fdc_status_main|=0x40;
 phase = 2;
}


decompiled_code:
int __fastcall fdc_write(unsigned __int8 a1)
{
int result; // eax
int v2; // edx
int v3; // eax

result = phase;
if ( !phase )
{
v2 = CmdByte;
fdccommand[CmdByte] = a1;
v3 = v2;
if ( v2 )
{
LABEL_5:
result = v3 + 1;
CmdByte = result;
if ( result == cmdlen_0 )
{
LABEL_9:
result = fdccommand[0] & 0x1F;
switch ( (char)result )
{
case 2:
result = puts("*** Unimplemented FDC Command READ DIAGNOSTIC");
break;
case 3:
fdc_status_main &= 0xEFu;
break;
case 4:
result = puts("*** Unimplemented FDC Command SENSE DRIVE");
break;
case 5:
result = puts("*** Unimplemented FDC Command WRITE DATA");
break;
case 6:
result = fdc_read_data();
break;
case 7:
result = fdc_seek(0);
break;
case 8:
result = fdc_senseint();
break;
case 9:
result = puts("*** Unimplemented FDC Command WRITE DELETED DATA");
break;
case 10:
result = fdc_read_id();
break;
case 12:
result = puts("*** Unimplemented FDC Command READ DELETED DATA");
break;
case 13:
result = puts("*** Unimplemented FDC Command WRITE ID");
break;
case 15:
result = fdc_seek((unsigned __int8)fdccommand[2]);
break;
case 16:
result = puts("*** Unimplemented FDC Command VERSION");
break;
case 17:
result = puts("*** Unimplemented FDC Command SCAN EQUAL");
break;
case 25:
result = puts("*** Unimplemented FDC Command SCAN LOW OR EQUAL");
break;
case 29:
result = puts("*** Unimplemented FDC Command SCAN HIGH OR EQUAL");
break;
default:
break;
}
CmdByte = 0;
}
}
else
{
result = a1 & 0x1F;
switch ( (char)result )
{
case 2:
case 5:
case 6:
case 9:
case 12:
case 17:
case 25:
case 29:
cmdlen_0 = 9;
break;
case 3:
case 15:
cmdlen_0 = 3;
break;
case 4:
case 7:
case 10:
cmdlen_0 = 2;
break;
case 8:
case 16:
cmdlen_0 = 1;
CmdByte = 1;
goto LABEL_9;
case 13:
cmdlen_0 = 6;
break;
default:
cmdlen_0 = 1;
printf("Invalid FDC Command received: 0x%x\n", a1);
v3 = CmdByte;
goto LABEL_5;
}
CmdByte = 1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(unsigned __int8 a1)
{
int result; // eax
int v2; // edx
int v3; // eax

result = phase;
if ( !phase )
{
v2 = CmdByte;
fdccommand[CmdByte] = a1;
v3 = v2;
if ( v2 )
{
LABEL_5:
result = v3 + 1;
CmdByte = result;
if ( result == cmdlen_0 )
{
LABEL_9:
result = fdccommand[0] & 0x1F;
switch ( (char)result )
{
case 2:
result = puts("*** Unimplemented FDC Command READ DIAGNOSTIC");
break;
case 3:
fdc_status_main &= 0xEFu;
break;
case 4:
result = puts("*** Unimplemented FDC Command SENSE DRIVE");
break;
case 5:
result = puts("*** Unimplemented FDC Command WRITE DATA");
break;
case 6:
result = func7();
break;
case 7:
result = func5(0);
break;
case 8:
result = func4();
break;
case 9:
result = puts("*** Unimplemented FDC Command WRITE DELETED DATA");
break;
case 10:
result = func8();
break;
case 12:
result = puts("*** Unimplemented FDC Command READ DELETED DATA");
break;
case 13:
result = puts("*** Unimplemented FDC Command WRITE ID");
break;
case 15:
result = func5((unsigned __int8)fdccommand[2]);
break;
case 16:
result = puts("*** Unimplemented FDC Command VERSION");
break;
case 17:
result = puts("*** Unimplemented FDC Command SCAN EQUAL");
break;
case 25:
result = puts("*** Unimplemented FDC Command SCAN LOW OR EQUAL");
break;
case 29:
result = puts("*** Unimplemented FDC Command SCAN HIGH OR EQUAL");
break;
default:
break;
}
CmdByte = 0;
}
}
else
{
result = a1 & 0x1F;
switch ( (char)result )
{
case 2:
case 5:
case 6:
case 9:
case 12:
case 17:
case 25:
case 29:
cmdlen_0 = 9;
break;
case 3:
case 15:
cmdlen_0 = 3;
break;
case 4:
case 7:
case 10:
cmdlen_0 = 2;
break;
case 8:
case 16:
cmdlen_0 = 1;
CmdByte = 1;
goto LABEL_9;
case 13:
cmdlen_0 = 6;
break;
default:
cmdlen_0 = 1;
printf("Invalid FDC Command received: 0x%x\n", a1);
v3 = CmdByte;
goto LABEL_5;
}
CmdByte = 1;
}
}
return result;
}

original_code:
void fdc_write(unsigned char a) {
 static int cmdlen = 9;
 switch(phase) {
  case 0:
   fdccommand[CmdByte] = a;
   if (CmdByte == 0) {
    switch(a&0x1F) {
     case 0x06:
     case 0x0C:
     case 0x05:
     case 0x09:
     case 0x02:
     case 0x11:
     case 0x19:
     case 0x1D:
      cmdlen = 9;
      break;
     case 0x0D:
      cmdlen = 6;
      break;
     case 0x03:
     case 0x0F:
      cmdlen = 3;
      break;
     case 0x0A:
     case 0x07:
     case 0x04:
      cmdlen = 2;
      break;
     case 0x08:
     case 0x10:
      cmdlen = 1;
      break;
     default:
      cmdlen = 1;
      printf("Invalid FDC Command received: 0x%x\n", a);
      break;
    }
   }
   CmdByte++;
   if (CmdByte == cmdlen) {
    switch(fdccommand[0]&0x1F) {
     case 0x06:
      fdc_read_data();
      break;
     case 0x0C:
      printf("*** Unimplemented FDC Command READ DELETED DATA\n");
      break;
     case 0x05:
      printf("*** Unimplemented FDC Command WRITE DATA\n");
      break;
     case 0x09:
      printf("*** Unimplemented FDC Command WRITE DELETED DATA\n");
      break;
     case 0x02:
      printf("*** Unimplemented FDC Command READ DIAGNOSTIC\n");
      break;
     case 0x0A:
      fdc_read_id();
      break;
     case 0x0D:
      printf("*** Unimplemented FDC Command WRITE ID\n");
      break;
     case 0x11:
      printf("*** Unimplemented FDC Command SCAN EQUAL\n");
      break;
     case 0x19:
      printf("*** Unimplemented FDC Command SCAN LOW OR EQUAL\n");
      break;
     case 0x1D:
      printf("*** Unimplemented FDC Command SCAN HIGH OR EQUAL\n");
      break;
     case 0x07:
      fdc_seek(0);
      break;
     case 0x08:
      fdc_senseint();
      break;
     case 0x03:
      fdc_status_main&=~0x10;
      break;
     case 0x04:
      printf("*** Unimplemented FDC Command SENSE DRIVE\n");
      break;
     case 0x10:
      printf("*** Unimplemented FDC Command VERSION\n");
      break;
     case 0x0F:
      fdc_seek(fdccommand[2]);
      break;
     default:
      break;
    }
    CmdByte = 0;
   }
   break;
  case 1:
   break;
  case 2:
   break;
 }
}

variable_types:
a
unsigned char

variable_types: cmdlen int


decompiled_code:
__int64 fdc_sendbyte()
{
__int64 result; // rax
int v1; // esi

result = 0LL;
if ( fdc_status_main < 0 )
{
v1 = fdc_sector;
result = *(unsigned __int8 *)(floppydata
+ fdc_bytepos
+ ((fdc_sector
+ 9 * ((((int)(unsigned __int8)fdccommand[1] >> 2) & 1) + 2 * fdc_track)
- 1) << 9));
if ( fdc_bytepos == 511 )
{
fdc_bytepos = 0;
++fdc_sector;
if ( v1 + 1 > 9 )
{
phase = 2;
fdc_status_main &= 0xDFu;
}
}
else
{
++fdc_bytepos;
}
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 result; // rax
int v1; // esi

result = 0LL;
if ( fdc_status_main < 0 )
{
v1 = fdc_sector;
result = *(unsigned __int8 *)(floppydata
+ fdc_bytepos
+ ((fdc_sector
+ 9 * ((((int)(unsigned __int8)fdccommand[1] >> 2) & 1) + 2 * fdc_track)
- 1) << 9));
if ( fdc_bytepos == 511 )
{
fdc_bytepos = 0;
++fdc_sector;
if ( v1 + 1 > 9 )
{
phase = 2;
fdc_status_main &= 0xDFu;
}
}
else
{
++fdc_bytepos;
}
}
return result;
}

original_code:
unsigned char fdc_sendbyte(void) {
 int head = (fdccommand[1]&0x04)>>2;
 int lba;
 unsigned char output;
 if (!(fdc_status_main&0x80)) return 0;
 lba = (( fdc_track * 2 + head) * 9) + fdc_sector - 1;
 output = floppydata[(lba*512) + fdc_bytepos];
 fdc_bytepos++;
 if (fdc_bytepos == 512) {
  fdc_bytepos = 0;
  fdc_sector++;
  if(fdc_sector > 9) {
   phase = 2;
   fdc_status_main&=~0x20;
  }
 }
 return output;
}

canonical_original_code:
unsigned char fdc_sendbyte() {
 int head = (fdccommand[1]&0x04)>>2;
 int lba;
 unsigned char output;
 if (!(fdc_status_main&0x80)) return 0;
 lba = (( fdc_track * 2 + head) * 9) + fdc_sector - 1;
 output = floppydata[(lba*512) + fdc_bytepos];
 fdc_bytepos++;
 if (fdc_bytepos == 512) {
  fdc_bytepos = 0;
  fdc_sector++;
  if(fdc_sector > 9) {
   phase = 2;
   fdc_status_main&=~0x20;
  }
 }
 return output;
}

variable_types: lba int

variable_types:
output
unsigned char


decompiled_code:
__int64 fdc_read()
{
unsigned int v0; // er8
__int64 result; // rax

if ( phase == 1 )
return fdc_sendbyte();
v0 = 0;
if ( phase != 2 )
return v0;
v0 = (unsigned __int8)fdcresult[ResByte++];
if ( ResByte != resultlen )
return v0;
fdc_status_main &= 0xAFu;
result = v0;
phase = 0;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v0; // er8
__int64 result; // rax

if ( phase == 1 )
return func10();
v0 = 0;
if ( phase != 2 )
return v0;
v0 = (unsigned __int8)fdcresult[ResByte++];
if ( ResByte != resultlen )
return v0;
fdc_status_main &= 0xAFu;
result = v0;
phase = 0;
return result;
}

original_code:
unsigned char fdc_read(void) {
 unsigned char output = 0;
 switch(phase) {
  case 1:
   output = fdc_sendbyte();
   break;
  case 2:
   output = fdcresult[ResByte];
   ResByte++;
   if (ResByte == resultlen) {
    phase = 0;
    fdc_status_main&=~0x40;
    fdc_status_main&=~0x10;
   }
   break;
 }
 return output;
}

canonical_original_code:
unsigned char fdc_read() {
 unsigned char output = 0;
 switch(phase) {
  case 1:
   output = fdc_sendbyte();
   break;
  case 2:
   output = fdcresult[ResByte];
   ResByte++;
   if (ResByte == resultlen) {
    phase = 0;
    fdc_status_main&=~0x40;
    fdc_status_main&=~0x10;
   }
   break;
 }
 return output;
}


libdir

decompiled_code:
char *__fastcall libdir(const char *a1)
{
char *v1; // rax
const char *v2; // r12
char *v4; // rax
const char *v5; // r12

v1 = getenv("NC100EM_HOME");
if ( v1 )
{
v2 = v1;
mkdir(v1, 0x1FFu);
snprintf(buf_0, 0x400uLL, "%s/%s", v2, a1);
return buf_0;
}
v4 = getenv("HOME");
v5 = v4;
if ( !v4 )
{
snprintf(buf_0, 0x400uLL, "%s", a1);
return buf_0;
}
snprintf(buf_0, 0x400uLL, "%s/nc100", v4);
mkdir(buf_0, 0x1FFu);
snprintf(buf_0, 0x400uLL, "%s/nc100/%s", v5, a1);
return buf_0;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
char *v1; // rax
const char *v2; // r12
char *v4; // rax
const char *v5; // r12

v1 = getenv("NC100EM_HOME");
if ( v1 )
{
v2 = v1;
mkdir(v1, 0x1FFu);
snprintf(buf_0, 0x400uLL, "%s/%s", v2, a1);
return buf_0;
}
v4 = getenv("HOME");
v5 = v4;
if ( !v4 )
{
snprintf(buf_0, 0x400uLL, "%s", a1);
return buf_0;
}
snprintf(buf_0, 0x400uLL, "%s/nc100", v4);
mkdir(buf_0, 0x1FFu);
snprintf(buf_0, 0x400uLL, "%s/nc100/%s", v5, a1);
return buf_0;
}

original_code:
char *libdir(char *file)
{
static char buf[1024];
char *nchome=getenv("NC100EM_HOME");
if(nchome)
  {
  mkdir(nchome,0777);
  snprintf(buf,sizeof(buf),"%s/%s",nchome,file);
  }
else
  {
  char *home=getenv("HOME");
  if(!home)
    snprintf(buf,sizeof(buf),"%s",file);
  else
    {
    snprintf(buf,sizeof(buf),"%s/nc100",home);
    mkdir(buf,0777);
    snprintf(buf,sizeof(buf),"%s/nc100/%s",home,file);
    }
  }
return(buf);
}

canonical_original_code:
char *libdir(char *file)
{
static char buf[1024];
char *nchome = getenv("NC100EM_HOME");
if(nchome)
  {
  mkdir(nchome,0777);
  snprintf(buf,sizeof(buf),"%s/%s",nchome,file);
  }
else
  {
  char *home = getenv("HOME");
  if(!home)
    snprintf(buf,sizeof(buf),"%s",file);
  else
    {
    snprintf(buf,sizeof(buf),"%s/nc100",home);
    mkdir(buf,0777);
    snprintf(buf,sizeof(buf),"%s/nc100/%s",home,file);
    }
  }
return(buf);
}

variable_types:
file
char *

variable_types:
buf
char [1024]

variable_types:
nchome
char *

variable_types:
home
char *

return_type: char *


decompiled_code:
__int64 __fastcall press_autokeys(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = 4LL * a1;
result = (unsigned int)autokeys[v1 + 3];
*((_BYTE *)&keyports + autokeys[v1]) |= autokeys[v1 + 1];
*((_BYTE *)&keyports + autokeys[v1 + 2]) |= result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = 4LL * a1;
result = (unsigned int)autokeys[v1 + 3];
*((_BYTE *)&keyports + autokeys[v1]) |= autokeys[v1 + 1];
*((_BYTE *)&keyports + autokeys[v1 + 2]) |= result;
return result;
}

original_code:
void press_autokeys(int pos)
{
keyports[autokeys[pos].port1]|=autokeys[pos].mask1;
keyports[autokeys[pos].port2]|=autokeys[pos].mask2;
}

variable_types: pos int


decompiled_code:
__int64 unpress_autokeys()
{
__int64 result; // rax

keyports = 0LL;
result = 0LL;
*((_WORD *)&keyports + 4) = 0;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

keyports = 0LL;
result = 0LL;
*((_WORD *)&keyports + 4) = 0;
return result;
}

original_code:
void unpress_autokeys(void)
{
int f;
for(f=0;f<10;f++) keyports[f]=0;
}

canonical_original_code:
void unpress_autokeys()
{
int f;
for(f=0;f<10;f++) keyports[f]=0;
}

variable_types: f int


repo: No-name/ball

decompiled_code:
void __fastcall list_del(__int64 a1)
{
_QWORD *v1; // rax

_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*v1 = 0LL;
v1[1] = 0LL;
}

original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = ((void *)0);
 entry->prev = ((void *)0);
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
_BOOL8 __fastcall list_empty(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

original_code:
static inline int list_empty(const struct list_head *head)
{
 return head->next == head;
}

canonical_original_code:
static inline int list_empty(struct list_head *head)
{
 return head->next == head;
}

user_defined_types: struct list_head { struct list_head *next; }


original_code:
void package_message(struct message_packet * msg)
{
 int len;
 char * p = msg->content;
 *(int *)p = htonl(msg->version);
 p += 4;
 *(int *)p = htonl(msg->type);
 p += 4;
 *(int *)p = 0;
 p += 4;
 switch (msg->type)
 {
  case MSG_TYPE_LOGIN:
   len = msg->login_info.name_len;
   *p = len;
   p += 1;
   memcpy(p, msg->login_info.name, len);
   p += len;
   len = msg->login_info.passwd_len;
   *p = len;
   p += 1;
   memcpy(p, msg->login_info.passwd, len);
   p += len;
   break;
  case MSG_TYPE_CHART:
   len = msg->chart_info.from_len;
   *p = len;
   p += 1;
   memcpy(p, msg->chart_info.from, len);
   p += len;
   len = msg->chart_info.to_len;
   *p = len;
   p += 1;
   memcpy(p, msg->chart_info.to, len);
   p += len;
   len = msg->chart_info.msg_len;
   *(unsigned short *)p = htons(len);
   p += 2;
   memcpy(p, msg->chart_info.msg, len);
   p += len;
   break;
  case MSG_TYPE_PEER_LIST:
   if (msg->peer_list.more)
    *p = 0x01;
   else
    *p = 0x00;
   p = msg->peer_list.end;
   break;
  default:
   ((void) sizeof ((0 && "message type invalid") ? 1 : 0), __extension__ ({ if (0 && "message type invalid") ; else __assert_fail ("0 && \"message type invalid\"", "ball.c", 135, __extension__ __PRETTY_FUNCTION__); }));
   msg->length = 0;
   return;
 }
 len = p - msg->content;
 ((int *)msg->content)[2] = htonl(len);
 msg->length = len;
 return;
}

canonical_original_code:
void package_message(struct message_packet *msg)
{
 int len;
 char *p = msg->content;
 *(int *)p = htonl(msg->version);
 p += 4;
 *(int *)p = htonl(msg->type);
 p += 4;
 *(int *)p = 0;
 p += 4;
 switch (msg->type)
 {
  case MSG_TYPE_LOGIN:
   len = msg->login_info.name_len;
   *p = len;
   p += 1;
   memcpy(p, msg->login_info.name, len);
   p += len;
   len = msg->login_info.passwd_len;
   *p = len;
   p += 1;
   memcpy(p, msg->login_info.passwd, len);
   p += len;
   break;
  case MSG_TYPE_CHART:
   len = msg->chart_info.from_len;
   *p = len;
   p += 1;
   memcpy(p, msg->chart_info.from, len);
   p += len;
   len = msg->chart_info.to_len;
   *p = len;
   p += 1;
   memcpy(p, msg->chart_info.to, len);
   p += len;
   len = msg->chart_info.msg_len;
   *(unsigned short *)p = htons(len);
   p += 2;
   memcpy(p, msg->chart_info.msg, len);
   p += len;
   break;
  case MSG_TYPE_PEER_LIST:
   if (msg->peer_list.more)
    *p = 0x01;
   else
    *p = 0x00;
   p = msg->peer_list.end;
   break;
  default:
   ((void) sizeof ((0 && "message type invalid") ? 1 : 0), __extension__ ({ if (0 && "message type invalid") ; else __assert_fail ("0 && \"message type invalid\"", "ball.c", 135, __extension__ __PRETTY_FUNCTION__); }));
   msg->length = 0;
   return;
 }
 len = p - msg->content;
 ((int *)msg->content)[2] = htonl(len);
 msg->length = len;
 return;
}

variable_types: struct message_packet { struct list_head { struct list_head *next; } next; int msg_is_ok; int type; int length; int version; union { struct { int name_len; int passwd_len; char *name; char *passwd; } login_info; struct { int from_len; int to_len; int msg_len; char *from; char *to; char *msg; } chart_info; struct { int more; char *start; char *end; } peer_list; }; char content[1024]; } *

variable_types: len int

variable_types:
p
char *

user_defined_types: struct message_packet { struct list_head next; int msg_is_ok; int type; int length; int version; union { struct { int name_len; int passwd_len; char *name; char *passwd; } login_info; struct { int from_len; int to_len; int msg_len; char *from; char *to; char *msg; } chart_info; struct { int more; char *start; char *end; } peer_list; }; char content[1024]; }


decompiled_code:
__int64 __fastcall message_package_head(__int64 a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned int v3; // edi
int v4; // eax
__int64 v5; // rdx
unsigned int v6; // edi
__int64 result; // rax
__int64 v8; // rdx

v1 = _bswap_32(*(_DWORD *)(a1 + 28));
v3 = *(_DWORD *)(v2 + 20);
*(_DWORD *)(v2 + 72) = v1;
v4 = _bswap_32(v3);
v6 = *(_DWORD *)(v5 + 24);
*(_DWORD *)(v5 + 76) = v4;
result = _bswap_32(v6);
*(_DWORD *)(v8 + 80) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned int v3; // edi
int v4; // eax
__int64 v5; // rdx
unsigned int v6; // edi
__int64 result; // rax
__int64 v8; // rdx

v1 = func4(*(_DWORD *)(a1 + 28));
v3 = *(_DWORD *)(v2 + 20);
*(_DWORD *)(v2 + 72) = v1;
v4 = func4(v3);
v6 = *(_DWORD *)(v5 + 24);
*(_DWORD *)(v5 + 76) = v4;
result = func4(v6);
*(_DWORD *)(v8 + 80) = result;
return result;
}

original_code:
void message_package_head(struct message_packet * msg)
{
 char * p = msg->content;
 *(int *)p = htonl(msg->version);
 p += 4;
 *(int *)p = htonl(msg->type);
 p += 4;
 *(int *)p = htonl(msg->length);
}

canonical_original_code:
void message_package_head(struct message_packet *msg)
{
 char *p = msg->content;
 *(int *)p = htonl(msg->version);
 p += 4;
 *(int *)p = htonl(msg->type);
 p += 4;
 *(int *)p = htonl(msg->length);
}


original_code:
void message_parse_body(struct message_packet * msg)
{
 char * p = msg->content;
 p += 12;
 switch (msg->type)
 {
  case MSG_TYPE_LOGIN:
   msg->login_info.name_len = *p;
   p += 1;
   msg->login_info.name = p;
   p += msg->login_info.name_len;
   msg->login_info.passwd_len = *p;
   p += 1;
   msg->login_info.passwd = p;
   break;
  case MSG_TYPE_CHART:
   msg->chart_info.from_len = *p;
   p += 1;
   msg->chart_info.from = p;
   p += msg->chart_info.from_len;
   msg->chart_info.to_len = *p;
   p += 1;
   msg->chart_info.to = p;
   p += msg->chart_info.to_len;
   msg->chart_info.msg_len = ntohs(*(short *)p);
   p += 2;
   msg->chart_info.msg = p;
   break;
  default:
   break;
 }
}

canonical_original_code:
void message_parse_body(struct message_packet *msg)
{
 char *p = msg->content;
 p += 12;
 switch (msg->type)
 {
  case MSG_TYPE_LOGIN:
   msg->login_info.name_len = *p;
   p += 1;
   msg->login_info.name = p;
   p += msg->login_info.name_len;
   msg->login_info.passwd_len = *p;
   p += 1;
   msg->login_info.passwd = p;
   break;
  case MSG_TYPE_CHART:
   msg->chart_info.from_len = *p;
   p += 1;
   msg->chart_info.from = p;
   p += msg->chart_info.from_len;
   msg->chart_info.to_len = *p;
   p += 1;
   msg->chart_info.to = p;
   p += msg->chart_info.to_len;
   msg->chart_info.msg_len = ntohs(*(short *)p);
   p += 2;
   msg->chart_info.msg = p;
   break;
  default:
   break;
 }
}


decompiled_code:
__int64 __fastcall put_message(char **a1, int a2)
{
int v2; // ebx
char *v3; // rbp
int v4; // eax
int *v5; // r8
__int64 result; // rax

v2 = *((_DWORD *)a1 + 3);
v3 = *a1;
while ( 1 )
{
v4 = write(a2, v3, v2);
if ( v4 == -1 )
break;
v3 += v4;
v2 -= v4;
if ( !v2 )
{
*((_DWORD *)a1 + 3) = 0;
result = 5LL;
*a1 = 0LL;
return result;
}
}
v5 = _errno_location();
result = 3LL;
if ( *v5 == 11 )
{
*((_DWORD *)a1 + 3) = v2;
result = 4LL;
*a1 = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char **a1, int a2)
{
int v2; // ebx
char *v3; // rbp
int v4; // eax
int *v5; // r8
__int64 result; // rax

v2 = *((_DWORD *)a1 + 3);
v3 = *a1;
while ( 1 )
{
v4 = write(a2, v3, v2);
if ( v4 == -1 )
break;
v3 += v4;
v2 -= v4;
if ( !v2 )
{
*((_DWORD *)a1 + 3) = 0;
result = 5LL;
*a1 = 0LL;
return result;
}
}
v5 = _errno_location();
result = 3LL;
if ( *v5 == 11 )
{
*((_DWORD *)a1 + 3) = v2;
result = 4LL;
*a1 = v3;
}
return result;
}

original_code:
int put_message(struct msg_status * status, int fd)
{
 int wbyte;
 int left = status->left;
 char * p = status->position;
 while (1)
 {
  wbyte = write(fd, p, left);
  if (wbyte == -1)
  {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)
   {
    status->left = left;
    status->position = p;
    return MSG_SEND_AGAIN;
   }
   return MSG_SEND_FAILED;
  }
  p += wbyte;
  left -= wbyte;
  if (left == 0)
  {
   status->left = 0;
   status->position = ((void *)0);
   return MSG_SEND_FINISH;
  }
 }
}

canonical_original_code:
int put_message(struct msg_status *status, int fd)
{
 int wbyte;
 int left = status->left;
 char *p = status->position;
 while (1)
 {
  wbyte = write(fd, p, left);
  if (wbyte == -1)
  {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)
   {
    status->left = left;
    status->position = p;
    return MSG_SEND_AGAIN;
   }
   return MSG_SEND_FAILED;
  }
  p += wbyte;
  left -= wbyte;
  if (left == 0)
  {
   status->left = 0;
   status->position = ((void *)0);
   return MSG_SEND_FINISH;
  }
 }
}

variable_types: struct msg_status { char *position; int phase; int left; } *

variable_types: fd int

variable_types: wbyte int

variable_types: left int


decompiled_code:
__int64 __fastcall send_message(__int64 a1, unsigned int a2)
{
_DWORD *i; // rbp
__int64 result; // rax

for ( i = *(_DWORD **)a1; ; *(_DWORD *)(a1 + 28) = i[6] )
{
puts("Hello");
if ( *(_DWORD *)(a1 + 28) )
{
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(a1 + 32))(a1 + 16, a2);
if ( (_DWORD)result != 5 )
return result;
list_del((__int64)i);
free(i);
}
if ( list_empty((_QWORD *)a1) )
break;
i = *(_DWORD **)a1;
*(_QWORD *)(a1 + 16) = *(_QWORD *)a1 + 72LL;
}
return 5LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, unsigned int a2)
{
_DWORD *i; // rbp
__int64 result; // rax

for ( i = *(_DWORD **)a1; ; *(_DWORD *)(a1 + 28) = i[6] )
{
puts("Hello");
if ( *(_DWORD *)(a1 + 28) )
{
result = (*(__int64 (__fastcall **)(__int64, _QWORD))(a1 + 32))(a1 + 16, a2);
if ( (_DWORD)result != 5 )
return result;
func1((__int64)i);
free(i);
}
if ( func2((_QWORD *)a1) )
break;
i = *(_DWORD **)a1;
*(_QWORD *)(a1 + 16) = *(_QWORD *)a1 + 72LL;
}
return 5LL;
}

original_code:
int send_message(struct message_send_queue * queue, int skfd)
{
 int ret;
 struct message_packet * msg;
 msg = (struct message_packet *)((char *)((&queue->msg_list)->next) - ((size_t) &((struct message_packet *)0)->next));
 while (1)
 {
  printf("Hello\n");
  if (queue->status.left)
  {
   ret = queue->put_msg(&queue->status, skfd);
   if (ret == MSG_SEND_FINISH)
   {
    list_del(&msg->next);
    free(msg);
   }
   else
   {
    return ret;
   }
  }
  if (list_empty(&queue->msg_list))
   break;
  msg = (struct message_packet *)((char *)((&queue->msg_list)->next) - ((size_t) &((struct message_packet *)0)->next));
  queue->status.position = msg->content;
  queue->status.left = msg->length;
 }
 return MSG_SEND_FINISH;
}

canonical_original_code:
int send_message(struct message_send_queue *queue, int skfd)
{
 int ret;
 struct message_packet *msg;
 msg = (struct message_packet *)((char *)((&queue->msg_list)->next) - ((unsigned long) &((struct message_packet *)0)->next));
 while (1)
 {
  printf("Hello\n");
  if (queue->status.left)
  {
   ret = queue->put_msg(&queue->status, skfd);
   if (ret == MSG_SEND_FINISH)
   {
    list_del(&msg->next);
    free(msg);
   }
   else
   {
    return ret;
   }
  }
  if (list_empty(&queue->msg_list))
   break;
  msg = (struct message_packet *)((char *)((&queue->msg_list)->next) - ((unsigned long) &((struct message_packet *)0)->next));
  queue->status.position = msg->content;
  queue->status.left = msg->length;
 }
 return MSG_SEND_FINISH;
}

variable_types: struct message_send_queue { struct list_head { struct list_head *next; } msg_list; struct msg_status { char *position; int phase; int left; } status; int (*put_msg)(struct msg_status *status, int skfd); } *

variable_types: skfd int

variable_types: ret int

user_defined_types: struct message_send_queue { struct list_head msg_list; struct msg_status status; int (*put_msg)(struct msg_status *status, int skfd); }


decompiled_code:
__int64 __fastcall get_message(__int64 a1, char **a2, int a3)
{
char *v4; // r14
int v5; // ebx
signed int v6; // eax
unsigned int v7; // er8
int v8; // eax
int *v9; // rax

v4 = *a2;
v5 = *((_DWORD *)a2 + 3);
while ( 1 )
{
do
{
v6 = read(a3, v4, v5);
v7 = v6;
if ( v6 == -1 )
{
v9 = _errno_location();
v7 = 0;
if ( *v9 == 11 )
{
*a2 = v4;
v7 = 1;
*((_DWORD *)a2 + 3) = v5;
}
return v7;
}
if ( !v6 )
return v7;
v4 += v6;
v5 -= v6;
}
while ( v5 );
if ( *((_DWORD *)a2 + 2) )
break;
message_parse_head(a1);
v8 = *(_DWORD *)(a1 + 24);
*a2 = v4;
*((_DWORD *)a2 + 2) = 1;
v5 = v8 - 12;
*((_DWORD *)a2 + 3) = v8 - 12;
}
message_parse_body(a1);
return 2LL;
}

original_code:
int get_message(struct message_packet * msg, struct msg_status * status, int fd)
{
 int rbyte;
 char * p;
 int left;
 p = status->position;
 left = status->left;
 while (1)
 {
  rbyte = read(fd, p, left);
  if (rbyte == -1)
  {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)
   {
    break;
   }
   return MSG_GET_FAILED;
  }
  else if (rbyte == 0)
  {
   return MSG_GET_FAILED;
  }
  p += rbyte;
  left -= rbyte;
  if (left == 0)
  {
   if (status->phase == MSG_PHASE_GET_HEAD)
   {
    message_parse_head(msg);
    status->position = p;
    status->left = msg->length - 12;
    status->phase = MSG_PHASE_GET_BODY;
    left = status->left;
   }
   else
   {
    message_parse_body(msg);
    return MSG_GET_FINISH;
   }
  }
 }
 status->position = p;
 status->left = left;
 return MSG_GET_AGAIN;
}

canonical_original_code:
int get_message(struct message_packet *msg, struct msg_status *status, int fd)
{
 int rbyte;
 char *p;
 int left;
 p = status->position;
 left = status->left;
 while (1)
 {
  rbyte = read(fd, p, left);
  if (rbyte == -1)
  {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)
   {
    break;
   }
   return MSG_GET_FAILED;
  }
  else if (rbyte == 0)
  {
   return MSG_GET_FAILED;
  }
  p += rbyte;
  left -= rbyte;
  if (left == 0)
  {
   if (status->phase == MSG_PHASE_GET_HEAD)
   {
    message_parse_head(msg);
    status->position = p;
    status->left = msg->length - 12;
    status->phase = MSG_PHASE_GET_BODY;
    left = status->left;
   }
   else
   {
    message_parse_body(msg);
    return MSG_GET_FINISH;
   }
  }
 }
 status->position = p;
 status->left = left;
 return MSG_GET_AGAIN;
}

variable_types: rbyte int

user_defined_types: struct msg_status { char *position; int phase; int left; }


decompiled_code:
char *__fastcall initial_new_input_msg(_QWORD *a1)
{
char *result; // rax

result = (char *)malloc(0x448uLL);
a1[1] = 0xC00000000LL;
*a1 = result + 72;
return result;
}

canonical_decompiled_code:
char *__fastcall func13(_QWORD *a1)
{
char *result; // rax

result = (char *)malloc(0x448uLL);
a1[1] = 0xC00000000LL;
*a1 = result + 72;
return result;
}

original_code:
struct message_packet * initial_new_input_msg(struct msg_status * status)
{
 struct message_packet * packet;
 packet = malloc(sizeof(struct message_packet));
 status->position = packet->content;
 status->left = 12;
 status->phase = MSG_PHASE_GET_HEAD;
 return packet;
}

canonical_original_code:
struct message_packet *initial_new_input_msg(struct msg_status *status)
{
 struct message_packet *packet;
 packet = malloc(sizeof(struct message_packet));
 status->position = packet->content;
 status->left = 12;
 status->phase = MSG_PHASE_GET_HEAD;
 return packet;
}

return_type: struct message_packet { struct list_head { struct list_head *next; } next; int msg_is_ok; int type; int length; int version; union { struct { int name_len; int passwd_len; char *name; char *passwd; } login_info; struct { int from_len; int to_len; int msg_len; char *from; char *to; char *msg; } chart_info; struct { int more; char *start; char *end; } peer_list; }; char content[1024]; } *


repo: No-name/smooth

decompiled_code:
int __fastcall smooth_lib_present_buf(__int64 a1, int a2)
{
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // rsi
void *v5; // rdx

if ( a2 > 0 )
{
v2 = 0LL;
v3 = a2;
do
{
v4 = (unsigned int)*(char *)(a1 + v2);
v5 = &unk_AC;
if ( (unsigned int)v2 % 0x14 != 19 )
v5 = &unk_AE;
++v2;
printf("%02x%s", v4, v5);
}
while ( v2 != v3 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2)
{
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // rsi
void *v5; // rdx

if ( a2 > 0 )
{
v2 = 0LL;
v3 = a2;
do
{
v4 = (unsigned int)*(char *)(a1 + v2);
v5 = &unk_AC;
if ( (unsigned int)v2 % 0x14 != 19 )
v5 = &unk_AE;
++v2;
printf("%02x%s", v4, v5);
}
while ( v2 != v3 );
}
return putchar(10);
}

original_code:
void smooth_lib_present_buf(char * buf, int len)
{
 int i;
 for (i = 0; i < len; ++i)
 {
  printf("%02x%s", buf[i], i % 20 == 19 ? "\n" : " ");
 }
 printf("\n");
}

canonical_original_code:
void smooth_lib_present_buf(char *buf, int len)
{
 int i;
 for (i = 0; i < len; ++i)
 {
  printf("%02x%s", buf[i], i % 20 == 19 ? "\n" : " ");
 }
 printf("\n");
}

variable_types:
buf
char *

variable_types: i int


echoDataHandler

decompiled_code:
ssize_t __fastcall echoDataHandler(int fd)
{
ssize_t result; // rax
char buf[2064]; // [rsp+0h] [rbp-810h] BYREF

while ( 1 )
{
result = read(fd, buf, 0x800uLL);
if ( (int)result <= 0 )
break;
if ( write(fd, buf, (int)result) != (int)result )
fwrite("Write failed\n", 1uLL, 0xDuLL, stderr);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd)
{
ssize_t result; // rax
char buf[2064]; // [rsp+0h] [rbp-810h] BYREF

while ( 1 )
{
result = read(fd, buf, 0x800uLL);
if ( (int)result <= 0 )
break;
if ( write(fd, buf, (int)result) != (int)result )
fwrite("Write failed\n", 1uLL, 0xDuLL, stderr);
}
return result;
}

original_code:
static void echoDataHandler(int cfd)
{
 char buf[2048];
 int readed;
 while ((readed = read(cfd, buf, sizeof(buf))) > 0)
 {
  if (write(cfd, buf, readed) != readed)
  {
   fprintf(stderr, "Write failed\n");
  }
 }
}

variable_types: cfd int

variable_types:
buf
char [2048]

variable_types: readed int


discardDataHandler

decompiled_code:
ssize_t __fastcall discardDataHandler(int fd)
{
ssize_t result; // rax
char buf[2056]; // [rsp+0h] [rbp-810h] BYREF

do
result = read(fd, buf, 0x800uLL);
while ( result > 0 );
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(int fd)
{
ssize_t result; // rax
char buf[2056]; // [rsp+0h] [rbp-810h] BYREF

do
result = read(fd, buf, 0x800uLL);
while ( result > 0 );
return result;
}

original_code:
static void discardDataHandler(int cfd)
{
 char buf[2048];
 while (read(cfd, buf, sizeof(buf)) > 0)
  asm("");
}


initClient

decompiled_code:
__int64 __fastcall initClient(const char *a1, __int64 a2)
{
__int16 v2; // ax
struct hostent *v3; // rax
char *v4; // rdx
__int64 result; // rax
int v6; // ecx
int *v7; // rax
char *v8; // rax

if ( a1 )
{
v2 = _bswap_16_constprop_0();
*(_WORD *)(a2 + 2) = v2;
if ( v2 )
{
v3 = gethostbyname(a1);
if ( v3 )
{
v4 = *v3->h_addr_list;
result = 0LL;
v6 = *(_DWORD *)v4;
*(_WORD *)a2 = 2;
*(_DWORD *)(a2 + 4) = v6;
}
else
{
v7 = _h_errno_location();
v8 = strerror(*v7);
fprintf(stderr, "Error resolving \"%s\" : %s.\n", a1, v8);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("Bad port number.\n", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("No remote hostname given.\n", 1uLL, 0x1AuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2)
{
__int16 v2; // ax
struct hostent *v3; // rax
char *v4; // rdx
__int64 result; // rax
int v6; // ecx
int *v7; // rax
char *v8; // rax

if ( a1 )
{
v2 = func2();
*(_WORD *)(a2 + 2) = v2;
if ( v2 )
{
v3 = gethostbyname(a1);
if ( v3 )
{
v4 = *v3->h_addr_list;
result = 0LL;
v6 = *(_DWORD *)v4;
*(_WORD *)a2 = 2;
*(_DWORD *)(a2 + 4) = v6;
}
else
{
v7 = _h_errno_location();
v8 = strerror(*v7);
fprintf(stderr, "Error resolving \"%s\" : %s.\n", a1, v8);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("Bad port number.\n", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("No remote hostname given.\n", 1uLL, 0x1AuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int initClient(char *host, struct sockaddr_in *sin)
{
        struct hostent *h;
 if (!host)
  fprintf(stderr, "No remote hostname given.\n");
        else if (!(sin->sin_port = htons(4242)))
                fputs("Bad port number.\n", stderr);
        else if (!(h = gethostbyname(host)))
                fprintf(stderr, "Error resolving \"%s\" : %s.\n",
                 host, strerror((*__h_errno_location ())));
        else
        {
  memcpy(&(sin->sin_addr), h->h_addr_list[0], sizeof(sin->sin_addr));
                sin->sin_family = 2;
  return (0);
 }
 return (-1);
}

variable_types:
host
char *

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: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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


tcpServer

decompiled_code:
void __fastcall __noreturn tcpServer(void (__fastcall *a1)(_QWORD))
{
int v1; // edx
int v2; // eax
int v3; // ebx
unsigned int v4; // eax
int v5; // er12
socklen_t v6; // [rsp-34h] [rbp-3Ch] BYREF
struct sockaddr v7[3]; // [rsp-30h] [rbp-38h] BYREF

v7[0] = 0LL;
v6 = 16;
v7[0].sa_family = 2;
*(_WORD *)v7[0].sa_data = _bswap_16_constprop_0(2LL, 1LL, 0LL);
v2 = socket(2, 1, v1);
if ( v2 != -1 )
{
v3 = v2;
if ( bind(v2, v7, 0x10u) != -1 && listen(v3, 10) != -1 )
{
puts("tcpechoServer started !");
while ( 1 )
{
v4 = accept(v3, v7, &v6);
v5 = v4;
if ( v4 == -1 )
break;
a1(v4);
close(v5);
puts("Client disconnected.");
}
fwrite("Accept failed\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
perror("Socket bind failed");
exit(1);
}
perror("Socket creation failed");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void (__fastcall *a1)(_QWORD))
{
int v1; // edx
int v2; // eax
int v3; // ebx
unsigned int v4; // eax
int v5; // er12
socklen_t v6; // [rsp-34h] [rbp-3Ch] BYREF
struct sockaddr v7[3]; // [rsp-30h] [rbp-38h] BYREF

v7[0] = 0LL;
v6 = 16;
v7[0].sa_family = 2;
*(_WORD *)v7[0].sa_data = func2(2LL, 1LL, 0LL);
v2 = socket(2, 1, v1);
if ( v2 != -1 )
{
v3 = v2;
if ( bind(v2, v7, 0x10u) != -1 && listen(v3, 10) != -1 )
{
puts("tcpechoServer started !");
while ( 1 )
{
v4 = accept(v3, v7, &v6);
v5 = v4;
if ( v4 == -1 )
break;
a1(v4);
close(v5);
puts("Client disconnected.");
}
fwrite("Accept failed\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
perror("Socket bind failed");
exit(1);
}
perror("Socket creation failed");
exit(1);
}

original_code:
static void tcpServer(void (*dataHandler)(int))
{
 struct sockaddr_in sin;
 int sfd;
 int cfd;
        socklen_t sinlen;
        sinlen = sizeof(sin);
 memset(&sin, 0, sizeof(sin));
 sin.sin_family = 2;
 sin.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 sin.sin_port = htons(4242);
 if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
 {
  perror("Socket creation failed");
  exit(1);
 }
 if (bind(sfd, (const struct sockaddr *)&sin, sizeof(sin)) == -1 ||
  listen(sfd, 10) == -1)
 {
  perror("Socket bind failed");
  exit(1);
 }
 puts("tcpechoServer started !");
 while (42)
 {
  if ((cfd = accept(sfd, (struct sockaddr *)&sin, &sinlen)) == -1)
  {
   fprintf(stderr, "Accept failed\n");
   exit(1);
  }
  dataHandler(cfd);
  close(cfd);
  puts("Client disconnected.");
 }
 close(sfd);
 puts("tcpechoServer shuted down.");
}

canonical_original_code:
static void tcpServer(void (*dataHandler)(int))
{
 struct sockaddr_in sin;
 int sfd;
 int cfd;
        unsigned int sinlen;
        sinlen = sizeof(sin);
 memset(&sin, 0, sizeof(sin));
 sin.sin_family = 2;
 sin.sin_addr.s_addr = ((unsigned int) 0x00000000);
 sin.sin_port = htons(4242);
 if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
 {
  perror("Socket creation failed");
  exit(1);
 }
 if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1 ||
  listen(sfd, 10) == -1)
 {
  perror("Socket bind failed");
  exit(1);
 }
 puts("tcpechoServer started !");
 while (42)
 {
  if ((cfd = accept(sfd, (struct sockaddr *)&sin, &sinlen)) == -1)
  {
   fprintf(stderr, "Accept failed\n");
   exit(1);
  }
  dataHandler(cfd);
  close(cfd);
  puts("Client disconnected.");
 }
 close(sfd);
 puts("tcpechoServer shuted down.");
}

variable_types:
dataHandler
void (*)(int)

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: sfd int

variable_types:
sinlen
unsigned int

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


tcpechoServer

decompiled_code:
void __noreturn tcpechoServer()
{
tcpServer((void (__fastcall *)(_QWORD))echoDataHandler);
}

canonical_decompiled_code:
void __noreturn func5()
{
func4((void (__fastcall *)(_QWORD))echoDataHandler);
}

original_code:
void tcpechoServer(char **arg)
{
 tcpServer(&echoDataHandler);
}

variable_types:
arg
char **


tcpdiscardServer

decompiled_code:
void __noreturn tcpdiscardServer()
{
tcpServer((void (__fastcall *)(_QWORD))discardDataHandler);
}

canonical_decompiled_code:
void __noreturn func6()
{
func4((void (__fastcall *)(_QWORD))discardDataHandler);
}

original_code:
void tcpdiscardServer(char **arg)
{
 tcpServer(&discardDataHandler);
}


tcpechoClient

decompiled_code:
int __fastcall tcpechoClient(const char **a1)
{
int v1; // eax
int v2; // er12
unsigned int v3; // er9
unsigned __int64 v4; // r14
unsigned int v5; // ebx
double v6; // xmm2_8
__int64 v7; // rsi
double v8; // xmm1_8
struct sockaddr addr; // [rsp+10h] [rbp-830h] BYREF
char buf[2080]; // [rsp+20h] [rbp-820h] BYREF

memset(buf, 42, 1460);
if ( (unsigned int)initClient(*a1, (__int64)&addr) == -1 )
LABEL_18:
exit(1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("Socket creation failed.");
goto LABEL_18;
}
if ( connect(v1, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v3 = gl_loopcycle;
v4 = __rdtsc();
if ( gl_loopcycle )
{
v5 = 0;
do
{
if ( write(v2, buf, 0x5B4uLL) != 1460 || read(v2, buf, 0x5B4uLL) != 1460 )
{
perror("Write or read failed");
exit(1);
}
v3 = gl_loopcycle;
++v5;
}
while ( gl_loopcycle > v5 );
v6 = (double)gl_loopcycle;
}
else
{
v6 = 0.0;
}
v7 = __rdtsc() - v4;
if ( v7 < 0 )
v8 = (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1)) + (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1));
else
v8 = (double)(int)v7;
printf(
"Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n",
v7,
v8 / v6,
v3);
gl_lastavgres = v8 / (double)gl_loopcycle;
return close(v2);
}

canonical_decompiled_code:
int __fastcall func7(const char **a1)
{
int v1; // eax
int v2; // er12
unsigned int v3; // er9
unsigned __int64 v4; // r14
unsigned int v5; // ebx
double v6; // xmm2_8
__int64 v7; // rsi
double v8; // xmm1_8
struct sockaddr addr; // [rsp+10h] [rbp-830h] BYREF
char buf[2080]; // [rsp+20h] [rbp-820h] BYREF

memset(buf, 42, 1460);
if ( (unsigned int)func3(*a1, (__int64)&addr) == -1 )
LABEL_18:
exit(1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("Socket creation failed.");
goto LABEL_18;
}
if ( connect(v1, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v3 = gl_loopcycle;
v4 = __rdtsc();
if ( gl_loopcycle )
{
v5 = 0;
do
{
if ( write(v2, buf, 0x5B4uLL) != 1460 || read(v2, buf, 0x5B4uLL) != 1460 )
{
perror("Write or read failed");
exit(1);
}
v3 = gl_loopcycle;
++v5;
}
while ( gl_loopcycle > v5 );
v6 = (double)gl_loopcycle;
}
else
{
v6 = 0.0;
}
v7 = __rdtsc() - v4;
if ( v7 < 0 )
v8 = (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1)) + (double)(int)(v7 & 1 | ((unsigned __int64)v7 >> 1));
else
v8 = (double)(int)v7;
printf(
"Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n",
v7,
v8 / v6,
v3);
gl_lastavgres = v8 / (double)gl_loopcycle;
return close(v2);
}

original_code:
void tcpechoClient(char **arg)
{
        struct sockaddr_in sin;
        int sfd;
 char buf[2048];
 memset(buf, 42, 1460);
 if (initClient(*arg, &sin) == -1)
  exit(1);
 if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
 {
  perror("Socket creation failed.");
  exit(1);
 }
 if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)))
 {
  perror("Socket connect failed");
  exit(1);
 }
 { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ if (write(sfd, buf, 1460) != 1460 || read(sfd, buf, 1460) != 1460) { perror("Write or read failed"); exit(1); }; } c = rdtsc(); if (1) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
 close(sfd);
}

variable_types:
b
unsigned long long

variable_types:
c
unsigned long long

variable_types:
measureloopcnt
unsigned int


tcpbandwidthClient

decompiled_code:
int __fastcall tcpbandwidthClient(const char **a1)
{
int v1; // eax
int v2; // er15
unsigned int v3; // er9
double v4; // xmm0_8
unsigned __int64 v5; // r13
__int64 v6; // rsi
double v7; // xmm1_8
unsigned int v9; // er14
int i; // ebx
int v11; // eax
struct sockaddr addr; // [rsp+10h] [rbp-840h] BYREF
char buf[2096]; // [rsp+20h] [rbp-830h] BYREF

memset(buf, 42, 1460);
if ( (unsigned int)initClient(*a1, (__int64)&addr) == -1 )
LABEL_17:
exit(1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("Socket creation failed.");
goto LABEL_17;
}
if ( connect(v1, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v3 = gl_loopcycle;
v4 = 0.0;
v5 = __rdtsc();
if ( gl_loopcycle )
{
v9 = 0;
do
{
for ( i = 0; i >= 0; i += v11 )
{
v11 = write(v2, buf, 0x5B4uLL);
if ( v11 == -1 )
{
perror("Write failed");
exit(1);
}
}
v3 = gl_loopcycle;
++v9;
}
while ( gl_loopcycle > v9 );
v4 = (double)gl_loopcycle;
}
v6 = __rdtsc() - v5;
if ( v6 < 0 )
v7 = (double)(int)(v6 & 1 | ((unsigned __int64)v6 >> 1)) + (double)(int)(v6 & 1 | ((unsigned __int64)v6 >> 1));
else
v7 = (double)(int)v6;
printf(
"Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n",
v6,
v7 / v4,
v3);
gl_lastavgres = v7 / (double)gl_loopcycle;
return close(v2);
}

canonical_decompiled_code:
int __fastcall func8(const char **a1)
{
int v1; // eax
int v2; // er15
unsigned int v3; // er9
double v4; // xmm0_8
unsigned __int64 v5; // r13
__int64 v6; // rsi
double v7; // xmm1_8
unsigned int v9; // er14
int i; // ebx
int v11; // eax
struct sockaddr addr; // [rsp+10h] [rbp-840h] BYREF
char buf[2096]; // [rsp+20h] [rbp-830h] BYREF

memset(buf, 42, 1460);
if ( (unsigned int)func3(*a1, (__int64)&addr) == -1 )
LABEL_17:
exit(1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 == -1 )
{
perror("Socket creation failed.");
goto LABEL_17;
}
if ( connect(v1, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v3 = gl_loopcycle;
v4 = 0.0;
v5 = __rdtsc();
if ( gl_loopcycle )
{
v9 = 0;
do
{
for ( i = 0; i >= 0; i += v11 )
{
v11 = write(v2, buf, 0x5B4uLL);
if ( v11 == -1 )
{
perror("Write failed");
exit(1);
}
}
v3 = gl_loopcycle;
++v9;
}
while ( gl_loopcycle > v9 );
v4 = (double)gl_loopcycle;
}
v6 = __rdtsc() - v5;
if ( v6 < 0 )
v7 = (double)(int)(v6 & 1 | ((unsigned __int64)v6 >> 1)) + (double)(int)(v6 & 1 | ((unsigned __int64)v6 >> 1));
else
v7 = (double)(int)v6;
printf(
"Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n",
v6,
v7 / v4,
v3);
gl_lastavgres = v7 / (double)gl_loopcycle;
return close(v2);
}

original_code:
void tcpbandwidthClient(char **arg)
{
        struct sockaddr_in sin;
        int sfd;
 char buf[2048];
 int res;
 int written;
 memset(buf, 42, 1460);
 if (initClient(*arg, &sin) == -1)
  exit(1);
 if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
 {
  perror("Socket creation failed.");
  exit(1);
 }
 if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)))
 {
  perror("Socket connect failed");
  exit(1);
 }
 { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ res = 0; while (res < ((unsigned int)(1 << 30) * 2)) { if ((written = write(sfd, buf, 1460)) == -1) { perror("Write failed"); exit(1); } res += written; }; } c = rdtsc(); if (1) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
 close(sfd);
}

variable_types: res int

variable_types: written int


tcpconnectClient

decompiled_code:
int __fastcall tcpconnectClient(const char **a1)
{
const char *v1; // rdi
unsigned int v2; // er12
unsigned __int64 v3; // r13
unsigned __int64 v4; // r14
unsigned __int64 v5; // r10
int v6; // er15
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rbx
unsigned __int64 v9; // rax
struct timespec requested_time; // [rsp+0h] [rbp-50h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-40h] BYREF

v1 = *a1;
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 1000000LL;
if ( (unsigned int)initClient(v1, (__int64)&addr) == -1 )
goto LABEL_9;
v2 = 0;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
if ( gl_loopcycle )
{
while ( 1 )
{
v6 = socket(2, 1, 0);
if ( v6 == -1 )
break;
v7 = __rdtsc();
if ( connect(v6, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v4 += __rdtsc() - v7;
v8 = __rdtsc();
close(v6);
v9 = __rdtsc();
++v2;
v3 += ((unsigned int)v9 | ((unsigned __int64)HIDWORD(v9) << 32)) - v8;
nanosleep(&requested_time, 0LL);
if ( gl_loopcycle <= v2 )
{
v5 = gl_loopcycle;
goto LABEL_7;
}
}
perror("Socket creation failed.");
LABEL_9:
exit(1);
}
LABEL_7:
printf("Connection avg time : %lld\n", v4 / v5);
return printf("Close avg time : %lld\n", v3 / gl_loopcycle);
}

canonical_decompiled_code:
int __fastcall func9(const char **a1)
{
const char *v1; // rdi
unsigned int v2; // er12
unsigned __int64 v3; // r13
unsigned __int64 v4; // r14
unsigned __int64 v5; // r10
int v6; // er15
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rbx
unsigned __int64 v9; // rax
struct timespec requested_time; // [rsp+0h] [rbp-50h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-40h] BYREF

v1 = *a1;
requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 1000000LL;
if ( (unsigned int)func3(v1, (__int64)&addr) == -1 )
goto LABEL_9;
v2 = 0;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
if ( gl_loopcycle )
{
while ( 1 )
{
v6 = socket(2, 1, 0);
if ( v6 == -1 )
break;
v7 = __rdtsc();
if ( connect(v6, &addr, 0x10u) )
{
perror("Socket connect failed");
exit(1);
}
v4 += __rdtsc() - v7;
v8 = __rdtsc();
close(v6);
v9 = __rdtsc();
++v2;
v3 += ((unsigned int)v9 | ((unsigned __int64)HIDWORD(v9) << 32)) - v8;
nanosleep(&requested_time, 0LL);
if ( gl_loopcycle <= v2 )
{
v5 = gl_loopcycle;
goto LABEL_7;
}
}
perror("Socket creation failed.");
LABEL_9:
exit(1);
}
LABEL_7:
printf("Connection avg time : %lld\n", v4 / v5);
return printf("Close avg time : %lld\n", v3 / gl_loopcycle);
}

original_code:
void tcpconnectClient(char **arg)
{
 struct timespec pauseTime;
        struct sockaddr_in sin;
 unsigned long long b, c, con = 0, clo = 0;
        int sfd, i;
 pauseTime.tv_sec = 0;
 pauseTime.tv_nsec = 1000000;
 if (initClient(*arg, &sin) == -1)
  exit(1);
 for (i = 0; i < gl_loopcycle; i++)
 {
  if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
  {
   perror("Socket creation failed.");
   exit(1);
  }
  b = rdtsc();
  if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)))
  {
   perror("Socket connect failed");
   exit(1);
  }
  c = rdtsc();
  con += c - b;
  b = rdtsc();
  close(sfd);
  c = rdtsc();
  clo += c - b;
  nanosleep(&pauseTime, ((void *)0));
 }
 printf("Connection avg time : %lld\n", con / gl_loopcycle);
 printf("Close avg time : %lld\n", clo / gl_loopcycle);
}

canonical_original_code:
void tcpconnectClient(char **arg)
{
 struct timespec pauseTime;
        struct sockaddr_in sin;
 unsigned long long b; unsigned long long c; unsigned long long con = 0; unsigned long long clo = 0;
        int sfd; int i;
 pauseTime.tv_sec = 0;
 pauseTime.tv_nsec = 1000000;
 if (initClient(*arg, &sin) == -1)
  exit(1);
 for (i = 0; i < gl_loopcycle; i++)
 {
  if ((sfd = socket(2, SOCK_STREAM, 0)) == -1)
  {
   perror("Socket creation failed.");
   exit(1);
  }
  b = rdtsc();
  if (connect(sfd, (struct sockaddr *)&sin, sizeof(sin)))
  {
   perror("Socket connect failed");
   exit(1);
  }
  c = rdtsc();
  con += c - b;
  b = rdtsc();
  close(sfd);
  c = rdtsc();
  clo += c - b;
  nanosleep(&pauseTime, ((void *)0));
 }
 printf("Connection avg time : %lld\n", con / gl_loopcycle);
 printf("Close avg time : %lld\n", clo / gl_loopcycle);
}

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types:
con
unsigned long long

variable_types:
clo
unsigned long long

variable_types: i int


fsCacheSize

decompiled_code:
void fsCacheSize()
{
int v0; // eax
int v1; // ebx
unsigned int v2; // er13
unsigned int i; // er15
int v4; // eax
unsigned int v5; // er14
int v6; // eax
double v7; // xmm1_8
__int64 v8; // r11
double v9; // xmm0_8
unsigned __int64 v10; // [rsp+8h] [rbp-858h]
char templatea[16]; // [rsp+10h] [rbp-850h] BYREF
char v12[8]; // [rsp+20h] [rbp-840h] BYREF
char buf[2096]; // [rsp+30h] [rbp-830h] BYREF

strcpy(v12, "XXXXXX");
memset(buf, 0x2Au, 0x800uLL);
*(__m128i *)templatea = _mm_load_si128((const __m128i *)&LC8);
v0 = mkstemp(templatea);
if ( v0 == -1 )
{
perror("File creation failed");
exit(1);
}
v1 = v0;
printf("Temporary file name is %s\n", templatea);
v2 = 0;
for ( i = 0x8000000; i != 0x80000000; i += 0x8000000 )
{
for ( ; v2 < i; v2 += v4 )
{
v4 = write(v1, buf, (((_WORD)i - (_WORD)v2) & 0x7FF) + 1LL);
if ( v4 == -1 )
{
perror("Write error");
exit(1);
}
}
v10 = __rdtsc();
if ( gl_loopcycle )
{
v5 = 0;
do
{
if ( lseek(v1, 0LL, 0) == -1 )
{
perror("Lseek error");
exit(1);
}
do
{
v6 = read(v1, buf, 0x800uLL);
if ( v6 == -1 )
{
perror("Read error");
exit(1);
}
}
while ( v6 );
++v5;
}
while ( gl_loopcycle > v5 );
v7 = (double)gl_loopcycle;
}
else
{
v7 = 0.0;
}
v8 = __rdtsc() - v10;
if ( v8 < 0 )
v9 = (double)(int)(((unsigned __int64)v8 >> 1) | v8 & 1) + (double)(int)(((unsigned __int64)v8 >> 1) | v8 & 1);
else
v9 = (double)(int)v8;
gl_lastavgres = v9 / v7;
printf("Average cycles for file of %dMB = %f\n", v2 >> 20, v9 / v7);
}
if ( unlink(templatea) == -1 )
perror("File remove failed");
}

canonical_decompiled_code:
void func0()
{
int v0; // eax
int v1; // ebx
unsigned int v2; // er13
unsigned int i; // er15
int v4; // eax
unsigned int v5; // er14
int v6; // eax
double v7; // xmm1_8
__int64 v8; // r11
double v9; // xmm0_8
unsigned __int64 v10; // [rsp+8h] [rbp-858h]
char templatea[16]; // [rsp+10h] [rbp-850h] BYREF
char v12[8]; // [rsp+20h] [rbp-840h] BYREF
char buf[2096]; // [rsp+30h] [rbp-830h] BYREF

strcpy(v12, "XXXXXX");
memset(buf, 0x2Au, 0x800uLL);
*(__m128i *)templatea = _mm_load_si128((const __m128i *)&LC8);
v0 = mkstemp(templatea);
if ( v0 == -1 )
{
perror("File creation failed");
exit(1);
}
v1 = v0;
printf("Temporary file name is %s\n", templatea);
v2 = 0;
for ( i = 0x8000000; i != 0x80000000; i += 0x8000000 )
{
for ( ; v2 < i; v2 += v4 )
{
v4 = write(v1, buf, (((_WORD)i - (_WORD)v2) & 0x7FF) + 1LL);
if ( v4 == -1 )
{
perror("Write error");
exit(1);
}
}
v10 = __rdtsc();
if ( gl_loopcycle )
{
v5 = 0;
do
{
if ( lseek(v1, 0LL, 0) == -1 )
{
perror("Lseek error");
exit(1);
}
do
{
v6 = read(v1, buf, 0x800uLL);
if ( v6 == -1 )
{
perror("Read error");
exit(1);
}
}
while ( v6 );
++v5;
}
while ( gl_loopcycle > v5 );
v7 = (double)gl_loopcycle;
}
else
{
v7 = 0.0;
}
v8 = __rdtsc() - v10;
if ( v8 < 0 )
v9 = (double)(int)(((unsigned __int64)v8 >> 1) | v8 & 1) + (double)(int)(((unsigned __int64)v8 >> 1) | v8 & 1);
else
v9 = (double)(int)v8;
gl_lastavgres = v9 / v7;
printf("Average cycles for file of %dMB = %f\n", v2 >> 20, v9 / v7);
}
if ( unlink(templatea) == -1 )
perror("File remove failed");
}

original_code:
void fsCacheSize(char **arg)
{
 char filename[] = "./fileCacheBenchXXXXXX";
 unsigned int targetsize = 0;
 unsigned int cursize = 0;
 int fd;
 char buf[2048];
 int res;
 memset(buf, 42, 2048);
 if ((fd = mkstemp(filename)) == -1)
 {
  perror("File creation failed");
  exit(1);
 }
 printf("Temporary file name is %s\n", filename);
 for (targetsize = (1 << 27); targetsize < ((unsigned int)(1 << 30) * 2); targetsize += (1 << 27))
 {
  while (cursize < targetsize)
  {
   if ((res = write(fd, buf, ((targetsize - cursize) % 2048) + 1)) == -1)
   {
    perror("Write error");
    exit(1);
   }
   cursize += res;
  }
  { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ if (lseek(fd, 0, 0) == -1) { perror("Lseek error"); exit(1); } do { if ((res = read(fd, buf, 2048)) == -1) { perror("Read error"); exit(1); } } while (res); } c = rdtsc(); if (0) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
  printf("Average cycles for file of %dMB = %f\n", cursize / (1024 * 1024),
   gl_lastavgres);
 }
 if (unlink(filename) == -1)
  perror("File remove failed");
}

variable_types:
filename
char []

variable_types:
targetsize
unsigned int

variable_types:
cursize
unsigned int

variable_types: fd int

variable_types:
buf
char [2048]


readFile

decompiled_code:
void readFile()
{
int v0; // ebx
__pid_t v1; // eax
int v2; // eax
int v3; // er12
unsigned int v4; // er13
int v5; // eax
unsigned __int64 v6; // rax
unsigned int v7; // ebx
int v8; // eax
int v9; // eax
double v10; // xmm1_8
unsigned __int64 v11; // rsi
double v12; // xmm0_8
unsigned int v13; // ebx
int v14; // eax
double v15; // xmm2_8
__int64 v16; // rcx
double v17; // xmm0_8
int v18; // [rsp+4h] [rbp-2006Ch]
__int64 v19; // [rsp+8h] [rbp-20068h]
unsigned int v20; // [rsp+10h] [rbp-20060h]
unsigned int v21; // [rsp+14h] [rbp-2005Ch]
unsigned int v22; // [rsp+18h] [rbp-20058h]
unsigned __int64 v23; // [rsp+18h] [rbp-20058h]
char templatea[16]; // [rsp+20h] [rbp-20050h] BYREF
char v25[8]; // [rsp+30h] [rbp-20040h] BYREF
char buf[131120]; // [rsp+40h] [rbp-20030h] BYREF

strcpy(v25, "XXXXXX");
*(__m128i *)templatea = _mm_load_si128((const __m128i *)&LC8);
v0 = time(0LL);
v1 = getpid();
srandom(v1 + v0);
memset(buf, 0x2Au, 0x800uLL);
v2 = mkstemp(templatea);
if ( v2 == -1 )
{
perror("File creation failed");
exit(1);
}
v3 = v2;
v4 = 0;
printf("Temporary file name is %s\n", templatea);
v18 = 7;
v21 = 0x1000000;
do
{
while ( v4 < v21 )
{
v5 = write(v3, buf, ((v21 - v4) & 0x1FFFF) + 1LL);
if ( v5 == -1 )
{
perror("Write error");
exit(1);
}
v4 += v5;
printf("Wrote %d bytes of %d bytes\r", v4, v21);
}
v6 = __rdtsc();
LODWORD(v19) = HIDWORD(v6);
v20 = v6;
if ( gl_loopcycle )
{
v22 = 0;
do
{
v7 = 0;
do
{
v8 = random();
if ( lseek(v3, (int)((v8 - v8 % 0x20000) % (v4 - 0x20000)), 0) == -1 )
{
LABEL_29:
perror("Lseek error");
exit(1);
}
v9 = read(v3, buf, 0x20000uLL);
if ( (unsigned int)(v9 + 1) <= 1 )
{
LABEL_28:
perror("Read error");
exit(1);
}
v7 += v9;
printf("Readed %d bytes of %d bytes\r", v7, v4);
}
while ( v7 < v4 );
++v22;
}
while ( gl_loopcycle > v22 );
v10 = (double)gl_loopcycle;
}
else
{
v10 = 0.0;
}
v11 = __rdtsc() - (v20 | (unsigned __int64)(v19 << 32));
if ( (v11 & 0x8000000000000000LL) != 0LL )
v12 = (double)(int)((v11 >> 1) | v11 & 1) + (double)(int)((v11 >> 1) | v11 & 1);
else
v12 = (double)(int)v11;
gl_lastavgres = v12 / v10;
printf("Random read of %dMB file = %f cycles\n", v4 >> 20, v12 / v10);
v23 = __rdtsc();
if ( gl_loopcycle )
{
v13 = 0;
do
{
if ( lseek(v3, 0LL, 0) == -1 )
goto LABEL_29;
do
{
v14 = read(v3, buf, 0x20000uLL);
if ( v14 == -1 )
goto LABEL_28;
}
while ( v14 );
++v13;
}
while ( gl_loopcycle > v13 );
v15 = (double)gl_loopcycle;
}
else
{
v15 = 0.0;
}
v16 = __rdtsc() - v23;
if ( v16 < 0 )
v17 = (double)(int)(((unsigned __int64)v16 >> 1) | v16 & 1)
+ (double)(int)(((unsigned __int64)v16 >> 1) | v16 & 1);
else
v17 = (double)(int)v16;
gl_lastavgres = v17 / v15;
printf("Sequential read of %dMB file = %f cycles\n", v4 >> 20, v17 / v15);
v21 *= 2;
--v18;
}
while ( v18 );
if ( unlink(templatea) == -1 )
perror("File remove failed");
}

canonical_decompiled_code:
void func1()
{
int v0; // ebx
__pid_t v1; // eax
int v2; // eax
int v3; // er12
unsigned int v4; // er13
int v5; // eax
unsigned __int64 v6; // rax
unsigned int v7; // ebx
int v8; // eax
int v9; // eax
double v10; // xmm1_8
unsigned __int64 v11; // rsi
double v12; // xmm0_8
unsigned int v13; // ebx
int v14; // eax
double v15; // xmm2_8
__int64 v16; // rcx
double v17; // xmm0_8
int v18; // [rsp+4h] [rbp-2006Ch]
__int64 v19; // [rsp+8h] [rbp-20068h]
unsigned int v20; // [rsp+10h] [rbp-20060h]
unsigned int v21; // [rsp+14h] [rbp-2005Ch]
unsigned int v22; // [rsp+18h] [rbp-20058h]
unsigned __int64 v23; // [rsp+18h] [rbp-20058h]
char templatea[16]; // [rsp+20h] [rbp-20050h] BYREF
char v25[8]; // [rsp+30h] [rbp-20040h] BYREF
char buf[131120]; // [rsp+40h] [rbp-20030h] BYREF

strcpy(v25, "XXXXXX");
*(__m128i *)templatea = _mm_load_si128((const __m128i *)&LC8);
v0 = time(0LL);
v1 = getpid();
srandom(v1 + v0);
memset(buf, 0x2Au, 0x800uLL);
v2 = mkstemp(templatea);
if ( v2 == -1 )
{
perror("File creation failed");
exit(1);
}
v3 = v2;
v4 = 0;
printf("Temporary file name is %s\n", templatea);
v18 = 7;
v21 = 0x1000000;
do
{
while ( v4 < v21 )
{
v5 = write(v3, buf, ((v21 - v4) & 0x1FFFF) + 1LL);
if ( v5 == -1 )
{
perror("Write error");
exit(1);
}
v4 += v5;
printf("Wrote %d bytes of %d bytes\r", v4, v21);
}
v6 = __rdtsc();
LODWORD(v19) = HIDWORD(v6);
v20 = v6;
if ( gl_loopcycle )
{
v22 = 0;
do
{
v7 = 0;
do
{
v8 = random();
if ( lseek(v3, (int)((v8 - v8 % 0x20000) % (v4 - 0x20000)), 0) == -1 )
{
LABEL_29:
perror("Lseek error");
exit(1);
}
v9 = read(v3, buf, 0x20000uLL);
if ( (unsigned int)(v9 + 1) <= 1 )
{
LABEL_28:
perror("Read error");
exit(1);
}
v7 += v9;
printf("Readed %d bytes of %d bytes\r", v7, v4);
}
while ( v7 < v4 );
++v22;
}
while ( gl_loopcycle > v22 );
v10 = (double)gl_loopcycle;
}
else
{
v10 = 0.0;
}
v11 = __rdtsc() - (v20 | (unsigned __int64)(v19 << 32));
if ( (v11 & 0x8000000000000000LL) != 0LL )
v12 = (double)(int)((v11 >> 1) | v11 & 1) + (double)(int)((v11 >> 1) | v11 & 1);
else
v12 = (double)(int)v11;
gl_lastavgres = v12 / v10;
printf("Random read of %dMB file = %f cycles\n", v4 >> 20, v12 / v10);
v23 = __rdtsc();
if ( gl_loopcycle )
{
v13 = 0;
do
{
if ( lseek(v3, 0LL, 0) == -1 )
goto LABEL_29;
do
{
v14 = read(v3, buf, 0x20000uLL);
if ( v14 == -1 )
goto LABEL_28;
}
while ( v14 );
++v13;
}
while ( gl_loopcycle > v13 );
v15 = (double)gl_loopcycle;
}
else
{
v15 = 0.0;
}
v16 = __rdtsc() - v23;
if ( v16 < 0 )
v17 = (double)(int)(((unsigned __int64)v16 >> 1) | v16 & 1)
+ (double)(int)(((unsigned __int64)v16 >> 1) | v16 & 1);
else
v17 = (double)(int)v16;
gl_lastavgres = v17 / v15;
printf("Sequential read of %dMB file = %f cycles\n", v4 >> 20, v17 / v15);
v21 *= 2;
--v18;
}
while ( v18 );
if ( unlink(templatea) == -1 )
perror("File remove failed");
}

original_code:
void readFile(char **arg)
{
 char filename[] = "./fileCacheBenchXXXXXX";
 unsigned int targetsize = 0;
 unsigned int cursize = 0;
 int fd;
 char buf[(128 * 1024)];
 int res;
 int readed;
 srandom(time(((void *)0)) + getpid());
 memset(buf, 42, 2048);
 if ((fd = mkstemp(filename)) == -1)
 {
  perror("File creation failed");
  exit(1);
 }
 printf("Temporary file name is %s\n", filename);
 for (targetsize = (1 << 24); targetsize < (1 << 31); targetsize = targetsize << 1)
 {
  while (cursize < targetsize)
  {
   if ((res = write(fd, buf, ((targetsize - cursize) % (128 * 1024)) + 1)) == -1)
   {
    perror("Write error");
    exit(1);
   }
   cursize += res;
   printf("Wrote %d bytes of %d bytes\r", cursize, targetsize);
  }
  { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ readed = 0; do { res = random(); res = (res - (res % (128 * 1024))) % (cursize - (128 * 1024)); if (lseek(fd, res, 0) == -1) { perror("Lseek error"); exit(1); } if ((res = read(fd, buf, (128 * 1024))) == -1 || res == 0) { perror("Read error"); exit(1); } readed += res; printf("Readed %d bytes of %d bytes\r", readed, cursize); } while (readed < cursize);; } c = rdtsc(); if (0) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
  printf("Random read of %dMB file = %f cycles\n", cursize / (1024 * 1024),
   gl_lastavgres);
  { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ if (lseek(fd, 0, 0) == -1) { perror("Lseek error"); exit(1); } do { if ((res = read(fd, buf, (128 * 1024))) == -1) { perror("Read error"); exit(1); } } while (res); } c = rdtsc(); if (0) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
  printf("Sequential read of %dMB file = %f cycles\n", cursize / (1024 * 1024),
   gl_lastavgres);
 }
 if (unlink(filename) == -1)
  perror("File remove failed");
}

canonical_original_code:
void readFile(char **arg)
{
 char filename[] = "./fileCacheBenchXXXXXX";
 unsigned int targetsize = 0;
 unsigned int cursize = 0;
 int fd;
 char buf[-1];
 int res;
 int readed;
 srandom(time(((void *)0)) + getpid());
 memset(buf, 42, 2048);
 if ((fd = mkstemp(filename)) == -1)
 {
  perror("File creation failed");
  exit(1);
 }
 printf("Temporary file name is %s\n", filename);
 for (targetsize = (1 << 24); targetsize < (1 << 31); targetsize = targetsize << 1)
 {
  while (cursize < targetsize)
  {
   if ((res = write(fd, buf, ((targetsize - cursize) % (128 * 1024)) + 1)) == -1)
   {
    perror("Write error");
    exit(1);
   }
   cursize += res;
   printf("Wrote %d bytes of %d bytes\r", cursize, targetsize);
  }
  { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ readed = 0; do { res = random(); res = (res - (res % (128 * 1024))) % (cursize - (128 * 1024)); if (lseek(fd, res, 0) == -1) { perror("Lseek error"); exit(1); } if ((res = read(fd, buf, (128 * 1024))) == -1 || res == 0) { perror("Read error"); exit(1); } readed += res; printf("Readed %d bytes of %d bytes\r", readed, cursize); } while (readed < cursize);; } c = rdtsc(); if (0) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
  printf("Random read of %dMB file = %f cycles\n", cursize / (1024 * 1024),
   gl_lastavgres);
  { unsigned long long b; unsigned long long c; unsigned int measureloopcnt; b = rdtsc(); for (measureloopcnt = 0; measureloopcnt < gl_loopcycle; ++measureloopcnt){ if (lseek(fd, 0, 0) == -1) { perror("Lseek error"); exit(1); } do { if ((res = read(fd, buf, (128 * 1024))) == -1) { perror("Read error"); exit(1); } } while (res); } c = rdtsc(); if (0) printf("Total execution time is : %llu\nEach execution takes about %f clock cycles (averaging from %d iterations)\n", c - b, (double)(c - b) / (double)gl_loopcycle, gl_loopcycle); gl_lastavgres = (double)(c - b) / (double)gl_loopcycle; }
  printf("Sequential read of %dMB file = %f cycles\n", cursize / (1024 * 1024),
   gl_lastavgres);
 }
 if (unlink(filename) == -1)
  perror("File remove failed");
}

variable_types:
buf
char [-1]

variable_types: readed int