decompiled_code:
__int16 __fastcall frame_crc_gen(__int64 a1, __int64 a2, unsigned int a3)
{
__int16 v3; // ax
__int16 result; // ax

v3 = get_crc_code(a2, a3);
result = _bswap_16(v3);
*(_WORD *)(a1 + 4) = result;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3)
{
__int16 v3; // ax
__int16 result; // ax

v3 = get_crc_code(a2, a3);
result = func0(v3);
*(_WORD *)(a1 + 4) = result;
return result;
}

original_code:
int frame_crc_gen(frame_header *f_header, unsigned char *begin, unsigned int len)
{
    f_header->crc = htons(get_crc_code(begin, len));
}

canonical_original_code:
int frame_crc_gen(struct frame_header_tag *f_header, unsigned char *begin, unsigned int len)
{
    f_header->crc = htons(get_crc_code(begin, len));
}

variable_types:
begin
unsigned char *

variable_types:
len
unsigned int

user_defined_types: struct frame_header_tag { unsigned short length; unsigned char type; unsigned char sub_type; unsigned short crc; }


repo: lazygunner/transfer

original_code:
int frame_build(frame_header *header, unsigned char *data, unsigned int data_len, unsigned char *buf_send)
{
    int buf_offset = 0, frame_len = 0;
    frame_len = sizeof(frame_header) + data_len + 1;
    header->length = htons(frame_len - 2);
    header->crc = htons(get_crc_code(data, data_len));
    buf_offset = 0;
    memcpy(buf_send, header, sizeof(frame_header));
    buf_offset += sizeof(frame_header);
    memcpy(buf_send + buf_offset, data, data_len);
    buf_offset += data_len;
    memset(buf_send + buf_offset, 0xFF, 1);
    return frame_len;
}

canonical_original_code:
int frame_build(struct frame_header_tag *header, unsigned char *data, unsigned int data_len, unsigned char *buf_send)
{
    int buf_offset = 0; int frame_len = 0;
    frame_len = sizeof(frame_header) + data_len + 1;
    header->length = htons(frame_len - 2);
    header->crc = htons(get_crc_code(data, data_len));
    buf_offset = 0;
    memcpy(buf_send, header, sizeof(frame_header));
    buf_offset += sizeof(frame_header);
    memcpy(buf_send + buf_offset, data, data_len);
    buf_offset += data_len;
    memset(buf_send + buf_offset, 0xFF, 1);
    return frame_len;
}

variable_types: struct frame_header_tag { unsigned short length; unsigned char type; unsigned char sub_type; unsigned short crc; } *

variable_types:
data
unsigned char *

return_type: int


decompiled_code:
__int64 __fastcall recv_frame(int fd, __int16 *a2)
{
unsigned __int16 v2; // bp
__int16 v3; // ax
__int16 v4; // dx

if ( !a2 )
return 0xFFFFFFFFLL;
*(_QWORD *)a2 = 0LL;
*((_QWORD *)a2 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(a2 + 4) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 1024) >> 3));
if ( !fd )
return 0xFFFFFFFFLL;
recv(fd, a2, 2uLL, 0);
v2 = _bswap_16(*a2);
if ( (unsigned __int16)(v2 - 1) > 0x63u )
{
t_log("receive package length error!");
return 0xFFFFFFFFLL;
}
if ( v2 != (unsigned __int16)recv(fd, a2 + 1, v2, 0) || *((_BYTE *)a2 + v2 + 1) != 0xFF )
return 0xFFFFFFFFLL;
get_crc_code(a2 + 3, (unsigned __int16)(v2 - 5));
v3 = _bswap_16(a2[2]);
if ( v3 == v4 )
return (unsigned __int16)(v2 + 2);
t_log("crc error");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, __int16 *a2)
{
unsigned __int16 v2; // bp
__int16 v3; // ax
__int16 v4; // dx

if ( !a2 )
return 0xFFFFFFFFLL;
*(_QWORD *)a2 = 0LL;
*((_QWORD *)a2 + 127) = 0LL;
memset(
(void *)((unsigned __int64)(a2 + 4) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8) + 1024) >> 3));
if ( !fd )
return 0xFFFFFFFFLL;
recv(fd, a2, 2uLL, 0);
v2 = func0(*a2);
if ( (unsigned __int16)(v2 - 1) > 0x63u )
{
t_log("receive package length error!");
return 0xFFFFFFFFLL;
}
if ( v2 != (unsigned __int16)recv(fd, a2 + 1, v2, 0) || *((_BYTE *)a2 + v2 + 1) != 0xFF )
return 0xFFFFFFFFLL;
get_crc_code(a2 + 3, (unsigned __int16)(v2 - 5));
v3 = func0(a2[2]);
if ( v3 == v4 )
return (unsigned __int16)(v2 + 2);
t_log("crc error");
return 0xFFFFFFFFLL;
}

original_code:
int recv_frame(int sock, unsigned char *buf)
{
    unsigned short len = 0, recv_len = 0, frame_len = 0, data_len = 0;
    unsigned short recv_crc = 0;
    if (!buf)
        return -1;
    memset(buf, 0, 1024);
    if (!sock)
        return -1;
    len = recv(sock, buf, 2, 0);
    recv_len = ntohs(*((unsigned short *)buf));
    if (recv_len > 100 || recv_len <= 0)
    {
        t_log("receive package length error!");
        return -1;
    }
    frame_len = recv(sock, buf + 2, recv_len, 0);
    if (frame_len != recv_len)
        return -1;
    if (buf[recv_len + 1] != 0xFF)
        return -1;
    data_len = recv_len + 1 - sizeof(frame_header);
    recv_crc = get_crc_code(buf + sizeof(frame_header), data_len);
    if(recv_crc != ntohs(((frame_header *)buf)->crc))
    {
        t_log("crc error");
        return -1;
    }
    return frame_len + 2;
}

canonical_original_code:
int recv_frame(int sock, unsigned char *buf)
{
    unsigned short len = 0; unsigned short recv_len = 0; unsigned short frame_len = 0; unsigned short data_len = 0;
    unsigned short recv_crc = 0;
    if (!buf)
        return -1;
    memset(buf, 0, 1024);
    if (!sock)
        return -1;
    len = recv(sock, buf, 2, 0);
    recv_len = ntohs(*((unsigned short *)buf));
    if (recv_len > 100 || recv_len <= 0)
    {
        t_log("receive package length error!");
        return -1;
    }
    frame_len = recv(sock, buf + 2, recv_len, 0);
    if (frame_len != recv_len)
        return -1;
    if (buf[recv_len + 1] != 0xFF)
        return -1;
    data_len = recv_len + 1 - sizeof(frame_header);
    recv_crc = get_crc_code(buf + sizeof(frame_header), data_len);
    if(recv_crc != ntohs(((struct frame_header_tag *)buf)->crc))
    {
        t_log("crc error");
        return -1;
    }
    return frame_len + 2;
}

variable_types: sock int

variable_types:
buf
unsigned char *

variable_types:
len
unsigned short


decompiled_code:
__int64 __fastcall ini_browse_callback(__int64 a1, const char *a2, const char *a3)
{
__int64 v5; // rbx
int *v6; // rdi
unsigned int v7; // esi
int v8; // eax

v5 = (unsigned int)((_DWORD)cfg_entities + 8);
do
{
while ( strcmp(a2, (const char *)v5) )
{
v5 += 32LL;
if ( (char *)v5 == &cfg_entities[200] )
return 1LL;
}
v6 = *(int **)(v5 - 8);
v7 = *(_DWORD *)(v5 + 20);
v5 += 32LL;
v8 = set_globle_values_isra_0(v6, v7, a3);
handle_config_status_constprop_0(v8);
}
while ( (char *)v5 != &cfg_entities[200] );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2, const char *a3)
{
__int64 v5; // rbx
int *v6; // rdi
unsigned int v7; // esi
int v8; // eax

v5 = (unsigned int)((_DWORD)cfg_entities + 8);
do
{
while ( strcmp(a2, (const char *)v5) )
{
v5 += 32LL;
if ( (char *)v5 == &cfg_entities[200] )
return 1LL;
}
v6 = *(int **)(v5 - 8);
v7 = *(_DWORD *)(v5 + 20);
v5 += 32LL;
v8 = func1(v6, v7, a3);
func0(v8);
}
while ( (char *)v5 != &cfg_entities[200] );
return 1LL;
}

original_code:
static int ini_browse_callback(const char *section, const char *key, const char *value, const void *userdata)
{
    int i;
    int entity_count = 0;
    entity_count = sizeof(cfg_entities) / sizeof(cfg_entity);
    for(i = 0; i < entity_count; i++)
    {
        if(!strcmp(key, cfg_entities[i].name))
            handle_config_status(cfg_entities[i].name, set_globle_values(cfg_entities[i], value));
    }
    return 1;
}

canonical_original_code:
static int ini_browse_callback(char *section, char *key, char *value, void *userdata)
{
    int i;
    int entity_count = 0;
    entity_count = sizeof(cfg_entities) / sizeof(cfg_entity);
    for(i = 0; i < entity_count; i++)
    {
        if(!strcmp(key, cfg_entities[i].name))
            handle_config_status(cfg_entities[i].name, set_globle_values(cfg_entities[i], value));
    }
    return 1;
}

variable_types:
section
char *

variable_types:
key
char *

variable_types:
value
char *

variable_types:
userdata
void *

variable_types: i int


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

g_control_port = 6260;
strcpy((char *)&g_server_ip, "192.168.2.158");
result = 0x3433323164636261LL;
g_data_port = 6160;
strcpy((char *)g_file_path, "files/");
strcpy((char *)&g_train_no, "abcd1234");
return result;
}

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

g_control_port = 6260;
strcpy((char *)&g_server_ip, "192.168.2.158");
result = 0x3433323164636261LL;
g_data_port = 6160;
strcpy((char *)g_file_path, "files/");
strcpy((char *)&g_train_no, "abcd1234");
return result;
}

original_code:
void set_default_config()
{
    g_control_port = 6260;
    g_data_port = 6160;
    strcpy(g_file_path, "files/");
    strcpy(g_server_ip, "192.168.2.158");
    strcpy(g_train_no, "abcd1234");
}

return_type: void


