writenbytes

decompiled_code:
__int64 __fastcall writenbytes(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // eax

for ( i = 0; ; i += v5 )
{
v5 = write(fd, (const void *)(a2 + i), a3 - i);
if ( v5 <= 0 )
break;
}
if ( v5 < 0 )
i = -1;
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2, int a3)
{
int i; // ebx
int v5; // eax

for ( i = 0; ; i += v5 )
{
v5 = write(fd, (const void *)(a2 + i), a3 - i);
if ( v5 <= 0 )
break;
}
if ( v5 < 0 )
i = -1;
return (unsigned int)i;
}

original_code:
int writenbytes(int fd,char *str,int size)
{
    int rc = 0;
    int totalwritten =0;
    while ((rc = write(fd,str+totalwritten,size-totalwritten)) > 0)
        totalwritten += rc;
    if (rc < 0)
        return -1;
    else
        return totalwritten;
}

canonical_original_code:
int writenbytes(int fd, char *str, int size)
{
    int rc = 0;
    int totalwritten = 0;
    while ((rc = write(fd,str+totalwritten,size-totalwritten)) > 0)
        totalwritten += rc;
    if (rc < 0)
        return -1;
    else
        return totalwritten;
}

variable_types:
str
char *

variable_types: totalwritten int


decompiled_code:
__int64 __fastcall parse_int_arg(const char *a1, const char *a2)
{
size_t v2; // r13
bool v3; // r14
bool v4; // r15
int i; // ebx
size_t v6; // rax
int v7; // ebp
const unsigned __int16 **v8; // rdx
char v9; // al
unsigned int v11; // [rsp+Ch] [rbp-3Ch]

v2 = strlen(a1);
v11 = 0;
v3 = 0;
v4 = 0;
for ( i = 0; i < v2; ++i )
{
if ( !v3 )
{
v3 = a1[i] == 63;
continue;
}
if ( !v4 )
{
v6 = strlen(a2);
v7 = v6;
if ( strncmp(&a1[i], a2, v6) )
continue;
i += v7;
}
v8 = _ctype_b_loc();
v9 = a1[i];
if ( ((*v8)[v9] & 0x800) == 0 )
return v11;
v11 = v9 + 10 * v11 - 48;
v4 = v3;
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
size_t v2; // r13
bool v3; // r14
bool v4; // r15
int i; // ebx
size_t v6; // rax
int v7; // ebp
const unsigned __int16 **v8; // rdx
char v9; // al
unsigned int v11; // [rsp+Ch] [rbp-3Ch]

v2 = strlen(a1);
v11 = 0;
v3 = 0;
v4 = 0;
for ( i = 0; i < v2; ++i )
{
if ( !v3 )
{
v3 = a1[i] == 63;
continue;
}
if ( !v4 )
{
v6 = strlen(a2);
v7 = v6;
if ( strncmp(&a1[i], a2, v6) )
continue;
i += v7;
}
v8 = _ctype_b_loc();
v9 = a1[i];
if ( ((*v8)[v9] & 0x800) == 0 )
return v11;
v11 = v9 + 10 * v11 - 48;
v4 = v3;
}
return v11;
}

original_code:
int parse_int_arg(char* filename, char* arg)
{
    int i;
    _Bool found_value_start = 0;
    _Bool found_arg_list_start = 0;
    int seatnum = 0;
    for(i=0; i < strlen(filename); i++)
    {
        if (!found_arg_list_start)
        {
            if (filename[i] == '?')
            {
                found_arg_list_start = 1;
            }
            continue;
        }
        if (!found_value_start && strncmp(&filename[i], arg, strlen(arg)) == 0)
        {
            found_value_start = 1;
            i += strlen(arg);
        }
        if (found_value_start)
        {
            if(((*__ctype_b_loc ())[(int) ((filename[i]))] & (unsigned short int) _ISdigit))
            {
                seatnum = seatnum * 10 + (int) filename[i] - (int) '0';
                continue;
            }
            else
            {
                break;
            }
        }
    }
    return seatnum;
}

canonical_original_code:
int parse_int_arg(char *filename, char *arg)
{
    int i;
    _Bool found_value_start = 0;
    _Bool found_arg_list_start = 0;
    int seatnum = 0;
    for(i=0; i < strlen(filename); i++)
    {
        if (!found_arg_list_start)
        {
            if (filename[i] == '?')
            {
                found_arg_list_start = 1;
            }
            continue;
        }
        if (!found_value_start && strncmp(&filename[i], arg, strlen(arg)) == 0)
        {
            found_value_start = 1;
            i += strlen(arg);
        }
        if (found_value_start)
        {
            if(((*__ctype_b_loc ())[(int) ((filename[i]))] & (unsigned short) _ISdigit))
            {
                seatnum = seatnum * 10 + (int) filename[i] - (int) '0';
                continue;
            }
            else
            {
                break;
            }
        }
    }
    return seatnum;
}

variable_types:
filename
char *

variable_types:
arg
char *

variable_types: i int

variable_types: seatnum int

return_type: int


decompiled_code:
int __fastcall handle_connection(unsigned int *a1)
{
__int64 v1; // r12
const unsigned __int16 *v2; // r8
__int64 i; // rdx
int v4; // er10
char v5; // al
int v6; // er9
_BOOL4 v7; // edi
char v8; // cl
__int64 v9; // rax
int v10; // edx
char *v11; // r10
char v12; // al
char *v13; // rax
char v14; // dl
size_t v15; // rax
size_t j; // r14
int v17; // ebx
int result; // eax
void *v19; // rsp
size_t v20; // rax
unsigned int v21; // er14
__int64 v22; // rdx
__int64 v23; // r8
__int64 v24; // rcx
const char *v25; // rdi
bool v26; // cf
bool v27; // zf
char *v28; // rsi
unsigned __int64 v29; // rcx
unsigned __int64 v30; // rcx
__int64 v31; // rcx
const char *v32; // rdi
bool v33; // cf
bool v34; // zf
char *v35; // rsi
int v36; // eax
int v37; // eax
int v38; // eax
int v39; // eax
int v40; // ebx
int v41; // eax
char v42[12]; // [rsp+0h] [rbp-4E0h] BYREF
unsigned int v43; // [rsp+Ch] [rbp-4D4h]
char s[112]; // [rsp+10h] [rbp-4D0h] BYREF
char v45[32]; // [rsp+80h] [rbp-460h] BYREF
char buf[1088]; // [rsp+A0h] [rbp-440h] BYREF

puts("In handle connection ");
v1 = *a1;
printf("connfd = %d \n", v1);
get_line(v1, (__int64)buf, 1024);
puts("About to parse instr. ");
v2 = *_ctype_b_loc();
for ( i = 0LL; ; ++i )
{
v4 = i;
v5 = buf[i];
if ( (v2[v5] & 0x2000) != 0 || i == 19 )
break;
v45[i] = v5;
}
v6 = i + 2;
v45[(int)i] = 0;
v27 = memcmp(v45, "GET", 3uLL) == 0;
v7 = !v27;
if ( v27 )
{
v8 = buf[v6];
v9 = 1LL;
if ( (v2[v8] & 0x2000) == 0 )
{
v10 = i + 2;
v11 = &buf[v4];
do
{
*((_BYTE *)&v43 + v9 + 3) = v8;
v7 = v9;
v6 = v10 + v9;
v8 = v11[v9 + 2];
if ( (v2[v8] & 0x2000) != 0 )
break;
++v9;
}
while ( v9 != 100 );
}
s[v7] = 0;
v12 = buf[v6 + 1];
if ( (v2[v12] & 0x2000) == 0 && v12 )
{
v13 = &buf[v6];
do
{
v14 = v13[2];
if ( (v2[v14] & 0x2000) != 0 )
break;
if ( !v14 )
break;
++v13;
}
while ( v13 != &buf[v6 + 19] );
}
while ( (int)get_line(v1, (__int64)buf, 1024) > 0 )
;
v15 = strlen(s);
for ( j = 0LL; ; ++j )
{
v17 = j;
if ( v15 == j || s[j] == 63 )
break;
}
v19 = alloca((int)j + 1);
v20 = strlen(s);
if ( v20 < j )
v17 = v20;
strncpy(v42, s, v17);
v42[v17] = 0;
v43 = parse_int_arg(s, "seat=");
v21 = parse_int_arg(s, "user=");
v23 = (unsigned int)parse_int_arg(s, "priority=");
v24 = 11LL;
if ( (unsigned __int64)v17 <= 0xB )
v24 = v17;
v25 = "list_seats";
v26 = 0;
v27 = 1;
v28 = v42;
do
{
if ( !v24 )
break;
v26 = (unsigned __int8)*v28 < (unsigned int)*v25;
v27 = *v28++ == *v25++;
--v24;
}
while ( v27 );
if ( (!v26 && !v27) == v26 )
{
list_seats(buf, 1024LL, v22, v24, v23);
writenbytes(v1, (__int64)"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n", 44);
v37 = strlen(buf);
writenbytes(v1, (__int64)buf, v37);
}
else
{
v29 = 10LL;
if ( (unsigned __int64)v17 <= 0xA )
v29 = v17;
if ( !memcmp(v42, "view_seat", v29) )
{
view_seat(buf, 1024LL, v43, v21, v23);
writenbytes(v1, (__int64)"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n", 44);
v38 = strlen(buf);
writenbytes(v1, (__int64)buf, v38);
}
else
{
v30 = 8LL;
if ( (unsigned __int64)v17 <= 8 )
v30 = v17;
if ( !memcmp(v42, "confirm", v30) )
{
confirm_seat(buf, 1024LL, v43, v21, v23);
writenbytes(v1, (__int64)"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n", 44);
v39 = strlen(buf);
writenbytes(v1, (__int64)buf, v39);
}
else
{
v31 = 7LL;
if ( (unsigned __int64)v17 <= 7 )
v31 = v17;
v32 = "cancel";
v33 = 0;
v34 = 1;
v35 = v42;
do
{
if ( !v31 )
break;
v33 = (unsigned __int8)*v35 < (unsigned int)*v32;
v34 = *v35++ == *v32++;
--v31;
}
while ( v34 );
if ( (!v33 && !v34) == v33 )
{
cancel(buf, 1024LL, v43, v21, v23);
writenbytes(v1, (__int64)"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n", 44);
v36 = strlen(buf);
writenbytes(v1, (__int64)buf, v36);
}
else
{
v40 = open(v42, 0, v22, v31, v23);
if ( v40 == -1 )
{
writenbytes(
v1,
(__int64)"HTTP/1.0 404 FILE NOT FOUND\r\n"
"Content-type: text/html\r\n"
"\r\n"
"\n"
"
404 FILE NOT FOUND
\n"
"\n",
137);
}
else
{
writenbytes(v1, (__int64)"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n", 44);
while ( 1 )
{
v41 = read(v40, buf, 0x400uLL);
if ( v41 <= 0 )
break;
writenbytes(v1, (__int64)buf, v41);
}
close(v40);
}
}
}
}
}
result = close(v1);
}
else
{
writenbytes(
v1,
(__int64)"HTTP/1.0 400 BAD REQUEST\r\n"
"Content-type: text/html\r\n"
"\r\n"
"
BAD REQUEST
\n",
100);
result = close(v1);
}
return result;
}

original_code:
void handle_connection(int* connfd_ptr)
{
    printf("In handle connection \n");
    int connfd = *(connfd_ptr);
    int fd;
    char buf[1024 +1];
    char instr[20];
    char file[100];
    char type[20];
    int i=0;
    int j=0;
    char *ok_response = "HTTP/1.0 200 OK\r\n" "Content-type: text/html\r\n\r\n";
    char *notok_response = "HTTP/1.0 404 FILE NOT FOUND\r\n" "Content-type: text/html\r\n\r\n" "\n" "
404 FILE NOT FOUND
\n" "\n";
    char *bad_request = "HTTP/1.0 400 BAD REQUEST\r\n" "Content-type: text/html\r\n\r\n" "
BAD REQUEST
" "\n";
    printf("connfd = %d \n", connfd);
    get_line(connfd, buf, 1024);
    printf("About to parse instr. \n");
    while( !((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short int) _ISspace) && (i < sizeof(instr) - 1))
    {
        instr[i] = buf[i];
        i++;
        j++;
    }
    j+=2;
    instr[i] = '\0';
    if (strncmp(instr, "GET", 3) != 0) {
        writenbytes(connfd, bad_request, strlen(bad_request));
        close(connfd);
        return;
    }
    i=0;
    while (!((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short int) _ISspace) && (i < sizeof(file) - 1))
    {
        file[i] = buf[j];
        i++;
        j++;
    }
    j++;
    file[i] = '\0';
    i=0;
    while (!((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short int) _ISspace) && (buf[j] != '\0') && (i < sizeof(type) - 1))
    {
        type[i] = buf[j];
        i++;
        j++;
    }
    type[i] = '\0';
    while (get_line(connfd, buf, 1024) > 0)
    {
    }
    int length;
    for(i = 0; i < strlen(file); i++)
    {
        if(file[i] == '?')
            break;
    }
    length = i;
    char resource[length+1];
    if (length > strlen(file)) {
      length = strlen(file);
    }
    strncpy(resource, file, length);
    resource[length] = 0;
    int seat_id = parse_int_arg(file, "seat=");
    int user_id = parse_int_arg(file, "user=");
    int customer_priority = parse_int_arg(file, "priority=");
    if (strncmp(resource, "list_seats", length) == 0)
    {
        list_seats(buf, 1024);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "view_seat", length) == 0)
    {
        view_seat(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "confirm", length) == 0)
    {
        confirm_seat(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "cancel", length) == 0)
    {
        cancel(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else
    {
        if ((fd = open(resource, 00)) == -1)
        {
            writenbytes(connfd, notok_response, strlen(notok_response));
        }
        else
        {
            writenbytes(connfd, ok_response, strlen(ok_response));
            int ret;
            while ( (ret = read(fd, buf, 1024)) > 0) {
                writenbytes(connfd, buf, ret);
            }
            close(fd);
        }
    }
    close(connfd);
}

canonical_original_code:
void handle_connection(int *connfd_ptr)
{
    printf("In handle connection \n");
    int connfd = *(connfd_ptr);
    int fd;
    char buf[-1];
    char instr[20];
    char file[100];
    char type[20];
    int i = 0;
    int j = 0;
    char *ok_response = "HTTP/1.0 200 OK\r\n" "Content-type: text/html\r\n\r\n";
    char *notok_response = "HTTP/1.0 404 FILE NOT FOUND\r\n" "Content-type: text/html\r\n\r\n" "\n" "
404 FILE NOT FOUND
\n" "\n";
    char *bad_request = "HTTP/1.0 400 BAD REQUEST\r\n" "Content-type: text/html\r\n\r\n" "
BAD REQUEST
" "\n";
    printf("connfd = %d \n", connfd);
    get_line(connfd, buf, 1024);
    printf("About to parse instr. \n");
    while( !((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short) _ISspace) && (i < sizeof(instr) - 1))
    {
        instr[i] = buf[i];
        i++;
        j++;
    }
    j+=2;
    instr[i] = '\0';
    if (strncmp(instr, "GET", 3) != 0) {
        writenbytes(connfd, bad_request, strlen(bad_request));
        close(connfd);
        return;
    }
    i=0;
    while (!((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short) _ISspace) && (i < sizeof(file) - 1))
    {
        file[i] = buf[j];
        i++;
        j++;
    }
    j++;
    file[i] = '\0';
    i=0;
    while (!((*__ctype_b_loc ())[(int) ((buf[j]))] & (unsigned short) _ISspace) && (buf[j] != '\0') && (i < sizeof(type) - 1))
    {
        type[i] = buf[j];
        i++;
        j++;
    }
    type[i] = '\0';
    while (get_line(connfd, buf, 1024) > 0)
    {
    }
    int length;
    for(i = 0; i < strlen(file); i++)
    {
        if(file[i] == '?')
            break;
    }
    length = i;
    char resource[-1];
    if (length > strlen(file)) {
      length = strlen(file);
    }
    strncpy(resource, file, length);
    resource[length] = 0;
    int seat_id = parse_int_arg(file, "seat=");
    int user_id = parse_int_arg(file, "user=");
    int customer_priority = parse_int_arg(file, "priority=");
    if (strncmp(resource, "list_seats", length) == 0)
    {
        list_seats(buf, 1024);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "view_seat", length) == 0)
    {
        view_seat(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "confirm", length) == 0)
    {
        confirm_seat(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else if(strncmp(resource, "cancel", length) == 0)
    {
        cancel(buf, 1024, seat_id, user_id, customer_priority);
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
    }
    else
    {
        if ((fd = open(resource, 00)) == -1)
        {
            writenbytes(connfd, notok_response, strlen(notok_response));
        }
        else
        {
            writenbytes(connfd, ok_response, strlen(ok_response));
            int ret;
            while ( (ret = read(fd, buf, 1024)) > 0) {
                writenbytes(connfd, buf, ret);
            }
            close(fd);
        }
    }
    close(connfd);
}

variable_types: connfd int

variable_types: fd int

variable_types:
buf
char [-1]

variable_types:
instr
char [20]

variable_types:
file
char [100]

variable_types:
type
char [20]

variable_types: j int

variable_types: length int

variable_types:
resource
char [-1]

variable_types: ret int

return_type: void


decompiled_code:
int __fastcall handle_connection_wrapper(unsigned int *a1)
{
return handle_connection(a1);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1)
{
return func4(a1);
}

original_code:
void handle_connection_wrapper(void* connfd_ptr)
{
handle_connection((int*) connfd_ptr);
}

canonical_original_code:
void handle_connection_wrapper(void *connfd_ptr)
{
handle_connection((int *) connfd_ptr);
}


decompiled_code:
void __fastcall __noreturn thread_do_work(void *a1)
{
void (__fastcall *v1)(__int64); // r12
void (__fastcall *v2)(__int64); // r14
void (__fastcall **v3)(__int64); // rbp

while ( 1 )
{
while ( pthread_mutex_lock((pthread_mutex_t *)a1) )
puts("pthread_mutex_lock error ");
if ( pthread_cond_wait((pthread_cond_t *)((char *)a1 + 40), (pthread_mutex_t *)a1) )
puts("pthread_cond_wait error ");
v3 = (void (__fastcall **)(__int64))*((_QWORD *)a1 + 12);
if ( v3 )
{
puts("Thread about to pull new task ");
if ( !*v3 )
{
pthread_mutex_unlock((pthread_mutex_t *)a1);
pthread_exit(0LL);
}
*((_QWORD *)a1 + 12) = *(_QWORD *)(*((_QWORD *)a1 + 12) + 16LL);
if ( pthread_mutex_unlock((pthread_mutex_t *)a1) )
puts("pthread_mutex_unlock error ");
v1 = *v3;
v2 = v3[1];
puts("About to free curr_task. ");
free(v3);
puts("Freed curr_task ");
v1((__int64)v2);
puts("Called function. ");
}
else
{
pthread_mutex_unlock((pthread_mutex_t *)a1);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
void (__fastcall *v1)(__int64); // r12
void (__fastcall *v2)(__int64); // r14
void (__fastcall **v3)(__int64); // rbp

while ( 1 )
{
while ( pthread_mutex_lock((pthread_mutex_t *)a1) )
puts("pthread_mutex_lock error ");
if ( pthread_cond_wait((pthread_cond_t *)((char *)a1 + 40), (pthread_mutex_t *)a1) )
puts("pthread_cond_wait error ");
v3 = (void (__fastcall **)(__int64))*((_QWORD *)a1 + 12);
if ( v3 )
{
puts("Thread about to pull new task ");
if ( !*v3 )
{
pthread_mutex_unlock((pthread_mutex_t *)a1);
pthread_exit(0LL);
}
*((_QWORD *)a1 + 12) = *(_QWORD *)(*((_QWORD *)a1 + 12) + 16LL);
if ( pthread_mutex_unlock((pthread_mutex_t *)a1) )
puts("pthread_mutex_unlock error ");
v1 = *v3;
v2 = v3[1];
puts("About to free curr_task. ");
free(v3);
puts("Freed curr_task ");
v1((__int64)v2);
puts("Called function. ");
}
else
{
pthread_mutex_unlock((pthread_mutex_t *)a1);
}
}
}

original_code:
static void *thread_do_work(void *threadpool)
{
    while(1) {
        threadpool_t* pool = (threadpool_t*) threadpool;
        pthread_mutex_t *lock = &(pool->lock);
        pthread_cond_t *notify = &(pool->notify);
        int err = pthread_mutex_lock(lock);
        if(err)
        {
    printf("pthread_mutex_lock error \n");
    continue;
 }
       err = pthread_cond_wait(notify, lock);
       if (err)
       {printf("pthread_cond_wait error \n");}
 threadpool_task_t* curr_task = pool->queue;
       if (curr_task==((void *)0))
         {
     pthread_mutex_unlock(lock);
            continue;
         }
        printf("Thread about to pull new task \n");
 if(curr_task->function == ((void *)0))
        {
         pthread_mutex_unlock(&(pool->lock));
         pthread_exit(((void *)0));
      return(((void *)0));
        }
        pool->queue=pool->queue->next;
 err = pthread_mutex_unlock(lock);
        if(err)
        {printf("pthread_mutex_unlock error \n");}
         void (*function) (void*);
         void *argument;
         function = curr_task->function;
         argument = curr_task->argument;
        printf("About to free curr_task. \n");
         free ((void*) curr_task);
 printf("Freed curr_task \n");
         function(argument);
        printf("Called function. \n");
    }
}

canonical_original_code:
static void *thread_do_work(void *threadpool)
{
    while(1) {
        struct threadpool_t *pool = (threadpool_t*) threadpool;
        union pthread_mutex_t *lock = &(pool->lock);
        union pthread_cond_t *notify = &(pool->notify);
        int err = pthread_mutex_lock(lock);
        if(err)
        {
    printf("pthread_mutex_lock error \n");
    continue;
 }
       err = pthread_cond_wait(notify, lock);
       if (err)
       {printf("pthread_cond_wait error \n");}
 struct threadpool_task_t *curr_task = pool->queue;
       if (curr_task==((void *)0))
         {
     pthread_mutex_unlock(lock);
            continue;
         }
        printf("Thread about to pull new task \n");
 if(curr_task->function == ((void *)0))
        {
         pthread_mutex_unlock(&(pool->lock));
         pthread_exit(((void *)0));
      return(((void *)0));
        }
        pool->queue=pool->queue->next;
 err = pthread_mutex_unlock(lock);
        if(err)
        {printf("pthread_mutex_unlock error \n");}
         void (*function)(void *);
         void *argument;
         function = curr_task->function;
         argument = curr_task->argument;
        printf("About to free curr_task. \n");
         free ((void *) curr_task);
 printf("Freed curr_task \n");
         function(argument);
        printf("Called function. \n");
    }
}

variable_types:
threadpool
void *

variable_types: struct threadpool_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } notify; unsigned long *threads; struct threadpool_task_t { void (*function)(void *); void *argument; struct threadpool_task_t *next; } *queue; int thread_count; int task_queue_size_limit; } *

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *

variable_types: union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } *

variable_types: err int

variable_types:
function
void (*)(void *)

variable_types:
argument
void *

return_type: void *

user_defined_types: struct threadpool_t { union pthread_mutex_t lock; union pthread_cond_t notify; unsigned long *threads; struct threadpool_task_t *queue; int thread_count; int task_queue_size_limit; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall threadpool_add_task(pthread_mutex_t *mutex, __int64 a2, __int64 a3)
{
unsigned int v4; // ebp
_QWORD *v5; // rcx
__int64 v6; // rax
__int64 v7; // rdx
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // ebx

puts("In critical section. Main thread is adding a task to the queue. Queue is locked from other threads ");
v4 = pthread_mutex_lock(mutex);
puts("aquired lock ");
if ( v4 )
{
printf("Error when locking mutex. pthread_mutex_lock returned: %d \n", v4);
v10 = -1;
}
else
{
v5 = malloc(0x18uLL);
*v5 = a2;
v5[1] = a3;
v5[2] = 0LL;
v6 = *(&mutex[2].__align + 2);
if ( v6 )
{
do
{
v7 = v6;
v6 = *(_QWORD *)(v6 + 16);
}
while ( v6 );
*(_QWORD *)(v7 + 16) = v5;
}
else
{
*(&mutex[2].__align + 2) = (__int64)v5;
}
puts("About to broadcast notify ");
v8 = pthread_cond_broadcast((pthread_cond_t *)&mutex[1]);
if ( v8 )
{
printf("Error when broadcasting cond. pthread_cond_broadcast returned: %d \n", v8);
v10 = -1;
}
else
{
v9 = pthread_mutex_unlock(mutex);
v10 = v9;
if ( v9 )
{
printf("Error when unlocking mutex. pthread_mutex_unlock returned: %d \n", v9);
v10 = -1;
}
else
{
puts("End of add_task function ");
}
}
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func1(pthread_mutex_t *mutex, __int64 a2, __int64 a3)
{
unsigned int v4; // ebp
_QWORD *v5; // rcx
__int64 v6; // rax
__int64 v7; // rdx
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // ebx

puts("In critical section. Main thread is adding a task to the queue. Queue is locked from other threads ");
v4 = pthread_mutex_lock(mutex);
puts("aquired lock ");
if ( v4 )
{
printf("Error when locking mutex. pthread_mutex_lock returned: %d \n", v4);
v10 = -1;
}
else
{
v5 = malloc(0x18uLL);
*v5 = a2;
v5[1] = a3;
v5[2] = 0LL;
v6 = *(&mutex[2].__align + 2);
if ( v6 )
{
do
{
v7 = v6;
v6 = *(_QWORD *)(v6 + 16);
}
while ( v6 );
*(_QWORD *)(v7 + 16) = v5;
}
else
{
*(&mutex[2].__align + 2) = (__int64)v5;
}
puts("About to broadcast notify ");
v8 = pthread_cond_broadcast((pthread_cond_t *)&mutex[1]);
if ( v8 )
{
printf("Error when broadcasting cond. pthread_cond_broadcast returned: %d \n", v8);
v10 = -1;
}
else
{
v9 = pthread_mutex_unlock(mutex);
v10 = v9;
if ( v9 )
{
printf("Error when unlocking mutex. pthread_mutex_unlock returned: %d \n", v9);
v10 = -1;
}
else
{
puts("End of add_task function ");
}
}
}
return v10;
}

original_code:
int threadpool_add_task(threadpool_t *pool, void (*function)(void *), void *argument)
{
    int err = 0;
    pthread_mutex_t* lock = &(pool->lock);
     printf("In critical section. Main thread is adding a task to the queue. Queue is locked from other threads \n");
    err = pthread_mutex_lock(lock);
    printf ("aquired lock \n");
if (err)
    {printf("Error when locking mutex. pthread_mutex_lock returned: %d \n", err);
return -1;
}
    threadpool_task_t* new_task = (threadpool_task_t*) malloc (sizeof(threadpool_task_t));
    new_task->function=function;
    new_task->argument=argument;
    new_task->next=((void *)0);
     if (pool->queue==((void *)0))
     {pool->queue=new_task;}
   else
     {
     threadpool_task_t* curr = pool->queue;
     while (curr->next!=((void *)0))
      {curr = curr->next;}
     curr->next = new_task;
     }
    pthread_cond_t* notify = &(pool->notify);
    printf("About to broadcast notify \n");
    err = pthread_cond_broadcast(notify);
    if (err)
    {printf("Error when broadcasting cond. pthread_cond_broadcast returned: %d \n", err);
return -1;
}
    err = pthread_mutex_unlock(lock);
       if (err)
    {printf("Error when unlocking mutex. pthread_mutex_unlock returned: %d \n", err);
return -1;
}
   printf("End of add_task function \n");
    return err;
}

canonical_original_code:
int threadpool_add_task(struct threadpool_t *pool, void (*function)(void *), void *argument)
{
    int err = 0;
    union pthread_mutex_t *lock = &(pool->lock);
     printf("In critical section. Main thread is adding a task to the queue. Queue is locked from other threads \n");
    err = pthread_mutex_lock(lock);
    printf ("aquired lock \n");
if (err)
    {printf("Error when locking mutex. pthread_mutex_lock returned: %d \n", err);
return -1;
}
    struct threadpool_task_t *new_task = (threadpool_task_t*) malloc (sizeof(threadpool_task_t));
    new_task->function=function;
    new_task->argument=argument;
    new_task->next=((void *)0);
     if (pool->queue==((void *)0))
     {pool->queue=new_task;}
   else
     {
     struct threadpool_task_t *curr = pool->queue;
     while (curr->next!=((void *)0))
      {curr = curr->next;}
     curr->next = new_task;
     }
    union pthread_cond_t *notify = &(pool->notify);
    printf("About to broadcast notify \n");
    err = pthread_cond_broadcast(notify);
    if (err)
    {printf("Error when broadcasting cond. pthread_cond_broadcast returned: %d \n", err);
return -1;
}
    err = pthread_mutex_unlock(lock);
       if (err)
    {printf("Error when unlocking mutex. pthread_mutex_unlock returned: %d \n", err);
return -1;
}
   printf("End of add_task function \n");
    return err;
}

variable_types: struct threadpool_task_t { void (*function)(void *); void *argument; struct threadpool_task_t *next; } *

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall threadpool_destroy(void *ptr)
{
__int64 v1; // rbx

threadpool_add_task((pthread_mutex_t *)ptr, 0LL, 0LL);
pthread_cond_broadcast((pthread_cond_t *)((char *)ptr + 40));
if ( *((int *)ptr + 26) > 0 )
{
v1 = 0LL;
do
pthread_join(*(_QWORD *)(*((_QWORD *)ptr + 11) + 8 * v1++), 0LL);
while ( *((_DWORD *)ptr + 26) > (int)v1 );
}
free(*((void **)ptr + 12));
free(*((void **)ptr + 11));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
__int64 v1; // rbx

func1((pthread_mutex_t *)ptr, 0LL, 0LL);
pthread_cond_broadcast((pthread_cond_t *)((char *)ptr + 40));
if ( *((int *)ptr + 26) > 0 )
{
v1 = 0LL;
do
pthread_join(*(_QWORD *)(*((_QWORD *)ptr + 11) + 8 * v1++), 0LL);
while ( *((_DWORD *)ptr + 26) > (int)v1 );
}
free(*((void **)ptr + 12));
free(*((void **)ptr + 11));
free(ptr);
return 0LL;
}

original_code:
int threadpool_destroy(threadpool_t *pool)
{
    int err = 0;
   threadpool_add_task(pool, ((void *)0), ((void *)0));
   pthread_cond_broadcast(&(pool->notify));
   int i;
 for (i=0; i<pool->thread_count; i++)
 {pthread_join(pool->threads[i],((void *)0));}
   free ((void*) pool->queue);
   free ((void*) pool->threads);
   free ((void*) pool);
    return err;
}

canonical_original_code:
int threadpool_destroy(struct threadpool_t *pool)
{
    int err = 0;
   threadpool_add_task(pool, ((void *)0), ((void *)0));
   pthread_cond_broadcast(&(pool->notify));
   int i;
 for (i=0; i<pool->thread_count; i++)
 {pthread_join(pool->threads[i],((void *)0));}
   free ((void *) pool->queue);
   free ((void *) pool->threads);
   free ((void *) pool);
    return err;
}

variable_types: i int

user_defined_types: struct threadpool_task_t { void (*function)(void *); void *argument; struct threadpool_task_t *next; }


Handlesignal

repo: iloster/linux

decompiled_code:
void __noreturn Handlesignal()
{
printf("end program!\n");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
printf("end program!\n");
exit(0);
}

original_code:
void Handlesignal(int signo) {
 printf("end program!\n", signo);
 exit(0);
}

variable_types: signo int


decompiled_code:
__int64 __fastcall isdigit_all(char *a1)
{
char v2; // bl

while ( 1 )
{
v2 = *a1;
if ( !*a1 )
break;
++a1;
if ( ((*_ctype_b_loc())[v2] & 0x800) == 0 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
char v2; // bl

while ( 1 )
{
v2 = *a1;
if ( !*a1 )
break;
++a1;
if ( ((*_ctype_b_loc())[v2] & 0x800) == 0 )
return 0LL;
}
return 1LL;
}

original_code:
int isdigit_all(char *str)
{
    while (*str != '\0')
    {
        if (!((*__ctype_b_loc ())[(int) ((*str++))] & (unsigned short int) _ISdigit))
            return 0;
    }
    return 1;
}

canonical_original_code:
int isdigit_all(char *str)
{
    while (*str != '\0')
    {
        if (!((*__ctype_b_loc ())[(int) ((*str++))] & (unsigned short) _ISdigit))
            return 0;
    }
    return 1;
}

variable_types:
str
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
size_t v4; // rbx
int v5; // ebp
int v6; // ebx
int v7; // eax
int v8; // ebx
int v9; // eax
pthread_t v10; // [rsp+0h] [rbp-38h] BYREF
pthread_t th; // [rsp+8h] [rbp-30h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-28h] BYREF
int arg[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( argc != 4 )
{
puts("error input! 1.filename_in,2.filename_out,3,length of buffer");
exit(0);
}
v3 = (char *)argv[3];
if ( !(unsigned int)isdigit_all(v3) )
{
puts("error input! 1.filename_in,2.filename_out,3,length of buffer");
puts("error length of buffer must be digit!");
exit(0);
}
result_copy = 0;
filename_in = (__int64)argv[1];
filename_out = (__int64)argv[2];
len = atoi(v3);
arg[0] = 0;
v4 = len;
data_s = malloc(len);
data_t = malloc(v4);
if ( signal(2, (__sighandler_t)Handlesignal) == (__sighandler_t)-1LL )
puts("error signal out");
v5 = sem_init(&read_sem, 0, 1u);
v6 = sem_init(&write_sem, 0, 0);
v7 = sem_init(&copy_sem, 0, 0);
if ( v6 == 0 || v5 == 0 || !v7 )
{
v8 = pthread_mutex_init(&read_mutex, 0LL);
v9 = pthread_mutex_init(&write_mutex, 0LL);
if ( v8 && v9 )
{
puts("mutex error");
exit(1);
}
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))&get, arg) )
{
puts("read thread create error!");
exit(1);
}
++arg[0];
if ( pthread_create(&th, 0LL, (void *(*)(void *))&copy, arg) )
{
puts("copy thread create error!");
exit(1);
}
++arg[0];
if ( pthread_create(&v10, 0LL, (void *(*)(void *))&put, arg) )
{
puts("write thread create error!");
exit(1);
}
pthread_join(newthread, 0LL);
pthread_join(th, 0LL);
pthread_join(v10, 0LL);
free(data_s);
free(data_t);
exit(0);
}
puts("sem error");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
size_t v4; // rbx
int v5; // ebp
int v6; // ebx
int v7; // eax
int v8; // ebx
int v9; // eax
pthread_t v10; // [rsp+0h] [rbp-38h] BYREF
pthread_t th; // [rsp+8h] [rbp-30h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-28h] BYREF
int arg[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( argc != 4 )
{
puts("error input! 1.filename_in,2.filename_out,3,length of buffer");
exit(0);
}
v3 = (char *)argv[3];
if ( !(unsigned int)func1(v3) )
{
puts("error input! 1.filename_in,2.filename_out,3,length of buffer");
puts("error length of buffer must be digit!");
exit(0);
}
result_copy = 0;
filename_in = (__int64)argv[1];
filename_out = (__int64)argv[2];
len = atoi(v3);
arg[0] = 0;
v4 = len;
data_s = malloc(len);
data_t = malloc(v4);
if ( signal(2, (__sighandler_t)Handlesignal) == (__sighandler_t)-1LL )
puts("error signal out");
v5 = sem_init(&read_sem, 0, 1u);
v6 = sem_init(&write_sem, 0, 0);
v7 = sem_init(&copy_sem, 0, 0);
if ( v6 == 0 || v5 == 0 || !v7 )
{
v8 = pthread_mutex_init(&read_mutex, 0LL);
v9 = pthread_mutex_init(&write_mutex, 0LL);
if ( v8 && v9 )
{
puts("mutex error");
exit(1);
}
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))&get, arg) )
{
puts("read thread create error!");
exit(1);
}
++arg[0];
if ( pthread_create(&th, 0LL, (void *(*)(void *))&copy, arg) )
{
puts("copy thread create error!");
exit(1);
}
++arg[0];
if ( pthread_create(&v10, 0LL, (void *(*)(void *))&put, arg) )
{
puts("write thread create error!");
exit(1);
}
pthread_join(newthread, 0LL);
pthread_join(th, 0LL);
pthread_join(v10, 0LL);
free(data_s);
free(data_t);
exit(0);
}
puts("sem error");
exit(1);
}

