waitForNextBlock

decompiled_code:
int __fastcall waitForNextBlock(__int64 a1)
{
pthread_mutex_t *v1; // rbp

v1 = (pthread_mutex_t *)(a1 + 24);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( !*(_DWORD *)(a1 + 112) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), v1);
--*(_DWORD *)(a1 + 112);
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
return pthread_mutex_unlock(v1);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
pthread_mutex_t *v1; // rbp

v1 = (pthread_mutex_t *)(a1 + 24);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( !*(_DWORD *)(a1 + 112) )
pthread_cond_wait((pthread_cond_t *)(a1 + 64), v1);
--*(_DWORD *)(a1 + 112);
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
return pthread_mutex_unlock(v1);
}

original_code:
static void waitForNextBlock(BufferedReaderData * data) {
 pthread_mutex_lock(&data->count_mutex);
 if (data->blockCount == 0) {
  pthread_cond_wait(&data->count_cond, &data->count_mutex);
 }
 data->blockCount--;
 pthread_cond_signal(&data->count_cond);
 pthread_mutex_unlock(&data->count_mutex);
}

canonical_original_code:
static void waitForNextBlock(struct bufferedReaderData_st *data) {
 pthread_mutex_lock(&data->count_mutex);
 if (data->blockCount == 0) {
  pthread_cond_wait(&data->count_cond, &data->count_mutex);
 }
 data->blockCount--;
 pthread_cond_signal(&data->count_cond);
 pthread_mutex_unlock(&data->count_mutex);
}

user_defined_types: struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; }

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

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


destroyBlockData

repo: Ensembl/WiggleTools

decompiled_code:
void __fastcall destroyBlockData(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
static void destroyBlockData(BlockData * data) {
 free(data->chrom);
 free(data->start);
 free(data->finish);
 free(data->value);
 free(data);
}

canonical_original_code:
static void destroyBlockData(struct blockData_st *data) {
 free(data->chrom);
 free(data->start);
 free(data->finish);
 free(data->value);
 free(data);
}

variable_types: struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; } *

return_type: void


goToNextBlock

decompiled_code:
void __fastcall goToNextBlock(__int64 a1)
{
_QWORD *v1; // rax

v1 = *(_QWORD **)(a1 + 8);
*(_QWORD *)(a1 + 8) = v1[5];
*(_DWORD *)(a1 + 116) = 0;
destroyBlockData(v1);
}

original_code:
static void goToNextBlock(BufferedReaderData * data) {
 BlockData * prevBlockData = data->blockData;
 data->blockData = data->blockData->next;
 data->readIndex = 0;
 destroyBlockData(prevBlockData);
}

canonical_original_code:
static void goToNextBlock(struct bufferedReaderData_st *data) {
 struct blockData_st *prevBlockData = data->blockData;
 data->blockData = data->blockData->next;
 data->readIndex = 0;
 destroyBlockData(prevBlockData);
}

variable_types: struct bufferedReaderData_st { unsigned long downloaderThreadID; struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; } *blockData; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } count_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } count_cond; int blockCount; int readIndex; void *readerData; char killed; } *

user_defined_types: struct bufferedReaderData_st { unsigned long downloaderThreadID; struct blockData_st *blockData; union pthread_mutex_t count_mutex; union pthread_cond_t count_cond; int blockCount; int readIndex; void *readerData; char killed; }

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

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

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


pushValuesToBuffer

original_code:
char pushValuesToBuffer(BufferedReaderData * data, const char * chrom, int start, int finish, double value) {
 if (data->blockData == ((void *)0))
  data->lastBlockData = data->blockData = createBlockData();
 else if (data->lastBlockData->count == BLOCK_SIZE) {
  data->lastBlockData->next = createBlockData();
  data->lastBlockData = data->lastBlockData->next;
  if (declareNewBlock(data))
   return 1;
 }
 int index = data->lastBlockData->count;
 data->lastBlockData->chrom[index] = chrom;
 data->lastBlockData->start[index] = start;
 data->lastBlockData->finish[index] = finish;
 data->lastBlockData->value[index] = value;
 data->lastBlockData->count++;
 return 0;
}

canonical_original_code:
char pushValuesToBuffer(struct bufferedReaderData_st *data, char *chrom, int start, int finish, double value) {
 if (data->blockData == ((void *)0))
  data->lastBlockData = data->blockData = createBlockData();
 else if (data->lastBlockData->count == BLOCK_SIZE) {
  data->lastBlockData->next = createBlockData();
  data->lastBlockData = data->lastBlockData->next;
  if (declareNewBlock(data))
   return 1;
 }
 int index = data->lastBlockData->count;
 data->lastBlockData->chrom[index] = chrom;
 data->lastBlockData->start[index] = start;
 data->lastBlockData->finish[index] = finish;
 data->lastBlockData->value[index] = value;
 data->lastBlockData->count++;
 return 0;
}

variable_types:
chrom
char *

variable_types: start int

variable_types: finish int

variable_types: value double

variable_types: index int

return_type: char


endBufferedSignal

decompiled_code:
__int64 __fastcall endBufferedSignal(__int64 a1)
{
declareNewBlock(a1);
return declareNewBlock(a1);
}

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

original_code:
void endBufferedSignal(BufferedReaderData * data) {
 declareNewBlock(data);
 declareNewBlock(data);
}

canonical_original_code:
void endBufferedSignal(struct bufferedReaderData_st *data) {
 declareNewBlock(data);
 declareNewBlock(data);
}


launchBufferedReader

decompiled_code:
int __fastcall launchBufferedReader(void *(*start_routine)(void *), void *arg, _QWORD *a3)
{
char *v4; // rbx
unsigned int v5; // eax

v4 = (char *)calloc(1uLL, 0x88uLL);
*a3 = v4;
*((_DWORD *)v4 + 29) = 0;
*((_QWORD *)v4 + 15) = arg;
pthread_mutex_init((pthread_mutex_t *)(v4 + 24), 0LL);
pthread_cond_init((pthread_cond_t *)(v4 + 64), 0LL);
v5 = pthread_create((pthread_t *)v4, 0LL, start_routine, arg);
if ( v5 )
{
fprintf(stderr, "Could not create new thread %i\n", v5);
abort();
}
return waitForNextBlock((__int64)v4);
}

canonical_decompiled_code:
int __fastcall func7(void *(*start_routine)(void *), void *arg, _QWORD *a3)
{
char *v4; // rbx
unsigned int v5; // eax

v4 = (char *)calloc(1uLL, 0x88uLL);
*a3 = v4;
*((_DWORD *)v4 + 29) = 0;
*((_QWORD *)v4 + 15) = arg;
pthread_mutex_init((pthread_mutex_t *)(v4 + 24), 0LL);
pthread_cond_init((pthread_cond_t *)(v4 + 64), 0LL);
v5 = pthread_create((pthread_t *)v4, 0LL, start_routine, arg);
if ( v5 )
{
fprintf(stderr, "Could not create new thread %i\n", v5);
abort();
}
return func2((__int64)v4);
}

original_code:
void launchBufferedReader(void * (* readFileFunction)(void *), void * f_data, BufferedReaderData ** buf_data) {
 BufferedReaderData * data = calloc(1, sizeof(BufferedReaderData));
 *buf_data = data;
 data->readIndex = 0;
 data->readerData = f_data;
 pthread_mutex_init(&data->count_mutex, ((void *)0));
 pthread_cond_init(&data->count_cond, ((void *)0));
 int err = pthread_create(&(data->downloaderThreadID), ((void *)0), readFileFunction, f_data);
 if (err) {
  fprintf(stderr, "Could not create new thread %i\n", err);
  abort();
 }
 waitForNextBlock(data);
}

canonical_original_code:
void launchBufferedReader(void *(*readFileFunction)(void *), void *f_data, struct bufferedReaderData_st **buf_data) {
 struct bufferedReaderData_st *data = calloc(1, sizeof(BufferedReaderData));
 *buf_data = data;
 data->readIndex = 0;
 data->readerData = f_data;
 pthread_mutex_init(&data->count_mutex, ((void *)0));
 pthread_cond_init(&data->count_cond, ((void *)0));
 int err = pthread_create(&(data->downloaderThreadID), ((void *)0), readFileFunction, f_data);
 if (err) {
  fprintf(stderr, "Could not create new thread %i\n", err);
  abort();
 }
 waitForNextBlock(data);
}

variable_types:
readFileFunction
void *(*)(void *)

variable_types: struct bufferedReaderData_st { unsigned long downloaderThreadID; struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; } *blockData; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } count_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } count_cond; int blockCount; int readIndex; void *readerData; char killed; } **

variable_types: err int


killBufferedReader

decompiled_code:
void __fastcall killBufferedReader(__int64 a1)
{
_QWORD *i; // rdi

if ( !*(_BYTE *)(a1 + 128) )
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
*(_DWORD *)(a1 + 112) = -1;
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
pthread_join(*(_QWORD *)a1, 0LL);
pthread_mutex_destroy((pthread_mutex_t *)(a1 + 24));
pthread_cond_destroy((pthread_cond_t *)(a1 + 64));
for ( i = *(_QWORD **)(a1 + 8); i; i = *(_QWORD **)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = i[5];
destroyBlockData(i);
}
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 112) = 0;
*(_BYTE *)(a1 + 128) = 1;
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
_QWORD *i; // rdi

if ( !*(_BYTE *)(a1 + 128) )
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
*(_DWORD *)(a1 + 112) = -1;
pthread_cond_signal((pthread_cond_t *)(a1 + 64));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
pthread_join(*(_QWORD *)a1, 0LL);
pthread_mutex_destroy((pthread_mutex_t *)(a1 + 24));
pthread_cond_destroy((pthread_cond_t *)(a1 + 64));
for ( i = *(_QWORD **)(a1 + 8); i; i = *(_QWORD **)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = i[5];
func3(i);
}
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 112) = 0;
*(_BYTE *)(a1 + 128) = 1;
}
}

original_code:
void killBufferedReader(BufferedReaderData * data) {
 if (data->killed)
  return;
 pthread_mutex_lock(&data->count_mutex);
 data->blockCount = -1;
 pthread_cond_signal(&data->count_cond);
 pthread_mutex_unlock(&data->count_mutex);
 pthread_join(data->downloaderThreadID, ((void *)0));
 pthread_mutex_destroy(&data->count_mutex);
 pthread_cond_destroy(&data->count_cond);
 while (data->blockData) {
  BlockData * prevData = data->blockData;
  data->blockData = data->blockData->next;
  destroyBlockData(prevData);
 }
 data->lastBlockData = ((void *)0);
 data->blockCount = 0;
 data->killed = 1;
}

canonical_original_code:
void killBufferedReader(struct bufferedReaderData_st *data) {
 if (data->killed)
  return;
 pthread_mutex_lock(&data->count_mutex);
 data->blockCount = -1;
 pthread_cond_signal(&data->count_cond);
 pthread_mutex_unlock(&data->count_mutex);
 pthread_join(data->downloaderThreadID, ((void *)0));
 pthread_mutex_destroy(&data->count_mutex);
 pthread_cond_destroy(&data->count_cond);
 while (data->blockData) {
  struct blockData_st *prevData = data->blockData;
  data->blockData = data->blockData->next;
  destroyBlockData(prevData);
 }
 data->lastBlockData = ((void *)0);
 data->blockCount = 0;
 data->killed = 1;
}


BufferedReaderPop

original_code:
void BufferedReaderPop(WiggleIterator * wi, BufferedReaderData * data) {
 if (wi->done)
  return;
 else if (data == ((void *)0) || data->blockData == ((void *)0)) {
  wi->done = 1;
  return;
 } else if (data->readIndex == data->blockData->count) {
  waitForNextBlock(data);
  goToNextBlock(data);
  if (data->blockData == ((void *)0)) {
   killBufferedReader(data);
   wi->done = 1;
   return;
  }
 }
 int index = data->readIndex;
 wi->chrom = data->blockData->chrom[index];
 wi->start = data->blockData->start[index];
 wi->finish = data->blockData->finish[index];
 wi->value = (double) data->blockData->value[index];
 data->readIndex++;
}

canonical_original_code:
void BufferedReaderPop(struct wiggleIterator_st *wi, struct bufferedReaderData_st *data) {
 if (wi->done)
  return;
 else if (data == ((void *)0) || data->blockData == ((void *)0)) {
  wi->done = 1;
  return;
 } else if (data->readIndex == data->blockData->count) {
  waitForNextBlock(data);
  goToNextBlock(data);
  if (data->blockData == ((void *)0)) {
   killBufferedReader(data);
   wi->done = 1;
   return;
  }
 }
 int index = data->readIndex;
 wi->chrom = data->blockData->chrom[index];
 wi->start = data->blockData->start[index];
 wi->finish = data->blockData->finish[index];
 wi->value = (double) data->blockData->value[index];
 data->readIndex++;
}

variable_types: struct wiggleIterator_st { char *chrom; int start; int finish; double value; void *valuePtr; char done; int strand; void *data; void (*pop)(struct wiggleIterator_st *); void (*seek)(struct wiggleIterator_st *, char *, int, int); char overlaps; double default_value; struct wiggleIterator_st *append; } *