decompiled_code:
int cfg_test()
{
int result; // eax
int v1; // ebp
__int64 v2; // rbx
const char *v3; // rdx
__int64 v4; // rsi

result = configs_count;
if ( configs_count > 0 )
{
v1 = 0;
v2 = (unsigned int)((_DWORD)cfg_entities + 8);
do
{
while ( 1 )
{
result = *(_DWORD *)(v2 + 20);
if ( result != 2 )
break;
result = printf("%s %s\r\n", (const char *)v2, *(const char **)(v2 - 8));
LABEL_5:
++v1;
v2 += 32LL;
if ( configs_count <= v1 )
return result;
}
if ( result != 3 )
{
if ( !result )
result = printf("%s %d\r\n", (const char *)v2, **(unsigned int **)(v2 - 8));
goto LABEL_5;
}
v3 = "disable";
v4 = v2;
if ( **(_BYTE **)(v2 - 8) == 1 )
v3 = "enable";
++v1;
v2 += 32LL;
result = printf("%s %s\r\n", v4, v3);
}
while ( configs_count > v1 );
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
int v1; // ebp
__int64 v2; // rbx
const char *v3; // rdx
__int64 v4; // rsi

result = configs_count;
if ( configs_count > 0 )
{
v1 = 0;
v2 = (unsigned int)((_DWORD)cfg_entities + 8);
do
{
while ( 1 )
{
result = *(_DWORD *)(v2 + 20);
if ( result != 2 )
break;
result = printf("%s %s\r\n", (const char *)v2, *(const char **)(v2 - 8));
LABEL_5:
++v1;
v2 += 32LL;
if ( configs_count <= v1 )
return result;
}
if ( result != 3 )
{
if ( !result )
result = printf("%s %d\r\n", (const char *)v2, **(unsigned int **)(v2 - 8));
goto LABEL_5;
}
v3 = "disable";
v4 = v2;
if ( **(_BYTE **)(v2 - 8) == 1 )
v3 = "enable";
++v1;
v2 += 32LL;
result = printf("%s %s\r\n", v4, v3);
}
while ( configs_count > v1 );
}
return result;
}

original_code:
void cfg_test()
{
    int i = 0;
    for (i =0; i < configs_count; i++)
    {
        switch(cfg_entities[i].type)
        {
            case CONFIG_TYPE_BOOL:
                printf("%s %s\r\n", cfg_entities[i].name, *((char *)cfg_entities[i].value) == 1 ? "enable" : "disable");
                break;
            case CONFIG_TYPE_INT:
                printf("%s %d\r\n", cfg_entities[i].name, *((int *)cfg_entities[i].value));
                break;
            case CONFIG_TYPE_STR:
                printf("%s %s\r\n", cfg_entities[i].name, (char *)cfg_entities[i].value);
                break;
            default:
                break;
        }
    }
    return;
}


decompiled_code:
char *cmd_init_config()
{
char *result; // rax
unsigned int v1; // edx
_DWORD *v2; // rdx
_WORD *v3; // rdx

configs_count = 6;
result = cfg_entities;
do
{
while ( 1 )
{
v1 = *((_DWORD *)result + 7);
if ( v1 == 2 )
{
v3 = *(_WORD **)result;
*((_BYTE *)v3 + 32) = 0;
*(_OWORD *)v3 = 0LL;
*((_OWORD *)v3 + 1) = 0LL;
goto LABEL_3;
}
if ( v1 > 2 )
{
if ( v1 == 3 )
**(_BYTE **)result = 0;
goto LABEL_3;
}
if ( !v1 )
break;
**(_WORD **)result = 0;
LABEL_3:
result += 32;
if ( result == &cfg_entities[192] )
return result;
}
v2 = *(_DWORD **)result;
result += 32;
*v2 = 0;
}
while ( result != &cfg_entities[192] );
return result;
}

canonical_decompiled_code:
char *func5()
{
char *result; // rax
unsigned int v1; // edx
_DWORD *v2; // rdx
_WORD *v3; // rdx

configs_count = 6;
result = cfg_entities;
do
{
while ( 1 )
{
v1 = *((_DWORD *)result + 7);
if ( v1 == 2 )
{
v3 = *(_WORD **)result;
*((_BYTE *)v3 + 32) = 0;
*(_OWORD *)v3 = 0LL;
*((_OWORD *)v3 + 1) = 0LL;
goto LABEL_3;
}
if ( v1 > 2 )
{
if ( v1 == 3 )
**(_BYTE **)result = 0;
goto LABEL_3;
}
if ( !v1 )
break;
**(_WORD **)result = 0;
LABEL_3:
result += 32;
if ( result == &cfg_entities[192] )
return result;
}
v2 = *(_DWORD **)result;
result += 32;
*v2 = 0;
}
while ( result != &cfg_entities[192] );
return result;
}

original_code:
void cmd_init_config()
{
    int i;
    configs_count = sizeof(cfg_entities) / sizeof(cfg_entity);
    for (i =0; i < configs_count; i++)
    {
        switch(cfg_entities[i].type)
        {
            case CONFIG_TYPE_BOOL:
                *((char *)cfg_entities[i].value) = 0;
                break;
            case CONFIG_TYPE_INT:
                *((int *)cfg_entities[i].value) = 0;
                break;
            case CONFIG_TYPE_SHORT:
                *((unsigned short *)cfg_entities[i].value) = 0;
                break;
            case CONFIG_TYPE_STR:
                memset((char *)cfg_entities[i].value, 0, 33);
                break;
            default:
                break;
         }
    }
    return;
}


decompiled_code:
__int64 cmd_parse_config()
{
bool v0; // zf
__int64 result; // rax

callback_status = 0;
v0 = (unsigned int)ini_browse(ini_browse_callback, 0LL, "transfer.ini") == 0;
result = 0xFFFFFFFFLL;
if ( !v0 )
result = (unsigned int)callback_status;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
bool v0; // zf
__int64 result; // rax

callback_status = 0;
v0 = (unsigned int)ini_browse(ini_browse_callback, 0LL, "transfer.ini") == 0;
result = 0xFFFFFFFFLL;
if ( !v0 )
result = (unsigned int)callback_status;
return result;
}

original_code:
enum status_config cmd_parse_config()
{
    callback_status = STATUS_CONFIG_SUCCESS;
    if (!ini_browse(ini_browse_callback, ((void *)0), "transfer.ini"))
        return -1;
    return callback_status;
}

return_type: enum status_config { STATUS_CONFIG_SUCCESS=0, STATUS_CONFIG_INVALID_VALUE=1, STATUS_CONFIG_SET_FAILED=2, STATUS_CONFIG_NOT_FOUND=404, STATUS_CONFIG_DEFAULT=405 }


decompiled_code:
__int64 __fastcall cmd_set_config(__int64 a1, __int64 a2, __int64 a3)
{
return 2 * (unsigned int)((unsigned int)ini_puts(a3, a1, a2, a3) == 0);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
return 2 * (unsigned int)((unsigned int)ini_puts(a3, a1, a2, a3) == 0);
}

original_code:
enum status_config cmd_set_config(char *config_name, char *value, char *file_name)
{
    int retval = 0;
    retval = ini_puts(file_name, config_name, value, file_name);
    return retval ? STATUS_CONFIG_SUCCESS : STATUS_CONFIG_SET_FAILED;
}

variable_types: retval int

user_defined_types: enum status_config { STATUS_CONFIG_SUCCESS=0, STATUS_CONFIG_INVALID_VALUE=1, STATUS_CONFIG_SET_FAILED=2, STATUS_CONFIG_NOT_FOUND=404, STATUS_CONFIG_DEFAULT=405 }


decompiled_code:
__int64 __fastcall connect_server(__int64 a1)
{
const char *v1; // rsi
int v2; // edx
int v3; // eax
unsigned int v4; // er12
const char *v5; // rsi
int v6; // edx
int v7; // eax
int v9; // edi

v1 = *(const char **)(a1 + 72);
*(_WORD *)(a1 + 88) = 2;
inet_pton(2, v1, (void *)(a1 + 92));
*(_WORD *)(a1 + 90) = _bswap_16(*(_WORD *)(a1 + 32));
v3 = socket(2, 1, v2);
if ( v3 < 0 )
goto LABEL_6;
v4 = v3;
if ( connect(v3, (const struct sockaddr *)(a1 + 88), 0x10u) < 0 )
{
v9 = v4;
v4 = -1;
close(v9);
return v4;
}
*(_DWORD *)(a1 + 44) = v4;
puts("connected to server");
v5 = *(const char **)(a1 + 72);
*(_WORD *)(a1 + 104) = 2;
inet_pton(2, v5, (void *)(a1 + 108));
*(_WORD *)(a1 + 106) = _bswap_16(*(_WORD *)(a1 + 36));
v7 = socket(2, 2, v6);
if ( v7 < 0 )
{
LABEL_6:
v4 = -1;
perror("socket");
}
else
{
*(_DWORD *)(a1 + 48) = v7;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
const char *v1; // rsi
int v2; // edx
int v3; // eax
unsigned int v4; // er12
const char *v5; // rsi
int v6; // edx
int v7; // eax
int v9; // edi

v1 = *(const char **)(a1 + 72);
*(_WORD *)(a1 + 88) = 2;
inet_pton(2, v1, (void *)(a1 + 92));
*(_WORD *)(a1 + 90) = func0(*(_WORD *)(a1 + 32));
v3 = socket(2, 1, v2);
if ( v3 < 0 )
goto LABEL_6;
v4 = v3;
if ( connect(v3, (const struct sockaddr *)(a1 + 88), 0x10u) < 0 )
{
v9 = v4;
v4 = -1;
close(v9);
return v4;
}
*(_DWORD *)(a1 + 44) = v4;
puts("connected to server");
v5 = *(const char **)(a1 + 72);
*(_WORD *)(a1 + 104) = 2;
inet_pton(2, v5, (void *)(a1 + 108));
*(_WORD *)(a1 + 106) = func0(*(_WORD *)(a1 + 36));
v7 = socket(2, 2, v6);
if ( v7 < 0 )
{
LABEL_6:
v4 = -1;
perror("socket");
}
else
{
*(_DWORD *)(a1 + 48) = v7;
}
return v4;
}

original_code:
int connect_server(transfer_session *session)
{
    int client_fd, data_fd;
    int len = 0;
    struct sockaddr_in *remote_addr;
    memset(&remote_addr, 0, sizeof(remote_addr));
    remote_addr = &(session->remote_con_addr);
    remote_addr->sin_family = 2;
    inet_pton(2, session->remote_addr, &(remote_addr->sin_addr.s_addr));
    remote_addr->sin_port = htons(session->control_port);
    if((client_fd = socket(2, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        return -1;
    }
    if(connect(client_fd, (struct sockaddr *)remote_addr, sizeof(struct sockaddr)) < 0)
    {
        close(client_fd);
        return -1;
    }
    session->fd = client_fd;
    printf("connected to server\n");
    remote_addr = &(session->remote_data_addr);
    remote_addr->sin_family = 2;
    inet_pton(2, session->remote_addr, &(remote_addr->sin_addr.s_addr));
    remote_addr->sin_port = htons(session->data_port);
    if((data_fd = socket(2, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        return -1;
    }
    session->data_fd = data_fd;
    return client_fd;
}

canonical_original_code:
int connect_server(struct transfer_session_tag *session)
{
    int client_fd; int data_fd;
    int len = 0;
    struct sockaddr_in *remote_addr;
    memset(&remote_addr, 0, sizeof(remote_addr));
    remote_addr = &(session->remote_con_addr);
    remote_addr->sin_family = 2;
    inet_pton(2, session->remote_addr, &(remote_addr->sin_addr.s_addr));
    remote_addr->sin_port = htons(session->control_port);
    if((client_fd = socket(2, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        return -1;
    }
    if(connect(client_fd, (struct sockaddr *)remote_addr, sizeof(struct sockaddr)) < 0)
    {
        close(client_fd);
        return -1;
    }
    session->fd = client_fd;
    printf("connected to server\n");
    remote_addr = &(session->remote_data_addr);
    remote_addr->sin_family = 2;
    inet_pton(2, session->remote_addr, &(remote_addr->sin_addr.s_addr));
    remote_addr->sin_port = htons(session->data_port);
    if((data_fd = socket(2, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        return -1;
    }
    session->data_fd = data_fd;
    return client_fd;
}

variable_types: struct transfer_session_tag { char *hostname; char *username; char *password; char *directory; unsigned int control_port; unsigned int data_port; int state; int fd; int data_fd; unsigned char train_no_len; unsigned char *train_no; unsigned char *hb; void *remote_addr; unsigned long remote_addr_len; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } remote_con_addr; struct sockaddr_in remote_data_addr; int ai_family; int package_qid; int data_qid; void *protocol_data; struct file_desc { unsigned char *file_name; int file_size; unsigned short file_id; struct file_block_desc_tag { unsigned char retry_flag; struct frame_index_tag { unsigned short block_index; unsigned short frame_index; struct frame_index_tag *next; } *index; struct file_block_desc_tag *next; } *block_head; struct file_block_desc_tag *block_tail; 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; } *block_list_lock; unsigned short block_count; unsigned short last_frame_count; unsigned short *frame_remain; } *f_desc; int (*init)(struct transfer_session_tag *session); unsigned long (*read_function)(struct transfer_session_tag *session, void *ptr, unsigned long size, int fd); unsigned long (*write_function)(struct transfer_session_tag *session, void *ptr, unsigned long size, int fd); int (*connect)(struct transfer_session_tag *session); int (*disconnect)(struct transfer_session_tag *session); long *transfer_file; } *

variable_types: len int

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

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

user_defined_types: struct transfer_session_tag { char *hostname; char *username; char *password; char *directory; unsigned int control_port; unsigned int data_port; int state; int fd; int data_fd; unsigned char train_no_len; unsigned char *train_no; unsigned char *hb; void *remote_addr; unsigned long remote_addr_len; struct sockaddr_in remote_con_addr; struct sockaddr_in remote_data_addr; int ai_family; int package_qid; int data_qid; void *protocol_data; struct file_desc *f_desc; int (*init)(struct transfer_session_tag *session); unsigned long (*read_function)(struct transfer_session_tag *session, void *ptr, unsigned long size, int fd); unsigned long (*write_function)(struct transfer_session_tag *session, void *ptr, unsigned long size, int fd); int (*connect)(struct transfer_session_tag *session); int (*disconnect)(struct transfer_session_tag *session); long *transfer_file; }

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

user_defined_types: struct file_desc { unsigned char *file_name; int file_size; unsigned short file_id; struct file_block_desc_tag *block_head; struct file_block_desc_tag *block_tail; union pthread_mutex_t *block_list_lock; unsigned short block_count; unsigned short last_frame_count; unsigned short *frame_remain; }

user_defined_types: struct file_block_desc_tag { unsigned char retry_flag; struct frame_index_tag *index; struct file_block_desc_tag *next; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; 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:
size_t __fastcall init_socket(__int64 a1, __int64 a2, unsigned __int16 a3, unsigned __int16 a4, const char *a5)
{
size_t result; // rax

*(_QWORD *)(a1 + 176) = connect_server;
*(_DWORD *)(a1 + 32) = a3;
*(_DWORD *)(a1 + 36) = a4;
*(_QWORD *)(a1 + 72) = a2;
*(_QWORD *)(a1 + 40) = 0x10000LL;
*(_QWORD *)(a1 + 56) = a5;
result = strlen(a5);
*(_QWORD *)(a1 + 144) = 0LL;
*(_BYTE *)(a1 + 52) = result;
return result;
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, __int64 a2, unsigned __int16 a3, unsigned __int16 a4, const char *a5)
{
size_t result; // rax

*(_QWORD *)(a1 + 176) = connect_server;
*(_DWORD *)(a1 + 32) = a3;
*(_DWORD *)(a1 + 36) = a4;
*(_QWORD *)(a1 + 72) = a2;
*(_QWORD *)(a1 + 40) = 0x10000LL;
*(_QWORD *)(a1 + 56) = a5;
result = strlen(a5);
*(_QWORD *)(a1 + 144) = 0LL;
*(_BYTE *)(a1 + 52) = result;
return result;
}

original_code:
int init_socket(transfer_session *session, char *host_ip, unsigned short control_port, unsigned short data_port,
                    char *train_no)
{
    session->connect = connect_server;
    session->control_port = control_port;
    session->data_port = data_port;
    session->remote_addr = host_ip;
    session->state = 0x00010000;
    session->fd = 0;
    session->train_no = train_no;
    session->train_no_len = strlen(train_no);
    session->f_desc = ((void *)0);
}

canonical_original_code:
int init_socket(struct transfer_session_tag *session, char *host_ip, unsigned short control_port, unsigned short data_port, char *train_no)
{
    session->connect = connect_server;
    session->control_port = control_port;
    session->data_port = data_port;
    session->remote_addr = host_ip;
    session->state = 0x00010000;
    session->fd = 0;
    session->train_no = train_no;
    session->train_no_len = strlen(train_no);
    session->f_desc = ((void *)0);
}

user_defined_types: struct frame_index_tag { unsigned short block_index; unsigned short frame_index; struct frame_index_tag *next; }

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


decompiled_code:
ssize_t __fastcall send_file_data(__int64 a1, const void *a2, int a3)
{
return sendto(*(_DWORD *)(a1 + 48), a2, a3, 0, (const struct sockaddr *)(a1 + 104), 0x10u);
}

canonical_decompiled_code:
ssize_t __fastcall func3(__int64 a1, const void *a2, int a3)
{
return sendto(*(_DWORD *)(a1 + 48), a2, a3, 0, (const struct sockaddr *)(a1 + 104), 0x10u);
}

original_code:
int send_file_data(transfer_session *session, char *buf_send, int data_len)
{
    int send_len;
    socklen_t len;
    len = sizeof(struct sockaddr);
    send_len = sendto(session->data_fd, buf_send, data_len, 0, (struct sockaddr*)&(session->remote_data_addr), len);
    return send_len;
}

canonical_original_code:
int send_file_data(struct transfer_session_tag *session, char *buf_send, int data_len)
{
    int send_len;
    unsigned int len;
    len = sizeof(struct sockaddr);
    send_len = sendto(session->data_fd, buf_send, data_len, 0, (struct sockaddr *)&(session->remote_data_addr), len);
    return send_len;
}


decompiled_code:
int __fastcall receive_handler(_DWORD *a1)
{
int v2; // er12
__int64 v3; // rbx
__int64 v4; // rbp
unsigned __int16 v6; // ax
int v7; // er15
__int64 v8; // r15
int v9; // eax
void *v10; // rax
__int64 v11; // rdi
__int16 *v12; // r15
__int64 v13; // rsi
int v14; // [rsp+8h] [rbp-A40h]
__int16 *v15; // [rsp+8h] [rbp-A40h]
int v16; // [rsp+10h] [rbp-A38h] BYREF
void *v17; // [rsp+18h] [rbp-A30h]
fd_set readfds; // [rsp+30h] [rbp-A18h] BYREF
__int16 buf; // [rsp+B0h] [rbp-998h] BYREF
char v20[4]; // [rsp+B2h] [rbp-996h] BYREF
char v21[2450]; // [rsp+B6h] [rbp-992h] BYREF

v2 = a1[11];
memset(&readfds, 0, sizeof(readfds));
v3 = v2 / 64;
v4 = 1LL << (v2 % 64);
while ( a1[10] != 0x10000 )
{
readfds.fds_bits[v3] |= v4;
select(v2 + 1, &readfds, 0LL, 0LL, 0LL);
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
recv(v2, &buf, 2uLL, 0);
v6 = _bswap_16(buf);
v7 = v6;
if ( (unsigned int)v6 - 1 > 0x95F )
{
t_log("receive package length error!length:%d\n", v6);
v14 = recv(v2, v20, 0x960uLL, 0);
printf("frame len:%d\n", (unsigned int)v14);
if ( v14 > 0 )
{
v12 = &buf;
v15 = (__int16 *)((char *)&buf + (unsigned int)(v14 - 1) + 1);
do
{
v13 = *(unsigned __int8 *)v12;
v12 = (__int16 *)((char *)v12 + 1);
printf("%02x ", v13);
}
while ( v12 != v15 );
}
putchar(10);
}
else if ( v6 == (unsigned int)recv(v2, v20, v6, 256) && *((_BYTE *)&buf + v7 + 1) == 0xFF )
{
get_crc_code(v21, (unsigned int)(v7 - 5));
v8 = (unsigned int)(v7 + 2);
v9 = t_malloc(v8);
v10 = memcpy((void *)v9, &buf, (int)v8);
v11 = (unsigned int)a1[31];
v17 = v10;
v16 = 1;
if ( (int)send_to_msg_q(v11, &v16, 24LL, 0LL) < 0 )
perror("msg closed! quit the system!");
}
}
}
return puts("exit receive handler");
}

canonical_decompiled_code:
int __fastcall func4(_DWORD *a1)
{
int v2; // er12
__int64 v3; // rbx
__int64 v4; // rbp
unsigned __int16 v6; // ax
int v7; // er15
__int64 v8; // r15
int v9; // eax
void *v10; // rax
__int64 v11; // rdi
__int16 *v12; // r15
__int64 v13; // rsi
int v14; // [rsp+8h] [rbp-A40h]
__int16 *v15; // [rsp+8h] [rbp-A40h]
int v16; // [rsp+10h] [rbp-A38h] BYREF
void *v17; // [rsp+18h] [rbp-A30h]
fd_set readfds; // [rsp+30h] [rbp-A18h] BYREF
__int16 buf; // [rsp+B0h] [rbp-998h] BYREF
char v20[4]; // [rsp+B2h] [rbp-996h] BYREF
char v21[2450]; // [rsp+B6h] [rbp-992h] BYREF

v2 = a1[11];
memset(&readfds, 0, sizeof(readfds));
v3 = v2 / 64;
v4 = 1LL << (v2 % 64);
while ( a1[10] != 0x10000 )
{
readfds.fds_bits[v3] |= v4;
select(v2 + 1, &readfds, 0LL, 0LL, 0LL);
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
recv(v2, &buf, 2uLL, 0);
v6 = func0(buf);
v7 = v6;
if ( (unsigned int)v6 - 1 > 0x95F )
{
t_log("receive package length error!length:%d\n", v6);
v14 = recv(v2, v20, 0x960uLL, 0);
printf("frame len:%d\n", (unsigned int)v14);
if ( v14 > 0 )
{
v12 = &buf;
v15 = (__int16 *)((char *)&buf + (unsigned int)(v14 - 1) + 1);
do
{
v13 = *(unsigned __int8 *)v12;
v12 = (__int16 *)((char *)v12 + 1);
printf("%02x ", v13);
}
while ( v12 != v15 );
}
putchar(10);
}
else if ( v6 == (unsigned int)recv(v2, v20, v6, 256) && *((_BYTE *)&buf + v7 + 1) == 0xFF )
{
get_crc_code(v21, (unsigned int)(v7 - 5));
v8 = (unsigned int)(v7 + 2);
v9 = t_malloc(v8);
v10 = memcpy((void *)v9, &buf, (int)v8);
v11 = (unsigned int)a1[31];
v17 = v10;
v16 = 1;
if ( (int)send_to_msg_q(v11, &v16, 24LL, 0LL) < 0 )
perror("msg closed! quit the system!");
}
}
}
return puts("exit receive handler");
}

original_code:
void receive_handler(void *args)
{
    fd_set rset;
    int sock_fd;
    int recv_len = 0;
    int frame_len;
    int len;
    int data_len;
    unsigned short recv_crc;
    unsigned char buf[2400];
    unsigned char *handle_package;
    q_msg package_msg;
    transfer_session *session;
    struct timeval timeout={1,0};
    int i;
    session = (transfer_session *)args;
    sock_fd = session->fd;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rset)->__fds_bits)[0]) : "memory"); } while (0);
    for(;;)
    {
        if(session->state == 0x00010000)
        {
            printf("exit receive handler\n");
            return;
        }
        ((void) (((&rset)->__fds_bits)[((sock_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock_fd) % (8 * (int) sizeof (__fd_mask)))))));
        select(sock_fd + 1, &rset, ((void *)0), ((void *)0), ((void *)0));
        if(((((&rset)->__fds_bits)[((sock_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
        {
            len = recv(sock_fd, buf, 2, 0);
            recv_len = 0;
            recv_len = ntohs(*((unsigned short *)buf));
            if (recv_len > 2400 || recv_len <= 0)
            {
                t_log("receive package length error!length:%d\n", recv_len);
                frame_len = recv(sock_fd, buf + 2, 2400, 0);
                printf("frame len:%d\n", frame_len);
                for(i = 0; i < frame_len; i++)
                    printf("%02x ", buf[i]);
                printf("\n");
                continue;
            }
            frame_len = recv(sock_fd, buf + 2, recv_len, MSG_WAITALL);
            if (frame_len != recv_len)
                continue;
            frame_len += 2;
            if (buf[recv_len + 1] != 0xFF)
                continue;
            data_len = recv_len + 1 - sizeof(frame_header);
            recv_crc = get_crc_code(buf + sizeof(frame_header), data_len);
            handle_package = (unsigned char *)t_malloc(frame_len);
            memcpy(handle_package, buf, frame_len);
            memcpy(&package_msg.msg_buf.data, &handle_package, sizeof(handle_package));
            package_msg.msg_type = 0x01;
            if(send_to_msg_q(session->package_qid, &package_msg, sizeof(q_msg), 0) < 0)
            {
                perror("msg closed! quit the system!");
            }
        }
    }
    return;
}

canonical_original_code:
void receive_handler(void *args)
{
    struct fd_set rset;
    int sock_fd;
    int recv_len = 0;
    int frame_len;
    int len;
    int data_len;
    unsigned short recv_crc;
    unsigned char buf[2400];
    unsigned char *handle_package;
    struct q_msg_tag package_msg;
    struct transfer_session_tag *session;
    struct timeval timeout = {1,0};
    int i;
    session = (struct transfer_session_tag *)args;
    sock_fd = session->fd;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rset)->__fds_bits)[0]) : "memory"); } while (0);
    for(;;)
    {
        if(session->state == 0x00010000)
        {
            printf("exit receive handler\n");
            return;
        }
        ((void) (((&rset)->__fds_bits)[((sock_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sock_fd) % (8 * (int) sizeof (__fd_mask)))))));
        select(sock_fd + 1, &rset, ((void *)0), ((void *)0), ((void *)0));
        if(((((&rset)->__fds_bits)[((sock_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((sock_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
        {
            len = recv(sock_fd, buf, 2, 0);
            recv_len = 0;
            recv_len = ntohs(*((unsigned short *)buf));
            if (recv_len > 2400 || recv_len <= 0)
            {
                t_log("receive package length error!length:%d\n", recv_len);
                frame_len = recv(sock_fd, buf + 2, 2400, 0);
                printf("frame len:%d\n", frame_len);
                for(i = 0; i < frame_len; i++)
                    printf("%02x ", buf[i]);
                printf("\n");
                continue;
            }
            frame_len = recv(sock_fd, buf + 2, recv_len, MSG_WAITALL);
            if (frame_len != recv_len)
                continue;
            frame_len += 2;
            if (buf[recv_len + 1] != 0xFF)
                continue;
            data_len = recv_len + 1 - sizeof(frame_header);
            recv_crc = get_crc_code(buf + sizeof(frame_header), data_len);
            handle_package = (unsigned char *)t_malloc(frame_len);
            memcpy(handle_package, buf, frame_len);
            memcpy(&package_msg.msg_buf.data, &handle_package, sizeof(handle_package));
            package_msg.msg_type = 0x01;
            if(send_to_msg_q(session->package_qid, &package_msg, sizeof(q_msg), 0) < 0)
            {
                perror("msg closed! quit the system!");
            }
        }
    }
    return;
}

variable_types:
args
void *

variable_types:
rset
struct fd_set { long __fds_bits[-1]; }

variable_types:
buf
unsigned char [2400]

variable_types: struct q_msg_tag { unsigned int msg_type; struct data_msg_tag { unsigned char *data; unsigned short data_len; } msg_buf; }

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

user_defined_types: struct q_msg_tag { unsigned int msg_type; struct data_msg_tag msg_buf; }


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

result = (unsigned int)(*(_DWORD *)(a1 + 40) - 65539);
if ( (unsigned int)result <= 2 )
{
while ( (unsigned int)send(*(_DWORD *)(a1 + 44), *(const void **)(a1 + 64), 7uLL, 0) == 7 )
{
sleep(1u);
result = (unsigned int)(*(_DWORD *)(a1 + 40) - 65539);
if ( (unsigned int)result > 2 )
return result;
}
*(_DWORD *)(a1 + 40) = 65543;
result = t_log("[heart_beat]connection lost!");
}
return result;
}

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

result = (unsigned int)(*(_DWORD *)(a1 + 40) - 65539);
if ( (unsigned int)result <= 2 )
{
while ( (unsigned int)send(*(_DWORD *)(a1 + 44), *(const void **)(a1 + 64), 7uLL, 0) == 7 )
{
sleep(1u);
result = (unsigned int)(*(_DWORD *)(a1 + 40) - 65539);
if ( (unsigned int)result > 2 )
return result;
}
*(_DWORD *)(a1 + 40) = 65543;
result = t_log("[heart_beat]connection lost!");
}
return result;
}

original_code:
void send_heartbeat_thread(void *args)
{
    transfer_session *session;
    int ret = 0;
    session = (transfer_session *)args;
    for(;;)
    {
        if(session->state < 0x00010003 || session->state >= 0x00010006)
            return;
        if((ret = send(session->fd, session->hb, 7, 0)) != 7)
        {
            session->state = 0x00010007;
            t_log("[heart_beat]connection lost!");
            return;
        }
        sleep(1);
    }
}

canonical_original_code:
void send_heartbeat_thread(void *args)
{
    struct transfer_session_tag *session;
    int ret = 0;
    session = (struct transfer_session_tag *)args;
    for(;;)
    {
        if(session->state < 0x00010003 || session->state >= 0x00010006)
            return;
        if((ret = send(session->fd, session->hb, 7, 0)) != 7)
        {
            session->state = 0x00010007;
            t_log("[heart_beat]connection lost!");
            return;
        }
        sleep(1);
    }
}

variable_types: ret int


decompiled_code:
__int64 __fastcall get_crc_code(__int64 a1, unsigned int a2)
{
unsigned int v2; // er8
int v3; // edx
__int64 v4; // rcx
char v5; // ah

v2 = 0;
if ( a2 )
{
v3 = 0;
do
{
v4 = (unsigned __int16)v3;
v5 = BYTE1(v2);
++v3;
v2 <<= 8;
LOWORD(v2) = crc_table[(unsigned __int8)(*(_BYTE *)(a1 + v4) ^ v5)] ^ v2;
}
while ( (unsigned __int16)v3 < a2 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
unsigned int v2; // er8
int v3; // edx
__int64 v4; // rcx
char v5; // ah

v2 = 0;
if ( a2 )
{
v3 = 0;
do
{
v4 = (unsigned __int16)v3;
v5 = BYTE1(v2);
++v3;
v2 <<= 8;
LOWORD(v2) = crc_table[(unsigned __int8)(*(_BYTE *)(a1 + v4) ^ v5)] ^ v2;
}
while ( (unsigned __int16)v3 < a2 );
}
return v2;
}

original_code:
unsigned short get_crc_code(const unsigned char *buf, unsigned int len)
{
    unsigned short i, j, crc = 0;
    for(i = 0; i < len; i++)
    {
            j = (crc >> 8) ^ buf[i];
            crc = (crc << 8) ^ crc_table[j];
    }
    return crc;
}

canonical_original_code:
unsigned short get_crc_code(unsigned char *buf, unsigned int len)
{
    unsigned short i; unsigned short j; unsigned short crc = 0;
    for(i = 0; i < len; i++)
    {
            j = (crc >> 8) ^ buf[i];
            crc = (crc << 8) ^ crc_table[j];
    }
    return crc;
}

variable_types:
buf
unsigned char *

variable_types:
len
unsigned int

variable_types:
i
unsigned short

variable_types:
j
unsigned short

variable_types:
crc
unsigned short

return_type: unsigned short


repo: lbillon/SEA-Linux-Scheduling

decompiled_code:
void *__fastcall task_b(void *a1)
{
puts("Hello world B");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
puts("Hello world B");
return 0LL;
}

original_code:
static void *task_b (void *p_data)
{
   puts ("Hello world B");
   (void) p_data;
   return ((void *)0);
}

canonical_original_code:
static void *task_b(void *p_data)
{
   puts ("Hello world B");
   (void) p_data;
   return ((void *)0);
}

return_type: void *


decompiled_code:
void *__fastcall task_a(void *a1)
{
puts("Hello world A");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
puts("Hello world A");
return 0LL;
}

original_code:
static void *task_a (void *p_data)
{
   puts ("Hello world A");
   (void) p_data;
   return ((void *)0);
}

canonical_original_code:
static void *task_a(void *p_data)
{
   puts ("Hello world A");
   (void) p_data;
   return ((void *)0);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t v4; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

puts("Hello world");
pthread_create(&v4, 0LL, task_a, 0LL);
pthread_create(newthread, 0LL, task_b, 0LL);
return 0;
}

original_code:
int main() {
   puts ("Hello world");
    pthread_t ta;
    pthread_t tb;
    pthread_attr_t high_prio_thread;
    pthread_attr_t low_prio_thread;
    pthread_create (&ta, ((void *)0), task_a, ((void *)0));
    pthread_create (&tb, ((void *)0), task_b, ((void *)0));
}

canonical_original_code:
int main() {
   puts ("Hello world");
    unsigned long ta;
    unsigned long tb;
    union pthread_attr_t high_prio_thread;
    union pthread_attr_t low_prio_thread;
    pthread_create (&ta, ((void *)0), task_a, ((void *)0));
    pthread_create (&tb, ((void *)0), task_b, ((void *)0));
}

variable_types:
ta
unsigned long

variable_types:
tb
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
void __fastcall __noreturn activite_philo(void *a1)
{
unsigned int v1; // ebp
__int64 v2; // r12
__int64 v3; // r13

v1 = nombre_philosophes - 1;
if ( (_DWORD)a1 )
v1 = (_DWORD)a1 - 1;
v2 = 40LL * (int)v1;
v3 = 40LL * (int)a1;
while ( 1 )
{
printf("%d: je veux ma fourchette droite: %d\n", (unsigned int)a1, v1);
pthread_mutex_lock((pthread_mutex_t *)(v2 + fourchettes));
printf("%d: j'ai ma fourchette droite: %d\n", (unsigned int)a1, v1);
sleep(3u);
printf("%d: je veux ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
if ( pthread_mutex_trylock((pthread_mutex_t *)(v3 + fourchettes)) )
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + fourchettes));
printf(aDJAiLach, (unsigned int)a1, v1);
}
else
{
printf("%d: j'ai ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
sleep(3u);
printf("%d: j'ai mes deux fourchettes! miam!\n", (unsigned int)a1);
sleep(5u);
printf("%d: je lache ma fourchette droite: %d\n", (unsigned int)a1, v1);
pthread_mutex_unlock((pthread_mutex_t *)(v2 + fourchettes));
printf("%d: j'ai lache' ma fourchette droite: %d\n", (unsigned int)a1, v1);
sleep(3u);
printf("%d: je lache ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
pthread_mutex_unlock((pthread_mutex_t *)(v3 + fourchettes));
printf("%d: j'ai lache' ma fourchette droite: %d\n", (unsigned int)a1, (unsigned int)a1);
}
sleep(3u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
unsigned int v1; // ebp
__int64 v2; // r12
__int64 v3; // r13

v1 = nombre_philosophes - 1;
if ( (_DWORD)a1 )
v1 = (_DWORD)a1 - 1;
v2 = 40LL * (int)v1;
v3 = 40LL * (int)a1;
while ( 1 )
{
printf("%d: je veux ma fourchette droite: %d\n", (unsigned int)a1, v1);
pthread_mutex_lock((pthread_mutex_t *)(v2 + fourchettes));
printf("%d: j'ai ma fourchette droite: %d\n", (unsigned int)a1, v1);
sleep(3u);
printf("%d: je veux ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
if ( pthread_mutex_trylock((pthread_mutex_t *)(v3 + fourchettes)) )
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + fourchettes));
printf(aDJAiLach, (unsigned int)a1, v1);
}
else
{
printf("%d: j'ai ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
sleep(3u);
printf("%d: j'ai mes deux fourchettes! miam!\n", (unsigned int)a1);
sleep(5u);
printf("%d: je lache ma fourchette droite: %d\n", (unsigned int)a1, v1);
pthread_mutex_unlock((pthread_mutex_t *)(v2 + fourchettes));
printf("%d: j'ai lache' ma fourchette droite: %d\n", (unsigned int)a1, v1);
sleep(3u);
printf("%d: je lache ma fourchette gauche: %d\n", (unsigned int)a1, (unsigned int)a1);
pthread_mutex_unlock((pthread_mutex_t *)(v3 + fourchettes));
printf("%d: j'ai lache' ma fourchette droite: %d\n", (unsigned int)a1, (unsigned int)a1);
}
sleep(3u);
}
}

original_code:
void* activite_philo(void* arg)
{
 int right=0;
 int left=0;
 int philoPos = (int)(long int)arg;
 int gauche = philoPos;
 int droite = philoPos - 1;
 if(droite == -1)
 {
  droite = nombre_philosophes-1;
 }
 while(1)
 {
  if(right == 0)
  {
   printf("%d: je veux ma fourchette droite: %d\n",philoPos,droite);
   pthread_mutex_lock(&fourchettes[droite]);
   right = 1;
   printf("%d: j'ai ma fourchette droite: %d\n",philoPos, droite);
   sleep(3);
  }
  if(left == 0)
  {
   printf("%d: je veux ma fourchette gauche: %d\n",philoPos,gauche);
   int try = pthread_mutex_trylock(&fourchettes[gauche]);
   if(try != 0)
   {
    if (right == 1)
    {
     pthread_mutex_unlock(&fourchettes[droite]);
     right = 0;
     printf("%d: j'ai laché ma fourchette droite parce que l'autre était prise: %d\n",philoPos, droite);
     sleep(3);
    }
   }
   else
   {
    left = 1;
    printf("%d: j'ai ma fourchette gauche: %d\n",philoPos,gauche);
    sleep(3);
   }
  }
  if ((left ==1) && (right == 1))
  {
   printf("%d: j'ai mes deux fourchettes! miam!\n",philoPos);
   sleep(5);
   printf("%d: je lache ma fourchette droite: %d\n",philoPos,droite);
   pthread_mutex_unlock(&fourchettes[droite]);
   right = 0;
   printf("%d: j'ai lache' ma fourchette droite: %d\n",philoPos, droite);
   sleep(3);
   printf("%d: je lache ma fourchette gauche: %d\n",philoPos,gauche);
   pthread_mutex_unlock(&fourchettes[gauche]);
   left = 0;
   printf("%d: j'ai lache' ma fourchette droite: %d\n",philoPos, gauche);
   sleep(3);
  }
 }
 return ((void *)0) ;
}

canonical_original_code:
void *activite_philo(void *arg)
{
 int right = 0;
 int left = 0;
 int philoPos = (int)(long int)arg;
 int gauche = philoPos;
 int droite = philoPos - 1;
 if(droite == -1)
 {
  droite = nombre_philosophes-1;
 }
 while(1)
 {
  if(right == 0)
  {
   printf("%d: je veux ma fourchette droite: %d\n",philoPos,droite);
   pthread_mutex_lock(&fourchettes[droite]);
   right = 1;
   printf("%d: j'ai ma fourchette droite: %d\n",philoPos, droite);
   sleep(3);
  }
  if(left == 0)
  {
   printf("%d: je veux ma fourchette gauche: %d\n",philoPos,gauche);
   int try = pthread_mutex_trylock(&fourchettes[gauche]);
   if(try != 0)
   {
    if (right == 1)
    {
     pthread_mutex_unlock(&fourchettes[droite]);
     right = 0;
     printf("%d: j'ai laché ma fourchette droite parce que l'autre était prise: %d\n",philoPos, droite);
     sleep(3);
    }
   }
   else
   {
    left = 1;
    printf("%d: j'ai ma fourchette gauche: %d\n",philoPos,gauche);
    sleep(3);
   }
  }
  if ((left ==1) && (right == 1))
  {
   printf("%d: j'ai mes deux fourchettes! miam!\n",philoPos);
   sleep(5);
   printf("%d: je lache ma fourchette droite: %d\n",philoPos,droite);
   pthread_mutex_unlock(&fourchettes[droite]);
   right = 0;
   printf("%d: j'ai lache' ma fourchette droite: %d\n",philoPos, droite);
   sleep(3);
   printf("%d: je lache ma fourchette gauche: %d\n",philoPos,gauche);
   pthread_mutex_unlock(&fourchettes[gauche]);
   left = 0;
   printf("%d: j'ai lache' ma fourchette droite: %d\n",philoPos, gauche);
   sleep(3);
  }
 }
 return ((void *)0) ;
}

variable_types:
arg
void *

variable_types: right int

variable_types: left int

variable_types: philoPos int

variable_types: gauche int

variable_types: droite int

variable_types: try int


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
pthread_mutex_t *v4; // rax
__int64 v5; // rsi
void *v6; // rbx

v3 = 0LL;
nombre_philosophes = 5;
philosophes = (__int64)calloc(5uLL, 8uLL);
v4 = (pthread_mutex_t *)calloc(5uLL, 0x28uLL);
for ( fourchettes = (__int64)v4; ; v4 = (pthread_mutex_t *)fourchettes )
{
if ( pthread_mutex_init(&v4[v3], 0LL) )
{
v5 = (unsigned int)v3++;
printf("mutex init error %d\n", v5);
if ( v3 == 5 )
{
LABEL_6:
v6 = 0LL;
while ( 1 )
{
if ( pthread_create((pthread_t *)(philosophes + 8LL * (_QWORD)v6), 0LL, activite_philo, v6) )
printf("thread create error %d\n", (unsigned int)v6);
v6 = (char *)v6 + 1;
if ( v6 == (void *)5 )
{
while ( 1 )
;
}
}
}
}
else if ( ++v3 == 5 )
{
goto LABEL_6;
}
}
}

original_code:
int main()
{
 nombre_philosophes = 5;
 philosophes = calloc(5, sizeof(pthread_t));
 fourchettes = calloc(5,sizeof(pthread_mutex_t));
 int i;
 for(i=0;i<5;i++)
 {
  int merror = pthread_mutex_init(&fourchettes[i],((void *)0));
  if(merror != 0)
  {
   printf("mutex init error %d\n", i);
  }
 }
 for(i=0;i<5;i++)
 {
  int terror = pthread_create(&philosophes[i], ((void *)0),activite_philo, (void*)(long int)i);
  if (terror != 0)
  {
   printf ("thread create error %d\n", i);
  }
 }
 for(;;);
}

variable_types: i int

variable_types: merror int

variable_types: terror int


strlen

repo: lbonn/nostd

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
_BYTE *v1; // rax
int v2; // edx

v1 = a1 + 1;
if ( !*a1 )
return 0LL;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
_BYTE *v1; // rax
int v2; // edx

v1 = a1 + 1;
if ( !*a1 )
return 0LL;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

original_code:
static size_t strlen(const char *str)
{
    int k = 0;
    while (*str++) {
        k++;
    }
    return k;
}

canonical_original_code:
static unsigned long strlen(char *str)
{
    int k = 0;
    while (*str++) {
        k++;
    }
    return k;
}

variable_types:
str
char *

variable_types: k int

return_type: unsigned long


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

result = (unsigned int)(a1 + 87);
if ( (unsigned __int8)a1 <= 9u )
result = (unsigned int)(a1 + 48);
return result;
}

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

result = (unsigned int)(a1 + 87);
if ( (unsigned __int8)a1 <= 9u )
result = (unsigned int)(a1 + 48);
return result;
}

original_code:
static unsigned char digit_char(unsigned char c)
{
    if (c < 10) {
        return c + '0';
    }
    return (c-10) + 'a';
}

variable_types:
c
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall digit_from_char(char a1, __int64 a2, unsigned __int8 *a3)
{
unsigned __int8 v3; // al
_BOOL4 v4; // er8
char v5; // al
__int64 result; // rax

v3 = a1 - 48;
if ( (unsigned __int8)(a1 - 48) <= 9u )
{
*a3 = v3;
result = v3 < a2;
}
else
{
v4 = 0;
if ( a2 > 10 )
{
v5 = a1 - 65;
if ( (unsigned __int8)(a1 - 65) <= 0x19u || (v5 = a1 - 97, (unsigned __int8)(a1 - 97) <= 0x19u) )
{
*a3 = v5 + 10;
v4 = v5 < a2;
}
}
result = v4;
}
return result;
}

original_code:
static int digit_from_char(char c, long base, char *digit)
{
    if (c >= '0' && c <= '9') {
        *digit = c - '0';
        return (*digit < base);
    }
    if (base <= 10) {
        return 0;
    }
    char p;
    if (c >= 'A' && c <= 'Z') {
        p = c - 'A';
    } else if (c >= 'a' && c <= 'z') {
        p = c - 'a';
    } else {
        return 0;
    }
    *digit = p + 10;
    return (p < base);
}

variable_types: c char

variable_types: base long

variable_types:
digit
char *

variable_types: p char


strtoull

decompiled_code:
__int64 __fastcall strtoull(char *a1)
{
char v1; // al
char *i; // r9
char v3; // di
__int64 v4; // rsi
__int64 v5; // r9
__int64 v6; // r10
unsigned __int8 v8; // [rsp+Fh] [rbp-1h] BYREF

v1 = *a1;
for ( i = a1; v1 == 32; ++i )
v1 = i[1];
if ( v1 == 48 )
{
v3 = i[1];
if ( v3 == 120 )
{
v3 = i[2];
v4 = 16LL;
}
else
{
v4 = 8LL;
}
}
else
{
v3 = *i;
v4 = 10LL;
}
while ( (unsigned int)digit_from_char(v3, v4, &v8) )
v3 = *(_BYTE *)(v5 + 1);
return v6;
}

original_code:
static unsigned long long strtoull(const char *nptr)
{
    char c;
    char digit;
    long base;
    unsigned long long l;
    while (*nptr == ' ') {
        nptr++;
    };
    if (nptr[0] == '0' && nptr[1] == 'x') {
        base = 16;
        nptr += 2;
    } else if (nptr[0] == '0') {
        base = 8;
        nptr += 1;
    } else {
        base = 10;
    }
    l = 0;
    while (digit_from_char(c = *nptr, base, &digit)) {
        l = l*base + digit;
        nptr++;
    }
    return l;
}

canonical_original_code:
static unsigned long long strtoull(char *nptr)
{
    char c;
    char digit;
    long base;
    unsigned long long l;
    while (*nptr == ' ') {
        nptr++;
    };
    if (nptr[0] == '0' && nptr[1] == 'x') {
        base = 16;
        nptr += 2;
    } else if (nptr[0] == '0') {
        base = 8;
        nptr += 1;
    } else {
        base = 10;
    }
    l = 0;
    while (digit_from_char(c = *nptr, base, &digit)) {
        l = l*base + digit;
        nptr++;
    }
    return l;
}

variable_types:
nptr
char *

variable_types: digit char

variable_types:
l
unsigned long long

return_type: unsigned long long


decompiled_code:
ssize_t __fastcall print_str(void *buf)
{
__int64 v1; // rax

v1 = strlen(buf);
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func4(void *buf)
{
__int64 v1; // rax

v1 = func0(buf);
return write(1, buf, v1);
}

original_code:
static void print_str(const char *str)
{
    write(stdout_fd, str, strlen(str));
}

canonical_original_code:
static void print_str(char *str)
{
    write(stdout_fd, str, strlen(str));
}


decompiled_code:
ssize_t __fastcall print_char(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func5(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

original_code:
static void print_char(char c)
{
    write(stdout_fd, &c, 1);
}


decompiled_code:
ssize_t __fastcall print_ull(unsigned __int64 a1, unsigned __int64 a2)
{
__int64 v4; // rsi
int v5; // ebx
char v6; // al
unsigned __int64 v7; // rcx
unsigned __int64 v8; // rdx
unsigned __int64 v9; // r9
__int64 i; // rbx
ssize_t result; // rax
char v12[88]; // [rsp+0h] [rbp-58h]

v4 = 0LL;
do
{
v5 = v4;
v6 = digit_char(a1 % a2);
v8 = v7;
a1 = v9;
v12[v4++] = v6;
}
while ( v8 >= a2 );
for ( i = v5 - 1; ; --i )
{
result = print_char(v6);
if ( (_DWORD)i == -1 )
break;
v6 = v12[i];
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func6(unsigned __int64 a1, unsigned __int64 a2)
{
__int64 v4; // rsi
int v5; // ebx
char v6; // al
unsigned __int64 v7; // rcx
unsigned __int64 v8; // rdx
unsigned __int64 v9; // r9
__int64 i; // rbx
ssize_t result; // rax
char v12[88]; // [rsp+0h] [rbp-58h]

v4 = 0LL;
do
{
v5 = v4;
v6 = func1(a1 % a2);
v8 = v7;
a1 = v9;
v12[v4++] = v6;
}
while ( v8 >= a2 );
for ( i = v5 - 1; ; --i )
{
result = func5(v6);
if ( (_DWORD)i == -1 )
break;
v6 = v12[i];
}
return result;
}

original_code:
static void print_ull(unsigned long long l, unsigned long base)
{
    unsigned char buf[sizeof(unsigned long long)*8+1];
    int k = 0;
    do {
        buf[k] = digit_char(l%base);
        l = l/base;
        k++;
    } while(l >0);
    k--;
    for (;k >= 0;k--) {
        print_char(buf[k]);
    }
}

canonical_original_code:
static void print_ull(unsigned long long l, unsigned long base)
{
    unsigned char buf[-1];
    int k = 0;
    do {
        buf[k] = digit_char(l%base);
        l = l/base;
        k++;
    } while(l >0);
    k--;
    for (;k >= 0;k--) {
        print_char(buf[k]);
    }
}

variable_types:
base
unsigned long

variable_types:
buf
unsigned char [-1]


decompiled_code:
ssize_t __fastcall print_it(unsigned __int64 a1)
{
print_ull(a1, 0xAuLL);
print_str(&unk_250);
print_ull(a1, 0x10uLL);
print_str(&unk_254);
print_ull(a1, 8uLL);
if ( a1 - 32 <= 0x5E )
{
print_str(&unk_257);
print_char(a1);
print_char(39);
}
return print_char(10);
}

canonical_decompiled_code:
ssize_t __fastcall func7(unsigned __int64 a1)
{
func6(a1, 0xAuLL);
func4(&unk_250);
func6(a1, 0x10uLL);
func4(&unk_254);
func6(a1, 8uLL);
if ( a1 - 32 <= 0x5E )
{
func4(&unk_257);
func5(a1);
func5(39);
}
return func5(10);
}

original_code:
static void print_it(unsigned long long l)
{
    print_ull(l, 10);
    print_str(" 0x");
    print_ull(l, 16);
    print_str(" 0");
    print_ull(l, 8);
    if (l >= 0x20 && l <= 0x7E) {
        print_str(" '");
        print_char((unsigned char)l);
        print_char('\'');
    }
    print_char('\n');
}


repo: lbrayat/progsystemetds

decompiled_code:
void __fastcall __noreturn affiche_max_fois_valeur_px_py(void *a1)
{
int v1; // ebx
char *v2; // rax

v1 = 0;
if ( (int)a1 > 0 )
{
do
{
while ( 1 )
{
pthread_mutex_lock(&mymutex_px);
if ( !pthread_mutex_trylock(&mymutex_py) )
break;
pthread_mutex_unlock(&mymutex_px);
}
++v1;
printf("[thread3] %f %f\n", *(float *)px, *(float *)py);
pthread_mutex_unlock(&mymutex_px);
pthread_mutex_unlock(&mymutex_py);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
int v1; // ebx
char *v2; // rax

v1 = 0;
if ( (int)a1 > 0 )
{
do
{
while ( 1 )
{
pthread_mutex_lock(&mymutex_px);
if ( !pthread_mutex_trylock(&mymutex_py) )
break;
pthread_mutex_unlock(&mymutex_px);
}
++v1;
printf("[thread3] %f %f\n", *(float *)px, *(float *)py);
pthread_mutex_unlock(&mymutex_px);
pthread_mutex_unlock(&mymutex_py);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

original_code:
static void * affiche_max_fois_valeur_px_py (void * p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  int allMutexOk = 0;
  while (allMutexOk == 0) {
   pthread_mutex_lock(&mymutex_px);
   if (pthread_mutex_trylock(&mymutex_py) == 0) {
    allMutexOk = 1;
   }
   else {
    pthread_mutex_unlock(&mymutex_px);
   }
  }
  printf("[thread3] %f %f\n", *px, *py);
  pthread_mutex_unlock(&mymutex_px);
  pthread_mutex_unlock(&mymutex_py);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

canonical_original_code:
static void *affiche_max_fois_valeur_px_py(void *p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  int allMutexOk = 0;
  while (allMutexOk == 0) {
   pthread_mutex_lock(&mymutex_px);
   if (pthread_mutex_trylock(&mymutex_py) == 0) {
    allMutexOk = 1;
   }
   else {
    pthread_mutex_unlock(&mymutex_px);
   }
  }
  printf("[thread3] %f %f\n", *px, *py);
  pthread_mutex_unlock(&mymutex_px);
  pthread_mutex_unlock(&mymutex_py);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

variable_types: max int

variable_types: allMutexOk int

variable_types:
resultChild
char *


decompiled_code:
void __fastcall __noreturn affiche_max_fois_valeur_py(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
pthread_mutex_lock(&mymutex_py);
printf("[thread2] %f\n", *(float *)py);
pthread_mutex_unlock(&mymutex_py);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
pthread_mutex_lock(&mymutex_py);
printf("[thread2] %f\n", *(float *)py);
pthread_mutex_unlock(&mymutex_py);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

original_code:
static void * affiche_max_fois_valeur_py (void * p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_py);
  printf("[thread2] %f\n", *py);
  pthread_mutex_unlock(&mymutex_py);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

canonical_original_code:
static void *affiche_max_fois_valeur_py(void *p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_py);
  printf("[thread2] %f\n", *py);
  pthread_mutex_unlock(&mymutex_py);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

variable_types: i int


decompiled_code:
void __fastcall __noreturn affiche_max_fois_valeur_px(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
pthread_mutex_lock(&mymutex_px);
printf("[thread1] %f\n", *(float *)px);
pthread_mutex_unlock(&mymutex_px);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
pthread_mutex_lock(&mymutex_px);
printf("[thread1] %f\n", *(float *)px);
pthread_mutex_unlock(&mymutex_px);
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

original_code:
static void * affiche_max_fois_valeur_px (void * p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_px);
  printf("[thread1] %f\n", *px);
  pthread_mutex_unlock(&mymutex_px);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

canonical_original_code:
static void *affiche_max_fois_valeur_px(void *p_data)
{
 int max = (int) p_data;
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_px);
  printf("[thread1] %f\n", *px);
  pthread_mutex_unlock(&mymutex_px);
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // ebx
void *v5; // r12
__int64 v6; // rsi
int v7; // [rsp+0h] [rbp-38h] BYREF
int v8; // [rsp+4h] [rbp-34h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
pthread_t v10; // [rsp+10h] [rbp-28h] BYREF
pthread_t v11[4]; // [rsp+18h] [rbp-20h] BYREF

if ( argc > 1 )
{
v4 = 0;
v5 = (void *)atoi(argv[1]);
pthread_mutex_init(&mymutex_px, 0LL);
pthread_mutex_init(&mymutex_py, 0LL);
v7 = 1065353216;
v8 = 0x40000000;
px = (__int64)&v7;
py = (__int64)&v8;
pthread_create(&newthread, 0LL, affiche_max_fois_valeur_px, v5);
pthread_create(&v10, 0LL, affiche_max_fois_valeur_py, v5);
pthread_create(v11, 0LL, affiche_max_fois_valeur_px_py, v5);
while ( v4 < (int)v5 )
{
pthread_mutex_lock(&mymutex_px);
v6 = (unsigned int)v4;
px = 0LL;
++v4;
printf("[main] %i\n", v6);
px = (__int64)&v7;
pthread_mutex_unlock(&mymutex_px);
}
puts("termine");
pthread_exit(0LL);
}
puts("erreur de parametres");
return -1;
}

original_code:
int main(int argc, char *argv[])
{
 if (argc < 2) {
  printf("erreur de parametres\n");
  return -1;
 }
 int max = atoi(argv[1]);
 pthread_mutex_init(&mymutex_px, ((void *)0));
 pthread_mutex_init(&mymutex_py, ((void *)0));
 float x = 1;
 float y = 2;
 px = &x;
 py = &y;
 pthread_t myThread1;
 int ret = pthread_create (
            & myThread1, ((void *)0),
            affiche_max_fois_valeur_px, (void *) max
         );
 pthread_t myThread2;
 ret = pthread_create (
            & myThread2, ((void *)0),
            affiche_max_fois_valeur_py, (void *) max
         );
 pthread_t myThread3;
 ret = pthread_create (
            & myThread3, ((void *)0),
            affiche_max_fois_valeur_px_py, (void *) max
         );
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_px);
  px = ((void *)0);
  printf("[main] %i\n", i);
  px = &x;
  pthread_mutex_unlock(&mymutex_px);
 }
 printf("termine\n");
        pthread_exit(((void *)0));
 pthread_mutex_destroy(&mymutex_px);
 pthread_mutex_destroy(&mymutex_py);
 return(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 if (argc < 2) {
  printf("erreur de parametres\n");
  return -1;
 }
 int max = atoi(argv[1]);
 pthread_mutex_init(&mymutex_px, ((void *)0));
 pthread_mutex_init(&mymutex_py, ((void *)0));
 float x = 1;
 float y = 2;
 px = &x;
 py = &y;
 unsigned long myThread1;
 int ret = pthread_create (
            & myThread1, ((void *)0),
            affiche_max_fois_valeur_px, (void *) max
         );
 unsigned long myThread2;
 ret = pthread_create (
            & myThread2, ((void *)0),
            affiche_max_fois_valeur_py, (void *) max
         );
 unsigned long myThread3;
 ret = pthread_create (
            & myThread3, ((void *)0),
            affiche_max_fois_valeur_px_py, (void *) max
         );
 int i;
 for (i = 0; i < max; i++) {
  pthread_mutex_lock(&mymutex_px);
  px = ((void *)0);
  printf("[main] %i\n", i);
  px = &x;
  pthread_mutex_unlock(&mymutex_px);
 }
 printf("termine\n");
        pthread_exit(((void *)0));
 pthread_mutex_destroy(&mymutex_px);
 pthread_mutex_destroy(&mymutex_py);
 return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: x float

variable_types: y float

variable_types: ret int


decompiled_code:
void __fastcall __noreturn affiche_n_fois_courroux(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
puts("et mon courroux");
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
int v1; // ebx
char *v2; // rax

if ( (int)a1 > 0 )
{
v1 = 0;
do
{
++v1;
puts("et mon courroux");
sleep(1u);
}
while ( (_DWORD)a1 != v1 );
}
v2 = (char *)malloc(0x14uLL);
strcpy(v2, "statusFinChild");
pthread_exit(v2);
}

original_code:
static void * affiche_n_fois_courroux (void * p_data)
{
 int nb = (int) p_data;
 int i;
 for (i = 0; i < nb; i++) {
  printf("et mon courroux\n");
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

canonical_original_code:
static void *affiche_n_fois_courroux(void *p_data)
{
 int nb = (int) p_data;
 int i;
 for (i = 0; i < nb; i++) {
  printf("et mon courroux\n");
  sleep(1);
 }
 char *resultChild = malloc(20);
 strcpy(resultChild, "statusFinChild");
 pthread_exit(resultChild);
    return ((void *)0);
}

variable_types: nb int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rbp
int v5; // ebx
int result; // eax
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

if ( argc <= 2 )
{
puts("erreur de parametres");
result = -1;
}
else
{
v3 = atoi(argv[1]);
v4 = (unsigned int)atoi(argv[2]);
printf("M=%i main/coucou\n", v4);
printf("N=%i thread/et mon courroux\n", (unsigned int)v3);
putchar(10);
pthread_create(newthread, 0LL, affiche_n_fois_courroux, (void *)v3);
if ( (int)v4 > 0 )
{
v5 = 0;
do
{
++v5;
puts("coucou");
sleep(1u);
}
while ( (_DWORD)v4 != v5 );
}
puts("termine");
pthread_cancel(newthread[0]);
result = 0;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
 if (argc < 3) {
  printf("erreur de parametres\n");
  return -1;
 }
 int n = atoi(argv[1]);
 int m = atoi(argv[2]);
 printf("M=%i main/coucou\n", m);
 printf("N=%i thread/et mon courroux\n", n);
 printf("\n");
 pthread_t myThread;
 int ret = pthread_create (
            & myThread, ((void *)0),
            affiche_n_fois_courroux, (void *) n
         );
 int i;
 for (i = 0; i < m; i++) {
  printf("coucou\n");
  sleep(1);
 }
 printf("termine\n");
 pthread_cancel(myThread);
 return(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 if (argc < 3) {
  printf("erreur de parametres\n");
  return -1;
 }
 int n = atoi(argv[1]);
 int m = atoi(argv[2]);
 printf("M=%i main/coucou\n", m);
 printf("N=%i thread/et mon courroux\n", n);
 printf("\n");
 unsigned long myThread;
 int ret = pthread_create (
            & myThread, ((void *)0),
            affiche_n_fois_courroux, (void *) n
         );
 int i;
 for (i = 0; i < m; i++) {
  printf("coucou\n");
  sleep(1);
 }
 printf("termine\n");
 pthread_cancel(myThread);
 return(0);
}

variable_types: n int

variable_types: m int

variable_types:
myThread
unsigned long


decompiled_code:
int __fastcall runlength_flush(__int64 a1, int a2, int a3, int a4, int a5)
{
__int64 v5; // rsi
__int64 v6; // rax
unsigned int v7; // er8
__int64 v8; // rsi
int result; // eax
__int64 v10; // rsi
__int64 v11; // rax
bool v12; // cc
__int64 v13; // rcx
__int64 v14; // rdx
__int64 v15; // rsi
__int64 v16; // rsi
__int64 v17; // rsi

if ( a5 > 3 )
{
v16 = *(unsigned int *)(*(_QWORD *)(a1 + 8LL * a2) + 4LL * a4);
if ( a3 - 1 <= a4 )
result = printf("ff %d %d", v16, (unsigned int)a5);
else
result = printf("ff %d %d ", v16, (unsigned int)a5);
}
else
{
switch ( a5 )
{
case 3:
v10 = *(_QWORD *)(a1 + 8LL * a2);
v11 = a4 + 2LL;
v12 = a4 + 2 < a3 - 1;
v13 = *(unsigned int *)(v10 + 4 * v11);
v14 = *(unsigned int *)(v10 + 4 * v11 - 4);
v15 = *(unsigned int *)(v10 + 4 * v11 - 8);
if ( v12 )
result = printf("%d %d %d ", v15, v14, v13);
else
result = printf("%d %d %d", v15, v14, v13);
break;
case 2:
v5 = *(_QWORD *)(a1 + 8LL * a2);
v6 = a4 + 1LL;
v7 = *(_DWORD *)(v5 + 4 * v6);
v8 = *(unsigned int *)(v5 + 4 * v6 - 4);
if ( a4 + 1 >= a3 - 1 )
result = printf("%d %d", v8, v7);
else
result = printf("%d %d ", v8, v7);
break;
case 1:
v17 = *(unsigned int *)(*(_QWORD *)(a1 + 8LL * a2) + 4LL * a4);
if ( a3 - 1 <= a4 )
result = printf("%d", v17);
else
result = printf("%d ", v17);
break;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2, int a3, int a4, int a5)
{
__int64 v5; // rsi
__int64 v6; // rax
unsigned int v7; // er8
__int64 v8; // rsi
int result; // eax
__int64 v10; // rsi
__int64 v11; // rax
bool v12; // cc
__int64 v13; // rcx
__int64 v14; // rdx
__int64 v15; // rsi
__int64 v16; // rsi
__int64 v17; // rsi

if ( a5 > 3 )
{
v16 = *(unsigned int *)(*(_QWORD *)(a1 + 8LL * a2) + 4LL * a4);
if ( a3 - 1 <= a4 )
result = printf("ff %d %d", v16, (unsigned int)a5);
else
result = printf("ff %d %d ", v16, (unsigned int)a5);
}
else
{
switch ( a5 )
{
case 3:
v10 = *(_QWORD *)(a1 + 8LL * a2);
v11 = a4 + 2LL;
v12 = a4 + 2 < a3 - 1;
v13 = *(unsigned int *)(v10 + 4 * v11);
v14 = *(unsigned int *)(v10 + 4 * v11 - 4);
v15 = *(unsigned int *)(v10 + 4 * v11 - 8);
if ( v12 )
result = printf("%d %d %d ", v15, v14, v13);
else
result = printf("%d %d %d", v15, v14, v13);
break;
case 2:
v5 = *(_QWORD *)(a1 + 8LL * a2);
v6 = a4 + 1LL;
v7 = *(_DWORD *)(v5 + 4 * v6);
v8 = *(unsigned int *)(v5 + 4 * v6 - 4);
if ( a4 + 1 >= a3 - 1 )
result = printf("%d %d", v8, v7);
else
result = printf("%d %d ", v8, v7);
break;
case 1:
v17 = *(unsigned int *)(*(_QWORD *)(a1 + 8LL * a2) + 4LL * a4);
if ( a3 - 1 <= a4 )
result = printf("%d", v17);
else
result = printf("%d ", v17);
break;
}
}
return result;
}

original_code:
void runlength_flush(int **img_matrix, int row, int total_cols, int i, int count)
{
 if(count > 3){
  if(i < total_cols -1) {
   printf("ff %d %d ",img_matrix[row][i], count);
  } else {
   printf("ff %d %d",img_matrix[row][i], count);
  }
  return;
 }
 if(count == 3)
 {
  if(i + 2 < total_cols -1)
   printf("%d %d %d ",img_matrix[row][i], img_matrix[row][i+1], img_matrix[row][i+2]);
  else
   printf("%d %d %d",img_matrix[row][i], img_matrix[row][i+1], img_matrix[row][i+2]);
 }
 if(count == 2)
 {
  if(i + 1 < total_cols -1)
   printf("%d %d ",img_matrix[row][i], img_matrix[row][i+1]);
  else
   printf("%d %d",img_matrix[row][i], img_matrix[row][i+1]);
 }
 if(count == 1)
 {
  if(i < total_cols -1)
   printf("%d ",img_matrix[row][i]);
  else
   printf("%d",img_matrix[row][i]);
 }
}

variable_types: row int

variable_types: count int


original_code:
void runlength_row_process(int **img_matrix, int row, int cols)
{
 int i, count;
 for(i = 0; i < cols -1; i++)
 {
  count = 1;
  if(img_matrix[row][i] == img_matrix[row][i+1]) {
   while(img_matrix[row][i] == img_matrix[row][i+1] && cols > i +1)
   {
    count++;
    i++;
   }
   if(count > 3)runlength_flush(img_matrix, row, cols, i, count);
   if(count == 3) {
    i -= 2;
    runlength_flush(img_matrix, row, cols, i, 3);
    i+=2;
   }
   if(count == 2) {
    i -= 1;
    runlength_flush(img_matrix, row, cols, i, 2);
    i += 1;
   }
  } else runlength_flush(img_matrix, row, cols, i, count);
 }
 count = 1;
 if(img_matrix[row][cols-2] != img_matrix[row][cols-1])
  runlength_flush(img_matrix, row, cols, cols -1, count);
}

canonical_original_code:
void runlength_row_process(int **img_matrix, int row, int cols)
{
 int i; int count;
 for(i = 0; i < cols -1; i++)
 {
  count = 1;
  if(img_matrix[row][i] == img_matrix[row][i+1]) {
   while(img_matrix[row][i] == img_matrix[row][i+1] && cols > i +1)
   {
    count++;
    i++;
   }
   if(count > 3)runlength_flush(img_matrix, row, cols, i, count);
   if(count == 3) {
    i -= 2;
    runlength_flush(img_matrix, row, cols, i, 3);
    i+=2;
   }
   if(count == 2) {
    i -= 1;
    runlength_flush(img_matrix, row, cols, i, 2);
    i += 1;
   }
  } else runlength_flush(img_matrix, row, cols, i, count);
 }
 count = 1;
 if(img_matrix[row][cols-2] != img_matrix[row][cols-1])
  runlength_flush(img_matrix, row, cols, cols -1, count);
}

variable_types: cols int


decompiled_code:
void __fastcall runlength_compress(const char *a1)
{
FILE *v1; // rax
int v2; // ebx
char *v3; // rax
_QWORD *v4; // r12
_QWORD *v5; // rbp
char *v6; // r15
char *v7; // rbx
int v8; // er14
size_t v9; // r13
int v10; // er13
__int64 v11; // rbx
__int64 v12; // rdx
int i; // ebx
int v14; // esi
__int64 v15; // rbx
void *v16; // rdi
char v17; // [rsp+2h] [rbp-46h] BYREF
int v18; // [rsp+4h] [rbp-44h] BYREF
int v19; // [rsp+8h] [rbp-40h] BYREF
unsigned int v20; // [rsp+Ch] [rbp-3Ch] BYREF

v1 = fopen(a1, "r");
fp_in = v1;
if ( v1 )
{
_isoc99_fscanf(v1, "%s", &v17);
_isoc99_fscanf(fp_in, "%d %d", &v19, &v18);
_isoc99_fscanf(fp_in, "%d", &v20);
puts("P8");
printf("%d %d\n", (unsigned int)v19, (unsigned int)v18);
printf("%d\n", v20);
v2 = v18;
v3 = (char *)malloc(8LL * v18);
v4 = v3;
if ( v2 > 0 )
{
v5 = v3;
v6 = &v3[8 * v2];
v7 = v3;
v8 = v19;
v9 = 4LL * v19;
do
{
v7 += 8;
*((_QWORD *)v7 - 1) = malloc(v9);
}
while ( v6 != v7 );
v10 = 0;
while ( 1 )
{
v11 = 0LL;
if ( v8 > 0 )
{
do
{
v12 = *v5 + 4 * v11++;
_isoc99_fscanf(fp_in, "%d", v12);
}
while ( v19 > (int)v11 );
}
++v10;
++v5;
if ( v18 <= v10 )
break;
v8 = v19;
}
if ( v18 > 0 )
{
for ( i = 0; i < v18; ++i )
{
v14 = i;
runlength_row_process((__int64)v4, v14, v19);
putchar(10);
}
if ( v18 > 0 )
{
v15 = 0LL;
do
{
v16 = (void *)v4[v15++];
free(v16);
}
while ( v18 > (int)v15 );
}
}
}
fclose(fp_in);
free(v4);
}
else
{
printf("Falha ao abrir arquivo %s\n ", a1);
}
}

original_code:
void runlength_compress(char *filename_in)
{
 int i, j;
 int rows, cols, range;
 char img_magic_number[2];
 int **img_matrix;
 fp_in = fopen(filename_in, "r");
 if(!fp_in){
  printf("Falha ao abrir arquivo %s\n ", filename_in);
  return;
 }
 fscanf(fp_in, "%s", img_magic_number);
 fscanf(fp_in, "%d %d", &cols, &rows);
 fscanf(fp_in, "%d", &range);
 printf("%s\n", "P8");
 printf("%d %d\n", cols, rows);
 printf("%d\n", range);
 img_matrix = (int**) malloc(sizeof(int*) * rows);
 for(i = 0; i < rows; i++)
 {
  img_matrix[i] = (int*) malloc(sizeof(int) * cols);
 }
 for(i = 0; i < rows; i++)
 {
  for(j = 0; j < cols; j++)
  {
   fscanf(fp_in, "%d",&img_matrix[i][j]);
  }
 }
 for(i = 0; i < rows; i++) {
  runlength_row_process(img_matrix, i, cols);
  printf("\n");
 }
 for(i = 0; i < rows; i++)
 {
  free(img_matrix[i]);
 }
 fclose(fp_in);
 free(img_matrix);
}

canonical_original_code:
void runlength_compress(char *filename_in)
{
 int i; int j;
 int rows; int cols; int range;
 char img_magic_number[2];
 int **img_matrix;
 fp_in = fopen(filename_in, "r");
 if(!fp_in){
  printf("Falha ao abrir arquivo %s\n ", filename_in);
  return;
 }
 fscanf(fp_in, "%s", img_magic_number);
 fscanf(fp_in, "%d %d", &cols, &rows);
 fscanf(fp_in, "%d", &range);
 printf("%s\n", "P8");
 printf("%d %d\n", cols, rows);
 printf("%d\n", range);
 img_matrix = (int **) malloc(sizeof(int *) * rows);
 for(i = 0; i < rows; i++)
 {
  img_matrix[i] = (int *) malloc(sizeof(int) * cols);
 }
 for(i = 0; i < rows; i++)
 {
  for(j = 0; j < cols; j++)
  {
   fscanf(fp_in, "%d",&img_matrix[i][j]);
  }
 }
 for(i = 0; i < rows; i++) {
  runlength_row_process(img_matrix, i, cols);
  printf("\n");
 }
 for(i = 0; i < rows; i++)
 {
  free(img_matrix[i]);
 }
 fclose(fp_in);
 free(img_matrix);
}

variable_types: j int

variable_types: rows int

variable_types: range int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *i; // rbp
char *v4; // rax
char *v5; // rax

for ( i = (char *)readLine(stdin); strcmp(i, "sair"); i = (char *)readLine(stdin) )
{
if ( !memcmp(i, "compactar run-length", 0x14uLL) )
{
strtok(i, " ");
strtok(0LL, " ");
v4 = strtok(0LL, " ");
runlength_compress(v4);
}
if ( !memcmp(i, "descompactar run-length", 0x17uLL) )
{
strtok(i, " ");
strtok(0LL, " ");
v5 = strtok(0LL, " ");
runlength_decompress(v5);
}
free(i);
}
free(i);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *i; // rbp
char *v4; // rax
char *v5; // rax

for ( i = (char *)func0(stdin); strcmp(i, "sair"); i = (char *)func0(stdin) )
{
if ( !memcmp(i, "compactar run-length", 0x14uLL) )
{
strtok(i, " ");
strtok(0LL, " ");
v4 = strtok(0LL, " ");
runlength_compress(v4);
}
if ( !memcmp(i, "descompactar run-length", 0x17uLL) )
{
strtok(i, " ");
strtok(0LL, " ");
v5 = strtok(0LL, " ");
runlength_decompress(v5);
}
free(i);
}
free(i);
return 0;
}

original_code:
int main(int argc, char * argv[])
{
 char *option, *token;
 option = readLine(stdin);
 while(strcmp(option, "sair") != 0)
 {
  if(strncmp(option, "compactar run-length", 20) == 0)
  {
   token = strtok(option, " ");
   token = strtok(((void *)0), " ");
   token = strtok(((void *)0), " ");
   runlength_compress(token);
  }
  if(strncmp(option, "descompactar run-length", 23) == 0)
  {
   token = strtok(option, " ");
   token = strtok(((void *)0), " ");
   token = strtok(((void *)0), " ");
   runlength_decompress(token);
  }
  if(option != ((void *)0)) free(option);
  option = readLine(stdin);
 }
 free(option);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 char *option; char *token;
 option = readLine(stdin);
 while(strcmp(option, "sair") != 0)
 {
  if(strncmp(option, "compactar run-length", 20) == 0)
  {
   token = strtok(option, " ");
   token = strtok(((void *)0), " ");
   token = strtok(((void *)0), " ");
   runlength_compress(token);
  }
  if(strncmp(option, "descompactar run-length", 23) == 0)
  {
   token = strtok(option, " ");
   token = strtok(((void *)0), " ");
   token = strtok(((void *)0), " ");
   runlength_decompress(token);
  }
  if(option != ((void *)0)) free(option);
  option = readLine(stdin);
 }
 free(option);
 return 0;
}

variable_types:
option
char *

variable_types:
token
char *


decompiled_code:
int __fastcall runlength_decompress(const char *a1)
{
FILE *v1; // rax
int i; // ebx
char v4; // [rsp+2h] [rbp-26h] BYREF
int v5; // [rsp+4h] [rbp-24h] BYREF
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fopen(a1, "r");
fp_in = v1;
if ( !v1 )
return printf("Falha ao abrir arquivo %s\n ", a1);
_isoc99_fscanf(v1, "%s", &v4);
_isoc99_fscanf(fp_in, "%d %d", &v6, &v5);
_isoc99_fscanf(fp_in, "%d", &v7);
puts("P2");
printf("%d %d\n", v6, (unsigned int)v5);
printf("%d\n", v7);
if ( (unsigned __int8)fgetc(fp_in) != 10 )
fseek(fp_in, -1LL, 1);
if ( v5 > 0 )
{
for ( i = 0; i < v5; ++i )
{
runlength_decompress_process_row(fp_in);
putchar(10);
}
}
return fclose(fp_in);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
FILE *v1; // rax
int i; // ebx
char v4; // [rsp+2h] [rbp-26h] BYREF
int v5; // [rsp+4h] [rbp-24h] BYREF
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = fopen(a1, "r");
fp_in = v1;
if ( !v1 )
return printf("Falha ao abrir arquivo %s\n ", a1);
_isoc99_fscanf(v1, "%s", &v4);
_isoc99_fscanf(fp_in, "%d %d", &v6, &v5);
_isoc99_fscanf(fp_in, "%d", &v7);
puts("P2");
printf("%d %d\n", v6, (unsigned int)v5);
printf("%d\n", v7);
if ( (unsigned __int8)fgetc(fp_in) != 10 )
fseek(fp_in, -1LL, 1);
if ( v5 > 0 )
{
for ( i = 0; i < v5; ++i )
{
func0(fp_in);
putchar(10);
}
}
return fclose(fp_in);
}

original_code:
void runlength_decompress(char *filename_in)
{
 int i, rows, cols, range;
 char img_magic_number[2];
 char ch;
 fp_in = fopen(filename_in, "r");
 if(!fp_in){
  printf("Falha ao abrir arquivo %s\n ", filename_in);
  return;
 }
 fscanf(fp_in, "%s", img_magic_number);
 fscanf(fp_in, "%d %d", &cols, &rows);
 fscanf(fp_in, "%d", &range);
 printf("%s\n", "P2");
 printf("%d %d\n", cols, rows);
 printf("%d\n", range);
 ch = fgetc(fp_in);
 if(ch != '\n')fseek(fp_in, -1, 1);
 for(i = 0; i < rows; i++)
 {
  runlength_decompress_process_row(fp_in, i);
  printf("\n");
 }
 fclose(fp_in);
}

canonical_original_code:
void runlength_decompress(char *filename_in)
{
 int i; int rows; int cols; int range;
 char img_magic_number[2];
 char ch;
 fp_in = fopen(filename_in, "r");
 if(!fp_in){
  printf("Falha ao abrir arquivo %s\n ", filename_in);
  return;
 }
 fscanf(fp_in, "%s", img_magic_number);
 fscanf(fp_in, "%d %d", &cols, &rows);
 fscanf(fp_in, "%d", &range);
 printf("%s\n", "P2");
 printf("%d %d\n", cols, rows);
 printf("%d\n", range);
 ch = fgetc(fp_in);
 if(ch != '\n')fseek(fp_in, -1, 1);
 for(i = 0; i < rows; i++)
 {
  runlength_decompress_process_row(fp_in, i);
  printf("\n");
 }
 fclose(fp_in);
}

variable_types: i int

variable_types: ch char


processArgs

repo: lcayton/bbtree

decompiled_code:
double __fastcall processArgs(int a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // ebx
_BYTE *v4; // rax
double result; // xmm0_8
__int64 v6; // rax

if ( a1 <= 1 )
{
puts(
"usage:\n"
" testBBT -f dataFile -q queryFile -n numPts -m numQueries -d dim [-e epsilon] [-b bregdiv] [-o outFile] [-s bucket"
"size] [-k numNeighbors] ");
printf(
"\n"
"\t where bregdiv = %d for l_2^2 (default) \n"
"\t bregdiv = %d for KL \n"
"\t bregdiv = %d for exponential (conj to KL) \n"
"\t bregdiv = %d for Itakura-Saito\n"
"\n",
0LL,
1LL,
2LL,
3LL);
exit(0);
}
v2 = (_QWORD *)(a2 + 8);
v3 = 2;
do
{
v4 = (_BYTE *)*v2;
if ( *(_BYTE *)*v2 != 45 )
{
LABEL_4:
fwrite("unrecognized option.. exiting \n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
if ( v4[1] != 100 || v4[2] )
{
if ( v4[1] != 110 || v4[2] )
{
if ( v4[1] != 109 || v4[2] )
{
if ( v4[1] != 102 || v4[2] )
{
if ( v4[1] != 113 || v4[2] )
{
if ( v4[1] != 115 || v4[2] )
{
if ( v4[1] != 111 || v4[2] )
{
if ( v4[1] != 101 || v4[2] )
{
if ( v4[1] != 98 || v4[2] )
{
if ( v4[1] != 107 || v4[2] )
goto LABEL_4;
k = atoi(*(const char **)(a2 + 8LL * v3));
}
else
{
divChoice = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
eps = atof(*(const char **)(a2 + 8LL * v3));
}
}
else
{
v6 = *(_QWORD *)(a2 + 8LL * v3);
isOutfile = 1;
outfile = v6;
}
}
else
{
bucketSize = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
queryfile = *(_QWORD *)(a2 + 8LL * v3);
}
}
else
{
datafile = *(_QWORD *)(a2 + 8LL * v3);
}
}
else
{
m = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
n = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
d = atoi(*(const char **)(a2 + 8LL * v3));
}
v3 += 2;
v2 += 2;
}
while ( v3 != ((a1 - 2) & 0xFFFFFFFE) + 4 );
if ( n == -1 || m == -1 || d == -1 || !datafile || !queryfile )
{
fwrite("more arguments needed.. exiting \n", 1uLL, 0x21uLL, stderr);
exit(1);
}
if ( (unsigned int)divChoice > 1 )
{
fwrite("not a legit bregman divergence.. exiting \n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
result = 0.0;
if ( *(double *)&eps < 0.0 )
{
fwrite("epsilon must be >= 0.. exiting \n", 1uLL, 0x20uLL, stderr);
exit(1);
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(int a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // ebx
_BYTE *v4; // rax
double result; // xmm0_8
__int64 v6; // rax

if ( a1 <= 1 )
{
puts(
"usage:\n"
" testBBT -f dataFile -q queryFile -n numPts -m numQueries -d dim [-e epsilon] [-b bregdiv] [-o outFile] [-s bucket"
"size] [-k numNeighbors] ");
printf(
"\n"
"\t where bregdiv = %d for l_2^2 (default) \n"
"\t bregdiv = %d for KL \n"
"\t bregdiv = %d for exponential (conj to KL) \n"
"\t bregdiv = %d for Itakura-Saito\n"
"\n",
0LL,
1LL,
2LL,
3LL);
exit(0);
}
v2 = (_QWORD *)(a2 + 8);
v3 = 2;
do
{
v4 = (_BYTE *)*v2;
if ( *(_BYTE *)*v2 != 45 )
{
LABEL_4:
fwrite("unrecognized option.. exiting \n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
if ( v4[1] != 100 || v4[2] )
{
if ( v4[1] != 110 || v4[2] )
{
if ( v4[1] != 109 || v4[2] )
{
if ( v4[1] != 102 || v4[2] )
{
if ( v4[1] != 113 || v4[2] )
{
if ( v4[1] != 115 || v4[2] )
{
if ( v4[1] != 111 || v4[2] )
{
if ( v4[1] != 101 || v4[2] )
{
if ( v4[1] != 98 || v4[2] )
{
if ( v4[1] != 107 || v4[2] )
goto LABEL_4;
k = atoi(*(const char **)(a2 + 8LL * v3));
}
else
{
divChoice = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
eps = atof(*(const char **)(a2 + 8LL * v3));
}
}
else
{
v6 = *(_QWORD *)(a2 + 8LL * v3);
isOutfile = 1;
outfile = v6;
}
}
else
{
bucketSize = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
queryfile = *(_QWORD *)(a2 + 8LL * v3);
}
}
else
{
datafile = *(_QWORD *)(a2 + 8LL * v3);
}
}
else
{
m = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
n = atoi(*(const char **)(a2 + 8LL * v3));
}
}
else
{
d = atoi(*(const char **)(a2 + 8LL * v3));
}
v3 += 2;
v2 += 2;
}
while ( v3 != ((a1 - 2) & 0xFFFFFFFE) + 4 );
if ( n == -1 || m == -1 || d == -1 || !datafile || !queryfile )
{
fwrite("more arguments needed.. exiting \n", 1uLL, 0x21uLL, stderr);
exit(1);
}
if ( (unsigned int)divChoice > 1 )
{
fwrite("not a legit bregman divergence.. exiting \n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
result = 0.0;
if ( *(double *)&eps < 0.0 )
{
fwrite("epsilon must be >= 0.. exiting \n", 1uLL, 0x20uLL, stderr);
exit(1);
}
return result;
}

original_code:
void processArgs(int argc, char**argv){
  int i=1;
  if(argc <= 1){
    printf("usage:\n testBBT -f dataFile -q queryFile -n numPts -m numQueries -d dim [-e epsilon] [-b bregdiv] [-o outFile] [-s bucketsize] [-k numNeighbors] \n");
    printf("\n\t where bregdiv = %d for l_2^2 (default) \n\t       bregdiv = %d for KL \n\t       bregdiv = %d for exponential (conj to KL) \n\t       bregdiv = %d for Itakura-Saito\n\n", 0, 1,2,3);
    exit(0);
  }
  while(i<argc){
    if(!strcmp(argv[i], "-d"))
      d = atoi(argv[++i]);
    else if(!strcmp(argv[i], "-n"))
      n=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-m"))
      m=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-f"))
      datafile = argv[++i];
    else if(!strcmp(argv[i], "-q"))
      queryfile= argv[++i];
    else if(!strcmp(argv[i], "-s"))
      bucketSize=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-o")){
      outfile= argv[++i];
      isOutfile=1;
    }
    else if(!strcmp(argv[i], "-e"))
      eps=atof(argv[++i]);
    else if(!strcmp(argv[i], "-b"))
      divChoice=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-k"))
      k=atoi(argv[++i]);
    else{
      fprintf(stderr,"unrecognized option.. exiting \n");
      exit(1);
    }
    i++;
  }
  if(n==-1 || m==-1 || d==-1 || datafile==((void *)0) || queryfile==((void *)0)){
    fprintf(stderr,"more arguments needed.. exiting \n");
    exit(1);
  }
  if(divChoice!=0 && divChoice!=1){
    fprintf(stderr,"not a legit bregman divergence.. exiting \n");
    exit(1);
  }
  if(eps<0){
    fprintf(stderr,"epsilon must be >= 0.. exiting \n");
    exit(1);
  }
}

canonical_original_code:
void processArgs(int argc, char **argv){
  int i = 1;
  if(argc <= 1){
    printf("usage:\n testBBT -f dataFile -q queryFile -n numPts -m numQueries -d dim [-e epsilon] [-b bregdiv] [-o outFile] [-s bucketsize] [-k numNeighbors] \n");
    printf("\n\t where bregdiv = %d for l_2^2 (default) \n\t       bregdiv = %d for KL \n\t       bregdiv = %d for exponential (conj to KL) \n\t       bregdiv = %d for Itakura-Saito\n\n", 0, 1,2,3);
    exit(0);
  }
  while(i<argc){
    if(!strcmp(argv[i], "-d"))
      d = atoi(argv[++i]);
    else if(!strcmp(argv[i], "-n"))
      n=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-m"))
      m=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-f"))
      datafile = argv[++i];
    else if(!strcmp(argv[i], "-q"))
      queryfile= argv[++i];
    else if(!strcmp(argv[i], "-s"))
      bucketSize=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-o")){
      outfile= argv[++i];
      isOutfile=1;
    }
    else if(!strcmp(argv[i], "-e"))
      eps=atof(argv[++i]);
    else if(!strcmp(argv[i], "-b"))
      divChoice=atoi(argv[++i]);
    else if(!strcmp(argv[i], "-k"))
      k=atoi(argv[++i]);
    else{
      fprintf(stderr,"unrecognized option.. exiting \n");
      exit(1);
    }
    i++;
  }
  if(n==-1 || m==-1 || d==-1 || datafile==((void *)0) || queryfile==((void *)0)){
    fprintf(stderr,"more arguments needed.. exiting \n");
    exit(1);
  }
  if(divChoice!=0 && divChoice!=1){
    fprintf(stderr,"not a legit bregman divergence.. exiting \n");
    exit(1);
  }
  if(eps<0){
    fprintf(stderr,"epsilon must be >= 0.. exiting \n");
    exit(1);
  }
}

variable_types: argc int

variable_types:
argv
char **


timediff

decompiled_code:
double __fastcall timediff(int a1, int a2, int a3, int a4)
{
return (double)a4 / 1000000.0 + (double)a3 - (double)a1 - (double)a2 / 1000000.0;
}

canonical_decompiled_code:
double __fastcall func1(int a1, int a2, int a3, int a4)
{
return (double)a4 / 1000000.0 + (double)a3 - (double)a1 - (double)a2 / 1000000.0;
}

original_code:
double timediff(struct timeval start, struct timeval end){
  return (double)(end.tv_sec+end.tv_usec/1e6 - start.tv_sec - start.tv_usec/1e6);
}

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

variable_types:
end
struct timeval

return_type: double