original_code:
void main(int argc, char* argv[]) {
 if(argc!=4){
  printf("error input! 1.filename_in,2.filename_out,3,length of buffer\n");
  exit(0);
 }
 if(isdigit_all(argv[3])==0){
  printf("error input! 1.filename_in,2.filename_out,3,length of buffer\n");
  printf("error length of buffer must be digit!\n");
  exit(0);
 }
 result_copy = 0;
 filename_in=argv[1];
 filename_out=argv[2];
 len=atoi(argv[3]);
 int thread_i = 0;
 data_s = (char *) malloc(len * sizeof(char));
 data_t = (char *) malloc(len * sizeof(char));
 if (signal(2, Handlesignal) == ((__sighandler_t) -1)) {
  printf("error signal out\n");
 }
 pthread_t read_pthread;
 pthread_t copy_pthread;
 pthread_t write_pthread;
 int ini1 = sem_init(&read_sem, 0, 1);
 int ini2 = sem_init(&write_sem, 0, 0);
 int ini5 = sem_init(&copy_sem, 0, 0);
 if (ini1 && ini2 && ini5 != 0) {
  printf("sem error\n");
  exit(1);
 }
 int ini3 = pthread_mutex_init(&read_mutex, ((void *)0));
 int ini4 = pthread_mutex_init(&write_mutex, ((void *)0));
 if (ini3 && ini4 != 0) {
  printf("mutex error\n");
  exit(1);
 }
 int thread_1 = pthread_create(&read_pthread, ((void *)0), (void *)&get,
   (void *) (&thread_i));
 if (thread_1 != 0) {
  printf("read thread create error!\n");
  exit(1);
 }
 thread_i++;
 int thread_2 = pthread_create(&copy_pthread, ((void *)0), (void *)&copy,
   (void *) (&thread_i));
 if (thread_2 != 0) {
  printf("copy thread create error!\n");
  exit(1);
 }
 thread_i++;
 int thread_3 = pthread_create(&write_pthread, ((void *)0), (void *)&put,
   (void *) (&thread_i));
 if (thread_3 != 0) {
  printf("write thread create error!\n");
  exit(1);
 }
 pthread_join(read_pthread, ((void *)0));
 pthread_join(copy_pthread, ((void *)0));
 pthread_join(write_pthread, ((void *)0));
 free(data_s);
 free(data_t);
 exit(0);
}