decompiled_code:
int __fastcall compare_chrom_lengths(const char **a1, const char **a2)
{
return strcmp(*a1, *a2);
}

canonical_decompiled_code:
int __fastcall func10(const char **a1, const char **a2)
{
return strcmp(*a1, *a2);
}

original_code:
int compare_chrom_lengths(const void * A, const void * B) {
 Chrom_length * cl_A = (Chrom_length *) A;
 Chrom_length * cl_B = (Chrom_length *) B;
 return strcmp(cl_A->chrom, cl_B->chrom);
}

canonical_original_code:
int compare_chrom_lengths(void *A, void *B) {
 struct chrom_length_st *cl_A = (Chrom_length *) A;
 struct chrom_length_st *cl_B = (Chrom_length *) B;
 return strcmp(cl_A->chrom, cl_B->chrom);
}

variable_types:
A
void *

variable_types:
B
void *

variable_types: struct chrom_length_st { char *chrom; int length; } *

return_type: int


main

canonical_name: main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // cf
bool v4; // zf
int v5; // ebx
const char **v6; // rbp
__int64 v7; // rcx

v3 = argc == 0;
v4 = argc == 1;
if ( argc <= 1 )
goto LABEL_6;
v5 = argc;
v6 = argv;
argv = (const char **)argv[1];
*(_QWORD *)&argc = "--help";
v7 = 7LL;
do
{
if ( !v7 )
break;
v3 = *(_BYTE *)argv < **(_BYTE **)&argc;
v4 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v7;
}
while ( v4 );
if ( (!v3 && !v4) == v3 )
{
LABEL_6:
printHelp(*(_QWORD *)&argc, argv, envp);
}
else
{
libBigWigInit(128000LL, argv, envp);
rollYourOwn((unsigned int)(v5 - 1), v6 + 1);
}
return 0;
}

