decompiled_code:
__int64 __fastcall nbt_read_long(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 7, 1LL);
v3 = gzread(a1, a2 + 6, 1LL) + v2;
v4 = gzread(a1, a2 + 5, 1LL) + v3;
v5 = gzread(a1, a2 + 4, 1LL) + v4;
v6 = gzread(a1, a2 + 3, 1LL) + v5;
v7 = gzread(a1, a2 + 2, 1LL) + v6;
v8 = gzread(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzread(a1, a2, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // ebx

if ( !a2 )
return 4294967292LL;
v2 = gzread(a1, a2 + 7, 1LL);
v3 = gzread(a1, a2 + 6, 1LL) + v2;
v4 = gzread(a1, a2 + 5, 1LL) + v3;
v5 = gzread(a1, a2 + 4, 1LL) + v4;
v6 = gzread(a1, a2 + 3, 1LL) + v5;
v7 = gzread(a1, a2 + 2, 1LL) + v6;
v8 = gzread(a1, a2 + 1, 1LL) + v7;
return v8 + (unsigned int)gzread(a1, a2, 1LL);
}

original_code:
static int nbt_read_long(gzFile file, nbt_long *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((nbt_byte *) payload) + 7, 1);
 read += gzread(file, ((nbt_byte *) payload) + 6, 1);
 read += gzread(file, ((nbt_byte *) payload) + 5, 1);
 read += gzread(file, ((nbt_byte *) payload) + 4, 1);
 read += gzread(file, ((nbt_byte *) payload) + 3, 1);
 read += gzread(file, ((nbt_byte *) payload) + 2, 1);
 read += gzread(file, ((nbt_byte *) payload) + 1, 1);
 read += gzread(file, ((nbt_byte *) payload), 1);
 return read;
}

canonical_original_code:
static int nbt_read_long(struct gzFile_s *file, long *payload)
{
 int read = 0;
 if (!payload) {
  return -4;
 }
 read += gzread(file, ((char *) payload) + 7, 1);
 read += gzread(file, ((char *) payload) + 6, 1);
 read += gzread(file, ((char *) payload) + 5, 1);
 read += gzread(file, ((char *) payload) + 4, 1);
 read += gzread(file, ((char *) payload) + 3, 1);
 read += gzread(file, ((char *) payload) + 2, 1);
 read += gzread(file, ((char *) payload) + 1, 1);
 read += gzread(file, ((char *) payload), 1);
 return read;
}


repo: ziocroc/snbtlib

decompiled_code:
__int64 __fastcall nbt_read_string(__int64 a1, __int16 *a2)
{
__int64 v2; // rbx
_BYTE *v3; // rax
__int64 v4; // rbx
int v5; // ecx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_short(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(v2 + 1);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( (__int16)v2 > 0 )
{
v4 = 0LL;
do
{
if ( !(unsigned int)gzread(a1, &v3[v4], 1LL) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v5 = ++v4;
v3 = (_BYTE *)*((_QWORD *)a2 + 1);
}
while ( *a2 > (int)v4 );
v3 += v5;
}
*v3 = 0;
return (unsigned int)(*a2 + 2);
}

original_code:
static int nbt_read_string(gzFile file, nbt_string *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_short(file, &payload->length)) {
  return -1;
 }
 payload->chars = malloc(sizeof(char) * payload->length + 1);
 if (!payload->chars) {
  return -2;
 }
 for (i = 0; i < payload->length; i++) {
  if (!gzread(file, payload->chars + i, 1)) {
   payload->chars[i] = 0;
  }
 }
 payload->chars[i] = 0;
 return payload->length + sizeof(nbt_short);
}

canonical_original_code:
static int nbt_read_string(struct gzFile_s *file, struct nbt_string *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_short(file, &payload->length)) {
  return -1;
 }
 payload->chars = malloc(sizeof(char) * payload->length + 1);
 if (!payload->chars) {
  return -2;
 }
 for (i = 0; i < payload->length; i++) {
  if (!gzread(file, payload->chars + i, 1)) {
   payload->chars[i] = 0;
  }
 }
 payload->chars[i] = 0;
 return payload->length + sizeof(nbt_short);
}

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *

variable_types: struct nbt_string { short length; char *chars; } *

variable_types: i int

return_type: int


decompiled_code:
__int64 __fastcall nbt_read_byte_array(__int64 a1, int *a2)
{
int v2; // er13
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_int(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(*a2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 > 0 )
{
v4 = 0LL;
while ( 1 )
{
if ( !(unsigned int)nbt_read_byte(a1, (__int64)v3 + v4) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v2 = *a2;
if ( *a2 <= (int)++v4 )
break;
v3 = (void *)*((_QWORD *)a2 + 1);
}
}
return (unsigned int)(v2 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int *a2)
{
int v2; // er13
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)func2(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(*a2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( v2 > 0 )
{
v4 = 0LL;
while ( 1 )
{
if ( !(unsigned int)func0(a1, (__int64)v3 + v4) )
*(_BYTE *)(*((_QWORD *)a2 + 1) + v4) = 0;
v2 = *a2;
if ( *a2 <= (int)++v4 )
break;
v3 = (void *)*((_QWORD *)a2 + 1);
}
}
return (unsigned int)(v2 + 4);
}

original_code:
static int nbt_read_byte_array(gzFile file, nbt_byte_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->bytes = (nbt_byte *) malloc(sizeof(nbt_byte) * payload->size);
 if (!payload->bytes) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_byte(file, payload->bytes + i)) {
   payload->bytes[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

canonical_original_code:
static int nbt_read_byte_array(struct gzFile_s *file, struct nbt_byte_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->bytes = (char *) malloc(sizeof(nbt_byte) * payload->size);
 if (!payload->bytes) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_byte(file, payload->bytes + i)) {
   payload->bytes[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

variable_types: struct nbt_byte_array { int size; char *bytes; } *

user_defined_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; }


decompiled_code:
__int64 __fastcall nbt_read_int_array(__int64 a1, int *a2)
{
__int64 v2; // r12
void *v3; // rax
__int64 v4; // rbx

if ( !a2 )
return 4294967292LL;
if ( !(unsigned int)nbt_read_int(a1, (__int64)a2) )
return 0xFFFFFFFFLL;
v2 = *a2;
v3 = malloc(4 * v2);
*((_QWORD *)a2 + 1) = v3;
if ( !v3 )
return 4294967294LL;
if ( (int)v2 > 0 )
{
v4 = 0LL;
while ( 1 )
{
if ( !(unsigned int)nbt_read_int(a1, (__int64)v3 + 4 * v4) )
*(_DWORD *)(*((_QWORD *)a2 + 1) + 4 * v4) = 0;
LODWORD(v2) = *a2;
if ( *a2 <= (int)++v4 )
break;
v3 = (void *)*((_QWORD *)a2 + 1);
}
}
return (unsigned int)(v2 + 4);
}

original_code:
static int nbt_read_int_array(gzFile file, nbt_int_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->ints = (nbt_int *) malloc(sizeof(nbt_int) * payload->size);
 if (!payload->ints) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_int(file, payload->ints + i)) {
   payload->ints[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

canonical_original_code:
static int nbt_read_int_array(struct gzFile_s *file, struct nbt_int_array *payload)
{
 int i;
 if (!payload) {
  return -4;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->ints = (int *) malloc(sizeof(nbt_int) * payload->size);
 if (!payload->ints) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  if (!nbt_read_int(file, payload->ints + i)) {
   payload->ints[i] = 0;
  }
 }
 return payload->size + sizeof(nbt_int);
}

variable_types: struct nbt_int_array { int size; int *ints; } *


decompiled_code:
__int64 __fastcall nbt_read_compound(__int64 a1, void **a2)
{
__int64 v3; // r14
char *v4; // rdi
unsigned int v5; // er12
int v6; // ebx
char *v7; // rax
__int64 v8; // r15
int v9; // eax
void *v10; // rdi

if ( !a2 )
return (unsigned int)-4;
*a2 = 0LL;
v3 = 0LL;
v4 = 0LL;
v5 = 0;
v6 = 0;
while ( 1 )
{
if ( v6 > (int)v3 )
goto LABEL_7;
v6 += 8;
v7 = (char *)realloc(v4, 48LL * v6);
v4 = v7;
if ( !v7 )
break;
*a2 = v7;
LABEL_7:
v8 = 48 * v3;
v9 = nbt_read_tag(a1, &v4[48 * v3]);
if ( v9 <= 0 )
{
v10 = *a2;
v5 = v9;
if ( !*a2 )
return v5;
goto LABEL_9;
}
v4 = (char *)*a2;
v5 += v9;
++v3;
if ( !*((_BYTE *)*a2 + v8) )
return v5;
}
v10 = *a2;
v5 = -2;
if ( *a2 )
LABEL_9:
free(v10);
return v5;
}

original_code:
static int nbt_read_compound(gzFile file, nbt_compound *payload)
{
 int i, max = 0, res, read = 0;
 void *tmpptr;
 if (!payload) {
  return -4;
 }
 *payload = (nbt_tag *) 0;
 for (i = 0;; i++) {
  if (i >= max) {
   max += 8;
   tmpptr = realloc(*payload, sizeof(struct nbt_tag) * max);
   if (!tmpptr) {
    if (*payload) {
     free(*payload);
    }
    return -2;
   }
   *payload = tmpptr;
  }
  res = nbt_read_tag(file, *payload + i);
  if (res > 0) {
   read += res;
   if ((*payload + i)->id == 0) {
    return read;
   }
  } else {
   if (*payload) {
    free(*payload);
   }
   return res;
  }
 }
 return read;
}

canonical_original_code:
static int nbt_read_compound(struct gzFile_s *file, struct nbt_tag **payload)
{
 int i; int max = 0; int res; int read = 0;
 void *tmpptr;
 if (!payload) {
  return -4;
 }
 *payload = (struct nbt_tag *) 0;
 for (i = 0;; i++) {
  if (i >= max) {
   max += 8;
   tmpptr = realloc(*payload, sizeof(struct nbt_tag) * max);
   if (!tmpptr) {
    if (*payload) {
     free(*payload);
    }
    return -2;
   }
   *payload = tmpptr;
  }
  res = nbt_read_tag(file, *payload + i);
  if (res > 0) {
   read += res;
   if ((*payload + i)->id == 0) {
    return read;
   }
  } else {
   if (*payload) {
    free(*payload);
   }
   return res;
  }
 }
 return read;
}

variable_types: struct nbt_tag { char id; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } payload; unsigned long position; struct nbt_string name; } **

variable_types: max int

variable_types: res int

variable_types: read int

variable_types:
tmpptr
void *

user_defined_types: struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; }

user_defined_types: union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array byte_array_payload; struct nbt_string string_payload; struct nbt_list list_payload; struct nbt_tag *compound_payload; struct nbt_int_array int_array_payload; }


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

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = nbt_read_byte(a1, (__int64)a3);
break;
case 2:
result = nbt_read_short(a1, (__int64)a3);
break;
case 3:
case 5:
result = nbt_read_int(a1, (__int64)a3);
break;
case 4:
case 6:
result = nbt_read_long(a1, (__int64)a3);
break;
case 7:
result = nbt_read_byte_array(a1, a3);
break;
case 8:
result = nbt_read_string(a1, (__int16 *)a3);
break;
case 9:
result = nbt_read_list(a1, a3);
break;
case 10:
result = nbt_read_compound(a1, (void **)a3);
break;
case 11:
result = nbt_read_int_array(a1, a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

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

switch ( a2 )
{
case 0:
result = 0LL;
break;
case 1:
result = func0(a1, (__int64)a3);
break;
case 2:
result = func1(a1, (__int64)a3);
break;
case 3:
case 5:
result = func2(a1, (__int64)a3);
break;
case 4:
case 6:
result = func3(a1, (__int64)a3);
break;
case 7:
result = func5(a1, a3);
break;
case 8:
result = func4(a1, (__int16 *)a3);
break;
case 9:
result = func10(a1, a3);
break;
case 10:
result = func7(a1, (void **)a3);
break;
case 11:
result = func6(a1, a3);
break;
default:
result = 4294967293LL;
break;
}
return result;
}

original_code:
static int nbt_read_payload(gzFile file, nbt_byte id, nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_read_byte(file, (nbt_byte *) payload);
  case 2:
   return nbt_read_short(file, (nbt_short *) payload);
  case 3:
   return nbt_read_int(file, (nbt_int *) payload);
  case 4:
   return nbt_read_long(file, (nbt_long *) payload);
  case 5:
   return nbt_read_float(file, (nbt_float *) payload);
  case 6:
   return nbt_read_double(file, (nbt_double *) payload);
  case 7:
   return nbt_read_byte_array(file, (nbt_byte_array *) payload);
  case 8:
   return nbt_read_string(file, (nbt_string *) payload);
  case 9:
   return nbt_read_list(file, (nbt_list *) payload);
  case 10:
   return nbt_read_compound(file, (nbt_compound *) payload);
  case 11:
   return nbt_read_int_array(file, (nbt_int_array *) payload);
  default:
   return -3;
 }
}

canonical_original_code:
static int nbt_read_payload(struct gzFile_s *file, char id, union nbt_payload *payload)
{
 switch (id) {
  case 0:
   return 0;
  case 1:
   return nbt_read_byte(file, (char *) payload);
  case 2:
   return nbt_read_short(file, (short *) payload);
  case 3:
   return nbt_read_int(file, (int *) payload);
  case 4:
   return nbt_read_long(file, (long *) payload);
  case 5:
   return nbt_read_float(file, (float *) payload);
  case 6:
   return nbt_read_double(file, (double *) payload);
  case 7:
   return nbt_read_byte_array(file, (struct nbt_byte_array *) payload);
  case 8:
   return nbt_read_string(file, (struct nbt_string *) payload);
  case 9:
   return nbt_read_list(file, (struct nbt_list *) payload);
  case 10:
   return nbt_read_compound(file, (struct nbt_tag **) payload);
  case 11:
   return nbt_read_int_array(file, (struct nbt_int_array *) payload);
  default:
   return -3;
 }
}

variable_types: id char

variable_types: union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; } *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } *


decompiled_code:
__int64 __fastcall nbt_read_tag(__int64 a1, __int64 a2)
{
char v3; // si
__int64 result; // rax

if ( !a2 )
return 4294967294LL;
if ( !(unsigned int)nbt_read_byte(a1, a2) )
return 0xFFFFFFFFLL;
v3 = *(_BYTE *)a2;
if ( !*(_BYTE *)a2 )
goto LABEL_4;
if ( !(unsigned int)nbt_read_string(a1, (__int16 *)(a2 + 32)) )
return 0xFFFFFFFFLL;
v3 = *(_BYTE *)a2;
LABEL_4:
result = nbt_read_payload(a1, v3, (int *)(a2 + 8));
if ( (int)result >= 0 )
result = (unsigned int)(result + *(__int16 *)(a2 + 32) + 3);
return result;
}

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

if ( !a2 )
return 4294967294LL;
if ( !(unsigned int)func0(a1, a2) )
return 0xFFFFFFFFLL;
v3 = *(_BYTE *)a2;
if ( !*(_BYTE *)a2 )
goto LABEL_4;
if ( !(unsigned int)func4(a1, (__int16 *)(a2 + 32)) )
return 0xFFFFFFFFLL;
v3 = *(_BYTE *)a2;
LABEL_4:
result = func8(a1, v3, (int *)(a2 + 8));
if ( (int)result >= 0 )
result = (unsigned int)(result + *(__int16 *)(a2 + 32) + 3);
return result;
}

original_code:
static int nbt_read_tag(gzFile file, nbt_tag *tag)
{
 int res;
 if (!tag) {
  return -2;
 }
 if (!(
  nbt_read_byte(file, &tag->id) &&
  (tag->id == 0 || nbt_read_string(file, &tag->name))
 )) {
  return -1;
 }
 res = nbt_read_payload(file, tag->id, &tag->payload);
 if (res >= 0) {
  return sizeof(nbt_byte) + sizeof(nbt_short) + tag->name.length + res;
 } else {
  return res;
 }
}

canonical_original_code:
static int nbt_read_tag(struct gzFile_s *file, struct nbt_tag *tag)
{
 int res;
 if (!tag) {
  return -2;
 }
 if (!(
  nbt_read_byte(file, &tag->id) &&
  (tag->id == 0 || nbt_read_string(file, &tag->name))
 )) {
  return -1;
 }
 res = nbt_read_payload(file, tag->id, &tag->payload);
 if (res >= 0) {
  return sizeof(nbt_byte) + sizeof(nbt_short) + tag->name.length + res;
 } else {
  return res;
 }
}

variable_types: struct nbt_tag { char id; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } payload; unsigned long position; struct nbt_string name; } *


decompiled_code:
__int64 __fastcall nbt_read_list(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
int *v3; // rax
__int64 v4; // rbp
unsigned int v5; // er13
int v6; // eax

if ( !a2 )
return (unsigned int)-4;
if ( !(unsigned int)nbt_read_byte(a1, a2) || !(unsigned int)nbt_read_int(a1, a2 + 4) )
return (unsigned int)-1;
v2 = *(int *)(a2 + 4);
v3 = (int *)malloc(16 * v2);
*(_QWORD *)(a2 + 8) = v3;
if ( !v3 )
return (unsigned int)-2;
if ( (int)v2 > 0 )
{
v4 = 0LL;
v5 = 5;
while ( 1 )
{
v6 = nbt_read_payload(a1, *(_BYTE *)a2, &v3[4 * v4]);
if ( v6 > 0 )
v5 += v6;
if ( *(_DWORD *)(a2 + 4) <= (int)++v4 )
break;
v3 = *(int **)(a2 + 8);
}
return v5;
}
return 5LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
int *v3; // rax
__int64 v4; // rbp
unsigned int v5; // er13
int v6; // eax

if ( !a2 )
return (unsigned int)-4;
if ( !(unsigned int)func0(a1, a2) || !(unsigned int)func2(a1, a2 + 4) )
return (unsigned int)-1;
v2 = *(int *)(a2 + 4);
v3 = (int *)malloc(16 * v2);
*(_QWORD *)(a2 + 8) = v3;
if ( !v3 )
return (unsigned int)-2;
if ( (int)v2 > 0 )
{
v4 = 0LL;
v5 = 5;
while ( 1 )
{
v6 = func8(a1, *(_BYTE *)a2, &v3[4 * v4]);
if ( v6 > 0 )
v5 += v6;
if ( *(_DWORD *)(a2 + 4) <= (int)++v4 )
break;
v3 = *(int **)(a2 + 8);
}
return v5;
}
return 5LL;
}

original_code:
static int nbt_read_list(gzFile file, nbt_list *payload)
{
 int res, i;
 int read = sizeof(nbt_byte) + sizeof(nbt_int);
 if (!payload) {
  return -4;
 }
 if (!nbt_read_byte(file, &payload->tag_id)) {
  return -1;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->tags = malloc(sizeof(nbt_payload) * payload->size);
 if (!payload->tags) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  res = nbt_read_payload(file, payload->tag_id, (nbt_payload *) (payload->tags + i));
  if (res > 0) {
   read += res;
  }
 }
 return read;
}

canonical_original_code:
static int nbt_read_list(struct gzFile_s *file, struct nbt_list *payload)
{
 int res; int i;
 int read = sizeof(nbt_byte) + sizeof(nbt_int);
 if (!payload) {
  return -4;
 }
 if (!nbt_read_byte(file, &payload->tag_id)) {
  return -1;
 }
 if (!nbt_read_int(file, &payload->size)) {
  return -1;
 }
 payload->tags = malloc(sizeof(nbt_payload) * payload->size);
 if (!payload->tags) {
  return -2;
 }
 for (i = 0; i < payload->size; i++) {
  res = nbt_read_payload(file, payload->tag_id, (union nbt_payload *) (payload->tags + i));
  if (res > 0) {
   read += res;
  }
 }
 return read;
}

variable_types: struct nbt_list { char tag_id; int size; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list list_payload; struct nbt_tag { char id; union nbt_payload payload; unsigned long position; struct nbt_string name; } *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } *tags; } *