canonical_original_code:
void main(int argc, char *argv[]) {
 if(argc!=4){
  printf("error input! 1.filename_in,2.filename_out,3,length of buffer\n");
  exit(0);
 }
 if(isdigit_all(argv[3])==0){
  printf("error input! 1.filename_in,2.filename_out,3,length of buffer\n");
  printf("error length of buffer must be digit!\n");
  exit(0);
 }
 result_copy = 0;
 filename_in=argv[1];
 filename_out=argv[2];
 len=atoi(argv[3]);
 int thread_i = 0;
 data_s = (char *) malloc(len * sizeof(char));
 data_t = (char *) malloc(len * sizeof(char));
 if (signal(2, Handlesignal) == ((__sighandler_t) -1)) {
  printf("error signal out\n");
 }
 unsigned long read_pthread;
 unsigned long copy_pthread;
 unsigned long write_pthread;
 int ini1 = sem_init(&read_sem, 0, 1);
 int ini2 = sem_init(&write_sem, 0, 0);
 int ini5 = sem_init(&copy_sem, 0, 0);
 if (ini1 && ini2 && ini5 != 0) {
  printf("sem error\n");
  exit(1);
 }
 int ini3 = pthread_mutex_init(&read_mutex, ((void *)0));
 int ini4 = pthread_mutex_init(&write_mutex, ((void *)0));
 if (ini3 && ini4 != 0) {
  printf("mutex error\n");
  exit(1);
 }
 int thread_1 = pthread_create(&read_pthread, ((void *)0), (void *)&get,
   (void *) (&thread_i));
 if (thread_1 != 0) {
  printf("read thread create error!\n");
  exit(1);
 }
 thread_i++;
 int thread_2 = pthread_create(&copy_pthread, ((void *)0), (void *)&copy,
   (void *) (&thread_i));
 if (thread_2 != 0) {
  printf("copy thread create error!\n");
  exit(1);
 }
 thread_i++;
 int thread_3 = pthread_create(&write_pthread, ((void *)0), (void *)&put,
   (void *) (&thread_i));
 if (thread_3 != 0) {
  printf("write thread create error!\n");
  exit(1);
 }
 pthread_join(read_pthread, ((void *)0));
 pthread_join(copy_pthread, ((void *)0));
 pthread_join(write_pthread, ((void *)0));
 free(data_s);
 free(data_t);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]