original_code:
int main(int argc, char ** argv) {
 if (argc < 2 || strcmp(argv[1], "--help") == 0) {
  printHelp();
  return 0;
 }
 libBigWigInit(128000);
 rollYourOwn(argc-1, argv+1);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 if (argc < 2 || strcmp(argv[1], "--help") == 0) {
  printHelp();
  return 0;
 }
 libBigWigInit(128000);
 rollYourOwn(argc-1, argv+1);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


initRecycleBin

decompiled_code:
unsigned __int64 __fastcall initRecycleBin(__int64 a1, unsigned __int64 a2, int a3)
{
__int64 v3; // rdx
unsigned __int64 v4; // rdx
bool v5; // cf
unsigned __int64 v6; // rax
unsigned __int64 result; // rax

v3 = a2 * a3;
v5 = v3 == -8;
v4 = v3 + 8;
if ( v5 || v4 == 1 )
{
v6 = 1LL;
}
else
{
v6 = 1LL;
do
v6 *= 2LL;
while ( v4 > v6 );
}
result = (v6 - 8) / a2;
*(_QWORD *)a1 = 0LL;
*(_DWORD *)(a1 + 24) = result;
*(_DWORD *)(a1 + 28) = result;
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, unsigned __int64 a2, int a3)
{
__int64 v3; // rdx
unsigned __int64 v4; // rdx
bool v5; // cf
unsigned __int64 v6; // rax
unsigned __int64 result; // rax

v3 = a2 * a3;
v5 = v3 == -8;
v4 = v3 + 8;
if ( v5 || v4 == 1 )
{
v6 = 1LL;
}
else
{
v6 = 1LL;
do
v6 *= 2LL;
while ( v4 > v6 );
}
result = (v6 - 8) / a2;
*(_QWORD *)a1 = 0LL;
*(_DWORD *)(a1 + 24) = result;
*(_DWORD *)(a1 + 28) = result;
*(_QWORD *)(a1 + 16) = a2;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}

original_code:
static void initRecycleBin(RecycleBin *recycleBin,
      size_t node_size, int nodes_per_chunk)
{
 size_t chunckSize, allocSize;
 chunckSize = sizeof(Chunk) + nodes_per_chunk * node_size;
 allocSize = 1;
 while (allocSize < chunckSize)
  allocSize <<= 1;
 nodes_per_chunk = (allocSize - sizeof(Chunk)) / node_size;
 recycleBin->chunk_list = ((void *)0);
 recycleBin->chunk_pos = nodes_per_chunk;
 recycleBin->nodes_per_chunk = nodes_per_chunk;
 recycleBin->node_size = node_size;
 recycleBin->recycle = ((void *)0);
}

canonical_original_code:
static void initRecycleBin(struct recycleBin_st *recycleBin, unsigned long node_size, int nodes_per_chunk)
{
 unsigned long chunckSize; unsigned long allocSize;
 chunckSize = sizeof(Chunk) + nodes_per_chunk * node_size;
 allocSize = 1;
 while (allocSize < chunckSize)
  allocSize <<= 1;
 nodes_per_chunk = (allocSize - sizeof(Chunk)) / node_size;
 recycleBin->chunk_list = ((void *)0);
 recycleBin->chunk_pos = nodes_per_chunk;
 recycleBin->nodes_per_chunk = nodes_per_chunk;
 recycleBin->node_size = node_size;
 recycleBin->recycle = ((void *)0);
}

variable_types: struct recycleBin_st { struct chunk_st { struct chunk_st *next; } *chunk_list; struct RecycleBin_Node { struct RecycleBin_Node *next; } *recycle; unsigned long node_size; int chunk_pos; int nodes_per_chunk; } *

variable_types:
chunckSize
unsigned long

variable_types:
allocSize
unsigned long

user_defined_types: struct recycleBin_st { struct chunk_st *chunk_list; struct RecycleBin_Node *recycle; unsigned long node_size; int chunk_pos; int nodes_per_chunk; }


newRecycleBin

decompiled_code:
void *__fastcall newRecycleBin(unsigned __int64 a1, int a2)
{
void *v2; // rax
void *v3; // rbx

if ( a1 <= 7 )
{
fwrite("Too small elements to create a recycle bin!\n", 1uLL, 0x2CuLL, stderr);
exit(-1);
}
v2 = malloc(0x20uLL);
v3 = v2;
if ( !v2 )
{
fwrite("Out of memory, exiting.\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
initRecycleBin((__int64)v2, a1, a2);
return v3;
}

canonical_decompiled_code:
void *__fastcall func2(unsigned __int64 a1, int a2)
{
void *v2; // rax
void *v3; // rbx

if ( a1 <= 7 )
{
fwrite("Too small elements to create a recycle bin!\n", 1uLL, 0x2CuLL, stderr);
exit(-1);
}
v2 = malloc(0x20uLL);
v3 = v2;
if ( !v2 )
{
fwrite("Out of memory, exiting.\n", 1uLL, 0x18uLL, stderr);
exit(-1);
}
func0((__int64)v2, a1, a2);
return v3;
}

original_code:
RecycleBin *newRecycleBin(size_t node_size, int nodes_per_chunk)
{
 RecycleBin *recycleBin;
 if (node_size < sizeof(RecycleBin_Node)) {
  fprintf(stderr, "Too small elements to create a recycle bin!\n");
  exit(-1);
 }
 recycleBin = malloc(sizeof(RecycleBin));
 if (!recycleBin) {
  fprintf(stderr, "Out of memory, exiting.\n");
  exit(-1);
 }
 initRecycleBin (recycleBin, node_size, nodes_per_chunk);
 return recycleBin;
}

canonical_original_code:
struct recycleBin_st *newRecycleBin(unsigned long node_size, int nodes_per_chunk)
{
 struct recycleBin_st *recycleBin;
 if (node_size < sizeof(RecycleBin_Node)) {
  fprintf(stderr, "Too small elements to create a recycle bin!\n");
  exit(-1);
 }
 recycleBin = malloc(sizeof(RecycleBin));
 if (!recycleBin) {
  fprintf(stderr, "Out of memory, exiting.\n");
  exit(-1);
 }
 initRecycleBin (recycleBin, node_size, nodes_per_chunk);
 return recycleBin;
}

return_type: struct recycleBin_st { struct chunk_st { struct chunk_st *next; } *chunk_list; struct RecycleBin_Node { struct RecycleBin_Node *next; } *recycle; unsigned long node_size; int chunk_pos; int nodes_per_chunk; } *


destroyRecycleBin

decompiled_code:
void __fastcall destroyRecycleBin(void *ptr)
{
if ( ptr )
{
destroyRecycleBinChunks((_QWORD **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
if ( ptr )
{
func1((_QWORD **)ptr);
free(ptr);
}
}

original_code:
void destroyRecycleBin(RecycleBin * recycleBin)
{
 if (recycleBin == ((void *)0))
  return;
 destroyRecycleBinChunks(recycleBin);
 free(recycleBin);
}

canonical_original_code:
void destroyRecycleBin(struct recycleBin_st *recycleBin)
{
 if (recycleBin == ((void *)0))
  return;
 destroyRecycleBinChunks(recycleBin);
 free(recycleBin);
}

user_defined_types: struct chunk_st { struct chunk_st *next; }

user_defined_types: struct RecycleBin_Node { struct RecycleBin_Node *next; }


deallocatePointer

decompiled_code:
__int64 __fastcall deallocatePointer(__int64 a1, _QWORD *a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*a2 = result;
*(_QWORD *)(a1 + 8) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _QWORD *a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*a2 = result;
*(_QWORD *)(a1 + 8) = a2;
return result;
}

original_code:
void deallocatePointer(RecycleBin * recycle_bin, void *data)
{
 RecycleBin_Node *node = data;
 node->next = recycle_bin->recycle;
 recycle_bin->recycle = node;
 return;
}

canonical_original_code:
void deallocatePointer(struct recycleBin_st *recycle_bin, void *data)
{
 struct RecycleBin_Node *node = data;
 node->next = recycle_bin->recycle;
 recycle_bin->recycle = node;
 return;
}

variable_types:
data
void *

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


isSeparator

decompiled_code:
__int64 __fastcall isSeparator(char a1)
{
const char *v1; // rax

v1 = "MIDNSHPX=";
while ( *v1 != a1 )
{
if ( ++v1 == "" )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
const char *v1; // rax

v1 = "MIDNSHPX=";
while ( *v1 != a1 )
{
if ( ++v1 == "" )
return 0LL;
}
return 1LL;
}

original_code:
static int isSeparator(char c) {
 char * separators = "MIDNSHPX=";
 int i;
 for (i = 0; i < 9; i++)
  if (c == separators[i])
   return 1;
 return 0;
}

canonical_original_code:
static int isSeparator(char c) {
 char *separators = "MIDNSHPX=";
 int i;
 for (i = 0; i < 9; i++)
  if (c == separators[i])
   return 1;
 return 0;
}

variable_types: c char

variable_types:
separators
char *

variable_types: i int


readNextCigarBlock

decompiled_code:
const char *__fastcall readNextCigarBlock(const char *a1, _BYTE *a2)
{
int v2; // eax
__int64 v3; // r13
__int64 i; // rbx
__int64 v5; // rax
__int64 v6; // rdx

v2 = strlen(a1);
if ( v2 <= 0 )
{
*a2 = 0;
v6 = 0LL;
}
else
{
v3 = (unsigned int)(v2 - 1) + 1LL;
for ( i = 1LL; ; ++i )
{
a2[i - 1] = a1[i - 1];
if ( i == v3 )
break;
if ( (_DWORD)i && (unsigned int)isSeparator(a1[i - 1]) )
{
v5 = (int)i;
a2[(int)i] = 0;
return &a1[v5];
}
}
v5 = (int)i;
a2[(int)i] = 0;
v6 = 0LL;
if ( (_DWORD)i )
return &a1[v5];
}
return (const char *)v6;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1, _BYTE *a2)
{
int v2; // eax
__int64 v3; // r13
__int64 i; // rbx
__int64 v5; // rax
__int64 v6; // rdx

v2 = strlen(a1);
if ( v2 <= 0 )
{
*a2 = 0;
v6 = 0LL;
}
else
{
v3 = (unsigned int)(v2 - 1) + 1LL;
for ( i = 1LL; ; ++i )
{
a2[i - 1] = a1[i - 1];
if ( i == v3 )
break;
if ( (_DWORD)i && (unsigned int)func0(a1[i - 1]) )
{
v5 = (int)i;
a2[(int)i] = 0;
return &a1[v5];
}
}
v5 = (int)i;
a2[(int)i] = 0;
v6 = 0LL;
if ( (_DWORD)i )
return &a1[v5];
}
return (const char *)v6;
}

original_code:
static char * readNextCigarBlock(char * cigar, char * block) {
 int i;
 int lgth = strlen(cigar);
 for (i = 0; i < lgth && (i == 0 || !isSeparator(cigar[i-1])); i++)
  block[i] = cigar[i];
 block[i] = '\0';
 if (i)
  return cigar + i;
 else
  return ((void *)0);
}

canonical_original_code:
static char *readNextCigarBlock(char *cigar, char *block) {
 int i;
 int lgth = strlen(cigar);
 for (i = 0; i < lgth && (i == 0 || !isSeparator(cigar[i-1])); i++)
  block[i] = cigar[i];
 block[i] = '\0';
 if (i)
  return cigar + i;
 else
  return ((void *)0);
}

variable_types:
cigar
char *

variable_types:
block
char *

variable_types: lgth int

return_type: char *


lowerMinNRows

original_code:
static void lowerMinNRows(Histogram * hist, double value) {
 if (hist->max != hist->min) {
  double ratio = (hist->max - hist->min) / (hist->max - value);
  int row, column;
  for (row = 0; row < hist->count; row++)
   for (column = hist->width; column >= 0; column--)
    reassignColumnRight(hist, ratio, column, row);
 } else {
  int row;
  for (row = 0; row < hist->count; row++) {
   hist->values[row][hist->width - 1] = hist->values[row][0];
   hist->values[row][0] = 0;
  }
 }
 hist->min = value;
}

canonical_original_code:
static void lowerMinNRows(struct histogram_st *hist, double value) {
 if (hist->max != hist->min) {
  double ratio = (hist->max - hist->min) / (hist->max - value);
  int row; int column;
  for (row = 0; row < hist->count; row++)
   for (column = hist->width; column >= 0; column--)
    reassignColumnRight(hist, ratio, column, row);
 } else {
  int row;
  for (row = 0; row < hist->count; row++) {
   hist->values[row][hist->width - 1] = hist->values[row][0];
   hist->values[row][0] = 0;
  }
 }
 hist->min = value;
}

variable_types: struct histogram_st { int width; int count; double **values; double min; double max; } *

variable_types: value double

variable_types: ratio double

variable_types: row int

variable_types: column int


raiseMaxNRows

decompiled_code:
double *__fastcall raiseMaxNRows(__int64 a1, double a2)
{
double v3; // xmm0_8
double v4; // xmm1_8
int i; // er12
double *result; // rax
int j; // ebx

v3 = *(double *)(a1 + 24);
v4 = *(double *)(a1 + 16);
if ( v3 != v4 )
{
for ( i = 0; *(_DWORD *)(a1 + 4) > i; ++i )
{
for ( j = 1; *(_DWORD *)a1 > j; ++j )
result = reassignColumnLeft(a1, j, i, (v3 - v4) / (a2 - v4));
}
}
*(double *)(a1 + 24) = a2;
return result;
}

canonical_decompiled_code:
double *__fastcall func3(__int64 a1, double a2)
{
double v3; // xmm0_8
double v4; // xmm1_8
int i; // er12
double *result; // rax
int j; // ebx

v3 = *(double *)(a1 + 24);
v4 = *(double *)(a1 + 16);
if ( v3 != v4 )
{
for ( i = 0; *(_DWORD *)(a1 + 4) > i; ++i )
{
for ( j = 1; *(_DWORD *)a1 > j; ++j )
result = func2(a1, j, i, (v3 - v4) / (a2 - v4));
}
}
*(double *)(a1 + 24) = a2;
return result;
}

original_code:
static void raiseMaxNRows(Histogram * hist, double value) {
 if (hist->max != hist->min) {
  double ratio = (hist->max - hist->min) / (value - hist->min);
  int row, column;
  for (row = 0; row < hist->count; row++)
   for (column = 1; column < hist->width; column++)
    reassignColumnLeft(hist, ratio, column, row);
 }
 hist->max = value;
}

canonical_original_code:
static void raiseMaxNRows(struct histogram_st *hist, double value) {
 if (hist->max != hist->min) {
  double ratio = (hist->max - hist->min) / (value - hist->min);
  int row; int column;
  for (row = 0; row < hist->count; row++)
   for (column = 1; column < hist->width; column++)
    reassignColumnLeft(hist, ratio, column, row);
 }
 hist->max = value;
}

user_defined_types: struct histogram_st { int width; int count; double **values; double min; double max; }


updateHistogram

decompiled_code:
__int64 __fastcall updateHistogram(__int64 a1, __int64 a2, int a3)
{
double v4; // xmm0_8
double *v5; // rdx
__int64 result; // rax

v4 = *(double *)(a2 + 16);
if ( v4 > *(double *)(a1 + 24) )
{
raiseMaxNRows(a1, v4);
}
else if ( *(double *)(a1 + 16) > v4 )
{
lowerMinNRows((double *)a1, v4);
}
if ( *(double *)(a1 + 16) != *(double *)(a1 + 24) )
return insertIntoHistogram(a1, a2, a3);
v5 = *(double **)(*(_QWORD *)(a1 + 8) + 8LL * a3);
result = (unsigned int)(*(_DWORD *)(a2 + 12) - *(_DWORD *)(a2 + 8));
*v5 = (double)(int)result + *v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3)
{
double v4; // xmm0_8
double *v5; // rdx
__int64 result; // rax

v4 = *(double *)(a2 + 16);
if ( v4 > *(double *)(a1 + 24) )
{
func3(a1, v4);
}
else if ( *(double *)(a1 + 16) > v4 )
{
func1((double *)a1, v4);
}
if ( *(double *)(a1 + 16) != *(double *)(a1 + 24) )
return func4(a1, a2, a3);
v5 = *(double **)(*(_QWORD *)(a1 + 8) + 8LL * a3);
result = (unsigned int)(*(_DWORD *)(a2 + 12) - *(_DWORD *)(a2 + 8));
*v5 = (double)(int)result + *v5;
return result;
}

original_code:
static void updateHistogram(Histogram * hist, WiggleIterator * wig, int row) {
 if (wig->value > hist->max)
  raiseMaxNRows(hist, wig->value);
 else if (wig->value < hist->min)
  lowerMinNRows(hist, wig->value);
 if (hist->min != hist->max)
  insertIntoHistogram(hist, wig, row);
 else
  hist->values[row][0] += wig->finish - wig->start;
}

canonical_original_code:
static void updateHistogram(struct histogram_st *hist, struct wiggleIterator_st *wig, int row) {
 if (wig->value > hist->max)
  raiseMaxNRows(hist, wig->value);
 else if (wig->value < hist->min)
  lowerMinNRows(hist, wig->value);
 if (hist->min != hist->max)
  insertIntoHistogram(hist, wig, row);
 else
  hist->values[row][0] += wig->finish - wig->start;
}

variable_types: struct wiggleIterator_st { char *chrom; int start; int finish; double value; void *valuePtr; char done; int strand; void *data; void (*pop)(struct wiggleIterator_st *); void (*seek)(struct wiggleIterator_st *, char *, int, int); char overlaps; double default_value; struct wiggleIterator_st *append; } *


updateProfile

decompiled_code:
unsigned __int64 __fastcall updateProfile(__int64 a1, __int64 a2, int a3, char a4, double a5)
{
int v6; // eax
int v7; // er12
unsigned __int64 result; // rax
int v9; // ecx
int v10; // edx
unsigned __int64 v11; // rsi
int v12; // edx
double v14; // [rsp+8h] [rbp-30h]

if ( a4 && (v6 = *(_DWORD *)(a1 + 36), v6 <= 0) )
{
if ( v6 >= 0 )
{
fwrite("Cannot provide stranded profile on non-stranded regions\n", 1uLL, 0x38uLL, stderr);
exit(1);
}
v14 = (double)(a3 - 1);
v7 = (int)round(v14 - (double)*(int *)(a1 + 12) * a5);
result = (unsigned int)(int)round(v14 - (double)*(int *)(a1 + 8) * a5);
}
else
{
v7 = (int)round((double)*(int *)(a1 + 8) * a5);
result = (unsigned int)(int)round((double)*(int *)(a1 + 12) * a5);
}
v9 = 0;
if ( v7 >= 0 )
v9 = v7;
if ( v9 >= (int)result )
result = (unsigned int)(v9 + 1);
v10 = a3;
if ( (int)result <= a3 )
v10 = result;
if ( v10 > v9 )
{
result = a2 + 8LL * v9;
v11 = a2 + 8 * (v9 + (unsigned __int64)(unsigned int)(v10 + ~v9)) + 8;
v12 = v10 - v9;
do
{
*(double *)result = *(double *)(a1 + 16) / (double)v12 + *(double *)result;
result += 8LL;
}
while ( result != v11 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func6(__int64 a1, __int64 a2, int a3, char a4, double a5)
{
int v6; // eax
int v7; // er12
unsigned __int64 result; // rax
int v9; // ecx
int v10; // edx
unsigned __int64 v11; // rsi
int v12; // edx
double v14; // [rsp+8h] [rbp-30h]

if ( a4 && (v6 = *(_DWORD *)(a1 + 36), v6 <= 0) )
{
if ( v6 >= 0 )
{
fwrite("Cannot provide stranded profile on non-stranded regions\n", 1uLL, 0x38uLL, stderr);
exit(1);
}
v14 = (double)(a3 - 1);
v7 = (int)round(v14 - (double)*(int *)(a1 + 12) * a5);
result = (unsigned int)(int)round(v14 - (double)*(int *)(a1 + 8) * a5);
}
else
{
v7 = (int)round((double)*(int *)(a1 + 8) * a5);
result = (unsigned int)(int)round((double)*(int *)(a1 + 12) * a5);
}
v9 = 0;
if ( v7 >= 0 )
v9 = v7;
if ( v9 >= (int)result )
result = (unsigned int)(v9 + 1);
v10 = a3;
if ( (int)result <= a3 )
v10 = result;
if ( v10 > v9 )
{
result = a2 + 8LL * v9;
v11 = a2 + 8 * (v9 + (unsigned __int64)(unsigned int)(v10 + ~v9)) + 8;
v12 = v10 - v9;
do
{
*(double *)result = *(double *)(a1 + 16) / (double)v12 + *(double *)result;
result += 8LL;
}
while ( result != v11 );
}
return result;
}

original_code:
static void updateProfile(WiggleIterator * wig, double compression, double * profile, int profile_width, char stranded) {
 int start, finish, pos;
 if (__builtin_isnan (wig->value))
  return;
 if (!stranded || wig->strand > 0) {
  start = (int) round(wig->start * compression);
  finish = (int) round(wig->finish * compression);
 } else if (wig->strand < 0) {
  start = (int) round(profile_width - 1 - (wig->finish * compression));
  finish = (int) round(profile_width - 1 - (wig->start * compression));
 } else {
  fprintf(stderr, "Cannot provide stranded profile on non-stranded regions\n");
  exit(1);
 }
 if (start < 0)
  start = 0;
 if (finish <= start)
  finish = start + 1;
 if (finish > profile_width)
  finish = profile_width;
 for (pos = start; pos < finish; pos++)
  profile[pos] += wig->value / (finish - start);
}

canonical_original_code:
static void updateProfile(struct wiggleIterator_st *wig, double compression, double *profile, int profile_width, char stranded) {
 int start; int finish; int pos;
 if (__builtin_isnan (wig->value))
  return;
 if (!stranded || wig->strand > 0) {
  start = (int) round(wig->start * compression);
  finish = (int) round(wig->finish * compression);
 } else if (wig->strand < 0) {
  start = (int) round(profile_width - 1 - (wig->finish * compression));
  finish = (int) round(profile_width - 1 - (wig->start * compression));
 } else {
  fprintf(stderr, "Cannot provide stranded profile on non-stranded regions\n");
  exit(1);
 }
 if (start < 0)
  start = 0;
 if (finish <= start)
  finish = start + 1;
 if (finish > profile_width)
  finish = profile_width;
 for (pos = start; pos < finish; pos++)
  profile[pos] += wig->value / (finish - start);
}

variable_types: compression double

variable_types:
profile
double *

variable_types: stranded char

variable_types: start int

variable_types: finish int

variable_types: pos int

user_defined_types: struct wiggleIterator_st { char *chrom; int start; int finish; double value; void *valuePtr; char done; int strand; void *data; void (*pop)(struct wiggleIterator_st *); void (*seek)(struct wiggleIterator_st *, char *, int, int); char overlaps; double default_value; struct wiggleIterator_st *append; }


regionProfile

decompiled_code:
void __fastcall regionProfile(__int64 a1, __int64 a2, int a3, int a4, char a5)
{
double v6; // r14
_QWORD *v7; // rax
__int64 v8; // rdx

v6 = (double)a3 / (double)a4;
if ( a3 > 0 )
{
v7 = (_QWORD *)a2;
v8 = a2 + 8LL * (unsigned int)(a3 - 1) + 8;
do
*v7++ = 0LL;
while ( v7 != (_QWORD *)v8 );
}
while ( !*(_BYTE *)(a1 + 32) )
{
updateProfile(a1, a2, a3, a5, v6);
pop(a1);
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, __int64 a2, int a3, int a4, char a5)
{
double v6; // r14
_QWORD *v7; // rax
__int64 v8; // rdx

v6 = (double)a3 / (double)a4;
if ( a3 > 0 )
{
v7 = (_QWORD *)a2;
v8 = a2 + 8LL * (unsigned int)(a3 - 1) + 8;
do
*v7++ = 0LL;
while ( v7 != (_QWORD *)v8 );
}
while ( !*(_BYTE *)(a1 + 32) )
{
func6(a1, a2, a3, a5, v6);
pop(a1);
}
}

original_code:
void regionProfile(WiggleIterator * wig, double * profile, int profile_width, int region_width, char stranded) {
 double compression = profile_width / (double) region_width;
 int pos;
 for (pos = 0; pos < profile_width; pos++)
  profile[pos] = 0;
 for (; !wig->done; pop(wig))
  updateProfile(wig, compression, profile, profile_width, stranded);
}

canonical_original_code:
void regionProfile(struct wiggleIterator_st *wig, double *profile, int profile_width, int region_width, char stranded) {
 double compression = profile_width / (double) region_width;
 int pos;
 for (pos = 0; pos < profile_width; pos++)
  profile[pos] = 0;
 for (; !wig->done; pop(wig))
  updateProfile(wig, compression, profile, profile_width, stranded);
}


addProfile

decompiled_code:
void __fastcall addProfile(__int64 a1, __int64 a2, int a3)
{
__int64 i; // rax

if ( a3 > 0 )
{
for ( i = 0LL; i != a3; ++i )
*(double *)(a1 + 8 * i) = *(double *)(a1 + 8 * i) + *(double *)(a2 + 8 * i);
}
}

original_code:
void addProfile(double * dest, double * source, int width) {
 int i;
 for (i=0; i<width; i++)
  dest[i] += source[i];
}

canonical_original_code:
void addProfile(double *dest, double *source, int width) {
 int i;
 for (i=0; i<width; i++)
  dest[i] += source[i];
}

variable_types:
dest
double *

variable_types:
source
double *

variable_types: width int


decompiled_code:
void __fastcall normalize_histogram(unsigned int *a1)
{
__int64 v1; // rcx
signed int i; // esi
double *v3; // rax
double *v4; // rdx
double v5; // xmm1_8
__int64 v6; // rax
double *v7; // rdx

v1 = 0LL;
for ( i = 0; (int)a1[1] > i; v1 += 8LL )
{
if ( (int)*a1 > 0 )
{
v3 = *(double **)(*((_QWORD *)a1 + 1) + v1);
v4 = &v3[*a1];
v5 = 0.0;
do
v5 = v5 + *v3++;
while ( v3 != v4 );
v6 = 0LL;
do
{
v7 = (double *)(*(_QWORD *)(*((_QWORD *)a1 + 1) + v1) + 8 * v6);
*v7 = *v7 / v5;
++v6;
}
while ( (int)*a1 > (int)v6 );
}
++i;
}
}

original_code:
void normalize_histogram(Histogram * hist) {
 double sum;
 int column, row;
 for (row = 0; row < hist->count; row++) {
  sum = 0;
  for (column = 0; column < hist->width; column++)
   sum += hist->values[row][column];
  for (column = 0; column < hist->width; column++)
   hist->values[row][column] /= sum;
 }
}

canonical_original_code:
void normalize_histogram(struct histogram_st *hist) {
 double sum;
 int column; int row;
 for (row = 0; row < hist->count; row++) {
  sum = 0;
  for (column = 0; column < hist->width; column++)
   sum += hist->values[row][column];
  for (column = 0; column < hist->width; column++)
   hist->values[row][column] /= sum;
 }
}

variable_types: sum double


countWords

decompiled_code:
__int64 __fastcall countWords(char *a1)
{
char v1; // al
unsigned int i; // edx

v1 = *a1;
for ( i = v1 != 32 && v1 != 9; *a1; v1 = *a1 )
{
if ( v1 == 32 || v1 == 9 )
++i;
++a1;
}
return i;
}

original_code:
static int countWords(char * line) {
 int count = 0;
 char * ptr;
 if (line[0] != ' ' && line[0] != '\t')
  count++;
 for (ptr = line; *ptr; ptr++)
  if (*ptr == ' ' || *ptr == '\t')
   count++;
 return count;
}

canonical_original_code:
static int countWords(char *line) {
 int count = 0;
 char *ptr;
 if (line[0] != ' ' && line[0] != '\t')
  count++;
 for (ptr = line; *ptr; ptr++)
  if (*ptr == ' ' || *ptr == '\t')
   count++;
 return count;
}

variable_types:
line
char *

variable_types: count int

variable_types:
ptr
char *


WiggleReaderReadVariableStepLine

decompiled_code:
__int64 __fastcall WiggleReaderReadVariableStepLine(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax

result = _isoc99_sscanf(a2, "%i\t%lf", a1 + 8, a1 + 16);
*(_DWORD *)(a1 + 12) = *(_DWORD *)(a1 + 8) + a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax

result = _isoc99_sscanf(a2, "%i\t%lf", a1 + 8, a1 + 16);
*(_DWORD *)(a1 + 12) = *(_DWORD *)(a1 + 8) + a3;
return result;
}

original_code:
static void WiggleReaderReadVariableStepLine(WiggleIterator * wi, char * line, int span) {
 sscanf(line, "%i\t%lf", &(wi->start), &(wi->value));
 wi->finish = wi->start + span;
}

canonical_original_code:
static void WiggleReaderReadVariableStepLine(struct wiggleIterator_st *wi, char *line, int span) {
 sscanf(line, "%i\t%lf", &(wi->start), &(wi->value));
 wi->finish = wi->start + span;
}

variable_types: span int


WiggleReaderReadFixedStepLine

decompiled_code:
__int64 __fastcall WiggleReaderReadFixedStepLine(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 result; // rax
int v7; // ebx

result = _isoc99_sscanf(a2, "%lf", a1 + 16);
v7 = *(_DWORD *)(a1 + 8) + a3;
*(_DWORD *)(a1 + 8) = v7;
*(_DWORD *)(a1 + 12) = a4 + v7;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 result; // rax
int v7; // ebx

result = _isoc99_sscanf(a2, "%lf", a1 + 16);
v7 = *(_DWORD *)(a1 + 8) + a3;
*(_DWORD *)(a1 + 8) = v7;
*(_DWORD *)(a1 + 12) = a4 + v7;
return result;
}

original_code:
static void WiggleReaderReadFixedStepLine(WiggleIterator * wi, char * line, int step, int span) {
 sscanf(line, "%lf", &(wi->value));
 wi->start += step;
 wi->finish = wi->start + span;
}

canonical_original_code:
static void WiggleReaderReadFixedStepLine(struct wiggleIterator_st *wi, char *line, int step, int span) {
 sscanf(line, "%lf", &(wi->value));
 wi->start += step;
 wi->finish = wi->start + span;
}

variable_types: step int


WiggleReaderReadBedGraphLine

decompiled_code:
void __fastcall WiggleReaderReadBedGraphLine(__int64 a1, __int64 a2)
{
void *v2; // rbp

v2 = calloc(0x1F4uLL, 1uLL);
_isoc99_sscanf(a2, "%s\t%i\t%i\t%lf", v2, a1 + 8, a1 + 12, a1 + 16);
++*(_DWORD *)(a1 + 8);
++*(_DWORD *)(a1 + 12);
if ( !strcmp((const char *)v2, *(const char **)a1) )
free(v2);
else
*(_QWORD *)a1 = v2;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2)
{
void *v2; // rbp

v2 = calloc(0x1F4uLL, 1uLL);
_isoc99_sscanf(a2, "%s\t%i\t%i\t%lf", v2, a1 + 8, a1 + 12, a1 + 16);
++*(_DWORD *)(a1 + 8);
++*(_DWORD *)(a1 + 12);
if ( !strcmp((const char *)v2, *(const char **)a1) )
free(v2);
else
*(_QWORD *)a1 = v2;
}

original_code:
static void WiggleReaderReadBedGraphLine(WiggleIterator * wi, char * line) {
 char * buffer = calloc(500, sizeof(char));
 sscanf(line, "%s\t%i\t%i\t%lf", buffer, &(wi->start), &(wi->finish), &(wi->value));
 wi->start++;
 wi->finish++;
 if (strcmp(buffer, wi->chrom))
  wi->chrom = buffer;
 else
  free(buffer);
}

canonical_original_code:
static void WiggleReaderReadBedGraphLine(struct wiggleIterator_st *wi, char *line) {
 char *buffer = calloc(500, sizeof(char));
 sscanf(line, "%s\t%i\t%i\t%lf", buffer, &(wi->start), &(wi->finish), &(wi->value));
 wi->start++;
 wi->finish++;
 if (strcmp(buffer, wi->chrom))
  wi->chrom = buffer;
 else
  free(buffer);
}

variable_types:
buffer
char *


destroyWiggleIterator

decompiled_code:
void __fastcall destroyWiggleIterator(void *ptr)
{
free(*((void **)ptr + 5));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*((void **)ptr + 5));
free(ptr);
}

original_code:
void destroyWiggleIterator(WiggleIterator * wi) {
 free(wi->data);
 free(wi);
}

canonical_original_code:
void destroyWiggleIterator(struct wiggleIterator_st *wi) {
 free(wi->data);
 free(wi);
}

variable_types: struct wiggleIterator_st { char *chrom; int start; int finish; double value; void *valuePtr; char done; int strand; void *data; void (*pop)(struct wiggleIterator_st *); void (*seek)(struct wiggleIterator_st *, char *, int, int); char overlaps; double default_value; struct wiggleIterator_st *append; } *


pop

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

if ( !*(_BYTE *)(a1 + 32) )
result = (*(__int64 (**)(void))(a1 + 48))();
return result;
}

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

if ( !*(_BYTE *)(a1 + 32) )
result = (*(__int64 (**)(void))(a1 + 48))();
return result;
}

original_code:
void pop(WiggleIterator * wi) {
 if (!wi->done)
  wi->pop(wi);
}

canonical_original_code:
void pop(struct wiggleIterator_st *wi) {
 if (!wi->done)
  wi->pop(wi);
}


runWiggleIterator

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

for ( ; !*(_BYTE *)(a1 + 32); result = (*(__int64 (__fastcall **)(__int64))(a1 + 48))(a1) )
;
return result;
}

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

for ( ; !*(_BYTE *)(a1 + 32); result = (*(__int64 (__fastcall **)(__int64))(a1 + 48))(a1) )
;
return result;
}

original_code:
void runWiggleIterator(WiggleIterator * wi) {
 while (!wi->done)
  wi->pop(wi);
}

canonical_original_code:
void runWiggleIterator(struct wiggleIterator_st *wi) {
 while (!wi->done)
  wi->pop(wi);
}


seek

original_code:
void seek(WiggleIterator * wi, const char * chrom, int start, int finish) {
 wi->done = 0;
 (*(wi->seek))(wi, chrom, start, finish);
}

canonical_original_code:
void seek(struct wiggleIterator_st *wi, char *chrom, int start, int finish) {
 wi->done = 0;
 (*(wi->seek))(wi, chrom, start, finish);
}

variable_types:
chrom
char *


goToNextBlock

decompiled_code:
__int64 __fastcall goToNextBlock(__int64 a1)
{
void *v1; // r12
pthread_mutex_t *v2; // rbp
int v3; // eax
unsigned int v4; // edx
int v5; // eax

v1 = *(void **)(a1 + 24);
++i_0;
v2 = (pthread_mutex_t *)(a1 + 56);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v3 = *(_DWORD *)(a1 + 40);
v4 = 1;
if ( v3 >= 0 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 40) = v5;
if ( !v5 && !*(_BYTE *)(a1 + 144) )
pthread_cond_wait((pthread_cond_t *)(a1 + 96), v2);
pthread_cond_signal((pthread_cond_t *)(a1 + 96));
pthread_mutex_unlock(v2);
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 240008LL);
free(v1);
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
void *v1; // r12
pthread_mutex_t *v2; // rbp
int v3; // eax
unsigned int v4; // edx
int v5; // eax

v1 = *(void **)(a1 + 24);
++i_0;
v2 = (pthread_mutex_t *)(a1 + 56);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v3 = *(_DWORD *)(a1 + 40);
v4 = 1;
if ( v3 >= 0 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 40) = v5;
if ( !v5 && !*(_BYTE *)(a1 + 144) )
pthread_cond_wait((pthread_cond_t *)(a1 + 96), v2);
pthread_cond_signal((pthread_cond_t *)(a1 + 96));
pthread_mutex_unlock(v2);
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 240008LL);
free(v1);
v4 = 0;
}
return v4;
}

original_code:
static void goToNextBlock(BufferedReaderData * data) {
 BlockData * prevBlockData = data->blockData;
 data->blockData = data->blockData->next;
 data->readIndex = 0;
 destroyBlockData(prevBlockData);
}

canonical_original_code:
static void goToNextBlock(struct bufferedReaderData_st *data) {
 struct blockData_st *prevBlockData = data->blockData;
 data->blockData = data->blockData->next;
 data->readIndex = 0;
 destroyBlockData(prevBlockData);
}

variable_types: struct bufferedReaderData_st { unsigned long downloaderThreadID; struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; } *blockData; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } count_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } count_cond; int blockCount; int readIndex; void *readerData; char killed; } *