user_defined_types: struct nbt_list { char tag_id; int size; union nbt_payload *tags; }


decompiled_code:
__int64 __fastcall nbt_read(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp
unsigned int v4; // er12

if ( !a1 )
return (unsigned int)-5;
v2 = gzopen(a2, &unk_6D8);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = nbt_read_tag(v2, a1);
gzclose(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp
unsigned int v4; // er12

if ( !a1 )
return (unsigned int)-5;
v2 = gzopen(a2, &unk_6D8);
v3 = v2;
if ( !v2 )
return (unsigned int)-1;
v4 = func9(v2, a1);
gzclose(v3);
return v4;
}

original_code:
int nbt_read(nbt_tag *tag, char *path)
{
 gzFile file;
 int res;
 if (!tag) {
  return -5;
 }
 file = gzopen(path, "rb");
 if (!file) {
  return -1;
 }
 res = nbt_read_tag(file, tag);
 gzclose(file);
 return res;
}

canonical_original_code:
int nbt_read(struct nbt_tag *tag, char *path)
{
 struct gzFile_s *file;
 int res;
 if (!tag) {
  return -5;
 }
 file = gzopen(path, "rb");
 if (!file) {
  return -1;
 }
 res = nbt_read_tag(file, tag);
 gzclose(file);
 return res;
}

variable_types:
path
char *

user_defined_types: struct nbt_byte_array { int size; char *bytes; }

user_defined_types: struct nbt_string { short length; char *chars; }

user_defined_types: struct nbt_int_array { int size; int *ints; }


decompiled_code:
char *__fastcall nbt_type_name(unsigned __int8 a1)
{
char *result; // rax

result = (char *)&unk_271;
if ( a1 != 127 )
{
result = (char *)&unk_275;
if ( a1 <= 0xBu )
result = typenames[(char)a1];
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned __int8 a1)
{
char *result; // rax

result = (char *)&unk_271;
if ( a1 != 127 )
{
result = (char *)&unk_275;
if ( a1 <= 0xBu )
result = typenames[(char)a1];
}
return result;
}

original_code:
char *nbt_type_name(nbt_byte id) {
 if (id == 127) {
  id = 12;
 } else if (id < 0 || id > 11) {
  id = 13;
 }
 return typenames[id];
}

canonical_original_code:
char *nbt_type_name(char id) {
 if (id == 127) {
  id = 12;
 } else if (id < 0 || id > 11) {
  id = 13;
 }
 return typenames[id];
}

return_type: char *


printmul

decompiled_code:
__int64 __fastcall printmul(__int64 a1, int a2)
{
unsigned int v2; // er15
__int64 v3; // r13
void *v4; // rbp
unsigned __int8 v5; // al
char *v6; // rax
__int64 i; // rbx
__m128i v8; // xmm1
__m128i v9; // xmm2
char v11; // [rsp+Ch] [rbp-9Ch]
__m128i v12; // [rsp+10h] [rbp-98h] BYREF
__m128i v13; // [rsp+20h] [rbp-88h] BYREF
__m128i v14; // [rsp+30h] [rbp-78h] BYREF
__int128 v15[6]; // [rsp+40h] [rbp-68h] BYREF

v2 = a2 + 1;
v3 = nbt_get_length();
v4 = malloc(a2 + 2);
v11 = nbt_get_children_type(a1);
memset(v4, 45, a2 + 1);
*((_BYTE *)v4 + a2 + 1) = 0;
v5 = nbt_get_children_type(a1);
v6 = nbt_type_name(v5);
printf("(%ss)\n", v6);
if ( v3 )
{
for ( i = 0LL; i != v3; ++i )
{
printf("%s", (const char *)v4);
if ( v11 == 7 || (unsigned __int8)(v11 - 9) <= 2u )
{
nbt_get_multiple(&v12, a1);
v8 = _mm_loadu_si128(&v13);
v9 = _mm_loadu_si128(&v14);
v15[0] = (__int128)_mm_loadu_si128(&v12);
v15[1] = (__int128)v8;
v15[2] = (__int128)v9;
printmul(v15, v2);
}
else
{
printval(a1, (unsigned int)v11, v2);
}
}
}
free(v4);
return 1LL;
}

original_code:
static int printmul(nbt_tag *tag, int depth)
{
 uint64_t length = nbt_get_length(tag);
 uint64_t i;
 char *depthchars = (char *) malloc(depth + 2);
 nbt_byte ctype = nbt_get_children_type(tag);
 nbt_tag muls;
 memset(depthchars, '-', depth + 1);
 depthchars[depth + 1] = 0;
 printf("(%ss)\n", nbt_type_name(nbt_get_children_type(tag)));
 for (i = 0; i < length; i++) {
  printf("%s", depthchars);
  if (( (ctype == 7) || ( (ctype >= 9) && (ctype <= 11) ) )) {
   muls = nbt_get_multiple(tag);
   printmul(&muls, depth + 1);
  } else {
   printval(tag, ctype, depth + 1);
  }
 }
 free(depthchars);
 return 1;
}

canonical_original_code:
static int printmul(struct nbt_tag *tag, int depth)
{
 unsigned long length = nbt_get_length(tag);
 unsigned long i;
 char *depthchars = (char *) malloc(depth + 2);
 char ctype = nbt_get_children_type(tag);
 struct nbt_tag muls;
 memset(depthchars, '-', depth + 1);
 depthchars[depth + 1] = 0;
 printf("(%ss)\n", nbt_type_name(nbt_get_children_type(tag)));
 for (i = 0; i < length; i++) {
  printf("%s", depthchars);
  if (( (ctype == 7) || ( (ctype >= 9) && (ctype <= 11) ) )) {
   muls = nbt_get_multiple(tag);
   printmul(&muls, depth + 1);
  } else {
   printval(tag, ctype, depth + 1);
  }
 }
 free(depthchars);
 return 1;
}

variable_types: depth int

variable_types:
length
unsigned long

variable_types:
i
unsigned long

variable_types:
depthchars
char *

variable_types: ctype char

variable_types: struct nbt_tag { char id; union nbt_payload { char byte_payload; short short_payload; int int_payload; long long_payload; float float_payload; double double_payload; struct nbt_byte_array { int size; char *bytes; } byte_array_payload; struct nbt_string { short length; char *chars; } string_payload; struct nbt_list { char tag_id; int size; union nbt_payload *tags; } list_payload; struct nbt_tag *compound_payload; struct nbt_int_array { int size; int *ints; } int_array_payload; } payload; unsigned long position; struct nbt_string name; }


printval

decompiled_code:
__int64 __fastcall printval(__int64 a1, char a2, unsigned int a3, double a4)
{
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
const char *v8; // rax

if ( (unsigned __int8)(a2 - 1) <= 3u )
{
v7 = nbt_get_integer();
printf("%ld\n", v7);
result = 1LL;
}
else if ( (unsigned __int8)(a2 - 5) <= 1u )
{
nbt_get_real();
printf("%f\n", a4);
result = 1LL;
}
else if ( a2 == 8 )
{
v8 = (const char *)nbt_get_string();
puts(v8);
result = 1LL;
}
else if ( (unsigned __int8)(a2 - 9) <= 2u || a2 == 7 )
{
result = printmul(a1, a3);
}
else if ( a2 == 127 )
{
v5 = nbt_get_tag();
result = printtag(v5, a3);
}
else
{
result = 4294967290LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, char a2, unsigned int a3, double a4)
{
__int64 v5; // rax
__int64 result; // rax
__int64 v7; // rax
const char *v8; // rax

if ( (unsigned __int8)(a2 - 1) <= 3u )
{
v7 = nbt_get_integer();
printf("%ld\n", v7);
result = 1LL;
}
else if ( (unsigned __int8)(a2 - 5) <= 1u )
{
nbt_get_real();
printf("%f\n", a4);
result = 1LL;
}
else if ( a2 == 8 )
{
v8 = (const char *)nbt_get_string();
puts(v8);
result = 1LL;
}
else if ( (unsigned __int8)(a2 - 9) <= 2u || a2 == 7 )
{
result = func1(a1, a3);
}
else if ( a2 == 127 )
{
v5 = nbt_get_tag();
result = func3(v5, a3);
}
else
{
result = 4294967290LL;
}
return result;
}

original_code:
static int printval(nbt_tag *tag, nbt_byte type, int depth)
{
 if (( (type >= 1) && (type <= 4) )) {
  printf("%ld\n", nbt_get_integer(tag));
 } else if (( (type == 5) || (type == 6) )) {
  printf("%f\n", nbt_get_real(tag));
 } else if ((type == 8)) {
  printf("%s\n", nbt_get_string(tag));
 } else if (( (type == 7) || ( (type >= 9) && (type <= 11) ) )) {
  return printmul(tag, depth);
 } else if ((type == 127)) {
  return printtag(nbt_get_tag(tag), depth);
 } else {
  return -6;
 }
 return 1;
}

canonical_original_code:
static int printval(struct nbt_tag *tag, char type, int depth)
{
 if (( (type >= 1) && (type <= 4) )) {
  printf("%ld\n", nbt_get_integer(tag));
 } else if (( (type == 5) || (type == 6) )) {
  printf("%f\n", nbt_get_real(tag));
 } else if ((type == 8)) {
  printf("%s\n", nbt_get_string(tag));
 } else if (( (type == 7) || ( (type >= 9) && (type <= 11) ) )) {
  return printmul(tag, depth);
 } else if ((type == 127)) {
  return printtag(nbt_get_tag(tag), depth);
 } else {
  return -6;
 }
 return 1;
}

variable_types: type char


printtag

decompiled_code:
__int64 __fastcall printtag(unsigned __int8 *a1, unsigned int a2, double a3)
{
char *v3; // rax
const char *v4; // rdx

nbt_get_name();
v3 = nbt_type_name(*a1);
printf("[%s] %s: ", v3, v4);
return printval((__int64)a1, *a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, unsigned int a2, double a3)
{
char *v3; // rax
const char *v4; // rdx

nbt_get_name();
v3 = func0(*a1);
printf("[%s] %s: ", v3, v4);
return func2((__int64)a1, *a1, a2, a3);
}

original_code:
static int printtag(nbt_tag *tag, int depth)
{
 printf("[%s] %s: ", nbt_type_name(tag->id), nbt_get_name(tag));
 return printval(tag, tag->id, depth);
}

canonical_original_code:
static int printtag(struct nbt_tag *tag, int depth)
{
 printf("[%s] %s: ", nbt_type_name(tag->id), nbt_get_name(tag));
 return printval(tag, tag->id, depth);
}


decompiled_code:
__int64 __fastcall nbt_print(unsigned __int8 *a1, double a2)
{
__int64 result; // rax

if ( a1 )
result = printtag(a1, 0, a2);
else
result = 4294967291LL;
return result;
}

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

if ( a1 )
result = func3(a1, 0, a2);
else
result = 4294967291LL;
return result;
}

original_code:
int nbt_print(nbt_tag *tag)
{
 if (!tag) {
  return -5;
 }
 return printtag(tag, 0);
}

canonical_original_code:
int nbt_print(struct nbt_tag *tag)
{
 if (!tag) {
  return -5;
 }
 return printtag(tag, 0);
}


main

canonical_name: main

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

display_add_log(&unk_19128, argv, envp);
if ( !(unsigned int)display_init() )
{
snake_start();
display_add_log(&unk_19142, argv, v4);
display_finalize();
display_add_log("koniec programu", argv, v5);
pthread_exit(0LL);
}
return -1;
}

original_code:
int main() {
 display_add_log("inicjowanie wyświetlacza");
 if (display_init()) {
  return -1;
 }
 snake_start();
 display_add_log("zwalnianie zasobów...");
 display_finalize();
 display_add_log("koniec programu");
 pthread_exit(((void *)0));
}


decompiled_code:
__int64 __fastcall snake_crash(__int64 a1, _BYTE *a2, pthread_t *a3)
{
char v3; // al
unsigned int v4; // er12

v3 = *a2 & 0xDF;
if ( v3 == 81 )
{
v4 = 1;
display_add_log(&unk_1A088);
pthread_join(*a3, 0LL);
snake_finish = 1;
if ( snake_pause )
pthread_cond_broadcast(&step_cond);
return v4;
}
if ( v3 != 80 )
{
if ( v3 != 82 )
{
LABEL_10:
v4 = 0;
if ( v3 != 83 )
return v4;
v4 = snake_pause;
if ( !snake_pause )
return v4;
goto LABEL_12;
}
v4 = snake_pause;
if ( !snake_pause )
return v4;
LABEL_9:
snake_pause = 0;
pthread_cond_broadcast(&step_cond);
v3 = *a2 & 0xDF;
goto LABEL_10;
}
snake_pause = 1;
if ( (*a2 & 0xDF) == 82 )
goto LABEL_9;
if ( (*a2 & 0xDF) != 83 )
return 0LL;
LABEL_12:
*a2 = 0;
pthread_cond_signal(&step_cond);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _BYTE *a2, pthread_t *a3)
{
char v3; // al
unsigned int v4; // er12

v3 = *a2 & 0xDF;
if ( v3 == 81 )
{
v4 = 1;
display_add_log(&unk_1A088);
pthread_join(*a3, 0LL);
snake_finish = 1;
if ( snake_pause )
pthread_cond_broadcast(&step_cond);
return v4;
}
if ( v3 != 80 )
{
if ( v3 != 82 )
{
LABEL_10:
v4 = 0;
if ( v3 != 83 )
return v4;
v4 = snake_pause;
if ( !snake_pause )
return v4;
goto LABEL_12;
}
v4 = snake_pause;
if ( !snake_pause )
return v4;
LABEL_9:
snake_pause = 0;
pthread_cond_broadcast(&step_cond);
v3 = *a2 & 0xDF;
goto LABEL_10;
}
snake_pause = 1;
if ( (*a2 & 0xDF) == 82 )
goto LABEL_9;
if ( (*a2 & 0xDF) != 83 )
return 0LL;
LABEL_12:
*a2 = 0;
pthread_cond_signal(&step_cond);
return 0LL;
}

original_code:
int snake_crash(Snake *s, char *c, pthread_t *kt) {
 if (*c == 'q' || *c == 'Q') {
  display_add_log("przerwanie programu przez użytkownika...");
  pthread_join(*kt, 0);
  snake_finish = 1;
  if (snake_pause) {
   pthread_cond_broadcast(&step_cond);
  }
  return 1;
 }
 if (*c == 'p' || *c == 'P') {
  snake_pause = 1;
 }
 if (*c == 'r' || *c == 'R') {
  if (snake_pause) {
   snake_pause = 0;
   pthread_cond_broadcast(&step_cond);
  }
 }
 if (*c == 's' || *c == 'S') {
  if (snake_pause) {
   *c = 0;
   pthread_cond_signal(&step_cond);
  }
 }
 return 0;
}

canonical_original_code:
int snake_crash(struct Snake *s, char *c, unsigned long *kt) {
 if (*c == 'q' || *c == 'Q') {
  display_add_log("przerwanie programu przez użytkownika...");
  pthread_join(*kt, 0);
  snake_finish = 1;
  if (snake_pause) {
   pthread_cond_broadcast(&step_cond);
  }
  return 1;
 }
 if (*c == 'p' || *c == 'P') {
  snake_pause = 1;
 }
 if (*c == 'r' || *c == 'R') {
  if (snake_pause) {
   snake_pause = 0;
   pthread_cond_broadcast(&step_cond);
  }
 }
 if (*c == 's' || *c == 'S') {
  if (snake_pause) {
   *c = 0;
   pthread_cond_signal(&step_cond);
  }
 }
 return 0;
}

variable_types: struct Snake { int id; char label[2]; int x[100]; int y[100]; int steps; int isize; int size; int turn; int alive; char dir; int color; int team; int grow; } *

variable_types:
c
char *

variable_types:
kt
unsigned long *


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

if ( a3[1] == a1[102] && *a3 == a1[2] )
{
result = food_put(a3);
a1[210] = 1;
}
else
{
result = (unsigned int)a1[203];
if ( (_DWORD)result )
{
a1[210] = 1;
result = (unsigned int)(result - 1);
a1[203] = result;
}
}
return result;
}

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

if ( a3[1] == a1[102] && *a3 == a1[2] )
{
result = food_put(a3);
a1[210] = 1;
}
else
{
result = (unsigned int)a1[203];
if ( (_DWORD)result )
{
a1[210] = 1;
result = (unsigned int)(result - 1);
a1[203] = result;
}
}
return result;
}