put

decompiled_code:
__int64 put()
{
unsigned int v0; // eax

v0 = open(filename_out, 66, 384LL);
if ( v0 == -1 )
{
puts("open file wrong!");
exit(0);
}
return file_out(v0, data_t, (unsigned int)len);
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax

v0 = open(filename_out, 66, 384LL);
if ( v0 == -1 )
{
puts("open file wrong!");
exit(0);
}
return file_out(v0, data_t, (unsigned int)len);
}

original_code:
void put() {
 int fd = open(filename_out, 0100 | 02, 0400 | 0200);
 if (fd == -1) {
  printf("open file wrong!\n");
  exit(0);
 }
 file_out(fd, data_t, len);
}

variable_types: fd int


decompiled_code:
int __fastcall file_in(int fd, void *buf, int a3)
{
size_t v4; // r13
int result; // eax

v4 = a3;
do
{
sem_wait(&read_sem);
pthread_mutex_lock(&read_mutex);
puts("data_in..........");
if ( lseek(fd, 0LL, 1) == -1 )
puts("lseek failed!");
::result = read(fd, buf, v4);
if ( ::result == -1 )
puts("read error! ");
puts("data_in..........complete");
pthread_mutex_unlock(&read_mutex);
result = sem_post(&copy_sem);
}
while ( ::result >= a3 );
::result = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd, void *buf, int a3)
{
size_t v4; // r13
int result; // eax

v4 = a3;
do
{
sem_wait(&read_sem);
pthread_mutex_lock(&read_mutex);
puts("data_in..........");
if ( lseek(fd, 0LL, 1) == -1 )
puts("lseek failed!");
::result = read(fd, buf, v4);
if ( ::result == -1 )
puts("read error! ");
puts("data_in..........complete");
pthread_mutex_unlock(&read_mutex);
result = sem_post(&copy_sem);
}
while ( ::result >= a3 );
::result = 0;
return result;
}

original_code:
void file_in(int fd, char *data, int len) {
 while (1) {
  sem_wait(&read_sem);
  pthread_mutex_lock(&read_mutex);
  printf("data_in..........\n");
  if (lseek(fd, 0, 1) == -1) {
   printf("lseek failed!\n");
  }
  if ((result = read(fd, data, len)) == -1) {
   printf("read error! \n");
  }
  int i=0,j=0;
  printf("data_in..........complete\n");
  pthread_mutex_unlock(&read_mutex);
  sem_post(&copy_sem);
  if(result<len){
   len=result;
   result=0;
   break;
  }
 }
}

canonical_original_code:
void file_in(int fd, char *data, int len) {
 while (1) {
  sem_wait(&read_sem);
  pthread_mutex_lock(&read_mutex);
  printf("data_in..........\n");
  if (lseek(fd, 0, 1) == -1) {
   printf("lseek failed!\n");
  }
  if ((result = read(fd, data, len)) == -1) {
   printf("read error! \n");
  }
  int i = 0; int j = 0;
  printf("data_in..........complete\n");
  pthread_mutex_unlock(&read_mutex);
  sem_post(&copy_sem);
  if(result<len){
   len=result;
   result=0;
   break;
  }
 }
}

variable_types:
data
char *

variable_types: len int

variable_types: j int