toStdout

decompiled_code:
__int64 __fastcall toStdout(__int64 a1, char a2, char a3)
{
__int64 v3; // rax

v3 = TeeWiggleIterator(a1, (__int64)stdout, a2, a3);
return runWiggleIterator(v3);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, char a2, char a3)
{
__int64 v3; // rax

v3 = func7(a1, (__int64)stdout, a2, a3);
return runWiggleIterator(v3);
}

original_code:
void toStdout(WiggleIterator * wi, char bedGraph, char holdFire) {
 runWiggleIterator(TeeWiggleIterator(wi, stdout, bedGraph, holdFire));
}

canonical_original_code:
void toStdout(struct wiggleIterator_st *wi, char bedGraph, char holdFire) {
 runWiggleIterator(TeeWiggleIterator(wi, stdout, bedGraph, holdFire));
}

variable_types: bedGraph char

variable_types: holdFire char


original_code:
static inline int ceillog2(int a)
{
 int oa;
 int i;
 int b;
 int cons;
 oa = a;
 b = (sizeof(int) * 8) / 2;
 i = 0;
 while (b) {
  i = (i << 1);
  cons = ((int) 1) << b;
  if (a >= cons) {
   a /= cons;
   i = i | 1;
  } else
   a &= cons - 1;
  b /= 2;
 }
 if ((((int) 1 << i)) == oa)
  return i;
 else
  return i + 1;
}

