RegWrite

decompiled_code:
int __fastcall RegWrite(__int64 a1, unsigned int a2, int a3, int a4)
{
__int64 v4; // rax
__int64 v7; // rax
int v8; // eax

v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 == -1LL )
{
LODWORD(v7) = printf("RegWrite failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
LODWORD(v7) = printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
}
else
{
if ( a3 != -1 )
{
v8 = RegRead(a1, a2);
a4 = v8 ^ a3 & (v8 ^ a4);
v4 = *(_QWORD *)a1;
}
v7 = *(unsigned int *)(a1 + 8) + v4;
*(_DWORD *)(v7 + a2) = a4;
}
return v7;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2, int a3, int a4)
{
__int64 v4; // rax
__int64 v7; // rax
int v8; // eax

v4 = *(_QWORD *)a1;
if ( *(_QWORD *)a1 == -1LL )
{
LODWORD(v7) = printf("RegWrite failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
LODWORD(v7) = printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
}
else
{
if ( a3 != -1 )
{
v8 = func0(a1, a2);
a4 = v8 ^ a3 & (v8 ^ a4);
v4 = *(_QWORD *)a1;
}
v7 = *(unsigned int *)(a1 + 8) + v4;
*(_DWORD *)(v7 + a2) = a4;
}
return v7;
}

original_code:
void RegWrite(raw_memory *base, unsigned int address, unsigned int mask, unsigned int value) {
 if (base->accessor == ((void *) -1)) {
  printf("RegWrite failed due to an unmapped base (%s)!\n", base->name);
 } else if (address >= base->length) {
  printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
 } else {
  if (mask != ((0x7fffffff * 2U + 1U))) {
   unsigned int read = RegRead(base, address);
   value &= mask;
   value |= (~mask) & read;
  }
  *((unsigned int *)(base->accessor + base->offset + address)) = value;
 }
}

canonical_original_code:
void RegWrite(struct raw_memory *base, unsigned int address, unsigned int mask, unsigned int value) {
 if (base->accessor == ((void *) -1)) {
  printf("RegWrite failed due to an unmapped base (%s)!\n", base->name);
 } else if (address >= base->length) {
  printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
 } else {
  if (mask != ((0x7fffffff * 2U + 1U))) {
   unsigned int read = RegRead(base, address);
   value &= mask;
   value |= (~mask) & read;
  }
  *((unsigned int *)(base->accessor + base->offset + address)) = value;
 }
}

variable_types:
read
unsigned int

user_defined_types: struct raw_memory { void *accessor; unsigned int offset; unsigned int length; char name[-1]; }


UnmapMemory

repo: raspberry-alpha-omega/ledborg-source

decompiled_code:
int __fastcall UnmapMemory(__int64 a1)
{
void *v2; // rdi
int result; // eax

v2 = *(void **)a1;
if ( v2 != (void *)-1LL )
{
result = munmap(v2, (unsigned int)(*(_DWORD *)(a1 + 8) + *(_DWORD *)(a1 + 12)));
*(_QWORD *)a1 = -1LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
void *v2; // rdi
int result; // eax

v2 = *(void **)a1;
if ( v2 != (void *)-1LL )
{
result = munmap(v2, (unsigned int)(*(_DWORD *)(a1 + 8) + *(_DWORD *)(a1 + 12)));
*(_QWORD *)a1 = -1LL;
}
return result;
}

original_code:
void UnmapMemory(raw_memory *base) {
 if (base->accessor != ((void *) -1)) {
  munmap(base->accessor, base->offset + base->length);
  base->accessor = ((void *) -1);
 } else {
 }
}

canonical_original_code:
void UnmapMemory(struct raw_memory *base) {
 if (base->accessor != ((void *) -1)) {
  munmap(base->accessor, base->offset + base->length);
  base->accessor = ((void *) -1);
 } else {
 }
}

variable_types: struct raw_memory { void *accessor; unsigned int offset; unsigned int length; char name[-1]; } *

return_type: void


shutdownGPIO

decompiled_code:
int shutdownGPIO()
{
int result; // eax

if ( rawMemGPIO != -1 )
result = UnmapMemory((__int64)&rawMemGPIO);
if ( memFile )
{
result = close(memFile);
memFile = 0;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax

if ( rawMemGPIO != -1 )
result = func2((__int64)&rawMemGPIO);
if ( memFile )
{
result = close(memFile);
memFile = 0;
}
return result;
}

original_code:
void shutdownGPIO(void) {
 if (rawMemGPIO.accessor != ((void *) -1)) {
  UnmapMemory(&rawMemGPIO);
 }
 if (memFile != ((int)0)) {
  close(memFile);
  memFile = ((int)0);
 }
}

canonical_original_code:
void shutdownGPIO() {
 if (rawMemGPIO.accessor != ((void *) -1)) {
  UnmapMemory(&rawMemGPIO);
 }
 if (memFile != ((int)0)) {
  close(memFile);
  memFile = ((int)0);
 }
}


MapMemory

decompiled_code:
__int64 __fastcall MapMemory(__int64 a1, int a2, unsigned int a3, __int64 a4)
{
unsigned int v6; // eax
__off_t v7; // r9
void *v8; // rax
unsigned int v9; // er8

if ( *(_QWORD *)a1 != -1LL )
UnmapMemory(a1);
v6 = getpagesize();
*(_DWORD *)(a1 + 12) = a4;
v7 = a3 / v6 * v6;
*(_DWORD *)(a1 + 8) = v7;
v8 = mmap(0LL, a3 % v6 + a4, 3, 1, a2, v7);
v9 = 0;
*(_QWORD *)a1 = v8;
if ( v8 != (void *)-1LL )
{
v9 = 1;
*(_DWORD *)(a1 + 8) = a3 - *(_DWORD *)(a1 + 8);
}
return v9;
}

original_code:
BOOL MapMemory(raw_memory *base, int device, unsigned int startAddress, size_t size) {
 if (base->accessor != ((void *) -1)) {
  UnmapMemory(base);
 }
 base->offset = (unsigned int)(startAddress / getpagesize()) * getpagesize();
 base->length = (unsigned int)size;
 size += (size_t)(startAddress - base->offset);
 base->accessor = mmap(((void *)0), size, 0x1|0x2, 0x01, device, base->offset);
 if (base->accessor == ((void *) -1)) {
  return (0);
 } else {
  base->offset = startAddress - base->offset;
  return (!(0));
 }
}

canonical_original_code:
unsigned char MapMemory(struct raw_memory *base, int device, unsigned int startAddress, unsigned long size) {
 if (base->accessor != ((void *) -1)) {
  UnmapMemory(base);
 }
 base->offset = (unsigned int)(startAddress / getpagesize()) * getpagesize();
 base->length = (unsigned int)size;
 size += (unsigned long)(startAddress - base->offset);
 base->accessor = mmap(((void *)0), size, 0x1|0x2, 0x01, device, base->offset);
 if (base->accessor == ((void *) -1)) {
  return (0);
 } else {
  base->offset = startAddress - base->offset;
  return (!(0));
 }
}

variable_types: device int

variable_types:
startAddress
unsigned int

variable_types:
size
unsigned long

return_type: unsigned char


initialiseGPIO

decompiled_code:
__int64 initialiseGPIO()
{
int v0; // esi
unsigned int v1; // er12
__int64 result; // rax

v0 = memFile;
if ( memFile || (memFile = open("/dev/mem", 1052674), (v0 = memFile) != 0) )
{
v1 = MapMemory((__int64)&rawMemGPIO, v0, 0x20200000u, 180LL);
if ( (_BYTE)v1 )
{
result = 1LL;
}
else
{
puts("Failed to map GPIO device memory (are we root?) !");
result = v1;
}
}
else
{
puts("Failed to open /dev/mem (are we root?) !");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // esi
unsigned int v1; // er12
__int64 result; // rax

v0 = memFile;
if ( memFile || (memFile = open("/dev/mem", 1052674), (v0 = memFile) != 0) )
{
v1 = func4((__int64)&rawMemGPIO, v0, 0x20200000u, 180LL);
if ( (_BYTE)v1 )
{
result = 1LL;
}
else
{
puts("Failed to map GPIO device memory (are we root?) !");
result = v1;
}
}
else
{
puts("Failed to open /dev/mem (are we root?) !");
result = 0LL;
}
return result;
}

original_code:
BOOL initialiseGPIO(void)
{
 if (memFile == ((int)0)) memFile = open("/dev/mem", 02 | 04010000);
 if (memFile == ((int)0)) {
  printf("Failed to open /dev/mem (are we root?) !\n");
  return (0);
 }
 if (!MapMemory(&rawMemGPIO, memFile, (0x20200000), (180))) {
  printf("Failed to map GPIO device memory (are we root?) !\n");
  return (0);
 }
 return (!(0));
}

canonical_original_code:
unsigned char initialiseGPIO()
{
 if (memFile == ((int)0)) memFile = open("/dev/mem", 02 | 04010000);
 if (memFile == ((int)0)) {
  printf("Failed to open /dev/mem (are we root?) !\n");
  return (0);
 }
 if (!MapMemory(&rawMemGPIO, memFile, (0x20200000), (180))) {
  printf("Failed to map GPIO device memory (are we root?) !\n");
  return (0);
 }
 return (!(0));
}


main

canonical_name: main

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

if ( !(unsigned __int8)initialiseGPIO(argc, argv, envp) )
{
shutdownGPIO();
return 1;
}
RegWrite((__int64)&rawMemGPIO, 4u, 14680064, 0x200000);
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x20000);
RegWrite((__int64)&rawMemGPIO, 8u, 14680064, 0x200000);
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x8000000);
RegWrite((__int64)&rawMemGPIO, 8u, 448, 64);
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x400000);
if ( argc <= 1 )
{
puts("No values provided!");
goto LABEL_15;
}
v4 = argv[1];
if ( strlen(v4) <= 2 )
{
puts("Not enough values provided!");
goto LABEL_15;
}
v5 = (unsigned int)*v4;
if ( (unsigned __int8)(*v4 - 49) <= 1u )
{
v6 = 0;
RegWrite((__int64)&rawMemGPIO, 0x1Cu, -1, 0x20000);
}
else if ( *v4 == 48 )
{
v6 = 0;
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x20000);
}
else
{
v6 = 1;
printf("Unusable red value '%c'\n", v5);
}
if ( (unsigned __int8)(argv[1][1] - 49) <= 1u )
{
RegWrite((__int64)&rawMemGPIO, 0x1Cu, -1, 0x8000000);
}
else if ( argv[1][1] == 48 )
{
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x8000000);
}
else
{
v6 = 1;
printf("Unusable blue value '%c'\n", (unsigned int)argv[1][1]);
}
if ( (unsigned __int8)(argv[1][2] - 49) <= 1u )
{
RegWrite((__int64)&rawMemGPIO, 0x1Cu, -1, 0x400000);
goto LABEL_21;
}
if ( argv[1][2] == 48 )
{
RegWrite((__int64)&rawMemGPIO, 0x28u, -1, 0x400000);
LABEL_21:
if ( !v6 )
goto LABEL_16;
goto LABEL_15;
}
printf("Unusable green value '%c'\n", (unsigned int)argv[1][2]);
LABEL_15:
printf("Usage: %s RGB\n Where R is red (0-2), G is green (0-2) and B is blue (0-2).\n", *argv);
LABEL_16:
shutdownGPIO();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rbp
__int64 v5; // rsi
char v6; // bp

if ( !(unsigned __int8)func5(argc, argv, envp) )
{
func3();
return 1;
}
func1((__int64)&rawMemGPIO, 4u, 14680064, 0x200000);
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x20000);
func1((__int64)&rawMemGPIO, 8u, 14680064, 0x200000);
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x8000000);
func1((__int64)&rawMemGPIO, 8u, 448, 64);
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x400000);
if ( argc <= 1 )
{
puts("No values provided!");
goto LABEL_15;
}
v4 = argv[1];
if ( strlen(v4) <= 2 )
{
puts("Not enough values provided!");
goto LABEL_15;
}
v5 = (unsigned int)*v4;
if ( (unsigned __int8)(*v4 - 49) <= 1u )
{
v6 = 0;
func1((__int64)&rawMemGPIO, 0x1Cu, -1, 0x20000);
}
else if ( *v4 == 48 )
{
v6 = 0;
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x20000);
}
else
{
v6 = 1;
printf("Unusable red value '%c'\n", v5);
}
if ( (unsigned __int8)(argv[1][1] - 49) <= 1u )
{
func1((__int64)&rawMemGPIO, 0x1Cu, -1, 0x8000000);
}
else if ( argv[1][1] == 48 )
{
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x8000000);
}
else
{
v6 = 1;
printf("Unusable blue value '%c'\n", (unsigned int)argv[1][1]);
}
if ( (unsigned __int8)(argv[1][2] - 49) <= 1u )
{
func1((__int64)&rawMemGPIO, 0x1Cu, -1, 0x400000);
goto LABEL_21;
}
if ( argv[1][2] == 48 )
{
func1((__int64)&rawMemGPIO, 0x28u, -1, 0x400000);
LABEL_21:
if ( !v6 )
goto LABEL_16;
goto LABEL_15;
}
printf("Unusable green value '%c'\n", (unsigned int)argv[1][2]);
LABEL_15:
printf("Usage: %s RGB\n Where R is red (0-2), G is green (0-2) and B is blue (0-2).\n", *argv);
LABEL_16:
func3();
return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
printUse
unsigned char

return_type: int


repo: raspberry-alpha-omega/pwm

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

v3 = setup(argc, argv, &levels);
hi = 0;
nchannels = v3;
emit();
emit();
emit();
emit();
emit();
describe();
return 0;
}

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

v3 = func7(argc, argv, &levels);
hi = 0;
nchannels = v3;
func11();
func11();
func11();
func11();
func11();
func12();
return 0;
}

original_code:
int main(int argc, char**argv) {
  int cycle;
  nchannels = setup(argc, argv, levels);
  hi = 0;
  for (cycle = 0; cycle < 5; ++cycle) {
    emit();
  }
  describe();
}

canonical_original_code:
int main(int argc, char **argv) {
  int cycle;
  nchannels = setup(argc, argv, levels);
  hi = 0;
  for (cycle = 0; cycle < 5; ++cycle) {
    emit();
  }
  describe();
}

variable_types:
argv
char **

variable_types: cycle int


setup

