getImage

decompiled_code:
int getImage()
{
int v0; // ebx
int v1; // eax
char v2; // al
__int64 v3; // rbp
int result; // eax
int v5; // ebx

*buf = -73;
buf[1] = 0;
if ( write(s, buf, 2uLL) <= 1 )
printf("Bluetooth sending failed");
v0 = 0;
while ( 1 )
{
v1 = read(s, &buf[v0], 3203 - v0);
if ( v1 <= 0 )
return printf("Bluetooth receiving failed, tmp=%i", (unsigned int)v1);
v0 += v1;
if ( v0 > 3202 )
{
v2 = *buf;
if ( *buf != 1 || buf[1] != 40 || buf[2] != 40 )
{
puts("Weird epuck camera settings. Reset epuck!");
v2 = *buf;
}
v3 = 3LL;
printf("buf[0]: %i\n", (unsigned int)v2);
printf("buf[1]: %i\n", (unsigned int)buf[1]);
printf("buf[2]: %i\n", (unsigned int)buf[2]);
system("clear");
while ( 1 )
{
v5 = (unsigned __int8)(8 * buf[v3 + 1])
+ ((buf[v3 + 1] >> 3) & 0x1C | (unsigned __int8)(32 * buf[v3]))
+ (buf[v3] & 0xF8);
if ( __ROR4__(-858993459 * (v3 - 31) + 429496720, 4) <= 0x3333332u )
putchar(10);
if ( v5 <= 179 )
break;
if ( v5 <= 419 )
{
result = putchar(43);
LABEL_11:
v3 += 2LL;
if ( v3 == 3203 )
return result;
}
else
{
v3 += 2LL;
result = putchar(46);
if ( v3 == 3203 )
return result;
}
}
result = putchar(36);
goto LABEL_11;
}
}
}

canonical_decompiled_code:
int func9()
{
int v0; // ebx
int v1; // eax
char v2; // al
__int64 v3; // rbp
int result; // eax
int v5; // ebx

*buf = -73;
buf[1] = 0;
if ( write(s, buf, 2uLL) <= 1 )
printf("Bluetooth sending failed");
v0 = 0;
while ( 1 )
{
v1 = read(s, &buf[v0], 3203 - v0);
if ( v1 <= 0 )
return printf("Bluetooth receiving failed, tmp=%i", (unsigned int)v1);
v0 += v1;
if ( v0 > 3202 )
{
v2 = *buf;
if ( *buf != 1 || buf[1] != 40 || buf[2] != 40 )
{
puts("Weird epuck camera settings. Reset epuck!");
v2 = *buf;
}
v3 = 3LL;
printf("buf[0]: %i\n", (unsigned int)v2);
printf("buf[1]: %i\n", (unsigned int)buf[1]);
printf("buf[2]: %i\n", (unsigned int)buf[2]);
system("clear");
while ( 1 )
{
v5 = (unsigned __int8)(8 * buf[v3 + 1])
+ ((buf[v3 + 1] >> 3) & 0x1C | (unsigned __int8)(32 * buf[v3]))
+ (buf[v3] & 0xF8);
if ( __ROR4__(-858993459 * (v3 - 31) + 429496720, 4) <= 0x3333332u )
putchar(10);
if ( v5 <= 179 )
break;
if ( v5 <= 419 )
{
result = putchar(43);
LABEL_11:
v3 += 2LL;
if ( v3 == 3203 )
return result;
}
else
{
v3 += 2LL;
result = putchar(46);
if ( v3 == 3203 )
return result;
}
}
result = putchar(36);
goto LABEL_11;
}
}
}

original_code:
void getImage(){
 buf[0] = -'I';
 buf[1] = '\0';
 if (write(s, buf, 2) < 2)
  printf("Bluetooth sending failed");
 int l=0;
 int tmp;
 for (l=0; l < 3203;)
 {
  tmp = read(s, buf+l, 3203-l);
  if (tmp < 1){
   printf("Bluetooth receiving failed, tmp=%i",tmp);
   return -1;
  }
  l += tmp;
 }
 if (buf[0] != 1 || buf[1] != 40 || buf[2] != 40)
  printf("Weird epuck camera settings. Reset epuck!\n");
 printf("buf[0]: %i\n",buf[0]);
 printf("buf[1]: %i\n",buf[1]);
 printf("buf[2]: %i\n",buf[2]);
 system("clear");
 int i=0,r,g,b;
 int im[42][42];
 int tlp;
 for (i = 0; i < 3200; i += 2)
 {
  r = buf[i+3]&0xF8;
  g = ((buf[i+3]&0x07)<<5) | ((buf[i+4]&0xE0)>>3);
  b = (buf[i+4]&0x1F)<<3;
  tlp=(r+g+b)/3;
  if((i-28)%80==0)
   printf("\n");
  if(tlp<60){
   printf("$");
  }else{
   if(tlp<140){
    printf("+");
   }else{
    printf(".");
   }
  }
 }
}

canonical_original_code:
void getImage(){
 buf[0] = -'I';
 buf[1] = '\0';
 if (write(s, buf, 2) < 2)
  printf("Bluetooth sending failed");
 int l = 0;
 int tmp;
 for (l=0; l < 3203;)
 {
  tmp = read(s, buf+l, 3203-l);
  if (tmp < 1){
   printf("Bluetooth receiving failed, tmp=%i",tmp);
   return -1;
  }
  l += tmp;
 }
 if (buf[0] != 1 || buf[1] != 40 || buf[2] != 40)
  printf("Weird epuck camera settings. Reset epuck!\n");
 printf("buf[0]: %i\n",buf[0]);
 printf("buf[1]: %i\n",buf[1]);
 printf("buf[2]: %i\n",buf[2]);
 system("clear");
 int i = 0; int r; int g; int b;
 int im[42][42];
 int tlp;
 for (i = 0; i < 3200; i += 2)
 {
  r = buf[i+3]&0xF8;
  g = ((buf[i+3]&0x07)<<5) | ((buf[i+4]&0xE0)>>3);
  b = (buf[i+4]&0x1F)<<3;
  tlp=(r+g+b)/3;
  if((i-28)%80==0)
   printf("\n");
  if(tlp<60){
   printf("$");
  }else{
   if(tlp<140){
    printf("+");
   }else{
    printf(".");
   }
  }
 }
}

variable_types: l int

variable_types: tmp int

variable_types: r int

variable_types: g int

variable_types: b int

variable_types: tlp int


repo: autonerf/autonerf-src