variable_types: a int

variable_types: oa int

variable_types: i int

variable_types: b int

variable_types: cons int


compareValueSetPairs

decompiled_code:
__int64 __fastcall compareValueSetPairs(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = *(double *)a1 > *(double *)a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *(double *)a2 <= *(double *)a1 )
result = *(double *)a1 > *(double *)a2;
return result;
}

original_code:
static int compareValueSetPairs(const void * A, const void * B) {
 ValueSetPair * vspA = (ValueSetPair *) A;
 ValueSetPair * vspB = (ValueSetPair *) B;
 if (vspA->value < vspB->value)
  return -1;
 if (vspA->value > vspB->value)
  return 1;
 return 0;
}

canonical_original_code:
static int compareValueSetPairs(void *A, void *B) {
 struct valueSetPair_st *vspA = (ValueSetPair *) A;
 struct valueSetPair_st *vspB = (ValueSetPair *) B;
 if (vspA->value < vspB->value)
  return -1;
 if (vspA->value > vspB->value)
  return 1;
 return 0;
}

variable_types:
A
void *

variable_types:
B
void *

variable_types: struct valueSetPair_st { double value; char set; } *


goToNextBlock

decompiled_code:
__int64 __fastcall goToNextBlock(__int64 a1)
{
void **v1; // r12
pthread_mutex_t *v2; // rbp
int v3; // eax
unsigned int v4; // edx
int v5; // eax

v1 = *(void ***)(a1 + 24);
++i_0;
v2 = (pthread_mutex_t *)(a1 + 56);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v3 = *(_DWORD *)(a1 + 40);
v4 = 1;
if ( v3 >= 0 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 40) = v5;
if ( !v5 && !*(_BYTE *)(a1 + 144) )
pthread_cond_wait((pthread_cond_t *)(a1 + 96), v2);
pthread_cond_signal((pthread_cond_t *)(a1 + 96));
pthread_mutex_unlock(v2);
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 160024LL);
free(v1[20000]);
free(v1);
v4 = 0;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
void **v1; // r12
pthread_mutex_t *v2; // rbp
int v3; // eax
unsigned int v4; // edx
int v5; // eax

v1 = *(void ***)(a1 + 24);
++i_0;
v2 = (pthread_mutex_t *)(a1 + 56);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v3 = *(_DWORD *)(a1 + 40);
v4 = 1;
if ( v3 >= 0 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 40) = v5;
if ( !v5 && !*(_BYTE *)(a1 + 144) )
pthread_cond_wait((pthread_cond_t *)(a1 + 96), v2);
pthread_cond_signal((pthread_cond_t *)(a1 + 96));
pthread_mutex_unlock(v2);
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 24) + 160024LL);
free(v1[20000]);
free(v1);
v4 = 0;
}
return v4;
}

variable_types: struct blockData_st { char **chrom; int *start; int *finish; double *value; int count; struct blockData_st *next; } *


timespecAdd

repo: ErikOnBike/light-play

decompiled_code:
__int64 __fastcall timespecAdd(_QWORD *a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 result; // rax

v2 = *a1 + *a2;
*a1 = v2;
result = a1[1] + a2[1];
a1[1] = result;
if ( result > 999999999 )
{
*a1 = v2 + 1;
result -= 1000000000LL;
a1[1] = result;
}
return result;
}

original_code:
void timespecAdd(struct timespec *time, const struct timespec *delta) {
 time->tv_sec += delta->tv_sec;
 time->tv_nsec += delta->tv_nsec;
 if(time->tv_nsec >= 1000000000L) {
  time->tv_sec++;
  time->tv_nsec -= 1000000000L;
 }
}

canonical_original_code:
void timespecAdd(struct timespec *time, struct timespec *delta) {
 time->tv_sec += delta->tv_sec;
 time->tv_nsec += delta->tv_nsec;
 if(time->tv_nsec >= 1000000000L) {
  time->tv_sec++;
  time->tv_nsec -= 1000000000L;
 }
}

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

variable_types:
delta
struct timespec *


getRandomNumber

decompiled_code:
__int64 __fastcall getRandomNumber(int *a1)
{
unsigned int v1; // ebx
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF

v1 = (unsigned __int8)isSeeded_0;
if ( !isSeeded_0 )
{
if ( clock_gettime(1, &v3) )
{
logWrite(1LL, "utils.c", "Cannot read the internal clock for using it in generating (pseudo)random numbers");
return v1;
}
if ( !(v3.tv_nsec + v3.tv_sec) )
{
logWrite(1LL, "utils.c", "Clock returned value 0 which cannot be used for generating (pseudo)random numbers");
return v1;
}
srand(LODWORD(v3.tv_nsec) + LODWORD(v3.tv_sec));
isSeeded_0 = 1;
}
*a1 = rand();
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1)
{
unsigned int v1; // ebx
struct timespec v3; // [rsp+0h] [rbp-28h] BYREF

v1 = (unsigned __int8)isSeeded_0;
if ( !isSeeded_0 )
{
if ( clock_gettime(1, &v3) )
{
logWrite(1LL, "utils.c", "Cannot read the internal clock for using it in generating (pseudo)random numbers");
return v1;
}
if ( !(v3.tv_nsec + v3.tv_sec) )
{
logWrite(1LL, "utils.c", "Clock returned value 0 which cannot be used for generating (pseudo)random numbers");
return v1;
}
srand(LODWORD(v3.tv_nsec) + LODWORD(v3.tv_sec));
isSeeded_0 = 1;
}
*a1 = rand();
return 1;
}

original_code:
_Bool getRandomNumber(uint32_t *randomValue) {
 static _Bool isSeeded = 0;
 struct timespec timeSpec;
 if(!isSeeded) {
  if(clock_gettime(1, &timeSpec) != 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read the internal clock for using it in generating (pseudo)random numbers");
   return 0;
  }
  if(timeSpec.tv_sec + timeSpec.tv_nsec == 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Clock returned value 0 which cannot be used for generating (pseudo)random numbers");
   return 0;
  }
  srand((unsigned int)(timeSpec.tv_sec + timeSpec.tv_nsec));
  isSeeded = 1;
 }
 if(sizeof(int) == sizeof(uint32_t)) {
  *randomValue = rand();
 } else if(2 * sizeof(int) == sizeof(uint32_t)) {
  *randomValue = ((uint32_t)rand()) << 16 && rand();
 } else {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Size of 'int' is unexpected and therefore (pseudo)random numbers can be less strong");
  *randomValue = (uint32_t)rand();
 }
 return 1;
}

canonical_original_code:
_Bool getRandomNumber(unsigned int *randomValue) {
 static _Bool isSeeded = 0;
 struct timespec timeSpec;
 if(!isSeeded) {
  if(clock_gettime(1, &timeSpec) != 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot read the internal clock for using it in generating (pseudo)random numbers");
   return 0;
  }
  if(timeSpec.tv_sec + timeSpec.tv_nsec == 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Clock returned value 0 which cannot be used for generating (pseudo)random numbers");
   return 0;
  }
  srand((unsigned int)(timeSpec.tv_sec + timeSpec.tv_nsec));
  isSeeded = 1;
 }
 if(sizeof(int) == sizeof(unsigned int)) {
  *randomValue = rand();
 } else if(2 * sizeof(int) == sizeof(unsigned int)) {
  *randomValue = ((unsigned int)rand()) << 16 && rand();
 } else {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Size of 'int' is unexpected and therefore (pseudo)random numbers can be less strong");
  *randomValue = (unsigned int)rand();
 }
 return 1;
}

variable_types:
randomValue
unsigned int *

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


decompiled_code:
void *__fastcall MD5_Update(unsigned int *a1, _QWORD *a2, size_t a3)
{
_DWORD *v3; // r12
size_t v4; // rbp
unsigned int v5; // eax
unsigned int v6; // edx
unsigned int v7; // eax
__int64 v8; // rcx
unsigned __int64 v9; // rdx
_QWORD *v10; // rax
unsigned __int64 v12; // r8
char *v13; // rax
char *v14; // r9
unsigned int v15; // eax
unsigned int v16; // eax
unsigned int v17; // esi

v3 = a2;
v4 = a3;
v5 = *a1;
v6 = (*a1 + a3) & 0x1FFFFFFF;
*a1 = v6;
if ( v6 < v5 )
++a1[1];
a1[1] += v4 >> 29;
v7 = v5 & 0x3F;
v8 = v7;
if ( v7 )
{
v9 = 64LL - v7;
if ( v4 < v9 )
return memcpy((char *)a1 + v7 + 24, a2, v4);
v10 = (_QWORD *)((char *)a1 + v7 + 24);
if ( (unsigned int)v9 >= 8 )
{
*v10 = *a2;
*(_QWORD *)((char *)v10 + (unsigned int)v9 - 8) = *(_QWORD *)((char *)a2 + (unsigned int)v9 - 8);
v12 = (unsigned __int64)(v10 + 1) & 0xFFFFFFFFFFFFFFF8LL;
v13 = (char *)v10 - v12;
v14 = (char *)((char *)a2 - v13);
v15 = (v9 + (_DWORD)v13) & 0xFFFFFFF8;
if ( v15 >= 8 )
{
v16 = v15 & 0xFFFFFFF8;
v17 = 0;
do
{
*(_QWORD *)(v12 + v17) = *(_QWORD *)&v14[v17];
v17 += 8;
}
while ( v17 < v16 );
}
}
else if ( (v9 & 4) != 0 )
{
*(_DWORD *)v10 = *(_DWORD *)a2;
*(_DWORD *)((char *)v10 + (unsigned int)v9 - 4) = *(_DWORD *)((char *)a2 + (unsigned int)v9 - 4);
}
else if ( (_DWORD)v9 )
{
*(_BYTE *)v10 = *(_BYTE *)a2;
if ( (v9 & 2) != 0 )
*(_WORD *)((char *)v10 + (unsigned int)v9 - 2) = *(_WORD *)((char *)a2 + (unsigned int)v9 - 2);
}
v3 = (_DWORD *)((char *)v3 + v9);
v4 = v8 + v4 - 64;
body(a1, a1 + 6, 64LL);
}
if ( v4 > 0x3F )
{
v3 = body(a1, v3, v4 & 0xFFFFFFFFFFFFFFC0LL);
v4 &= 0x3Fu;
}
return memcpy(a1 + 6, v3, v4);
}

canonical_decompiled_code:
void *__fastcall func1(unsigned int *a1, _QWORD *a2, size_t a3)
{
_DWORD *v3; // r12
size_t v4; // rbp
unsigned int v5; // eax
unsigned int v6; // edx
unsigned int v7; // eax
__int64 v8; // rcx
unsigned __int64 v9; // rdx
_QWORD *v10; // rax
unsigned __int64 v12; // r8
char *v13; // rax
char *v14; // r9
unsigned int v15; // eax
unsigned int v16; // eax
unsigned int v17; // esi

v3 = a2;
v4 = a3;
v5 = *a1;
v6 = (*a1 + a3) & 0x1FFFFFFF;
*a1 = v6;
if ( v6 < v5 )
++a1[1];
a1[1] += v4 >> 29;
v7 = v5 & 0x3F;
v8 = v7;
if ( v7 )
{
v9 = 64LL - v7;
if ( v4 < v9 )
return memcpy((char *)a1 + v7 + 24, a2, v4);
v10 = (_QWORD *)((char *)a1 + v7 + 24);
if ( (unsigned int)v9 >= 8 )
{
*v10 = *a2;
*(_QWORD *)((char *)v10 + (unsigned int)v9 - 8) = *(_QWORD *)((char *)a2 + (unsigned int)v9 - 8);
v12 = (unsigned __int64)(v10 + 1) & 0xFFFFFFFFFFFFFFF8LL;
v13 = (char *)v10 - v12;
v14 = (char *)((char *)a2 - v13);
v15 = (v9 + (_DWORD)v13) & 0xFFFFFFF8;
if ( v15 >= 8 )
{
v16 = v15 & 0xFFFFFFF8;
v17 = 0;
do
{
*(_QWORD *)(v12 + v17) = *(_QWORD *)&v14[v17];
v17 += 8;
}
while ( v17 < v16 );
}
}
else if ( (v9 & 4) != 0 )
{
*(_DWORD *)v10 = *(_DWORD *)a2;
*(_DWORD *)((char *)v10 + (unsigned int)v9 - 4) = *(_DWORD *)((char *)a2 + (unsigned int)v9 - 4);
}
else if ( (_DWORD)v9 )
{
*(_BYTE *)v10 = *(_BYTE *)a2;
if ( (v9 & 2) != 0 )
*(_WORD *)((char *)v10 + (unsigned int)v9 - 2) = *(_WORD *)((char *)a2 + (unsigned int)v9 - 2);
}
v3 = (_DWORD *)((char *)v3 + v9);
v4 = v8 + v4 - 64;
body(a1, a1 + 6, 64LL);
}
if ( v4 > 0x3F )
{
v3 = body(a1, v3, v4 & 0xFFFFFFFFFFFFFFC0LL);
v4 &= 0x3Fu;
}
return memcpy(a1 + 6, v3, v4);
}

