decompiled_code:
__int64 __fastcall alt_buffer_read(__int64 a1, _BYTE *a2, unsigned int a3)
{
_BYTE *v3; // r12
__int64 v6; // rcx
unsigned int v7; // edi
unsigned int v8; // edx
__int64 v9; // r9
unsigned int v10; // ecx
unsigned int v11; // esi
unsigned int v12; // edx
unsigned int v13; // ecx
unsigned int v14; // ecx
unsigned int v15; // esi
unsigned int v16; // edx
unsigned int v17; // eax
__int64 v18; // r13
char *v19; // r14
__int64 v20; // r13
unsigned int v22; // ecx
unsigned int v23; // esi
unsigned int v24; // edx
unsigned int v25; // ecx
unsigned int *v26; // r14
unsigned int v27; // edx
__int64 v28; // rax
unsigned int v29; // edx
char *v30; // rcx
char v31; // si
int v32; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = a2;
puts("A");
if ( !a1 || !a2 )
{
LODWORD(v20) = -1;
return (unsigned int)v20;
}
puts("B");
while ( 1 )
{
v6 = *(unsigned int *)(a1 + 32);
v7 = *(_DWORD *)(a1 + 52) % (unsigned int)v6;
v8 = *(_DWORD *)(a1 + 56) % (unsigned int)v6;
v9 = v7 - v8;
if ( v7 < v8 )
v9 = (_DWORD)v6 + v7 - v8;
printf("C: %i %i %i %i %i\n", *(unsigned int *)(a1 + 56), *(unsigned int *)(a1 + 44), v6, a3, v9);
v10 = *(_DWORD *)(a1 + 32);
v11 = *(_DWORD *)(a1 + 52) % v10;
v12 = *(_DWORD *)(a1 + 56) % v10;
v13 = v11 + v10 - v12;
if ( v11 >= v12 )
v13 = v11 - v12;
if ( v13 >= a3 )
break;
puts("D");
v14 = *(_DWORD *)(a1 + 32);
v15 = *(_DWORD *)(a1 + 52) % v14;
v16 = *(_DWORD *)(a1 + 48) % v14;
v17 = v14 + v15 - v16;
if ( v15 >= v16 )
v17 = v15 - v16;
v18 = v14 - v17;
v19 = (char *)malloc_or_die(v18);
v20 = (unsigned int)net_read(*(unsigned int *)(a1 + 76), v19, (unsigned int)v18, 0LL, 10LL);
printf("D.2: %i\n", v20);
if ( !(_DWORD)v20 )
{
free(v19);
goto LABEL_13;
}
printf("READ SOME BYTES: %i\n", (unsigned int)v20);
system("pause");
v29 = *(_DWORD *)(a1 + 52);
v30 = v19;
do
{
v31 = *v30++;
*(_BYTE *)(*(_QWORD *)(a1 + 8) + v29) = v31;
v29 = (unsigned int)(*(_DWORD *)(a1 + 52) + 1) % *(_DWORD *)(a1 + 32);
*(_DWORD *)(a1 + 52) = v29;
}
while ( &v19[(unsigned int)(v20 - 1) + 1] != v30 );
free(v19);
}
puts("C.2");
v22 = *(_DWORD *)(a1 + 32);
v23 = *(_DWORD *)(a1 + 44) % v22;
v24 = *(_DWORD *)(a1 + 56) % v22;
v25 = v23 + v22 - v24;
if ( v23 >= v24 )
v25 = v23 - v24;
if ( v25 < a3 || (v26 = (unsigned int *)(a1 + 56), *(_DWORD *)(a1 + 80) == *(_DWORD *)(a1 + 84)) )
{
v26 = (unsigned int *)&v32;
v32 = *(_DWORD *)(a1 + 40);
puts("C.3");
if ( a3 )
{
LABEL_20:
v27 = *v26;
LODWORD(v20) = 0;
do
{
if ( *(_DWORD *)(a1 + 44) == v27 )
break;
v28 = (unsigned int)v20 + v27;
LODWORD(v20) = v20 + 1;
*v3++ = *(_BYTE *)(*(_QWORD *)(a1 + 8) + v28);
v27 = (*v26 + 1) % *(_DWORD *)(a1 + 32);
*v26 = v27;
}
while ( a3 != (_DWORD)v20 );
puts("C.4");
goto LABEL_13;
}
}
else
{
puts("C.3");
if ( a3 )
goto LABEL_20;
}
LODWORD(v20) = 0;
puts("C.4");
LABEL_13:
puts("E");
printf("CONSUMED BYTES: %i\n", (unsigned int)v20);
return (unsigned int)v20;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, _BYTE *a2, unsigned int a3)
{
_BYTE *v3; // r12
__int64 v6; // rcx
unsigned int v7; // edi
unsigned int v8; // edx
__int64 v9; // r9
unsigned int v10; // ecx
unsigned int v11; // esi
unsigned int v12; // edx
unsigned int v13; // ecx
unsigned int v14; // ecx
unsigned int v15; // esi
unsigned int v16; // edx
unsigned int v17; // eax
__int64 v18; // r13
char *v19; // r14
__int64 v20; // r13
unsigned int v22; // ecx
unsigned int v23; // esi
unsigned int v24; // edx
unsigned int v25; // ecx
unsigned int *v26; // r14
unsigned int v27; // edx
__int64 v28; // rax
unsigned int v29; // edx
char *v30; // rcx
char v31; // si
int v32; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = a2;
puts("A");
if ( !a1 || !a2 )
{
LODWORD(v20) = -1;
return (unsigned int)v20;
}
puts("B");
while ( 1 )
{
v6 = *(unsigned int *)(a1 + 32);
v7 = *(_DWORD *)(a1 + 52) % (unsigned int)v6;
v8 = *(_DWORD *)(a1 + 56) % (unsigned int)v6;
v9 = v7 - v8;
if ( v7 < v8 )
v9 = (_DWORD)v6 + v7 - v8;
printf("C: %i %i %i %i %i\n", *(unsigned int *)(a1 + 56), *(unsigned int *)(a1 + 44), v6, a3, v9);
v10 = *(_DWORD *)(a1 + 32);
v11 = *(_DWORD *)(a1 + 52) % v10;
v12 = *(_DWORD *)(a1 + 56) % v10;
v13 = v11 + v10 - v12;
if ( v11 >= v12 )
v13 = v11 - v12;
if ( v13 >= a3 )
break;
puts("D");
v14 = *(_DWORD *)(a1 + 32);
v15 = *(_DWORD *)(a1 + 52) % v14;
v16 = *(_DWORD *)(a1 + 48) % v14;
v17 = v14 + v15 - v16;
if ( v15 >= v16 )
v17 = v15 - v16;
v18 = v14 - v17;
v19 = (char *)malloc_or_die(v18);
v20 = (unsigned int)net_read(*(unsigned int *)(a1 + 76), v19, (unsigned int)v18, 0LL, 10LL);
printf("D.2: %i\n", v20);
if ( !(_DWORD)v20 )
{
free(v19);
goto LABEL_13;
}
printf("READ SOME BYTES: %i\n", (unsigned int)v20);
system("pause");
v29 = *(_DWORD *)(a1 + 52);
v30 = v19;
do
{
v31 = *v30++;
*(_BYTE *)(*(_QWORD *)(a1 + 8) + v29) = v31;
v29 = (unsigned int)(*(_DWORD *)(a1 + 52) + 1) % *(_DWORD *)(a1 + 32);
*(_DWORD *)(a1 + 52) = v29;
}
while ( &v19[(unsigned int)(v20 - 1) + 1] != v30 );
free(v19);
}
puts("C.2");
v22 = *(_DWORD *)(a1 + 32);
v23 = *(_DWORD *)(a1 + 44) % v22;
v24 = *(_DWORD *)(a1 + 56) % v22;
v25 = v23 + v22 - v24;
if ( v23 >= v24 )
v25 = v23 - v24;
if ( v25 < a3 || (v26 = (unsigned int *)(a1 + 56), *(_DWORD *)(a1 + 80) == *(_DWORD *)(a1 + 84)) )
{
v26 = (unsigned int *)&v32;
v32 = *(_DWORD *)(a1 + 40);
puts("C.3");
if ( a3 )
{
LABEL_20:
v27 = *v26;
LODWORD(v20) = 0;
do
{
if ( *(_DWORD *)(a1 + 44) == v27 )
break;
v28 = (unsigned int)v20 + v27;
LODWORD(v20) = v20 + 1;
*v3++ = *(_BYTE *)(*(_QWORD *)(a1 + 8) + v28);
v27 = (*v26 + 1) % *(_DWORD *)(a1 + 32);
*v26 = v27;
}
while ( a3 != (_DWORD)v20 );
puts("C.4");
goto LABEL_13;
}
}
else
{
puts("C.3");
if ( a3 )
goto LABEL_20;
}
LODWORD(v20) = 0;
puts("C.4");
LABEL_13:
puts("E");
printf("CONSUMED BYTES: %i\n", (unsigned int)v20);
return (unsigned int)v20;
}

original_code:
int
alt_buffer_read(alt_buffer_t *buffer, uint8 *data, unsigned int length, unsigned int *seqn, unsigned int *ackn)
{
 int consumed_bytes;
 unsigned int *pos, temp_pos;
 unsigned int read_more_bytes;
 unsigned int read_bytes, r;
 uint8 *temp;
printf("A\n");
 if (!buffer || !data) {
  return -1;
 }
printf("B\n");
 do {
  printf("C: %i %i %i %i %i\n", buffer->read_pos, buffer->read_window_end, buffer->size, length, ( (buffer->read_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_end % buffer->size) ));
  if (( (buffer->read_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_end % buffer->size) ) >= length) {
   printf("C.2\n");
   if (( (buffer->read_window_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_window_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_window_end % buffer->size) ) >= length
     && buffer->same_ack_counter != buffer->max_same_ack) {
    pos = &(buffer->read_pos);
   } else {
    temp_pos = buffer->read_window_start;
    pos = &temp_pos;
   }
   printf("C.3\n");
   for (consumed_bytes = 0; consumed_bytes < length && *pos != buffer->read_window_end; consumed_bytes++) {
    data[consumed_bytes] = buffer->read_data[*pos + consumed_bytes];
    (*pos = (*pos + 1) % buffer->size);
   }
   printf("C.4\n");
  } else {
   printf("D\n");
   read_more_bytes = buffer->size - ( (buffer->read_end % buffer->size >= buffer->read_start % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_start % buffer->size) : (buffer->size - buffer->read_start % buffer->size + buffer->read_end % buffer->size) );
   temp = (uint8 *) malloc_or_die(read_more_bytes);
   read_bytes = net_read(buffer->read_fd, temp, read_more_bytes, 0, 10);
   printf("D.2: %i\n", read_bytes);
   if (!read_bytes) {
    consumed_bytes = 0;
   } else {
    printf("READ SOME BYTES: %i\n", read_bytes);
    system("pause");
    for (r = 0; r < read_bytes; r++) {
     buffer->read_data[buffer->read_end] = temp[r];
     (buffer->read_end = (buffer->read_end + 1) % buffer->size);
    }
    consumed_bytes = -1;
   }
   free(temp);
  }
 } while (consumed_bytes == -1);
printf("E\n");
 printf("CONSUMED BYTES: %i\n", consumed_bytes);
 return consumed_bytes;
}

canonical_original_code:
int alt_buffer_read(struct _alt_buffer_t *buffer, unsigned char *data, unsigned int length, unsigned int *seqn, unsigned int *ackn)
{
 int consumed_bytes;
 unsigned int *pos; unsigned int temp_pos;
 unsigned int read_more_bytes;
 unsigned int read_bytes; unsigned int r;
 unsigned char *temp;
printf("A\n");
 if (!buffer || !data) {
  return -1;
 }
printf("B\n");
 do {
  printf("C: %i %i %i %i %i\n", buffer->read_pos, buffer->read_window_end, buffer->size, length, ( (buffer->read_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_end % buffer->size) ));
  if (( (buffer->read_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_end % buffer->size) ) >= length) {
   printf("C.2\n");
   if (( (buffer->read_window_end % buffer->size >= buffer->read_pos % buffer->size) ? (buffer->read_window_end % buffer->size - buffer->read_pos % buffer->size) : (buffer->size - buffer->read_pos % buffer->size + buffer->read_window_end % buffer->size) ) >= length
     && buffer->same_ack_counter != buffer->max_same_ack) {
    pos = &(buffer->read_pos);
   } else {
    temp_pos = buffer->read_window_start;
    pos = &temp_pos;
   }
   printf("C.3\n");
   for (consumed_bytes = 0; consumed_bytes < length && *pos != buffer->read_window_end; consumed_bytes++) {
    data[consumed_bytes] = buffer->read_data[*pos + consumed_bytes];
    (*pos = (*pos + 1) % buffer->size);
   }
   printf("C.4\n");
  } else {
   printf("D\n");
   read_more_bytes = buffer->size - ( (buffer->read_end % buffer->size >= buffer->read_start % buffer->size) ? (buffer->read_end % buffer->size - buffer->read_start % buffer->size) : (buffer->size - buffer->read_start % buffer->size + buffer->read_end % buffer->size) );
   temp = (unsigned char *) malloc_or_die(read_more_bytes);
   read_bytes = net_read(buffer->read_fd, temp, read_more_bytes, 0, 10);
   printf("D.2: %i\n", read_bytes);
   if (!read_bytes) {
    consumed_bytes = 0;
   } else {
    printf("READ SOME BYTES: %i\n", read_bytes);
    system("pause");
    for (r = 0; r < read_bytes; r++) {
     buffer->read_data[buffer->read_end] = temp[r];
     (buffer->read_end = (buffer->read_end + 1) % buffer->size);
    }
    consumed_bytes = -1;
   }
   free(temp);
  }
 } while (consumed_bytes == -1);
printf("E\n");
 printf("CONSUMED BYTES: %i\n", consumed_bytes);
 return consumed_bytes;
}

variable_types:
length
unsigned int

variable_types:
seqn
unsigned int *

variable_types:
ackn
unsigned int *

variable_types:
pos
unsigned int *

variable_types:
r
unsigned int


debug

repo: xant/heyoka-unix

decompiled_code:
int debug(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("[DEBUG]: ");
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("[DEBUG]: ");
return vfprintf(stderr, format, arg);
}