decompiled_code:
int __fastcall file_out(int fd, void *buf, int a3)
{
size_t v3; // rbx
int result; // eax

v3 = a3;
do
{
sem_wait(&write_sem);
pthread_mutex_lock(&write_mutex);
puts("data_out..........");
if ( write(fd, buf, v3) == -1 )
puts("write error! ");
puts("data_out..........complete");
result = pthread_mutex_unlock(&write_mutex);
}
while ( result_copy != 1 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(int fd, void *buf, int a3)
{
size_t v3; // rbx
int result; // eax

v3 = a3;
do
{
sem_wait(&write_sem);
pthread_mutex_lock(&write_mutex);
puts("data_out..........");
if ( write(fd, buf, v3) == -1 )
puts("write error! ");
puts("data_out..........complete");
result = pthread_mutex_unlock(&write_mutex);
}
while ( result_copy != 1 );
return result;
}

original_code:
void file_out(int fd, char *data, int len) {
 while (1) {
  sem_wait(&write_sem);
  pthread_mutex_lock(&write_mutex);
  printf("data_out..........\n");
  if (write(fd, data, len) == -1) {
   printf("write error! \n");
  }
  printf("data_out..........complete\n");
  pthread_mutex_unlock(&write_mutex);
  if(result_copy==1){
   break;
  }
 }
}


copy

decompiled_code:
int copy()
{
__int64 v0; // rax
int result; // eax

do
{
sem_wait(&copy_sem);
pthread_mutex_lock(&read_mutex);
pthread_mutex_lock(&write_mutex);
puts("copy..........");
strcpy(data_t, data_s);
if ( len > 0 )
{
v0 = 0LL;
do
data_s[v0++] = 32;
while ( len > (int)v0 );
}
puts("copy..........complete");
pthread_mutex_unlock(&write_mutex);
pthread_mutex_unlock(&read_mutex);
sem_post(&write_sem);
result = sem_post(&read_sem);
}
while ( ::result );
result_copy = 1;
return result;
}

canonical_decompiled_code:
int func0()
{
__int64 v0; // rax
int result; // eax

do
{
sem_wait(&copy_sem);
pthread_mutex_lock(&read_mutex);
pthread_mutex_lock(&write_mutex);
puts("copy..........");
strcpy(data_t, data_s);
if ( len > 0 )
{
v0 = 0LL;
do
data_s[v0++] = 32;
while ( len > (int)v0 );
}
puts("copy..........complete");
pthread_mutex_unlock(&write_mutex);
pthread_mutex_unlock(&read_mutex);
sem_post(&write_sem);
result = sem_post(&read_sem);
}
while ( ::result );
result_copy = 1;
return result;
}

original_code:
void copy() {
 while (1) {
  sem_wait(&copy_sem);
  pthread_mutex_lock(&read_mutex);
  pthread_mutex_lock(&write_mutex);
  printf("copy..........\n");
  strcpy(data_t,data_s);
  int i=0;
  for(;i<len;i++){
   data_s[i]=' ';
  }
  printf("copy..........complete\n");
  pthread_mutex_unlock(&write_mutex);
  pthread_mutex_unlock(&read_mutex);
  sem_post(&write_sem);
  sem_post(&read_sem);
  if (result == 0) {
   result_copy = 1;
   break;
  }
 }
}

canonical_original_code:
void copy() {
 while (1) {
  sem_wait(&copy_sem);
  pthread_mutex_lock(&read_mutex);
  pthread_mutex_lock(&write_mutex);
  printf("copy..........\n");
  strcpy(data_t,data_s);
  int i = 0;
  for(;i<len;i++){
   data_s[i]=' ';
  }
  printf("copy..........complete\n");
  pthread_mutex_unlock(&write_mutex);
  pthread_mutex_unlock(&read_mutex);
  sem_post(&write_sem);
  sem_post(&read_sem);
  if (result == 0) {
   result_copy = 1;
   break;
  }
 }
}


get

decompiled_code:
__int64 get()
{
unsigned int v0; // eax

v0 = open(filename_in, 66, 384LL);
if ( v0 == -1 )
{
puts("open file wrong!");
exit(0);
}
return file_in(v0, data_s, (unsigned int)len);
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax

v0 = open(filename_in, 66, 384LL);
if ( v0 == -1 )
{
puts("open file wrong!");
exit(0);
}
return file_in(v0, data_s, (unsigned int)len);
}

original_code:
void get() {
 int fd = open(filename_in, 0100 | 02, 0400 | 0200);
 if (fd == -1) {
  printf("open file wrong!\n");
  exit(0);
 }
 file_in(fd, data_s, len);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
omp_set_num_threads();
fatinit(100LL, argv);
iteration(1);
printf("%llf\n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
omp_set_num_threads();
func8(100LL, argv);
func7(1);
printf("%llf\n");
return 0;
}

original_code:
int main(int argc, char **argv){
  if ( 1)
    omp_set_num_threads(1);
  else
    omp_set_num_threads(omp_get_num_procs());
  fatinit(100);
  printf("%llf\n", iteration(1));
  return 0;
}

variable_types:
argv
char **


fatinit

decompiled_code:
_BOOL8 __fastcall fatinit(int a1)
{
precalc = (__int64)malloc(4LL * a1);
return precalc != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(int a1)
{
precalc = (__int64)malloc(4LL * a1);
return precalc != 0;
}

original_code:
int fatinit(int n){
  precalc = malloc(n*sizeof(*precalc));
  if (!precalc)
    return 0;
  return 1;
}

variable_types: n int


fatorial

decompiled_code:
__int64 __fastcall fatorial(unsigned int a1)
{
_DWORD *v1; // rax

printf("int -> %d\n", a1);
v1 = (_DWORD *)(precalc + 4LL * (int)a1);
if ( !*v1 )
{
if ( a1 == 1 )
*v1 = 1;
else
*(_DWORD *)(precalc + 4LL * (int)a1) = fatorial(a1 - 1) * a1;
}
printf("saida-> %d\n", *(unsigned int *)(precalc + 4LL * (int)a1));
return *(unsigned int *)(precalc + 4LL * (int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
_DWORD *v1; // rax

printf("int -> %d\n", a1);
v1 = (_DWORD *)(precalc + 4LL * (int)a1);
if ( !*v1 )
{
if ( a1 == 1 )
*v1 = 1;
else
*(_DWORD *)(precalc + 4LL * (int)a1) = func9(a1 - 1) * a1;
}
printf("saida-> %d\n", *(unsigned int *)(precalc + 4LL * (int)a1));
return *(unsigned int *)(precalc + 4LL * (int)a1);
}

original_code:
int fatorial(int n){
  printf("int -> %d\n", n);
  if (!precalc[n]){
    if (n==1)
      precalc[n] = 1;
    else
      precalc[n] = n*fatorial(n-1);
  }
  printf("saida-> %d\n", precalc[n]);
  return precalc[n];
}


ftype

repo: imazen-discovery/grlibs-misc

decompiled_code:
__int64 __fastcall ftype(const char *a1)
{
size_t v1; // r13
const char *v2; // rbx
__int64 v3; // rbp
size_t v4; // rax

v1 = strlen(a1);
check(v1 > 5, "Invalid filename '%s' (too short).", a1);
v2 = s2;
if ( !s2 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = strlen(v2);
if ( !strcasecmp(&a1[v1 - v4], v2) )
break;
++v3;
v2 = (&s2)[2 * v3];
if ( !v2 )
return 0LL;
}
return (unsigned int)Types[4 * (int)v3];
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
size_t v1; // r13
const char *v2; // rbx
__int64 v3; // rbp
size_t v4; // rax

v1 = strlen(a1);
func10(v1 > 5, "Invalid filename '%s' (too short).", a1);
v2 = s2;
if ( !s2 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = strlen(v2);
if ( !strcasecmp(&a1[v1 - v4], v2) )
break;
++v3;
v2 = (&s2)[2 * v3];
if ( !v2 )
return 0LL;
}
return (unsigned int)Types[4 * (int)v3];
}

original_code:
enum FType
ftype(const char *filename) {
    size_t len = strlen(filename);
    int n;
    check(len > 5, "Invalid filename '%s' (too short).", filename);
    for (n = 0; Types[n].ext; n++) {
        const char *ext = &filename[len - strlen(Types[n].ext)];
        if (strcasecmp(ext, Types[n].ext) == 0) {
            return Types[n].id;
        }
    }
    return UNKNOWN;
}

canonical_original_code:
enum FType ftype(char *filename) {
    unsigned long len = strlen(filename);
    int n;
    check(len > 5, "Invalid filename '%s' (too short).", filename);
    for (n = 0; Types[n].ext; n++) {
        char *ext = &filename[len - strlen(Types[n].ext)];
        if (strcasecmp(ext, Types[n].ext) == 0) {
            return Types[n].id;
        }
    }
    return UNKNOWN;
}

variable_types:
filename
char *

variable_types:
len
unsigned long

variable_types:
ext
char *

return_type: enum FType { UNKNOWN=0, PNG=1, JPG=2, GIF=3, TIFF=4 }


check

decompiled_code:
void *check(int a1, const char *a2, ...)
{
void *result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
result = arg[0].reg_save_area;
if ( !a1 )
{
printf("Fatal error: ");
vprintf(a2, arg);
putchar(10);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *func10(int a1, const char *a2, ...)
{
void *result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
result = arg[0].reg_save_area;
if ( !a1 )
{
printf("Fatal error: ");
vprintf(a2, arg);
putchar(10);
exit(1);
}
return result;
}

original_code:
void
check(int cond, const char *fmt, ...) {
    va_list ap;
    __builtin_va_start(ap,fmt);
    if (!cond) {
        printf("Fatal error: ");
        vprintf (fmt, ap);
        printf("\n");
        exit(1);
    }
    __builtin_va_end(ap);
}

canonical_original_code:
void check(int cond, char *fmt, ...) {
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    if (!cond) {
        printf("Fatal error: ");
        vprintf (fmt, ap);
        printf("\n");
        exit(1);
    }
    __builtin_va_end(ap);
}

variable_types: cond int

variable_types:
fmt
char *


floattime

decompiled_code:
double __fastcall floattime(int a1, int a2)
{
return (double)a2 / 1000000000.0 + (double)a1;
}

canonical_decompiled_code:
double __fastcall func11(int a1, int a2)
{
return (double)a2 / 1000000000.0 + (double)a1;
}

original_code:
static inline double floattime(struct timespec now) {
    return (double)now.tv_sec + (double)now.tv_nsec/1000000000.0;
}

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

return_type: double


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

va_start(arg, a2);
times = realloc(times, 272LL * (unsigned int)(current + 1));
check(times != 0LL, "realloc failed!\n");
v2 = clock_gettime(1, &last_time);
check(v2 == 0, "clock_gettime() failed.");
*((_QWORD *)times + 34 * (unsigned int)current) = a1;
return vsnprintf((char *)times + 272 * (unsigned int)current + 8, 0xFFuLL, a2, arg);
}

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

va_start(arg, a2);
times = realloc(times, 272LL * (unsigned int)(current + 1));
func10(times != 0LL, "realloc failed!\n");
v2 = clock_gettime(1, &last_time);
func10(v2 == 0, "clock_gettime() failed.");
*((_QWORD *)times + 34 * (unsigned int)current) = a1;
return vsnprintf((char *)times + 272 * (unsigned int)current + 8, 0xFFuLL, a2, arg);
}

original_code:
void
timer_start(const char *file, const char *descfmt, ...) {
    va_list ap;
    __builtin_va_start(ap,descfmt);
    times = realloc(times, sizeof(struct Times)* (current + 1));
    check(!!times, "realloc failed!\n");
    check(!clock_gettime(1, &last_time), "clock_gettime() failed.");
    times[current].file = file;
    vsnprintf(times[current].desc, 255, descfmt, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void timer_start(char *file, char *descfmt, ...) {
    __builtin_va_list ap;
    __builtin_va_start(ap,descfmt);
    times = realloc(times, sizeof(struct Times)* (current + 1));
    check(!!times, "realloc failed!\n");
    check(!clock_gettime(1, &last_time), "clock_gettime() failed.");
    times[current].file = file;
    vsnprintf(times[current].desc, 255, descfmt, ap);
    __builtin_va_end(ap);
}

variable_types:
file
char *

variable_types:
descfmt
char *

user_defined_types: struct Times { char *file; char desc[255]; double elapsed; }


decompiled_code:
double *timer_done()
{
int v0; // eax
double v1; // rbx
double v2; // xmm0_8
int v3; // ecx
double *result; // rax
struct timespec v5; // [rsp+0h] [rbp-18h] BYREF

v0 = clock_gettime(1, &v5);
check(v0 == 0, "clock_gettime() failed.");
v1 = floattime(v5.tv_sec, v5.tv_nsec);
v2 = floattime(last_time.tv_sec, last_time.tv_nsec);
v3 = current;
result = (double *)((char *)times + 272 * (unsigned int)current);
result[33] = v1 - v2;
current = v3 + 1;
return result;
}

canonical_decompiled_code:
double *func13()
{
int v0; // eax
double v1; // rbx
double v2; // xmm0_8
int v3; // ecx
double *result; // rax
struct timespec v5; // [rsp+0h] [rbp-18h] BYREF

v0 = clock_gettime(1, &v5);
func10(v0 == 0, "clock_gettime() failed.");
v1 = func11(v5.tv_sec, v5.tv_nsec);
v2 = func11(last_time.tv_sec, last_time.tv_nsec);
v3 = current;
result = (double *)((char *)times + 272 * (unsigned int)current);
result[33] = v1 - v2;
current = v3 + 1;
return result;
}

original_code:
void
timer_done() {
    struct timespec now;
    check(!clock_gettime(1, &now), "clock_gettime() failed.");
    times[current].elapsed = floattime(now) - floattime(last_time);
    ++current;
}

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


decompiled_code:
int print_times()
{
__int64 v0; // rbx
double v1; // r12
unsigned int v2; // ebp
int result; // eax

if ( current )
{
v0 = 0LL;
v1 = 0.0;
v2 = 0;
do
{
printf(
"%s\t%s\t%f\n",
*(const char **)((char *)times + v0),
(const char *)times + v0 + 8,
1000.0 * *(double *)((char *)times + v0 + 264));
v1 = v1 + *(double *)((char *)times + v0 + 264);
++v2;
v0 += 272LL;
}
while ( current > v2 );
}
else
{
v1 = 0.0;
}
result = printf("(none)\tTotal:\t%f\n", v1 * 1000.0);
current = 0;
return result;
}

canonical_decompiled_code:
int func14()
{
__int64 v0; // rbx
double v1; // r12
unsigned int v2; // ebp
int result; // eax

if ( current )
{
v0 = 0LL;
v1 = 0.0;
v2 = 0;
do
{
printf(
"%s\t%s\t%f\n",
*(const char **)((char *)times + v0),
(const char *)times + v0 + 8,
1000.0 * *(double *)((char *)times + v0 + 264));
v1 = v1 + *(double *)((char *)times + v0 + 264);
++v2;
v0 += 272LL;
}
while ( current > v2 );
}
else
{
v1 = 0.0;
}
result = printf("(none)\tTotal:\t%f\n", v1 * 1000.0);
current = 0;
return result;
}

original_code:
void
print_times() {
    int n = 0;
    double total = 0;
    for (n = 0; n < current; n++) {
        printf ("%s\t%s\t%f\n", times[n].file, times[n].desc,
                times[n].elapsed*1000.0);
        total += times[n].elapsed;
    }
    printf("(none)\tTotal:\t%f\n", total*1000.0);
    current = 0;
}

variable_types: total double


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // ebp
double v5; // r12
const char *v6; // r13
__int64 v7; // rbx
__int64 v8; // rdi
unsigned int v9; // eax
unsigned int v11; // eax

check(argc == 5, "USAGE: blur <input> <radius> <sigma> <output>", envp);
v3 = (char *)argv[1];
v4 = atoi(argv[2]);
v5 = atof(argv[3]);
v6 = argv[4];
puts("Loading...");
v7 = load(v3);
puts("Blurring...");
if ( v4 <= 0 )
{
puts("Using old blur...");
timer_start("", "old-blur");
v11 = gdImageGaussianBlur();
check(v11, "gdImageGaussianBlur() failed!");
((void (*)(void))timer_done)();
}
else
{
puts("Using new blur...");
timer_start("", "new-blur");
v8 = v7;
v7 = gdImageCopyGaussianBlurred();
timer_done(v8, (unsigned int)v4, v5);
check(v7 != 0, "Gaussian blur failed.");
}
puts("Saving...");
v9 = ftype(v3);
save(v7, v6, v9);
print_times();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // ebp
double v5; // r12
const char *v6; // r13
__int64 v7; // rbx
__int64 v8; // rdi
unsigned int v9; // eax
unsigned int v11; // eax

func10(argc == 5, "USAGE: blur <input> <radius> <sigma> <output>", envp);
v3 = (char *)argv[1];
v4 = atoi(argv[2]);
v5 = atof(argv[3]);
v6 = argv[4];
puts("Loading...");
v7 = func9(v3);
puts("Blurring...");
if ( v4 <= 0 )
{
puts("Using old blur...");
func12("", "old-blur");
v11 = gdImageGaussianBlur();
func10(v11, "gdImageGaussianBlur() failed!");
((void (*)(void))timer_done)();
}
else
{
puts("Using new blur...");
func12("", "new-blur");
v8 = v7;
v7 = gdImageCopyGaussianBlurred();
func13(v8, (unsigned int)v4, v5);
func10(v7 != 0, "Gaussian blur failed.");
}
puts("Saving...");
v9 = func7(v3);
func8(v7, v6, v9);
func14();
return 0;
}

original_code:
int
main(int argc, char *argv[]) {
    char *ifile, *ofile;
    int radius;
    double sigma;
    gdImagePtr im, result;
    check(argc == 5, "USAGE: blur <input> <radius> <sigma> <output>");
    ifile = argv[1];
    radius = atoi(argv[2]);
    sigma = atof(argv[3]);
    ofile = argv[4];
    printf("Loading...\n");
    im = load(ifile);
    printf("Blurring...\n");
    if (radius <= 0) {
        printf("Using old blur...\n");
        timer_start("", "old-blur");
        check(gdImageGaussianBlur(im), "gdImageGaussianBlur() failed!");
        timer_done();
        result = im;
    } else {
        printf("Using new blur...\n");
        timer_start("", "new-blur");
        result = gdImageCopyGaussianBlurred(im, radius, sigma);
        timer_done();
        check(!!result, "Gaussian blur failed.");
    }
    printf("Saving...\n");
    save(result, ofile, ftype(ifile));
    print_times();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    char *ifile; char *ofile;
    int radius;
    double sigma;
    struct gdImageStruct *im; struct gdImageStruct *result;
    check(argc == 5, "USAGE: blur <input> <radius> <sigma> <output>");
    ifile = argv[1];
    radius = atoi(argv[2]);
    sigma = atof(argv[3]);
    ofile = argv[4];
    printf("Loading...\n");
    im = load(ifile);
    printf("Blurring...\n");
    if (radius <= 0) {
        printf("Using old blur...\n");
        timer_start("", "old-blur");
        check(gdImageGaussianBlur(im), "gdImageGaussianBlur() failed!");
        timer_done();
        result = im;
    } else {
        printf("Using new blur...\n");
        timer_start("", "new-blur");
        result = gdImageCopyGaussianBlurred(im, radius, sigma);
        timer_done();
        check(!!result, "Gaussian blur failed.");
    }
    printf("Saving...\n");
    save(result, ofile, ftype(ifile));
    print_times();
    return 0;
}

variable_types: argc int

variable_types:
ifile
char *

variable_types:
ofile
char *

variable_types: radius int

variable_types: sigma double

variable_types: struct gdImageStruct { unsigned char **pixels; int sx; int sy; int colorsTotal; int red[256]; int green[256]; int blue[256]; int open[256]; int transparent; int *polyInts; int polyAllocated; struct gdImageStruct *brush; struct gdImageStruct *tile; int brushColorMap[256]; int tileColorMap[256]; int styleLength; int stylePos; int *style; int interlace; int thick; int alpha[256]; int trueColor; int **tpixels; int alphaBlendingFlag; int saveAlphaFlag; int AA; int AA_color; int AA_dont_blend; int cx1; int cy1; int cx2; int cy2; unsigned int res_x; unsigned int res_y; int paletteQuantizationMethod; int paletteQuantizationSpeed; int paletteQuantizationMinQuality; int paletteQuantizationMaxQuality; enum gdInterpolationMethod { GD_DEFAULT=0, GD_BELL=1, GD_BESSEL=2, GD_BILINEAR_FIXED=3, GD_BICUBIC=4, GD_BICUBIC_FIXED=5, GD_BLACKMAN=6, GD_BOX=7, GD_BSPLINE=8, GD_CATMULLROM=9, GD_GAUSSIAN=10, GD_GENERALIZED_CUBIC=11, GD_HERMITE=12, GD_HAMMING=13, GD_HANNING=14, GD_MITCHELL=15, GD_NEAREST_NEIGHBOUR=16, GD_POWER=17, GD_QUADRATIC=18, GD_SINC=19, GD_TRIANGLE=20, GD_WEIGHTED4=21, GD_LINEAR=22, GD_METHOD_COUNT=23 } interpolation_id; double (*interpolation)(double); } *

user_defined_types: struct gdImageStruct { unsigned char **pixels; int sx; int sy; int colorsTotal; int red[256]; int green[256]; int blue[256]; int open[256]; int transparent; int *polyInts; int polyAllocated; struct gdImageStruct *brush; struct gdImageStruct *tile; int brushColorMap[256]; int tileColorMap[256]; int styleLength; int stylePos; int *style; int interlace; int thick; int alpha[256]; int trueColor; int **tpixels; int alphaBlendingFlag; int saveAlphaFlag; int AA; int AA_color; int AA_dont_blend; int cx1; int cy1; int cx2; int cy2; unsigned int res_x; unsigned int res_y; int paletteQuantizationMethod; int paletteQuantizationSpeed; int paletteQuantizationMinQuality; int paletteQuantizationMaxQuality; enum gdInterpolationMethod interpolation_id; double (*interpolation)(double); }


original_code:
static double *
gaussian_coeffs(int radius, double *sigmaArg) {
    const double sigma = (*sigmaArg <= 0.0) ? (2.0/3.0)*radius : *sigmaArg;
    const double s = 2.0 * sigma * sigma;
    double *result;
    double sum = 0;
    int x, n, count;
    count = 2*radius + 1;
    result = malloc(sizeof(double) * count);
    if (!result) {
        return ((void *)0);
    }
    for (x = -radius; x <= radius; x++) {
        double coeff = exp(-(x*x)/s);
        sum += coeff;
        result[x + radius] = coeff;
    }
    for (n = 0; n < count; n++) {
        result[n] /= sum;
    }
    *sigmaArg = sigma;
    return result;
}

canonical_original_code:
static double *gaussian_coeffs(int radius, double *sigmaArg) {
    double sigma = (*sigmaArg <= 0.0) ? (2.0/3.0)*radius : *sigmaArg;
    double s = 2.0 * sigma * sigma;
    double *result;
    double sum = 0;
    int x; int n; int count;
    count = 2*radius + 1;
    result = malloc(sizeof(double) * count);
    if (!result) {
        return ((void *)0);
    }
    for (x = -radius; x <= radius; x++) {
        double coeff = exp(-(x*x)/s);
        sum += coeff;
        result[x + radius] = coeff;
    }
    for (n = 0; n < count; n++) {
        result[n] /= sum;
    }
    *sigmaArg = sigma;
    return result;
}

variable_types:
sigmaArg
double *

variable_types: s double

variable_types:
result
double *

variable_types: sum double

variable_types: x int

variable_types: count int

variable_types: coeff double

return_type: double *


check

canonical_decompiled_code:
void *func8(int a1, const char *a2, ...)
{
void *result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
result = arg[0].reg_save_area;
if ( !a1 )
{
printf("Fatal error: ");
vprintf(a2, arg);
putchar(10);
exit(1);
}
return result;
}

variable_types: cond int

variable_types:
fmt
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rdx
double *v5; // rbp
__int64 v6; // rdx
int v7; // eax
double *v8; // rbx
double *v9; // rbp
__int64 v11; // rdx
double v12[4]; // [rsp+8h] [rbp-20h] BYREF

v12[0] = -1.0;
check((unsigned int)(argc - 2) <= 1, "USAGE: gbcoeffs <radius> [<sigma>]", envp);
v3 = atoi(argv[1]);
check(v3 > 0, "Radius must be 1 or greater.", v4);
if ( argc == 3 )
{
v12[0] = atof(argv[2]);
check(v12[0] > 0.0, "Sigma must be greater than 0.0.", v11);
}
v5 = gaussian_coeffs(v3, v12);
check(v5 != 0LL, "coeff error!!!!!!!!", v6);
printf("sigma = %f\n", v12[0]);
printf("coeffs = {");
v7 = 2 * v3;
if ( (v3 & 0x40000000) == 0 )
{
v8 = v5;
v9 = &v5[v7 + 1];
do
printf("%lf, ", *v8++);
while ( v8 != v9 );
}
puts("}");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rdx
double *v5; // rbp
__int64 v6; // rdx
int v7; // eax
double *v8; // rbx
double *v9; // rbp
__int64 v11; // rdx
double v12[4]; // [rsp+8h] [rbp-20h] BYREF

v12[0] = -1.0;
func8((unsigned int)(argc - 2) <= 1, "USAGE: gbcoeffs <radius> [<sigma>]", envp);
v3 = atoi(argv[1]);
func8(v3 > 0, "Radius must be 1 or greater.", v4);
if ( argc == 3 )
{
v12[0] = atof(argv[2]);
func8(v12[0] > 0.0, "Sigma must be greater than 0.0.", v11);
}
v5 = func7(v3, v12);
func8(v5 != 0LL, "coeff error!!!!!!!!", v6);
printf("sigma = %f\n", v12[0]);
printf("coeffs = {");
v7 = 2 * v3;
if ( (v3 & 0x40000000) == 0 )
{
v8 = v5;
v9 = &v5[v7 + 1];
do
printf("%lf, ", *v8++);
while ( v8 != v9 );
}
puts("}");
return 0;
}

original_code:
int main(int argc, char *argv[]) {
    int radius, n;
    double sigma = -1.0;
    double *coeffs;
    check(argc == 2 || argc == 3, "USAGE: gbcoeffs <radius> [<sigma>]");
    radius = atoi(argv[1]);
    check(radius > 0, "Radius must be 1 or greater.");
    if (argc == 3) {
        sigma = atof(argv[2]);
        check(sigma > 0, "Sigma must be greater than 0.0.");
    }
    coeffs = gaussian_coeffs(radius, &sigma);
    check(!!coeffs, "coeff error!!!!!!!!");
    printf("sigma = %f\n", sigma);
    printf("coeffs = {");
    for (n = 0; n < 2*radius + 1; n++) {
        printf("%lf, ", coeffs[n]);
    }
    printf("}\n");
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int radius; int n;
    double sigma = -1.0;
    double *coeffs;
    check(argc == 2 || argc == 3, "USAGE: gbcoeffs <radius> [<sigma>]");
    radius = atoi(argv[1]);
    check(radius > 0, "Radius must be 1 or greater.");
    if (argc == 3) {
        sigma = atof(argv[2]);
        check(sigma > 0, "Sigma must be greater than 0.0.");
    }
    coeffs = gaussian_coeffs(radius, &sigma);
    check(!!coeffs, "coeff error!!!!!!!!");
    printf("sigma = %f\n", sigma);
    printf("coeffs = {");
    for (n = 0; n < 2*radius + 1; n++) {
        printf("%lf, ", coeffs[n]);
    }
    printf("}\n");
    return 0;
}

variable_types:
argv
char *[]

variable_types: n int

variable_types:
coeffs
double *


repo: imnotlistening/deval

original_code:
int gene_pool_create(struct gene_pool *pool, int solutions, int threads,
        struct devol_params params){
  int i, j;
  int err;
  time_t t_start;
  time_t t_stop;
  struct timeb tmp_time;
  pool->params = params;
  if ( pool->params.breed_fitness > .5 ){
    printf("# Warning: breed fitness > .5. Setting to .5\n");
    pool->params.breed_fitness = .5;
  }
  err = thread_pool_init(&(pool->workers), pool, threads, solutions);
  if ( err )
    return -1;
  pool->solutions = (solution_t *)malloc(sizeof(solution_t) * solutions);
  if ( ! pool->solutions )
    return -1;
  pool->solution_count = solutions;
  ftime(&tmp_time);
  t_start = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("Generating %d initial solutions... ", solutions);
  for ( i = 0; i < solutions; i++){
    pool->solutions[i].mutate = params.mutate;
    pool->solutions[i].fitness = params.fitness;
    pool->solutions[i].init = params.init;
    pool->solutions[i].destroy = params.destroy;
    for ( j = 0; j < threads; j++){
      if ( i >= pool->workers.controllers[j].start &&
    i < pool->workers.controllers[j].stop )
 pool->solutions[i].cont = &(pool->workers.controllers[j]);
    }
    params.init(&(pool->solutions[i]));
  }
  printf("Done\n");
  ftime(&tmp_time);
  t_stop = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("Time to allocate initial solutions: %ld ms\n", t_stop - t_start);
  pool->flags = 1;
  return 0;
}

canonical_original_code:
int gene_pool_create(struct gene_pool *pool, int solutions, int threads, struct devol_params params){
  int i; int j;
  int err;
  long t_start;
  long t_stop;
  struct timeb tmp_time;
  pool->params = params;
  if ( pool->params.breed_fitness > .5 ){
    printf("# Warning: breed fitness > .5. Setting to .5\n");
    pool->params.breed_fitness = .5;
  }
  err = thread_pool_init(&(pool->workers), pool, threads, solutions);
  if ( err )
    return -1;
  pool->solutions = (struct solution *)malloc(sizeof(solution_t) * solutions);
  if ( ! pool->solutions )
    return -1;
  pool->solution_count = solutions;
  ftime(&tmp_time);
  t_start = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("Generating %d initial solutions... ", solutions);
  for ( i = 0; i < solutions; i++){
    pool->solutions[i].mutate = params.mutate;
    pool->solutions[i].fitness = params.fitness;
    pool->solutions[i].init = params.init;
    pool->solutions[i].destroy = params.destroy;
    for ( j = 0; j < threads; j++){
      if ( i >= pool->workers.controllers[j].start &&
    i < pool->workers.controllers[j].stop )
 pool->solutions[i].cont = &(pool->workers.controllers[j]);
    }
    params.init(&(pool->solutions[i]));
  }
  printf("Done\n");
  ftime(&tmp_time);
  t_stop = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("Time to allocate initial solutions: %ld ms\n", t_stop - t_start);
  pool->flags = 1;
  return 0;
}

variable_types: struct gene_pool { struct solution { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double fitness_val; union { unsigned long uint_64; void *ptr; double dp_fp; } private; struct devol_controller { int tid; int start; int stop; int state; int die; unsigned short rstate[3]; struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } rdata; struct thread_pool { unsigned long *threads; int thread_count; struct devol_controller *controllers; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } sync_lock; int term_ready; } *pool; struct gene_pool *gene_pool; char __padding[52]; } *cont; } *solutions; unsigned long solution_count; unsigned int flags; struct thread_pool workers; struct devol_params { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double gene_dispersal_factor; double reproduction_rate; double breed_fitness; unsigned short rstate[3]; } params; struct solution *new_solutions; int new_count; int breeder_window; struct devol_controller controller; } *

variable_types: solutions int

variable_types: threads int

variable_types:
params
struct devol_params

variable_types: i int

variable_types: j int

variable_types: err int

variable_types: struct timeb { long time; unsigned short millitm; short timezone; short dstflag; }

user_defined_types: struct gene_pool { struct solution *solutions; unsigned long solution_count; unsigned int flags; struct thread_pool workers; struct devol_params params; struct solution *new_solutions; int new_count; int breeder_window; struct devol_controller controller; }

user_defined_types: struct solution { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double fitness_val; union { unsigned long uint_64; void *ptr; double dp_fp; } private; struct devol_controller *cont; }

user_defined_types: struct devol_controller { int tid; int start; int stop; int state; int die; unsigned short rstate[3]; struct drand48_data rdata; struct thread_pool *pool; struct gene_pool *gene_pool; char __padding[52]; }

user_defined_types: struct thread_pool { unsigned long *threads; int thread_count; struct devol_controller *controllers; union pthread_mutex_t sync_lock; int term_ready; }


original_code:
int gene_pool_create_seq(struct gene_pool *pool, int solutions,
     struct devol_params params){
  int i;
  time_t t_start;
  time_t t_stop;
  struct timeb tmp_time;
  pool->params = params;
  if ( pool->params.breed_fitness > .5 ){
    printf("# Warning: breed fitness > .5. Setting to .5\n");
    pool->params.breed_fitness = .5;
  }
  memset(&(pool->workers), 0, sizeof(struct thread_pool));
  pool->solutions = (solution_t *)malloc(sizeof(solution_t) * solutions);
  if ( ! pool->solutions )
    return -1;
  pool->solution_count = solutions;
  pool->new_count = (int)(params.reproduction_rate * solutions);
  pool->breeder_window = (int)(params.breed_fitness * solutions);
  pool->new_solutions =
    (solution_t *)malloc(sizeof(solution_t) * pool->new_count);
  if ( ! pool->new_solutions ){
    free(pool->solutions);
    return -1;
  }
  pool->controller.tid = 0;
  pool->controller.start = 0;
  pool->controller.stop = pool->solution_count;
  pool->controller.pool = ((void *)0);
  pool->controller.gene_pool = pool;
  pool->controller.rstate[0] = params.rstate[0];
  pool->controller.rstate[1] = params.rstate[1];
  pool->controller.rstate[2] = params.rstate[2];
  memset(&(pool->controller.rdata), 0, sizeof(struct drand48_data));
  ftime(&tmp_time);
  t_start = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("# Generating %d initial solutions... ", solutions);
  for ( i = 0; i < solutions; i++){
    pool->solutions[i].mutate = params.mutate;
    pool->solutions[i].fitness = params.fitness;
    pool->solutions[i].init = params.init;
    pool->solutions[i].destroy = params.destroy;
    pool->solutions[i].cont = &pool->controller;
    params.init(&(pool->solutions[i]));
  }
  printf("Done\n");
  ftime(&tmp_time);
  t_stop = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("# Time to allocate initial solutions: %ld ms\n", t_stop - t_start);
  pool->flags = 0;
  return 0;
}

canonical_original_code:
int gene_pool_create_seq(struct gene_pool *pool, int solutions, struct devol_params params){
  int i;
  long t_start;
  long t_stop;
  struct timeb tmp_time;
  pool->params = params;
  if ( pool->params.breed_fitness > .5 ){
    printf("# Warning: breed fitness > .5. Setting to .5\n");
    pool->params.breed_fitness = .5;
  }
  memset(&(pool->workers), 0, sizeof(struct thread_pool));
  pool->solutions = (struct solution *)malloc(sizeof(solution_t) * solutions);
  if ( ! pool->solutions )
    return -1;
  pool->solution_count = solutions;
  pool->new_count = (int)(params.reproduction_rate * solutions);
  pool->breeder_window = (int)(params.breed_fitness * solutions);
  pool->new_solutions =
    (struct solution *)malloc(sizeof(solution_t) * pool->new_count);
  if ( ! pool->new_solutions ){
    free(pool->solutions);
    return -1;
  }
  pool->controller.tid = 0;
  pool->controller.start = 0;
  pool->controller.stop = pool->solution_count;
  pool->controller.pool = ((void *)0);
  pool->controller.gene_pool = pool;
  pool->controller.rstate[0] = params.rstate[0];
  pool->controller.rstate[1] = params.rstate[1];
  pool->controller.rstate[2] = params.rstate[2];
  memset(&(pool->controller.rdata), 0, sizeof(struct drand48_data));
  ftime(&tmp_time);
  t_start = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("# Generating %d initial solutions... ", solutions);
  for ( i = 0; i < solutions; i++){
    pool->solutions[i].mutate = params.mutate;
    pool->solutions[i].fitness = params.fitness;
    pool->solutions[i].init = params.init;
    pool->solutions[i].destroy = params.destroy;
    pool->solutions[i].cont = &pool->controller;
    params.init(&(pool->solutions[i]));
  }
  printf("Done\n");
  ftime(&tmp_time);
  t_stop = (tmp_time.time * 1000) + tmp_time.millitm;
  printf("# Time to allocate initial solutions: %ld ms\n", t_stop - t_start);
  pool->flags = 0;
  return 0;
}

user_defined_types: struct timeb { long time; unsigned short millitm; short timezone; short dstflag; }

user_defined_types: struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; }

user_defined_types: struct devol_params { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double gene_dispersal_factor; double reproduction_rate; double breed_fitness; unsigned short rstate[3]; }


original_code:
int gene_pool_iterate_seq(struct gene_pool *pool){
  int i;
  int s1_ind, s2_ind, die_ind;
  double tmp;
  solution_t *s1, *s2, *die;
  _gene_pool_calculate_fitnesses_p(pool, 0, pool->solution_count);
  qsort(pool->solutions, pool->solution_count,
 sizeof(solution_t), _compare_solutions);
  printf("%d new solutions...\n", pool->new_count);
  for ( i = 0; i < pool->new_count; i++){
    devol_rand48(pool->controller.rstate, &(pool->controller.rdata), &tmp);
    s1_ind = (int)(tmp * pool->breeder_window);
    do {
      devol_rand48(pool->controller.rstate, &(pool->controller.rdata), &tmp);
      s2_ind = (int)(tmp * pool->breeder_window);
    } while (s1_ind == s2_ind);
    s1 = (solution_t *)&(pool->solutions[s1_ind]);
    s2 = (solution_t *)&(pool->solutions[s2_ind]);
    pool->new_solutions[i].mutate = s1->mutate;
    pool->new_solutions[i].fitness = s1->fitness;
    pool->new_solutions[i].init = s1->init;
    pool->new_solutions[i].destroy = s1->destroy;
    pool->new_solutions[i].cont = &pool->controller;
    s1->mutate(s1, s2, &(pool->new_solutions[i]));
    die_ind = pool->solution_count - (i % pool->breeder_window) - 1;
    die = (solution_t *)&(pool->solutions[die_ind]);
    die->destroy(die);
    *die = pool->new_solutions[i];
  }
  _gene_pool_calculate_fitnesses_p(pool, 0, pool->solution_count);
  return 0;
}

canonical_original_code:
int gene_pool_iterate_seq(struct gene_pool *pool){
  int i;
  int s1_ind; int s2_ind; int die_ind;
  double tmp;
  struct solution *s1; struct solution *s2; struct solution *die;
  _gene_pool_calculate_fitnesses_p(pool, 0, pool->solution_count);
  qsort(pool->solutions, pool->solution_count,
 sizeof(solution_t), _compare_solutions);
  printf("%d new solutions...\n", pool->new_count);
  for ( i = 0; i < pool->new_count; i++){
    devol_rand48(pool->controller.rstate, &(pool->controller.rdata), &tmp);
    s1_ind = (int)(tmp * pool->breeder_window);
    do {
      devol_rand48(pool->controller.rstate, &(pool->controller.rdata), &tmp);
      s2_ind = (int)(tmp * pool->breeder_window);
    } while (s1_ind == s2_ind);
    s1 = (struct solution *)&(pool->solutions[s1_ind]);
    s2 = (struct solution *)&(pool->solutions[s2_ind]);
    pool->new_solutions[i].mutate = s1->mutate;
    pool->new_solutions[i].fitness = s1->fitness;
    pool->new_solutions[i].init = s1->init;
    pool->new_solutions[i].destroy = s1->destroy;
    pool->new_solutions[i].cont = &pool->controller;
    s1->mutate(s1, s2, &(pool->new_solutions[i]));
    die_ind = pool->solution_count - (i % pool->breeder_window) - 1;
    die = (struct solution *)&(pool->solutions[die_ind]);
    die->destroy(die);
    *die = pool->new_solutions[i];
  }
  _gene_pool_calculate_fitnesses_p(pool, 0, pool->solution_count);
  return 0;
}

variable_types: tmp double

variable_types: struct solution { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double fitness_val; union { unsigned long uint_64; void *ptr; double dp_fp; } private; struct devol_controller { int tid; int start; int stop; int state; int die; unsigned short rstate[3]; struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } rdata; struct thread_pool { unsigned long *threads; int thread_count; struct devol_controller *controllers; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } sync_lock; int term_ready; } *pool; struct gene_pool { struct solution *solutions; unsigned long solution_count; unsigned int flags; struct thread_pool workers; struct devol_params { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double gene_dispersal_factor; double reproduction_rate; double breed_fitness; unsigned short rstate[3]; } params; struct solution *new_solutions; int new_count; int breeder_window; struct devol_controller controller; } *gene_pool; char __padding[52]; } *cont; } *


original_code:
void gene_pool_disperse(struct gene_pool *pool){
  int disperse;
  int s1, s2;
  if ( pool->params.swap == ((void *)0))
    return;
  disperse = (int)(pool->params.gene_dispersal_factor * pool->solution_count);
  while ( disperse-- > 0 ){
    s1 = nrand48(pool->controller.rstate) % pool->solution_count;
    do {
      s2 = nrand48(pool->controller.rstate) % pool->solution_count;
    } while (s1 == s2);
    pool->params.swap(&pool->solutions[s1], &pool->solutions[s2]);
  }
}

canonical_original_code:
void gene_pool_disperse(struct gene_pool *pool){
  int disperse;
  int s1; int s2;
  if ( pool->params.swap == ((void *)0))
    return;
  disperse = (int)(pool->params.gene_dispersal_factor * pool->solution_count);
  while ( disperse-- > 0 ){
    s1 = nrand48(pool->controller.rstate) % pool->solution_count;
    do {
      s2 = nrand48(pool->controller.rstate) % pool->solution_count;
    } while (s1 == s2);
    pool->params.swap(&pool->solutions[s1], &pool->solutions[s2]);
  }
}

variable_types: disperse int


original_code:
int thread_pool_init(struct thread_pool *pool,
       struct gene_pool *gene_pool, int threads, int solutions){
  int i;
  int err;
  int block_size;
  int start, stop;
  pool->thread_count = threads;
  pool->term_ready = 0;
  pthread_mutex_init(&(pool->sync_lock), ((void *)0));
  pthread_mutex_lock(&(pool->sync_lock));
  pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * threads);
  if ( ! pool->threads )
    return -1;
  pool->controllers = (struct devol_controller *)
    malloc(sizeof(struct devol_controller) * threads);
  if ( ! pool->controllers ){
    free(pool->threads);
    return -1;
  }
  block_size = solutions / threads;
  start = 0;
  stop = block_size;
  for ( i = 0; i < threads; i++){
    pool->controllers[i].start = start;
    pool->controllers[i].stop = stop;
    start = stop;
    stop += block_size;
  }
  pool->controllers[threads-1].stop = solutions;
  for ( i = 0; i < threads; i++){
    pool->controllers[i].tid = i;
    pool->controllers[i].die = 0;
    pool->controllers[i].state = 1;
    pool->controllers[i].pool = pool;
    pool->controllers[i].gene_pool = gene_pool;
    pool->controllers[i].rstate[0] = gene_pool->params.rstate[0] + i;
    pool->controllers[i].rstate[1] = gene_pool->params.rstate[1] + i+1;
    pool->controllers[i].rstate[2] = gene_pool->params.rstate[2] + i+2;
    memset(&(pool->controllers[i].rdata), 0, sizeof(rdata_t));
    err = pthread_create( &(pool->threads[i]), ((void *)0), _devol_thread_main,
     &(pool->controllers[i]));
  }
  return 0;
}

