decompiled_code:
unsigned __int16 *__fastcall fat_sb_create(unsigned __int8 *a1)
{
unsigned __int16 *v1; // rax
unsigned __int16 *v2; // rbp

if ( !a1 )
return 0LL;
printf("MRB info %x,%x,%x\n", *a1, a1[1], a1[2]);
v1 = (unsigned __int16 *)kzalloc(64LL, 3264LL);
v2 = v1;
if ( v1 && !(unsigned int)fat_sb_read(v1, (__int64)a1) )
{
fat_sb_print(v2);
}
else
{
fat_sb_free(v2);
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
unsigned __int16 *__fastcall func8(unsigned __int8 *a1)
{
unsigned __int16 *v1; // rax
unsigned __int16 *v2; // rbp

if ( !a1 )
return 0LL;
printf("MRB info %x,%x,%x\n", *a1, a1[1], a1[2]);
v1 = (unsigned __int16 *)kzalloc(64LL, 3264LL);
v2 = v1;
if ( v1 && !(unsigned int)func5(v1, (__int64)a1) )
{
func6(v2);
}
else
{
func7(v2);
v2 = 0LL;
}
return v2;
}

original_code:
struct fat_sb * fat_sb_create(u8 * data, unsigned long size)
{
    struct fat_sb * fsb;
    if(!data){
        return ((void *)0);
    }
    printf("MRB info %x,%x,%x\n", data[0], data[1], data[2]);
    fsb = (struct fat_sb*)kzalloc(sizeof(struct fat_sb), (((gfp_t)(0x400u|0x800u)) | ((gfp_t)0x40u) | ((gfp_t)0x80u)));
    if(fsb && !fat_sb_read(fsb, (const struct fat_boot_sector*)data)){
        fat_sb_print(fsb);
        return fsb;
    }
    fat_sb_free(fsb);
    return ((void *)0);
}

canonical_original_code:
struct fat_sb *fat_sb_create(unsigned char *data, unsigned long size)
{
    struct fat_sb *fsb;
    if(!data){
        return ((void *)0);
    }
    printf("MRB info %x,%x,%x\n", data[0], data[1], data[2]);
    fsb = (struct fat_sb *)kzalloc(sizeof(struct fat_sb), (((gfp_t)(0x400u|0x800u)) | ((unsigned int)0x40u) | ((unsigned int)0x80u)));
    if(fsb && !fat_sb_read(fsb, (struct fat_boot_sector *)data)){
        fat_sb_print(fsb);
        return fsb;
    }
    fat_sb_free(fsb);
    return ((void *)0);
}

variable_types:
data
unsigned char *

variable_types:
size
unsigned long

return_type: struct fat_sb { unsigned short sector_size; unsigned char fat_bits; unsigned int fsinfo_sector; unsigned short fat_sector_start; unsigned int fat_length; unsigned char fats; unsigned int root_dir_sector_start; unsigned short root_dir_entries; unsigned int data_sector_start; unsigned int max_sectors; unsigned short sec_per_clus; unsigned int root_cluster; unsigned int max_cluster; unsigned int cluster_size; unsigned short cluster_bits; int dir_per_block; int dir_per_block_bits; } *


kzalloc

repo: yunccll/fat

decompiled_code:
void *__fastcall kzalloc(size_t a1)
{
return calloc(a1, 1uLL);
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
return calloc(a1, 1uLL);
}

original_code:
void * kzalloc(size_t size, gfp_t flags){
 return calloc(size, 1);
}

canonical_original_code:
void *kzalloc(unsigned long size, unsigned int flags){
 return calloc(size, 1);
}

variable_types:
flags
unsigned int

return_type: void *


kfree

decompiled_code:
void __fastcall kfree(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
free(a1);
}

original_code:
void kfree(void *p){
 free(p);
}

canonical_original_code:
void kfree(void *p){
 free(p);
}

variable_types:
p
void *

return_type: void


decompiled_code:
_BOOL8 __fastcall is_power_of_2(__int64 a1)
{
bool v1; // al

v1 = 0;
if ( a1 )
v1 = (a1 & (a1 - 1)) == 0;
return v1;
}

original_code:
static inline bool is_power_of_2(unsigned long n)
{
    return (n != 0 && ((n & (n - 1)) == 0));
}

canonical_original_code:
static inline _Bool is_power_of_2(unsigned long n)
{
    return (n != 0 && ((n & (n - 1)) == 0));
}

variable_types:
n
unsigned long


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

LODWORD(result) = 8;
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 > 0x100 );
return result;
}

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

LODWORD(result) = 8;
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 > 0x100 );
return result;
}

original_code:
static unsigned int blksize_bits(unsigned int size)
{
    unsigned int bits = 8;
    do {
        bits++;
        size >>= 1;
    } while (size > 256);
    return bits;
}

variable_types:
size
unsigned int

variable_types:
bits
unsigned int

return_type: unsigned int


decompiled_code:
__int64 test_fat_device()
{
return test_device();
}

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

original_code:
void test_fat_device(void){
    test_device();
}

canonical_original_code:
void test_fat_device(){
    test_device();
}


decompiled_code:
bool __fastcall atomic_dec_and_test(_DWORD *a1)
{
return (*a1)-- == 1;
}

canonical_decompiled_code:
bool __fastcall func3(_DWORD *a1)
{
return (*a1)-- == 1;
}

original_code:
static inline bool atomic_dec_and_test(atomic_t *v)
{
    return (--v->counter) == 0 ? 1 : 0;
}

canonical_original_code:
static inline _Bool atomic_dec_and_test(struct atomic_t *v)
{
    return (--v->counter) == 0 ? 1 : 0;
}

variable_types:
v
struct atomic_t { int counter; } *


