decompiled_code:
int open_file()
{
int v0; // eax
int v1; // eax
int result; // eax
int v3; // edi

v0 = open_fs();
fd[0] = v0;
if ( v0 < 0 )
return open_error_dialog();
v1 = read_superblock_gui((unsigned int)v0, (unsigned int)((_DWORD)fd + 8));
if ( v1 == -1 )
{
superblock_error_dialog();
v3 = fd[0];
fd[1] = 0;
result = close(v3);
}
else if ( v1 == 1 && (unsigned int)superblock_warning_dialog() == -1 )
{
fd[1] = 0;
result = close_fs((unsigned int)fd[0]);
}
else
{
fd[1] = 1;
init_tree();
result = explore_dir((unsigned int)fd[0], (unsigned int)((_DWORD)fd + 8), 2LL);
}
return result;
}

canonical_decompiled_code:
int func3()
{
int v0; // eax
int v1; // eax
int result; // eax
int v3; // edi

v0 = open_fs();
fd[0] = v0;
if ( v0 < 0 )
return func0();
v1 = read_superblock_gui((unsigned int)v0, (unsigned int)((_DWORD)fd + 8));
if ( v1 == -1 )
{
func1();
v3 = fd[0];
fd[1] = 0;
result = close(v3);
}
else if ( v1 == 1 && (unsigned int)func2() == -1 )
{
fd[1] = 0;
result = close_fs((unsigned int)fd[0]);
}
else
{
fd[1] = 1;
init_tree();
result = explore_dir((unsigned int)fd[0], (unsigned int)((_DWORD)fd + 8), 2LL);
}
return result;
}

original_code:
void open_file( gchar *path )
{
  int ret;
  fs.fd = open_fs ( (char*)path );
  if ( fs.fd < 0 ) {
    open_error_dialog();
    return;
  }
  ret = read_superblock_gui( fs.fd, &fs.sb );
  if ( ret == -1 ) {
    superblock_error_dialog();
    fs.isopen = 0;
    close(fs.fd);
  }
  else if ( ret == 1 ) {
    ret = superblock_warning_dialog();
    if ( ret == -1 ) {
      fs.isopen = 0;
      close_fs( fs.fd );
    }
    else {
      fs.isopen = 1;
      init_tree();
      explore_dir ( fs.fd, &fs.sb, 2, tree.iter);
    }
  }
  else {
    fs.isopen = 1;
    init_tree();
    explore_dir ( fs.fd, &fs.sb, 2, tree.iter );
  }
}

canonical_original_code:
void open_file(char *path)
{
  int ret;
  fs.fd = open_fs ( (char *)path );
  if ( fs.fd < 0 ) {
    open_error_dialog();
    return;
  }
  ret = read_superblock_gui( fs.fd, &fs.sb );
  if ( ret == -1 ) {
    superblock_error_dialog();
    fs.isopen = 0;
    close(fs.fd);
  }
  else if ( ret == 1 ) {
    ret = superblock_warning_dialog();
    if ( ret == -1 ) {
      fs.isopen = 0;
      close_fs( fs.fd );
    }
    else {
      fs.isopen = 1;
      init_tree();
      explore_dir ( fs.fd, &fs.sb, 2, tree.iter);
    }
  }
  else {
    fs.isopen = 1;
    init_tree();
    explore_dir ( fs.fd, &fs.sb, 2, tree.iter );
  }
}

variable_types:
path
char *

variable_types: ret int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx

stack(t1, ft1, envp);
stack(t2, ft2, v3);
if ( !_setjmp(taskmain) )
longjmp(t1, 1);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx

func8(t1, ft1, envp);
func8(t2, ft2, v3);
if ( !_setjmp(taskmain) )
longjmp(t1, 1);
return 0;
}