original_code:
void
debug(const char *format, ...)
{
 va_list args;
 __builtin_va_start(args,format);
 printf("[DEBUG]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}

canonical_original_code:
void debug(char *format, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,format);
 printf("[DEBUG]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}

variable_types:
format
char *

return_type: void


error

decompiled_code:
int error(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("[ERROR]: ");
return vfprintf(stderr, format, arg);
}

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

va_start(arg, format);
printf("[ERROR]: ");
return vfprintf(stderr, format, arg);
}

original_code:
void
error(const char *format, ...)
{
 char buffer[256];
 va_list args;
 __builtin_va_start(args,format);
 printf("[ERROR]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}

canonical_original_code:
void error(char *format, ...)
{
 char buffer[256];
 __builtin_va_list args;
 __builtin_va_start(args,format);
 printf("[ERROR]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}

variable_types:
buffer
char [256]


warning

decompiled_code:
int warning(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("[WARNING]: ");
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func2(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("[WARNING]: ");
return vfprintf(stderr, format, arg);
}

original_code:
void
warning(const char *format, ...)
{
 va_list args;
 __builtin_va_start(args,format);
 printf("[WARNING]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}

canonical_original_code:
void warning(char *format, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,format);
 printf("[WARNING]: ");
 vfprintf(stderr, format, args);
 __builtin_va_end(args);
}


out

decompiled_code:
int out(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

canonical_decompiled_code:
int func3(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stdout, format, arg);
}

original_code:
void
out(const char *format, ...)
{
 va_list args;
 __builtin_va_start(args,format);
 vfprintf(stdout, format, args);
 __builtin_va_end(args);
}

canonical_original_code:
void out(char *format, ...)
{
 __builtin_va_list args;
 __builtin_va_start(args,format);
 vfprintf(stdout, format, args);
 __builtin_va_end(args);
}


hexdump

decompiled_code:
int __fastcall hexdump(const char *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
unsigned int v4; // ebx
const char *v5; // rbp
char s[4]; // [rsp+Ch] [rbp-BCh] BYREF
char v8[64]; // [rsp+10h] [rbp-B8h] BYREF
char dest[120]; // [rsp+50h] [rbp-78h] BYREF

v3 = a3;
out("%s (%i bytes):\n", a1, a3);
v8[0] = 0;
dest[0] = 0;
if ( v3 )
{
v4 = 1;
while ( 1 )
{
v5 = (const char *)(a2 + v4 - 1);
sprintf(s, "%.2X ", *(unsigned __int8 *)v5);
strcat(v8, s);
if ( (unsigned __int8)(*v5 - 33) <= 0x5Du )
{
strncat(dest, v5, 1uLL);
if ( (v4 & 0xF) != 0 )
goto LABEL_4;
LABEL_7:
out("%s %s\n", v8, dest);
++v4;
v8[0] = 0;
dest[0] = 0;
if ( v3 < v4 )
return out("%s %s\n", v8, dest);
}
else
{
*(_WORD *)&dest[strlen(dest)] = 46;
if ( (v4 & 0xF) == 0 )
goto LABEL_7;
LABEL_4:
if ( v3 < ++v4 )
return out("%s %s\n", v8, dest);
}
}
}
return out("%s %s\n", v8, dest);
}

canonical_decompiled_code:
int __fastcall func4(const char *a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
unsigned int v4; // ebx
const char *v5; // rbp
char s[4]; // [rsp+Ch] [rbp-BCh] BYREF
char v8[64]; // [rsp+10h] [rbp-B8h] BYREF
char dest[120]; // [rsp+50h] [rbp-78h] BYREF

v3 = a3;
func3("%s (%i bytes):\n", a1, a3);
v8[0] = 0;
dest[0] = 0;
if ( v3 )
{
v4 = 1;
while ( 1 )
{
v5 = (const char *)(a2 + v4 - 1);
sprintf(s, "%.2X ", *(unsigned __int8 *)v5);
strcat(v8, s);
if ( (unsigned __int8)(*v5 - 33) <= 0x5Du )
{
strncat(dest, v5, 1uLL);
if ( (v4 & 0xF) != 0 )
goto LABEL_4;
LABEL_7:
func3("%s %s\n", v8, dest);
++v4;
v8[0] = 0;
dest[0] = 0;
if ( v3 < v4 )
return func3("%s %s\n", v8, dest);
}
else
{
*(_WORD *)&dest[strlen(dest)] = 46;
if ( (v4 & 0xF) == 0 )
goto LABEL_7;
LABEL_4:
if ( v3 < ++v4 )
return func3("%s %s\n", v8, dest);
}
}
}
return func3("%s %s\n", v8, dest);
}

original_code:
void
hexdump(const char *label, const unsigned char *data,
            const unsigned int length)
{
        unsigned int i = 0;
        char line[16 * 3 + 1];
        char hrline[16 * 3 + 1];
        char hex[4];
        out("%s (%i bytes):\n", label, length);
        line[0] = '\0';
        hrline[0] = '\0';
        for (i = 1; i <= length; i++) {
                sprintf(hex, "%.2X ", data[i - 1]);
                strcat(line, hex);
                if (data[i - 1] >= 33 && data[i - 1] <= 126) {
                    strncat(hrline, data + (i - 1), 1);
                } else {
                    strcat(hrline, ".");
                }
                if (i % 16 == 0) {
                        out("%s   %s\n", line, hrline);
                        line[0] = '\0';
                        hrline[0] = '\0';
                }
        }
        out("%s   %s\n", line, hrline);
}

canonical_original_code:
void hexdump(char *label, unsigned char *data, unsigned int length)
{
        unsigned int i = 0;
        char line[-1];
        char hrline[-1];
        char hex[4];
        out("%s (%i bytes):\n", label, length);
        line[0] = '\0';
        hrline[0] = '\0';
        for (i = 1; i <= length; i++) {
                sprintf(hex, "%.2X ", data[i - 1]);
                strcat(line, hex);
                if (data[i - 1] >= 33 && data[i - 1] <= 126) {
                    strncat(hrline, data + (i - 1), 1);
                } else {
                    strcat(hrline, ".");
                }
                if (i % 16 == 0) {
                        out("%s   %s\n", line, hrline);
                        line[0] = '\0';
                        hrline[0] = '\0';
                }
        }
        out("%s   %s\n", line, hrline);
}

variable_types:
label
char *

variable_types:
data
unsigned char *

variable_types:
i
unsigned int

variable_types:
line
char [-1]

variable_types:
hrline
char [-1]

variable_types:
hex
char [4]


decompiled_code:
void *__fastcall malloc_or_die(size_t a1)
{
void *result; // rax

result = calloc(a1, 1uLL);
if ( !result )
{
error("out of memory; could not allocate %u bytes\n", a1);
exit(-1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(size_t a1)
{
void *result; // rax

result = calloc(a1, 1uLL);
if ( !result )
{
func1("out of memory; could not allocate %u bytes\n", a1);
exit(-1);
}
return result;
}

original_code:
void *
malloc_or_die(size_t size)
{
 void *ptr;
 ptr = (void *) malloc (size);
 if (!ptr) {
  error("out of memory; could not allocate %u bytes\n", size);
  exit(-1);
 }
 memset(ptr, 0x00, size);
 return ptr;
}

canonical_original_code:
void *malloc_or_die(unsigned long size)
{
 void *ptr;
 ptr = (void *) malloc (size);
 if (!ptr) {
  error("out of memory; could not allocate %u bytes\n", size);
  exit(-1);
 }
 memset(ptr, 0x00, size);
 return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


decompiled_code:
__int64 __fastcall is_inbetween(int a1, int a2, int a3, int a4)
{
return (a3 % a4 >= a2 % a4) ^ (unsigned __int8)((a1 % a4 <= a2 % a4) ^ (a1 % a4 < a3 % a4));
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, int a3, int a4)
{
return (a3 % a4 >= a2 % a4) ^ (unsigned __int8)((a1 % a4 <= a2 % a4) ^ (a1 % a4 < a3 % a4));
}

original_code:
static int
is_inbetween(int start, int val, int end, int mod)
{
 start %= mod;
 end %= mod;
 val %= mod;
 return ((end > start) ^ (val >= start) ^ (val <= end));
}

variable_types: start int

variable_types: val int

variable_types: end int

variable_types: mod int

return_type: int


delta

decompiled_code:
__int64 __fastcall delta(int a1, int a2, int a3)
{
int v4; // er8
int v5; // edx
unsigned int v6; // ecx
__int64 result; // rax

v4 = a1 % a3;
v5 = a2 % a3;
v6 = a2 % a3 + a3 - v4;
result = (unsigned int)(v5 - v4);
if ( v4 > v5 )
result = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3)
{
int v4; // er8
int v5; // edx
unsigned int v6; // ecx
__int64 result; // rax

v4 = a1 % a3;
v5 = a2 % a3;
v6 = a2 % a3 + a3 - v4;
result = (unsigned int)(v5 - v4);
if ( v4 > v5 )
result = v6;
return result;
}

original_code:
static unsigned int
delta(int start, int end, int mod)
{
 start %= mod;
 end %= mod;
 if (end >= start) {
  return end - start;
 }
 return mod - start + end;
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall buffer_new(size_t n, int a2, int a3)
{
__int64 v4; // rax
__int64 v5; // r12
void *v6; // rax
__int64 v7; // rax

v4 = malloc_or_die(96LL);
*(_DWORD *)v4 = n;
v5 = v4;
*(_DWORD *)(v4 + 4) = 0xFFFF;
*(_QWORD *)(v4 + 8) = malloc_or_die((unsigned int)n);
v6 = (void *)malloc_or_die((unsigned int)n);
*(_QWORD *)(v5 + 16) = v6;
memset(v6, 0, (unsigned int)n);
*(_DWORD *)(v5 + 24) = a3;
*(_QWORD *)(v5 + 28) = 4294901760LL;
v7 = malloc_or_die((unsigned int)n);
*(_DWORD *)(v5 + 48) = a2;
*(_QWORD *)(v5 + 40) = v7;
pthread_mutex_init((pthread_mutex_t *)(v5 + 56), 0LL);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(size_t n, int a2, int a3)
{
__int64 v4; // rax
__int64 v5; // r12
void *v6; // rax
__int64 v7; // rax

v4 = malloc_or_die(96LL);
*(_DWORD *)v4 = n;
v5 = v4;
*(_DWORD *)(v4 + 4) = 0xFFFF;
*(_QWORD *)(v4 + 8) = malloc_or_die((unsigned int)n);
v6 = (void *)malloc_or_die((unsigned int)n);
*(_QWORD *)(v5 + 16) = v6;
memset(v6, 0, (unsigned int)n);
*(_DWORD *)(v5 + 24) = a3;
*(_QWORD *)(v5 + 28) = 4294901760LL;
v7 = malloc_or_die((unsigned int)n);
*(_DWORD *)(v5 + 48) = a2;
*(_QWORD *)(v5 + 40) = v7;
pthread_mutex_init((pthread_mutex_t *)(v5 + 56), 0LL);
return v5;
}

original_code:
buffer_t *
buffer_new(unsigned int size, int fd_r, int fd_w)
{
 buffer_t *buffer;
 buffer = (buffer_t *) malloc_or_die(sizeof(buffer_t));
 buffer->size = size;
 buffer->ackn_r = 65535;
 buffer->ack_r_counter = 0;
 buffer->data_write = (uint8 *) malloc_or_die(size);
 buffer->map_write = (uint8 *) malloc_or_die(size);
 memset(buffer->map_write, 0, size);
 buffer->fd_w = fd_w;
 buffer->seqn_s = 0;
 buffer->ackn_s = 65535;
 buffer->endbuf = 0;
 buffer->data_read = (uint8 *) malloc_or_die(size);
 buffer->fd_r = fd_r;
        pthread_mutex_init(&buffer->lock, ((void *)0));
 return buffer;
}

canonical_original_code:
struct _buffer_t *buffer_new(unsigned int size, int fd_r, int fd_w)
{
 struct _buffer_t *buffer;
 buffer = (struct _buffer_t *) malloc_or_die(sizeof(buffer_t));
 buffer->size = size;
 buffer->ackn_r = 65535;
 buffer->ack_r_counter = 0;
 buffer->data_write = (unsigned char *) malloc_or_die(size);
 buffer->map_write = (unsigned char *) malloc_or_die(size);
 memset(buffer->map_write, 0, size);
 buffer->fd_w = fd_w;
 buffer->seqn_s = 0;
 buffer->ackn_s = 65535;
 buffer->endbuf = 0;
 buffer->data_read = (unsigned char *) malloc_or_die(size);
 buffer->fd_r = fd_r;
        pthread_mutex_init(&buffer->lock, ((void *)0));
 return buffer;
}

variable_types:
size
unsigned int

variable_types: struct _buffer_t { unsigned int size; unsigned short ackn_r; unsigned short ack_r_counter; unsigned char *data_write; unsigned char *map_write; int fd_w; unsigned short seqn_s; unsigned short ackn_s; unsigned int endbuf; unsigned char *data_read; int fd_r; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *

user_defined_types: struct _buffer_t { unsigned int size; unsigned short ackn_r; unsigned short ack_r_counter; unsigned char *data_write; unsigned char *map_write; int fd_w; unsigned short seqn_s; unsigned short ackn_s; unsigned int endbuf; unsigned char *data_read; int fd_r; union pthread_mutex_t lock; }

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

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


decompiled_code:
__int64 __fastcall buffer_write(__int64 a1, _BYTE *a2, unsigned int a3, unsigned __int16 a4, __int16 a5)
{
pthread_mutex_t *v5; // rbp
unsigned __int16 v11; // r12
int v12; // eax
unsigned int v13; // er10
int v14; // er11
int v15; // er9
unsigned int v16; // edx
_BYTE *v17; // rsi
unsigned int v18; // er8
int v19; // edi
unsigned int v20; // ecx
unsigned int v21; // edx
unsigned int v22; // er14
unsigned int v23; // er13
unsigned int v24; // eax
__int64 v25; // r15
bool v26; // cc
void *v28; // rdi
int v29; // esi
int v30; // eax
unsigned int v31; // esi
unsigned int v32; // [rsp+8h] [rbp-40h]
int v33; // [rsp+Ch] [rbp-3Ch]

if ( !a1 )
{
puts("buffer error");
return 0xFFFFFFFFLL;
}
v5 = (pthread_mutex_t *)(a1 + 56);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
if ( *(_WORD *)(a1 + 4) != a5 || a5 + 1 == *(_WORD *)(a1 + 28) )
{
*(_WORD *)(a1 + 4) = a5;
*(_WORD *)(a1 + 6) = 0;
}
else
{
++*(_WORD *)(a1 + 6);
}
v11 = *(_WORD *)(a1 + 30);
pthread_mutex_unlock(v5);
if ( a3 )
{
v32 = v11 + 1;
v12 = is_inbetween(v32, a4, (unsigned __int16)(v11 + 12000), *(_DWORD *)a1);
v15 = v14 + 12000;
if ( v12 )
{
v16 = a4 % v13;
LABEL_7:
if ( v16 != v15 )
{
v17 = a2;
v18 = v16 - (_DWORD)a2;
while ( 1 )
{
*(_BYTE *)(*(_QWORD *)(a1 + 8) + (v18 + (unsigned int)v17) % v13) = *v17;
*(_BYTE *)(*(_QWORD *)(a1 + 16) + (v18 + (unsigned int)v17) % *(_DWORD *)a1) = 1;
if ( a3 <= 1 - (int)a2 + (int)v17 || &a2[v14 + 11999 - v16] == v17 )
break;
v13 = *(_DWORD *)a1;
++v17;
}
}
goto LABEL_12;
}
v29 = a3 + a4;
v33 = v14 + 12000;
v30 = is_inbetween(v32, v29, v15, v13);
v15 = v33;
if ( !v30 )
return 0LL;
v16 = v32 % v13;
a3 = v32 % v13 + a3 - a4;
v31 = v29 - v32 % v13;
if ( v32 % v13 >= a4 )
a3 = v31;
if ( a3 )
goto LABEL_7;
}
LABEL_12:
v19 = *(_DWORD *)(a1 + 24);
if ( v19 == -1 )
{
if ( verbose > 0 )
debug("writing FD not ready yet\n");
return 0LL;
}
v20 = *(_DWORD *)a1;
v21 = ((unsigned int)v11 + 1) % *(_DWORD *)a1;
v22 = v21;
if ( *(_DWORD *)a1 )
{
v23 = 0;
do
{
if ( !*(_BYTE *)(*(_QWORD *)(a1 + 16) + v21) )
{
v24 = v22 + v23;
goto LABEL_18;
}
++v23;
v21 = (v21 + 1) % v20;
}
while ( v20 != v23 );
v25 = v22;
if ( v20 >= v22 + v20 )
goto LABEL_19;
}
else
{
v24 = ((unsigned int)v11 + 1) % *(_DWORD *)a1;
v23 = 0;
LABEL_18:
v25 = v22;
if ( v20 >= v24 )
goto LABEL_19;
}
v11 = -1;
send(v19, (const void *)(v25 + *(_QWORD *)(a1 + 8)), v20 - v22, 0);
v28 = (void *)(v25 + *(_QWORD *)(a1 + 16));
v25 = 0LL;
memset(v28, 0, *(_DWORD *)a1 - v22);
v19 = *(_DWORD *)(a1 + 24);
v23 += v22 - *(_DWORD *)a1;
LABEL_19:
send(v19, (const void *)(v25 + *(_QWORD *)(a1 + 8)), v23, 0);
memset((void *)(v25 + *(_QWORD *)(a1 + 16)), 0, v23);
pthread_mutex_lock(v5);
v26 = verbose <= 2;
*(_WORD *)(a1 + 30) = v11 + v23;
if ( !v26 )
printf("buffer.c: wrote %i bytes (ackn was %i is now: %i)\n", v23, v11, (unsigned __int16)(v11 + v23));
pthread_mutex_unlock(v5);
return v23;
}

original_code:
int
buffer_write(buffer_t *buffer, uint8 *data, unsigned int length, uint16 seqn, uint16 ackn)
{
 unsigned int i;
 unsigned int pos = 0;
 unsigned int pos_start;
 unsigned int nbytes = 0;
 uint16 ackn_s;
 if (!buffer) {
  printf("buffer error\n");
  return -1;
 }
        pthread_mutex_lock(&buffer->lock);
 if ( (buffer->ackn_r == ackn)
   && (buffer->seqn_s != ((buffer->ackn_r + 1) % (65535 + 1)))) {
  buffer->ack_r_counter++;
 } else {
  buffer->ackn_r = ackn;
  buffer->ack_r_counter = 0;
 }
 ackn_s = buffer->ackn_s;
        pthread_mutex_unlock(&buffer->lock);
 if (length > 0) {
  if (is_inbetween(ackn_s + 1, seqn, (uint16)(ackn_s + 12000), buffer->size)) {
   pos = seqn % buffer->size;
  } else {
   if (is_inbetween(ackn_s + 1, seqn + length, ackn_s + 12000, buffer->size)) {
    pos_start = (ackn_s + 1) % buffer->size;
    if (seqn > pos_start) {
     length -= seqn - pos_start;
    } else {
     length -= pos_start - seqn;
    }
    pos = pos_start;
   } else {
    return 0;
   }
  }
  for (i = 0; (i < length) && ((pos + i) != (ackn_s + 12000)); i++) {
   buffer->data_write[(pos + i) % buffer->size] = data[i];
   buffer->map_write[(pos + i) % buffer->size] = 1;
  }
 }
 if (buffer->fd_w == -1) {
  if (verbose > 0) {
   debug("writing FD not ready yet\n");
  }
  return 0;
 }
 pos_start = (ackn_s + 1) % buffer->size;
 pos = pos_start;
 for (nbytes = 0; nbytes < buffer->size; nbytes++) {
  if (!(buffer->map_write[pos])) {
   break;
  }
  pos = (pos + 1) % buffer->size;
 }
 if ((pos_start + nbytes) > buffer->size) {
  send(buffer->fd_w, buffer->data_write + pos_start, buffer->size - pos_start, 0);
  memset(buffer->map_write + pos_start, 0x00, buffer->size - pos_start);
  nbytes -= buffer->size - pos_start;
  pos_start = 0;
  ackn_s = 65535;
 }
 send(buffer->fd_w, buffer->data_write + pos_start , nbytes, 0);
 memset(buffer->map_write + pos_start, 0x00, nbytes);
        pthread_mutex_lock(&buffer->lock);
 buffer->ackn_s = (ackn_s + nbytes) % (65535 + 1);
 if (verbose > 2) {
  printf("buffer.c: wrote %i bytes (ackn was %i is now: %i)\n", nbytes, ackn_s, buffer->ackn_s);
 }
        pthread_mutex_unlock(&buffer->lock);
 return nbytes;
}

canonical_original_code:
int buffer_write(struct _buffer_t *buffer, unsigned char *data, unsigned int length, unsigned short seqn, unsigned short ackn)
{
 unsigned int i;
 unsigned int pos = 0;
 unsigned int pos_start;
 unsigned int nbytes = 0;
 unsigned short ackn_s;
 if (!buffer) {
  printf("buffer error\n");
  return -1;
 }
        pthread_mutex_lock(&buffer->lock);
 if ( (buffer->ackn_r == ackn)
   && (buffer->seqn_s != ((buffer->ackn_r + 1) % (65535 + 1)))) {
  buffer->ack_r_counter++;
 } else {
  buffer->ackn_r = ackn;
  buffer->ack_r_counter = 0;
 }
 ackn_s = buffer->ackn_s;
        pthread_mutex_unlock(&buffer->lock);
 if (length > 0) {
  if (is_inbetween(ackn_s + 1, seqn, (uint16)(ackn_s + 12000), buffer->size)) {
   pos = seqn % buffer->size;
  } else {
   if (is_inbetween(ackn_s + 1, seqn + length, ackn_s + 12000, buffer->size)) {
    pos_start = (ackn_s + 1) % buffer->size;
    if (seqn > pos_start) {
     length -= seqn - pos_start;
    } else {
     length -= pos_start - seqn;
    }
    pos = pos_start;
   } else {
    return 0;
   }
  }
  for (i = 0; (i < length) && ((pos + i) != (ackn_s + 12000)); i++) {
   buffer->data_write[(pos + i) % buffer->size] = data[i];
   buffer->map_write[(pos + i) % buffer->size] = 1;
  }
 }
 if (buffer->fd_w == -1) {
  if (verbose > 0) {
   debug("writing FD not ready yet\n");
  }
  return 0;
 }
 pos_start = (ackn_s + 1) % buffer->size;
 pos = pos_start;
 for (nbytes = 0; nbytes < buffer->size; nbytes++) {
  if (!(buffer->map_write[pos])) {
   break;
  }
  pos = (pos + 1) % buffer->size;
 }
 if ((pos_start + nbytes) > buffer->size) {
  send(buffer->fd_w, buffer->data_write + pos_start, buffer->size - pos_start, 0);
  memset(buffer->map_write + pos_start, 0x00, buffer->size - pos_start);
  nbytes -= buffer->size - pos_start;
  pos_start = 0;
  ackn_s = 65535;
 }
 send(buffer->fd_w, buffer->data_write + pos_start , nbytes, 0);
 memset(buffer->map_write + pos_start, 0x00, nbytes);
        pthread_mutex_lock(&buffer->lock);
 buffer->ackn_s = (ackn_s + nbytes) % (65535 + 1);
 if (verbose > 2) {
  printf("buffer.c: wrote %i bytes (ackn was %i is now: %i)\n", nbytes, ackn_s, buffer->ackn_s);
 }
        pthread_mutex_unlock(&buffer->lock);
 return nbytes;
}

variable_types:
length
unsigned int

variable_types:
seqn
unsigned short

variable_types:
ackn
unsigned short

variable_types:
pos
unsigned int

variable_types:
nbytes
unsigned int

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


decompiled_code:
__int64 __fastcall buffer_read(__int64 a1, __int64 a2, unsigned int a3, _WORD *a4, _WORD *a5, _DWORD *a6)
{
unsigned int v11; // er13
unsigned int v12; // eax
unsigned int v13; // er9
__int64 v14; // rsi
unsigned int v15; // er9
__int64 v16; // rdi
__int64 result; // rax
unsigned int v18; // er13
int v19; // edx
int v20; // edi
unsigned int v21; // ebx
unsigned int v22; // edx
unsigned int v23; // er9
_BYTE *v24; // r10
int v25; // eax
int v26; // edi
unsigned int v27; // eax
unsigned int v28; // er9
__int64 v29; // rcx
__int16 v30; // dx
unsigned int v31; // eax
unsigned int v32; // edx
unsigned __int16 v34; // [rsp+1Ch] [rbp-3Ch]
__int16 v35; // [rsp+1Eh] [rbp-3Ah]

if ( !a1 )
return 0xFFFFFFFFLL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v11 = *(unsigned __int16 *)(a1 + 4);
v35 = *(_WORD *)(a1 + 30);
v34 = *(_WORD *)(a1 + 6);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 56));
if ( a6 )
*a6 = 0;
if ( verbose > 1 )
printf(
"buffer.c: buffer->ackn_r:%u buffer->seqn_s:%u\n",
*(unsigned __int16 *)(a1 + 4),
*(unsigned __int16 *)(a1 + 28));
v12 = delta(v11, *(unsigned __int16 *)(a1 + 28), *(_DWORD *)a1);
if ( v34 > 2u || v12 > 0x2EE0 )
{
v18 = v11 + 1;
v19 = v18 % v13;
v20 = v18 % v13;
if ( a3 )
{
v21 = 0;
while ( 1 )
{
if ( (unsigned int)delta(v20, *(_DWORD *)(a1 + 32), v13) <= v21 )
{
v19 = v18 % v23;
result = v21;
goto LABEL_19;
}
v22 = (v20 + v21++) % v23;
*v24 = *(_BYTE *)(*(_QWORD *)(a1 + 40) + v22);
if ( a3 == v21 )
break;
v13 = *(_DWORD *)a1;
}
v19 = v18 % *(_DWORD *)a1;
result = a3;
}
else
{
result = 0LL;
}
LABEL_19:
*a4 = v19;
*a5 = v35;
if ( a6 )
*a6 = 1;
}
else
{
if ( verbose > 2 )
{
v31 = delta(v11 % v13, *(_DWORD *)(a1 + 32), v13);
printf("buffer.c: I REQUIRE %i bytes there are %i\n", a3, v31);
v13 = *(_DWORD *)a1;
}
v14 = *(unsigned int *)(a1 + 32);
if ( (unsigned int)delta(v11, v14, v13) < a3 )
{
v16 = *(unsigned int *)(a1 + 48);
if ( (int)v16 <= 0 )
{
*a4 = *(_WORD *)(a1 + 28);
*a5 = v35;
return 0LL;
}
v25 = net_read(v16, *(_QWORD *)(a1 + 40) + v14, v15 - (unsigned int)v14, 0LL, 100LL);
v15 = *(_DWORD *)a1;
if ( v25 )
{
v32 = (*(_DWORD *)(a1 + 32) + v25) % v15;
*(_DWORD *)(a1 + 32) = v32;
LODWORD(v14) = v32;
}
else
{
LODWORD(v14) = *(_DWORD *)(a1 + 32);
}
}
v26 = *(unsigned __int16 *)(a1 + 28);
v27 = delta(v26, v14, v15);
*a4 = v26;
if ( a3 > v27 )
a3 = v27;
if ( a3 )
{
v29 = 0LL;
while ( 1 )
{
*(_BYTE *)(a2 + v29) = *(_BYTE *)(*(_QWORD *)(a1 + 40)
+ ((unsigned int)v29 + *(unsigned __int16 *)(a1 + 28)) % v28);
if ( a3 == ++v29 )
break;
v28 = *(_DWORD *)a1;
}
v30 = a3;
result = a3;
}
else
{
result = 0LL;
v30 = 0;
}
*(_WORD *)(a1 + 28) += v30;
*a5 = v35;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int a3, _WORD *a4, _WORD *a5, _DWORD *a6)
{
unsigned int v11; // er13
unsigned int v12; // eax
unsigned int v13; // er9
__int64 v14; // rsi
unsigned int v15; // er9
__int64 v16; // rdi
__int64 result; // rax
unsigned int v18; // er13
int v19; // edx
int v20; // edi
unsigned int v21; // ebx
unsigned int v22; // edx
unsigned int v23; // er9
_BYTE *v24; // r10
int v25; // eax
int v26; // edi
unsigned int v27; // eax
unsigned int v28; // er9
__int64 v29; // rcx
__int16 v30; // dx
unsigned int v31; // eax
unsigned int v32; // edx
unsigned __int16 v34; // [rsp+1Ch] [rbp-3Ch]
__int16 v35; // [rsp+1Eh] [rbp-3Ah]

if ( !a1 )
return 0xFFFFFFFFLL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
v11 = *(unsigned __int16 *)(a1 + 4);
v35 = *(_WORD *)(a1 + 30);
v34 = *(_WORD *)(a1 + 6);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 56));
if ( a6 )
*a6 = 0;
if ( verbose > 1 )
printf(
"buffer.c: buffer->ackn_r:%u buffer->seqn_s:%u\n",
*(unsigned __int16 *)(a1 + 4),
*(unsigned __int16 *)(a1 + 28));
v12 = func1(v11, *(unsigned __int16 *)(a1 + 28), *(_DWORD *)a1);
if ( v34 > 2u || v12 > 0x2EE0 )
{
v18 = v11 + 1;
v19 = v18 % v13;
v20 = v18 % v13;
if ( a3 )
{
v21 = 0;
while ( 1 )
{
if ( (unsigned int)func1(v20, *(_DWORD *)(a1 + 32), v13) <= v21 )
{
v19 = v18 % v23;
result = v21;
goto LABEL_19;
}
v22 = (v20 + v21++) % v23;
*v24 = *(_BYTE *)(*(_QWORD *)(a1 + 40) + v22);
if ( a3 == v21 )
break;
v13 = *(_DWORD *)a1;
}
v19 = v18 % *(_DWORD *)a1;
result = a3;
}
else
{
result = 0LL;
}
LABEL_19:
*a4 = v19;
*a5 = v35;
if ( a6 )
*a6 = 1;
}
else
{
if ( verbose > 2 )
{
v31 = func1(v11 % v13, *(_DWORD *)(a1 + 32), v13);
printf("buffer.c: I REQUIRE %i bytes there are %i\n", a3, v31);
v13 = *(_DWORD *)a1;
}
v14 = *(unsigned int *)(a1 + 32);
if ( (unsigned int)func1(v11, v14, v13) < a3 )
{
v16 = *(unsigned int *)(a1 + 48);
if ( (int)v16 <= 0 )
{
*a4 = *(_WORD *)(a1 + 28);
*a5 = v35;
return 0LL;
}
v25 = net_read(v16, *(_QWORD *)(a1 + 40) + v14, v15 - (unsigned int)v14, 0LL, 100LL);
v15 = *(_DWORD *)a1;
if ( v25 )
{
v32 = (*(_DWORD *)(a1 + 32) + v25) % v15;
*(_DWORD *)(a1 + 32) = v32;
LODWORD(v14) = v32;
}
else
{
LODWORD(v14) = *(_DWORD *)(a1 + 32);
}
}
v26 = *(unsigned __int16 *)(a1 + 28);
v27 = func1(v26, v14, v15);
*a4 = v26;
if ( a3 > v27 )
a3 = v27;
if ( a3 )
{
v29 = 0LL;
while ( 1 )
{
*(_BYTE *)(a2 + v29) = *(_BYTE *)(*(_QWORD *)(a1 + 40)
+ ((unsigned int)v29 + *(unsigned __int16 *)(a1 + 28)) % v28);
if ( a3 == ++v29 )
break;
v28 = *(_DWORD *)a1;
}
v30 = a3;
result = a3;
}
else
{
result = 0LL;
v30 = 0;
}
*(_WORD *)(a1 + 28) += v30;
*a5 = v35;
}
return result;
}

original_code:
int
buffer_read(buffer_t *buffer, uint8 *data, unsigned int length, uint16 *seqn, uint16 *ackn, int *reread)
{
 unsigned int pos;
 unsigned int nbytes;
 unsigned int i;
 uint16 ackn_r;
 uint16 ackn_s;
 unsigned int ack_r_counter;
 if (!buffer) {
  return -1;
 }
        pthread_mutex_lock(&buffer->lock);
 ackn_r = buffer->ackn_r;
 ackn_s = buffer->ackn_s;
 ack_r_counter = buffer->ack_r_counter;
        pthread_mutex_unlock(&buffer->lock);
 if (reread) {
  *reread = 0;
 }
 if (verbose > 1) {
  printf("buffer.c: buffer->ackn_r:%u buffer->seqn_s:%u\n",buffer->ackn_r,buffer->seqn_s);
 }
 if ((delta(ackn_r, buffer->seqn_s, buffer->size) > 12000) ||
  (ack_r_counter >= 3)) {
  pos = (ackn_r + 1) % buffer->size;
  for (nbytes = 0; nbytes < length
        && (nbytes < delta(pos, buffer->endbuf, buffer->size)); nbytes++) {
   data[nbytes] = buffer->data_read[(pos + nbytes) % buffer->size];
  }
  *seqn = (ackn_r + 1) % buffer->size;
  *ackn = (ackn_s);
  if (reread) {
   *reread = 1;
  }
  return nbytes;
 } else {
  if (verbose > 2) {
   printf("buffer.c: I REQUIRE %i bytes there are %i\n", length, delta((ackn_r % buffer->size), buffer->endbuf, buffer->size));
  }
  if (delta(ackn_r, buffer->endbuf, buffer->size) < length) {
   if (buffer->fd_r > 0) {
    i = net_read(buffer->fd_r, buffer->data_read + buffer->endbuf, buffer->size - buffer->endbuf, 0, 100);
   } else {
    *seqn = buffer->seqn_s;
    *ackn = ackn_s;
    return 0;
   }
   if (i > 0) {
    buffer->endbuf = (buffer->endbuf + i) % buffer->size;
   }
  }
  i = delta(buffer->seqn_s, buffer->endbuf, buffer->size);
  *seqn = buffer->seqn_s;
  for (nbytes = 0; nbytes < length && nbytes < i; nbytes++) {
   data[nbytes] = buffer->data_read[(buffer->seqn_s + nbytes) % buffer->size];
  }
  buffer->seqn_s = (buffer->seqn_s + nbytes) % (65535 + 1);
  *ackn =ackn_s;
  return nbytes;
 }
}

canonical_original_code:
int buffer_read(struct _buffer_t *buffer, unsigned char *data, unsigned int length, unsigned short *seqn, unsigned short *ackn, int *reread)
{
 unsigned int pos;
 unsigned int nbytes;
 unsigned int i;
 unsigned short ackn_r;
 unsigned short ackn_s;
 unsigned int ack_r_counter;
 if (!buffer) {
  return -1;
 }
        pthread_mutex_lock(&buffer->lock);
 ackn_r = buffer->ackn_r;
 ackn_s = buffer->ackn_s;
 ack_r_counter = buffer->ack_r_counter;
        pthread_mutex_unlock(&buffer->lock);
 if (reread) {
  *reread = 0;
 }
 if (verbose > 1) {
  printf("buffer.c: buffer->ackn_r:%u buffer->seqn_s:%u\n",buffer->ackn_r,buffer->seqn_s);
 }
 if ((delta(ackn_r, buffer->seqn_s, buffer->size) > 12000) ||
  (ack_r_counter >= 3)) {
  pos = (ackn_r + 1) % buffer->size;
  for (nbytes = 0; nbytes < length
        && (nbytes < delta(pos, buffer->endbuf, buffer->size)); nbytes++) {
   data[nbytes] = buffer->data_read[(pos + nbytes) % buffer->size];
  }
  *seqn = (ackn_r + 1) % buffer->size;
  *ackn = (ackn_s);
  if (reread) {
   *reread = 1;
  }
  return nbytes;
 } else {
  if (verbose > 2) {
   printf("buffer.c: I REQUIRE %i bytes there are %i\n", length, delta((ackn_r % buffer->size), buffer->endbuf, buffer->size));
  }
  if (delta(ackn_r, buffer->endbuf, buffer->size) < length) {
   if (buffer->fd_r > 0) {
    i = net_read(buffer->fd_r, buffer->data_read + buffer->endbuf, buffer->size - buffer->endbuf, 0, 100);
   } else {
    *seqn = buffer->seqn_s;
    *ackn = ackn_s;
    return 0;
   }
   if (i > 0) {
    buffer->endbuf = (buffer->endbuf + i) % buffer->size;
   }
  }
  i = delta(buffer->seqn_s, buffer->endbuf, buffer->size);
  *seqn = buffer->seqn_s;
  for (nbytes = 0; nbytes < length && nbytes < i; nbytes++) {
   data[nbytes] = buffer->data_read[(buffer->seqn_s + nbytes) % buffer->size];
  }
  buffer->seqn_s = (buffer->seqn_s + nbytes) % (65535 + 1);
  *ackn =ackn_s;
  return nbytes;
 }
}

variable_types:
data
unsigned char *

variable_types:
seqn
unsigned short *

variable_types:
ackn
unsigned short *

variable_types:
reread
int *

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall master_create_slave(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // er14
__int64 v4; // rbx
unsigned int v5; // er12
__int64 v6; // r15
struct timeval *v7; // rax
__int64 v8; // r13
__int64 v9; // rbx
__int64 v10; // rax
const char *v11; // rdx
in_addr_t v12; // eax
const char *v13; // rdx
pthread_t newthread[8]; // [rsp+18h] [rbp-40h] BYREF

v3 = a3;
v4 = 1LL;
while ( 1 )
{
v5 = v4;
if ( !slaves[v4] )
break;
if ( ++v4 == 255 )
{
v5 = -1;
debug("All slave slots are full, sorry\n", a2, a3, a1);
return v5;
}
}
v6 = (int)v4;
v7 = (struct timeval *)malloc_or_die(48LL);
slaves[(int)v4] = (__int64)v7;
gettimeofday(v7, 0LL);
v8 = slaves[(int)v4];
*(_BYTE *)(v8 + 16) = v4;
*(_QWORD *)(v8 + 24) = a1;
*(_DWORD *)(v8 + 40) = (unsigned __int16)a2;
*(_QWORD *)(v8 + 32) = buffer_new(0x10000LL, 0xFFFFFFFFLL, 0xFFFFFFFFLL);
if ( v3 )
{
pthread_create(newthread, 0LL, master_wait_for_connection, (void *)slaves[(int)v4]);
pthread_detach(newthread[0]);
}
else
{
v9 = *(_QWORD *)(slaves[(int)v4] + 32);
*(_DWORD *)(v9 + 48) = net_socket(1LL, 0LL, 0LL, 0LL);
v10 = *(_QWORD *)(slaves[v6] + 32);
v11 = (const char *)*(unsigned int *)(v10 + 48);
if ( (_DWORD)v11 == -1 )
{
v5 = 0;
error((int)"Unable to create socket... exiting\n", 0, v11);
}
else
{
*(_DWORD *)(v10 + 24) = (_DWORD)v11;
debug("Connecting to 127.0.0.1:%i\n", (unsigned __int16)a2);
v12 = inet_addr("127.0.0.1");
if ( (unsigned int)net_connect(*(unsigned int *)(*(_QWORD *)(slaves[v6] + 32) + 48LL), v12, (unsigned __int16)a2) == -1 )
{
v5 = 0;
error((int)"Unable to connect to: 127.0.0.1:%i\n", (unsigned __int16)a2, v13);
}
else
{
debug("Connected to 127.0.0.1:%i\n", (unsigned __int16)a2);
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
int v3; // er14
__int64 v4; // rbx
unsigned int v5; // er12
__int64 v6; // r15
struct timeval *v7; // rax
__int64 v8; // r13
__int64 v9; // rbx
__int64 v10; // rax
const char *v11; // rdx
in_addr_t v12; // eax
const char *v13; // rdx
pthread_t newthread[8]; // [rsp+18h] [rbp-40h] BYREF

v3 = a3;
v4 = 1LL;
while ( 1 )
{
v5 = v4;
if ( !slaves[v4] )
break;
if ( ++v4 == 255 )
{
v5 = -1;
debug("All slave slots are full, sorry\n", a2, a3, a1);
return v5;
}
}
v6 = (int)v4;
v7 = (struct timeval *)malloc_or_die(48LL);
slaves[(int)v4] = (__int64)v7;
gettimeofday(v7, 0LL);
v8 = slaves[(int)v4];
*(_BYTE *)(v8 + 16) = v4;
*(_QWORD *)(v8 + 24) = a1;
*(_DWORD *)(v8 + 40) = (unsigned __int16)a2;
*(_QWORD *)(v8 + 32) = buffer_new(0x10000LL, 0xFFFFFFFFLL, 0xFFFFFFFFLL);
if ( v3 )
{
pthread_create(newthread, 0LL, master_wait_for_connection, (void *)slaves[(int)v4]);
pthread_detach(newthread[0]);
}
else
{
v9 = *(_QWORD *)(slaves[(int)v4] + 32);
*(_DWORD *)(v9 + 48) = net_socket(1LL, 0LL, 0LL, 0LL);
v10 = *(_QWORD *)(slaves[v6] + 32);
v11 = (const char *)*(unsigned int *)(v10 + 48);
if ( (_DWORD)v11 == -1 )
{
v5 = 0;
error((int)"Unable to create socket... exiting\n", 0, v11);
}
else
{
*(_DWORD *)(v10 + 24) = (_DWORD)v11;
debug("Connecting to 127.0.0.1:%i\n", (unsigned __int16)a2);
v12 = inet_addr("127.0.0.1");
if ( (unsigned int)net_connect(*(unsigned int *)(*(_QWORD *)(slaves[v6] + 32) + 48LL), v12, (unsigned __int16)a2) == -1 )
{
v5 = 0;
error((int)"Unable to connect to: 127.0.0.1:%i\n", (unsigned __int16)a2, v13);
}
else
{
debug("Connected to 127.0.0.1:%i\n", (unsigned __int16)a2);
}
}
}
return v5;
}

original_code:
static int
master_create_slave(char *domain, uint16 port, int do_listen)
{
 uint8 ticket;
 for (ticket = 1; ticket < 255; ticket++) {
  if (!slaves[ticket]) {
   slaves[ticket] = (master_slave_tracker_t *) malloc_or_die(sizeof(master_slave_tracker_t));
            gettimeofday(&(slaves[ticket]->last_used), ((void *)0));
   slaves[ticket]->ticket = ticket;
   slaves[ticket]->domain = domain;
   slaves[ticket]->tcp_port = port;
   slaves[ticket]->buffer = buffer_new(65535 + 1, -1, -1);
   if (do_listen) {
                pthread_t new_thread;
                pthread_create(&new_thread, ((void *)0), master_wait_for_connection, slaves[ticket]);
                pthread_detach(new_thread);
   } else {
    slaves[ticket]->buffer->fd_r = net_socket(SOCK_STREAM, 0, 0, 0);
    if (slaves[ticket]->buffer->fd_r == -1) {
     error("Unable to create socket... exiting\n");
     return 0;
    }
    slaves[ticket]->buffer->fd_w = slaves[ticket]->buffer->fd_r;
    debug("Connecting to 127.0.0.1:%i\n", port);
    if (net_connect(slaves[ticket]->buffer->fd_r,
          inet_addr("127.0.0.1"),
           port) == -1) {
     error("Unable to connect to: 127.0.0.1:%i\n", port);
     return 0;
    }
    debug("Connected to 127.0.0.1:%i\n", port);
   }
   return ticket;
  }
 }
 debug("All slave slots are full, sorry\n");
 return -1;
}

canonical_original_code:
static int master_create_slave(char *domain, unsigned short port, int do_listen)
{
 unsigned char ticket;
 for (ticket = 1; ticket < 255; ticket++) {
  if (!slaves[ticket]) {
   slaves[ticket] = (struct _master_slave_tracker_t *) malloc_or_die(sizeof(master_slave_tracker_t));
            gettimeofday(&(slaves[ticket]->last_used), ((void *)0));
   slaves[ticket]->ticket = ticket;
   slaves[ticket]->domain = domain;
   slaves[ticket]->tcp_port = port;
   slaves[ticket]->buffer = buffer_new(65535 + 1, -1, -1);
   if (do_listen) {
                unsigned long new_thread;
                pthread_create(&new_thread, ((void *)0), master_wait_for_connection, slaves[ticket]);
                pthread_detach(new_thread);
   } else {
    slaves[ticket]->buffer->fd_r = net_socket(SOCK_STREAM, 0, 0, 0);
    if (slaves[ticket]->buffer->fd_r == -1) {
     error("Unable to create socket... exiting\n");
     return 0;
    }
    slaves[ticket]->buffer->fd_w = slaves[ticket]->buffer->fd_r;
    debug("Connecting to 127.0.0.1:%i\n", port);
    if (net_connect(slaves[ticket]->buffer->fd_r,
          inet_addr("127.0.0.1"),
           port) == -1) {
     error("Unable to connect to: 127.0.0.1:%i\n", port);
     return 0;
    }
    debug("Connected to 127.0.0.1:%i\n", port);
   }
   return ticket;
  }
 }
 debug("All slave slots are full, sorry\n");
 return -1;
}

variable_types:
domain
char *

variable_types:
port
unsigned short

variable_types:
ticket
unsigned char

user_defined_types: struct _master_slave_tracker_t { struct timeval last_used; unsigned char ticket; char *domain; struct _buffer_t *buffer; int tcp_port; }

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


decompiled_code:
__int64 __fastcall master_handle_hello(_BYTE *a1, unsigned __int8 *a2, unsigned int a3, const char *a4, unsigned __int16 a5, unsigned int a6)
{
unsigned __int8 v9; // r14
int v10; // eax
unsigned int v12; // eax
char v14; // [rsp+Fh] [rbp-203Dh] BYREF
int v15; // [rsp+10h] [rbp-203Ch] BYREF
unsigned __int8 dest; // [rsp+14h] [rbp-2038h] BYREF
int v17; // [rsp+15h] [rbp-2037h]
char v18; // [rsp+1014h] [rbp-1038h] BYREF
char v19[4151]; // [rsp+1015h] [rbp-1037h] BYREF

if ( !a1 )
return 0LL;
if ( !a4 )
return 0LL;
debug("Slave is attempting a handshake\n");
v9 = *a2;
if ( !*a2 )
{
v12 = master_create_slave((__int64)a4, a5, a6);
v9 = v12;
if ( v12 != -1 )
{
debug("created new slave instance with ticket %i\n", v12);
goto LABEL_5;
}
return 0LL;
}
debug("the slave is already using ticket %i\n", v9);
LABEL_5:
v14 = (*a1 >> 1) & 4 | 2;
v18 = net_encode_flag(&v14);
memcpy(&dest, a2, a3);
dest = v9;
v17 = 0;
v10 = strlen(a4);
codec_encode((unsigned int)((v14 & 4) == 0) + 1, &dest, a3, v19, &v15, (unsigned int)(253 - v10));
debug("sending HELLO response\n");
return net_craft_dns_rr_answer(&v18, (unsigned int)(v15 + 1), a4);
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, unsigned __int8 *a2, unsigned int a3, const char *a4, unsigned __int16 a5, unsigned int a6)
{
unsigned __int8 v9; // r14
int v10; // eax
unsigned int v12; // eax
char v14; // [rsp+Fh] [rbp-203Dh] BYREF
int v15; // [rsp+10h] [rbp-203Ch] BYREF
unsigned __int8 dest; // [rsp+14h] [rbp-2038h] BYREF
int v17; // [rsp+15h] [rbp-2037h]
char v18; // [rsp+1014h] [rbp-1038h] BYREF
char v19[4151]; // [rsp+1015h] [rbp-1037h] BYREF

if ( !a1 )
return 0LL;
if ( !a4 )
return 0LL;
debug("Slave is attempting a handshake\n");
v9 = *a2;
if ( !*a2 )
{
v12 = func0((__int64)a4, a5, a6);
v9 = v12;
if ( v12 != -1 )
{
debug("created new slave instance with ticket %i\n", v12);
goto LABEL_5;
}
return 0LL;
}
debug("the slave is already using ticket %i\n", v9);
LABEL_5:
v14 = (*a1 >> 1) & 4 | 2;
v18 = net_encode_flag(&v14);
memcpy(&dest, a2, a3);
dest = v9;
v17 = 0;
v10 = strlen(a4);
codec_encode((unsigned int)((v14 & 4) == 0) + 1, &dest, a3, v19, &v15, (unsigned int)(253 - v10));
debug("sending HELLO response\n");
return net_craft_dns_rr_answer(&v18, (unsigned int)(v15 + 1), a4);
}

original_code:
static struct net_dns_rr_answer_t *
master_handle_hello(tunnel_header_slave_flag_t *slave_flag,
     unsigned char *dec_data,
     unsigned int dec_data_length,
     char *domain,
     uint16 port, int do_listen)
{
 char data[4096];
 char enc_data[4096];
 tunnel_header_master_flag_t master_flag;
 tunnel_header_t hdr, *slave_hdr;
 int ticket;
 unsigned int data_length, enc_data_length, max_out_length;
 if (!slave_flag || !dec_data || !domain || !domain) {
  return 0;
 }
 debug("Slave is attempting a handshake\n");
 slave_hdr = (tunnel_header_t *) dec_data;
 if (!slave_hdr->ticket) {
  ticket = master_create_slave(domain, port, do_listen);
  if (ticket == -1) {
   return 0;
  }
  debug("created new slave instance with ticket %i\n", ticket);
 } else {
  ticket = slave_hdr->ticket;
  debug("the slave is already using ticket %i\n", ticket);
 }
 memset(&master_flag, 0x00, sizeof(tunnel_header_flag_t));
 master_flag.hello_reply = 1;
 master_flag.binary_txt = slave_flag->expect_binary;
 *((uint8 *)enc_data) = net_encode_flag((tunnel_header_flag_t *) &master_flag);
 hdr.last_received = 0;
 hdr.sequence = 0;
 hdr.ticket = ticket;
 memcpy(data, dec_data, dec_data_length);
 memcpy(data, &hdr, sizeof(tunnel_header_t));
 data_length = dec_data_length;
 max_out_length = 254 - strlen(domain) - 1;
 codec_encode(master_flag.binary_txt ? 0x1 : 0x2,
      data, data_length, enc_data + 1, &enc_data_length, max_out_length);
 debug("sending HELLO response\n");
 return net_craft_dns_rr_answer(enc_data, enc_data_length + 1, domain);
}

canonical_original_code:
static struct net_dns_rr_answer_t *master_handle_hello(struct _tunnel_header_slave_flag_t *slave_flag, unsigned char *dec_data, unsigned int dec_data_length, char *domain, unsigned short port, int do_listen)
{
 char data[4096];
 char enc_data[4096];
 struct _tunnel_header_master_flag_t master_flag;
 struct _tunnel_header_t hdr; struct _tunnel_header_t *slave_hdr;
 int ticket;
 unsigned int data_length; unsigned int enc_data_length; unsigned int max_out_length;
 if (!slave_flag || !dec_data || !domain || !domain) {
  return 0;
 }
 debug("Slave is attempting a handshake\n");
 slave_hdr = (struct _tunnel_header_t *) dec_data;
 if (!slave_hdr->ticket) {
  ticket = master_create_slave(domain, port, do_listen);
  if (ticket == -1) {
   return 0;
  }
  debug("created new slave instance with ticket %i\n", ticket);
 } else {
  ticket = slave_hdr->ticket;
  debug("the slave is already using ticket %i\n", ticket);
 }
 memset(&master_flag, 0x00, sizeof(tunnel_header_flag_t));
 master_flag.hello_reply = 1;
 master_flag.binary_txt = slave_flag->expect_binary;
 *((unsigned char *)enc_data) = net_encode_flag((struct _tunnel_header_flag_t *) &master_flag);
 hdr.last_received = 0;
 hdr.sequence = 0;
 hdr.ticket = ticket;
 memcpy(data, dec_data, dec_data_length);
 memcpy(data, &hdr, sizeof(tunnel_header_t));
 data_length = dec_data_length;
 max_out_length = 254 - strlen(domain) - 1;
 codec_encode(master_flag.binary_txt ? 0x1 : 0x2,
      data, data_length, enc_data + 1, &enc_data_length, max_out_length);
 debug("sending HELLO response\n");
 return net_craft_dns_rr_answer(enc_data, enc_data_length + 1, domain);
}

variable_types: struct _tunnel_header_slave_flag_t { unsigned char spoofed; unsigned char codec; unsigned char hello; unsigned char expect_binary; unsigned char control; unsigned char zero; } *

variable_types:
data
char [4096]

variable_types: struct _tunnel_header_master_flag_t { unsigned char more_data; unsigned char hello_reply; unsigned char binary_txt; unsigned char control; unsigned char zero; }

variable_types: struct _tunnel_header_t { unsigned char ticket; unsigned short sequence; unsigned short last_received; } *

variable_types: ticket int

return_type: struct net_dns_rr_answer_t { unsigned short name; unsigned short type; unsigned short class; unsigned int ttl; unsigned short length; unsigned char data[254]; } *

user_defined_types: struct _tunnel_header_flag_t { union { struct _tunnel_header_slave_flag_t slave; struct _tunnel_header_master_flag_t master; } un; }


decompiled_code:
void *__fastcall master_wait_for_connection(void *a1)
{
unsigned int v1; // eax
const char *v2; // rdx
__int64 v3; // rsi
unsigned int v4; // ebp
__int64 v5; // r12
__int64 v6; // rax
const char *v7; // rdx
void *result; // rax

v1 = net_socket(1LL, 0LL, *((unsigned __int16 *)a1 + 20), 1LL);
if ( v1 == -1 )
{
error((int)"cannot create master data socket\n", 0, v2);
result = 0LL;
}
else
{
v3 = *((unsigned int *)a1 + 10);
v4 = v1;
debug("Waiting for connection on %i/TCP\n", v3);
v5 = *((_QWORD *)a1 + 4);
*(_DWORD *)(v5 + 48) = net_accept(v4);
v6 = *((_QWORD *)a1 + 4);
v7 = (const char *)*(unsigned int *)(v6 + 48);
if ( (_DWORD)v7 == -1 )
{
error((int)"connection was not accepted\n", v3, v7);
}
else
{
*(_DWORD *)(v6 + 24) = (_DWORD)v7;
debug("connection established\n");
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
unsigned int v1; // eax
const char *v2; // rdx
__int64 v3; // rsi
unsigned int v4; // ebp
__int64 v5; // r12
__int64 v6; // rax
const char *v7; // rdx
void *result; // rax

v1 = net_socket(1LL, 0LL, *((unsigned __int16 *)a1 + 20), 1LL);
if ( v1 == -1 )
{
error((int)"cannot create master data socket\n", 0, v2);
result = 0LL;
}
else
{
v3 = *((unsigned int *)a1 + 10);
v4 = v1;
debug("Waiting for connection on %i/TCP\n", v3);
v5 = *((_QWORD *)a1 + 4);
*(_DWORD *)(v5 + 48) = net_accept(v4);
v6 = *((_QWORD *)a1 + 4);
v7 = (const char *)*(unsigned int *)(v6 + 48);
if ( (_DWORD)v7 == -1 )
{
error((int)"connection was not accepted\n", v3, v7);
}
else
{
*(_DWORD *)(v6 + 24) = (_DWORD)v7;
debug("connection established\n");
}
result = 0LL;
}
return result;
}

original_code:
static void *
master_wait_for_connection(void *s_ptr)
{
 master_slave_tracker_t *slave;
 int sockfd;
 slave = (master_slave_tracker_t *) s_ptr;
 sockfd = net_socket(SOCK_STREAM, 0, slave->tcp_port, 0x01);
 if (sockfd == -1) {
  error("cannot create master data socket\n");
  return 0;
 }
 debug("Waiting for connection on %i/TCP\n", slave->tcp_port);
 slave->buffer->fd_r = net_accept(sockfd);
 if (slave->buffer->fd_r == -1) {
  error("connection was not accepted\n");
  return 0;
 }
 slave->buffer->fd_w = slave->buffer->fd_r;
 debug("connection established\n");
 return 0;
}

canonical_original_code:
static void *master_wait_for_connection(void *s_ptr)
{
 struct _master_slave_tracker_t *slave;
 int sockfd;
 slave = (struct _master_slave_tracker_t *) s_ptr;
 sockfd = net_socket(SOCK_STREAM, 0, slave->tcp_port, 0x01);
 if (sockfd == -1) {
  error("cannot create master data socket\n");
  return 0;
 }
 debug("Waiting for connection on %i/TCP\n", slave->tcp_port);
 slave->buffer->fd_r = net_accept(sockfd);
 if (slave->buffer->fd_r == -1) {
  error("connection was not accepted\n");
  return 0;
 }
 slave->buffer->fd_w = slave->buffer->fd_r;
 debug("connection established\n");
 return 0;
}

variable_types: struct _master_slave_tracker_t { struct timeval { long tv_sec; long tv_usec; } last_used; unsigned char ticket; char *domain; struct _buffer_t { unsigned int size; unsigned short ackn_r; unsigned short ack_r_counter; unsigned char *data_write; unsigned char *map_write; int fd_w; unsigned short seqn_s; unsigned short ackn_s; unsigned int endbuf; unsigned char *data_read; int fd_r; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *buffer; int tcp_port; } *

variable_types: sockfd int


decompiled_code:
__int64 __fastcall master_handle_data(unsigned __int8 *a1, unsigned __int8 *a2, unsigned int a3)
{
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // r9
__int64 v8; // rdx
unsigned __int8 *v9; // rsi
__int64 v10; // rsi
const char *v11; // rdx
unsigned __int8 v12; // al
unsigned int v13; // ebx
int v14; // eax
_BOOL4 v15; // edi
__int64 v16; // rsi
char v18; // [rsp+Bh] [rbp-202Dh] BYREF
unsigned int v19; // [rsp+Ch] [rbp-202Ch] BYREF
char v20; // [rsp+10h] [rbp-2028h] BYREF
unsigned __int16 v21; // [rsp+11h] [rbp-2027h] BYREF
unsigned __int16 v22; // [rsp+13h] [rbp-2025h] BYREF
char v23[4091]; // [rsp+15h] [rbp-2023h] BYREF
char v24; // [rsp+1010h] [rbp-1028h] BYREF
char v25[4135]; // [rsp+1011h] [rbp-1027h] BYREF

v3 = *a2;
v4 = slaves[v3];
v5 = v3;
if ( v4 )
{
if ( verbose > 0 )
debug(
"received (seq=%.5i ack=%.5i size=%i flag=0x%.2x ticket=%i)\n",
*(unsigned __int16 *)(a2 + 1),
*(unsigned __int16 *)(a2 + 3),
a3,
*a1,
v3);
if ( (*a1 & 1) != 0 )
{
v9 = a2 + 5;
v8 = a3 - 5;
}
else
{
v8 = 0LL;
v9 = 0LL;
}
v10 = (unsigned int)buffer_write(
*(_QWORD *)(v4 + 32),
v9,
v8,
*(unsigned __int16 *)(a2 + 1),
*(unsigned __int16 *)(a2 + 3),
v5);
if ( verbose > 1 )
printf(
"master.c: wrote %i bytes (now: seq=%i ack=%i)\n",
v10,
*(unsigned __int16 *)(*(_QWORD *)(v4 + 32) + 28LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 32) + 30LL));
gettimeofday((struct timeval *)v4, 0LL);
v12 = *a1;
if ( (*a1 & 1) != 0 )
{
v4 = 0LL;
}
else
{
v18 = 0;
if ( (v12 & 8) != 0 )
{
v18 = 4;
v13 = 248;
}
else
{
v13 = 0;
error((int)"sending back BASE64 enc -- not implemented yet\n", 0, v11);
}
v14 = buffer_read(*(_QWORD *)(v4 + 32), v23, v13, &v21, &v22, 0LL);
v15 = (*a1 & 8) == 0;
v18 = (v13 == v14) | v18 & 0xFE;
v20 = *(_BYTE *)(v4 + 16);
codec_encode((unsigned int)(v15 + 1), &v20, (unsigned int)(v14 + 5), v25, &v19, 253LL);
v24 = net_encode_flag(&v18);
v16 = ++v19;
if ( verbose > 0 )
{
debug("sending (seq=%u ack=%u size=%u)\n", v21, v22, (unsigned int)v16);
v16 = v19;
}
v4 = net_craft_dns_rr_answer(&v24, v16, *(_QWORD *)(v4 + 24));
}
}
else
{
debug("received packet for non-existing slave (%u)\n", (unsigned int)v3);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, unsigned __int8 *a2, unsigned int a3)
{
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // r9
__int64 v8; // rdx
unsigned __int8 *v9; // rsi
__int64 v10; // rsi
const char *v11; // rdx
unsigned __int8 v12; // al
unsigned int v13; // ebx
int v14; // eax
_BOOL4 v15; // edi
__int64 v16; // rsi
char v18; // [rsp+Bh] [rbp-202Dh] BYREF
unsigned int v19; // [rsp+Ch] [rbp-202Ch] BYREF
char v20; // [rsp+10h] [rbp-2028h] BYREF
unsigned __int16 v21; // [rsp+11h] [rbp-2027h] BYREF
unsigned __int16 v22; // [rsp+13h] [rbp-2025h] BYREF
char v23[4091]; // [rsp+15h] [rbp-2023h] BYREF
char v24; // [rsp+1010h] [rbp-1028h] BYREF
char v25[4135]; // [rsp+1011h] [rbp-1027h] BYREF

v3 = *a2;
v4 = slaves[v3];
v5 = v3;
if ( v4 )
{
if ( verbose > 0 )
debug(
"received (seq=%.5i ack=%.5i size=%i flag=0x%.2x ticket=%i)\n",
*(unsigned __int16 *)(a2 + 1),
*(unsigned __int16 *)(a2 + 3),
a3,
*a1,
v3);
if ( (*a1 & 1) != 0 )
{
v9 = a2 + 5;
v8 = a3 - 5;
}
else
{
v8 = 0LL;
v9 = 0LL;
}
v10 = (unsigned int)buffer_write(
*(_QWORD *)(v4 + 32),
v9,
v8,
*(unsigned __int16 *)(a2 + 1),
*(unsigned __int16 *)(a2 + 3),
v5);
if ( verbose > 1 )
printf(
"master.c: wrote %i bytes (now: seq=%i ack=%i)\n",
v10,
*(unsigned __int16 *)(*(_QWORD *)(v4 + 32) + 28LL),
*(unsigned __int16 *)(*(_QWORD *)(v4 + 32) + 30LL));
gettimeofday((struct timeval *)v4, 0LL);
v12 = *a1;
if ( (*a1 & 1) != 0 )
{
v4 = 0LL;
}
else
{
v18 = 0;
if ( (v12 & 8) != 0 )
{
v18 = 4;
v13 = 248;
}
else
{
v13 = 0;
error((int)"sending back BASE64 enc -- not implemented yet\n", 0, v11);
}
v14 = buffer_read(*(_QWORD *)(v4 + 32), v23, v13, &v21, &v22, 0LL);
v15 = (*a1 & 8) == 0;
v18 = (v13 == v14) | v18 & 0xFE;
v20 = *(_BYTE *)(v4 + 16);
codec_encode((unsigned int)(v15 + 1), &v20, (unsigned int)(v14 + 5), v25, &v19, 253LL);
v24 = net_encode_flag(&v18);
v16 = ++v19;
if ( verbose > 0 )
{
debug("sending (seq=%u ack=%u size=%u)\n", v21, v22, (unsigned int)v16);
v16 = v19;
}
v4 = net_craft_dns_rr_answer(&v24, v16, *(_QWORD *)(v4 + 24));
}
}
else
{
debug("received packet for non-existing slave (%u)\n", (unsigned int)v3);
}
return v4;
}

original_code:
static struct net_dns_rr_answer_t *
master_handle_data(tunnel_header_slave_flag_t *slave_flag,
      unsigned char *dec_data,
       unsigned int dec_data_length)
{
 tunnel_header_t *slave_hdr;
 tunnel_header_t *master_hdr;
 master_slave_tracker_t *slave;
 uint8 *payload;
 unsigned int payload_length;
 int written_bytes;
 tunnel_header_master_flag_t master_flag;
 unsigned int max_data_length;
 unsigned char master_data[4096];
 unsigned char *master_payload;
 unsigned int master_data_length;
 unsigned char master_enc_data[4096];
 unsigned int master_enc_data_length;
 unsigned int max_out_length;
 slave_hdr = (tunnel_header_t *) dec_data;
 slave = slaves[slave_hdr->ticket];
 if (!slave) {
  debug("received packet for non-existing slave (%u)\n", slave_hdr->ticket);
  return 0;
 }
 if (verbose > 0) {
  debug("received (seq=%.5i ack=%.5i size=%i flag=0x%.2x ticket=%i)\n", slave_hdr->sequence , slave_hdr->last_received, dec_data_length, *(uint8 *)slave_flag, slave_hdr->ticket);
 }
 if (slave_flag->spoofed) {
  payload = dec_data + sizeof(tunnel_header_t);
  payload_length = (unsigned int) (dec_data_length - sizeof(tunnel_header_t));
 } else {
  payload = ((void *)0);
  payload_length = 0;
 }
 written_bytes = buffer_write(
   slave->buffer,
    payload,
     payload_length,
      slave_hdr->sequence,
       slave_hdr->last_received);
 if (verbose > 1) {
  printf("master.c: wrote %i bytes (now: seq=%i ack=%i)\n", written_bytes, slave->buffer->seqn_s, slave->buffer->ackn_s);
 }
    gettimeofday(&(slave->last_used), ((void *)0));
 if (slave_flag->spoofed) {
  return 0;
 }
 memset(&master_flag, 0x00, sizeof(tunnel_header_flag_t));
 max_data_length = 0;
 if (slave_flag->expect_binary) {
  max_data_length = 254
        - sizeof(tunnel_header_t)
         - sizeof(tunnel_header_master_flag_t);
  master_flag.binary_txt = 1;
 } else {
  master_flag.binary_txt = 0;
  error("sending back BASE64 enc -- not implemented yet\n");
 }
 master_hdr = (tunnel_header_t *) master_data;
 master_payload = master_data + sizeof(tunnel_header_t);
 master_data_length = buffer_read(
       slave->buffer,
        master_payload,
         max_data_length,
          &(master_hdr->sequence),
           &(master_hdr->last_received), 0);
 if (master_data_length == max_data_length) {
  master_flag.more_data = 1;
 } else {
  master_flag.more_data = 0;
 }
 master_data_length += sizeof(tunnel_header_t);
 master_hdr->ticket = slave->ticket;
 max_out_length = 254 - 1;
 codec_encode(slave_flag->expect_binary ? 0x1 : 0x2,
       master_data,
        master_data_length,
         master_enc_data + sizeof(tunnel_header_master_flag_t),
          &master_enc_data_length,
           max_out_length);
 *(uint8 *) master_enc_data = net_encode_flag((tunnel_header_flag_t *) &master_flag);
 master_enc_data_length += sizeof(tunnel_header_flag_t);
 if (verbose > 0) {
  debug("sending (seq=%u ack=%u size=%u)\n",master_hdr->sequence, master_hdr->last_received, master_enc_data_length);
 }
 return net_craft_dns_rr_answer(
        master_enc_data,
         master_enc_data_length,
          slave->domain);
}

canonical_original_code:
static struct net_dns_rr_answer_t *master_handle_data(struct _tunnel_header_slave_flag_t *slave_flag, unsigned char *dec_data, unsigned int dec_data_length)
{
 struct _tunnel_header_t *slave_hdr;
 struct _tunnel_header_t *master_hdr;
 struct _master_slave_tracker_t *slave;
 unsigned char *payload;
 unsigned int payload_length;
 int written_bytes;
 struct _tunnel_header_master_flag_t master_flag;
 unsigned int max_data_length;
 unsigned char master_data[4096];
 unsigned char *master_payload;
 unsigned int master_data_length;
 unsigned char master_enc_data[4096];
 unsigned int master_enc_data_length;
 unsigned int max_out_length;
 slave_hdr = (struct _tunnel_header_t *) dec_data;
 slave = slaves[slave_hdr->ticket];
 if (!slave) {
  debug("received packet for non-existing slave (%u)\n", slave_hdr->ticket);
  return 0;
 }
 if (verbose > 0) {
  debug("received (seq=%.5i ack=%.5i size=%i flag=0x%.2x ticket=%i)\n", slave_hdr->sequence , slave_hdr->last_received, dec_data_length, *(unsigned char *)slave_flag, slave_hdr->ticket);
 }
 if (slave_flag->spoofed) {
  payload = dec_data + sizeof(tunnel_header_t);
  payload_length = (unsigned int) (dec_data_length - sizeof(tunnel_header_t));
 } else {
  payload = ((void *)0);
  payload_length = 0;
 }
 written_bytes = buffer_write(
   slave->buffer,
    payload,
     payload_length,
      slave_hdr->sequence,
       slave_hdr->last_received);
 if (verbose > 1) {
  printf("master.c: wrote %i bytes (now: seq=%i ack=%i)\n", written_bytes, slave->buffer->seqn_s, slave->buffer->ackn_s);
 }
    gettimeofday(&(slave->last_used), ((void *)0));
 if (slave_flag->spoofed) {
  return 0;
 }
 memset(&master_flag, 0x00, sizeof(tunnel_header_flag_t));
 max_data_length = 0;
 if (slave_flag->expect_binary) {
  max_data_length = 254
        - sizeof(tunnel_header_t)
         - sizeof(tunnel_header_master_flag_t);
  master_flag.binary_txt = 1;
 } else {
  master_flag.binary_txt = 0;
  error("sending back BASE64 enc -- not implemented yet\n");
 }
 master_hdr = (struct _tunnel_header_t *) master_data;
 master_payload = master_data + sizeof(tunnel_header_t);
 master_data_length = buffer_read(
       slave->buffer,
        master_payload,
         max_data_length,
          &(master_hdr->sequence),
           &(master_hdr->last_received), 0);
 if (master_data_length == max_data_length) {
  master_flag.more_data = 1;
 } else {
  master_flag.more_data = 0;
 }
 master_data_length += sizeof(tunnel_header_t);
 master_hdr->ticket = slave->ticket;
 max_out_length = 254 - 1;
 codec_encode(slave_flag->expect_binary ? 0x1 : 0x2,
       master_data,
        master_data_length,
         master_enc_data + sizeof(tunnel_header_master_flag_t),
          &master_enc_data_length,
           max_out_length);
 *(unsigned char *) master_enc_data = net_encode_flag((struct _tunnel_header_flag_t *) &master_flag);
 master_enc_data_length += sizeof(tunnel_header_flag_t);
 if (verbose > 0) {
  debug("sending (seq=%u ack=%u size=%u)\n",master_hdr->sequence, master_hdr->last_received, master_enc_data_length);
 }
 return net_craft_dns_rr_answer(
        master_enc_data,
         master_enc_data_length,
          slave->domain);
}

variable_types:
payload
unsigned char *

user_defined_types: struct _tunnel_header_master_flag_t { unsigned char more_data; unsigned char hello_reply; unsigned char binary_txt; unsigned char control; unsigned char zero; }

user_defined_types: struct _tunnel_header_slave_flag_t { unsigned char spoofed; unsigned char codec; unsigned char hello; unsigned char expect_binary; unsigned char control; unsigned char zero; }

user_defined_types: struct _tunnel_header_t { unsigned char ticket; unsigned short sequence; unsigned short last_received; }

user_defined_types: struct net_dns_rr_answer_t { unsigned short name; unsigned short type; unsigned short class; unsigned int ttl; unsigned short length; unsigned char data[254]; }


decompiled_code:
__int64 __fastcall master_run(unsigned int a1, const char *a2, unsigned __int16 a3, unsigned int a4)
{
__int64 result; // rax
unsigned __int8 *v8; // r14
const char *v9; // rdx
unsigned int v10; // ebp
__int64 v11; // r15
const char *v12; // rdx
unsigned __int8 *v13; // rax
int v14; // edx
unsigned int v15; // eax
const char *v16; // rdx
_QWORD *v17; // rax
unsigned __int8 *v18; // r10
_QWORD *v19; // r11
__int64 v20; // rax
int v21; // esi
const char *v22; // rdx
int v23; // [rsp+0h] [rbp-1058h]
unsigned __int8 *v24; // [rsp+0h] [rbp-1058h]
_QWORD *v25; // [rsp+0h] [rbp-1058h]
_QWORD *v26; // [rsp+0h] [rbp-1058h]
unsigned int v27; // [rsp+Ch] [rbp-104Ch]
char v28[16]; // [rsp+10h] [rbp-1048h] BYREF
unsigned __int8 v29[4152]; // [rsp+20h] [rbp-1038h] BYREF

if ( a2 && *a2 )
{
debug("Master starting for (%s) listening on %i/UDP\n", a2, a1);
v8 = (unsigned __int8 *)malloc_or_die(255LL);
v10 = net_socket(2LL, 0LL, (unsigned __int16)a1, 1LL);
if ( v10 == -1 )
{
error((int)"cannot create server udp socket\n", 0, v9);
result = 0LL;
}
else
{
memset(slaves, 0, sizeof(slaves));
v11 = net_new_packet();
while ( (unsigned int)net_recv_packet(v10, v11, 0LL, v28) != -1 )
{
v23 = net_dns_get_qname(*(_QWORD *)(v11 + 24), v8, a2);
if ( v23 )
{
v13 = (unsigned __int8 *)net_decode_flag(*v8);
v14 = v23;
v24 = v13;
v15 = codec_decode((*v13 & 2) != 0, v8 + 1, (unsigned int)(v14 - 1), v29);
if ( v15 )
{
v27 = v15;
v17 = (_QWORD *)net_new_packet();
v18 = v24;
v19 = v17;
v17[2] = *(_QWORD *)(v11 + 16);
v17[3] = *(_QWORD *)(v11 + 24);
if ( (*v24 & 4) != 0 )
{
v25 = v17;
v20 = master_handle_hello(v18, v29, v27, a2, a3, a4);
v19 = v25;
v25[4] = v20;
}
else if ( (*v24 & 0x10) != 0 )
{
v20 = v17[4];
}
else
{
v26 = v17;
v20 = master_handle_data(v18, v29, v27);
v19 = v26;
v26[4] = v20;
}
if ( !v20 )
*(_BYTE *)(v19[2] + 3LL) = *(_BYTE *)(v19[2] + 3LL) & 0xF0 | 3;
v21 = (int)v19;
*(_WORD *)(*(_QWORD *)(v11 + 16) + 2LL) |= 0x2080u;
if ( (unsigned int)net_send_packet(v10, v19, v28) == -1 )
{
error((int)"cannot send packet\n", v21, v22);
goto LABEL_17;
}
}
else
{
error((int)"decoding of data failed (%.1x); skip packet.\n", (*v24 & 2) != 0, v16);
}
}
}
error((int)"could not receive packet\n", v11, v12);
LABEL_17:
debug("server error\n");
result = 0xFFFFFFFFLL;
}
}
else
{
debug("You need to specify at least one domain\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, const char *a2, unsigned __int16 a3, unsigned int a4)
{
__int64 result; // rax
unsigned __int8 *v8; // r14
const char *v9; // rdx
unsigned int v10; // ebp
__int64 v11; // r15
const char *v12; // rdx
unsigned __int8 *v13; // rax
int v14; // edx
unsigned int v15; // eax
const char *v16; // rdx
_QWORD *v17; // rax
unsigned __int8 *v18; // r10
_QWORD *v19; // r11
__int64 v20; // rax
int v21; // esi
const char *v22; // rdx
int v23; // [rsp+0h] [rbp-1058h]
unsigned __int8 *v24; // [rsp+0h] [rbp-1058h]
_QWORD *v25; // [rsp+0h] [rbp-1058h]
_QWORD *v26; // [rsp+0h] [rbp-1058h]
unsigned int v27; // [rsp+Ch] [rbp-104Ch]
char v28[16]; // [rsp+10h] [rbp-1048h] BYREF
unsigned __int8 v29[4152]; // [rsp+20h] [rbp-1038h] BYREF

if ( a2 && *a2 )
{
debug("Master starting for (%s) listening on %i/UDP\n", a2, a1);
v8 = (unsigned __int8 *)malloc_or_die(255LL);
v10 = net_socket(2LL, 0LL, (unsigned __int16)a1, 1LL);
if ( v10 == -1 )
{
error((int)"cannot create server udp socket\n", 0, v9);
result = 0LL;
}
else
{
memset(slaves, 0, sizeof(slaves));
v11 = net_new_packet();
while ( (unsigned int)net_recv_packet(v10, v11, 0LL, v28) != -1 )
{
v23 = net_dns_get_qname(*(_QWORD *)(v11 + 24), v8, a2);
if ( v23 )
{
v13 = (unsigned __int8 *)net_decode_flag(*v8);
v14 = v23;
v24 = v13;
v15 = codec_decode((*v13 & 2) != 0, v8 + 1, (unsigned int)(v14 - 1), v29);
if ( v15 )
{
v27 = v15;
v17 = (_QWORD *)net_new_packet();
v18 = v24;
v19 = v17;
v17[2] = *(_QWORD *)(v11 + 16);
v17[3] = *(_QWORD *)(v11 + 24);
if ( (*v24 & 4) != 0 )
{
v25 = v17;
v20 = func1(v18, v29, v27, a2, a3, a4);
v19 = v25;
v25[4] = v20;
}
else if ( (*v24 & 0x10) != 0 )
{
v20 = v17[4];
}
else
{
v26 = v17;
v20 = func3(v18, v29, v27);
v19 = v26;
v26[4] = v20;
}
if ( !v20 )
*(_BYTE *)(v19[2] + 3LL) = *(_BYTE *)(v19[2] + 3LL) & 0xF0 | 3;
v21 = (int)v19;
*(_WORD *)(*(_QWORD *)(v11 + 16) + 2LL) |= 0x2080u;
if ( (unsigned int)net_send_packet(v10, v19, v28) == -1 )
{
error((int)"cannot send packet\n", v21, v22);
goto LABEL_17;
}
}
else
{
error((int)"decoding of data failed (%.1x); skip packet.\n", (*v24 & 2) != 0, v16);
}
}
}
error((int)"could not receive packet\n", v11, v12);
LABEL_17:
debug("server error\n");
result = 0xFFFFFFFFLL;
}
}
else
{
debug("You need to specify at least one domain\n");
result = 0LL;
}
return result;
}

original_code:
int
master_run(int listen_port, char *domain, uint16 port, int do_listen)
{
 int sock, rs;
 char * qname;
 net_dns_header_t *dns;
 struct net_dns_rr_question_t *rr_q;
 net_packet_t *packet, *out_packet;
 struct sockaddr_in slave_addr;
 tunnel_header_slave_flag_t *slave_flag;
 tunnel_header_t *hdr;
 char dec_data[4096];
 unsigned int dec_data_length = 0, enc_data_length = 0, qname_length;
 unsigned int in_buf_size = 0;
 if (!domain || strlen(domain) == 0) {
  debug("You need to specify at least one domain\n");
  return 0;
 }
 debug("Master starting for (%s) listening on %i/UDP\n", domain, listen_port);
 qname = malloc_or_die(255);
 sock = net_socket(SOCK_DGRAM, 0, listen_port, 1);
 if (sock == -1) {
  error("cannot create server udp socket\n");
  return 0;
 }
 memset(slaves, 0x00, sizeof(master_slave_tracker_t *) * 256);
 packet = net_new_packet();
 do {
  rs = net_recv_packet(sock, packet, 0, &slave_addr);
  if (rs == -1) {
   error("could not receive packet\n");
   break;
  }
  dns = packet->dns;
  rr_q = packet->rr_q;
  qname_length = net_dns_get_qname(rr_q, qname, domain);
  if (qname_length == 0) {
   continue;
  }
  slave_flag = (tunnel_header_slave_flag_t *) net_decode_flag(*(uint8 *) qname);
  dec_data_length = codec_decode(slave_flag->codec,
         qname + sizeof(tunnel_header_slave_flag_t),
          qname_length - sizeof(tunnel_header_slave_flag_t), dec_data);
  if (!dec_data_length) {
   error("decoding of data failed (%.1x); skip packet.\n", slave_flag->codec);
   continue;
  }
  hdr = (tunnel_header_t *) dec_data;
  out_packet = net_new_packet();
  out_packet->dns = packet->dns;
  out_packet->rr_q = packet->rr_q;
  if (slave_flag->hello) {
   out_packet->rr_a = master_handle_hello(slave_flag,
            dec_data,
            dec_data_length,
            domain,
            port, do_listen);
  } else {
   if (slave_flag->control) {
   } else {
    out_packet->rr_a = master_handle_data(slave_flag,
            dec_data,
             dec_data_length);
   }
  }
  if (!out_packet->rr_a) {
   (out_packet->dns->rcode = 0x03);
  }
  packet->dns->qr = 1;
  packet->dns->ad = 1;
  if ( (net_send_packet(sock, out_packet, &slave_addr) == -1) ) {
   error("cannot send packet\n");
   break;
  }
 } while (rs != -1);
 debug("server error\n");
 return -1;
}

canonical_original_code:
int master_run(int listen_port, char *domain, unsigned short port, int do_listen)
{
 int sock; int rs;
 char *qname;
 struct _net_dns_header_t *dns;
 struct net_dns_rr_question_t *rr_q;
 struct _net_packet_t *packet; struct _net_packet_t *out_packet;
 struct sockaddr_in slave_addr;
 struct _tunnel_header_slave_flag_t *slave_flag;
 struct _tunnel_header_t *hdr;
 char dec_data[4096];
 unsigned int dec_data_length = 0; unsigned int enc_data_length = 0; unsigned int qname_length;
 unsigned int in_buf_size = 0;
 if (!domain || strlen(domain) == 0) {
  debug("You need to specify at least one domain\n");
  return 0;
 }
 debug("Master starting for (%s) listening on %i/UDP\n", domain, listen_port);
 qname = malloc_or_die(255);
 sock = net_socket(SOCK_DGRAM, 0, listen_port, 1);
 if (sock == -1) {
  error("cannot create server udp socket\n");
  return 0;
 }
 memset(slaves, 0x00, sizeof(struct _master_slave_tracker_t *) * 256);
 packet = net_new_packet();
 do {
  rs = net_recv_packet(sock, packet, 0, &slave_addr);
  if (rs == -1) {
   error("could not receive packet\n");
   break;
  }
  dns = packet->dns;
  rr_q = packet->rr_q;
  qname_length = net_dns_get_qname(rr_q, qname, domain);
  if (qname_length == 0) {
   continue;
  }
  slave_flag = (struct _tunnel_header_slave_flag_t *) net_decode_flag(*(unsigned char *) qname);
  dec_data_length = codec_decode(slave_flag->codec,
         qname + sizeof(tunnel_header_slave_flag_t),
          qname_length - sizeof(tunnel_header_slave_flag_t), dec_data);
  if (!dec_data_length) {
   error("decoding of data failed (%.1x); skip packet.\n", slave_flag->codec);
   continue;
  }
  hdr = (struct _tunnel_header_t *) dec_data;
  out_packet = net_new_packet();
  out_packet->dns = packet->dns;
  out_packet->rr_q = packet->rr_q;
  if (slave_flag->hello) {
   out_packet->rr_a = master_handle_hello(slave_flag,
            dec_data,
            dec_data_length,
            domain,
            port, do_listen);
  } else {
   if (slave_flag->control) {
   } else {
    out_packet->rr_a = master_handle_data(slave_flag,
            dec_data,
             dec_data_length);
   }
  }
  if (!out_packet->rr_a) {
   (out_packet->dns->rcode = 0x03);
  }
  packet->dns->qr = 1;
  packet->dns->ad = 1;
  if ( (net_send_packet(sock, out_packet, &slave_addr) == -1) ) {
   error("cannot send packet\n");
   break;
  }
 } while (rs != -1);
 debug("server error\n");
 return -1;
}

variable_types: sock int

variable_types: rs int

variable_types:
qname
char *

variable_types: struct _net_packet_t { struct _net_ip_header_t { unsigned char hdr_len; unsigned char version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short flags_offset; unsigned char ttl; unsigned char proto; unsigned short cksum; unsigned int src_ip; unsigned int dst_ip; } *ip; struct _net_udp_header_t { unsigned short src_port; unsigned short dst_port; unsigned short length; unsigned short cksum; } *udp; struct _net_dns_header_t { unsigned short id; unsigned char rd; unsigned char tc; unsigned char aa; unsigned char opcode; unsigned char qr; unsigned char rcode; unsigned char cd; unsigned char ad; unsigned char z; unsigned char ra; unsigned short question_num; unsigned short answer_num; unsigned short authority_num; unsigned short additional_num; } *dns; struct net_dns_rr_question_t { unsigned int qname_length; char qname[255]; unsigned short type; unsigned short class; } *rr_q; struct net_dns_rr_answer_t { unsigned short name; unsigned short type; unsigned short class; unsigned int ttl; unsigned short length; unsigned char data[254]; } *rr_a; } *

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

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

user_defined_types: struct _net_packet_t { struct _net_ip_header_t *ip; struct _net_udp_header_t *udp; struct _net_dns_header_t *dns; struct net_dns_rr_question_t *rr_q; struct net_dns_rr_answer_t *rr_a; }


original_code:
unsigned int
codec_base32_encode(unsigned char *input, unsigned int in_len,
       unsigned char *output, unsigned int max_out_len)
{
 unsigned char *p = 0;
    unsigned int i = 0, j = 0, mod = 0;
 unsigned int len = 0;
 len = max_out_len * 5 / 8;
 if (in_len < len) {
  len = in_len;
 }
    p = output;
    mod = len % 5;
    i = 0;
    while (i < len - mod) {
        *p++ = b32table[input[i] >> 3];
        *p++ = b32table[(input[i] << 2 | input[i + 1] >> 6) & 0x1f];
        *p++ = b32table[(input[i + 1] >> 1) & 0x1f];
        *p++ = b32table[(input[i + 1] << 4 | input[i + 2] >> 4) & 0x1f];
        *p++ = b32table[(input[i + 2] << 1 | input[i + 3] >> 7) & 0x1f];
        *p++ = b32table[(input[i + 3] >> 2) & 0x1f];
        *p++ = b32table[(input[i + 3] << 3 | input[i + 4] >> 5) & 0x1f];
        *p++ = b32table[input[i + 4] & 0x1f];
        i += 5;
    }
    if (mod == 0) {
        *p = 0;
        return len;
    }
    *p++ = b32table[input[i] >> 3];
    if (mod == 1) {
       *p++ = b32table[(input[i] << 2) & 0x1f];
   for (j=0;j<6;j++) {
   *p++ = 55;
  }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i] << 2 | input[i+1] >> 6) & 0x1f];
    *p++ = b32table[(input[i+1] >> 1) & 0x1f];
    if (mod == 2) {
        *p++ = b32table[(input[i+1] << 4) & 0x1f];
        for (j=0;j<4;j++) {
   *p++ = 55;
        }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i+1] << 4 | input[i+2] >> 4) & 0x1f];
    if (mod == 3) {
        *p++ = b32table[(input[i+2] << 1) & 0x1f];
        for (j=0;j<3;j++) {
   *p++ = 55;
  }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i + 2] << 1 | input[i + 3] >> 7) & 0x1f];
    *p++ = b32table[(input[i + 3] >> 2) & 0x1f];
    *p++ = b32table[(input[i + 3] << 3) & 0x1f];
    *p++ = 55;
    *p = 0;
    return len;
}

canonical_original_code:
unsigned int codec_base32_encode(unsigned char *input, unsigned int in_len, unsigned char *output, unsigned int max_out_len)
{
 unsigned char *p = 0;
    unsigned int i = 0; unsigned int j = 0; unsigned int mod = 0;
 unsigned int len = 0;
 len = max_out_len * 5 / 8;
 if (in_len < len) {
  len = in_len;
 }
    p = output;
    mod = len % 5;
    i = 0;
    while (i < len - mod) {
        *p++ = b32table[input[i] >> 3];
        *p++ = b32table[(input[i] << 2 | input[i + 1] >> 6) & 0x1f];
        *p++ = b32table[(input[i + 1] >> 1) & 0x1f];
        *p++ = b32table[(input[i + 1] << 4 | input[i + 2] >> 4) & 0x1f];
        *p++ = b32table[(input[i + 2] << 1 | input[i + 3] >> 7) & 0x1f];
        *p++ = b32table[(input[i + 3] >> 2) & 0x1f];
        *p++ = b32table[(input[i + 3] << 3 | input[i + 4] >> 5) & 0x1f];
        *p++ = b32table[input[i + 4] & 0x1f];
        i += 5;
    }
    if (mod == 0) {
        *p = 0;
        return len;
    }
    *p++ = b32table[input[i] >> 3];
    if (mod == 1) {
       *p++ = b32table[(input[i] << 2) & 0x1f];
   for (j=0;j<6;j++) {
   *p++ = 55;
  }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i] << 2 | input[i+1] >> 6) & 0x1f];
    *p++ = b32table[(input[i+1] >> 1) & 0x1f];
    if (mod == 2) {
        *p++ = b32table[(input[i+1] << 4) & 0x1f];
        for (j=0;j<4;j++) {
   *p++ = 55;
        }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i+1] << 4 | input[i+2] >> 4) & 0x1f];
    if (mod == 3) {
        *p++ = b32table[(input[i+2] << 1) & 0x1f];
        for (j=0;j<3;j++) {
   *p++ = 55;
  }
        *p = 0;
        return len;
    }
    *p++ = b32table[(input[i + 2] << 1 | input[i + 3] >> 7) & 0x1f];
    *p++ = b32table[(input[i + 3] >> 2) & 0x1f];
    *p++ = b32table[(input[i + 3] << 3) & 0x1f];
    *p++ = 55;
    *p = 0;
    return len;
}

variable_types:
input
unsigned char *

variable_types:
output
unsigned char *

variable_types:
p
unsigned char *

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
mod
unsigned int

variable_types:
len
unsigned int


original_code:
unsigned int
codec_base64_encode(unsigned char *input, unsigned int in_len,
       unsigned char *output, unsigned int max_out_len)
{
 unsigned char fragment = 0;
 unsigned int len, consumed;
 len = max_out_len * 6 / 8;
 if (in_len < len) {
  len = in_len;
 }
 consumed = len;
 for (; len >= 3; len -= 3) {
  *output++ = base64_charset[input[0] >> 2];
  *output++ = base64_charset[((input[0] << 4) & 0x30) | (input[1] >> 4)];
  *output++ = base64_charset[((input[1] << 2) & 0x3c) | (input[2] >> 6)];
  *output++ = base64_charset[input[2] & 0x3f];
  input += 3;
    }
    if (len > 0) {
  *output++ = base64_charset[input[0] >> 2];
  fragment = (input[0] << 4) & 0x30;
  if (len > 1) {
      fragment |= input[1] >> 4;
  }
  *output++ = base64_charset[fragment];
  *output++ = (len < 2) ? '=' : base64_charset[(input[1] << 2) & 0x3c];
  *output++ = '=';
    }
    *output = '\0';
 return consumed;
}

canonical_original_code:
unsigned int codec_base64_encode(unsigned char *input, unsigned int in_len, unsigned char *output, unsigned int max_out_len)
{
 unsigned char fragment = 0;
 unsigned int len; unsigned int consumed;
 len = max_out_len * 6 / 8;
 if (in_len < len) {
  len = in_len;
 }
 consumed = len;
 for (; len >= 3; len -= 3) {
  *output++ = base64_charset[input[0] >> 2];
  *output++ = base64_charset[((input[0] << 4) & 0x30) | (input[1] >> 4)];
  *output++ = base64_charset[((input[1] << 2) & 0x3c) | (input[2] >> 6)];
  *output++ = base64_charset[input[2] & 0x3f];
  input += 3;
    }
    if (len > 0) {
  *output++ = base64_charset[input[0] >> 2];
  fragment = (input[0] << 4) & 0x30;
  if (len > 1) {
      fragment |= input[1] >> 4;
  }
  *output++ = base64_charset[fragment];
  *output++ = (len < 2) ? '=' : base64_charset[(input[1] << 2) & 0x3c];
  *output++ = '=';
    }
    *output = '\0';
 return consumed;
}

variable_types:
fragment
unsigned char

variable_types:
consumed
unsigned int


original_code:
unsigned int
codec_base32_decode(unsigned char *input, unsigned char *output)
{
    unsigned int i = 0, j = 0;
    unsigned char c = 0;
    char dbytes[5], ebytes[8];
    unsigned int len = 0;
    for (i = 0; i < strlen(input); i += 8) {
     for (j = 0; j < 8; j++) {
         c = (unsigned char) input[i + j];
         ebytes[j] = ((c == base32_pad_char) ? 0 : base32_dmap[c]);
     }
     dbytes[0] = ((ebytes[0] << 3) & 0xf8) | ((ebytes[1] >> 2) & 0x07);
     dbytes[1] = ((ebytes[1] & 0x03) << 6)
      | ((ebytes[2] & 0x1f) << 1) | ((ebytes[3] >> 4) & 1);
     dbytes[2] = ((ebytes[3] & 0x0f) << 4) | ((ebytes[4] >> 1) & 0x0f);
     dbytes[3] = ((ebytes[4] & 1) << 7)
       | ((ebytes[5] & 0x1f) << 2) | ((ebytes[6] >> 3) & 0x03);
     dbytes[4] = ((ebytes[6] & 0x07) << 5) | (ebytes[7] & 0x1f);
     for (j = 0; j < sizeof(dbytes); j++) {
         *output++ = (unsigned char) dbytes[j];
         len++;
     }
    }
    return len;
}

canonical_original_code:
unsigned int codec_base32_decode(unsigned char *input, unsigned char *output)
{
    unsigned int i = 0; unsigned int j = 0;
    unsigned char c = 0;
    char dbytes[5]; char ebytes[8];
    unsigned int len = 0;
    for (i = 0; i < strlen(input); i += 8) {
     for (j = 0; j < 8; j++) {
         c = (unsigned char) input[i + j];
         ebytes[j] = ((c == base32_pad_char) ? 0 : base32_dmap[c]);
     }
     dbytes[0] = ((ebytes[0] << 3) & 0xf8) | ((ebytes[1] >> 2) & 0x07);
     dbytes[1] = ((ebytes[1] & 0x03) << 6)
      | ((ebytes[2] & 0x1f) << 1) | ((ebytes[3] >> 4) & 1);
     dbytes[2] = ((ebytes[3] & 0x0f) << 4) | ((ebytes[4] >> 1) & 0x0f);
     dbytes[3] = ((ebytes[4] & 1) << 7)
       | ((ebytes[5] & 0x1f) << 2) | ((ebytes[6] >> 3) & 0x03);
     dbytes[4] = ((ebytes[6] & 0x07) << 5) | (ebytes[7] & 0x1f);
     for (j = 0; j < sizeof(dbytes); j++) {
         *output++ = (unsigned char) dbytes[j];
         len++;
     }
    }
    return len;
}

variable_types:
c
unsigned char

variable_types:
dbytes
char [5]

variable_types:
ebytes
char [8]


original_code:
unsigned int
codec_base64_decode(unsigned char *input, unsigned char *output)
{
   unsigned int chunk = 0, chars = 0, len = 0;
   unsigned char *org = 0;
   org = output;
   chunk = 0;
   chars = 0;
   while (*input) {
       if (*input == base64_pad_char) {
           break;
       }
    chars++;
       if (*input > 0 && base64_dmap[*input] != -1) {
           chunk |= base64_dmap[*input];
           if (chars == 4) {
               *output++ = chunk >> 16;
               *output++ = (chunk >> 8) & 0x00ff;
      *output++ = chunk & 0x00ff;
      chunk = 0;
               chars = 0;
      len += 3;
           } else {
               chunk <<= 6;
           }
       }
       input++;
   }
   switch(chars) {
       case 2:
           *output++ = chunk >> 10;
     len++;
           break;
       case 3:
           *output++ = chunk >> 16;
           *output++ = (chunk >> 8) & 0x0ff;
     len += 2;
           break;
   }
   return len;
}

canonical_original_code:
unsigned int codec_base64_decode(unsigned char *input, unsigned char *output)
{
   unsigned int chunk = 0; unsigned int chars = 0; unsigned int len = 0;
   unsigned char *org = 0;
   org = output;
   chunk = 0;
   chars = 0;
   while (*input) {
       if (*input == base64_pad_char) {
           break;
       }
    chars++;
       if (*input > 0 && base64_dmap[*input] != -1) {
           chunk |= base64_dmap[*input];
           if (chars == 4) {
               *output++ = chunk >> 16;
               *output++ = (chunk >> 8) & 0x00ff;
      *output++ = chunk & 0x00ff;
      chunk = 0;
               chars = 0;
      len += 3;
           } else {
               chunk <<= 6;
           }
       }
       input++;
   }
   switch(chars) {
       case 2:
           *output++ = chunk >> 10;
     len++;
           break;
       case 3:
           *output++ = chunk >> 16;
           *output++ = (chunk >> 8) & 0x0ff;
     len += 2;
           break;
   }
   return len;
}

variable_types:
chunk
unsigned int

variable_types:
chars
unsigned int

variable_types:
org
unsigned char *


decompiled_code:
__int64 __fastcall codec_binary_encode(void *src, size_t n, void *dest, unsigned int *a4, unsigned int a5)
{
__int64 result; // rax

if ( a5 <= (unsigned int)n )
{
memcpy(dest, src, a5);
*a4 = a5;
result = a5;
}
else
{
memcpy(dest, src, (unsigned int)n);
*a4 = n;
result = (unsigned int)n;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *src, size_t n, void *dest, unsigned int *a4, unsigned int a5)
{
__int64 result; // rax

if ( a5 <= (unsigned int)n )
{
memcpy(dest, src, a5);
*a4 = a5;
result = a5;
}
else
{
memcpy(dest, src, (unsigned int)n);
*a4 = n;
result = (unsigned int)n;
}
return result;
}

original_code:
unsigned int
codec_binary_encode(unsigned char *input, unsigned int in_len,
       unsigned char *output, unsigned int *out_len,
             unsigned int max_out_len)
{
 if (max_out_len > in_len) {
  memcpy(output, input, in_len);
  *out_len = in_len;
  return in_len;
 } else {
  memcpy(output, input, max_out_len);
  *out_len = max_out_len;
  return max_out_len;
 }
}


decompiled_code:
__int64 __fastcall codec_encode(unsigned __int8 a1, void *a2, const char *a3, _BYTE *a4, unsigned int *a5, unsigned int a6)
{
__int64 result; // rax
unsigned int v9; // er13
unsigned int v10; // er13

if ( a1 == 1 )
return codec_binary_encode(a2, (unsigned int)a3, a4, a5, a6);
if ( a1 == 2 )
{
v10 = codec_base64_encode((unsigned __int8 *)a2, (unsigned int)a3, a4, a6);
*a5 = strlen(a4);
result = v10;
}
else if ( a1 )
{
error((int)"unknown codec: %.2x\n", a1, a3);
result = 0LL;
}
else
{
v9 = codec_base32_encode((unsigned __int8 *)a2, (unsigned int)a3, (__int64)a4, a6);
*a5 = strlen(a4);
result = v9;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 a1, void *a2, const char *a3, _BYTE *a4, unsigned int *a5, unsigned int a6)
{
__int64 result; // rax
unsigned int v9; // er13
unsigned int v10; // er13

if ( a1 == 1 )
return func4(a2, (unsigned int)a3, a4, a5, a6);
if ( a1 == 2 )
{
v10 = func1((unsigned __int8 *)a2, (unsigned int)a3, a4, a6);
*a5 = strlen(a4);
result = v10;
}
else if ( a1 )
{
error((int)"unknown codec: %.2x\n", a1, a3);
result = 0LL;
}
else
{
v9 = func0((unsigned __int8 *)a2, (unsigned int)a3, (__int64)a4, a6);
*a5 = strlen(a4);
result = v9;
}
return result;
}

original_code:
unsigned int
codec_encode(unsigned char codec, unsigned char *input,
     unsigned int in_len, unsigned char *output,
       unsigned int *out_len, unsigned int max_out_len)
{
 unsigned int consumed = 0;
 switch(codec) {
  case 0x0:
    consumed = codec_base32_encode(input, in_len,
              output, max_out_len);
    *out_len = strlen(output);
    break;
  case 0x2:
    consumed = codec_base64_encode(input, in_len,
              output, max_out_len);
    *out_len = strlen(output);
    break;
  case 0x1:
    consumed = codec_binary_encode(input, in_len,
            output, out_len, max_out_len);
    break;
  default:
   error("unknown codec: %.2x\n", codec);
 }
 return consumed;
}

variable_types:
codec
unsigned char


decompiled_code:
__int64 __fastcall codec_binary_decode(void *src, size_t n, void *dest)
{
memcpy(dest, src, (unsigned int)n);
return (unsigned int)n;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *src, size_t n, void *dest)
{
memcpy(dest, src, (unsigned int)n);
return (unsigned int)n;
}

original_code:
unsigned int
codec_binary_decode(unsigned char *input, unsigned int in_len,
             unsigned char *output)
{
 memcpy(output, input, in_len);
 return in_len;
}


decompiled_code:
__int64 __fastcall codec_decode(unsigned __int8 a1, void *a2, const char *a3, _BYTE *a4)
{
switch ( a1 )
{
case 1u:
return codec_binary_decode(a2, (unsigned int)a3, a4);
case 2u:
return codec_base64_decode((unsigned __int8 *)a2, a4);
case 0u:
return codec_base32_decode((char *)a2, a4);
}
error((int)"unknown codec: %.2x\n", a1, a3, a4, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1, void *a2, const char *a3, _BYTE *a4)
{
switch ( a1 )
{
case 1u:
return func6(a2, (unsigned int)a3, a4);
case 2u:
return func3((unsigned __int8 *)a2, a4);
case 0u:
return func2((char *)a2, a4);
}
error((int)"unknown codec: %.2x\n", a1, a3, a4, a2);
return 0LL;
}

original_code:
unsigned int
codec_decode(unsigned char codec, unsigned char *input,
        unsigned int in_len, unsigned char *output)
{
 unsigned int ret = 0;
 switch(codec) {
  case 0x0:
    ret = codec_base32_decode(input, output);
    break;
  case 0x2:
    ret = codec_base64_decode(input, output);
    break;
  case 0x1:
    ret = codec_binary_decode(input, in_len, output);
    break;
  default:
   error("unknown codec: %.2x\n", codec);
 }
 return ret;
}

variable_types:
ret
unsigned int


decompiled_code:
__int64 __fastcall codec_required_space(int errnum, unsigned int a2, const char *a3)
{
__int64 result; // rax

result = a2;
if ( errnum != 1 )
{
if ( errnum == 2 )
{
result = (unsigned int)((int)(6 * a2) / 8);
}
else if ( errnum )
{
error((int)"unknown codec: %.2x\n", errnum, a3);
result = 0LL;
}
else
{
result = (unsigned int)((int)(5 * a2) / 8);
}
}
return result;
}

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

result = a2;
if ( errnum != 1 )
{
if ( errnum == 2 )
{
result = (unsigned int)((int)(6 * a2) / 8);
}
else if ( errnum )
{
error((int)"unknown codec: %.2x\n", errnum, a3);
result = 0LL;
}
else
{
result = (unsigned int)((int)(5 * a2) / 8);
}
}
return result;
}

original_code:
unsigned int
codec_required_space(int codec, int length)
{
 int ret = 0;
 switch (codec) {
  case 0x0:
    ret = (unsigned int)((length * 5) / 8);
    break;
  case 0x1:
    ret = length;
    break;
  case 0x2:
    ret = (unsigned int)((length * 6) / 8);
    break;
  default:
    error("unknown codec: %.2x\n", codec);
 }
 return ret;
}

variable_types: codec int

variable_types: length int

variable_types: ret int


repo: kyuba/curie

decompiled_code:
void __noreturn term_signal()
{
cexit(0LL);
JUMPOUT(0x1BLL);
}

canonical_decompiled_code:
void __noreturn func0()
{
cexit(0LL);
JUMPOUT(0x1BLL);
}

original_code:
static enum signal_callback_result term_signal (enum signal signal, void *aux)
{
    cexit (0);
    return scr_keep;
}

canonical_original_code:
static enum signal_callback_result term_signal(enum signal signal, void *aux)
{
    cexit (0);
    return scr_keep;
}

variable_types: enum signal { sig_unused=0, sig_hup=1, sig_int=2, sig_quit=3, sig_ill=4, sig_abrt=5, sig_fpe=6, sig_kill=7, sig_segv=8, sig_pipe=9, sig_alrm=10, sig_term=11, sig_chld=12, sig_cont=13, sig_stop=14, sig_tstp=15, sig_ttin=16, sig_ttou=17, sig_usr1=18, sig_usr2=19, sig_bus=20, sig_poll=21, sig_prof=22, sig_sys=23, sig_trap=24, sig_urg=25, sig_vtalrm=26, sig_xcpu=27, sig_xfsz=28, sig_iot=29, sig_emt=30, sig_stkflt=31, sig_io=32, sig_pwr=33, sig_info=34, sig_lost=35, sig_winch=36 }

variable_types:
aux
void *

return_type: enum signal_callback_result { scr_keep=0, scr_ditch=1 }


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

result = *(_QWORD *)(a1 + 16);
if ( result >= 0 )
{
*(_QWORD *)(a1 + 16) = --result;
if ( !result )
{
*a2 = *(_QWORD *)(a1 + 56);
result = free_pool_mem();
}
}
return result;
}

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

result = *(_QWORD *)(a1 + 16);
if ( result >= 0 )
{
*(_QWORD *)(a1 + 16) = --result;
if ( !result )
{
*a2 = *(_QWORD *)(a1 + 56);
result = free_pool_mem();
}
}
return result;
}

original_code:
static void event_repeat (struct event *e, struct event **p)
{
    if (e->repeat < 0)
    {
        return;
    }
    e->repeat--;
    if (e->repeat == 0)
    {
        *p = e->next;
        free_pool_mem (e);
    }
}

canonical_original_code:
static void event_repeat(struct event *e, struct event **p)
{
    if (e->repeat < 0)
    {
        return;
    }
    e->repeat--;
    if (e->repeat == 0)
    {
        *p = e->next;
        free_pool_mem (e);
    }
}

variable_types: struct event { struct datetime { unsigned long long date; unsigned int time; } queue_time; long long repeat; enum event_model { em_seconds=0 } model; union { unsigned long long seconds; } model_data; unsigned long output; unsigned long then; struct event *next; } *

variable_types:
p
struct event **

user_defined_types: struct event { struct datetime queue_time; long long repeat; enum event_model model; union { unsigned long long seconds; } model_data; unsigned long output; unsigned long then; struct event *next; }