canonical_original_code:
int thread_pool_init(struct thread_pool *pool, struct gene_pool *gene_pool, int threads, int solutions){
  int i;
  int err;
  int block_size;
  int start; int stop;
  pool->thread_count = threads;
  pool->term_ready = 0;
  pthread_mutex_init(&(pool->sync_lock), ((void *)0));
  pthread_mutex_lock(&(pool->sync_lock));
  pool->threads = (unsigned long *)malloc(sizeof(pthread_t) * threads);
  if ( ! pool->threads )
    return -1;
  pool->controllers = (struct devol_controller *)
    malloc(sizeof(struct devol_controller) * threads);
  if ( ! pool->controllers ){
    free(pool->threads);
    return -1;
  }
  block_size = solutions / threads;
  start = 0;
  stop = block_size;
  for ( i = 0; i < threads; i++){
    pool->controllers[i].start = start;
    pool->controllers[i].stop = stop;
    start = stop;
    stop += block_size;
  }
  pool->controllers[threads-1].stop = solutions;
  for ( i = 0; i < threads; i++){
    pool->controllers[i].tid = i;
    pool->controllers[i].die = 0;
    pool->controllers[i].state = 1;
    pool->controllers[i].pool = pool;
    pool->controllers[i].gene_pool = gene_pool;
    pool->controllers[i].rstate[0] = gene_pool->params.rstate[0] + i;
    pool->controllers[i].rstate[1] = gene_pool->params.rstate[1] + i+1;
    pool->controllers[i].rstate[2] = gene_pool->params.rstate[2] + i+2;
    memset(&(pool->controllers[i].rdata), 0, sizeof(rdata_t));
    err = pthread_create( &(pool->threads[i]), ((void *)0), _devol_thread_main,
     &(pool->controllers[i]));
  }
  return 0;
}

