vmasm

decompiled_code:
__int64 __fastcall vmasm(int a1, const char **a2, const char *a3)
{
const char **v4; // rbp
unsigned int v5; // er14
char *v6; // r15
const char *v7; // r15
char *v8; // rbx

v4 = a2;
if ( !(unsigned int)vminitout(a3) )
{
v5 = 0;
if ( a1 <= 0 )
{
LABEL_10:
fclose(vm_ofp);
return v5;
}
while ( 1 )
{
v7 = *v4;
v8 = strrchr(*v4, 46);
if ( !v8 )
v8 = (char *)&v7[strlen(v7)];
vm_ifn = (char *)calloc(v8 - v7 + 3, 1uLL);
v6 = vm_ifn;
strncpy(vm_ifn, *v4, v8 - *v4);
strcat(v6, ".s");
printf("%s -> %s\n", v6, a3);
if ( (unsigned int)vmout(vm_ifn) )
break;
++v4;
remove(vm_ifn);
if ( v4 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_10;
}
}
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, const char **a2, const char *a3)
{
const char **v4; // rbp
unsigned int v5; // er14
char *v6; // r15
const char *v7; // r15
char *v8; // rbx

v4 = a2;
if ( !(unsigned int)func2(a3) )
{
v5 = 0;
if ( a1 <= 0 )
{
LABEL_10:
fclose(vm_ofp);
return v5;
}
while ( 1 )
{
v7 = *v4;
v8 = strrchr(*v4, 46);
if ( !v8 )
v8 = (char *)&v7[strlen(v7)];
vm_ifn = (char *)calloc(v8 - v7 + 3, 1uLL);
v6 = vm_ifn;
strncpy(vm_ifn, *v4, v8 - *v4);
strcat(v6, ".s");
printf("%s -> %s\n", v6, a3);
if ( (unsigned int)func3(vm_ifn) )
break;
++v4;
remove(vm_ifn);
if ( v4 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_10;
}
}
return 1;
}

original_code:
int vmasm(int inc, char** in, char* out)
{
 if(vminitout(out) != 0)
  return 1;
 for(int i=0; i<inc; i++)
 {
  char* sp = strrchr(in[i], '.');
  if(sp == ((void *)0))
   sp = in[i] + strlen(in[i]);
  _vm_ifn = (char*) calloc(sp-in[i]+3, sizeof(char));
  strncpy(_vm_ifn, in[i], sp-in[i]);
  strcat(_vm_ifn, ".s\0");
  printf("%s -> %s\n", _vm_ifn, out);
  if(vmout(_vm_ifn) != 0)
   return 1;
  remove(_vm_ifn);
 }
 fclose(_vm_ofp);
 return 0;
}

canonical_original_code:
int vmasm(int inc, char **in, char *out)
{
 if(vminitout(out) != 0)
  return 1;
 for(int i = 0; i<inc; i++)
 {
  char *sp = strrchr(in[i], '.');
  if(sp == ((void *)0))
   sp = in[i] + strlen(in[i]);
  _vm_ifn = (char *) calloc(sp-in[i]+3, sizeof(char));
  strncpy(_vm_ifn, in[i], sp-in[i]);
  strcat(_vm_ifn, ".s\0");
  printf("%s -> %s\n", _vm_ifn, out);
  if(vmout(_vm_ifn) != 0)
   return 1;
  remove(_vm_ifn);
 }
 fclose(_vm_ofp);
 return 0;
}

variable_types: inc int

variable_types:
in
char **

variable_types:
sp
char *


repo: lilingxin/mapred

decompiled_code:
__int64 thread_start()
{
if ( pthread_create(wet, 0LL, wthread_proc, wet) >= 0 && pthread_create(ret, 0LL, rthread_proc, ret) >= 0 )
return 0LL;
fwrite("pthread_create error\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func6()
{
if ( pthread_create(wet, 0LL, wthread_proc, wet) >= 0 && pthread_create(ret, 0LL, rthread_proc, ret) >= 0 )
return 0LL;
fwrite("pthread_create error\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int thread_start()
{
    if (pthread_create(&wet->id, ((void *)0), wthread_proc, (void*)wet) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    if (pthread_create(&ret->id, ((void *)0), rthread_proc, (void*)ret) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    return 0;
}

canonical_original_code:
int thread_start()
{
    if (pthread_create(&wet->id, ((void *)0), wthread_proc, (void *)wet) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    if (pthread_create(&ret->id, ((void *)0), rthread_proc, (void *)ret) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    return 0;
}

return_type: int


decompiled_code:
__int64 thread_term()
{
pthread_t *v0; // rdi
__int64 v1; // rbp
int v2; // ebx
pthread_t *v3; // rdi
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

event_base_free(wet[1]);
term_buf(wet + 2);
free(wet);
thread_return = 0LL;
pthread_join(*ret, &thread_return);
event_base_free(ret[1]);
v0 = ret;
if ( *((int *)ret + 4) > 0 )
{
v1 = 20LL;
v2 = 0;
do
{
v3 = &v0[v1];
++v2;
v1 += 21LL;
term_buf(v3);
v0 = ret;
}
while ( *((_DWORD *)ret + 4) > v2 );
}
free(v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
pthread_t *v0; // rdi
__int64 v1; // rbp
int v2; // ebx
pthread_t *v3; // rdi
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

event_base_free(wet[1]);
term_buf(wet + 2);
free(wet);
thread_return = 0LL;
pthread_join(*ret, &thread_return);
event_base_free(ret[1]);
v0 = ret;
if ( *((int *)ret + 4) > 0 )
{
v1 = 20LL;
v2 = 0;
do
{
v3 = &v0[v1];
++v2;
v1 += 21LL;
term_buf(v3);
v0 = ret;
}
while ( *((_DWORD *)ret + 4) > v2 );
}
free(v0);
return 0LL;
}

original_code:
int thread_term()
{
    event_base_free(wet->base);
    term_buf(&wet->buf);
    free(wet);
    void* retval = ((void *)0);
    pthread_join(ret->id, &retval);
    event_base_free(ret->base);
    for (int i = 0; i < ret->evno; ++i) {
        term_buf(&ret->evs[i].buf);
    }
    free(ret);
    return 0;
}

canonical_original_code:
int thread_term()
{
    event_base_free(wet->base);
    term_buf(&wet->buf);
    free(wet);
    void *retval = ((void *)0);
    pthread_join(ret->id, &retval);
    event_base_free(ret->base);
    for (int i = 0; i < ret->evno; ++i) {
        term_buf(&ret->evs[i].buf);
    }
    free(ret);
    return 0;
}

variable_types:
retval
void *

variable_types: i int


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(
stdout,
"%s usage : \n"
"%s [option] [INPUT] \n"
"option is :\n"
" --mapper | -m the command to execute in shell\n"
" --count | -c the process num in background\n"
" --help | -h \n",
a1,
a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(
stdout,
"%s usage : \n"
"%s [option] [INPUT] \n"
"option is :\n"
" --mapper | -m the command to execute in shell\n"
" --count | -c the process num in background\n"
" --help | -h \n",
a1,
a1);
}

original_code:
static void usage(char* proc)
{
    fprintf(stdout, "%s usage : \n"
            "%s [option] [INPUT] \n"
            "option is :\n"
            " --mapper | -m the command to execute in shell\n"
            " --count  | -c the process num in background\n"
            " --help   | -h \n"
            , proc, proc);
}

canonical_original_code:
static void usage(char *proc)
{
    fprintf(stdout, "%s usage : \n"
            "%s [option] [INPUT] \n"
            "option is :\n"
            " --mapper | -m the command to execute in shell\n"
            " --count  | -c the process num in background\n"
            " --help   | -h \n"
            , proc, proc);
}

variable_types:
proc
char *

return_type: void


decompiled_code:
void __fastcall sig_handler(int a1)
{
__int64 v2; // rbx
__pid_t v3; // edi

if ( ((unsigned int)(a1 - 2) <= 1 || a1 == 15) && count > 0 )
{
v2 = 0LL;
do
{
v3 = pids[v2++];
kill(v3, a1);
}
while ( v2 != count );
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
__int64 v2; // rbx
__pid_t v3; // edi

if ( ((unsigned int)(a1 - 2) <= 1 || a1 == 15) && count > 0 )
{
v2 = 0LL;
do
{
v3 = pids[v2++];
kill(v3, a1);
}
while ( v2 != count );
}
}

original_code:
void sig_handler(int signo)
{
    if (signo == 3 || signo == 15
        || signo == 2) {
        for (int i = 0; i < count; ++i) {
            kill(pids[i], signo);
        }
    }
}

variable_types: signo int


getopts

decompiled_code:
// write access to const memory has been detected, the output may be wrong!
__int64 __fastcall getopts(int argc, char **argv)
{
int v2; // eax
char *v3; // r13
size_t v4; // rax
const char *v6; // rdi
int v7; // eax
int v8; // ebp
int *v9; // rax
struct option v10; // [rsp+0h] [rbp-88h] BYREF
const char *v11; // [rsp+20h] [rbp-68h]
int v12; // [rsp+28h] [rbp-60h]
__int64 v13; // [rsp+30h] [rbp-58h]
int v14; // [rsp+38h] [rbp-50h]
const char *v15; // [rsp+40h] [rbp-48h]
int v16; // [rsp+48h] [rbp-40h]
__int64 v17; // [rsp+50h] [rbp-38h]
int v18; // [rsp+58h] [rbp-30h]

v10.name = "mapper";
v10.has_arg = 1;
v10.flag = 0LL;
v10.val = 109;
v11 = "count";
v12 = 1;
v13 = 0LL;
v14 = 99;
v15 = "help";
v16 = 0;
v17 = 0LL;
v18 = 104;
while ( 1 )
{
v2 = getopt_long(argc, argv, "m:c:h", &v10, 0LL);
if ( v2 == -1 )
break;
switch ( v2 )
{
case 'h':
usage(*argv);
break;
case 'm':
v3 = optarg;
memset(&cmd, 0, 0x1000uLL);
v4 = strlen(optarg);
memcpy(&cmd, v3, v4);
break;
case 'c':
count = atoi(optarg);
break;
}
}
if ( optind >= argc )
return 0LL;
v6 = argv[optind++];
v7 = open(v6, 0);
v8 = v7;
if ( v7 < 0 || dup2(v7, 0) < 0 )
{
v9 = _errno_location();
error_at_line(1, *v9, "main.c", 0x44u, "open file error");
JUMPOUT(0x1E8LL);
}
close(v8);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int argc, char **argv)
{
int v2; // eax
char *v3; // r13
size_t v4; // rax
const char *v6; // rdi
int v7; // eax
int v8; // ebp
int *v9; // rax
struct option v10; // [rsp+0h] [rbp-88h] BYREF
const char *v11; // [rsp+20h] [rbp-68h]
int v12; // [rsp+28h] [rbp-60h]
__int64 v13; // [rsp+30h] [rbp-58h]
int v14; // [rsp+38h] [rbp-50h]
const char *v15; // [rsp+40h] [rbp-48h]
int v16; // [rsp+48h] [rbp-40h]
__int64 v17; // [rsp+50h] [rbp-38h]
int v18; // [rsp+58h] [rbp-30h]

v10.name = "mapper";
v10.has_arg = 1;
v10.flag = 0LL;
v10.val = 109;
v11 = "count";
v12 = 1;
v13 = 0LL;
v14 = 99;
v15 = "help";
v16 = 0;
v17 = 0LL;
v18 = 104;
while ( 1 )
{
v2 = getopt_long(argc, argv, "m:c:h", &v10, 0LL);
if ( v2 == -1 )
break;
switch ( v2 )
{
case 'h':
func0(*argv);
break;
case 'm':
v3 = optarg;
memset(&cmd, 0, 0x1000uLL);
v4 = strlen(optarg);
memcpy(&cmd, v3, v4);
break;
case 'c':
count = atoi(optarg);
break;
}
}
if ( optind >= argc )
return 0LL;
v6 = argv[optind++];
v7 = open(v6, 0);
v8 = v7;
if ( v7 < 0 || dup2(v7, 0) < 0 )
{
v9 = _errno_location();
error_at_line(1, *v9, "main.c", 0x44u, "open file error");
JUMPOUT(0x1E8LL);
}
close(v8);
return 0LL;
}

original_code:
int getopts(int argc, char** argv)
{
    char* short_opt = "m:c:h";
    struct option long_opt[] = {
        {"mapper", 1, ((void *)0), 'm'},
        {"count", 1, ((void *)0), 'c'},
        {"help", 0, ((void *)0), 'h'}
    };
    int o = -1;
    while ((o = getopt_long(argc, argv, short_opt, long_opt, ((void *)0))) != -1) {
        switch (o) {
        case 'm':
            memset(cmd, 0, 4096);
            memcpy(cmd, optarg, strlen(optarg));
            break;
        case 'c':
            count = atoi(optarg);
            break;
        case 'h':
            usage(argv[0]);
            break;
        }
    }
    if (optind < argc) {
        int fd = open(argv[optind++], 00);
        if (fd < 0 || dup2(fd, 0) < 0) {
            error_at_line(1, (*__errno_location ()), "main.c", 68, "open file error");
        }
        close(fd);
    }
    return 0;
}

canonical_original_code:
int getopts(int argc, char **argv)
{
    char *short_opt = "m:c:h";
    struct option long_opt[] = {
        {"mapper", 1, ((void *)0), 'm'},
        {"count", 1, ((void *)0), 'c'},
        {"help", 0, ((void *)0), 'h'}
    };
    int o = -1;
    while ((o = getopt_long(argc, argv, short_opt, long_opt, ((void *)0))) != -1) {
        switch (o) {
        case 'm':
            memset(cmd, 0, 4096);
            memcpy(cmd, optarg, strlen(optarg));
            break;
        case 'c':
            count = atoi(optarg);
            break;
        case 'h':
            usage(argv[0]);
            break;
        }
    }
    if (optind < argc) {
        int fd = open(argv[optind++], 00);
        if (fd < 0 || dup2(fd, 0) < 0) {
            error_at_line(1, (*__errno_location ()), "main.c", 68, "open file error");
        }
        close(fd);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: o int

variable_types: fd int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // ebp
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

signal(13, (__sighandler_t)((char *)usage + 1));
signal(3, sig_handler);
signal(2, sig_handler);
signal(15, sig_handler);
getopts(argc, (char **)argv);
thread_init((unsigned int)count);
v6 = 0;
v7[0] = 0;
if ( count > 0 )
{
v3 = 0LL;
do
{
v4 = spawn_process(&cmd, &v6, v7);
set_cloexec(v7[0]);
set_cloexec(v6);
thread_add(v7[0], v6);
pids[v3++] = v4;
}
while ( count > (int)v3 );
}
thread_start();
wait_children();
thread_term();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // ebp
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

signal(13, (__sighandler_t)((char *)usage + 1));
signal(3, sig_handler);
signal(2, sig_handler);
signal(15, sig_handler);
func2(argc, (char **)argv);
thread_init((unsigned int)count);
v6 = 0;
v7[0] = 0;
if ( count > 0 )
{
v3 = 0LL;
do
{
v4 = spawn_process(&cmd, &v6, v7);
set_cloexec(v7[0]);
set_cloexec(v6);
thread_add(v7[0], v6);
pids[v3++] = v4;
}
while ( count > (int)v3 );
}
thread_start();
wait_children();
thread_term();
return 0;
}

original_code:
int main(int argc, char* argv[])
{
    signal(13, ((__sighandler_t) 1));
    signal(3, sig_handler);
    signal(2, sig_handler);
    signal(15, sig_handler);
    getopts(argc, argv);
    thread_init(count);
    int in = 0, out = 0;
    for (int i = 0; i < count; ++i) {
        int pid = spawn_process(cmd, &in, &out);
        set_cloexec(out);
        set_cloexec(in);
        thread_add(out, in);
        pids[i] = pid;
    }
    thread_start();
    wait_children();
    thread_term();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    signal(13, ((void (*)(int)) 1));
    signal(3, sig_handler);
    signal(2, sig_handler);
    signal(15, sig_handler);
    getopts(argc, argv);
    thread_init(count);
    int in = 0; int out = 0;
    for (int i = 0; i < count; ++i) {
        int pid = spawn_process(cmd, &in, &out);
        set_cloexec(out);
        set_cloexec(in);
        thread_add(out, in);
        pids[i] = pid;
    }
    thread_start();
    wait_children();
    thread_term();
    return 0;
}

variable_types:
argv
char *[]

variable_types: in int

variable_types: out int

variable_types: pid int


decompiled_code:
__int64 __fastcall init_buf(_QWORD *a1, int a2)
{
void *v2; // rax

a1[3] = a2;
a1[2] = 0LL;
v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 1LL;
a1[1] = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, int a2)
{
void *v2; // rax

a1[3] = a2;
a1[2] = 0LL;
v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 1LL;
a1[1] = v2;
return 0LL;
}

original_code:
int init_buf(struct buf_t* b, int alloc)
{
    b->capacity = alloc;
    b->bytes = 0;
    b->ptr = (char*) malloc(b->capacity);
    if (!b->ptr) {
        return E_ERROR;
    }
    b->cur = b->ptr;
    return E_OK;
}

canonical_original_code:
int init_buf(struct buf_t *b, int alloc)
{
    b->capacity = alloc;
    b->bytes = 0;
    b->ptr = (char *) malloc(b->capacity);
    if (!b->ptr) {
        return E_ERROR;
    }
    b->cur = b->ptr;
    return E_OK;
}

variable_types: struct buf_t { char *ptr; char *cur; unsigned long bytes; unsigned long capacity; } *

variable_types: alloc int


decompiled_code:
__int64 __fastcall term_buf(__int64 a1)
{
void *v2; // rdi
__int64 result; // rax

v2 = *(void **)a1;
if ( v2 )
free(v2);
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
void *v2; // rdi
__int64 result; // rax

v2 = *(void **)a1;
if ( v2 )
free(v2);
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)(a1 + 24) = 0LL;
return result;
}

original_code:
int term_buf(struct buf_t* b)
{
    if (b->ptr) {
        free(b->ptr);
    }
    b->ptr = b->cur = ((void *)0);
    b->capacity = b->bytes = 0;
    return E_OK;
}

canonical_original_code:
int term_buf(struct buf_t *b)
{
    if (b->ptr) {
        free(b->ptr);
    }
    b->ptr = b->cur = ((void *)0);
    b->capacity = b->bytes = 0;
    return E_OK;
}

user_defined_types: struct buf_t { char *ptr; char *cur; unsigned long bytes; unsigned long capacity; }


decompiled_code:
__int64 __fastcall try_read_more(__int64 a1, int a2)
{
const void *v4; // rsi
void *v5; // rdi
unsigned __int64 v6; // r8
unsigned __int64 v7; // rdx
char *v8; // rax
int v9; // eax
bool v10; // zf
__int64 result; // rax
__int64 v12; // rcx
int v13; // edi
char *v14; // rax
int *v15; // rax

v4 = *(const void **)(a1 + 8);
v5 = *(void **)a1;
v6 = *(_QWORD *)(a1 + 16);
if ( v4 != v5 )
{
if ( v6 )
{
memmove(v5, v4, *(_QWORD *)(a1 + 16));
v5 = *(void **)a1;
v6 = *(_QWORD *)(a1 + 16);
}
*(_QWORD *)(a1 + 8) = v5;
}
v7 = *(_QWORD *)(a1 + 24);
if ( v7 <= v6 )
{
v8 = (char *)realloc(*(void **)a1, 2 * v7);
if ( !v8 )
{
v15 = _errno_location();
error_at_line(1, *v15, "buf.c", 0x2Du, "not enough memory, realloc buf");
return get_line();
}
v12 = *(_QWORD *)(a1 + 24);
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)a1 = v8;
*(_QWORD *)(a1 + 8) = v8;
LODWORD(v7) = 2 * v12;
*(_QWORD *)(a1 + 24) = 2 * v12;
}
else
{
v8 = *(char **)(a1 + 8);
}
v9 = read(a2, &v8[v6], (int)v7 - (int)v6);
v10 = v9 == 0;
if ( v9 <= 0 )
{
result = 1LL;
if ( !v10 )
{
v13 = *_errno_location();
result = 0LL;
if ( v13 != 11 )
{
v14 = strerror(v13);
fprintf(stderr, "read error, %s\n", v14);
result = 1LL;
}
}
}
else
{
*(_QWORD *)(a1 + 16) += v9;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
const void *v4; // rsi
void *v5; // rdi
unsigned __int64 v6; // r8
unsigned __int64 v7; // rdx
char *v8; // rax
int v9; // eax
bool v10; // zf
__int64 result; // rax
__int64 v12; // rcx
int v13; // edi
char *v14; // rax
int *v15; // rax

v4 = *(const void **)(a1 + 8);
v5 = *(void **)a1;
v6 = *(_QWORD *)(a1 + 16);
if ( v4 != v5 )
{
if ( v6 )
{
memmove(v5, v4, *(_QWORD *)(a1 + 16));
v5 = *(void **)a1;
v6 = *(_QWORD *)(a1 + 16);
}
*(_QWORD *)(a1 + 8) = v5;
}
v7 = *(_QWORD *)(a1 + 24);
if ( v7 <= v6 )
{
v8 = (char *)realloc(*(void **)a1, 2 * v7);
if ( !v8 )
{
v15 = _errno_location();
error_at_line(1, *v15, "buf.c", 0x2Du, "not enough memory, realloc buf");
return func3();
}
v12 = *(_QWORD *)(a1 + 24);
v6 = *(_QWORD *)(a1 + 16);
*(_QWORD *)a1 = v8;
*(_QWORD *)(a1 + 8) = v8;
LODWORD(v7) = 2 * v12;
*(_QWORD *)(a1 + 24) = 2 * v12;
}
else
{
v8 = *(char **)(a1 + 8);
}
v9 = read(a2, &v8[v6], (int)v7 - (int)v6);
v10 = v9 == 0;
if ( v9 <= 0 )
{
result = 1LL;
if ( !v10 )
{
v13 = *_errno_location();
result = 0LL;
if ( v13 != 11 )
{
v14 = strerror(v13);
fprintf(stderr, "read error, %s\n", v14);
result = 1LL;
}
}
}
else
{
*(_QWORD *)(a1 + 16) += v9;
result = 0LL;
}
return result;
}

original_code:
int try_read_more(struct buf_t* b, int fd)
{
    if (b->cur != b->ptr) {
        if (b->bytes != 0) {
            memmove(b->ptr, b->cur, b->bytes);
        }
        b->cur = b->ptr;
    }
    if (b->bytes >= b->capacity) {
        char* new_ptr = (char*)realloc(b->ptr, b->capacity * 2);
        if (!new_ptr) {
            error_at_line(1, (*__errno_location ()), "buf.c", 45, "not enough memory, realloc buf");
        }
        b->cur = b->ptr = new_ptr;
        b->capacity *= 2;
    }
    int avail = b->capacity - b->bytes;
    int nread = read(fd, b->cur + b->bytes, avail);
    if (nread > 0) {
        b->bytes += nread;
    }
    if (nread == 0) return E_ERROR;
    if (nread < 0) {
        if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
            return E_OK;
        }
        fprintf(stderr, "read error, %s\n", strerror((*__errno_location ())));
        return E_ERROR;
    }
    return E_OK;
}

canonical_original_code:
int try_read_more(struct buf_t *b, int fd)
{
    if (b->cur != b->ptr) {
        if (b->bytes != 0) {
            memmove(b->ptr, b->cur, b->bytes);
        }
        b->cur = b->ptr;
    }
    if (b->bytes >= b->capacity) {
        char *new_ptr = (char*)realloc(b->ptr, b->capacity * 2);
        if (!new_ptr) {
            error_at_line(1, (*__errno_location ()), "buf.c", 45, "not enough memory, realloc buf");
        }
        b->cur = b->ptr = new_ptr;
        b->capacity *= 2;
    }
    int avail = b->capacity - b->bytes;
    int nread = read(fd, b->cur + b->bytes, avail);
    if (nread > 0) {
        b->bytes += nread;
    }
    if (nread == 0) return E_ERROR;
    if (nread < 0) {
        if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
            return E_OK;
        }
        fprintf(stderr, "read error, %s\n", strerror((*__errno_location ())));
        return E_ERROR;
    }
    return E_OK;
}

variable_types: avail int

variable_types: nread int


decompiled_code:
__int64 __fastcall get_line(__int64 a1, _QWORD *a2, _QWORD *a3)
{
const void *v4; // r13
char *v5; // rax
char *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

v4 = *(const void **)(a1 + 8);
v5 = (char *)memchr(v4, 10, *(_QWORD *)(a1 + 16));
if ( !v5 )
return 2LL;
v6 = v5 + 1;
*a2 = v4;
v7 = v6 - (_BYTE *)v4;
*a3 = v6 - (_BYTE *)v4;
*(_QWORD *)(a1 + 8) = v6;
result = 0LL;
*(_QWORD *)(a1 + 16) -= v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _QWORD *a2, _QWORD *a3)
{
const void *v4; // r13
char *v5; // rax
char *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

v4 = *(const void **)(a1 + 8);
v5 = (char *)memchr(v4, 10, *(_QWORD *)(a1 + 16));
if ( !v5 )
return 2LL;
v6 = v5 + 1;
*a2 = v4;
v7 = v6 - (_BYTE *)v4;
*a3 = v6 - (_BYTE *)v4;
*(_QWORD *)(a1 + 8) = v6;
result = 0LL;
*(_QWORD *)(a1 + 16) -= v7;
return result;
}

original_code:
int get_line(struct buf_t* b, char** line, size_t* len)
{
    char* bp = (char*)memchr(b->cur, '\n', b->bytes);
    if (bp == ((void *)0)) {
        return E_NEED_MORE;
    }
    *line = b->cur;
    *len = ++bp - b->cur;
    b->cur = bp;
    b->bytes -= *len;
    return E_OK;
}

canonical_original_code:
int get_line(struct buf_t *b, char **line, unsigned long *len)
{
    char *bp = (char*)memchr(b->cur, '\n', b->bytes);
    if (bp == ((void *)0)) {
        return E_NEED_MORE;
    }
    *line = b->cur;
    *len = ++bp - b->cur;
    b->cur = bp;
    b->bytes -= *len;
    return E_OK;
}

variable_types:
line
char **

variable_types:
len
unsigned long *

variable_types:
bp
char *


decompiled_code:
char *get_shell()
{
char *result; // rax

result = getenv("SHELL");
if ( !result )
result = "/bin/sh";
return result;
}

canonical_decompiled_code:
char *func0()
{
char *result; // rax

result = getenv("SHELL");
if ( !result )
result = "/bin/sh";
return result;
}

original_code:
static char* get_shell()
{
    char* shell = getenv("SHELL");
    return shell ? shell : "/bin/sh";
}

canonical_original_code:
static char *get_shell()
{
    char *shell = getenv("SHELL");
    return shell ? shell : "/bin/sh";
}

variable_types:
shell
char *

return_type: char *


decompiled_code:
char *__fastcall last_component(const char *a1)
{
return strrchr(a1, 47) + 1;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1)
{
return strrchr(a1, 47) + 1;
}

original_code:
static char* last_component(const char* str)
{
    char* bp = strrchr(str, (int)'/');
    return ++bp;
}

canonical_original_code:
static char *last_component(char *str)
{
    char *bp = strrchr(str, (int)'/');
    return ++bp;
}

variable_types:
str
char *


decompiled_code:
int __fastcall set_noblocking(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

original_code:
int set_noblocking(int fd)
{
    int flags = fcntl(fd, 1, 0);
    do { if ((flags >= 0) < 0) { error(1, (*__errno_location ()), "fcntl error"); } } while (0);
    return fcntl(fd, 2, flags | 1);
}

variable_types: flags int


decompiled_code:
// attributes: thunk
int __fastcall set_cloexec(int fd)
{
return set_noblocking(fd);
}

canonical_decompiled_code:
int __fastcall func3(int fd)
{
return func2(fd);
}

original_code:
int set_cloexec(int fd)
{
    int flags = fcntl(fd, 1, 0);
    do { if ((flags >= 0) < 0) { error(1, (*__errno_location ()), "fcntl error"); } } while (0);
    return fcntl(fd, 2, flags | 1);
}


decompiled_code:
__pid_t wait_children()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc[0] = -1;
while ( 1 )
{
result = waitpid(-1, stat_loc, 0);
if ( result <= 0 )
break;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
fprintf(stderr, "child [%d] exit by signal %d\n", (unsigned int)result, stat_loc[0] & 0x7F);
}
return result;
}

canonical_decompiled_code:
__pid_t func4()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc[0] = -1;
while ( 1 )
{
result = waitpid(-1, stat_loc, 0);
if ( result <= 0 )
break;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
fprintf(stderr, "child [%d] exit by signal %d\n", (unsigned int)result, stat_loc[0] & 0x7F);
}
return result;
}

original_code:
void wait_children()
{
    int status = -1;
    int pid = -1;
    while ( (pid = waitpid(-1, &status, 0)) > 0 ) {
        if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            int signo = ((status) & 0x7f);
            fprintf(stderr, "child [%d] exit by signal %d\n", pid, signo);
        }
    }
}

canonical_original_code:
void wait_children()
{
    int status = -1;
    int pid = -1;
    while ( (pid = waitpid(-1, &status, 0)) > 0 ) {
        if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            int signo = ((status) & 0x7f);
            fprintf(stderr, "child [%d] exit by signal %d\n", pid, signo);
        }
    }
}

variable_types: status int

variable_types: signo int


decompiled_code:
__int64 __fastcall spawn_process(__int64 a1, int *a2, char *a3)
{
__pid_t v5; // eax
unsigned int v6; // er12
int v7; // ebp
char *v8; // rax
int *v10; // rax
int *v11; // rax
int fd; // [rsp+0h] [rbp-38h] BYREF
int v13; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(&fd) < 0 || pipe(pipedes) < 0 )
{
LABEL_10:
v10 = _errno_location();
error(1, *v10, "pipe error");
goto LABEL_11;
}
v5 = fork();
v6 = v5;
if ( v5 < 0 )
{
LABEL_11:
v11 = _errno_location();
error(1, *v11, "fork error");
JUMPOUT(0x22BLL);
}
if ( !v5 )
{
close(fd);
close(pipedes[1]);
v7 = dup2(pipedes[0], 0);
a2 = _errno_location();
if ( v7 >= 0 && dup2(v13, 1) >= 0 )
{
close(pipedes[0]);
a3 = get_shell();
v8 = last_component(a3);
execl(a3, v8, "-c", a1, 0LL);
error(1, *a2, "execl error");
goto LABEL_8;
}
error(1, *a2, "dup2 error");
goto LABEL_10;
}
LABEL_8:
close(v13);
close(pipedes[0]);
*a2 = fd;
*(_DWORD *)a3 = pipedes[1];
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int *a2, char *a3)
{
__pid_t v5; // eax
unsigned int v6; // er12
int v7; // ebp
char *v8; // rax
int *v10; // rax
int *v11; // rax
int fd; // [rsp+0h] [rbp-38h] BYREF
int v13; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(&fd) < 0 || pipe(pipedes) < 0 )
{
LABEL_10:
v10 = _errno_location();
error(1, *v10, "pipe error");
goto LABEL_11;
}
v5 = fork();
v6 = v5;
if ( v5 < 0 )
{
LABEL_11:
v11 = _errno_location();
error(1, *v11, "fork error");
JUMPOUT(0x22BLL);
}
if ( !v5 )
{
close(fd);
close(pipedes[1]);
v7 = dup2(pipedes[0], 0);
a2 = _errno_location();
if ( v7 >= 0 && dup2(v13, 1) >= 0 )
{
close(pipedes[0]);
a3 = func0();
v8 = func1(a3);
execl(a3, v8, "-c", a1, 0LL);
error(1, *a2, "execl error");
goto LABEL_8;
}
error(1, *a2, "dup2 error");
goto LABEL_10;
}
LABEL_8:
close(v13);
close(pipedes[0]);
*a2 = fd;
*(_DWORD *)a3 = pipedes[1];
return v6;
}

original_code:
int spawn_process(const char* cmd, int* in, int* out)
{
    int in_fds[2], out_fds[2];
    if (pipe(in_fds) < 0 || pipe(out_fds) < 0) {
        error(1, (*__errno_location ()), "pipe error");
    }
    int pid = fork();
    if (pid < 0) {
        error(1, (*__errno_location ()), "fork error");
    }
    if (pid == 0) {
        close(in_fds[0]);
        close(out_fds[1]);
        if (dup2(out_fds[0], 0) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        if (dup2(in_fds[1], 1) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        close(out_fds[0]);
        char* shell = get_shell();
        execl(shell, last_component(shell), "-c", cmd, ((void *)0));
        error(1, (*__errno_location ()), "execl error");
    }
    close(in_fds[1]);
    close(out_fds[0]);
    *in = in_fds[0];
    *out = out_fds[1];
    return pid;
}

canonical_original_code:
int spawn_process(char *cmd, int *in, int *out)
{
    int in_fds[2]; int out_fds[2];
    if (pipe(in_fds) < 0 || pipe(out_fds) < 0) {
        error(1, (*__errno_location ()), "pipe error");
    }
    int pid = fork();
    if (pid < 0) {
        error(1, (*__errno_location ()), "fork error");
    }
    if (pid == 0) {
        close(in_fds[0]);
        close(out_fds[1]);
        if (dup2(out_fds[0], 0) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        if (dup2(in_fds[1], 1) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        close(out_fds[0]);
        char *shell = get_shell();
        execl(shell, last_component(shell), "-c", cmd, ((void *)0));
        error(1, (*__errno_location ()), "execl error");
    }
    close(in_fds[1]);
    close(out_fds[0]);
    *in = in_fds[0];
    *out = out_fds[1];
    return pid;
}

variable_types:
cmd
char *

variable_types:
in
int *

variable_types:
out
int *


repo: limoges/luma

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
putchar(10);
printf("%s", "\x1B[1mET_BRIGHT \x1B[0m");
printf("%s", "\x1B[2mET_FAINT \x1B[0m");
printf("%s", "\x1B[3mET_ITALIC \x1B[0m");
printf("%s", "\x1B[4mET_UNDERLINE\x1B[0m");
putchar(10);
printf("%s", "\x1B[5mET_BLINK \x1B[0m");
printf("%s", "\x1B[7mET_REVERSE \x1B[0m");
printf("%s", "\x1B[9mET_CROSSED \x1B[0m");
putchar(10);
printf("%s", "\x1B[30mEF_BLACK \x1B[0m");
printf("%s", "\x1B[31mEF_RED \x1B[0m");
printf("%s", "\x1B[32mEF_GREEN \x1B[0m");
printf("%s", "\x1B[33mEF_YELLOW \x1B[0m");
putchar(10);
printf("%s", "\x1B[34mEF_BLUE \x1B[0m");
printf("%s", "\x1B[35mEF_MAGENTA \x1B[0m");
printf("%s", "\x1B[36mEF_CYAN \x1B[0m");
printf("%s", "\x1B[37mEF_WHITE \x1B[0m");
putchar(10);
printf("%s", "\x1B[40mEB_BLACK \x1B[0m");
printf("%s", "\x1B[41mEB_RED \x1B[0m");
printf("%s", "\x1B[42mEB_GREEN \x1B[0m");
printf("%s", "\x1B[43mEB_YELLOW \x1B[0m");
putchar(10);
printf("%s", "\x1B[44mEB_BLUE \x1B[0m");
printf("%s", "\x1B[45mEB_MAGENTA \x1B[0m");
printf("%s", "\x1B[46mEB_CYAN \x1B[0m");
printf("%s", "\x1B[47mEB_WHITE \x1B[0m");
exit(0);
}


repo: lindenb/hershey

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er13
int v5; // eax
int v6; // ebx
const unsigned __int16 **v7; // rbp
__int64 v8; // rsi
int v9; // er13
int v10; // er12
int v11; // ebx
int v12; // ebx
int v13; // eax
int v14; // eax

LABEL_1:
v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = fgetc(stdin);
v6 = v5;
if ( v5 == -1 )
return 0;
if ( v5 == 10 )
{
v3 = 0;
}
else
{
++v3;
v7 = __ctype_b_loc();
if ( ((*v7)[v6] & 0x2000) == 0 )
v4 = v6 + 10 * v4 - 48;
if ( v3 == 5 )
{
if ( !v4 )
return v4;
v8 = v4;
v9 = 3;
printf("%d\t", v8);
v10 = 0;
do
{
v11 = fgetc(stdin);
fputc(v11, _bss_start);
if ( ((*v7)[v11] & 0x2000) == 0 )
v10 = v11 + 10 * v10 - 48;
--v9;
}
while ( v9 );
if ( v10 > 0 )
{
v12 = 0;
do
{
++v12;
v13 = fgetskipcr();
putchar(v13);
v14 = fgetskipcr();
putchar(v14);
}
while ( v10 != v12 );
}
printf("\t%d", (unsigned int)v10);
fputc(10, _bss_start);
if ( fgetc(stdin) != 10 )
{
v4 = -1;
fwrite("syntax error\n", 1uLL, 0xDuLL, stderr);
return v4;
}
goto LABEL_1;
}
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er13
int v5; // eax
int v6; // ebx
const unsigned __int16 **v7; // rbp
__int64 v8; // rsi
int v9; // er13
int v10; // er12
int v11; // ebx
int v12; // ebx
int v13; // eax
int v14; // eax

LABEL_1:
v3 = 0;
v4 = 0;
while ( 1 )
{
v5 = fgetc(stdin);
v6 = v5;
if ( v5 == -1 )
return 0;
if ( v5 == 10 )
{
v3 = 0;
}
else
{
++v3;
v7 = __ctype_b_loc();
if ( ((*v7)[v6] & 0x2000) == 0 )
v4 = v6 + 10 * v4 - 48;
if ( v3 == 5 )
{
if ( !v4 )
return v4;
v8 = v4;
v9 = 3;
printf("%d\t", v8);
v10 = 0;
do
{
v11 = fgetc(stdin);
fputc(v11, _bss_start);
if ( ((*v7)[v11] & 0x2000) == 0 )
v10 = v11 + 10 * v10 - 48;
--v9;
}
while ( v9 );
if ( v10 > 0 )
{
v12 = 0;
do
{
++v12;
v13 = func7();
putchar(v13);
v14 = func7();
putchar(v14);
}
while ( v10 != v12 );
}
printf("\t%d", (unsigned int)v10);
fputc(10, _bss_start);
if ( fgetc(stdin) != 10 )
{
v4 = -1;
fwrite("syntax error\n", 1uLL, 0xDuLL, stderr);
return v4;
}
goto LABEL_1;
}
}
}
}

original_code:
int main(int argc,char** argv)
 {
 for(;;)
  {
  int letter_id=0,num_vertices=0,i=0,vertex;
  int c;
  while(i< 5)
   {
   c=fgetc(stdin);
   if(c==(-1)) { letter_id=0; break;}
   if(c=='\n') {i=0;continue;}
   i++;
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace)) continue;
   letter_id=letter_id*10+(c-'0');
   }
  if(letter_id==0) break;
  printf("%d\t",letter_id);
  for( i=0;i< 3;++i)
   {
   int c=fgetc(stdin);
   fprintf(stdout,"%c",c);
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace)) continue;
   num_vertices = num_vertices*10+(c-'0');
   }
  for( vertex=0;vertex < num_vertices;++vertex )
   {
   int c1 = fgetskipcr();
   printf("%c",c1);
   c1 = fgetskipcr();
   printf("%c",c1);
   }
  printf("\t%d",num_vertices);
  fputc('\n',stdout);
  if((c=fgetc(stdin))!='\n')
   {
   fprintf(stderr, "syntax error\n");
   return -1;
   }
  }
 return 0;
 }

canonical_original_code:
int main(int argc, char **argv)
 {
 for(;;)
  {
  int letter_id = 0; int num_vertices = 0; int i = 0; int vertex;
  int c;
  while(i< 5)
   {
   c=fgetc(stdin);
   if(c==(-1)) { letter_id=0; break;}
   if(c=='\n') {i=0;continue;}
   i++;
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISspace)) continue;
   letter_id=letter_id*10+(c-'0');
   }
  if(letter_id==0) break;
  printf("%d\t",letter_id);
  for( i=0;i< 3;++i)
   {
   int c = fgetc(stdin);
   fprintf(stdout,"%c",c);
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISspace)) continue;
   num_vertices = num_vertices*10+(c-'0');
   }
  for( vertex=0;vertex < num_vertices;++vertex )
   {
   int c1 = fgetskipcr();
   printf("%c",c1);
   c1 = fgetskipcr();
   printf("%c",c1);
   }
  printf("\t%d",num_vertices);
  fputc('\n',stdout);
  if((c=fgetc(stdin))!='\n')
   {
   fprintf(stderr, "syntax error\n");
   return -1;
   }
  }
 return 0;
 }

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: vertex int

