decompiled_code:
int __fastcall stack_magic_setup(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 (__fastcall *v6)(unsigned __int64); // rax
void (*v7)(); // rdx
int v8; // eax
int result; // eax
void (*v11)(); // [rsp+10h] [rbp-48h]
__int64 v15; // [rsp+30h] [rbp-28h] BYREF
__int64 v16; // [rsp+38h] [rbp-20h] BYREF
__int64 v17; // [rsp+48h] [rbp-10h] BYREF

keep_variable((__int64)detect_stack_direction);
keep_variable((__int64)detect_stack_direction_wrong);
keep_variable((__int64)stack_wrapper);
keep_variable((__int64)stack_wrapper_wrong);
v6 = detect_stack_direction;
if ( !one )
v6 = (__int64 (__fastcall *)(unsigned __int64))detect_stack_direction_wrong;
v7 = stack_wrapper_wrong;
if ( one )
v7 = (void (*)())stack_wrapper;
v11 = v7;
v16 = a1;
v8 = v6((unsigned __int64)&v15);
if ( v8 == 2 )
{
v15 = (__int64)&v17 - v16 - a2;
}
else
{
if ( v8 != 3 )
abort();
v15 = v16 + 16 - (_QWORD)&v16;
}
result = setjmp(a3);
if ( !result )
result = ((__int64 (__fastcall *)(__int64, __int64, __int64, struct __jmp_buf_tag *, __int64))v11)(
a5,
a6,
v15,
a3,
a4);
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 (__fastcall *v6)(unsigned __int64); // rax
void (*v7)(); // rdx
int v8; // eax
int result; // eax
void (*v11)(); // [rsp+10h] [rbp-48h]
__int64 v15; // [rsp+30h] [rbp-28h] BYREF
__int64 v16; // [rsp+38h] [rbp-20h] BYREF
__int64 v17; // [rsp+48h] [rbp-10h] BYREF

func3((__int64)detect_stack_direction);
func3((__int64)detect_stack_direction_wrong);
func3((__int64)stack_wrapper);
func3((__int64)stack_wrapper_wrong);
v6 = detect_stack_direction;
if ( !one )
v6 = (__int64 (__fastcall *)(unsigned __int64))detect_stack_direction_wrong;
v7 = stack_wrapper_wrong;
if ( one )
v7 = (void (*)())stack_wrapper;
v11 = v7;
v16 = a1;
v8 = v6((unsigned __int64)&v15);
if ( v8 == 2 )
{
v15 = (__int64)&v17 - v16 - a2;
}
else
{
if ( v8 != 3 )
abort();
v15 = v16 + 16 - (_QWORD)&v16;
}
result = setjmp(a3);
if ( !result )
result = ((__int64 (__fastcall *)(__int64, __int64, __int64, struct __jmp_buf_tag *, __int64))v11)(
a5,
a6,
v15,
a3,
a4);
return result;
}

original_code:
void stack_magic_setup (void *stackbase, size_t stacksize,
 jmp_buf parent, jmp_buf child,
 stack_magic_callback callback, void *data)
{
 keep_variable (detect_stack_direction);
 keep_variable (detect_stack_direction_wrong);
 keep_variable (stack_wrapper);
 keep_variable (stack_wrapper_wrong);
 enum stack_direction (*dsdfunc)(void *) = one ?
  detect_stack_direction : detect_stack_direction_wrong;
 void (*swfunc)(stack_magic_callback, void *, size_t, jmp_buf, jmp_buf) = one ?
  stack_wrapper : stack_wrapper_wrong;
 uintptr_t target = (uintptr_t) stackbase;
 uintptr_t source = (uintptr_t) ⌖
 size_t dummysize;
 switch (dsdfunc (&dummysize))
 {
  case STACK_DIRECTION_DOWN:
   dummysize = source - (target + stacksize) + (16);
   break;
  case STACK_DIRECTION_UP:
   dummysize = target - source + (16);
   break;
  default:
   do { abort (); } while (0);;
   break;
 }
 if (_setjmp (parent) == STACK_SJLJ_INITIAL)
  swfunc (callback, data, dummysize, parent, child);
}

canonical_original_code:
void stack_magic_setup(void *stackbase, unsigned long stacksize, struct __jmp_buf_tag parent[1], struct __jmp_buf_tag child[1], void (*callback)(void *, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1]), void *data)
{
 keep_variable (detect_stack_direction);
 keep_variable (detect_stack_direction_wrong);
 keep_variable (stack_wrapper);
 keep_variable (stack_wrapper_wrong);
 enum stack_direction (*dsdfunc)(void *) = one ?
  detect_stack_direction : detect_stack_direction_wrong;
 void (*swfunc)(void (*)(void *, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1]), void *, unsigned long, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1]) = one ?
  stack_wrapper : stack_wrapper_wrong;
 unsigned long target = (uintptr_t) stackbase;
 unsigned long source = (uintptr_t) ⌖
 unsigned long dummysize;
 switch (dsdfunc (&dummysize))
 {
  case STACK_DIRECTION_DOWN:
   dummysize = source - (target + stacksize) + (16);
   break;
  case STACK_DIRECTION_UP:
   dummysize = target - source + (16);
   break;
  default:
   do { abort (); } while (0);;
   break;
 }
 if (_setjmp (parent) == STACK_SJLJ_INITIAL)
  swfunc (callback, data, dummysize, parent, child);
}

variable_types:
stackbase
void *

variable_types:
stacksize
unsigned long

variable_types: enum stack_direction { STACK_DIRECTION_UNSET=0, STACK_DIRECTION_UNKNOWN=1, STACK_DIRECTION_DOWN=2, STACK_DIRECTION_UP=3 } (*)(void *)

variable_types: void (*)(void (*)(void *, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1], struct __jmp_buf_tag [1]), void *, unsigned long, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1])

variable_types:
target
unsigned long

variable_types:
source
unsigned long


decompiled_code:
int __fastcall stack_magic_switch(struct __jmp_buf_tag *a1, struct __jmp_buf_tag *a2, int a3)
{
int result; // eax

result = setjmp(a1);
if ( !result )
longjmp(a2, a3);
return result;
}

canonical_decompiled_code:
int __fastcall func6(struct __jmp_buf_tag *a1, struct __jmp_buf_tag *a2, int a3)
{
int result; // eax

result = setjmp(a1);
if ( !result )
longjmp(a2, a3);
return result;
}

original_code:
int stack_magic_switch (jmp_buf source, jmp_buf target, int num)
{
 int res = _setjmp (source);
 if (res == STACK_SJLJ_INITIAL)
  longjmp (target, num);
 return res;
}

canonical_original_code:
int stack_magic_switch(struct __jmp_buf_tag source[1], struct __jmp_buf_tag target[1], int num)
{
 int res = _setjmp (source);
 if (res == STACK_SJLJ_INITIAL)
  longjmp (target, num);
 return res;
}

variable_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1]

variable_types:
target
struct __jmp_buf_tag [1]

variable_types: num int

variable_types: res int

return_type: int

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }


decompiled_code:
void __fastcall __noreturn stack_wrapper(void (__fastcall *a1)(__int64, struct __jmp_buf_tag *, struct __jmp_buf_tag *), __int64 a2, signed __int64 a3, struct __jmp_buf_tag *a4, struct __jmp_buf_tag *a5)
{
void *v7; // rsp
__int64 v8; // [rsp+0h] [rbp-20h] BYREF

v7 = alloca(a3);
keep_variable((__int64)&v8);
stack_magic_switch(a5, a4, 3);
a1(a2, a4, a5);
while ( 1 )
stack_magic_switch(a5, a4, 4);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void (__fastcall *a1)(__int64, struct __jmp_buf_tag *, struct __jmp_buf_tag *), __int64 a2, signed __int64 a3, struct __jmp_buf_tag *a4, struct __jmp_buf_tag *a5)
{
void *v7; // rsp
__int64 v8; // [rsp+0h] [rbp-20h] BYREF

v7 = alloca(a3);
func3((__int64)&v8);
func6(a5, a4, 3);
a1(a2, a4, a5);
while ( 1 )
func6(a5, a4, 4);
}

original_code:
static void stack_wrapper (stack_magic_callback callback,
 void *data, size_t dummysize,
 jmp_buf parent, jmp_buf child)
{
 char dummy[dummysize];
 keep_variable (dummy);
 stack_magic_switch (child, parent, STACK_SJLJ_STARTING);
 callback (data, parent, child);
 while (1)
  stack_magic_switch (child, parent, STACK_SJLJ_STOPPING);
}

canonical_original_code:
static void stack_wrapper(void (*callback)(void *, struct __jmp_buf_tag [1], struct __jmp_buf_tag [1]), void *data, unsigned long dummysize, struct __jmp_buf_tag parent[1], struct __jmp_buf_tag child[1])
{
 char dummy[-1];
 keep_variable (dummy);
 stack_magic_switch (child, parent, STACK_SJLJ_STARTING);
 callback (data, parent, child);
 while (1)
  stack_magic_switch (child, parent, STACK_SJLJ_STOPPING);
}

variable_types: void (*)(void *, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1], struct __jmp_buf_tag [1])

variable_types:
data
void *

variable_types:
dummysize
unsigned long

variable_types:
parent
struct __jmp_buf_tag [1]

variable_types:
child
struct __jmp_buf_tag [1]

variable_types:
dummy
char [-1]

return_type: void


main

canonical_name: main

repo: ghills/StructTranslate

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v4[2]; // [rsp+0h] [rbp-28h] BYREF
__int16 v5; // [rsp+2h] [rbp-26h]
int v6; // [rsp+4h] [rbp-24h]
int v7; // [rsp+8h] [rbp-20h]
__int16 v8; // [rsp+Ch] [rbp-1Ch]
_WORD v9[5]; // [rsp+Eh] [rbp-1Ah] BYREF

v4[0] = 117;
v5 = 12345;
v6 = 97293122;
v7 = 24579234;
v8 = 3812;
strcpy((char *)v9, "g?r");
v9[2] = -32203;
*(_DWORD *)&v9[3] = 8394023;
puts("BEFORE:");
strans_print_by_map(v4, &foo_map, 6LL);
putchar(10);
strans_reverse_bytes_by_map(v4, &foo_map, 6LL);
puts("AFTER:");
strans_print_by_map(v4, &foo_map, 6LL);
return 0;
}

