decompiled_code:
__int64 __fastcall amp_encode_sizeof_list(unsigned __int64 *a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // r12

if ( !*a1 )
return 9LL;
v1 = 0LL;
v2 = 9LL;
do
{
v2 += amp_encode_sizeof(LODWORD(a1[2 * (int)v1 + 2]), a1[2 * (int)v1 + 3]);
++v1;
}
while ( *a1 > v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned __int64 *a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // r12

if ( !*a1 )
return 9LL;
v1 = 0LL;
v2 = 9LL;
do
{
v2 += amp_encode_sizeof(LODWORD(a1[2 * (int)v1 + 2]), a1[2 * (int)v1 + 3]);
++v1;
}
while ( *a1 > v1 );
return v2;
}

original_code:
size_t amp_encode_sizeof_list(amp_list_t *l)
{
  size_t result = 9;
  for (int i = 0; i < l->size; i++)
  {
    result += amp_encode_sizeof(l->values[i]);
  }
  return result;
}

canonical_original_code:
unsigned long amp_encode_sizeof_list(struct amp_list_st *l)
{
  unsigned long result = 9;
  for (int i = 0; i < l->size; i++)
  {
    result += amp_encode_sizeof(l->values[i]);
  }
  return result;
}


repo: rhs/amp

original_code:
size_t amp_encode_list(amp_list_t *l, char *out)
{
  char *old = out;
  char *start;
  amp_write_start(&out, out + 1024, &start);
  for (int i = 0; i < l->size; i++)
  {
    out += amp_encode(l->values[i], out);
  }
  amp_write_list(&out, out + 1024, start, l->size);
  return out - old;
}

canonical_original_code:
unsigned long amp_encode_list(struct amp_list_st *l, char *out)
{
  char *old = out;
  char *start;
  amp_write_start(&out, out + 1024, &start);
  for (int i = 0; i < l->size; i++)
  {
    out += amp_encode(l->values[i], out);
  }
  amp_write_list(&out, out + 1024, start, l->size);
  return out - old;
}

variable_types: struct amp_list_st { unsigned long size; unsigned long capacity; struct amp_value_st { enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 } type; union { _Bool as_boolean; unsigned char as_ubyte; unsigned short as_ushort; unsigned int as_uint; unsigned long as_ulong; char as_byte; short as_short; int as_int; long as_long; float as_float; double as_double; int as_char; struct amp_string_st { unsigned long size; int wcs[]; } *as_string; struct amp_binary_st { unsigned long size; char bytes[]; } *as_binary; struct amp_array_st { enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 } type; unsigned long size; unsigned long capacity; struct amp_value_st values[]; } *as_array; struct amp_list_st *as_list; struct amp_map_st { unsigned long size; unsigned long capacity; struct amp_value_st pairs[]; } *as_map; struct amp_tag_st { struct amp_value_st descriptor; struct amp_value_st value; } *as_tag; void *as_ref; } u; } values[]; } *

variable_types:
out
char *

variable_types:
old
char *

variable_types:
start
char *

variable_types: i int

return_type: unsigned long

user_defined_types: struct amp_list_st { unsigned long size; unsigned long capacity; struct amp_value_st values[]; }

user_defined_types: struct amp_value_st { enum TYPE type; union { _Bool as_boolean; unsigned char as_ubyte; unsigned short as_ushort; unsigned int as_uint; unsigned long as_ulong; char as_byte; short as_short; int as_int; long as_long; float as_float; double as_double; int as_char; struct amp_string_st *as_string; struct amp_binary_st *as_binary; struct amp_array_st *as_array; struct amp_list_st *as_list; struct amp_map_st *as_map; struct amp_tag_st *as_tag; void *as_ref; } u; }

user_defined_types: struct amp_array_st { enum TYPE type; unsigned long size; unsigned long capacity; struct amp_value_st values[]; }


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

switch ( a1 )
{
case 0:
result = 110LL;
break;
case 2:
result = 66LL;
break;
case 3:
result = 72LL;
break;
case 4:
result = 73LL;
break;
case 5:
result = 76LL;
break;
case 6:
result = 98LL;
break;
case 7:
result = 104LL;
break;
case 8:
result = 105LL;
break;
case 9:
result = 108LL;
break;
case 10:
result = 102LL;
break;
case 11:
result = 100LL;
break;
case 12:
result = 67LL;
break;
case 13:
result = 83LL;
break;
case 14:
result = 122LL;
break;
case 16:
result = 116LL;
break;
case 17:
result = 109LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
result = 110LL;
break;
case 2:
result = 66LL;
break;
case 3:
result = 72LL;
break;
case 4:
result = 73LL;
break;
case 5:
result = 76LL;
break;
case 6:
result = 98LL;
break;
case 7:
result = 104LL;
break;
case 8:
result = 105LL;
break;
case 9:
result = 108LL;
break;
case 10:
result = 102LL;
break;
case 11:
result = 100LL;
break;
case 12:
result = 67LL;
break;
case 13:
result = 83LL;
break;
case 14:
result = 122LL;
break;
case 16:
result = 116LL;
break;
case 17:
result = 109LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
static char type_to_code(enum TYPE type)
{
  switch (type)
  {
  case EMPTY: return 'n';
  case BYTE: return 'b';
  case UBYTE: return 'B';
  case SHORT: return 'h';
  case USHORT: return 'H';
  case INT: return 'i';
  case UINT: return 'I';
  case LONG: return 'l';
  case ULONG: return 'L';
  case FLOAT: return 'f';
  case DOUBLE: return 'd';
  case CHAR: return 'C';
  case STRING: return 'S';
  case BINARY: return 'z';
  case LIST: return 't';
  case MAP: return 'm';
  default: return -1;
  }
}

variable_types: enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 }

return_type: char


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

switch ( a1 )
{
case 0u:
result = 64LL;
break;
case 1u:
result = 86LL;
break;
case 2u:
result = 80LL;
break;
case 3u:
result = 96LL;
break;
case 4u:
result = 112LL;
break;
case 5u:
result = 4294967168LL;
break;
case 6u:
result = 81LL;
break;
case 7u:
result = 97LL;
break;
case 8u:
result = 113LL;
break;
case 9u:
result = 4294967169LL;
break;
case 0xAu:
result = 114LL;
break;
case 0xBu:
result = 4294967170LL;
break;
case 0xCu:
result = 115LL;
break;
case 0xDu:
result = 4294967217LL;
break;
case 0xEu:
result = 4294967216LL;
break;
case 0xFu:
result = 4294967280LL;
break;
case 0x10u:
result = 4294967248LL;
break;
case 0x11u:
result = 4294967249LL;
break;
default:
amp_fatal("no amqp code for type: %i", a1);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

switch ( a1 )
{
case 0u:
result = 64LL;
break;
case 1u:
result = 86LL;
break;
case 2u:
result = 80LL;
break;
case 3u:
result = 96LL;
break;
case 4u:
result = 112LL;
break;
case 5u:
result = 4294967168LL;
break;
case 6u:
result = 81LL;
break;
case 7u:
result = 97LL;
break;
case 8u:
result = 113LL;
break;
case 9u:
result = 4294967169LL;
break;
case 0xAu:
result = 114LL;
break;
case 0xBu:
result = 4294967170LL;
break;
case 0xCu:
result = 115LL;
break;
case 0xDu:
result = 4294967217LL;
break;
case 0xEu:
result = 4294967216LL;
break;
case 0xFu:
result = 4294967280LL;
break;
case 0x10u:
result = 4294967248LL;
break;
case 0x11u:
result = 4294967249LL;
break;
default:
amp_fatal("no amqp code for type: %i", a1);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
static uint8_t type_to_amqp_code(enum TYPE type)
{
  switch (type)
  {
  case EMPTY: return (0x40);
  case BOOLEAN: return (0x56);
  case BYTE: return (0x51);
  case UBYTE: return (0x50);
  case SHORT: return (0x61);
  case USHORT: return (0x60);
  case INT: return (0x71);
  case UINT: return (0x70);
  case CHAR: return (0x73);
  case LONG: return (0x81);
  case ULONG: return (0x80);
  case FLOAT: return (0x72);
  case DOUBLE: return (0x82);
  case STRING: return (0xb1);
  case BINARY: return (0xb0);
  case LIST: return (0xd0);
  case MAP: return (0xd1);
  case ARRAY: return (0xf0);
  default:
    amp_fatal("no amqp code for type: %i", type);
    return -1;
  }
}

canonical_original_code:
static unsigned char type_to_amqp_code(enum TYPE type)
{
  switch (type)
  {
  case EMPTY: return (0x40);
  case BOOLEAN: return (0x56);
  case BYTE: return (0x51);
  case UBYTE: return (0x50);
  case SHORT: return (0x61);
  case USHORT: return (0x60);
  case INT: return (0x71);
  case UINT: return (0x70);
  case CHAR: return (0x73);
  case LONG: return (0x81);
  case ULONG: return (0x80);
  case FLOAT: return (0x72);
  case DOUBLE: return (0x82);
  case STRING: return (0xb1);
  case BINARY: return (0xb0);
  case LIST: return (0xd0);
  case MAP: return (0xd1);
  case ARRAY: return (0xf0);
  default:
    amp_fatal("no amqp code for type: %i", type);
    return -1;
  }
}

return_type: unsigned char

user_defined_types: enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 }


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

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

original_code:
void amp_free_array(amp_array_t *a)
{
  free(a);
}

canonical_original_code:
void amp_free_array(struct amp_array_st *a)
{
  free(a);
}

variable_types: struct amp_array_st { enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 } type; unsigned long size; unsigned long capacity; struct amp_value_st { enum TYPE { EMPTY=0, BOOLEAN=1, UBYTE=2, USHORT=3, UINT=4, ULONG=5, BYTE=6, SHORT=7, INT=8, LONG=9, FLOAT=10, DOUBLE=11, CHAR=12, STRING=13, BINARY=14, ARRAY=15, LIST=16, MAP=17, TAG=18, REF=19 } type; union { _Bool as_boolean; unsigned char as_ubyte; unsigned short as_ushort; unsigned int as_uint; unsigned long as_ulong; char as_byte; short as_short; int as_int; long as_long; float as_float; double as_double; int as_char; struct amp_string_st { unsigned long size; int wcs[]; } *as_string; struct amp_binary_st { unsigned long size; char bytes[]; } *as_binary; struct amp_array_st *as_array; struct amp_list_st { unsigned long size; unsigned long capacity; struct amp_value_st values[]; } *as_list; struct amp_map_st { unsigned long size; unsigned long capacity; struct amp_value_st pairs[]; } *as_map; struct amp_tag_st { struct amp_value_st descriptor; struct amp_value_st value; } *as_tag; void *as_ref; } u; } values[]; } *

return_type: void