decompiled_code:
__int64 __fastcall pwm_check_pin(char a1)
{
char *v1; // rax
char i; // dl

v1 = (char *)&unk_609;
for ( i = 2; a1 != i; i = *v1++ )
{
if ( v1 == (char *)1556 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
char *v1; // rax
char i; // dl

v1 = (char *)&unk_609;
for ( i = 2; a1 != i; i = *v1++ )
{
if ( v1 == (char *)1556 )
return 0LL;
}
return 1LL;
}

original_code:
uint8_t
pwm_check_pin(register const uint8_t io)
{
    static uint8_t pins[] = {2, 3, 23, 22, 50, 51, 70, 71, 80, 81, 110, 111};
    uint8_t i = 0;
    for(; i < (sizeof(pins) / sizeof(uint8_t)); i++){
        if(io == pins[i]){
            return 1;
        }
    }
    return 0;
}

canonical_original_code:
unsigned char pwm_check_pin(unsigned char io)
{
    static unsigned char pins[] = {2, 3, 23, 22, 50, 51, 70, 71, 80, 81, 110, 111};
    unsigned char i = 0;
    for(; i < (sizeof(pins) / sizeof(unsigned char)); i++){
        if(io == pins[i]){
            return 1;
        }
    }
    return 0;
}

variable_types:
io
unsigned char

variable_types:
pins
unsigned char []

variable_types:
i
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall pwm_freq_to_period(unsigned int a1)
{
return 0x3B9ACA00 / a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
return 0x3B9ACA00 / a1;
}

original_code:
uint32_t
pwm_freq_to_period(register const uint32_t frequency)
{
    return (1000000000 / frequency);
}

canonical_original_code:
unsigned int pwm_freq_to_period(unsigned int frequency)
{
    return (1000000000 / frequency);
}

variable_types:
frequency
unsigned int

return_type: unsigned int


decompiled_code:
int __fastcall pwm_set_duty_cycle(unsigned __int8 a1, unsigned int a2)
{
int *v2; // rdx
int v3; // eax
__int64 v4; // rdi

v2 = channels;
v3 = 0;
while ( *(_BYTE *)v2 != a1 )
{
++v3;
v2 += 3;
if ( v3 == 6 )
{
printf("[%s]%s", "ERROR", "\t\t");
printf("GPIO[%d]%d is not a PWM pin or is not initialized", a1 >> 5, a1 & 0x1F);
putchar(10);
v4 = 18LL;
return pwm_write_file((__int64)&channels[v4], a2, "duty");
}
}
v4 = 3LL * v3;
return pwm_write_file((__int64)&channels[v4], a2, "duty");
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int8 a1, unsigned int a2)
{
int *v2; // rdx
int v3; // eax
__int64 v4; // rdi

v2 = channels;
v3 = 0;
while ( *(_BYTE *)v2 != a1 )
{
++v3;
v2 += 3;
if ( v3 == 6 )
{
printf("[%s]%s", "ERROR", "\t\t");
printf("GPIO[%d]%d is not a PWM pin or is not initialized", a1 >> 5, a1 & 0x1F);
putchar(10);
v4 = 18LL;
return func0((__int64)&channels[v4], a2, "duty");
}
}
v4 = 3LL * v3;
return func0((__int64)&channels[v4], a2, "duty");
}

original_code:
void
pwm_set_duty_cycle(register const uint8_t io, register const uint32_t cycle)
{
    register uint8_t i = 0;
    for (; i < 6; i++) {
        if (_channels[i].io == io) {
            break;
        }
    }
    if (i >= 6) {
        { printf("[%s]%s", "ERROR", "\t\t"); printf("GPIO[%d]%d is not a PWM pin or is not initialized", (io / 32), (io % 32)); printf("\n"); };
    }
    pwm_write_file(&_channels[i], cycle, "duty");
}

canonical_original_code:
void pwm_set_duty_cycle(unsigned char io, unsigned int cycle)
{
    register unsigned char i = 0;
    for (; i < 6; i++) {
        if (_channels[i].io == io) {
            break;
        }
    }
    if (i >= 6) {
        { printf("[%s]%s", "ERROR", "\t\t"); printf("GPIO[%d]%d is not a PWM pin or is not initialized", (io / 32), (io % 32)); printf("\n"); };
    }
    pwm_write_file(&_channels[i], cycle, "duty");
}

variable_types:
cycle
unsigned int

return_type: void


original_code:
void
pwm_set_frequency(register const uint8_t io, register const uint32_t period)
{
    uint8_t pin_identifier[2][6] = {
        {23, 22, 81, 80, 70, 71},
        {50, 51, 3, 2, 111, 110}
    };
    uint8_t pin_position[2][6] = {
        {12, 19, 34, 36, 45, 46},
        {14, 16, 21, 22, 29, 31}
    };
    uint8_t pin_generator[2][6] = {
        {6, 5, 4, 3, 5, 6},
        {3, 4, 2, 1, 2, 1}
    };
    uint8_t pin_suffix[2][6] = {
        {0, 10, 0, 11, 0, 0},
        {0, 0, 0, 0, 0, 0}
    };
    uint8_t i;
    pwm_t info;
    for (i = 0; i < 6; i++) {
        register uint8_t header = 2;
        if (io == pin_identifier[0][i]) { header = 0; }
        if (io == pin_identifier[1][i]) { header = 1; }
        if (header != 2) {
            info.header = (header + 8);
            info.hpin = pin_position[header][i];
            info.pwm_pin = pin_generator[header][i];
            info.suffix = pin_suffix[header][i];
        }
    }
    info.io = io;
    info.period = period;
    for (i = 0; i < 6; i++) {
        if (_channels[i].header != 0) {
            if (info.pwm_pin == _channels[i].pwm_pin) {
                _channels[i] = info;
                { printf("[%s]%s", "INFO", "\t\t"); printf("Changed PWM channel entry %d", i); printf("\n"); };
                break;
            }
        } else {
            _channels[i] = info;
            { printf("[%s]%s", "INFO", "\t\t"); printf("Assigned new PWM channel to entry %d", i); printf("\n"); };
            break;
        }
    }
    if (i == 6) {
        { printf("[%s]%s", "EMERGENCY", "\t"); printf("Couldn't assign PWM I/O pin %d, all slots are full", io); printf("\n"); };
        return;
    }
    pwm_write_file(&_channels[i], _channels[i].period, "period");
}

canonical_original_code:
void pwm_set_frequency(unsigned char io, unsigned int period)
{
    unsigned char pin_identifier[2][6] = {
        {23, 22, 81, 80, 70, 71},
        {50, 51, 3, 2, 111, 110}
    };
    unsigned char pin_position[2][6] = {
        {12, 19, 34, 36, 45, 46},
        {14, 16, 21, 22, 29, 31}
    };
    unsigned char pin_generator[2][6] = {
        {6, 5, 4, 3, 5, 6},
        {3, 4, 2, 1, 2, 1}
    };
    unsigned char pin_suffix[2][6] = {
        {0, 10, 0, 11, 0, 0},
        {0, 0, 0, 0, 0, 0}
    };
    unsigned char i;
    struct pwm_t info;
    for (i = 0; i < 6; i++) {
        register unsigned char header = 2;
        if (io == pin_identifier[0][i]) { header = 0; }
        if (io == pin_identifier[1][i]) { header = 1; }
        if (header != 2) {
            info.header = (header + 8);
            info.hpin = pin_position[header][i];
            info.pwm_pin = pin_generator[header][i];
            info.suffix = pin_suffix[header][i];
        }
    }
    info.io = io;
    info.period = period;
    for (i = 0; i < 6; i++) {
        if (_channels[i].header != 0) {
            if (info.pwm_pin == _channels[i].pwm_pin) {
                _channels[i] = info;
                { printf("[%s]%s", "INFO", "\t\t"); printf("Changed PWM channel entry %d", i); printf("\n"); };
                break;
            }
        } else {
            _channels[i] = info;
            { printf("[%s]%s", "INFO", "\t\t"); printf("Assigned new PWM channel to entry %d", i); printf("\n"); };
            break;
        }
    }
    if (i == 6) {
        { printf("[%s]%s", "EMERGENCY", "\t"); printf("Couldn't assign PWM I/O pin %d, all slots are full", io); printf("\n"); };
        return;
    }
    pwm_write_file(&_channels[i], _channels[i].period, "period");
}

variable_types:
period
unsigned int

variable_types: struct pwm_t { unsigned char io; unsigned char header; unsigned char hpin; unsigned char pwm_pin; unsigned short suffix; unsigned int period; }

variable_types:
header
unsigned char


decompiled_code:
int __fastcall pwm_init(unsigned __int8 a1, int a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
char v4; // r8
char v5; // r9

if ( !(unsigned __int8)pwm_check_pin(a1) )
{
printf("[%s]%s", "ERROR", "\t\t");
printf("Pin %d:%d is not a PWM pin", a1 >> 5, a1 & 0x1F);
putchar(10);
}
pwm_set_frequency(a1, a2, v2, v3, v4, v5);
return pwm_set_duty_cycle(a1, 0);
}

canonical_decompiled_code:
int __fastcall func5(unsigned __int8 a1, int a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
char v4; // r8
char v5; // r9

if ( !(unsigned __int8)func1(a1) )
{
printf("[%s]%s", "ERROR", "\t\t");
printf("Pin %d:%d is not a PWM pin", a1 >> 5, a1 & 0x1F);
putchar(10);
}
func4(a1, a2, v2, v3, v4, v5);
return func3(a1, 0);
}

original_code:
void
pwm_init(register const uint8_t io, register const uint32_t period)
{
    if (!pwm_check_pin(io)) {
        { printf("[%s]%s", "ERROR", "\t\t"); printf("Pin %d:%d is not a PWM pin", (io / 32), (io % 32)); printf("\n"); };
    }
    pwm_set_frequency(io, period);
    pwm_set_duty_cycle(io, 0);
}

canonical_original_code:
void pwm_init(unsigned char io, unsigned int period)
{
    if (!pwm_check_pin(io)) {
        { printf("[%s]%s", "ERROR", "\t\t"); printf("Pin %d:%d is not a PWM pin", (io / 32), (io % 32)); printf("\n"); };
    }
    pwm_set_frequency(io, period);
    pwm_set_duty_cycle(io, 0);
}


decompiled_code:
__int64 control_init()
{
char v0; // al
__int64 v1; // rdi

pan = gpio_calculate(0LL, 22LL);
v0 = gpio_calculate(2LL, 16LL);
v1 = (unsigned __int8)pan;
tilt = v0;
*((_QWORD *)&pan + 1) = 9448928051900000LL;
*((_QWORD *)&tilt + 1) = 0x1F06B500087854LL;
*((_DWORD *)&pan + 1) = LC0;
*((_DWORD *)&tilt + 1) = LC0;
servo_init(v1, 1450000LL);
return servo_init(
(unsigned __int8)tilt,
*((_DWORD *)&tilt + 2) + ((unsigned int)(*((_DWORD *)&tilt + 3) - *((_DWORD *)&tilt + 2)) >> 1));
}

canonical_decompiled_code:
__int64 func0()
{
char v0; // al
__int64 v1; // rdi

pan = gpio_calculate(0LL, 22LL);
v0 = gpio_calculate(2LL, 16LL);
v1 = (unsigned __int8)pan;
tilt = v0;
*((_QWORD *)&pan + 1) = 9448928051900000LL;
*((_QWORD *)&tilt + 1) = 0x1F06B500087854LL;
*((_DWORD *)&pan + 1) = LC0;
*((_DWORD *)&tilt + 1) = LC0;
servo_init(v1, 1450000LL);
return servo_init(
(unsigned __int8)tilt,
*((_DWORD *)&tilt + 2) + ((unsigned int)(*((_DWORD *)&tilt + 3) - *((_DWORD *)&tilt + 2)) >> 1));
}

original_code:
void
control_init(void)
{
  pan.io = gpio_calculate(0, 22);
  tilt.io = gpio_calculate(2, 16);
  pan.position = 90.0;
  tilt.position = 90.0;
  pan.min = 700000;
  pan.max = 2200000;
  tilt.min = 555092;
  tilt.max = 2033333;
  servo_init(pan.io, ((pan.max - pan.min) / 2) + pan.min);
  servo_init(tilt.io, ((tilt.max - tilt.min) / 2) + tilt.min);
}

canonical_original_code:
void control_init()
{
  pan.io = gpio_calculate(0, 22);
  tilt.io = gpio_calculate(2, 16);
  pan.position = 90.0;
  tilt.position = 90.0;
  pan.min = 700000;
  pan.max = 2200000;
  tilt.min = 555092;
  tilt.max = 2033333;
  servo_init(pan.io, ((pan.max - pan.min) / 2) + pan.min);
  servo_init(tilt.io, ((tilt.max - tilt.min) / 2) + tilt.min);
}


decompiled_code:
__int64 __fastcall control_set_pan(float a1)
{
float v1; // xmm0_4
__int64 v2; // rdi
__int64 v3; // rdx

if ( a1 > 180.0 )
v1 = *(float *)&LC1;
else
v1 = fmaxf(0.0, a1);
v2 = (unsigned __int8)pan;
v3 = *((unsigned int *)&pan + 3);
*((float *)&pan + 1) = v1;
return servo_set_position(v2, *((unsigned int *)&pan + 2), v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(float a1)
{
float v1; // xmm0_4
__int64 v2; // rdi
__int64 v3; // rdx

if ( a1 > 180.0 )
v1 = *(float *)&LC1;
else
v1 = fmaxf(0.0, a1);
v2 = (unsigned __int8)pan;
v3 = *((unsigned int *)&pan + 3);
*((float *)&pan + 1) = v1;
return servo_set_position(v2, *((unsigned int *)&pan + 2), v3);
}

original_code:
void
control_set_pan(float position)
{
    if (position > 180.0) { position = 180.0; }
    if (position < 0.0) { position = 0.0; }
    pan.position = position;
    servo_set_position(pan.io, pan.position, pan.min, pan.max);
}

variable_types: position float


decompiled_code:
__int64 __fastcall control_set_tilt(float a1)
{
double v1; // xmm1_8

if ( a1 > 180.0 )
{
v1 = *(double *)&LC3;
a1 = *(float *)&LC1;
}
else
{
v1 = 0.0;
if ( a1 >= 0.0 )
v1 = a1;
else
a1 = 0.0;
}
*((float *)&tilt + 1) = a1;
printf("New position: %f\n", v1);
return servo_set_position((unsigned __int8)tilt, *((unsigned int *)&tilt + 2), *((unsigned int *)&tilt + 3));
}

canonical_decompiled_code:
__int64 __fastcall func2(float a1)
{
double v1; // xmm1_8

if ( a1 > 180.0 )
{
v1 = *(double *)&LC3;
a1 = *(float *)&LC1;
}
else
{
v1 = 0.0;
if ( a1 >= 0.0 )
v1 = a1;
else
a1 = 0.0;
}
*((float *)&tilt + 1) = a1;
printf("New position: %f\n", v1);
return servo_set_position((unsigned __int8)tilt, *((unsigned int *)&tilt + 2), *((unsigned int *)&tilt + 3));
}

original_code:
void
control_set_tilt(float position)
{
    if (position > 180.0) { position = 180.0; }
    if (position < 0.0) { position = 0.0; }
    tilt.position = position;
    printf("New position: %f\n", position);
    servo_set_position(tilt.io, tilt.position, tilt.min, tilt.max);
}


decompiled_code:
__int64 __fastcall control_set_pan_delta(float a1)
{
float v1; // xmm0_4

v1 = a1 + *((float *)&pan + 1);
*((float *)&pan + 1) = v1;
return control_set_pan(v1);
}

canonical_decompiled_code:
__int64 __fastcall func3(float a1)
{
float v1; // xmm0_4

v1 = a1 + *((float *)&pan + 1);
*((float *)&pan + 1) = v1;
return func1(v1);
}

original_code:
void
control_set_pan_delta(float delta)
{
    pan.position += delta;
    control_set_pan(pan.position);
}

variable_types: delta float


decompiled_code:
__int64 __fastcall control_set_tilt_delta(float a1)
{
float v1; // xmm0_4

v1 = a1 + *((float *)&tilt + 1);
*((float *)&tilt + 1) = v1;
return control_set_tilt(v1);
}

canonical_decompiled_code:
__int64 __fastcall func4(float a1)
{
float v1; // xmm0_4

v1 = a1 + *((float *)&tilt + 1);
*((float *)&tilt + 1) = v1;
return func2(v1);
}

original_code:
void
control_set_tilt_delta(float delta)
{
    tilt.position += delta;
    control_set_tilt(tilt.position);
}


decompiled_code:
int __fastcall gpio_init(unsigned __int8 a1, char a2)
{
const char *v3; // rsi

gpio_export(a1);
v3 = "in";
if ( a2 != 1 )
v3 = "out";
return gpio_set_direction(a1, v3);
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int8 a1, char a2)
{
const char *v3; // rsi

func1(a1);
v3 = "in";
if ( a2 != 1 )
v3 = "out";
return func0(a1, v3);
}

original_code:
void
gpio_init(register const uint8_t io, register const uint8_t direction)
{
    gpio_export(io);
    gpio_set_direction(io, (direction == 1) ? "in" : "out");
}

canonical_original_code:
void gpio_init(unsigned char io, unsigned char direction)
{
    gpio_export(io);
    gpio_set_direction(io, (direction == 1) ? "in" : "out");
}

variable_types:
direction
unsigned char


decompiled_code:
__int64 __fastcall gpio_calculate(int a1, int a2)
{
return (unsigned int)(32 * a1 + a2);
}

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

original_code:
uint8_t
gpio_calculate(register const uint8_t chip, register const uint8_t pin)
{
    return ((chip * 32) + pin);
}

canonical_original_code:
unsigned char gpio_calculate(unsigned char chip, unsigned char pin)
{
    return ((chip * 32) + pin);
}

variable_types:
chip
unsigned char

variable_types:
pin
unsigned char


decompiled_code:
__int64 __fastcall servo_init(unsigned __int8 a1, unsigned int a2)
{
pwm_init(a1, 20000000LL);
return pwm_set_duty_cycle(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, unsigned int a2)
{
pwm_init(a1, 20000000LL);
return pwm_set_duty_cycle(a1, a2);
}

original_code:
void
servo_init(register const uint8_t io, register const uint32_t servo_min)
{
    pwm_init(io, 20000000);
    pwm_set_duty_cycle(io, servo_min);
}

canonical_original_code:
void servo_init(unsigned char io, unsigned int servo_min)
{
    pwm_init(io, 20000000);
    pwm_set_duty_cycle(io, servo_min);
}


decompiled_code:
__int64 __fastcall servo_set_position(unsigned __int8 a1, __int64 a2, unsigned int a3, float a4)
{
unsigned int v4; // eax

v4 = a2 + (int)(float)((float)((float)(int)(a3 - a2) * a4) / 180.0);
if ( (unsigned int)a2 < v4 )
a2 = v4;
if ( a3 <= (unsigned int)a2 )
a2 = a3;
return pwm_set_duty_cycle(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, __int64 a2, unsigned int a3, float a4)
{
unsigned int v4; // eax

v4 = a2 + (int)(float)((float)((float)(int)(a3 - a2) * a4) / 180.0);
if ( (unsigned int)a2 < v4 )
a2 = v4;
if ( a3 <= (unsigned int)a2 )
a2 = a3;
return pwm_set_duty_cycle(a1, a2);
}

original_code:
void
servo_set_position(register const uint8_t io, register const float position, register const uint32_t servo_min, register const uint32_t servo_max)
{
    uint32_t cycle = servo_min + ((uint32_t) (position * (float)(servo_max - servo_min) / 180.0f));
    if (cycle < servo_min) { cycle = servo_min; }
    if (cycle > servo_max) { cycle = servo_max; }
    pwm_set_duty_cycle(io, cycle);
}

canonical_original_code:
void servo_set_position(unsigned char io, float position, unsigned int servo_min, unsigned int servo_max)
{
    unsigned int cycle = servo_min + ((uint32_t) (position * (float)(servo_max - servo_min) / 180.0f));
    if (cycle < servo_min) { cycle = servo_min; }
    if (cycle > servo_max) { cycle = servo_max; }
    pwm_set_duty_cycle(io, cycle);
}


ChildcareInit

decompiled_code:
int ChildcareInit()
{
sem_init(&mutex, 0, 1u);
sem_init(&adultQueue, 0, 0);
return sem_init(&childQueue, 0, 0);
}

canonical_decompiled_code:
int func0()
{
sem_init(&mutex, 0, 1u);
sem_init(&adultQueue, 0, 0);
return sem_init(&childQueue, 0, 0);
}

original_code:
void ChildcareInit ()
{
 sem_init (&mutex, 0, 1);
 sem_init (&adultQueue, 0, 0);
 sem_init (&childQueue, 0, 0);
}

canonical_original_code:
void ChildcareInit()
{
 sem_init (&mutex, 0, 1);
 sem_init (&adultQueue, 0, 0);
 sem_init (&childQueue, 0, 0);
}


Child

decompiled_code:
__int64 Child()
{
unsigned int v0; // ebp
int v1; // eax
int v2; // esi
int v3; // edx
unsigned int v5; // ebp

sem_wait(&mutex);
if ( 3 * adults <= children )
{
++waiting;
sem_post(&mutex);
v5 = Screen_AddWaitingChild();
sem_wait(&childQueue);
Screen_RemoveWaitingChild(v5);
}
else
{
++children;
sem_post(&mutex);
}
v0 = Screen_AddActiveChild();
v1 = rand();
SDL_Delay((unsigned int)(v1 % 6000 + 2000));
sem_wait(&mutex);
v2 = children;
v3 = --children;
if ( leaving && v3 <= 3 * (adults - 1) )
{
--adults;
--leaving;
sem_post(&adultQueue);
}
else if ( waiting )
{
children = v2;
--waiting;
sem_post(&childQueue);
}
sem_post(&mutex);
Screen_RemoveActiveChild(v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebp
int v1; // eax
int v2; // esi
int v3; // edx
unsigned int v5; // ebp

sem_wait(&mutex);
if ( 3 * adults <= children )
{
++waiting;
sem_post(&mutex);
v5 = Screen_AddWaitingChild();
sem_wait(&childQueue);
Screen_RemoveWaitingChild(v5);
}
else
{
++children;
sem_post(&mutex);
}
v0 = Screen_AddActiveChild();
v1 = rand();
SDL_Delay((unsigned int)(v1 % 6000 + 2000));
sem_wait(&mutex);
v2 = children;
v3 = --children;
if ( leaving && v3 <= 3 * (adults - 1) )
{
--adults;
--leaving;
sem_post(&adultQueue);
}
else if ( waiting )
{
children = v2;
--waiting;
sem_post(&childQueue);
}
sem_post(&mutex);
Screen_RemoveActiveChild(v0);
return 0LL;
}

original_code:
void * Child (void *v) {
 int i;
 sem_wait(&mutex);
 if (children < 3 * adults) {
  children++;
  sem_post(&mutex);
 }
 else {
  waiting++;
  sem_post(&mutex);
  i = Screen_AddWaitingChild();
  sem_wait(&childQueue);
  Screen_RemoveWaitingChild( i );
 }
 i = Screen_AddActiveChild();
 SDL_Delay( ( rand() % 6000 ) + 2000 );
 sem_wait(&mutex);
 children--;
 if (leaving && children <= 3*(adults-1)) {
  leaving--;
  adults--;
  sem_post(&adultQueue);
 }
 else if (waiting) {
  waiting--;
  children++;
  sem_post(&childQueue);
 }
 sem_post(&mutex);
 Screen_RemoveActiveChild( i );
 return 0;
}

canonical_original_code:
void *Child(void *v) {
 int i;
 sem_wait(&mutex);
 if (children < 3 * adults) {
  children++;
  sem_post(&mutex);
 }
 else {
  waiting++;
  sem_post(&mutex);
  i = Screen_AddWaitingChild();
  sem_wait(&childQueue);
  Screen_RemoveWaitingChild( i );
 }
 i = Screen_AddActiveChild();
 SDL_Delay( ( rand() % 6000 ) + 2000 );
 sem_wait(&mutex);
 children--;
 if (leaving && children <= 3*(adults-1)) {
  leaving--;
  adults--;
  sem_post(&adultQueue);
 }
 else if (waiting) {
  waiting--;
  children++;
  sem_post(&childQueue);
 }
 sem_post(&mutex);
 Screen_RemoveActiveChild( i );
 return 0;
}

variable_types:
v
void *

variable_types: i int

return_type: void *


Adult

decompiled_code:
__int64 Adult()
{
int v0; // edx
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
int v4; // eax
unsigned int v6; // ebp

sem_wait(&mutex);
v0 = adults++;
v1 = waiting;
if ( waiting )
{
v2 = 3;
do
{
++children;
waiting = v1 - 1;
sem_post(&childQueue);
if ( !--v2 )
break;
v1 = waiting;
}
while ( waiting );
}
else if ( leaving )
{
adults = v0;
--leaving;
sem_post(&adultQueue);
}
sem_post(&mutex);
v3 = Screen_AddActiveAdult();
v4 = rand();
SDL_Delay((unsigned int)(v4 % 10000 + 4444));
sem_wait(&mutex);
Screen_RemoveActiveAdult(v3);
if ( 3 * (adults - 1) < children )
{
++leaving;
sem_post(&mutex);
v6 = Screen_AddWaitingAdult();
sem_wait(&adultQueue);
Screen_RemoveWaitingAdult(v6);
}
else
{
--adults;
sem_post(&mutex);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // edx
int v1; // eax
int v2; // ebx
unsigned int v3; // ebp
int v4; // eax
unsigned int v6; // ebp

sem_wait(&mutex);
v0 = adults++;
v1 = waiting;
if ( waiting )
{
v2 = 3;
do
{
++children;
waiting = v1 - 1;
sem_post(&childQueue);
if ( !--v2 )
break;
v1 = waiting;
}
while ( waiting );
}
else if ( leaving )
{
adults = v0;
--leaving;
sem_post(&adultQueue);
}
sem_post(&mutex);
v3 = Screen_AddActiveAdult();
v4 = rand();
SDL_Delay((unsigned int)(v4 % 10000 + 4444));
sem_wait(&mutex);
Screen_RemoveActiveAdult(v3);
if ( 3 * (adults - 1) < children )
{
++leaving;
sem_post(&mutex);
v6 = Screen_AddWaitingAdult();
sem_wait(&adultQueue);
Screen_RemoveWaitingAdult(v6);
}
else
{
--adults;
sem_post(&mutex);
}
return 0LL;
}

original_code:
void * Adult (void *v) {
 int i;
 sem_wait(&mutex);
 adults++;
 if (waiting) {
  for (i = 0; i < 3; i++) {
   if (waiting) {
    waiting--;
    children++;
    sem_post(&childQueue);
   }
   else
    break;
  }
 }
 else if (leaving) {
  adults--;
  leaving--;
  sem_post(&adultQueue);
 }
 sem_post(&mutex);
 i = Screen_AddActiveAdult();
 SDL_Delay( ( rand() % 10000 ) + 4444 );
 sem_wait(&mutex);
 Screen_RemoveActiveAdult( i );
 if (children <= 3*(adults-1)) {
  adults--;
  sem_post(&mutex);
 }
 else {
  leaving++;
  sem_post(&mutex);
  i = Screen_AddWaitingAdult();
  sem_wait(&adultQueue);
  Screen_RemoveWaitingAdult( i );
 }
 return 0;
}

canonical_original_code:
void *Adult(void *v) {
 int i;
 sem_wait(&mutex);
 adults++;
 if (waiting) {
  for (i = 0; i < 3; i++) {
   if (waiting) {
    waiting--;
    children++;
    sem_post(&childQueue);
   }
   else
    break;
  }
 }
 else if (leaving) {
  adults--;
  leaving--;
  sem_post(&adultQueue);
 }
 sem_post(&mutex);
 i = Screen_AddActiveAdult();
 SDL_Delay( ( rand() % 10000 ) + 4444 );
 sem_wait(&mutex);
 Screen_RemoveActiveAdult( i );
 if (children <= 3*(adults-1)) {
  adults--;
  sem_post(&mutex);
 }
 else {
  leaving++;
  sem_post(&mutex);
  i = Screen_AddWaitingAdult();
  sem_wait(&adultQueue);
  Screen_RemoveWaitingAdult( i );
 }
 return 0;
}


GetWaitingChildTotal

decompiled_code:
__int64 GetWaitingChildTotal()
{
return (unsigned int)waiting;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)waiting;
}

original_code:
int GetWaitingChildTotal() {
        return waiting;
}

return_type: int


GetActiveChildTotal

decompiled_code:
__int64 GetActiveChildTotal()
{
return (unsigned int)children;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)children;
}

original_code:
int GetActiveChildTotal() {
        return children;
}


GetWaitingAdultTotal

decompiled_code:
__int64 GetWaitingAdultTotal()
{
return (unsigned int)leaving;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)leaving;
}

original_code:
int GetWaitingAdultTotal() {
        return leaving;
}


GetActiveAdultTotal

decompiled_code:
__int64 GetActiveAdultTotal()
{
return (unsigned int)(adults - leaving);
}

canonical_decompiled_code:
__int64 func6()
{
return (unsigned int)(adults - leaving);
}

original_code:
int GetActiveAdultTotal() {
        return adults - leaving;
}


MakeChild

decompiled_code:
void __fastcall __noreturn MakeChild(void *a1)
{
int v1; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

while ( 1 )
{
v1 = rand();
SDL_Delay((unsigned int)(*(_DWORD *)a1 + v1 % (*((_DWORD *)a1 + 1) - *(_DWORD *)a1)));
pthread_create(newthread, 0LL, (void *(*)(void *))&Child, 0LL);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
int v1; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

while ( 1 )
{
v1 = rand();
SDL_Delay((unsigned int)(*(_DWORD *)a1 + v1 % (*((_DWORD *)a1 + 1) - *(_DWORD *)a1)));
pthread_create(newthread, 0LL, (void *(*)(void *))&Child, 0LL);
}
}

original_code:
void * MakeChild( void * v ) {
 pthread_t thrChild;
 struct MakerParams * param = (struct MakerParams *) v;
 int waitTime = 0;
 while(1) {
  waitTime = rand()%(param->maxMakeDelay - param->minMakeDelay);
  waitTime += param->minMakeDelay;
  SDL_Delay( waitTime );
  pthread_create( &thrChild, ((void *)0), Child, ((void *)0) );
 }
 return 0;
}

canonical_original_code:
void *MakeChild(void *v) {
 unsigned long thrChild;
 struct MakerParams *param = (struct MakerParams *) v;
 int waitTime = 0;
 while(1) {
  waitTime = rand()%(param->maxMakeDelay - param->minMakeDelay);
  waitTime += param->minMakeDelay;
  SDL_Delay( waitTime );
  pthread_create( &thrChild, ((void *)0), Child, ((void *)0) );
 }
 return 0;
}

variable_types:
thrChild
unsigned long

variable_types: struct MakerParams { int minMakeDelay; int maxMakeDelay; } *

variable_types: waitTime int


MakeAdult

decompiled_code:
void __fastcall __noreturn MakeAdult(void *a1)
{
int v1; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

while ( 1 )
{
v1 = rand();
SDL_Delay((unsigned int)(*(_DWORD *)a1 + v1 % (*((_DWORD *)a1 + 1) - *(_DWORD *)a1)));
pthread_create(newthread, 0LL, (void *(*)(void *))&Adult, 0LL);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
int v1; // eax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

while ( 1 )
{
v1 = rand();
SDL_Delay((unsigned int)(*(_DWORD *)a1 + v1 % (*((_DWORD *)a1 + 1) - *(_DWORD *)a1)));
pthread_create(newthread, 0LL, (void *(*)(void *))&Adult, 0LL);
}
}

original_code:
void * MakeAdult( void * v ) {
 pthread_t thrAdult;
 struct MakerParams * param = (struct MakerParams *) v;
 int waitTime = 0;
 while(1) {
  waitTime = rand()%(param->maxMakeDelay - param->minMakeDelay);
  waitTime += param->minMakeDelay;
  SDL_Delay( waitTime );
  pthread_create( &thrAdult, ((void *)0), Adult, ((void *)0) );
 }
 return 0;
}

canonical_original_code:
void *MakeAdult(void *v) {
 unsigned long thrAdult;
 struct MakerParams *param = (struct MakerParams *) v;
 int waitTime = 0;
 while(1) {
  waitTime = rand()%(param->maxMakeDelay - param->minMakeDelay);
  waitTime += param->minMakeDelay;
  SDL_Delay( waitTime );
  pthread_create( &thrAdult, ((void *)0), Adult, ((void *)0) );
 }
 return 0;
}

variable_types:
thrAdult
unsigned long

user_defined_types: struct MakerParams { int minMakeDelay; int maxMakeDelay; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // eax
const char *v5; // rdi
int v6; // eax
const char *v7; // rdi
int v8; // eax
const char *v9; // rdi
int v10; // er8
pthread_t v11; // [rsp+0h] [rbp-28h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-20h] BYREF
int arg[2]; // [rsp+10h] [rbp-18h] BYREF
int v14[4]; // [rsp+18h] [rbp-10h] BYREF

if ( argc == 5 )
{
ChildcareInit(argc, argv, envp);
v4 = atol(argv[1]);
v5 = argv[2];
arg[0] = v4;
v6 = atol(v5);
v7 = argv[3];
arg[1] = v6;
v8 = atol(v7);
v9 = argv[4];
v14[0] = v8;
v14[1] = atol(v9);
LODWORD(v9) = time(0LL);
srand((unsigned int)v9);
v10 = InitMySDL();
result = 1;
if ( v10 )
{
pthread_create(&v11, 0LL, (void *(*)(void *))MakeChild, arg);
pthread_create(&newthread, 0LL, (void *(*)(void *))MakeAdult, v14);
DrawAndEventLoop();
CleanMySDL();
result = 0;
}
}
else
{
fwrite(
"Usage: ChildCare minMakeDelay(child) maxMakeDelay(child)\\\n"
" minMakeDelay(adult) maxMakeDelay(adult)\n",
1uLL,
0x73uLL,
stderr);
fwrite("All delays are in ms and must keep the form:\n", 1uLL, 0x2DuLL, stderr);
fwrite(" 0 < minMakeDelay < maxMakeDelay\n", 1uLL, 0x22uLL, stderr);
result = 1;
}
return result;
}

original_code:
int main( int argc, char* argv[] ) {
 pthread_t thrMakeChild, thrMakeAdult;
 struct MakerParams pmMakeChild, pmMakeAdult;
 if( argc != 5 ) {
  fprintf( stderr,
    "Usage: ChildCare minMakeDelay(child) maxMakeDelay(child)\\\n                 minMakeDelay(adult) maxMakeDelay(adult)\n");
  fprintf( stderr,
   "All delays are in ms and must keep the form:\n" );
  fprintf( stderr,
   "  0 < minMakeDelay < maxMakeDelay\n" );
  return 1;
 }
 ChildcareInit ();
 pmMakeChild.minMakeDelay = atol( argv[1] );
 pmMakeChild.maxMakeDelay = atol( argv[2] );
 pmMakeAdult.minMakeDelay = atol( argv[3] );
 pmMakeAdult.maxMakeDelay = atol( argv[4] );
 srand( time( ((void *)0) ) );
 if( !InitMySDL() )
  return 1;
 pthread_create( &thrMakeChild, ((void *)0), MakeChild, &pmMakeChild );
 pthread_create( &thrMakeAdult, ((void *)0), MakeAdult, &pmMakeAdult );
 DrawAndEventLoop();
 CleanMySDL();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 unsigned long thrMakeChild; unsigned long thrMakeAdult;
 struct MakerParams pmMakeChild; struct MakerParams pmMakeAdult;
 if( argc != 5 ) {
  fprintf( stderr,
    "Usage: ChildCare minMakeDelay(child) maxMakeDelay(child)\\\n                 minMakeDelay(adult) maxMakeDelay(adult)\n");
  fprintf( stderr,
   "All delays are in ms and must keep the form:\n" );
  fprintf( stderr,
   "  0 < minMakeDelay < maxMakeDelay\n" );
  return 1;
 }
 ChildcareInit ();
 pmMakeChild.minMakeDelay = atol( argv[1] );
 pmMakeChild.maxMakeDelay = atol( argv[2] );
 pmMakeAdult.minMakeDelay = atol( argv[3] );
 pmMakeAdult.maxMakeDelay = atol( argv[4] );
 srand( time( ((void *)0) ) );
 if( !InitMySDL() )
  return 1;
 pthread_create( &thrMakeChild, ((void *)0), MakeChild, &pmMakeChild );
 pthread_create( &thrMakeAdult, ((void *)0), MakeAdult, &pmMakeAdult );
 DrawAndEventLoop();
 CleanMySDL();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
thrMakeChild
unsigned long

variable_types:
thrMakeAdult
unsigned long

variable_types: struct MakerParams { int minMakeDelay; int maxMakeDelay; }


repo: auxten/zax

decompiled_code:
__int64 __fastcall arg_to_int(__int64 a1, int a2, int a3, signed int a4, const char *a5)
{
unsigned int v5; // er12
__int64 result; // rax

v5 = a4;
if ( !a1 )
return v5;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
if ( a4 <= a3 && a4 >= a2 )
return (unsigned int)a4;
fprintf(stderr, "%s: argument out of integer range.\n", a5);
result = v5;
}
else
{
fprintf(stderr, "%s: integer argument required.\n", a5);
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2, int a3, signed int a4, const char *a5)
{
unsigned int v5; // er12
__int64 result; // rax

v5 = a4;
if ( !a1 )
return v5;
if ( (unsigned int)__isoc99_sscanf() == 1 )
{
if ( a4 <= a3 && a4 >= a2 )
return (unsigned int)a4;
fprintf(stderr, "%s: argument out of integer range.\n", a5);
result = v5;
}
else
{
fprintf(stderr, "%s: integer argument required.\n", a5);
result = v5;
}
return result;
}

original_code:
int arg_to_int(const char* arg, int min, int max, int defalt, const char* opt)
{
 int i = defalt;
 int rv;
 if(!arg) goto done;
 rv = sscanf(arg, "%d", &i);
 if(rv != 1) {
  fprintf(stderr, "%s: integer argument required.\n", opt);
  i = defalt;
  goto done;
 }
 if(i < min || max < i) {
  fprintf(stderr, "%s: argument out of integer range.\n", opt);
  i = defalt;
  goto done;
 }
done:
 return i;
}

canonical_original_code:
int arg_to_int(char *arg, int min, int max, int defalt, char *opt)
{
 int i = defalt;
 int rv;
 if(!arg) goto done;
 rv = sscanf(arg, "%d", &i);
 if(rv != 1) {
  fprintf(stderr, "%s: integer argument required.\n", opt);
  i = defalt;
  goto done;
 }
 if(i < min || max < i) {
  fprintf(stderr, "%s: argument out of integer range.\n", opt);
  i = defalt;
  goto done;
 }
done:
 return i;
}

variable_types:
arg
char *

variable_types: min int

variable_types: max int

variable_types: defalt int

variable_types:
opt
char *

variable_types: rv int


decompiled_code:
__int64 increment_index()
{
__int64 result; // rax
_BYTE *v1; // rdx
int v2; // er8
bool v3; // cl
bool v4; // dl

if ( argv_index >= argv_index2 )
{
result = (unsigned int)(argv_index + 1);
}
else
{
result = argv_index + 1;
while ( 1 )
{
v1 = *(_BYTE **)(prev_argv + 8 * result);
v2 = result;
if ( !v1 )
break;
v3 = *v1 != 45;
v4 = argv_index2 >= (int)result++;
if ( !v4 || !v3 )
{
argv_index = v2;
opt_offset = 1;
return result;
}
}
}
argv_index = result;
opt_offset = 1;
return result;
}

canonical_decompiled_code:
__int64 func9()
{
__int64 result; // rax
_BYTE *v1; // rdx
int v2; // er8
bool v3; // cl
bool v4; // dl

if ( argv_index >= argv_index2 )
{
result = (unsigned int)(argv_index + 1);
}
else
{
result = argv_index + 1;
while ( 1 )
{
v1 = *(_BYTE **)(prev_argv + 8 * result);
v2 = result;
if ( !v1 )
break;
v3 = *v1 != 45;
v4 = argv_index2 >= (int)result++;
if ( !v4 || !v3 )
{
argv_index = v2;
opt_offset = 1;
return result;
}
}
}
argv_index = result;
opt_offset = 1;
return result;
}

original_code:
static void increment_index()
{
 if(argv_index < argv_index2)
 {
  while(prev_argv[++argv_index] && prev_argv[argv_index][0] != '-'
    && argv_index < argv_index2+1);
 }
 else argv_index++;
 opt_offset = 1;
}


decompiled_code:
__int64 permute_argv_once()
{
int v0; // ebp
__int64 v1; // rbx
__int64 v3; // r12
__int64 *v4; // rdi
__int64 v5; // r13

v0 = nonopt;
v1 = prev_argc;
if ( argv_index + nonopt >= prev_argc )
return 1LL;
v3 = prev_argv;
v4 = (__int64 *)(prev_argv + 8LL * argv_index);
v5 = *v4;
memmove(v4, v4 + 1, 8LL * (prev_argc - argv_index - 1));
*(_QWORD *)(v3 + 8 * v1 - 8) = v5;
nonopt = v0 + 1;
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // ebp
__int64 v1; // rbx
__int64 v3; // r12
__int64 *v4; // rdi
__int64 v5; // r13

v0 = nonopt;
v1 = prev_argc;
if ( argv_index + nonopt >= prev_argc )
return 1LL;
v3 = prev_argv;
v4 = (__int64 *)(prev_argv + 8LL * argv_index);
v5 = *v4;
memmove(v4, v4 + 1, 8LL * (prev_argc - argv_index - 1));
*(_QWORD *)(v3 + 8 * v1 - 8) = v5;
nonopt = v0 + 1;
return 0LL;
}

original_code:
static int permute_argv_once()
{
 if(argv_index + nonopt >= prev_argc) return 1;
 else
 {
  char* tmp = prev_argv[argv_index];
  memmove(&prev_argv[argv_index], &prev_argv[argv_index+1],
    sizeof(char**) * (prev_argc - argv_index - 1));
  prev_argv[prev_argc - 1] = tmp;
  nonopt++;
  return 0;
 }
}

canonical_original_code:
static int permute_argv_once()
{
 if(argv_index + nonopt >= prev_argc) return 1;
 else
 {
  char *tmp = prev_argv[argv_index];
  memmove(&prev_argv[argv_index], &prev_argv[argv_index+1],
    sizeof(char **) * (prev_argc - argv_index - 1));
  prev_argv[prev_argc - 1] = tmp;
  nonopt++;
  return 0;
 }
}

variable_types:
tmp
char *


getopt

decompiled_code:
__int64 __fastcall getopt(int a1, const char **a2, const char *a3)
{
int v6; // ebp
const char *v7; // rbx
bool v8; // zf
unsigned int v9; // er15
const char *v10; // rsi
FILE *v11; // rdi
char *v12; // rax
__int64 v13; // rdx
char *v14; // r9
const char *v15; // rbx
char v16; // al
int v18; // eax
__int64 v19; // rax
int v20; // ecx
__int64 v21; // r9
__int64 v22; // rcx
int v23; // [rsp+Ch] [rbp-3Ch]

if ( (const char **)prev_argv != a2 || prev_argc != a1 )
{
prev_argv = (__int64)a2;
v6 = 1;
prev_argc = a1;
argv_index = 1;
argv_index2 = 1;
opt_offset = 1;
dashdash = 0;
nonopt = 0;
goto LABEL_11;
}
while ( 1 )
{
v6 = argv_index;
LABEL_11:
optarg = 0LL;
v7 = a2[v6];
if ( !v7 )
goto LABEL_23;
if ( *v7 != 45 || v7[1] != 45 || v7[2] )
break;
dashdash = 1;
((void (*)(void))increment_index)();
v7 = a2[argv_index];
v6 = argv_index;
if ( !v7 )
goto LABEL_23;
LABEL_6:
if ( *a3 == 45 )
{
optarg = (__int64)v7;
v9 = 1;
((void (*)(void))increment_index)();
goto LABEL_30;
}
if ( *a3 == 43 || getenv("POSIXLY_CORRECT") )
{
nonopt = a1 - v6;
goto LABEL_24;
}
if ( (unsigned int)permute_argv_once() )
goto LABEL_23;
}
if ( dashdash || *v7 != 45 || !v7[1] )
goto LABEL_6;
v8 = *a3 == 45;
v9 = v7[opt_offset++];
v23 = opt_offset;
if ( v8 )
{
v11 = (FILE *)(a3 + 1);
v10 = (const char *)v9;
v12 = strchr(a3 + 1, v9);
}
else
{
v10 = (const char *)v9;
v11 = (FILE *)a3;
v12 = strchr(a3, v9);
}
v13 = v23;
v14 = v12;
if ( !v12 )
{
if ( opterr )
{
v11 = stderr;
v10 = "%s: invalid option -- %c\n";
fprintf(stderr, "%s: invalid option -- %c\n", *a2, v9);
}
optopt = v9;
v9 = 63;
increment_index(v11, v10, v13);
goto LABEL_30;
}
v15 = &v7[v23];
v16 = *v15;
if ( v14[1] != 58 )
{
if ( !v16 )
increment_index(v11, v10, v23);
goto LABEL_22;
}
if ( v16 )
{
optarg = (__int64)v15;
increment_index(v11, v10, v23);
}
else
{
if ( v14[2] != 58 )
{
v18 = v6;
if ( argv_index2 >= v6 )
v18 = argv_index2;
v19 = v18 + 1;
do
{
v13 = (__int64)a2[v19];
v20 = v19;
if ( !v13 )
{
argv_index2 = v19;
goto LABEL_43;
}
++v19;
}
while ( *(_BYTE *)v13 == 45 );
argv_index2 = v20;
LABEL_43:
optarg = v13;
if ( nonopt + v20 >= prev_argc )
optarg = 0LL;
}
increment_index(v11, v10, v13);
}
if ( !optarg && *(_BYTE *)(v21 + 2) != 58 )
{
optopt = v9;
if ( opterr )
{
v22 = v9;
v9 = 63;
fprintf(stderr, "%s: option requires an argument -- %c\n", *a2, v22);
}
else
{
v9 = 63;
}
goto LABEL_30;
}
LABEL_22:
if ( v9 == -1 )
{
LABEL_23:
v6 = a1 - nonopt;
LABEL_24:
optind = v6;
return (unsigned int)-1;
}
LABEL_30:
optind = argv_index;
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, const char **a2, const char *a3)
{
int v6; // ebp
const char *v7; // rbx
bool v8; // zf
unsigned int v9; // er15
const char *v10; // rsi
FILE *v11; // rdi
char *v12; // rax
__int64 v13; // rdx
char *v14; // r9
const char *v15; // rbx
char v16; // al
int v18; // eax
__int64 v19; // rax
int v20; // ecx
__int64 v21; // r9
__int64 v22; // rcx
int v23; // [rsp+Ch] [rbp-3Ch]

if ( (const char **)prev_argv != a2 || prev_argc != a1 )
{
prev_argv = (__int64)a2;
v6 = 1;
prev_argc = a1;
argv_index = 1;
argv_index2 = 1;
opt_offset = 1;
dashdash = 0;
nonopt = 0;
goto LABEL_11;
}
while ( 1 )
{
v6 = argv_index;
LABEL_11:
optarg = 0LL;
v7 = a2[v6];
if ( !v7 )
goto LABEL_23;
if ( *v7 != 45 || v7[1] != 45 || v7[2] )
break;
dashdash = 1;
((void (*)(void))increment_index)();
v7 = a2[argv_index];
v6 = argv_index;
if ( !v7 )
goto LABEL_23;
LABEL_6:
if ( *a3 == 45 )
{
optarg = (__int64)v7;
v9 = 1;
((void (*)(void))increment_index)();
goto LABEL_30;
}
if ( *a3 == 43 || getenv("POSIXLY_CORRECT") )
{
nonopt = a1 - v6;
goto LABEL_24;
}
if ( (unsigned int)func10() )
goto LABEL_23;
}
if ( dashdash || *v7 != 45 || !v7[1] )
goto LABEL_6;
v8 = *a3 == 45;
v9 = v7[opt_offset++];
v23 = opt_offset;
if ( v8 )
{
v11 = (FILE *)(a3 + 1);
v10 = (const char *)v9;
v12 = strchr(a3 + 1, v9);
}
else
{
v10 = (const char *)v9;
v11 = (FILE *)a3;
v12 = strchr(a3, v9);
}
v13 = v23;
v14 = v12;
if ( !v12 )
{
if ( opterr )
{
v11 = stderr;
v10 = "%s: invalid option -- %c\n";
fprintf(stderr, "%s: invalid option -- %c\n", *a2, v9);
}
optopt = v9;
v9 = 63;
func9(v11, v10, v13);
goto LABEL_30;
}
v15 = &v7[v23];
v16 = *v15;
if ( v14[1] != 58 )
{
if ( !v16 )
func9(v11, v10, v23);
goto LABEL_22;
}
if ( v16 )
{
optarg = (__int64)v15;
func9(v11, v10, v23);
}
else
{
if ( v14[2] != 58 )
{
v18 = v6;
if ( argv_index2 >= v6 )
v18 = argv_index2;
v19 = v18 + 1;
do
{
v13 = (__int64)a2[v19];
v20 = v19;
if ( !v13 )
{
argv_index2 = v19;
goto LABEL_43;
}
++v19;
}
while ( *(_BYTE *)v13 == 45 );
argv_index2 = v20;
LABEL_43:
optarg = v13;
if ( nonopt + v20 >= prev_argc )
optarg = 0LL;
}
func9(v11, v10, v13);
}
if ( !optarg && *(_BYTE *)(v21 + 2) != 58 )
{
optopt = v9;
if ( opterr )
{
v22 = v9;
v9 = 63;
fprintf(stderr, "%s: option requires an argument -- %c\n", *a2, v22);
}
else
{
v9 = 63;
}
goto LABEL_30;
}
LABEL_22:
if ( v9 == -1 )
{
LABEL_23:
v6 = a1 - nonopt;
LABEL_24:
optind = v6;
return (unsigned int)-1;
}
LABEL_30:
optind = argv_index;
return v9;
}

original_code:
int getopt(int argc, char** argv, char* optstr)
{
 int c = 0;
 if(prev_argv != argv || prev_argc != argc)
 {
  prev_argv = argv;
  prev_argc = argc;
  argv_index = 1;
  argv_index2 = 1;
  opt_offset = 1;
  dashdash = 0;
  nonopt = 0;
 }
 getopt_top:
 optarg = ((void *)0);
 if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
 {
  dashdash = 1;
  increment_index();
 }
 if(argv[argv_index] == ((void *)0))
 {
  c = -1;
 }
 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
 {
  if(optstr[0] == '-')
  {
   c = 1;
   optarg = argv[argv_index];
   increment_index();
  }
  else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
  {
   c = -1;
   nonopt = argc - argv_index;
  }
  else
  {
   if(!permute_argv_once()) goto getopt_top;
   else c = -1;
  }
 }
 else
 {
  char* opt_ptr = ((void *)0);
  c = argv[argv_index][opt_offset++];
  if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
  else opt_ptr = strchr(optstr, c);
  if(!opt_ptr)
  {
   if(opterr)
   {
    fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
   }
   optopt = c;
   c = '?';
   increment_index();
  }
  else if(opt_ptr[1] == ':')
  {
   if(argv[argv_index][opt_offset] != '\0')
   {
    optarg = &argv[argv_index][opt_offset];
    increment_index();
   }
   else if(opt_ptr[2] != ':')
   {
    if(argv_index2 < argv_index) argv_index2 = argv_index;
    while(argv[++argv_index2] && argv[argv_index2][0] == '-');
    optarg = argv[argv_index2];
    if(argv_index2 + nonopt >= prev_argc) optarg = ((void *)0);
    increment_index();
   }
   else
   {
    increment_index();
   }
   if(optarg == ((void *)0) && opt_ptr[2] != ':')
   {
    optopt = c;
    c = '?';
    if(opterr)
    {
     fprintf(stderr,"%s: option requires an argument -- %c\n",
       argv[0], optopt);
    }
   }
  }
  else
  {
   if(argv[argv_index][opt_offset] == '\0')
   {
    increment_index();
   }
  }
 }
 if(c == -1)
 {
  optind = argc - nonopt;
 }
 else
 {
  optind = argv_index;
 }
 return c;
}

canonical_original_code:
int getopt(int argc, char **argv, char *optstr)
{
 int c = 0;
 if(prev_argv != argv || prev_argc != argc)
 {
  prev_argv = argv;
  prev_argc = argc;
  argv_index = 1;
  argv_index2 = 1;
  opt_offset = 1;
  dashdash = 0;
  nonopt = 0;
 }
 getopt_top:
 optarg = ((void *)0);
 if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
 {
  dashdash = 1;
  increment_index();
 }
 if(argv[argv_index] == ((void *)0))
 {
  c = -1;
 }
 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
 {
  if(optstr[0] == '-')
  {
   c = 1;
   optarg = argv[argv_index];
   increment_index();
  }
  else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
  {
   c = -1;
   nonopt = argc - argv_index;
  }
  else
  {
   if(!permute_argv_once()) goto getopt_top;
   else c = -1;
  }
 }
 else
 {
  char *opt_ptr = ((void *)0);
  c = argv[argv_index][opt_offset++];
  if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
  else opt_ptr = strchr(optstr, c);
  if(!opt_ptr)
  {
   if(opterr)
   {
    fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
   }
   optopt = c;
   c = '?';
   increment_index();
  }
  else if(opt_ptr[1] == ':')
  {
   if(argv[argv_index][opt_offset] != '\0')
   {
    optarg = &argv[argv_index][opt_offset];
    increment_index();
   }
   else if(opt_ptr[2] != ':')
   {
    if(argv_index2 < argv_index) argv_index2 = argv_index;
    while(argv[++argv_index2] && argv[argv_index2][0] == '-');
    optarg = argv[argv_index2];
    if(argv_index2 + nonopt >= prev_argc) optarg = ((void *)0);
    increment_index();
   }
   else
   {
    increment_index();
   }
   if(optarg == ((void *)0) && opt_ptr[2] != ':')
   {
    optopt = c;
    c = '?';
    if(opterr)
    {
     fprintf(stderr,"%s: option requires an argument -- %c\n",
       argv[0], optopt);
    }
   }
  }
  else
  {
   if(argv[argv_index][opt_offset] == '\0')
   {
    increment_index();
   }
  }
 }
 if(c == -1)
 {
  optind = argc - nonopt;
 }
 else
 {
  optind = argv_index;
 }
 return c;
}

variable_types:
argv
char **

variable_types:
optstr
char *

variable_types: c int


main

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

while ( 1 )
{
v3 = getopt((unsigned int)argc, argv, "-ifrhv::o:");
if ( v3 == -1 )
break;
if ( v3 == 1 )
{
in_fname = optarg;
}
else
{
switch ( v3 )
{
case 'f':
flags |= 2u;
break;
case 'h':
help((unsigned int)argc, v3);
exit(0);
case 'i':
flags |= 1u;
break;
case 'o':
out_fname = optarg;
break;
case 'r':
flags |= 4u;
break;
case 'v':
verbose = arg_to_int(optarg, 0LL, 10LL, 5LL, "v");
break;
default:
printf("Option '%c' (%d) with '%s'\n", v3, v3, (const char *)optarg);
break;
}
}
}
printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
if ( (flags & 1) != 0 )
puts("in interactive mode");
else
puts("not in interactive mode");
if ( (flags & 2) != 0 )
puts("in force mode");
else
puts("not in force mode");
if ( (flags & 4) != 0 )
puts("in recursive mode");
else
puts("not in recursive mode");
printf("verbosity level: %d\n", (unsigned int)verbose);
if ( in_fname )
printf("input filename: %s\n", (const char *)in_fname);
else
puts("no input filename");
if ( out_fname )
printf("output filename: %s\n", (const char *)out_fname);
else
puts("no output filename");
return 0;
}

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

while ( 1 )
{
v3 = func11((unsigned int)argc, argv, "-ifrhv::o:");
if ( v3 == -1 )
break;
if ( v3 == 1 )
{
in_fname = optarg;
}
else
{
switch ( v3 )
{
case 'f':
flags |= 2u;
break;
case 'h':
func8((unsigned int)argc, v3);
exit(0);
case 'i':
flags |= 1u;
break;
case 'o':
out_fname = optarg;
break;
case 'r':
flags |= 4u;
break;
case 'v':
verbose = func7(optarg, 0LL, 10LL, 5LL, "v");
break;
default:
printf("Option '%c' (%d) with '%s'\n", v3, v3, (const char *)optarg);
break;
}
}
}
printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
if ( (flags & 1) != 0 )
puts("in interactive mode");
else
puts("not in interactive mode");
if ( (flags & 2) != 0 )
puts("in force mode");
else
puts("not in force mode");
if ( (flags & 4) != 0 )
puts("in recursive mode");
else
puts("not in recursive mode");
printf("verbosity level: %d\n", (unsigned int)verbose);
if ( in_fname )
printf("input filename: %s\n", (const char *)in_fname);
else
puts("no input filename");
if ( out_fname )
printf("output filename: %s\n", (const char *)out_fname);
else
puts("no output filename");
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 while(1) {
  int c = getopt(argc, argv, "-ifrhv::o:");
  if(c == -1) break;
  switch(c) {
   case 'i': flags |= 0x0001; break;
   case 'f': flags |= 0x0002; break;
   case 'r': flags |= 0x0004; break;
   case 'h': help(); exit(0);
   case 'v': verbose = arg_to_int(optarg, 0, 10, 5, "v"); break;
   case 'o': out_fname = optarg; break;
   case 1: in_fname = optarg; break;
   default:
    printf("Option '%c' (%d) with '%s'\n", c, c, optarg);
  }
 }
  printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
 if(flags & 0x0001) printf("in interactive mode\n");
 else printf("not in interactive mode\n");
 if(flags & 0x0002) printf("in force mode\n");
 else printf("not in force mode\n");
 if(flags & 0x0004) printf("in recursive mode\n");
 else printf("not in recursive mode\n");
 printf("verbosity level: %d\n", verbose);
 if(in_fname) printf("input filename: %s\n", in_fname);
 else printf("no input filename\n");
 if(out_fname) printf("output filename: %s\n", out_fname);
 else printf("no output filename\n");
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 while(1) {
  int c = getopt(argc, argv, "-ifrhv::o:");
  if(c == -1) break;
  switch(c) {
   case 'i': flags |= 0x0001; break;
   case 'f': flags |= 0x0002; break;
   case 'r': flags |= 0x0004; break;
   case 'h': help(); exit(0);
   case 'v': verbose = arg_to_int(optarg, 0, 10, 5, "v"); break;
   case 'o': out_fname = optarg; break;
   case 1: in_fname = optarg; break;
   default:
    printf("Option '%c' (%d) with '%s'\n", c, c, optarg);
  }
 }
  printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
 if(flags & 0x0001) printf("in interactive mode\n");
 else printf("not in interactive mode\n");
 if(flags & 0x0002) printf("in force mode\n");
 else printf("not in force mode\n");
 if(flags & 0x0004) printf("in recursive mode\n");
 else printf("not in recursive mode\n");
 printf("verbosity level: %d\n", verbose);
 if(in_fname) printf("input filename: %s\n", in_fname);
 else printf("no input filename\n");
 if(out_fname) printf("output filename: %s\n", out_fname);
 else printf("no output filename\n");
 return 0;
}


help

decompiled_code:
int help()
{
return puts(
"getopt test program\n"
"Usage: test [OPTION] [INPUT]\n"
" INPUT set input filename (doesn't do anything)\n"
" -h help menu (this screen)\n"
" -i interactive mode (doesn't do anything)\n"
" -f force mode (doesn't do anything)\n"
" -r recursive mode (doesn't do anything)\n"
" -v[level] set verbosity level (5 is default; doesn't do anything)\n"
" -o filename set output filename (doesn't do anything)");
}

canonical_decompiled_code:
int func8()
{
return puts(
"getopt test program\n"
"Usage: test [OPTION] [INPUT]\n"
" INPUT set input filename (doesn't do anything)\n"
" -h help menu (this screen)\n"
" -i interactive mode (doesn't do anything)\n"
" -f force mode (doesn't do anything)\n"
" -r recursive mode (doesn't do anything)\n"
" -v[level] set verbosity level (5 is default; doesn't do anything)\n"
" -o filename set output filename (doesn't do anything)");
}

original_code:
void help()
{
 printf(
"getopt test program\n"
"Usage: test [OPTION] [INPUT]\n"
"   INPUT           set input filename (doesn't do anything)\n"
"   -h              help menu (this screen)\n"
"   -i              interactive mode (doesn't do anything)\n"
"   -f              force mode (doesn't do anything)\n"
"   -r              recursive mode (doesn't do anything)\n"
"   -v[level]       set verbosity level (5 is default; doesn't do anything)\n"
"   -o filename     set output filename (doesn't do anything)\n"
 );
}


repo: avasyukov/mipt-cs-labyrinth-challenge

decompiled_code:
int *__fastcall do_next_step(int a1, int a2, int *a3)
{
int *result; // rax
int v5; // [rsp+14h] [rbp-Ch] BYREF
int v6; // [rsp+18h] [rbp-8h] BYREF
int v7; // [rsp+1Ch] [rbp-4h]

v7 = 0;
find_nearest_treasure(a1, a2, &v6, &v5);
printf("My coords: %d %d. Target: %d %d\n", (unsigned int)a1, (unsigned int)a2, (unsigned int)v6, (unsigned int)v5);
if ( a1 <= v6 )
{
if ( a1 >= v6 )
{
if ( a2 <= v5 )
{
if ( a2 < v5 )
{
puts("Move UP");
v7 = 2;
}
}
else
{
puts("Move DOWN");
v7 = 4;
}
}
else
{
puts("Move RIGHT");
v7 = 3;
}
}
else
{
puts("Move LEFT");
v7 = 1;
}
result = a3;
*a3 = v7;
return result;
}

canonical_decompiled_code:
int *__fastcall func1(int a1, int a2, int *a3)
{
int *result; // rax
int v5; // [rsp+14h] [rbp-Ch] BYREF
int v6; // [rsp+18h] [rbp-8h] BYREF
int v7; // [rsp+1Ch] [rbp-4h]

v7 = 0;
func0(a1, a2, &v6, &v5);
printf("My coords: %d %d. Target: %d %d\n", (unsigned int)a1, (unsigned int)a2, (unsigned int)v6, (unsigned int)v5);
if ( a1 <= v6 )
{
if ( a1 >= v6 )
{
if ( a2 <= v5 )
{
if ( a2 < v5 )
{
puts("Move UP");
v7 = 2;
}
}
else
{
puts("Move DOWN");
v7 = 4;
}
}
else
{
puts("Move RIGHT");
v7 = 3;
}
}
else
{
puts("Move LEFT");
v7 = 1;
}
result = a3;
*a3 = v7;
return result;
}

original_code:
void do_next_step(int cur_x, int cur_y, int *command)
{
 int res = rand() % 5;
 *command = res;
}

variable_types:
command
int *

variable_types: res int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int m; // [rsp+8h] [rbp-18h]
int l; // [rsp+Ch] [rbp-14h]
unsigned int v7; // [rsp+10h] [rbp-10h]
int k; // [rsp+14h] [rbp-Ch]
int j; // [rsp+18h] [rbp-8h]
int i; // [rsp+1Ch] [rbp-4h]

v3 = time(0LL);
srand(v3);
for ( i = 0; i <= 0; ++i )
(&player_data)[i] = (int *)malloc(0x10uLL);
for ( j = 0; j <= 0; ++j )
get_random_place((&player_data)[j], (&player_data)[j] + 1);
for ( k = 0; k <= 0; ++k )
{
(&player_data)[k][2] = -1;
(&player_data)[k][3] = 0;
scores[k] = 0;
update_player_map();
}
v7 = 0;
init_monsters();
init_treasures();
update_main_map();
print_map(&player_data, 1);
while ( (unsigned int)continue_game(v7) == 1 )
{
move_monsters();
update_main_map();
print_map(&player_data, 1);
for ( l = 0; l <= 0; ++l )
{
do_next_step(*(&player_data)[l], (&player_data)[l][1], (&player_data)[l] + 2);
if ( (&player_data)[l][2] != -1 )
{
scores[l] += process_next_move((&player_data)[l], (&player_data)[l] + 1, (&player_data)[l][2]);
update_player_map();
(&player_data)[l][2] = -1;
}
}
printf("Step %d\n", v7);
for ( m = 0; m <= 0; ++m )
printf("Player %d. Scores: %d\n", (unsigned int)m, (unsigned int)scores[m]);
++v7;
sleep(1u);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int m; // [rsp+8h] [rbp-18h]
int l; // [rsp+Ch] [rbp-14h]
unsigned int v7; // [rsp+10h] [rbp-10h]
int k; // [rsp+14h] [rbp-Ch]
int j; // [rsp+18h] [rbp-8h]
int i; // [rsp+1Ch] [rbp-4h]

v3 = time(0LL);
srand(v3);
for ( i = 0; i <= 0; ++i )
(&player_data)[i] = (int *)malloc(0x10uLL);
for ( j = 0; j <= 0; ++j )
func7((&player_data)[j], (&player_data)[j] + 1);
for ( k = 0; k <= 0; ++k )
{
(&player_data)[k][2] = -1;
(&player_data)[k][3] = 0;
scores[k] = 0;
func12();
}
v7 = 0;
func8();
func9();
func13();
func10(&player_data, 1);
while ( (unsigned int)func2(v7) == 1 )
{
func6();
func13();
func10(&player_data, 1);
for ( l = 0; l <= 0; ++l )
{
func1(*(&player_data)[l], (&player_data)[l][1], (&player_data)[l] + 2);
if ( (&player_data)[l][2] != -1 )
{
scores[l] += func11((&player_data)[l], (&player_data)[l] + 1, (&player_data)[l][2]);
func12();
(&player_data)[l][2] = -1;
}
}
printf("Step %d\n", v7);
for ( m = 0; m <= 0; ++m )
printf("Player %d. Scores: %d\n", (unsigned int)m, (unsigned int)scores[m]);
++v7;
sleep(1u);
}
return 0;
}

original_code:
int main()
{
 key_t data_shm_key = 4567;
 key_t server_sem_key = 5678;
 key_t player_sem_key = 7890;
 key_t map_shm_key = 6789;
 struct sembuf sem_check;
 sem_check.sem_num = 0;
 sem_check.sem_op = -1;
 sem_check.sem_flg = 04000;
 int start_sem_id = semget(server_sem_key+100, 1, 01000 | 0666);
 int res = semop(start_sem_id, &sem_check, 1);
 int i = 1;
 if( res == -1 )
  i++;
 printf("Joining as player %d\n", i);
 void *data_shm_addr = ((void *)0);
 int data_shm_size = sizeof(int) * 4;
 int data_shm_id = shmget(data_shm_key+i-1, data_shm_size, 0666);
 int* player_data = (int*)shmat(data_shm_id, data_shm_addr, 0);
 void *map_shm_addr = ((void *)0);
 int map_shm_size = sizeof(char) * 100 * 25;
 int map_shm_id = shmget(map_shm_key+i-1, map_shm_size, 0666);
 char* player_map = (char*)shmat(map_shm_id, map_shm_addr, 0);
 int server_sem_id = semget(server_sem_key+i-1, 1, 01000 | 0666);
 int player_sem_id = semget(player_sem_key+i-1, 1, 01000 | 0666);
 struct sembuf sem_down;
 sem_down.sem_num = 0;
 sem_down.sem_op = -1;
 sem_down.sem_flg = 0;
 struct sembuf sem_up;
 sem_up.sem_num = 0;
 sem_up.sem_op = 1;
 sem_up.sem_flg = 0;
 semop(player_sem_id, &sem_up, 1);
 semop(server_sem_id, &sem_down, 1);
 while(player_data[3] == 0) {
  memcpy(map, player_map, 25 * 100 * sizeof(char));
  do_next_step( player_data[0], player_data[1], &player_data[2] );
  semop(server_sem_id, &sem_down, 1);
 }
 semop(player_sem_id, &sem_up, 1);
 return 0;
}

canonical_original_code:
int main()
{
 int data_shm_key = 4567;
 int server_sem_key = 5678;
 int player_sem_key = 7890;
 int map_shm_key = 6789;
 struct sembuf sem_check;
 sem_check.sem_num = 0;
 sem_check.sem_op = -1;
 sem_check.sem_flg = 04000;
 int start_sem_id = semget(server_sem_key+100, 1, 01000 | 0666);
 int res = semop(start_sem_id, &sem_check, 1);
 int i = 1;
 if( res == -1 )
  i++;
 printf("Joining as player %d\n", i);
 void *data_shm_addr = ((void *)0);
 int data_shm_size = sizeof(int) * 4;
 int data_shm_id = shmget(data_shm_key+i-1, data_shm_size, 0666);
 int *player_data = (int*)shmat(data_shm_id, data_shm_addr, 0);
 void *map_shm_addr = ((void *)0);
 int map_shm_size = sizeof(char) * 100 * 25;
 int map_shm_id = shmget(map_shm_key+i-1, map_shm_size, 0666);
 char *player_map = (char*)shmat(map_shm_id, map_shm_addr, 0);
 int server_sem_id = semget(server_sem_key+i-1, 1, 01000 | 0666);
 int player_sem_id = semget(player_sem_key+i-1, 1, 01000 | 0666);
 struct sembuf sem_down;
 sem_down.sem_num = 0;
 sem_down.sem_op = -1;
 sem_down.sem_flg = 0;
 struct sembuf sem_up;
 sem_up.sem_num = 0;
 sem_up.sem_op = 1;
 sem_up.sem_flg = 0;
 semop(player_sem_id, &sem_up, 1);
 semop(server_sem_id, &sem_down, 1);
 while(player_data[3] == 0) {
  memcpy(map, player_map, 25 * 100 * sizeof(char));
  do_next_step( player_data[0], player_data[1], &player_data[2] );
  semop(server_sem_id, &sem_down, 1);
 }
 semop(player_sem_id, &sem_up, 1);
 return 0;
}

variable_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }

variable_types: i int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebp
int v5; // eax
unsigned int *v6; // r15
int v7; // eax
void *v8; // rbx
int v9; // er12
int v10; // eax
int v11; // er14
struct sembuf sops; // [rsp+Eh] [rbp-4Ah] BYREF
struct sembuf v14; // [rsp+14h] [rbp-44h] BYREF
struct sembuf v15; // [rsp+1Ah] [rbp-3Eh] BYREF

sops.sem_flg = 2048;
*(_DWORD *)&sops.sem_num = -65536;
v3 = semget(5778, 1, 950);
v4 = (semop(v3, &sops, 1uLL) == -1) + 1;
printf("Joining as player %d\n", v4);
v5 = shmget(v4 + 4566, 0x10uLL, 438);
v6 = (unsigned int *)shmat(v5, 0LL, 0);
v7 = shmget(v4 + 6788, 0x9C4uLL, 438);
v8 = shmat(v7, 0LL, 0);
v9 = semget(v4 + 5677, 1, 950);
v10 = semget(v4 + 7889, 1, 950);
v15.sem_flg = 0;
v11 = v10;
v14.sem_flg = 0;
*(_DWORD *)&v14.sem_num = -65536;
*(_DWORD *)&v15.sem_num = 0x10000;
semop(v10, &v15, 1uLL);
semop(v9, &v14, 1uLL);
while ( !v6[3] )
{
qmemcpy(&map, v8, 0x9C4uLL);
do_next_step(*v6, v6[1], v6 + 2);
semop(v9, &v14, 1uLL);
}
semop(v11, &v15, 1uLL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebp
int v5; // eax
unsigned int *v6; // r15
int v7; // eax
void *v8; // rbx
int v9; // er12
int v10; // eax
int v11; // er14
struct sembuf sops; // [rsp+Eh] [rbp-4Ah] BYREF
struct sembuf v14; // [rsp+14h] [rbp-44h] BYREF
struct sembuf v15; // [rsp+1Ah] [rbp-3Eh] BYREF

sops.sem_flg = 2048;
*(_DWORD *)&sops.sem_num = -65536;
v3 = semget(5778, 1, 950);
v4 = (semop(v3, &sops, 1uLL) == -1) + 1;
printf("Joining as player %d\n", v4);
v5 = shmget(v4 + 4566, 0x10uLL, 438);
v6 = (unsigned int *)shmat(v5, 0LL, 0);
v7 = shmget(v4 + 6788, 0x9C4uLL, 438);
v8 = shmat(v7, 0LL, 0);
v9 = semget(v4 + 5677, 1, 950);
v10 = semget(v4 + 7889, 1, 950);
v15.sem_flg = 0;
v11 = v10;
v14.sem_flg = 0;
*(_DWORD *)&v14.sem_num = -65536;
*(_DWORD *)&v15.sem_num = 0x10000;
semop(v10, &v15, 1uLL);
semop(v9, &v14, 1uLL);
while ( !v6[3] )
{
qmemcpy(&map, v8, 0x9C4uLL);
func7(*v6, v6[1], v6 + 2);
semop(v9, &v14, 1uLL);
}
semop(v11, &v15, 1uLL);
return 0;
}

user_defined_types: struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }


decompiled_code:
__int64 __fastcall do_next_step(__int64 a1, __int64 a2, int *a3)
{
int v4; // edx
__int64 result; // rax

v4 = rand();
result = (unsigned int)(5 * (v4 / 5));
*a3 = v4 % 5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int *a3)
{
int v4; // edx
__int64 result; // rax

v4 = rand();
result = (unsigned int)(5 * (v4 / 5));
*a3 = v4 % 5;
return result;
}

canonical_original_code:
void do_next_step(int cur_x, int cur_y, int *command)
{
 int res = rand() % 5;
 *command = res;
}


repo: avgo/avgmoney

decompiled_code:
__int64 __fastcall CUList_ForEach(__int64 *a1, __int64 (__fastcall *a2)(__int64, __int64), __int64 a3)
{
__int64 v3; // rbx
__int64 result; // rax

v3 = *a1;
if ( !*a1 )
return 1LL;
for ( result = a2(v3, a3); (_DWORD)result; result = a2(v3, a3) )
{
v3 = *(_QWORD *)(v3 + 24);
if ( !v3 )
return 1LL;
}
return result;
}

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

v3 = *a1;
if ( !*a1 )
return 1LL;
for ( result = a2(v3, a3); (_DWORD)result; result = a2(v3, a3) )
{
v3 = *(_QWORD *)(v3 + 24);
if ( !v3 )
return 1LL;
}
return result;
}

original_code:
int CUList_ForEach(CUList* list, int (*f)(CUListNode*, void*), void* arg)
{
 CUListNode* CurNode;
 for (CurNode = list->first; CurNode != ((void *)0); CurNode = CurNode->next)
  if (!f(CurNode, arg))
   return 0;
 return 1;
}

canonical_original_code:
int CUList_ForEach(struct CUList_ *list, int (*f)(struct CUListNode_ *, void *), void *arg)
{
 struct CUListNode_ *CurNode;
 for (CurNode = list->first; CurNode != ((void *)0); CurNode = CurNode->next)
  if (!f(CurNode, arg))
   return 0;
 return 1;
}

variable_types: struct CUList_ { struct CUListNode_ { unsigned int id; unsigned int parent_id; unsigned int old_id; unsigned int old_parent_id; unsigned int middle; struct CUListNode_ *next; } *first; struct CUListNode_ *last; } *

variable_types:
f
int (*)(struct CUListNode_ *, void *)

variable_types:
arg
void *

user_defined_types: struct CUList_ { struct CUListNode_ *first; struct CUListNode_ *last; }


decompiled_code:
void __fastcall CUList_Free(_QWORD *a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[3];
free(v3);
}
while ( v2 );
}
CUList_Init(a1);
}

canonical_decompiled_code:
void __fastcall func6(_QWORD *a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[3];
free(v3);
}
while ( v2 );
}
func4(a1);
}