original_code:
int main (int argc, char **argv) {
 stack(t1, ft1);
 stack(t2, ft2);
 (_setjmp (taskmain) == 0 ? (longjmp(t1,1), 0) : 0);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 stack(t1, ft1);
 stack(t2, ft2);
 (_setjmp (taskmain) == 0 ? (longjmp(t1,1), 0) : 0);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
int __fastcall hace_stack(struct __jmp_buf_tag *a1, void (*a2)(void), unsigned int a3, __int64 a4)
{
int result; // eax
unsigned int v5; // [rsp+Ch] [rbp-Ch] BYREF

v5 = a3;
if ( a4 - (__int64)&v5 < a3 )
return hace_stack(a1, a2, a3);
result = _setjmp(a1);
if ( result )
{
a2();
abort();
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(struct __jmp_buf_tag *a1, void (*a2)(void), unsigned int a3, __int64 a4)
{
int result; // eax
unsigned int v5; // [rsp+Ch] [rbp-Ch] BYREF

v5 = a3;
if ( a4 - (__int64)&v5 < a3 )
return func7(a1, a2, a3);
result = _setjmp(a1);
if ( result )
{
a2();
abort();
}
return result;
}

original_code:
static void hace_stack (jmp_buf buf, void (*pf)(), unsigned prof, char *dummy) {
 if((dummy - (char*) & prof) >= prof) {
  if(_setjmp (buf) != 0) {
   pf(); abort();
  }
 } else {
  hace_stack(buf,pf,prof,dummy);
 }
}

canonical_original_code:
static void hace_stack(struct __jmp_buf_tag buf[1], void (*pf)(), unsigned int prof, char *dummy) {
 if((dummy - (char *) & prof) >= prof) {
  if(_setjmp (buf) != 0) {
   pf(); abort();
  }
 } else {
  hace_stack(buf,pf,prof,dummy);
 }
}

variable_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1]

variable_types:
pf
void (*)()

variable_types:
prof
unsigned int

variable_types:
dummy
char *

return_type: void

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }


stack

decompiled_code:
int __fastcall stack(struct __jmp_buf_tag *a1, void (*a2)(void))
{
int result; // eax
char v3[9]; // [rsp+Fh] [rbp-9h] BYREF

result = hace_stack(a1, a2, ctas_0 << 12, (__int64)v3);
--ctas_0;
return result;
}

canonical_decompiled_code:
int __fastcall func8(struct __jmp_buf_tag *a1, void (*a2)(void))
{
int result; // eax
char v3[9]; // [rsp+Fh] [rbp-9h] BYREF

result = func7(a1, a2, ctas_0 << 12, (__int64)v3);
--ctas_0;
return result;
}

original_code:
void stack (jmp_buf buf, void (*pf)()) {
 static int ctas = 10;
 char dummy;
 hace_stack(buf, pf, 4096 *ctas, &dummy);
 ctas--;
}

canonical_original_code:
void stack(struct __jmp_buf_tag buf[1], void (*pf)()) {
 static int ctas = 10;
 char dummy;
 hace_stack(buf, pf, 4096 *ctas, &dummy);
 ctas--;
}

variable_types: ctas int

variable_types: dummy char

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


decompiled_code:
void __noreturn ft1()
{
do
printf("d = %f", -1.0);
while ( _setjmp(t1) );
longjmp(t2, 1);
}

canonical_decompiled_code:
void __noreturn func9()
{
do
printf("d = %f", -1.0);
while ( _setjmp(t1) );
longjmp(t2, 1);
}

variable_types: d double


decompiled_code:
int ft2()
{
int result; // eax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 0; i <= 9999; ++i )
{
printf("i = %d", (unsigned int)i);
if ( !_setjmp(t2) )
longjmp(t1, 1);
}
result = _setjmp(t2);
if ( !result )
longjmp(taskmain, 1);
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax
int i; // [rsp+Ch] [rbp-Ch]

for ( i = 0; i <= 9999; ++i )
{
printf("i = %d", (unsigned int)i);
if ( !_setjmp(t2) )
longjmp(t1, 1);
}
result = _setjmp(t2);
if ( !result )
longjmp(taskmain, 1);
return result;
}

variable_types: i int


randomInit

repo: lawlesmj/showdown-combo

decompiled_code:
__int64 __fastcall randomInit(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // eax
int v5; // edx

result = (unsigned int)numTypes;
if ( (_DWORD)numTypes )
{
v2 = 0LL;
do
{
v3 = *((_DWORD *)claim + v2) - *((_DWORD *)allocated + v2);
v4 = rand();
v5 = v3 % v4;
result = v3 / v4;
*(_DWORD *)(a1 + 4 * v2++) = v5;
}
while ( (unsigned int)numTypes > (unsigned int)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // eax
int v5; // edx

result = (unsigned int)numTypes;
if ( (_DWORD)numTypes )
{
v2 = 0LL;
do
{
v3 = *((_DWORD *)claim + v2) - *((_DWORD *)allocated + v2);
v4 = rand();
v5 = v3 % v4;
result = v3 / v4;
*(_DWORD *)(a1 + 4 * v2++) = v5;
}
while ( (unsigned int)numTypes > (unsigned int)v2 );
}
return result;
}

original_code:
void randomInit(unsigned int request[]){
        int i;
        for(i = 0; i < numTypes; i++){
                request[i] = (claim[i] - allocated[i] ) % ((unsigned int)rand());
        }
}

variable_types:
request
unsigned int []


randomRelease

decompiled_code:
__int64 __fastcall randomRelease(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // eax
int v5; // edx

result = (unsigned int)numTypes;
if ( (_DWORD)numTypes )
{
v2 = 0LL;
do
{
v3 = *((_DWORD *)allocated + v2);
v4 = rand();
v5 = v3 % v4;
result = v3 / v4;
*(_DWORD *)(a1 + 4 * v2++) = v5;
}
while ( (unsigned int)numTypes > (unsigned int)v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // eax
int v5; // edx

result = (unsigned int)numTypes;
if ( (_DWORD)numTypes )
{
v2 = 0LL;
do
{
v3 = *((_DWORD *)allocated + v2);
v4 = rand();
v5 = v3 % v4;
result = v3 / v4;
*(_DWORD *)(a1 + 4 * v2++) = v5;
}
while ( (unsigned int)numTypes > (unsigned int)v2 );
}
return result;
}

original_code:
void randomRelease(unsigned int request[]){
        int i;
        for(i = 0; i < numTypes; i++){
                request[i] = (allocated[i] ) % ((unsigned int)rand());
        }
}


wait

decompiled_code:
int wait()
{
unsigned int v0; // eax

v0 = rand();
return usleep(0x2328 % v0 + 1000);
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // eax

v0 = rand();
return usleep(0x2328 % v0 + 1000);
}

original_code:
void wait(){
        unsigned int time = 1000 + 9000 % ((unsigned int)rand());
        usleep(time);
}

variable_types:
time
unsigned int


cleanUp

decompiled_code:
void __fastcall cleanUp(int a1, void *a2, void *a3)
{
switch ( a1 )
{
case -1:
case 4:
if ( msgctl(myqid, 0, 0LL) == -1 )
perror("msgctl");
goto LABEL_4;
case 0:
goto LABEL_7;
case 1:
goto LABEL_6;
case 2:
goto LABEL_5;
case 3:
LABEL_4:
free(a3);
LABEL_5:
free(a2);
LABEL_6:
free(allocated);
LABEL_7:
free(claim);
break;
default:
return;
}
}

canonical_decompiled_code:
void __fastcall func10(int a1, void *a2, void *a3)
{
switch ( a1 )
{
case -1:
case 4:
if ( msgctl(myqid, 0, 0LL) == -1 )
perror("msgctl");
goto LABEL_4;
case 0:
goto LABEL_7;
case 1:
goto LABEL_6;
case 2:
goto LABEL_5;
case 3:
LABEL_4:
free(a3);
LABEL_5:
free(a2);
LABEL_6:
free(allocated);
LABEL_7:
free(claim);
break;
default:
return;
}
}

original_code:
void cleanUp(int instance, msgbuf_t * msgBuf, msgbuf_t * rspBuf){
        switch(instance){
         case -1:
                case 4:
                        if(msgctl(myqid, 0, ((void *)0)) == -1)
                         perror("msgctl");
                case 3:
                        free(rspBuf);
  case 2:
                        free(msgBuf);
                case 1:
                        free(allocated);
                case 0:
                        free(claim);
                default:
                        break;
        }
}

canonical_original_code:
void cleanUp(int instance, struct msgbuf_t *msgBuf, struct msgbuf_t *rspBuf){
        switch(instance){
         case -1:
                case 4:
                        if(msgctl(myqid, 0, ((void *)0)) == -1)
                         perror("msgctl");
                case 3:
                        free(rspBuf);
  case 2:
                        free(msgBuf);
                case 1:
                        free(allocated);
                case 0:
                        free(claim);
                default:
                        break;
        }
}

variable_types: instance int

variable_types: struct msgbuf_t { long mtype; struct { unsigned int sender; unsigned int serialNum; unsigned int inReply; int retAddr; int resourceVector[1]; } request; } *

variable_types:
rspBuf
struct msgbuf_t *


decompiled_code:
int __fastcall display_msg(unsigned int *a1, int a2)
{
__int64 v3; // rsi
unsigned int *v4; // rbx
__int64 v5; // rsi

printf("\tClient id: %d\n", a1[2]);
v3 = a1[4];
if ( !(_DWORD)v3 )
v3 = a1[3];
printf("\tSerial number: %u\n", v3);
printf("\tReturn address: queueID %u\n", a1[5]);
printf("\tMessage type: %d-", *a1);
switch ( *(_QWORD *)a1 )
{
case 1LL:
printf("Request");
break;
case 2LL:
printf("Release");
break;
case 3LL:
printf("Register");
break;
case 4LL:
printf("Request granted");
break;
case 5LL:
printf("Request denied: unsafe");
break;
case 6LL:
printf("Request denied: excessive");
break;
case 7LL:
printf("Release successful");
break;
case 8LL:
printf("Release failed");
break;
case 9LL:
printf("Registration accepted");
break;
case 0xALL:
printf("Registration denied");
break;
case 0xBLL:
printf("Release All");
break;
case 0xCLL:
printf("Request denied: Unavailable");
break;
default:
puts("UNKNOWN");
break;
}
putchar(10);
printf("\tresources: { ");
if ( a2 )
{
v4 = a1 + 6;
do
{
v5 = *v4++;
printf("%u, ", v5);
}
while ( v4 != &a1[a2 - 1 + 7] );
}
return puts("}");
}

canonical_decompiled_code:
int __fastcall func12(unsigned int *a1, int a2)
{
__int64 v3; // rsi
unsigned int *v4; // rbx
__int64 v5; // rsi

printf("\tClient id: %d\n", a1[2]);
v3 = a1[4];
if ( !(_DWORD)v3 )
v3 = a1[3];
printf("\tSerial number: %u\n", v3);
printf("\tReturn address: queueID %u\n", a1[5]);
printf("\tMessage type: %d-", *a1);
switch ( *(_QWORD *)a1 )
{
case 1LL:
printf("Request");
break;
case 2LL:
printf("Release");
break;
case 3LL:
printf("Register");
break;
case 4LL:
printf("Request granted");
break;
case 5LL:
printf("Request denied: unsafe");
break;
case 6LL:
printf("Request denied: excessive");
break;
case 7LL:
printf("Release successful");
break;
case 8LL:
printf("Release failed");
break;
case 9LL:
printf("Registration accepted");
break;
case 0xALL:
printf("Registration denied");
break;
case 0xBLL:
printf("Release All");
break;
case 0xCLL:
printf("Request denied: Unavailable");
break;
default:
puts("UNKNOWN");
break;
}
putchar(10);
printf("\tresources: { ");
if ( a2 )
{
v4 = a1 + 6;
do
{
v5 = *v4++;
printf("%u, ", v5);
}
while ( v4 != &a1[a2 - 1 + 7] );
}
return puts("}");
}

original_code:
void display_msg(msgbuf_t * msgbuf, unsigned int numTypes) {
        int i;
        printf("\tClient id: %d\n", msgbuf->request.sender);
        if(msgbuf->request.inReply > 0)
         printf("\tSerial number: %u\n",msgbuf->request.inReply);
 else
  printf("\tSerial number: %u\n",msgbuf->request.serialNum);
        printf("\tReturn address: queueID %u\n", msgbuf->request.retAddr);
        printf("\tMessage type: %d-", (int) msgbuf->mtype);
        switch(msgbuf->mtype) {
                case 1:
                        printf("Request");
                        break;
                case 2:
                        printf("Release");
                        break;
                case 3:
                        printf("Register");
                        break;
                case 11:
                        printf("Release All");
                        break;
                case 4:
                        printf("Request granted");
                        break;
                case 5:
                        printf("Request denied: unsafe");
                        break;
                case 6:
                        printf("Request denied: excessive");
                        break;
                case 12:
                        printf("Request denied: Unavailable");
                        break;
                case 7:
                        printf("Release successful");
                        break;
                case 8:
                        printf("Release failed");
                        break;
                case 9:
                        printf("Registration accepted");
                        break;
                case 10:
                        printf("Registration denied");
                        break;
                default:
                        printf("UNKNOWN\n");
        }
        printf("\n");
        printf("\tresources: { ");
        for(i=0; i < numTypes; i++) {
                printf("%u, ", msgbuf->request.resourceVector[i]);
        }
        printf("}\n");
}

canonical_original_code:
void display_msg(struct msgbuf_t *msgbuf, unsigned int numTypes) {
        int i;
        printf("\tClient id: %d\n", msgbuf->request.sender);
        if(msgbuf->request.inReply > 0)
         printf("\tSerial number: %u\n",msgbuf->request.inReply);
 else
  printf("\tSerial number: %u\n",msgbuf->request.serialNum);
        printf("\tReturn address: queueID %u\n", msgbuf->request.retAddr);
        printf("\tMessage type: %d-", (int) msgbuf->mtype);
        switch(msgbuf->mtype) {
                case 1:
                        printf("Request");
                        break;
                case 2:
                        printf("Release");
                        break;
                case 3:
                        printf("Register");
                        break;
                case 11:
                        printf("Release All");
                        break;
                case 4:
                        printf("Request granted");
                        break;
                case 5:
                        printf("Request denied: unsafe");
                        break;
                case 6:
                        printf("Request denied: excessive");
                        break;
                case 12:
                        printf("Request denied: Unavailable");
                        break;
                case 7:
                        printf("Release successful");
                        break;
                case 8:
                        printf("Release failed");
                        break;
                case 9:
                        printf("Registration accepted");
                        break;
                case 10:
                        printf("Registration denied");
                        break;
                default:
                        printf("UNKNOWN\n");
        }
        printf("\n");
        printf("\tresources: { ");
        for(i=0; i < numTypes; i++) {
                printf("%u, ", msgbuf->request.resourceVector[i]);
        }
        printf("}\n");
}

variable_types:
numTypes
unsigned int

user_defined_types: struct msgbuf_t { long mtype; struct { unsigned int sender; unsigned int serialNum; unsigned int inReply; int retAddr; int resourceVector[1]; } request; }


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // er14
size_t v5; // r12
_DWORD *v6; // rbp
unsigned int *v7; // r13
key_t v8; // eax
int v9; // eax
int v10; // er12
__int64 v11; // rdx
const void *v12; // rsi
char *v13; // rsi
unsigned int i; // eax
char *v15; // rcx
int v16; // er15
int v17; // er14
int v18; // eax
int v19; // edi
__int64 v20; // rsi
__int64 v21; // rax
int v22; // eax
int v23; // edi
_DWORD *v24; // rax
__int64 v25; // rdx

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

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // er14
size_t v5; // r12
_DWORD *v6; // rbp
unsigned int *v7; // r13
key_t v8; // eax
int v9; // eax
int v10; // er12
__int64 v11; // rdx
const void *v12; // rsi
char *v13; // rsi
unsigned int i; // eax
char *v15; // rcx
int v16; // er15
int v17; // er14
int v18; // eax
int v19; // edi
__int64 v20; // rsi
__int64 v21; // rax
int v22; // eax
int v23; // edi
_DWORD *v24; // rax
__int64 v25; // rdx

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

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


decompiled_code:
__int64 main_thread()
{
int v0; // eax
int v1; // ebx
__int64 v2; // rbp
unsigned int v3; // eax
size_t v4; // r15
int v5; // eax
__int64 v6; // r14
int v7; // er15
__int64 v8; // rcx
__int64 v9; // rax
_QWORD *v10; // rsi
char v11; // al
__int16 v12; // ax
__int64 v13; // r8
char v14; // al
__int64 v15; // rbx
char *v16; // rsi
char *v17; // rax
unsigned __int16 v18; // bx
char *v19; // r15
__int64 v20; // rbx
char *v21; // rax
__int16 *v22; // rbx
unsigned __int16 v23; // ax
int v25; // [rsp+4h] [rbp-664h]
__int64 v26; // [rsp+8h] [rbp-660h]
__int64 v27; // [rsp+10h] [rbp-658h] BYREF
__int64 v28; // [rsp+18h] [rbp-650h] BYREF
struct timeval v29; // [rsp+20h] [rbp-648h] BYREF
struct timeval tv; // [rsp+30h] [rbp-638h] BYREF
char v31[8]; // [rsp+40h] [rbp-628h] BYREF
__int64 v32; // [rsp+48h] [rbp-620h]
char v33[40]; // [rsp+60h] [rbp-608h] BYREF
int v34; // [rsp+88h] [rbp-5E0h]
int fd; // [rsp+8Ch] [rbp-5DCh]
int v36; // [rsp+90h] [rbp-5D8h]
unsigned __int8 v37; // [rsp+94h] [rbp-5D4h]
_QWORD *v38; // [rsp+98h] [rbp-5D0h]
char v39[60]; // [rsp+A0h] [rbp-5C8h] BYREF
unsigned int v40; // [rsp+DCh] [rbp-58Ch]
int v41; // [rsp+E0h] [rbp-588h]
__int64 v42; // [rsp+F0h] [rbp-578h]
int (__fastcall *v43)(char *); // [rsp+110h] [rbp-558h]
char dest[256]; // [rsp+130h] [rbp-538h] BYREF
char buf[1080]; // [rsp+230h] [rbp-438h] BYREF

v27 = 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 )
{
v26 = v0;
v1 = 20;
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);
while ( 1 )
{
while ( (int)recv_msg_q(v40, v31, 24LL, 1LL, 2048LL) >= 0 )
{
v2 = v32;
switch ( v34 )
{
case 65536:
goto LABEL_31;
case 65537:
goto LABEL_16;
case 65538:
if ( !v32 )
goto LABEL_50;
v1 = 20;
if ( *(_WORD *)(v32 + 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_14;
case 65539:
goto LABEL_6;
case 65540:
if ( !v32 )
goto LABEL_48;
if ( *(_BYTE *)(v32 + 2) == 1 )
{
v14 = *(_BYTE *)(v32 + 3);
if ( v14 == 3 )
{
v19 = (char *)(v32 + 7);
t_log("[server]continue transmit");
v20 = *(unsigned __int8 *)(v2 + 6);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v21 = &dest[strlen(dest)];
if ( (unsigned int)v20 < 8 )
{
if ( (v20 & 4) != 0 )
{
*(_DWORD *)v21 = *(_DWORD *)v19;
*(_DWORD *)&v21[v20 - 4] = *(_DWORD *)&v19[v20 - 4];
}
else if ( (_DWORD)v20 )
{
*v21 = *v19;
if ( (v20 & 2) != 0 )
*(_WORD *)&v21[v20 - 2] = *(_WORD *)&v19[v20 - 2];
}
}
else
{
*(_QWORD *)v21 = *(_QWORD *)(v2 + 7);
*(_QWORD *)&v21[v20 - 8] = *(_QWORD *)&v19[v20 - 8];
qmemcpy(
(void *)((unsigned __int64)(v21 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v19 - &v21[-((unsigned __int64)(v21 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v20 + (_DWORD)v21 - (((_DWORD)v21 + 8) & 0xFFFFFFF8)) >> 3));
}
v22 = (__int16 *)&v19[v20];
v23 = _bswap_16(*v22);
if ( (int)set_file_desc(v26, v23) < 0 )
{
v1 = 20;
t_log("[re_transmit]file error!");
v34 = 65540;
}
else if ( (int)handle_re_transimit_frame(v26, v22 + 1) < 0 )
{
v1 = 20;
clear_file_desc(v26);
v34 = 65540;
}
else
{
v34 = 65541;
v1 = 20;
}
}
else
{
v1 = 20;
if ( v14 == 5 )
{
v15 = *(unsigned __int8 *)(v32 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v16 = (char *)(v2 + 8);
v17 = &dest[strlen(dest)];
if ( (unsigned int)v15 < 8 )
{
if ( (v15 & 4) != 0 )
{
*(_DWORD *)v17 = *(_DWORD *)v16;
*(_DWORD *)&v17[v15 - 4] = *(_DWORD *)&v16[v15 - 4];
}
else if ( (_DWORD)v15 )
{
*v17 = *v16;
if ( (v15 & 2) != 0 )
*(_WORD *)&v17[v15 - 2] = *(_WORD *)&v16[v15 - 2];
}
}
else
{
*(_QWORD *)v17 = *(_QWORD *)(v2 + 8);
*(_QWORD *)&v17[v15 - 8] = *(_QWORD *)(v2 + v15);
qmemcpy(
(void *)((unsigned __int64)(v17 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v16 - &v17[-((unsigned __int64)(v17 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v15 + (_DWORD)v17 - (((_DWORD)v17 + 8) & 0xFFFFFFF8)) >> 3));
}
v18 = _bswap_16(*(_WORD *)(v2 + v15 + 8));
t_log("[server]download file: %s ", dest);
set_file_desc(v26, v18);
v1 = 20;
init_send_list(v26);
t_log("[client]sending file...");
gettimeofday(&v29, 0LL);
v34 = 65541;
}
}
}
else
{
v1 = 20;
t_log("receive type error");
}
goto LABEL_14;
case 65541:
if ( !v32 )
goto LABEL_52;
if ( *(_BYTE *)(v32 + 2) != 1 )
{
v1 = 20;
t_log("[client]receive error");
LABEL_9:
if ( v27 )
{
t_free(v27);
v27 = 0LL;
}
if ( v28 )
{
t_free(v28);
v28 = 0LL;
}
goto LABEL_13;
}
v11 = *(_BYTE *)(v32 + 3);
if ( v11 == 3 )
{
v1 = 20;
t_log("[server]retransmit");
if ( (int)handle_re_transimit_frame(v42, v2 + *(unsigned __int8 *)(v2 + 6) + 9) < 0 )
{
clear_file_desc(v42);
v34 = 65540;
}
}
else
{
if ( v11 == 8 )
{
gettimeofday(&tv, 0LL);
t_log(
"time_used = %f s",
(float)((float)(LODWORD(tv.tv_usec)
+ 1000000 * (LODWORD(tv.tv_sec) - LODWORD(v29.tv_sec))
- LODWORD(v29.tv_usec))
/ 1000000.0));
t_log("[server]transfer finished");
v12 = _bswap_16(*(_WORD *)(v2 + 6));
if ( *(_WORD *)(v13 + 12) != v12 )
{
v1 = 20;
puts("[finish]frame file id error!");
goto LABEL_14;
}
clear_file_desc(v13);
v34 = 65540;
sleep(2u);
}
v1 = 20;
}
LABEL_14:
t_free(v2);
break;
case 65542:
t_log("[state]transfer finished");
v34 = 65540;
goto LABEL_13;
case 65543:
goto LABEL_29;
default:
goto LABEL_13;
}
}
switch ( v34 )
{
case 65536:
v2 = 0LL;
LABEL_31:
if ( v43(v33) < 0 )
{
puts("[client]connect to server failed!");
sleep(1u);
}
else
{
v34 = 65537;
create_thread(&t_recv, &receive_handler, v33);
}
goto LABEL_13;
case 65537:
v2 = 0LL;
LABEL_16:
if ( fd < 0 )
{
t_log("connection socket lost!");
v34 = 65543;
if ( v27 )
goto LABEL_64;
}
else
{
if ( (int)frame_header_init(1LL, 1LL, &v27) < 0 )
{
t_log("frame init failed!");
if ( !v27 )
goto LABEL_13;
LABEL_64:
((void (*)(void))t_free)();
v27 = 0LL;
goto LABEL_13;
}
v5 = t_malloc((unsigned int)v37 + 1);
v6 = v5;
v7 = v5;
if ( v5 )
{
v8 = v5 + 1LL;
*(_BYTE *)v5 = v37;
v9 = v37;
v10 = v38;
if ( v37 >= 8u )
{
*(_QWORD *)(v6 + 1) = *v38;
*(_QWORD *)(v8 + v9 - 8) = *(_QWORD *)((char *)v10 + v9 - 8);
qmemcpy(
(void *)((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(char *)v10 - (v8 - ((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL)),
8LL * (((unsigned int)v9 + (_DWORD)v8 - (((_DWORD)v6 + 9) & 0xFFFFFFF8)) >> 3));
}
else if ( (v37 & 4) != 0 )
{
*(_DWORD *)(v6 + 1) = *(_DWORD *)v38;
*(_DWORD *)(v8 + v9 - 4) = *(_DWORD *)((char *)v10 + v9 - 4);
}
else if ( v37 )
{
*(_BYTE *)(v6 + 1) = *(_BYTE *)v38;
if ( (v9 & 2) != 0 )
*(_WORD *)(v8 + v9 - 2) = *(_WORD *)((char *)v10 + v9 - 2);
}
v25 = frame_build(v27, v6, (unsigned int)v37 + 1, buf);
if ( v25 == (unsigned int)send(fd, buf, v25, 0) )
{
t_log("[client]login sent");
v34 = 65538;
if ( !v27 )
{
LABEL_27:
t_free(v6);
goto LABEL_13;
}
}
else
{
t_log("[login]lost connection!");
v34 = 65543;
if ( !v27 )
goto LABEL_27;
}
}
else
{
t_log("malloc login_buf frame error!");
if ( !v27 )
goto LABEL_13;
}
((void (*)(void))t_free)();
v27 = 0LL;
if ( v7 )
goto LABEL_27;
}
LABEL_13:
if ( v2 )
goto LABEL_14;
break;
case 65538:
LABEL_50:
if ( v1 )
goto LABEL_66;
v34 = 65543;
v1 = 20;
continue;
case 65539:
v2 = 0LL;
LABEL_6:
if ( (int)frame_header_init(1LL, 4LL, &v27) < 0 )
{
t_log("frame init failed!");
}
else
{
v3 = get_file_info(&g_file_path, &v28);
v4 = (int)frame_build(v27, v28, v3, buf);
if ( (_DWORD)v4 == (unsigned int)send(fd, buf, v4, 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_9;
case 65540:
LABEL_48:
if ( v1 )
{
LABEL_66:
--v1;
sleep(1u);
}
else
{
v1 = 20;
clear_file_desc(v42);
v34 = 65540;
t_log("[client]timeout! all file tranfer completed!");
}
continue;
case 65541:
LABEL_52:
sleep(1u);
continue;
case 65542:
t_log("[state]transfer finished");
v34 = 65540;
continue;
case 65543:
v2 = 0LL;
LABEL_29:
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_13;
default:
continue;
}
}
}
return t_log("malloc f_desc frame error!");
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
int v1; // ebx
__int64 v2; // rbp
unsigned int v3; // eax
size_t v4; // r15
int v5; // eax
__int64 v6; // r14
int v7; // er15
__int64 v8; // rcx
__int64 v9; // rax
_QWORD *v10; // rsi
char v11; // al
__int16 v12; // ax
__int64 v13; // r8
char v14; // al
__int64 v15; // rbx
char *v16; // rsi
char *v17; // rax
unsigned __int16 v18; // bx
char *v19; // r15
__int64 v20; // rbx
char *v21; // rax
__int16 *v22; // rbx
unsigned __int16 v23; // ax
int v25; // [rsp+4h] [rbp-664h]
__int64 v26; // [rsp+8h] [rbp-660h]
__int64 v27; // [rsp+10h] [rbp-658h] BYREF
__int64 v28; // [rsp+18h] [rbp-650h] BYREF
struct timeval v29; // [rsp+20h] [rbp-648h] BYREF
struct timeval tv; // [rsp+30h] [rbp-638h] BYREF
char v31[8]; // [rsp+40h] [rbp-628h] BYREF
__int64 v32; // [rsp+48h] [rbp-620h]
char v33[40]; // [rsp+60h] [rbp-608h] BYREF
int v34; // [rsp+88h] [rbp-5E0h]
int fd; // [rsp+8Ch] [rbp-5DCh]
int v36; // [rsp+90h] [rbp-5D8h]
unsigned __int8 v37; // [rsp+94h] [rbp-5D4h]
_QWORD *v38; // [rsp+98h] [rbp-5D0h]
char v39[60]; // [rsp+A0h] [rbp-5C8h] BYREF
unsigned int v40; // [rsp+DCh] [rbp-58Ch]
int v41; // [rsp+E0h] [rbp-588h]
__int64 v42; // [rsp+F0h] [rbp-578h]
int (__fastcall *v43)(char *); // [rsp+110h] [rbp-558h]
char dest[256]; // [rsp+130h] [rbp-538h] BYREF
char buf[1080]; // [rsp+230h] [rbp-438h] BYREF

v27 = 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 )
{
v26 = v0;
v1 = 20;
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);
while ( 1 )
{
while ( (int)recv_msg_q(v40, v31, 24LL, 1LL, 2048LL) >= 0 )
{
v2 = v32;
switch ( v34 )
{
case 65536:
goto LABEL_31;
case 65537:
goto LABEL_16;
case 65538:
if ( !v32 )
goto LABEL_50;
v1 = 20;
if ( *(_WORD *)(v32 + 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_14;
case 65539:
goto LABEL_6;
case 65540:
if ( !v32 )
goto LABEL_48;
if ( *(_BYTE *)(v32 + 2) == 1 )
{
v14 = *(_BYTE *)(v32 + 3);
if ( v14 == 3 )
{
v19 = (char *)(v32 + 7);
t_log("[server]continue transmit");
v20 = *(unsigned __int8 *)(v2 + 6);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v21 = &dest[strlen(dest)];
if ( (unsigned int)v20 < 8 )
{
if ( (v20 & 4) != 0 )
{
*(_DWORD *)v21 = *(_DWORD *)v19;
*(_DWORD *)&v21[v20 - 4] = *(_DWORD *)&v19[v20 - 4];
}
else if ( (_DWORD)v20 )
{
*v21 = *v19;
if ( (v20 & 2) != 0 )
*(_WORD *)&v21[v20 - 2] = *(_WORD *)&v19[v20 - 2];
}
}
else
{
*(_QWORD *)v21 = *(_QWORD *)(v2 + 7);
*(_QWORD *)&v21[v20 - 8] = *(_QWORD *)&v19[v20 - 8];
qmemcpy(
(void *)((unsigned __int64)(v21 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v19 - &v21[-((unsigned __int64)(v21 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v20 + (_DWORD)v21 - (((_DWORD)v21 + 8) & 0xFFFFFFF8)) >> 3));
}
v22 = (__int16 *)&v19[v20];
v23 = func1(*v22);
if ( (int)set_file_desc(v26, v23) < 0 )
{
v1 = 20;
t_log("[re_transmit]file error!");
v34 = 65540;
}
else if ( (int)handle_re_transimit_frame(v26, v22 + 1) < 0 )
{
v1 = 20;
clear_file_desc(v26);
v34 = 65540;
}
else
{
v34 = 65541;
v1 = 20;
}
}
else
{
v1 = 20;
if ( v14 == 5 )
{
v15 = *(unsigned __int8 *)(v32 + 7);
memset(dest, 0, sizeof(dest));
strcpy(dest, &g_file_path);
v16 = (char *)(v2 + 8);
v17 = &dest[strlen(dest)];
if ( (unsigned int)v15 < 8 )
{
if ( (v15 & 4) != 0 )
{
*(_DWORD *)v17 = *(_DWORD *)v16;
*(_DWORD *)&v17[v15 - 4] = *(_DWORD *)&v16[v15 - 4];
}
else if ( (_DWORD)v15 )
{
*v17 = *v16;
if ( (v15 & 2) != 0 )
*(_WORD *)&v17[v15 - 2] = *(_WORD *)&v16[v15 - 2];
}
}
else
{
*(_QWORD *)v17 = *(_QWORD *)(v2 + 8);
*(_QWORD *)&v17[v15 - 8] = *(_QWORD *)(v2 + v15);
qmemcpy(
(void *)((unsigned __int64)(v17 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v16 - &v17[-((unsigned __int64)(v17 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v15 + (_DWORD)v17 - (((_DWORD)v17 + 8) & 0xFFFFFFF8)) >> 3));
}
v18 = func1(*(_WORD *)(v2 + v15 + 8));
t_log("[server]download file: %s ", dest);
set_file_desc(v26, v18);
v1 = 20;
init_send_list(v26);
t_log("[client]sending file...");
gettimeofday(&v29, 0LL);
v34 = 65541;
}
}
}
else
{
v1 = 20;
t_log("receive type error");
}
goto LABEL_14;
case 65541:
if ( !v32 )
goto LABEL_52;
if ( *(_BYTE *)(v32 + 2) != 1 )
{
v1 = 20;
t_log("[client]receive error");
LABEL_9:
if ( v27 )
{
t_free(v27);
v27 = 0LL;
}
if ( v28 )
{
t_free(v28);
v28 = 0LL;
}
goto LABEL_13;
}
v11 = *(_BYTE *)(v32 + 3);
if ( v11 == 3 )
{
v1 = 20;
t_log("[server]retransmit");
if ( (int)handle_re_transimit_frame(v42, v2 + *(unsigned __int8 *)(v2 + 6) + 9) < 0 )
{
clear_file_desc(v42);
v34 = 65540;
}
}
else
{
if ( v11 == 8 )
{
gettimeofday(&tv, 0LL);
t_log(
"time_used = %f s",
(float)((float)(LODWORD(tv.tv_usec)
+ 1000000 * (LODWORD(tv.tv_sec) - LODWORD(v29.tv_sec))
- LODWORD(v29.tv_usec))
/ 1000000.0));
t_log("[server]transfer finished");
v12 = func1(*(_WORD *)(v2 + 6));
if ( *(_WORD *)(v13 + 12) != v12 )
{
v1 = 20;
puts("[finish]frame file id error!");
goto LABEL_14;
}
clear_file_desc(v13);
v34 = 65540;
sleep(2u);
}
v1 = 20;
}
LABEL_14:
t_free(v2);
break;
case 65542:
t_log("[state]transfer finished");
v34 = 65540;
goto LABEL_13;
case 65543:
goto LABEL_29;
default:
goto LABEL_13;
}
}
switch ( v34 )
{
case 65536:
v2 = 0LL;
LABEL_31:
if ( v43(v33) < 0 )
{
puts("[client]connect to server failed!");
sleep(1u);
}
else
{
v34 = 65537;
create_thread(&t_recv, &receive_handler, v33);
}
goto LABEL_13;
case 65537:
v2 = 0LL;
LABEL_16:
if ( fd < 0 )
{
t_log("connection socket lost!");
v34 = 65543;
if ( v27 )
goto LABEL_64;
}
else
{
if ( (int)frame_header_init(1LL, 1LL, &v27) < 0 )
{
t_log("frame init failed!");
if ( !v27 )
goto LABEL_13;
LABEL_64:
((void (*)(void))t_free)();
v27 = 0LL;
goto LABEL_13;
}
v5 = t_malloc((unsigned int)v37 + 1);
v6 = v5;
v7 = v5;
if ( v5 )
{
v8 = v5 + 1LL;
*(_BYTE *)v5 = v37;
v9 = v37;
v10 = v38;
if ( v37 >= 8u )
{
*(_QWORD *)(v6 + 1) = *v38;
*(_QWORD *)(v8 + v9 - 8) = *(_QWORD *)((char *)v10 + v9 - 8);
qmemcpy(
(void *)((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL),
(char *)v10 - (v8 - ((v6 + 9) & 0xFFFFFFFFFFFFFFF8LL)),
8LL * (((unsigned int)v9 + (_DWORD)v8 - (((_DWORD)v6 + 9) & 0xFFFFFFF8)) >> 3));
}
else if ( (v37 & 4) != 0 )
{
*(_DWORD *)(v6 + 1) = *(_DWORD *)v38;
*(_DWORD *)(v8 + v9 - 4) = *(_DWORD *)((char *)v10 + v9 - 4);
}
else if ( v37 )
{
*(_BYTE *)(v6 + 1) = *(_BYTE *)v38;
if ( (v9 & 2) != 0 )
*(_WORD *)(v8 + v9 - 2) = *(_WORD *)((char *)v10 + v9 - 2);
}
v25 = frame_build(v27, v6, (unsigned int)v37 + 1, buf);
if ( v25 == (unsigned int)send(fd, buf, v25, 0) )
{
t_log("[client]login sent");
v34 = 65538;
if ( !v27 )
{
LABEL_27:
t_free(v6);
goto LABEL_13;
}
}
else
{
t_log("[login]lost connection!");
v34 = 65543;
if ( !v27 )
goto LABEL_27;
}
}
else
{
t_log("malloc login_buf frame error!");
if ( !v27 )
goto LABEL_13;
}
((void (*)(void))t_free)();
v27 = 0LL;
if ( v7 )
goto LABEL_27;
}
LABEL_13:
if ( v2 )
goto LABEL_14;
break;
case 65538:
LABEL_50:
if ( v1 )
goto LABEL_66;
v34 = 65543;
v1 = 20;
continue;
case 65539:
v2 = 0LL;
LABEL_6:
if ( (int)frame_header_init(1LL, 4LL, &v27) < 0 )
{
t_log("frame init failed!");
}
else
{
v3 = get_file_info(&g_file_path, &v28);
v4 = (int)frame_build(v27, v28, v3, buf);
if ( (_DWORD)v4 == (unsigned int)send(fd, buf, v4, 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_9;
case 65540:
LABEL_48:
if ( v1 )
{
LABEL_66:
--v1;
sleep(1u);
}
else
{
v1 = 20;
clear_file_desc(v42);
v34 = 65540;
t_log("[client]timeout! all file tranfer completed!");
}
continue;
case 65541:
LABEL_52:
sleep(1u);
continue;
case 65542:
t_log("[state]transfer finished");
v34 = 65540;
continue;
case 65543:
v2 = 0LL;
LABEL_29:
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_13;
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

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


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:
// attributes: thunk
__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:
// attributes: thunk
int lock_t_lock(pthread_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}

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

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:
// attributes: thunk
int unlock_t_lock(pthread_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}

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

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:
// attributes: thunk
int t_release(sem_t *sem)
{
return sem_post(sem);
}

canonical_decompiled_code:
int func6(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:
// attributes: thunk
int t_aquire(sem_t *sem)
{
return sem_wait(sem);
}

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

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


decompiled_code:
// attributes: thunk
int t_aquire_nb(sem_t *sem)
{
return sem_trywait(sem);
}

canonical_decompiled_code:
int func8(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, _QWORD *a3)
{
int v4; // eax
__int64 result; // rax

v4 = t_malloc(6LL);
*a3 = v4;
if ( v4 )
{
if ( a1 > 2u || a2 > 6u )
{
t_log("type unknown");
result = 0xFFFFFFFFLL;
}
else
{
*((_BYTE *)&loc_2 + v4) = a1;
*((_BYTE *)&loc_2 + v4 + 1) = a2;
*(_WORD *)((char *)&loc_2 + v4 + 2) = 0;
*(_WORD *)v4 = 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, _QWORD *a3)
{
int v4; // eax
__int64 result; // rax

v4 = t_malloc(6LL);
*a3 = v4;
if ( v4 )
{
if ( a1 > 2u || a2 > 6u )
{
t_log("type unknown");
result = 0xFFFFFFFFLL;
}
else
{
*((_BYTE *)&loc_2 + v4) = a1;
*((_BYTE *)&loc_2 + v4 + 1) = a2;
*(_WORD *)((char *)&loc_2 + v4 + 2) = 0;
*(_WORD *)v4 = 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; } **


decompiled_code:
__int64 __fastcall frame_heartbeat_init(__int64 a1)
{
int v1; // eax
__int16 *v2; // rdx
__int16 *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = 0LL;
frame_header_init(0, 0, &v4);
*v4 = _bswap_16(5);
v1 = t_malloc(7LL);
v2 = v4;
*(_QWORD *)a1 = v1;
*(_DWORD *)v1 = *(_DWORD *)v2;
*(_WORD *)((char *)&loc_2 + v1 + 2) = v2[2];
*(_BYTE *)(*(_QWORD *)a1 + 6LL) = -1;
t_free(v4);
return 0LL;
}

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