original_code:
int main( int argc, char * argv[] )
{
    foo_t foo;
    foo = foo_init_data;
    printf("BEFORE:\n");
    strans_print_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    putchar( '\n' );
    strans_reverse_bytes_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    printf("AFTER:\n");
    strans_print_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    return( 0 );
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct foo_t foo;
    foo = foo_init_data;
    printf("BEFORE:\n");
    strans_print_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    putchar( '\n' );
    strans_reverse_bytes_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    printf("AFTER:\n");
    strans_print_by_map( &foo, foo_map, ( sizeof(foo_map) / sizeof(foo_map[0]) ) );
    return( 0 );
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct foo_t { unsigned char uint8; unsigned short uint16; unsigned int uint32; struct subtype_t { unsigned int uint32; } subtype; unsigned short arr[4]; unsigned int uint32_2; }

user_defined_types: struct foo_t { unsigned char uint8; unsigned short uint16; unsigned int uint32; struct subtype_t subtype; unsigned short arr[4]; unsigned int uint32_2; }


decompiled_code:
__int64 __fastcall strans_reverse_bytes(__int64 a1, unsigned __int8 a2)
{
__int64 v2; // rdx
__int64 result; // rax
char v4; // cl

if ( !a2 )
__assert_fail("cnt > 0", "stranslate.c", 0x65u, "strans_reverse_bytes");
v2 = a1 + a2;
result = 0LL;
if ( a2 > 1u )
{
do
{
v4 = *(_BYTE *)(a1 + result);
*(_BYTE *)(a1 + result) = *(_BYTE *)(v2 - 1);
*(_BYTE *)(v2 - 1) = v4;
++result;
--v2;
}
while ( (unsigned __int8)(a2 >> 1) > (unsigned __int8)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 a2)
{
__int64 v2; // rdx
__int64 result; // rax
char v4; // cl

if ( !a2 )
__assert_fail("cnt > 0", "stranslate.c", 0x65u, "strans_reverse_bytes");
v2 = a1 + a2;
result = 0LL;
if ( a2 > 1u )
{
do
{
v4 = *(_BYTE *)(a1 + result);
*(_BYTE *)(a1 + result) = *(_BYTE *)(v2 - 1);
*(_BYTE *)(v2 - 1) = v4;
++result;
--v2;
}
while ( (unsigned __int8)(a2 >> 1) > (unsigned __int8)result );
}
return result;
}

original_code:
void strans_reverse_bytes
    (
    uint8_t * const data,
    uint8_t cnt
    )
{
uint8_t i;
((cnt > 0) ? (void) (0) : __assert_fail ("cnt > 0", "stranslate.c", 101, __extension__ __PRETTY_FUNCTION__));
for( i = 0; i < ( cnt / 2 ); i++ )
    {
    {uint8_t _t; _t = data[i]; data[i] = data[cnt-1-i]; data[cnt-1-i] = _t; };
    }
}

canonical_original_code:
void strans_reverse_bytes(unsigned char *data, unsigned char cnt)
{
unsigned char i;
((cnt > 0) ? (void) (0) : __assert_fail ("cnt > 0", "stranslate.c", 101, __extension__ __PRETTY_FUNCTION__));
for( i = 0; i < ( cnt / 2 ); i++ )
    {
    {unsigned char _t; _t = data[i]; data[i] = data[cnt-1-i]; data[cnt-1-i] = _t; };
    }
}

variable_types:
data
unsigned char *

variable_types:
cnt
unsigned char

variable_types:
i
unsigned char


decompiled_code:
__int64 __fastcall reverse_bytes_by_map(__int64 a1, unsigned __int16 *a2, __int16 a3, char a4)
{
unsigned __int16 *v5; // r12
unsigned __int16 v6; // dx
__int64 result; // rax
__int64 v8; // r15
unsigned __int8 v9; // r14
unsigned __int16 v10; // bp
__int64 v11; // rsi

if ( !a3 )
__assert_fail("map_count > 0", "stranslate.c", 0x33u, "reverse_bytes_by_map");
v5 = a2;
v6 = a3 - 1;
result = 3LL * v6;
v8 = (__int64)&a2[12 * v6 + 12];
v9 = a4 - 1;
do
{
if ( !v5[1] )
__assert_fail("map[i].elem_count > 0", "stranslate.c", 0x37u, "reverse_bytes_by_map");
if ( *v5 != 1 )
{
v10 = 0;
do
{
v11 = *((_QWORD *)v5 + 1);
if ( v11 )
{
if ( !a4 )
{
perror("Recursion Depth");
exit(-1);
}
result = reverse_bytes_by_map(a1 + v5[2] + (__int64)(v10 * *v5), v11, v5[8], v9);
}
else
{
result = strans_reverse_bytes(a1 + v5[2] + (__int64)(*v5 * v10), *v5);
}
++v10;
}
while ( v5[1] > v10 );
}
v5 += 12;
}
while ( v5 != (unsigned __int16 *)v8 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int16 *a2, __int16 a3, char a4)
{
unsigned __int16 *v5; // r12
unsigned __int16 v6; // dx
__int64 result; // rax
__int64 v8; // r15
unsigned __int8 v9; // r14
unsigned __int16 v10; // bp
__int64 v11; // rsi

if ( !a3 )
__assert_fail("map_count > 0", "stranslate.c", 0x33u, "reverse_bytes_by_map");
v5 = a2;
v6 = a3 - 1;
result = 3LL * v6;
v8 = (__int64)&a2[12 * v6 + 12];
v9 = a4 - 1;
do
{
if ( !v5[1] )
__assert_fail("map[i].elem_count > 0", "stranslate.c", 0x37u, "reverse_bytes_by_map");
if ( *v5 != 1 )
{
v10 = 0;
do
{
v11 = *((_QWORD *)v5 + 1);
if ( v11 )
{
if ( !a4 )
{
perror("Recursion Depth");
exit(-1);
}
result = func8(a1 + v5[2] + (__int64)(v10 * *v5), v11, v5[8], v9);
}
else
{
result = func7(a1 + v5[2] + (__int64)(*v5 * v10), *v5);
}
++v10;
}
while ( v5[1] > v10 );
}
v5 += 12;
}
while ( v5 != (unsigned __int16 *)v8 );
return result;
}

original_code:
static void reverse_bytes_by_map
    (
    void * const data,
    type_map_t const * const
                 map,
    uint16_t map_count,
    uint8_t iters_remain
    )
{
uint16_t i;
uint16_t j;
((map_count > 0) ? (void) (0) : __assert_fail ("map_count > 0", "stranslate.c", 51, __extension__ __PRETTY_FUNCTION__));
for( i = 0; i < map_count; i++ )
    {
    ((map[i].elem_count > 0) ? (void) (0) : __assert_fail ("map[i].elem_count > 0", "stranslate.c", 55, __extension__ __PRETTY_FUNCTION__));
    if( map[i].elem_size == 1 )
        {
        continue;
        }
    for( j = 0; j < map[i].elem_count; j++ )
        {
        if( map[i].type_map )
            {
            if( iters_remain > 0 )
                {
                reverse_bytes_by_map( (void *)( (uint8_t *)data + map[i].offset + (j * map[i].elem_size)), map[i].type_map, map[i].type_count, iters_remain - 1 );
                }
            else
                {
                perror("Recursion Depth");
                exit(-1);
                }
            }
        else
            {
            strans_reverse_bytes( (void *)( (uint8_t *)data + map[i].offset + (j * map[i].elem_size)), map[i].elem_size );
            }
        }
    }
}

canonical_original_code:
static void reverse_bytes_by_map(void *data, struct type_map_t *map, unsigned short map_count, unsigned char iters_remain)
{
unsigned short i;
unsigned short j;
((map_count > 0) ? (void) (0) : __assert_fail ("map_count > 0", "stranslate.c", 51, __extension__ __PRETTY_FUNCTION__));
for( i = 0; i < map_count; i++ )
    {
    ((map[i].elem_count > 0) ? (void) (0) : __assert_fail ("map[i].elem_count > 0", "stranslate.c", 55, __extension__ __PRETTY_FUNCTION__));
    if( map[i].elem_size == 1 )
        {
        continue;
        }
    for( j = 0; j < map[i].elem_count; j++ )
        {
        if( map[i].type_map )
            {
            if( iters_remain > 0 )
                {
                reverse_bytes_by_map( (void *)( (unsigned char *)data + map[i].offset + (j * map[i].elem_size)), map[i].type_map, map[i].type_count, iters_remain - 1 );
                }
            else
                {
                perror("Recursion Depth");
                exit(-1);
                }
            }
        else
            {
            strans_reverse_bytes( (void *)( (unsigned char *)data + map[i].offset + (j * map[i].elem_size)), map[i].elem_size );
            }
        }
    }
}

variable_types: struct type_map_t { unsigned short elem_size; unsigned short elem_count; unsigned short offset; void *type_map; unsigned short type_count; } *

variable_types:
i
unsigned short

variable_types:
j
unsigned short


decompiled_code:
__int64 __fastcall strans_reverse_bytes_by_map(__int64 a1, unsigned __int16 *a2, __int16 a3)
{
return reverse_bytes_by_map(a1, a2, a3, 255);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int16 *a2, __int16 a3)
{
return func8(a1, a2, a3, 255);
}

original_code:
void strans_reverse_bytes_by_map
    (
    void * const data,
    type_map_t const * const
                 map,
    uint16_t map_count
    )
{
reverse_bytes_by_map( data, map, map_count, ( 255 ) );
}

canonical_original_code:
void strans_reverse_bytes_by_map(void *data, struct type_map_t *map, unsigned short map_count)
{
reverse_bytes_by_map( data, map, map_count, ( 255 ) );
}

user_defined_types: struct type_map_t { unsigned short elem_size; unsigned short elem_count; unsigned short offset; void *type_map; unsigned short type_count; }


decompiled_code:
int __fastcall print_n_bytes(unsigned __int8 *a1, __int16 a2)
{
unsigned __int8 *v2; // rbx
int result; // eax

if ( a2 )
{
v2 = a1;
do
result = printf("%02X ", *v2++);
while ( v2 != &a1[(unsigned __int16)(a2 - 1) + 1] );
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned __int8 *a1, __int16 a2)
{
unsigned __int8 *v2; // rbx
int result; // eax

if ( a2 )
{
v2 = a1;
do
result = printf("%02X ", *v2++);
while ( v2 != &a1[(unsigned __int16)(a2 - 1) + 1] );
}
return result;
}

original_code:
static void print_n_bytes
    (
    uint8_t const * const
                data,
    uint16_t cnt
    )
{
uint16_t i;
for( i = 0; i < cnt; i++ )
    {
    printf( "%02X ", data[i] );
    }
}

canonical_original_code:
static void print_n_bytes(unsigned char *data, unsigned short cnt)
{
unsigned short i;
for( i = 0; i < cnt; i++ )
    {
    printf( "%02X ", data[i] );
    }
}

variable_types:
cnt
unsigned short


decompiled_code:
int __fastcall print_n_elements(unsigned __int8 *a1, __int16 a2, unsigned __int16 a3)
{
__int64 v3; // r12
int v5; // ebp
int result; // eax

if ( a2 )
{
v3 = a3;
v5 = 0;
do
{
putchar(91);
putchar(32);
print_n_bytes(a1, a3);
result = putchar(93);
++v5;
a1 += v3;
}
while ( a2 != (_WORD)v5 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(unsigned __int8 *a1, __int16 a2, unsigned __int16 a3)
{
__int64 v3; // r12
int v5; // ebp
int result; // eax

if ( a2 )
{
v3 = a3;
v5 = 0;
do
{
putchar(91);
putchar(32);
func10(a1, a3);
result = putchar(93);
++v5;
a1 += v3;
}
while ( a2 != (_WORD)v5 );
}
return result;
}

original_code:
static void print_n_elements
    (
    uint8_t const * const
                data,
    uint16_t cnt,
    uint16_t elem_sz
    )
{
uint16_t i;
for( i = 0; i < cnt; i++ )
    {
    putchar('[');
    putchar(' ');
    print_n_bytes( data + ( elem_sz * i ), elem_sz );
    putchar(']');
    }
}

canonical_original_code:
static void print_n_elements(unsigned char *data, unsigned short cnt, unsigned short elem_sz)
{
unsigned short i;
for( i = 0; i < cnt; i++ )
    {
    putchar('[');
    putchar(' ');
    print_n_bytes( data + ( elem_sz * i ), elem_sz );
    putchar(']');
    }
}


decompiled_code:
int __fastcall print_by_map(__int64 a1, unsigned __int16 *a2, __int16 a3, unsigned __int8 a4)
{
unsigned __int16 *v5; // rbp
__int64 v6; // r14
unsigned __int8 v7; // r15
unsigned __int16 v8; // r12
int result; // eax
int v10; // er12

if ( a3 )
{
v5 = a2;
v6 = (__int64)&a2[12 * (unsigned __int16)(a3 - 1) + 12];
v7 = a4 + 1;
do
{
v10 = 0;
if ( a4 )
{
do
{
putchar(32);
putchar(32);
++v10;
}
while ( a4 != (_BYTE)v10 );
}
if ( *((_QWORD *)v5 + 1) )
{
if ( v5[1] )
{
v8 = 0;
do
{
putchar(123);
putchar(10);
if ( a4 > 9u )
puts("MAX DEPTH REACHED");
else
print_by_map(a1 + v5[2] + (__int64)(v8 * *v5), *((_QWORD *)v5 + 1), v5[8], v7);
putchar(125);
++v8;
}
while ( v5[1] > v8 );
}
}
else
{
print_n_elements((unsigned __int8 *)(a1 + v5[2]), v5[1], *v5);
}
result = putchar(10);
v5 += 12;
}
while ( (unsigned __int16 *)v6 != v5 );
}
return result;
}

original_code:
static void print_by_map
    (
    void const * const
                 data,
    type_map_t const * const
                 map,
    uint16_t map_count,
    uint8_t iter_depth
    )
{
uint16_t i;
uint16_t j;
uint8_t * ptr;
uint8_t tab_i;
ptr = (uint8_t *)data;
for( i = 0; i < map_count; i++ )
    {
    for( tab_i = 0; tab_i < iter_depth; tab_i++ )
        {
        putchar(' ');
        putchar(' ');
        }
    if( map[i].type_map )
        {
        for( j = 0; j < map[i].elem_count; j++ )
            {
            putchar('{');
            putchar('\n');
            if( iter_depth < ( 10 ) )
                {
                print_by_map( &ptr[map[i].offset] + (j*map[i].elem_size), map[i].type_map, map[i].type_count, iter_depth + 1 );
                }
            else
                {
                printf("MAX DEPTH REACHED\n");
                }
            putchar('}');
            }
        }
    else
        {
        print_n_elements( &ptr[ map[i].offset ], map[i].elem_count, map[i].elem_size );
        }
    putchar('\n');
    }
}

canonical_original_code:
static void print_by_map(void *data, struct type_map_t *map, unsigned short map_count, unsigned char iter_depth)
{
unsigned short i;
unsigned short j;
unsigned char *ptr;
unsigned char tab_i;
ptr = (unsigned char *)data;
for( i = 0; i < map_count; i++ )
    {
    for( tab_i = 0; tab_i < iter_depth; tab_i++ )
        {
        putchar(' ');
        putchar(' ');
        }
    if( map[i].type_map )
        {
        for( j = 0; j < map[i].elem_count; j++ )
            {
            putchar('{');
            putchar('\n');
            if( iter_depth < ( 10 ) )
                {
                print_by_map( &ptr[map[i].offset] + (j*map[i].elem_size), map[i].type_map, map[i].type_count, iter_depth + 1 );
                }
            else
                {
                printf("MAX DEPTH REACHED\n");
                }
            putchar('}');
            }
        }
    else
        {
        print_n_elements( &ptr[ map[i].offset ], map[i].elem_count, map[i].elem_size );
        }
    putchar('\n');
    }
}

variable_types:
ptr
unsigned char *


decompiled_code:
int __fastcall strans_print_by_map(__int64 a1, unsigned __int16 *a2, __int16 a3)
{
return print_by_map(a1, a2, a3, 0);
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, unsigned __int16 *a2, __int16 a3)
{
return func12(a1, a2, a3, 0);
}

original_code:
void strans_print_by_map
    (
    void const * const
                 data,
    type_map_t const * const
                 map,
    uint16_t map_count
    )
{
print_by_map( data, map, map_count, 0 );
}

canonical_original_code:
void strans_print_by_map(void *data, struct type_map_t *map, unsigned short map_count)
{
print_by_map( data, map, map_count, 0 );
}


Usage

decompiled_code:
void __fastcall __noreturn Usage(int a1, const char *a2)
{
bool v2; // zf
FILE *v3; // rdi

v2 = a1 == 0;
v3 = (FILE *)stdout;
if ( v2 )
v3 = stderr;
fprintf(
v3,
"Usage: %s [OPTIONS]...\n"
"\t -p pid - process ID to send signal to\n"
"\t -d delay - post trigger delay (ms)\n"
"\t -t threshold - Sound power threshold at which to send signal\n"
"\t -o out_file - Output filename (default stdout)\n"
"\t -i in_file - file to get input (default stdin)\n"
"\n"
"\n"
"Example:\n"
" $ arecord -D hw:1,0 -f S16_LE | %s -p `pidof raspivid` -d 1000\n",
a2,
a2);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(int a1, const char *a2)
{
bool v2; // zf
FILE *v3; // rdi

v2 = a1 == 0;
v3 = (FILE *)stdout;
if ( v2 )
v3 = stderr;
fprintf(
v3,
"Usage: %s [OPTIONS]...\n"
"\t -p pid - process ID to send signal to\n"
"\t -d delay - post trigger delay (ms)\n"
"\t -t threshold - Sound power threshold at which to send signal\n"
"\t -o out_file - Output filename (default stdout)\n"
"\t -i in_file - file to get input (default stdin)\n"
"\n"
"\n"
"Example:\n"
" $ arecord -D hw:1,0 -f S16_LE | %s -p `pidof raspivid` -d 1000\n",
a2,
a2);
exit(-1);
}

original_code:
void Usage(int asked, char *argv0)
{
   fprintf(asked ? stdout : stderr,
      "Usage: %s [OPTIONS]...\n"
           "\t -p pid - process ID to send signal to\n"
           "\t -d delay - post trigger delay (ms)\n"
           "\t -t threshold - Sound power threshold at which to send signal\n"
           "\t -o out_file - Output filename (default stdout)\n"
           "\t -i in_file - file to get input (default stdin)\n\n\n"
           "Example:\n"
           "   $ arecord -D hw:1,0 -f S16_LE | %s -p `pidof raspivid` -d 1000\n"
      ,argv0, argv0);
   exit(-1);
}

variable_types: asked int


repo: ghorn/mathlib

decompiled_code:
__int64 __fastcall xyz_memcpy(__m128i *a1, const __m128i *a2)
{
__int64 result; // rax

*a1 = _mm_loadu_si128(a2);
result = a2[1].m128i_i64[0];
a1[1].m128i_i64[0] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__m128i *a1, const __m128i *a2)
{
__int64 result; // rax

*a1 = _mm_loadu_si128(a2);
result = a2[1].m128i_i64[0];
a1[1].m128i_i64[0] = result;
return result;
}

original_code:
void
xyz_memcpy( xyz_t * x1, const xyz_t * const x0)
{
  memcpy( x1, x0, sizeof(xyz_t) );
}

canonical_original_code:
void xyz_memcpy(struct xyz_t *x1, struct xyz_t *x0)
{
  memcpy( x1, x0, sizeof(xyz_t) );
}

variable_types: struct xyz_t { double x; double y; double z; } *


decompiled_code:
void __fastcall xyz_set_all(double *a1, double a2)
{
*a1 = a2;
a1[1] = a2;
a1[2] = a2;
}

canonical_decompiled_code:
void __fastcall func1(double *a1, double a2)
{
*a1 = a2;
a1[1] = a2;
a1[2] = a2;
}

original_code:
void
xyz_set_all( xyz_t * xyz, double val)
{
  xyz->x = val;
  xyz->y = val;
  xyz->z = val;
}

canonical_original_code:
void xyz_set_all(struct xyz_t *xyz, double val)
{
  xyz->x = val;
  xyz->y = val;
  xyz->z = val;
}

variable_types: val double

user_defined_types: struct xyz_t { double x; double y; double z; }


decompiled_code:
void __fastcall xyz_set(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

canonical_decompiled_code:
void __fastcall func2(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

original_code:
void
xyz_set( xyz_t * xyz, double x, double y, double z)
{
  xyz->x = x;
  xyz->y = y;
  xyz->z = z;
}

canonical_original_code:
void xyz_set(struct xyz_t *xyz, double x, double y, double z)
{
  xyz->x = x;
  xyz->y = y;
  xyz->z = z;
}

variable_types: x double

variable_types: y double

variable_types: z double


original_code:
void
xyz_diff(xyz_t *diff, const xyz_t * const a, const xyz_t * const b)
{
  diff->x = a->x - b->x;
  diff->y = a->y - b->y;
  diff->z = a->z - b->z;
}

canonical_original_code:
void xyz_diff(struct xyz_t *diff, struct xyz_t *a, struct xyz_t *b)
{
  diff->x = a->x - b->x;
  diff->y = a->y - b->y;
  diff->z = a->z - b->z;
}

variable_types:
a
struct xyz_t *

variable_types:
b
struct xyz_t *


original_code:
void
xyz_sum(xyz_t *sum, const xyz_t * const a, const xyz_t * const b)
{
  sum->x = a->x + b->x;
  sum->y = a->y + b->y;
  sum->z = a->z + b->z;
}

canonical_original_code:
void xyz_sum(struct xyz_t *sum, struct xyz_t *a, struct xyz_t *b)
{
  sum->x = a->x + b->x;
  sum->y = a->y + b->y;
  sum->z = a->z + b->z;
}


original_code:
void
xyz_sum_scale(xyz_t *sum, const double sum_scale,
              const xyz_t * const a, const double a_scale,
              const xyz_t * const b, const double b_scale)
{
  sum->x = sum->x*sum_scale + a->x*a_scale + b->x*b_scale;
  sum->y = sum->y*sum_scale + a->y*a_scale + b->y*b_scale;
  sum->z = sum->z*sum_scale + a->z*a_scale + b->z*b_scale;
}

canonical_original_code:
void xyz_sum_scale(struct xyz_t *sum, double sum_scale, struct xyz_t *a, double a_scale, struct xyz_t *b, double b_scale)
{
  sum->x = sum->x*sum_scale + a->x*a_scale + b->x*b_scale;
  sum->y = sum->y*sum_scale + a->y*a_scale + b->y*b_scale;
  sum->z = sum->z*sum_scale + a->z*a_scale + b->z*b_scale;
}


original_code:
void
xyz_cross( xyz_t * c, const xyz_t * const a, const xyz_t * const b)
{
  c->x = a->y*b->z - a->z*b->y;
  c->y = - a->x*b->z + a->z*b->x;
  c->z = a->x*b->y - a->y*b->x;
}

canonical_original_code:
void xyz_cross(struct xyz_t *c, struct xyz_t *a, struct xyz_t *b)
{
  c->x = a->y*b->z - a->z*b->y;
  c->y = - a->x*b->z + a->z*b->x;
  c->z = a->x*b->y - a->y*b->x;
}


decompiled_code:
double __fastcall xyz_dot(double *a1, double *a2)
{
return *a1 * *a2 + a1[1] * a2[1] + a1[2] * a2[2];
}

canonical_decompiled_code:
double __fastcall func7(double *a1, double *a2)
{
return *a1 * *a2 + a1[1] * a2[1] + a1[2] * a2[2];
}

original_code:
double
xyz_dot( const xyz_t * const a, const xyz_t * const b)
{
  return a->x*b->x + a->y*b->y + a->z*b->z;
}

canonical_original_code:
double xyz_dot(struct xyz_t *a, struct xyz_t *b)
{
  return a->x*b->x + a->y*b->y + a->z*b->z;
}

return_type: double


decompiled_code:
double __fastcall xyz_norm_squared(double *a1)
{
return xyz_dot(a1, a1);
}

canonical_decompiled_code:
double __fastcall func8(double *a1)
{
return func7(a1, a1);
}

original_code:
double
xyz_norm_squared( const xyz_t * const vec)
{
  return xyz_dot( vec, vec);
}

canonical_original_code:
double xyz_norm_squared(struct xyz_t *vec)
{
  return xyz_dot( vec, vec);
}


decompiled_code:
double __fastcall xyz_norm(double *a1)
{
double v1; // xmm0_8
double result; // xmm0_8

v1 = xyz_norm_squared(a1);
if ( v1 < 0.0 )
result = sqrt(v1);
else
result = sqrt(v1);
return result;
}

canonical_decompiled_code:
double __fastcall func9(double *a1)
{
double v1; // xmm0_8
double result; // xmm0_8

v1 = func8(a1);
if ( v1 < 0.0 )
result = sqrt(v1);
else
result = sqrt(v1);
return result;
}

original_code:
double
xyz_norm( const xyz_t * const vec)
{
  return sqrt(xyz_norm_squared(vec));
}

canonical_original_code:
double xyz_norm(struct xyz_t *vec)
{
  return sqrt(xyz_norm_squared(vec));
}


decompiled_code:
double __fastcall xyz_distance(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

xyz_diff(v3, a1, a2);
return xyz_norm(v3);
}

canonical_decompiled_code:
double __fastcall func10(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

func3(v3, a1, a2);
return func9(v3);
}

original_code:
double
xyz_distance( const xyz_t * const a, const xyz_t * const b )
{
  xyz_t diff;
  xyz_diff( &diff, a, b);
  return xyz_norm( &diff );
}

canonical_original_code:
double xyz_distance(struct xyz_t *a, struct xyz_t *b)
{
  struct xyz_t diff;
  xyz_diff( &diff, a, b);
  return xyz_norm( &diff );
}

variable_types: struct xyz_t { double x; double y; double z; }


decompiled_code:
void __fastcall xyz_scale(double *a1, double *a2, double a3)
{
*a1 = a3 * *a2;
a1[1] = a3 * a2[1];
a1[2] = a3 * a2[2];
}

original_code:
void
xyz_scale( xyz_t * vec_out, double scale_factor, const xyz_t * const vec_in)
{
  vec_out->x = scale_factor*vec_in->x;
  vec_out->y = scale_factor*vec_in->y;
  vec_out->z = scale_factor*vec_in->z;
}

canonical_original_code:
void xyz_scale(struct xyz_t *vec_out, double scale_factor, struct xyz_t *vec_in)
{
  vec_out->x = scale_factor*vec_in->x;
  vec_out->y = scale_factor*vec_in->y;
  vec_out->z = scale_factor*vec_in->z;
}


decompiled_code:
void __fastcall xyz_scale_self(double *a1, double a2)
{
xyz_scale(a1, a1, a2);
}

canonical_decompiled_code:
void __fastcall func12(double *a1, double a2)
{
func11(a1, a1, a2);
}

original_code:
void
xyz_scale_self( xyz_t * vec, double scale_factor)
{
  xyz_scale(vec, scale_factor, vec);
}

canonical_original_code:
void xyz_scale_self(struct xyz_t *vec, double scale_factor)
{
  xyz_scale(vec, scale_factor, vec);
}


decompiled_code:
void __fastcall xyz_normalize_to(double *a1, double *a2, double a3)
{
double v4; // xmm1_8

v4 = xyz_norm(a2) + 1.0e-12;
xyz_scale(a1, a2, a3 / v4);
}

canonical_decompiled_code:
void __fastcall func13(double *a1, double *a2, double a3)
{
double v4; // xmm1_8

v4 = func9(a2) + 1.0e-12;
func11(a1, a2, a3 / v4);
}

original_code:
void
xyz_normalize_to( xyz_t * vec_out, const double new_norm, const xyz_t * const vec_in)
{
  xyz_scale( vec_out, new_norm/(xyz_norm(vec_in) + 1e-12), vec_in );
}

canonical_original_code:
void xyz_normalize_to(struct xyz_t *vec_out, double new_norm, struct xyz_t *vec_in)
{
  xyz_scale( vec_out, new_norm/(xyz_norm(vec_in) + 1e-12), vec_in );
}


decompiled_code:
void __fastcall xyz_normalize(double *a1, double *a2)
{
xyz_normalize_to(a1, a2, 1.0);
}

canonical_decompiled_code:
void __fastcall func14(double *a1, double *a2)
{
func13(a1, a2, 1.0);
}

original_code:
void
xyz_normalize( xyz_t * vec_out, const xyz_t * const vec_in)
{
  xyz_normalize_to( vec_out, 1.0, vec_in);
}

canonical_original_code:
void xyz_normalize(struct xyz_t *vec_out, struct xyz_t *vec_in)
{
  xyz_normalize_to( vec_out, 1.0, vec_in);
}


decompiled_code:
void __fastcall xyz_normalize_self_to(double *a1, double a2)
{
double v3; // xmm1_8

v3 = xyz_norm(a1) + 1.0e-12;
xyz_scale_self(a1, a2 / v3);
}

canonical_decompiled_code:
void __fastcall func15(double *a1, double a2)
{
double v3; // xmm1_8

v3 = func9(a1) + 1.0e-12;
func12(a1, a2 / v3);
}

original_code:
void
xyz_normalize_self_to( xyz_t * vec, const double new_norm )
{
  xyz_scale_self( vec, new_norm/(xyz_norm(vec) + 1e-12) );
}

canonical_original_code:
void xyz_normalize_self_to(struct xyz_t *vec, double new_norm)
{
  xyz_scale_self( vec, new_norm/(xyz_norm(vec) + 1e-12) );
}


decompiled_code:
void __fastcall xyz_normalize_self(double *a1)
{
xyz_normalize_self_to(a1, 1.0);
}

canonical_decompiled_code:
void __fastcall func16(double *a1)
{
func15(a1, 1.0);
}

original_code:
void
xyz_normalize_self( xyz_t * vec )
{
  xyz_normalize_self_to( vec, 1.0 );
}

canonical_original_code:
void xyz_normalize_self(struct xyz_t *vec)
{
  xyz_normalize_self_to( vec, 1.0 );
}


original_code:
void
xyz_mult_3x3_by_xyz(xyz_t * v_out, const double * const M, const xyz_t * const v)
{
  v_out->x = M[0]*v->x + M[1]*v->y + M[2]*v->z;
  v_out->y = M[3]*v->x + M[4]*v->y + M[5]*v->z;
  v_out->z = M[6]*v->x + M[7]*v->y + M[8]*v->z;
}

variable_types:
M
double *

variable_types:
v
struct xyz_t *


original_code:
void
xyz_mult_3x3_transpose_by_xyz(xyz_t * v_out, const double * const M, const xyz_t * const v)
{
  v_out->x = M[0]*v->x + M[3]*v->y + M[6]*v->z;
  v_out->y = M[1]*v->x + M[4]*v->y + M[7]*v->z;
  v_out->z = M[2]*v->x + M[5]*v->y + M[8]*v->z;
}

canonical_original_code:
void xyz_mult_3x3_transpose_by_xyz(struct xyz_t *v_out, double *M, struct xyz_t *v)
{
  v_out->x = M[0]*v->x + M[3]*v->y + M[6]*v->z;
  v_out->y = M[1]*v->x + M[4]*v->y + M[7]*v->z;
  v_out->z = M[2]*v->x + M[5]*v->y + M[8]*v->z;
}


decompiled_code:
void __fastcall quat_memcpy(__m128i *a1, const __m128i *a2)
{
*a1 = _mm_loadu_si128(a2);
a1[1] = _mm_loadu_si128(a2 + 1);
}

original_code:
void
quat_memcpy( quat_t * q1, const quat_t * const q0)
{
  memcpy( q1, q0, sizeof(quat_t) );
}

canonical_original_code:
void quat_memcpy(struct quat_t *q1, struct quat_t *q0)
{
  memcpy( q1, q0, sizeof(quat_t) );
}

variable_types: struct quat_t { double q0; double q1; double q2; double q3; } *


original_code:
void
quat_inv(quat_t *q_out, const quat_t * const q_in)
{
  q_out->q0 = q_in->q0;
  q_out->q1 = -q_in->q1;
  q_out->q2 = -q_in->q2;
  q_out->q3 = -q_in->q3;
}

canonical_original_code:
void quat_inv(struct quat_t *q_out, struct quat_t *q_in)
{
  q_out->q0 = q_in->q0;
  q_out->q1 = -q_in->q1;
  q_out->q2 = -q_in->q2;
  q_out->q3 = -q_in->q3;
}

user_defined_types: struct quat_t { double q0; double q1; double q2; double q3; }


decompiled_code:
double __fastcall quat_norm(double *a1)
{
double v1; // xmm0_8
double result; // xmm0_8

v1 = a1[3] * a1[3] + a1[1] * a1[1] + *a1 * *a1 + a1[2] * a1[2];
if ( v1 < 0.0 )
result = sqrt(v1);
else
result = sqrt(v1);
return result;
}

canonical_decompiled_code:
double __fastcall func2(double *a1)
{
double v1; // xmm0_8
double result; // xmm0_8

v1 = a1[3] * a1[3] + a1[1] * a1[1] + *a1 * *a1 + a1[2] * a1[2];
if ( v1 < 0.0 )
result = sqrt(v1);
else
result = sqrt(v1);
return result;
}

original_code:
double
quat_norm(const quat_t * const q)
{
  return sqrt( q->q0*q->q0 + q->q1*q->q1 + q->q2*q->q2 + q->q3*q->q3);
}

canonical_original_code:
double quat_norm(struct quat_t *q)
{
  return sqrt( q->q0*q->q0 + q->q1*q->q1 + q->q2*q->q2 + q->q3*q->q3);
}


original_code:
void
quat_normalize(quat_t * q)
{
  double norm_inv = 1.0/quat_norm(q);
  q->q0 = q->q0*norm_inv;
  q->q1 = q->q1*norm_inv;
  q->q2 = q->q2*norm_inv;
  q->q3 = q->q3*norm_inv;
}

canonical_original_code:
void quat_normalize(struct quat_t *q)
{
  double norm_inv = 1.0/quat_norm(q);
  q->q0 = q->q0*norm_inv;
  q->q1 = q->q1*norm_inv;
  q->q2 = q->q2*norm_inv;
  q->q3 = q->q3*norm_inv;
}


original_code:
void
quat_mult(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  q_out->q0 = qa->q0*qb->q0 - qa->q1*qb->q1 - qa->q2*qb->q2 - qa->q3*qb->q3;
  q_out->q1 = qa->q0*qb->q1 + qa->q1*qb->q0 + qa->q2*qb->q3 - qa->q3*qb->q2;
  q_out->q2 = qa->q0*qb->q2 - qa->q1*qb->q3 + qa->q2*qb->q0 + qa->q3*qb->q1;
  q_out->q3 = qa->q0*qb->q3 + qa->q1*qb->q2 - qa->q2*qb->q1 + qa->q3*qb->q0;
  if (q_out->q0 < 0){
    q_out->q0 = -q_out->q0;
    q_out->q1 = -q_out->q1;
    q_out->q2 = -q_out->q2;
    q_out->q3 = -q_out->q3;
  }
  quat_normalize( q_out );
}

canonical_original_code:
void quat_mult(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  q_out->q0 = qa->q0*qb->q0 - qa->q1*qb->q1 - qa->q2*qb->q2 - qa->q3*qb->q3;
  q_out->q1 = qa->q0*qb->q1 + qa->q1*qb->q0 + qa->q2*qb->q3 - qa->q3*qb->q2;
  q_out->q2 = qa->q0*qb->q2 - qa->q1*qb->q3 + qa->q2*qb->q0 + qa->q3*qb->q1;
  q_out->q3 = qa->q0*qb->q3 + qa->q1*qb->q2 - qa->q2*qb->q1 + qa->q3*qb->q0;
  if (q_out->q0 < 0){
    q_out->q0 = -q_out->q0;
    q_out->q1 = -q_out->q1;
    q_out->q2 = -q_out->q2;
    q_out->q3 = -q_out->q3;
  }
  quat_normalize( q_out );
}

variable_types:
qa
struct quat_t *

variable_types:
qb
struct quat_t *


decompiled_code:
void __fastcall quat_mult_inv(double *a1, double *a2, _QWORD *a3)
{
double v3[7]; // [rsp+0h] [rbp-38h] BYREF

quat_inv(v3, a3);
quat_mult(a1, a2, v3);
}

canonical_decompiled_code:
void __fastcall func5(double *a1, double *a2, _QWORD *a3)
{
double v3[7]; // [rsp+0h] [rbp-38h] BYREF

func1(v3, a3);
func4(a1, a2, v3);
}

original_code:
void
quat_mult_inv(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  quat_t qb_inv;
  quat_inv(&qb_inv,qb);
  quat_mult(q_out, qa, &qb_inv);
}

canonical_original_code:
void quat_mult_inv(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  struct quat_t qb_inv;
  quat_inv(&qb_inv,qb);
  quat_mult(q_out, qa, &qb_inv);
}

variable_types: struct quat_t { double q0; double q1; double q2; double q3; }


decompiled_code:
void __fastcall quat_inv_mult(double *a1, _QWORD *a2, double *a3)
{
double v4[7]; // [rsp+0h] [rbp-38h] BYREF

quat_inv(v4, a2);
quat_mult(a1, v4, a3);
}

canonical_decompiled_code:
void __fastcall func6(double *a1, _QWORD *a2, double *a3)
{
double v4[7]; // [rsp+0h] [rbp-38h] BYREF

func1(v4, a2);
func4(a1, v4, a3);
}

original_code:
void
quat_inv_mult(quat_t *q_out, const quat_t * const qa, const quat_t * const qb)
{
  quat_t qa_inv;
  quat_inv(&qa_inv,qa);
  quat_mult(q_out, &qa_inv, qb);
}

canonical_original_code:
void quat_inv_mult(struct quat_t *q_out, struct quat_t *qa, struct quat_t *qb)
{
  struct quat_t qa_inv;
  quat_inv(&qa_inv,qa);
  quat_mult(q_out, &qa_inv, qb);
}


decompiled_code:
void __fastcall rotate_xyz_about_x(__int64 a1, __int64 a2, double a3)
{
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a3, sinx, &v5);
v3 = v5;
v4 = sinx[0];
*(_QWORD *)a1 = *(_QWORD *)a2;
*(double *)(a1 + 8) = v3 * *(double *)(a2 + 8) + v4 * *(double *)(a2 + 16);
*(double *)(a1 + 16) = v3 * *(double *)(a2 + 16) - v4 * *(double *)(a2 + 8);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, __int64 a2, double a3)
{
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a3, sinx, &v5);
v3 = v5;
v4 = sinx[0];
*(_QWORD *)a1 = *(_QWORD *)a2;
*(double *)(a1 + 8) = v3 * *(double *)(a2 + 8) + v4 * *(double *)(a2 + 16);
*(double *)(a1 + 16) = v3 * *(double *)(a2 + 16) - v4 * *(double *)(a2 + 8);
}

original_code:
void
rotate_xyz_about_x( xyz_t * b, const xyz_t * const a, const double rot_angle)
{
  double cos_theta = cos(rot_angle);
  double sin_theta = sin(rot_angle);
  b->x = a->x;
  b->y = a->y*cos_theta + a->z*sin_theta;
  b->z = -a->y*sin_theta + a->z*cos_theta;
}

canonical_original_code:
void rotate_xyz_about_x(struct xyz_t *b, struct xyz_t *a, double rot_angle)
{
  double cos_theta = cos(rot_angle);
  double sin_theta = sin(rot_angle);
  b->x = a->x;
  b->y = a->y*cos_theta + a->z*sin_theta;
  b->z = -a->y*sin_theta + a->z*cos_theta;
}


variable_types: struct euler_t { double roll; double pitch; double yaw; } *


original_code:
void
euler321_of_dcm(euler_t *e, const double * const R)
{
  double r11 = R[0];
  double r12 = R[1];
  double mr13 = -R[2];
  double r23 = R[5];
  double r33 = R[8];
  e->yaw = atan2( r12, r11 );
  if (mr13 > 1.0) mr13 = 1.0;
  else if (mr13 < -1.0) mr13 = -1.0;
  e->pitch = asin( mr13 );
  e->roll = atan2( r23, r33 );
}

canonical_original_code:
void euler321_of_dcm(struct euler_t *e, double *R)
{
  double r11 = R[0];
  double r12 = R[1];
  double mr13 = -R[2];
  double r23 = R[5];
  double r33 = R[8];
  e->yaw = atan2( r12, r11 );
  if (mr13 > 1.0) mr13 = 1.0;
  else if (mr13 < -1.0) mr13 = -1.0;
  e->pitch = asin( mr13 );
  e->roll = atan2( r23, r33 );
}

variable_types:
R
double *

user_defined_types: struct euler_t { double roll; double pitch; double yaw; }


original_code:
void
quat_of_euler321(quat_t * q, const euler_t * const e)
{
  double sr2 = sin(0.5*e->roll);
  double cr2 = cos(0.5*e->roll);
  double sp2 = sin(0.5*e->pitch);
  double cp2 = cos(0.5*e->pitch);
  double sy2 = sin(0.5*e->yaw);
  double cy2 = cos(0.5*e->yaw);
  q->q0 = cr2*cp2*cy2 + sr2*sp2*sy2;
  q->q1 = sr2*cp2*cy2 - cr2*sp2*sy2;
  q->q2 = cr2*sp2*cy2 + sr2*cp2*sy2;
  q->q3 = cr2*cp2*sy2 - sr2*sp2*cy2;
  if (q->q0 < 0){
    q->q0 = -q->q0;
    q->q1 = -q->q1;
    q->q2 = -q->q2;
    q->q3 = -q->q3;
  }
  quat_normalize(q);
}

canonical_original_code:
void quat_of_euler321(struct quat_t *q, struct euler_t *e)
{
  double sr2 = sin(0.5*e->roll);
  double cr2 = cos(0.5*e->roll);
  double sp2 = sin(0.5*e->pitch);
  double cp2 = cos(0.5*e->pitch);
  double sy2 = sin(0.5*e->yaw);
  double cy2 = cos(0.5*e->yaw);
  q->q0 = cr2*cp2*cy2 + sr2*sp2*sy2;
  q->q1 = sr2*cp2*cy2 - cr2*sp2*sy2;
  q->q2 = cr2*sp2*cy2 + sr2*cp2*sy2;
  q->q3 = cr2*cp2*sy2 - sr2*sp2*cy2;
  if (q->q0 < 0){
    q->q0 = -q->q0;
    q->q1 = -q->q1;
    q->q2 = -q->q2;
    q->q3 = -q->q3;
  }
  quat_normalize(q);
}


decompiled_code:
__int64 __fastcall quat_of_dcm_a2b(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

euler321_of_dcm(v3, a2);
return quat_of_euler321(a1, v3);
}

canonical_decompiled_code:
__int64 __fastcall func4(double *a1, double *a2)
{
double v3[5]; // [rsp+0h] [rbp-28h] BYREF

func2(v3, a2);
return func3(a1, v3);
}

original_code:
void
quat_of_dcm_a2b(quat_t * q, const double * const R)
{
  euler_t e;
  euler321_of_dcm( &e, R);
  quat_of_euler321( q, &e);
}

canonical_original_code:
void quat_of_dcm_a2b(struct quat_t *q, double *R)
{
  struct euler_t e;
  euler321_of_dcm( &e, R);
  quat_of_euler321( q, &e);
}

variable_types: struct euler_t { double roll; double pitch; double yaw; }


decompiled_code:
__int64 __fastcall quat_of_dcm_b2a(double *a1, double *a2)
{
quat_of_dcm_a2b(a1, a2);
return quat_inv(a1, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(double *a1, double *a2)
{
func4(a1, a2);
return quat_inv(a1, a1);
}

original_code:
void
quat_of_dcm_b2a(quat_t * q, const double * const R)
{
  quat_of_dcm_a2b(q, R);
  quat_inv(q, q);
}

canonical_original_code:
void quat_of_dcm_b2a(struct quat_t *q, double *R)
{
  quat_of_dcm_a2b(q, R);
  quat_inv(q, q);
}


original_code:
void
dcm_of_quat_b2a(double *R_b2a, const quat_t * const q_a2b)
{
  quat_t q_b2a;
  q_b2a.q0 = q_a2b->q0;
  q_b2a.q1 = -q_a2b->q1;
  q_b2a.q2 = -q_a2b->q2;
  q_b2a.q3 = -q_a2b->q3;
  dcm_of_quat_a2b(R_b2a, &q_b2a);
}

canonical_original_code:
void dcm_of_quat_b2a(double *R_b2a, struct quat_t *q_a2b)
{
  struct quat_t q_b2a;
  q_b2a.q0 = q_a2b->q0;
  q_b2a.q1 = -q_a2b->q1;
  q_b2a.q2 = -q_a2b->q2;
  q_b2a.q3 = -q_a2b->q3;
  dcm_of_quat_a2b(R_b2a, &q_b2a);
}


decompiled_code:
__int64 rot_vec_by_dcm_a2b()
{
return xyz_mult_3x3_by_xyz();
}

canonical_decompiled_code:
__int64 func8()
{
return xyz_mult_3x3_by_xyz();
}

original_code:
void
rot_vec_by_dcm_a2b(xyz_t *vec_b, const double * const R_a2b, const xyz_t * const vec_a)
{
  xyz_mult_3x3_by_xyz( vec_b, R_a2b, vec_a );
}

canonical_original_code:
void rot_vec_by_dcm_a2b(struct xyz_t *vec_b, double *R_a2b, struct xyz_t *vec_a)
{
  xyz_mult_3x3_by_xyz( vec_b, R_a2b, vec_a );
}

variable_types: struct xyz_t { double x; double y; double z; } *


decompiled_code:
__int64 rot_vec_by_dcm_b2a()
{
return xyz_mult_3x3_transpose_by_xyz();
}

canonical_decompiled_code:
__int64 func9()
{
return xyz_mult_3x3_transpose_by_xyz();
}

original_code:
void
rot_vec_by_dcm_b2a(xyz_t *vec_a, const double * const R_a2b, const xyz_t * const vec_b)
{
  xyz_mult_3x3_transpose_by_xyz( vec_a, R_a2b, vec_b );
}

canonical_original_code:
void rot_vec_by_dcm_b2a(struct xyz_t *vec_a, double *R_a2b, struct xyz_t *vec_b)
{
  xyz_mult_3x3_transpose_by_xyz( vec_a, R_a2b, vec_b );
}


decompiled_code:
__int64 __fastcall rot_vec_by_quat_a2b(__int64 a1, double *a2, __int64 a3)
{
double v5[13]; // [rsp+0h] [rbp-68h] BYREF

dcm_of_quat_a2b(v5, a2);
return rot_vec_by_dcm_a2b(a1, v5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, double *a2, __int64 a3)
{
double v5[13]; // [rsp+0h] [rbp-68h] BYREF

func6(v5, a2);
return func8(a1, v5, a3);
}

original_code:
void
rot_vec_by_quat_a2b(xyz_t *vec_b, const quat_t * const q_a2b, const xyz_t * const vec_a)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_a2b(vec_b, R_a2b, vec_a);
}

canonical_original_code:
void rot_vec_by_quat_a2b(struct xyz_t *vec_b, struct quat_t *q_a2b, struct xyz_t *vec_a)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_a2b(vec_b, R_a2b, vec_a);
}


decompiled_code:
__int64 __fastcall rot_vec_by_quat_b2a(__int64 a1, double *a2, __int64 a3)
{
double v5[13]; // [rsp+0h] [rbp-68h] BYREF

dcm_of_quat_a2b(v5, a2);
return rot_vec_by_dcm_b2a(a1, v5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, double *a2, __int64 a3)
{
double v5[13]; // [rsp+0h] [rbp-68h] BYREF

func6(v5, a2);
return func9(a1, v5, a3);
}

original_code:
void
rot_vec_by_quat_b2a(xyz_t *vec_a, const quat_t * const q_a2b, const xyz_t * const vec_b)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_b2a(vec_a, R_a2b, vec_b);
}

canonical_original_code:
void rot_vec_by_quat_b2a(struct xyz_t *vec_a, struct quat_t *q_a2b, struct xyz_t *vec_b)
{
  double R_a2b[9];
  dcm_of_quat_a2b(R_a2b,q_a2b);
  rot_vec_by_dcm_b2a(vec_a, R_a2b, vec_b);
}


decompiled_code:
void __fastcall get_wind_angles_from_v_bw_b(double *a1, double *a2, double *a3, double *a4)
{
double *v5; // rbp
double v6; // [rsp+8h] [rbp-30h] BYREF

if ( a3 )
{
v5 = a3;
*a3 = xyz_norm(a4) + 1.0e-12;
if ( !a2 )
goto LABEL_4;
goto LABEL_3;
}
if ( a2 )
{
v6 = xyz_norm(a4) + 1.0e-12;
v5 = &v6;
LABEL_3:
*a2 = asin(a4[1] / *v5);
}
LABEL_4:
if ( a1 )
*a1 = atan2(a4[2], *a4);
}

canonical_decompiled_code:
void __fastcall func12(double *a1, double *a2, double *a3, double *a4)
{
double *v5; // rbp
double v6; // [rsp+8h] [rbp-30h] BYREF

if ( a3 )
{
v5 = a3;
*a3 = xyz_norm(a4) + 1.0e-12;
if ( !a2 )
goto LABEL_4;
goto LABEL_3;
}
if ( a2 )
{
v6 = xyz_norm(a4) + 1.0e-12;
v5 = &v6;
LABEL_3:
*a2 = asin(a4[1] / *v5);
}
LABEL_4:
if ( a1 )
*a1 = atan2(a4[2], *a4);
}

original_code:
void
get_wind_angles_from_v_bw_b(double * alpha, double * beta, double * airspeed, const xyz_t * const v_bw_b)
{
  double airspeed_internal_memory;
  double * airspeed_internal = &airspeed_internal_memory;
  if (airspeed != ((void *)0))
    *airspeed = xyz_norm(v_bw_b) + 1e-12;
  if (beta != ((void *)0))
  {
    if (airspeed != ((void *)0))
      airspeed_internal = airspeed;
    else
      *airspeed_internal = xyz_norm(v_bw_b) + 1e-12;
    *beta = asin ( v_bw_b->y / *airspeed_internal );
  }
  if (alpha != ((void *)0))
    *alpha = atan2( v_bw_b->z, v_bw_b->x );
}

canonical_original_code:
void get_wind_angles_from_v_bw_b(double *alpha, double *beta, double *airspeed, struct xyz_t *v_bw_b)
{
  double airspeed_internal_memory;
  double *airspeed_internal = &airspeed_internal_memory;
  if (airspeed != ((void *)0))
    *airspeed = xyz_norm(v_bw_b) + 1e-12;
  if (beta != ((void *)0))
  {
    if (airspeed != ((void *)0))
      airspeed_internal = airspeed;
    else
      *airspeed_internal = xyz_norm(v_bw_b) + 1e-12;
    *beta = asin ( v_bw_b->y / *airspeed_internal );
  }
  if (alpha != ((void *)0))
    *alpha = atan2( v_bw_b->z, v_bw_b->x );
}

variable_types:
alpha
double *

variable_types:
beta
double *

variable_types:
airspeed
double *


decompiled_code:
void __fastcall get_wind_angles(double *a1, double *a2, double *a3, __int64 a4, double *a5, __int64 a6, __int64 a7)
{
double v10[4]; // [rsp+0h] [rbp-68h] BYREF
char v11[72]; // [rsp+20h] [rbp-48h] BYREF

rot_vec_by_quat_a2b((__int64)v11, a5, a7);
xyz_diff(v10, a6, v11);
get_wind_angles_from_v_bw_b(a1, a2, a3, v10);
if ( a4 )
xyz_memcpy(a4, v10);
}

canonical_decompiled_code:
void __fastcall func13(double *a1, double *a2, double *a3, __int64 a4, double *a5, __int64 a6, __int64 a7)
{
double v10[4]; // [rsp+0h] [rbp-68h] BYREF
char v11[72]; // [rsp+20h] [rbp-48h] BYREF

func10((__int64)v11, a5, a7);
xyz_diff(v10, a6, v11);
func12(a1, a2, a3, v10);
if ( a4 )
xyz_memcpy(a4, v10);
}

original_code:
void
get_wind_angles( double * alpha,
                 double * beta,
                 double * airspeed,
                 xyz_t * v_bw_b_out,
                 const quat_t * const q_n2b,
                 const xyz_t * const v_bn_b,
                 const xyz_t * const v_wn_n)
{
  xyz_t v_wn_b;
  rot_vec_by_quat_a2b( &v_wn_b, q_n2b, v_wn_n);
  xyz_t v_bw_b;
  xyz_diff( &v_bw_b, v_bn_b, &v_wn_b);
  get_wind_angles_from_v_bw_b( alpha, beta, airspeed, &v_bw_b );
  if (v_bw_b_out != ((void *)0))
    xyz_memcpy( v_bw_b_out, &v_bw_b);
}

canonical_original_code:
void get_wind_angles(double *alpha, double *beta, double *airspeed, struct xyz_t *v_bw_b_out, struct quat_t *q_n2b, struct xyz_t *v_bn_b, struct xyz_t *v_wn_n)
{
  struct xyz_t v_wn_b;
  rot_vec_by_quat_a2b( &v_wn_b, q_n2b, v_wn_n);
  struct xyz_t v_bw_b;
  xyz_diff( &v_bw_b, v_bn_b, &v_wn_b);
  get_wind_angles_from_v_bw_b( alpha, beta, airspeed, &v_bw_b );
  if (v_bw_b_out != ((void *)0))
    xyz_memcpy( v_bw_b_out, &v_bw_b);
}


decompiled_code:
void __fastcall v_bw_b_from_wind_angles(double *a1, double a2, double a3, double a4)
{
double v4; // rbx
double v5; // r12
double cosx; // [rsp+10h] [rbp-38h] BYREF
double sinx[6]; // [rsp+18h] [rbp-30h] BYREF

sincos(a3, sinx, &cosx);
v4 = cosx;
v5 = sinx[0];
sincos(a2, sinx, &cosx);
*a1 = a4 * cosx * v4;
a1[1] = v5 * a4;
a1[2] = v4 * a4 * sinx[0];
}

canonical_decompiled_code:
void __fastcall func14(double *a1, double a2, double a3, double a4)
{
double v4; // rbx
double v5; // r12
double cosx; // [rsp+10h] [rbp-38h] BYREF
double sinx[6]; // [rsp+18h] [rbp-30h] BYREF

sincos(a3, sinx, &cosx);
v4 = cosx;
v5 = sinx[0];
sincos(a2, sinx, &cosx);
*a1 = a4 * cosx * v4;
a1[1] = v5 * a4;
a1[2] = v4 * a4 * sinx[0];
}

original_code:
void
v_bw_b_from_wind_angles( xyz_t * v_bw_b, const double alpha, const double beta, const double airspeed)
{
  v_bw_b->x = airspeed*cos(alpha)*cos(beta);
  v_bw_b->y = airspeed*sin(beta);
  v_bw_b->z = airspeed*cos(beta)*sin(alpha);
}

canonical_original_code:
void v_bw_b_from_wind_angles(struct xyz_t *v_bw_b, double alpha, double beta, double airspeed)
{
  v_bw_b->x = airspeed*cos(alpha)*cos(beta);
  v_bw_b->y = airspeed*sin(beta);
  v_bw_b->z = airspeed*cos(beta)*sin(alpha);
}

variable_types: alpha double

variable_types: beta double

variable_types: airspeed double


bound

decompiled_code:
double __fastcall bound(double a1, double a2, double a3)
{
float v3; // xmm0_4

v3 = a1;
if ( a2 > v3 )
v3 = a2;
if ( v3 > a3 )
v3 = a3;
return v3;
}

canonical_decompiled_code:
double __fastcall func0(double a1, double a2, double a3)
{
float v3; // xmm0_4

v3 = a1;
if ( a2 > v3 )
v3 = a2;
if ( v3 > a3 )
v3 = a3;
return v3;
}

original_code:
double bound(const double in, const double min, const double max)
{
 float ret = in;
 if(ret<min) ret = min;
 if(ret>max) ret = max;
 return ret;
}

canonical_original_code:
double bound(double in, double min, double max)
{
 float ret = in;
 if(ret<min) ret = min;
 if(ret>max) ret = max;
 return ret;
}

variable_types: in double

variable_types: min double

variable_types: max double

variable_types: ret float


decompiled_code:
void __fastcall simple_lowpass(double *a1, double a2, double a3, double a4)
{
double v4; // xmm0_8

v4 = exp(COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC0) / a3);
*a1 = (1.0 - v4) * a4 + v4 * *a1;
}

canonical_decompiled_code:
void __fastcall func0(double *a1, double a2, double a3, double a4)
{
double v4; // xmm0_8

v4 = exp(COERCE_DOUBLE(*(_QWORD *)&a2 ^ LC0) / a3);
*a1 = (1.0 - v4) * a4 + v4 * *a1;
}

original_code:
void
simple_lowpass(double dt, double tau, double *state, double input)
{
  double emdt = exp(-dt/tau);
  *state = (*state)*emdt + input*(1-emdt);
}

variable_types: dt double

variable_types: tau double

variable_types:
state
double *

variable_types: input double

variable_types: emdt double


decompiled_code:
void __fastcall xyz_simple_lowpass(double *a1, double *a2, double a3, double a4)
{
simple_lowpass(a1, a3, a4, *a2);
simple_lowpass(a1 + 1, a3, a4, a2[1]);
simple_lowpass(a1 + 2, a3, a4, a2[2]);
}

canonical_decompiled_code:
void __fastcall func1(double *a1, double *a2, double a3, double a4)
{
func0(a1, a3, a4, *a2);
func0(a1 + 1, a3, a4, a2[1]);
func0(a1 + 2, a3, a4, a2[2]);
}

original_code:
void
xyz_simple_lowpass(double dt, double tau, xyz_t * state, const xyz_t * const input)
{
  simple_lowpass(dt, tau, &(state->x), input->x);
  simple_lowpass(dt, tau, &(state->y), input->y);
  simple_lowpass(dt, tau, &(state->z), input->z);
}

canonical_original_code:
void xyz_simple_lowpass(double dt, double tau, struct xyz_t *state, struct xyz_t *input)
{
  simple_lowpass(dt, tau, &(state->x), input->x);
  simple_lowpass(dt, tau, &(state->y), input->y);
  simple_lowpass(dt, tau, &(state->z), input->z);
}

variable_types:
input
struct xyz_t *


decompiled_code:
double __fastcall simple_pid(double *a1, double *a2, double a3, double a4, double a5, double a6, double a7)
{
double v9; // xmm0_8
double v10; // xmm4_8
double v11; // xmm3_8
double v12; // xmm1_8

if ( *a2 < 0.0 )
puts("warning: negative p gain in simple_pid");
if ( a2[1] < 0.0 )
puts("warning: negative i gain in simple_pid");
if ( a2[2] < 0.0 )
puts("warning: negative d gain in simple_pid");
if ( a2[3] < 0.0 )
puts("warning: negative integral state bound simple_pid");
v9 = a6 - a4;
v10 = a7 * (a5 - a3) + *a1;
*a1 = v10;
v11 = fmin(a2[3], v10);
*a1 = v11;
v12 = fmax(COERCE_DOUBLE(*((_QWORD *)a2 + 3) ^ LC0), v11);
*a1 = v12;
return (v9 * a2[2] + v12 * a2[1] + a5 - a3) * *a2;
}

canonical_decompiled_code:
double __fastcall func2(double *a1, double *a2, double a3, double a4, double a5, double a6, double a7)
{
double v9; // xmm0_8
double v10; // xmm4_8
double v11; // xmm3_8
double v12; // xmm1_8

if ( *a2 < 0.0 )
puts("warning: negative p gain in simple_pid");
if ( a2[1] < 0.0 )
puts("warning: negative i gain in simple_pid");
if ( a2[2] < 0.0 )
puts("warning: negative d gain in simple_pid");
if ( a2[3] < 0.0 )
puts("warning: negative integral state bound simple_pid");
v9 = a6 - a4;
v10 = a7 * (a5 - a3) + *a1;
*a1 = v10;
v11 = fmin(a2[3], v10);
*a1 = v11;
v12 = fmax(COERCE_DOUBLE(*((_QWORD *)a2 + 3) ^ LC0), v11);
*a1 = v12;
return (v9 * a2[2] + v12 * a2[1] + a5 - a3) * *a2;
}

original_code:
double
simple_pid(const double y,
           const double y_dot,
           const double r,
           const double r_dot,
           const double dt,
           double *i_state,
           const pid_conf_t * const c)
{
  if (c->kp < 0.0) printf("warning: negative p gain in simple_pid\n");
  if (c->ki < 0.0) printf("warning: negative i gain in simple_pid\n");
  if (c->kd < 0.0) printf("warning: negative d gain in simple_pid\n");
  if (c->integral_bound < 0.0) printf("warning: negative integral state bound simple_pid\n");
  double err = r - y;
  double err_dot = r_dot - y_dot;
  *i_state += err*dt;
  { if ((*i_state) > (c->integral_bound)) (*i_state) = (c->integral_bound); if ((*i_state) < (-c->integral_bound)) (*i_state) = (-c->integral_bound); };
  return c->kp * (err + c->ki * (*i_state) + c->kd * err_dot);
}

canonical_original_code:
double simple_pid(double y, double y_dot, double r, double r_dot, double dt, double *i_state, struct pid_conf_t *c)
{
  if (c->kp < 0.0) printf("warning: negative p gain in simple_pid\n");
  if (c->ki < 0.0) printf("warning: negative i gain in simple_pid\n");
  if (c->kd < 0.0) printf("warning: negative d gain in simple_pid\n");
  if (c->integral_bound < 0.0) printf("warning: negative integral state bound simple_pid\n");
  double err = r - y;
  double err_dot = r_dot - y_dot;
  *i_state += err*dt;
  { if ((*i_state) > (c->integral_bound)) (*i_state) = (c->integral_bound); if ((*i_state) < (-c->integral_bound)) (*i_state) = (-c->integral_bound); };
  return c->kp * (err + c->ki * (*i_state) + c->kd * err_dot);
}

variable_types: y double

variable_types: r double

variable_types: struct pid_conf_t { double kp; double ki; double kd; double integral_bound; } *

variable_types: err double


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

result = malloc(0x20uLL);
if ( !result )
exit(1);
result[6] = 0;
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

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

result = malloc(0x20uLL);
if ( !result )
exit(1);
result[6] = 0;
*(_QWORD *)result = 0LL;
*((_QWORD *)result + 1) = 0LL;
*((_QWORD *)result + 2) = 0LL;
return result;
}

original_code:
buffer *buffer_init(){
    buffer *buf;
    buf = (buffer *)malloc(sizeof(buffer));
    if(!buf)
        exit(1);
 buf->size = 0;
 buf->first = ((void *)0);
 buf->last = ((void *)0);
    buf->current = ((void *)0);
    return buf;
}

canonical_original_code:
struct buffer *buffer_init(){
    struct buffer *buf;
    buf = (struct buffer *)malloc(sizeof(buffer));
    if(!buf)
        exit(1);
 buf->size = 0;
 buf->first = ((void *)0);
 buf->last = ((void *)0);
    buf->current = ((void *)0);
    return buf;
}

variable_types: struct buffer { struct node { struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *val; struct node *next; } *first; struct node *last; struct node *current; int size; } *

user_defined_types: struct buffer { struct node *first; struct node *last; struct node *current; int size; }

user_defined_types: struct node { struct matrix *val; struct node *next; }

user_defined_types: struct matrix { int row; struct elemt *first; }


enqueue

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

result = *((unsigned int *)a1 + 6);
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
a1[1] = a2;
result = *a1;
*(_QWORD *)(*a1 + 8) = a2;
++*((_DWORD *)a1 + 6);
}
else if ( (int)result > 1 )
{
result = a1[1];
*(_QWORD *)(result + 8) = a2;
a1[1] = a2;
++*((_DWORD *)a1 + 6);
}
}
else
{
*a1 = a2;
*((_DWORD *)a1 + 6) = 1;
a1[2] = a2;
}
return result;
}

original_code:
void enqueue(buffer *buf, struct node *new){
 if(buf->size==0){
  buf->first = new;
  buf->size = buf->size+1;
        buf->current = new;
 }
 else if(buf->size==1){
  buf->last=new;
  buf->first->next = new;
  buf->size = buf->size+1;
 }
 else if(buf->size > 1){
  (buf->last)->next = new;
  buf->last = new;
  buf->size = buf->size+1;
 }
}

canonical_original_code:
void enqueue(struct buffer *buf, struct node *new){
 if(buf->size==0){
  buf->first = new;
  buf->size = buf->size+1;
        buf->current = new;
 }
 else if(buf->size==1){
  buf->last=new;
  buf->first->next = new;
  buf->size = buf->size+1;
 }
 else if(buf->size > 1){
  (buf->last)->next = new;
  buf->last = new;
  buf->size = buf->size+1;
 }
}

variable_types:
new
struct node *

user_defined_types: struct elemt { int rPos; struct elemt *next; }


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

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

original_code:
void node_free(struct node *node){
 free(node);
}

variable_types: struct node { struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *val; struct node *next; } *


dequeue

original_code:
matrix *dequeue(buffer *buf){
 struct node *tempNode;
 matrix *temp;
 if(buf->size==2){
  temp = (buf->first)->val;
  buf->size=buf->size-1;
  tempNode = buf->first;
  buf->first = buf->last;
  buf->last = ((void *)0);
  node_free(tempNode);
        buf->current = buf->first;
 }
 else if(buf->size==1){
  temp = (buf->first)->val;
  tempNode = buf->first;
  buf->size=buf->size-1;
  node_free(tempNode);
  buf->first=((void *)0);
        buf->current=((void *)0);
 }
 else if(buf->size > 2){
        if(buf->current == buf->first){
            buf->current = buf->first->next;
        }
  tempNode = buf->first;
  temp = tempNode->val;
  buf->first=(buf->first)->next;
  node_free(tempNode);
  buf->size-=1;
 }
 return temp;
}

canonical_original_code:
struct matrix *dequeue(struct buffer *buf){
 struct node *tempNode;
 struct matrix *temp;
 if(buf->size==2){
  temp = (buf->first)->val;
  buf->size=buf->size-1;
  tempNode = buf->first;
  buf->first = buf->last;
  buf->last = ((void *)0);
  node_free(tempNode);
        buf->current = buf->first;
 }
 else if(buf->size==1){
  temp = (buf->first)->val;
  tempNode = buf->first;
  buf->size=buf->size-1;
  node_free(tempNode);
  buf->first=((void *)0);
        buf->current=((void *)0);
 }
 else if(buf->size > 2){
        if(buf->current == buf->first){
            buf->current = buf->first->next;
        }
  tempNode = buf->first;
  temp = tempNode->val;
  buf->first=(buf->first)->next;
  node_free(tempNode);
  buf->size-=1;
 }
 return temp;
}

variable_types: struct buffer { struct node { struct matrix *val; struct node *next; } *first; struct node *last; struct node *current; int size; } *

variable_types: struct matrix { int row; struct elemt { int rPos; struct elemt *next; } *first; } *


decompiled_code:
void __fastcall node_matrix_free(void **a1)
{
matrix_free(*a1);
node_free(a1);
}

canonical_decompiled_code:
void __fastcall func11(void **a1)
{
func13(*a1);
func9(a1);
}

original_code:
void node_matrix_free(struct node *node){
 matrix_free(node->val);
 node_free(node);
}


decompiled_code:
void __fastcall buffer_free(void *ptr)
{
int v2; // eax
void **v3; // rdi

v2 = *((_DWORD *)ptr + 6);
if ( v2 )
{
if ( v2 == 1 )
{
node_matrix_free(*(void ***)ptr);
free(ptr);
}
else
{
v3 = *(void ***)ptr;
if ( v3 )
{
do
{
*(_QWORD *)ptr = v3[1];
node_matrix_free(v3);
v3 = *(void ***)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}
}
else
{
node_free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
int v2; // eax
void **v3; // rdi

v2 = *((_DWORD *)ptr + 6);
if ( v2 )
{
if ( v2 == 1 )
{
func11(*(void ***)ptr);
free(ptr);
}
else
{
v3 = *(void ***)ptr;
if ( v3 )
{
do
{
*(_QWORD *)ptr = v3[1];
func11(v3);
v3 = *(void ***)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}
}
else
{
func9(*(void **)ptr);
free(ptr);
}
}

original_code:
void buffer_free(buffer *buf){
 if(buf->size == 0){
  node_free(buf->first);
  free(buf);
 }
 else if(buf->size == 1){
        node_matrix_free(buf->first);
  free(buf);
 }
 else{
  while(buf->first != ((void *)0)){
   struct node *temp = buf->first;
   buf->first = buf->first->next;
   node_matrix_free(temp);
  }
  free(buf);
 }
}

canonical_original_code:
void buffer_free(struct buffer *buf){
 if(buf->size == 0){
  node_free(buf->first);
  free(buf);
 }
 else if(buf->size == 1){
        node_matrix_free(buf->first);
  free(buf);
 }
 else{
  while(buf->first != ((void *)0)){
   struct node *temp = buf->first;
   buf->first = buf->first->next;
   node_matrix_free(temp);
  }
  free(buf);
 }
}


repo: ghostshadow/cgaus

decompiled_code:
void __fastcall mat_mull(double *a1, int a2, double a3)
{
double *v3; // rax

if ( a2 > 0 )
{
v3 = a1;
do
{
*v3 = a3 * *v3;
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

canonical_decompiled_code:
void __fastcall func8(double *a1, int a2, double a3)
{
double *v3; // rax

if ( a2 > 0 )
{
v3 = a1;
do
{
*v3 = a3 * *v3;
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

original_code:
static void mat_mull(double* line,int col,double fact) {
 for(int ct=0;ct<col;++ct) line[ct]*=fact;
}

canonical_original_code:
static void mat_mull(double *line, int col, double fact) {
 for(int ct = 0;ct<col;++ct) line[ct]*=fact;
}

variable_types:
line
double *

variable_types: col int

variable_types: fact double

variable_types: ct int


original_code:
static void mat_addl(double* tline,int col,double* sline) {
 for(int ct=0;ct<col;++ct) tline[ct]+=sline[ct];
}

canonical_original_code:
static void mat_addl(double *tline, int col, double *sline) {
 for(int ct = 0;ct<col;++ct) tline[ct]+=sline[ct];
}

variable_types:
tline
double *

variable_types:
sline
double *


REPOOM

decompiled_code:
void __fastcall __noreturn REPOOM(const char *a1)
{
fprintf(stderr, "\n\nOut of memory: %s\n\n", a1);
abort();
}

canonical_decompiled_code:
void __fastcall __noreturn func11(const char *a1)
{
fprintf(stderr, "\n\nOut of memory: %s\n\n", a1);
abort();
}

original_code:
static inline void REPOOM(char* x) {
 fprintf(stderr,"\n\nOut of memory: %s\n\n",x);
 abort();
}

canonical_original_code:
static inline void REPOOM(char *x) {
 fprintf(stderr,"\n\nOut of memory: %s\n\n",x);
 abort();
}

variable_types:
x
char *


original_code:
static int mat_swapl(mxp mat,int spos) {
 if((mat->val)[spos-1][spos-1]!=0) ;
 double* tmpp;
 if(!(tmpp=(mat->val)[spos-1]))
  REPOOM("tmpp@mat_swapl()");
 for(int rt=spos;rt<(mat->row);++rt) {
  if((mat->val)[rt][spos-1]==0)
   continue;
  (mat->val)[spos-1]=(mat->val)[rt];
  (mat->val)[rt]=tmpp;
  return 1;
 }
 return 0;
}

canonical_original_code:
static int mat_swapl(struct mat *mat, int spos) {
 if((mat->val)[spos-1][spos-1]!=0) ;
 double *tmpp;
 if(!(tmpp=(mat->val)[spos-1]))
  REPOOM("tmpp@mat_swapl()");
 for(int rt = spos;rt<(mat->row);++rt) {
  if((mat->val)[rt][spos-1]==0)
   continue;
  (mat->val)[spos-1]=(mat->val)[rt];
  (mat->val)[rt]=tmpp;
  return 1;
 }
 return 0;
}

variable_types:
mat
struct mat { double **val; int col; } *

variable_types: spos int

variable_types:
tmpp
double *

variable_types: rt int


decompiled_code:
__int64 __fastcall mat_get_col(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code: int mat_get_col(mxp mat) {return mat->col;}

canonical_original_code: int mat_get_col(struct mat *mat) {return mat->col;}


decompiled_code:
__int64 __fastcall mat_get_row(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code: int mat_get_row(mxp mat) {return mat->row;}

canonical_original_code: int mat_get_row(struct mat *mat) {return mat->row;}


decompiled_code:
_QWORD *mat_init()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
_QWORD *v2; // rbp
_QWORD *v3; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
REPOOM("mat@mat_init()");
v1 = v0;
*((_DWORD *)v0 + 2) = 2;
*((_DWORD *)v0 + 3) = 1;
v2 = malloc(8uLL);
*v1 = v2;
if ( !v2 )
REPOOM("mat->val@mat_init()");
v3 = malloc(0x10uLL);
*v2 = v3;
if ( !v3 )
REPOOM("mat->val[0]@mat_init()");
*v3 = 0LL;
v3[1] = 0LL;
return v1;
}

canonical_decompiled_code:
_QWORD *func17()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
_QWORD *v2; // rbp
_QWORD *v3; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
func11("mat@mat_init()");
v1 = v0;
*((_DWORD *)v0 + 2) = 2;
*((_DWORD *)v0 + 3) = 1;
v2 = malloc(8uLL);
*v1 = v2;
if ( !v2 )
func11("mat->val@mat_init()");
v3 = malloc(0x10uLL);
*v2 = v3;
if ( !v3 )
func11("mat->val[0]@mat_init()");
*v3 = 0LL;
v3[1] = 0LL;
return v1;
}

original_code:
mxp mat_init() {
 mxp mat;
 if(!(mat=malloc(sizeof(struct mat))))
  REPOOM("mat@mat_init()");
 (mat->col)=2;
 (mat->row)=1;
 if(!((mat->val)=malloc(sizeof(double*))))
  REPOOM("mat->val@mat_init()");
 if(!((mat->val)[0]=malloc(2*sizeof(double))))
  REPOOM("mat->val[0]@mat_init()");
 (mat->val)[0][0]=0;
 (mat->val)[0][1]=0;
 return mat;
}

canonical_original_code:
struct mat *mat_init() {
 struct mat *mat;
 if(!(mat=malloc(sizeof(struct mat))))
  REPOOM("mat@mat_init()");
 (mat->col)=2;
 (mat->row)=1;
 if(!((mat->val)=malloc(sizeof(double *))))
  REPOOM("mat->val@mat_init()");
 if(!((mat->val)[0]=malloc(2*sizeof(double))))
  REPOOM("mat->val[0]@mat_init()");
 (mat->val)[0][0]=0;
 (mat->val)[0][1]=0;
 return mat;
}

return_type: struct mat { double **val; int col; } *


decompiled_code:
void __fastcall mat_destr(void *ptr)
{
__int64 v1; // rbx

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

canonical_decompiled_code:
void __fastcall func18(void *ptr)
{
__int64 v1; // rbx

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

original_code:
void mat_destr(mxp mat) {
 if(!(mat==((void *)0))) {
  for(int rt=0;rt<(mat->row);++rt)
   free((mat->val)[rt]);
  free(mat->val);
  (mat->col)=0;
  (mat->row)=0;
  free(mat);
 }
}

canonical_original_code:
void mat_destr(struct mat *mat) {
 if(!(mat==((void *)0))) {
  for(int rt = 0;rt<(mat->row);++rt)
   free((mat->val)[rt]);
  free(mat->val);
  (mat->col)=0;
  (mat->row)=0;
  free(mat);
 }
}


decompiled_code:
double __fastcall mat_get_val(__int64 a1, int a2, int a3)
{
double result; // xmm0_8

result = 0.0;
if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 0.0;
else
result = *(double *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 8LL * a3);
}
return result;
}

canonical_decompiled_code:
double __fastcall func20(__int64 a1, int a2, int a3)
{
double result; // xmm0_8

result = 0.0;
if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 0.0;
else
result = *(double *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) + 8LL * a3);
}
return result;
}

original_code:
double mat_get_val(mxp mat,int r,int c) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  return (mat->val)[r][c];
 return -0;
}

canonical_original_code:
double mat_get_val(struct mat *mat, int r, int c) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  return (mat->val)[r][c];
 return -0;
}

variable_types: r int

variable_types: c int


decompiled_code:
__int64 __fastcall mat_set_val(__int64 a1, int a2, int a3, double a4)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 && a2 >= 0 && a3 >= 0 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
*(double *)(result + 8LL * a3) = a4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, int a2, int a3, double a4)
{
__int64 result; // rax

if ( *(_DWORD *)(a1 + 12) > a2 && *(_DWORD *)(a1 + 8) > a3 && a2 >= 0 && a3 >= 0 )
{
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
*(double *)(result + 8LL * a3) = a4;
}
return result;
}

original_code:
void mat_set_val(mxp mat,int r,int c,double val) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  (mat->val)[r][c]=val;
}

canonical_original_code:
void mat_set_val(struct mat *mat, int r, int c, double val) {
 if(r<(mat->row) && c<(mat->col) && r>=0 && c>=0)
  (mat->val)[r][c]=val;
}

variable_types: val double


decompiled_code:
__int64 __fastcall mat_solve(__int64 *a1)
{
mat_solve_norm((__int64)a1);
return mat_solvable(a1);
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 *a1)
{
func14((__int64)a1);
return func10(a1);
}

original_code:
int mat_solve(mxp mat) {
 mat_solve_norm(mat);
 return mat_solvable(mat);
}

canonical_original_code:
int mat_solve(struct mat *mat) {
 mat_solve_norm(mat);
 return mat_solvable(mat);
}


repo: ghuls/moreutils-parallel

decompiled_code:
size_t warning_prefix()
{
fputs(progname, stderr);
return fwrite(&unk_402004, 1uLL, 2uLL, stderr);
}

canonical_decompiled_code:
size_t func7()
{
fputs(progname, stderr);
return fwrite(&unk_402004, 1uLL, 2uLL, stderr);
}

original_code:
static void warning_prefix(void) {
 fputs(progname, stderr);
 fputs(": ", stderr);
}

canonical_original_code:
static void warning_prefix() {
 fputs(progname, stderr);
 fputs(": ", stderr);
}


decompiled_code:
void __fastcall __noreturn error_with_errno(char *s)
{
unsigned int v1; // ebp

v1 = *__errno_location();
warning_prefix();
fputs(s, stderr);
fwrite(": error number ", 1uLL, 0xFuLL, stderr);
fprintf(stderr, "%i\n", v1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(char *s)
{
unsigned int v1; // ebp

v1 = *__errno_location();
func7();
fputs(s, stderr);
fwrite(": error number ", 1uLL, 0xFuLL, stderr);
fprintf(stderr, "%i\n", v1);
exit(1);
}

original_code:
__attribute__(( noreturn ))
static void error_with_errno(const char* message) {
 int saved_errno;
 saved_errno=(*__errno_location ());
 warning_prefix();
 fputs(message, stderr);
 fputs(": error number ", stderr);
 fprintf(stderr, "%i\n", saved_errno);
 exit(1);
}

canonical_original_code:
static void error_with_errno(char *message) {
 int saved_errno;
 saved_errno=(*__errno_location ());
 warning_prefix();
 fputs(message, stderr);
 fputs(": error number ", stderr);
 fprintf(stderr, "%i\n", saved_errno);
 exit(1);
}

variable_types:
message
char *


decompiled_code:
void __fastcall __noreturn subprocess2(char *command)
{
if ( close(0) )
error_with_errno("Failed (in child) closing standard input");
if ( dup2(filedes[0], 0) != -1 )
{
if ( close(filedes[0]) )
error_with_errno("Failed (in child) closing filedes[0]");
if ( close(fd) )
error_with_errno("Failed (in child) closing filedes[1]");
system(command);
if ( close(0) )
error_with_errno("Failed (in child) closing standard output (while cleaning up)");
exit(0);
}
error_with_errno("Failed (in child) redefining standard input");
}

canonical_decompiled_code:
void __fastcall __noreturn func9(char *command)
{
if ( close(0) )
func8("Failed (in child) closing standard input");
if ( dup2(filedes[0], 0) != -1 )
{
if ( close(filedes[0]) )
func8("Failed (in child) closing filedes[0]");
if ( close(fd) )
func8("Failed (in child) closing filedes[1]");
system(command);
if ( close(0) )
func8("Failed (in child) closing standard output (while cleaning up)");
exit(0);
}
func8("Failed (in child) redefining standard input");
}

original_code:
__attribute__(( noreturn ))
static void subprocess2(const char* cmd) {
 if (close(0))
  error_with_errno("Failed (in child) closing standard input");
 if (dup2(filedes[0], 0) == -1)
  error_with_errno("Failed (in child) redefining standard input");
 if (close(filedes[0]))
  error_with_errno("Failed (in child) closing filedes[0]");
 if (close(filedes[1]))
  error_with_errno("Failed (in child) closing filedes[1]");
 system(cmd);
 if (close(0))
  error_with_errno("Failed (in child) closing standard output "
   " (while cleaning up)");
 exit(0);
}

canonical_original_code:
static void subprocess2(char *cmd) {
 if (close(0))
  error_with_errno("Failed (in child) closing standard input");
 if (dup2(filedes[0], 0) == -1)
  error_with_errno("Failed (in child) redefining standard input");
 if (close(filedes[0]))
  error_with_errno("Failed (in child) closing filedes[0]");
 if (close(filedes[1]))
  error_with_errno("Failed (in child) closing filedes[1]");
 system(cmd);
 if (close(0))
  error_with_errno("Failed (in child) closing standard output "
   " (while cleaning up)");
 exit(0);
}

variable_types:
cmd
char *


decompiled_code:
__int64 __fastcall shorten_status(__int16 a1)
{
int v2; // edi

v2 = a1 & 0x7F;
if ( !v2 )
return HIBYTE(a1);
if ( !__OFSUB__((a1 & 0x7F) + 1, 1) && (a1 & 0x7F) != 0 )
return (unsigned int)(v2 + 128);
if ( (_BYTE)a1 != 127 )
{
if ( (a1 & 0x80u) != 0 )
error("Ow, somebody dumped core!");
error("shorten_status got an invalid status (?!)");
}
return (unsigned int)HIBYTE(a1) + 128;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int16 a1)
{
int v2; // edi

v2 = a1 & 0x7F;
if ( !v2 )
return HIBYTE(a1);
if ( !__OFSUB__((a1 & 0x7F) + 1, 1) && (a1 & 0x7F) != 0 )
return (unsigned int)(v2 + 128);
if ( (_BYTE)a1 != 127 )
{
if ( (a1 & 0x80u) != 0 )
func10("Ow, somebody dumped core!");
func10("shorten_status got an invalid status (?!)");
}
return (unsigned int)HIBYTE(a1) + 128;
}

original_code:
__attribute__(( const ))
static int shorten_status(int status_big) {
 if ((((status_big) & 0x7f) == 0))
  return (((status_big) & 0xff00) >> 8);
 if ((((signed char) (((status_big) & 0x7f) + 1) >> 1) > 0))
  return 128+((status_big) & 0x7f);
 if ((((status_big) & 0xff) == 0x7f))
  return 128+(((status_big) & 0xff00) >> 8);
 if (((status_big) & 0x80))
   error("Ow, somebody dumped core!");
 error("shorten_status got an invalid status (?!)");
}

canonical_original_code:
static int shorten_status(int status_big) {
 if ((((status_big) & 0x7f) == 0))
  return (((status_big) & 0xff00) >> 8);
 if ((((char) (((status_big) & 0x7f) + 1) >> 1) > 0))
  return 128+((status_big) & 0x7f);
 if ((((status_big) & 0xff) == 0x7f))
  return 128+(((status_big) & 0xff00) >> 8);
 if (((status_big) & 0x80))
   error("Ow, somebody dumped core!");
 error("shorten_status got an invalid status (?!)");
}


error

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

va_start(arg, format);
warning_prefix();
vfprintf(stderr, format, arg);
exit(1);
}

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

va_start(arg, format);
func7();
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
__attribute__(( noreturn, format (printf, 1, 2) ))
static void error(const char* formatstr, ...) {
 va_list ap;
 __builtin_va_start(ap,formatstr);
 warning_prefix();
 vfprintf(stderr, formatstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
static void error(char *formatstr, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,formatstr);
 warning_prefix();
 vfprintf(stderr, formatstr, ap);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
formatstr
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // ebx
__int16 v5; // bp
unsigned int v6; // eax

progname = (char *)*argv;
if ( argc != 3 )
error("Wrong number of args, aborting\n", argv, envp);
if ( pipe(filedes) )
error_with_errno("pipe() failed");
v3 = fork();
v4 = v3;
if ( !v3 )
subprocess2((char *)argv[2]);
if ( v3 == -1 )
error_with_errno("fork() failed");
if ( close(1) )
error_with_errno("Failed closing standard output");
if ( dup2(fd, 1) == -1 )
error_with_errno("Failed redefining standard output");
if ( close(fd) )
error_with_errno("Failed closing filedes[1]");
if ( close(filedes[0]) )
error_with_errno("Failed closing filedes[0]");
v5 = system(argv[1]);
if ( close(1) )
error_with_errno("Failed closing standard output (while cleaning up)");
v6 = waitpid(v4, 0LL, 2);
if ( v6 == -1 )
error_with_errno("waitpid() failed");
if ( v4 != v6 )
error("waitpid(): Who died? %i\n", v6);
return shorten_status(v5);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
__pid_t v4; // ebx
__int16 v5; // bp
unsigned int v6; // eax

progname = (char *)*argv;
if ( argc != 3 )
func10("Wrong number of args, aborting\n", argv, envp);
if ( pipe(filedes) )
func8("pipe() failed");
v3 = fork();
v4 = v3;
if ( !v3 )
func9((char *)argv[2]);
if ( v3 == -1 )
func8("fork() failed");
if ( close(1) )
func8("Failed closing standard output");
if ( dup2(fd, 1) == -1 )
func8("Failed redefining standard output");
if ( close(fd) )
func8("Failed closing filedes[1]");
if ( close(filedes[0]) )
func8("Failed closing filedes[0]");
v5 = system(argv[1]);
if ( close(1) )
func8("Failed closing standard output (while cleaning up)");
v6 = waitpid(v4, 0LL, 2);
if ( v6 == -1 )
func8("waitpid() failed");
if ( v4 != v6 )
func10("waitpid(): Who died? %i\n", v6);
return func11(v5);
}

original_code:
int main (int argc, const char ** argv) {
 int status_big;
 pid_t child2_pid;
 pid_t dead_pid;
 progname = argv[0];
 if (argc != 3)
  error("Wrong number of args, aborting\n");
 if (pipe(filedes))
  error_with_errno("pipe() failed");
 child2_pid = fork();
 if (child2_pid == 0)
  subprocess2(argv[2]);
 if (child2_pid == -1)
  error_with_errno("fork() failed");
 if (close(1))
  error_with_errno("Failed closing standard output");
 if (dup2(filedes[1], 1) == -1)
  error_with_errno("Failed redefining standard output");
 if (close(filedes[1]))
  error_with_errno("Failed closing filedes[1]");
 if (close(filedes[0]))
  error_with_errno("Failed closing filedes[0]");
 status_big = system(argv[1]);
 if (close(1))
  error_with_errno("Failed closing standard output (while cleaning up)");
 dead_pid = waitpid(child2_pid, ((void *)0), 2);
 if (dead_pid == -1) {
  error_with_errno("waitpid() failed");
 }
 else if (dead_pid != child2_pid) {
  error("waitpid(): Who died? %i\n", dead_pid);
 }
 return shorten_status(status_big);
}

canonical_original_code:
int main(int argc, char **argv) {
 int status_big;
 int child2_pid;
 int dead_pid;
 progname = argv[0];
 if (argc != 3)
  error("Wrong number of args, aborting\n");
 if (pipe(filedes))
  error_with_errno("pipe() failed");
 child2_pid = fork();
 if (child2_pid == 0)
  subprocess2(argv[2]);
 if (child2_pid == -1)
  error_with_errno("fork() failed");
 if (close(1))
  error_with_errno("Failed closing standard output");
 if (dup2(filedes[1], 1) == -1)
  error_with_errno("Failed redefining standard output");
 if (close(filedes[1]))
  error_with_errno("Failed closing filedes[1]");
 if (close(filedes[0]))
  error_with_errno("Failed closing filedes[0]");
 status_big = system(argv[1]);
 if (close(1))
  error_with_errno("Failed closing standard output (while cleaning up)");
 dead_pid = waitpid(child2_pid, ((void *)0), 2);
 if (dead_pid == -1) {
  error_with_errno("waitpid() failed");
 }
 else if (dead_pid != child2_pid) {
  error("waitpid(): Who died? %i\n", dead_pid);
 }
 return shorten_status(status_big);
}

variable_types: argc int

variable_types:
argv
char **


sigalarm

decompiled_code:
void __noreturn sigalarm()
{
if ( quiet )
_exit(75);
error(0, 75, "lock file `%s' is already locked (timeout waiting)", *(const char **)lckfile);
}

canonical_decompiled_code:
void __noreturn func8()
{
if ( quiet )
_exit(75);
func7(0, 75, "lock file `%s' is already locked (timeout waiting)", *(const char **)lckfile);
}

original_code:
static void sigalarm(int sig) {
 if (quiet)
  _exit(75);
 error(0, 75,
  "lock file `%s' is already locked (timeout waiting)", lckfile);
}

variable_types: sig int


error

decompiled_code:
void __noreturn error(int errnum, int status, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: ", (const char *)progname);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s\n", v4);
}
else
{
fputc(10, stderr);
}
exit(status);
}

canonical_decompiled_code:
void __noreturn func7(int errnum, int status, char *format, ...)
{
char *v4; // rax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fprintf(stderr, "%s: ", (const char *)progname);
vfprintf(stderr, format, arg);
if ( errnum )
{
v4 = strerror(errnum);
fprintf(stderr, ": %s\n", v4);
}
else
{
fputc(10, stderr);
}
exit(status);
}

original_code:
static void
__attribute__((format(printf,3,4)))
__attribute__((noreturn))
error(int errnum, int exitcode, const char *fmt, ...) {
 va_list ap;
 fprintf(stderr, "%s: ", progname);
 __builtin_va_start(ap,fmt); vfprintf(stderr, fmt, ap); __builtin_va_end(ap);
 if (errnum)
  fprintf(stderr, ": %s\n", strerror(errnum));
 else
  fputs("\n", stderr);
 exit(exitcode);
}

canonical_original_code:
static void error(int errnum, int exitcode, char *fmt, ...) {
 __builtin_va_list ap;
 fprintf(stderr, "%s: ", progname);
 __builtin_va_start(ap,fmt); vfprintf(stderr, fmt, ap); __builtin_va_end(ap);
 if (errnum)
  fprintf(stderr, ": %s\n", strerror(errnum));
 else
  fputs("\n", stderr);
 exit(exitcode);
}

variable_types: errnum int

variable_types: exitcode int

variable_types:
fmt
char *