main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er13
unsigned int v4; // er14
size_t v5; // rbp
_DWORD *v6; // rbx
unsigned int *v7; // r12
key_t v8; // eax
int v9; // eax
int v10; // ebp
unsigned int v11; // eax
_DWORD *v12; // rcx
int v13; // er15
unsigned int v14; // er14
_DWORD *v15; // rax
__int64 v16; // rdx
__int64 v17; // rsi
__int64 v18; // rax

v3 = atoi(argv[1]);
init_resources(&numTypes, &claim);
allocated = calloc((unsigned int)numTypes, 4uLL);
if ( !allocated )
{
perror("calloc allocated");
cleanUp(0, 0LL, 0LL);
exit(1);
}
srand(v3);
randomInit((__int64)claim);
v4 = numTypes;
v5 = 4LL * (unsigned int)(numTypes - 1) + 8;
v6 = malloc(v5);
if ( !v6 )
{
printf("Error allocating message buffer of size %d.", v4);
cleanUp(1, 0LL, 0LL);
exit(1);
}
v7 = (unsigned int *)malloc(v5);
if ( !v7 )
{
printf("Error allocating message buffer of size %d.", v4);
cleanUp(2, v6, 0LL);
exit(1);
}
bankqid = msgget(-262926940, 420);
if ( bankqid == -1 )
{
printf("Cannot get the banker's address");
cleanUp(3, v6, v7);
exit(1);
}
v8 = ftok("initial.data", v3);
v9 = msgget(v8, 1956);
myqid = v9 == -1;
if ( v9 == -1 )
{
perror("Cannot create an exclusive message queue exiting");
cleanUp(3, v6, v7);
exit(1);
}
v10 = 1000 * v3;
*(_QWORD *)v6 = 3LL;
v6[2] = v3;
v6[5] = 0;
while ( 1 )
{
v6[3] = ++v10;
memcpy(v6 + 6, claim, 4LL * (unsigned int)numTypes);
printf("Sent message to queueID %u:\n", (unsigned int)bankqid);
display_msg(v6, (unsigned int)numTypes);
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) == -1 )
{
printf("Error sending message");
cleanUp(4, v6, v7);
exit(1);
}
if ( msgrcv(myqid, v7, 4LL * (unsigned int)(numTypes - 1), 0LL, 0) == -1 )
{
perror("Error recieving message");
cleanUp(4, v6, v7);
exit(1);
}
printf("Received message at queueID %u:\n", (unsigned int)myqid);
display_msg(v6, (unsigned int)numTypes);
if ( (_DWORD)numTypes )
{
v11 = 0;
do
{
v12 = (char *)claim + 4 * v11;
if ( *v12 )
--*v12;
++v11;
}
while ( (unsigned int)numTypes > v11 );
}
if ( *(_QWORD *)v7 != 10LL )
break;
if ( v6[3] > 0xAu )
{
v13 = 11;
goto LABEL_44;
}
}
v13 = 11;
LABEL_44:
*(_QWORD *)v6 = 1LL;
randomInit((__int64)(v6 + 6));
v14 = 0;
while ( 1 )
{
v6[3] = ++v10;
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) == -1 )
{
printf("Error sending message");
cleanUp(4, v6, v7);
exit(1);
}
printf("Sent message to queueID %u:\n", (unsigned int)bankqid);
display_msg(v6, (unsigned int)numTypes);
if ( msgrcv(myqid, v7, 4LL * (unsigned int)(numTypes - 1), 0LL, 0) == -1 )
{
printf("Error recieving message");
cleanUp(4, v6, v7);
exit(1);
}
v17 = v7[3];
if ( (_DWORD)v17 != v10 )
{
printf("Banker error their banker serial# %d client serial# \n", v17);
cleanUp(4, v6, v7);
exit(1);
}
printf("Received message at queueID %u:\n", (unsigned int)bankqid);
display_msg(v6, (unsigned int)numTypes);
v18 = *(_QWORD *)v7;
if ( *(_QWORD *)v7 == 6LL )
{
if ( (_DWORD)numTypes )
{
v15 = v6 + 6;
v16 = (__int64)&v6[(unsigned int)(numTypes - 1) + 7];
do
--*v15++;
while ( (_DWORD *)v16 != v15 );
}
++v14;
goto LABEL_34;
}
if ( *(__int64 *)v7 > 6 )
{
if ( v18 == 12 )
goto LABEL_42;
}
else
{
if ( v18 == 4 )
{
LABEL_43:
wait();
if ( !--v13 )
{
*(_QWORD *)v6 = 11LL;
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) != -1 )
{
cleanUp(-1, v6, v7);
exit(0);
}
printf("Error sending message");
cleanUp(4, v6, v7);
exit(1);
}
goto LABEL_44;
}
if ( v18 == 5 )
{
LABEL_42:
wait();
goto LABEL_34;
}
}
++v14;
LABEL_34:
if ( v14 > 9 )
goto LABEL_43;
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // er13
unsigned int v4; // er14
size_t v5; // rbp
_DWORD *v6; // rbx
unsigned int *v7; // r12
key_t v8; // eax
int v9; // eax
int v10; // ebp
unsigned int v11; // eax
_DWORD *v12; // rcx
int v13; // er15
unsigned int v14; // er14
_DWORD *v15; // rax
__int64 v16; // rdx
__int64 v17; // rsi
__int64 v18; // rax

v3 = atoi(argv[1]);
func11(&numTypes, &claim);
allocated = calloc((unsigned int)numTypes, 4uLL);
if ( !allocated )
{
perror("calloc allocated");
func10(0, 0LL, 0LL);
exit(1);
}
srand(v3);
func7((__int64)claim);
v4 = numTypes;
v5 = 4LL * (unsigned int)(numTypes - 1) + 8;
v6 = malloc(v5);
if ( !v6 )
{
printf("Error allocating message buffer of size %d.", v4);
func10(1, 0LL, 0LL);
exit(1);
}
v7 = (unsigned int *)malloc(v5);
if ( !v7 )
{
printf("Error allocating message buffer of size %d.", v4);
func10(2, v6, 0LL);
exit(1);
}
bankqid = msgget(-262926940, 420);
if ( bankqid == -1 )
{
printf("Cannot get the banker's address");
func10(3, v6, v7);
exit(1);
}
v8 = ftok("initial.data", v3);
v9 = msgget(v8, 1956);
myqid = v9 == -1;
if ( v9 == -1 )
{
perror("Cannot create an exclusive message queue exiting");
func10(3, v6, v7);
exit(1);
}
v10 = 1000 * v3;
*(_QWORD *)v6 = 3LL;
v6[2] = v3;
v6[5] = 0;
while ( 1 )
{
v6[3] = ++v10;
memcpy(v6 + 6, claim, 4LL * (unsigned int)numTypes);
printf("Sent message to queueID %u:\n", (unsigned int)bankqid);
func12(v6, (unsigned int)numTypes);
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) == -1 )
{
printf("Error sending message");
func10(4, v6, v7);
exit(1);
}
if ( msgrcv(myqid, v7, 4LL * (unsigned int)(numTypes - 1), 0LL, 0) == -1 )
{
perror("Error recieving message");
func10(4, v6, v7);
exit(1);
}
printf("Received message at queueID %u:\n", (unsigned int)myqid);
func12(v6, (unsigned int)numTypes);
if ( (_DWORD)numTypes )
{
v11 = 0;
do
{
v12 = (char *)claim + 4 * v11;
if ( *v12 )
--*v12;
++v11;
}
while ( (unsigned int)numTypes > v11 );
}
if ( *(_QWORD *)v7 != 10LL )
break;
if ( v6[3] > 0xAu )
{
v13 = 11;
goto LABEL_44;
}
}
v13 = 11;
LABEL_44:
*(_QWORD *)v6 = 1LL;
func7((__int64)(v6 + 6));
v14 = 0;
while ( 1 )
{
v6[3] = ++v10;
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) == -1 )
{
printf("Error sending message");
func10(4, v6, v7);
exit(1);
}
printf("Sent message to queueID %u:\n", (unsigned int)bankqid);
func12(v6, (unsigned int)numTypes);
if ( msgrcv(myqid, v7, 4LL * (unsigned int)(numTypes - 1), 0LL, 0) == -1 )
{
printf("Error recieving message");
func10(4, v6, v7);
exit(1);
}
v17 = v7[3];
if ( (_DWORD)v17 != v10 )
{
printf("Banker error their banker serial# %d client serial# \n", v17);
func10(4, v6, v7);
exit(1);
}
printf("Received message at queueID %u:\n", (unsigned int)bankqid);
func12(v6, (unsigned int)numTypes);
v18 = *(_QWORD *)v7;
if ( *(_QWORD *)v7 == 6LL )
{
if ( (_DWORD)numTypes )
{
v15 = v6 + 6;
v16 = (__int64)&v6[(unsigned int)(numTypes - 1) + 7];
do
--*v15++;
while ( (_DWORD *)v16 != v15 );
}
++v14;
goto LABEL_34;
}
if ( *(__int64 *)v7 > 6 )
{
if ( v18 == 12 )
goto LABEL_42;
}
else
{
if ( v18 == 4 )
{
LABEL_43:
func9();
if ( !--v13 )
{
*(_QWORD *)v6 = 11LL;
if ( msgsnd(bankqid, v6, 4LL * (unsigned int)(numTypes - 1), 0) != -1 )
{
func10(-1, v6, v7);
exit(0);
}
printf("Error sending message");
func10(4, v6, v7);
exit(1);
}
goto LABEL_44;
}
if ( v18 == 5 )
{
LABEL_42:
func9();
goto LABEL_34;
}
}
++v14;
LABEL_34:
if ( v14 > 9 )
goto LABEL_43;
}
}