original_code:
void CUList_Free(CUList* list)
{
 CUListNode* node;
 CUListNode* next;
 for (node = list->first; node != ((void *)0); )
 {
  next = node->next;
  free(node);
  node = next;
 }
 CUList_Init(list);
}

canonical_original_code:
void CUList_Free(struct CUList_ *list)
{
 struct CUListNode_ *node;
 struct CUListNode_ *next;
 for (node = list->first; node != ((void *)0); )
 {
  next = node->next;
  free(node);
  node = next;
 }
 CUList_Init(list);
}

variable_types: struct CUListNode_ { unsigned int id; unsigned int parent_id; unsigned int old_id; unsigned int old_parent_id; unsigned int middle; struct CUListNode_ *next; } *


decompiled_code:
int __fastcall CUList_Print(unsigned int **a1)
{
unsigned int *i; // rbx
int result; // eax

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 3) )
result = printf(
"id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d\n",
*i,
i[1],
i[2],
i[3],
i[4]);
return result;
}

canonical_decompiled_code:
int __fastcall func8(unsigned int **a1)
{
unsigned int *i; // rbx
int result; // eax

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 3) )
result = printf(
"id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d\n",
*i,
i[1],
i[2],
i[3],
i[4]);
return result;
}

original_code:
void CUList_Print(CUList* list)
{
 CUListNode* CurNode;
 for (CurNode = list->first; CurNode != ((void *)0); CurNode = CurNode->next)
 {
  printf("id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d\n",
    CurNode->id, CurNode->parent_id, CurNode->old_id, CurNode->old_parent_id, CurNode->middle);
 }
}