variable_types: struct thread_pool { unsigned long *threads; int thread_count; struct devol_controller { int tid; int start; int stop; int state; int die; unsigned short rstate[3]; struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } rdata; struct thread_pool *pool; struct gene_pool { struct solution { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double fitness_val; union { unsigned long uint_64; void *ptr; double dp_fp; } private; struct devol_controller *cont; } *solutions; unsigned long solution_count; unsigned int flags; struct thread_pool workers; struct devol_params { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double gene_dispersal_factor; double reproduction_rate; double breed_fitness; unsigned short rstate[3]; } params; struct solution *new_solutions; int new_count; int breeder_window; struct devol_controller controller; } *gene_pool; char __padding[52]; } *controllers; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } sync_lock; int term_ready; } *

variable_types: start int

variable_types: stop int


original_code:
int thread_pool_destroy(struct thread_pool *pool){
  int i;
  for ( i = 0; i < pool->thread_count; i++){
    pool->controllers[i].die = 1;
  }
  pthread_mutex_unlock(&(pool->sync_lock));
  for ( i = 0; i < pool->thread_count; i++){
    pthread_join(pool->threads[i], ((void *)0));
  }
  free(pool->threads);
  free(pool->controllers);
  return 0;
}


decompiled_code:
__int64 __fastcall gene_pool_iterate(__int64 a1)
{
int v1; // edx
__int64 v2; // rcx
_DWORD *v3; // rax
unsigned __int64 v4; // rdx
int v5; // edx
__int64 v6; // rcx
_DWORD *v7; // rax
unsigned __int64 v8; // rdx

*(_DWORD *)(a1 + 88) = 0;
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
while ( 1 )
{
usleep(5u);
v1 = *(_DWORD *)(a1 + 32);
if ( v1 <= 0 )
break;
v2 = *(_QWORD *)(a1 + 40);
v3 = (_DWORD *)(v2 + 12);
v4 = v2 + ((unsigned __int64)(unsigned int)(v1 - 1) << 7) + 140;
while ( !*v3 )
{
v3 += 32;
if ( v3 == (_DWORD *)v4 )
goto LABEL_6;
}
}
LABEL_6:
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
*(_DWORD *)(a1 + 88) = 1;
while ( 1 )
{
usleep(5u);
v5 = *(_DWORD *)(a1 + 32);
if ( v5 <= 0 )
break;
v6 = *(_QWORD *)(a1 + 40);
v7 = (_DWORD *)(v6 + 12);
v8 = v6 + ((unsigned __int64)(unsigned int)(v5 - 1) << 7) + 140;
while ( *v7 == 1 )
{
v7 += 32;
if ( v7 == (_DWORD *)v8 )
goto LABEL_11;
}
}
LABEL_11:
gene_pool_disperse(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
int v1; // edx
__int64 v2; // rcx
_DWORD *v3; // rax
unsigned __int64 v4; // rdx
int v5; // edx
__int64 v6; // rcx
_DWORD *v7; // rax
unsigned __int64 v8; // rdx

*(_DWORD *)(a1 + 88) = 0;
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 48));
while ( 1 )
{
usleep(5u);
v1 = *(_DWORD *)(a1 + 32);
if ( v1 <= 0 )
break;
v2 = *(_QWORD *)(a1 + 40);
v3 = (_DWORD *)(v2 + 12);
v4 = v2 + ((unsigned __int64)(unsigned int)(v1 - 1) << 7) + 140;
while ( !*v3 )
{
v3 += 32;
if ( v3 == (_DWORD *)v4 )
goto LABEL_6;
}
}
LABEL_6:
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
*(_DWORD *)(a1 + 88) = 1;
while ( 1 )
{
usleep(5u);
v5 = *(_DWORD *)(a1 + 32);
if ( v5 <= 0 )
break;
v6 = *(_QWORD *)(a1 + 40);
v7 = (_DWORD *)(v6 + 12);
v8 = v6 + ((unsigned __int64)(unsigned int)(v5 - 1) << 7) + 140;
while ( *v7 == 1 )
{
v7 += 32;
if ( v7 == (_DWORD *)v8 )
goto LABEL_11;
}
}
LABEL_11:
gene_pool_disperse(a1);
return 0LL;
}