decompiled_code:
__int64 __fastcall setup(int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
__int64 v5; // rbx

v3 = a1 - 1;
if ( a1 - 1 <= 0 )
{
v3 = 0;
}
else
{
v5 = 1LL;
do
{
*(_DWORD *)(a3 + 4 * v5 - 4) = atoi(*(const char **)(a2 + 8 * v5));
++v5;
}
while ( v5 != (unsigned int)(a1 - 2) + 2LL );
}
printf("running %d cycles of %d channels\n", 5LL, v3);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // er13
__int64 v5; // rbx

v3 = a1 - 1;
if ( a1 - 1 <= 0 )
{
v3 = 0;
}
else
{
v5 = 1LL;
do
{
*(_DWORD *)(a3 + 4 * v5 - 4) = atoi(*(const char **)(a2 + 8 * v5));
++v5;
}
while ( v5 != (unsigned int)(a1 - 2) + 2LL );
}
printf("running %d cycles of %d channels\n", 5LL, v3);
return v3;
}

original_code:
int setup(int argc, char**argv, int* channels) {
  int ret = 0;
  for (ret = 0; ret < argc-1; ++ret) {
    channels[ret] = atoi(argv[ret+1]);
  }
  printf("running %d cycles of %d channels\n", 5, ret);
  return ret;
}

canonical_original_code:
int setup(int argc, char **argv, int *channels) {
  int ret = 0;
  for (ret = 0; ret < argc-1; ++ret) {
    channels[ret] = atoi(argv[ret+1]);
  }
  printf("running %d cycles of %d channels\n", 5, ret);
  return ret;
}

variable_types:
channels
int *

variable_types: ret int


decompiled_code:
void __fastcall switch_on(int a1)
{
state[a1] = 1;
}

canonical_decompiled_code:
void __fastcall func8(int a1)
{
state[a1] = 1;
}

original_code:
void switch_on(int channel) {
  state[channel] = 1;
}

variable_types: channel int


decompiled_code:
void __fastcall switch_off(int a1)
{
state[a1] = 0;
}

canonical_decompiled_code:
void __fastcall func9(int a1)
{
state[a1] = 0;
}

original_code:
void switch_off(int channel) {
  state[channel] = 0;
}


wait

original_code:
void wait(int ticks) {
  int i;
  int channel;
  for (i = 0; i < ticks; ++i) {
    for (channel = 0; channel < nchannels; ++channel) {
      history[channel][hi] = state[channel] ? '^' : '_';
    }
    usleep(1000);
    ++hi;
  }
}

variable_types: ticks int

variable_types: i int


emit

decompiled_code:
void emit()
{
int i; // ebx
int v1; // er8
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax

for ( i = 0; i != 4; ++i )
{
v1 = 1 << i;
if ( nchannels > 0 )
{
v2 = 0LL;
do
{
while ( (levels[v2] & v1) != 0 )
{
switch_on(v2);
v2 = v3 + 1;
if ( nchannels <= (int)v2 )
goto LABEL_7;
}
switch_off(v2);
v2 = v4 + 1;
}
while ( nchannels > (int)v2 );
}
LABEL_7:
wait(v1);
}
}

canonical_decompiled_code:
void func11()
{
int i; // ebx
int v1; // er8
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax

for ( i = 0; i != 4; ++i )
{
v1 = 1 << i;
if ( nchannels > 0 )
{
v2 = 0LL;
do
{
while ( (levels[v2] & v1) != 0 )
{
func8(v2);
v2 = v3 + 1;
if ( nchannels <= (int)v2 )
goto LABEL_7;
}
func9(v2);
v2 = v4 + 1;
}
while ( nchannels > (int)v2 );
}
LABEL_7:
func10(v1);
}
}

original_code:
void emit() {
  int bit;
  int power;
  int channel;
  for (bit = 0; bit < 4; ++bit) {
    power = 1 << bit;
    for (channel = 0; channel < nchannels; ++channel) {
      if ((levels[channel] & power) > 0) {
        switch_on(channel);
      } else {
        switch_off(channel);
      }
    }
    wait(power);
  }
}

variable_types: bit int

variable_types: power int


describe