canonical_original_code:
void CUList_Print(struct CUList_ *list)
{
 struct CUListNode_ *CurNode;
 for (CurNode = list->first; CurNode != ((void *)0); CurNode = CurNode->next)
 {
  printf("id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d\n",
    CurNode->id, CurNode->parent_id, CurNode->old_id, CurNode->old_parent_id, CurNode->middle);
 }
}

user_defined_types: struct CUListNode_ { unsigned int id; unsigned int parent_id; unsigned int old_id; unsigned int old_parent_id; unsigned int middle; struct CUListNode_ *next; }


decompiled_code:
void *CUListNode_Alloc()
{
void *v0; // r12

v0 = malloc(0x20uLL);
if ( !v0 )
fwrite("NewNode = NULL\n", 1uLL, 0xFuLL, stderr);
return v0;
}

canonical_decompiled_code:
void *func10()
{
void *v0; // r12

v0 = malloc(0x20uLL);
if ( !v0 )
fwrite("NewNode = NULL\n", 1uLL, 0xFuLL, stderr);
return v0;
}

original_code:
CUListNode* CUListNode_Alloc()
{
 CUListNode* NewNode;
 NewNode = (CUListNode*) malloc(sizeof(CUListNode));
 if (NewNode == ((void *)0))
 {
  fprintf(stderr, "NewNode = NULL\n");
 }
 return NewNode;
}