decompiled_code:
__int64 __fastcall amp_visit_array(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rbx
__int64 result; // rax

if ( *(_QWORD *)(a1 + 8) )
{
v2 = 0LL;
do
{
result = amp_visit(
*(unsigned int *)(a1 + 16 * ((int)v2 + 1LL) + 8),
*(_QWORD *)(a1 + 16 * ((int)v2 + 1LL) + 16),
a2);
++v2;
}
while ( *(_QWORD *)(a1 + 8) > v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rbx
__int64 result; // rax

if ( *(_QWORD *)(a1 + 8) )
{
v2 = 0LL;
do
{
result = amp_visit(
*(unsigned int *)(a1 + 16 * ((int)v2 + 1LL) + 8),
*(_QWORD *)(a1 + 16 * ((int)v2 + 1LL) + 16),
a2);
++v2;
}
while ( *(_QWORD *)(a1 + 8) > v2 );
}
return result;
}

original_code:
void amp_visit_array(amp_array_t *a, void (*visitor)(amp_value_t))
{
  for (int i = 0; i < a->size; i++)
  {
    amp_visit(a->values[i], visitor);
  }
}

canonical_original_code:
void amp_visit_array(struct amp_array_st *a, void (*visitor)(struct amp_value_st))
{
  for (int i = 0; i < a->size; i++)
  {
    amp_visit(a->values[i], visitor);
  }
}

variable_types:
visitor
void (*)(struct amp_value_st)

user_defined_types: struct amp_string_st { unsigned long size; int wcs[]; }

user_defined_types: struct amp_binary_st { unsigned long size; char bytes[]; }

user_defined_types: struct amp_map_st { unsigned long size; unsigned long capacity; struct amp_value_st pairs[]; }

user_defined_types: struct amp_tag_st { struct amp_value_st descriptor; struct amp_value_st value; }


decompiled_code:
__int64 __fastcall amp_format_sizeof_array(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // r12

if ( !*(_QWORD *)(a1 + 8) )
return 4LL;
v1 = 0LL;
v2 = 4LL;
do
{
v2 += amp_format_sizeof(
*(unsigned int *)(a1 + 16 * ((int)v1 + 1LL) + 8),
*(_QWORD *)(a1 + 16 * ((int)v1 + 1LL) + 16))
+ 2;
++v1;
}
while ( *(_QWORD *)(a1 + 8) > v1 );
return v2;
}

original_code:
size_t amp_format_sizeof_array(amp_array_t *array)
{
  size_t result = 4;
  for (int i = 0; i < array->size; i++)
  {
    result += amp_format_sizeof(array->values[i]) + 2;
  }
  return result;
}

canonical_original_code:
unsigned long amp_format_sizeof_array(struct amp_array_st *array)
{
  unsigned long result = 4;
  for (int i = 0; i < array->size; i++)
  {
    result += amp_format_sizeof(array->values[i]) + 2;
  }
  return result;
}

variable_types:
result
unsigned long


decompiled_code:
__int64 __fastcall amp_format_array(__int64 a1, __int64 a2, __int64 a3)
{
char v4; // al
__int64 result; // rax

v4 = type_to_code(*(_DWORD *)a3);
result = amp_fmt(a1, a2, "@%c[", (unsigned int)v4);
if ( !(_DWORD)result )
{
result = amp_format_value(a1, a2, a3 + 24, *(_QWORD *)(a3 + 8));
if ( !(_DWORD)result )
result = amp_fmt(a1, a2, "]");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3)
{
char v4; // al
__int64 result; // rax

v4 = func0(*(_DWORD *)a3);
result = amp_fmt(a1, a2, "@%c[", (unsigned int)v4);
if ( !(_DWORD)result )
{
result = amp_format_value(a1, a2, a3 + 24, *(_QWORD *)(a3 + 8));
if ( !(_DWORD)result )
result = amp_fmt(a1, a2, "]");
}
return result;
}

original_code:
int amp_format_array(char **pos, char *limit, amp_array_t *array)
{
  int e;
  if ((e = amp_fmt(pos, limit, "@%c[", type_to_code(array->type)))) return e;
  if ((e = amp_format_value(pos, limit, array->values, array->size))) return e;
  if ((e = amp_fmt(pos, limit, "]"))) return e;
  return 0;
}

canonical_original_code:
int amp_format_array(char **pos, char *limit, struct amp_array_st *array)
{
  int e;
  if ((e = amp_fmt(pos, limit, "@%c[", type_to_code(array->type)))) return e;
  if ((e = amp_format_value(pos, limit, array->values, array->size))) return e;
  if ((e = amp_fmt(pos, limit, "]"))) return e;
  return 0;
}

variable_types:
pos
char **

variable_types:
limit
char *

variable_types: e int

return_type: int


decompiled_code:
__int64 __fastcall amp_encode_sizeof_array(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // r12

if ( !*(_QWORD *)(a1 + 8) )
return 9LL;
v1 = 0LL;
v2 = 9LL;
do
{
v2 += amp_encode_sizeof(
*(unsigned int *)(a1 + 16 * ((int)v1 + 1LL) + 8),
*(_QWORD *)(a1 + 16 * ((int)v1 + 1LL) + 16));
++v1;
}
while ( *(_QWORD *)(a1 + 8) > v1 );
return v2;
}

original_code:
size_t amp_encode_sizeof_array(amp_array_t *array)
{
  size_t result = 9;
  for (int i = 0; i < array->size; i++)
  {
    result += amp_encode_sizeof(array->values[i]);
  }
  return result;
}

canonical_original_code:
unsigned long amp_encode_sizeof_array(struct amp_array_st *array)
{
  unsigned long result = 9;
  for (int i = 0; i < array->size; i++)
  {
    result += amp_encode_sizeof(array->values[i]);
  }
  return result;
}


original_code:
size_t amp_encode_array(amp_array_t *array, char *out)
{
  out[0] = (uint8_t) (0xf0);
  *((uint32_t *) (out + 5)) = htonl(array->size);
  out[9] = (uint8_t) type_to_amqp_code(array->type);
  char *vout = out + 10;
  for (int i = 0; i < array->size; i++)
  {
    char *codeptr = vout - 1;
    char codeval = *codeptr;
    vout += amp_encode(array->values[i], vout-1) - 1;
    *codeptr = codeval;
  }
  *((uint32_t *) (out + 1)) = htonl(vout - out - 5);
  return vout - out;
}

canonical_original_code:
unsigned long amp_encode_array(struct amp_array_st *array, char *out)
{
  out[0] = (unsigned char) (0xf0);
  *((unsigned int *) (out + 5)) = htonl(array->size);
  out[9] = (unsigned char) type_to_amqp_code(array->type);
  char *vout = out + 10;
  for (int i = 0; i < array->size; i++)
  {
    char *codeptr = vout - 1;
    char codeval = *codeptr;
    vout += amp_encode(array->values[i], vout-1) - 1;
    *codeptr = codeval;
  }
  *((unsigned int *) (out + 1)) = htonl(vout - out - 5);
  return vout - out;
}

variable_types:
vout
char *

variable_types:
codeptr
char *

variable_types: codeval char


decompiled_code:
void __fastcall __noreturn amp_vfatal(char *format, __gnuc_va_list arg)
{
vfprintf(stderr, format, arg);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(char *format, __gnuc_va_list arg)
{
vfprintf(stderr, format, arg);
exit(-1);
}

original_code:
void amp_vfatal(char *fmt, va_list ap)
{
  vfprintf(stderr, fmt, ap);
  exit(-1);
}

canonical_original_code:
void amp_vfatal(char *fmt, __builtin_va_list ap)
{
  vfprintf(stderr, fmt, ap);
  exit(-1);
}

variable_types:
fmt
char *


decompiled_code:
void __noreturn amp_fatal(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
amp_vfatal(a1, arg);
}

canonical_decompiled_code:
void __noreturn func1(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
func0(a1, arg);
}

original_code:
void amp_fatal(char *fmt, ...)
{
  va_list ap;
  __builtin_va_start(ap,fmt);
  amp_vfatal(fmt, ap);
  __builtin_va_end(ap);
}

canonical_original_code:
void amp_fatal(char *fmt, ...)
{
  __builtin_va_list ap;
  __builtin_va_start(ap,fmt);
  amp_vfatal(fmt, ap);
  __builtin_va_end(ap);
}


original_code:
size_t amp_read_frame(amp_frame_t *frame, char *bytes, size_t available)
{
  if (available >= (8)) {
    size_t size = htonl(*((uint32_t *) bytes));
    if (available >= size)
    {
      int doff = bytes[4]*4;
      frame->size = size - doff;
      frame->ex_size = doff - (8);
      frame->type = bytes[5];
      frame->channel = htons(*((uint16_t *) (bytes + 6)));
      frame->extended = bytes + (8);
      frame->payload = bytes + doff;
      return size;
    }
  }
  return 0;
}

canonical_original_code:
unsigned long amp_read_frame(struct amp_frame_t *frame, char *bytes, unsigned long available)
{
  if (available >= (8)) {
    unsigned long size = htonl(*((uint32_t *) bytes));
    if (available >= size)
    {
      int doff = bytes[4]*4;
      frame->size = size - doff;
      frame->ex_size = doff - (8);
      frame->type = bytes[5];
      frame->channel = htons(*((unsigned short *) (bytes + 6)));
      frame->extended = bytes + (8);
      frame->payload = bytes + doff;
      return size;
    }
  }
  return 0;
}

variable_types: struct amp_frame_t { unsigned char type; unsigned short channel; unsigned long ex_size; char *extended; unsigned long size; char *payload; } *

variable_types:
bytes
char *

variable_types:
available
unsigned long

variable_types:
size
unsigned long

variable_types: doff int


decompiled_code:
size_t __fastcall amp_write_frame(__int64 a1, size_t a2, int a3, int a4, int a5, int a6, int a7, size_t n, void *src, size_t a10, void *a11)
{
size_t v11; // rbp

v11 = a10 + n + 8;
if ( v11 > a2 )
return 0LL;
*(_DWORD *)a1 = _bswap_32(v11);
*(_BYTE *)(a1 + 4) = ((n + 7) >> 2) + 1;
*(_BYTE *)(a1 + 5) = a7;
*(_WORD *)(a1 + 6) = _bswap_16(SHIWORD(a7));
memmove((void *)(a1 + 8), src, n);
memmove((void *)(a1 + (int)(4 * (((n + 7) >> 2) + 1))), a11, a10);
return v11;
}

original_code:
size_t amp_write_frame(char *bytes, size_t available, amp_frame_t frame)
{
  size_t size = (8) + frame.ex_size + frame.size;
  if (size <= available)
  {
    *((uint32_t *) bytes) = ntohl(size);
    int doff = (frame.ex_size + (8) - 1)/4 + 1;
    bytes[4] = doff;
    bytes[5] = frame.type;
    *((uint16_t *) (bytes + 6)) = ntohs(frame.channel);
    memmove(bytes + (8), frame.extended, frame.ex_size);
    memmove(bytes + 4*doff, frame.payload, frame.size);
    return size;
  } else {
    return 0;
  }
}

canonical_original_code:
unsigned long amp_write_frame(char *bytes, unsigned long available, struct amp_frame_t frame)
{
  unsigned long size = (8) + frame.ex_size + frame.size;
  if (size <= available)
  {
    *((unsigned int *) bytes) = ntohl(size);
    int doff = (frame.ex_size + (8) - 1)/4 + 1;
    bytes[4] = doff;
    bytes[5] = frame.type;
    *((unsigned short *) (bytes + 6)) = ntohs(frame.channel);
    memmove(bytes + (8), frame.extended, frame.ex_size);
    memmove(bytes + 4*doff, frame.payload, frame.size);
    return size;
  } else {
    return 0;
  }
}

variable_types: struct amp_frame_t { unsigned char type; unsigned short channel; unsigned long ex_size; char *extended; unsigned long size; char *payload; }


repo: rhyttr/SocketCAN

decompiled_code:
__int64 __fastcall test_sockets(int a1, int a2, int a3)
{
int v4; // eax
int v5; // ebp
__int64 v6; // r14
int v7; // er12
__int64 v8; // r13
int i; // ebx
__int64 v11; // [rsp+8h] [rbp-F0h]
int nfds; // [rsp+10h] [rbp-E8h]
unsigned int v13; // [rsp+18h] [rbp-E0h]
int buf; // [rsp+20h] [rbp-D8h] BYREF
char v15; // [rsp+24h] [rbp-D4h]
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

v4 = a1;
if ( a2 >= a1 )
v4 = a2;
nfds = v4 + 1;
buf = a3;
v15 = 0;
if ( write(a1, &buf, 0x10uLL) < 0 )
{
perror("write");
exit(1);
}
v5 = a1 / 64;
v6 = 1LL << (a1 % 64);
v7 = a2 / 64;
v8 = 1LL << (a2 % 64);
LODWORD(v11) = 0;
v13 = 0;
LABEL_5:
for ( i = v11; ; ++i )
{
LODWORD(v11) = i;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v5] |= v6;
readfds.fds_bits[v7] |= v8;
timeout.tv_sec = 0LL;
timeout.tv_usec = 50000LL;
if ( select(nfds, &readfds, 0LL, 0LL, &timeout) < 0 )
{
perror("select");
exit(1);
}
if ( (readfds.fds_bits[v5] & v6) == 0 )
break;
if ( (int)read(a1, &buf, 0x10uLL) < 0 )
{
perror("read");
exit(1);
}
if ( buf != a3 )
{
fwrite("received wrong can_id!\n", 1uLL, 0x17uLL, _bss_start);
exit(1);
}
++v13;
if ( (readfds.fds_bits[v7] & v8) == 0 )
goto LABEL_5;
LABEL_11:
if ( (int)read(a2, &buf, 0x10uLL) < 0 )
{
perror("read");
exit(1);
}
if ( buf != a3 )
{
fwrite("received wrong can_id!\n", 1uLL, 0x17uLL, _bss_start);
exit(1);
}
}
if ( (readfds.fds_bits[v7] & v8) != 0 )
goto LABEL_11;
return (v11 << 32) | v13;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, int a3)
{
int v4; // eax
int v5; // ebp
__int64 v6; // r14
int v7; // er12
__int64 v8; // r13
int i; // ebx
__int64 v11; // [rsp+8h] [rbp-F0h]
int nfds; // [rsp+10h] [rbp-E8h]
unsigned int v13; // [rsp+18h] [rbp-E0h]
int buf; // [rsp+20h] [rbp-D8h] BYREF
char v15; // [rsp+24h] [rbp-D4h]
struct timeval timeout; // [rsp+30h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+40h] [rbp-B8h] BYREF

v4 = a1;
if ( a2 >= a1 )
v4 = a2;
nfds = v4 + 1;
buf = a3;
v15 = 0;
if ( write(a1, &buf, 0x10uLL) < 0 )
{
perror("write");
exit(1);
}
v5 = a1 / 64;
v6 = 1LL << (a1 % 64);
v7 = a2 / 64;
v8 = 1LL << (a2 % 64);
LODWORD(v11) = 0;
v13 = 0;
LABEL_5:
for ( i = v11; ; ++i )
{
LODWORD(v11) = i;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v5] |= v6;
readfds.fds_bits[v7] |= v8;
timeout.tv_sec = 0LL;
timeout.tv_usec = 50000LL;
if ( select(nfds, &readfds, 0LL, 0LL, &timeout) < 0 )
{
perror("select");
exit(1);
}
if ( (readfds.fds_bits[v5] & v6) == 0 )
break;
if ( (int)read(a1, &buf, 0x10uLL) < 0 )
{
perror("read");
exit(1);
}
if ( buf != a3 )
{
fwrite("received wrong can_id!\n", 1uLL, 0x17uLL, _bss_start);
exit(1);
}
++v13;
if ( (readfds.fds_bits[v7] & v8) == 0 )
goto LABEL_5;
LABEL_11:
if ( (int)read(a2, &buf, 0x10uLL) < 0 )
{
perror("read");
exit(1);
}
if ( buf != a3 )
{
fwrite("received wrong can_id!\n", 1uLL, 0x17uLL, _bss_start);
exit(1);
}
}
if ( (readfds.fds_bits[v7] & v8) != 0 )
goto LABEL_11;
return (v11 << 32) | v13;
}

original_code:
struct rxs test_sockets(int s, int t, canid_t can_id)
{
 fd_set rdfs;
 struct timeval tv;
 int m = (s > t ? s : t)+1;
 int have_rx = 1;
 struct can_frame frame;
 struct rxs rx;
 int ret;
 frame.can_id = can_id;
 frame.can_dlc = 0;
 if (write(s, &frame, sizeof(frame)) < 0) {
  perror("write");
  exit(1);
 }
 rx.s = rx.t = 0;
 while (have_rx) {
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rdfs)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&rdfs)->__fds_bits)[((t) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((t) % (8 * (int) sizeof (__fd_mask)))))));
  tv.tv_sec = 0;
  tv.tv_usec = 50000;
  have_rx = 0;
  ret = select(m, &rdfs, ((void *)0), ((void *)0), &tv);
  if (ret < 0) {
   perror("select");
   exit(1);
  }
  if (((((&rdfs)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   have_rx = 1;
   ret = read(s, &frame, sizeof(struct can_frame));
   if (ret < 0) {
    perror("read");
    exit(1);
   }
   if (frame.can_id != can_id) {
    fprintf(stderr, "received wrong can_id!\n");
    exit(1);
   }
   rx.s++;
  }
  if (((((&rdfs)->__fds_bits)[((t) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   have_rx = 1;
   ret = read(t, &frame, sizeof(struct can_frame));
   if (ret < 0) {
    perror("read");
    exit(1);
   }
   if (frame.can_id != can_id) {
    fprintf(stderr, "received wrong can_id!\n");
    exit(1);
   }
   rx.t++;
  }
 }
 return rx;
}

canonical_original_code:
struct rxs test_sockets(int s, int t, unsigned int can_id)
{
 struct fd_set rdfs;
 struct timeval tv;
 int m = (s > t ? s : t)+1;
 int have_rx = 1;
 struct can_frame frame;
 struct rxs rx;
 int ret;
 frame.can_id = can_id;
 frame.can_dlc = 0;
 if (write(s, &frame, sizeof(frame)) < 0) {
  perror("write");
  exit(1);
 }
 rx.s = rx.t = 0;
 while (have_rx) {
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdfs)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rdfs)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
  ((void) (((&rdfs)->__fds_bits)[((t) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((t) % (8 * (int) sizeof (__fd_mask)))))));
  tv.tv_sec = 0;
  tv.tv_usec = 50000;
  have_rx = 0;
  ret = select(m, &rdfs, ((void *)0), ((void *)0), &tv);
  if (ret < 0) {
   perror("select");
   exit(1);
  }
  if (((((&rdfs)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   have_rx = 1;
   ret = read(s, &frame, sizeof(struct can_frame));
   if (ret < 0) {
    perror("read");
    exit(1);
   }
   if (frame.can_id != can_id) {
    fprintf(stderr, "received wrong can_id!\n");
    exit(1);
   }
   rx.s++;
  }
  if (((((&rdfs)->__fds_bits)[((t) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((t) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
   have_rx = 1;
   ret = read(t, &frame, sizeof(struct can_frame));
   if (ret < 0) {
    perror("read");
    exit(1);
   }
   if (frame.can_id != can_id) {
    fprintf(stderr, "received wrong can_id!\n");
    exit(1);
   }
   rx.t++;
  }
 }
 return rx;
}

variable_types: s int

variable_types: t int

variable_types:
rdfs
struct fd_set { long __fds_bits[-1]; }

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

variable_types: m int

variable_types: struct can_frame { unsigned int can_id; unsigned char can_dlc; unsigned char data[8]; }

variable_types:
rx
struct rxs { int s; int t; }

variable_types: ret int

return_type: struct rxs { int s; int t; }


setopts

decompiled_code:
int __fastcall setopts(int fd, unsigned int a2, unsigned int a3)
{
unsigned int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = a2;
v4 = a3;
setsockopt(fd, 101, 3, optval, 4u);
setsockopt(fd, 101, 4, &v4, 4u);
return printf("check loopback %d recv_own_msgs %d ... ", optval[0], v4);
}

canonical_decompiled_code:
int __fastcall func8(int fd, unsigned int a2, unsigned int a3)
{
unsigned int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = a2;
v4 = a3;
setsockopt(fd, 101, 3, optval, 4u);
setsockopt(fd, 101, 4, &v4, 4u);
return printf("check loopback %d recv_own_msgs %d ... ", optval[0], v4);
}

original_code:
void setopts(int s, int loopback, int recv_own_msgs)
{
 setsockopt(s, (100 + 1), CAN_RAW_LOOPBACK,
     &loopback, sizeof(loopback));
 setsockopt(s, (100 + 1), CAN_RAW_RECV_OWN_MSGS,
     &recv_own_msgs, sizeof(recv_own_msgs));
 printf("check loopback %d recv_own_msgs %d ... ",
        loopback, recv_own_msgs);
}

variable_types: loopback int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v5; // ebp
int v6; // er12
__int64 v7; // rax
char v8[16]; // [rsp+0h] [rbp-58h] BYREF
int v9; // [rsp+10h] [rbp-48h]
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

if ( argc == 2 )
{
v5 = socket(29, 3, 1);
if ( v5 < 0 || (v6 = socket(29, 3, 1), v6 < 0) )
{
perror("socket");
v3 = 1;
}
else
{
strcpy(v8, argv[1]);
if ( ioctl(v5, 0x8933uLL, v8) < 0 )
{
perror("SIOCGIFINDEX");
v3 = 1;
}
else
{
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_family = 29;
if ( bind(v5, &addr, 0x10u) < 0 || bind(v6, &addr, 0x10u) < 0 )
{
perror("bind");
v3 = 1;
}
else
{
puts("Starting PF_CAN frame flow test.");
printf("checking socket default settings ... ");
if ( test_sockets(v5, v6, 832) != 0x100000000LL )
goto LABEL_21;
puts("ok.");
setopts(v5, 0, 0);
if ( test_sockets(v5, v6, 833) )
goto LABEL_21;
puts("ok.");
setopts(v5, 0, 1u);
v7 = test_sockets(v5, v6, 834);
v3 = v7 | HIDWORD(v7);
if ( v7 )
goto LABEL_21;
puts("ok.");
setopts(v5, 1u, 0);
if ( test_sockets(v5, v6, 835) == 0x100000000LL
&& (puts("ok."), setopts(v5, 1u, 1u), test_sockets(v5, v6, 836) == 0x100000001LL) )
{
puts("ok.");
puts("PF_CAN frame flow test was successful.");
close(v5);
close(v6);
}
else
{
LABEL_21:
puts("failure!");
v3 = 1;
}
}
}
}
}
else
{
fprintf(_bss_start, "Usage: %s <device>\n", *argv);
v3 = 1;
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v5; // ebp
int v6; // er12
__int64 v7; // rax
char v8[16]; // [rsp+0h] [rbp-58h] BYREF
int v9; // [rsp+10h] [rbp-48h]
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

if ( argc == 2 )
{
v5 = socket(29, 3, 1);
if ( v5 < 0 || (v6 = socket(29, 3, 1), v6 < 0) )
{
perror("socket");
v3 = 1;
}
else
{
strcpy(v8, argv[1]);
if ( ioctl(v5, 0x8933uLL, v8) < 0 )
{
perror("SIOCGIFINDEX");
v3 = 1;
}
else
{
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_family = 29;
if ( bind(v5, &addr, 0x10u) < 0 || bind(v6, &addr, 0x10u) < 0 )
{
perror("bind");
v3 = 1;
}
else
{
puts("Starting PF_CAN frame flow test.");
printf("checking socket default settings ... ");
if ( func7(v5, v6, 832) != 0x100000000LL )
goto LABEL_21;
puts("ok.");
func8(v5, 0, 0);
if ( func7(v5, v6, 833) )
goto LABEL_21;
puts("ok.");
func8(v5, 0, 1u);
v7 = func7(v5, v6, 834);
v3 = v7 | HIDWORD(v7);
if ( v7 )
goto LABEL_21;
puts("ok.");
func8(v5, 1u, 0);
if ( func7(v5, v6, 835) == 0x100000000LL
&& (puts("ok."), func8(v5, 1u, 1u), func7(v5, v6, 836) == 0x100000001LL) )
{
puts("ok.");
puts("PF_CAN frame flow test was successful.");
close(v5);
close(v6);
}
else
{
LABEL_21:
puts("failure!");
v3 = 1;
}
}
}
}
}
else
{
fprintf(_bss_start, "Usage: %s <device>\n", *argv);
v3 = 1;
}
return v3;
}

original_code:
int main(int argc, char **argv)
{
 int s, t;
 struct sockaddr_can addr;
 struct ifreq ifr;
 struct rxs rx;
 if (argc != 2) {
  fprintf(stderr, "Usage: %s <device>\n", argv[0]);
  return 1;
 }
 if ((s = socket(29, SOCK_RAW, 1)) < 0) {
  perror("socket");
  return 1;
 }
 if ((t = socket(29, SOCK_RAW, 1)) < 0) {
  perror("socket");
  return 1;
 }
 strcpy(ifr.ifr_ifrn.ifrn_name, argv[1]);
 if (ioctl(s, 0x8933, &ifr) < 0) {
  perror("SIOCGIFINDEX");
  return 1;
 }
 addr.can_ifindex = ifr.ifr_ifru.ifru_ivalue;
 addr.can_family = 29;
 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 printf("Starting PF_CAN frame flow test.\n");
 printf("checking socket default settings ... ");
 rx = test_sockets(s, t, 0x340);
 if (rx.s == 0 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 0, 0);
 rx = test_sockets(s, t, 0x341);
 if (rx.s == 0 && rx.t == 0)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 0, 1);
 rx = test_sockets(s, t, 0x342);
 if (rx.s == 0 && rx.t == 0)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 1, 0);
 rx = test_sockets(s, t, 0x343);
 if (rx.s == 0 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 1, 1);
 rx = test_sockets(s, t, 0x344);
 if (rx.s == 1 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 printf("PF_CAN frame flow test was successful.\n");
 close(s);
 close(t);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int s; int t;
 struct sockaddr_can addr;
 struct ifreq ifr;
 struct rxs rx;
 if (argc != 2) {
  fprintf(stderr, "Usage: %s <device>\n", argv[0]);
  return 1;
 }
 if ((s = socket(29, SOCK_RAW, 1)) < 0) {
  perror("socket");
  return 1;
 }
 if ((t = socket(29, SOCK_RAW, 1)) < 0) {
  perror("socket");
  return 1;
 }
 strcpy(ifr.ifr_ifrn.ifrn_name, argv[1]);
 if (ioctl(s, 0x8933, &ifr) < 0) {
  perror("SIOCGIFINDEX");
  return 1;
 }
 addr.can_ifindex = ifr.ifr_ifru.ifru_ivalue;
 addr.can_family = 29;
 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 printf("Starting PF_CAN frame flow test.\n");
 printf("checking socket default settings ... ");
 rx = test_sockets(s, t, 0x340);
 if (rx.s == 0 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 0, 0);
 rx = test_sockets(s, t, 0x341);
 if (rx.s == 0 && rx.t == 0)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 0, 1);
 rx = test_sockets(s, t, 0x342);
 if (rx.s == 0 && rx.t == 0)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 1, 0);
 rx = test_sockets(s, t, 0x343);
 if (rx.s == 0 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 setopts(s, 1, 1);
 rx = test_sockets(s, t, 0x344);
 if (rx.s == 1 && rx.t == 1)
  printf("ok.\n");
 else {
  printf("failure!\n");
  return 1;
 }
 printf("PF_CAN frame flow test was successful.\n");
 close(s);
 close(t);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct sockaddr_can { unsigned short can_family; int can_ifindex; union { struct { unsigned int rx_id; } tp; } can_addr; }

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


decompiled_code:
__int64 __fastcall addattr_l(_DWORD *a1, unsigned int a2, __int16 a3, const void *a4, int a5)
{
unsigned int v6; // edi
unsigned int v7; // ebp
_WORD *v8; // rdi
__int64 result; // rax

v6 = (*a1 + 3) & 0xFFFFFFFC;
v7 = (a5 + 7) & 0xFFFFFFFC;
if ( v6 + v7 > a2 )
{
fprintf(_bss_start, "addattr_l: message exceeded bound of %d\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v8 = (_WORD *)((char *)a1 + v6);
v8[1] = a3;
*v8 = a5 + 4;
memcpy(v8 + 2, a4, a5);
*a1 = ((*a1 + 3) & 0xFFFFFFFC) + v7;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, unsigned int a2, __int16 a3, const void *a4, int a5)
{
unsigned int v6; // edi
unsigned int v7; // ebp
_WORD *v8; // rdi
__int64 result; // rax

v6 = (*a1 + 3) & 0xFFFFFFFC;
v7 = (a5 + 7) & 0xFFFFFFFC;
if ( v6 + v7 > a2 )
{
fprintf(_bss_start, "addattr_l: message exceeded bound of %d\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v8 = (_WORD *)((char *)a1 + v6);
v8[1] = a3;
*v8 = a5 + 4;
memcpy(v8 + 2, a4, a5);
*a1 = ((*a1 + 3) & 0xFFFFFFFC) + v7;
result = 0LL;
}
return result;
}

original_code:
int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
       int alen)
{
 int len = (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (alen));
 struct rtattr *rta;
 if (( ((n->nlmsg_len)+4U -1) & ~(4U -1) ) + ( ((len)+4U -1) & ~(4U -1) ) > maxlen) {
  fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
   maxlen);
  return -1;
 }
 rta = ((struct rtattr *)(((void *) (n)) + ( (((n)->nlmsg_len)+4U -1) & ~(4U -1) )));
 rta->rta_type = type;
 rta->rta_len = len;
 memcpy(((void*)(((char*)(rta)) + (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (0)))), data, alen);
 n->nlmsg_len = ( ((n->nlmsg_len)+4U -1) & ~(4U -1) ) + ( ((len)+4U -1) & ~(4U -1) );
 return 0;
}

canonical_original_code:
int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
{
 int len = (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (alen));
 struct rtattr *rta;
 if (( ((n->nlmsg_len)+4U -1) & ~(4U -1) ) + ( ((len)+4U -1) & ~(4U -1) ) > maxlen) {
  fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
   maxlen);
  return -1;
 }
 rta = ((struct rtattr *)(((void *) (n)) + ( (((n)->nlmsg_len)+4U -1) & ~(4U -1) )));
 rta->rta_type = type;
 rta->rta_len = len;
 memcpy(((void *)(((char *)(rta)) + (( ((sizeof(struct rtattr))+4U -1) & ~(4U -1) ) + (0)))), data, alen);
 n->nlmsg_len = ( ((n->nlmsg_len)+4U -1) & ~(4U -1) ) + ( ((len)+4U -1) & ~(4U -1) );
 return 0;
}

variable_types: struct nlmsghdr { unsigned int nlmsg_len; unsigned short nlmsg_type; unsigned short nlmsg_flags; unsigned int nlmsg_seq; unsigned int nlmsg_pid; } *

variable_types: maxlen int

variable_types: type int

variable_types:
data
void *

variable_types: alen int

variable_types: len int

variable_types: struct rtattr { unsigned short rta_len; unsigned short rta_type; } *


original_code:
canid_t calc_id(int testcase)
{
 canid_t id = 0x123;
 if (testcase & 1)
  id |= 0x80000000U;
 if (testcase & 2)
  id |= 0x40000000U;
 return id;
}

canonical_original_code:
unsigned int calc_id(int testcase)
{
 unsigned int id = 0x123;
 if (testcase & 1)
  id |= 0x80000000U;
 if (testcase & 2)
  id |= 0x40000000U;
 return id;
}

variable_types: testcase int

variable_types:
id
unsigned int

return_type: unsigned int


sigterm

decompiled_code:
void __fastcall sigterm(unsigned int a1)
{
printf("got signal %d\n", a1);
running = 0;
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
printf("got signal %d\n", a1);
running = 0;
}

original_code:
void sigterm(int signo)
{
 printf("got signal %d\n", signo);
 running = 0;
}

variable_types: signo int


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(stderr, "Usage: %s [can-interface]\n", a1);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
return fprintf(stderr, "Usage: %s [can-interface]\n", a1);
}

original_code:
void print_usage(char *prg)
{
 fprintf(stderr, "Usage: %s [can-interface]\n", prg);
}

variable_types:
prg
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er15
unsigned int v5; // er14
int v6; // eax
int v7; // ebp
int result; // eax
char *v9; // rax
__int64 v10; // rbx
unsigned int type; // [rsp+Ch] [rbp-8Ch]
int buf; // [rsp+10h] [rbp-88h] BYREF
unsigned __int8 v13; // [rsp+14h] [rbp-84h]
char v14[8]; // [rsp+18h] [rbp-80h]
char dest[16]; // [rsp+20h] [rbp-78h] BYREF
int v16; // [rsp+30h] [rbp-68h]
struct sockaddr addr; // [rsp+50h] [rbp-48h] BYREF

signal(15, (__sighandler_t)sigterm);
signal(1, (__sighandler_t)sigterm);
v3 = 0;
v4 = 1;
type = 3;
v5 = 29;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "f:t:p:v");
if ( v6 == -1 )
break;
if ( v6 == 112 )
{
v4 = atoi(optarg);
}
else if ( v6 > 112 )
{
if ( v6 == 116 )
{
type = atoi(optarg);
}
else
{
if ( v6 != 118 )
goto LABEL_13;
v3 = 1;
}
}
else if ( v6 != 63 )
{
if ( v6 == 102 )
v5 = atoi(optarg);
else
LABEL_13:
fprintf(stderr, "Unknown option %c\n", (unsigned int)v6);
}
}
if ( optind == argc )
{
v9 = __xpg_basename((char *)*argv);
print_usage(v9);
exit(0);
}
printf("interface = %s, family = %d, type = %d, proto = %d\n", argv[optind], v5, type, v4);
v7 = socket(v5, type, v4);
s = v7;
if ( v7 < 0 )
{
perror("socket");
result = 1;
}
else
{
addr.sa_family = v5;
strcpy(dest, argv[optind]);
ioctl(v7, 0x8933uLL, dest);
*(_DWORD *)&addr.sa_data[2] = v16;
if ( bind(v7, &addr, 0x10u) < 0 )
{
perror("bind");
result = 1;
}
else
{
result = running;
if ( running )
{
while ( (int)read(s, &buf, 0x10uLL) >= 0 )
{
if ( v3 )
{
printf("%03X: ", buf & 0x1FFFFFFF);
if ( (buf & 0x40000000) != 0 )
{
printf("remote request");
}
else
{
printf("[%d]", v13);
if ( v13 )
{
v10 = 0LL;
do
printf(" %02X", (unsigned __int8)v14[v10++]);
while ( v13 > (int)v10 );
}
}
putchar(10);
}
++buf;
write(s, &buf, 0x10uLL);
result = running;
if ( !running )
return result;
}
perror("read");
result = 1;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
unsigned int v4; // er15
unsigned int v5; // er14
int v6; // eax
int v7; // ebp
int result; // eax
char *v9; // rax
__int64 v10; // rbx
unsigned int type; // [rsp+Ch] [rbp-8Ch]
int buf; // [rsp+10h] [rbp-88h] BYREF
unsigned __int8 v13; // [rsp+14h] [rbp-84h]
char v14[8]; // [rsp+18h] [rbp-80h]
char dest[16]; // [rsp+20h] [rbp-78h] BYREF
int v16; // [rsp+30h] [rbp-68h]
struct sockaddr addr; // [rsp+50h] [rbp-48h] BYREF

signal(15, (__sighandler_t)sigterm);
signal(1, (__sighandler_t)sigterm);
v3 = 0;
v4 = 1;
type = 3;
v5 = 29;
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "f:t:p:v");
if ( v6 == -1 )
break;
if ( v6 == 112 )
{
v4 = atoi(optarg);
}
else if ( v6 > 112 )
{
if ( v6 == 116 )
{
type = atoi(optarg);
}
else
{
if ( v6 != 118 )
goto LABEL_13;
v3 = 1;
}
}
else if ( v6 != 63 )
{
if ( v6 == 102 )
v5 = atoi(optarg);
else
LABEL_13:
fprintf(stderr, "Unknown option %c\n", (unsigned int)v6);
}
}
if ( optind == argc )
{
v9 = __xpg_basename((char *)*argv);
func8(v9);
exit(0);
}
printf("interface = %s, family = %d, type = %d, proto = %d\n", argv[optind], v5, type, v4);
v7 = socket(v5, type, v4);
s = v7;
if ( v7 < 0 )
{
perror("socket");
result = 1;
}
else
{
addr.sa_family = v5;
strcpy(dest, argv[optind]);
ioctl(v7, 0x8933uLL, dest);
*(_DWORD *)&addr.sa_data[2] = v16;
if ( bind(v7, &addr, 0x10u) < 0 )
{
perror("bind");
result = 1;
}
else
{
result = running;
if ( running )
{
while ( (int)read(s, &buf, 0x10uLL) >= 0 )
{
if ( v3 )
{
printf("%03X: ", buf & 0x1FFFFFFF);
if ( (buf & 0x40000000) != 0 )
{
printf("remote request");
}
else
{
printf("[%d]", v13);
if ( v13 )
{
v10 = 0LL;
do
printf(" %02X", (unsigned __int8)v14[v10++]);
while ( v13 > (int)v10 );
}
}
putchar(10);
}
++buf;
write(s, &buf, 0x10uLL);
result = running;
if ( !running )
return result;
}
perror("read");
result = 1;
}
}
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 int family = 29, type = SOCK_RAW, proto = 1;
 int opt;
 struct sockaddr_can addr;
 struct ifreq ifr;
 struct can_frame frame;
 int nbytes, i;
 int verbose = 0;
 signal(15, sigterm);
 signal(1, sigterm);
 while ((opt = getopt(argc, argv, "f:t:p:v")) != -1) {
  switch (opt) {
  case 'f':
   family = atoi(optarg);
   break;
  case 't':
   type = atoi(optarg);
   break;
  case 'p':
   proto = atoi(optarg);
   break;
  case 'v':
   verbose = 1;
   break;
  case '?':
   break;
  default:
   fprintf(stderr, "Unknown option %c\n", opt);
   break;
  }
 }
 if (optind == argc) {
  print_usage(__xpg_basename(argv[0]));
  exit(0);
 }
 printf("interface = %s, family = %d, type = %d, proto = %d\n",
        argv[optind], family, type, proto);
 if ((s = socket(family, type, proto)) < 0) {
  perror("socket");
  return 1;
 }
 addr.can_family = family;
 strcpy(ifr.ifr_ifrn.ifrn_name, argv[optind]);
 ioctl(s, 0x8933, &ifr);
 addr.can_ifindex = ifr.ifr_ifru.ifru_ivalue;
 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 while (running) {
  if ((nbytes = read(s, &frame, sizeof(frame))) < 0) {
   perror("read");
   return 1;
  }
  if (verbose) {
   printf("%03X: ", frame.can_id & 0x1FFFFFFFU);
   if (frame.can_id & 0x40000000U) {
    printf("remote request");
   } else {
    printf("[%d]", frame.can_dlc);
    for (i = 0; i < frame.can_dlc; i++) {
     printf(" %02X", frame.data[i]);
    }
   }
   printf("\n");
  }
  frame.can_id++;
  write(s, &frame, sizeof(frame));
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int family = 29; int type = SOCK_RAW; int proto = 1;
 int opt;
 struct sockaddr_can addr;
 struct ifreq ifr;
 struct can_frame frame;
 int nbytes; int i;
 int verbose = 0;
 signal(15, sigterm);
 signal(1, sigterm);
 while ((opt = getopt(argc, argv, "f:t:p:v")) != -1) {
  switch (opt) {
  case 'f':
   family = atoi(optarg);
   break;
  case 't':
   type = atoi(optarg);
   break;
  case 'p':
   proto = atoi(optarg);
   break;
  case 'v':
   verbose = 1;
   break;
  case '?':
   break;
  default:
   fprintf(stderr, "Unknown option %c\n", opt);
   break;
  }
 }
 if (optind == argc) {
  print_usage(__xpg_basename(argv[0]));
  exit(0);
 }
 printf("interface = %s, family = %d, type = %d, proto = %d\n",
        argv[optind], family, type, proto);
 if ((s = socket(family, type, proto)) < 0) {
  perror("socket");
  return 1;
 }
 addr.can_family = family;
 strcpy(ifr.ifr_ifrn.ifrn_name, argv[optind]);
 ioctl(s, 0x8933, &ifr);
 addr.can_ifindex = ifr.ifr_ifru.ifru_ivalue;
 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  perror("bind");
  return 1;
 }
 while (running) {
  if ((nbytes = read(s, &frame, sizeof(frame))) < 0) {
   perror("read");
   return 1;
  }
  if (verbose) {
   printf("%03X: ", frame.can_id & 0x1FFFFFFFU);
   if (frame.can_id & 0x40000000U) {
    printf("remote request");
   } else {
    printf("[%d]", frame.can_dlc);
    for (i = 0; i < frame.can_dlc; i++) {
     printf(" %02X", frame.data[i]);
    }
   }
   printf("\n");
  }
  frame.can_id++;
  write(s, &frame, sizeof(frame));
 }
 return 0;
}

variable_types: family int

variable_types: proto int

variable_types: opt int

variable_types: struct can_frame { unsigned int can_id; unsigned char can_dlc; unsigned char data[8]; }

variable_types: nbytes int

variable_types: i int

variable_types: verbose int

user_defined_types: struct sockaddr_can { unsigned short can_family; int can_ifindex; union { struct { unsigned int rx_id; } tp; } can_addr; }

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

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }


use

decompiled_code:
int __fastcall use(const char *a1)
{
return fprintf(stdout, "%s w/r VID PID size\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(stdout, "%s w/r VID PID size\n", a1);
}

original_code:
void use(char *prog){
 fprintf(stdout,"%s w/r VID PID size\n",prog);
 return;
}

variable_types:
prog
char *


decompiled_code:
int __fastcall use(const char *a1)
{
return fprintf(stderr, "Use: %s TTYS0_DEV\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(stderr, "Use: %s TTYS0_DEV\n", a1);
}

original_code:
void use(char* name){
 fprintf(stderr,"Use: %s TTYS0_DEV\n",name);
 return;
}

canonical_original_code:
void use(char *name){
 fprintf(stderr,"Use: %s TTYS0_DEV\n",name);
 return;
}

variable_types:
name
char *


decompiled_code:
__int64 __fastcall do_write(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

v4 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v5 = write(fd, buf, v4);
if ( v5 < 0 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
perror("do_write");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

v4 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v5 = write(fd, buf, v4);
if ( v5 < 0 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
perror("do_write");
return 0xFFFFFFFFLL;
}

original_code:
int do_write(int fd, uint8_t *buffer, int len){
 int ret;
 while(len){
  ret=write(fd,buffer,len);
  if (ret<0){
   perror("do_write");
   return -1;
  }
  buffer+=ret;
  len-=ret;
 }
 return 0;
}

canonical_original_code:
int do_write(int fd, unsigned char *buffer, int len){
 int ret;
 while(len){
  ret=write(fd,buffer,len);
  if (ret<0){
   perror("do_write");
   return -1;
  }
  buffer+=ret;
  len-=ret;
 }
 return 0;
}

variable_types: fd int

variable_types:
buffer
unsigned char *

variable_types: ret int


decompiled_code:
__int64 __fastcall do_read(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

v4 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v5 = read(fd, buf, v4);
if ( v5 < 0 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
perror("do_read");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

v4 = a3;
if ( !a3 )
return 0LL;
while ( 1 )
{
v5 = read(fd, buf, v4);
if ( v5 < 0 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
perror("do_read");
return 0xFFFFFFFFLL;
}

original_code:
int do_read(int fd, uint8_t *buffer, int len){
 int ret;
 while(len){
  ret=read(fd,buffer,len);
  if (ret<0){
   perror("do_read");
   return -1;
  }
  buffer+=ret;
  len-=ret;
 }
 return 0;
}

canonical_original_code:
int do_read(int fd, unsigned char *buffer, int len){
 int ret;
 while(len){
  ret=read(fd,buffer,len);
  if (ret<0){
   perror("do_read");
   return -1;
  }
  buffer+=ret;
  len-=ret;
 }
 return 0;
}

variable_types: len int


decompiled_code:
__int64 __fastcall config_port(int fd)
{
int v1; // edx
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-4Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

if ( isatty(fd) )
{
tcgetattr(fd, &termios_p);
cfsetispeed(&termios_p, 0xDu);
cfsetospeed(&termios_p, 0xDu);
termios_p.c_iflag &= 0xFFFFFA84;
termios_p.c_oflag = 0;
termios_p.c_lflag &= 0xFFFF7FB4;
termios_p.c_cflag = termios_p.c_cflag & 0x7FFFFECF | 0x30;
termios_p.c_cc[6] = 1;
termios_p.c_cc[5] = 10;
tcsetattr(fd, 2, &termios_p);
if ( ioctl(fd, 0x5415uLL, &v3) == -1 )
{
perror("setRTS(): TIOCMGET");
result = 0LL;
}
else
{
v3 |= 6u;
v1 = ioctl(fd, 0x5418uLL, &v3);
result = 0LL;
if ( v1 == -1 )
{
perror("setRTS(): TIOCMSET");
result = 0LL;
}
}
}
else
{
fwrite("You must specify a valid serial port\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
int v1; // edx
__int64 result; // rax
int v3; // [rsp+Ch] [rbp-4Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

if ( isatty(fd) )
{
tcgetattr(fd, &termios_p);
cfsetispeed(&termios_p, 0xDu);
cfsetospeed(&termios_p, 0xDu);
termios_p.c_iflag &= 0xFFFFFA84;
termios_p.c_oflag = 0;
termios_p.c_lflag &= 0xFFFF7FB4;
termios_p.c_cflag = termios_p.c_cflag & 0x7FFFFECF | 0x30;
termios_p.c_cc[6] = 1;
termios_p.c_cc[5] = 10;
tcsetattr(fd, 2, &termios_p);
if ( ioctl(fd, 0x5415uLL, &v3) == -1 )
{
perror("setRTS(): TIOCMGET");
result = 0LL;
}
else
{
v3 |= 6u;
v1 = ioctl(fd, 0x5418uLL, &v3);
result = 0LL;
if ( v1 == -1 )
{
perror("setRTS(): TIOCMSET");
result = 0LL;
}
}
}
else
{
fwrite("You must specify a valid serial port\n", 1uLL, 0x25uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int config_port(int fd){
 struct termios options;
 int status;
 if (!isatty(fd)){
  fprintf(stderr,"You must specify a valid serial port\n");
  return -1;
 }
 tcgetattr(fd, &options);
 cfsetispeed(&options, 0000015);
 cfsetospeed(&options, 0000015);
 options.c_iflag &= ~(0000001 | 0000002 | 0000400 |
                    0000100 | 0000010 | 0000020 | 0000040 | 0002000);
 options.c_oflag = 0;
 options.c_lflag &= ~(0000010 | 0000100 | 0000002 | 0100000 | 0000001);
 options.c_cflag &= ~(0000060 | 0000400 | 020000000000);
 options.c_cflag |= 0000060;
 options.c_cc[6] = 1;
 options.c_cc[5] = 10;
 tcsetattr(fd,2,&options);
 if (ioctl(fd, 0x5415, &status) == -1) {
  perror("setRTS(): TIOCMGET");
  return 0;
 }
 status |= 0x004;
 status |= 0x002;
 if (ioctl(fd, 0x5418, &status) == -1) {
  perror("setRTS(): TIOCMSET");
  return 0;
 }
 return 0;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: status int


original_code:
int read_id(int fd){
 uint8_t buffer[]={0x02, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 uint8_t exp_id[]={0x33, 0x37, 0x34, 0x0d};
 int i;
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,buffer,sizeof(exp_id));
 if (!(memcmp(exp_id,buffer,sizeof(exp_id))))
  return 0;
 for (i=0;i<sizeof(exp_id);i++)
  fprintf(stdout, "%.2X ",buffer[i]);
 fprintf(stdout, "\n");
 return -1;
}

canonical_original_code:
int read_id(int fd){
 unsigned char buffer[] = {0x02, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 unsigned char exp_id[] = {0x33, 0x37, 0x34, 0x0d};
 int i;
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,buffer,sizeof(exp_id));
 if (!(memcmp(exp_id,buffer,sizeof(exp_id))))
  return 0;
 for (i=0;i<sizeof(exp_id);i++)
  fprintf(stdout, "%.2X ",buffer[i]);
 fprintf(stdout, "\n");
 return -1;
}

variable_types:
buffer
unsigned char []


decompiled_code:
__int64 __fastcall init_temp(int fd)
{
__int64 v2; // [rsp+0h] [rbp-38h] BYREF
__int64 buf; // [rsp+26h] [rbp-12h] BYREF
__int16 v4; // [rsp+2Eh] [rbp-Ah]

buf = 24834LL;
v4 = 768;
do_write(fd, &buf, 10);
do_read(fd, &v2, 33);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int fd)
{
__int64 v2; // [rsp+0h] [rbp-38h] BYREF
__int64 buf; // [rsp+26h] [rbp-12h] BYREF
__int16 v4; // [rsp+2Eh] [rbp-Ah]

buf = 24834LL;
v4 = 768;
func8(fd, &buf, 10);
func9(fd, &v2, 33);
return 0LL;
}

original_code:
int init_temp(int fd){
 uint8_t buffer[]={0x02, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 uint8_t temp[33];
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,temp,sizeof(temp));
 return 0;
}

canonical_original_code:
int init_temp(int fd){
 unsigned char buffer[] = {0x02, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 unsigned char temp[33];
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,temp,sizeof(temp));
 return 0;
}

variable_types:
temp
unsigned char [33]


original_code:
int read_temp(int fd){
 uint8_t buffer[]={0x02, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 uint8_t temp[33];
 int i;
 int aux=0;
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,temp,sizeof(temp));
 do_read(fd,temp,sizeof(temp));
 if ((temp[0]!=2)||(temp[33 -1]!=3)){
  fprintf(stderr, "Package error:\n");
  for (i=0;i<sizeof(temp);i++)
   fprintf(stdout, "%.2X ",temp[i]);
  fprintf(stdout, "\n");
  return -1;
 }
 for (i=1;i<33 -2;i++)
  aux+=temp[i];
 if ((aux&0xff)!=temp[33 -2]){
  fprintf(stderr, "CRC error:\n");
  for (i=0;i<sizeof(temp);i++)
   fprintf(stdout, "%.2X ",temp[i]);
  fprintf(stdout, "\n");
  return -1;
 }
 fprintf(stdout, "HOLD:%s T1-T2(T5):%s REC:%s LIGHT:%s\n",
   (temp[1]&0x40)?" on":"off",
   (temp[1]&0x20)?" on":"off",
   (temp[1]&0x10)?" on":"off",
   (temp[2]&0x01)?"off":" on"
  );
 for (i=0;i<4;i++){
  int neg=0;
  if ((i==2)&&(temp[1]&0x20))
   i=4;
  aux=(temp[i*2+3])*256;
  aux+=temp[i*2+4];
  if (aux&0x8000){
   aux-=0x8000;
   neg=1;
  }
  if (aux==0x7fff)
   fprintf(stdout, "T%1d: ----- %c\n",i+1,(temp[1]&0x8)?'C':'F');
  else
   fprintf(stdout, "T%1d:%c%3d.%1d %c\n",i+1,neg?'-':' ',aux/10,aux%10,(temp[1]&0x8)?'C':'F');
  if (i==4)
   i=2;
 }
 fprintf(stdout, "\n");
 return 0;
}

canonical_original_code:
int read_temp(int fd){
 unsigned char buffer[] = {0x02, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
 unsigned char temp[33];
 int i;
 int aux = 0;
 do_write(fd,buffer,sizeof(buffer));
 do_read(fd,temp,sizeof(temp));
 do_read(fd,temp,sizeof(temp));
 if ((temp[0]!=2)||(temp[33 -1]!=3)){
  fprintf(stderr, "Package error:\n");
  for (i=0;i<sizeof(temp);i++)
   fprintf(stdout, "%.2X ",temp[i]);
  fprintf(stdout, "\n");
  return -1;
 }
 for (i=1;i<33 -2;i++)
  aux+=temp[i];
 if ((aux&0xff)!=temp[33 -2]){
  fprintf(stderr, "CRC error:\n");
  for (i=0;i<sizeof(temp);i++)
   fprintf(stdout, "%.2X ",temp[i]);
  fprintf(stdout, "\n");
  return -1;
 }
 fprintf(stdout, "HOLD:%s T1-T2(T5):%s REC:%s LIGHT:%s\n",
   (temp[1]&0x40)?" on":"off",
   (temp[1]&0x20)?" on":"off",
   (temp[1]&0x10)?" on":"off",
   (temp[2]&0x01)?"off":" on"
  );
 for (i=0;i<4;i++){
  int neg = 0;
  if ((i==2)&&(temp[1]&0x20))
   i=4;
  aux=(temp[i*2+3])*256;
  aux+=temp[i*2+4];
  if (aux&0x8000){
   aux-=0x8000;
   neg=1;
  }
  if (aux==0x7fff)
   fprintf(stdout, "T%1d: ----- %c\n",i+1,(temp[1]&0x8)?'C':'F');
  else
   fprintf(stdout, "T%1d:%c%3d.%1d %c\n",i+1,neg?'-':' ',aux/10,aux%10,(temp[1]&0x8)?'C':'F');
  if (i==4)
   i=2;
 }
 fprintf(stdout, "\n");
 return 0;
}

variable_types: aux int

variable_types: neg int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx

if ( argc <= 1 )
{
use(*argv);
}
else
{
v3 = open(argv[1], 2, envp);
v4 = v3;
if ( v3 < 0 )
{
fprintf(stderr, "Unable to open %s\n", argv[1]);
}
else
{
if ( (int)config_port(v3) >= 0 )
{
while ( (unsigned int)read_id(v4) )
{
fwrite("Wrong ID\n", 1uLL, 9uLL, stderr);
sleep(1u);
}
fwrite("Device Recognized!\n", 1uLL, 0x13uLL, _bss_start);
init_temp(v4);
while ( 1 )
{
read_temp(v4);
sleep(1u);
}
}
fwrite("Unable to config serial port\n", 1uLL, 0x1DuLL, stderr);
}
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebx

if ( argc <= 1 )
{
func7(*argv);
}
else
{
v3 = open(argv[1], 2, envp);
v4 = v3;
if ( v3 < 0 )
{
fprintf(stderr, "Unable to open %s\n", argv[1]);
}
else
{
if ( (int)func10(v3) >= 0 )
{
while ( (unsigned int)func11(v4) )
{
fwrite("Wrong ID\n", 1uLL, 9uLL, stderr);
sleep(1u);
}
fwrite("Device Recognized!\n", 1uLL, 0x13uLL, _bss_start);
func12(v4);
while ( 1 )
{
func13(v4);
sleep(1u);
}
}
fwrite("Unable to config serial port\n", 1uLL, 0x1DuLL, stderr);
}
}
return -1;
}

original_code:
int main(int argc, char *argv[]){
 int temp_fd;
 if (argc<2){
  use(argv[0]);
  return -1;
 }
 temp_fd=open(argv[1], 02);
 if (temp_fd<0){
  fprintf(stderr,"Unable to open %s\n",argv[1]);
  return -1;
 }
 if (config_port(temp_fd)<0){
  fprintf(stderr,"Unable to config serial port\n");
  return -1;
 }
 while(read_id(temp_fd)!=0){
  fprintf(stderr,"Wrong ID\n");
  sleep(1);
 }
 fprintf(stdout,"Device Recognized!\n");
 init_temp(temp_fd);
 while(1){
  read_temp(temp_fd);
  sleep(1);
 }
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]


repo: riccardoangius/calculus-invaders

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // eax
int i; // ebx
_DWORD *v6; // rax
unsigned __int8 v7; // al
__int64 v9; // [rsp-320h] [rbp-410h] BYREF
unsigned int v10; // [rsp+4h] [rbp-ECh] BYREF
pthread_t newthread; // [rsp+8h] [rbp-E8h] BYREF
int v12; // [rsp+14h] [rbp-DCh] BYREF
int v13; // [rsp+18h] [rbp-D8h] BYREF
int v14; // [rsp+1Ch] [rbp-D4h] BYREF
sem_t v15; // [rsp+20h] [rbp-D0h] BYREF
sem_t sem; // [rsp+40h] [rbp-B0h] BYREF
sem_t v17; // [rsp+60h] [rbp-90h] BYREF
__int64 *arg; // [rsp+80h] [rbp-70h] BYREF
unsigned int value; // [rsp+88h] [rbp-68h]
int *v20; // [rsp+90h] [rbp-60h]
sem_t *v21; // [rsp+98h] [rbp-58h]
sem_t *v22; // [rsp+A0h] [rbp-50h]
sem_t *v23; // [rsp+A8h] [rbp-48h]
int *v24; // [rsp+B0h] [rbp-40h]
int v25; // [rsp+B8h] [rbp-38h]
int *v26; // [rsp+C0h] [rbp-30h]

if ( argc <= 1 )
{
puts("Fornire come primo parametro il numero M di navicelle nemiche.");
exit(1);
}
v3 = atoi(argv[1]);
if ( v3 > 80 )
v3 = 80;
v25 = v3;
strcpy((char *)&pics, " _ | | _| ");
*(_WORD *)((char *)&pics + 13) = 47;
*((_WORD *)&pics + 13) = 92;
*(_WORD *)((char *)&pics + 39) = 79;
*((_WORD *)&pics + 26) = 56;
strcpy((char *)&pics + 65, "dxdy");
strcpy((char *)&pics + 78, "d2xdy2");
strcpy((char *)&pics + 91, "d3xdy3");
initscr();
noecho();
curs_set(0);
welcome_screen();
v4 = time(0LL);
srand(v4);
mvprintw(0, 0, "CALCULUS INVADERS");
value = 100;
arg = &v9;
sem_init(&sem, 0, 0);
sem_init(&v17, 0, value);
sem_init(&v15, 0, 1u);
v21 = &v17;
v22 = &sem;
v23 = &v15;
v14 = 0;
v24 = &v14;
v13 = 0;
v26 = &v13;
v12 = 0;
v20 = &v12;
v10 = 0;
pthread_create(&newthread, 0LL, (void *(*)(void *))&player_task, &arg);
if ( v25 > 0 )
{
for ( i = 0; i < v25; ++i )
{
v6 = malloc(0x18uLL);
if ( !v6 )
{
endwin();
perror("Couldn't malloc");
exit(1);
}
*(_QWORD *)v6 = &arg;
v6[3] = i;
v6[2] = 6 * (i / 20);
v6[4] = 2;
pthread_create(&newthread, 0LL, (void *(*)(void *))&fiend_task, v6);
}
}
v7 = manager(&arg, 23LL, 80LL, &v10);
game_over_screen(v10, v7);
while ( v13 > 0 )
usleep(0xC350u);
sem_destroy(&sem);
sem_destroy(&v17);
sem_destroy(&v15);
endwin();
return 0;
}

original_code:
int main(int argc, char * argv[]) {
 if (argc < 2) {
  printf("Fornire come primo parametro il numero M di navicelle nemiche.\n");
  exit(1);
 }
 std_args shared;
 shared.fiends_no = atoi(argv[1]);
 if (shared.fiends_no > (((80/(3 +1)))*(((24 -1 -4)/(2 +2)))))
  shared.fiends_no = (((80/(3 +1)))*(((24 -1 -4)/(2 +2))));
 strcpy(pics[player_p], "  _ |  | _| ");
 strcpy(pics[rocket_r_p], "/");
 strcpy(pics[rocket_l_p], "\\");
 strcpy(pics[rocket_s_p], "O");
 strcpy(pics[bomb_p], "8");
 strcpy(pics[fiend1_p], "dxdy");
 strcpy(pics[fiend2_p], "d2xdy2");
 strcpy(pics[fiend3_p], "d3xdy3");
 initscr();
 noecho();
 curs_set(0);
 welcome_screen();
 srand(time(((void *)0)));
 mvprintw(0, 0, "CALCULUS INVADERS");
 int i, j, y;
 shared.buffer_size = 100;
 object * buffer[shared.buffer_size];
 shared.buffer = buffer;
 sem_t available, present, mutex;
 sem_init (&present, 0, 0);
 sem_init (&available, 0, shared.buffer_size);
 sem_init (&mutex, 0, 1);
 shared.available = &available;
 shared.present = &present;
 shared.mutex = &mutex;
 sig_atomic_t game_over = 0;
 shared.game_over = &game_over;
 sig_atomic_t running_threads = 0;
 shared.running_threads = &running_threads;
 int in = 0;
 shared.in = ∈
 pthread_t tmp;
 int game_points = 0;
 _Bool win;
 pthread_create(&tmp, ((void *)0), &player_task, &shared);
 for (i = 0; i < shared.fiends_no; i++) {
  fiend_args * orders = malloc(sizeof(fiend_args));
  if (!orders) { endwin(); perror("Couldn't malloc"); exit(1); }
  orders->shared = &shared;
  orders->fiend_id = i;
  orders->y = ((i / (80/(3 +1))) * (4 + 2));
  orders->level = lv1;
  pthread_create(&tmp, ((void *)0), &fiend_task, orders);
 }
 win = manager(&shared, 24 - 1, 80, &game_points);
 game_over_screen(game_points, win);
 while (running_threads > 0)
  usleep(50000);
 sem_destroy(&present);
 sem_destroy(&available);
 sem_destroy(&mutex);
 endwin();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 if (argc < 2) {
  printf("Fornire come primo parametro il numero M di navicelle nemiche.\n");
  exit(1);
 }
 struct std_args shared;
 shared.fiends_no = atoi(argv[1]);
 if (shared.fiends_no > (((80/(3 +1)))*(((24 -1 -4)/(2 +2)))))
  shared.fiends_no = (((80/(3 +1)))*(((24 -1 -4)/(2 +2))));
 strcpy(pics[player_p], "  _ |  | _| ");
 strcpy(pics[rocket_r_p], "/");
 strcpy(pics[rocket_l_p], "\\");
 strcpy(pics[rocket_s_p], "O");
 strcpy(pics[bomb_p], "8");
 strcpy(pics[fiend1_p], "dxdy");
 strcpy(pics[fiend2_p], "d2xdy2");
 strcpy(pics[fiend3_p], "d3xdy3");
 initscr();
 noecho();
 curs_set(0);
 welcome_screen();
 srand(time(((void *)0)));
 mvprintw(0, 0, "CALCULUS INVADERS");
 int i; int j; int y;
 shared.buffer_size = 100;
 struct object *buffer[-1];
 shared.buffer = buffer;
 union sem_t available; union sem_t present; union sem_t mutex;
 sem_init (&present, 0, 0);
 sem_init (&available, 0, shared.buffer_size);
 sem_init (&mutex, 0, 1);
 shared.available = &available;
 shared.present = &present;
 shared.mutex = &mutex;
 int game_over = 0;
 shared.game_over = &game_over;
 int running_threads = 0;
 shared.running_threads = &running_threads;
 int in = 0;
 shared.in = ∈
 unsigned long tmp;
 int game_points = 0;
 _Bool win;
 pthread_create(&tmp, ((void *)0), &player_task, &shared);
 for (i = 0; i < shared.fiends_no; i++) {
  struct fiend_args *orders = malloc(sizeof(fiend_args));
  if (!orders) { endwin(); perror("Couldn't malloc"); exit(1); }
  orders->shared = &shared;
  orders->fiend_id = i;
  orders->y = ((i / (80/(3 +1))) * (4 + 2));
  orders->level = lv1;
  pthread_create(&tmp, ((void *)0), &fiend_task, orders);
 }
 win = manager(&shared, 24 - 1, 80, &game_points);
 game_over_screen(game_points, win);
 while (running_threads > 0)
  usleep(50000);
 sem_destroy(&present);
 sem_destroy(&available);
 sem_destroy(&mutex);
 endwin();
 return 0;
}

variable_types: j int

variable_types: y int

variable_types: struct object { short id; enum object_class { null_c=0, player_c=1, fiend_c=2, rocket_c=3, bomb_c=4, sig_quit=5, sig_pause=6, sig_bomb=7, sig_rocket=8, sig_fiend=9 } class; enum object_status { dead=0, alive=1, lv1=2, lv2=3, lv3=4 } status; unsigned long tid; int y; int prev_y; int height; int x; int prev_x; int width; int hp; union sem_t { char __size[32]; long __align; } obj_mutex; int revision; int hdirection; int vdirection; char *pic; } *[-1]

variable_types: in int

variable_types:
tmp
unsigned long

variable_types: struct fiend_args { struct std_args { struct object { short id; enum object_class { null_c=0, player_c=1, fiend_c=2, rocket_c=3, bomb_c=4, sig_quit=5, sig_pause=6, sig_bomb=7, sig_rocket=8, sig_fiend=9 } class; enum object_status { dead=0, alive=1, lv1=2, lv2=3, lv3=4 } status; unsigned long tid; int y; int prev_y; int height; int x; int prev_x; int width; int hp; union sem_t { char __size[32]; long __align; } obj_mutex; int revision; int hdirection; int vdirection; char *pic; } **buffer; int buffer_size; int *in; union sem_t *available; union sem_t *present; union sem_t *mutex; int *game_over; int fiends_no; int *running_threads; } *shared; int y; int fiend_id; enum object_status { dead=0, alive=1, lv1=2, lv2=3, lv3=4 } level; } *

user_defined_types: struct std_args { struct object **buffer; int buffer_size; int *in; union sem_t *available; union sem_t *present; union sem_t *mutex; int *game_over; int fiends_no; int *running_threads; }

user_defined_types: struct fiend_args { struct std_args *shared; int y; int fiend_id; enum object_status level; }

user_defined_types: struct object { short id; enum object_class class; enum object_status status; unsigned long tid; int y; int prev_y; int height; int x; int prev_x; int width; int hp; union sem_t obj_mutex; int revision; int hdirection; int vdirection; char *pic; }


sampleInit

repo: richarddurbin/pbwt

decompiled_code:
_DWORD *sampleInit()
{
_DWORD *result; // rax

sampleDict = dictCreate(4096LL);
populationDict = dictCreate(64LL);
samples = uArrayCreate(4096LL, 20LL);
result = (_DWORD *)uArray(samples, 0LL);
*result = 0;
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

sampleDict = dictCreate(4096LL);
populationDict = dictCreate(64LL);
samples = uArrayCreate(4096LL, 20LL);
result = (_DWORD *)uArray(samples, 0LL);
*result = 0;
return result;
}

original_code:
void sampleInit (void)
{
  sampleDict = dictCreate (4096) ;
  populationDict = dictCreate (64) ;
  samples = uArrayCreate(4096,sizeof(Sample)) ;
  (*(Sample*)uArray(samples,0)).nameD = 0 ;
}

canonical_original_code:
void sampleInit()
{
  sampleDict = dictCreate (4096) ;
  populationDict = dictCreate (64) ;
  samples = uArrayCreate(4096,sizeof(Sample)) ;
  (*(struct SampleStruct *)uArray(samples,0)).nameD = 0 ;
}

user_defined_types: struct SampleStruct { int nameD; int father; int mother; int popD; char isMale; char isFemale; }


sampleDestroy

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

if ( sampleDict )
result = dictDestroy();
if ( populationDict )
result = dictDestroy();
if ( samples )
result = arrayDestroy();
return result;
}

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

if ( sampleDict )
result = dictDestroy();
if ( populationDict )
result = dictDestroy();
if ( samples )
result = arrayDestroy();
return result;
}

original_code:
void sampleDestroy (void)
{
  if (sampleDict) dictDestroy(sampleDict);
  if (populationDict) dictDestroy(populationDict);
  if (samples) arrayDestroy(samples);
}

canonical_original_code:
void sampleDestroy()
{
  if (sampleDict) dictDestroy(sampleDict);
  if (populationDict) dictDestroy(populationDict);
  if (samples) arrayDestroy(samples);
}


sampleAdd

decompiled_code:
__int64 __fastcall sampleAdd(__int64 a1)
{
unsigned int *v2; // rax
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)dictAdd(sampleDict, a1, v3) )
{
v2 = (unsigned int *)uArray(samples, (int)v3[0]);
*v2 = v3[0];
}
return v3[0];
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int *v2; // rax
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( (unsigned int)dictAdd(sampleDict, a1, v3) )
{
v2 = (unsigned int *)uArray(samples, (int)v3[0]);
*v2 = v3[0];
}
return v3[0];
}

original_code:
int sampleAdd (char *name, char *father, char *mother, char *pop)
{
  int k ;
  if (dictAdd (sampleDict, name, &k))
    ((Sample*)uArray(samples,k))->nameD = k ;
  return k ;
}

canonical_original_code:
int sampleAdd(char *name, char *father, char *mother, char *pop)
{
  int k;
  if (dictAdd (sampleDict, name, &k))
    ((struct SampleStruct *)uArray(samples,k))->nameD = k ;
  return k ;
}

variable_types:
name
char *

variable_types:
father
char *

variable_types:
mother
char *

variable_types:
pop
char *

variable_types: k int


sample

decompiled_code:
__int64 __fastcall sample(__int64 a1, int a2)
{
unsigned int v2; // ebx

v2 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL) + *(_DWORD *)(*(_QWORD *)(a1 + 24) + 24LL) * a2);
if ( (int)v2 >= *(_QWORD *)(samples + 32) )
die("sample index %d out of range %ld", v2);
return *(_QWORD *)(samples + 8) + (int)(*(_DWORD *)(samples + 24) * v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
unsigned int v2; // ebx

v2 = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL) + *(_DWORD *)(*(_QWORD *)(a1 + 24) + 24LL) * a2);
if ( (int)v2 >= *(_QWORD *)(samples + 32) )
die("sample index %d out of range %ld", v2);
return *(_QWORD *)(samples + 8) + (int)(*(_DWORD *)(samples + 24) * v2);
}

original_code:
Sample *sample (PBWT *p, int i)
{
  i = ((*(int*)((p->samples)->base + (i)*(p->samples)->size))) ;
  if (i >= ((samples)->max))
    die ("sample index %d out of range %ld", i, ((samples)->max)) ;
  return (((Sample*)((samples)->base + (i)*(samples)->size))) ;
}

canonical_original_code:
struct SampleStruct *sample(struct PBWTstruct *p, int i)
{
  i = ((*(int *)((p->samples)->base + (i)*(p->samples)->size))) ;
  if (i >= ((samples)->max))
    die ("sample index %d out of range %ld", i, ((samples)->max)) ;
  return (((struct SampleStruct *)((samples)->base + (i)*(samples)->size))) ;
}

variable_types: struct PBWTstruct { int N; int M; char *chrom; struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; } *sites; struct ArrayStruct *samples; struct ArrayStruct *yz; int *aFstart; struct ArrayStruct *zz; int *aRstart; struct ArrayStruct *zMissing; struct ArrayStruct *missingOffset; struct ArrayStruct *zDosage; struct ArrayStruct *dosageOffset; char isRefFreq; char isUnphased; } *

variable_types: i int

return_type: struct SampleStruct { int nameD; int father; int mother; int popD; char isMale; char isFemale; } *

user_defined_types: struct PBWTstruct { int N; int M; char *chrom; struct ArrayStruct *sites; struct ArrayStruct *samples; struct ArrayStruct *yz; int *aFstart; struct ArrayStruct *zz; int *aRstart; struct ArrayStruct *zMissing; struct ArrayStruct *missingOffset; struct ArrayStruct *zDosage; struct ArrayStruct *dosageOffset; char isRefFreq; char isUnphased; }


sampleName

decompiled_code:
__int64 __fastcall sampleName(unsigned int *a1)
{
return dictName(sampleDict, *a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
return dictName(sampleDict, *a1);
}

original_code: char* sampleName (Sample *s) { return dictName (sampleDict, s->nameD) ; }

canonical_original_code: char *sampleName(struct SampleStruct *s) { return dictName (sampleDict, s->nameD) ; }

variable_types: struct SampleStruct { int nameD; int father; int mother; int popD; char isMale; char isFemale; } *

return_type: char *


popName

decompiled_code:
__int64 __fastcall popName(__int64 a1)
{
return dictName(populationDict, *(unsigned int *)(a1 + 12));
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return dictName(populationDict, *(unsigned int *)(a1 + 12));
}

original_code: char* popName (Sample *s) { return dictName (populationDict, s->popD) ; }

canonical_original_code: char *popName(struct SampleStruct *s) { return dictName (populationDict, s->popD) ; }


pbwtSubSample

original_code:
PBWT *pbwtSubSample (PBWT *pOld, Array select)
{
  if (!pOld || !pOld->yz) die ("subSample called without valid pbwt") ;
  PBWT *pNew = pbwtCreate (((select)->max), pOld->N) ;
  int i, j, nOld = 0 ;
  uchar *x = (uchar*)_myalloc((pNew->M)*sizeof(uchar)) ;
  int *ainv = (int*)_myalloc((pOld->M)*sizeof(int)) ;
  PbwtCursor *uOld = pbwtCursorCreate (pOld, 1, 1) ;
  PbwtCursor *uNew = pbwtCursorCreate (pNew, 1, 1) ;
  for (i = 0 ; i < pOld->N ; ++i)
    { for (j = 0 ; j < pOld->M ; ++j) ainv[uOld->a[j]] = j ;
      for (j = 0 ; j < pNew->M ; ++j) x[j] = uOld->y[ainv[((*(int*)((select)->base + (j)*(select)->size)))]] ;
      for (j = 0 ; j < pNew->M ; ++j) uNew->y[j] = x[uNew->a[j]] ;
      pbwtCursorWriteForwards (uNew) ;
      pbwtCursorForwardsRead (uOld) ;
    }
  pbwtCursorToAFend (uNew, pNew) ;
  if (pOld->samples)
    { pNew->samples = uArrayCreate(pNew->M,sizeof(int)) ;
      for (j = 0 ; j < pNew->M ; ++j)
 (*(int*)uArray(pNew->samples,j)) = ((*(int*)((pOld->samples)->base + (((*(int*)((select)->base + (j)*(select)->size))))*(pOld->samples)->size))) ;
    }
  pNew->chrom = pOld->chrom ; pOld->chrom = 0 ;
  pNew->sites = pOld->sites ; pOld->sites = 0 ;
  pbwtDestroy (pOld) ;
  free(x) ; free(ainv) ; pbwtCursorDestroy (uOld) ; pbwtCursorDestroy (uNew) ;
  return pNew ;
}

canonical_original_code:
struct PBWTstruct *pbwtSubSample(struct PBWTstruct *pOld, struct ArrayStruct *select)
{
  if (!pOld || !pOld->yz) die ("subSample called without valid pbwt") ;
  struct PBWTstruct *pNew = pbwtCreate (((select)->max), pOld->N);
  int i; int j; int nOld = 0;
  unsigned char *x = (uchar*)_myalloc((pNew->M)*sizeof(uchar));
  int *ainv = (int*)_myalloc((pOld->M)*sizeof(int));
  struct PbwtCursor *uOld = pbwtCursorCreate (pOld, 1, 1);
  struct PbwtCursor *uNew = pbwtCursorCreate (pNew, 1, 1);
  for (i = 0 ; i < pOld->N ; ++i)
    { for (j = 0 ; j < pOld->M ; ++j) ainv[uOld->a[j]] = j ;
      for (j = 0 ; j < pNew->M ; ++j) x[j] = uOld->y[ainv[((*(int *)((select)->base + (j)*(select)->size)))]] ;
      for (j = 0 ; j < pNew->M ; ++j) uNew->y[j] = x[uNew->a[j]] ;
      pbwtCursorWriteForwards (uNew) ;
      pbwtCursorForwardsRead (uOld) ;
    }
  pbwtCursorToAFend (uNew, pNew) ;
  if (pOld->samples)
    { pNew->samples = uArrayCreate(pNew->M,sizeof(int)) ;
      for (j = 0 ; j < pNew->M ; ++j)
 (*(int *)uArray(pNew->samples,j)) = ((*(int *)((pOld->samples)->base + (((*(int *)((select)->base + (j)*(select)->size))))*(pOld->samples)->size))) ;
    }
  pNew->chrom = pOld->chrom ; pOld->chrom = 0 ;
  pNew->sites = pOld->sites ; pOld->sites = 0 ;
  pbwtDestroy (pOld) ;
  free(x) ; free(ainv) ; pbwtCursorDestroy (uOld) ; pbwtCursorDestroy (uNew) ;
  return pNew ;
}

variable_types:
pOld
struct PBWTstruct *

variable_types:
select
struct ArrayStruct *

variable_types: nOld int

variable_types:
x
unsigned char *

variable_types:
ainv
int *

variable_types: struct PbwtCursor { int M; struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; } *z; long n; char isBlockEnd; unsigned char *y; int c; int *a; int *d; int *u; int *b; int *e; long nBlockStart; } *

return_type: struct PBWTstruct { int N; int M; char *chrom; struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; } *sites; struct ArrayStruct *samples; struct ArrayStruct *yz; int *aFstart; struct ArrayStruct *zz; int *aRstart; struct ArrayStruct *zMissing; struct ArrayStruct *missingOffset; struct ArrayStruct *zDosage; struct ArrayStruct *dosageOffset; char isRefFreq; char isUnphased; } *

user_defined_types: struct PbwtCursor { int M; struct ArrayStruct *z; long n; char isBlockEnd; unsigned char *y; int c; int *a; int *d; int *u; int *b; int *e; long nBlockStart; }


pbwtSubSampleInterval

decompiled_code:
__int64 __fastcall pbwtSubSampleInterval(unsigned int *a1, __int64 a2, int a3)
{
__int64 v4; // r13
__int64 v5; // rbx
__int64 v6; // rbx

if ( (int)a2 < 0 || a3 <= 0 )
{
die("bad start %d, Mnew %d in subsample", (unsigned int)a2);
v4 = uArrayCreate(a3, 4LL);
if ( a3 <= 0 )
goto LABEL_7;
}
else if ( (int)a2 + a3 > (int)a1[1] )
{
die("bad start %d, Mnew %d in subsample", a2);
v4 = uArrayCreate(a3, 4LL);
}
else
{
v4 = uArrayCreate(a3, 4LL);
}
v5 = 0LL;
do
{
*(_DWORD *)uArray(v4, v5) = a2 + v5;
++v5;
}
while ( a3 > (int)v5 );
LABEL_7:
v6 = pbwtSubSample(a1, v4);
arrayDestroy(v4);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1, __int64 a2, int a3)
{
__int64 v4; // r13
__int64 v5; // rbx
__int64 v6; // rbx

if ( (int)a2 < 0 || a3 <= 0 )
{
die("bad start %d, Mnew %d in subsample", (unsigned int)a2);
v4 = uArrayCreate(a3, 4LL);
if ( a3 <= 0 )
goto LABEL_7;
}
else if ( (int)a2 + a3 > (int)a1[1] )
{
die("bad start %d, Mnew %d in subsample", a2);
v4 = uArrayCreate(a3, 4LL);
}
else
{
v4 = uArrayCreate(a3, 4LL);
}
v5 = 0LL;
do
{
*(_DWORD *)uArray(v4, v5) = a2 + v5;
++v5;
}
while ( a3 > (int)v5 );
LABEL_7:
v6 = func6(a1, v4);
arrayDestroy(v4);
return v6;
}

original_code:
PBWT *pbwtSubSampleInterval (PBWT *pOld, int start, int Mnew)
{
  if (start < 0 || Mnew <= 0 || start + Mnew > pOld->M)
    die ("bad start %d, Mnew %d in subsample", start, Mnew) ;
  int i ;
  Array select = uArrayCreate(Mnew,sizeof(int)) ;
  for (i = 0 ; i < Mnew ; ++i)
    (*(int*)uArray(select,i)) = start + i ;
  PBWT *pNew = pbwtSubSample (pOld, select) ;
  arrayDestroy (select) ;
  return pNew ;
}

canonical_original_code:
struct PBWTstruct *pbwtSubSampleInterval(struct PBWTstruct *pOld, int start, int Mnew)
{
  if (start < 0 || Mnew <= 0 || start + Mnew > pOld->M)
    die ("bad start %d, Mnew %d in subsample", start, Mnew) ;
  int i;
  struct ArrayStruct *select = uArrayCreate(Mnew,sizeof(int));
  for (i = 0 ; i < Mnew ; ++i)
    (*(int *)uArray(select,i)) = start + i ;
  struct PBWTstruct *pNew = pbwtSubSample (pOld, select);
  arrayDestroy (select) ;
  return pNew ;
}

variable_types: start int

variable_types: Mnew int

variable_types: struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; } *


hashDouble

original_code:
static void hashDouble (TRUE_HASH *h)
{
  int oldsize, newsize ;
  long int hash, delta = 0 ;
  int *oldKeys, *kp ;
  int *oldValues, i ;
  HASHKEY hk ;
  oldsize = 1 << h->nbits ;
  ++h->nbits ;
  newsize = 1 << h->nbits ;
  h->mask = (1 << h->nbits) - 1 ;
  h->guard = (1 << (h->nbits - 1)) ;
  oldKeys = h->keys ;
  h->keys = (int*)_mycalloc(newsize,sizeof(int)) ;
  oldValues = h->values ;
  h->values = (int*)_myalloc((newsize)*sizeof(int)) ;
  for (i = 0, kp = oldKeys ; i < oldsize ; i++)
    if (*kp && *kp != REMOVED)
      { hk.i = *kp ; { register int z = IS5, x = hk.i ; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
        while (1)
          if (!h->keys[hash])
     { h->keys[hash] = *kp ;
       h->values[hash] = oldValues[i] ;
       --h->guard ;
       ++nAdded ;
       break ;
     }
   else
            { nBounced++ ;
       if (!delta) { register int z = IS7, x = hk.i ; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
       hash = (hash + delta) & h->mask ;
     }
      }
  free (oldKeys) ;
  free (oldValues) ;
}

canonical_original_code:
static void hashDouble(struct TRUE_HASH *h)
{
  int oldsize; int newsize;
  long hash; long delta = 0;
  int *oldKeys; int *kp;
  int *oldValues; int i;
  union HASHKEY hk;
  oldsize = 1 << h->nbits ;
  ++h->nbits ;
  newsize = 1 << h->nbits ;
  h->mask = (1 << h->nbits) - 1 ;
  h->guard = (1 << (h->nbits - 1)) ;
  oldKeys = h->keys ;
  h->keys = (int *)_mycalloc(newsize,sizeof(int)) ;
  oldValues = h->values ;
  h->values = (int *)_myalloc((newsize)*sizeof(int)) ;
  for (i = 0, kp = oldKeys ; i < oldsize ; i++)
    if (*kp && *kp != REMOVED)
      { hk.i = *kp ; { register int z = IS5; register int x = hk.i; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
        while (1)
          if (!h->keys[hash])
     { h->keys[hash] = *kp ;
       h->values[hash] = oldValues[i] ;
       --h->guard ;
       ++nAdded ;
       break ;
     }
   else
            { nBounced++ ;
       if (!delta) { register int z = IS7; register int x = hk.i; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
       hash = (hash + delta) & h->mask ;
     }
      }
  free (oldKeys) ;
  free (oldValues) ;
}

variable_types: struct TRUE_HASH { int nbits; unsigned int mask; int n; int guard; int *keys; int *values; struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; } *freeList; int nFree; int iter; } *

variable_types: oldsize int

variable_types: newsize int

variable_types: hash long

variable_types: delta long

variable_types:
oldKeys
int *

variable_types:
kp
int *

variable_types:
oldValues
int *

variable_types: union HASHKEY { long i; float f; void *p; }

variable_types: z int

variable_types: x int

user_defined_types: struct TRUE_HASH { int nbits; unsigned int mask; int n; int guard; int *keys; int *values; struct ArrayStruct *freeList; int nFree; int iter; }


hashCreate

original_code:
HASH hashCreate (int n)
{
  TRUE_HASH *h = (TRUE_HASH*)_myalloc((1)*sizeof(TRUE_HASH)) ;
  if (n < 64) n = 64 ;
  --n ;
  h->nbits = 1 ;
  while (n >>= 1) ++h->nbits ;
  h->mask = (1 << h->nbits) - 1 ;
  h->guard = (1 << (h->nbits - 1)) ;
  h->keys = (int*)_mycalloc(1 << h->nbits,sizeof(int)) ;
  h->values = (int*)_myalloc((1 << h->nbits)*sizeof(int)) ;
  h->n = 0 ;
  h->freeList = uArrayCreate(32,sizeof(int)) ;
  h->nFree = 0 ;
  ++nCreated ;
  return (HASH) h ;
}

canonical_original_code:
void *hashCreate(int n)
{
  struct TRUE_HASH *h = (TRUE_HASH*)_myalloc((1)*sizeof(TRUE_HASH));
  if (n < 64) n = 64 ;
  --n ;
  h->nbits = 1 ;
  while (n >>= 1) ++h->nbits ;
  h->mask = (1 << h->nbits) - 1 ;
  h->guard = (1 << (h->nbits - 1)) ;
  h->keys = (int *)_mycalloc(1 << h->nbits,sizeof(int)) ;
  h->values = (int *)_myalloc((1 << h->nbits)*sizeof(int)) ;
  h->n = 0 ;
  h->freeList = uArrayCreate(32,sizeof(int)) ;
  h->nFree = 0 ;
  ++nCreated ;
  return (void *) h ;
}

variable_types: n int

return_type: void *

user_defined_types: struct ArrayStruct { int magic; char *base; long dim; int size; long max; int id; }


hashDestroy

decompiled_code:
void __fastcall hashDestroy(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
arrayDestroy(*((_QWORD *)ptr + 4));
free(ptr);
++nDestroyed;
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
arrayDestroy(*((_QWORD *)ptr + 4));
free(ptr);
++nDestroyed;
}

original_code:
void hashDestroy (HASH hx)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  free (h->keys) ;
  free (h->values) ;
  arrayDestroy (h->freeList) ;
  free (h) ;
  ++nDestroyed ;
}

canonical_original_code:
void hashDestroy(void *hx)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  free (h->keys) ;
  free (h->values) ;
  arrayDestroy (h->freeList) ;
  free (h) ;
  ++nDestroyed ;
}

variable_types:
hx
void *


hashClear

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

*(_DWORD *)(a1 + 8) = 0;
memset(*(void **)(a1 + 16), 0, 4LL * (1 << *(_DWORD *)a1));
result = uArrayReCreate(*(_QWORD *)(a1 + 32), 32LL, 4LL);
*(_QWORD *)(a1 + 32) = result;
return result;
}

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

*(_DWORD *)(a1 + 8) = 0;
memset(*(void **)(a1 + 16), 0, 4LL * (1 << *(_DWORD *)a1));
result = uArrayReCreate(*(_QWORD *)(a1 + 32), 32LL, 4LL);
*(_QWORD *)(a1 + 32) = result;
return result;
}

original_code:
void hashClear (HASH hx)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  h->n = 0 ;
  memset (h->keys, 0, sizeof(int)*(1 << h->nbits)) ;
  h->freeList = uArrayReCreate(h->freeList,32,sizeof(int)) ;
}

canonical_original_code:
void hashClear(void *hx)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  h->n = 0 ;
  memset (h->keys, 0, sizeof(int)*(1 << h->nbits)) ;
  h->freeList = uArrayReCreate(h->freeList,32,sizeof(int)) ;
}


hashFind

original_code:
int hashFind (HASH hx, HASHKEY k)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  long int hash, delta = 0 ;
  { register int z = IS5, x = k.i ; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (h->keys[hash] == k.i)
      { nFound++ ;
 return h->values[hash] ;
      }
    else if (!h->keys[hash])
      { nNotFound++ ;
 return 0 ;
      }
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7, x = k.i ; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}

canonical_original_code:
int hashFind(void *hx, union HASHKEY k)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  long hash; long delta = 0;
  { register int z = IS5; register int x = k.i; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (h->keys[hash] == k.i)
      { nFound++ ;
 return h->values[hash] ;
      }
    else if (!h->keys[hash])
      { nNotFound++ ;
 return 0 ;
      }
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7; register int x = k.i; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}

user_defined_types: union HASHKEY { long i; float f; void *p; }


hashAdd

original_code:
int hashAdd (HASH hx, HASHKEY k)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  long int hash, delta = 0 ;
  int test ;
  if (!h->guard)
    hashDouble (h) ;
  { register int z = IS5, x = k.i ; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (!h->keys[hash] || h->keys[hash] == REMOVED)
      { if (!h->keys[hash]) --h->guard ;
 h->keys[hash] = k.i ;
 if (h->nFree)
   h->values[hash] = ((*(int*)((h->freeList)->base + (h->nFree--)*(h->freeList)->size))) ;
 else
   h->values[hash] = ++h->n ;
 nAdded++ ;
 return h->values[hash] ;
      }
    else if (h->keys[hash] == k.i)
      return h->values[hash] ;
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7, x = k.i ; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}

canonical_original_code:
int hashAdd(void *hx, union HASHKEY k)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  long hash; long delta = 0;
  int test;
  if (!h->guard)
    hashDouble (h) ;
  { register int z = IS5; register int x = k.i; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (!h->keys[hash] || h->keys[hash] == REMOVED)
      { if (!h->keys[hash]) --h->guard ;
 h->keys[hash] = k.i ;
 if (h->nFree)
   h->values[hash] = ((*(int *)((h->freeList)->base + (h->nFree--)*(h->freeList)->size))) ;
 else
   h->values[hash] = ++h->n ;
 nAdded++ ;
 return h->values[hash] ;
      }
    else if (h->keys[hash] == k.i)
      return h->values[hash] ;
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7; register int x = k.i; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}

variable_types: test int


hashRemove

original_code:
BOOL hashRemove (HASH hx, HASHKEY k)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  long int hash, delta = 0 ;
  { register int z = IS5, x = k.i ; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (h->keys[hash] == k.i)
      { h->keys[hash] = REMOVED ;
 (*(int*)uArray(h->freeList,++h->nFree)) = h->values[hash] ;
 ++nFound ;
 return 1 ;
      }
    else if (!h->keys[hash])
      { nNotFound++ ;
 return 0 ;
      }
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7, x = k.i ; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}

canonical_original_code:
char hashRemove(void *hx, union HASHKEY k)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  long hash; long delta = 0;
  { register int z = IS5; register int x = k.i; for (hash = x, x >>= 5 ; z-- ; x >>= 5) hash ^= x ; hash &= h->mask ; } ;
  while (1)
    if (h->keys[hash] == k.i)
      { h->keys[hash] = REMOVED ;
 (*(int *)uArray(h->freeList,++h->nFree)) = h->values[hash] ;
 ++nFound ;
 return 1 ;
      }
    else if (!h->keys[hash])
      { nNotFound++ ;
 return 0 ;
      }
    else
      { nBounced++ ;
 if (!delta) { register int z = IS7; register int x = k.i; for (delta = x, x >>= 7 ; z-- ; x >>= 7) delta ^= x ; delta = (delta & h->mask) | 0x01 ; } ;
 hash = (hash + delta) & h->mask ;
      }
}


hashInitIterator

decompiled_code:
void __fastcall hashInitIterator(__int64 a1)
{
*(_DWORD *)(a1 + 44) = -1;
}

original_code:
void hashInitIterator (HASH hx)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  h->iter = -1 ;
}

canonical_original_code:
void hashInitIterator(void *hx)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  h->iter = -1 ;
}


hashNextKeyValue

decompiled_code:
__int64 __fastcall hashNextKeyValue(__int64 a1, _QWORD *a2, _DWORD *a3)
{
int v3; // ecx
int v4; // eax
unsigned int v5; // eax
__int64 result; // rax

v3 = 1 << *(_DWORD *)a1;
while ( 1 )
{
v4 = *(_DWORD *)(a1 + 44) + 1;
*(_DWORD *)(a1 + 44) = v4;
if ( v4 >= v3 )
break;
v5 = *(_DWORD *)(*(_QWORD *)(a1 + 16) + 4LL * v4);
if ( v5 > 1 )
{
*a2 = (int)v5;
result = *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL * *(int *)(a1 + 44));
*a3 = result;
return result;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, _QWORD *a2, _DWORD *a3)
{
int v3; // ecx
int v4; // eax
unsigned int v5; // eax
__int64 result; // rax

v3 = 1 << *(_DWORD *)a1;
while ( 1 )
{
v4 = *(_DWORD *)(a1 + 44) + 1;
*(_DWORD *)(a1 + 44) = v4;
if ( v4 >= v3 )
break;
v5 = *(_DWORD *)(*(_QWORD *)(a1 + 16) + 4LL * v4);
if ( v5 > 1 )
{
*a2 = (int)v5;
result = *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL * *(int *)(a1 + 44));
*a3 = result;
return result;
}
}
return 0LL;
}

original_code:
int hashNextKeyValue (HASH hx, HASHKEY *kp, int *ip)
{
  TRUE_HASH *h = (TRUE_HASH*) hx ;
  int size = 1 << h->nbits ;
  while (++h->iter < size)
    if (h->keys[h->iter] && h->keys[h->iter] != REMOVED)
      { kp->i = h->keys[h->iter] ;
 *ip = h->values[h->iter] ;
 return *ip ;
      }
  return 0 ;
}

canonical_original_code:
int hashNextKeyValue(void *hx, union HASHKEY *kp, int *ip)
{
  struct TRUE_HASH *h = (TRUE_HASH*) hx;
  int size = 1 << h->nbits;
  while (++h->iter < size)
    if (h->keys[h->iter] && h->keys[h->iter] != REMOVED)
      { kp->i = h->keys[h->iter] ;
 *ip = h->values[h->iter] ;
 return *ip ;
      }
  return 0 ;
}

variable_types: union HASHKEY { long i; float f; void *p; } *

variable_types:
ip
int *

variable_types: size int


hashStats

decompiled_code:
int hashStats()
{
printf(
"%d hashes (%d created, %d destroyed)\n",
(unsigned int)(nCreated - nDestroyed),
(unsigned int)nCreated,
(unsigned int)nDestroyed);
return printf(
"%d added, %d found, %d bounced, %d not found\n",
(unsigned int)nAdded,
(unsigned int)nFound,
(unsigned int)nBounced,
(unsigned int)nNotFound);
}

canonical_decompiled_code:
int func9()
{
printf(
"%d hashes (%d created, %d destroyed)\n",
(unsigned int)(nCreated - nDestroyed),
(unsigned int)nCreated,
(unsigned int)nDestroyed);
return printf(
"%d added, %d found, %d bounced, %d not found\n",
(unsigned int)nAdded,
(unsigned int)nFound,
(unsigned int)nBounced,
(unsigned int)nNotFound);
}

original_code:
void hashStats (void)
{
  printf ("%d hashes (%d created, %d destroyed)\n",
   nCreated - nDestroyed, nCreated, nDestroyed) ;
  printf ("%d added, %d found, %d bounced, %d not found\n",
   nAdded, nFound, nBounced, nNotFound) ;
}

canonical_original_code:
void hashStats()
{
  printf ("%d hashes (%d created, %d destroyed)\n",
   nCreated - nDestroyed, nCreated, nDestroyed) ;
  printf ("%d added, %d found, %d bounced, %d not found\n",
   nAdded, nFound, nBounced, nNotFound) ;
}


hashString

decompiled_code:
__int64 __fastcall hashString(char *a1, unsigned int a2, int a3)
{
int v4; // ecx
char v5; // al
unsigned int v6; // edx
unsigned int v7; // eax
unsigned int i; // ecx
__int64 result; // rax

v4 = a3 == 0 ? 13 : 21;
v5 = *a1;
if ( *a1 )
{
v6 = 0;
do
{
++a1;
v6 = v5 ^ __ROL4__(v6, v4);
v5 = *a1;
}
while ( *a1 );
}
else
{
v6 = 0;
}
if ( a2 > 3 )
{
v7 = v6;
}
else
{
v7 = v6;
for ( i = a2; i <= 3; i += a2 )
v7 ^= v6 >> i;
}
result = ((1 << a2) - 1) & v7;
if ( a3 )
result = (unsigned int)result | 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, unsigned int a2, int a3)
{
int v4; // ecx
char v5; // al
unsigned int v6; // edx
unsigned int v7; // eax
unsigned int i; // ecx
__int64 result; // rax

v4 = a3 == 0 ? 13 : 21;
v5 = *a1;
if ( *a1 )
{
v6 = 0;
do
{
++a1;
v6 = v5 ^ __ROL4__(v6, v4);
v5 = *a1;
}
while ( *a1 );
}
else
{
v6 = 0;
}
if ( a2 > 3 )
{
v7 = v6;
}
else
{
v7 = v6;
for ( i = a2; i <= 3; i += a2 )
v7 ^= v6 >> i;
}
result = ((1 << a2) - 1) & v7;
if ( a3 )
result = (unsigned int)result | 1;
return result;
}

original_code:
static int hashString (char *cp, int n, int isDiff)
{
  int i ;
  unsigned int j, x = 0 ;
  int rotate = isDiff ? 21 : 13 ;
  int leftover = 8 * sizeof(int) - rotate ;
  while (*cp)
    x = (*cp++) ^ ((x >> leftover) | (x << rotate)) ;
  for (j = x, i = n ; i < sizeof(int) ; i += n)
    j ^= (x >> i) ;
  j &= (1 << n) - 1 ;
  if (isDiff)
    j |= 1 ;
  return j ;
}

canonical_original_code:
static int hashString(char *cp, int n, int isDiff)
{
  int i;
  unsigned int j; unsigned int x = 0;
  int rotate = isDiff ? 21 : 13;
  int leftover = 8 * sizeof(int) - rotate;
  while (*cp)
    x = (*cp++) ^ ((x >> leftover) | (x << rotate)) ;
  for (j = x, i = n ; i < sizeof(int) ; i += n)
    j ^= (x >> i) ;
  j &= (1 << n) - 1 ;
  if (isDiff)
    j |= 1 ;
  return j ;
}

variable_types:
cp
char *

variable_types: isDiff int

variable_types: i int

variable_types:
j
unsigned int

variable_types:
x
unsigned int

variable_types: rotate int

variable_types: leftover int


remap

decompiled_code:
void *__fastcall remap(void *ptr, int a2, int a3)
{
void *v3; // rbp

v3 = (void *)mycalloc(a3, 1LL);
memcpy(v3, ptr, a2);
free(ptr);
return v3;
}

canonical_decompiled_code:
void *__fastcall func1(void *ptr, int a2, int a3)
{
void *v3; // rbp

v3 = (void *)mycalloc(a3, 1LL);
memcpy(v3, ptr, a2);
free(ptr);
return v3;
}

original_code:
static void* remap (void *old, int oldSize, int newSize)
{
  void* new = _mycalloc (newSize, 1) ;
  memcpy (new, old, oldSize) ;
  free (old) ;
  return new ;
}

canonical_original_code:
static void *remap(void *old, int oldSize, int newSize)
{
  void *new = _mycalloc (newSize, 1);
  memcpy (new, old, oldSize) ;
  free (old) ;
  return new ;
}

variable_types:
old
void *

variable_types: oldSize int

variable_types: newSize int

variable_types:
new
void *


dictCreate

original_code:
DICT *dictCreate (int size)
{
  DICT *dict = (DICT*)_mycalloc(1,sizeof(DICT)) ;
  for (dict->dim = 10, dict->size = 1024 ; dict->size < size ; ++dict->dim, dict->size *= 2) ;
  dict->table = (int*)_mycalloc(dict->size,sizeof(int)) ;
  dict->names = (char**)_mycalloc(dict->size/2,sizeof(char*)) ;
  return dict ;
}

canonical_original_code:
struct DICT *dictCreate(int size)
{
  struct DICT *dict = (DICT*)_mycalloc(1,sizeof(DICT));
  for (dict->dim = 10, dict->size = 1024 ; dict->size < size ; ++dict->dim, dict->size *= 2) ;
  dict->table = (int *)_mycalloc(dict->size,sizeof(int)) ;
  dict->names = (char **)_mycalloc(dict->size/2,sizeof(char *)) ;
  return dict ;
}

variable_types: struct DICT { char **names; int *table; int max; int dim; int size; } *


dictDestroy

decompiled_code:
void __fastcall dictDestroy(void *ptr)
{
__int64 v1; // rax
__int64 v2; // rbx

if ( *((int *)ptr + 4) > 0 )
{
v1 = 0LL;
do
{
v2 = v1 + 1;
free(*(void **)(*(_QWORD *)ptr + 8 * (v1 + 1)));
v1 = v2;
}
while ( *((_DWORD *)ptr + 4) >= (int)v2 + 1 );
}
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void dictDestroy (DICT *dict)
{
  int i ;
  for (i = 1 ; i <= dict->max ; ++i) free (dict->names[i]) ;
  free (dict->names) ;
  free (dict->table) ;
  free (dict) ;
}

canonical_original_code:
void dictDestroy(struct DICT *dict)
{
  int i;
  for (i = 1 ; i <= dict->max ; ++i) free (dict->names[i]) ;
  free (dict->names) ;
  free (dict->table) ;
  free (dict) ;
}

user_defined_types: struct DICT { char **names; int *table; int max; int dim; int size; }