original_code:
void snake_eat_and_grow(Snake *s, Board *b, Food *f) {
 int x = s->x[0];
 int y = s->y[0];
 if (f->y == y && f->x == x) {
  food_put(f, b);
  s->grow = 1;
 } else {
  if (s->isize) {
   --s->isize;
   s->grow = 1;
  }
 }
}

canonical_original_code:
void snake_eat_and_grow(struct Snake *s, struct Board *b, struct Food *f) {
 int x = s->x[0];
 int y = s->y[0];
 if (f->y == y && f->x == x) {
  food_put(f, b);
  s->grow = 1;
 } else {
  if (s->isize) {
   --s->isize;
   s->grow = 1;
  }
 }
}

variable_types: struct Board { int field[24][80]; int score[2]; int best_score[2]; } *

variable_types:
f
struct Food { int x; } *

variable_types: x int

variable_types: y int

return_type: void

user_defined_types: struct Snake { int id; char label[2]; int x[100]; int y[100]; int steps; int isize; int size; int turn; int alive; char dir; int color; int team; int grow; }


original_code:
int snake_check_head(Snake *snake, Board *b) {
 int i;
 int ib = (snake->team ? 20/2 : 0);
 int ie = (snake->team ? 20 : 20/2);
 int x = snake->x[0];
 int y = snake->y[0];
 int sx, sy;
 for (i=ib; i<ie; ++i) {
  if (i != snake->id) {
   if (s[i].alive == 1 && pthread_mutex_trylock(&snake_mutex[i]) == 0) {
    sx = s[i].x[0];
    sy = s[i].y[0];
    if ((sx == x && sy == y-1) || (sx == x && sy == y+1) ||
     (sx == x+1 && sy == y) || (sx == x-1 && sy == y)) {
     snake->steps = 1;
     pthread_mutex_unlock(&snake_mutex[i]);
     return 1;
    }
    pthread_mutex_unlock(&snake_mutex[i]);
   }
  }
 }
 return 0;
}

canonical_original_code:
int snake_check_head(struct Snake *snake, struct Board *b) {
 int i;
 int ib = (snake->team ? 20/2 : 0);
 int ie = (snake->team ? 20 : 20/2);
 int x = snake->x[0];
 int y = snake->y[0];
 int sx; int sy;
 for (i=ib; i<ie; ++i) {
  if (i != snake->id) {
   if (s[i].alive == 1 && pthread_mutex_trylock(&snake_mutex[i]) == 0) {
    sx = s[i].x[0];
    sy = s[i].y[0];
    if ((sx == x && sy == y-1) || (sx == x && sy == y+1) ||
     (sx == x+1 && sy == y) || (sx == x-1 && sy == y)) {
     snake->steps = 1;
     pthread_mutex_unlock(&snake_mutex[i]);
     return 1;
    }
    pthread_mutex_unlock(&snake_mutex[i]);
   }
  }
 }
 return 0;
}

variable_types: i int

variable_types: ib int

variable_types: ie int

variable_types: sx int

variable_types: sy int

user_defined_types: struct Board { int field[24][80]; int score[2]; int best_score[2]; }


decompiled_code:
int __fastcall snake_cut_tail(__int64 a1, __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // er12
unsigned int v4; // ebp

v2 = *(int *)(a1 + 816);
v3 = *(_DWORD *)(a1 + 4 * v2 + 8);
v4 = *(_DWORD *)(a1 + 4 * v2 + 408);
pthread_mutex_lock(&field_mutex[80 * (int)v4 + (int)v3]);
board_set_field(a2, v3, v4, 2LL);
pthread_mutex_lock(&board_mutex);
wattr_on(stdscr, 0x200uLL, 0LL);
mvprintw(v4, v3, " ");
wattr_off(stdscr, 0x200uLL, 0LL);
return pthread_mutex_unlock(&board_mutex);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // er12
unsigned int v4; // ebp

v2 = *(int *)(a1 + 816);
v3 = *(_DWORD *)(a1 + 4 * v2 + 8);
v4 = *(_DWORD *)(a1 + 4 * v2 + 408);
pthread_mutex_lock(&field_mutex[80 * (int)v4 + (int)v3]);
board_set_field(a2, v3, v4, 2LL);
pthread_mutex_lock(&board_mutex);
wattr_on(stdscr, 0x200uLL, 0LL);
mvprintw(v4, v3, " ");
wattr_off(stdscr, 0x200uLL, 0LL);
return pthread_mutex_unlock(&board_mutex);
}