decompiled_code:
int describe()
{
char *v0; // rbp
int v1; // ebx
__int64 v2; // rax
const char *v3; // rdi

if ( nchannels > 0 )
{
v0 = history;
v1 = 0;
do
{
v2 = v1;
v3 = v0;
++v1;
v0 += 81;
history[81 * v2 + hi] = 0;
puts(v3);
}
while ( nchannels > v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int func12()
{
char *v0; // rbp
int v1; // ebx
__int64 v2; // rax
const char *v3; // rdi

if ( nchannels > 0 )
{
v0 = history;
v1 = 0;
do
{
v2 = v1;
v3 = v0;
++v1;
v0 += 81;
history[81 * v2 + hi] = 0;
puts(v3);
}
while ( nchannels > v1 );
}
return putchar(10);
}

original_code:
void describe() {
  int channel;
  for (channel = 0; channel < nchannels; ++channel) {
    history[channel][hi] = 0;
    puts(history[channel]);
  }
  putchar('\n');
}


repo: rastersoft/fbzx

decompiled_code:
__int64 compare_processors()
{
__int64 v0; // rdx
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rsi
__int64 v6; // rdx
__int64 v7; // rsi
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rdx
__int64 v11; // rsi
__int64 v12; // rdx
__int64 v13; // rsi
__int64 v14; // rdx
__int64 v15; // rsi
__int64 v16; // rdx
__int64 v17; // rsi
__int64 v18; // rdx
__int64 v19; // rsi
__int64 v20; // rdx
__int64 v21; // rsi
__int64 v22; // rdx
__int64 v23; // rsi
__int64 v24; // rdx
__int64 v25; // rsi
__int64 v26; // rdx
__int64 v27; // rsi
__int64 v28; // rdx
__int64 v29; // rsi
__int64 v30; // rdx
__int64 v31; // rsi
__int64 v32; // rdx
__int64 v33; // rsi

v0 = (unsigned __int8)*(&emulator + 15);
v1 = (unsigned __int8)*(&emulator2 + 15);
if ( (_BYTE)v0 != (_BYTE)v1 )
printf("A should be %X but is %X\n", v1, v0);
if ( ((unsigned __int8)*(&emulator + 14) | 0x28) != ((unsigned __int8)*(&emulator2 + 14) | 0x28) )
printf("F should be %X but is %X\n", (unsigned __int8)*(&emulator2 + 14), (unsigned __int8)*(&emulator + 14));
v2 = *((unsigned __int16 *)&emulator + 8);
v3 = *((unsigned __int16 *)&emulator2 + 8);
if ( (_WORD)v2 != (_WORD)v3 )
printf("BC should be %X but is %X\n", v3, v2);
v4 = *((unsigned __int16 *)&emulator + 9);
v5 = *((unsigned __int16 *)&emulator2 + 9);
if ( (_WORD)v4 != (_WORD)v5 )
printf("DE should be %X but is %X\n", v5, v4);
v6 = *((unsigned __int16 *)&emulator + 10);
v7 = *((unsigned __int16 *)&emulator2 + 10);
if ( (_WORD)v6 != (_WORD)v7 )
printf("HL should be %X but is %X\n", v7, v6);
v8 = (unsigned __int8)*(&emulator + 1);
v9 = (unsigned __int8)*(&emulator2 + 1);
if ( (_BYTE)v8 != (_BYTE)v9 )
printf("A' should be %X but is %X\n", v9, v8);
if ( ((unsigned __int8)emulator | 0x28) != ((unsigned __int8)emulator2 | 0x28) )
printf("F' should be %X but is %X\n", (unsigned __int8)emulator2, (unsigned __int8)emulator);
v10 = *((unsigned __int16 *)&emulator + 1);
v11 = *((unsigned __int16 *)&emulator2 + 1);
if ( (_WORD)v10 != (_WORD)v11 )
printf("BC' should be %X but is %X\n", v11, v10);
v12 = *((unsigned __int16 *)&emulator + 2);
v13 = *((unsigned __int16 *)&emulator2 + 2);
if ( (_WORD)v12 != (_WORD)v13 )
printf("DE' should be %X but is %X\n", v13, v12);
v14 = *((unsigned __int16 *)&emulator + 3);
v15 = *((unsigned __int16 *)&emulator2 + 3);
if ( (_WORD)v14 != (_WORD)v15 )
printf("HL' should be %X but is %X\n", v15, v14);
v16 = *((unsigned __int16 *)&emulator + 11);
v17 = *((unsigned __int16 *)&emulator2 + 11);
if ( (_WORD)v16 != (_WORD)v17 )
printf("IX should be %X but is %X\n", v17, v16);
v18 = *((unsigned __int16 *)&emulator + 12);
v19 = *((unsigned __int16 *)&emulator2 + 12);
if ( (_WORD)v18 != (_WORD)v19 )
printf("IY should be %X but is %X\n", v19, v18);
v20 = *((unsigned __int16 *)&emulator + 14);
v21 = *((unsigned __int16 *)&emulator2 + 14);
if ( (_WORD)v20 != (_WORD)v21 )
printf("PC should be %X but is %X\n", v21, v20);
v22 = *((unsigned __int16 *)&emulator + 13);
v23 = *((unsigned __int16 *)&emulator2 + 13);
if ( (_WORD)v22 != (_WORD)v23 )
printf("SP should be %X but is %X\n", v23, v22);
v24 = (unsigned __int8)*(&emulator + 36);
v25 = (unsigned __int8)*(&emulator2 + 36);
if ( (_BYTE)v24 != (_BYTE)v25 )
printf("HALT should be %X but is %X\n", v25, v24);
v26 = (unsigned __int8)*(&emulator + 32);
v27 = (unsigned __int8)*(&emulator2 + 32);
if ( (_BYTE)v26 != (_BYTE)v27 )
printf("I should be %X but is %X\n", v27, v26);
if ( (((unsigned __int8)*(&emulator2 + 30) ^ (unsigned __int8)*(&emulator + 30)) & 0x7F) != 0 )
printf("R should be %X but is %X\n", *(&emulator2 + 30) & 0x7F, *(&emulator + 30) & 0x7F);
v28 = (unsigned __int8)*(&emulator + 35);
v29 = (unsigned __int8)*(&emulator2 + 35);
if ( (_BYTE)v28 != (_BYTE)v29 )
printf("IM should be %X but is %X\n", v29, v28);
v30 = (unsigned __int8)*(&emulator + 33);
v31 = (unsigned __int8)*(&emulator2 + 33);
if ( ((_BYTE)v30 == 1) != ((_BYTE)v31 == 1) )
printf("IFF1 should be %X but is %X\n", v31, v30);
v32 = (unsigned __int8)*(&emulator + 34);
v33 = (unsigned __int8)*(&emulator2 + 34);
if ( (_BYTE)v32 != (_BYTE)v33 )
printf("IFF2 should be %X but is %X\n", v33, v32);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rdx
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rsi
__int64 v6; // rdx
__int64 v7; // rsi
__int64 v8; // rdx
__int64 v9; // rsi
__int64 v10; // rdx
__int64 v11; // rsi
__int64 v12; // rdx
__int64 v13; // rsi
__int64 v14; // rdx
__int64 v15; // rsi
__int64 v16; // rdx
__int64 v17; // rsi
__int64 v18; // rdx
__int64 v19; // rsi
__int64 v20; // rdx
__int64 v21; // rsi
__int64 v22; // rdx
__int64 v23; // rsi
__int64 v24; // rdx
__int64 v25; // rsi
__int64 v26; // rdx
__int64 v27; // rsi
__int64 v28; // rdx
__int64 v29; // rsi
__int64 v30; // rdx
__int64 v31; // rsi
__int64 v32; // rdx
__int64 v33; // rsi

v0 = (unsigned __int8)*(&emulator + 15);
v1 = (unsigned __int8)*(&emulator2 + 15);
if ( (_BYTE)v0 != (_BYTE)v1 )
printf("A should be %X but is %X\n", v1, v0);
if ( ((unsigned __int8)*(&emulator + 14) | 0x28) != ((unsigned __int8)*(&emulator2 + 14) | 0x28) )
printf("F should be %X but is %X\n", (unsigned __int8)*(&emulator2 + 14), (unsigned __int8)*(&emulator + 14));
v2 = *((unsigned __int16 *)&emulator + 8);
v3 = *((unsigned __int16 *)&emulator2 + 8);
if ( (_WORD)v2 != (_WORD)v3 )
printf("BC should be %X but is %X\n", v3, v2);
v4 = *((unsigned __int16 *)&emulator + 9);
v5 = *((unsigned __int16 *)&emulator2 + 9);
if ( (_WORD)v4 != (_WORD)v5 )
printf("DE should be %X but is %X\n", v5, v4);
v6 = *((unsigned __int16 *)&emulator + 10);
v7 = *((unsigned __int16 *)&emulator2 + 10);
if ( (_WORD)v6 != (_WORD)v7 )
printf("HL should be %X but is %X\n", v7, v6);
v8 = (unsigned __int8)*(&emulator + 1);
v9 = (unsigned __int8)*(&emulator2 + 1);
if ( (_BYTE)v8 != (_BYTE)v9 )
printf("A' should be %X but is %X\n", v9, v8);
if ( ((unsigned __int8)emulator | 0x28) != ((unsigned __int8)emulator2 | 0x28) )
printf("F' should be %X but is %X\n", (unsigned __int8)emulator2, (unsigned __int8)emulator);
v10 = *((unsigned __int16 *)&emulator + 1);
v11 = *((unsigned __int16 *)&emulator2 + 1);
if ( (_WORD)v10 != (_WORD)v11 )
printf("BC' should be %X but is %X\n", v11, v10);
v12 = *((unsigned __int16 *)&emulator + 2);
v13 = *((unsigned __int16 *)&emulator2 + 2);
if ( (_WORD)v12 != (_WORD)v13 )
printf("DE' should be %X but is %X\n", v13, v12);
v14 = *((unsigned __int16 *)&emulator + 3);
v15 = *((unsigned __int16 *)&emulator2 + 3);
if ( (_WORD)v14 != (_WORD)v15 )
printf("HL' should be %X but is %X\n", v15, v14);
v16 = *((unsigned __int16 *)&emulator + 11);
v17 = *((unsigned __int16 *)&emulator2 + 11);
if ( (_WORD)v16 != (_WORD)v17 )
printf("IX should be %X but is %X\n", v17, v16);
v18 = *((unsigned __int16 *)&emulator + 12);
v19 = *((unsigned __int16 *)&emulator2 + 12);
if ( (_WORD)v18 != (_WORD)v19 )
printf("IY should be %X but is %X\n", v19, v18);
v20 = *((unsigned __int16 *)&emulator + 14);
v21 = *((unsigned __int16 *)&emulator2 + 14);
if ( (_WORD)v20 != (_WORD)v21 )
printf("PC should be %X but is %X\n", v21, v20);
v22 = *((unsigned __int16 *)&emulator + 13);
v23 = *((unsigned __int16 *)&emulator2 + 13);
if ( (_WORD)v22 != (_WORD)v23 )
printf("SP should be %X but is %X\n", v23, v22);
v24 = (unsigned __int8)*(&emulator + 36);
v25 = (unsigned __int8)*(&emulator2 + 36);
if ( (_BYTE)v24 != (_BYTE)v25 )
printf("HALT should be %X but is %X\n", v25, v24);
v26 = (unsigned __int8)*(&emulator + 32);
v27 = (unsigned __int8)*(&emulator2 + 32);
if ( (_BYTE)v26 != (_BYTE)v27 )
printf("I should be %X but is %X\n", v27, v26);
if ( (((unsigned __int8)*(&emulator2 + 30) ^ (unsigned __int8)*(&emulator + 30)) & 0x7F) != 0 )
printf("R should be %X but is %X\n", *(&emulator2 + 30) & 0x7F, *(&emulator + 30) & 0x7F);
v28 = (unsigned __int8)*(&emulator + 35);
v29 = (unsigned __int8)*(&emulator2 + 35);
if ( (_BYTE)v28 != (_BYTE)v29 )
printf("IM should be %X but is %X\n", v29, v28);
v30 = (unsigned __int8)*(&emulator + 33);
v31 = (unsigned __int8)*(&emulator2 + 33);
if ( ((_BYTE)v30 == 1) != ((_BYTE)v31 == 1) )
printf("IFF1 should be %X but is %X\n", v31, v30);
v32 = (unsigned __int8)*(&emulator + 34);
v33 = (unsigned __int8)*(&emulator2 + 34);
if ( (_BYTE)v32 != (_BYTE)v33 )
printf("IFF2 should be %X but is %X\n", v33, v32);
return 0LL;
}

original_code:
int compare_processors() {
 if (emulator.Rm.br.A!=emulator2.Rm.br.A) {
  printf("A should be %X but is %X\n",emulator2.Rm.br.A,emulator.Rm.br.A);
 }
 if ((emulator.Rm.br.F|0x28)!=(emulator2.Rm.br.F|0x28)) {
  printf("F should be %X but is %X\n",emulator2.Rm.br.F,emulator.Rm.br.F);
 }
 if (emulator.Rm.wr.BC!=emulator2.Rm.wr.BC) {
  printf("BC should be %X but is %X\n",emulator2.Rm.wr.BC,emulator.Rm.wr.BC);
 }
 if (emulator.Rm.wr.DE!=emulator2.Rm.wr.DE) {
  printf("DE should be %X but is %X\n",emulator2.Rm.wr.DE,emulator.Rm.wr.DE);
 }
 if (emulator.Rm.wr.HL!=emulator2.Rm.wr.HL) {
  printf("HL should be %X but is %X\n",emulator2.Rm.wr.HL,emulator.Rm.wr.HL);
 }
 if (emulator.Ra.br.A!=emulator2.Ra.br.A) {
  printf("A' should be %X but is %X\n",emulator2.Ra.br.A,emulator.Ra.br.A);
 }
 if ((emulator.Ra.br.F|0x28)!=(emulator2.Ra.br.F|0x28)) {
  printf("F' should be %X but is %X\n",emulator2.Ra.br.F,emulator.Ra.br.F);
 }
 if (emulator.Ra.wr.BC!=emulator2.Ra.wr.BC) {
  printf("BC' should be %X but is %X\n",emulator2.Ra.wr.BC,emulator.Ra.wr.BC);
 }
 if (emulator.Ra.wr.DE!=emulator2.Ra.wr.DE) {
  printf("DE' should be %X but is %X\n",emulator2.Ra.wr.DE,emulator.Ra.wr.DE);
 }
 if (emulator.Ra.wr.HL!=emulator2.Ra.wr.HL) {
  printf("HL' should be %X but is %X\n",emulator2.Ra.wr.HL,emulator.Ra.wr.HL);
 }
 if (emulator.Rm.wr.IX!=emulator2.Rm.wr.IX) {
  printf("IX should be %X but is %X\n",emulator2.Rm.wr.IX,emulator.Rm.wr.IX);
 }
 if (emulator.Rm.wr.IY!=emulator2.Rm.wr.IY) {
  printf("IY should be %X but is %X\n",emulator2.Rm.wr.IY,emulator.Rm.wr.IY);
 }
 if (emulator.PC!=emulator2.PC) {
  printf("PC should be %X but is %X\n",emulator2.PC,emulator.PC);
 }
 if (emulator.Rm.wr.SP!=emulator2.Rm.wr.SP) {
  printf("SP should be %X but is %X\n",emulator2.Rm.wr.SP,emulator.Rm.wr.SP);
 }
 if (emulator.HALT!=emulator2.HALT) {
  printf("HALT should be %X but is %X\n",emulator2.HALT,emulator.HALT);
 }
 if (emulator.I!=emulator2.I) {
  printf("I should be %X but is %X\n",emulator2.I,emulator.I);
 }
 if ((emulator.R&0x7F)!=(emulator2.R&0x7F)) {
  printf("R should be %X but is %X\n",(emulator2.R&0x7F),(emulator.R&0x7F));
 }
 if (emulator.IM!=emulator2.IM) {
  printf("IM should be %X but is %X\n",emulator2.IM,emulator.IM);
 }
 if ((emulator.IFF1 == 1) != (emulator2.IFF1 == 1)) {
  printf("IFF1 should be %X but is %X\n",emulator2.IFF1,emulator.IFF1);
 }
 if (emulator.IFF2!=emulator2.IFF2) {
  printf("IFF2 should be %X but is %X\n",emulator2.IFF2,emulator.IFF2);
 }
 return 0;
}


original_code:
int read_elements(Z80FREE *emulador) {
 int tstates,retval;
 unsigned short int v1,v2,v3,v4,v5,v6,v7,v8,v9,v10;
 Z80free_reset(emulador);
 retval=fscanf(infile,"%hx %hx %hx %hx",(unsigned short int *)&emulador->Rm.wr.AF,(unsigned short int *)&emulador->Rm.wr.BC,(unsigned short int *)&emulador->Rm.wr.DE,(unsigned short int *)&emulador->Rm.wr.HL);
 retval=fscanf(infile,"%hx %hx %hx %hx",&v7,&v8,&v9,&v10);
 emulador->Ra.wr.AF=v7;
 emulador->Ra.wr.BC=v8;
 emulador->Ra.wr.DE=v9;
 emulador->Ra.wr.HL=v10;
 retval=fscanf(infile,"%hx %hx %hx %hx",(unsigned short int *)&emulador->Rm.wr.IX,(unsigned short int *)&emulador->Rm.wr.IY,(unsigned short int *)&emulador->Rm.wr.SP,(unsigned short int *)&emulador->PC);
 retval=fscanf(infile,"%hx %hx %hx %hx %hx %hx %d",&v1,&v2,&v3,&v4,&v5,&v6,&tstates);
 emulador->I=v1;
 emulador->R=v2;
 emulador->R2=v2;
 emulador->IFF1=v3;
 emulador->IFF2=v4;
 emulador->IM=v5;
 emulador->HALT=v6;
 return (tstates);
}

canonical_original_code:
int read_elements(struct Z80FREE *emulador) {
 int tstates; int retval;
 unsigned short v1; unsigned short v2; unsigned short v3; unsigned short v4; unsigned short v5; unsigned short v6; unsigned short v7; unsigned short v8; unsigned short v9; unsigned short v10;
 Z80free_reset(emulador);
 retval=fscanf(infile,"%hx %hx %hx %hx",(unsigned short *)&emulador->Rm.wr.AF,(unsigned short *)&emulador->Rm.wr.BC,(unsigned short *)&emulador->Rm.wr.DE,(unsigned short *)&emulador->Rm.wr.HL);
 retval=fscanf(infile,"%hx %hx %hx %hx",&v7,&v8,&v9,&v10);
 emulador->Ra.wr.AF=v7;
 emulador->Ra.wr.BC=v8;
 emulador->Ra.wr.DE=v9;
 emulador->Ra.wr.HL=v10;
 retval=fscanf(infile,"%hx %hx %hx %hx",(unsigned short *)&emulador->Rm.wr.IX,(unsigned short *)&emulador->Rm.wr.IY,(unsigned short *)&emulador->Rm.wr.SP,(unsigned short *)&emulador->PC);
 retval=fscanf(infile,"%hx %hx %hx %hx %hx %hx %d",&v1,&v2,&v3,&v4,&v5,&v6,&tstates);
 emulador->I=v1;
 emulador->R=v2;
 emulador->R2=v2;
 emulador->IFF1=v3;
 emulador->IFF2=v4;
 emulador->IM=v5;
 emulador->HALT=v6;
 return (tstates);
}

variable_types: struct Z80FREE { union Z80FRegs { struct { unsigned short AF; } wr; struct { unsigned char F; } br; } Ra; union Z80FRegs Rm; unsigned short PC; unsigned char R; unsigned char R2; unsigned char I; unsigned char IFF1; unsigned char IFF2; unsigned char IM; unsigned char HALT; unsigned char INT_P; unsigned char NMI_P; unsigned char empty_bus; unsigned short IAddr; unsigned char IAddr_done; enum Z80Status { Z80XX=0, Z80CB=1, Z80DD=2, Z80ED=3, Z80FD=4, Z80INT=5 } Status; _Bool M1; unsigned short subtstates; } *

variable_types: tstates int

variable_types: retval int

user_defined_types: struct Z80FREE { union Z80FRegs Ra; union Z80FRegs Rm; unsigned short PC; unsigned char R; unsigned char R2; unsigned char I; unsigned char IFF1; unsigned char IFF2; unsigned char IM; unsigned char HALT; unsigned char INT_P; unsigned char NMI_P; unsigned char empty_bus; unsigned short IAddr; unsigned char IAddr_done; enum Z80Status Status; _Bool M1; unsigned short subtstates; }


decompiled_code:
__int64 __fastcall read_memory(char a1)
{
__int64 result; // rax
unsigned int v2; // esi
__int64 v3; // rcx
unsigned int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int v5; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_fscanf(infile, "%x", &v4);
result = v4;
if ( v4 != -1 )
{
while ( 1 )
{
while ( 1 )
{
_isoc99_fscanf(infile, "%x", &v5);
if ( v5 == -1 )
return 0LL;
v2 = v4;
if ( a1 )
break;
memory[v4] = v5;
LABEL_9:
v4 = v2 + 1;
}
v3 = (unsigned __int8)memory[v4];
if ( v5 == (_DWORD)v3 )
goto LABEL_9;
printf("Error in memory, address %X. Expected value: %X, current value: %X\n", v4++, v5, v3);
if ( v5 == -1 )
return 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
__int64 result; // rax
unsigned int v2; // esi
__int64 v3; // rcx
unsigned int v4; // [rsp+8h] [rbp-10h] BYREF
unsigned int v5; // [rsp+Ch] [rbp-Ch] BYREF

_isoc99_fscanf(infile, "%x", &v4);
result = v4;
if ( v4 != -1 )
{
while ( 1 )
{
while ( 1 )
{
_isoc99_fscanf(infile, "%x", &v5);
if ( v5 == -1 )
return 0LL;
v2 = v4;
if ( a1 )
break;
memory[v4] = v5;
LABEL_9:
v4 = v2 + 1;
}
v3 = (unsigned __int8)memory[v4];
if ( v5 == (_DWORD)v3 )
goto LABEL_9;
printf("Error in memory, address %X. Expected value: %X, current value: %X\n", v4++, v5, v3);
if ( v5 == -1 )
return 0LL;
}
}
return result;
}

original_code:
int read_memory(char mode) {
 int position,retval,value;
 retval=fscanf(infile,"%x",&position);
 if (position==-1) {
  return -1;
 }
 do {
  retval=fscanf(infile,"%x",&value);
  if (value!=-1) {
   if (mode) {
    if (memory[position]!=value) {
     printf("Error in memory, address %X. Expected value: %X, current value: %X\n",position,value,memory[position]);
    }
   } else {
    memory[position]=(byte)value;
   }
   position++;
  }
 } while(value!=-1);
 return 0;
}

canonical_original_code:
int read_memory(char mode) {
 int position; int retval; int value;
 retval=fscanf(infile,"%x",&position);
 if (position==-1) {
  return -1;
 }
 do {
  retval=fscanf(infile,"%x",&value);
  if (value!=-1) {
   if (mode) {
    if (memory[position]!=value) {
     printf("Error in memory, address %X. Expected value: %X, current value: %X\n",position,value,memory[position]);
    }
   } else {
    memory[position]=(unsigned char)value;
   }
   position++;
  }
 } while(value!=-1);
 return 0;
}

variable_types: mode char

variable_types: position int

variable_types: value int


decompiled_code:
__int64 read_inports()
{
__int64 v0; // rdx
int v1; // eax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4; // [rsp+Ch] [rbp-Ch] BYREF

portpos = 0;
for ( elements = 0; ; elements = v1 + 1 )
{
_isoc99_fscanf(infile, "%x", &v3);
if ( v3 == -1 )
break;
_isoc99_fscanf(infile, "%x", &v4);
v0 = elements;
v1 = elements;
portdirs[elements] = v3;
ports[v0] = v4;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rdx
int v1; // eax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4; // [rsp+Ch] [rbp-Ch] BYREF

portpos = 0;
for ( elements = 0; ; elements = v1 + 1 )
{
_isoc99_fscanf(infile, "%x", &v3);
if ( v3 == -1 )
break;
_isoc99_fscanf(infile, "%x", &v4);
v0 = elements;
v1 = elements;
portdirs[elements] = v3;
ports[v0] = v4;
}
return 0LL;
}

original_code:
int read_inports() {
 int addr,value,retval;
 portpos=0;
 elements=0;
 while (1) {
  retval=fscanf(infile,"%x",&addr);
  if (addr==-1) {
   break;
  }
  retval=fscanf(infile,"%x",&value);
  portdirs[elements]=(word)addr;
  ports[elements]=(byte)value;
  elements++;
 }
 return 0;
}

canonical_original_code:
int read_inports() {
 int addr; int value; int retval;
 portpos=0;
 elements=0;
 while (1) {
  retval=fscanf(infile,"%x",&addr);
  if (addr==-1) {
   break;
  }
  retval=fscanf(infile,"%x",&value);
  portdirs[elements]=(unsigned short)addr;
  ports[elements]=(unsigned char)value;
  elements++;
 }
 return 0;
}

variable_types: addr int


decompiled_code:
__int64 read_test()
{
int v0; // ebx
int v1; // ebp
unsigned int v2; // er12

if ( (int)_isoc99_fscanf(infile, "%s", test_name) <= 0 )
return 0xFFFFFFFFLL;
printf("Test %s\n", test_name);
v0 = read_elements((__int64)&emulator);
while ( !(unsigned int)read_memory(0) )
;
v1 = 0;
read_inports();
if ( v0 > 0 )
{
do
v1 += Z80free_step(&emulator);
while ( v0 > v1 );
}
v2 = read_elements((__int64)&emulator2);
compare_processors();
if ( v2 != v1 )
printf("Test needed %d tstates, but should needed %d\n", (unsigned int)v1, v2);
while ( !(unsigned int)read_memory(1) )
;
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // ebx
int v1; // ebp
unsigned int v2; // er12

if ( (int)_isoc99_fscanf(infile, "%s", test_name) <= 0 )
return 0xFFFFFFFFLL;
printf("Test %s\n", test_name);
v0 = func1((__int64)&emulator);
while ( !(unsigned int)func2(0) )
;
v1 = 0;
func3();
if ( v0 > 0 )
{
do
v1 += Z80free_step(&emulator);
while ( v0 > v1 );
}
v2 = func1((__int64)&emulator2);
func0();
if ( v2 != v1 )
printf("Test needed %d tstates, but should needed %d\n", (unsigned int)v1, v2);
while ( !(unsigned int)func2(1) )
;
return 0LL;
}

original_code:
int read_test() {
 int retval,tstates,tst2,totaltst;
 retval=fscanf(infile,"%s",test_name);
 if (retval<=0) {
  return -1;
 }
 printf("Test %s\n",test_name);
 tstates=read_elements(&emulator);
 while(0==read_memory(0)) {
 }
 read_inports();
 totaltst=0;
 while(totaltst<tstates) {
  totaltst+=Z80free_step(&emulator);
 }
 tst2=read_elements(&emulator2);
 compare_processors();
 if (totaltst!=tst2) {
  printf("Test needed %d tstates, but should needed %d\n",totaltst,tst2);
 }
 while(0==read_memory(1)) {
 }
 return 0;
}

canonical_original_code:
int read_test() {
 int retval; int tstates; int tst2; int totaltst;
 retval=fscanf(infile,"%s",test_name);
 if (retval<=0) {
  return -1;
 }
 printf("Test %s\n",test_name);
 tstates=read_elements(&emulator);
 while(0==read_memory(0)) {
 }
 read_inports();
 totaltst=0;
 while(totaltst<tstates) {
  totaltst+=Z80free_step(&emulator);
 }
 tst2=read_elements(&emulator2);
 compare_processors();
 if (totaltst!=tst2) {
  printf("Test needed %d tstates, but should needed %d\n",totaltst,tst2);
 }
 while(0==read_memory(1)) {
 }
 return 0;
}

variable_types: totaltst int


decompiled_code:
__int64 __fastcall Z80free_Rd(unsigned __int16 a1)
{
return (unsigned __int8)memory[a1];
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1)
{
return (unsigned __int8)memory[a1];
}

original_code:
byte Z80free_Rd (word Addr) {
 return ((byte)memory[Addr]);
}

canonical_original_code:
unsigned char Z80free_Rd(unsigned short Addr) {
 return ((unsigned char)memory[Addr]);
}

variable_types:
Addr
unsigned short


decompiled_code:
void __fastcall Z80free_Wr(unsigned __int16 a1, char a2)
{
memory[a1] = a2;
}

canonical_decompiled_code:
void __fastcall func6(unsigned __int16 a1, char a2)
{
memory[a1] = a2;
}

original_code:
void Z80free_Wr (word Addr, byte Value) {
 memory[Addr]=(unsigned char) Value;
}

canonical_original_code:
void Z80free_Wr(unsigned short Addr, unsigned char Value) {
 memory[Addr]=(unsigned char) Value;
}

variable_types:
Value
unsigned char


decompiled_code:
__int64 __fastcall Z80free_In(unsigned __int16 a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned __int16 v3; // cx
unsigned int v4; // er8
__int64 result; // rax

v1 = portpos;
if ( portpos >= elements )
{
puts("IN beyond values");
result = 0LL;
}
else
{
v2 = portpos;
v3 = portdirs[portpos];
if ( v3 != a1 )
{
printf("IN to port %X (should be %X)\n", a1, v3);
v2 = portpos;
v1 = portpos;
}
v4 = (unsigned __int8)ports[v2];
portpos = v1 + 1;
result = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 a1)
{
int v1; // eax
__int64 v2; // rdx
unsigned __int16 v3; // cx
unsigned int v4; // er8
__int64 result; // rax

v1 = portpos;
if ( portpos >= elements )
{
puts("IN beyond values");
result = 0LL;
}
else
{
v2 = portpos;
v3 = portdirs[portpos];
if ( v3 != a1 )
{
printf("IN to port %X (should be %X)\n", a1, v3);
v2 = portpos;
v1 = portpos;
}
v4 = (unsigned __int8)ports[v2];
portpos = v1 + 1;
result = v4;
}
return result;
}

original_code:
byte Z80free_In (word Port) {
 byte value;
 if (portpos>=elements) {
  printf("IN beyond values\n");
  return 0;
 }
 if (Port!=portdirs[portpos])
  printf("IN to port %X (should be %X)\n",Port,portdirs[portpos]);
 value=ports[portpos];
 portpos++;
 return (value);
}

canonical_original_code:
unsigned char Z80free_In(unsigned short Port) {
 unsigned char value;
 if (portpos>=elements) {
  printf("IN beyond values\n");
  return 0;
 }
 if (Port!=portdirs[portpos])
  printf("IN to port %X (should be %X)\n",Port,portdirs[portpos]);
 value=ports[portpos];
 portpos++;
 return (value);
}

variable_types:
Port
unsigned short

variable_types:
value
unsigned char


decompiled_code:
void Z80free_Out()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code:
void Z80free_Out (word Port, byte Value) {
}

canonical_original_code:
void Z80free_Out(unsigned short Port, unsigned char Value) {
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
infile = (__int64)fopen("tests.z80free", "r");
while ( !(unsigned int)read_test() )
;
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
infile = (__int64)fopen("tests.z80free", "r");
while ( !(unsigned int)func4() )
;
return 0;
}

original_code:
int main() {
 infile=fopen("tests.z80free","r");
 while(0==read_test()) {
 }
 return 0;
}


repo: rata/drill

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // r12
int v4; // eax
int v5; // er12
int v7; // eax
int v8; // ebp

v3 = 0x8000LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "s:h");
if ( v4 == -1 )
break;
if ( v4 == 104 )
{
usage();
return 0;
}
if ( v4 != 115 )
goto LABEL_8;
v3 = parse_hole_size(optarg);
if ( !v3 )
return 1;
}
if ( _bss_start == argc )
{
puts("Error: no filename specified");
LABEL_8:
usage();
return 1;
}
if ( v3 > 0x63FFFFF )
{
printf("WARNING: %zu MB RAM will be used\n", v3 >> 20);
sleep(3u);
}
v7 = open(argv[argc - 1], 2);
v8 = v7;
if ( v7 == -1 )
{
v5 = 1;
perror("Error opening file");
}
else
{
v5 = drill(v7, v3);
if ( close(v8) )
{
v5 = 1;
perror("write failed");
}
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
size_t v3; // r12
int v4; // eax
int v5; // er12
int v7; // eax
int v8; // ebp

v3 = 0x8000LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "s:h");
if ( v4 == -1 )
break;
if ( v4 == 104 )
{
func7();
return 0;
}
if ( v4 != 115 )
goto LABEL_8;
v3 = func8(optarg);
if ( !v3 )
return 1;
}
if ( _bss_start == argc )
{
puts("Error: no filename specified");
LABEL_8:
func7();
return 1;
}
if ( v3 > 0x63FFFFF )
{
printf("WARNING: %zu MB RAM will be used\n", v3 >> 20);
sleep(3u);
}
v7 = open(argv[argc - 1], 2);
v8 = v7;
if ( v7 == -1 )
{
v5 = 1;
perror("Error opening file");
}
else
{
v5 = func10(v7, v3);
if ( close(v8) )
{
v5 = 1;
perror("write failed");
}
}
return v5;
}

original_code:
int main(int argc, char **argv)
{
 int opt;
 size_t hole_size = 1024 * 32;
 while ((opt = getopt(argc, argv, "s:h")) != -1) {
  switch(opt) {
  case 's':
   hole_size = parse_hole_size(optarg);
   if (hole_size == 0)
    return 1;
   break;
  case 'h':
   usage();
   return 0;
  default:
   usage();
   return 1;
  }
 }
 if (optind == argc) {
  printf("Error: no filename specified\n");
  usage();
  return 1;
 }
 if (hole_size >= 100 * 1024 * 1024) {
  size_t ram_mb = hole_size / 1024 / 1024;
  printf("WARNING: %zu MB RAM will be used\n", ram_mb);
  sleep(3);
 }
 char *fname = argv[argc - 1];
 int fd = open(fname, 02);
 if (fd == -1) {
  perror("Error opening file");
  return 1;
 }
 int ret = drill(fd, hole_size);
 int err = close(fd);
 if (err) {
  perror("write failed");
  ret = 1;
 }
 return ret;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int opt;
 unsigned long hole_size = 1024 * 32;
 while ((opt = getopt(argc, argv, "s:h")) != -1) {
  switch(opt) {
  case 's':
   hole_size = parse_hole_size(optarg);
   if (hole_size == 0)
    return 1;
   break;
  case 'h':
   usage();
   return 0;
  default:
   usage();
   return 1;
  }
 }
 if (optind == argc) {
  printf("Error: no filename specified\n");
  usage();
  return 1;
 }
 if (hole_size >= 100 * 1024 * 1024) {
  unsigned long ram_mb = hole_size / 1024 / 1024;
  printf("WARNING: %zu MB RAM will be used\n", ram_mb);
  sleep(3);
 }
 char *fname = argv[argc - 1];
 int fd = open(fname, 02);
 if (fd == -1) {
  perror("Error opening file");
  return 1;
 }
 int ret = drill(fd, hole_size);
 int err = close(fd);
 if (err) {
  perror("write failed");
  ret = 1;
 }
 return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types:
fname
char *

variable_types: fd int

variable_types: ret int

variable_types: err int


usage

decompiled_code:
int usage()
{
char v1[712]; // [rsp+0h] [rbp-2C8h] BYREF

strcpy(
v1,
"Usage: drill [options] <filename>\n"
"\n"
"Make a file sparse without using extra disk space, by just doing holes\n"
"in the file when possible.\n"
"\n"
"You can think of this as doing a cp --sparse and renaming the dest\n"
"file as the original, without the need for extra disk space.\n"
"\n"
"It uses linux and fs specific syscalls. It only works on Linux >= 2.6.38\n"
"with filesystems that support FALLOC_FL_PUNCH_HOLE fallocate(2) mode.\n"
"\n"
"Options: \n"
" -s HOLE_SIZE Size in kb of the minimum hole to dig (default: 32)\n"
" -h Show this help\n"
"\n"
"Note that too small values for HOLE_SIZE might be ignored. And\n"
"too big values might use lot of RAM and not detect many holes.\n"
"\n"
"Please report bugs to Rodrigo Campos <rodrigo@sdfg.com.ar>\n");
return printf("%s", v1);
}

canonical_decompiled_code:
int func7()
{
char v1[712]; // [rsp+0h] [rbp-2C8h] BYREF

strcpy(
v1,
"Usage: drill [options] <filename>\n"
"\n"
"Make a file sparse without using extra disk space, by just doing holes\n"
"in the file when possible.\n"
"\n"
"You can think of this as doing a cp --sparse and renaming the dest\n"
"file as the original, without the need for extra disk space.\n"
"\n"
"It uses linux and fs specific syscalls. It only works on Linux >= 2.6.38\n"
"with filesystems that support FALLOC_FL_PUNCH_HOLE fallocate(2) mode.\n"
"\n"
"Options: \n"
" -s HOLE_SIZE Size in kb of the minimum hole to dig (default: 32)\n"
" -h Show this help\n"
"\n"
"Note that too small values for HOLE_SIZE might be ignored. And\n"
"too big values might use lot of RAM and not detect many holes.\n"
"\n"
"Please report bugs to Rodrigo Campos <rodrigo@sdfg.com.ar>\n");
return printf("%s", v1);
}

original_code:
void usage(void)
{
 char h[] = "Usage: drill [options] <filename>\n"
 "\n"
 "Make a file sparse without using extra disk space, by just doing holes\n"
 "in the file when possible.\n"
 "\n"
 "You can think of this as doing a cp --sparse and renaming the dest\n"
 "file as the original, without the need for extra disk space.\n"
 "\n"
 "It uses linux and fs specific syscalls. It only works on Linux >= 2.6.38\n"
 "with filesystems that support FALLOC_FL_PUNCH_HOLE fallocate(2) mode.\n"
 "\n"
 "Options: \n"
 "  -s HOLE_SIZE  Size in kb of the minimum hole to dig (default: 32)\n"
 "  -h            Show this help\n"
 "\n"
 "Note that too small values for HOLE_SIZE might be ignored. And\n"
 "too big values might use lot of RAM and not detect many holes.\n"
 "\n"
 "Please report bugs to Rodrigo Campos <rodrigo@sdfg.com.ar>\n";
 printf("%s", h);
}

canonical_original_code:
void usage()
{
 char h[] = "Usage: drill [options] <filename>\n"
 "\n"
 "Make a file sparse without using extra disk space, by just doing holes\n"
 "in the file when possible.\n"
 "\n"
 "You can think of this as doing a cp --sparse and renaming the dest\n"
 "file as the original, without the need for extra disk space.\n"
 "\n"
 "It uses linux and fs specific syscalls. It only works on Linux >= 2.6.38\n"
 "with filesystems that support FALLOC_FL_PUNCH_HOLE fallocate(2) mode.\n"
 "\n"
 "Options: \n"
 "  -s HOLE_SIZE  Size in kb of the minimum hole to dig (default: 32)\n"
 "  -h            Show this help\n"
 "\n"
 "Note that too small values for HOLE_SIZE might be ignored. And\n"
 "too big values might use lot of RAM and not detect many holes.\n"
 "\n"
 "Please report bugs to Rodrigo Campos <rodrigo@sdfg.com.ar>\n";
 printf("%s", h);
}

variable_types:
h
char []


decompiled_code:
unsigned __int64 __fastcall parse_hole_size(const char *a1)
{
unsigned __int64 v1; // rax
unsigned __int64 result; // rax

v1 = strtoul(a1, 0LL, 0);
if ( v1 == -1LL )
{
perror("Hole size");
result = 0LL;
}
else if ( v1 )
{
result = v1 << 10;
}
else
{
puts("Error: hole size should be greater than 0");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(const char *a1)
{
unsigned __int64 v1; // rax
unsigned __int64 result; // rax

v1 = strtoul(a1, 0LL, 0);
if ( v1 == -1LL )
{
perror("Hole size");
result = 0LL;
}
else if ( v1 )
{
result = v1 << 10;
}
else
{
puts("Error: hole size should be greater than 0");
result = 0LL;
}
return result;
}

original_code:
size_t parse_hole_size(char *optarg)
{
 size_t hole_size_kb = strtoul(optarg, ((void *)0), 0);
 if (hole_size_kb == (0x7fffffffffffffffL * 2UL + 1UL)) {
  perror("Hole size");
  return 0;
 }
 if (hole_size_kb == 0) {
  printf("Error: hole size should be greater than 0\n");
  return 0;
 }
 return hole_size_kb * 1024;
}

canonical_original_code:
unsigned long parse_hole_size(char *optarg)
{
 unsigned long hole_size_kb = strtoul(optarg, ((void *)0), 0);
 if (hole_size_kb == (0x7fffffffffffffffL * 2UL + 1UL)) {
  perror("Hole size");
  return 0;
 }
 if (hole_size_kb == 0) {
  printf("Error: hole size should be greater than 0\n");
  return 0;
 }
 return hole_size_kb * 1024;
}

variable_types:
optarg
char *

return_type: unsigned long


decompiled_code:
int __fastcall dig_hole(int a1, __off_t a2, __off_t a3)
{
int result; // eax
int v4; // [rsp+Ch] [rbp-Ch]

result = fallocate(a1, 3, a2, a3);
if ( result )
{
v4 = result;
perror("fallocate failed");
result = v4;
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1, __off_t a2, __off_t a3)
{
int result; // eax
int v4; // [rsp+Ch] [rbp-Ch]

result = fallocate(a1, 3, a2, a3);
if ( result )
{
v4 = result;
perror("fallocate failed");
result = v4;
}
return result;
}

original_code:
int dig_hole(int fd, off_t offset, off_t length)
{
 int err = fallocate(fd, 0x02|0x01,
                   offset, length);
 if (err)
  perror("fallocate failed");
 return err;
}

canonical_original_code:
int dig_hole(int fd, long offset, long length)
{
 int err = fallocate(fd, 0x02|0x01,
                   offset, length);
 if (err)
  perror("fallocate failed");
 return err;
}

variable_types: offset long

variable_types: length long


drill

decompiled_code:
__int64 __fastcall drill(int fd, size_t len)
{
void *v2; // rax
void *v3; // r14
void *v4; // r12
unsigned __int64 v5; // rax
__off_t v6; // r15
int v7; // er8
size_t v8; // rax
__off_t v9; // rbx
int v10; // eax
int v11; // er8
unsigned int v12; // er8
unsigned __int64 v14; // [rsp+8h] [rbp-40h]
int v15; // [rsp+8h] [rbp-40h]
int v16; // [rsp+8h] [rbp-40h]

v2 = mmap(0LL, len, 1, 34, -1, 0LL);
if ( v2 == (void *)-1LL )
{
perror("mmap failed");
v12 = 1;
}
else
{
v3 = v2;
v4 = malloc(len);
if ( v4 )
{
v5 = lseek(fd, 0LL, 2);
v14 = v5;
if ( v5 == -1LL )
{
perror("lseek failed");
v7 = 1;
}
else
{
v6 = 0LL;
if ( len <= v5 )
{
while ( 1 )
{
v8 = pread(fd, v4, len, v6);
v9 = v8;
if ( v8 == -1LL )
break;
if ( !memcmp(v4, v3, v8) )
{
v7 = dig_hole(fd, v6, v9);
if ( v7 )
goto LABEL_11;
}
v6 += v9;
if ( v6 + len > v14 )
goto LABEL_15;
}
perror("pread failed");
v7 = 1;
}
else
{
LABEL_15:
v7 = 0;
}
}
}
else
{
v7 = 1;
}
LABEL_11:
v15 = v7;
v10 = munmap(v3, len);
v11 = v15;
if ( v10 )
{
perror("munmap failed");
v11 = 1;
}
v16 = v11;
free(v4);
v12 = v16;
}
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd, size_t len)
{
void *v2; // rax
void *v3; // r14
void *v4; // r12
unsigned __int64 v5; // rax
__off_t v6; // r15
int v7; // er8
size_t v8; // rax
__off_t v9; // rbx
int v10; // eax
int v11; // er8
unsigned int v12; // er8
unsigned __int64 v14; // [rsp+8h] [rbp-40h]
int v15; // [rsp+8h] [rbp-40h]
int v16; // [rsp+8h] [rbp-40h]

v2 = mmap(0LL, len, 1, 34, -1, 0LL);
if ( v2 == (void *)-1LL )
{
perror("mmap failed");
v12 = 1;
}
else
{
v3 = v2;
v4 = malloc(len);
if ( v4 )
{
v5 = lseek(fd, 0LL, 2);
v14 = v5;
if ( v5 == -1LL )
{
perror("lseek failed");
v7 = 1;
}
else
{
v6 = 0LL;
if ( len <= v5 )
{
while ( 1 )
{
v8 = pread(fd, v4, len, v6);
v9 = v8;
if ( v8 == -1LL )
break;
if ( !memcmp(v4, v3, v8) )
{
v7 = func9(fd, v6, v9);
if ( v7 )
goto LABEL_11;
}
v6 += v9;
if ( v6 + len > v14 )
goto LABEL_15;
}
perror("pread failed");
v7 = 1;
}
else
{
LABEL_15:
v7 = 0;
}
}
}
else
{
v7 = 1;
}
LABEL_11:
v15 = v7;
v10 = munmap(v3, len);
v11 = v15;
if ( v10 )
{
perror("munmap failed");
v11 = 1;
}
v16 = v11;
free(v4);
v12 = v16;
}
return v12;
}

original_code:
int drill(int fd, size_t hole_size)
{
 int ret = 0;
 int err;
 void *zeros = mmap(((void *)0), hole_size, 0x1,
                    0x02|0x20, -1, 0);
 if (zeros == ((void *) -1)) {
  perror("mmap failed");
  return 1;
 }
 ssize_t buf_len = hole_size;
 void *buf = malloc(buf_len);
 if (buf == ((void *)0)) {
  ret = 1;
  goto out;
 }
 off_t end = lseek(fd, 0, 2);
 if (end == -1) {
  perror("lseek failed");
  ret = 1;
  goto out;
 }
 for (off_t offset = 0; offset + hole_size <= end; offset += buf_len) {
  buf_len = pread(fd, buf, hole_size, offset);
  if (buf_len == -1) {
   perror("pread failed");
   ret = 1;
   goto out;
  }
  int not_zeros = memcmp(buf, zeros, buf_len);
  if (not_zeros)
   continue;
  ret = dig_hole(fd, offset, buf_len);
  if (ret)
   goto out;
 }
out:
 err = munmap(zeros, hole_size);
 if (err) {
  perror("munmap failed");
  ret = 1;
 }
 free(buf);
 return ret;
}

canonical_original_code:
int drill(int fd, unsigned long hole_size)
{
 int ret = 0;
 int err;
 void *zeros = mmap(((void *)0), hole_size, 0x1,
                    0x02|0x20, -1, 0);
 if (zeros == ((void *) -1)) {
  perror("mmap failed");
  return 1;
 }
 long buf_len = hole_size;
 void *buf = malloc(buf_len);
 if (buf == ((void *)0)) {
  ret = 1;
  goto out;
 }
 long end = lseek(fd, 0, 2);
 if (end == -1) {
  perror("lseek failed");
  ret = 1;
  goto out;
 }
 for (long offset = 0; offset + hole_size <= end; offset += buf_len) {
  buf_len = pread(fd, buf, hole_size, offset);
  if (buf_len == -1) {
   perror("pread failed");
   ret = 1;
   goto out;
  }
  int not_zeros = memcmp(buf, zeros, buf_len);
  if (not_zeros)
   continue;
  ret = dig_hole(fd, offset, buf_len);
  if (ret)
   goto out;
 }
out:
 err = munmap(zeros, hole_size);
 if (err) {
  perror("munmap failed");
  ret = 1;
 }
 free(buf);
 return ret;
}

variable_types:
zeros
void *

variable_types:
buf
void *

variable_types: end long


destroyWorkers

decompiled_code:
__int64 __fastcall destroyWorkers(unsigned int *a1)
{
__int64 v2; // rdi
__int64 result; // rax
int v4; // ebx
__int64 v5; // rbx
__int64 v6; // rdi
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = *((_QWORD *)a1 + 2);
shutDownServer = 1;
v7[0] = 0LL;
lpx_threadpool_join(v2, v7);
result = *a1;
if ( (int)result > 0 )
{
v4 = 0;
do
{
++v4;
lpx_pcq_enqueue(a1 + 6, 0LL);
result = *a1;
}
while ( (int)result > v4 );
if ( (int)result > 0 )
{
v5 = 0LL;
do
{
v6 = *(_QWORD *)(*((_QWORD *)a1 + 1) + 8 * v5++);
result = lpx_threadpool_join(v6, v7);
}
while ( (int)*a1 > (int)v5 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
__int64 v2; // rdi
__int64 result; // rax
int v4; // ebx
__int64 v5; // rbx
__int64 v6; // rdi
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = *((_QWORD *)a1 + 2);
shutDownServer = 1;
v7[0] = 0LL;
lpx_threadpool_join(v2, v7);
result = *a1;
if ( (int)result > 0 )
{
v4 = 0;
do
{
++v4;
lpx_pcq_enqueue(a1 + 6, 0LL);
result = *a1;
}
while ( (int)result > v4 );
if ( (int)result > 0 )
{
v5 = 0LL;
do
{
v6 = *(_QWORD *)(*((_QWORD *)a1 + 1) + 8 * v5++);
result = lpx_threadpool_join(v6, v7);
}
while ( (int)*a1 > (int)v5 );
}
}
return result;
}

original_code:
void destroyWorkers(lpx_tcpserver_t *server)
{
    int i = 0;
    void *shutdownRequest = ((void *)0);
    void *retval = ((void *)0);
    shutDownServer = 1;
    lpx_threadpool_join(server->dispatcher, &retval);
    for(i = 0; i < server->numWorkers; i++) {
        lpx_pcq_enqueue(&server->requestQueue, shutdownRequest);
    }
    for(i = 0; i < server->numWorkers; i++) {
 lpx_threadpool_join(server->futures[i], &retval);
    }
    return;
}

canonical_original_code:
void destroyWorkers(struct __lpx_tcpserver_t *server)
{
    int i = 0;
    void *shutdownRequest = ((void *)0);
    void *retval = ((void *)0);
    shutDownServer = 1;
    lpx_threadpool_join(server->dispatcher, &retval);
    for(i = 0; i < server->numWorkers; i++) {
        lpx_pcq_enqueue(&server->requestQueue, shutdownRequest);
    }
    for(i = 0; i < server->numWorkers; i++) {
 lpx_threadpool_join(server->futures[i], &retval);
    }
    return;
}

variable_types: struct __lpx_tcpserver_t { int numWorkers; struct __lpx_thread_future_t { struct __semaphore_t { unsigned int initialized; int value; 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; } sem_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } sem_cvar; } resultAvailable; void *result; } **futures; struct __lpx_thread_future_t *dispatcher; struct __lpx_pcq_t { int maxSize; struct __mempool_fixed_t { union pthread_mutex_t *poolMutex; void *pool; void *freeList; long poolSize; long storedObjectSize; int magic; } pool; struct __semaphore_t dqSem; struct __semaphore_t nqSem; union pthread_mutex_t qMutex; struct __lpx_pcq_node_t { void *data; struct __lpx_pcq_node_t *next; struct __lpx_pcq_node_t *prev; } *head; struct __lpx_pcq_node_t *tail; } requestQueue; unsigned short port; int listener; struct __lpx_threadpool_t { int minThreads; int maxThreads; int numAlive; struct __Thread { unsigned long tid; int index; struct __semaphore_t workAvailable; struct __WorkItem { void *(*callback)(void *); void *param; struct __lpx_thread_future_t *future; } *workItem; struct __lpx_threadpool_t *parent; } **threads; char *availability; union pthread_mutex_t avlblMutex; struct __semaphore_t threadCounter; } *workers; struct __mempool_fixed_t cDataPool; void *workerData; int (*handler)(struct __lpx_connection_data_t { 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]; } peer; int fd; struct timespec { long tv_sec; long tv_nsec; } timestamp; long age_milliseconds; } *, int); } *

variable_types: i int

variable_types:
shutdownRequest
void *

variable_types:
retval
void *

user_defined_types: struct __lpx_tcpserver_t { int numWorkers; struct __lpx_thread_future_t **futures; struct __lpx_thread_future_t *dispatcher; struct __lpx_pcq_t requestQueue; unsigned short port; int listener; struct __lpx_threadpool_t *workers; struct __mempool_fixed_t cDataPool; void *workerData; int (*handler)(struct __lpx_connection_data_t *, int); }

user_defined_types: struct __lpx_pcq_t { int maxSize; struct __mempool_fixed_t pool; struct __semaphore_t dqSem; struct __semaphore_t nqSem; union pthread_mutex_t qMutex; struct __lpx_pcq_node_t *head; struct __lpx_pcq_node_t *tail; }

user_defined_types: struct __lpx_threadpool_t { int minThreads; int maxThreads; int numAlive; struct __Thread **threads; char *availability; union pthread_mutex_t avlblMutex; struct __semaphore_t threadCounter; }

user_defined_types: struct __lpx_thread_future_t { struct __semaphore_t resultAvailable; void *result; }

user_defined_types: struct __semaphore_t { unsigned int initialized; int value; union pthread_mutex_t sem_mutex; union pthread_cond_t sem_cvar; }

user_defined_types: struct __lpx_connection_data_t { struct sockaddr_in peer; int fd; struct timespec timestamp; long age_milliseconds; }

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

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

user_defined_types: struct __Thread { unsigned long tid; int index; struct __semaphore_t workAvailable; struct __WorkItem *workItem; struct __lpx_threadpool_t *parent; }

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 __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


dispatcher

decompiled_code:
__int64 __fastcall dispatcher(__int64 a1)
{
__int64 v1; // r12
__int64 v3; // rax
__int64 v4; // rbp
int v5; // edi
socklen_t addr_len[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1 + 344;
addr_len[0] = 0;
while ( !shutDownServer )
{
v3 = lpx_mempool_fixed_alloc(v1);
*(_OWORD *)v3 = 0LL;
v4 = v3;
*(_OWORD *)(v3 + 16) = 0LL;
*(_OWORD *)(v3 + 32) = 0LL;
v5 = *(_DWORD *)(a1 + 332);
addr_len[0] = 16;
*(_DWORD *)(v3 + 16) = accept(v5, (struct sockaddr *)v3, addr_len);
clock_gettime(0, (struct timespec *)(v4 + 24));
if ( (unsigned int)lpx_pcq_enqueue(a1 + 24, v4) )
lpx_mempool_fixed_free(v4);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r12
__int64 v3; // rax
__int64 v4; // rbp
int v5; // edi
socklen_t addr_len[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = a1 + 344;
addr_len[0] = 0;
while ( !shutDownServer )
{
v3 = lpx_mempool_fixed_alloc(v1);
*(_OWORD *)v3 = 0LL;
v4 = v3;
*(_OWORD *)(v3 + 16) = 0LL;
*(_OWORD *)(v3 + 32) = 0LL;
v5 = *(_DWORD *)(a1 + 332);
addr_len[0] = 16;
*(_DWORD *)(v3 + 16) = accept(v5, (struct sockaddr *)v3, addr_len);
clock_gettime(0, (struct timespec *)(v4 + 24));
if ( (unsigned int)lpx_pcq_enqueue(a1 + 24, v4) )
lpx_mempool_fixed_free(v4);
}
return 0LL;
}

original_code:
static void *dispatcher(void *param)
{
    lpx_tcpserver_t *server = (lpx_tcpserver_t *)param;
    lpx_connection_data_t *request = ((void *)0);
    socklen_t addrLen = 0;
    while(!shutDownServer) {
        request = lpx_mempool_fixed_alloc(&server->cDataPool);
 memset(request, 0, sizeof(lpx_connection_data_t));
        addrLen = sizeof(struct sockaddr_in);
 request->fd = accept(server->listener, (void *)&request->peer, &addrLen);
 clock_gettime(0, &request->timestamp);
 if (0 != lpx_pcq_enqueue(&server->requestQueue, request)) {
     lpx_mempool_fixed_free(request);
 }
    }
    return ((void *)0);
}

canonical_original_code:
static void *dispatcher(void *param)
{
    struct __lpx_tcpserver_t *server = (lpx_tcpserver_t *)param;
    struct __lpx_connection_data_t *request = ((void *)0);
    unsigned int addrLen = 0;
    while(!shutDownServer) {
        request = lpx_mempool_fixed_alloc(&server->cDataPool);
 memset(request, 0, sizeof(lpx_connection_data_t));
        addrLen = sizeof(struct sockaddr_in);
 request->fd = accept(server->listener, (void *)&request->peer, &addrLen);
 clock_gettime(0, &request->timestamp);
 if (0 != lpx_pcq_enqueue(&server->requestQueue, request)) {
     lpx_mempool_fixed_free(request);
 }
    }
    return ((void *)0);
}

variable_types:
param
void *

variable_types: struct __lpx_connection_data_t { 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]; } peer; int fd; struct timespec { long tv_sec; long tv_nsec; } timestamp; long age_milliseconds; } *

variable_types:
addrLen
unsigned int

return_type: void *

user_defined_types: struct __mempool_fixed_t { union pthread_mutex_t *poolMutex; void *pool; void *freeList; long poolSize; long storedObjectSize; int magic; }

user_defined_types: struct __lpx_pcq_node_t { void *data; struct __lpx_pcq_node_t *next; struct __lpx_pcq_node_t *prev; }

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

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

user_defined_types: struct __WorkItem { void *(*callback)(void *); void *param; struct __lpx_thread_future_t *future; }


releaseServerResources

decompiled_code:
__int64 __fastcall releaseServerResources(__int64 a1)
{
lpx_threadpool_destroy(*(_QWORD *)(a1 + 336));
lpx_mempool_destroy_fixed_pool(a1 + 344);
return lpx_pcq_destroy(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
lpx_threadpool_destroy(*(_QWORD *)(a1 + 336));
lpx_mempool_destroy_fixed_pool(a1 + 344);
return lpx_pcq_destroy(a1 + 24);
}

original_code:
static void releaseServerResources(lpx_tcpserver_t *server)
{
    lpx_threadpool_destroy(server->workers);
    lpx_mempool_destroy_fixed_pool(&server->cDataPool);
    lpx_pcq_destroy(&server->requestQueue);
}

canonical_original_code:
static void releaseServerResources(struct __lpx_tcpserver_t *server)
{
    lpx_threadpool_destroy(server->workers);
    lpx_mempool_destroy_fixed_pool(&server->cDataPool);
    lpx_pcq_destroy(&server->requestQueue);
}

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


decompiled_code:
__int64 __fastcall lpx_tcpserver_init(__int64 a1, __int16 a2, int a3, int a4, __int64 a5)
{
__int64 v7; // rbx
void *v8; // rax
_QWORD *v9; // rax
_QWORD *v10; // r14
__int64 v11; // rax
__int64 v12; // rax
_QWORD *v13; // rbp
__int64 v14; // rbx
__int64 result; // rax
int v16; // [rsp+4h] [rbp-44h]

v16 = a4;
if ( a1 )
{
if ( a5 )
{
if ( a4 > 0 && a3 > 0 )
{
*(_QWORD *)(a1 + 400) = a5;
v7 = a3;
*(_WORD *)(a1 + 328) = a2;
*(_DWORD *)a1 = a3;
v8 = malloc(8LL * a3);
*(_QWORD *)(a1 + 8) = v8;
if ( v8 )
{
v9 = malloc(16 * v7);
v10 = v9;
if ( v9 )
{
*(_QWORD *)(a1 + 392) = v9;
v11 = lpx_threadpool_init((unsigned int)(a3 + 1), (unsigned int)(a3 + 1), 0LL);
*(_QWORD *)(a1 + 336) = v11;
if ( v11 )
{
if ( !(unsigned int)lpx_pcq_init(a1 + 24, (unsigned int)a4) )
{
v12 = *(_QWORD *)(a1 + 8);
v13 = v10;
v14 = 0LL;
while ( 1 )
{
*(_DWORD *)v13 = v14;
v13[1] = a1;
*(_QWORD *)(v12 + 8 * v14) = lpx_threadpool_execute(*(_QWORD *)(a1 + 336), worker, v13);
v12 = *(_QWORD *)(a1 + 8);
if ( !*(_QWORD *)(v12 + 8 * v14) )
break;
++v14;
v13 += 2;
if ( a3 <= (int)v14 )
{
result = lpx_mempool_create_fixed_pool(a1 + 344, 48LL, (unsigned int)(a3 + v16), 2LL);
if ( !(_DWORD)result )
return result;
break;
}
}
destroyWorkers((unsigned int *)a1);
lpx_threadpool_destroy(*(_QWORD *)(a1 + 336));
}
lpx_pcq_destroy(a1 + 24);
}
free(v10);
}
free(*(void **)(a1 + 8));
}
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int16 a2, int a3, int a4, __int64 a5)
{
__int64 v7; // rbx
void *v8; // rax
_QWORD *v9; // rax
_QWORD *v10; // r14
__int64 v11; // rax
__int64 v12; // rax
_QWORD *v13; // rbp
__int64 v14; // rbx
__int64 result; // rax
int v16; // [rsp+4h] [rbp-44h]

v16 = a4;
if ( a1 )
{
if ( a5 )
{
if ( a4 > 0 && a3 > 0 )
{
*(_QWORD *)(a1 + 400) = a5;
v7 = a3;
*(_WORD *)(a1 + 328) = a2;
*(_DWORD *)a1 = a3;
v8 = malloc(8LL * a3);
*(_QWORD *)(a1 + 8) = v8;
if ( v8 )
{
v9 = malloc(16 * v7);
v10 = v9;
if ( v9 )
{
*(_QWORD *)(a1 + 392) = v9;
v11 = lpx_threadpool_init((unsigned int)(a3 + 1), (unsigned int)(a3 + 1), 0LL);
*(_QWORD *)(a1 + 336) = v11;
if ( v11 )
{
if ( !(unsigned int)lpx_pcq_init(a1 + 24, (unsigned int)a4) )
{
v12 = *(_QWORD *)(a1 + 8);
v13 = v10;
v14 = 0LL;
while ( 1 )
{
*(_DWORD *)v13 = v14;
v13[1] = a1;
*(_QWORD *)(v12 + 8 * v14) = lpx_threadpool_execute(*(_QWORD *)(a1 + 336), worker, v13);
v12 = *(_QWORD *)(a1 + 8);
if ( !*(_QWORD *)(v12 + 8 * v14) )
break;
++v14;
v13 += 2;
if ( a3 <= (int)v14 )
{
result = lpx_mempool_create_fixed_pool(a1 + 344, 48LL, (unsigned int)(a3 + v16), 2LL);
if ( !(_DWORD)result )
return result;
break;
}
}
func1((unsigned int *)a1);
lpx_threadpool_destroy(*(_QWORD *)(a1 + 336));
}
lpx_pcq_destroy(a1 + 24);
}
free(v10);
}
free(*(void **)(a1 + 8));
}
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int lpx_tcpserver_init(lpx_tcpserver_t *server,
                       unsigned short port,
         int num_workers,
         int queue_length,
                       int (*handler)(lpx_connection_data_t *request, int worker_index))
{
    int i = 0;
    lpx_tcpworker_data_t *workerData = ((void *)0);
    if (server == ((void *)0) || handler == ((void *)0)) {
        return -1;
    }
    if (queue_length <= 0 || num_workers <= 0) {
        return -1;
    }
    server->handler = handler;
    server->port = port;
    server->numWorkers = num_workers;
    server->futures =
            (lpx_thread_future_t **)malloc(sizeof(lpx_thread_future_t *) * num_workers);
    if (server->futures == ((void *)0)) {
        return -1;
    }
    workerData =
             (lpx_tcpworker_data_t *)malloc(sizeof(lpx_tcpworker_data_t) * num_workers);
    if (((void *)0) == workerData) {
        goto server_destroy1;
    }
    server->workerData = (void *)workerData;
    server->workers =
              lpx_threadpool_init(num_workers + 1, num_workers + 1, THREAD_POOL_FIXED);
    if (server->workers == ((void *)0)) {
        goto server_destroy2;
    }
    if (0 != lpx_pcq_init(&server->requestQueue, queue_length)) {
        goto server_destroy3;
    }
    for (i = 0; i < num_workers; i++) {
        workerData[i].threadNum = i;
 workerData[i].server = server;
 server->futures[i] =
              lpx_threadpool_execute(server->workers, worker, &workerData[i]);
        if (server->futures[i] == ((void *)0)) {
     goto server_destroy4;
 }
    }
    if (0 != lpx_mempool_create_fixed_pool(&server->cDataPool,
                                   sizeof(lpx_connection_data_t),
       queue_length + num_workers, 2)) {
        goto server_destroy4;
    }
    return 0;
server_destroy4: destroyWorkers(server);
                 lpx_threadpool_destroy(server->workers);
server_destroy3: lpx_pcq_destroy(&server->requestQueue);
server_destroy2: free(workerData);
server_destroy1: free(server->futures);
    return -1;
}

canonical_original_code:
int lpx_tcpserver_init(struct __lpx_tcpserver_t *server, unsigned short port, int num_workers, int queue_length, int (*handler)(struct __lpx_connection_data_t *request, int worker_index))
{
    int i = 0;
    struct __lpx_tcpworker_data_t *workerData = ((void *)0);
    if (server == ((void *)0) || handler == ((void *)0)) {
        return -1;
    }
    if (queue_length <= 0 || num_workers <= 0) {
        return -1;
    }
    server->handler = handler;
    server->port = port;
    server->numWorkers = num_workers;
    server->futures =
            (struct __lpx_thread_future_t **)malloc(sizeof(struct __lpx_thread_future_t *) * num_workers);
    if (server->futures == ((void *)0)) {
        return -1;
    }
    workerData =
             (struct __lpx_tcpworker_data_t *)malloc(sizeof(lpx_tcpworker_data_t) * num_workers);
    if (((void *)0) == workerData) {
        goto server_destroy1;
    }
    server->workerData = (void *)workerData;
    server->workers =
              lpx_threadpool_init(num_workers + 1, num_workers + 1, THREAD_POOL_FIXED);
    if (server->workers == ((void *)0)) {
        goto server_destroy2;
    }
    if (0 != lpx_pcq_init(&server->requestQueue, queue_length)) {
        goto server_destroy3;
    }
    for (i = 0; i < num_workers; i++) {
        workerData[i].threadNum = i;
 workerData[i].server = server;
 server->futures[i] =
              lpx_threadpool_execute(server->workers, worker, &workerData[i]);
        if (server->futures[i] == ((void *)0)) {
     goto server_destroy4;
 }
    }
    if (0 != lpx_mempool_create_fixed_pool(&server->cDataPool,
                                   sizeof(lpx_connection_data_t),
       queue_length + num_workers, 2)) {
        goto server_destroy4;
    }
    return 0;
server_destroy4: destroyWorkers(server);
                 lpx_threadpool_destroy(server->workers);
server_destroy3: lpx_pcq_destroy(&server->requestQueue);
server_destroy2: free(workerData);
server_destroy1: free(server->futures);
    return -1;
}

variable_types:
port
unsigned short

variable_types: int (*)(struct __lpx_connection_data_t *request, int worker_index)

variable_types: struct __lpx_tcpworker_data_t { int threadNum; struct __lpx_tcpserver_t { int numWorkers; struct __lpx_thread_future_t { struct __semaphore_t { unsigned int initialized; int value; 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; } sem_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } sem_cvar; } resultAvailable; void *result; } **futures; struct __lpx_thread_future_t *dispatcher; struct __lpx_pcq_t { int maxSize; struct __mempool_fixed_t { union pthread_mutex_t *poolMutex; void *pool; void *freeList; long poolSize; long storedObjectSize; int magic; } pool; struct __semaphore_t dqSem; struct __semaphore_t nqSem; union pthread_mutex_t qMutex; struct __lpx_pcq_node_t { void *data; struct __lpx_pcq_node_t *next; struct __lpx_pcq_node_t *prev; } *head; struct __lpx_pcq_node_t *tail; } requestQueue; unsigned short port; int listener; struct __lpx_threadpool_t { int minThreads; int maxThreads; int numAlive; struct __Thread { unsigned long tid; int index; struct __semaphore_t workAvailable; struct __WorkItem { void *(*callback)(void *); void *param; struct __lpx_thread_future_t *future; } *workItem; struct __lpx_threadpool_t *parent; } **threads; char *availability; union pthread_mutex_t avlblMutex; struct __semaphore_t threadCounter; } *workers; struct __mempool_fixed_t cDataPool; void *workerData; int (*handler)(struct __lpx_connection_data_t { 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]; } peer; int fd; struct timespec { long tv_sec; long tv_nsec; } timestamp; long age_milliseconds; } *, int); } *server; } *

user_defined_types: struct __lpx_tcpserver_t { int numWorkers; struct __lpx_thread_future_t **futures; struct __lpx_thread_future_t *dispatcher; struct __lpx_pcq_t requestQueue; unsigned short port; int listener; struct __lpx_threadpool_t *workers; struct __mempool_fixed_t cDataPool; void *workerData; int (*handler)(struct __lpx_connection_data_t *, int); }

user_defined_types: struct __lpx_tcpworker_data_t { int threadNum; struct __lpx_tcpserver_t *server; }


decompiled_code:
__int64 __fastcall lpx_tcpserver_start(__int64 a1)
{
int v2; // eax
__int16 v3; // di
__int16 v4; // ax
int v5; // edi
socklen_t v6; // edx
unsigned int v7; // er12
__int64 v8; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v11; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
if ( !a1 )
goto LABEL_7;
v2 = socket(2, 1, 0);
*(_DWORD *)(a1 + 332) = v2;
if ( v2 < 0 )
goto LABEL_7;
setsockopt(v2, 1, 2, &optval, 4u);
v3 = *(_WORD *)(a1 + 328);
v11 = 0LL;
v11.sa_family = 2;
v4 = _bswap_16(v3);
v5 = *(_DWORD *)(a1 + 332);
*(_WORD *)v11.sa_data = v4;
v7 = bind(v5, &v11, v6);
if ( v7 || (v8 = lpx_threadpool_execute(*(_QWORD *)(a1 + 336), dispatcher, a1), (*(_QWORD *)(a1 + 16) = v8) == 0LL) )
{
close(*(_DWORD *)(a1 + 332));
LABEL_7:
v7 = -1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v2; // eax
__int16 v3; // di
__int16 v4; // ax
int v5; // edi
socklen_t v6; // edx
unsigned int v7; // er12
__int64 v8; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v11; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
if ( !a1 )
goto LABEL_7;
v2 = socket(2, 1, 0);
*(_DWORD *)(a1 + 332) = v2;
if ( v2 < 0 )
goto LABEL_7;
setsockopt(v2, 1, 2, &optval, 4u);
v3 = *(_WORD *)(a1 + 328);
v11 = 0LL;
v11.sa_family = 2;
v4 = func3(v3);
v5 = *(_DWORD *)(a1 + 332);
*(_WORD *)v11.sa_data = v4;
v7 = bind(v5, &v11, v6);
if ( v7 || (v8 = lpx_threadpool_execute(*(_QWORD *)(a1 + 336), dispatcher, a1), (*(_QWORD *)(a1 + 16) = v8) == 0LL) )
{
close(*(_DWORD *)(a1 + 332));
LABEL_7:
v7 = -1;
}
return v7;
}

original_code:
int lpx_tcpserver_start(lpx_tcpserver_t *server)
{
    struct sockaddr_in serverAddr;
    int reuseAddr = 1;
    if (server == ((void *)0)) {
        return -1;
    }
    server->listener = socket(2, SOCK_STREAM, 0);
    if (server->listener < 0) {
        return -1;
    }
    setsockopt(server->listener, 1, 2, &reuseAddr, sizeof(int));
    memset(&serverAddr, 0, sizeof(struct sockaddr_in));
    serverAddr.sin_family = 2;
    serverAddr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    serverAddr.sin_port = htons(server->port);
    if (bind(server->listener, (struct sockaddr *)&serverAddr,
                                            sizeof(serverAddr)) != 0) {
        goto destroy_start1;
    }
    server->dispatcher = lpx_threadpool_execute(server->workers, dispatcher, server);
    if (server->dispatcher == ((void *)0)) {
        goto destroy_start1;
    }
    return 0;
destroy_start1: close(server->listener);
    return -1;
}

canonical_original_code:
int lpx_tcpserver_start(struct __lpx_tcpserver_t *server)
{
    struct sockaddr_in serverAddr;
    int reuseAddr = 1;
    if (server == ((void *)0)) {
        return -1;
    }
    server->listener = socket(2, SOCK_STREAM, 0);
    if (server->listener < 0) {
        return -1;
    }
    setsockopt(server->listener, 1, 2, &reuseAddr, sizeof(int));
    memset(&serverAddr, 0, sizeof(struct sockaddr_in));
    serverAddr.sin_family = 2;
    serverAddr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    serverAddr.sin_port = htons(server->port);
    if (bind(server->listener, (struct sockaddr *)&serverAddr,
                                            sizeof(serverAddr)) != 0) {
        goto destroy_start1;
    }
    server->dispatcher = lpx_threadpool_execute(server->workers, dispatcher, server);
    if (server->dispatcher == ((void *)0)) {
        goto destroy_start1;
    }
    return 0;
destroy_start1: close(server->listener);
    return -1;
}

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

variable_types: reuseAddr int

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

user_defined_types: struct __lpx_pcq_t { int maxSize; struct __mempool_fixed_t pool; struct __semaphore_t dqSem; struct __semaphore_t nqSem; union pthread_mutex_t qMutex; struct __lpx_pcq_node_t *head; struct __lpx_pcq_node_t *tail; }

user_defined_types: struct __lpx_threadpool_t { int minThreads; int maxThreads; int numAlive; struct __Thread **threads; char *availability; union pthread_mutex_t avlblMutex; struct __semaphore_t threadCounter; }


decompiled_code:
__int64 __fastcall lpx_tcpserver_clean_shutdown(unsigned int *a1)
{
destroyWorkers(a1);
releaseServerResources((__int64)a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1)
{
func1(a1);
func4((__int64)a1);
return 0LL;
}

original_code:
int lpx_tcpserver_clean_shutdown(lpx_tcpserver_t *server)
{
    destroyWorkers(server);
    releaseServerResources(server);
    return 0;
}

canonical_original_code:
int lpx_tcpserver_clean_shutdown(struct __lpx_tcpserver_t *server)
{
    destroyWorkers(server);
    releaseServerResources(server);
    return 0;
}

user_defined_types: struct __lpx_thread_future_t { struct __semaphore_t resultAvailable; void *result; }

user_defined_types: struct __semaphore_t { unsigned int initialized; int value; union pthread_mutex_t sem_mutex; union pthread_cond_t sem_cvar; }

user_defined_types: struct __lpx_connection_data_t { struct sockaddr_in peer; int fd; struct timespec timestamp; long age_milliseconds; }


worker

decompiled_code:
__int64 worker()
{
__int64 v0; // rbp
__int64 v1; // rax
__int64 v2; // rdi
__int64 v4; // [rsp+8h] [rbp-40h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

while ( 1 )
{
do
v4 = 0LL;
while ( (unsigned int)lpx_pcq_dequeue(loc_8 + 24LL, &v4) );
if ( !v4 )
break;
clock_gettime(0, &tp);
v0 = v4;
v1 = timespecDiffMillis(tp.tv_sec, tp.tv_nsec, *(_QWORD *)(v4 + 24), *(_QWORD *)(v4 + 32));
v2 = v4;
*(_QWORD *)(v0 + 40) = v1;
(*(void (__fastcall **)(__int64, _QWORD))(loc_8 + 400LL))(v2, *(unsigned int *)worker);
close(*(_DWORD *)(v4 + 16));
lpx_mempool_fixed_free(v4);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbp
__int64 v1; // rax
__int64 v2; // rdi
__int64 v4; // [rsp+8h] [rbp-40h] BYREF
struct timespec tp; // [rsp+10h] [rbp-38h] BYREF

while ( 1 )
{
do
v4 = 0LL;
while ( (unsigned int)lpx_pcq_dequeue(loc_8 + 24LL, &v4) );
if ( !v4 )
break;
clock_gettime(0, &tp);
v0 = v4;
v1 = timespecDiffMillis(tp.tv_sec, tp.tv_nsec, *(_QWORD *)(v4 + 24), *(_QWORD *)(v4 + 32));
v2 = v4;
*(_QWORD *)(v0 + 40) = v1;
(*(void (__fastcall **)(__int64, _QWORD))(loc_8 + 400LL))(v2, *(unsigned int *)worker);
close(*(_DWORD *)(v4 + 16));
lpx_mempool_fixed_free(v4);
}
return 0LL;
}

original_code:
static void *worker(void *param)
{
    lpx_tcpworker_data_t *data = (lpx_tcpworker_data_t *)data;
    lpx_tcpserver_t *parent = data->server;
    int threadNumber = data->threadNum;
    lpx_connection_data_t *request = ((void *)0);
    struct timespec now;
    while(1) {
        request = ((void *)0);
 if (0 == lpx_pcq_dequeue(&parent->requestQueue, (void **)&request)) {
     if (request == ((void *)0)) {
         break;
            }
     clock_gettime(0, &now);
     request->age_milliseconds = timespecDiffMillis(now, request->timestamp);
     parent->handler(request, threadNumber);
     close(request->fd);
     lpx_mempool_fixed_free(request);
 }
    }
    return ((void *)0);
}

canonical_original_code:
static void *worker(void *param)
{
    struct __lpx_tcpworker_data_t *data = (lpx_tcpworker_data_t *)data;
    struct __lpx_tcpserver_t *parent = data->server;
    int threadNumber = data->threadNum;
    struct __lpx_connection_data_t *request = ((void *)0);
    struct timespec now;
    while(1) {
        request = ((void *)0);
 if (0 == lpx_pcq_dequeue(&parent->requestQueue, (void **)&request)) {
     if (request == ((void *)0)) {
         break;
            }
     clock_gettime(0, &now);
     request->age_milliseconds = timespecDiffMillis(now, request->timestamp);
     parent->handler(request, threadNumber);
     close(request->fd);
     lpx_mempool_fixed_free(request);
 }
    }
    return ((void *)0);
}

variable_types: threadNumber int

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

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

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

user_defined_types: struct __Thread { unsigned long tid; int index; struct __semaphore_t workAvailable; struct __WorkItem *workItem; struct __lpx_threadpool_t *parent; }


splitBlock

decompiled_code:
unsigned __int64 *__fastcall splitBlock(__int64 a1, unsigned __int64 *a2, unsigned __int64 *a3)
{
unsigned __int64 v3; // r8
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rax
unsigned __int64 v6; // rcx
unsigned __int64 v7; // rax
unsigned __int64 *result; // rax

v3 = *a2;
v4 = *a3;
v5 = *a2 - *a3;
if ( v5 > 0x1F && v3 != v4 )
{
*a2 = v5;
result = (unsigned __int64 *)((char *)a2 + v5);
goto LABEL_7;
}
v6 = a2[1];
v7 = a2[2];
if ( v6 )
{
*(_QWORD *)(v6 + 16) = v7;
if ( v7 )
{
*(_QWORD *)(v7 + 8) = v6;
v4 = v3;
result = a2;
LABEL_7:
*a3 = v4;
return result;
}
}
else
{
*(_QWORD *)(a1 + 24) = v7;
if ( v7 )
{
*(_QWORD *)(v7 + 8) = 0LL;
v4 = v3;
result = a2;
goto LABEL_7;
}
}
result = a2;
*a3 = v3;
return result;
}

canonical_decompiled_code:
unsigned __int64 *__fastcall func0(__int64 a1, unsigned __int64 *a2, unsigned __int64 *a3)
{
unsigned __int64 v3; // r8
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rax
unsigned __int64 v6; // rcx
unsigned __int64 v7; // rax
unsigned __int64 *result; // rax

v3 = *a2;
v4 = *a3;
v5 = *a2 - *a3;
if ( v5 > 0x1F && v3 != v4 )
{
*a2 = v5;
result = (unsigned __int64 *)((char *)a2 + v5);
goto LABEL_7;
}
v6 = a2[1];
v7 = a2[2];
if ( v6 )
{
*(_QWORD *)(v6 + 16) = v7;
if ( v7 )
{
*(_QWORD *)(v7 + 8) = v6;
v4 = v3;
result = a2;
LABEL_7:
*a3 = v4;
return result;
}
}
else
{
*(_QWORD *)(a1 + 24) = v7;
if ( v7 )
{
*(_QWORD *)(v7 + 8) = 0LL;
v4 = v3;
result = a2;
goto LABEL_7;
}
}
result = a2;
*a3 = v3;
return result;
}

original_code:
static void *splitBlock(lpx_mempool_variable_t *pool, void *addr, long *requestSize)
{
    void *allocatedBlock = ((void *)0);
    long *blockMetadata= (long *)addr;
    long blockSize = blockMetadata[0];
    long *prevBlock = ((void *)0);
    long *nextBlock = ((void *)0);
    long size = *requestSize;
    if ((blockSize - size) < (4 * sizeof(long))) {
        size = blockSize;
    }
    if (blockSize == size) {
 allocatedBlock = addr;
        prevBlock = (long *)blockMetadata[1];
        nextBlock = (long *)blockMetadata[2];
 if (prevBlock != ((void *)0)) {
     prevBlock[2] = (long)nextBlock;
     if (nextBlock != ((void *)0)) {
         nextBlock[1] = (long)prevBlock;
     }
 } else {
     pool->freeList = (void *)nextBlock;
     if (nextBlock != ((void *)0)) {
         nextBlock[1] = 0;
     }
 }
    } else {
 blockMetadata[0] = blockSize - size;
 allocatedBlock = (void *)((long)addr + (blockSize - size));
    }
    *requestSize = size;
    return allocatedBlock;
}

canonical_original_code:
static void *splitBlock(struct __mempool_variable_t *pool, void *addr, long *requestSize)
{
    void *allocatedBlock = ((void *)0);
    long *blockMetadata = (long *)addr;
    long blockSize = blockMetadata[0];
    long *prevBlock = ((void *)0);
    long *nextBlock = ((void *)0);
    long size = *requestSize;
    if ((blockSize - size) < (4 * sizeof(long))) {
        size = blockSize;
    }
    if (blockSize == size) {
 allocatedBlock = addr;
        prevBlock = (long *)blockMetadata[1];
        nextBlock = (long *)blockMetadata[2];
 if (prevBlock != ((void *)0)) {
     prevBlock[2] = (long)nextBlock;
     if (nextBlock != ((void *)0)) {
         nextBlock[1] = (long)prevBlock;
     }
 } else {
     pool->freeList = (void *)nextBlock;
     if (nextBlock != ((void *)0)) {
         nextBlock[1] = 0;
     }
 }
    } else {
 blockMetadata[0] = blockSize - size;
 allocatedBlock = (void *)((long)addr + (blockSize - size));
    }
    *requestSize = size;
    return allocatedBlock;
}

variable_types: struct __mempool_variable_t { 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; } *poolMutex; void *pool; long poolSize; void *freeList; int magic; } *

variable_types:
addr
void *

variable_types:
requestSize
long *

variable_types:
allocatedBlock
void *

variable_types:
blockMetadata
long *

variable_types: blockSize long

variable_types:
prevBlock
long *

variable_types:
nextBlock
long *

variable_types: size long

user_defined_types: struct __mempool_variable_t { union pthread_mutex_t *poolMutex; void *pool; long poolSize; void *freeList; int magic; }

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


insertAfter

decompiled_code:
__int64 __fastcall insertAfter(__int64 a1, __int64 a2)
{
__int64 result; // rax

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

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

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

original_code:
static void insertAfter(long *prev, long *blockToInsert)
{
    long *next = (long *)prev[2];
    prev[2] = (long)blockToInsert;
    blockToInsert[1] = (long)prev;
    blockToInsert[2] = (long)next;
    if (next != ((void *)0)) {
        next[1] = (long)blockToInsert;
    }
    return;
}

variable_types:
prev
long *

variable_types:
blockToInsert
long *

variable_types:
next
long *


insertBefore

decompiled_code:
__int64 __fastcall insertBefore(__int64 a1, __int64 a2)
{
__int64 result; // rax

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

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

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

original_code:
static void insertBefore(long *after, long *blockToInsert)
{
    long *prev = (long *)after[1];
    prev[2] = (long)blockToInsert;
    blockToInsert[1] = (long)prev;
    after[1] = (long)blockToInsert;
    blockToInsert[2] = (long)after;
    return;
}

variable_types:
after
long *


coalesceBlocks

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

if ( a3 )
result = coalesce(a2, a3);
if ( a1 )
result = coalesce(a1, a2);
return result;
}

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

if ( a3 )
result = func3(a2, a3);
if ( a1 )
result = func3(a1, a2);
return result;
}

original_code:
static void coalesceBlocks(long *prev, long *current, long *next)
{
    if (next != ((void *)0)) {
        coalesce(current, next);
    }
    if (prev != ((void *)0)) {
        coalesce(prev, current);
    }
    return;
}

variable_types:
current
long *


insertIntoFreeList

decompiled_code:
__int64 __fastcall insertIntoFreeList(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rdx
__int64 v4; // rdi
unsigned __int64 v5; // rax

v2 = *(_QWORD *)(a1 + 24);
if ( !v2 )
{
*(_QWORD *)(a2 + 16) = 0LL;
LABEL_10:
*(_QWORD *)(a1 + 24) = a2;
*(_QWORD *)(a2 + 8) = 0LL;
return coalesceBlocks(*(__int64 **)(a2 + 8), (__int64 *)a2, *(_QWORD **)(a2 + 16));
}
if ( v2 > a2 )
v2 = a2;
v4 = v2;
if ( a2 > v2 )
{
while ( 1 )
{
v5 = *(_QWORD *)(v4 + 16);
if ( !v5 )
break;
v4 = *(_QWORD *)(v4 + 16);
if ( a2 <= v5 )
goto LABEL_12;
}
insertAfter(v4, a2);
}
else
{
LABEL_12:
insertBefore(v4, a2);
}
if ( *(_QWORD *)(a1 + 24) != a2 )
goto LABEL_10;
return coalesceBlocks(*(__int64 **)(a2 + 8), (__int64 *)a2, *(_QWORD **)(a2 + 16));
}

original_code:
static void insertIntoFreeList(lpx_mempool_variable_t *pool, void *addr)
{
    long *listHead = (long *)pool->freeList;
    long *node = (long *)pool->freeList;
    long *prev = ((void *)0);
    long *current = (long *)addr;
    long *next = ((void *)0);
    int insertAfterInstead = 0;
    if (listHead == ((void *)0)) {
 current[2] = 0;
 listHead = current;
    } else {
 listHead = (listHead > current) ? current : listHead;
 node = listHead;
        while (node < current) {
    if (node[2] == 0) {
        insertAfterInstead = 1;
        break;
    }
    node = (long *)(node[2]);
        }
 if (insertAfterInstead) {
     insertAfter(node, current);
 } else {
     insertBefore(node, current);
 }
    }
    if (pool->freeList != listHead) {
       pool->freeList = (void *)listHead;
       listHead[1] = 0;
    }
    prev = (long *)current[1];
    next = (long *)current[2];
    coalesceBlocks(prev, current, next);
    return;
}

canonical_original_code:
static void insertIntoFreeList(struct __mempool_variable_t *pool, void *addr)
{
    long *listHead = (long *)pool->freeList;
    long *node = (long *)pool->freeList;
    long *prev = ((void *)0);
    long *current = (long *)addr;
    long *next = ((void *)0);
    int insertAfterInstead = 0;
    if (listHead == ((void *)0)) {
 current[2] = 0;
 listHead = current;
    } else {
 listHead = (listHead > current) ? current : listHead;
 node = listHead;
        while (node < current) {
    if (node[2] == 0) {
        insertAfterInstead = 1;
        break;
    }
    node = (long *)(node[2]);
        }
 if (insertAfterInstead) {
     insertAfter(node, current);
 } else {
     insertBefore(node, current);
 }
    }
    if (pool->freeList != listHead) {
       pool->freeList = (void *)listHead;
       listHead[1] = 0;
    }
    prev = (long *)current[1];
    next = (long *)current[2];
    coalesceBlocks(prev, current, next);
    return;
}

variable_types:
listHead
long *

variable_types:
node
long *

variable_types: insertAfterInstead int


decompiled_code:
__int64 __fastcall lpx_mempool_create_fixed_pool_from_block(__int64 a1, __int64 a2, int a3, unsigned __int64 a4, int a5, _QWORD *a6)
{
__int64 v8; // rbx
signed __int64 v11; // rax
_QWORD *v12; // rdx
int v13; // eax
_QWORD *v14; // rbx
__int64 result; // rax
pthread_mutex_t *v16; // rdi
pthread_mutex_t *v17; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a6 )
return 0xFFFFFFFFLL;
if ( a2 <= 0 )
return 0xFFFFFFFFLL;
if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v8 = a2 + 8;
if ( a2 + 8 > a4 )
return 0xFFFFFFFFLL;
if ( a5 == 2 )
{
v17 = (pthread_mutex_t *)malloc(0x28uLL);
*(_QWORD *)a1 = v17;
if ( !v17 )
return 0xFFFFFFFFLL;
if ( pthread_mutex_init(v17, 0LL) )
goto LABEL_16;
}
else
{
*(_QWORD *)a1 = 0LL;
}
*(_QWORD *)(a1 + 32) = v8;
v11 = v8 * a3;
*(_QWORD *)(a1 + 24) = v11;
if ( v11 > (__int64)a4 )
{
v16 = *(pthread_mutex_t **)a1;
if ( !*(_QWORD *)a1 )
{
LABEL_17:
free(v16);
return 0xFFFFFFFFLL;
}
pthread_mutex_destroy(v16);
LABEL_16:
v16 = *(pthread_mutex_t **)a1;
goto LABEL_17;
}
*(_QWORD *)(a1 + 8) = a6;
if ( a3 == 1 )
{
v14 = a6;
}
else
{
v12 = a6;
v13 = 0;
do
{
++v13;
*v12 = (char *)v12 + v8;
v12 = (_QWORD *)((char *)v12 + v8);
}
while ( a3 - 1 != v13 );
v14 = (_QWORD *)((char *)a6 + v8 + v8 * (unsigned int)(a3 - 2));
}
*v14 = 0LL;
result = 0LL;
*(_QWORD *)(a1 + 16) = a6;
*(_DWORD *)(a1 + 40) = -557125341;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3, unsigned __int64 a4, int a5, _QWORD *a6)
{
__int64 v8; // rbx
signed __int64 v11; // rax
_QWORD *v12; // rdx
int v13; // eax
_QWORD *v14; // rbx
__int64 result; // rax
pthread_mutex_t *v16; // rdi
pthread_mutex_t *v17; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a6 )
return 0xFFFFFFFFLL;
if ( a2 <= 0 )
return 0xFFFFFFFFLL;
if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v8 = a2 + 8;
if ( a2 + 8 > a4 )
return 0xFFFFFFFFLL;
if ( a5 == 2 )
{
v17 = (pthread_mutex_t *)malloc(0x28uLL);
*(_QWORD *)a1 = v17;
if ( !v17 )
return 0xFFFFFFFFLL;
if ( pthread_mutex_init(v17, 0LL) )
goto LABEL_16;
}
else
{
*(_QWORD *)a1 = 0LL;
}
*(_QWORD *)(a1 + 32) = v8;
v11 = v8 * a3;
*(_QWORD *)(a1 + 24) = v11;
if ( v11 > (__int64)a4 )
{
v16 = *(pthread_mutex_t **)a1;
if ( !*(_QWORD *)a1 )
{
LABEL_17:
free(v16);
return 0xFFFFFFFFLL;
}
pthread_mutex_destroy(v16);
LABEL_16:
v16 = *(pthread_mutex_t **)a1;
goto LABEL_17;
}
*(_QWORD *)(a1 + 8) = a6;
if ( a3 == 1 )
{
v14 = a6;
}
else
{
v12 = a6;
v13 = 0;
do
{
++v13;
*v12 = (char *)v12 + v8;
v12 = (_QWORD *)((char *)v12 + v8);
}
while ( a3 - 1 != v13 );
v14 = (_QWORD *)((char *)a6 + v8 + v8 * (unsigned int)(a3 - 2));
}
*v14 = 0LL;
result = 0LL;
*(_QWORD *)(a1 + 16) = a6;
*(_DWORD *)(a1 + 40) = -557125341;
return result;
}

original_code:
int lpx_mempool_create_fixed_pool_from_block(lpx_mempool_fixed_t *pool,
                                             long objectSize,
          int numObjects,
                                             long size,
                                             int isProtected,
          void *base)
{
    long *currentBlockHeader = ((void *)0);
    long storedObjectSize = 0;
    int i = 0;
    if (__builtin_expect((pool == ((void *)0) || base == ((void *)0) || objectSize <= 0 || numObjects <= 0 || size < objectSize + (sizeof (void *))), 0)) {
        return -1;
    }
    if (isProtected == 2) {
        pool->poolMutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
        if (pool->poolMutex == ((void *)0)) {
            return -1;
        }
        if (0 != pthread_mutex_init(pool->poolMutex, ((void *)0))) {
     goto cfbpool_destroy1;
 }
    } else {
        pool->poolMutex = ((void *)0);
    }
    pool->storedObjectSize = objectSize + (sizeof (void *));
    pool->poolSize = pool->storedObjectSize * numObjects;
    if (size < pool->poolSize) {
        goto cfbpool_destroy2;
    }
    pool->pool = base;
    storedObjectSize = pool->storedObjectSize;
    currentBlockHeader = (long *)pool->pool;
    for (i = 0; i < numObjects - 1; i++) {
        *currentBlockHeader = (long)currentBlockHeader + storedObjectSize;
 currentBlockHeader = (long *)*currentBlockHeader;
    }
    *currentBlockHeader = 0;
    pool->freeList = pool->pool;
    pool->magic = 0xdecaf123;
    return 0;
cfbpool_destroy2: if (pool->poolMutex != ((void *)0)) { pthread_mutex_destroy(pool->poolMutex); }
cfbpool_destroy1: free(pool->poolMutex);
    return -1;
}

canonical_original_code:
int lpx_mempool_create_fixed_pool_from_block(struct __mempool_fixed_t *pool, long objectSize, int numObjects, long size, int isProtected, void *base)
{
    long *currentBlockHeader = ((void *)0);
    long storedObjectSize = 0;
    int i = 0;
    if (__builtin_expect((pool == ((void *)0) || base == ((void *)0) || objectSize <= 0 || numObjects <= 0 || size < objectSize + (sizeof (void *))), 0)) {
        return -1;
    }
    if (isProtected == 2) {
        pool->poolMutex = (union pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
        if (pool->poolMutex == ((void *)0)) {
            return -1;
        }
        if (0 != pthread_mutex_init(pool->poolMutex, ((void *)0))) {
     goto cfbpool_destroy1;
 }
    } else {
        pool->poolMutex = ((void *)0);
    }
    pool->storedObjectSize = objectSize + (sizeof (void *));
    pool->poolSize = pool->storedObjectSize * numObjects;
    if (size < pool->poolSize) {
        goto cfbpool_destroy2;
    }
    pool->pool = base;
    storedObjectSize = pool->storedObjectSize;
    currentBlockHeader = (long *)pool->pool;
    for (i = 0; i < numObjects - 1; i++) {
        *currentBlockHeader = (long)currentBlockHeader + storedObjectSize;
 currentBlockHeader = (long *)*currentBlockHeader;
    }
    *currentBlockHeader = 0;
    pool->freeList = pool->pool;
    pool->magic = 0xdecaf123;
    return 0;
cfbpool_destroy2: if (pool->poolMutex != ((void *)0)) { pthread_mutex_destroy(pool->poolMutex); }
cfbpool_destroy1: free(pool->poolMutex);
    return -1;
}

variable_types: struct __mempool_fixed_t { 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; } *poolMutex; void *pool; void *freeList; long poolSize; long storedObjectSize; int magic; } *

variable_types: objectSize long

variable_types: numObjects int

variable_types: isProtected int

variable_types:
base
void *

variable_types:
currentBlockHeader
long *

variable_types: storedObjectSize long

variable_types: i int

user_defined_types: struct __mempool_fixed_t { union pthread_mutex_t *poolMutex; void *pool; void *freeList; long poolSize; long storedObjectSize; int magic; }