original_code:
int gene_pool_iterate(struct gene_pool *gene_pool){
  int i;
  int done;
  int waiting;
  gene_pool->workers.term_ready = 0;
  pthread_mutex_unlock(&(gene_pool->workers.sync_lock));
  waiting = 1;
  while ( waiting ){
    done = 0;
    usleep(5);
    for ( i = 0; i < gene_pool->workers.thread_count; i++){
      if ( gene_pool->workers.controllers[i].state != 0 ){
 done = 1;
 break;
      }
    }
    waiting = done;
  }
  pthread_mutex_lock(&(gene_pool->workers.sync_lock));
  gene_pool->workers.term_ready = 1;
  waiting = 1;
  while ( waiting ){
    done = 0;
    usleep(5);
    for ( i = 0; i < gene_pool->workers.thread_count; i++){
      if ( gene_pool->workers.controllers[i].state != 1 ){
 done = 1;
 break;
      }
    }
    waiting = done;
  }
  gene_pool_disperse(gene_pool);
  return 0;
}

variable_types: done int

variable_types: waiting int


original_code:
double gene_pool_avg_fitness(struct gene_pool *pool){
  int i;
  double total = 0.0;
  if ( ! pool )
    return 0;
  for ( i = 0; i < pool->solution_count; i++){
    pool->solutions[i].fitness_val =
      pool->solutions[i].fitness(&(pool->solutions[i]));
    total += pool->solutions[i].fitness_val;
  }
  return total / pool->solution_count;
}

variable_types: total double


decompiled_code:
int __fastcall gene_pool_display_fitnesses(_QWORD *a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rbp
int result; // eax

if ( a1 && a1[1] )
{
v1 = 0LL;
do
{
v2 = *a1 + (v1 << 6);
*(double *)(v2 + 40) = (*(double (__fastcall **)(__int64))(v2 + 8))(v2);
result = printf("Solution %5d: fitness=%lf\n", (unsigned int)v1, *(double *)(*a1 + (v1 << 6) + 40));
++v1;
}
while ( a1[1] > v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(_QWORD *a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rbp
int result; // eax

if ( a1 && a1[1] )
{
v1 = 0LL;
do
{
v2 = *a1 + (v1 << 6);
*(double *)(v2 + 40) = (*(double (__fastcall **)(__int64))(v2 + 8))(v2);
result = printf("Solution %5d: fitness=%lf\n", (unsigned int)v1, *(double *)(*a1 + (v1 << 6) + 40));
++v1;
}
while ( a1[1] > v1 );
}
return result;
}

original_code:
void gene_pool_display_fitnesses(struct gene_pool *pool){
  int i;
  if ( ! pool )
    return;
  for ( i = 0; i < pool->solution_count; i++){
    pool->solutions[i].fitness_val =
      pool->solutions[i].fitness(&(pool->solutions[i]));
    printf("Solution %5d: fitness=%lf\n", i, pool->solutions[i].fitness_val);
  }
}

variable_types: struct gene_pool { struct solution { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double fitness_val; union { unsigned long uint_64; void *ptr; double dp_fp; } private; struct devol_controller { int tid; int start; int stop; int state; int die; unsigned short rstate[3]; struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } rdata; struct thread_pool { unsigned long *threads; int thread_count; struct devol_controller *controllers; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } sync_lock; int term_ready; } *pool; struct gene_pool *gene_pool; char __padding[52]; } *cont; } *solutions; unsigned long solution_count; unsigned int flags; struct thread_pool workers; struct devol_params { int (*mutate)(struct solution *par1, struct solution *par2, struct solution *dest); double (*fitness)(struct solution *solution); int (*init)(struct solution *solution); int (*destroy)(struct solution *solution); void (*swap)(struct solution *left, struct solution *right); double gene_dispersal_factor; double reproduction_rate; double breed_fitness; unsigned short rstate[3]; } params; struct solution *new_solutions; int new_count; int breeder_window; struct devol_controller controller; } *


decompiled_code:
int __fastcall devol_rand48(unsigned __int16 *a1, struct drand48_data *a2, double *a3)
{
return erand48_r(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 *a1, struct drand48_data *a2, double *a3)
{
return erand48_r(a1, a2, a3);
}

original_code:
void devol_rand48(unsigned short rstate[3], rdata_t *rdata, double *d){
  erand48_r(rstate, rdata, d);
}

canonical_original_code:
void devol_rand48(unsigned short rstate[3], struct drand48_data *rdata, double *d){
  erand48_r(rstate, rdata, d);
}

variable_types:
rstate
unsigned short [3]

variable_types: struct drand48_data { unsigned short __x[3]; unsigned short __old_x[3]; unsigned short __c; unsigned short __init; unsigned long long __a; } *

variable_types:
d
double *


decompiled_code:
int __fastcall devol_nrand48(unsigned __int16 *a1, struct drand48_data *a2, __int64 *a3)
{
return nrand48_r(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func5(unsigned __int16 *a1, struct drand48_data *a2, __int64 *a3)
{
return nrand48_r(a1, a2, a3);
}

original_code:
void devol_nrand48(unsigned short rstate[3], rdata_t *rdata, long int *d){
  nrand48_r(rstate, rdata, d);
}

canonical_original_code:
void devol_nrand48(unsigned short rstate[3], struct drand48_data *rdata, long *d){
  nrand48_r(rstate, rdata, d);
}

variable_types:
d
long *


decompiled_code:
int __fastcall devol_jrand48(unsigned __int16 *a1, struct drand48_data *a2, __int64 *a3)
{
return jrand48_r(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func6(unsigned __int16 *a1, struct drand48_data *a2, __int64 *a3)
{
return jrand48_r(a1, a2, a3);
}

original_code:
void devol_jrand48(unsigned short rstate[3], rdata_t *rdata, long int *d){
  jrand48_r(rstate, rdata, d);
}

canonical_original_code:
void devol_jrand48(unsigned short rstate[3], struct drand48_data *rdata, long *d){
  jrand48_r(rstate, rdata, d);
}


repo: imnotlistening/rsh

decompiled_code:
__int64 rsh_init_fs()
{
void *v0; // rax
void *v1; // rdx
__int64 result; // rax

v0 = malloc(0x280uLL);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = v0;
ptr = v0;
*((_DWORD *)&ptr + 2) = 8;
*((_DWORD *)&ptr + 3) = 0;
result = 0LL;
memset(v1, 0, 0x280uLL);
*(&ptr + 2) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
void *v0; // rax
void *v1; // rdx
__int64 result; // rax

v0 = malloc(0x280uLL);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = v0;
ptr = v0;
*((_DWORD *)&ptr + 2) = 8;
*((_DWORD *)&ptr + 3) = 0;
result = 0LL;
memset(v1, 0, 0x280uLL);
*(&ptr + 2) = 0LL;
return result;
}

original_code:
int rsh_init_fs(){
  struct rsh_file *files = (struct rsh_file *)
    malloc(sizeof(struct rsh_file) * 8 );
  if ( ! files )
    return -1;
  fs.ftable = files;
  fs.ft_length = 8;
  fs.used = 0;
  memset(fs.ftable, 0, sizeof(struct rsh_file) * 8 );
  fs.fops = ((void *)0);
  return 0;
}

variable_types: struct rsh_file { int used; int references; long offset; char *path; struct rsh_io_ops { long (*read)(struct rsh_file *file, void *buf, unsigned long count); long (*write)(struct rsh_file *file, void *buf, unsigned long count); int (*open)(struct rsh_file *file, char *pathname, int flags); int (*close)(struct rsh_file *file); int (*readdir)(struct rsh_file *file, void *buf, unsigned long space); int (*mkdir)(char *path); int (*unlink)(char *path); } *fops; unsigned int mode; long size; long block_size; long blocks; long access_time; void *local; } *

user_defined_types: struct rsh_file { int used; int references; long offset; char *path; struct rsh_io_ops *fops; unsigned int mode; long size; long block_size; long blocks; long access_time; void *local; }