variable_types: c int


fgetskipcr

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

do
{
result = fgetc(stdin);
if ( result == -1 )
{
fwrite("Expected char but got EOF", 1uLL, 0x19uLL, stderr);
exit(1);
}
}
while ( result == 10 );
return result;
}

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

do
{
result = fgetc(stdin);
if ( result == -1 )
{
fwrite("Expected char but got EOF", 1uLL, 0x19uLL, stderr);
exit(1);
}
}
while ( result == 10 );
return result;
}

original_code:
static int fgetskipcr()
 {
 int c;
 while((c=fgetc(stdin))!=(-1))
  {
  if(c=='\n') continue;
  return c;
  }
 fprintf(stderr,"Expected char but got EOF");
 exit(1);
 }


connectTCP

repo: linfan/TCP-IP-Network-basic-examples

decompiled_code:
__int64 __fastcall connectTCP(char *a1, char a2)
{
return clientsock(a1, a2, (char)&unk_402004, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, char a2)
{
return func12(a1, a2, (char)&unk_402004, 0LL);
}

original_code:
int connectTCP(const char* host, const char* port)
{
    return clientsock(host, port, "TCP", ((void *)0));
}

canonical_original_code:
int connectTCP(char *host, char *port)
{
    return clientsock(host, port, "TCP", ((void *)0));
}

variable_types:
host
char *

variable_types:
port
char *


readn

decompiled_code:
size_t __fastcall readn(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
v5 = n;
if ( !n )
return v3;
do
{
while ( 1 )
{
v6 = recv(fd, buf, v5, 0);
if ( v6 < 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
}
while ( *__errno_location() == 4 );
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func8(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
v5 = n;
if ( !n )
return v3;
do
{
while ( 1 )
{
v6 = recv(fd, buf, v5, 0);
if ( v6 < 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
}
while ( *__errno_location() == 4 );
return -1LL;
}

original_code:
ssize_t readn(int fd, void* vptr, size_t n)
{
    size_t nleft;
    ssize_t nread;
    char* ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0)
    {
        if ((nread = recv(fd, ptr, nleft, 0)) < 0)
        {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return -1;
        }
        nleft -= nread;
        ptr += nread;
    }
    if (n - nleft < 0)
        echo("[CLIENT] Error: readn error.\n");
    return (n - nleft);
}

canonical_original_code:
long readn(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0)
    {
        if ((nread = recv(fd, ptr, nleft, 0)) < 0)
        {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return -1;
        }
        nleft -= nread;
        ptr += nread;
    }
    if (n - nleft < 0)
        echo("[CLIENT] Error: readn error.\n");
    return (n - nleft);
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *

return_type: long


errexit

decompiled_code:
void __noreturn errexit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func9(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
int errexit(const char* format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    exit(1);
}

canonical_original_code:
int errexit(char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    exit(1);
}

variable_types:
format
char *


echo

decompiled_code:
int echo(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void echo(const char* format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
}

canonical_original_code:
void echo(char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r15
int v4; // er12
int i; // ebp
__pid_t v6; // er14
int v8; // er12
size_t v9; // rax
char v10; // [rsp+0h] [rbp-1058h]
const char *v11; // [rsp+0h] [rbp-1058h]
int v12; // [rsp+8h] [rbp-1050h]
int v13; // [rsp+Ch] [rbp-104Ch]
char s[10]; // [rsp+16h] [rbp-1042h] BYREF
char buf[4152]; // [rsp+20h] [rbp-1038h] BYREF

if ( argc == 6 )
{
v3 = argv[2];
v11 = argv[1];
v4 = atoi(argv[3]);
v12 = atoi(argv[4]);
v13 = atoi(argv[5]);
snprintf(s, 0xAuLL, "%d", (unsigned int)v13);
if ( v4 <= 0 )
return 0;
}
else
{
v4 = 10;
echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n", v10);
v3 = "10011";
snprintf(s, 0xAuLL, "%d", 4000LL);
v13 = 4000;
v12 = 500;
v11 = "127.0.0.1";
}
for ( i = 0; i < v4; ++i )
{
v6 = fork();
if ( !v6 )
{
if ( v12 > 0 )
{
do
{
v8 = connectTCP(v11, v3);
v9 = strlen(s);
send(v8, s, v9, 0);
if ( v13 != readn(v8, buf, v13) )
echo("[CLIENT] Error: request %d bytes, received %d bytes\n", (char)v11);
++v6;
close(v8);
}
while ( v6 != v12 );
}
echo("[CLIENT] child %d done.\n", (char)v11);
exit(0);
}
while ( wait(0LL) > 0 )
;
if ( *__errno_location() != 10 )
errexit("[CLIENT] wait failed.\n", 10);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r15
int v4; // er12
int i; // ebp
__pid_t v6; // er14
int v8; // er12
size_t v9; // rax
char v10; // [rsp+0h] [rbp-1058h]
const char *v11; // [rsp+0h] [rbp-1058h]
int v12; // [rsp+8h] [rbp-1050h]
int v13; // [rsp+Ch] [rbp-104Ch]
char s[10]; // [rsp+16h] [rbp-1042h] BYREF
char buf[4152]; // [rsp+20h] [rbp-1038h] BYREF

if ( argc == 6 )
{
v3 = argv[2];
v11 = argv[1];
v4 = atoi(argv[3]);
v12 = atoi(argv[4]);
v13 = atoi(argv[5]);
snprintf(s, 0xAuLL, "%d", (unsigned int)v13);
if ( v4 <= 0 )
return 0;
}
else
{
v4 = 10;
func10("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n", v10);
v3 = "10011";
snprintf(s, 0xAuLL, "%d", 4000LL);
v13 = 4000;
v12 = 500;
v11 = "127.0.0.1";
}
for ( i = 0; i < v4; ++i )
{
v6 = fork();
if ( !v6 )
{
if ( v12 > 0 )
{
do
{
v8 = func7(v11, v3);
v9 = strlen(s);
send(v8, s, v9, 0);
if ( v13 != func8(v8, buf, v13) )
func10("[CLIENT] Error: request %d bytes, received %d bytes\n", (char)v11);
++v6;
close(v8);
}
while ( v6 != v12 );
}
func10("[CLIENT] child %d done.\n", (char)v11);
exit(0);
}
while ( wait(0LL) > 0 )
;
if ( *__errno_location() != 10 )
func9("[CLIENT] wait failed.\n", 10);
}
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    int i, j, fd;
    pid_t pid;
    ssize_t n;
    char request[10], reply[4096];
    char *hostname = "127.0.0.1";
    char *port = "10011";
    int nchildren = 10;
    int nloops = 500;
    int nbytes = 4000;
    if (argc != 6)
        echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n");
    else
    {
        hostname = argv[1];
        port = argv[2];
        nchildren = atoi(argv[3]);
        nloops = atoi(argv[4]);
        nbytes = atoi(argv[5]);
    }
    snprintf(request, sizeof(request), "%d", nbytes);
    for (i = 0; i < nchildren; ++i)
    {
        if ((pid = fork()) == 0)
        {
            for (j = 0; j < nloops; ++j)
            {
                fd = connectTCP(hostname, port);
                send(fd, request, strlen(request), 0);
                if ((n = readn(fd, reply, nbytes)) != nbytes)
                    echo("[CLIENT] Error: request %d bytes, received %d bytes\n", nbytes, n);
                close(fd);
            }
            echo("[CLIENT] child %d done.\n", i);
            exit(0);
        }
        while(wait(((void *)0)) > 0)
            ;
        if ((*__errno_location ()) != 10)
            errexit("[CLIENT] wait failed.\n");
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int i; int j; int fd;
    int pid;
    long n;
    char request[10]; char reply[4096];
    char *hostname = "127.0.0.1";
    char *port = "10011";
    int nchildren = 10;
    int nloops = 500;
    int nbytes = 4000;
    if (argc != 6)
        echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n");
    else
    {
        hostname = argv[1];
        port = argv[2];
        nchildren = atoi(argv[3]);
        nloops = atoi(argv[4]);
        nbytes = atoi(argv[5]);
    }
    snprintf(request, sizeof(request), "%d", nbytes);
    for (i = 0; i < nchildren; ++i)
    {
        if ((pid = fork()) == 0)
        {
            for (j = 0; j < nloops; ++j)
            {
                fd = connectTCP(hostname, port);
                send(fd, request, strlen(request), 0);
                if ((n = readn(fd, reply, nbytes)) != nbytes)
                    echo("[CLIENT] Error: request %d bytes, received %d bytes\n", nbytes, n);
                close(fd);
            }
            echo("[CLIENT] child %d done.\n", i);
            exit(0);
        }
        while(wait(((void *)0)) > 0)
            ;
        if ((*__errno_location ()) != 10)
            errexit("[CLIENT] wait failed.\n");
    }
    return 0;
}

variable_types:
argv
char *[]

variable_types: j int

variable_types: pid int

variable_types: n long

variable_types:
request
char [10]

variable_types:
reply
char [4096]

variable_types:
hostname
char *

variable_types: nchildren int

variable_types: nloops int

variable_types: nbytes int


recvFromServer

decompiled_code:
__int64 __fastcall recvFromServer(int fd, __int64 a2, int a3)
{
unsigned int v3; // er14
int *v5; // rax
int v6; // edx
int *v7; // r13
int v8; // eax
char v10; // [rsp+0h] [rbp-28h]

v3 = 0;
v5 = __errno_location();
v6 = a3;
*v5 = 0;
v7 = v5;
if ( a3 <= 0 )
return 0;
while ( 1 )
{
v8 = recv(fd, (void *)(a2 + (int)v3), v6, 64);
if ( v8 <= 0 )
break;
v3 += v8;
v6 = a3 - v3;
if ( (int)(a3 - v3) <= 0 )
return v3;
}
if ( !v3 )
{
if ( *v7 == 11 )
{
v3 = -1;
echo("[CLIENT] no message received.\n", v10);
return v3;
}
return 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, __int64 a2, int a3)
{
unsigned int v3; // er14
int *v5; // rax
int v6; // edx
int *v7; // r13
int v8; // eax
char v10; // [rsp+0h] [rbp-28h]

v3 = 0;
v5 = __errno_location();
v6 = a3;
*v5 = 0;
v7 = v5;
if ( a3 <= 0 )
return 0;
while ( 1 )
{
v8 = recv(fd, (void *)(a2 + (int)v3), v6, 64);
if ( v8 <= 0 )
break;
v3 += v8;
v6 = a3 - v3;
if ( (int)(a3 - v3) <= 0 )
return v3;
}
if ( !v3 )
{
if ( *v7 == 11 )
{
v3 = -1;
func12("[CLIENT] no message received.\n", v10);
return v3;
}
return 0;
}
return v3;
}

original_code:
int recvFromServer(int sockfd, char* buf, int buf_size)
{
    int n, offset = 0;
    (*__errno_location ()) = 0;
    while (buf_size - offset > 0 &&
            (n = recv(sockfd, buf + offset, buf_size - offset, MSG_DONTWAIT)) > 0)
    {
        offset += n;
    }
    if (offset == 0 && (*__errno_location ()) == 11)
    {
        echo("[CLIENT] no message received.\n");
        return -1;
    }
    else
    {
        return offset;
    }
}

canonical_original_code:
int recvFromServer(int sockfd, char *buf, int buf_size)
{
    int n; int offset = 0;
    (*__errno_location ()) = 0;
    while (buf_size - offset > 0 &&
            (n = recv(sockfd, buf + offset, buf_size - offset, MSG_DONTWAIT)) > 0)
    {
        offset += n;
    }
    if (offset == 0 && (*__errno_location ()) == 11)
    {
        echo("[CLIENT] no message received.\n");
        return -1;
    }
    else
    {
        return offset;
    }
}

variable_types: sockfd int

variable_types:
buf
char *

variable_types: n int

variable_types: offset int


errexit

canonical_decompiled_code:
void __noreturn func11(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}


echo

canonical_decompiled_code:
int func12(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned __int16 v4; // ax
struct in_addr v5; // er8
unsigned __int16 v7; // r12
const char *v8; // rbp
int *v9; // rax
char v10; // al
int *v11; // rax
char v12; // al
struct in_addr in[4]; // [rsp+0h] [rbp-28h] BYREF

switch ( argc )
{
case 2:
v8 = argv[1];
v7 = 10012;
break;
case 3:
v8 = argv[1];
v7 = atoi(argv[2]);
break;
case 1:
v7 = 10012;
v8 = "127.0.0.1";
break;
default:
errexit("usage: %s [host [port]]\n", (char)*argv);
}
if ( !gethostbyname(v8) )
errexit("gethostbyname failed.\n", (char)argv);
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
v11 = __errno_location();
v12 = (unsigned __int8)strerror(*v11);
errexit("create socket failed: %s\n", v12);
}
*(_OWORD *)&in[0].s_addr = 0LL;
v4 = _bswap_16(v7);
_bswap_16(v4);
inet_ntoa(v5);
echo("[CLIENT] server addr: %s, port: %u\n", 2);
if ( connect(v3, (const struct sockaddr *)in, 0x10u) < 0 )
{
v9 = __errno_location();
v10 = (unsigned __int8)strerror(*v9);
errexit("connect to server failed: %s\n", v10);
}
inet_ntoa(in[1]);
echo("[CLIENT] connected to server %s\n", in[0].s_addr);
process(_bss_start, v3);
close(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned __int16 v4; // ax
struct in_addr v5; // er8
unsigned __int16 v7; // r12
const char *v8; // rbp
int *v9; // rax
char v10; // al
int *v11; // rax
char v12; // al
struct in_addr in[4]; // [rsp+0h] [rbp-28h] BYREF

switch ( argc )
{
case 2:
v8 = argv[1];
v7 = 10012;
break;
case 3:
v8 = argv[1];
v7 = atoi(argv[2]);
break;
case 1:
v7 = 10012;
v8 = "127.0.0.1";
break;
default:
func11("usage: %s [host [port]]\n", (char)*argv);
}
if ( !gethostbyname(v8) )
func11("gethostbyname failed.\n", (char)argv);
v3 = socket(2, 1, 0);
if ( v3 < 0 )
{
v11 = __errno_location();
v12 = (unsigned __int8)strerror(*v11);
func11("create socket failed: %s\n", v12);
}
*(_OWORD *)&in[0].s_addr = 0LL;
v4 = func7(v7);
func7(v4);
inet_ntoa(v5);
func12("[CLIENT] server addr: %s, port: %u\n", 2);
if ( connect(v3, (const struct sockaddr *)in, 0x10u) < 0 )
{
v9 = __errno_location();
v10 = (unsigned __int8)strerror(*v9);
func11("connect to server failed: %s\n", v10);
}
inet_ntoa(in[1]);
func12("[CLIENT] connected to server %s\n", in[0].s_addr);
func10(_bss_start, v3);
close(v3);
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    int sock;
    struct hostent *hent;
    struct sockaddr_in server;
    char *host = "127.0.0.1";
    unsigned short port = 10012;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            port = atoi(argv[2]);
            break;
        default:
            errexit("usage: %s [host [port]]\n", argv[0]);
    }
    if ((hent = gethostbyname(host)) == ((void *)0))
        errexit("gethostbyname failed.\n");
    if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
        errexit("create socket failed: %s\n", strerror((*__errno_location ())));
    bzero(&server, sizeof(server));
    server.sin_family = 2;
    server.sin_port = htons(port);
    server.sin_addr = *((struct in_addr*)hent->h_addr_list[0]);
    echo("[CLIENT] server addr: %s, port: %u\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
    if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
        errexit("connect to server failed: %s\n", strerror((*__errno_location ())));
    echo("[CLIENT] connected to server %s\n", inet_ntoa(server.sin_addr));
    process(stdin, sock);
    close(sock);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int sock;
    struct hostent *hent;
    struct sockaddr_in server;
    char *host = "127.0.0.1";
    unsigned short port = 10012;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            port = atoi(argv[2]);
            break;
        default:
            errexit("usage: %s [host [port]]\n", argv[0]);
    }
    if ((hent = gethostbyname(host)) == ((void *)0))
        errexit("gethostbyname failed.\n");
    if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
        errexit("create socket failed: %s\n", strerror((*__errno_location ())));
    bzero(&server, sizeof(server));
    server.sin_family = 2;
    server.sin_port = htons(port);
    server.sin_addr = *((struct in_addr *)hent->h_addr_list[0]);
    echo("[CLIENT] server addr: %s, port: %u\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
    if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
        errexit("connect to server failed: %s\n", strerror((*__errno_location ())));
    echo("[CLIENT] connected to server %s\n", inet_ntoa(server.sin_addr));
    process(stdin, sock);
    close(sock);
}

variable_types: argc int

variable_types: sock int

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

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

variable_types:
host
char *

variable_types:
port
unsigned short

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

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


connectUDP

decompiled_code:
__int64 __fastcall connectUDP(char *a1, char a2, void *a3)
{
return clientsock(a1, a2, (char)&unk_402004, a3);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, char a2, void *a3)
{
return func10(a1, a2, (char)&unk_402004, a3);
}

original_code:
int connectUDP(const char *host, const char *service, struct sockaddr_in* sin)
{
    return clientsock(host, service, "UDP", sin);
}

canonical_original_code:
int connectUDP(char *host, char *service, struct sockaddr_in *sin)
{
    return clientsock(host, service, "UDP", sin);
}

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


errexit

variable_types:
format
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // rdx
char *v6; // rax
const char *v8; // rsi
const char *v9; // rdi
int *v10; // rax
char v11; // al
socklen_t addr_len; // [rsp+4h] [rbp-24h] BYREF
time_t buf; // [rsp+8h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

switch ( argc )
{
case 2:
v9 = argv[1];
v8 = "10010";
break;
case 3:
v9 = argv[1];
v8 = argv[2];
break;
case 1:
v8 = "10010";
v9 = "127.0.0.1";
break;
default:
errexit("usage: UDPtime [host [port]]\n", (char)argv);
}
v3 = connectUDP(v9, v8, &addr);
fwrite("[CLIENT] sending message to service..\n", 1uLL, 0x26uLL, stderr);
sendto(v3, "what time is it ?\n", 0x12uLL, 0, &addr, 0x10u);
fwrite("[CLIENT] message sent to service, waiting for respond..\n", 1uLL, 0x38uLL, stderr);
addr_len = 16;
if ( (int)recvfrom(v3, &buf, 8uLL, 0, &addr, &addr_len) < 0 )
{
v10 = __errno_location();
v11 = (unsigned __int8)strerror(*v10);
errexit("read failed: %s\n", v11);
}
v4 = _bswap_32((unsigned int)buf, &buf, -2208988800LL);
buf = v5 + v4;
v6 = ctime(&buf);
fprintf(stderr, "[CLIENT] now time is : %s\n", v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
unsigned int v4; // eax
__int64 v5; // rdx
char *v6; // rax
const char *v8; // rsi
const char *v9; // rdi
int *v10; // rax
char v11; // al
socklen_t addr_len; // [rsp+4h] [rbp-24h] BYREF
time_t buf; // [rsp+8h] [rbp-20h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

switch ( argc )
{
case 2:
v9 = argv[1];
v8 = "10010";
break;
case 3:
v9 = argv[1];
v8 = argv[2];
break;
case 1:
v8 = "10010";
v9 = "127.0.0.1";
break;
default:
func11("usage: UDPtime [host [port]]\n", (char)argv);
}
v3 = func8(v9, v8, &addr);
fwrite("[CLIENT] sending message to service..\n", 1uLL, 0x26uLL, stderr);
sendto(v3, "what time is it ?\n", 0x12uLL, 0, &addr, 0x10u);
fwrite("[CLIENT] message sent to service, waiting for respond..\n", 1uLL, 0x38uLL, stderr);
addr_len = 16;
if ( (int)recvfrom(v3, &buf, 8uLL, 0, &addr, &addr_len) < 0 )
{
v10 = __errno_location();
v11 = (unsigned __int8)strerror(*v10);
func11("read failed: %s\n", v11);
}
v4 = func7((unsigned int)buf, &buf, -2208988800LL);
buf = v5 + v4;
v6 = ctime(&buf);
fprintf(stderr, "[CLIENT] now time is : %s\n", v6);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    char *host = "127.0.0.1";
    char *service = "10010";
    time_t now;
    int s, n;
    switch(argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            service = argv[2];
            break;
        default:
            errexit("usage: UDPtime [host [port]]\n");
    }
    struct sockaddr_in sin;
    s = connectUDP(host, service, &sin);
    fprintf(stderr, "[CLIENT] sending message to service..\n");
    (void) sendto(s, "what time is it ?\n", strlen("what time is it ?\n"), 0, (struct sockaddr*)&sin, sizeof(sin));
    fprintf(stderr, "[CLIENT] message sent to service, waiting for respond..\n");
    int recvlen = sizeof(sin);
    n = recvfrom(s, (char*)&now, sizeof(now), 0, (struct sockaddr*)&sin, &recvlen);
    if (n < 0)
        errexit("read failed: %s\n", strerror((*__errno_location ())));
    now = ntohl((unsigned long)now);
    now -= 2208988800UL;
    fprintf(stderr, "[CLIENT] now time is : %s\n", ctime(&now));
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char *host = "127.0.0.1";
    char *service = "10010";
    long now;
    int s; int n;
    switch(argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            service = argv[2];
            break;
        default:
            errexit("usage: UDPtime [host [port]]\n");
    }
    struct sockaddr_in sin;
    s = connectUDP(host, service, &sin);
    fprintf(stderr, "[CLIENT] sending message to service..\n");
    (void) sendto(s, "what time is it ?\n", strlen("what time is it ?\n"), 0, (struct sockaddr *)&sin, sizeof(sin));
    fprintf(stderr, "[CLIENT] message sent to service, waiting for respond..\n");
    int recvlen = sizeof(sin);
    n = recvfrom(s, (char *)&now, sizeof(now), 0, (struct sockaddr *)&sin, &recvlen);
    if (n < 0)
        errexit("read failed: %s\n", strerror((*__errno_location ())));
    now = ntohl((unsigned long)now);
    now -= 2208988800UL;
    fprintf(stderr, "[CLIENT] now time is : %s\n", ctime(&now));
    return 0;
}

variable_types:
argv
char *[]

variable_types: now long

variable_types: s int

variable_types: recvlen int


readtask

decompiled_code:
void __fastcall __noreturn readtask(void *a1)
{
int *v1; // r12
int *i; // rdi
int v3; // er13
_DWORD *v4; // rax
_BYTE *v5; // r14
epoll_data_t v6; // rbp
int v7; // eax
int v8; // ecx
int v9; // edi
_BYTE *v10; // rdx
int v11; // eax
int v12; // edi

pthread_self();
v1 = _errno_location();
while ( 1 )
{
pthread_mutex_lock(&r_mutex);
for ( i = (int *)readhead; !readhead; i = (int *)readhead )
pthread_cond_wait(&r_condl, &r_mutex);
v3 = *i;
readhead = (void *)*((_QWORD *)i + 1);
free(i);
pthread_mutex_unlock(&r_mutex);
echo();
v4 = malloc(0x14uLL);
*v4 = v3;
v5 = v4 + 2;
v6.u64 = (uint64_t)v4;
v7 = recv(v3, v4 + 2, 0xAuLL, 0);
v8 = v7;
if ( v7 < 0 )
{
v9 = *v1;
if ( *v1 == 104 )
{
close(v3);
v9 = *v1;
}
strerror(v9);
echo();
free(v6.ptr);
}
else if ( v7 )
{
*(_DWORD *)(v6.u64 + 4) = v7;
v10 = v5;
v11 = 0;
do
{
++v11;
if ( *v10 == 10 )
{
*v10 = 0;
*(_DWORD *)(v6.u64 + 4) = v11;
}
++v10;
}
while ( v8 > v11 );
echo();
if ( *(_BYTE *)(v6.u64 + 8) )
{
v12 = epfd;
ev.data = v6;
ev.events = -2147483644;
epoll_ctl(v12, 3, v3, &ev);
}
}
else
{
close(v3);
echo();
free(v6.ptr);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
int *v1; // r12
int *i; // rdi
int v3; // er13
_DWORD *v4; // rax
_BYTE *v5; // r14
epoll_data_t v6; // rbp
int v7; // eax
int v8; // ecx
int v9; // edi
_BYTE *v10; // rdx
int v11; // eax
int v12; // edi

pthread_self();
v1 = _errno_location();
while ( 1 )
{
pthread_mutex_lock(&r_mutex);
for ( i = (int *)readhead; !readhead; i = (int *)readhead )
pthread_cond_wait(&r_condl, &r_mutex);
v3 = *i;
readhead = (void *)*((_QWORD *)i + 1);
free(i);
pthread_mutex_unlock(&r_mutex);
echo();
v4 = malloc(0x14uLL);
*v4 = v3;
v5 = v4 + 2;
v6.u64 = (uint64_t)v4;
v7 = recv(v3, v4 + 2, 0xAuLL, 0);
v8 = v7;
if ( v7 < 0 )
{
v9 = *v1;
if ( *v1 == 104 )
{
close(v3);
v9 = *v1;
}
strerror(v9);
echo();
free(v6.ptr);
}
else if ( v7 )
{
*(_DWORD *)(v6.u64 + 4) = v7;
v10 = v5;
v11 = 0;
do
{
++v11;
if ( *v10 == 10 )
{
*v10 = 0;
*(_DWORD *)(v6.u64 + 4) = v11;
}
++v10;
}
while ( v8 > v11 );
echo();
if ( *(_BYTE *)(v6.u64 + 8) )
{
v12 = epfd;
ev.data = v6;
ev.events = -2147483644;
epoll_ctl(v12, 3, v3, &ev);
}
}
else
{
close(v3);
echo();
free(v6.ptr);
}
}
}

original_code:
void *readtask(void *args)
{
    int fd = -1;
    int n, i;
    struct user_data* data = ((void *)0);
    while(1)
    {
        pthread_mutex_lock(&r_mutex);
        while(readhead == ((void *)0))
            pthread_cond_wait(&r_condl, &r_mutex);
        fd = readhead->data.fd;
        struct task* tmp = readhead;
        readhead = readhead->next;
        free(tmp);
        pthread_mutex_unlock(&r_mutex);
        echo("[SERVER] readtask %d handling %d\n", pthread_self(), fd);
        data = malloc(sizeof(struct user_data));
        data->fd = fd;
        if ((n = recv(fd, data->line, 10, 0)) < 0)
        {
            if ((*__errno_location ()) == 104)
                close(fd);
            echo("[SERVER] Error: readline failed: %s\n", strerror((*__errno_location ())));
            if (data != ((void *)0))
                free(data);
        }
        else if (n == 0)
        {
            close(fd);
            echo("[SERVER] Error: client closed connection.\n");
            if (data != ((void *)0))
                free(data);
        }
        else
        {
            data->n_size = n;
            for (i = 0; i < n; ++i)
            {
                if (data->line[i] == '\n' || data->line[i] > 128)
                {
                    data->line[i] = '\0';
                    data->n_size = i + 1;
                }
            }
            echo("[SERVER] readtask %d received %d : [%d] %s\n", pthread_self(), fd, data->n_size, data->line);
            if (data->line[0] != '\0')
            {
                ev.data.ptr = data;
                ev.events = EPOLLOUT | EPOLLET;
                epoll_ctl(epfd, 3, fd, &ev);
            }
        }
    }
}

canonical_original_code:
void *readtask(void *args)
{
    int fd = -1;
    int n; int i;
    struct user_data *data = ((void *)0);
    while(1)
    {
        pthread_mutex_lock(&r_mutex);
        while(readhead == ((void *)0))
            pthread_cond_wait(&r_condl, &r_mutex);
        fd = readhead->data.fd;
        struct task *tmp = readhead;
        readhead = readhead->next;
        free(tmp);
        pthread_mutex_unlock(&r_mutex);
        echo("[SERVER] readtask %d handling %d\n", pthread_self(), fd);
        data = malloc(sizeof(struct user_data));
        data->fd = fd;
        if ((n = recv(fd, data->line, 10, 0)) < 0)
        {
            if ((*__errno_location ()) == 104)
                close(fd);
            echo("[SERVER] Error: readline failed: %s\n", strerror((*__errno_location ())));
            if (data != ((void *)0))
                free(data);
        }
        else if (n == 0)
        {
            close(fd);
            echo("[SERVER] Error: client closed connection.\n");
            if (data != ((void *)0))
                free(data);
        }
        else
        {
            data->n_size = n;
            for (i = 0; i < n; ++i)
            {
                if (data->line[i] == '\n' || data->line[i] > 128)
                {
                    data->line[i] = '\0';
                    data->n_size = i + 1;
                }
            }
            echo("[SERVER] readtask %d received %d : [%d] %s\n", pthread_self(), fd, data->n_size, data->line);
            if (data->line[0] != '\0')
            {
                ev.data.ptr = data;
                ev.events = EPOLLOUT | EPOLLET;
                epoll_ctl(epfd, 3, fd, &ev);
            }
        }
    }
}

variable_types:
args
void *

variable_types: fd int

variable_types: i int

variable_types: struct user_data { int fd; unsigned int n_size; char line[10]; } *

variable_types: struct task { union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; struct task *next; } *

return_type: void *

user_defined_types: struct task { union epoll_data data; struct task *next; }


writetask

decompiled_code:
void __fastcall __noreturn writetask(void *a1)
{
__int64 *i; // rdi
__int64 v2; // rbp
int v3; // edx
int v4; // edi

pthread_self();
while ( 1 )
{
pthread_mutex_lock(&w_mutex);
for ( i = (__int64 *)writehead; !writehead; i = (__int64 *)writehead )
pthread_cond_wait(&w_condl, &w_mutex);
v2 = *i;
writehead = (void *)i[1];
free(i);
pthread_mutex_unlock(&w_mutex);
echo();
if ( (unsigned int)send(*(_DWORD *)v2, (const void *)(v2 + 8), *(unsigned int *)(v2 + 4), 0) )
{
v3 = *(_DWORD *)v2;
v4 = epfd;
ev.events = -2147483647;
ev.data.fd = v3;
epoll_ctl(v4, 3, v3, &ev);
}
else
{
close(*(_DWORD *)v2);
echo();
}
free((void *)v2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
__int64 *i; // rdi
__int64 v2; // rbp
int v3; // edx
int v4; // edi

pthread_self();
while ( 1 )
{
pthread_mutex_lock(&w_mutex);
for ( i = (__int64 *)writehead; !writehead; i = (__int64 *)writehead )
pthread_cond_wait(&w_condl, &w_mutex);
v2 = *i;
writehead = (void *)i[1];
free(i);
pthread_mutex_unlock(&w_mutex);
echo();
if ( (unsigned int)send(*(_DWORD *)v2, (const void *)(v2 + 8), *(unsigned int *)(v2 + 4), 0) )
{
v3 = *(_DWORD *)v2;
v4 = epfd;
ev.events = -2147483647;
ev.data.fd = v3;
epoll_ctl(v4, 3, v3, &ev);
}
else
{
close(*(_DWORD *)v2);
echo();
}
free((void *)v2);
}
}

original_code:
void *writetask(void *args)
{
    unsigned int n;
    struct user_data *rdata = ((void *)0);
    while(1)
    {
        pthread_mutex_lock(&w_mutex);
        while(writehead == ((void *)0))
            pthread_cond_wait(&w_condl, &w_mutex);
        rdata = (struct user_data*)writehead->data.ptr;
        struct task* tmp = writehead;
        writehead = writehead->next;
        free(tmp);
        pthread_mutex_unlock(&w_mutex);
        echo("[SERVER] writetask %d sending %d : [%d] %s\n", pthread_self(), rdata->fd, rdata->n_size, rdata->line);
        if ((n = send(rdata->fd, rdata->line, rdata->n_size, 0)) < 0)
        {
            if ((*__errno_location ()) == 104)
                close(rdata->fd);
            echo("[SERVER] Error: send responce failed: %s\n", strerror((*__errno_location ())));
        }
        else if (n == 0)
        {
            close(rdata->fd);
            echo("[SERVER] Error: client closed connection.");
        }
        else
        {
            ev.data.fd = rdata->fd;
            ev.events = EPOLLIN | EPOLLET;
            epoll_ctl(epfd, 3, rdata->fd, &ev);
        }
        free(rdata);
    }
}

canonical_original_code:
void *writetask(void *args)
{
    unsigned int n;
    struct user_data *rdata = ((void *)0);
    while(1)
    {
        pthread_mutex_lock(&w_mutex);
        while(writehead == ((void *)0))
            pthread_cond_wait(&w_condl, &w_mutex);
        rdata = (struct user_data *)writehead->data.ptr;
        struct task *tmp = writehead;
        writehead = writehead->next;
        free(tmp);
        pthread_mutex_unlock(&w_mutex);
        echo("[SERVER] writetask %d sending %d : [%d] %s\n", pthread_self(), rdata->fd, rdata->n_size, rdata->line);
        if ((n = send(rdata->fd, rdata->line, rdata->n_size, 0)) < 0)
        {
            if ((*__errno_location ()) == 104)
                close(rdata->fd);
            echo("[SERVER] Error: send responce failed: %s\n", strerror((*__errno_location ())));
        }
        else if (n == 0)
        {
            close(rdata->fd);
            echo("[SERVER] Error: client closed connection.");
        }
        else
        {
            ev.data.fd = rdata->fd;
            ev.events = EPOLLIN | EPOLLET;
            epoll_ctl(epfd, 3, rdata->fd, &ev);
        }
        free(rdata);
    }
}

variable_types:
n
unsigned int

user_defined_types: struct user_data { int fd; unsigned int n_size; char line[10]; }

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


setnonblocking

decompiled_code:
int __fastcall setnonblocking(int fd)
{
int v1; // ebx
__int64 v2; // rdx
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
errexit("GETFL %d failed", (unsigned int)fd);
v2 = (unsigned int)v1;
BYTE1(v2) = BYTE1(v1) | 8;
result = fcntl(fd, 4, v2);
if ( result < 0 )
result = errexit("SETFL %d failed", (unsigned int)fd);
return result;
}

canonical_decompiled_code:
int __fastcall func3(int fd)
{
int v1; // ebx
__int64 v2; // rdx
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
errexit("GETFL %d failed", (unsigned int)fd);
v2 = (unsigned int)v1;
BYTE1(v2) = BYTE1(v1) | 8;
result = fcntl(fd, 4, v2);
if ( result < 0 )
result = errexit("SETFL %d failed", (unsigned int)fd);
return result;
}

original_code:
void setnonblocking(int sock)
{
    int opts;
    if ((opts = fcntl(sock, 3)) < 0)
        errexit("GETFL %d failed", sock);
    opts = opts | 04000;
    if (fcntl(sock, 4, opts) < 0)
        errexit("SETFL %d failed", sock);
}

variable_types: sock int

variable_types: opts int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v4; // edi
socklen_t v5; // edx
int v6; // eax
__int64 v7; // rbx
int v8; // esi
__int64 v9; // r12
int v10; // eax
_QWORD *v11; // r13
int v12; // eax
__int64 v13; // rax
int v14; // er13
int v15; // edi
_QWORD *v16; // r13
__int64 v17; // rax
__int64 v18; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-6Ch] BYREF
pthread_t newthread; // [rsp+10h] [rbp-68h] BYREF
pthread_t v21; // [rsp+18h] [rbp-60h] BYREF
pthread_t v22; // [rsp+20h] [rbp-58h] BYREF
pthread_t v23; // [rsp+28h] [rbp-50h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
struct sockaddr v25[3]; // [rsp+40h] [rbp-38h] BYREF

pthread_mutex_init(&r_mutex, 0LL);
pthread_cond_init(&r_condl, 0LL);
pthread_mutex_init(&w_mutex, 0LL);
pthread_cond_init(&w_condl, 0LL);
pthread_create(&newthread, 0LL, (void *(*)(void *))readtask, 0LL);
pthread_create(&v21, 0LL, (void *(*)(void *))readtask, 0LL);
pthread_create(&v22, 0LL, (void *(*)(void *))writetask, 0LL);
pthread_create(&v23, 0LL, (void *(*)(void *))writetask, 0LL);
epfd = epoll_create(256);
v3 = socket(2, 1, 0);
setnonblocking(v3);
v4 = epfd;
ev.data.fd = v3;
ev.events = -2147483647;
epoll_ctl(v4, 1, v3, &ev);
v25[0] = 0LL;
v25[0].sa_family = 2;
inet_aton("127.0.0.1", (struct in_addr *)&v25[0].sa_data[2]);
*(_WORD *)v25[0].sa_data = _bswap_16_constprop_0(v3, v25, 16LL);
bind(v3, v25, v5);
listen(v3, 20);
while ( 1 )
{
do
v6 = epoll_wait(epfd, &events, 20, 500);
while ( v6 <= 0 );
v7 = 0LL;
v8 = events.data.fd;
v9 = 12LL * v6;
if ( v8 != v3 )
break;
while ( 1 )
{
v14 = accept(v3, &addr, &addr_len);
if ( v14 < 0 )
errexit("connfd < 0");
setnonblocking(v14);
inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
echo();
v15 = epfd;
ev.data.fd = v14;
ev.events = -2147483647;
epoll_ctl(v15, 1, v14, &ev);
LABEL_9:
v7 += 12LL;
if ( v7 == v9 )
break;
v8 = *(int *)((char *)&events.data.fd + v7);
if ( v8 != v3 )
goto LABEL_4;
}
}
LABEL_4:
v10 = *(uint32_t *)((char *)&events.events + v7);
if ( (v10 & 1) != 0 )
{
if ( v8 >= 0 )
{
echo();
v11 = malloc(0x10uLL);
v12 = *(int *)((char *)&events.data.fd + v7);
v11[1] = 0LL;
*(_DWORD *)v11 = v12;
pthread_mutex_lock(&r_mutex);
if ( readhead )
{
v13 = readtail;
readtail = (__int64)v11;
*(_QWORD *)(v13 + 8) = v11;
}
else
{
readhead = v11;
readtail = (__int64)v11;
}
pthread_cond_broadcast(&r_condl);
pthread_mutex_unlock(&r_mutex);
}
}
else if ( (v10 & 4) != 0 )
{
if ( *(uint64_t *)((char *)&events.data.u64 + v7) )
{
echo();
v16 = malloc(0x10uLL);
v17 = *(__int64 *)((char *)&events.data.ptr + v7);
v16[1] = 0LL;
*v16 = v17;
pthread_mutex_lock(&w_mutex);
if ( writehead )
{
v18 = writetail;
writetail = (__int64)v16;
*(_QWORD *)(v18 + 8) = v16;
}
else
{
writehead = v16;
writetail = (__int64)v16;
}
pthread_cond_broadcast(&w_condl);
pthread_mutex_unlock(&w_mutex);
}
}
else
{
echo();
}
goto LABEL_9;
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
int v4; // edi
socklen_t v5; // edx
int v6; // eax
__int64 v7; // rbx
int v8; // esi
__int64 v9; // r12
int v10; // eax
_QWORD *v11; // r13
int v12; // eax
__int64 v13; // rax
int v14; // er13
int v15; // edi
_QWORD *v16; // r13
__int64 v17; // rax
__int64 v18; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-6Ch] BYREF
pthread_t newthread; // [rsp+10h] [rbp-68h] BYREF
pthread_t v21; // [rsp+18h] [rbp-60h] BYREF
pthread_t v22; // [rsp+20h] [rbp-58h] BYREF
pthread_t v23; // [rsp+28h] [rbp-50h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
struct sockaddr v25[3]; // [rsp+40h] [rbp-38h] BYREF

pthread_mutex_init(&r_mutex, 0LL);
pthread_cond_init(&r_condl, 0LL);
pthread_mutex_init(&w_mutex, 0LL);
pthread_cond_init(&w_condl, 0LL);
pthread_create(&newthread, 0LL, (void *(*)(void *))readtask, 0LL);
pthread_create(&v21, 0LL, (void *(*)(void *))readtask, 0LL);
pthread_create(&v22, 0LL, (void *(*)(void *))writetask, 0LL);
pthread_create(&v23, 0LL, (void *(*)(void *))writetask, 0LL);
epfd = epoll_create(256);
v3 = socket(2, 1, 0);
func3(v3);
v4 = epfd;
ev.data.fd = v3;
ev.events = -2147483647;
epoll_ctl(v4, 1, v3, &ev);
v25[0] = 0LL;
v25[0].sa_family = 2;
inet_aton("127.0.0.1", (struct in_addr *)&v25[0].sa_data[2]);
*(_WORD *)v25[0].sa_data = func2(v3, v25, 16LL);
bind(v3, v25, v5);
listen(v3, 20);
while ( 1 )
{
do
v6 = epoll_wait(epfd, &events, 20, 500);
while ( v6 <= 0 );
v7 = 0LL;
v8 = events.data.fd;
v9 = 12LL * v6;
if ( v8 != v3 )
break;
while ( 1 )
{
v14 = accept(v3, &addr, &addr_len);
if ( v14 < 0 )
errexit("connfd < 0");
func3(v14);
inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
echo();
v15 = epfd;
ev.data.fd = v14;
ev.events = -2147483647;
epoll_ctl(v15, 1, v14, &ev);
LABEL_9:
v7 += 12LL;
if ( v7 == v9 )
break;
v8 = *(int *)((char *)&events.data.fd + v7);
if ( v8 != v3 )
goto LABEL_4;
}
}
LABEL_4:
v10 = *(uint32_t *)((char *)&events.events + v7);
if ( (v10 & 1) != 0 )
{
if ( v8 >= 0 )
{
echo();
v11 = malloc(0x10uLL);
v12 = *(int *)((char *)&events.data.fd + v7);
v11[1] = 0LL;
*(_DWORD *)v11 = v12;
pthread_mutex_lock(&r_mutex);
if ( readhead )
{
v13 = readtail;
readtail = (__int64)v11;
*(_QWORD *)(v13 + 8) = v11;
}
else
{
readhead = v11;
readtail = (__int64)v11;
}
pthread_cond_broadcast(&r_condl);
pthread_mutex_unlock(&r_mutex);
}
}
else if ( (v10 & 4) != 0 )
{
if ( *(uint64_t *)((char *)&events.data.u64 + v7) )
{
echo();
v16 = malloc(0x10uLL);
v17 = *(__int64 *)((char *)&events.data.ptr + v7);
v16[1] = 0LL;
*v16 = v17;
pthread_mutex_lock(&w_mutex);
if ( writehead )
{
v18 = writetail;
writetail = (__int64)v16;
*(_QWORD *)(v18 + 8) = v16;
}
else
{
writehead = v16;
writetail = (__int64)v16;
}
pthread_cond_broadcast(&w_condl);
pthread_mutex_unlock(&w_mutex);
}
}
else
{
echo();
}
goto LABEL_9;
}

original_code:
int main(int argc,char* argv[])
{
    int i, maxi, nfds;
    int listenfd, connfd;
    pthread_t tid1, tid2;
    pthread_t tid3, tid4;
    struct task *new_task = ((void *)0);
    socklen_t clilen;
    struct sockaddr_in clientaddr;
    struct sockaddr_in serveraddr;
    pthread_mutex_init(&r_mutex, ((void *)0));
    pthread_cond_init(&r_condl, ((void *)0));
    pthread_mutex_init(&w_mutex, ((void *)0));
    pthread_cond_init(&w_condl, ((void *)0));
    pthread_create(&tid1, ((void *)0), readtask, ((void *)0));
    pthread_create(&tid2, ((void *)0), readtask, ((void *)0));
    pthread_create(&tid3, ((void *)0), writetask, ((void *)0));
    pthread_create(&tid4, ((void *)0), writetask, ((void *)0));
    epfd = epoll_create(256);
    listenfd = socket(2, SOCK_STREAM, 0);
    setnonblocking(listenfd);
    ev.data.fd = listenfd;
    ev.events = EPOLLIN | EPOLLET;
    epoll_ctl(epfd, 1, listenfd, &ev);
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = 2;
    char *local_addr = "127.0.0.1";
    inet_aton(local_addr, &(serveraddr.sin_addr));
    serveraddr.sin_port = htons(10012);
    bind(listenfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
    listen(listenfd, 20);
    maxi = 0;
    for(;;)
    {
        nfds = epoll_wait(epfd, events, 20, 500);
        for (i = 0; i < nfds; ++i)
        {
            if (events[i].data.fd == listenfd)
            {
                connfd = accept(listenfd, (struct sockaddr*)&clientaddr, &clilen);
                if (connfd < 0)
                    errexit("connfd < 0");
                setnonblocking(connfd);
                echo("[SERVER] connect from %s \n", inet_ntoa(clientaddr.sin_addr));
                ev.data.fd = connfd;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epfd, 1, connfd, &ev);
            }
            else if (events[i].events & EPOLLIN)
            {
                if (events[i].data.fd < 0)
                    continue;
                echo("[SERVER] put task %d to read queue\n", events[i].data.fd);
                new_task = malloc(sizeof(struct task));
                new_task->data.fd = events[i].data.fd;
                new_task->next = ((void *)0);
                pthread_mutex_lock(&r_mutex);
                if (readhead == ((void *)0))
                {
                    readhead = new_task;
                    readtail = new_task;
                }
                else
                {
                    readtail->next = new_task;
                    readtail = new_task;
                }
                pthread_cond_broadcast(&r_condl);
                pthread_mutex_unlock(&r_mutex);
            }
            else if (events[i].events & EPOLLOUT)
            {
                if (events[i].data.ptr == ((void *)0))
                    continue;
                echo("[SERVER] put task %d to write queue\n", ((struct task*)events[i].data.ptr)->data.fd);
                new_task = malloc(sizeof(struct task));
                new_task->data.ptr = (struct user_data*)events[i].data.ptr;
                new_task->next = ((void *)0);
                pthread_mutex_lock(&w_mutex);
                if (writehead == ((void *)0))
                {
                    writehead = new_task;
                    writetail = new_task;
                }
                else
                {
                    writetail->next = new_task;
                    writetail = new_task;
                }
                pthread_cond_broadcast(&w_condl);
                pthread_mutex_unlock(&w_mutex);
            }
            else
            {
                echo("[SERVER] Error: unknown epoll event");
            }
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int i; int maxi; int nfds;
    int listenfd; int connfd;
    unsigned long tid1; unsigned long tid2;
    unsigned long tid3; unsigned long tid4;
    struct task *new_task = ((void *)0);
    unsigned int clilen;
    struct sockaddr_in clientaddr;
    struct sockaddr_in serveraddr;
    pthread_mutex_init(&r_mutex, ((void *)0));
    pthread_cond_init(&r_condl, ((void *)0));
    pthread_mutex_init(&w_mutex, ((void *)0));
    pthread_cond_init(&w_condl, ((void *)0));
    pthread_create(&tid1, ((void *)0), readtask, ((void *)0));
    pthread_create(&tid2, ((void *)0), readtask, ((void *)0));
    pthread_create(&tid3, ((void *)0), writetask, ((void *)0));
    pthread_create(&tid4, ((void *)0), writetask, ((void *)0));
    epfd = epoll_create(256);
    listenfd = socket(2, SOCK_STREAM, 0);
    setnonblocking(listenfd);
    ev.data.fd = listenfd;
    ev.events = EPOLLIN | EPOLLET;
    epoll_ctl(epfd, 1, listenfd, &ev);
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = 2;
    char *local_addr = "127.0.0.1";
    inet_aton(local_addr, &(serveraddr.sin_addr));
    serveraddr.sin_port = htons(10012);
    bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    listen(listenfd, 20);
    maxi = 0;
    for(;;)
    {
        nfds = epoll_wait(epfd, events, 20, 500);
        for (i = 0; i < nfds; ++i)
        {
            if (events[i].data.fd == listenfd)
            {
                connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clilen);
                if (connfd < 0)
                    errexit("connfd < 0");
                setnonblocking(connfd);
                echo("[SERVER] connect from %s \n", inet_ntoa(clientaddr.sin_addr));
                ev.data.fd = connfd;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epfd, 1, connfd, &ev);
            }
            else if (events[i].events & EPOLLIN)
            {
                if (events[i].data.fd < 0)
                    continue;
                echo("[SERVER] put task %d to read queue\n", events[i].data.fd);
                new_task = malloc(sizeof(struct task));
                new_task->data.fd = events[i].data.fd;
                new_task->next = ((void *)0);
                pthread_mutex_lock(&r_mutex);
                if (readhead == ((void *)0))
                {
                    readhead = new_task;
                    readtail = new_task;
                }
                else
                {
                    readtail->next = new_task;
                    readtail = new_task;
                }
                pthread_cond_broadcast(&r_condl);
                pthread_mutex_unlock(&r_mutex);
            }
            else if (events[i].events & EPOLLOUT)
            {
                if (events[i].data.ptr == ((void *)0))
                    continue;
                echo("[SERVER] put task %d to write queue\n", ((struct task *)events[i].data.ptr)->data.fd);
                new_task = malloc(sizeof(struct task));
                new_task->data.ptr = (struct user_data *)events[i].data.ptr;
                new_task->next = ((void *)0);
                pthread_mutex_lock(&w_mutex);
                if (writehead == ((void *)0))
                {
                    writehead = new_task;
                    writetail = new_task;
                }
                else
                {
                    writetail->next = new_task;
                    writetail = new_task;
                }
                pthread_cond_broadcast(&w_condl);
                pthread_mutex_unlock(&w_mutex);
            }
            else
            {
                echo("[SERVER] Error: unknown epoll event");
            }
        }
    }
    return 0;
}

variable_types: argc int

variable_types: maxi int

variable_types: nfds int

variable_types: listenfd int

variable_types: connfd int

variable_types:
clilen
unsigned 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]; }


passiveTCP

decompiled_code:
__int64 __fastcall passiveTCP(char a1)
{
return passivesock(a1, (char)&unk_402004, 1);
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
return func10(a1, (char)&unk_402004, 1);
}

original_code:
int passiveTCP(const char* service)
{
    return passivesock(service, "TCP", 1);
}

canonical_original_code:
int passiveTCP(char *service)
{
    return passivesock(service, "TCP", 1);
}

variable_types:
service
char *


TCPdaytimed

decompiled_code:
ssize_t __fastcall TCPdaytimed(int fd)
{
char *v1; // rbp
size_t v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v1 = ctime(&timer);
v2 = strlen(v1);
return send(fd, v1, v2, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int fd)
{
char *v1; // rbp
size_t v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v1 = ctime(&timer);
v2 = strlen(v1);
return send(fd, v1, v2, 0);
}

original_code:
void TCPdaytimed(int fd)
{
    char *pts;
    time_t now;
    (void) time(&now);
    pts = ctime(&now);
    (void) send(fd, pts, strlen(pts), 0);
}

canonical_original_code:
void TCPdaytimed(int fd)
{
    char *pts;
    long now;
    (void) time(&now);
    pts = ctime(&now);
    (void) send(fd, pts, strlen(pts), 0);
}

variable_types:
pts
char *

variable_types: now long


errexit

variable_types:
format
char *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int64 v4; // rbx
int *v5; // r12
int v6; // eax
int v7; // ebp
char v8; // al
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v3 = "10086";
if ( argc != 1 )
{
if ( argc != 2 )
errexit("[SERVICE] usage: TCPdaytimed [port]\n", (char)argv);
v3 = argv[1];
}
fwrite("[SERVICE] creating passive socket..\n", 1uLL, 0x24uLL, stderr);
v4 = (unsigned int)passiveTCP(v3);
fprintf(stderr, "[SERVICE] passive socket %d created.\n", v4);
v5 = __errno_location();
while ( 1 )
{
addr_len = 16;
fwrite("[SERVICE] waiting for client..\n", 1uLL, 0x1FuLL, stderr);
v6 = accept(v4, &addr, &addr_len);
v7 = v6;
if ( v6 < 0 )
break;
fprintf(stderr, "[SERVICE] temporary socket %d created.\n", (unsigned int)v6);
TCPdaytimed(v7);
close(v7);
}
v8 = (unsigned __int8)strerror(*v5);
errexit("accept failed: %s\n", v8);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
__int64 v4; // rbx
int *v5; // r12
int v6; // eax
int v7; // ebp
char v8; // al
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v3 = "10086";
if ( argc != 1 )
{
if ( argc != 2 )
func11("[SERVICE] usage: TCPdaytimed [port]\n", (char)argv);
v3 = argv[1];
}
fwrite("[SERVICE] creating passive socket..\n", 1uLL, 0x24uLL, stderr);
v4 = (unsigned int)func7(v3);
fprintf(stderr, "[SERVICE] passive socket %d created.\n", v4);
v5 = __errno_location();
while ( 1 )
{
addr_len = 16;
fwrite("[SERVICE] waiting for client..\n", 1uLL, 0x1FuLL, stderr);
v6 = accept(v4, &addr, &addr_len);
v7 = v6;
if ( v6 < 0 )
break;
fprintf(stderr, "[SERVICE] temporary socket %d created.\n", (unsigned int)v6);
func8(v7);
close(v7);
}
v8 = (unsigned __int8)strerror(*v5);
func11("accept failed: %s\n", v8);
}

original_code:
int main(int argc,char* argv[])
{
    struct sockaddr_in fsin;
    char *service = "10086";
    int msock, ssock;
    unsigned int alen;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            service = argv[1];
            break;
        default:
            errexit("[SERVICE] usage: TCPdaytimed [port]\n");
    }
    fprintf(stderr, "[SERVICE] creating passive socket..\n");
    msock = passiveTCP(service);
    fprintf(stderr, "[SERVICE] passive socket %d created.\n", msock);
    while(1)
    {
        alen = sizeof(fsin);
        fprintf(stderr, "[SERVICE] waiting for client..\n");
        ssock = accept(msock, (struct sockaddr*)&fsin, &alen);
        if (ssock < 0)
            errexit("accept failed: %s\n", strerror((*__errno_location ())));
        fprintf(stderr, "[SERVICE] temporary socket %d created.\n", ssock);
        TCPdaytimed(ssock);
        (void) close(ssock);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in fsin;
    char *service = "10086";
    int msock; int ssock;
    unsigned int alen;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            service = argv[1];
            break;
        default:
            errexit("[SERVICE] usage: TCPdaytimed [port]\n");
    }
    fprintf(stderr, "[SERVICE] creating passive socket..\n");
    msock = passiveTCP(service);
    fprintf(stderr, "[SERVICE] passive socket %d created.\n", msock);
    while(1)
    {
        alen = sizeof(fsin);
        fprintf(stderr, "[SERVICE] waiting for client..\n");
        ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
        if (ssock < 0)
            errexit("accept failed: %s\n", strerror((*__errno_location ())));
        fprintf(stderr, "[SERVICE] temporary socket %d created.\n", ssock);
        TCPdaytimed(ssock);
        (void) close(ssock);
    }
    return 0;
}

variable_types:
argv
char *[]

variable_types: msock int

variable_types: ssock int

variable_types:
alen
unsigned int


passiveUDP

decompiled_code:
__int64 __fastcall passiveUDP(char a1)
{
return passivesock(a1, (char)&unk_402004, 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1)
{
return func10(a1, (char)&unk_402004, 0);
}

original_code:
int passiveUDP(const char *service)
{
    return passivesock(service, "UDP", 0);
}

canonical_original_code:
int passiveUDP(char *service)
{
    return passivesock(service, "UDP", 0);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v4; // ebx
int *v5; // rbp
char v6; // al
size_t v7; // rdx
int v8; // ecx
const struct sockaddr *v9; // r8
socklen_t v10; // er9
char buf; // [rsp+3h] [rbp-35h] BYREF
socklen_t addr_len; // [rsp+4h] [rbp-34h] BYREF
time_t timer; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v3 = "10010";
if ( argc != 1 )
{
if ( argc != 2 )
errexit("usage: UDPtimed [port]\n", (char)argv);
v3 = argv[1];
}
v4 = passiveUDP(v3, argv, envp);
v5 = __errno_location();
while ( 1 )
{
addr_len = 16;
if ( recvfrom(v4, &buf, 1uLL, 0, &addr, &addr_len) < 0 )
break;
time(&timer);
timer = (unsigned int)_bswap_32((unsigned int)(timer - 2085978496), &timer, 8LL, 0LL, &addr, 16LL);
sendto(v4, &timer, v7, v8, v9, v10);
}
v6 = (unsigned __int8)strerror(*v5);
errexit("recvfrom: %s \n", v6);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v4; // ebx
int *v5; // rbp
char v6; // al
size_t v7; // rdx
int v8; // ecx
const struct sockaddr *v9; // r8
socklen_t v10; // er9
char buf; // [rsp+3h] [rbp-35h] BYREF
socklen_t addr_len; // [rsp+4h] [rbp-34h] BYREF
time_t timer; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v3 = "10010";
if ( argc != 1 )
{
if ( argc != 2 )
func11("usage: UDPtimed [port]\n", (char)argv);
v3 = argv[1];
}
v4 = func8(v3, argv, envp);
v5 = __errno_location();
while ( 1 )
{
addr_len = 16;
if ( recvfrom(v4, &buf, 1uLL, 0, &addr, &addr_len) < 0 )
break;
time(&timer);
timer = (unsigned int)func7((unsigned int)(timer - 2085978496), &timer, 8LL, 0LL, &addr, 16LL);
sendto(v4, &timer, v7, v8, v9, v10);
}
v6 = (unsigned __int8)strerror(*v5);
func11("recvfrom: %s \n", v6);
}

original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in fsin;
    char *service = "10010";
    char buf[1];
    int sock;
    time_t now;
    unsigned int alen;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            service = argv[1];
            break;
        default:
            errexit("usage: UDPtimed [port]\n");
    }
    sock = passiveUDP(service);
    while(1)
    {
        alen = sizeof(fsin);
        if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&fsin, &alen) < 0)
            errexit("recvfrom: %s \n", strerror((*__errno_location ())));
        (void) time(&now);
        now = htonl((unsigned long)(now + 2208988800UL));
        (void) sendto(sock, (char*)&now, sizeof(now), 0, (struct sockaddr*)&fsin, sizeof(fsin));
    }
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in fsin;
    char *service = "10010";
    char buf[1];
    int sock;
    long now;
    unsigned int alen;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            service = argv[1];
            break;
        default:
            errexit("usage: UDPtimed [port]\n");
    }
    sock = passiveUDP(service);
    while(1)
    {
        alen = sizeof(fsin);
        if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0)
            errexit("recvfrom: %s \n", strerror((*__errno_location ())));
        (void) time(&now);
        now = htonl((unsigned long)(now + 2208988800UL));
        (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin));
    }
}

variable_types:
buf
char [1]

variable_types: sock int


TCPdaytime

decompiled_code:
size_t __fastcall TCPdaytime(char *a1, char a2)
{
__int64 v2; // rbp
int v3; // ebx
FILE *v4; // rsi
int v5; // eax
char v7[168]; // [rsp+0h] [rbp-A8h] BYREF

v2 = (unsigned int)clientsock(a1, a2, (char)&unk_402004, 0LL);
fprintf(stderr, "[CLIENT] socket %d created.\n", v2);
sleep(1u);
while ( 1 )
{
v5 = recv(v2, v7, 0x80uLL, 0);
if ( v5 <= 0 )
break;
v3 = v5;
fprintf(stderr, "[CLIENT] %d data read: \n", (unsigned int)v5);
v4 = stderr;
v7[v3] = 0;
fputs(v7, v4);
}
return fwrite("[CLIENT] data receive finish. \n", 1uLL, 0x1FuLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func7(char *a1, char a2)
{
__int64 v2; // rbp
int v3; // ebx
FILE *v4; // rsi
int v5; // eax
char v7[168]; // [rsp+0h] [rbp-A8h] BYREF

v2 = (unsigned int)func9(a1, a2, (char)&unk_402004, 0LL);
fprintf(stderr, "[CLIENT] socket %d created.\n", v2);
sleep(1u);
while ( 1 )
{
v5 = recv(v2, v7, 0x80uLL, 0);
if ( v5 <= 0 )
break;
v3 = v5;
fprintf(stderr, "[CLIENT] %d data read: \n", (unsigned int)v5);
v4 = stderr;
v7[v3] = 0;
fputs(v7, v4);
}
return fwrite("[CLIENT] data receive finish. \n", 1uLL, 0x1FuLL, stderr);
}

original_code:
void TCPdaytime(const char* host, const char* service)
{
    char buf[128 + 1];
    int s, n;
    s = clientsock(host, service, "TCP", ((void *)0));
    fprintf(stderr, "[CLIENT] socket %d created.\n", s);
    sleep(1);
    while((n = recv(s, buf, 128, 0)) > 0)
    {
        fprintf(stderr, "[CLIENT] %d data read: \n", n);
        buf[n] = '\0';
        (void) fputs(buf, stderr);
    }
    fprintf(stderr, "[CLIENT] data receive finish. \n");
}

canonical_original_code:
void TCPdaytime(char *host, char *service)
{
    char buf[-1];
    int s; int n;
    s = clientsock(host, service, "TCP", ((void *)0));
    fprintf(stderr, "[CLIENT] socket %d created.\n", s);
    sleep(1);
    while((n = recv(s, buf, 128, 0)) > 0)
    {
        fprintf(stderr, "[CLIENT] %d data read: \n", n);
        buf[n] = '\0';
        (void) fputs(buf, stderr);
    }
    fprintf(stderr, "[CLIENT] data receive finish. \n");
}

variable_types:
host
char *

variable_types:
buf
char [-1]

variable_types: s int

variable_types: n int


errexit

decompiled_code:
void __noreturn errexit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
int errexit(const char* format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    exit(1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc != 2 && argc != 3 && argc != 1 )
errexit("usage: TCPdaytime [host [port]]\n", (char)argv);
TCPdaytime();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc != 2 && argc != 3 && argc != 1 )
func10("usage: TCPdaytime [host [port]]\n", (char)argv);
func7();
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    char *host = "127.0.0.1";
    char *service = "10086";
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            service = argv[2];
            break;
        default:
            errexit("usage: TCPdaytime [host [port]]\n");
    }
    TCPdaytime(host, service);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char *host = "127.0.0.1";
    char *service = "10086";
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            service = argv[2];
            break;
        default:
            errexit("usage: TCPdaytime [host [port]]\n");
    }
    TCPdaytime(host, service);
}

variable_types: argc int