original_code:
void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
{
 MD5_u32plus saved_lo;
 unsigned long used, available;
 saved_lo = ctx->lo;
 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  ctx->hi++;
 ctx->hi += size >> 29;
 used = saved_lo & 0x3f;
 if (used) {
  available = 64 - used;
  if (size < available) {
   memcpy(&ctx->buffer[used], data, size);
   return;
  }
  memcpy(&ctx->buffer[used], data, available);
  data = (const unsigned char *)data + available;
  size -= available;
  body(ctx, ctx->buffer, 64);
 }
 if (size >= 64) {
  data = body(ctx, data, size & ~(unsigned long)0x3f);
  size &= 0x3f;
 }
 memcpy(ctx->buffer, data, size);
}

canonical_original_code:
void MD5_Update(struct MD5_CTX *ctx, void *data, unsigned long size)
{
 unsigned int saved_lo;
 unsigned long used; unsigned long available;
 saved_lo = ctx->lo;
 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  ctx->hi++;
 ctx->hi += size >> 29;
 used = saved_lo & 0x3f;
 if (used) {
  available = 64 - used;
  if (size < available) {
   memcpy(&ctx->buffer[used], data, size);
   return;
  }
  memcpy(&ctx->buffer[used], data, available);
  data = (unsigned char *)data + available;
  size -= available;
  body(ctx, ctx->buffer, 64);
 }
 if (size >= 64) {
  data = body(ctx, data, size & ~(unsigned long)0x3f);
  size &= 0x3f;
 }
 memcpy(ctx->buffer, data, size);
}

variable_types: struct MD5_CTX { unsigned int lo; unsigned int a; unsigned char buffer[64]; unsigned int block[16]; } *

variable_types:
data
void *

variable_types:
size
unsigned long

variable_types:
used
unsigned long

variable_types:
available
unsigned long


original_code:
void MD5_Final(unsigned char *result, MD5_CTX *ctx)
{
 unsigned long used, available;
 used = ctx->lo & 0x3f;
 ctx->buffer[used++] = 0x80;
 available = 64 - used;
 if (available < 8) {
  memset(&ctx->buffer[used], 0, available);
  body(ctx, ctx->buffer, 64);
  used = 0;
  available = 64;
 }
 memset(&ctx->buffer[used], 0, available - 8);
 ctx->lo <<= 3;
 ctx->buffer[56] = ctx->lo;
 ctx->buffer[57] = ctx->lo >> 8;
 ctx->buffer[58] = ctx->lo >> 16;
 ctx->buffer[59] = ctx->lo >> 24;
 ctx->buffer[60] = ctx->hi;
 ctx->buffer[61] = ctx->hi >> 8;
 ctx->buffer[62] = ctx->hi >> 16;
 ctx->buffer[63] = ctx->hi >> 24;
 body(ctx, ctx->buffer, 64);
 result[0] = ctx->a;
 result[1] = ctx->a >> 8;
 result[2] = ctx->a >> 16;
 result[3] = ctx->a >> 24;
 result[4] = ctx->b;
 result[5] = ctx->b >> 8;
 result[6] = ctx->b >> 16;
 result[7] = ctx->b >> 24;
 result[8] = ctx->c;
 result[9] = ctx->c >> 8;
 result[10] = ctx->c >> 16;
 result[11] = ctx->c >> 24;
 result[12] = ctx->d;
 result[13] = ctx->d >> 8;
 result[14] = ctx->d >> 16;
 result[15] = ctx->d >> 24;
 memset(ctx, 0, sizeof(*ctx));
}

canonical_original_code:
void MD5_Final(unsigned char *result, struct MD5_CTX *ctx)
{
 unsigned long used; unsigned long available;
 used = ctx->lo & 0x3f;
 ctx->buffer[used++] = 0x80;
 available = 64 - used;
 if (available < 8) {
  memset(&ctx->buffer[used], 0, available);
  body(ctx, ctx->buffer, 64);
  used = 0;
  available = 64;
 }
 memset(&ctx->buffer[used], 0, available - 8);
 ctx->lo <<= 3;
 ctx->buffer[56] = ctx->lo;
 ctx->buffer[57] = ctx->lo >> 8;
 ctx->buffer[58] = ctx->lo >> 16;
 ctx->buffer[59] = ctx->lo >> 24;
 ctx->buffer[60] = ctx->hi;
 ctx->buffer[61] = ctx->hi >> 8;
 ctx->buffer[62] = ctx->hi >> 16;
 ctx->buffer[63] = ctx->hi >> 24;
 body(ctx, ctx->buffer, 64);
 result[0] = ctx->a;
 result[1] = ctx->a >> 8;
 result[2] = ctx->a >> 16;
 result[3] = ctx->a >> 24;
 result[4] = ctx->b;
 result[5] = ctx->b >> 8;
 result[6] = ctx->b >> 16;
 result[7] = ctx->b >> 24;
 result[8] = ctx->c;
 result[9] = ctx->c >> 8;
 result[10] = ctx->c >> 16;
 result[11] = ctx->c >> 24;
 result[12] = ctx->d;
 result[13] = ctx->d >> 8;
 result[14] = ctx->d >> 16;
 result[15] = ctx->d >> 24;
 memset(ctx, 0, sizeof(*ctx));
}

variable_types:
result
unsigned char *

user_defined_types: struct MD5_CTX { unsigned int lo; unsigned int a; unsigned char buffer[64]; unsigned int block[16]; }


networkGetAddressInfo

decompiled_code:
__int64 __fastcall networkGetAddressInfo(char *name, char *service, int a3, struct addrinfo **a4)
{
char *v4; // rbx
int v5; // edx
__int64 result; // rax
struct addrinfo v7; // [rsp+0h] [rbp-48h] BYREF

v4 = name;
*(_QWORD *)&v7.ai_flags = 0LL;
*(_QWORD *)&v7.ai_socktype = 0LL;
*(_QWORD *)&v7.ai_addrlen = 0LL;
v7.ai_addr = 0LL;
v7.ai_canonname = 0LL;
v7.ai_next = 0LL;
if ( a3 == 1 )
{
*(_QWORD *)&v7.ai_socktype = 0x1100000002LL;
v7.ai_flags = 1;
}
else
{
if ( a3 )
{
logWrite(1LL, "network.c", "Unsupported connection type. Unclear how to setup a connection. Failing.");
return 0LL;
}
*(_QWORD *)&v7.ai_socktype = 0x600000001LL;
}
v5 = getaddrinfo(name, service, &v7, a4);
result = 1LL;
if ( v5 )
{
if ( !name )
v4 = "<any>";
logWrite(1LL, "network.c", "Cannot get address information for host \"%s\" with port \"%s\".", v4, service);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *name, char *service, int a3, struct addrinfo **a4)
{
char *v4; // rbx
int v5; // edx
__int64 result; // rax
struct addrinfo v7; // [rsp+0h] [rbp-48h] BYREF

v4 = name;
*(_QWORD *)&v7.ai_flags = 0LL;
*(_QWORD *)&v7.ai_socktype = 0LL;
*(_QWORD *)&v7.ai_addrlen = 0LL;
v7.ai_addr = 0LL;
v7.ai_canonname = 0LL;
v7.ai_next = 0LL;
if ( a3 == 1 )
{
*(_QWORD *)&v7.ai_socktype = 0x1100000002LL;
v7.ai_flags = 1;
}
else
{
if ( a3 )
{
logWrite(1LL, "network.c", "Unsupported connection type. Unclear how to setup a connection. Failing.");
return 0LL;
}
*(_QWORD *)&v7.ai_socktype = 0x600000001LL;
}
v5 = getaddrinfo(name, service, &v7, a4);
result = 1LL;
if ( v5 )
{
if ( !name )
v4 = "<any>";
logWrite(1LL, "network.c", "Cannot get address information for host \"%s\" with port \"%s\".", v4, service);
result = 0LL;
}
return result;
}

original_code:
_Bool networkGetAddressInfo(const char *hostName, const char *portName, NetworkConnectionType connectionType, struct addrinfo **addressInfo) {
 struct addrinfo hints;
 memset(&hints, 0, sizeof(struct addrinfo));
 if(connectionType == UDP_CONNECTION) {
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = IPPROTO_UDP;
  hints.ai_flags = 0x0001;
 } else if(connectionType == TCP_CONNECTION) {
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = 0;
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unsupported connection type. Unclear how to setup a connection. Failing.");
  return 0;
 }
 hints.ai_family = 0;
 hints.ai_canonname = ((void *)0);
 hints.ai_addr = ((void *)0);
 hints.ai_next = ((void *)0);
 if(getaddrinfo(hostName, portName, &hints, addressInfo) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot get address information for host \"%s\" with port \"%s\".", hostName == ((void *)0) ? "<any>" : hostName, portName);
  return 0;
 }
 return 1;
}

canonical_original_code:
_Bool networkGetAddressInfo(char *hostName, char *portName, enum NetworkConnectionType connectionType, struct addrinfo **addressInfo) {
 struct addrinfo hints;
 memset(&hints, 0, sizeof(struct addrinfo));
 if(connectionType == UDP_CONNECTION) {
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = IPPROTO_UDP;
  hints.ai_flags = 0x0001;
 } else if(connectionType == TCP_CONNECTION) {
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = 0;
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unsupported connection type. Unclear how to setup a connection. Failing.");
  return 0;
 }
 hints.ai_family = 0;
 hints.ai_canonname = ((void *)0);
 hints.ai_addr = ((void *)0);
 hints.ai_next = ((void *)0);
 if(getaddrinfo(hostName, portName, &hints, addressInfo) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot get address information for host \"%s\" with port \"%s\".", hostName == ((void *)0) ? "<any>" : hostName, portName);
  return 0;
 }
 return 1;
}

variable_types:
hostName
char *

variable_types:
portName
char *

variable_types: enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 }

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

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


networkCopySocketAddress