canonical_original_code:
struct CUListNode_ *CUListNode_Alloc()
{
 struct CUListNode_ *NewNode;
 NewNode = (struct CUListNode_ *) malloc(sizeof(CUListNode));
 if (NewNode == ((void *)0))
 {
  fprintf(stderr, "NewNode = NULL\n");
 }
 return NewNode;
}

return_type: struct CUListNode_ { unsigned int id; unsigned int parent_id; unsigned int old_id; unsigned int old_parent_id; unsigned int middle; struct CUListNode_ *next; } *


decompiled_code:
__int64 __fastcall CUListNode_Print1(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
a1[4],
a1[1],
a1[2],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
a1[4],
a1[1],
a1[2],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

original_code:
int CUListNode_Print1(CUListNode* ListNode, void* arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->middle, ListNode->parent_id, ListNode->old_id,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}

canonical_original_code:
int CUListNode_Print1(struct CUListNode_ *ListNode, void *arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->middle, ListNode->parent_id, ListNode->old_id,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}


decompiled_code:
__int64 __fastcall CUListNode_Print2(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
*a1,
a1[1],
a1[2],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
*a1,
a1[1],
a1[2],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

original_code:
int CUListNode_Print2(CUListNode* ListNode, void* arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->id, ListNode->parent_id, ListNode->old_id,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}

canonical_original_code:
int CUListNode_Print2(struct CUListNode_ *ListNode, void *arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->id, ListNode->parent_id, ListNode->old_id,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}


decompiled_code:
__int64 __fastcall CUListNode_Print3(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
*a1,
a1[1],
a1[4],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int *a1)
{
printf(
"UPDATE categories SET id = %d, parent_id = %d WHERE id = %d; (id = %d, parent_id = %d, old_id = %d, old_parent_id"
" = %d, middle = %d).\n",
*a1,
a1[1],
a1[4],
*a1,
a1[1],
a1[2],
a1[3],
a1[4]);
return 1LL;
}

original_code:
int CUListNode_Print3(CUListNode* ListNode, void* arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->id, ListNode->parent_id, ListNode->middle,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}

canonical_original_code:
int CUListNode_Print3(struct CUListNode_ *ListNode, void *arg)
{
 printf("UPDATE categories SET id = %d, parent_id = %d WHERE id = %d;    "
   "(id = %d, parent_id = %d, old_id = %d, old_parent_id = %d, middle = %d).\n",
   ListNode->id, ListNode->parent_id, ListNode->middle,
   ListNode->id, ListNode->parent_id, ListNode->old_id, ListNode->old_parent_id, ListNode->middle);
 return 1;
}


decompiled_code:
void *AvgMoney_Create()
{
void *result; // rax

if ( AvgMoneyApp || (result = malloc(0x750uLL), (AvgMoneyApp = (__int64)result) == 0) )
{
g_print("error\n");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *func1()
{
void *result; // rax

if ( AvgMoneyApp || (result = malloc(0x750uLL), (AvgMoneyApp = (__int64)result) == 0) )
{
g_print("error\n");
exit(1);
}
return result;
}

original_code:
void AvgMoney_Create()
{
 if (AvgMoneyApp != ((void *)0))
 {
  g_print("error\n");
  exit(1);
 }
 AvgMoneyApp = malloc(sizeof(AvgMoney));
 if (AvgMoneyApp == ((void *)0))
 {
  g_print("error\n");
  exit(1);
 }
}


decompiled_code:
__int64 AvgMoney_MySQLPrepare()
{
unsigned int v1; // er12
const char *v2; // rax

if ( !mysql_init(AvgMoneyApp) )
{
g_print("AvgMoney_MySQLPrepare(): \n");
exit(1);
}
mysql_options(AvgMoneyApp, 7LL, "utf8");
if ( !mysql_real_connect(AvgMoneyApp, &locale, &locale, &locale, 0LL, 0LL, 0LL, 0LL) )
{
v1 = mysql_errno(AvgMoneyApp);
v2 = (const char *)mysql_error(AvgMoneyApp);
g_print("%s() %s (%u).\n", "AvgMoney_MySQLPrepare", v2, v1);
exit(1);
}
return AvgMoney_CheckCreateDB();
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v1; // er12
const char *v2; // rax

if ( !mysql_init(AvgMoneyApp) )
{
g_print("AvgMoney_MySQLPrepare(): \n");
exit(1);
}
mysql_options(AvgMoneyApp, 7LL, "utf8");
if ( !mysql_real_connect(AvgMoneyApp, &locale, &locale, &locale, 0LL, 0LL, 0LL, 0LL) )
{
v1 = mysql_errno(AvgMoneyApp);
v2 = (const char *)mysql_error(AvgMoneyApp);
g_print("%s() %s (%u).\n", "AvgMoney_MySQLPrepare", v2, v1);
exit(1);
}
return func0();
}

original_code:
void AvgMoney_MySQLPrepare()
{
 if (mysql_init(&AvgMoneyApp->MySQL) == ((void *)0))
 {
  g_print("AvgMoney_MySQLPrepare(): \n");
  exit(1);
 }
 mysql_options(&AvgMoneyApp->MySQL, MYSQL_SET_CHARSET_NAME, "utf8");
 if (mysql_real_connect(&AvgMoneyApp->MySQL, "", "", "", ((void *)0),
   0, ((void *)0), 0) == ((void *)0))
 {
  g_print("%s() %s (%u).\n", __func__,
    mysql_error(&AvgMoneyApp->MySQL),
    mysql_errno(&AvgMoneyApp->MySQL));
  exit(1);
 }
 AvgMoney_CheckCreateDB();
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v4; // [rsp+0h] [rbp-18h] BYREF
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = argc;
v4 = argv;
gtk_init(v5, &v4, envp);
setlocale(6, &locale);
setlocale(1, "C");
AvgMoney_Create();
AvgMoney_MySQLPrepare();
FormAccounts_Init(AvgMoneyApp + 1272);
FormAccounts_Create(AvgMoneyApp + 1272);
FormAccounts_UpdateData(AvgMoneyApp + 1272);
gtk_main();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v4; // [rsp+0h] [rbp-18h] BYREF
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = argc;
v4 = argv;
gtk_init(v5, &v4, envp);
setlocale(6, &locale);
setlocale(1, "C");
func1();
func2();
FormAccounts_Init(AvgMoneyApp + 1272);
FormAccounts_Create(AvgMoneyApp + 1272);
FormAccounts_UpdateData(AvgMoneyApp + 1272);
gtk_main();
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 gtk_init(&argc, &argv);
 setlocale(6, "");
 setlocale(1, "C");
 AvgMoney_Create();
 AvgMoney_MySQLPrepare();
 FormAccounts_Init(&AvgMoneyApp->form_accounts);
 FormAccounts_Create(&AvgMoneyApp->form_accounts, &AvgMoneyApp->MySQL);
 FormAccounts_UpdateData(&AvgMoneyApp->form_accounts);
 gtk_main();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 gtk_init(&argc, &argv);
 setlocale(6, "");
 setlocale(1, "C");
 AvgMoney_Create();
 AvgMoney_MySQLPrepare();
 FormAccounts_Init(&AvgMoneyApp->form_accounts);
 FormAccounts_Create(&AvgMoneyApp->form_accounts, &AvgMoneyApp->MySQL);
 FormAccounts_UpdateData(&AvgMoneyApp->form_accounts);
 gtk_main();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


DateStrToDMY

decompiled_code:
__int64 __fastcall DateStrToDMY(char *nptr, _DWORD *a2, _DWORD *a3, _DWORD *a4)
{
int *v6; // rax
int *v7; // r14
char *v8; // rax
__int64 result; // rax
char *v10; // rbx
char *v11; // rax
char *v12; // rbx
char *endptr[6]; // [rsp+0h] [rbp-30h] BYREF

if ( !nptr )
return 0xFFFFFFFFLL;
v6 = _errno_location();
*v6 = 0;
v7 = v6;
*a2 = strtoul(nptr, endptr, 10);
v8 = endptr[0];
if ( endptr[0] > nptr
&& *endptr[0] == 45
&& (*v7 = 0, v10 = v8 + 1, *a3 = strtoul(v8 + 1, endptr, 10), v11 = endptr[0], endptr[0] > v10)
&& *endptr[0] == 45
&& (*v7 = 0, v12 = v11 + 1, *a4 = strtoul(v11 + 1, endptr, 10), endptr[0] > v12) )
{
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *nptr, _DWORD *a2, _DWORD *a3, _DWORD *a4)
{
int *v6; // rax
int *v7; // r14
char *v8; // rax
__int64 result; // rax
char *v10; // rbx
char *v11; // rax
char *v12; // rbx
char *endptr[6]; // [rsp+0h] [rbp-30h] BYREF

if ( !nptr )
return 0xFFFFFFFFLL;
v6 = _errno_location();
*v6 = 0;
v7 = v6;
*a2 = strtoul(nptr, endptr, 10);
v8 = endptr[0];
if ( endptr[0] > nptr
&& *endptr[0] == 45
&& (*v7 = 0, v10 = v8 + 1, *a3 = strtoul(v8 + 1, endptr, 10), v11 = endptr[0], endptr[0] > v10)
&& *endptr[0] == 45
&& (*v7 = 0, v12 = v11 + 1, *a4 = strtoul(v11 + 1, endptr, 10), endptr[0] > v12) )
{
result = 1LL;
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int DateStrToDMY(char* DateStr, unsigned int* Year, unsigned int* Month, unsigned int* Day)
{
 char* DateStr1;
 char* DateStr2;
 if (DateStr == ((void *)0))
 {
  return -1;
 }
 DateStr1 = DateStr; (*__errno_location ()) = 0;
 *Year = strtoul(DateStr1, &DateStr2, 10);
 if (DateStr1 < DateStr2 && *DateStr2 == '-')
 {
  DateStr1 = DateStr2 + 1; (*__errno_location ()) = 0;
  *Month = strtoul(DateStr1, &DateStr2, 10);
  if (DateStr1 < DateStr2 && *DateStr2 == '-')
  {
   DateStr1 = DateStr2 + 1; (*__errno_location ()) = 0;
   *Day = strtoul(DateStr1, &DateStr2, 10);
   if (DateStr1 < DateStr2)
   {
    return 1;
   }
  }
 }
 return -1;
}

canonical_original_code:
int DateStrToDMY(char *DateStr, unsigned int *Year, unsigned int *Month, unsigned int *Day)
{
 char *DateStr1;
 char *DateStr2;
 if (DateStr == ((void *)0))
 {
  return -1;
 }
 DateStr1 = DateStr; (*__errno_location ()) = 0;
 *Year = strtoul(DateStr1, &DateStr2, 10);
 if (DateStr1 < DateStr2 && *DateStr2 == '-')
 {
  DateStr1 = DateStr2 + 1; (*__errno_location ()) = 0;
  *Month = strtoul(DateStr1, &DateStr2, 10);
  if (DateStr1 < DateStr2 && *DateStr2 == '-')
  {
   DateStr1 = DateStr2 + 1; (*__errno_location ()) = 0;
   *Day = strtoul(DateStr1, &DateStr2, 10);
   if (DateStr1 < DateStr2)
   {
    return 1;
   }
  }
 }
 return -1;
}

variable_types:
DateStr
char *

variable_types:
Year
unsigned int *

variable_types:
Month
unsigned int *

variable_types:
Day
unsigned int *


decompiled_code:
void FormCategories_TreeViewCategoriesRowActivated()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void FormCategories_TreeViewCategoriesRowActivated(unsigned int id, char* Desc, void* arg)
{
}

canonical_original_code:
void FormCategories_TreeViewCategoriesRowActivated(unsigned int id, char *Desc, void *arg)
{
}

variable_types:
id
unsigned int

variable_types:
Desc
char *


Calc

decompiled_code:
__int64 __fastcall Calc(const char *a1, double *a2)
{
double v2; // xmm0_8
char *v3; // rcx
char v4; // dl
char *v5; // rax
char *v6; // rdi
char *v7; // rax
double v8; // xmm0_8
char *v9; // rax
__int64 result; // rax
double v11; // [rsp+0h] [rbp-38h]
__int64 v12; // [rsp+8h] [rbp-30h]
char *endptr; // [rsp+10h] [rbp-28h] BYREF
char *v14; // [rsp+18h] [rbp-20h] BYREF

*a2 = 0.0;
v2 = strtod(a1, &endptr);
v3 = endptr;
v11 = v2;
if ( endptr != a1 )
{
v4 = *endptr;
if ( *endptr == 32 )
{
v5 = endptr + 1;
do
{
endptr = v5;
v3 = v5;
v4 = *v5++;
}
while ( v4 == 32 );
}
LABEL_5:
if ( !v4 )
{
LABEL_16:
result = 1LL;
*a2 = v11;
return result;
}
while ( 1 )
{
if ( v4 == 43 )
{
v6 = v3 + 1;
endptr = v3 + 1;
v12 = LC0;
}
else
{
v6 = endptr;
if ( v4 == 45 )
{
v6 = v3 + 1;
endptr = v3 + 1;
v12 = LC1;
}
}
v7 = v6 + 1;
if ( *v6 == 32 )
{
do
{
endptr = v7;
v6 = v7++;
}
while ( *(v7 - 1) == 32 );
}
v8 = strtod(v6, &v14);
v3 = v14;
if ( endptr == v14 )
break;
endptr = v14;
v4 = *v14;
v11 = v8 * *(double *)&v12 + v11;
if ( *v14 != 32 )
goto LABEL_5;
v9 = v14 + 1;
do
{
endptr = v9;
v3 = v9;
v4 = *v9++;
}
while ( v4 == 32 );
if ( !v4 )
goto LABEL_16;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, double *a2)
{
double v2; // xmm0_8
char *v3; // rcx
char v4; // dl
char *v5; // rax
char *v6; // rdi
char *v7; // rax
double v8; // xmm0_8
char *v9; // rax
__int64 result; // rax
double v11; // [rsp+0h] [rbp-38h]
__int64 v12; // [rsp+8h] [rbp-30h]
char *endptr; // [rsp+10h] [rbp-28h] BYREF
char *v14; // [rsp+18h] [rbp-20h] BYREF

*a2 = 0.0;
v2 = strtod(a1, &endptr);
v3 = endptr;
v11 = v2;
if ( endptr != a1 )
{
v4 = *endptr;
if ( *endptr == 32 )
{
v5 = endptr + 1;
do
{
endptr = v5;
v3 = v5;
v4 = *v5++;
}
while ( v4 == 32 );
}
LABEL_5:
if ( !v4 )
{
LABEL_16:
result = 1LL;
*a2 = v11;
return result;
}
while ( 1 )
{
if ( v4 == 43 )
{
v6 = v3 + 1;
endptr = v3 + 1;
v12 = LC0;
}
else
{
v6 = endptr;
if ( v4 == 45 )
{
v6 = v3 + 1;
endptr = v3 + 1;
v12 = LC1;
}
}
v7 = v6 + 1;
if ( *v6 == 32 )
{
do
{
endptr = v7;
v6 = v7++;
}
while ( *(v7 - 1) == 32 );
}
v8 = strtod(v6, &v14);
v3 = v14;
if ( endptr == v14 )
break;
endptr = v14;
v4 = *v14;
v11 = v8 * *(double *)&v12 + v11;
if ( *v14 != 32 )
goto LABEL_5;
v9 = v14 + 1;
do
{
endptr = v9;
v3 = v9;
v4 = *v9++;
}
while ( v4 == 32 );
if ( !v4 )
goto LABEL_16;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int Calc(const char* Str, double* Result)
{
 char* cp1;
 char* cp2;
 double d1, d2, sign;
 *Result = 0;
 d2 = strtod(Str, &cp1);
 if (cp1 == Str)
 {
  return -1;
 }
 while (*cp1 == ' ')
 {
  ++cp1;
 }
 while (*cp1 != '\0')
 {
  if (*cp1 == '+')
  {
   sign = 1;
   ++cp1;
  }
  else
  if (*cp1 == '-')
  {
   sign = -1;
   ++cp1;
  }
  while (*cp1 == ' ')
  {
   ++cp1;
  }
  d1 = strtod(cp1, &cp2);
  if (cp1 == cp2)
  {
   return -1;
  }
  d2 = d2 + sign * d1;
  cp1 = cp2;
  while (*cp1 == ' ')
  {
   ++cp1;
  }
 }
 *Result = d2; return 1;
}

canonical_original_code:
int Calc(char *Str, double *Result)
{
 char *cp1;
 char *cp2;
 double d1; double d2; double sign;
 *Result = 0;
 d2 = strtod(Str, &cp1);
 if (cp1 == Str)
 {
  return -1;
 }
 while (*cp1 == ' ')
 {
  ++cp1;
 }
 while (*cp1 != '\0')
 {
  if (*cp1 == '+')
  {
   sign = 1;
   ++cp1;
  }
  else
  if (*cp1 == '-')
  {
   sign = -1;
   ++cp1;
  }
  while (*cp1 == ' ')
  {
   ++cp1;
  }
  d1 = strtod(cp1, &cp2);
  if (cp1 == cp2)
  {
   return -1;
  }
  d2 = d2 + sign * d1;
  cp1 = cp2;
  while (*cp1 == ' ')
  {
   ++cp1;
  }
 }
 *Result = d2; return 1;
}

variable_types:
Str
char *

variable_types:
Result
double *

variable_types: sign double


repo: avsej/vcprompt

decompiled_code:
int __fastcall parse_args(int argc, char **argv, __int64 a3)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "f:d");
if ( result == -1 )
break;
if ( result == 100 )
{
*(_DWORD *)a3 = 1;
}
else if ( result == 102 )
{
*(_QWORD *)(a3 + 8) = optarg;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int argc, char **argv, __int64 a3)
{
int result; // eax

while ( 1 )
{
result = getopt(argc, argv, "f:d");
if ( result == -1 )
break;
if ( result == 100 )
{
*(_DWORD *)a3 = 1;
}
else if ( result == 102 )
{
*(_QWORD *)(a3 + 8) = optarg;
}
}
return result;
}

original_code:
void parse_args(int argc, char** argv, options_t* options)
{
    int opt;
    while ((opt = getopt(argc, argv, "f:d")) != -1) {
        switch (opt) {
            case 'f':
                options->format = optarg;
                break;
            case 'd':
                options->debug = 1;
                break;
        }
    }
}

canonical_original_code:
void parse_args(int argc, char **argv, struct options_t *options)
{
    int opt;
    while ((opt = getopt(argc, argv, "f:d")) != -1) {
        switch (opt) {
            case 'f':
                options->format = optarg;
                break;
            case 'd':
                options->debug = 1;
                break;
        }
    }
}

variable_types:
argv
char **

variable_types: struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *

variable_types: opt int


decompiled_code:
__int64 __fastcall parse_format(__int64 a1)
{
const char *v1; // r12
int v2; // ebx
size_t v3; // rdx
__int64 result; // rax
char v5; // al

v1 = *(const char **)(a1 + 8);
v2 = 0;
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = 0LL;
v3 = strlen(v1);
while ( 1 )
{
result = v2;
if ( v2 >= v3 )
return result;
if ( v1[v2] != 37 )
goto LABEL_2;
v5 = v1[++v2];
if ( v5 > 117 )
{
LABEL_10:
fprintf(stderr, "error: invalid format string: %%%c\n", (unsigned int)v5);
v3 = strlen(v1);
goto LABEL_2;
}
if ( v5 <= 97 )
{
if ( v5 && v5 != 37 )
goto LABEL_10;
}
else
{
switch ( v5 )
{
case 'b':
*(_DWORD *)(a1 + 16) = 1;
v3 = strlen(v1);
break;
case 'm':
*(_DWORD *)(a1 + 28) = 1;
v3 = strlen(v1);
break;
case 'n':
break;
case 'r':
*(_DWORD *)(a1 + 20) = 1;
v3 = strlen(v1);
break;
case 'u':
*(_DWORD *)(a1 + 24) = 1;
v3 = strlen(v1);
break;
default:
goto LABEL_10;
}
}
LABEL_2:
++v2;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
const char *v1; // r12
int v2; // ebx
size_t v3; // rdx
__int64 result; // rax
char v5; // al

v1 = *(const char **)(a1 + 8);
v2 = 0;
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = 0LL;
v3 = strlen(v1);
while ( 1 )
{
result = v2;
if ( v2 >= v3 )
return result;
if ( v1[v2] != 37 )
goto LABEL_2;
v5 = v1[++v2];
if ( v5 > 117 )
{
LABEL_10:
fprintf(stderr, "error: invalid format string: %%%c\n", (unsigned int)v5);
v3 = strlen(v1);
goto LABEL_2;
}
if ( v5 <= 97 )
{
if ( v5 && v5 != 37 )
goto LABEL_10;
}
else
{
switch ( v5 )
{
case 'b':
*(_DWORD *)(a1 + 16) = 1;
v3 = strlen(v1);
break;
case 'm':
*(_DWORD *)(a1 + 28) = 1;
v3 = strlen(v1);
break;
case 'n':
break;
case 'r':
*(_DWORD *)(a1 + 20) = 1;
v3 = strlen(v1);
break;
case 'u':
*(_DWORD *)(a1 + 24) = 1;
v3 = strlen(v1);
break;
default:
goto LABEL_10;
}
}
LABEL_2:
++v2;
}
}

original_code:
void parse_format(options_t* options)
{
    int i;
    options->show_branch = 0;
    options->show_unknown = 0;
    options->show_modified = 0;
    char* format = options->format;
    for (i = 0; i < strlen(format); i++) {
        if (format[i] == '%') {
            i++;
            switch (format[i]) {
                case '\0':
                    break;
                case 'b':
                    options->show_branch = 1;
                    break;
                case 'r':
                    options->show_revision = 1;
                    break;
                case 'u':
                    options->show_unknown = 1;
                    break;
                case 'm':
                    options->show_modified = 1;
                    break;
                case 'n':
                case '%':
                    break;
                default:
                    fprintf(stderr,
                            "error: invalid format string: %%%c\n",
                            format[i]);
                    break;
            }
        }
    }
}

canonical_original_code:
void parse_format(struct options_t *options)
{
    int i;
    options->show_branch = 0;
    options->show_unknown = 0;
    options->show_modified = 0;
    char *format = options->format;
    for (i = 0; i < strlen(format); i++) {
        if (format[i] == '%') {
            i++;
            switch (format[i]) {
                case '\0':
                    break;
                case 'b':
                    options->show_branch = 1;
                    break;
                case 'r':
                    options->show_revision = 1;
                    break;
                case 'u':
                    options->show_unknown = 1;
                    break;
                case 'm':
                    options->show_modified = 1;
                    break;
                case 'n':
                case '%':
                    break;
                default:
                    fprintf(stderr,
                            "error: invalid format string: %%%c\n",
                            format[i]);
                    break;
            }
        }
    }
}

variable_types: i int

variable_types:
format
char *

user_defined_types: struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; }


decompiled_code:
size_t __fastcall print_result(const char **a1, __int64 a2, __int64 a3)
{
int v5; // ebx
const char *v6; // rbp
size_t result; // rax
__int64 v8; // rdx
int v9; // edi
const char *v10; // rdi

v5 = 0;
v6 = *(const char **)(a2 + 8);
result = strlen(v6);
v8 = 0LL;
if ( result )
{
do
{
v9 = v6[v8];
if ( v6[v8] != 37 )
goto LABEL_6;
v9 = v6[++v5];
if ( v6[v5] > 117 )
goto LABEL_6;
if ( (char)v9 > 97 )
{
switch ( (char)v9 )
{
case 'b':
v10 = *(const char **)a3;
if ( !*(_QWORD *)a3 )
goto LABEL_7;
goto LABEL_12;
case 'm':
if ( *(_DWORD *)(a3 + 20) )
{
putc(33, stdout);
result = strlen(v6);
}
goto LABEL_7;
case 'n':
fputs(*a1, stdout);
result = strlen(v6);
goto LABEL_7;
case 'r':
v10 = *(const char **)(a3 + 8);
if ( v10 )
{
LABEL_12:
fputs(v10, stdout);
result = strlen(v6);
}
break;
case 'u':
if ( *(_DWORD *)(a3 + 16) )
{
putc(63, stdout);
result = strlen(v6);
}
break;
default:
goto LABEL_6;
}
goto LABEL_7;
}
if ( (_BYTE)v9 == 37 || (_BYTE)v9 == 48 )
{
putc(37, stdout);
result = strlen(v6);
}
else
{
LABEL_6:
putc(v9, stdout);
result = strlen(v6);
}
LABEL_7:
v8 = ++v5;
}
while ( v5 < result );
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func2(const char **a1, __int64 a2, __int64 a3)
{
int v5; // ebx
const char *v6; // rbp
size_t result; // rax
__int64 v8; // rdx
int v9; // edi
const char *v10; // rdi

v5 = 0;
v6 = *(const char **)(a2 + 8);
result = strlen(v6);
v8 = 0LL;
if ( result )
{
do
{
v9 = v6[v8];
if ( v6[v8] != 37 )
goto LABEL_6;
v9 = v6[++v5];
if ( v6[v5] > 117 )
goto LABEL_6;
if ( (char)v9 > 97 )
{
switch ( (char)v9 )
{
case 'b':
v10 = *(const char **)a3;
if ( !*(_QWORD *)a3 )
goto LABEL_7;
goto LABEL_12;
case 'm':
if ( *(_DWORD *)(a3 + 20) )
{
putc(33, stdout);
result = strlen(v6);
}
goto LABEL_7;
case 'n':
fputs(*a1, stdout);
result = strlen(v6);
goto LABEL_7;
case 'r':
v10 = *(const char **)(a3 + 8);
if ( v10 )
{
LABEL_12:
fputs(v10, stdout);
result = strlen(v6);
}
break;
case 'u':
if ( *(_DWORD *)(a3 + 16) )
{
putc(63, stdout);
result = strlen(v6);
}
break;
default:
goto LABEL_6;
}
goto LABEL_7;
}
if ( (_BYTE)v9 == 37 || (_BYTE)v9 == 48 )
{
putc(37, stdout);
result = strlen(v6);
}
else
{
LABEL_6:
putc(v9, stdout);
result = strlen(v6);
}
LABEL_7:
v8 = ++v5;
}
while ( v5 < result );
}
return result;
}

original_code:
void print_result(vccontext_t* context, options_t* options, result_t* result)
{
    int i;
    char* format = options->format;
    for (i = 0; i < strlen(format); i++) {
        if (format[i] == '%') {
            i++;
            switch (format[i]) {
                case '0':
                case '%':
                    putc('%', stdout);
                    break;
                case 'b':
                    if (result->branch != ((void *)0))
                        fputs(result->branch, stdout);
                    break;
                case 'r':
                    if (result->revision != ((void *)0))
                        fputs(result->revision, stdout);
                    break;
                case 'u':
                    if (result->unknown)
                        putc('?', stdout);
                    break;
                case 'm':
                    if (result->modified)
                        putc('!', stdout);
                    break;
                case 'n':
                    fputs(context->name, stdout);
                    break;
                default:
                    putc(format[i], stdout);
            }
        }
        else {
            putc(format[i], stdout);
        }
    }
}

canonical_original_code:
void print_result(struct vccontext_t *context, struct options_t *options, struct result_t *result)
{
    int i;
    char *format = options->format;
    for (i = 0; i < strlen(format); i++) {
        if (format[i] == '%') {
            i++;
            switch (format[i]) {
                case '0':
                case '%':
                    putc('%', stdout);
                    break;
                case 'b':
                    if (result->branch != ((void *)0))
                        fputs(result->branch, stdout);
                    break;
                case 'r':
                    if (result->revision != ((void *)0))
                        fputs(result->revision, stdout);
                    break;
                case 'u':
                    if (result->unknown)
                        putc('?', stdout);
                    break;
                case 'm':
                    if (result->modified)
                        putc('!', stdout);
                    break;
                case 'n':
                    fputs(context->name, stdout);
                    break;
                default:
                    putc(format[i], stdout);
            }
        }
        else {
            putc(format[i], stdout);
        }
    }
}

variable_types: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t { char *branch; char *revision; int unknown; int modified; } *(*get_info)(struct vccontext_t *); } *

variable_types:
options
struct options_t *

variable_types:
result
struct result_t *

user_defined_types: struct vccontext_t { char *name; struct options_t *options; int (*probe)(struct vccontext_t *); struct result_t *(*get_info)(struct vccontext_t *); }


decompiled_code:
__int64 __fastcall probe_all(_QWORD *a1, int a2)
{
_QWORD *v2; // rbx
__int64 v3; // r12

if ( a2 <= 0 )
return 0LL;
v2 = a1;
while ( 1 )
{
v3 = *v2;
if ( (*(unsigned int (__fastcall **)(_QWORD))(*v2 + 16LL))(*v2) )
break;
if ( ++v2 == &a1[(unsigned int)(a2 - 1) + 1] )
return 0LL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, int a2)
{
_QWORD *v2; // rbx
__int64 v3; // r12

if ( a2 <= 0 )
return 0LL;
v2 = a1;
while ( 1 )
{
v3 = *v2;
if ( (*(unsigned int (__fastcall **)(_QWORD))(*v2 + 16LL))(*v2) )
break;
if ( ++v2 == &a1[(unsigned int)(a2 - 1) + 1] )
return 0LL;
}
return v3;
}

original_code:
vccontext_t* probe_all(vccontext_t** contexts, int num_contexts)
{
    int idx;
    for (idx = 0; idx < num_contexts; idx++) {
        vccontext_t* ctx = contexts[idx];
        if (ctx->probe(ctx)) {
            return ctx;
        }
    }
    return ((void *)0);
}

canonical_original_code:
struct vccontext_t *probe_all(struct vccontext_t **contexts, int num_contexts)
{
    int idx;
    for (idx = 0; idx < num_contexts; idx++) {
        struct vccontext_t *ctx = contexts[idx];
        if (ctx->probe(ctx)) {
            return ctx;
        }
    }
    return ((void *)0);
}

variable_types:
contexts
struct vccontext_t **

variable_types: idx int

return_type: struct vccontext_t { char *name; struct options_t { int debug; char *format; int show_branch; int show_revision; int show_unknown; int show_modified; } *options; int (*probe)(struct vccontext_t *); struct result_t { char *branch; char *revision; int unknown; int modified; } *(*get_info)(struct vccontext_t *); } *