original_code:
int main(int argc, char *argv[]){
        msgbuf_t * msgbuf = ((void *)0);
        msgbuf_t * respbuf = ((void *)0);
        unsigned int serial;
        key_t key;
        int id = atoi(argv[1]);
        unsigned int i,j;
        unsigned int attempts = 0;
        init_resources(&numTypes, &claim);
        allocated = (unsigned int *) calloc(numTypes,sizeof(unsigned int));
        if(allocated == ((void *)0)) {
         perror("calloc allocated");
  cleanUp(0, msgbuf, respbuf);
         exit(1);
        }
        srand(id);
        randomInit(claim);
        msgbuf = (msgbuf_t *) malloc((sizeof(msgbuf) + sizeof(int) * (numTypes - 1)));
        if(msgbuf == ((void *)0)) {
                printf("Error allocating message buffer of size %d.", numTypes);
                cleanUp(1, msgbuf, respbuf);
                exit(1);
        }
        respbuf = (msgbuf_t *) malloc((sizeof(msgbuf) + sizeof(int) * (numTypes - 1)));
        if(respbuf == ((void *)0)) {
                printf("Error allocating message buffer of size %d.", numTypes);
                cleanUp(2, msgbuf, respbuf);
                exit(1);
        }
     key = 0xF0540DA4;
        if((bankqid = msgget(key, 0644)) == -1) {
                printf("Cannot get the banker's address");
                cleanUp(3, msgbuf, respbuf);
                exit(1);
          }
        key = ftok("initial.data" , id);
        if(myqid = msgget(key,0644| 01000 | 02000) == -1){
                perror("Cannot create an exclusive message queue exiting");
                cleanUp(3, msgbuf, respbuf);
                exit(1);
        }
        serial = id * 1000;
        msgbuf->mtype = 3;
        msgbuf->request.sender = id;
        msgbuf->request.serialNum = serial;
        msgbuf->request.retAddr = myqid;
        do{
                serial++;
                msgbuf->request.serialNum = msgbuf->request.serialNum = serial;
                memcpy( msgbuf->request.resourceVector, claim, sizeof(unsigned int) * numTypes);
                printf("Sent message to queueID %u:\n", bankqid);
                display_msg(msgbuf, numTypes);
                if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                        printf("Error sending message");
                        cleanUp(4, msgbuf, respbuf);
                        exit(1);
                }
                if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                        perror("Error recieving message");
                        cleanUp(4, msgbuf, respbuf);
                        exit(1);
                }
                printf("Received message at queueID %u:\n", myqid);
                display_msg(msgbuf, numTypes);
                for(i = 0; i < numTypes; i++){
                        if(claim[i] > 0)
                                claim[i] = claim[i] - 1;
                }
        }while(respbuf->mtype == 10 && msgbuf->request.serialNum <= MAX_ITERATIONS );
        for(j = 0; j <= MAX_ITERATIONS; j++){
                msgbuf->mtype = 1;
                randomInit(msgbuf->request.resourceVector);
                do{
                        serial++;
                        msgbuf->request.serialNum = serial;
                        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                                printf("Error sending message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        printf("Sent message to queueID %u:\n", bankqid);
                        display_msg(msgbuf, numTypes);
                        if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                                printf("Error recieving message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if(respbuf->request.serialNum != serial){
                                printf("Banker error their banker serial# %d client serial# \n", respbuf->request.serialNum, serial);
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        printf("Received message at queueID %u:\n", bankqid);
                        display_msg(msgbuf, numTypes);
                        switch(respbuf->mtype){
                                case 4:
                                        attempts = MAX_ATTEMPTS;
                                        break;
                                case 6:
                                        for(i = 0; i < numTypes; i++){
                                                msgbuf->request.resourceVector[i] = msgbuf->request.resourceVector[i] - 1;
                                        }
                                        attempts++;
                                        break;
                                case 5:
                                case 12:
                                        wait();
                                        break;
                                default:
                                        attempts++;
                                        break;
                        }
                }while(attempts < MAX_ATTEMPTS);
                if(attempts < MAX_ATTEMPTS){
                        for(i = 0; i < numTypes; i++){
                                allocated[i] = allocated[i] + msgbuf->request.resourceVector[i];
                        }
                        wait();
                        msgbuf->mtype = 2;
                        randomRelease(msgbuf->request.resourceVector);
                        serial++;
                        msgbuf->request.serialNum = serial;
                        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                                printf("Error sending message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                                printf("Error recieving message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if(respbuf->request.inReply != serial){
                                printf("Banker error their banker serial# %d client serial# \n", respbuf->request.inReply, serial);
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if( respbuf->mtype == 8){
                                for(i = 0; i < numTypes; i++){
                                        allocated[i] = allocated[i] - msgbuf->request.resourceVector[i];
                                }
                                display_msg(msgbuf, numTypes);
                        }
                        else{
                                display_msg(msgbuf, numTypes);
                        }
                }
                wait();
                attempts = 0;
        }
        msgbuf->mtype = 11;
        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                printf("Error sending message");
                cleanUp(4, msgbuf, respbuf);
                exit(1);
        }
        cleanUp(-1, msgbuf, respbuf);
        exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]){
        struct msgbuf_t *msgbuf = ((void *)0);
        struct msgbuf_t *respbuf = ((void *)0);
        unsigned int serial;
        int key;
        int id = atoi(argv[1]);
        unsigned int i; unsigned int j;
        unsigned int attempts = 0;
        init_resources(&numTypes, &claim);
        allocated = (unsigned int *) calloc(numTypes,sizeof(unsigned int));
        if(allocated == ((void *)0)) {
         perror("calloc allocated");
  cleanUp(0, msgbuf, respbuf);
         exit(1);
        }
        srand(id);
        randomInit(claim);
        msgbuf = (struct msgbuf_t *) malloc((sizeof(msgbuf) + sizeof(int) * (numTypes - 1)));
        if(msgbuf == ((void *)0)) {
                printf("Error allocating message buffer of size %d.", numTypes);
                cleanUp(1, msgbuf, respbuf);
                exit(1);
        }
        respbuf = (struct msgbuf_t *) malloc((sizeof(msgbuf) + sizeof(int) * (numTypes - 1)));
        if(respbuf == ((void *)0)) {
                printf("Error allocating message buffer of size %d.", numTypes);
                cleanUp(2, msgbuf, respbuf);
                exit(1);
        }
     key = 0xF0540DA4;
        if((bankqid = msgget(key, 0644)) == -1) {
                printf("Cannot get the banker's address");
                cleanUp(3, msgbuf, respbuf);
                exit(1);
          }
        key = ftok("initial.data" , id);
        if(myqid = msgget(key,0644| 01000 | 02000) == -1){
                perror("Cannot create an exclusive message queue exiting");
                cleanUp(3, msgbuf, respbuf);
                exit(1);
        }
        serial = id * 1000;
        msgbuf->mtype = 3;
        msgbuf->request.sender = id;
        msgbuf->request.serialNum = serial;
        msgbuf->request.retAddr = myqid;
        do{
                serial++;
                msgbuf->request.serialNum = msgbuf->request.serialNum = serial;
                memcpy( msgbuf->request.resourceVector, claim, sizeof(unsigned int) * numTypes);
                printf("Sent message to queueID %u:\n", bankqid);
                display_msg(msgbuf, numTypes);
                if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                        printf("Error sending message");
                        cleanUp(4, msgbuf, respbuf);
                        exit(1);
                }
                if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                        perror("Error recieving message");
                        cleanUp(4, msgbuf, respbuf);
                        exit(1);
                }
                printf("Received message at queueID %u:\n", myqid);
                display_msg(msgbuf, numTypes);
                for(i = 0; i < numTypes; i++){
                        if(claim[i] > 0)
                                claim[i] = claim[i] - 1;
                }
        }while(respbuf->mtype == 10 && msgbuf->request.serialNum <= MAX_ITERATIONS );
        for(j = 0; j <= MAX_ITERATIONS; j++){
                msgbuf->mtype = 1;
                randomInit(msgbuf->request.resourceVector);
                do{
                        serial++;
                        msgbuf->request.serialNum = serial;
                        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                                printf("Error sending message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        printf("Sent message to queueID %u:\n", bankqid);
                        display_msg(msgbuf, numTypes);
                        if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                                printf("Error recieving message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if(respbuf->request.serialNum != serial){
                                printf("Banker error their banker serial# %d client serial# \n", respbuf->request.serialNum, serial);
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        printf("Received message at queueID %u:\n", bankqid);
                        display_msg(msgbuf, numTypes);
                        switch(respbuf->mtype){
                                case 4:
                                        attempts = MAX_ATTEMPTS;
                                        break;
                                case 6:
                                        for(i = 0; i < numTypes; i++){
                                                msgbuf->request.resourceVector[i] = msgbuf->request.resourceVector[i] - 1;
                                        }
                                        attempts++;
                                        break;
                                case 5:
                                case 12:
                                        wait();
                                        break;
                                default:
                                        attempts++;
                                        break;
                        }
                }while(attempts < MAX_ATTEMPTS);
                if(attempts < MAX_ATTEMPTS){
                        for(i = 0; i < numTypes; i++){
                                allocated[i] = allocated[i] + msgbuf->request.resourceVector[i];
                        }
                        wait();
                        msgbuf->mtype = 2;
                        randomRelease(msgbuf->request.resourceVector);
                        serial++;
                        msgbuf->request.serialNum = serial;
                        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                                printf("Error sending message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if (msgrcv(myqid, respbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0, 0) == -1){
                                printf("Error recieving message");
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if(respbuf->request.inReply != serial){
                                printf("Banker error their banker serial# %d client serial# \n", respbuf->request.inReply, serial);
                                cleanUp(4, msgbuf, respbuf);
                                exit(1);
                        }
                        if( respbuf->mtype == 8){
                                for(i = 0; i < numTypes; i++){
                                        allocated[i] = allocated[i] - msgbuf->request.resourceVector[i];
                                }
                                display_msg(msgbuf, numTypes);
                        }
                        else{
                                display_msg(msgbuf, numTypes);
                        }
                }
                wait();
                attempts = 0;
        }
        msgbuf->mtype = 11;
        if (msgsnd(bankqid, msgbuf, (sizeof(msgbuf) - sizeof(long) + sizeof(int) * (numTypes - 1)), 0) == -1){
                printf("Error sending message");
                cleanUp(4, msgbuf, respbuf);
                exit(1);
        }
        cleanUp(-1, msgbuf, respbuf);
        exit(0);
}

variable_types:
argv
char *[]

variable_types:
serial
unsigned int

variable_types: key int

variable_types: id int

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
attempts
unsigned int


destroy

repo: lazygunner/transfer

decompiled_code:
void __noreturn destroy()
{
if ( t_send )
((void (*)(void))cancel_thread)();
if ( t_handle[0] )
{
((void (*)(void))cancel_thread)();
cancel_thread(t_handle[1]);
cancel_thread(t_handle[2]);
}
if ( t_recv )
cancel_thread(t_recv);
if ( t_heartbeat )
cancel_thread(t_heartbeat);
cancel_thread(t_main);
destroy_msg_q((unsigned int)g_recv_qid);
destroy_msg_q((unsigned int)g_data_qid);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
if ( t_send )
((void (*)(void))cancel_thread)();
if ( t_handle[0] )
{
((void (*)(void))cancel_thread)();
cancel_thread(t_handle[1]);
cancel_thread(t_handle[2]);
}
if ( t_recv )
cancel_thread(t_recv);
if ( t_heartbeat )
cancel_thread(t_heartbeat);
cancel_thread(t_main);
destroy_msg_q((unsigned int)g_recv_qid);
destroy_msg_q((unsigned int)g_data_qid);
exit(0);
}

original_code:
void destroy(int sig)
{
    int i = 0;
    if(t_send)
        cancel_thread(t_send);
    if(t_handle[0])
        for(; i < 3; i++)
            cancel_thread(t_handle[i]);
    if(t_recv)
        cancel_thread(t_recv);
    if(t_heartbeat)
        cancel_thread(t_heartbeat);
    cancel_thread(t_main);
    destroy_msg_q(g_recv_qid);
    destroy_msg_q(g_data_qid);
    exit(0);
}

variable_types: sig int

variable_types: i int

return_type: void


decompiled_code:
__int64 main_thread()
{
int v0; // eax
__int64 v1; // r12
int v2; // ebp
__int64 v4; // rbx
int v5; // eax
__int64 v6; // r13
_DWORD *v7; // rcx
__int64 v8; // rax
char *v9; // rsi
int v10; // eax
unsigned int v11; // eax
int v12; // eax
char v13; // al
unsigned __int8 v14; // r13
char *v15; // rbp
char *v16; // rsi
unsigned __int16 v17; // bp
unsigned __int8 v18; // r13
char *v19; // r14
char *v20; // rbp
__int16 *v21; // r13
unsigned __int16 v22; // ax
char v23; // al
__int64 v24; // rbp
struct timeval v25; // [rsp+0h] [rbp-648h] BYREF
struct timeval tv; // [rsp+10h] [rbp-638h] BYREF
__int64 v27; // [rsp+28h] [rbp-620h] BYREF
char dest[256]; // [rsp+30h] [rbp-618h] BYREF
char buf[1024]; // [rsp+130h] [rbp-518h] BYREF
char v30[8]; // [rsp+530h] [rbp-118h] BYREF
__int64 v31; // [rsp+538h] [rbp-110h]
__int64 v32; // [rsp+548h] [rbp-100h] BYREF
char v33[40]; // [rsp+550h] [rbp-F8h] BYREF
int v34; // [rsp+578h] [rbp-D0h]
int fd; // [rsp+57Ch] [rbp-CCh]
int v36; // [rsp+580h] [rbp-C8h]
unsigned __int8 v37; // [rsp+584h] [rbp-C4h]
char *v38; // [rsp+588h] [rbp-C0h]
char v39[60]; // [rsp+590h] [rbp-B8h] BYREF
unsigned int v40; // [rsp+5CCh] [rbp-7Ch]
int v41; // [rsp+5D0h] [rbp-78h]
__int64 v42; // [rsp+5E0h] [rbp-68h]
int (__fastcall *v43)(char *); // [rsp+600h] [rbp-48h]

v32 = 0LL;
init_socket(v33, &g_server_ip, g_control_port, g_data_port, &g_train_no);
frame_heartbeat_init(v39);
v0 = init_file_desc();
if ( v0 )
{
v1 = v0;
v42 = v0;
v40 = g_recv_qid;
v41 = g_data_qid;
create_thread(&t_send, &send_thread, v33);
create_thread(t_handle, &read_thread, v33);
create_thread((unsigned int)((_DWORD)t_handle + 8), &read_thread, v33);
create_thread((unsigned int)((_DWORD)t_handle + 16), &read_thread, v33);
v2 = 20;
while ( 1 )
{
if ( (int)recv_msg_q(v40, v30, 24LL, 1LL, 2048LL) >= 0 )
{
v4 = v31;
switch ( v34 )
{
case 65536:
goto LABEL_5;
case 65537:
goto LABEL_9;
case 65538:
goto LABEL_33;
case 65539:
goto LABEL_42;
case 65540:
goto LABEL_52;
case 65541:
goto LABEL_81;
case 65542:
goto LABEL_94;
case 65543:
goto LABEL_100;
default:
goto LABEL_95;
}
}
switch ( v34 )
{
case 65536:
v4 = 0LL;
LABEL_5:
if ( v43(v33) < 0 )
{
puts("[client]connect to server failed!");
sleep(1u);
}
else
{
v34 = 65537;
create_thread(&t_recv, &receive_handler, v33);
}
goto LABEL_95;
case 65537:
v4 = 0LL;
LABEL_9:
if ( fd < 0 )
{
t_log("connection socket lost!");
v6 = 0LL;
goto LABEL_18;
}
if ( (int)frame_header_init(1LL, 1LL, &v32) < 0 )
{
t_log("frame init failed!");
v6 = 0LL;
if ( !v32 )
goto LABEL_95;
}
else
{
v5 = t_malloc((unsigned int)v37 + 1);
v6 = v5;
if ( !v5 )
{
t_log("malloc login_buf frame error!");
goto LABEL_19;
}
*(_BYTE *)v5 = v37;
v7 = (_DWORD *)(v5 + 1LL);
v8 = v37;
v9 = v38;
if ( v37 >= 8u )
{
*(_QWORD *)(v6 + 1) = *(_QWORD *)v38;
*(_QWORD *)((char *)v7 + v8 - 8) = *(_QWORD *)&v9[v8 - 8];
qmemcpy(
(void *)((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v9 - ((char *)v7 - ((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v8 + (_DWORD)v7 - (((_DWORD)v6 + 9) & 0xFFFFFFF8)) >> 3));
}
else if ( (v37 & 4) != 0 )
{
*v7 = *(_DWORD *)v38;
*(_DWORD *)((char *)v7 + v8 - 4) = *(_DWORD *)&v9[v8 - 4];
}
else if ( v37 )
{
*(_BYTE *)v7 = *v38;
if ( (v8 & 2) != 0 )
*(_WORD *)((char *)v7 + v8 - 2) = *(_WORD *)&v9[v8 - 2];
}
v10 = frame_build(v32, v6, (unsigned int)v37 + 1, buf);
if ( v10 != (unsigned int)send(fd, buf, v10, 0) )
{
t_log("[login]lost connection!");
LABEL_18:
v34 = 65543;
LABEL_19:
if ( !v32 )
goto LABEL_21;
goto LABEL_20;
}
t_log("[client]login sent");
v34 = 65538;
if ( !v32 )
{
LABEL_22:
t_free(v6);
goto LABEL_95;
}
}
LABEL_20:
((void (*)(void))t_free)();
v32 = 0LL;
LABEL_21:
if ( v6 )
goto LABEL_22;
LABEL_95:
if ( v4 )
LABEL_96:
t_free(v4);
break;
case 65538:
v4 = 0LL;
LABEL_33:
if ( v4 )
{
if ( *(_WORD *)(v4 + 2) == 513 )
{
v34 = 65539;
t_log("[client]login complete!");
create_thread(&t_heartbeat, &send_heartbeat_thread, v33);
}
else
{
t_log("[login_sent]receive error");
}
goto LABEL_36;
}
if ( v2 )
goto LABEL_62;
v34 = 65543;
v2 = 20;
continue;
case 65539:
v4 = 0LL;
LABEL_42:
if ( (int)frame_header_init(1LL, 4LL, &v32) < 0 )
{
t_log("frame init failed!");
}
else
{
v11 = get_file_info(&g_file_path, &v27);
v12 = frame_build(v32, v27, v11, buf);
if ( v12 == (unsigned int)send(fd, buf, v12, 0) )
{
t_log("[client]file info sent");
v34 = 65540;
}
else
{
t_log("[login]send file info failed.");
t_log("[client]re conn");
v34 = 65543;
}
}
goto LABEL_45;
case 65540:
v4 = 0LL;
LABEL_52:
if ( v4 )
{
if ( *(_BYTE *)(v4 + 2) != 1 )
{
t_log("receive type error");
LABEL_36:
v2 = 20;
goto LABEL_96;
}
v13 = *(_BYTE *)(v4 + 3);
if ( v13 == 3 )
{
t_log("[server]continue transmit");
v18 = *(_BYTE *)(v4 + 6);
v19 = (char *)(v4 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v20 = &dest[strlen(dest)];
if ( v18 >= 8u )
{
*(_QWORD *)v20 = *(_QWORD *)(v4 + 7);
*(_QWORD *)&v20[v18 - 8] = *(_QWORD *)&v19[v18 - 8];
qmemcpy(
(void *)((unsigned __int64)(v20 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v19 - &v20[-((unsigned __int64)(v20 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * ((v18 + (unsigned int)v20 - (((_DWORD)v20 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v18 & 4) != 0 )
{
*(_DWORD *)v20 = *(_DWORD *)v19;
*(_DWORD *)&v20[v18 - 4] = *(_DWORD *)&v19[v18 - 4];
}
else if ( v18 )
{
*v20 = *v19;
if ( (v18 & 2) != 0 )
*(_WORD *)&v20[v18 - 2] = *(_WORD *)&v19[v18 - 2];
}
v21 = (__int16 *)&v19[v18];
v22 = _bswap_16(*v21);
if ( (int)set_file_desc(v1, v22, dest) < 0 )
{
t_log("[re_transmit]file error!");
v34 = 65540;
}
else if ( (int)handle_re_transimit_frame(v1, v21 + 1) < 0 )
{
clear_file_desc(v1);
v34 = 65540;
}
else
{
v34 = 65541;
}
goto LABEL_36;
}
if ( v13 == 5 )
{
v14 = *(_BYTE *)(v4 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v15 = &dest[strlen(dest)];
v16 = (char *)(v4 + 8);
if ( v14 >= 8u )
{
*(_QWORD *)v15 = *(_QWORD *)(v4 + 8);
*(_QWORD *)&v15[v14 - 8] = *(_QWORD *)(v4 + v14);
qmemcpy(
(void *)((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v16 - &v15[-((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * ((v14 + (unsigned int)v15 - (((_DWORD)v15 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v14 & 4) != 0 )
{
*(_DWORD *)v15 = *(_DWORD *)v16;
*(_DWORD *)&v15[v14 - 4] = *(_DWORD *)&v16[v14 - 4];
}
else if ( v14 )
{
*v15 = *v16;
if ( (v14 & 2) != 0 )
*(_WORD *)&v15[v14 - 2] = *(_WORD *)&v16[v14 - 2];
}
v17 = _bswap_16(*(_WORD *)(v4 + v14 + 8));
t_log("[server]download file: %s ", dest);
set_file_desc(v1, v17, dest);
init_send_list(v1);
t_log("[client]sending file...");
gettimeofday(&tv, 0LL);
v34 = 65541;
goto LABEL_36;
}
goto LABEL_85;
}
if ( v2 )
{
LABEL_62:
--v2;
sleep(1u);
}
else
{
clear_file_desc(v42);
v34 = 65540;
t_log("[client]timeout! all file tranfer completed!");
v2 = 20;
}
continue;
case 65541:
v4 = 0LL;
LABEL_81:
if ( v4 )
{
if ( *(_BYTE *)(v4 + 2) == 1 )
{
v23 = *(_BYTE *)(v4 + 3);
if ( v23 == 3 )
{
t_log("[server]retransmit");
if ( (int)handle_re_transimit_frame(v42, v4 + *(unsigned __int8 *)(v4 + 6) + 9) < 0 )
{
clear_file_desc(v42);
v34 = 65540;
}
goto LABEL_36;
}
if ( v23 == 8 )
{
gettimeofday(&v25, 0LL);
t_log(
"time_used = %f s",
(float)((float)(LODWORD(v25.tv_usec)
+ 1000000 * (LODWORD(v25.tv_sec) - LODWORD(tv.tv_sec))
- LODWORD(tv.tv_usec))
/ 1000000.0));
t_log("[server]transfer finished");
v24 = v42;
if ( *(_WORD *)(v24 + 12) == _bswap_16(*(_WORD *)(v4 + 6)) )
{
clear_file_desc(v24);
v34 = 65540;
sleep(2u);
}
else
{
puts("[finish]frame file id error!");
}
goto LABEL_36;
}
LABEL_85:
v2 = 20;
goto LABEL_96;
}
t_log("[client]receive error");
v2 = 20;
LABEL_45:
if ( v32 )
{
((void (*)(void))t_free)();
v32 = 0LL;
}
if ( v27 )
{
((void (*)(void))t_free)();
v27 = 0LL;
}
goto LABEL_95;
}
sleep(1u);
continue;
case 65542:
v4 = 0LL;
LABEL_94:
t_log("[state]transfer finished");
v34 = 65540;
goto LABEL_95;
case 65543:
v4 = 0LL;
LABEL_100:
t_log("[state]connection lost");
v34 = 0x10000;
sleep(3u);
shutdown(fd, 2);
fd = 0;
shutdown(v36, 2);
v36 = 0;
clear_file_desc(v42);
goto LABEL_95;
default:
continue;
}
}
}
return t_log("malloc f_desc frame error!");
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
__int64 v1; // r12
int v2; // ebp
__int64 v4; // rbx
int v5; // eax
__int64 v6; // r13
_DWORD *v7; // rcx
__int64 v8; // rax
char *v9; // rsi
int v10; // eax
unsigned int v11; // eax
int v12; // eax
char v13; // al
unsigned __int8 v14; // r13
char *v15; // rbp
char *v16; // rsi
unsigned __int16 v17; // bp
unsigned __int8 v18; // r13
char *v19; // r14
char *v20; // rbp
__int16 *v21; // r13
unsigned __int16 v22; // ax
char v23; // al
__int64 v24; // rbp
struct timeval v25; // [rsp+0h] [rbp-648h] BYREF
struct timeval tv; // [rsp+10h] [rbp-638h] BYREF
__int64 v27; // [rsp+28h] [rbp-620h] BYREF
char dest[256]; // [rsp+30h] [rbp-618h] BYREF
char buf[1024]; // [rsp+130h] [rbp-518h] BYREF
char v30[8]; // [rsp+530h] [rbp-118h] BYREF
__int64 v31; // [rsp+538h] [rbp-110h]
__int64 v32; // [rsp+548h] [rbp-100h] BYREF
char v33[40]; // [rsp+550h] [rbp-F8h] BYREF
int v34; // [rsp+578h] [rbp-D0h]
int fd; // [rsp+57Ch] [rbp-CCh]
int v36; // [rsp+580h] [rbp-C8h]
unsigned __int8 v37; // [rsp+584h] [rbp-C4h]
char *v38; // [rsp+588h] [rbp-C0h]
char v39[60]; // [rsp+590h] [rbp-B8h] BYREF
unsigned int v40; // [rsp+5CCh] [rbp-7Ch]
int v41; // [rsp+5D0h] [rbp-78h]
__int64 v42; // [rsp+5E0h] [rbp-68h]
int (__fastcall *v43)(char *); // [rsp+600h] [rbp-48h]

v32 = 0LL;
init_socket(v33, &g_server_ip, g_control_port, g_data_port, &g_train_no);
frame_heartbeat_init(v39);
v0 = init_file_desc();
if ( v0 )
{
v1 = v0;
v42 = v0;
v40 = g_recv_qid;
v41 = g_data_qid;
create_thread(&t_send, &send_thread, v33);
create_thread(t_handle, &read_thread, v33);
create_thread((unsigned int)((_DWORD)t_handle + 8), &read_thread, v33);
create_thread((unsigned int)((_DWORD)t_handle + 16), &read_thread, v33);
v2 = 20;
while ( 1 )
{
if ( (int)recv_msg_q(v40, v30, 24LL, 1LL, 2048LL) >= 0 )
{
v4 = v31;
switch ( v34 )
{
case 65536:
goto LABEL_5;
case 65537:
goto LABEL_9;
case 65538:
goto LABEL_33;
case 65539:
goto LABEL_42;
case 65540:
goto LABEL_52;
case 65541:
goto LABEL_81;
case 65542:
goto LABEL_94;
case 65543:
goto LABEL_100;
default:
goto LABEL_95;
}
}
switch ( v34 )
{
case 65536:
v4 = 0LL;
LABEL_5:
if ( v43(v33) < 0 )
{
puts("[client]connect to server failed!");
sleep(1u);
}
else
{
v34 = 65537;
create_thread(&t_recv, &receive_handler, v33);
}
goto LABEL_95;
case 65537:
v4 = 0LL;
LABEL_9:
if ( fd < 0 )
{
t_log("connection socket lost!");
v6 = 0LL;
goto LABEL_18;
}
if ( (int)frame_header_init(1LL, 1LL, &v32) < 0 )
{
t_log("frame init failed!");
v6 = 0LL;
if ( !v32 )
goto LABEL_95;
}
else
{
v5 = t_malloc((unsigned int)v37 + 1);
v6 = v5;
if ( !v5 )
{
t_log("malloc login_buf frame error!");
goto LABEL_19;
}
*(_BYTE *)v5 = v37;
v7 = (_DWORD *)(v5 + 1LL);
v8 = v37;
v9 = v38;
if ( v37 >= 8u )
{
*(_QWORD *)(v6 + 1) = *(_QWORD *)v38;
*(_QWORD *)((char *)v7 + v8 - 8) = *(_QWORD *)&v9[v8 - 8];
qmemcpy(
(void *)((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v9 - ((char *)v7 - ((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v8 + (_DWORD)v7 - (((_DWORD)v6 + 9) & 0xFFFFFFF8)) >> 3));
}
else if ( (v37 & 4) != 0 )
{
*v7 = *(_DWORD *)v38;
*(_DWORD *)((char *)v7 + v8 - 4) = *(_DWORD *)&v9[v8 - 4];
}
else if ( v37 )
{
*(_BYTE *)v7 = *v38;
if ( (v8 & 2) != 0 )
*(_WORD *)((char *)v7 + v8 - 2) = *(_WORD *)&v9[v8 - 2];
}
v10 = frame_build(v32, v6, (unsigned int)v37 + 1, buf);
if ( v10 != (unsigned int)send(fd, buf, v10, 0) )
{
t_log("[login]lost connection!");
LABEL_18:
v34 = 65543;
LABEL_19:
if ( !v32 )
goto LABEL_21;
goto LABEL_20;
}
t_log("[client]login sent");
v34 = 65538;
if ( !v32 )
{
LABEL_22:
t_free(v6);
goto LABEL_95;
}
}
LABEL_20:
((void (*)(void))t_free)();
v32 = 0LL;
LABEL_21:
if ( v6 )
goto LABEL_22;
LABEL_95:
if ( v4 )
LABEL_96:
t_free(v4);
break;
case 65538:
v4 = 0LL;
LABEL_33:
if ( v4 )
{
if ( *(_WORD *)(v4 + 2) == 513 )
{
v34 = 65539;
t_log("[client]login complete!");
create_thread(&t_heartbeat, &send_heartbeat_thread, v33);
}
else
{
t_log("[login_sent]receive error");
}
goto LABEL_36;
}
if ( v2 )
goto LABEL_62;
v34 = 65543;
v2 = 20;
continue;
case 65539:
v4 = 0LL;
LABEL_42:
if ( (int)frame_header_init(1LL, 4LL, &v32) < 0 )
{
t_log("frame init failed!");
}
else
{
v11 = get_file_info(&g_file_path, &v27);
v12 = frame_build(v32, v27, v11, buf);
if ( v12 == (unsigned int)send(fd, buf, v12, 0) )
{
t_log("[client]file info sent");
v34 = 65540;
}
else
{
t_log("[login]send file info failed.");
t_log("[client]re conn");
v34 = 65543;
}
}
goto LABEL_45;
case 65540:
v4 = 0LL;
LABEL_52:
if ( v4 )
{
if ( *(_BYTE *)(v4 + 2) != 1 )
{
t_log("receive type error");
LABEL_36:
v2 = 20;
goto LABEL_96;
}
v13 = *(_BYTE *)(v4 + 3);
if ( v13 == 3 )
{
t_log("[server]continue transmit");
v18 = *(_BYTE *)(v4 + 6);
v19 = (char *)(v4 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v20 = &dest[strlen(dest)];
if ( v18 >= 8u )
{
*(_QWORD *)v20 = *(_QWORD *)(v4 + 7);
*(_QWORD *)&v20[v18 - 8] = *(_QWORD *)&v19[v18 - 8];
qmemcpy(
(void *)((unsigned __int64)(v20 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v19 - &v20[-((unsigned __int64)(v20 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * ((v18 + (unsigned int)v20 - (((_DWORD)v20 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v18 & 4) != 0 )
{
*(_DWORD *)v20 = *(_DWORD *)v19;
*(_DWORD *)&v20[v18 - 4] = *(_DWORD *)&v19[v18 - 4];
}
else if ( v18 )
{
*v20 = *v19;
if ( (v18 & 2) != 0 )
*(_WORD *)&v20[v18 - 2] = *(_WORD *)&v19[v18 - 2];
}
v21 = (__int16 *)&v19[v18];
v22 = func1(*v21);
if ( (int)set_file_desc(v1, v22, dest) < 0 )
{
t_log("[re_transmit]file error!");
v34 = 65540;
}
else if ( (int)handle_re_transimit_frame(v1, v21 + 1) < 0 )
{
clear_file_desc(v1);
v34 = 65540;
}
else
{
v34 = 65541;
}
goto LABEL_36;
}
if ( v13 == 5 )
{
v14 = *(_BYTE *)(v4 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v15 = &dest[strlen(dest)];
v16 = (char *)(v4 + 8);
if ( v14 >= 8u )
{
*(_QWORD *)v15 = *(_QWORD *)(v4 + 8);
*(_QWORD *)&v15[v14 - 8] = *(_QWORD *)(v4 + v14);
qmemcpy(
(void *)((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v16 - &v15[-((unsigned __int64)(v15 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * ((v14 + (unsigned int)v15 - (((_DWORD)v15 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v14 & 4) != 0 )
{
*(_DWORD *)v15 = *(_DWORD *)v16;
*(_DWORD *)&v15[v14 - 4] = *(_DWORD *)&v16[v14 - 4];
}
else if ( v14 )
{
*v15 = *v16;
if ( (v14 & 2) != 0 )
*(_WORD *)&v15[v14 - 2] = *(_WORD *)&v16[v14 - 2];
}
v17 = func1(*(_WORD *)(v4 + v14 + 8));
t_log("[server]download file: %s ", dest);
set_file_desc(v1, v17, dest);
init_send_list(v1);
t_log("[client]sending file...");
gettimeofday(&tv, 0LL);
v34 = 65541;
goto LABEL_36;
}
goto LABEL_85;
}
if ( v2 )
{
LABEL_62:
--v2;
sleep(1u);
}
else
{
clear_file_desc(v42);
v34 = 65540;
t_log("[client]timeout! all file tranfer completed!");
v2 = 20;
}
continue;
case 65541:
v4 = 0LL;
LABEL_81:
if ( v4 )
{
if ( *(_BYTE *)(v4 + 2) == 1 )
{
v23 = *(_BYTE *)(v4 + 3);
if ( v23 == 3 )
{
t_log("[server]retransmit");
if ( (int)handle_re_transimit_frame(v42, v4 + *(unsigned __int8 *)(v4 + 6) + 9) < 0 )
{
clear_file_desc(v42);
v34 = 65540;
}
goto LABEL_36;
}
if ( v23 == 8 )
{
gettimeofday(&v25, 0LL);
t_log(
"time_used = %f s",
(float)((float)(LODWORD(v25.tv_usec)
+ 1000000 * (LODWORD(v25.tv_sec) - LODWORD(tv.tv_sec))
- LODWORD(tv.tv_usec))
/ 1000000.0));
t_log("[server]transfer finished");
v24 = v42;
if ( *(_WORD *)(v24 + 12) == func1(*(_WORD *)(v4 + 6)) )
{
clear_file_desc(v24);
v34 = 65540;
sleep(2u);
}
else
{
puts("[finish]frame file id error!");
}
goto LABEL_36;
}
LABEL_85:
v2 = 20;
goto LABEL_96;
}
t_log("[client]receive error");
v2 = 20;
LABEL_45:
if ( v32 )
{
((void (*)(void))t_free)();
v32 = 0LL;
}
if ( v27 )
{
((void (*)(void))t_free)();
v27 = 0LL;
}
goto LABEL_95;
}
sleep(1u);
continue;
case 65542:
v4 = 0LL;
LABEL_94:
t_log("[state]transfer finished");
v34 = 65540;
goto LABEL_95;
case 65543:
v4 = 0LL;
LABEL_100:
t_log("[state]connection lost");
v34 = 0x10000;
sleep(3u);
shutdown(fd, 2);
fd = 0;
shutdown(v36, 2);
v36 = 0;
clear_file_desc(v42);
goto LABEL_95;
default:
continue;
}
}
}
return t_log("malloc f_desc frame error!");
}

original_code:
void main_thread(void *args)
{
    transfer_session session;
    transfer_frame *f_send = ((void *)0), *f_recv = ((void *)0);
    frame_header *f_header = ((void *)0), *recv_header = ((void *)0);
    login_frame *f_logini = ((void *)0);
    file_desc *f_desc = ((void *)0);
    finish_frame *finish = ((void *)0);
    q_msg pkg_msg;
    char *login_buf;
    unsigned char *buf = ((void *)0);
    unsigned char buf_send[1024];
    unsigned char buf_file_name[256];
    unsigned char *buf_file_info;
    unsigned char *buf_pos;
    unsigned char re_connect = 0;
    int len = 0, data_len = 0, frame_len = 0;
    unsigned short recv_len = 0;
    unsigned short file_id = 0;
    int buf_offset = 0;
    int fd;
    int code = 0;
    unsigned char file_name_len = 0, t_count = 0;
    int wait_seconds = 20;
    struct timeval start;
    struct timeval end;
    float time_used;
    init_socket(&session, g_server_ip, g_control_port, g_data_port, g_train_no);
    frame_heartbeat_init(&session.hb);
    if(((void *)0) == (f_desc = init_file_desc()))
    {
        t_log("malloc f_desc frame error!");
        return;
    }
    session.f_desc = f_desc;
    session.package_qid = g_recv_qid;
    session.data_qid = g_data_qid;
    create_thread(&t_send, send_thread, &session);
    for(t_count = 0; t_count < 3; t_count++)
        create_thread(&t_handle[t_count], read_thread, &session);
    for(;;)
    {
        if(recv_msg_q(session.package_qid, &pkg_msg, sizeof(q_msg), 0x01, 04000) < 0)
            buf = ((void *)0);
        else
            buf = pkg_msg.msg_buf.data;
        switch (session.state)
        {
        case 0x00010000:
            if(session.connect(&session) < 0)
            {
                printf("[client]connect to server failed!\n");
                sleep(1);
                break;
            }
            session.state = 0x00010001;
            create_thread(&t_recv, receive_handler, &session);
            break;
        case 0x00010001:
            if (session.fd < 0)
            {
                t_log("connection socket lost!");
                re_connect = 1;
                goto re_conn;
            }
            if(frame_header_init(0x01, 0X01, &f_header) < 0)
            {
                t_log("frame init failed!");
                goto re_conn;
            }
            if((login_buf = (char *)t_malloc(1 + session.train_no_len)) == ((void *)0))
            {
                t_log("malloc login_buf frame error!");
                goto re_conn;
            }
            memset(login_buf, session.train_no_len, 1);
            memcpy(login_buf + 1, session.train_no, session.train_no_len);
            data_len = session.train_no_len + 1;
            frame_len = frame_build(f_header, login_buf, data_len, buf_send);
            if((len = send(session.fd, buf_send, frame_len, 0)) != frame_len)
            {
                t_log("[login]lost connection!");
                re_connect = 1;
                goto re_conn;
            }
            t_log("[client]login sent");
            session.state = 0x00010002;
re_conn:
            if(re_connect)
            {
                re_connect = 0;
                session.state = 0x00010007;
            }
            if(f_header)
            {
                t_free(f_header);
                f_header = ((void *)0);
            }
            if(login_buf)
            {
                t_free(login_buf);
                login_buf = ((void *)0);
            }
            break;
        case 0x00010002:
            if(buf)
            {
                recv_header = (frame_header *)buf;
                wait_seconds = 20;
            }
            else
            {
                if(wait_seconds)
                {
                    wait_seconds--;
                    sleep(1);
                    break;
                }
                else
                {
                    wait_seconds = 20;
                    session.state = 0x00010007;
                    break;
                }
             }
            if (recv_header->type != 0x01 || recv_header->sub_type != 0X02)
            {
                t_log("[login_sent]receive error");
                break;
            }
            session.state = 0x00010003;
            t_log("[client]login complete!");
            create_thread(&t_heartbeat, send_heartbeat_thread, &session);
            break;
        case 0x00010003:
            if(frame_header_init(0x01, 0X04, &f_header) < 0)
            {
                t_log("frame init failed!");
                goto re_login;
            }
            data_len = get_file_info(g_file_path, &buf_file_info);
            frame_len = frame_build(f_header, buf_file_info, data_len, buf_send);
            if ((len = send(session.fd, buf_send, frame_len, 0)) != frame_len)
            {
                t_log("[login]send file info failed.");
                re_connect = 1;
                goto re_login;
            }
            t_log("[client]file info sent");
            session.state = 0x00010004;
re_login:
            if(re_connect)
            {
                t_log("[client]re conn");
                re_connect = 0;
                session.state = 0x00010007;
            }
            if(f_header)
            {
                t_free(f_header);
                f_header = ((void *)0);
            }
            if(buf_file_info)
            {
                t_free(buf_file_info);
                buf_file_info = ((void *)0);
            }
            break;
        case 0x00010004:
            if(buf)
            {
                recv_header = (frame_header *)buf;
                wait_seconds = 20;
            }
            else
            {
                if(wait_seconds)
                {
                    wait_seconds--;
                    sleep(1);
                    break;
                }
                else
                {
                    wait_seconds = 20;
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    t_log("[client]timeout! all file tranfer completed!");
                    break;
                }
            }
            if (recv_header->type != 0x01)
            {
                t_log("receive type error");
                break;
            }
            switch(recv_header->sub_type)
            {
            case 0X05:
                file_name_len = *(buf + sizeof(frame_header) + 1);
                memset(buf_file_name, 0, 256);
                memcpy(buf_file_name + strlen(strcpy(buf_file_name, g_file_path)), buf + sizeof(frame_header) + 2, file_name_len);
                file_id = htons(*((unsigned short *)( buf + sizeof(frame_header) + 2 + file_name_len)));
                t_log("[server]download file: %s ", buf_file_name);
                set_file_desc(f_desc, file_id, buf_file_name);
                init_send_list(f_desc);
                t_log("[client]sending file...");
                gettimeofday(&start, ((void *)0));
                session.state = 0x00010005;
                break;
            case 0X03:
                t_log("[server]continue transmit");
                buf_pos = buf;
                buf_pos += sizeof(frame_header);
                file_name_len = *buf_pos;
                buf_pos += 1;
                memset(buf_file_name, 0, 256);
                memcpy(buf_file_name + strlen(strcpy(buf_file_name, g_file_path)), buf_pos, file_name_len);
                buf_pos += file_name_len;
                file_id = ntohs(*((unsigned short *)buf_pos));
                if(set_file_desc(f_desc, file_id, buf_file_name) < 0)
                {
                    t_log("[re_transmit]file error!");
                    session.state = 0x00010004;
                    break;
                }
                buf_pos += 2;
                if(handle_re_transimit_frame(f_desc, buf_pos) < 0)
                {
                    clear_file_desc(f_desc);
                    session.state = 0x00010004;
                    break;
                }
                session.state = 0x00010005;
                break;
            default:
                break;
            }
            break;
        case 0x00010005:
            if(buf)
            {
                recv_header = (frame_header *)buf;
                wait_seconds = 20;
            }
            else
            {
                sleep(1);
                break;
            }
            if (recv_header->type != 0x01)
            {
                t_log("[client]receive error");
                goto re_login;
            }
            switch(recv_header->sub_type)
            {
            case 0X03:
                t_log("[server]retransmit");
                buf_pos = buf;
                buf_pos += sizeof(frame_header);
                buf_pos += 1 + buf_pos[0] + 2;
                if (handle_re_transimit_frame(session.f_desc, buf_pos) < 0)
                {
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    break;
                }
                break;
             case 0X08:
                gettimeofday(&end,((void *)0));
                time_used = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
                time_used /= 1000000;
                t_log("time_used = %f s", time_used);
                t_log("[server]transfer finished");
                finish = (finish_frame *)buf;
                if(session.f_desc->file_id == htons(finish->file_id))
                {
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    sleep(2);
                }
                else
                    printf("[finish]frame file id error!\n");
                break;
            default:
                break;
            }
            break;
        case 0x00010006:
            t_log("[state]transfer finished");
            session.state = 0x00010004;
            break;
        case 0x00010007:
            t_log("[state]connection lost");
            session.state = 0x00010000;
            sleep(3);
            shutdown(session.fd, 2);
            session.fd = ((void *)0);
            shutdown(session.data_fd, 2);
            session.data_fd = ((void *)0);
            clear_file_desc(session.f_desc);
        default:
            break;
        }
        if(buf)
        {
            t_free(buf);
            buf = ((void *)0);
        }
    }
}

canonical_original_code:
void main_thread(void *args)
{
    struct transfer_session_tag session;
    struct transfer_frame_tag *f_send = ((void *)0); struct transfer_frame_tag *f_recv = ((void *)0);
    struct frame_header_tag *f_header = ((void *)0); struct frame_header_tag *recv_header = ((void *)0);
    struct login_frame_tag *f_logini = ((void *)0);
    struct file_desc *f_desc = ((void *)0);
    struct finish_frame_tag *finish = ((void *)0);
    struct q_msg_tag pkg_msg;
    char *login_buf;
    unsigned char *buf = ((void *)0);
    unsigned char buf_send[1024];
    unsigned char buf_file_name[256];
    unsigned char *buf_file_info;
    unsigned char *buf_pos;
    unsigned char re_connect = 0;
    int len = 0; int data_len = 0; int frame_len = 0;
    unsigned short recv_len = 0;
    unsigned short file_id = 0;
    int buf_offset = 0;
    int fd;
    int code = 0;
    unsigned char file_name_len = 0; unsigned char t_count = 0;
    int wait_seconds = 20;
    struct timeval start;
    struct timeval end;
    float time_used;
    init_socket(&session, g_server_ip, g_control_port, g_data_port, g_train_no);
    frame_heartbeat_init(&session.hb);
    if(((void *)0) == (f_desc = init_file_desc()))
    {
        t_log("malloc f_desc frame error!");
        return;
    }
    session.f_desc = f_desc;
    session.package_qid = g_recv_qid;
    session.data_qid = g_data_qid;
    create_thread(&t_send, send_thread, &session);
    for(t_count = 0; t_count < 3; t_count++)
        create_thread(&t_handle[t_count], read_thread, &session);
    for(;;)
    {
        if(recv_msg_q(session.package_qid, &pkg_msg, sizeof(q_msg), 0x01, 04000) < 0)
            buf = ((void *)0);
        else
            buf = pkg_msg.msg_buf.data;
        switch (session.state)
        {
        case 0x00010000:
            if(session.connect(&session) < 0)
            {
                printf("[client]connect to server failed!\n");
                sleep(1);
                break;
            }
            session.state = 0x00010001;
            create_thread(&t_recv, receive_handler, &session);
            break;
        case 0x00010001:
            if (session.fd < 0)
            {
                t_log("connection socket lost!");
                re_connect = 1;
                goto re_conn;
            }
            if(frame_header_init(0x01, 0X01, &f_header) < 0)
            {
                t_log("frame init failed!");
                goto re_conn;
            }
            if((login_buf = (char *)t_malloc(1 + session.train_no_len)) == ((void *)0))
            {
                t_log("malloc login_buf frame error!");
                goto re_conn;
            }
            memset(login_buf, session.train_no_len, 1);
            memcpy(login_buf + 1, session.train_no, session.train_no_len);
            data_len = session.train_no_len + 1;
            frame_len = frame_build(f_header, login_buf, data_len, buf_send);
            if((len = send(session.fd, buf_send, frame_len, 0)) != frame_len)
            {
                t_log("[login]lost connection!");
                re_connect = 1;
                goto re_conn;
            }
            t_log("[client]login sent");
            session.state = 0x00010002;
re_conn:
            if(re_connect)
            {
                re_connect = 0;
                session.state = 0x00010007;
            }
            if(f_header)
            {
                t_free(f_header);
                f_header = ((void *)0);
            }
            if(login_buf)
            {
                t_free(login_buf);
                login_buf = ((void *)0);
            }
            break;
        case 0x00010002:
            if(buf)
            {
                recv_header = (struct frame_header_tag *)buf;
                wait_seconds = 20;
            }
            else
            {
                if(wait_seconds)
                {
                    wait_seconds--;
                    sleep(1);
                    break;
                }
                else
                {
                    wait_seconds = 20;
                    session.state = 0x00010007;
                    break;
                }
             }
            if (recv_header->type != 0x01 || recv_header->sub_type != 0X02)
            {
                t_log("[login_sent]receive error");
                break;
            }
            session.state = 0x00010003;
            t_log("[client]login complete!");
            create_thread(&t_heartbeat, send_heartbeat_thread, &session);
            break;
        case 0x00010003:
            if(frame_header_init(0x01, 0X04, &f_header) < 0)
            {
                t_log("frame init failed!");
                goto re_login;
            }
            data_len = get_file_info(g_file_path, &buf_file_info);
            frame_len = frame_build(f_header, buf_file_info, data_len, buf_send);
            if ((len = send(session.fd, buf_send, frame_len, 0)) != frame_len)
            {
                t_log("[login]send file info failed.");
                re_connect = 1;
                goto re_login;
            }
            t_log("[client]file info sent");
            session.state = 0x00010004;
re_login:
            if(re_connect)
            {
                t_log("[client]re conn");
                re_connect = 0;
                session.state = 0x00010007;
            }
            if(f_header)
            {
                t_free(f_header);
                f_header = ((void *)0);
            }
            if(buf_file_info)
            {
                t_free(buf_file_info);
                buf_file_info = ((void *)0);
            }
            break;
        case 0x00010004:
            if(buf)
            {
                recv_header = (struct frame_header_tag *)buf;
                wait_seconds = 20;
            }
            else
            {
                if(wait_seconds)
                {
                    wait_seconds--;
                    sleep(1);
                    break;
                }
                else
                {
                    wait_seconds = 20;
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    t_log("[client]timeout! all file tranfer completed!");
                    break;
                }
            }
            if (recv_header->type != 0x01)
            {
                t_log("receive type error");
                break;
            }
            switch(recv_header->sub_type)
            {
            case 0X05:
                file_name_len = *(buf + sizeof(frame_header) + 1);
                memset(buf_file_name, 0, 256);
                memcpy(buf_file_name + strlen(strcpy(buf_file_name, g_file_path)), buf + sizeof(frame_header) + 2, file_name_len);
                file_id = htons(*((unsigned short *)( buf + sizeof(frame_header) + 2 + file_name_len)));
                t_log("[server]download file: %s ", buf_file_name);
                set_file_desc(f_desc, file_id, buf_file_name);
                init_send_list(f_desc);
                t_log("[client]sending file...");
                gettimeofday(&start, ((void *)0));
                session.state = 0x00010005;
                break;
            case 0X03:
                t_log("[server]continue transmit");
                buf_pos = buf;
                buf_pos += sizeof(frame_header);
                file_name_len = *buf_pos;
                buf_pos += 1;
                memset(buf_file_name, 0, 256);
                memcpy(buf_file_name + strlen(strcpy(buf_file_name, g_file_path)), buf_pos, file_name_len);
                buf_pos += file_name_len;
                file_id = ntohs(*((unsigned short *)buf_pos));
                if(set_file_desc(f_desc, file_id, buf_file_name) < 0)
                {
                    t_log("[re_transmit]file error!");
                    session.state = 0x00010004;
                    break;
                }
                buf_pos += 2;
                if(handle_re_transimit_frame(f_desc, buf_pos) < 0)
                {
                    clear_file_desc(f_desc);
                    session.state = 0x00010004;
                    break;
                }
                session.state = 0x00010005;
                break;
            default:
                break;
            }
            break;
        case 0x00010005:
            if(buf)
            {
                recv_header = (struct frame_header_tag *)buf;
                wait_seconds = 20;
            }
            else
            {
                sleep(1);
                break;
            }
            if (recv_header->type != 0x01)
            {
                t_log("[client]receive error");
                goto re_login;
            }
            switch(recv_header->sub_type)
            {
            case 0X03:
                t_log("[server]retransmit");
                buf_pos = buf;
                buf_pos += sizeof(frame_header);
                buf_pos += 1 + buf_pos[0] + 2;
                if (handle_re_transimit_frame(session.f_desc, buf_pos) < 0)
                {
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    break;
                }
                break;
             case 0X08:
                gettimeofday(&end,((void *)0));
                time_used = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
                time_used /= 1000000;
                t_log("time_used = %f s", time_used);
                t_log("[server]transfer finished");
                finish = (struct finish_frame_tag *)buf;
                if(session.f_desc->file_id == htons(finish->file_id))
                {
                    clear_file_desc(session.f_desc);
                    session.state = 0x00010004;
                    sleep(2);
                }
                else
                    printf("[finish]frame file id error!\n");
                break;
            default:
                break;
            }
            break;
        case 0x00010006:
            t_log("[state]transfer finished");
            session.state = 0x00010004;
            break;
        case 0x00010007:
            t_log("[state]connection lost");
            session.state = 0x00010000;
            sleep(3);
            shutdown(session.fd, 2);
            session.fd = ((void *)0);
            shutdown(session.data_fd, 2);
            session.data_fd = ((void *)0);
            clear_file_desc(session.f_desc);
        default:
            break;
        }
        if(buf)
        {
            t_free(buf);
            buf = ((void *)0);
        }
    }
}

variable_types:
args
void *

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: struct transfer_frame_tag { struct frame_header_tag { unsigned short length; unsigned char type; unsigned char sub_type; unsigned short crc; } header; unsigned char *data; unsigned char tail; } *

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

variable_types: struct login_frame_tag { unsigned char train_no_len; unsigned char *train_no; } *

variable_types: struct finish_frame_tag { struct frame_header_tag { unsigned short length; unsigned char type; unsigned char sub_type; unsigned short crc; } f_header; unsigned short file_id; unsigned char f_tail; } *

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:
buf
unsigned char *

variable_types: len int

variable_types: fd int

variable_types: code int

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

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 q_msg_tag { unsigned int msg_type; struct data_msg_tag msg_buf; }

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 transfer_frame_tag { struct frame_header_tag header; unsigned char *data; unsigned char tail; }

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 finish_frame_tag { struct frame_header_tag f_header; unsigned short file_id; unsigned char f_tail; }

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


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
sigset_t v3; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v3);
sigaddset(&v3, 13);
if ( pthread_sigmask(0, &v3, 0LL) )
puts("block sigpipe error");
signal(2, (__sighandler_t)destroy);
t_log("[system]initializing memory pool...");
mem_pool_init();
t_log("[system]done");
t_log("[system]initializing system config...");
cmd_init_config();
if ( (unsigned int)cmd_parse_config() )
{
set_default_config();
t_log("config file invalid, use default configs.\r\n");
}
else
{
t_log("[system]done.");
}
g_recv_qid = create_msg_q(1LL);
g_data_qid = create_msg_q(2LL);
create_thread(&t_main, main_thread, 0LL);
while ( 1 )
sleep(0x64u);
}

original_code:
int main(int argc, char *argv[])
{
    sigset_t signal_mask;
    sigemptyset (&signal_mask);
    sigaddset (&signal_mask, 13);
    int rc = pthread_sigmask (0, &signal_mask, ((void *)0));
    if (rc != 0) {
           printf("block sigpipe error\n");
    }
    signal(2, destroy);
    t_log("[system]initializing memory pool...");
    mem_pool_init();
    t_log("[system]done");
    t_log("[system]initializing system config...");
    cmd_init_config();
    if(STATUS_CONFIG_SUCCESS != cmd_parse_config())
    {
        set_default_config();
        t_log("config file invalid, use default configs.\r\n");
    }
    else
        t_log("[system]done.");
    g_recv_qid = create_msg_q(0x01);
    g_data_qid = create_msg_q(0x02);
    create_thread(&t_main, main_thread, ((void *)0));
    for(;;)
        sleep(100);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct __sigset_t signal_mask;
    sigemptyset (&signal_mask);
    sigaddset (&signal_mask, 13);
    int rc = pthread_sigmask (0, &signal_mask, ((void *)0));
    if (rc != 0) {
           printf("block sigpipe error\n");
    }
    signal(2, destroy);
    t_log("[system]initializing memory pool...");
    mem_pool_init();
    t_log("[system]done");
    t_log("[system]initializing system config...");
    cmd_init_config();
    if(STATUS_CONFIG_SUCCESS != cmd_parse_config())
    {
        set_default_config();
        t_log("config file invalid, use default configs.\r\n");
    }
    else
        t_log("[system]done.");
    g_recv_qid = create_msg_q(0x01);
    g_data_qid = create_msg_q(0x02);
    create_thread(&t_main, main_thread, ((void *)0));
    for(;;)
        sleep(100);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct __sigset_t { unsigned long __val[-1]; }

variable_types: rc int

return_type: int


decompiled_code:
__int64 __fastcall t_malloc(__int64 a1)
{
return mem_alloc(a1, 1LL);
}

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

original_code:
void *t_malloc(int x)
{
    void *ptr;
    ptr = mem_alloc ((x), (1));
    return ptr;
}

variable_types: x int

variable_types:
ptr
void *

return_type: void *


decompiled_code:
__int64 t_free()
{
return mem_free();
}

canonical_decompiled_code:
__int64 func1()
{
return mem_free();
}

original_code:
void t_free(void *x)
{
    return mem_free ((x));
}

variable_types:
x
void *


decompiled_code:
int __fastcall init_lock(pthread_mutex_t *a1)
{
return pthread_mutex_init(a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t *a1)
{
return pthread_mutex_init(a1, 0LL);
}

original_code:
int init_lock(pthread_mutex_t *lock)
{
    return pthread_mutex_init(lock, ((void *)0));
}

canonical_original_code:
int init_lock(union pthread_mutex_t *lock)
{
    return pthread_mutex_init(lock, ((void *)0));
}

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


decompiled_code:
int __fastcall lock_t_lock(pthread_mutex_t *a1)
{
return pthread_mutex_lock(a1);
}

canonical_decompiled_code:
int __fastcall func3(pthread_mutex_t *a1)
{
return pthread_mutex_lock(a1);
}

original_code:
int lock_t_lock(pthread_mutex_t *lock)
{
    return pthread_mutex_lock(lock);
}

canonical_original_code:
int lock_t_lock(union pthread_mutex_t *lock)
{
    return pthread_mutex_lock(lock);
}

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


decompiled_code:
int __fastcall unlock_t_lock(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

canonical_decompiled_code:
int __fastcall func4(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

original_code:
int unlock_t_lock(pthread_mutex_t *lock)
{
    return pthread_mutex_unlock(lock);
}

canonical_original_code:
int unlock_t_lock(union pthread_mutex_t *lock)
{
    return pthread_mutex_unlock(lock);
}


decompiled_code:
int __fastcall init_sem(sem_t *a1, unsigned __int8 a2)
{
return sem_init(a1, 0, a2);
}

canonical_decompiled_code:
int __fastcall func5(sem_t *a1, unsigned __int8 a2)
{
return sem_init(a1, 0, a2);
}

original_code:
int init_sem(sem_t *sem, unsigned char count)
{
    return sem_init(sem, 0, count);
}

canonical_original_code:
int init_sem(union sem_t *sem, unsigned char count)
{
    return sem_init(sem, 0, count);
}

variable_types: union sem_t { char __size[32]; long __align; } *

variable_types:
count
unsigned char


decompiled_code:
int __fastcall t_release(sem_t *a1)
{
return sem_post(a1);
}

canonical_decompiled_code:
int __fastcall func6(sem_t *a1)
{
return sem_post(a1);
}

original_code:
int t_release(sem_t *sem)
{
    return sem_post(sem);
}

canonical_original_code:
int t_release(union sem_t *sem)
{
    return sem_post(sem);
}

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
int __fastcall t_aquire(sem_t *a1)
{
return sem_wait(a1);
}

canonical_decompiled_code:
int __fastcall func7(sem_t *a1)
{
return sem_wait(a1);
}

original_code:
int t_aquire(sem_t *sem)
{
    return sem_wait(sem);
}

canonical_original_code:
int t_aquire(union sem_t *sem)
{
    return sem_wait(sem);
}


decompiled_code:
int __fastcall t_aquire_nb(sem_t *a1)
{
return sem_trywait(a1);
}

canonical_decompiled_code:
int __fastcall func8(sem_t *a1)
{
return sem_trywait(a1);
}

original_code:
int t_aquire_nb(sem_t *sem)
{
    return sem_trywait(sem);
}

canonical_original_code:
int t_aquire_nb(union sem_t *sem)
{
    return sem_trywait(sem);
}


decompiled_code:
int __fastcall create_thread(pthread_t *a1, void *(*a2)(void *), void *a3)
{
return pthread_create(a1, 0LL, a2, a3);
}

canonical_decompiled_code:
int __fastcall func9(pthread_t *a1, void *(*a2)(void *), void *a3)
{
return pthread_create(a1, 0LL, a2, a3);
}

original_code:
int create_thread(pthread_t *tid, void *(*func)(void *), void *args)
{
    return pthread_create(tid, ((void *)0), func, args);
}

canonical_original_code:
int create_thread(unsigned long *tid, void *(*func)(void *), void *args)
{
    return pthread_create(tid, ((void *)0), func, args);
}

variable_types:
tid
unsigned long *

variable_types:
func
void *(*)(void *)

variable_types:
args
void *


decompiled_code:
int __fastcall cancel_thread(pthread_t *a1)
{
return pthread_cancel(*a1);
}

canonical_decompiled_code:
int __fastcall func10(pthread_t *a1)
{
return pthread_cancel(*a1);
}

original_code:
int cancel_thread(pthread_t *tid)
{
    return pthread_cancel(*tid);
}

canonical_original_code:
int cancel_thread(unsigned long *tid)
{
    return pthread_cancel(*tid);
}


decompiled_code:
int __fastcall create_msg_q(int proj_id)
{
key_t v1; // eax
int result; // eax

v1 = ftok(".", proj_id);
if ( v1 == -1 )
{
perror("Create key error!\n");
result = 0;
}
else
{
result = msgget(v1, 1974);
if ( result == -1 )
{
perror("message queue already exitsted!");
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(int proj_id)
{
key_t v1; // eax
int result; // eax

v1 = ftok(".", proj_id);
if ( v1 == -1 )
{
perror("Create key error!\n");
result = 0;
}
else
{
result = msgget(v1, 1974);
if ( result == -1 )
{
perror("message queue already exitsted!");
result = 0;
}
}
return result;
}

original_code:
int create_msg_q(int key_id)
{
    key_t key;
    int qid;
    if(-1 == (key = ftok(".", key_id)))
    {
        perror("Create key error!\n");
        return ((void *)0);
    }
    if(-1 == (qid = msgget(key, 01000|02000|0666)))
    {
        perror("message queue already exitsted!");
        return ((void *)0);
    }
    return qid;
}

canonical_original_code:
int create_msg_q(int key_id)
{
    int key;
    int qid;
    if(-1 == (key = ftok(".", key_id)))
    {
        perror("Create key error!\n");
        return ((void *)0);
    }
    if(-1 == (qid = msgget(key, 01000|02000|0666)))
    {
        perror("message queue already exitsted!");
        return ((void *)0);
    }
    return qid;
}

variable_types: key int

variable_types: qid int


decompiled_code:
int __fastcall destroy_msg_q(int a1)
{
return msgctl(a1, 0, 0LL);
}

canonical_decompiled_code:
int __fastcall func12(int a1)
{
return msgctl(a1, 0, 0LL);
}

original_code:
int destroy_msg_q(int qid)
{
    msgctl(qid, 0, ((void *)0));
}


decompiled_code:
int __fastcall send_to_msg_q(int a1, const void *a2, int a3, int a4)
{
return msgsnd(a1, a2, a3, a4);
}

canonical_decompiled_code:
int __fastcall func13(int a1, const void *a2, int a3, int a4)
{
return msgsnd(a1, a2, a3, a4);
}

original_code:
int send_to_msg_q(int qid, void *buf, int size, int flag)
{
    return msgsnd(qid, buf, size, flag);
}

variable_types:
buf
void *

variable_types: size int

variable_types: flag int


decompiled_code:
ssize_t __fastcall recv_msg_q(int a1, void *a2, int a3, int a4, int a5)
{
return msgrcv(a1, a2, a3, a4, a5);
}

canonical_decompiled_code:
ssize_t __fastcall func14(int a1, void *a2, int a3, int a4, int a5)
{
return msgrcv(a1, a2, a3, a4, a5);
}

original_code:
int recv_msg_q(int qid, void *buf, int size, int msg_type, int flag)
{
    return msgrcv(qid, buf, size, msg_type, flag);
}


decompiled_code:
__int64 __fastcall frame_header_init(unsigned __int8 a1, unsigned __int8 a2, __int64 a3)
{
int v4; // eax
__int64 result; // rax

v4 = t_malloc(6LL);
*(_QWORD *)a3 = v4;
if ( v4 )
{
if ( a1 > 2u || a2 > 6u )
{
t_log("type unknown");
result = 0xFFFFFFFFLL;
}
else
{
*((_BYTE *)&loc_2 + v4) = a1;
*(_BYTE *)(*(_QWORD *)a3 + 3LL) = a2;
*(_WORD *)(*(_QWORD *)a3 + 4LL) = 0;
**(_WORD **)a3 = 0;
result = 0LL;
}
}
else
{
t_log("malloc frame header error!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, unsigned __int8 a2, __int64 a3)
{
int v4; // eax
__int64 result; // rax

v4 = t_malloc(6LL);
*(_QWORD *)a3 = v4;
if ( v4 )
{
if ( a1 > 2u || a2 > 6u )
{
t_log("type unknown");
result = 0xFFFFFFFFLL;
}
else
{
*((_BYTE *)&loc_2 + v4) = a1;
*(_BYTE *)(*(_QWORD *)a3 + 3LL) = a2;
*(_WORD *)(*(_QWORD *)a3 + 4LL) = 0;
**(_WORD **)a3 = 0;
result = 0LL;
}
}
else
{
t_log("malloc frame header error!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int frame_header_init(unsigned char type, unsigned char sub_type, frame_header **header)
{
    if((*header = (frame_header *)t_malloc(sizeof(frame_header))) == ((void *)0))
    {
        t_log("malloc frame header error!");
        return -1;
    }
    if(type >= 0 && type < 0X03 && sub_type >= 0 && sub_type < 0X07)
    {
        (*header)->type = type;
        (*header)->sub_type = sub_type;
        (*header)->crc = 0;
        (*header)->length = 0;
        return 0;
    }
    else
    {
        t_log("type unknown");
        return -1;
    }
}

canonical_original_code:
int frame_header_init(unsigned char type, unsigned char sub_type, struct frame_header_tag **header)
{
    if((*header = (struct frame_header_tag *)t_malloc(sizeof(frame_header))) == ((void *)0))
    {
        t_log("malloc frame header error!");
        return -1;
    }
    if(type >= 0 && type < 0X03 && sub_type >= 0 && sub_type < 0X07)
    {
        (*header)->type = type;
        (*header)->sub_type = sub_type;
        (*header)->crc = 0;
        (*header)->length = 0;
        return 0;
    }
    else
    {
        t_log("type unknown");
        return -1;
    }
}

variable_types:
type
unsigned char

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


original_code:
int frame_heartbeat_init(unsigned char **heartbeat_buf)
{
    frame_header *f_header = ((void *)0);
    frame_header_init(0X00, 0X00, &f_header);
    f_header->length = htons(5);
    *heartbeat_buf = (unsigned char *)t_malloc(sizeof(frame_header) + 1);
    memcpy(*heartbeat_buf, f_header, sizeof(frame_header));
    memset(*heartbeat_buf + sizeof(frame_header), 0xFF, 1);
    t_free(f_header);
    return 0;
}

canonical_original_code:
int frame_heartbeat_init(unsigned char **heartbeat_buf)
{
    struct frame_header_tag *f_header = ((void *)0);
    frame_header_init(0X00, 0X00, &f_header);
    f_header->length = htons(5);
    *heartbeat_buf = (unsigned char *)t_malloc(sizeof(frame_header) + 1);
    memcpy(*heartbeat_buf, f_header, sizeof(frame_header));
    memset(*heartbeat_buf + sizeof(frame_header), 0xFF, 1);
    t_free(f_header);
    return 0;
}

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


original_code:
int frame_block_sent_init(block_sent_frame *block_sent_buf)
{
    frame_header *f_header = ((void *)0);
    f_header = &(block_sent_buf->f_header);
    f_header->type = 0x01;
    f_header->sub_type = 0X07;
    f_header->length = htons(9);
    block_sent_buf->f_tail = 0xFF;
    return 0;
}

canonical_original_code:
int frame_block_sent_init(struct block_sent_frame_tag *block_sent_buf)
{
    struct frame_header_tag *f_header = ((void *)0);
    f_header = &(block_sent_buf->f_header);
    f_header->type = 0x01;
    f_header->sub_type = 0X07;
    f_header->length = htons(9);
    block_sent_buf->f_tail = 0xFF;
    return 0;
}

variable_types: struct block_sent_frame_tag { struct frame_header_tag { unsigned short length; unsigned char type; unsigned char sub_type; unsigned short crc; } f_header; unsigned short file_id; unsigned short block_index; unsigned char f_tail; } *

user_defined_types: struct block_sent_frame_tag { struct frame_header_tag f_header; unsigned short file_id; unsigned short block_index; unsigned char f_tail; }


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


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:
data
unsigned char *


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

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!");
result = 0xFFFFFFFFLL;
}
else
{
v3 = recv(fd, a2 + 1, v2, 0);
v4 = v3;
if ( v2 == v3 )
{
if ( *((_BYTE *)a2 + v2 + 1) == 0xFF )
{
v5 = get_crc_code(a2 + 3, (unsigned __int16)(v3 - 5));
if ( _bswap_16(a2[2]) == v5 )
{
result = (unsigned int)v4 + 2;
}
else
{
t_log("crc error");
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

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!");
result = 0xFFFFFFFFLL;
}
else
{
v3 = recv(fd, a2 + 1, v2, 0);
v4 = v3;
if ( v2 == v3 )
{
if ( *((_BYTE *)a2 + v2 + 1) == 0xFF )
{
v5 = get_crc_code(a2 + 3, (unsigned __int16)(v3 - 5));
if ( func0(a2[2]) == v5 )
{
result = (unsigned int)v4 + 2;
}
else
{
t_log("crc error");
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
else
{
result = 0xFFFFFFFFLL;
}
}
return result;
}

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, char *a3)
{
__int64 v4; // rbp
int i; // ebx
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
int v10; // eax

v4 = (unsigned int)((_DWORD)cfg_entities + 8);
for ( i = 0; i != 6; ++i )
{
if ( !strcmp(a2, (const char *)v4) )
{
v10 = set_globle_values(
a3,
v4,
v6,
v7,
v8,
v9,
*(char **)&cfg_entities[32 * i],
*(_QWORD *)&cfg_entities[32 * i + 8],
*(_QWORD *)&cfg_entities[32 * i + 16],
*(_QWORD *)&cfg_entities[32 * i + 24]);
handle_config_status(v4, v10);
}
v4 += 32LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2, char *a3)
{
__int64 v4; // rbp
int i; // ebx
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
__int64 v9; // r9
int v10; // eax

v4 = (unsigned int)((_DWORD)cfg_entities + 8);
for ( i = 0; i != 6; ++i )
{
if ( !strcmp(a2, (const char *)v4) )
{
v10 = func1(
a3,
v4,
v6,
v7,
v8,
v9,
*(char **)&cfg_entities[32 * i],
*(_QWORD *)&cfg_entities[32 * i + 8],
*(_QWORD *)&cfg_entities[32 * i + 16],
*(_QWORD *)&cfg_entities[32 * i + 24]);
func0(v4, v10);
}
v4 += 32LL;
}
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;
g_data_port = 6160;
strcpy((char *)g_file_path, "files/");
strcpy((char *)g_server_ip, "192.168.2.158");
result = 0x3433323164636261LL;
strcpy((char *)g_train_no, "abcd1234");
return result;
}

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

g_control_port = 6260;
g_data_port = 6160;
strcpy((char *)g_file_path, "files/");
strcpy((char *)g_server_ip, "192.168.2.158");
result = 0x3433323164636261LL;
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");
}


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

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

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

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

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
_WORD *v2; // rdx

configs_count = 6;
result = cfg_entities;
do
{
v1 = *((_DWORD *)result + 7);
if ( v1 == 2 )
{
v2 = *(_WORD **)result;
*(_QWORD *)v2 = 0LL;
*((_QWORD *)v2 + 1) = 0LL;
*((_QWORD *)v2 + 2) = 0LL;
*((_QWORD *)v2 + 3) = 0LL;
*((_BYTE *)v2 + 32) = 0;
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
**(_BYTE **)result = 0;
}
else if ( v1 )
{
**(_WORD **)result = 0;
}
else
{
**(_DWORD **)result = 0;
}
result += 32;
}
while ( result != (char *)(unsigned int)((_DWORD)cfg_entities + 192) );
return result;
}

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

configs_count = 6;
result = cfg_entities;
do
{
v1 = *((_DWORD *)result + 7);
if ( v1 == 2 )
{
v2 = *(_WORD **)result;
*(_QWORD *)v2 = 0LL;
*((_QWORD *)v2 + 1) = 0LL;
*((_QWORD *)v2 + 2) = 0LL;
*((_QWORD *)v2 + 3) = 0LL;
*((_BYTE *)v2 + 32) = 0;
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
**(_BYTE **)result = 0;
}
else if ( v1 )
{
**(_WORD **)result = 0;
}
else
{
**(_DWORD **)result = 0;
}
result += 32;
}
while ( result != (char *)(unsigned int)((_DWORD)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 }


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