decompiled_code:
__int64 __fastcall networkCopySocketAddress(void **a1, unsigned int *a2, const void *a3, unsigned int a4)
{
size_t v5; // rbx
unsigned int v6; // ebp

*a2 = a4;
v5 = a4;
v6 = voidBufferAllocate(a1, a4, "socket address");
if ( (_BYTE)v6 )
memcpy(*a1, a3, v5);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(void **a1, unsigned int *a2, const void *a3, unsigned int a4)
{
size_t v5; // rbx
unsigned int v6; // ebp

*a2 = a4;
v5 = a4;
v6 = voidBufferAllocate(a1, a4, "socket address");
if ( (_BYTE)v6 )
memcpy(*a1, a3, v5);
return v6;
}

original_code:
_Bool networkCopySocketAddress(struct sockaddr **destinationAddress, socklen_t *destinationAddressSize, struct sockaddr *sourceAddress, socklen_t sourceAddressSize) {
 *destinationAddressSize = sourceAddressSize;
 if(!voidBufferAllocate((void **)destinationAddress, sourceAddressSize, "socket address")) {
  return 0;
 }
 memcpy(*destinationAddress, sourceAddress, sourceAddressSize);
 return 1;
}

canonical_original_code:
_Bool networkCopySocketAddress(struct sockaddr **destinationAddress, unsigned int *destinationAddressSize, struct sockaddr *sourceAddress, unsigned int sourceAddressSize) {
 *destinationAddressSize = sourceAddressSize;
 if(!voidBufferAllocate((void **)destinationAddress, sourceAddressSize, "socket address")) {
  return 0;
 }
 memcpy(*destinationAddress, sourceAddress, sourceAddressSize);
 return 1;
}

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

variable_types:
destinationAddressSize
unsigned int *

variable_types:
sourceAddress
struct sockaddr *

variable_types:
sourceAddressSize
unsigned int


networkCopyAddressInfo

decompiled_code:
__int64 __fastcall networkCopyAddressInfo(__int64 a1, char *a2, char *a3)
{
unsigned int v4; // ebp
int *v5; // rax
struct addrinfo *ai; // [rsp+0h] [rbp-48h] BYREF
socklen_t len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

len = 16;
if ( getsockname(*(_DWORD *)a1, &addr, &len) )
{
v5 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve local address from socket (errno = %d)", (unsigned int)*v5);
v4 = 0;
}
else
{
v4 = networkCopySocketAddress((void **)(a1 + 16), (unsigned int *)(a1 + 24), &addr, len);
if ( (_BYTE)v4 )
{
v4 = networkGetAddressInfo(a2, a3, 1, &ai);
if ( (_BYTE)v4 )
{
if ( (ai->ai_family & 0xFFFFFFF7) == 2 )
{
v4 = networkCopySocketAddress((void **)(a1 + 32), (unsigned int *)(a1 + 40), ai->ai_addr, ai->ai_addrlen);
}
else
{
logWrite(
1LL,
"network.c",
"Unknown ai_family %d found when retrieving remote address.",
(unsigned int)ai->ai_family);
v4 = 0;
}
freeaddrinfo(ai);
}
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char *a2, char *a3)
{
unsigned int v4; // ebp
int *v5; // rax
struct addrinfo *ai; // [rsp+0h] [rbp-48h] BYREF
socklen_t len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

len = 16;
if ( getsockname(*(_DWORD *)a1, &addr, &len) )
{
v5 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve local address from socket (errno = %d)", (unsigned int)*v5);
v4 = 0;
}
else
{
v4 = func1((void **)(a1 + 16), (unsigned int *)(a1 + 24), &addr, len);
if ( (_BYTE)v4 )
{
v4 = func0(a2, a3, 1, &ai);
if ( (_BYTE)v4 )
{
if ( (ai->ai_family & 0xFFFFFFF7) == 2 )
{
v4 = func1((void **)(a1 + 32), (unsigned int *)(a1 + 40), ai->ai_addr, ai->ai_addrlen);
}
else
{
logWrite(
1LL,
"network.c",
"Unknown ai_family %d found when retrieving remote address.",
(unsigned int)ai->ai_family);
v4 = 0;
}
freeaddrinfo(ai);
}
}
}
return v4;
}

original_code:
_Bool networkCopyAddressInfo(NetworkConnection *networkConnection, const char *hostName, const char *portName) {
 struct sockaddr localAddress;
 socklen_t localAddressSize;
 struct addrinfo* remoteAddressInfo;
 _Bool result;
 localAddressSize = sizeof(struct sockaddr);
 if(getsockname(networkConnection->socketDescriptor, &localAddress, &localAddressSize) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve local address from socket (errno = %d)", (*__errno_location ()));
  return 0;
 }
 if(!networkCopySocketAddress(&networkConnection->localAddress, &networkConnection->localAddressSize, &localAddress, localAddressSize)) {
  return 0;
 }
 if(!networkGetAddressInfo(hostName, portName, UDP_CONNECTION, &remoteAddressInfo)) {
  return 0;
 }
 result = 1;
 if(remoteAddressInfo->ai_family == 2 || remoteAddressInfo->ai_family == 10) {
  if(!networkCopySocketAddress(&networkConnection->remoteAddress, &networkConnection->remoteAddressSize, remoteAddressInfo->ai_addr, remoteAddressInfo->ai_addrlen)) {
   result = 0;
  }
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown ai_family %d found when retrieving remote address.", remoteAddressInfo->ai_family);
  result = 0;
 }
 freeaddrinfo(remoteAddressInfo);
 return result;
}

canonical_original_code:
_Bool networkCopyAddressInfo(struct NetworkConnectionStruct *networkConnection, char *hostName, char *portName) {
 struct sockaddr localAddress;
 unsigned int localAddressSize;
 struct addrinfo *remoteAddressInfo;
 _Bool result;
 localAddressSize = sizeof(struct sockaddr);
 if(getsockname(networkConnection->socketDescriptor, &localAddress, &localAddressSize) != 0) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve local address from socket (errno = %d)", (*__errno_location ()));
  return 0;
 }
 if(!networkCopySocketAddress(&networkConnection->localAddress, &networkConnection->localAddressSize, &localAddress, localAddressSize)) {
  return 0;
 }
 if(!networkGetAddressInfo(hostName, portName, UDP_CONNECTION, &remoteAddressInfo)) {
  return 0;
 }
 result = 1;
 if(remoteAddressInfo->ai_family == 2 || remoteAddressInfo->ai_family == 10) {
  if(!networkCopySocketAddress(&networkConnection->remoteAddress, &networkConnection->remoteAddressSize, remoteAddressInfo->ai_addr, remoteAddressInfo->ai_addrlen)) {
   result = 0;
  }
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown ai_family %d found when retrieving remote address.", remoteAddressInfo->ai_family);
  result = 0;
 }
 freeaddrinfo(remoteAddressInfo);
 return result;
}

variable_types: struct NetworkConnectionStruct { int socketDescriptor; enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 } connectionType; _Bool isClient; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *localAddress; unsigned int localAddressSize; struct sockaddr *remoteAddress; unsigned int remoteAddressSize; } *

variable_types:
localAddressSize
unsigned int

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

user_defined_types: struct NetworkConnectionStruct { int socketDescriptor; enum NetworkConnectionType connectionType; _Bool isClient; struct sockaddr *localAddress; unsigned int localAddressSize; struct sockaddr *remoteAddress; unsigned int remoteAddressSize; }


networkCloseSocket

decompiled_code:
__int64 __fastcall networkCloseSocket(__int64 a1)
{
int v2; // edi
unsigned int v3; // ebp
__int64 v5; // rcx
int *v6; // rax

v2 = *(_DWORD *)a1;
v3 = 1;
if ( v2 != -1 )
{
v3 = *(unsigned __int8 *)(a1 + 8);
if ( (_BYTE)v3 )
{
if ( shutdown(v2, 2) )
{
v5 = (unsigned int)*_errno_location();
if ( (_DWORD)v5 != 107 )
{
logWrite(1LL, "network.c", "Cannot shutdown the network connection with the server. (errno = %d)", v5);
v3 = 0;
}
}
}
else
{
v3 = 1;
}
if ( close(*(_DWORD *)a1) )
{
v6 = _errno_location();
logWrite(1LL, "network.c", "Cannot close the socket connection with the server. (errno = %d)", (unsigned int)*v6);
v3 = 0;
}
*(_DWORD *)a1 = -1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
int v2; // edi
unsigned int v3; // ebp
__int64 v5; // rcx
int *v6; // rax

v2 = *(_DWORD *)a1;
v3 = 1;
if ( v2 != -1 )
{
v3 = *(unsigned __int8 *)(a1 + 8);
if ( (_BYTE)v3 )
{
if ( shutdown(v2, 2) )
{
v5 = (unsigned int)*_errno_location();
if ( (_DWORD)v5 != 107 )
{
logWrite(1LL, "network.c", "Cannot shutdown the network connection with the server. (errno = %d)", v5);
v3 = 0;
}
}
}
else
{
v3 = 1;
}
if ( close(*(_DWORD *)a1) )
{
v6 = _errno_location();
logWrite(1LL, "network.c", "Cannot close the socket connection with the server. (errno = %d)", (unsigned int)*v6);
v3 = 0;
}
*(_DWORD *)a1 = -1;
}
return v3;
}

original_code:
_Bool networkCloseSocket(NetworkConnection *networkConnection) {
 _Bool result;
 result = 1;
 if(networkConnection->socketDescriptor != -1) {
  if(networkConnection->isClient) {
   if(shutdown(networkConnection->socketDescriptor, SHUT_RDWR) != 0) {
    if((*__errno_location ()) != 107) {
     logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot shutdown the network connection with the server. (errno = %d)", (*__errno_location ()));
     result = 0;
    }
   }
  }
  if(close(networkConnection->socketDescriptor) != 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot close the socket connection with the server. (errno = %d)", (*__errno_location ()));
   result = 0;
  }
  networkConnection->socketDescriptor = -1;
 }
 return result;
}

canonical_original_code:
_Bool networkCloseSocket(struct NetworkConnectionStruct *networkConnection) {
 _Bool result;
 result = 1;
 if(networkConnection->socketDescriptor != -1) {
  if(networkConnection->isClient) {
   if(shutdown(networkConnection->socketDescriptor, SHUT_RDWR) != 0) {
    if((*__errno_location ()) != 107) {
     logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot shutdown the network connection with the server. (errno = %d)", (*__errno_location ()));
     result = 0;
    }
   }
  }
  if(close(networkConnection->socketDescriptor) != 0) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot close the socket connection with the server. (errno = %d)", (*__errno_location ()));
   result = 0;
  }
  networkConnection->socketDescriptor = -1;
 }
 return result;
}

user_defined_types: enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 }

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


networkGetAddressName

decompiled_code:
__int64 __fastcall networkGetAddressName(unsigned __int16 *a1, char *a2, socklen_t a3)
{
unsigned __int16 v3; // cx
const char *v4; // rdx
__int64 result; // rax
int *v6; // rax
const char *v7; // rdx
int *v8; // rax

v3 = *a1;
if ( *a1 == 2 )
{
v7 = inet_ntop(2, a1 + 2, a2, a3);
result = 1LL;
if ( !v7 )
{
v8 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve IPv4 address through inet_ntop (errno = %d)", (unsigned int)*v8);
result = 0LL;
}
}
else if ( v3 == 10 )
{
v4 = inet_ntop(10, a1 + 4, a2, a3);
result = 1LL;
if ( !v4 )
{
v6 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve IPv6 address through inet_ntop (errno = %d)", (unsigned int)*v6);
result = 0LL;
}
}
else
{
logWrite(1LL, "network.c", "Unknown sa_family %d found when retrieving IP address", v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 *a1, char *a2, socklen_t a3)
{
unsigned __int16 v3; // cx
const char *v4; // rdx
__int64 result; // rax
int *v6; // rax
const char *v7; // rdx
int *v8; // rax

v3 = *a1;
if ( *a1 == 2 )
{
v7 = inet_ntop(2, a1 + 2, a2, a3);
result = 1LL;
if ( !v7 )
{
v8 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve IPv4 address through inet_ntop (errno = %d)", (unsigned int)*v8);
result = 0LL;
}
}
else if ( v3 == 10 )
{
v4 = inet_ntop(10, a1 + 4, a2, a3);
result = 1LL;
if ( !v4 )
{
v6 = _errno_location();
logWrite(1LL, "network.c", "Cannot retrieve IPv6 address through inet_ntop (errno = %d)", (unsigned int)*v6);
result = 0LL;
}
}
else
{
logWrite(1LL, "network.c", "Unknown sa_family %d found when retrieving IP address", v3);
result = 0LL;
}
return result;
}

original_code:
_Bool networkGetAddressName(struct sockaddr *address, char *addressName, int maxAddressNameSize) {
 if(address->sa_family == 2) {
  if(inet_ntop(2, &((struct sockaddr_in *)address)->sin_addr, addressName, (socklen_t)maxAddressNameSize) == ((void *)0)) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve IPv4 address through inet_ntop (errno = %d)", (*__errno_location ()));
   return 0;
  }
 } else if(address->sa_family == 10) {
  if(inet_ntop(10, &((struct sockaddr_in6 *)address)->sin6_addr, addressName, (socklen_t)maxAddressNameSize) == ((void *)0)) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve IPv6 address through inet_ntop (errno = %d)", (*__errno_location ()));
   return 0;
  }
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown sa_family %d found when retrieving IP address", address->sa_family);
  return 0;
 }
 return 1;
}

canonical_original_code:
_Bool networkGetAddressName(struct sockaddr *address, char *addressName, int maxAddressNameSize) {
 if(address->sa_family == 2) {
  if(inet_ntop(2, &((struct sockaddr_in *)address)->sin_addr, addressName, (unsigned int)maxAddressNameSize) == ((void *)0)) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve IPv4 address through inet_ntop (errno = %d)", (*__errno_location ()));
   return 0;
  }
 } else if(address->sa_family == 10) {
  if(inet_ntop(10, &((struct sockaddr_in6 *)address)->sin6_addr, addressName, (unsigned int)maxAddressNameSize) == ((void *)0)) {
   logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot retrieve IPv6 address through inet_ntop (errno = %d)", (*__errno_location ()));
   return 0;
  }
 } else {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Unknown sa_family %d found when retrieving IP address", address->sa_family);
  return 0;
 }
 return 1;
}

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

variable_types:
addressName
char *

variable_types: maxAddressNameSize 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]; }

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


networkReceiveMessageInternal