original_code:
void snake_cut_tail(Snake *s, Board *b) {
 int x = s->x[s->size];
 int y = s->y[s->size];
 pthread_mutex_lock(&field_mutex[y][x]);
 board_set_field(b, x, y, 2);
 pthread_mutex_lock(&board_mutex);
 wattr_on(stdscr, (attr_t)(((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0)); mvprintw(y, x, " "); wattr_off(stdscr, (attr_t)(((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
 pthread_mutex_unlock(&board_mutex);
}

canonical_original_code:
void snake_cut_tail(struct Snake *s, struct Board *b) {
 int x = s->x[s->size];
 int y = s->y[s->size];
 pthread_mutex_lock(&field_mutex[y][x]);
 board_set_field(b, x, y, 2);
 pthread_mutex_lock(&board_mutex);
 wattr_on(stdscr, (attr_t)(((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0)); mvprintw(y, x, " "); wattr_off(stdscr, (attr_t)(((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
 pthread_mutex_unlock(&board_mutex);
}


decompiled_code:
__int64 __fastcall snake_change_size(_DWORD *a1, __int64 a2, int a3)
{
__int64 v4; // rax
int v5; // ebx
int v6; // eax
__int64 result; // rax

a1[204] += a3;
a1[202] = 0;
pthread_mutex_lock(&score_mutex);
v4 = a2 + 4LL * (int)a1[209];
v5 = *(_DWORD *)(v4 + 7680) + a3;
*(_DWORD *)(v4 + 7680) = v5;
if ( v5 > *(_DWORD *)(v4 + 7688) )
*(_DWORD *)(v4 + 7688) = v5;
pthread_mutex_unlock(&score_mutex);
v6 = a1[204];
if ( v6 <= 0 )
{
a1[206] = 0;
result = 1LL;
}
else if ( v6 == 100 )
{
a1[204] = 99;
result = 0LL;
}
else
{
result = 1LL;
}
return result;
}

original_code:
int snake_change_size(Snake *s, Board *b, int x) {
 s->size += x;
 s->steps = 0;
 pthread_mutex_lock(&score_mutex);
 b->score[s->team] += x;
 if (b->score[s->team] > b->best_score[s->team]) {
  b->best_score[s->team] = b->score[s->team];
 }
 pthread_mutex_unlock(&score_mutex);
 if (s->size < 1) {
  s->alive = 0;
  return 1;
 }
 if (s->size == 100) {
  s->size = 100 -1;
  return 0;
 }
 return 1;
}

canonical_original_code:
int snake_change_size(struct Snake *s, struct Board *b, int x) {
 s->size += x;
 s->steps = 0;
 pthread_mutex_lock(&score_mutex);
 b->score[s->team] += x;
 if (b->score[s->team] > b->best_score[s->team]) {
  b->best_score[s->team] = b->score[s->team];
 }
 pthread_mutex_unlock(&score_mutex);
 if (s->size < 1) {
  s->alive = 0;
  return 1;
 }
 if (s->size == 100) {
  s->size = 100 -1;
  return 0;
 }
 return 1;
}


original_code:
void snake_init(Snake *s, Board *b, int id) {
 s->id = id;
 s->label[0] = 'a'+id;
 s->label[1] = 0;
 s->isize = 4;
 s->size = 0;
 s->steps = 0;
 s->turn = 1;
 s->alive = 1;
 s->dir = 3;
 s->team = (id >= 20/2);
 s->color = (s->team ? 6 : 5);
 s->grow = 0;
 board_rand_bg(b, s->x, s->y);
 board_set_field(b, s->x[0], s->y[0], s->color);
 snake_change_size(s, b, 1);
}

canonical_original_code:
void snake_init(struct Snake *s, struct Board *b, int id) {
 s->id = id;
 s->label[0] = 'a'+id;
 s->label[1] = 0;
 s->isize = 4;
 s->size = 0;
 s->steps = 0;
 s->turn = 1;
 s->alive = 1;
 s->dir = 3;
 s->team = (id >= 20/2);
 s->color = (s->team ? 6 : 5);
 s->grow = 0;
 board_rand_bg(b, s->x, s->y);
 board_set_field(b, s->x[0], s->y[0], s->color);
 snake_change_size(s, b, 1);
}

variable_types: id int


variable_types: tx int

variable_types: ty int


decompiled_code:
__int64 __fastcall snake_check_tail(_DWORD *a1, __int64 a2, int a3, int a4)
{
__int64 v5; // rbx
__int64 v6; // rbp
int v7; // edx
unsigned int v8; // er13
int v9; // eax
__int64 v10; // rax
pthread_mutex_t *v11; // rdi
int v13; // [rsp+Ch] [rbp-4Ch]

v5 = a1[209] == 0 ? 0xA : 0;
v6 = 211 * v5;
v7 = a1[209] == 0 ? 20 : 10;
while ( 1 )
{
while ( 1 )
{
if ( *a1 != (_DWORD)v5 )
{
v8 = s[v6 + 206];
if ( v8 == 1 )
{
v13 = v7;
v9 = pthread_mutex_trylock(&snake_mutex + v5);
v7 = v13;
if ( !v9 )
break;
}
}
++v5;
v6 += 211LL;
if ( v7 <= (int)v5 )
return 0;
}
v10 = s[v6 + 204] - 1 + 211LL * (int)v5;
if ( s[v10 + 2] == a3 && s[v10 + 102] == a4 )
break;
v11 = &snake_mutex + v5++;
v6 += 211LL;
pthread_mutex_unlock(v11);
v7 = v13;
if ( v13 <= (int)v5 )
return 0;
}
snake_change_size(&s[211 * v5], a2, -1);
a1[210] = 1;
pthread_mutex_unlock(&snake_mutex + v5);
return v8;
}

original_code:
int snake_check_tail(Snake *snake, Board *b, int x, int y) {
 int i;
 int ib = (snake->team ? 0 : 20/2);
 int ie = (snake->team ? 20/2 : 20);
 int last;
 for (i=ib; i<ie; ++i) {
  if (i != snake->id) {
   if (s[i].alive == 1 && pthread_mutex_trylock(&snake_mutex[i]) == 0) {
    last = s[i].size-1;
    if (s[i].x[last] == x && s[i].y[last] == y) {
     snake_change_size(&s[i], b, -1);
     snake->grow = 1;
     pthread_mutex_unlock(&snake_mutex[i]);
     return 1;
    }
    pthread_mutex_unlock(&snake_mutex[i]);
   }
  }
 }
 return 0;
}

canonical_original_code:
int snake_check_tail(struct Snake *snake, struct Board *b, int x, int y) {
 int i;
 int ib = (snake->team ? 0 : 20/2);
 int ie = (snake->team ? 20/2 : 20);
 int last;
 for (i=ib; i<ie; ++i) {
  if (i != snake->id) {
   if (s[i].alive == 1 && pthread_mutex_trylock(&snake_mutex[i]) == 0) {
    last = s[i].size-1;
    if (s[i].x[last] == x && s[i].y[last] == y) {
     snake_change_size(&s[i], b, -1);
     snake->grow = 1;
     pthread_mutex_unlock(&snake_mutex[i]);
     return 1;
    }
    pthread_mutex_unlock(&snake_mutex[i]);
   }
  }
 }
 return 0;
}

variable_types: last int


decompiled_code:
__int64 __fastcall snake_rate_direction(_DWORD *a1, __int64 a2, __int64 a3, int a4, int a5, int a6, pthread_mutex_t **a7, _DWORD *a8, int *a9, int a10)
{
pthread_mutex_t *v12; // r13
int v13; // er12
int v16; // [rsp+Ch] [rbp-3Ch]

if ( *a9 == 4
|| (v12 = &field_mutex[80 * a5 + a4],
v16 = *(_DWORD *)(a2 + 4 * (a4 + 80LL * a5)),
(v13 = pthread_mutex_trylock(v12)) != 0) )
{
v13 = 0;
}
else
{
if ( v16 <= 2 )
{
v13 = a6 + 1;
}
else if ( (a1[209] == 0) + 5 == v16 )
{
v13 = 4 * snake_check_tail(a1, a2, a4, a5);
}
if ( *a9 >= v13 )
{
pthread_mutex_unlock(v12);
}
else
{
if ( *a7 )
pthread_mutex_unlock(*a7);
*a8 = a10;
*a9 = v13;
*a7 = v12;
}
}
return (unsigned int)v13;
}

original_code:
int snake_rate_direction(Snake *s, Board *b, Food *f, int x, int y, int fd, pthread_mutex_t **m, int *best, int *best_result, int id) {
 int v = b->field[y][x];
 int r;
 if (*best_result == 4) {
  return 0;
 }
 if (pthread_mutex_trylock(&field_mutex[y][x]) == 0) {
  r = v <= 2 ?
   fd+1 :
   (v == (s->team ? 5 : 6) ?
    snake_check_tail(s, b, x, y)*4 :
    0);
  if (r > *best_result) {
   if (*m != 0) {
    pthread_mutex_unlock(*m);
   }
   *best = id;
   *best_result = r;
   *m = &field_mutex[y][x];
  } else {
   pthread_mutex_unlock(&field_mutex[y][x]);
  }
  return r;
 }
 return 0;
}

canonical_original_code:
int snake_rate_direction(struct Snake *s, struct Board *b, struct Food *f, int x, int y, int fd, union pthread_mutex_t **m, int *best, int *best_result, int id) {
 int v = b->field[y][x];
 int r;
 if (*best_result == 4) {
  return 0;
 }
 if (pthread_mutex_trylock(&field_mutex[y][x]) == 0) {
  r = v <= 2 ?
   fd+1 :
   (v == (s->team ? 5 : 6) ?
    snake_check_tail(s, b, x, y)*4 :
    0);
  if (r > *best_result) {
   if (*m != 0) {
    pthread_mutex_unlock(*m);
   }
   *best = id;
   *best_result = r;
   *m = &field_mutex[y][x];
  } else {
   pthread_mutex_unlock(&field_mutex[y][x]);
  }
  return r;
 }
 return 0;
}

variable_types: fd int

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } **

variable_types:
best
int *

variable_types: v int

variable_types: r int

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

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


decompiled_code:
__int64 __fastcall snake_check_direction(_DWORD *a1, __int64 a2, int *a3, char a4, pthread_mutex_t **a5, _DWORD *a6, int *a7, int a8)
{
int v9; // er10
int v11; // er8
int v12; // ecx
int v13; // er9

v9 = a1[2];
v11 = a1[102];
if ( a4 == 2 )
return snake_rate_direction(a1, a2, (__int64)a3, v9, v11 + 1, a3[1] > v11, a5, a6, a7, a8);
if ( a4 > 2 )
{
if ( a4 == 3 )
{
v12 = v9 - 1;
v13 = *a3 < v9;
return snake_rate_direction(a1, a2, (__int64)a3, v12, v11, v13, a5, a6, a7, a8);
}
return 0LL;
}
if ( a4 )
{
if ( a4 == 1 )
{
v12 = v9 + 1;
v13 = *a3 > v9;
return snake_rate_direction(a1, a2, (__int64)a3, v12, v11, v13, a5, a6, a7, a8);
}
return 0LL;
}
return snake_rate_direction(a1, a2, (__int64)a3, v9, v11 - 1, a3[1] < v11, a5, a6, a7, a8);
}

original_code:
int snake_check_direction(Snake *s, Board *b, Food *f, char dir, pthread_mutex_t **m, int *best, int *best_result, int id) {
 int x = s->x[0];
 int y = s->y[0];
 switch (dir) {
  case 0:
   return snake_rate_direction(s, b, f, x, y-1, f->y < y, m, best, best_result, id);
  case 1:
   return snake_rate_direction(s, b, f, x+1, y, f->x > x, m, best, best_result, id);
  case 2:
   return snake_rate_direction(s, b, f, x, y+1, f->y > y, m, best, best_result, id);
  case 3:
   return snake_rate_direction(s, b, f, x-1, y, f->x < x, m, best, best_result, id);
 }
 return 0;
}

canonical_original_code:
int snake_check_direction(struct Snake *s, struct Board *b, struct Food *f, char dir, union pthread_mutex_t **m, int *best, int *best_result, int id) {
 int x = s->x[0];
 int y = s->y[0];
 switch (dir) {
  case 0:
   return snake_rate_direction(s, b, f, x, y-1, f->y < y, m, best, best_result, id);
  case 1:
   return snake_rate_direction(s, b, f, x+1, y, f->x > x, m, best, best_result, id);
  case 2:
   return snake_rate_direction(s, b, f, x, y+1, f->y > y, m, best, best_result, id);
  case 3:
   return snake_rate_direction(s, b, f, x-1, y, f->x < x, m, best, best_result, id);
 }
 return 0;
}

variable_types: struct Snake { int id; char label[2]; int x[100]; int y[100]; int steps; int isize; int size; int turn; int alive; char dir; int color; int team; int grow; } *

variable_types: struct Board { int field[24][80]; int score[2]; int best_score[2]; } *

variable_types:
f
struct Food { int x; } *

variable_types: dir char

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


original_code:
void snake_reverse(Snake *s) {
 int i, i2;
 int n = s->size;
 for (i=0; i<n/2; ++i) {
  i2 = n-i-1;
  swap(&s->y[i], &s->y[i2]);
  swap(&s->x[i], &s->x[i2]);
 }
}

canonical_original_code:
void snake_reverse(struct Snake *s) {
 int i; int i2;
 int n = s->size;
 for (i=0; i<n/2; ++i) {
  i2 = n-i-1;
  swap(&s->y[i], &s->y[i2]);
  swap(&s->x[i], &s->x[i2]);
 }
}

variable_types: i int

variable_types: n int


original_code:
int snake_decide(Snake *s, Board *b, Food *f) {
 int best = 3;
 int best_result = 0;
 int x, y;
 pthread_mutex_t *m = 0;
 snake_check_head(s, b);
 if (++s->steps > (80 +24)*20/8) {
  s->turn = -s->turn;
  if (snake_change_size(s, b, -1)) {
   snake_cut_tail(s, b);
   if (s->alive == 0) {
    pthread_exit(((void *)0));
   }
  }
 }
 x = s->x[0];
 y = s->y[0];
 snake_check_direction(s, b, f, pos_mod(s->dir+s->turn, 4), &m, &best, &best_result, 0);
 snake_check_direction(s, b, f, s->dir, &m, &best, &best_result, 1);
 snake_check_direction(s, b, f, pos_mod(s->dir-s->turn, 4), &m, &best, &best_result, 2);
 switch (best) {
  case 0:
   s->dir = (s->dir+s->turn+4)%4;
   s->turn = -s->turn;
   break;
  case 1:
   break;
  case 2:
   s->dir = (s->dir-s->turn+4)%4;
   break;
 }
 if (best < 3) {
  return 1;
 }
 snake_reverse(s);
 return 0;
}

canonical_original_code:
int snake_decide(struct Snake *s, struct Board *b, struct Food *f) {
 int best = 3;
 int best_result = 0;
 int x; int y;
 union pthread_mutex_t *m = 0;
 snake_check_head(s, b);
 if (++s->steps > (80 +24)*20/8) {
  s->turn = -s->turn;
  if (snake_change_size(s, b, -1)) {
   snake_cut_tail(s, b);
   if (s->alive == 0) {
    pthread_exit(((void *)0));
   }
  }
 }
 x = s->x[0];
 y = s->y[0];
 snake_check_direction(s, b, f, pos_mod(s->dir+s->turn, 4), &m, &best, &best_result, 0);
 snake_check_direction(s, b, f, s->dir, &m, &best, &best_result, 1);
 snake_check_direction(s, b, f, pos_mod(s->dir-s->turn, 4), &m, &best, &best_result, 2);
 switch (best) {
  case 0:
   s->dir = (s->dir+s->turn+4)%4;
   s->turn = -s->turn;
   break;
  case 1:
   break;
  case 2:
   s->dir = (s->dir-s->turn+4)%4;
   break;
 }
 if (best < 3) {
  return 1;
 }
 snake_reverse(s);
 return 0;
}

variable_types: best int

variable_types: x int

variable_types: y int

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *


decompiled_code:
void __fastcall __noreturn snake_thread(void *a1)
{
pthread_mutex_t *v1; // rbp
int *v2; // rbx

v1 = &snake_mutex + (int)a1;
pthread_mutex_lock(v1);
v2 = &s[211 * (int)a1];
snake_init((__int64)v2, (__int64)&b, (int)a1);
pthread_mutex_unlock(v1);
while ( !snake_finish )
{
if ( snake_pause )
{
pthread_cond_wait(&step_cond, &step_mutex);
pthread_mutex_unlock(&step_mutex);
}
pthread_mutex_lock(v1);
if ( !v2[206] )
break;
snake_eat_and_grow(v2, (__int64)&b, f);
if ( (unsigned int)snake_decide((__int64)v2, (__int64)&b, f) )
snake_move((__int64)v2, (__int64)&b);
pthread_mutex_unlock(v1);
board_show_score(&b);
pthread_mutex_lock(&board_mutex);
wrefresh(stdscr);
pthread_mutex_unlock(&board_mutex);
usleep(0x4E20u);
}
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func12(void *a1)
{
pthread_mutex_t *v1; // rbp
int *v2; // rbx

v1 = &snake_mutex + (int)a1;
pthread_mutex_lock(v1);
v2 = &s[211 * (int)a1];
func5((__int64)v2, (__int64)&b, (int)a1);
pthread_mutex_unlock(v1);
while ( !snake_finish )
{
if ( snake_pause )
{
pthread_cond_wait(&step_cond, &step_mutex);
pthread_mutex_unlock(&step_mutex);
}
pthread_mutex_lock(v1);
if ( !v2[206] )
break;
func1(v2, (__int64)&b, f);
if ( (unsigned int)func11((__int64)v2, (__int64)&b, f) )
func6((__int64)v2, (__int64)&b);
pthread_mutex_unlock(v1);
board_show_score(&b);
pthread_mutex_lock(&board_mutex);
wrefresh(stdscr);
pthread_mutex_unlock(&board_mutex);
usleep(0x4E20u);
}
pthread_exit(0LL);
}

original_code:
void *snake_thread(void *x) {
 int id = (int)x;
 pthread_mutex_lock(&snake_mutex[id]);
 snake_init(&s[id], &b, id);
 pthread_mutex_unlock(&snake_mutex[id]);
 while(!snake_finish) {
  if (snake_pause) {
   pthread_cond_wait(&step_cond, &step_mutex);
   pthread_mutex_unlock(&step_mutex);
  }
  pthread_mutex_lock(&snake_mutex[id]);
  if (s[id].alive != 0) {
   snake_eat_and_grow(&s[id], &b, &f);
   if (snake_decide(&s[id], &b, &f)) {
    snake_move(&s[id], &b, &f);
   }
  } else {
   pthread_exit(((void *)0));
  }
  pthread_mutex_unlock(&snake_mutex[id]);
  board_show_score(&b);
  pthread_mutex_lock(&board_mutex);
  wrefresh(stdscr);
  pthread_mutex_unlock(&board_mutex);
  usleep(20000);
 }
 pthread_exit(((void *)0));
}

variable_types:
x
void *

variable_types: id int

return_type: void *


decompiled_code:
int snake_init_mutex()
{
pthread_mutex_t *v0; // rbx
pthread_mutex_t *v1; // rdi

v0 = &snake_mutex;
do
{
v1 = v0++;
pthread_mutex_init(v1, 0LL);
}
while ( v0 != &snake_mutex + 20 );
pthread_mutex_init(&step_mutex, 0LL);
pthread_mutex_init(&crash_mutex, 0LL);
pthread_cond_init(&step_cond, 0LL);
return pthread_cond_init(&crash_cond, 0LL);
}

canonical_decompiled_code:
int func13()
{
pthread_mutex_t *v0; // rbx
pthread_mutex_t *v1; // rdi

v0 = &snake_mutex;
do
{
v1 = v0++;
pthread_mutex_init(v1, 0LL);
}
while ( v0 != &snake_mutex + 20 );
pthread_mutex_init(&step_mutex, 0LL);
pthread_mutex_init(&crash_mutex, 0LL);
pthread_cond_init(&step_cond, 0LL);
return pthread_cond_init(&crash_cond, 0LL);
}

original_code:
void snake_init_mutex() {
 int i;
 for (i=0; i<20; ++i) {
  pthread_mutex_init(&snake_mutex[i], ((void *)0));
 }
 pthread_mutex_init(&step_mutex, ((void *)0));
 pthread_mutex_init(&crash_mutex, ((void *)0));
 pthread_cond_init(&step_cond, ((void *)0));
 pthread_cond_init(&crash_cond, ((void *)0));
}


decompiled_code:
int snake_finalize_mutex()
{
pthread_mutex_t *v0; // rbx
pthread_mutex_t *v1; // rdi

v0 = &snake_mutex;
do
{
v1 = v0++;
pthread_mutex_destroy(v1);
}
while ( v0 != &snake_mutex + 20 );
pthread_mutex_destroy(&step_mutex);
pthread_cond_destroy(&step_cond);
pthread_mutex_destroy(&crash_mutex);
return pthread_cond_destroy(&crash_cond);
}

canonical_decompiled_code:
int func14()
{
pthread_mutex_t *v0; // rbx
pthread_mutex_t *v1; // rdi

v0 = &snake_mutex;
do
{
v1 = v0++;
pthread_mutex_destroy(v1);
}
while ( v0 != &snake_mutex + 20 );
pthread_mutex_destroy(&step_mutex);
pthread_cond_destroy(&step_cond);
pthread_mutex_destroy(&crash_mutex);
return pthread_cond_destroy(&crash_cond);
}

original_code:
void snake_finalize_mutex() {
 int i;
 for (i=0; i<20; ++i) {
  pthread_mutex_destroy(&snake_mutex[i]);
 }
 pthread_mutex_destroy(&step_mutex);
 pthread_cond_destroy(&step_cond);
 pthread_mutex_destroy(&crash_mutex);
 pthread_cond_destroy(&crash_cond);
}


decompiled_code:
int snake_start()
{
char *v0; // rbp
pthread_t *v1; // rbx
pthread_t *v2; // r12
pthread_t v3; // rdi
char arg; // [rsp+7h] [rbp-C1h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-C0h] BYREF
pthread_t v7[20]; // [rsp+10h] [rbp-B8h] BYREF
__int64 v8; // [rsp+B0h] [rbp-18h] BYREF

v0 = 0LL;
snake_finish = 0;
arg = 0;
v1 = v7;
snake_pause = 0;
v2 = v7;
snake_step = 0;
display_add_log(&unk_1A0B8);
pthread_create(&newthread, 0LL, (void *(*)(void *))&display_getch, &arg);
display_add_log("inicjacja mutexow...");
board_init_mutex();
snake_init_mutex();
display_add_log("inicjacja planszy...");
board_init(&b);
display_add_log("inicjacja jedzenia...");
food_init(f, &b);
display_add_log(&unk_1A11B);
do
pthread_create(v2++, 0LL, (void *(*)(void *))snake_thread, v0++);
while ( v0 != (_BYTE *)&loc_F + 5 );
display_add_log(&unk_1A131);
while ( !(unsigned int)snake_crash((__int64)s, &arg, &newthread) )
{
pthread_cond_wait(&crash_cond, &crash_mutex);
pthread_mutex_unlock(&crash_mutex);
}
display_add_log(&unk_1A149);
do
{
v3 = *v1++;
pthread_join(v3, 0LL);
}
while ( &v8 != (__int64 *)v1 );
display_add_log("niszczenie mutexow...");
board_finalize_mutex();
return snake_finalize_mutex();
}

canonical_decompiled_code:
int func15()
{
char *v0; // rbp
pthread_t *v1; // rbx
pthread_t *v2; // r12
pthread_t v3; // rdi
char arg; // [rsp+7h] [rbp-C1h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-C0h] BYREF
pthread_t v7[20]; // [rsp+10h] [rbp-B8h] BYREF
__int64 v8; // [rsp+B0h] [rbp-18h] BYREF

v0 = 0LL;
snake_finish = 0;
arg = 0;
v1 = v7;
snake_pause = 0;
v2 = v7;
snake_step = 0;
display_add_log(&unk_1A0B8);
pthread_create(&newthread, 0LL, (void *(*)(void *))&display_getch, &arg);
display_add_log("inicjacja mutexow...");
board_init_mutex();
func13();
display_add_log("inicjacja planszy...");
board_init(&b);
display_add_log("inicjacja jedzenia...");
food_init(f, &b);
display_add_log(&unk_1A11B);
do
pthread_create(v2++, 0LL, (void *(*)(void *))snake_thread, v0++);
while ( v0 != (_BYTE *)&loc_F + 5 );
display_add_log(&unk_1A131);
while ( !(unsigned int)func0((__int64)s, &arg, &newthread) )
{
pthread_cond_wait(&crash_cond, &crash_mutex);
pthread_mutex_unlock(&crash_mutex);
}
display_add_log(&unk_1A149);
do
{
v3 = *v1++;
pthread_join(v3, 0LL);
}
while ( &v8 != (__int64 *)v1 );
display_add_log("niszczenie mutexow...");
board_finalize_mutex();
return func14();
}

original_code:
void snake_start() {
 char c = 0;
 int i;
 snake_finish = 0;
 snake_pause = 0;
 snake_step = 0;
 pthread_t kt, st[20];
 display_add_log("startowanie wątku klawiatury...");
 pthread_create(&kt, ((void *)0), display_getch, (void *) &c);
 display_add_log("inicjacja mutexow...");
 board_init_mutex();
 snake_init_mutex();
 display_add_log("inicjacja planszy...");
 board_init(&b);
 display_add_log("inicjacja jedzenia...");
 food_init(&f, &b);
 display_add_log("startowanie węży...");
 for (i=0; i<20; ++i) {
  pthread_create(&st[i], ((void *)0), snake_thread, (void *) i);
 }
 display_add_log("wyświetlanie węży...");
 while (!snake_crash(s, &c, &kt)) {
  pthread_cond_wait(&crash_cond, &crash_mutex);
  pthread_mutex_unlock(&crash_mutex);
 }
 display_add_log("kończenie wątków węży...");
 for (i=0; i<20; ++i) {
  pthread_join(st[i], ((void *)0));
 }
 display_add_log("niszczenie mutexow...");
 board_finalize_mutex();
 snake_finalize_mutex();
}

canonical_original_code:
void snake_start() {
 char c = 0;
 int i;
 snake_finish = 0;
 snake_pause = 0;
 snake_step = 0;
 unsigned long kt; unsigned long st[20];
 display_add_log("startowanie wątku klawiatury...");
 pthread_create(&kt, ((void *)0), display_getch, (void *) &c);
 display_add_log("inicjacja mutexow...");
 board_init_mutex();
 snake_init_mutex();
 display_add_log("inicjacja planszy...");
 board_init(&b);
 display_add_log("inicjacja jedzenia...");
 food_init(&f, &b);
 display_add_log("startowanie węży...");
 for (i=0; i<20; ++i) {
  pthread_create(&st[i], ((void *)0), snake_thread, (void *) i);
 }
 display_add_log("wyświetlanie węży...");
 while (!snake_crash(s, &c, &kt)) {
  pthread_cond_wait(&crash_cond, &crash_mutex);
  pthread_mutex_unlock(&crash_mutex);
 }
 display_add_log("kończenie wątków węży...");
 for (i=0; i<20; ++i) {
  pthread_join(st[i], ((void *)0));
 }
 display_add_log("niszczenie mutexow...");
 board_finalize_mutex();
 snake_finalize_mutex();
}

variable_types: c char

variable_types:
kt
unsigned long

variable_types:
st
unsigned long [20]


decompiled_code:
int display_color_init()
{
init_pair(1, 0, 1);
init_pair(2, 0, 0);
init_pair(3, 0, 7);
init_pair(4, 0, 6);
init_pair(5, 7, 2);
return init_pair(6, 7, 5);
}

canonical_decompiled_code:
int func0()
{
init_pair(1, 0, 1);
init_pair(2, 0, 0);
init_pair(3, 0, 7);
init_pair(4, 0, 6);
init_pair(5, 7, 2);
return init_pair(6, 7, 5);
}

original_code:
void display_color_init() {
 init_pair(1, 0, 1);
 init_pair(2, 0, 0);
 init_pair(3, 0, 7);
 init_pair(4, 0, 6);
 init_pair(5, 7, 2);
 init_pair(6, 7, 5);
}


decompiled_code:
void __fastcall __noreturn display_getch(_BYTE *a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

do
{
read(0, buf, 1uLL);
*a1 = buf[0];
pthread_cond_signal(&crash_cond);
}
while ( (buf[0] & 0xDF) != 81 );
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(_BYTE *a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

do
{
read(0, buf, 1uLL);
*a1 = buf[0];
pthread_cond_signal(&crash_cond);
}
while ( (buf[0] & 0xDF) != 81 );
pthread_exit(0LL);
}

original_code:
void *display_getch(void *c) {
 char tmp;
 do {
  read(0, &tmp, 1);
  *((char*)c) = tmp;
  pthread_cond_signal(&crash_cond);
 } while (tmp != 'q' && tmp != 'Q');
 pthread_exit(0);
}

canonical_original_code:
void *display_getch(void *c) {
 char tmp;
 do {
  read(0, &tmp, 1);
  *((char *)c) = tmp;
  pthread_cond_signal(&crash_cond);
 } while (tmp != 'q' && tmp != 'Q');
 pthread_exit(0);
}

variable_types:
c
void *

variable_types: tmp char


decompiled_code:
// attributes: thunk
int display_finalize(void)
{
return endwin();
}

canonical_decompiled_code:
int func2(void)
{
return endwin();
}

original_code:
void display_finalize() {
 endwin();
}

canonical_original_code:
void display_finalize() {
 endwin();
}


decompiled_code:
int display_init_log()
{
return system("rm -f log.txt");
}

canonical_decompiled_code:
int func3()
{
return system("rm -f log.txt");
}

original_code:
void display_init_log() {
 system("rm -f log.txt");
}


decompiled_code:
__int64 display_init()
{
unsigned int v0; // eax

initscr();
noecho();
cbreak();
curs_set(0);
start_color();
display_color_init();
v0 = time(0LL);
srand(v0);
display_init_log();
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // eax

initscr();
noecho();
cbreak();
curs_set(0);
start_color();
func0();
v0 = time(0LL);
srand(v0);
func3();
return 0LL;
}

original_code:
int display_init() {
 initscr();
 noecho();
 cbreak();
 curs_set(0);
 start_color();
 display_color_init();
 srand(time(((void *)0)));
 display_init_log();
 return 0;
}


decompiled_code:
int __fastcall display_add_log(const char *a1)
{
char v2[120]; // [rsp+0h] [rbp-78h] BYREF

sprintf(v2, "echo \"%s\" >> log.txt", a1);
return system(v2);
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
char v2[120]; // [rsp+0h] [rbp-78h] BYREF

sprintf(v2, "echo \"%s\" >> log.txt", a1);
return system(v2);
}

original_code:
void display_add_log(const char *str) {
 char cmd[100];
 sprintf(cmd, "echo \"%s\" >> log.txt", str);
 system(cmd);
}

canonical_original_code:
void display_add_log(char *str) {
 char cmd[100];
 sprintf(cmd, "echo \"%s\" >> log.txt", str);
 system(cmd);
}

variable_types:
str
char *

variable_types:
cmd
char [100]


decompiled_code:
__int64 __fastcall pos_mod(int a1, int a2)
{
return (unsigned int)((a1 + a2) % a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
return (unsigned int)((a1 + a2) % a2);
}

original_code:
int pos_mod(int x, int m) {
 return (x+m)%m;
}

variable_types: m int


decompiled_code:
_BOOL8 __fastcall rand_shot(int a1)
{
return rand() % a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int a1)
{
return rand() % a1 == 0;
}

original_code:
int rand_shot(int p) {
 return rand()%p == 0;
}

variable_types: p int


swap

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

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

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

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

original_code:
void swap(int *x, int *y) {
 int t;
 t = *x;
 *x = *y;
 *y = t;
}

variable_types:
x
int *

variable_types:
y
int *

variable_types: t int


decompiled_code:
int __fastcall food_draw(int *a1)
{
wattr_on(stdscr, 0x100uLL, 0LL);
mvprintw(a1[1], *a1, " ");
return wattr_off(stdscr, 0x100uLL, 0LL);
}

canonical_decompiled_code:
int __fastcall func0(int *a1)
{
wattr_on(stdscr, 0x100uLL, 0LL);
mvprintw(a1[1], *a1, " ");
return wattr_off(stdscr, 0x100uLL, 0LL);
}

variable_types:
f
struct Food { int x; } *


decompiled_code:
int __fastcall food_put(int *a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rdx
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

board_rand_bg(a2, &v6, v7);
v3 = v6;
v4 = v7[0];
*a1 = v6;
a1[1] = v4;
board_set_field(a2, v3, v4, 1LL);
pthread_mutex_lock(&board_mutex);
food_draw(a1);
return pthread_mutex_unlock(&board_mutex);
}

canonical_decompiled_code:
int __fastcall func1(int *a1, __int64 a2)
{
__int64 v3; // rsi
__int64 v4; // rdx
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

board_rand_bg(a2, &v6, v7);
v3 = v6;
v4 = v7[0];
*a1 = v6;
a1[1] = v4;
board_set_field(a2, v3, v4, 1LL);
pthread_mutex_lock(&board_mutex);
func0(a1);
return pthread_mutex_unlock(&board_mutex);
}

original_code:
void food_put(Food *f, Board *b) {
 int x, y;
 board_rand_bg(b, &x, &y);
 f->x = x;
 f->y = y;
 board_set_field(b, x, y, 1);
 pthread_mutex_lock(&board_mutex);
 food_draw(f);
 pthread_mutex_unlock(&board_mutex);
}

canonical_original_code:
void food_put(struct Food *f, struct Board *b) {
 int x; int y;
 board_rand_bg(b, &x, &y);
 f->x = x;
 f->y = y;
 board_set_field(b, x, y, 1);
 pthread_mutex_lock(&board_mutex);
 food_draw(f);
 pthread_mutex_unlock(&board_mutex);
}

variable_types: struct Board { int field[24][80]; int score[2]; int best_score[2]; } *

variable_types: x int

variable_types: y int


decompiled_code:
// attributes: thunk
int __fastcall food_init(int *a1, __int64 a2)
{
return food_put(a1, a2);
}

canonical_decompiled_code:
int __fastcall func2(int *a1, __int64 a2)
{
return func1(a1, a2);
}

original_code:
void food_init(Food *f, Board *b) {
 food_put(f, b);
}

canonical_original_code:
void food_init(struct Food *f, struct Board *b) {
 food_put(f, b);
}


decompiled_code:
int __fastcall board_draw(__int64 a1)
{
int i; // ebp
__int64 j; // rbx
int v4; // esi
int result; // eax

for ( i = 0; i != 24; ++i )
{
for ( j = 0LL; j != 80; ++j )
{
wattr_on(stdscr, (unsigned __int16)((unsigned __int16)*(_DWORD *)(a1 + 4 * j) << 8), 0LL);
mvprintw(i, j, " ");
v4 = *(_DWORD *)(a1 + 4 * j);
result = wattr_off(stdscr, (unsigned __int16)((_WORD)v4 << 8), 0LL);
}
a1 += 320LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int i; // ebp
__int64 j; // rbx
int v4; // esi
int result; // eax

for ( i = 0; i != 24; ++i )
{
for ( j = 0LL; j != 80; ++j )
{
wattr_on(stdscr, (unsigned __int16)((unsigned __int16)*(_DWORD *)(a1 + 4 * j) << 8), 0LL);
mvprintw(i, j, " ");
v4 = *(_DWORD *)(a1 + 4 * j);
result = wattr_off(stdscr, (unsigned __int16)((_WORD)v4 << 8), 0LL);
}
a1 += 320LL;
}
return result;
}

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall board_rand_bg(__int64 a1, int *a2, int *a3)
{
int v4; // eax
__int64 v5; // rcx
__int64 result; // rax

while ( 1 )
{
do
{
*a2 = rand() % 78 + 1;
v4 = rand() % 22 + 1;
*a3 = v4;
}
while ( pthread_mutex_trylock(&field_mutex[80 * v4 + *a2]) );
v5 = *a2;
result = v5 + 80LL * *a3;
if ( *(_DWORD *)(a1 + 4 * result) == 2 )
break;
pthread_mutex_unlock(&field_mutex[80 * *a3 + v5]);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int *a2, int *a3)
{
int v4; // eax
__int64 v5; // rcx
__int64 result; // rax

while ( 1 )
{
do
{
*a2 = rand() % 78 + 1;
v4 = rand() % 22 + 1;
*a3 = v4;
}
while ( pthread_mutex_trylock(&field_mutex[80 * v4 + *a2]) );
v5 = *a2;
result = v5 + 80LL * *a3;
if ( *(_DWORD *)(a1 + 4 * result) == 2 )
break;
pthread_mutex_unlock(&field_mutex[80 * *a3 + v5]);
}
return result;
}

original_code:
void board_rand_bg(Board *b, int *x, int *y) {
 while(1) {
  *x = rand()%(80 -2)+1;
  *y = rand()%(24 -2)+1;
  if (pthread_mutex_trylock(&field_mutex[*y][*x]) == 0) {
   if (b->field[*y][*x] == 2) {
    break;
   } else {
    pthread_mutex_unlock(&field_mutex[*y][*x]);
   }
  }
 }
}

canonical_original_code:
void board_rand_bg(struct Board *b, int *x, int *y) {
 while(1) {
  *x = rand()%(80 -2)+1;
  *y = rand()%(24 -2)+1;
  if (pthread_mutex_trylock(&field_mutex[*y][*x]) == 0) {
   if (b->field[*y][*x] == 2) {
    break;
   } else {
    pthread_mutex_unlock(&field_mutex[*y][*x]);
   }
  }
 }
}


decompiled_code:
int __fastcall board_show_score(unsigned int *a1)
{
char v3[32]; // [rsp+0h] [rbp-48h] BYREF
char s[40]; // [rsp+20h] [rbp-28h] BYREF

pthread_mutex_lock(&score_mutex);
sprintf(v3, "Team 0: %d/%d ", a1[1920], a1[1922]);
sprintf(s, "Team 1: %d/%d ", a1[1921], a1[1923]);
pthread_mutex_unlock(&score_mutex);
pthread_mutex_lock(&board_mutex);
wattr_on(stdscr, 0x500uLL, 0LL);
mvprintw(0, 0, v3);
wattr_off(stdscr, 0x500uLL, 0LL);
wattr_on(stdscr, 0x600uLL, 0LL);
mvprintw(23, 0, s);
wattr_off(stdscr, 0x600uLL, 0LL);
return pthread_mutex_unlock(&board_mutex);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int *a1)
{
char v3[32]; // [rsp+0h] [rbp-48h] BYREF
char s[40]; // [rsp+20h] [rbp-28h] BYREF

pthread_mutex_lock(&score_mutex);
sprintf(v3, "Team 0: %d/%d ", a1[1920], a1[1922]);
sprintf(s, "Team 1: %d/%d ", a1[1921], a1[1923]);
pthread_mutex_unlock(&score_mutex);
pthread_mutex_lock(&board_mutex);
wattr_on(stdscr, 0x500uLL, 0LL);
mvprintw(0, 0, v3);
wattr_off(stdscr, 0x500uLL, 0LL);
wattr_on(stdscr, 0x600uLL, 0LL);
mvprintw(23, 0, s);
wattr_off(stdscr, 0x600uLL, 0LL);
return pthread_mutex_unlock(&board_mutex);
}

canonical_original_code:
void board_show_score(struct Board *b) {
 char score0[30];
 char score1[30];
 pthread_mutex_lock(&score_mutex);
 sprintf(score0, "Team 0: %d/%d ", b->score[0], b->best_score[0]);
 sprintf(score1, "Team 1: %d/%d ", b->score[1], b->best_score[1]);
 pthread_mutex_unlock(&score_mutex);
 pthread_mutex_lock(&board_mutex);
 wattr_on(stdscr, (attr_t)(((((chtype)(((5))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0)); mvprintw(0, 0, score0); wattr_off(stdscr, (attr_t)(((((chtype)(((5))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
 wattr_on(stdscr, (attr_t)(((((chtype)(((6))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0)); mvprintw(24 -1, 0, score1); wattr_off(stdscr, (attr_t)(((((chtype)(((6))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8))))), ((void *)0));
 pthread_mutex_unlock(&board_mutex);
}


decompiled_code:
int board_init_mutex()
{
pthread_mutex_t *v0; // rbp
pthread_mutex_t *v1; // rbx
pthread_mutex_t *v2; // rdi

v0 = (pthread_mutex_t *)(unsigned int)((_DWORD)field_mutex + 3200);
do
{
v1 = v0 - 80;
do
{
v2 = v1++;
pthread_mutex_init(v2, 0LL);
}
while ( v0 != v1 );
v0 += 80;
}
while ( v0 != &field_mutex[2000] );
pthread_mutex_init(&board_mutex, 0LL);
pthread_mutex_init(&score_mutex, 0LL);
return pthread_mutex_init(&pause_mutex, 0LL);
}

canonical_decompiled_code:
int func4()
{
pthread_mutex_t *v0; // rbp
pthread_mutex_t *v1; // rbx
pthread_mutex_t *v2; // rdi

v0 = (pthread_mutex_t *)(unsigned int)((_DWORD)field_mutex + 3200);
do
{
v1 = v0 - 80;
do
{
v2 = v1++;
pthread_mutex_init(v2, 0LL);
}
while ( v0 != v1 );
v0 += 80;
}
while ( v0 != &field_mutex[2000] );
pthread_mutex_init(&board_mutex, 0LL);
pthread_mutex_init(&score_mutex, 0LL);
return pthread_mutex_init(&pause_mutex, 0LL);
}

original_code:
void board_init_mutex() {
 int i, j;
 for (i=0; i<24; ++i) {
  for (j=0; j<80; ++j) {
    pthread_mutex_init(&field_mutex[i][j], ((void *)0));
  }
 }
 pthread_mutex_init(&board_mutex, ((void *)0));
 pthread_mutex_init(&score_mutex, ((void *)0));
 pthread_mutex_init(&pause_mutex, ((void *)0));
}

canonical_original_code:
void board_init_mutex() {
 int i; int j;
 for (i=0; i<24; ++i) {
  for (j=0; j<80; ++j) {
    pthread_mutex_init(&field_mutex[i][j], ((void *)0));
  }
 }
 pthread_mutex_init(&board_mutex, ((void *)0));
 pthread_mutex_init(&score_mutex, ((void *)0));
 pthread_mutex_init(&pause_mutex, ((void *)0));
}


decompiled_code:
int board_finalize_mutex()
{
pthread_mutex_t *v0; // rbp
pthread_mutex_t *v1; // rbx
pthread_mutex_t *v2; // rdi

v0 = (pthread_mutex_t *)(unsigned int)((_DWORD)field_mutex + 3200);
do
{
v1 = v0 - 80;
do
{
v2 = v1++;
pthread_mutex_destroy(v2);
}
while ( v0 != v1 );
v0 += 80;
}
while ( v0 != &field_mutex[2000] );
pthread_mutex_destroy(&board_mutex);
pthread_mutex_destroy(&score_mutex);
return pthread_mutex_destroy(&pause_mutex);
}

canonical_decompiled_code:
int func5()
{
pthread_mutex_t *v0; // rbp
pthread_mutex_t *v1; // rbx
pthread_mutex_t *v2; // rdi

v0 = (pthread_mutex_t *)(unsigned int)((_DWORD)field_mutex + 3200);
do
{
v1 = v0 - 80;
do
{
v2 = v1++;
pthread_mutex_destroy(v2);
}
while ( v0 != v1 );
v0 += 80;
}
while ( v0 != &field_mutex[2000] );
pthread_mutex_destroy(&board_mutex);
pthread_mutex_destroy(&score_mutex);
return pthread_mutex_destroy(&pause_mutex);
}

original_code:
void board_finalize_mutex() {
 int i, j;
 for (i=0; i<24; ++i) {
  for (j=0; j<80; ++j) {
    pthread_mutex_destroy(&field_mutex[i][j]);
  }
 }
 pthread_mutex_destroy(&board_mutex);
 pthread_mutex_destroy(&score_mutex);
 pthread_mutex_destroy(&pause_mutex);
}

canonical_original_code:
void board_finalize_mutex() {
 int i; int j;
 for (i=0; i<24; ++i) {
  for (j=0; j<80; ++j) {
    pthread_mutex_destroy(&field_mutex[i][j]);
  }
 }
 pthread_mutex_destroy(&board_mutex);
 pthread_mutex_destroy(&score_mutex);
 pthread_mutex_destroy(&pause_mutex);
}


decompiled_code:
int __fastcall board_set_field(__int64 a1, int a2, int a3, int a4)
{
__int64 v4; // rdx

v4 = 5LL * a3;
*(_DWORD *)(a1 + 4 * (a2 + 16 * v4)) = a4;
return pthread_mutex_unlock(&field_mutex[16 * v4 + a2]);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, int a2, int a3, int a4)
{
__int64 v4; // rdx

v4 = 5LL * a3;
*(_DWORD *)(a1 + 4 * (a2 + 16 * v4)) = a4;
return pthread_mutex_unlock(&field_mutex[16 * v4 + a2]);
}

original_code:
void board_set_field(Board *b, int x, int y, int v) {
 b->field[y][x] = v;
 pthread_mutex_unlock(&field_mutex[y][x]);
}

canonical_original_code:
void board_set_field(struct Board *b, int x, int y, int v) {
 b->field[y][x] = v;
 pthread_mutex_unlock(&field_mutex[y][x]);
}

variable_types: v int


repo: ziransun/quarqd-ext

decompiled_code:
__int64 __fastcall channel_manager_init(__int64 a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
__int64 v3; // rsi
__int64 result; // rax

v1 = a1;
for ( i = 0; i != 4; ++i )
{
v3 = i;
result = ant_channel_init(v1, v3, a1);
v1 += 328LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
__int64 v3; // rsi
__int64 result; // rax

v1 = a1;
for ( i = 0; i != 4; ++i )
{
v3 = i;
result = ant_channel_init(v1, v3, a1);
v1 += 328LL;
}
return result;
}

original_code:
void channel_manager_init(channel_manager_t *self) {
  int i;
  for (i=0; i<4; i++)
    ant_channel_init(self->channels+i, i, self);
  if (0) {
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_POWER);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_HR);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_WG);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_BG);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_BP);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_PD);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_SandC);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_SPEED);
    return;
  }
}

canonical_original_code:
void channel_manager_init(struct channel_manager *self) {
  int i;
  for (i=0; i<4; i++)
    ant_channel_init(self->channels+i, i, self);
  if (0) {
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_POWER);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_HR);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_WG);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_BG);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_BP);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_PD);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_SandC);
    channel_manager_remember_device(self, 17, CHANNEL_TYPE_SPEED);
    return;
  }
}

variable_types: struct channel_manager { struct ant_channel { int number; int state; int channel_type; int channel_type_flags; int device_number; int device_id; int search_type; struct channel_manager *parent; double last_message_timestamp; double blanking_timestamp; int blanked; char id[10]; int channel_assigned; struct messages_initialization_t { int first_time_crank_torque; int first_time_crank_SRM; int first_time_wheel_torque; int first_time_standard_power; int first_time_torque_support; int first_time_calibration_pass; int first_time_calibration_fail; int first_time_heart_rate; int first_time_speed; int first_time_cadence; int first_time_speed_cadence; int first_time_manufacturer; int first_time_product; int first_time_battery_voltage; } mi; int messages_received; int messages_dropped; unsigned char rx_burst_data[128]; int rx_burst_data_index; unsigned char rx_burst_next_sequence; void (*rx_burst_disposition)(struct ant_channel *); void (*tx_ack_disposition)(struct ant_channel *); int is_cinqo; int is_old_cinqo; struct ant_channel *control_channel; int manufacturer_id; int product_id; int product_version; } channels[4]; } *

user_defined_types: struct channel_manager { struct ant_channel channels[4]; }

user_defined_types: struct ant_channel { int number; int state; int channel_type; int channel_type_flags; int device_number; int device_id; int search_type; struct channel_manager *parent; double last_message_timestamp; double blanking_timestamp; int blanked; char id[10]; int channel_assigned; struct messages_initialization_t mi; int messages_received; int messages_dropped; unsigned char rx_burst_data[128]; int rx_burst_data_index; unsigned char rx_burst_next_sequence; void (*rx_burst_disposition)(struct ant_channel *); void (*tx_ack_disposition)(struct ant_channel *); int is_cinqo; int is_old_cinqo; struct ant_channel *control_channel; int manufacturer_id; int product_id; int product_version; }


decompiled_code:
__int64 __fastcall channel_manager_add_device(__int64 a1, unsigned int a2, unsigned int a3, int a4)
{
_DWORD *v6; // rsi
int v7; // ecx
_DWORD *v8; // rdx
unsigned int v9; // ebx
__int64 result; // rax
__int64 v11; // rbp

if ( a4 >= 0 )
{
v11 = a1 + 328LL * a4;
ant_channel_close(v11);
ant_channel_open(v11, a2, a3);
result = 1LL;
}
else
{
v6 = (_DWORD *)(a1 + 8);
v7 = 0;
v8 = (_DWORD *)(a1 + 8);
do
{
if ( (*v8 & 0xF) == a3 && v8[2] == a2 )
{
ant_channel_channel_info(a1 + 328LL * v7);
return 1LL;
}
++v7;
v8 += 82;
}
while ( v7 != 4 );
v9 = 0;
while ( *v6 )
{
++v9;
v6 += 82;
if ( v9 == 4 )
return 0LL;
}
fprintf(stderr, "opening channel #%d\n", v9);
ant_channel_open(a1 + 328LL * (int)v9, a2, a3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3, int a4)
{
_DWORD *v6; // rsi
int v7; // ecx
_DWORD *v8; // rdx
unsigned int v9; // ebx
__int64 result; // rax
__int64 v11; // rbp

if ( a4 >= 0 )
{
v11 = a1 + 328LL * a4;
ant_channel_close(v11);
ant_channel_open(v11, a2, a3);
result = 1LL;
}
else
{
v6 = (_DWORD *)(a1 + 8);
v7 = 0;
v8 = (_DWORD *)(a1 + 8);
do
{
if ( (*v8 & 0xF) == a3 && v8[2] == a2 )
{
ant_channel_channel_info(a1 + 328LL * v7);
return 1LL;
}
++v7;
v8 += 82;
}
while ( v7 != 4 );
v9 = 0;
while ( *v6 )
{
++v9;
v6 += 82;
if ( v9 == 4 )
return 0LL;
}
fprintf(stderr, "opening channel #%d\n", v9);
ant_channel_open(a1 + 328LL * (int)v9, a2, a3);
result = 1LL;
}
return result;
}

original_code:
int channel_manager_add_device(channel_manager_t *self, int device_number, int device_type, int channel_number) {
  int i;
  if (channel_number>-1) {
    ant_channel_close(self->channels+channel_number);
    ant_channel_open(self->channels+channel_number, device_number, device_type);
    return 1;
  }
  for (i=0; i<4; i++)
    if (((self->channels[i].channel_type & 0xf ) == device_type) &&
 (self->channels[i].device_number == device_number)) {
      ant_channel_channel_info(self->channels+i);
      return 1;
    }
  for (i=0; i<4; i++)
    if (self->channels[i].channel_type == CHANNEL_TYPE_UNUSED) {
      fprintf(stderr, "opening channel #%d\n",i);
      ant_channel_open(self->channels+i, device_number, device_type);
      return 1;
    }
  return 0;
}

canonical_original_code:
int channel_manager_add_device(struct channel_manager *self, int device_number, int device_type, int channel_number) {
  int i;
  if (channel_number>-1) {
    ant_channel_close(self->channels+channel_number);
    ant_channel_open(self->channels+channel_number, device_number, device_type);
    return 1;
  }
  for (i=0; i<4; i++)
    if (((self->channels[i].channel_type & 0xf ) == device_type) &&
 (self->channels[i].device_number == device_number)) {
      ant_channel_channel_info(self->channels+i);
      return 1;
    }
  for (i=0; i<4; i++)
    if (self->channels[i].channel_type == CHANNEL_TYPE_UNUSED) {
      fprintf(stderr, "opening channel #%d\n",i);
      ant_channel_open(self->channels+i, device_number, device_type);
      return 1;
    }
  return 0;
}

user_defined_types: struct messages_initialization_t { int first_time_crank_torque; int first_time_crank_SRM; int first_time_wheel_torque; int first_time_standard_power; int first_time_torque_support; int first_time_calibration_pass; int first_time_calibration_fail; int first_time_heart_rate; int first_time_speed; int first_time_cadence; int first_time_speed_cadence; int first_time_manufacturer; int first_time_product; int first_time_battery_voltage; }


decompiled_code:
__int64 __fastcall channel_manager_remove_device(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // rbp
__int64 v5; // rax

v3 = a1;
while ( *(_DWORD *)(v3 + 8) != a3 || *(_DWORD *)(v3 + 16) != (_DWORD)a2 )
{
v3 += 328LL;
if ( a1 + 1312 == v3 )
return 0LL;
}
v5 = *(_QWORD *)(v3 + 304);
if ( v5 )
{
if ( v5 != v3 )
channel_manager_remove_device(a1, a2, *(unsigned int *)(v5 + 8));
}
ant_channel_close(v3);
*(_DWORD *)(v3 + 8) = 0;
*(_DWORD *)(v3 + 16) = 0;
ant_channel_set_id(v3);
return 1LL;
}

original_code:
int channel_manager_remove_device(channel_manager_t *self, int device_number, int channel_type) {
  int i;
  for (i=0; i<4; i++) {
    ant_channel_t *ac = self->channels+i;
    if ((ac->channel_type == channel_type) &&
 (ac->device_number == device_number)) {
      if ((ac->control_channel!=ac) && ac->control_channel) {
 channel_manager_remove_device(self, device_number, ac->control_channel->channel_type);
      }
      ant_channel_close(ac);
      ac->channel_type=CHANNEL_TYPE_UNUSED;
      ac->device_number=0;
      ant_channel_set_id(ac);
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
int channel_manager_remove_device(struct channel_manager *self, int device_number, int channel_type) {
  int i;
  for (i=0; i<4; i++) {
    struct ant_channel *ac = self->channels+i;
    if ((ac->channel_type == channel_type) &&
 (ac->device_number == device_number)) {
      if ((ac->control_channel!=ac) && ac->control_channel) {
 channel_manager_remove_device(self, device_number, ac->control_channel->channel_type);
      }
      ant_channel_close(ac);
      ac->channel_type=CHANNEL_TYPE_UNUSED;
      ac->device_number=0;
      ant_channel_set_id(ac);
      return 1;
    }
  }
  return 0;
}

variable_types: struct ant_channel { int number; int state; int channel_type; int channel_type_flags; int device_number; int device_id; int search_type; struct channel_manager { struct ant_channel channels[4]; } *parent; double last_message_timestamp; double blanking_timestamp; int blanked; char id[10]; int channel_assigned; struct messages_initialization_t { int first_time_crank_torque; int first_time_crank_SRM; int first_time_wheel_torque; int first_time_standard_power; int first_time_torque_support; int first_time_calibration_pass; int first_time_calibration_fail; int first_time_heart_rate; int first_time_speed; int first_time_cadence; int first_time_speed_cadence; int first_time_manufacturer; int first_time_product; int first_time_battery_voltage; } mi; int messages_received; int messages_dropped; unsigned char rx_burst_data[128]; int rx_burst_data_index; unsigned char rx_burst_next_sequence; void (*rx_burst_disposition)(struct ant_channel *); void (*tx_ack_disposition)(struct ant_channel *); int is_cinqo; int is_old_cinqo; struct ant_channel *control_channel; int manufacturer_id; int product_id; int product_version; } *


decompiled_code:
__int64 __fastcall channel_manager_start_waiting_search(__int64 a1)
{
_DWORD *v1; // rdx
_DWORD *v3; // rsi
_DWORD *v4; // rax
int v5; // edi
unsigned int v6; // er8

v1 = (_DWORD *)(a1 + 12);
v3 = (_DWORD *)(a1 + 1324);
v4 = (_DWORD *)(a1 + 12);
do
{
v5 = *v4 & 0x10;
if ( v5 )
return 0LL;
v4 += 82;
}
while ( v4 != v3 );
while ( 1 )
{
v6 = *v1 & 0x20;
if ( v6 )
break;
++v5;
v1 += 82;
if ( v5 == 4 )
return v6;
}
*(_DWORD *)(a1 + 328LL * v5 + 12) = *v1 & 0xFFFFFFDF;
ANT_UnassignChannel();
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
_DWORD *v1; // rdx
_DWORD *v3; // rsi
_DWORD *v4; // rax
int v5; // edi
unsigned int v6; // er8

v1 = (_DWORD *)(a1 + 12);
v3 = (_DWORD *)(a1 + 1324);
v4 = (_DWORD *)(a1 + 12);
do
{
v5 = *v4 & 0x10;
if ( v5 )
return 0LL;
v4 += 82;
}
while ( v4 != v3 );
while ( 1 )
{
v6 = *v1 & 0x20;
if ( v6 )
break;
++v5;
v1 += 82;
if ( v5 == 4 )
return v6;
}
*(_DWORD *)(a1 + 328LL * v5 + 12) = *v1 & 0xFFFFFFDF;
ANT_UnassignChannel();
return 1LL;
}

original_code:
int channel_manager_start_waiting_search(channel_manager_t *self) {
  int i;
  for (i=0; i<4; i++) {
    if (self->channels[i].channel_type_flags & 0x10) {
      return 0;
    }
  }
  for (i=0; i<4; i++) {
    if (self->channels[i].channel_type_flags & 0x20) {
      self->channels[i].channel_type_flags &= ~0x20;
      ANT_UnassignChannel(i);
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
int channel_manager_start_waiting_search(struct channel_manager *self) {
  int i;
  for (i=0; i<4; i++) {
    if (self->channels[i].channel_type_flags & 0x10) {
      return 0;
    }
  }
  for (i=0; i<4; i++) {
    if (self->channels[i].channel_type_flags & 0x20) {
      self->channels[i].channel_type_flags &= ~0x20;
      ANT_UnassignChannel(i);
      return 1;
    }
  }
  return 0;
}


decompiled_code:
__int64 __fastcall channel_manager_report(__int64 a1)
{
ant_channel_channel_info(a1);
ant_channel_channel_info(a1 + 328);
ant_channel_channel_info(a1 + 656);
return ant_channel_channel_info(a1 + 984);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
ant_channel_channel_info(a1);
ant_channel_channel_info(a1 + 328);
ant_channel_channel_info(a1 + 656);
return ant_channel_channel_info(a1 + 984);
}

original_code:
void channel_manager_report(channel_manager_t *self) {
  int i;
  for (i=0; i<4; i++)
    ant_channel_channel_info(self->channels+i);
}

canonical_original_code:
void channel_manager_report(struct channel_manager *self) {
  int i;
  for (i=0; i<4; i++)
    ant_channel_channel_info(self->channels+i);
}


decompiled_code:
__int64 __fastcall channel_manager_associate_control_channels(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax
unsigned int v3; // esi
__int64 v4; // r13

v1 = a1;
a1[38] = 0LL;
a1[79] = 0LL;
a1[120] = 0LL;
a1[161] = 0LL;
do
{
result = *((unsigned int *)v1 + 2);
if ( (_DWORD)result == 6 )
{
if ( *((_DWORD *)v1 + 74) )
{
if ( *((_DWORD *)v1 + 75) )
{
v3 = *((_DWORD *)v1 + 4);
v4 = channel_manager_find_device((__int64)a1, v3, 10);
if ( v4
|| (v4 = channel_manager_find_device((__int64)a1, v3, 11)) != 0
|| (v4 = channel_manager_find_device((__int64)a1, v3, 12)) != 0 )
{
result = ant_channel_is_searching(v4);
if ( !(_DWORD)result )
{
v1[38] = v4;
result = ant_channel_send_cinqo_success(v1);
}
}
else
{
result = channel_manager_add_device((__int64)a1, v3, 0xAu, -1);
}
}
else
{
v1[38] = v1;
result = ant_channel_send_cinqo_success(v1);
}
}
}
else
{
result = (unsigned int)(result - 10);
if ( (unsigned int)result <= 2 )
{
*((_DWORD *)v1 + 74) = 1;
v1[38] = v1;
}
}
v1 += 41;
}
while ( v1 != a1 + 164 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax
unsigned int v3; // esi
__int64 v4; // r13

v1 = a1;
a1[38] = 0LL;
a1[79] = 0LL;
a1[120] = 0LL;
a1[161] = 0LL;
do
{
result = *((unsigned int *)v1 + 2);
if ( (_DWORD)result == 6 )
{
if ( *((_DWORD *)v1 + 74) )
{
if ( *((_DWORD *)v1 + 75) )
{
v3 = *((_DWORD *)v1 + 4);
v4 = func3((__int64)a1, v3, 10);
if ( v4
|| (v4 = func3((__int64)a1, v3, 11)) != 0
|| (v4 = func3((__int64)a1, v3, 12)) != 0 )
{
result = ant_channel_is_searching(v4);
if ( !(_DWORD)result )
{
v1[38] = v4;
result = ant_channel_send_cinqo_success(v1);
}
}
else
{
result = func1((__int64)a1, v3, 0xAu, -1);
}
}
else
{
v1[38] = v1;
result = ant_channel_send_cinqo_success(v1);
}
}
}
else
{
result = (unsigned int)(result - 10);
if ( (unsigned int)result <= 2 )
{
*((_DWORD *)v1 + 74) = 1;
v1[38] = v1;
}
}
v1 += 41;
}
while ( v1 != a1 + 164 );
return result;
}

original_code:
void channel_manager_associate_control_channels(channel_manager_t *self) {
  int i;
  for (i=0; i<4; i++)
    self->channels[i].control_channel=((void *)0);
  for (i=0; i<4; i++) {
    ant_channel_t *ac=self->channels+i;
    switch (ac->channel_type) {
    case CHANNEL_TYPE_POWER:
      if (ac->is_cinqo) {
 if (ac->is_old_cinqo) {
   ant_channel_t * my_ant_channel;
   my_ant_channel=channel_manager_find_device(self,
           ac->device_number,
           CHANNEL_TYPE_QUARQ);
   if (!my_ant_channel) {
     my_ant_channel=channel_manager_find_device(self,
             ac->device_number,
             CHANNEL_TYPE_FAST_QUARQ);
   }
   if (!my_ant_channel) {
     my_ant_channel=channel_manager_find_device(self,
             ac->device_number,
             CHANNEL_TYPE_FAST_QUARQ_NEW);
   }
   if (my_ant_channel) {
     if (ant_channel_is_searching(my_ant_channel)) {
       ;
     } else {
       ac->control_channel=my_ant_channel;
       ant_channel_send_cinqo_success(ac);
     }
   } else {
     channel_manager_add_device(self, ac->device_number,
           CHANNEL_TYPE_QUARQ, -1);
   }
 } else {
   ac->control_channel=ac;
   ant_channel_send_cinqo_success(ac);
 }
      }
      break;
    case CHANNEL_TYPE_FAST_QUARQ:
    case CHANNEL_TYPE_FAST_QUARQ_NEW:
    case CHANNEL_TYPE_QUARQ:
      ac->is_cinqo=1;
      ac->control_channel=ac;
    default:
      ;
    }
  }
}

canonical_original_code:
void channel_manager_associate_control_channels(struct channel_manager *self) {
  int i;
  for (i=0; i<4; i++)
    self->channels[i].control_channel=((void *)0);
  for (i=0; i<4; i++) {
    struct ant_channel *ac = self->channels+i;
    switch (ac->channel_type) {
    case CHANNEL_TYPE_POWER:
      if (ac->is_cinqo) {
 if (ac->is_old_cinqo) {
   struct ant_channel *my_ant_channel;
   my_ant_channel=channel_manager_find_device(self,
           ac->device_number,
           CHANNEL_TYPE_QUARQ);
   if (!my_ant_channel) {
     my_ant_channel=channel_manager_find_device(self,
             ac->device_number,
             CHANNEL_TYPE_FAST_QUARQ);
   }
   if (!my_ant_channel) {
     my_ant_channel=channel_manager_find_device(self,
             ac->device_number,
             CHANNEL_TYPE_FAST_QUARQ_NEW);
   }
   if (my_ant_channel) {
     if (ant_channel_is_searching(my_ant_channel)) {
       ;
     } else {
       ac->control_channel=my_ant_channel;
       ant_channel_send_cinqo_success(ac);
     }
   } else {
     channel_manager_add_device(self, ac->device_number,
           CHANNEL_TYPE_QUARQ, -1);
   }
 } else {
   ac->control_channel=ac;
   ant_channel_send_cinqo_success(ac);
 }
      }
      break;
    case CHANNEL_TYPE_FAST_QUARQ:
    case CHANNEL_TYPE_FAST_QUARQ_NEW:
    case CHANNEL_TYPE_QUARQ:
      ac->is_cinqo=1;
      ac->control_channel=ac;
    default:
      ;
    }
  }
}


decompiled_code:
__int64 __fastcall parse_config(const char *a1, const char *a2)
{
int *v3; // rax
int *v4; // rbp
int v5; // eax
int *v6; // rax
int *v7; // rbp
int v8; // eax
int *v9; // rax
int *v10; // rbp
int v11; // eax

if ( (*(_BYTE *)(&quarqd_config + 5) & 8) != 0 )
fprintf(stderr, "Looking at name %s\n value %s\n", a1, a2);
if ( !strcmp(a1, "QUARQD_PORT") )
{
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = strtol(a2, 0LL, 0);
if ( !v5 || !*v4 )
{
quarqd_config = v5;
return 1LL;
}
return 0LL;
}
if ( !strcmp(a1, "ANT_TTY") )
{
*((_QWORD *)&quarqd_config + 1) = strdup(a2);
return 1LL;
}
if ( strcmp(a1, "ANT_BAUDRATE") )
{
if ( !strcmp(a1, "QUARQD_DEBUG") )
{
v6 = _errno_location();
*v6 = 0;
v7 = v6;
v8 = strtol(a2, 0LL, 0);
if ( !v8 || !*v7 )
{
*(&quarqd_config + 5) = v8;
return 1LL;
}
}
return 0LL;
}
v9 = _errno_location();
*v9 = 0;
v10 = v9;
v11 = strtol(a2, 0LL, 0);
if ( v11 && *v10 )
return 0LL;
*(&quarqd_config + 4) = v11;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, const char *a2)
{
int *v3; // rax
int *v4; // rbp
int v5; // eax
int *v6; // rax
int *v7; // rbp
int v8; // eax
int *v9; // rax
int *v10; // rbp
int v11; // eax

if ( (*(_BYTE *)(&quarqd_config + 5) & 8) != 0 )
fprintf(stderr, "Looking at name %s\n value %s\n", a1, a2);
if ( !strcmp(a1, "QUARQD_PORT") )
{
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = strtol(a2, 0LL, 0);
if ( !v5 || !*v4 )
{
quarqd_config = v5;
return 1LL;
}
return 0LL;
}
if ( !strcmp(a1, "ANT_TTY") )
{
*((_QWORD *)&quarqd_config + 1) = strdup(a2);
return 1LL;
}
if ( strcmp(a1, "ANT_BAUDRATE") )
{
if ( !strcmp(a1, "QUARQD_DEBUG") )
{
v6 = _errno_location();
*v6 = 0;
v7 = v6;
v8 = strtol(a2, 0LL, 0);
if ( !v8 || !*v7 )
{
*(&quarqd_config + 5) = v8;
return 1LL;
}
}
return 0LL;
}
v9 = _errno_location();
*v9 = 0;
v10 = v9;
v11 = strtol(a2, 0LL, 0);
if ( v11 && *v10 )
return 0LL;
*(&quarqd_config + 4) = v11;
return 1LL;
}

original_code:
int parse_config(char *name, char *value) {
  if (quarqd_config.debug_level & DEBUG_LEVEL_CONFIG_PARSE) fprintf(stderr, "Looking at name %s\n value %s\n",name,value);
  if (!strcmp(name,"QUARQD_PORT")) {
    int val; (*__errno_location ())=0; if ((val=strtol(value, ((void *)0), 0)) && (*__errno_location ())) return 0;
    quarqd_config.quarqd_port=val;
  } else if (!strcmp(name,"ANT_TTY")) {
    quarqd_config.ant_tty=strdup(value);
  } else if (!strcmp(name,"ANT_BAUDRATE")) {
    int val; (*__errno_location ())=0; if ((val=strtol(value, ((void *)0), 0)) && (*__errno_location ())) return 0;
    quarqd_config.ant_baudrate=val;
  } else if (!strcmp(name,"QUARQD_DEBUG")) {
    int val; (*__errno_location ())=0; if ((val=strtol(value, ((void *)0), 0)) && (*__errno_location ())) return 0;
    quarqd_config.debug_level=val;
  } else return 0;
  return 1;
}

variable_types:
name
char *

variable_types:
value
char *

variable_types: val int


decompiled_code:
__int64 read_config_env()
{
unsigned int v0; // er12
char *v1; // rax
char *v2; // rsi
char *v3; // rsi
char *v4; // rsi

v0 = 0;
v1 = getenv("QUARQD_PORT");
if ( v1 )
{
v0 = 1;
parse_config("QUARQD_PORT", v1);
}
v2 = getenv("ANT_TTY");
if ( v2 )
{
v0 = 1;
parse_config("ANT_TTY", v2);
}
v3 = getenv("ANT_BAUDRATE");
if ( v3 )
{
v0 = 1;
parse_config("ANT_BAUDRATE", v3);
}
v4 = getenv("QUARQD_DEBUG");
if ( v4 )
{
v0 = 1;
parse_config("QUARQD_DEBUG", v4);
}
return v0;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // er12
char *v1; // rax
char *v2; // rsi
char *v3; // rsi
char *v4; // rsi

v0 = 0;
v1 = getenv("QUARQD_PORT");
if ( v1 )
{
v0 = 1;
func0("QUARQD_PORT", v1);
}
v2 = getenv("ANT_TTY");
if ( v2 )
{
v0 = 1;
func0("ANT_TTY", v2);
}
v3 = getenv("ANT_BAUDRATE");
if ( v3 )
{
v0 = 1;
func0("ANT_BAUDRATE", v3);
}
v4 = getenv("QUARQD_DEBUG");
if ( v4 )
{
v0 = 1;
func0("QUARQD_DEBUG", v4);
}
return v0;
}

original_code:
int read_config_env(void) {
  char *c;
  int ret=0;
  c=getenv("QUARQD_PORT"); if (c) { parse_config("QUARQD_PORT",c); ret = 1; };
  c=getenv("ANT_TTY"); if (c) { parse_config("ANT_TTY",c); ret = 1; };
  c=getenv("ANT_BAUDRATE"); if (c) { parse_config("ANT_BAUDRATE",c); ret = 1; };
  c=getenv("QUARQD_DEBUG"); if (c) { parse_config("QUARQD_DEBUG",c); ret = 1; };
  return ret;
}

canonical_original_code:
int read_config_env() {
  char *c;
  int ret = 0;
  c=getenv("QUARQD_PORT"); if (c) { parse_config("QUARQD_PORT",c); ret = 1; };
  c=getenv("ANT_TTY"); if (c) { parse_config("ANT_TTY",c); ret = 1; };
  c=getenv("ANT_BAUDRATE"); if (c) { parse_config("ANT_BAUDRATE",c); ret = 1; };
  c=getenv("QUARQD_DEBUG"); if (c) { parse_config("QUARQD_DEBUG",c); ret = 1; };
  return ret;
}

variable_types:
c
char *

variable_types: ret int


decompiled_code:
__int64 init_config()
{
quarqd_config = 8168;
*((_QWORD *)&quarqd_config + 1) = "/dev/ttyANT";
*((_QWORD *)&quarqd_config + 2) = 0x100004B00LL;
read_config_dotfile();
return read_config_env();
}

canonical_decompiled_code:
__int64 func3()
{
quarqd_config = 8168;
*((_QWORD *)&quarqd_config + 1) = "/dev/ttyANT";
*((_QWORD *)&quarqd_config + 2) = 0x100004B00LL;
func1();
return func2();
}

original_code:
void init_config(void) {
  quarqd_config.quarqd_port=8168;
  quarqd_config.ant_tty="/dev/ttyANT";
  quarqd_config.ant_baudrate=19200;
  quarqd_config.debug_level=1;
  read_config_dotfile();
  read_config_env();
}

canonical_original_code:
void init_config() {
  quarqd_config.quarqd_port=8168;
  quarqd_config.ant_tty="/dev/ttyANT";
  quarqd_config.ant_baudrate=19200;
  quarqd_config.debug_level=1;
  read_config_dotfile();
  read_config_env();
}


HandleChannelEvent

decompiled_code:
__int64 HandleChannelEvent()
{
__int64 result; // rax
__int64 (__fastcall *v1)(__int64, char *); // rdx

result = (unsigned __int8)byte_65B;
if ( (byte_65B & 4) == 0 )
{
result = byte_65B & 7;
v1 = (__int64 (__fastcall *)(__int64, char *))eventFuncs[result];
if ( v1 )
result = v1(privateData[result], &rxMessage);
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
__int64 (__fastcall *v1)(__int64, char *); // rdx

result = (unsigned __int8)byte_65B;
if ( (byte_65B & 4) == 0 )
{
result = byte_65B & 7;
v1 = (__int64 (__fastcall *)(__int64, char *))eventFuncs[result];
if ( v1 )
result = v1(privateData[result], &rxMessage);
}
return result;
}

original_code:
static void HandleChannelEvent(void) {
 int channel = rxMessage[3] & 0x7;
 if(channel >= 0 && channel < 4) {
   if(eventFuncs[channel] != ((void *)0)) {
     eventFuncs[channel](privateData[channel],rxMessage);
   }
 }
}

canonical_original_code:
static void HandleChannelEvent() {
 int channel = rxMessage[3] & 0x7;
 if(channel >= 0 && channel < 4) {
   if(eventFuncs[channel] != ((void *)0)) {
     eventFuncs[channel](privateData[channel],rxMessage);
   }
 }
}

variable_types: channel int