decompiled_code:
__int64 __fastcall atomic_inc_not_zero(_DWORD *a1)
{
unsigned int v1; // edx

v1 = 0;
if ( *a1 )
{
++*a1;
v1 = 1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(_DWORD *a1)
{
unsigned int v1; // edx

v1 = 0;
if ( *a1 )
{
++*a1;
v1 = 1;
}
return v1;
}

original_code:
static inline bool atomic_inc_not_zero(atomic_t *v)
{
    if(v->counter != 0){
        ++v->counter;
        return 1;
    }
    return 0;
}

canonical_original_code:
static inline _Bool atomic_inc_not_zero(struct atomic_t *v)
{
    if(v->counter != 0){
        ++v->counter;
        return 1;
    }
    return 0;
}


decompiled_code:
bool __fastcall IS_ERR(__int64 a1)
{
return (unsigned __int64)(a1 + 4095) <= 0xFFF;
}

canonical_decompiled_code:
bool __fastcall func5(__int64 a1)
{
return (unsigned __int64)(a1 + 4095) <= 0xFFF;
}

original_code:
static inline bool IS_ERR(const void * ptr)
{
    return (((long)(void *)(ptr)) <= 0 && ((long)(void *)(ptr)) >= -4095);
}

canonical_original_code:
static inline _Bool IS_ERR(void *ptr)
{
    return (((long)(void *)(ptr)) <= 0 && ((long)(void *)(ptr)) >= -4095);
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall PTR_ERR(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return a1;
}

original_code:
static inline long PTR_ERR(const void * ptr)
{
    return (long)ptr;
}

canonical_original_code:
static inline long PTR_ERR(void *ptr)
{
    return (long)ptr;
}

return_type: long


decompiled_code:
__int64 __fastcall ERR_PTR(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return a1;
}

original_code:
static inline void * ERR_PTR(long error)
{
    return (void *) error;
}

canonical_original_code:
static inline void *ERR_PTR(long error)
{
    return (void *) error;
}

variable_types: error long


decompiled_code:
__int64 fat_make_fs()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0xFFFFFFFFLL;
}

original_code:
int fat_make_fs(fat_dev_t * pdev){
    return -1;
}

canonical_original_code:
int fat_make_fs(struct fat_dev_s *pdev){
    return -1;
}

variable_types:
pdev
struct fat_dev_s { int fd; } *

return_type: int


decompiled_code:
void *__fastcall list_add(_QWORD *a1, _DWORD *a2)
{
return _list_add(a1, a2, *(_QWORD *)a2);
}

canonical_decompiled_code:
void *__fastcall func8(_QWORD *a1, _DWORD *a2)
{
return func7(a1, a2, *(_QWORD *)a2);
}

original_code:
static inline void list_add(struct list_head *new, struct list_head *head)
{
 __list_add(new, head, head->next);
}

variable_types: struct list_head { struct list_head *next; } *

variable_types:
head
struct list_head *


canonical_decompiled_code:
bool __fastcall func0(_DWORD *a1)
{
return (*a1)-- == 1;
}


repo: yunfengHe/UnixC

decompiled_code:
ssize_t __fastcall sig_parentsideint(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

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

canonical_decompiled_code:
ssize_t __fastcall func7(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

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

original_code:
static void sig_parentsideint(int signo){
  char rett = signo;
  write(sigpipe1, &rett,1);
}

variable_types: signo int

variable_types: rett char


decompiled_code:
ssize_t __fastcall sig_usr(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

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

canonical_decompiled_code:
ssize_t __fastcall func8(char a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

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

original_code:
static void sig_usr(int signo){
 char ret = signo;
 write(sigpipe,&ret,1);
}

variable_types: ret char


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int result; // eax
int v5; // ebp
__pid_t v6; // ebx
char buf; // [rsp+Fh] [rbp-169h] BYREF
struct sigaction v8; // [rsp+10h] [rbp-168h] BYREF
struct sigaction v9; // [rsp+B0h] [rbp-C8h] BYREF
int v10[2]; // [rsp+150h] [rbp-28h] BYREF
int pipedes[2]; // [rsp+158h] [rbp-20h] BYREF

if ( pipe(pipedes) )
__assert_fail("pipe(pipefd) == 0", "IPCpipe.c", 0x1Fu, "main");
sigpipe = pipedes[1];
if ( pipe(v10) )
__assert_fail("pipe(pipefd1) == 0", "IPCpipe.c", 0x22u, "main");
sigpipe1 = v10[1];
sigemptyset(&v9.sa_mask);
v9.sa_flags = 0;
v9.sa_handler = (__sighandler_t)sig_usr;
if ( sigaction(10, &v9, 0LL) )
__assert_fail("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 0x2Du, "main");
if ( sigaction(12, &v9, 0LL) )
__assert_fail("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 0x2Eu, "main");
sigemptyset(&v8.sa_mask);
v8.sa_flags = 0;
v8.sa_handler = (__sighandler_t)sig_parentsideint;
if ( sigaction(2, &v8, 0LL) )
__assert_fail("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 0x35u, "main");
v3 = fork();
if ( v3 > 0 )
{
close(pipedes[0]);
close(pipedes[1]);
while ( (unsigned int)read(v10[0], &buf, 1uLL) != 1 )
;
v5 = 10;
do
{
kill(v3, 10);
while ( (unsigned int)read(v10[0], &buf, 1uLL) != 1 )
;
kill(v3, 12);
while ( (unsigned int)read(v10[0], &buf, 1uLL) != 1 )
;
--v5;
}
while ( v5 );
result = kill(v3, 9);
}
else
{
result = 0;
if ( !v3 )
{
close(v10[1]);
close(v10[0]);
v6 = getppid();
while ( 1 )
{
kill(v6, 2);
while ( (unsigned int)read(pipedes[0], &buf, 1uLL) != 1 )
;
if ( buf == 10 )
{
puts("received SIGUSR1");
}
else if ( buf == 12 )
{
puts("received SIGUSR2");
}
}
}
}
return result;
}

original_code:
int main(void){
 int pipefd[2];
 int pipefd1[2];
 ((void) sizeof ((pipe(pipefd) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd) == 0) ; else __assert_fail ("pipe(pipefd) == 0", "IPCpipe.c", 31, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe = pipefd[1];
 ((void) sizeof ((pipe(pipefd1) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd1) == 0) ; else __assert_fail ("pipe(pipefd1) == 0", "IPCpipe.c", 34, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe1 = pipefd1[1];
 pid_t tpid;
 struct sigaction sig;
 sigemptyset(&sig.sa_mask);
 sig.sa_flags = 0;
 sig.__sigaction_handler.sa_handler = sig_usr;
 ((void) sizeof (((sigaction(10, &sig,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(10, &sig,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof (((sigaction(12, &sig,((void *)0))) == 0) ? 1 : 0), __extension__ ({ if ((sigaction(12, &sig,((void *)0))) == 0) ; else __assert_fail ("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 46, __extension__ __PRETTY_FUNCTION__); }));
 struct sigaction sig1;
 sigemptyset(&sig1.sa_mask);
 sig1.sa_flags = 0;
 sig1.__sigaction_handler.sa_handler = sig_parentsideint;
 ((void) sizeof (((sigaction(2, &sig1,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(2, &sig1,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 tpid = fork();
 if (tpid>0){
  close(pipefd[0]);
  close(pipefd[1]);
  int i=0;
  for(;;){
   char mysignal;
   int res = read(pipefd1[0],&mysignal,1);
   if (res == 1)
    break;
   else
    continue;
  }
  for (i=0; i<10;i++){
   kill(tpid, 10);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
   kill(tpid,12);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
  }
  kill(tpid,9);
  return;
 }
 if(tpid == 0){
  close(pipefd1[1]);
  close(pipefd1[0]);
  pid_t ppid = getppid();
  for(;;){
   kill(ppid,2);
   for(;;){
    char mysignal;
    int res = read(pipefd[0],&mysignal,1);
    if (res == 1){
     if (mysignal == 10)
            printf("received SIGUSR1\n");
            else if (mysignal == 12)
      printf("received SIGUSR2\n");
     break;
     }
    else
     continue;
   }
  }
 }
 return 0;
}

canonical_original_code:
int main(){
 int pipefd[2];
 int pipefd1[2];
 ((void) sizeof ((pipe(pipefd) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd) == 0) ; else __assert_fail ("pipe(pipefd) == 0", "IPCpipe.c", 31, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe = pipefd[1];
 ((void) sizeof ((pipe(pipefd1) == 0) ? 1 : 0), __extension__ ({ if (pipe(pipefd1) == 0) ; else __assert_fail ("pipe(pipefd1) == 0", "IPCpipe.c", 34, __extension__ __PRETTY_FUNCTION__); }));
 sigpipe1 = pipefd1[1];
 int tpid;
 struct sigaction sig;
 sigemptyset(&sig.sa_mask);
 sig.sa_flags = 0;
 sig.__sigaction_handler.sa_handler = sig_usr;
 ((void) sizeof (((sigaction(10, &sig,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(10, &sig,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGUSR1, &sig,NULL))== 0", "IPCpipe.c", 45, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof (((sigaction(12, &sig,((void *)0))) == 0) ? 1 : 0), __extension__ ({ if ((sigaction(12, &sig,((void *)0))) == 0) ; else __assert_fail ("(sigaction(SIGUSR2, &sig,NULL)) == 0", "IPCpipe.c", 46, __extension__ __PRETTY_FUNCTION__); }));
 struct sigaction sig1;
 sigemptyset(&sig1.sa_mask);
 sig1.sa_flags = 0;
 sig1.__sigaction_handler.sa_handler = sig_parentsideint;
 ((void) sizeof (((sigaction(2, &sig1,((void *)0)))== 0) ? 1 : 0), __extension__ ({ if ((sigaction(2, &sig1,((void *)0)))== 0) ; else __assert_fail ("(sigaction(SIGINT, &sig1,NULL))== 0", "IPCpipe.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 tpid = fork();
 if (tpid>0){
  close(pipefd[0]);
  close(pipefd[1]);
  int i = 0;
  for(;;){
   char mysignal;
   int res = read(pipefd1[0],&mysignal,1);
   if (res == 1)
    break;
   else
    continue;
  }
  for (i=0; i<10;i++){
   kill(tpid, 10);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
   kill(tpid,12);
   for(;;){
    char mysignal;
    int res = read(pipefd1[0],&mysignal,1);
    if (res == 1)
     break;
    else
     continue;
   }
  }
  kill(tpid,9);
  return;
 }
 if(tpid == 0){
  close(pipefd1[1]);
  close(pipefd1[0]);
  int ppid = getppid();
  for(;;){
   kill(ppid,2);
   for(;;){
    char mysignal;
    int res = read(pipefd[0],&mysignal,1);
    if (res == 1){
     if (mysignal == 10)
            printf("received SIGUSR1\n");
            else if (mysignal == 12)
      printf("received SIGUSR2\n");
     break;
     }
    else
     continue;
   }
  }
 }
 return 0;
}

variable_types:
pipefd
int [2]

variable_types: tpid int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: i int

variable_types: mysignal char

variable_types: res int

variable_types: ppid int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
void __fastcall sig_int(int a1)
{
signal(2, sig_int);
signal(10, sig_int);
if ( a1 == 2 )
{
puts("caught SIGINT");
}
else if ( a1 == 10 )
{
puts("caught SIGUSR1");
}
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
signal(2, sig_int);
signal(10, sig_int);
if ( a1 == 2 )
{
puts("caught SIGINT");
}
else if ( a1 == 10 )
{
puts("caught SIGUSR1");
}
}

original_code:
static void sig_int(int signo){
 signal(2,sig_int);
 signal(10,sig_int);
 if(signo ==2)
  printf("caught SIGINT\n");
 if(signo==10)
  printf("caught SIGUSR1\n");
 return;
}


CloseWalkDir

repo: yunus-idrisov/image-viewer

decompiled_code:
__int64 CloseWalkDir()
{
unsigned int v0; // ebx
int *v2; // rax
char *v3; // rax
int *v4; // rax
char *v5; // rax
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

v0 = mode;
if ( mode == 1 )
{
pthread_join(thread_id, &thread_return);
DeleteStrList(&imageNames);
if ( closedir(dir) )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "closedir(...) error: %s.\n", v3);
v0 = -1;
}
else
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
}
}
else
{
DeleteStrList(&imageNames);
v0 = 1;
if ( closedir(dir) )
{
v4 = _errno_location();
v5 = strerror(*v4);
fprintf(stderr, "closedir(...) error: %s.\n", v5);
v0 = -1;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // ebx
int *v2; // rax
char *v3; // rax
int *v4; // rax
char *v5; // rax
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

v0 = mode;
if ( mode == 1 )
{
pthread_join(thread_id, &thread_return);
DeleteStrList(&imageNames);
if ( closedir(dir) )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "closedir(...) error: %s.\n", v3);
v0 = -1;
}
else
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
}
}
else
{
DeleteStrList(&imageNames);
v0 = 1;
if ( closedir(dir) )
{
v4 = _errno_location();
v5 = strerror(*v4);
fprintf(stderr, "closedir(...) error: %s.\n", v5);
v0 = -1;
}
}
return v0;
}

original_code:
int CloseWalkDir(){
 if( mode == 1 ){
  void* status;
  pthread_join( thread_id, &status );
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
  FreeImage_Unload( pPrevFIBITMAP );
  pPrevFIBITMAP = 0;
  FreeImage_Unload( pNextFIBITMAP );
  pNextFIBITMAP = 0;
 }
 else{
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
 }
 return 1;
}

canonical_original_code:
int CloseWalkDir(){
 if( mode == 1 ){
  void *status;
  pthread_join( thread_id, &status );
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
  FreeImage_Unload( pPrevFIBITMAP );
  pPrevFIBITMAP = 0;
  FreeImage_Unload( pNextFIBITMAP );
  pNextFIBITMAP = 0;
 }
 else{
  DeleteStrList(&imageNames);
  if( closedir(dir) != 0 ){
   fprintf(stderr, "closedir(...) error: %s.\n", strerror((*__errno_location ())));
   return -1;
  }
 }
 return 1;
}

variable_types:
status
void *


GetNextNodeCycle

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( !result )
result = *(_QWORD *)imageNames;
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 8);
if ( !result )
result = *(_QWORD *)imageNames;
return result;
}

original_code:
ListNode* GetNextNodeCycle(ListNode* curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->next;
  if( curImageNode == 0 )
   curImageNode = imageNames->head;
  return curImageNode;
 }else
  return 0;
}

canonical_original_code:
struct ListNode *GetNextNodeCycle(struct ListNode *curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->next;
  if( curImageNode == 0 )
   curImageNode = imageNames->head;
  return curImageNode;
 }else
  return 0;
}

variable_types:
curImageNode
struct ListNode *

return_type: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *


GetPrevNodeCycle

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 16);
if ( !result )
result = *(_QWORD *)(imageNames + 8);
return result;
}

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

if ( !a1 )
return 0LL;
result = *(_QWORD *)(a1 + 16);
if ( !result )
result = *(_QWORD *)(imageNames + 8);
return result;
}

original_code:
ListNode* GetPrevNodeCycle(ListNode* curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->prev;
  if( curImageNode == 0 )
   curImageNode = imageNames->tail;
  return curImageNode;
 }else
  return 0;
}

canonical_original_code:
struct ListNode *GetPrevNodeCycle(struct ListNode *curImageNode){
 if( curImageNode != 0 ){
  curImageNode = curImageNode->prev;
  if( curImageNode == 0 )
   curImageNode = imageNames->tail;
  return curImageNode;
 }else
  return 0;
}

user_defined_types: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; }


OpenWalkDir

decompiled_code:
__int64 __fastcall OpenWalkDir(char *src)
{
size_t v1; // rax
int *v2; // r12
struct dirent *v3; // rax
char *v4; // rbx
int *v5; // rax
char *v6; // rax
unsigned int v7; // ebx
__int64 v8; // rdi
char *v10; // rax
const char **v11; // rax
const char *v12; // rsi

InitStrList(&imageNames);
dir = opendir(src);
if ( dir )
{
strcpy(imagePath, src);
v1 = strlen(imagePath);
if ( *((_BYTE *)&thread_id + v1 + 7) != 47 )
*(_WORD *)&imagePath[v1] = 47;
imagePathLen = strlen(imagePath);
v2 = _errno_location();
*v2 = 0;
while ( 1 )
{
v3 = readdir(dir);
if ( !v3 )
break;
if ( v3->d_type == 8 )
{
v4 = v3->d_name;
if ( (unsigned int)IsSupTexture(v3->d_name) )
AddStringToStrList(imageNames, v4);
}
}
if ( *v2 )
{
v10 = strerror(*v2);
fprintf(stderr, "Read path \"%s\" error: %s\n", src, v10);
DeleteStrList(&imageNames);
v7 = -1;
}
else
{
SortStrList(imageNames);
v8 = *(_QWORD *)imageNames;
curImagePointer = v8;
v7 = 1;
if ( v8 )
{
v7 = mode;
if ( mode == 1 )
{
v11 = (const char **)GetPrevNodeCycle(v8);
strcat(imagePath, *v11);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
v12 = *(const char **)GetNextNodeCycle(curImagePointer);
strcat(imagePath, v12);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
else
{
v7 = 1;
}
}
}
}
else
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Error in open path \"%s\": %s\n", src, v6);
DeleteStrList(&imageNames);
v7 = -1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src)
{
size_t v1; // rax
int *v2; // r12
struct dirent *v3; // rax
char *v4; // rbx
int *v5; // rax
char *v6; // rax
unsigned int v7; // ebx
__int64 v8; // rdi
char *v10; // rax
const char **v11; // rax
const char *v12; // rsi

InitStrList(&imageNames);
dir = opendir(src);
if ( dir )
{
strcpy(imagePath, src);
v1 = strlen(imagePath);
if ( *((_BYTE *)&thread_id + v1 + 7) != 47 )
*(_WORD *)&imagePath[v1] = 47;
imagePathLen = strlen(imagePath);
v2 = _errno_location();
*v2 = 0;
while ( 1 )
{
v3 = readdir(dir);
if ( !v3 )
break;
if ( v3->d_type == 8 )
{
v4 = v3->d_name;
if ( (unsigned int)IsSupTexture(v3->d_name) )
AddStringToStrList(imageNames, v4);
}
}
if ( *v2 )
{
v10 = strerror(*v2);
fprintf(stderr, "Read path \"%s\" error: %s\n", src, v10);
DeleteStrList(&imageNames);
v7 = -1;
}
else
{
SortStrList(imageNames);
v8 = *(_QWORD *)imageNames;
curImagePointer = v8;
v7 = 1;
if ( v8 )
{
v7 = mode;
if ( mode == 1 )
{
v11 = (const char **)func3(v8);
strcat(imagePath, *v11);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
v12 = *(const char **)func1(curImagePointer);
strcat(imagePath, v12);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
else
{
v7 = 1;
}
}
}
}
else
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Error in open path \"%s\": %s\n", src, v6);
DeleteStrList(&imageNames);
v7 = -1;
}
return v7;
}

original_code:
int OpenWalkDir(const char* path){
 InitStrList(&imageNames);
 dir = opendir(path);
 if( dir == ((void *)0) ){
  fprintf(stderr, "Error in open path \"%s\": %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 strcpy(imagePath, path);
 if( imagePath[strlen(imagePath)-1] != '/' )
  strcat(imagePath, "/");
 imagePathLen = strlen(imagePath);
 struct dirent* de;
 (*__errno_location ()) = 0;
 while( (de = readdir(dir)) != ((void *)0) ){
  if( (de->d_type == DT_REG) && IsSupTexture(de->d_name) )
   AddStringToStrList(imageNames, de->d_name);
 }
 if( (*__errno_location ()) != 0 ){
  fprintf(stderr, "Read path \"%s\" error: %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 SortStrList(imageNames);
 curImagePointer = imageNames->head;
 if( curImagePointer != 0 && mode == 1 ){
  ListNode* prevImagePointer = GetPrevNodeCycle( curImagePointer );
  strcat(imagePath, prevImagePointer->str);
  pPrevFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
  ListNode* nextImagePointer = GetNextNodeCycle( curImagePointer );
  strcat(imagePath, nextImagePointer->str);
  pNextFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
 }
 return 1;
}

canonical_original_code:
int OpenWalkDir(char *path){
 InitStrList(&imageNames);
 dir = opendir(path);
 if( dir == ((void *)0) ){
  fprintf(stderr, "Error in open path \"%s\": %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 strcpy(imagePath, path);
 if( imagePath[strlen(imagePath)-1] != '/' )
  strcat(imagePath, "/");
 imagePathLen = strlen(imagePath);
 struct dirent *de;
 (*__errno_location ()) = 0;
 while( (de = readdir(dir)) != ((void *)0) ){
  if( (de->d_type == DT_REG) && IsSupTexture(de->d_name) )
   AddStringToStrList(imageNames, de->d_name);
 }
 if( (*__errno_location ()) != 0 ){
  fprintf(stderr, "Read path \"%s\" error: %s\n", path, strerror((*__errno_location ())));
  DeleteStrList(&imageNames);
  return -1;
 }
 SortStrList(imageNames);
 curImagePointer = imageNames->head;
 if( curImagePointer != 0 && mode == 1 ){
  struct ListNode *prevImagePointer = GetPrevNodeCycle( curImagePointer );
  strcat(imagePath, prevImagePointer->str);
  pPrevFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
  struct ListNode *nextImagePointer = GetNextNodeCycle( curImagePointer );
  strcat(imagePath, nextImagePointer->str);
  pNextFIBITMAP = GetFIBITMAP( imagePath );
  imagePath[imagePathLen] = '\0';
 }
 return 1;
}

variable_types:
path
char *

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *

variable_types: struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *


decompiled_code:
void *__fastcall thread_func(void *a1)
{
const char **v2; // rax
const char **v3; // rax

if ( imageToLoad )
{
if ( imageToLoad == 2 && curImagePointer )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
curImagePointer = GetNextNodeCycle(curImagePointer);
v3 = (const char **)GetNextNodeCycle(curImagePointer);
strcat(imagePath, *v3);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
}
else if ( curImagePointer )
{
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
curImagePointer = GetPrevNodeCycle(curImagePointer);
v2 = (const char **)GetPrevNodeCycle(curImagePointer);
strcat(imagePath, *v2);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
const char **v2; // rax
const char **v3; // rax

if ( imageToLoad )
{
if ( imageToLoad == 2 && curImagePointer )
{
FreeImage_Unload(pPrevFIBITMAP);
pPrevFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
curImagePointer = func1(curImagePointer);
v3 = (const char **)func1(curImagePointer);
strcat(imagePath, *v3);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
}
else if ( curImagePointer )
{
FreeImage_Unload(pNextFIBITMAP);
pNextFIBITMAP = 0LL;
strcat(imagePath, *(const char **)curImagePointer);
pNextFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
curImagePointer = func3(curImagePointer);
v2 = (const char **)func3(curImagePointer);
strcat(imagePath, *v2);
pPrevFIBITMAP = GetFIBITMAP(imagePath);
imagePath[imagePathLen] = 0;
}
return 0LL;
}

original_code:
static void* thread_func(void* arg){
 switch( imageToLoad ){
  case 0:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pNextFIBITMAP );
    pNextFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetPrevNodeCycle( curImagePointer );
    ListNode* prevImagePointer = GetPrevNodeCycle( curImagePointer );
    strcat(imagePath, prevImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
  case 2:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pPrevFIBITMAP );
    pPrevFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetNextNodeCycle( curImagePointer );
    ListNode* nextImagePointer = GetNextNodeCycle( curImagePointer );
    strcat(imagePath, nextImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
 }
 return 0;
}

canonical_original_code:
static void *thread_func(void *arg){
 switch( imageToLoad ){
  case 0:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pNextFIBITMAP );
    pNextFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetPrevNodeCycle( curImagePointer );
    struct ListNode *prevImagePointer = GetPrevNodeCycle( curImagePointer );
    strcat(imagePath, prevImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
  case 2:
   if( curImagePointer != 0 ){
    FreeImage_Unload( pPrevFIBITMAP );
    pPrevFIBITMAP = 0;
    strcat(imagePath, curImagePointer->str);
    pPrevFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
    curImagePointer = GetNextNodeCycle( curImagePointer );
    struct ListNode *nextImagePointer = GetNextNodeCycle( curImagePointer );
    strcat(imagePath, nextImagePointer->str);
    pNextFIBITMAP = GetFIBITMAP( imagePath );
    imagePath[imagePathLen] = '\0';
   }
   break;
 }
 return 0;
}

variable_types:
arg
void *


AddStringToStrList

original_code:
void AddStringToStrList(StrList* list, const char* str){
 if( strlen(str) == 0 )
  return;
 ListNode* tmp = malloc(sizeof(ListNode));
 int len = strlen(str);
 tmp->str = malloc(sizeof(char)*(len + 1));
 strcpy(tmp->str, str);
 tmp->next = 0;
 if( (list->head == 0) && (list->tail == 0) ){
  tmp->prev = 0;
  list->head = tmp;
  list->tail = tmp;
 }
 else {
  tmp->prev = list->tail;
  list->tail->next = tmp;
  list->tail = tmp;
 }
}

canonical_original_code:
void AddStringToStrList(struct StrList *list, char *str){
 if( strlen(str) == 0 )
  return;
 struct ListNode *tmp = malloc(sizeof(ListNode));
 int len = strlen(str);
 tmp->str = malloc(sizeof(char)*(len + 1));
 strcpy(tmp->str, str);
 tmp->next = 0;
 if( (list->head == 0) && (list->tail == 0) ){
  tmp->prev = 0;
  list->head = tmp;
  list->tail = tmp;
 }
 else {
  tmp->prev = list->tail;
  list->tail->next = tmp;
  list->tail = tmp;
 }
}

variable_types: struct StrList { struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *head; struct ListNode *tail; } *

variable_types:
str
char *

variable_types: len int

user_defined_types: struct StrList { struct ListNode *head; struct ListNode *tail; }


DeleteStrList

original_code:
void DeleteStrList(StrList** list){
 if( (*list) == 0 )
  return;
 ListNode* cur = (*list)->head;
 while( cur != 0 ){
  ListNode* next = cur->next;
  free(cur->str);
  free(cur);
  cur = next;
 }
 free(*list);
 *list = 0;
}

canonical_original_code:
void DeleteStrList(struct StrList **list){
 if( (*list) == 0 )
  return;
 struct ListNode *cur = (*list)->head;
 while( cur != 0 ){
  struct ListNode *next = cur->next;
  free(cur->str);
  free(cur);
  cur = next;
 }
 free(*list);
 *list = 0;
}

variable_types: struct StrList { struct ListNode { char *str; struct ListNode *next; struct ListNode *prev; } *head; struct ListNode *tail; } **


SortStrList

original_code:
void SortStrList(StrList* list){
 if( list == 0 )
  return;
 int elemCount = 0;
 ListNode* p = list->head;
 while( p != 0 ){
  elemCount++;
  p = p->next;
 }
 MergeSort( list, list->head, list->tail, elemCount );
}

canonical_original_code:
void SortStrList(struct StrList *list){
 if( list == 0 )
  return;
 int elemCount = 0;
 struct ListNode *p = list->head;
 while( p != 0 ){
  elemCount++;
  p = p->next;
 }
 MergeSort( list, list->head, list->tail, elemCount );
}

variable_types: elemCount int


decompiled_code:
__int64 __fastcall setup_socket(__int16 a1, int a2, int a3)
{
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
int *v8; // rbx
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rbp
char *v15; // rax
int *v16; // rbp
char *v17; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v4 = socket(2, a2, a3);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "mtp3io.c", 65LL, "setup_socket", "Cannot create UDP socket, errno=%d: %s\n", (unsigned int)*v8, v9);
v5 = -1;
}
else
{
v5 = v4;
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
v6 = fcntl(v5, 3);
if ( v6 < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(3LL, "mtp3io.c", 73LL, "setup_socket", "Could not obtain flags for socket fd: %s.\n", v11);
v5 = -1;
}
else
{
BYTE1(v6) |= 8u;
if ( fcntl(v5, 4, (unsigned int)v6) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
ast_log(3LL, "mtp3io.c", 79LL, "setup_socket", "Could not set socket fd non-blocking: %s.\n", v13);
v5 = -1;
}
else if ( bind(v5, &addr, 0x10u) < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
ast_log(
4LL,
"mtp3io.c",
84LL,
"setup_socket",
"Cannot bind receiver socket, errno=%d: %s\n",
(unsigned int)*v14,
v15);
close(v5);
v5 = -1;
}
else if ( a2 != 2 && listen(v5, 8) < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
ast_log(
4LL,
"mtp3io.c",
90LL,
"setup_socket",
"Cannot listen on socket, errno=%d: %s\n",
(unsigned int)*v16,
v17);
close(v5);
v5 = -1;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, int a2, int a3)
{
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
int *v8; // rbx
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rbp
char *v15; // rax
int *v16; // rbp
char *v17; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
v4 = socket(2, a2, a3);
if ( v4 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "mtp3io.c", 65LL, "setup_socket", "Cannot create UDP socket, errno=%d: %s\n", (unsigned int)*v8, v9);
v5 = -1;
}
else
{
v5 = v4;
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
v6 = fcntl(v5, 3);
if ( v6 < 0 )
{
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(3LL, "mtp3io.c", 73LL, "setup_socket", "Could not obtain flags for socket fd: %s.\n", v11);
v5 = -1;
}
else
{
BYTE1(v6) |= 8u;
if ( fcntl(v5, 4, (unsigned int)v6) < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
ast_log(3LL, "mtp3io.c", 79LL, "setup_socket", "Could not set socket fd non-blocking: %s.\n", v13);
v5 = -1;
}
else if ( bind(v5, &addr, 0x10u) < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
ast_log(
4LL,
"mtp3io.c",
84LL,
"setup_socket",
"Cannot bind receiver socket, errno=%d: %s\n",
(unsigned int)*v14,
v15);
close(v5);
v5 = -1;
}
else if ( a2 != 2 && listen(v5, 8) < 0 )
{
v16 = _errno_location();
v17 = strerror(*v16);
ast_log(
4LL,
"mtp3io.c",
90LL,
"setup_socket",
"Cannot listen on socket, errno=%d: %s\n",
(unsigned int)*v16,
v17);
close(v5);
v5 = -1;
}
}
}
return v5;
}

original_code:
static int setup_socket(int localport, int sockettype, int ipproto)
{
  struct sockaddr_in sock;
  in_addr_t addr = ((in_addr_t) 0x00000000);
  int parm, res, flags;
  int s;
  memset(&sock, 0, sizeof(struct sockaddr_in));
  sock.sin_family = 2;
  sock.sin_port = htons(localport);
  memcpy(&sock.sin_addr, &addr, sizeof(addr));
  s = socket(2, sockettype, ipproto);
  if (s < 0) {
    ast_log(4, "mtp3io.c", 65, __PRETTY_FUNCTION__, "Cannot create UDP socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return -1;
  }
  parm = 1;
  setsockopt(s, 1, 2, &parm, sizeof(int));
  res = fcntl(s, 3);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 73, __PRETTY_FUNCTION__, "Could not obtain flags for socket fd: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  flags = res | 04000;
  res = fcntl(s, 4, flags);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 79, __PRETTY_FUNCTION__, "Could not set socket fd non-blocking: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  if (bind(s, &sock, sizeof(sock)) < 0) {
    ast_log(4, "mtp3io.c", 84, __PRETTY_FUNCTION__, "Cannot bind receiver socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    close(s);
    return -1;
  }
  if (sockettype != SOCK_DGRAM)
    if (listen(s, 8) < 0) {
      ast_log(4, "mtp3io.c", 90, __PRETTY_FUNCTION__, "Cannot listen on socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      close(s);
      return -1;
    }
  return s;
}

canonical_original_code:
static int setup_socket(int localport, int sockettype, int ipproto)
{
  struct sockaddr_in sock;
  unsigned int addr = ((in_addr_t) 0x00000000);
  int parm; int res; int flags;
  int s;
  memset(&sock, 0, sizeof(struct sockaddr_in));
  sock.sin_family = 2;
  sock.sin_port = htons(localport);
  memcpy(&sock.sin_addr, &addr, sizeof(addr));
  s = socket(2, sockettype, ipproto);
  if (s < 0) {
    ast_log(4, "mtp3io.c", 65, __PRETTY_FUNCTION__, "Cannot create UDP socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return -1;
  }
  parm = 1;
  setsockopt(s, 1, 2, &parm, sizeof(int));
  res = fcntl(s, 3);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 73, __PRETTY_FUNCTION__, "Could not obtain flags for socket fd: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  flags = res | 04000;
  res = fcntl(s, 4, flags);
  if(res < 0) {
    ast_log(3, "mtp3io.c", 79, __PRETTY_FUNCTION__, "Could not set socket fd non-blocking: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  if (bind(s, &sock, sizeof(sock)) < 0) {
    ast_log(4, "mtp3io.c", 84, __PRETTY_FUNCTION__, "Cannot bind receiver socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    close(s);
    return -1;
  }
  if (sockettype != SOCK_DGRAM)
    if (listen(s, 8) < 0) {
      ast_log(4, "mtp3io.c", 90, __PRETTY_FUNCTION__, "Cannot listen on socket, errno=%d: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      close(s);
      return -1;
    }
  return s;
}

variable_types: localport int

variable_types: sockettype int

variable_types: ipproto int

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

variable_types:
addr
unsigned int

variable_types: parm int

variable_types: flags int

variable_types: s int

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


decompiled_code:
__int64 __fastcall mtp3_setup_socket(__int16 a1, __int16 a2)
{
return setup_socket(a2 + a1, mtp3_sockettype, mtp3_ipproto);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, __int16 a2)
{
return func1(a2 + a1, mtp3_sockettype, mtp3_ipproto);
}

original_code:
int mtp3_setup_socket(int port, int schannel)
{
  return setup_socket(port + schannel, mtp3_sockettype, mtp3_ipproto);
}

variable_types: port int

variable_types: schannel int


decompiled_code:
__int64 __fastcall mtp3_connect_socket(const char *a1, const char *a2)
{
int v2; // er13
int v3; // er14
int v4; // eax
struct addrinfo *i; // rbx
const char *v6; // rax
__int64 v7; // rbp
unsigned int v8; // eax
unsigned __int16 v9; // r12
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct addrinfo *pai[2]; // [rsp+18h] [rbp-70h] BYREF
int v15; // [rsp+28h] [rbp-60h]
int v16; // [rsp+2Ch] [rbp-5Ch]
__int64 v17; // [rsp+30h] [rbp-58h]
__int64 v18; // [rsp+38h] [rbp-50h]
__int64 v19; // [rsp+40h] [rbp-48h]
__int64 v20; // [rsp+48h] [rbp-40h]

pai[1] = (struct addrinfo *)0x200000000LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v20 = 0LL;
v2 = mtp3_sockettype;
v15 = mtp3_sockettype;
v3 = mtp3_ipproto;
v16 = mtp3_ipproto;
v4 = getaddrinfo(a1, a2, 0LL, pai);
if ( v4 )
{
v6 = gai_strerror(v4);
ast_log(
4LL,
"mtp3io.c",
117LL,
"mtp3_connect_socket",
"Invalid hostname/IP address '%s' or port '%s': %s.\n",
a1,
a2,
v6);
LODWORD(v7) = -1;
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v8 = socket(i->ai_family, v2, v3);
v7 = v8;
if ( v8 != -1 )
{
v9 = _bswap_16(HIWORD(i->ai_flags));
v10 = inet_ntoa((struct in_addr)i->ai_family);
ast_log(0LL, "mtp3io.c", 126LL, "mtp3_connect_socket", "connecting to mtp3d %s:%d, fd %d\n", v10, v9, v7);
if ( connect(v7, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_10;
close(v7);
}
}
v11 = _errno_location();
v12 = strerror(*v11);
ast_log(
4LL,
"mtp3io.c",
132LL,
"mtp3_connect_socket",
"Could not connect to hostname/IP address '%s', port '%s': %s.\n",
a1,
a2,
v12);
LODWORD(v7) = -1;
LABEL_10:
freeaddrinfo(pai[0]);
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, const char *a2)
{
int v2; // er13
int v3; // er14
int v4; // eax
struct addrinfo *i; // rbx
const char *v6; // rax
__int64 v7; // rbp
unsigned int v8; // eax
unsigned __int16 v9; // r12
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct addrinfo *pai[2]; // [rsp+18h] [rbp-70h] BYREF
int v15; // [rsp+28h] [rbp-60h]
int v16; // [rsp+2Ch] [rbp-5Ch]
__int64 v17; // [rsp+30h] [rbp-58h]
__int64 v18; // [rsp+38h] [rbp-50h]
__int64 v19; // [rsp+40h] [rbp-48h]
__int64 v20; // [rsp+48h] [rbp-40h]

pai[1] = (struct addrinfo *)0x200000000LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v20 = 0LL;
v2 = mtp3_sockettype;
v15 = mtp3_sockettype;
v3 = mtp3_ipproto;
v16 = mtp3_ipproto;
v4 = getaddrinfo(a1, a2, 0LL, pai);
if ( v4 )
{
v6 = gai_strerror(v4);
ast_log(
4LL,
"mtp3io.c",
117LL,
"mtp3_connect_socket",
"Invalid hostname/IP address '%s' or port '%s': %s.\n",
a1,
a2,
v6);
LODWORD(v7) = -1;
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v8 = socket(i->ai_family, v2, v3);
v7 = v8;
if ( v8 != -1 )
{
v9 = func0(HIWORD(i->ai_flags));
v10 = inet_ntoa((struct in_addr)i->ai_family);
ast_log(0LL, "mtp3io.c", 126LL, "mtp3_connect_socket", "connecting to mtp3d %s:%d, fd %d\n", v10, v9, v7);
if ( connect(v7, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_10;
close(v7);
}
}
v11 = _errno_location();
v12 = strerror(*v11);
ast_log(
4LL,
"mtp3io.c",
132LL,
"mtp3_connect_socket",
"Could not connect to hostname/IP address '%s', port '%s': %s.\n",
a1,
a2,
v12);
LODWORD(v7) = -1;
LABEL_10:
freeaddrinfo(pai[0]);
}
return (unsigned int)v7;
}

original_code:
int mtp3_connect_socket(const char* host, const char* port)
{
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = mtp3_sockettype;
  hints.ai_protocol = mtp3_ipproto;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    ast_log(4, "mtp3io.c", 117, __PRETTY_FUNCTION__, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    ast_log(0, "mtp3io.c", 126, __PRETTY_FUNCTION__, "connecting to mtp3d %s:%d, fd %d\n", inet_ntoa(((struct sockaddr_in*) rp)->sin_addr), ntohs(((struct sockaddr_in*) rp)->sin_port), res);
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    ast_log(4, "mtp3io.c", 132, __PRETTY_FUNCTION__, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

canonical_original_code:
int mtp3_connect_socket(char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *result; struct addrinfo *rp;
  int s; int res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = mtp3_sockettype;
  hints.ai_protocol = mtp3_ipproto;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    ast_log(4, "mtp3io.c", 117, __PRETTY_FUNCTION__, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    ast_log(0, "mtp3io.c", 126, __PRETTY_FUNCTION__, "connecting to mtp3d %s:%d, fd %d\n", inet_ntoa(((struct sockaddr_in *) rp)->sin_addr), ntohs(((struct sockaddr_in *) rp)->sin_port), res);
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    ast_log(4, "mtp3io.c", 132, __PRETTY_FUNCTION__, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

variable_types:
host
char *

variable_types:
port
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: res int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall mtp3_send(int fd, void *buf, size_t n)
{
unsigned int v4; // ebx
int v5; // eax
int v6; // er13
int *v8; // rax
char *v9; // rax

v4 = n;
do
{
v5 = send(fd, buf, v4, 0);
v6 = v5;
if ( v5 < 0 )
{
if ( *_errno_location() != 4 )
goto LABEL_9;
}
else if ( v5 > 0 )
{
buf = (char *)buf + v5;
v4 -= v5;
}
}
while ( v4 );
if ( v6 >= 0 )
return (unsigned int)v6;
LABEL_9:
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "mtp3io.c", 155LL, "mtp3_send", "Cannot send mtp3 packet: %s\n", v9);
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, void *buf, size_t n)
{
unsigned int v4; // ebx
int v5; // eax
int v6; // er13
int *v8; // rax
char *v9; // rax

v4 = n;
do
{
v5 = send(fd, buf, v4, 0);
v6 = v5;
if ( v5 < 0 )
{
if ( *_errno_location() != 4 )
goto LABEL_9;
}
else if ( v5 > 0 )
{
buf = (char *)buf + v5;
v4 -= v5;
}
}
while ( v4 );
if ( v6 >= 0 )
return (unsigned int)v6;
LABEL_9:
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "mtp3io.c", 155LL, "mtp3_send", "Cannot send mtp3 packet: %s\n", v9);
return (unsigned int)v6;
}

original_code:
int mtp3_send(int s, const unsigned char* buff, unsigned int len)
{
  int res;
  do {
    res = send(s, buff, len, 0);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res < 0) {
    ast_log(4, "mtp3io.c", 155, __PRETTY_FUNCTION__, "Cannot send mtp3 packet: %s\n", strerror((*__errno_location ())));
  }
  return res;
}

canonical_original_code:
int mtp3_send(int s, unsigned char *buff, unsigned int len)
{
  int res;
  do {
    res = send(s, buff, len, 0);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res < 0) {
    ast_log(4, "mtp3io.c", 155, __PRETTY_FUNCTION__, "Cannot send mtp3 packet: %s\n", strerror((*__errno_location ())));
  }
  return res;
}

variable_types:
buff
unsigned char *

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall mtp3_reply(int fd, void *buf, size_t n, struct sockaddr *addr, socklen_t addr_len)
{
unsigned int v6; // ebx
unsigned __int16 v9; // r15
char *v10; // rax
__int64 result; // rax
int v12; // er15
int *v13; // rax
char *v14; // rax

v6 = n;
v9 = _bswap_16(*(_WORD *)addr->sa_data);
v10 = inet_ntoa(*(struct in_addr *)&addr->sa_data[2]);
ast_log(0LL, "mtp3io.c", 162LL, "mtp3_reply", "Send packet to %s:%d\n", v10, v9);
do
{
result = sendto(fd, buf, v6, 0, addr, addr_len);
v12 = result;
if ( (int)result < 0 )
{
result = (__int64)_errno_location();
if ( *(_DWORD *)result != 4 )
break;
}
else if ( (int)result > 0 )
{
buf = (char *)buf + (int)result;
v6 -= result;
}
}
while ( v6 );
if ( v12 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
result = ast_log(4LL, "mtp3io.c", 176LL, "mtp3_reply", "Cannot send reply mtp3 packet: %s\n", v14);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, void *buf, size_t n, struct sockaddr *addr, socklen_t addr_len)
{
unsigned int v6; // ebx
unsigned __int16 v9; // r15
char *v10; // rax
__int64 result; // rax
int v12; // er15
int *v13; // rax
char *v14; // rax

v6 = n;
v9 = func0(*(_WORD *)addr->sa_data);
v10 = inet_ntoa(*(struct in_addr *)&addr->sa_data[2]);
ast_log(0LL, "mtp3io.c", 162LL, "mtp3_reply", "Send packet to %s:%d\n", v10, v9);
do
{
result = sendto(fd, buf, v6, 0, addr, addr_len);
v12 = result;
if ( (int)result < 0 )
{
result = (__int64)_errno_location();
if ( *(_DWORD *)result != 4 )
break;
}
else if ( (int)result > 0 )
{
buf = (char *)buf + (int)result;
v6 -= result;
}
}
while ( v6 );
if ( v12 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
result = ast_log(4LL, "mtp3io.c", 176LL, "mtp3_reply", "Cannot send reply mtp3 packet: %s\n", v14);
}
return result;
}

original_code:
void mtp3_reply(int s, const unsigned char* buff, unsigned int len, const struct sockaddr* to, socklen_t tolen)
{
  ast_log(0, "mtp3io.c", 162, __PRETTY_FUNCTION__, "Send packet to %s:%d\n", inet_ntoa(((struct sockaddr_in*) to)->sin_addr), ntohs(((struct sockaddr_in*) to)->sin_port));
  int res;
  do {
    res = sendto(s, buff, len, 0, to, tolen);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res == -1)
    ast_log(4, "mtp3io.c", 176, __PRETTY_FUNCTION__, "Cannot send reply mtp3 packet: %s\n", strerror((*__errno_location ())));
}

canonical_original_code:
void mtp3_reply(int s, unsigned char *buff, unsigned int len, struct sockaddr *to, unsigned int tolen)
{
  ast_log(0, "mtp3io.c", 162, __PRETTY_FUNCTION__, "Send packet to %s:%d\n", inet_ntoa(((struct sockaddr_in *) to)->sin_addr), ntohs(((struct sockaddr_in *) to)->sin_port));
  int res;
  do {
    res = sendto(s, buff, len, 0, to, tolen);
    if (res < 0) {
      if ((*__errno_location ()) != 4)
 break;
    }
    else if (res > 0) {
      buff += res;
      len -= res;
    }
  } while (len > 0);
  if (res == -1)
    ast_log(4, "mtp3io.c", 176, __PRETTY_FUNCTION__, "Cannot send reply mtp3 packet: %s\n", strerror((*__errno_location ())));
}

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

variable_types:
tolen
unsigned int


decompiled_code:
__int64 __fastcall connect_socket(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *i; // rbx
int *v4; // rax
char *v5; // rax
unsigned int v6; // ebp
const char *v8; // rax
int v9; // eax
struct addrinfo *pai[2]; // [rsp+8h] [rbp-60h] BYREF
int v11; // [rsp+18h] [rbp-50h]
int v12; // [rsp+1Ch] [rbp-4Ch]
__int64 v13; // [rsp+20h] [rbp-48h]
__int64 v14; // [rsp+28h] [rbp-40h]
__int64 v15; // [rsp+30h] [rbp-38h]
__int64 v16; // [rsp+38h] [rbp-30h]

pai[1] = (struct addrinfo *)0x200000000LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v11 = 1;
v12 = 6;
v2 = getaddrinfo(a1, a2, 0LL, pai);
if ( v2 )
{
v8 = gai_strerror(v2);
fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", a1, a2, v8);
v6 = -1;
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v9 = socket(i->ai_family, 1, 6);
v6 = v9;
if ( v9 != -1 )
{
if ( connect(v9, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_4;
close(v6);
}
}
v4 = __errno_location();
v5 = strerror(*v4);
fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", a1, a2, v5);
v6 = -1;
LABEL_4:
freeaddrinfo(pai[0]);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *i; // rbx
int *v4; // rax
char *v5; // rax
unsigned int v6; // ebp
const char *v8; // rax
int v9; // eax
struct addrinfo *pai[2]; // [rsp+8h] [rbp-60h] BYREF
int v11; // [rsp+18h] [rbp-50h]
int v12; // [rsp+1Ch] [rbp-4Ch]
__int64 v13; // [rsp+20h] [rbp-48h]
__int64 v14; // [rsp+28h] [rbp-40h]
__int64 v15; // [rsp+30h] [rbp-38h]
__int64 v16; // [rsp+38h] [rbp-30h]

pai[1] = (struct addrinfo *)0x200000000LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v11 = 1;
v12 = 6;
v2 = getaddrinfo(a1, a2, 0LL, pai);
if ( v2 )
{
v8 = gai_strerror(v2);
fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", a1, a2, v8);
v6 = -1;
}
else
{
for ( i = pai[0]; i; i = i->ai_next )
{
v9 = socket(i->ai_family, 1, 6);
v6 = v9;
if ( v9 != -1 )
{
if ( connect(v9, i->ai_addr, i->ai_addrlen) != -1 )
goto LABEL_4;
close(v6);
}
}
v4 = __errno_location();
v5 = strerror(*v4);
fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", a1, a2, v5);
v6 = -1;
LABEL_4:
freeaddrinfo(pai[0]);
}
return v6;
}

original_code:
static int connect_socket(const char* host, const char* port)
{
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

canonical_original_code:
static int connect_socket(char *host, char *port)
{
  struct addrinfo hints;
  struct addrinfo *result; struct addrinfo *rp;
  int s; int res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = 2;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  res = getaddrinfo(host, port, ((void *)0), &result);
  if (res != 0) {
    fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n", host, port, gai_strerror(res)
     );
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == ((void *)0)) {
    fprintf(stderr, "Could not connect to hostname/IP address '%s', port '%s': %s.\n", host, port, strerror((*__errno_location ())));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // rbx
const char **v5; // r12
int v6; // eax
int v7; // er12
unsigned int v8; // ebx
int *v9; // rax
char *v10; // rax
int v11; // edi
char *v13; // rax
struct pollfd fds; // [rsp+8h] [rbp-28C0h] BYREF
int buf[29]; // [rsp+10h] [rbp-28B8h] BYREF
int v16; // [rsp+84h] [rbp-2844h]
char s[10126]; // [rsp+88h] [rbp-2840h] BYREF
char v18[8]; // [rsp+2816h] [rbp-B2h] BYREF
__int16 v19; // [rsp+281Eh] [rbp-AAh]
char dest[16]; // [rsp+2820h] [rbp-A8h] BYREF
__int64 v21; // [rsp+2830h] [rbp-98h]
__int64 v22; // [rsp+2838h] [rbp-90h]
__int64 v23; // [rsp+2840h] [rbp-88h]
__int64 v24; // [rsp+2848h] [rbp-80h]
__int64 v25; // [rsp+2850h] [rbp-78h]
__int64 v26; // [rsp+2858h] [rbp-70h]
__int64 v27; // [rsp+2860h] [rbp-68h]
__int64 v28; // [rsp+2868h] [rbp-60h]
__int64 v29; // [rsp+2870h] [rbp-58h]
__int64 v30; // [rsp+2878h] [rbp-50h]
__int64 v31; // [rsp+2880h] [rbp-48h]
__int64 v32; // [rsp+2888h] [rbp-40h]
__int64 v33; // [rsp+2890h] [rbp-38h]
__int64 v34; // [rsp+2898h] [rbp-30h]

strcpy(dest, "localhost");
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
v28 = 0LL;
v29 = 0LL;
v30 = 0LL;
v31 = 0LL;
v32 = 0LL;
v33 = 0LL;
v34 = 0LL;
*(_QWORD *)v18 = 0x3939393131LL;
v19 = 0;
while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "h:p:");
if ( v3 == -1 )
break;
if ( v3 == 104 )
{
strcpy(dest, optarg);
}
else
{
if ( v3 != 112 )
usage();
strcpy(v18, optarg);
}
}
buf[0] = 6;
s[0] = 0;
if ( argc > optind )
{
v4 = &argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
strcat(s, *v4);
*(_WORD *)&s[strlen(s)] = 10;
++v4;
}
while ( v4 != v5 );
}
v16 = strlen(s) + 1;
v6 = connect_socket(dest, v18);
v7 = v6;
if ( v6 != -1 )
{
if ( (unsigned int)write(v6, buf, v16 + 120LL) == -1 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "Could not write socket: %s.\n", v10);
v7 = -1;
}
else
{
fds.fd = v7;
fds.events = 57;
poll(&fds, 1uLL, 10000);
shutdown(v7, 1);
while ( 1 )
{
v8 = read(v7, buf, 0x2800uLL);
printf("read %d\n", v8);
if ( v8 == -1 )
break;
if ( !(unsigned int)write(1, buf, (int)v8) )
return 0;
}
v11 = *__errno_location();
v7 = 0;
if ( v11 != 107 )
{
v13 = strerror(v11);
fprintf(stderr, "Could not read socket: %s.\n", v13);
v7 = -1;
}
}
}
return v7;
}

original_code:
int main(int argc, char* argv[])
{
  char dumpfn[4096] = {0,};
  int c;
  strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
  while ((c = getopt(argc, argv, "fc:m:dp")) != -1) {
    switch (c) {
    case 'c':
      strcpy(ast_config_AST_CONFIG_DIR, optarg);
      break;
    case 'm':
      strcpy(dumpfn, optarg);
      break;
    case 'd':
      option_debug++;
      break;
    case 'f':
      do_fork = 1;
      break;
    case 'p':
      do_pid = 1;
      break;
    default:
      usage();
    }
  }
  is_mtp3d = 1;
  if (do_fork) {
    reopen_logfiles();
  }
  printf("Using %s for config directory\n", ast_config_AST_CONFIG_DIR);
  if(load_config(0)) {
    return -1;
  }
  setsigactions();
  if (*dumpfn)
    setup_dump(dumpfn);
  if (do_pid || do_fork)
    if (setup_daemon())
      return -1;
  if(mtp_init()) {
    ast_log(4, "mtp3d.c", 773, __PRETTY_FUNCTION__, "Unable to initialize MTP.\n");
    return -1;
  }
  if (do_pid)
  make_pid_file();
  if(start_mtp_thread()) {
    ast_log(4, "mtp3d.c", 779, __PRETTY_FUNCTION__, "Unable to start MTP thread.\n");
    return -1;
  }
  monitor_running = 1;
  if(pthread_create(&monitor_thread, ((void *)0), monitor_main, ((void *)0)) < 0) {
    ast_log(4, "mtp3d.c", 785, __PRETTY_FUNCTION__, "Unable to start monitor thread.\n");
    monitor_running = 0;
    return -1;
  }
  mtp_mainloop();
  stop_mtp_thread();
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char dumpfn[4096] = {0,};
  int c;
  strcpy(ast_config_AST_CONFIG_DIR, "/etc/asterisk");
  while ((c = getopt(argc, argv, "fc:m:dp")) != -1) {
    switch (c) {
    case 'c':
      strcpy(ast_config_AST_CONFIG_DIR, optarg);
      break;
    case 'm':
      strcpy(dumpfn, optarg);
      break;
    case 'd':
      option_debug++;
      break;
    case 'f':
      do_fork = 1;
      break;
    case 'p':
      do_pid = 1;
      break;
    default:
      usage();
    }
  }
  is_mtp3d = 1;
  if (do_fork) {
    reopen_logfiles();
  }
  printf("Using %s for config directory\n", ast_config_AST_CONFIG_DIR);
  if(load_config(0)) {
    return -1;
  }
  setsigactions();
  if (*dumpfn)
    setup_dump(dumpfn);
  if (do_pid || do_fork)
    if (setup_daemon())
      return -1;
  if(mtp_init()) {
    ast_log(4, "mtp3d.c", 773, __PRETTY_FUNCTION__, "Unable to initialize MTP.\n");
    return -1;
  }
  if (do_pid)
  make_pid_file();
  if(start_mtp_thread()) {
    ast_log(4, "mtp3d.c", 779, __PRETTY_FUNCTION__, "Unable to start MTP thread.\n");
    return -1;
  }
  monitor_running = 1;
  if(pthread_create(&monitor_thread, ((void *)0), monitor_main, ((void *)0)) < 0) {
    ast_log(4, "mtp3d.c", 785, __PRETTY_FUNCTION__, "Unable to start monitor thread.\n");
    monitor_running = 0;
    return -1;
  }
  mtp_mainloop();
  stop_mtp_thread();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
dumpfn
char [4096]

variable_types: c int


decompiled_code:
ssize_t wakeup_monitor()
{
int v0; // eax
ssize_t result; // rax
int *v2; // rax
char *v3; // rax

v0 = get_receive_pipe();
result = write(v0, &LC0, 1uLL);
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = ast_log(4LL, "utils.c", 89LL, "wakeup_monitor", "Failed to write receive_pipe: %s.\n", v3);
}
return result;
}

canonical_decompiled_code:
ssize_t func0()
{
int v0; // eax
ssize_t result; // rax
int *v2; // rax
char *v3; // rax

v0 = get_receive_pipe();
result = write(v0, &LC0, 1uLL);
if ( (_DWORD)result == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
result = ast_log(4LL, "utils.c", 89LL, "wakeup_monitor", "Failed to write receive_pipe: %s.\n", v3);
}
return result;
}

original_code:
static void wakeup_monitor(void)
{
  int res = write(get_receive_pipe(), "", 1);
  if (res == -1) {
    ast_log(4, "utils.c", 89, __PRETTY_FUNCTION__, "Failed to write receive_pipe: %s.\n", strerror((*__errno_location ())));
  }
}

canonical_original_code:
static void wakeup_monitor()
{
  int res = write(get_receive_pipe(), "", 1);
  if (res == -1) {
    ast_log(4, "utils.c", 89, __PRETTY_FUNCTION__, "Failed to write receive_pipe: %s.\n", strerror((*__errno_location ())));
  }
}


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

result = mtp_sched_wait(monitor_sched);
if ( (unsigned int)(result - 1) >= 0x1F4 )
result = 500LL;
return result;
}

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

result = mtp_sched_wait(monitor_sched);
if ( (unsigned int)(result - 1) >= 0x1F4 )
result = 500LL;
return result;
}

original_code:
int timers_wait(void)
{
  int timeout = mtp_sched_wait(monitor_sched);
  if(timeout <= 0 || timeout > 500) {
    timeout = 500;
  }
  return timeout;
}

canonical_original_code:
int timers_wait()
{
  int timeout = mtp_sched_wait(monitor_sched);
  if(timeout <= 0 || timeout > 500) {
    timeout = 500;
  }
  return timeout;
}

variable_types: timeout int


decompiled_code:
__int64 __fastcall start_timer(int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebp

v3 = mtp_sched_add(monitor_sched, (unsigned int)a1, a2, a3);
if ( a1 <= 499 )
wakeup_monitor();
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebp

v3 = mtp_sched_add(monitor_sched, (unsigned int)a1, a2, a3);
if ( a1 <= 499 )
func0();
return v3;
}

original_code:
int start_timer(int msec, int (*cb)(const void *), void *data)
{
  int id = mtp_sched_add(monitor_sched, msec, cb, data);
  if(msec < 500) {
    wakeup_monitor();
  }
  return id;
}

canonical_original_code:
int start_timer(int msec, int (*cb)(void *), void *data)
{
  int id = mtp_sched_add(monitor_sched, msec, cb, data);
  if(msec < 500) {
    wakeup_monitor();
  }
  return id;
}

variable_types: msec int

variable_types:
cb
int (*)(void *)

variable_types:
data
void *

variable_types: id int


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

result = mtp_sched_del(monitor_sched, a1);
if ( !(_DWORD)result )
result = ast_log(0LL, "utils.c", 118LL, "stop_timer", "Failed to delete timer\n");
return result;
}

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

result = mtp_sched_del(monitor_sched, a1);
if ( !(_DWORD)result )
result = ast_log(0LL, "utils.c", 118LL, "stop_timer", "Failed to delete timer\n");
return result;
}

original_code:
void stop_timer(int tid)
{
  int res = mtp_sched_del(monitor_sched, tid);
  if (!res) {
    ast_log(0, "utils.c", 118, __PRETTY_FUNCTION__, "Failed to delete timer\n");
  }
}

variable_types: tid int


decompiled_code:
__int64 timers_init()
{
monitor_sched = mtp_sched_context_create();
if ( monitor_sched )
return 0LL;
ast_log(4LL, "utils.c", 128LL, "timers_init", "Unable to create monitor scheduling context.\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func5()
{
monitor_sched = mtp_sched_context_create();
if ( monitor_sched )
return 0LL;
ast_log(4LL, "utils.c", 128LL, "timers_init", "Unable to create monitor scheduling context.\n");
return 0xFFFFFFFFLL;
}

original_code:
int timers_init(void)
{
  monitor_sched = mtp_sched_context_create();
  if(monitor_sched == ((void *)0)) {
    ast_log(4, "utils.c", 128, __PRETTY_FUNCTION__, "Unable to create monitor scheduling context.\n");
    return -1;
  }
  return 0;
}

canonical_original_code:
int timers_init()
{
  monitor_sched = mtp_sched_context_create();
  if(monitor_sched == ((void *)0)) {
    ast_log(4, "utils.c", 128, __PRETTY_FUNCTION__, "Unable to create monitor scheduling context.\n");
    return -1;
  }
  return 0;
}


decompiled_code:
__int64 timers_cleanup()
{
if ( monitor_sched )
{
mtp_sched_context_destroy();
monitor_sched = 0LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
if ( monitor_sched )
{
mtp_sched_context_destroy();
monitor_sched = 0LL;
}
return 0LL;
}

original_code:
int timers_cleanup(void)
{
  if(monitor_sched) {
    mtp_sched_context_destroy(monitor_sched);
    monitor_sched = ((void *)0);
  }
  return 0;
}

canonical_original_code:
int timers_cleanup()
{
  if(monitor_sched) {
    mtp_sched_context_destroy(monitor_sched);
    monitor_sched = ((void *)0);
  }
  return 0;
}


decompiled_code:
int run_timers()
{
pthread_mutex_lock(&glock);
mtp_sched_runq(monitor_sched);
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func7()
{
pthread_mutex_lock(&glock);
mtp_sched_runq(monitor_sched);
return pthread_mutex_unlock(&glock);
}

original_code:
void run_timers(void)
{
  pthread_mutex_lock(&glock);
  mtp_sched_runq(monitor_sched);
  pthread_mutex_unlock(&glock);
}

canonical_original_code:
void run_timers()
{
  pthread_mutex_lock(&glock);
  mtp_sched_runq(monitor_sched);
  pthread_mutex_unlock(&glock);
}


decompiled_code:
int lock_global()
{
return pthread_mutex_lock(&glock);
}

canonical_decompiled_code:
int func8()
{
return pthread_mutex_lock(&glock);
}

original_code:
void lock_global(void)
{
  pthread_mutex_lock(&glock);
}

canonical_original_code:
void lock_global()
{
  pthread_mutex_lock(&glock);
}


decompiled_code:
int unlock_global()
{
return pthread_mutex_unlock(&glock);
}

canonical_decompiled_code:
int func9()
{
return pthread_mutex_unlock(&glock);
}

original_code:
void unlock_global(void)
{
  pthread_mutex_unlock(&glock);
}

canonical_original_code:
void unlock_global()
{
  pthread_mutex_unlock(&glock);
}


decompiled_code:
__int64 __fastcall start_thread(pthread_t *a1, void *(*a2)(void *), _DWORD *a3, int a4)
{
unsigned int v6; // ebx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
struct sched_param param; // [rsp+Ch] [rbp-2Ch] BYREF

if ( pthread_create(a1, 0LL, a2, 0LL) )
{
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "utils.c", 168LL, "start_thread", "Failed to create thread: %s.\n", v9);
v6 = -1;
}
else
{
v6 = 0;
param.__sched_priority = a4;
if ( pthread_setschedparam(*a1, 2, &param) )
{
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(3LL, "utils.c", 176LL, "start_thread", "Failed to set thread to realtime priority: %s.\n", v11);
}
*a3 = 1;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func10(pthread_t *a1, void *(*a2)(void *), _DWORD *a3, int a4)
{
unsigned int v6; // ebx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
struct sched_param param; // [rsp+Ch] [rbp-2Ch] BYREF

if ( pthread_create(a1, 0LL, a2, 0LL) )
{
v8 = _errno_location();
v9 = strerror(*v8);
ast_log(4LL, "utils.c", 168LL, "start_thread", "Failed to create thread: %s.\n", v9);
v6 = -1;
}
else
{
v6 = 0;
param.__sched_priority = a4;
if ( pthread_setschedparam(*a1, 2, &param) )
{
v10 = _errno_location();
v11 = strerror(*v10);
ast_log(3LL, "utils.c", 176LL, "start_thread", "Failed to set thread to realtime priority: %s.\n", v11);
}
*a3 = 1;
}
return v6;
}

original_code:
int start_thread(pthread_t* t, void* (*thread_main)(void*data), int* running, int prio)
{
  struct sched_param sp;
  int res;
  res = pthread_create(t, ((void *)0), thread_main, ((void *)0));
  if(res != 0) {
    ast_log(4, "utils.c", 168, __PRETTY_FUNCTION__, "Failed to create thread: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  memset(&sp, 0, sizeof(sp));
  sp.sched_priority = prio;
  res = pthread_setschedparam(*t, 2, &sp);
  if(res != 0) {
    ast_log(3, "utils.c", 176, __PRETTY_FUNCTION__, "Failed to set thread to realtime priority: %s.\n",
            strerror((*__errno_location ())));
  }
  *running = 1;
  return 0;
}

canonical_original_code:
int start_thread(unsigned long *t, void *(*thread_main)(void *data), int *running, int prio)
{
  struct sched_param sp;
  int res;
  res = pthread_create(t, ((void *)0), thread_main, ((void *)0));
  if(res != 0) {
    ast_log(4, "utils.c", 168, __PRETTY_FUNCTION__, "Failed to create thread: %s.\n", strerror((*__errno_location ())));
    return -1;
  }
  memset(&sp, 0, sizeof(sp));
  sp.sched_priority = prio;
  res = pthread_setschedparam(*t, 2, &sp);
  if(res != 0) {
    ast_log(3, "utils.c", 176, __PRETTY_FUNCTION__, "Failed to set thread to realtime priority: %s.\n",
            strerror((*__errno_location ())));
  }
  *running = 1;
  return 0;
}

variable_types:
t
unsigned long *

variable_types:
running
int *

variable_types: prio int

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
int __fastcall stop_thread(pthread_t *a1, _DWORD *a2)
{
int result; // eax

if ( *a2 )
{
result = pthread_join(*a1, 0LL);
*a2 = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(pthread_t *a1, _DWORD *a2)
{
int result; // eax

if ( *a2 )
{
result = pthread_join(*a1, 0LL);
*a2 = 0;
}
return result;
}

original_code:
void stop_thread(pthread_t *t, int* running) {
  if(*running) {
    pthread_join(*t, ((void *)0));
    *running = 0;
  }
}

canonical_original_code:
void stop_thread(unsigned long *t, int *running) {
  if(*running) {
    pthread_join(*t, ((void *)0));
    *running = 0;
  }
}


decompiled_code:
const char *__fastcall inaddr2s(int a1)
{
return mtp_inet_ntoa(buf_0, 0x14u, a1);
}

canonical_decompiled_code:
const char *__fastcall func12(int a1)
{
return func1(buf_0, 0x14u, a1);
}

original_code:
const char* inaddr2s(struct in_addr addr)
{
  static char buf[20];
  return mtp_inet_ntoa(buf, sizeof(buf), addr);
}

canonical_original_code:
char *inaddr2s(struct in_addr addr)
{
  static char buf[20];
  return mtp_inet_ntoa(buf, sizeof(buf), addr);
}

variable_types:
addr
struct in_addr { unsigned int s_addr; }

variable_types:
buf
char [20]

return_type: char *


decompiled_code:
void __fastcall lffifo_free(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
free(a1);
}

original_code:
void lffifo_free(struct lffifo *fifo) {
  free(fifo);
}

variable_types: struct lffifo { int size; int start; int end; unsigned char buf[]; } *


original_code:
int lffifo_put(struct lffifo *fifo, unsigned char *data, int size) {
  int i,j;
  int x;
  int iteration;
  if(size <= 0 || size > 0x10000000) {
    return 1;
  }
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->end;
    for(j = 0; j <= size; j++) {
      x = (j == size ? -1 : data[j]);
      if(iteration == 0 && fifo->buf[i] != 0xfe) {
        return 1;
      }
      if(x == 0xfe || x == 0xfd || x == -1) {
        if(iteration == 1) {
          fifo->buf[i] = 0xfd;
        }
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        if(iteration == 0 && fifo->buf[i] != 0xfe) {
          return 1;
        }
        if(x == 0xfe) {
          x = 0x00;
        } else if(x == 0xfd) {
          x = 0x01;
        } else {
          x = 0x02;
        }
      }
      if(iteration == 1) {
        fifo->buf[i] = x;
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
    }
  }
  fifo->end = i;
  return 0;
}

canonical_original_code:
int lffifo_put(struct lffifo *fifo, unsigned char *data, int size) {
  int i; int j;
  int x;
  int iteration;
  if(size <= 0 || size > 0x10000000) {
    return 1;
  }
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->end;
    for(j = 0; j <= size; j++) {
      x = (j == size ? -1 : data[j]);
      if(iteration == 0 && fifo->buf[i] != 0xfe) {
        return 1;
      }
      if(x == 0xfe || x == 0xfd || x == -1) {
        if(iteration == 1) {
          fifo->buf[i] = 0xfd;
        }
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        if(iteration == 0 && fifo->buf[i] != 0xfe) {
          return 1;
        }
        if(x == 0xfe) {
          x = 0x00;
        } else if(x == 0xfd) {
          x = 0x01;
        } else {
          x = 0x02;
        }
      }
      if(iteration == 1) {
        fifo->buf[i] = x;
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
    }
  }
  fifo->end = i;
  return 0;
}

variable_types:
data
unsigned char *

variable_types: size int

variable_types: i int

variable_types: j int

variable_types: x int

variable_types: iteration int

user_defined_types: struct lffifo { int size; int start; int end; unsigned char buf[]; }


original_code:
int lffifo_get(struct lffifo *fifo, unsigned char *buf, int bufsize) {
  int i,j;
  int x;
  int iteration;
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->start;
    j = 0;
    for(;;) {
      x = fifo->buf[i];
      if(iteration == 0) {
        if(x == 0xfe) {
          return 0;
        }
      } else {
        fifo->buf[i] = 0xfe;
      }
      if(x == 0xfd) {
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        x = fifo->buf[i];
        if(iteration == 0) {
          if(x == 0xfe) {
            return 0;
          }
        } else {
          fifo->buf[i] = 0xfe;
        }
        if(x == 0x00) {
          x = 0xfe;
        } else if(x == 0x01) {
          x = 0xfd;
        } else {
          x = -1;
        }
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
      if(x == -1) {
        if(j > bufsize) {
          if(iteration == 1) {
            fifo->start = i;
          }
          return bufsize - j;
        }
        break;
      } else {
        if(iteration == 1) {
          if(j < bufsize) {
            buf[j] = x;
          }
        }
        j++;
      }
      if(j > fifo->size) {
        fifo->start = fifo->end;
        if(iteration == 0) {
          break;
        } else {
          return 0;
        }
      }
    }
  }
  fifo->start = i;
  return j;
}

canonical_original_code:
int lffifo_get(struct lffifo *fifo, unsigned char *buf, int bufsize) {
  int i; int j;
  int x;
  int iteration;
  for(iteration = 0; iteration < 2; iteration++) {
    i = fifo->start;
    j = 0;
    for(;;) {
      x = fifo->buf[i];
      if(iteration == 0) {
        if(x == 0xfe) {
          return 0;
        }
      } else {
        fifo->buf[i] = 0xfe;
      }
      if(x == 0xfd) {
        i++;
        if(i >= fifo->size) {
          i = 0;
        }
        x = fifo->buf[i];
        if(iteration == 0) {
          if(x == 0xfe) {
            return 0;
          }
        } else {
          fifo->buf[i] = 0xfe;
        }
        if(x == 0x00) {
          x = 0xfe;
        } else if(x == 0x01) {
          x = 0xfd;
        } else {
          x = -1;
        }
      }
      i++;
      if(i >= fifo->size) {
        i = 0;
      }
      if(x == -1) {
        if(j > bufsize) {
          if(iteration == 1) {
            fifo->start = i;
          }
          return bufsize - j;
        }
        break;
      } else {
        if(iteration == 1) {
          if(j < bufsize) {
            buf[j] = x;
          }
        }
        j++;
      }
      if(j > fifo->size) {
        fifo->start = fifo->end;
        if(iteration == 0) {
          break;
        } else {
          return 0;
        }
      }
    }
  }
  fifo->start = i;
  return j;
}

variable_types:
buf
unsigned char *

variable_types: bufsize int


gotpkt

repo: yuri-tolstov/projects

decompiled_code:
__int64 __fastcall gotpkt(__int64 a1, __int64 a2)
{
__int64 v2; // rax
bool v3; // sf
__int64 result; // rax

v2 = *(unsigned int *)(a2 + 20);
v3 = g_totlen + v2 < 0;
result = g_totlen + v2;
g_totlen = result;
if ( v3 )
{
g_totlen = 0LL;
g_pktcnt = 0LL;
}
else
{
++g_pktcnt;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
bool v3; // sf
__int64 result; // rax

v2 = *(unsigned int *)(a2 + 20);
v3 = g_totlen + v2 < 0;
result = g_totlen + v2;
g_totlen = result;
if ( v3 )
{
g_totlen = 0LL;
g_pktcnt = 0LL;
}
else
{
++g_pktcnt;
}
return result;
}

original_code:
void gotpkt(u_char *args, const struct pcap_pkthdr *pkth, const u_char *pkt)
{
   g_totlen += pkth->len;
   if (g_totlen < 0) {
      g_pktcnt = g_totlen = 0;
   }
   else {
      g_pktcnt++;
   }
}

canonical_original_code:
void gotpkt(unsigned char *args, struct pcap_pkthdr *pkth, unsigned char *pkt)
{
   g_totlen += pkth->len;
   if (g_totlen < 0) {
      g_pktcnt = g_totlen = 0;
   }
   else {
      g_pktcnt++;
   }
}

variable_types:
args
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
pkt
unsigned char *

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


tmdiff

original_code:
double tmdiff(struct timespec t0, struct timespec t1)
{
   struct timespec tt;
   if ((t1.tv_nsec - t0.tv_nsec) < 0) {
      tt.tv_sec = t1.tv_sec - t0.tv_sec - 1;
      tt.tv_nsec = 1000000000 + t1.tv_nsec - t0.tv_nsec;
   } else {
      tt.tv_sec = t1.tv_sec - t0.tv_sec;
      tt.tv_nsec = t1.tv_nsec - t0.tv_nsec;
   }
   return (double)tt.tv_sec + (double)tt.tv_nsec / 1000000000;
}

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

return_type: double


decompiled_code:
void __fastcall __noreturn monitor_thread(void *a1)
{
__int64 v1; // r13
__int64 v2; // r14
double v3; // xmm0_8
__int64 v4; // rbx
__int64 v5; // rbp
__m128i v6; // [rsp+0h] [rbp-48h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

v1 = g_pktcnt;
v2 = g_totlen;
clock_gettime(0, &tp);
while ( 1 )
{
sleep(1u);
v4 = g_pktcnt;
v5 = g_totlen;
clock_gettime(0, (struct timespec *)&v6);
if ( v4 - v1 > 0 && v4 > 0 )
{
v3 = tmdiff(tp.tv_sec, tp.tv_nsec, v6.m128i_i32[0], v6.m128i_i64[1]);
printf(
"dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
v3,
v4 - v1,
(v5 - v2) / (v4 - v1),
v5 / v4,
v5,
v4);
}
tp = (struct timespec)_mm_loadu_si128(&v6);
v2 = v5;
v1 = v4;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
__int64 v1; // r13
__int64 v2; // r14
double v3; // xmm0_8
__int64 v4; // rbx
__int64 v5; // rbp
__m128i v6; // [rsp+0h] [rbp-48h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

v1 = g_pktcnt;
v2 = g_totlen;
clock_gettime(0, &tp);
while ( 1 )
{
sleep(1u);
v4 = g_pktcnt;
v5 = g_totlen;
clock_gettime(0, (struct timespec *)&v6);
if ( v4 - v1 > 0 && v4 > 0 )
{
v3 = func1(tp.tv_sec, tp.tv_nsec, v6.m128i_i32[0], v6.m128i_i64[1]);
printf(
"dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
v3,
v4 - v1,
(v5 - v2) / (v4 - v1),
v5 / v4,
v5,
v4);
}
tp = (struct timespec)_mm_loadu_si128(&v6);
v2 = v5;
v1 = v4;
}
}

original_code:
void* monitor_thread(void *parm)
{
   int64_t n0, n1, dn, l0, l1, dl;
   struct timespec t0, t1;
   double td;
   n0 = g_pktcnt;
   l0 = g_totlen;
   clock_gettime(0, &t0);
   while (1) {
      sleep(1);
      n1 = g_pktcnt;
      l1 = g_totlen;
      clock_gettime(0, &t1);
      td = tmdiff(t0, t1);
      dn = n1 - n0;
      dl = l1 - l0;
      if (dn > 0 && n1 > 0) {
         printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
                td, dn, dl / dn, l1 / n1, l1, n1);
      }
      t0 = t1;
      n0 = n1;
      l0 = l1;
   }
   return ((void *)0);
}

canonical_original_code:
void *monitor_thread(void *parm)
{
   long n0; long n1; long dn; long l0; long l1; long dl;
   struct timespec t0; struct timespec t1;
   double td;
   n0 = g_pktcnt;
   l0 = g_totlen;
   clock_gettime(0, &t0);
   while (1) {
      sleep(1);
      n1 = g_pktcnt;
      l1 = g_totlen;
      clock_gettime(0, &t1);
      td = tmdiff(t0, t1);
      dn = n1 - n0;
      dl = l1 - l0;
      if (dn > 0 && n1 > 0) {
         printf("dt=%1.3f dn=%ld clen=%ld alen=%ld tlen=%ld tpkts=%ld\n",
                td, dn, dl / dn, l1 / n1, l1, n1);
      }
      t0 = t1;
      n0 = n1;
      l0 = l1;
   }
   return ((void *)0);
}

variable_types:
parm
void *

variable_types: dn long

variable_types: dl long

variable_types: td double

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


partition

original_code:
static int partition (int a[], int l, int r)
{
   int x = a[r];
   int i = l - 1;
   for (int j = l; j <= r - 1; j++) {
      if (a[j] <= x) {
         i++;
         swap(&a[i], &a[j]);
      }
   }
   swap(&a[i + 1], &a[r]);
   return i + 1;
}

canonical_original_code:
static int partition(int a[], int l, int r)
{
   int x = a[r];
   int i = l - 1;
   for (int j = l; j <= r - 1; j++) {
      if (a[j] <= x) {
         i++;
         swap(&a[i], &a[j]);
      }
   }
   swap(&a[i + 1], &a[r]);
   return i + 1;
}

variable_types:
a
int []

variable_types: l int

variable_types: r int


quicksort

decompiled_code:
void __fastcall quicksort(__int64 a1, int a2, int a3)
{
int v4; // ebp

if ( a2 < a3 )
{
v4 = partition(a1, a2, a3);
quicksort(a1, (unsigned int)a2, (unsigned int)(v4 - 1));
quicksort(a1, (unsigned int)(v4 + 1), (unsigned int)a3);
}
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2, int a3)
{
int v4; // ebp

if ( a2 < a3 )
{
v4 = func8(a1, a2, a3);
func9(a1, (unsigned int)a2, (unsigned int)(v4 - 1));
func9(a1, (unsigned int)(v4 + 1), (unsigned int)a3);
}
}

original_code:
static void quicksort(int a[], int l, int r)
{
   if (l < r) {
      int p = partition(a, l, r);
      quicksort(a, l, p - 1);
      quicksort(a, p + 1, r);
   }
}

variable_types: p int


swap

decompiled_code:
__int64 __fastcall swap(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void swap(int *a, int *b)
{
   int t = *a;
   *a = *b;
   *b = t;
}

variable_types:
a
int *

variable_types:
b
int *

variable_types: t int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
int v6[6]; // [rsp+0h] [rbp-38h] BYREF
char v7[32]; // [rsp+18h] [rbp-20h] BYREF

v6[0] = 10;
v6[1] = 7;
v6[2] = 8;
v6[3] = 9;
v6[4] = 1;
v6[5] = 5;
printf("Original array (n=%d): ", 6LL);
v3 = (char *)v6;
v4 = (char *)v6;
do
{
printf("%d ", *(unsigned int *)v4);
v4 += 4;
}
while ( v4 != v7 );
putchar(10);
quicksort((__int64)v6, 0, 5);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v7 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
int v6[6]; // [rsp+0h] [rbp-38h] BYREF
char v7[32]; // [rsp+18h] [rbp-20h] BYREF

v6[0] = 10;
v6[1] = 7;
v6[2] = 8;
v6[3] = 9;
v6[4] = 1;
v6[5] = 5;
printf("Original array (n=%d): ", 6LL);
v3 = (char *)v6;
v4 = (char *)v6;
do
{
printf("%d ", *(unsigned int *)v4);
v4 += 4;
}
while ( v4 != v7 );
putchar(10);
func9((__int64)v6, 0, 5);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v7 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i;
   int a[] = {10, 7, 8, 9, 1, 5};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   quicksort(a, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i;
   int a[] = {10, 7, 8, 9, 1, 5};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   quicksort(a, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

variable_types: i int

variable_types: n int


heapify

decompiled_code:
__int64 __fastcall heapify(unsigned int *a1, int a2)
{
int v3; // edx
int v4; // ebp
__int64 result; // rax
__int64 v6; // rdi

v3 = 2 * a2 + 1;
v4 = 2 * a2 + 2;
result = *a1;
if ( (int)result > v3 )
{
v6 = *((_QWORD *)a1 + 1);
if ( *(_DWORD *)(v6 + 4LL * v3) > *(_DWORD *)(v6 + 4LL * a2) )
{
if ( (int)result <= v4 )
{
v4 = 2 * a2 + 1;
}
else
{
result = v6;
if ( *(_DWORD *)(v6 + 4LL * v4) <= *(_DWORD *)(v6 + 4LL * v3) )
v4 = 2 * a2 + 1;
}
goto LABEL_10;
}
}
if ( (int)result > v4 )
{
result = v4;
if ( *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v4) > *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * a2) )
{
LABEL_10:
if ( v4 != a2 )
{
swap((_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v4), (_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * a2));
result = heapify(a1, (unsigned int)v4);
}
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int *a1, int a2)
{
int v3; // edx
int v4; // ebp
__int64 result; // rax
__int64 v6; // rdi

v3 = 2 * a2 + 1;
v4 = 2 * a2 + 2;
result = *a1;
if ( (int)result > v3 )
{
v6 = *((_QWORD *)a1 + 1);
if ( *(_DWORD *)(v6 + 4LL * v3) > *(_DWORD *)(v6 + 4LL * a2) )
{
if ( (int)result <= v4 )
{
v4 = 2 * a2 + 1;
}
else
{
result = v6;
if ( *(_DWORD *)(v6 + 4LL * v4) <= *(_DWORD *)(v6 + 4LL * v3) )
v4 = 2 * a2 + 1;
}
goto LABEL_10;
}
}
if ( (int)result > v4 )
{
result = v4;
if ( *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v4) > *(_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * a2) )
{
LABEL_10:
if ( v4 != a2 )
{
func7((_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * v4), (_DWORD *)(*((_QWORD *)a1 + 1) + 4LL * a2));
result = func8(a1, (unsigned int)v4);
}
return result;
}
}
return result;
}

original_code:
void heapify(maxheap_t *heap, int idx)
{
    int g = idx;
    int l = (idx << 1) + 1;
    int r = (idx + 1) << 1;
    if ((l < heap->size) && (heap->array[l] > heap->array[g])) {
        g = l;
    }
    if ((r < heap->size) && (heap->array[r] > heap->array[g])) {
        g = r;
    }
    if (g != idx) {
        swap(&heap->array[g], &heap->array[idx]);
        heapify(heap, g);
    }
}

canonical_original_code:
void heapify(struct maxheap_s *heap, int idx)
{
    int g = idx;
    int l = (idx << 1) + 1;
    int r = (idx + 1) << 1;
    if ((l < heap->size) && (heap->array[l] > heap->array[g])) {
        g = l;
    }
    if ((r < heap->size) && (heap->array[r] > heap->array[g])) {
        g = r;
    }
    if (g != idx) {
        swap(&heap->array[g], &heap->array[idx]);
        heapify(heap, g);
    }
}

variable_types: struct maxheap_s { int size; int *array; } *

variable_types: idx int

variable_types: g int


swap

original_code:
static void swap(int* a, int* b) {
   int t = *a;
   *a = *b;
   *b = t;
}

canonical_original_code:
static void swap(int *a, int *b) {
   int t = *a;
   *a = *b;
   *b = t;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
int v6[6]; // [rsp+0h] [rbp-38h] BYREF
char v7[32]; // [rsp+18h] [rbp-20h] BYREF

v6[0] = 12;
v6[1] = 11;
v6[2] = 13;
v6[3] = 5;
v6[4] = 6;
v6[5] = 7;
printf("Original array (n=%d): ", 6LL);
v3 = (char *)v6;
v4 = (char *)v6;
do
{
printf("%d ", *(unsigned int *)v4);
v4 += 4;
}
while ( v4 != v7 );
putchar(10);
heapsort((__int64)v6, 6);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v7 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
int v6[6]; // [rsp+0h] [rbp-38h] BYREF
char v7[32]; // [rsp+18h] [rbp-20h] BYREF

v6[0] = 12;
v6[1] = 11;
v6[2] = 13;
v6[3] = 5;
v6[4] = 6;
v6[5] = 7;
printf("Original array (n=%d): ", 6LL);
v3 = (char *)v6;
v4 = (char *)v6;
do
{
printf("%d ", *(unsigned int *)v4);
v4 += 4;
}
while ( v4 != v7 );
putchar(10);
func10((__int64)v6, 6);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v7 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i;
   int a[] = {12, 11, 13, 5, 6, 7};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   heapsort(a, n);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i;
   int a[] = {12, 11, 13, 5, 6, 7};
   int n = sizeof(a) / sizeof(a[0]);
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   heapsort(a, n);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",a[i]);}
   printf("\n");
   return 0;
}


merge

variable_types: m int

variable_types: j int

variable_types: k int

variable_types:
la
int [-1]

variable_types:
ra
int [-1]


mergesort

decompiled_code:
__int64 __fastcall mergesort(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // er13
__int64 result; // rax

if ( (int)a2 < a3 )
{
v4 = ((int)a2 + a3) / 2;
mergesort(a1, a2, v4);
mergesort(a1, v4 + 1, (unsigned int)a3);
result = merge(a1, a2, v4, a3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // er13
__int64 result; // rax

if ( (int)a2 < a3 )
{
v4 = ((int)a2 + a3) / 2;
func8(a1, a2, v4);
func8(a1, v4 + 1, (unsigned int)a3);
result = func7(a1, a2, v4, a3);
}
return result;
}

original_code:
void mergesort(int a[], int l, int r)
{
    int m;
    if (l < r) {
         m = (l + r) / 2;
         mergesort(a, l, m);
         mergesort(a, m + 1, r);
         merge(a, l, m, r);
    }
}

variable_types:
a
int []


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
char *v5; // rbp
_BYTE v7[48]; // [rsp+0h] [rbp-48h] BYREF
char v8[24]; // [rsp+30h] [rbp-18h] BYREF

v3 = v7;
v4 = v7;
do
{
*(_DWORD *)v4 = rand() % 12;
v4 += 4;
}
while ( v4 != v8 );
printf("Original array (n=%d): ", 12LL);
v5 = v7;
do
{
printf("%d ", *(unsigned int *)v5);
v5 += 4;
}
while ( v5 != v8 );
putchar(10);
mergesort((__int64)v7, 0LL, 11);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v8 );
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
char *v5; // rbp
_BYTE v7[48]; // [rsp+0h] [rbp-48h] BYREF
char v8[24]; // [rsp+30h] [rbp-18h] BYREF

v3 = v7;
v4 = v7;
do
{
*(_DWORD *)v4 = rand() % 12;
v4 += 4;
}
while ( v4 != v8 );
printf("Original array (n=%d): ", 12LL);
v5 = v7;
do
{
printf("%d ", *(unsigned int *)v5);
v5 += 4;
}
while ( v5 != v8 );
putchar(10);
func8((__int64)v7, 0LL, 11);
printf("Sorted array: ");
do
{
printf("%d ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v8 );
putchar(10);
return 0;
}

original_code:
int main(void)
{
   int i, n = 12;
   int mtable[12];
   for (i = 0; i < 12; i++) {
      mtable[i] = rand() % 12;
   }
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   mergesort(mtable, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   return 0;
}

canonical_original_code:
int main()
{
   int i; int n = 12;
   int mtable[12];
   for (i = 0; i < 12; i++) {
      mtable[i] = rand() % 12;
   }
   printf("Original array (n=%d): ", n);
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   mergesort(mtable, 0, n - 1);
   printf("Sorted array: ");
   for(i = 0; i < n; i++) {printf("%d ",mtable[i]);}
   printf("\n");
   return 0;
}

variable_types:
mtable
int [12]


dictGenericDelete

original_code:
static int dictGenericDelete(struct dict *d, const void *key, int nofree)
{
    unsigned int h, idx;
    struct dictEntry *he, *prevHe;
    if (d->size == 0) return 1;
    h = (d)->type->hashFunction(key);
    idx = h & d->sizemask;
    he = d->table[idx];
    prevHe = ((void *)0);
    while(he) {
        if ((((d)->type->keyCompare) ? (d)->type->keyCompare(key, he->key) : (key) == (he->key))) {
            if (prevHe)
                prevHe->next = he->next;
            else
                d->table[idx] = he->next;
            if (!nofree) {
                if ((d)->type->keyDestructor && he->key) (d)->type->keyDestructor((he)->key);
                if ((d)->type->valDestructor && (he)->v.val) (d)->type->valDestructor((he)->v.val);
            }
            wfree(he);
            d->used--;
            return 0;
        }
        prevHe = he;
        he = he->next;
    }
    return 1;
}

canonical_original_code:
static int dictGenericDelete(struct dict *d, void *key, int nofree)
{
    unsigned int h; unsigned int idx;
    struct dictEntry *he; struct dictEntry *prevHe;
    if (d->size == 0) return 1;
    h = (d)->type->hashFunction(key);
    idx = h & d->sizemask;
    he = d->table[idx];
    prevHe = ((void *)0);
    while(he) {
        if ((((d)->type->keyCompare) ? (d)->type->keyCompare(key, he->key) : (key) == (he->key))) {
            if (prevHe)
                prevHe->next = he->next;
            else
                d->table[idx] = he->next;
            if (!nofree) {
                if ((d)->type->keyDestructor && he->key) (d)->type->keyDestructor((he)->key);
                if ((d)->type->valDestructor && (he)->v.val) (d)->type->valDestructor((he)->v.val);
            }
            wfree(he);
            d->used--;
            return 0;
        }
        prevHe = he;
        he = he->next;
    }
    return 1;
}

variable_types: struct dict { struct dictEntry { void *key; union { void *val; unsigned long u64; long s64; } v; struct dictEntry *next; } **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType { unsigned int (*hashFunction)(void *key); void *(*keyDup)(void *key); void *(*valDup)(void *obj); int (*keyCompare)(void *key1, void *key2); void (*keyDestructor)(void *key); void (*valDestructor)(void *obj); } *type; } *

variable_types:
key
void *

variable_types: nofree int

variable_types:
h
unsigned int

variable_types:
idx
unsigned int

user_defined_types: struct dict { struct dictEntry **table; unsigned long size; unsigned long sizemask; unsigned long used; struct dictType *type; }