decompiled_code:
__int64 __fastcall networkReceiveMessageInternal(__int64 a1, void *a2, size_t a3, ssize_t *a4, int a5)
{
ssize_t v6; // rbx
__int64 result; // rax
int *v8; // rax
socklen_t addr_len; // [rsp+0h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-28h] BYREF

if ( !a1 )
return 0LL;
if ( *(_BYTE *)(a1 + 8) )
{
v6 = recv(*(_DWORD *)a1, a2, a3, a5);
}
else
{
addr_len = 16;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v6 = recvfrom(*(_DWORD *)a1, a2, a3, a5, &addr, &addr_len);
}
if ( v6 == -1 )
{
v8 = _errno_location();
logWrite(1LL, "network.c", "Cannot receive network message from host. (errno = %d)", (unsigned int)*v8);
result = 0LL;
}
else
{
if ( !v6 )
logWrite(
2LL,
"network.c",
"Cannot receive network message from host (host may already have closed the connection).");
*a4 = v6;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, void *a2, size_t a3, ssize_t *a4, int a5)
{
ssize_t v6; // rbx
__int64 result; // rax
int *v8; // rax
socklen_t addr_len; // [rsp+0h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-28h] BYREF

if ( !a1 )
return 0LL;
if ( *(_BYTE *)(a1 + 8) )
{
v6 = recv(*(_DWORD *)a1, a2, a3, a5);
}
else
{
addr_len = 16;
*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
v6 = recvfrom(*(_DWORD *)a1, a2, a3, a5, &addr, &addr_len);
}
if ( v6 == -1 )
{
v8 = _errno_location();
logWrite(1LL, "network.c", "Cannot receive network message from host. (errno = %d)", (unsigned int)*v8);
result = 0LL;
}
else
{
if ( !v6 )
logWrite(
2LL,
"network.c",
"Cannot receive network message from host (host may already have closed the connection).");
*a4 = v6;
result = 1LL;
}
return result;
}

original_code:
_Bool networkReceiveMessageInternal(NetworkConnection *networkConnection, uint8_t *messageBuffer, size_t maxMessageSize, size_t *messageSize, int flags) {
 struct sockaddr remoteAddress;
 socklen_t remoteAddressSize;
 ssize_t result;
 if(networkConnection == ((void *)0)) {
  return 0;
 }
 if(networkConnection->isClient) {
  result = recv(networkConnection->socketDescriptor, messageBuffer, maxMessageSize, flags);
 } else {
  remoteAddressSize = sizeof(struct sockaddr);
  memset(&remoteAddress, 0, remoteAddressSize);
  result = recvfrom(networkConnection->socketDescriptor, messageBuffer, maxMessageSize, flags, &remoteAddress, &remoteAddressSize);
 }
 if(result == -1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot receive network message from host. (errno = %d)", (*__errno_location ()));
  return 0;
 } else if(result == 0) {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Cannot receive network message from host (host may already have closed the connection).");
 }
 *messageSize = result;
 return 1;
}

canonical_original_code:
_Bool networkReceiveMessageInternal(struct NetworkConnectionStruct *networkConnection, unsigned char *messageBuffer, unsigned long maxMessageSize, unsigned long *messageSize, int flags) {
 struct sockaddr remoteAddress;
 unsigned int remoteAddressSize;
 long result;
 if(networkConnection == ((void *)0)) {
  return 0;
 }
 if(networkConnection->isClient) {
  result = recv(networkConnection->socketDescriptor, messageBuffer, maxMessageSize, flags);
 } else {
  remoteAddressSize = sizeof(struct sockaddr);
  memset(&remoteAddress, 0, remoteAddressSize);
  result = recvfrom(networkConnection->socketDescriptor, messageBuffer, maxMessageSize, flags, &remoteAddress, &remoteAddressSize);
 }
 if(result == -1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot receive network message from host. (errno = %d)", (*__errno_location ()));
  return 0;
 } else if(result == 0) {
  logWrite(LOG_LEVEL_WARNING, LOG_COMPONENT_NAME, "Cannot receive network message from host (host may already have closed the connection).");
 }
 *messageSize = result;
 return 1;
}

variable_types:
messageBuffer
unsigned char *

variable_types:
maxMessageSize
unsigned long

variable_types:
messageSize
unsigned long *

variable_types: flags int

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

variable_types:
remoteAddressSize
unsigned int

variable_types: result long


networkGetConnectionType

decompiled_code:
__int64 __fastcall networkGetConnectionType(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
NetworkConnectionType networkGetConnectionType(NetworkConnection *networkConnection) {
 return networkConnection->connectionType;
}

canonical_original_code:
enum NetworkConnectionType networkGetConnectionType(struct NetworkConnectionStruct *networkConnection) {
 return networkConnection->connectionType;
}

return_type: enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 }


networkGetLocalAddressName

decompiled_code:
__int64 __fastcall networkGetLocalAddressName(__int64 a1, char *a2, socklen_t a3)
{
return networkGetAddressName(*(unsigned __int16 **)(a1 + 16), a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, char *a2, socklen_t a3)
{
return func4(*(unsigned __int16 **)(a1 + 16), a2, a3);
}

original_code:
_Bool networkGetLocalAddressName(NetworkConnection *networkConnection, char *addressName, int maxAddressNameSize) {
 return networkGetAddressName(networkConnection->localAddress, addressName, maxAddressNameSize);
}

canonical_original_code:
_Bool networkGetLocalAddressName(struct NetworkConnectionStruct *networkConnection, char *addressName, int maxAddressNameSize) {
 return networkGetAddressName(networkConnection->localAddress, addressName, maxAddressNameSize);
}


networkGetRemoteAddressName

decompiled_code:
__int64 __fastcall networkGetRemoteAddressName(__int64 a1, char *a2, socklen_t a3)
{
return networkGetAddressName(*(unsigned __int16 **)(a1 + 32), a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, char *a2, socklen_t a3)
{
return func4(*(unsigned __int16 **)(a1 + 32), a2, a3);
}

original_code:
_Bool networkGetRemoteAddressName(NetworkConnection *networkConnection, char *addressName, int maxAddressNameSize) {
 return networkGetAddressName(networkConnection->remoteAddress, addressName, maxAddressNameSize);
}

canonical_original_code:
_Bool networkGetRemoteAddressName(struct NetworkConnectionStruct *networkConnection, char *addressName, int maxAddressNameSize) {
 return networkGetAddressName(networkConnection->remoteAddress, addressName, maxAddressNameSize);
}


networkSendMessage

decompiled_code:
__int64 __fastcall networkSendMessage(__int64 a1, const void *a2, size_t a3)
{
ssize_t v4; // rax
unsigned int v5; // edx
int *v7; // rax
int *v8; // rax

if ( !a1 )
return 0LL;
if ( *(_BYTE *)(a1 + 8) )
v4 = send(*(_DWORD *)a1, a2, a3, 0);
else
v4 = sendto(*(_DWORD *)a1, a2, a3, 0, *(const struct sockaddr **)(a1 + 32), *(_DWORD *)(a1 + 40));
if ( v4 == -1 )
{
v7 = _errno_location();
logWrite(1LL, "network.c", "Cannot send network message to server. (errno = %d)", (unsigned int)*v7);
v5 = 0;
}
else
{
v5 = 1;
if ( a3 != v4 )
{
v8 = _errno_location();
logWrite(
1LL,
"network.c",
"Cannot send full network message to server (ie partial send). (errno = %d)",
(unsigned int)*v8);
v5 = 0;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, const void *a2, size_t a3)
{
ssize_t v4; // rax
unsigned int v5; // edx
int *v7; // rax
int *v8; // rax

if ( !a1 )
return 0LL;
if ( *(_BYTE *)(a1 + 8) )
v4 = send(*(_DWORD *)a1, a2, a3, 0);
else
v4 = sendto(*(_DWORD *)a1, a2, a3, 0, *(const struct sockaddr **)(a1 + 32), *(_DWORD *)(a1 + 40));
if ( v4 == -1 )
{
v7 = _errno_location();
logWrite(1LL, "network.c", "Cannot send network message to server. (errno = %d)", (unsigned int)*v7);
v5 = 0;
}
else
{
v5 = 1;
if ( a3 != v4 )
{
v8 = _errno_location();
logWrite(
1LL,
"network.c",
"Cannot send full network message to server (ie partial send). (errno = %d)",
(unsigned int)*v8);
v5 = 0;
}
}
return v5;
}

original_code:
_Bool networkSendMessage(NetworkConnection *networkConnection, uint8_t *messageBuffer, size_t messageSize) {
 ssize_t result;
 if(networkConnection == ((void *)0)) {
  return 0;
 }
 if(networkConnection->isClient) {
  result = send(networkConnection->socketDescriptor, messageBuffer, messageSize, 0);
 } else {
  result = sendto(networkConnection->socketDescriptor, messageBuffer, messageSize, 0, networkConnection->remoteAddress, networkConnection->remoteAddressSize);
 }
 if(result == -1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot send network message to server. (errno = %d)", (*__errno_location ()));
  return 0;
 } else if(result != messageSize) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot send full network message to server (ie partial send). (errno = %d)", (*__errno_location ()));
  return 0;
 }
 return 1;
}

canonical_original_code:
_Bool networkSendMessage(struct NetworkConnectionStruct *networkConnection, unsigned char *messageBuffer, unsigned long messageSize) {
 long result;
 if(networkConnection == ((void *)0)) {
  return 0;
 }
 if(networkConnection->isClient) {
  result = send(networkConnection->socketDescriptor, messageBuffer, messageSize, 0);
 } else {
  result = sendto(networkConnection->socketDescriptor, messageBuffer, messageSize, 0, networkConnection->remoteAddress, networkConnection->remoteAddressSize);
 }
 if(result == -1) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot send network message to server. (errno = %d)", (*__errno_location ()));
  return 0;
 } else if(result != messageSize) {
  logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot send full network message to server (ie partial send). (errno = %d)", (*__errno_location ()));
  return 0;
 }
 return 1;
}

variable_types:
messageSize
unsigned long


networkReceiveMessage

decompiled_code:
__int64 __fastcall networkReceiveMessage(__int64 a1, void *a2, size_t a3, ssize_t *a4)
{
return networkReceiveMessageInternal(a1, a2, a3, a4, 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, void *a2, size_t a3, ssize_t *a4)
{
return func5(a1, a2, a3, a4, 0);
}

original_code:
_Bool networkReceiveMessage(NetworkConnection *networkConnection, uint8_t *messageBuffer, size_t maxMessageSize, size_t *messageSize) {
 return networkReceiveMessageInternal(networkConnection, messageBuffer, maxMessageSize, messageSize, 0);
}

canonical_original_code:
_Bool networkReceiveMessage(struct NetworkConnectionStruct *networkConnection, unsigned char *messageBuffer, unsigned long maxMessageSize, unsigned long *messageSize) {
 return networkReceiveMessageInternal(networkConnection, messageBuffer, maxMessageSize, messageSize, 0);
}


networkIsMessageAvailable

decompiled_code:
_BOOL8 __fastcall networkIsMessageAvailable(__int64 a1)
{
char v1; // dl
bool v2; // al
ssize_t v4; // [rsp+0h] [rbp-18h] BYREF
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = networkReceiveMessageInternal(a1, v5, 1uLL, &v4, 2);
v2 = 0;
if ( v1 )
v2 = v4 == 1;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(__int64 a1)
{
char v1; // dl
bool v2; // al
ssize_t v4; // [rsp+0h] [rbp-18h] BYREF
char v5[9]; // [rsp+Fh] [rbp-9h] BYREF

v1 = func5(a1, v5, 1uLL, &v4, 2);
v2 = 0;
if ( v1 )
v2 = v4 == 1;
return v2;
}

original_code:
_Bool networkIsMessageAvailable(NetworkConnection *networkConnection) {
 uint8_t byte;
 size_t size;
 return networkReceiveMessageInternal(networkConnection, &byte, 1, &size, MSG_PEEK) && size == 1;
}

canonical_original_code:
_Bool networkIsMessageAvailable(struct NetworkConnectionStruct *networkConnection) {
 unsigned char byte;
 unsigned long size;
 return networkReceiveMessageInternal(networkConnection, &byte, 1, &size, MSG_PEEK) && size == 1;
}

variable_types:
byte
unsigned char

variable_types:
size
unsigned long


networkCloseConnection

decompiled_code:
__int64 __fastcall networkCloseConnection(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = networkCloseSocket(v2);
v4 = voidBufferFree(*a1 + 16);
if ( !(_BYTE)v4 )
v3 = v4;
v5 = voidBufferFree(*a1 + 32);
if ( !(_BYTE)v5 )
v3 = v5;
v6 = voidBufferFree(a1);
if ( !(_BYTE)v6 )
v3 = v6;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 *a1)
{
__int64 v2; // rdi
unsigned int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax

v2 = *a1;
v3 = 1;
if ( v2 )
{
v3 = func3(v2);
v4 = voidBufferFree(*a1 + 16);
if ( !(_BYTE)v4 )
v3 = v4;
v5 = voidBufferFree(*a1 + 32);
if ( !(_BYTE)v5 )
v3 = v5;
v6 = voidBufferFree(a1);
if ( !(_BYTE)v6 )
v3 = v6;
}
return v3;
}

original_code:
_Bool networkCloseConnection(NetworkConnection **networkConnection) {
 _Bool result;
 result = 1;
 if(*networkConnection != ((void *)0)) {
  if(!networkCloseSocket(*networkConnection)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*networkConnection)->localAddress)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*networkConnection)->remoteAddress)) {
   result = 0;
  }
  if(!voidBufferFree((void **)networkConnection)) {
   result = 0;
  }
 }
 return result;
}

canonical_original_code:
_Bool networkCloseConnection(struct NetworkConnectionStruct **networkConnection) {
 _Bool result;
 result = 1;
 if(*networkConnection != ((void *)0)) {
  if(!networkCloseSocket(*networkConnection)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*networkConnection)->localAddress)) {
   result = 0;
  }
  if(!voidBufferFree((void **)&(*networkConnection)->remoteAddress)) {
   result = 0;
  }
  if(!voidBufferFree((void **)networkConnection)) {
   result = 0;
  }
 }
 return result;
}

variable_types: struct NetworkConnectionStruct { int socketDescriptor; enum NetworkConnectionType { TCP_CONNECTION=0, UDP_CONNECTION=1 } connectionType; _Bool isClient; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *localAddress; unsigned int localAddressSize; struct sockaddr *remoteAddress; unsigned int remoteAddressSize; } **