decompiled_code:
char **init_term()
{
struct termios *v0; // rdi
__int64 v1; // rcx
struct termios *v2; // rsi
char v3; // al
char **result; // rax
struct termios _0; // [rsp+0h] [rbp+0h] BYREF

if ( tcgetattr(0, &_0) == -1 )
{
if ( *__errno_location() != 25 )
perror("tcgetaddr returned");
if ( !isterm )
{
if ( colormode != 97 )
goto LABEL_7;
v3 = 109;
goto LABEL_6;
}
}
else
{
v0 = &saved_tios;
v1 = 15LL;
v2 = &_0;
isterm = 1;
while ( v1 )
{
v0->c_iflag = v2->c_iflag;
v2 = (struct termios *)((char *)v2 + 4);
v0 = (struct termios *)((char *)v0 + 4);
--v1;
}
}
_0.c_lflag &= 0xFFFFFFF5;
tcsetattr(0, 0, &_0);
v3 = 108;
if ( colormode == 97 )
LABEL_6:
colormode = v3;
LABEL_7:
result = get_colors();
colors = (__int64)result;
return result;
}

canonical_decompiled_code:
char **func13()
{
struct termios *v0; // rdi
__int64 v1; // rcx
struct termios *v2; // rsi
char v3; // al
char **result; // rax
struct termios _0; // [rsp+0h] [rbp+0h] BYREF

if ( tcgetattr(0, &_0) == -1 )
{
if ( *__errno_location() != 25 )
perror("tcgetaddr returned");
if ( !isterm )
{
if ( colormode != 97 )
goto LABEL_7;
v3 = 109;
goto LABEL_6;
}
}
else
{
v0 = &saved_tios;
v1 = 15LL;
v2 = &_0;
isterm = 1;
while ( v1 )
{
v0->c_iflag = v2->c_iflag;
v2 = (struct termios *)((char *)v2 + 4);
v0 = (struct termios *)((char *)v0 + 4);
--v1;
}
}
_0.c_lflag &= 0xFFFFFFF5;
tcsetattr(0, 0, &_0);
v3 = 108;
if ( colormode == 97 )
LABEL_6:
colormode = v3;
LABEL_7:
result = func12();
colors = (__int64)result;
return result;
}

original_code:
static void init_term(void)
{
 struct termios tios;
 if (tcgetattr(0, &tios) == -1) {
  if ((*__errno_location ()) != 25)
   perror("tcgetaddr returned");
 } else {
  isterm = 1;
  saved_tios = tios;
 }
 if (isterm) {
  tios.c_lflag &= ~(0000002|0000010);
  tcsetattr(0, 0, &tios);
 }
 if (colormode == 'a')
  colormode = isterm? 'l':'m';
 colors = get_colors();
}

canonical_original_code:
static void init_term()
{
 struct termios tios;
 if (tcgetattr(0, &tios) == -1) {
  if ((*__errno_location ()) != 25)
   perror("tcgetaddr returned");
 } else {
  isterm = 1;
  saved_tios = tios;
 }
 if (isterm) {
  tios.c_lflag &= ~(0000002|0000010);
  tcsetattr(0, 0, &tios);
 }
 if (colormode == 'a')
  colormode = isterm? 'l':'m';
 colors = get_colors();
}

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


mkanswer

repo: neuschaefer/mind

decompiled_code:
__int64 mkanswer()
{
int *v0; // rbx
unsigned int v1; // eax
__int64 result; // rax

v0 = (int *)answer;
v1 = time(0LL);
srand(v1);
do
{
v0 = (int *)((char *)v0 + 1);
result = (unsigned int)(rand() % 9 + 48);
*((_BYTE *)v0 - 1) = result;
}
while ( v0 != &chances );
return result;
}

canonical_decompiled_code:
__int64 func14()
{
int *v0; // rbx
unsigned int v1; // eax
__int64 result; // rax

v0 = (int *)answer;
v1 = time(0LL);
srand(v1);
do
{
v0 = (int *)((char *)v0 + 1);
result = (unsigned int)(rand() % 9 + 48);
*((_BYTE *)v0 - 1) = result;
}
while ( v0 != &chances );
return result;
}

original_code:
static void mkanswer(void)
{
 int i;
 srand(time(((void *)0)));
 for (i = 0; i < 4; i++) {
  answer[i] = '0' + (rand() % ('9'-'0'));
 }
}

canonical_original_code:
static void mkanswer()
{
 int i;
 srand(time(((void *)0)));
 for (i = 0; i < 4; i++) {
  answer[i] = '0' + (rand() % ('9'-'0'));
 }
}

variable_types: i int

return_type: void


decompiled_code:
_BOOL8 __fastcall get_choice(__int64 a1)
{
int v1; // er12
int v3; // ebp
_BOOL8 result; // rax
__int64 v5; // rax
int v6; // edi

v1 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
v3 = getchar();
result = is_valid(v3);
if ( !result || v1 > 3 )
break;
v5 = v1++;
*(_BYTE *)(a1 + v5) = v3;
putchar(v3);
putchar(32);
}
if ( v3 == 10 || v3 == 13 )
break;
if ( ((*__ctype_b_loc())[v3] & 2) != 0 )
{
if ( v3 == 127 && v1 )
{
--v1;
choice_back(1);
}
else if ( v3 == 21 )
{
if ( v1 )
{
v6 = v1;
v1 = 0;
choice_back(v6);
}
}
}
}
}
while ( v1 != 4 );
return result;
}

original_code:
static void get_choice(char *choice)
{
 int pos = 0, ch;
 while (1) {
  ch = getchar();
  if (is_valid(ch) && pos < 4) {
   choice[pos] = ch;
   putchar(ch);
   putchar(' ');
   pos++;
  } else if (ch == '\n' || ch == '\r') {
   if (pos == 4)
    break;
  } else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _IScntrl)) {
   if (ch == 127 && pos) {
    choice_back(1);
    --pos;
   } else if (ch == 21 && pos) {
    choice_back(pos);
    pos = 0;
   }
  }
 }
}

canonical_original_code:
static void get_choice(char *choice)
{
 int pos = 0; int ch;
 while (1) {
  ch = getchar();
  if (is_valid(ch) && pos < 4) {
   choice[pos] = ch;
   putchar(ch);
   putchar(' ');
   pos++;
  } else if (ch == '\n' || ch == '\r') {
   if (pos == 4)
    break;
  } else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _IScntrl)) {
   if (ch == 127 && pos) {
    choice_back(1);
    --pos;
   } else if (ch == 21 && pos) {
    choice_back(pos);
    pos = 0;
   }
  }
 }
}

variable_types:
choice
char *

variable_types: pos int

variable_types: ch int


repo: never-run/spark

decompiled_code:
_BOOL8 __fastcall so_filter(const struct dirent *a1)
{
return strcmp(rindex(a1->d_name, 46), ".so") == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const struct dirent *a1)
{
return strcmp(rindex(a1->d_name, 46), ".so") == 0;
}

original_code:
static int so_filter(const struct dirent *de)
{
 if (!strcmp(rindex(de->d_name, '.'), ".so"))
  return 1;
 else
  return 0;
}

canonical_original_code:
static int so_filter(struct dirent *de)
{
 if (!strcmp(rindex(de->d_name, '.'), ".so"))
  return 1;
 else
  return 0;
}

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *

return_type: int


decompiled_code:
void *__fastcall get_exact_lib(char *src, __int64 a2, int a3, const char *a4)
{
__int64 v5; // rbx
__int64 v6; // r13
__int64 (*v7)(void); // rax
const char *v8; // rax
void *v9; // rax
void *v10; // rbp
char *v11; // rax
char *v13; // rax
char v14[296]; // [rsp+0h] [rbp-128h] BYREF

if ( a3 )
{
v5 = a2 + 8LL * (a3 - 1);
v6 = v5 - 8 - 8LL * (unsigned int)(a3 - 1);
do
{
while ( 1 )
{
strncpy(v14, src, 0x100uLL);
strncat(v14, (const char *)(*(_QWORD *)v5 + 19LL), 0x100uLL);
v9 = dlopen(v14, 258);
v10 = v9;
if ( v9 )
break;
v11 = dlerror();
v5 -= 8LL;
fprintf(stderr, "dlopen : %s\n", v11);
if ( v5 == v6 )
return 0LL;
}
v7 = (__int64 (*)(void))dlsym(v9, "cap_mod_getname");
if ( v7 )
{
v8 = (const char *)v7();
if ( !strcmp(v8, a4) )
return v10;
dlclose(v10);
}
else
{
v13 = dlerror();
fprintf(stderr, "dlsym cap_mod_getname : %s\n", v13);
dlclose(v10);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(char *src, __int64 a2, int a3, const char *a4)
{
__int64 v5; // rbx
__int64 v6; // r13
__int64 (*v7)(void); // rax
const char *v8; // rax
void *v9; // rax
void *v10; // rbp
char *v11; // rax
char *v13; // rax
char v14[296]; // [rsp+0h] [rbp-128h] BYREF

if ( a3 )
{
v5 = a2 + 8LL * (a3 - 1);
v6 = v5 - 8 - 8LL * (unsigned int)(a3 - 1);
do
{
while ( 1 )
{
strncpy(v14, src, 0x100uLL);
strncat(v14, (const char *)(*(_QWORD *)v5 + 19LL), 0x100uLL);
v9 = dlopen(v14, 258);
v10 = v9;
if ( v9 )
break;
v11 = dlerror();
v5 -= 8LL;
fprintf(stderr, "dlopen : %s\n", v11);
if ( v5 == v6 )
return 0LL;
}
v7 = (__int64 (*)(void))dlsym(v9, "cap_mod_getname");
if ( v7 )
{
v8 = (const char *)v7();
if ( !strcmp(v8, a4) )
return v10;
dlclose(v10);
}
else
{
v13 = dlerror();
fprintf(stderr, "dlsym cap_mod_getname : %s\n", v13);
dlclose(v10);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
return 0LL;
}

original_code:
static void *get_exact_lib(const char *path, struct dirent **namelist, int nr, const char *mod_name)
{
 void *lib = ((void *)0);
 spk_cap_mod_getname_func_t mod_getname = ((void *)0);
 char absolute_path[256] = {0};
 while (nr--) {
  strncpy(absolute_path, path, 256);
  strncat(absolute_path, namelist[nr]->d_name, 256);
  lib = dlopen(absolute_path, 0x00002 | 0x00100 );
  if (lib == ((void *)0)) {
   fprintf ( stderr, "dlopen : %s\n", dlerror() ) ;
   continue;
  }
  mod_getname = dlsym(lib, "cap_mod_getname");
  if ( mod_getname == ((void *)0) ) {
   fprintf ( stderr, "dlsym cap_mod_getname : %s\n", dlerror() ) ;
   dlclose(lib);
   continue;
  }
  if (!strcmp(mod_getname(), mod_name))
   return lib;
  else
   dlclose(lib);
 }
 return ((void *)0);
}

canonical_original_code:
static void *get_exact_lib(char *path, struct dirent **namelist, int nr, char *mod_name)
{
 void *lib = ((void *)0);
 char *(*mod_getname)() = ((void *)0);
 char absolute_path[256] = {0};
 while (nr--) {
  strncpy(absolute_path, path, 256);
  strncat(absolute_path, namelist[nr]->d_name, 256);
  lib = dlopen(absolute_path, 0x00002 | 0x00100 );
  if (lib == ((void *)0)) {
   fprintf ( stderr, "dlopen : %s\n", dlerror() ) ;
   continue;
  }
  mod_getname = dlsym(lib, "cap_mod_getname");
  if ( mod_getname == ((void *)0) ) {
   fprintf ( stderr, "dlsym cap_mod_getname : %s\n", dlerror() ) ;
   dlclose(lib);
   continue;
  }
  if (!strcmp(mod_getname(), mod_name))
   return lib;
  else
   dlclose(lib);
 }
 return ((void *)0);
}

variable_types:
path
char *

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **

variable_types: nr int

variable_types:
lib
void *

return_type: void *


decompiled_code:
void __fastcall free_namelist(void *ptr, int a2)
{
void **v3; // rbx
__int64 v4; // rbp
void *v5; // rdi

if ( a2 )
{
v3 = (void **)((char *)ptr + 8 * a2 - 8);
v4 = (__int64)&v3[-(a2 - 1) - 1];
do
{
v5 = *v3--;
free(v5);
}
while ( v3 != (void **)v4 );
}
free(ptr);
}

original_code:
static inline void free_namelist(struct dirent **namelist, int n)
{
 while (n--)
  free(namelist[n]);
 free(namelist);
}

variable_types: n int

user_defined_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; }


decompiled_code:
void *__fastcall spk_cap_adpt_load(char *src, const char *a2)
{
int v2; // eax
int v3; // er12
void *v4; // rax
void *v5; // rbp
void (__fastcall *v6)(void *); // rbx
void *v7; // rax
void *v8; // r13
void *result; // rax
char *v10; // rax
struct dirent **namelist; // [rsp+8h] [rbp-30h] BYREF

if ( !src )
return 0LL;
v2 = scandir(src, &namelist, (int (*)(const struct dirent *))so_filter, 0LL);
v3 = v2;
if ( v2 < 0 )
{
perror("scandir");
result = 0LL;
}
else
{
v4 = get_exact_lib(src, (__int64)namelist, v2, a2);
v5 = v4;
if ( v4 )
{
v6 = (void (__fastcall *)(void *))dlsym(v4, "cap_mod_register");
if ( v6 )
{
v7 = calloc(0x38uLL, 1uLL);
v8 = v7;
if ( v7 )
{
v6(v7);
free_namelist(namelist, v3);
return v8;
}
fwrite("malloc capture_adapter_ctx error\n", 1uLL, 0x21uLL, stderr);
}
else
{
v10 = dlerror();
fprintf(stderr, "dlsym capture_mod_register : %s\n", v10);
}
dlclose(v5);
}
free_namelist(namelist, v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(char *src, const char *a2)
{
int v2; // eax
int v3; // er12
void *v4; // rax
void *v5; // rbp
void (__fastcall *v6)(void *); // rbx
void *v7; // rax
void *v8; // r13
void *result; // rax
char *v10; // rax
struct dirent **namelist; // [rsp+8h] [rbp-30h] BYREF

if ( !src )
return 0LL;
v2 = scandir(src, &namelist, (int (*)(const struct dirent *))so_filter, 0LL);
v3 = v2;
if ( v2 < 0 )
{
perror("scandir");
result = 0LL;
}
else
{
v4 = func1(src, (__int64)namelist, v2, a2);
v5 = v4;
if ( v4 )
{
v6 = (void (__fastcall *)(void *))dlsym(v4, "cap_mod_register");
if ( v6 )
{
v7 = calloc(0x38uLL, 1uLL);
v8 = v7;
if ( v7 )
{
v6(v7);
func2(namelist, v3);
return v8;
}
fwrite("malloc capture_adapter_ctx error\n", 1uLL, 0x21uLL, stderr);
}
else
{
v10 = dlerror();
fprintf(stderr, "dlsym capture_mod_register : %s\n", v10);
}
dlclose(v5);
}
func2(namelist, v3);
result = 0LL;
}
return result;
}

original_code:
void *spk_cap_adpt_load (const char *mod_path, const char *mod_name)
{
 int err = 0 ;
 void *lib = ((void *)0) ;
 spk_cap_mod_reg_func_t mod_register = ((void *)0) ;
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0) ;
 struct dirent **namelist;
 int nr, n;
 if (mod_path == ((void *)0))
  return ((void *)0);
 n = nr = scandir(mod_path, &namelist, so_filter, ((void *)0));
 if (nr < 0) {
  perror("scandir");
  return ((void *)0);
 } else {
  lib = get_exact_lib(mod_path, namelist, nr, mod_name);
  if (lib == ((void *)0)) {
   err = 1;
   goto err;
  }
 }
 mod_register = dlsym( lib, "cap_mod_register" ) ;
 if ( mod_register == ((void *)0) ) {
  err = 2 ;
  fprintf ( stderr, "dlsym capture_mod_register : %s\n", dlerror() ) ;
  goto err ;
 }
 adpt_ctx = ( struct spk_cap_adpt_ctx * ) malloc ( sizeof ( struct spk_cap_adpt_ctx ) ) ;
 if ( adpt_ctx == ((void *)0) ) {
  err = 3 ;
  fprintf ( stderr, "malloc capture_adapter_ctx error\n" ) ;
  goto err ;
 }
 memset ( adpt_ctx, 0, sizeof ( struct spk_cap_adpt_ctx ) ) ;
 mod_register ( (void *)adpt_ctx ) ;
 free_namelist(namelist, n);
 return (void *)adpt_ctx ;
err :
 switch (err) {
  case 3 :
  case 2 :
   if ( lib ) dlclose ( lib ) ;
  case 1 :
   free_namelist(namelist, n);
   break ;
 }
 return ((void *)0) ;
}

canonical_original_code:
void *spk_cap_adpt_load(char *mod_path, char *mod_name)
{
 int err = 0;
 void *lib = ((void *)0);
 void (*mod_register)(void *ctx) = ((void *)0);
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0);
 struct dirent **namelist;
 int nr; int n;
 if (mod_path == ((void *)0))
  return ((void *)0);
 n = nr = scandir(mod_path, &namelist, so_filter, ((void *)0));
 if (nr < 0) {
  perror("scandir");
  return ((void *)0);
 } else {
  lib = get_exact_lib(mod_path, namelist, nr, mod_name);
  if (lib == ((void *)0)) {
   err = 1;
   goto err;
  }
 }
 mod_register = dlsym( lib, "cap_mod_register" ) ;
 if ( mod_register == ((void *)0) ) {
  err = 2 ;
  fprintf ( stderr, "dlsym capture_mod_register : %s\n", dlerror() ) ;
  goto err ;
 }
 adpt_ctx = ( struct spk_cap_adpt_ctx * ) malloc ( sizeof ( struct spk_cap_adpt_ctx ) ) ;
 if ( adpt_ctx == ((void *)0) ) {
  err = 3 ;
  fprintf ( stderr, "malloc capture_adapter_ctx error\n" ) ;
  goto err ;
 }
 memset ( adpt_ctx, 0, sizeof ( struct spk_cap_adpt_ctx ) ) ;
 mod_register ( (void *)adpt_ctx ) ;
 free_namelist(namelist, n);
 return (void *)adpt_ctx ;
err :
 switch (err) {
  case 3 :
  case 2 :
   if ( lib ) dlclose ( lib ) ;
  case 1 :
   free_namelist(namelist, n);
   break ;
 }
 return ((void *)0) ;
}

variable_types: err int

variable_types: struct spk_cap_adpt_ctx { char name[-1]; void *dev_ctx; struct spk_cap_adpt_func { void *(*open)(void *ctx, char *dev); void (*close)(void *ctx); int (*get_pkts)(void *ctx, struct spark_packet { unsigned int len; unsigned int caplen; struct timeval { long tv_sec; long tv_usec; } ts; unsigned char *data; } *pkt); } func; } *

user_defined_types: struct spk_cap_adpt_ctx { char name[-1]; void *dev_ctx; struct spk_cap_adpt_func func; }

user_defined_types: struct spk_cap_adpt_func { void *(*open)(void *ctx, char *dev); void (*close)(void *ctx); int (*get_pkts)(void *ctx, struct spark_packet *pkt); }

user_defined_types: struct spark_packet { unsigned int len; unsigned int caplen; struct timeval ts; unsigned char *data; }


decompiled_code:
__int64 __fastcall spk_cap_adpt_get_dev(__int64 a1)
{
return *(_QWORD *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(a1 + 24);
}

original_code:
void *spk_cap_adpt_get_dev ( void *ctx )
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0) ;
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 return adpt_ctx->dev_ctx ;
}

canonical_original_code:
void *spk_cap_adpt_get_dev(void *ctx)
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0);
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 return adpt_ctx->dev_ctx ;
}

variable_types:
ctx
void *

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


original_code:
int spk_cap_adpt_open ( void *ctx, char *dev )
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0) ;
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 adpt_ctx->dev_ctx = ( void * )adpt_ctx->func.open ( ctx, dev ) ;
 if ( adpt_ctx->dev_ctx == ((void *)0) )
  return -1 ;
 return 0 ;
}

canonical_original_code:
int spk_cap_adpt_open(void *ctx, char *dev)
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0);
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 adpt_ctx->dev_ctx = ( void * )adpt_ctx->func.open ( ctx, dev ) ;
 if ( adpt_ctx->dev_ctx == ((void *)0) )
  return -1 ;
 return 0 ;
}

variable_types:
dev
char *


decompiled_code:
void __fastcall spk_cap_adpt_close(void *ptr)
{
(*((void (**)(void))ptr + 5))();
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
(*((void (**)(void))ptr + 5))();
free(ptr);
}

original_code:
void spk_cap_adpt_close ( void *ctx )
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0) ;
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 adpt_ctx->func.close ( ctx ) ;
 if ( ctx )
  free ( ctx ) ;
}

canonical_original_code:
void spk_cap_adpt_close(void *ctx)
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0);
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 adpt_ctx->func.close ( ctx ) ;
 if ( ctx )
  free ( ctx ) ;
}


decompiled_code:
__int64 __fastcall spk_cap_adpt_get_pkts(__int64 a1)
{
return (*(__int64 (**)(void))(a1 + 48))();
}

original_code:
int spk_cap_adpt_get_pkts ( void *ctx, struct spark_packet *pkts )
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0) ;
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 return adpt_ctx->func.get_pkts ( ctx, pkts ) ;
}

canonical_original_code:
int spk_cap_adpt_get_pkts(void *ctx, struct spark_packet *pkts)
{
 struct spk_cap_adpt_ctx *adpt_ctx = ((void *)0);
 adpt_ctx = ( struct spk_cap_adpt_ctx * )ctx ;
 return adpt_ctx->func.get_pkts ( ctx, pkts ) ;
}

variable_types: struct spark_packet { unsigned int len; unsigned int caplen; struct timeval { long tv_sec; long tv_usec; } ts; unsigned char *data; } *


usage

decompiled_code:
int usage()
{
return puts("spark_demo -i <interface>");
}

canonical_decompiled_code:
int func0()
{
return puts("spark_demo -i <interface>");
}

original_code:
void usage ( void )
{
 printf ( "spark_demo -i <interface>\n" ) ;
}

canonical_original_code:
void usage()
{
 printf ( "spark_demo -i <interface>\n" ) ;
}


decompiled_code:
__int64 __fastcall get_options(int argc, char **argv, char **a3)
{
unsigned int v3; // er13
int v5; // eax
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = -1;
v5 = getopt_long(argc, argv, "i:h?", &opt_long, longind);
if ( v5 != -1 )
{
while ( v5 != 104 )
{
if ( v5 == 105 )
{
v3 = 1;
*a3 = optarg;
}
else if ( v5 == 63 )
{
break;
}
LABEL_4:
v5 = getopt_long(argc, argv, "i:h?", &opt_long, longind);
if ( v5 == -1 )
return v3;
}
usage();
v3 = 0;
goto LABEL_4;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv, char **a3)
{
unsigned int v3; // er13
int v5; // eax
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = -1;
v5 = getopt_long(argc, argv, "i:h?", &opt_long, longind);
if ( v5 != -1 )
{
while ( v5 != 104 )
{
if ( v5 == 105 )
{
v3 = 1;
*a3 = optarg;
}
else if ( v5 == 63 )
{
break;
}
LABEL_4:
v5 = getopt_long(argc, argv, "i:h?", &opt_long, longind);
if ( v5 == -1 )
return v3;
}
func0();
v3 = 0;
goto LABEL_4;
}
return v3;
}

original_code:
int get_options ( int argc, char *argv[], struct options *options )
{
 int ret = -1 ;
 int opt = 0 ;
 int opt_index ;
 opt = getopt_long( argc, argv, opt_str, opt_long, &opt_index );
 while( opt != -1 ) {
  switch( opt ) {
   case 'i':
    options->device = optarg ;
    ret = 1 ;
    break;
   case 'h':
   case '?':
    usage();
    ret = 0 ;
    break;
   default:
    break;
  }
  opt = getopt_long( argc, argv, opt_str, opt_long, &opt_index );
 }
 return ret ;
}

canonical_original_code:
int get_options(int argc, char *argv[], struct options *options)
{
 int ret = -1;
 int opt = 0;
 int opt_index;
 opt = getopt_long( argc, argv, opt_str, opt_long, &opt_index );
 while( opt != -1 ) {
  switch( opt ) {
   case 'i':
    options->device = optarg ;
    ret = 1 ;
    break;
   case 'h':
   case '?':
    usage();
    ret = 0 ;
    break;
   default:
    break;
  }
  opt = getopt_long( argc, argv, opt_str, opt_long, &opt_index );
 }
 return ret ;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
options
struct options { char *device; } *

variable_types: ret int

variable_types: opt int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbx
int v6; // eax
int v8[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v9; // [rsp+8h] [rbp-20h]
__int64 v10; // [rsp+10h] [rbp-18h]

v3 = get_options(argc, (char **)argv, (char **)&g_options);
if ( v3 )
{
if ( v3 < 0 )
{
usage();
}
else
{
v4 = spk_cap_adpt_load("../mod/", "PCAP");
v5 = v4;
if ( v4 )
{
spk_cap_adpt_open(v4, g_options);
while ( 1 )
{
LABEL_5:
while ( 1 )
{
v6 = spk_cap_adpt_get_pkts(v5, v8);
if ( v6 != 1 )
break;
LABEL_9:
printf(
"[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
(unsigned int)v8[0],
(unsigned int)v8[1],
v9,
v10);
}
while ( v6 )
{
if ( (unsigned int)(v6 + 2) > 1 )
goto LABEL_5;
fwrite("error\n", 1uLL, 6uLL, stderr);
v6 = spk_cap_adpt_get_pkts(v5, v8);
if ( v6 == 1 )
goto LABEL_9;
}
puts("timeout");
}
}
fwrite("get adapter context failed\n", 1uLL, 0x1BuLL, stderr);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
__int64 v5; // rbx
int v6; // eax
int v8[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v9; // [rsp+8h] [rbp-20h]
__int64 v10; // [rsp+10h] [rbp-18h]

v3 = func1(argc, (char **)argv, (char **)&g_options);
if ( v3 )
{
if ( v3 < 0 )
{
func0();
}
else
{
v4 = spk_cap_adpt_load("../mod/", "PCAP");
v5 = v4;
if ( v4 )
{
spk_cap_adpt_open(v4, g_options);
while ( 1 )
{
LABEL_5:
while ( 1 )
{
v6 = spk_cap_adpt_get_pkts(v5, v8);
if ( v6 != 1 )
break;
LABEL_9:
printf(
"[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
(unsigned int)v8[0],
(unsigned int)v8[1],
v9,
v10);
}
while ( v6 )
{
if ( (unsigned int)(v6 + 2) > 1 )
goto LABEL_5;
fwrite("error\n", 1uLL, 6uLL, stderr);
v6 = spk_cap_adpt_get_pkts(v5, v8);
if ( v6 == 1 )
goto LABEL_9;
}
puts("timeout");
}
}
fwrite("get adapter context failed\n", 1uLL, 0x1BuLL, stderr);
}
}
return 0;
}

original_code:
int main ( int argc, char *argv[] )
{
 int ret = 0 ;
 void *cap_adpt_ctx = ((void *)0) ;
 struct spark_packet pkt ;
 ret = get_options ( argc, argv, &g_options ) ;
 if ( ret == 0 )
  goto main_err ;
 if ( ret < 0 ) {
  usage () ;
  goto main_err ;
 }
 cap_adpt_ctx = spk_cap_adpt_load ("../mod/", "PCAP") ;
 if (cap_adpt_ctx == ((void *)0)) {
  fprintf(stderr, "get adapter context failed\n");
  goto main_err;
 }
 spk_cap_adpt_open ( cap_adpt_ctx, g_options.device ) ;
 while (1) {
  ret = spk_cap_adpt_get_pkts ( cap_adpt_ctx, &pkt ) ;
  if ( ret == 1 ) {
   printf ( "[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
     pkt.len,
     pkt.caplen,
     pkt.ts.tv_sec,
     pkt.ts.tv_usec) ;
  } else if ( ret == 0 ) {
   printf ( "timeout\n" ) ;
  } else if ( ret == -1 ) {
   fprintf ( stderr, "error\n" ) ;
  } else if ( ret == -2 ) {
   fprintf ( stderr, "error\n" ) ;
  }
 }
 spk_cap_adpt_close ( cap_adpt_ctx ) ;
main_err :
 return 0 ;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int ret = 0;
 void *cap_adpt_ctx = ((void *)0);
 struct spark_packet pkt;
 ret = get_options ( argc, argv, &g_options ) ;
 if ( ret == 0 )
  goto main_err ;
 if ( ret < 0 ) {
  usage () ;
  goto main_err ;
 }
 cap_adpt_ctx = spk_cap_adpt_load ("../mod/", "PCAP") ;
 if (cap_adpt_ctx == ((void *)0)) {
  fprintf(stderr, "get adapter context failed\n");
  goto main_err;
 }
 spk_cap_adpt_open ( cap_adpt_ctx, g_options.device ) ;
 while (1) {
  ret = spk_cap_adpt_get_pkts ( cap_adpt_ctx, &pkt ) ;
  if ( ret == 1 ) {
   printf ( "[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
     pkt.len,
     pkt.caplen,
     pkt.ts.tv_sec,
     pkt.ts.tv_usec) ;
  } else if ( ret == 0 ) {
   printf ( "timeout\n" ) ;
  } else if ( ret == -1 ) {
   fprintf ( stderr, "error\n" ) ;
  } else if ( ret == -2 ) {
   fprintf ( stderr, "error\n" ) ;
  }
 }
 spk_cap_adpt_close ( cap_adpt_ctx ) ;
main_err :
 return 0 ;
}

variable_types: struct spark_packet { unsigned int len; unsigned int caplen; struct timeval { long tv_sec; long tv_usec; } ts; unsigned char *data; }


original_code:
static inline void list_del(struct list_head *entry)
{
 __list_del(entry->prev, entry->next);
 entry->next = ((void *) 0x00100100 + 0);
 entry->prev = ((void *) 0x00200200 + 0);
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
void init_timervecs()
{
__int64 v0; // rax
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 *v4; // rax
__int64 *v5; // rdx

v0 = 8LL;
do
{
INIT_LIST_HEAD((char *)&tv5 + v0);
INIT_LIST_HEAD((char *)&tv4 + v1);
INIT_LIST_HEAD((char *)&tv3 + v2);
INIT_LIST_HEAD((char *)&tv2 + v3);
}
while ( v0 != 1032 );
v4 = &qword_5C8;
do
INIT_LIST_HEAD(v4);
while ( v5 != v4 );
}

original_code:
static inline void init_timervecs(void)
{
 int i;
 for (i = 0; i < (1 << 6); i++) {
  INIT_LIST_HEAD(tv5.vec + i);
  INIT_LIST_HEAD(tv4.vec + i);
  INIT_LIST_HEAD(tv3.vec + i);
  INIT_LIST_HEAD(tv2.vec + i);
 }
 for (i = 0; i < (1 << 8); i++)
  INIT_LIST_HEAD(tv1.vec + i);
}

canonical_original_code:
static inline void init_timervecs()
{
 int i;
 for (i = 0; i < (1 << 6); i++) {
  INIT_LIST_HEAD(tv5.vec + i);
  INIT_LIST_HEAD(tv4.vec + i);
  INIT_LIST_HEAD(tv3.vec + i);
  INIT_LIST_HEAD(tv2.vec + i);
 }
 for (i = 0; i < (1 << 8); i++)
  INIT_LIST_HEAD(tv1.vec + i);
}

variable_types: i int


decompiled_code:
_BOOL8 __fastcall detach_timer(__int64 *a1)
{
_BOOL8 result; // rax
__int64 v2; // r8

result = timer_pending_isra_0(*a1);
if ( result )
{
list_del(v2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(__int64 *a1)
{
_BOOL8 result; // rax
__int64 v2; // r8

result = func9(*a1);
if ( result )
{
func4(v2);
result = 1LL;
}
return result;
}

original_code:
static inline int detach_timer(struct timer_list *timer)
{
 if (!timer_pending(timer))
  return 0;
 list_del(&timer->list);
 return 1;
}

variable_types: struct timer_list { struct list_head { struct list_head *next; } list; unsigned long expires; unsigned long data; void (*function)(unsigned long); } *

user_defined_types: struct timer_list { struct list_head list; unsigned long expires; unsigned long data; void (*function)(unsigned long); }


decompiled_code:
int __fastcall add_timer(__int64 *a1)
{
int result; // eax
const void *retaddr; // [rsp+8h] [rbp+0h]

a1[2] = timer_jiffies + 2 * a1[2];
init_timer(a1);
pthread_mutex_lock(&timerlist_lock);
if ( timer_pending_isra_0(*a1) )
{
pthread_mutex_unlock(&timerlist_lock);
result = printf("bug: kernel timer added twice at %p.\n", retaddr);
}
else
{
internal_add_timer(a1);
result = pthread_mutex_unlock(&timerlist_lock);
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(__int64 *a1)
{
int result; // eax
const void *retaddr; // [rsp+8h] [rbp+0h]

a1[2] = timer_jiffies + 2 * a1[2];
func5(a1);
pthread_mutex_lock(&timerlist_lock);
if ( func9(*a1) )
{
pthread_mutex_unlock(&timerlist_lock);
result = printf("bug: kernel timer added twice at %p.\n", retaddr);
}
else
{
func7(a1);
result = pthread_mutex_unlock(&timerlist_lock);
}
return result;
}

original_code:
void add_timer(struct timer_list *timer)
{
 unsigned long expires = timer->expires;
 timer->expires = expires * (1000000 / 500000) + timer_jiffies;
 init_timer(timer);
 pthread_mutex_lock(&timerlist_lock);
 if (timer_pending(timer))
  goto bug;
 internal_add_timer(timer);
 pthread_mutex_unlock(&timerlist_lock);
 return;
bug:
 pthread_mutex_unlock(&timerlist_lock);
 printf("bug: kernel timer added twice at %p.\n", __builtin_return_address(0));
}

variable_types:
expires
unsigned long

user_defined_types: struct list_head { struct list_head *next; }


decompiled_code:
_BOOL8 __fastcall mod_timer(__int64 *a1, __int64 a2)
{
_BOOL4 v2; // er12

pthread_mutex_lock(&timerlist_lock);
a1[2] = a2;
v2 = detach_timer(a1);
internal_add_timer(a1);
pthread_mutex_unlock(&timerlist_lock);
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(__int64 *a1, __int64 a2)
{
_BOOL4 v2; // er12

pthread_mutex_lock(&timerlist_lock);
a1[2] = a2;
v2 = func10(a1);
func7(a1);
pthread_mutex_unlock(&timerlist_lock);
return v2;
}

original_code:
int mod_timer(struct timer_list *timer, unsigned long expires)
{
 int ret;
 pthread_mutex_lock(&timerlist_lock);
 timer->expires = expires;
 ret = detach_timer(timer);
 internal_add_timer(timer);
 pthread_mutex_unlock(&timerlist_lock);
 return ret;
}


decompiled_code:
_BOOL8 __fastcall del_timer(__int64 *a1)
{
_BOOL4 v1; // eax
_BOOL4 v2; // er12

pthread_mutex_lock(&timerlist_lock);
v1 = detach_timer(a1);
a1[1] = 0LL;
*a1 = 0LL;
v2 = v1;
pthread_mutex_unlock(&timerlist_lock);
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(__int64 *a1)
{
_BOOL4 v1; // eax
_BOOL4 v2; // er12

pthread_mutex_lock(&timerlist_lock);
v1 = func10(a1);
a1[1] = 0LL;
*a1 = 0LL;
v2 = v1;
pthread_mutex_unlock(&timerlist_lock);
return v2;
}

original_code:
int del_timer(struct timer_list * timer)
{
 int ret;
 pthread_mutex_lock(&timerlist_lock);
 ret = detach_timer(timer);
 timer->list.next = timer->list.prev = ((void *)0);
 pthread_mutex_unlock(&timerlist_lock);
 return ret;
}

canonical_original_code:
int del_timer(struct timer_list *timer)
{
 int ret;
 pthread_mutex_lock(&timerlist_lock);
 ret = detach_timer(timer);
 timer->list.next = timer->list.prev = ((void *)0);
 pthread_mutex_unlock(&timerlist_lock);
 return ret;
}


decompiled_code:
__int64 begin_timer()
{
unsigned int v0; // er12
int v1; // eax
struct sched_param param; // [rsp+4h] [rbp-54h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-48h] BYREF

init_timervecs();
if ( pthread_attr_init(&attr) )
{
v0 = -1;
puts("pthread attr init failed");
}
else if ( pthread_attr_setscope(&attr, 0) )
{
v0 = -1;
puts("pthread attr set scope PTHREAD_SCOPE_SYSTEM failed");
}
else
{
v0 = pthread_attr_setschedpolicy(&attr, 2);
if ( v0 )
{
v0 = -1;
puts("pthread attr set schedule policy failed");
}
else
{
v1 = sched_get_priority_max(2);
if ( v1 == -1 )
{
v0 = -1;
puts("get RR priority max failed");
}
else
{
param.__sched_priority = v1;
pthread_attr_setschedparam(&attr, &param);
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))run_timer_list, 0LL) < 0 )
{
v0 = -1;
puts("thread create failed");
}
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func15()
{
unsigned int v0; // er12
int v1; // eax
struct sched_param param; // [rsp+4h] [rbp-54h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-50h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-48h] BYREF

func6();
if ( pthread_attr_init(&attr) )
{
v0 = -1;
puts("pthread attr init failed");
}
else if ( pthread_attr_setscope(&attr, 0) )
{
v0 = -1;
puts("pthread attr set scope PTHREAD_SCOPE_SYSTEM failed");
}
else
{
v0 = pthread_attr_setschedpolicy(&attr, 2);
if ( v0 )
{
v0 = -1;
puts("pthread attr set schedule policy failed");
}
else
{
v1 = sched_get_priority_max(2);
if ( v1 == -1 )
{
v0 = -1;
puts("get RR priority max failed");
}
else
{
param.__sched_priority = v1;
pthread_attr_setschedparam(&attr, &param);
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))run_timer_list, 0LL) < 0 )
{
v0 = -1;
puts("thread create failed");
}
}
}
}
return v0;
}

original_code:
int begin_timer(void)
{
 pthread_t pid;
 pthread_attr_t attr;
 int rr_max_priority;
 struct sched_param thread_param;
 int ret;
 init_timervecs();
 ret = pthread_attr_init(&attr);
 if (ret != 0) {
  printf("pthread attr init failed\n");
  return -1;
 }
 ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
 if (ret != 0) {
  printf("pthread attr set scope PTHREAD_SCOPE_SYSTEM failed\n");
  return -1;
 }
 ret = pthread_attr_setschedpolicy(&attr, 2);
 if (ret != 0) {
  printf("pthread attr set schedule policy failed\n");
  return -1;
 }
 rr_max_priority = sched_get_priority_max(2);
 if (rr_max_priority == -1) {
  printf("get RR priority max failed\n");
  return -1;
 }
 memset(&thread_param, 0, sizeof(struct sched_param));
 thread_param.sched_priority = rr_max_priority;
 pthread_attr_setschedparam(&attr, &thread_param);
 ret = pthread_create(&pid, ((void *)0), run_timer_list, ((void *)0));
 if (ret < 0) {
  printf("thread create failed\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int begin_timer()
{
 unsigned long pid;
 union pthread_attr_t attr;
 int rr_max_priority;
 struct sched_param thread_param;
 int ret;
 init_timervecs();
 ret = pthread_attr_init(&attr);
 if (ret != 0) {
  printf("pthread attr init failed\n");
  return -1;
 }
 ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
 if (ret != 0) {
  printf("pthread attr set scope PTHREAD_SCOPE_SYSTEM failed\n");
  return -1;
 }
 ret = pthread_attr_setschedpolicy(&attr, 2);
 if (ret != 0) {
  printf("pthread attr set schedule policy failed\n");
  return -1;
 }
 rr_max_priority = sched_get_priority_max(2);
 if (rr_max_priority == -1) {
  printf("get RR priority max failed\n");
  return -1;
 }
 memset(&thread_param, 0, sizeof(struct sched_param));
 thread_param.sched_priority = rr_max_priority;
 pthread_attr_setschedparam(&attr, &thread_param);
 ret = pthread_create(&pid, ((void *)0), run_timer_list, ((void *)0));
 if (ret < 0) {
  printf("thread create failed\n");
  return -1;
 }
 return 0;
}

variable_types:
pid
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
__int64 cap_mod_register()
{
return spk_cap_adpt_reg();
}

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

original_code:
void cap_mod_register ( void *ctx )
{
 struct spk_cap_adpt_func func_list ;
 func_list.open = spk_pcap_open ;
 func_list.close = spk_pcap_close ;
 func_list.get_pkts = spk_pcap_get_pkts ;
 spk_cap_adpt_reg ( ctx, &func_list ) ;
}

canonical_original_code:
void cap_mod_register(void *ctx)
{
 struct spk_cap_adpt_func func_list;
 func_list.open = spk_pcap_open ;
 func_list.close = spk_pcap_close ;
 func_list.get_pkts = spk_pcap_get_pkts ;
 spk_cap_adpt_reg ( ctx, &func_list ) ;
}

variable_types: struct spk_cap_adpt_func { void *(*open)(void *ctx, char *dev); void (*close)(void *ctx); int (*get_pkts)(void *ctx, struct spark_packet { unsigned int len; unsigned int caplen; struct timeval { long tv_sec; long tv_usec; } ts; unsigned char *data; } *pkt); }


decompiled_code:
const char *cap_mod_getname()
{
return "PCAP";
}

canonical_decompiled_code:
const char *func10()
{
return "PCAP";
}

original_code:
char *cap_mod_getname(void)
{
 return "PCAP";
}

return_type: char *


decompiled_code:
int __fastcall get_data_file_format_version(char *src)
{
char *v1; // rax
char *v2; // rax
int result; // eax
char v4[136]; // [rsp+0h] [rbp-88h] BYREF

strncpy(v4, src, 0x80uLL);
v1 = strchr(v4, 59);
if ( v1 && ((*v1 = 0, (v2 = strchr(v4, 86)) != 0LL) || (v2 = strchr(v4, 118)) != 0LL) )
result = atoi(v2 + 1);
else
result = 1;
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *src)
{
char *v1; // rax
char *v2; // rax
int result; // eax
char v4[136]; // [rsp+0h] [rbp-88h] BYREF

strncpy(v4, src, 0x80uLL);
v1 = strchr(v4, 59);
if ( v1 && ((*v1 = 0, (v2 = strchr(v4, 86)) != 0LL) || (v2 = strchr(v4, 118)) != 0LL) )
result = atoi(v2 + 1);
else
result = 1;
return result;
}

original_code:
static int get_data_file_format_version(char *header)
{
 char line[128] = {0 , };
 char *p;
 strncpy(line, header, sizeof(line));
 p = strchr(line, ';');
 if ( p == ((void *)0) ) {
  return 1;
 }
 *p = 0;
 p = strchr(line, 'V');
 if ( p == ((void *)0) ) {
  p = strchr(line, 'v');
  if ( p == ((void *)0) ) {
   return 1;
  }
 }
 p++;
 return atoi(p);
}

variable_types:
header
char *

variable_types:
line
char [128]

variable_types:
p
char *


decompiled_code:
__int64 __fastcall parse_drill_retouch_file(const void *a1, int a2, __int64 a3)
{
char *v4; // rax
unsigned int v5; // eax
int v6; // ebp
_BOOL8 v7; // r12
unsigned int v8; // ebp
unsigned int v10; // [rsp+0h] [rbp-12Ch] BYREF
char s[16]; // [rsp+4h] [rbp-128h] BYREF
char haystack[16]; // [rsp+14h] [rbp-118h] BYREF
__int128 v13; // [rsp+24h] [rbp-108h]
__int128 v14; // [rsp+34h] [rbp-F8h]
__int128 v15; // [rsp+44h] [rbp-E8h]
__int128 v16; // [rsp+54h] [rbp-D8h]
__int128 v17; // [rsp+64h] [rbp-C8h]
__int128 v18; // [rsp+74h] [rbp-B8h]
__int128 v19; // [rsp+84h] [rbp-A8h]
char src[16]; // [rsp+94h] [rbp-98h] BYREF
__int128 v21; // [rsp+A4h] [rbp-88h]
__int128 v22; // [rsp+B4h] [rbp-78h]
__int128 v23; // [rsp+C4h] [rbp-68h]
__int128 v24; // [rsp+D4h] [rbp-58h]
__int128 v25; // [rsp+E4h] [rbp-48h]
__int128 v26; // [rsp+F4h] [rbp-38h]
__int128 v27; // [rsp+104h] [rbp-28h]

v10 = 0;
*(_OWORD *)haystack = 0LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
*(_OWORD *)src = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
*(_OWORD *)s = 0LL;
v4 = (char *)memchr(a1, 35, a2);
if ( !v4 )
return 4294867279LL;
mem_get_line(v4 + 1, haystack, 128LL);
printf("line=[%s]\n", haystack);
if ( !strstr(haystack, "drill_retouch") )
return 4294867279LL;
v5 = get_data_file_format_version(haystack);
printf("ver = %d\n", v5);
set_parse_param_sep_str(";");
v6 = parse_param(haystack, "siss", 0LL, &v10, src, s);
set_parse_param_sep_str(" ");
if ( v6 <= 3 )
return 4294867279LL;
LODWORD(v7) = 0;
ltrim(src);
rtrim(src);
if ( !strchr(s, 82) && !strchr(s, 114) )
v7 = strchr(s, 48) == 0LL;
v8 = v10;
*(_DWORD *)(a3 + 132) = v7;
*(_DWORD *)a3 = v8;
strncpy((char *)(a3 + 4), src, 0x80uLL);
printf("id=%d, jon_no[%s], side=%d\n", v8, src, v7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1, int a2, __int64 a3)
{
char *v4; // rax
unsigned int v5; // eax
int v6; // ebp
_BOOL8 v7; // r12
unsigned int v8; // ebp
unsigned int v10; // [rsp+0h] [rbp-12Ch] BYREF
char s[16]; // [rsp+4h] [rbp-128h] BYREF
char haystack[16]; // [rsp+14h] [rbp-118h] BYREF
__int128 v13; // [rsp+24h] [rbp-108h]
__int128 v14; // [rsp+34h] [rbp-F8h]
__int128 v15; // [rsp+44h] [rbp-E8h]
__int128 v16; // [rsp+54h] [rbp-D8h]
__int128 v17; // [rsp+64h] [rbp-C8h]
__int128 v18; // [rsp+74h] [rbp-B8h]
__int128 v19; // [rsp+84h] [rbp-A8h]
char src[16]; // [rsp+94h] [rbp-98h] BYREF
__int128 v21; // [rsp+A4h] [rbp-88h]
__int128 v22; // [rsp+B4h] [rbp-78h]
__int128 v23; // [rsp+C4h] [rbp-68h]
__int128 v24; // [rsp+D4h] [rbp-58h]
__int128 v25; // [rsp+E4h] [rbp-48h]
__int128 v26; // [rsp+F4h] [rbp-38h]
__int128 v27; // [rsp+104h] [rbp-28h]

v10 = 0;
*(_OWORD *)haystack = 0LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
*(_OWORD *)src = 0LL;
v21 = 0LL;
v22 = 0LL;
v23 = 0LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
*(_OWORD *)s = 0LL;
v4 = (char *)memchr(a1, 35, a2);
if ( !v4 )
return 4294867279LL;
mem_get_line(v4 + 1, haystack, 128LL);
printf("line=[%s]\n", haystack);
if ( !strstr(haystack, "drill_retouch") )
return 4294867279LL;
v5 = func0(haystack);
printf("ver = %d\n", v5);
set_parse_param_sep_str(";");
v6 = parse_param(haystack, "siss", 0LL, &v10, src, s);
set_parse_param_sep_str(" ");
if ( v6 <= 3 )
return 4294867279LL;
LODWORD(v7) = 0;
ltrim(src);
rtrim(src);
if ( !strchr(s, 82) && !strchr(s, 114) )
v7 = strchr(s, 48) == 0LL;
v8 = v10;
*(_DWORD *)(a3 + 132) = v7;
*(_DWORD *)a3 = v8;
strncpy((char *)(a3 + 4), src, 0x80uLL);
printf("id=%d, jon_no[%s], side=%d\n", v8, src, v7);
return 0LL;
}

original_code:
static int parse_drill_retouch_file(char *buf, int buf_size, DRILL_RETOUCH_FILE *p_dr)
{
 char line[128] = {0 , };
 char *p;
 int ret;
 int id = 0;
 char job_no[128] = {0, };
 char side_str[16] = {0, };
 int side = 0;
 int ver = 1;
 p = (char*)memchr(buf, '#', buf_size);
 if ( p == ((void *)0) ) {
  return -ERR_FORMAT;
 }
 p++;
 mem_get_line(p, line, sizeof(line));
 printf("line=[%s]\n", line);
 if ( strstr(line, "drill_retouch") == ((void *)0) ) {
  return -ERR_FORMAT;
 }
 ver = get_data_file_format_version(line);
 printf("ver = %d\n", ver);
 set_parse_param_sep_str(";");
 ret = parse_param(line, "siss", ((void *)0), &id, job_no, side_str );
 set_parse_param_sep_str(" ");
 if ( ret < 4 ) {
  return -ERR_FORMAT;
 }
 ltrim(job_no);
 rtrim(job_no);
 if ( strstr(side_str, "R") || strstr(side_str, "r") || strstr(side_str, "0") ) {
  side = 0;
 } else {
  side = 1;
 }
 if ( p_dr ) {
  p_dr->id = id;
  p_dr->side = side;
  strncpy(p_dr->job_no, job_no, 128);
 }
 printf("id=%d, jon_no[%s], side=%d\n", id, job_no, side);
 return 0;
}

canonical_original_code:
static int parse_drill_retouch_file(char *buf, int buf_size, struct drill_retouch_file_rec *p_dr)
{
 char line[128] = {0 , };
 char *p;
 int ret;
 int id = 0;
 char job_no[128] = {0, };
 char side_str[16] = {0, };
 int side = 0;
 int ver = 1;
 p = (char *)memchr(buf, '#', buf_size);
 if ( p == ((void *)0) ) {
  return -ERR_FORMAT;
 }
 p++;
 mem_get_line(p, line, sizeof(line));
 printf("line=[%s]\n", line);
 if ( strstr(line, "drill_retouch") == ((void *)0) ) {
  return -ERR_FORMAT;
 }
 ver = get_data_file_format_version(line);
 printf("ver = %d\n", ver);
 set_parse_param_sep_str(";");
 ret = parse_param(line, "siss", ((void *)0), &id, job_no, side_str );
 set_parse_param_sep_str(" ");
 if ( ret < 4 ) {
  return -ERR_FORMAT;
 }
 ltrim(job_no);
 rtrim(job_no);
 if ( strstr(side_str, "R") || strstr(side_str, "r") || strstr(side_str, "0") ) {
  side = 0;
 } else {
  side = 1;
 }
 if ( p_dr ) {
  p_dr->id = id;
  p_dr->side = side;
  strncpy(p_dr->job_no, job_no, 128);
 }
 printf("id=%d, jon_no[%s], side=%d\n", id, job_no, side);
 return 0;
}

variable_types:
buf
char *

variable_types: struct drill_retouch_file_rec { int id; char job_no[128]; int side; } *

variable_types: id int

variable_types: side int

variable_types: ver int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // eax
__int128 v6[5]; // [rsp+0h] [rbp-118h] BYREF
__int64 v7; // [rsp+50h] [rbp-C8h]
__int64 v8; // [rsp+58h] [rbp-C0h]
__int128 v9; // [rsp+60h] [rbp-B8h]
__int128 v10; // [rsp+70h] [rbp-A8h]
char v11[152]; // [rsp+80h] [rbp-98h] BYREF

v7 = 171653947LL;
v6[0] = (__int128)_mm_load_si128((const __m128i *)&LC9);
v8 = 0LL;
v6[1] = (__int128)_mm_load_si128((const __m128i *)&LC10);
v6[2] = (__int128)_mm_load_si128((const __m128i *)&LC11);
v6[3] = (__int128)_mm_load_si128((const __m128i *)&LC12);
v6[4] = (__int128)_mm_load_si128((const __m128i *)&LC13);
v9 = 0LL;
v10 = 0LL;
ssl_set_printf_func(&printf, argv, envp);
puts("test");
v3 = strlen((const char *)v6);
v4 = parse_drill_retouch_file(v6, v3, (__int64)v11);
printf("ret = %d\n", v4);
return 0;
}

original_code:
int main(void)
{
 char buf[128] = {
  "# drill_retouch V1;001;12345;R\n"
  "DRILLE=B;CF;-26.20;-1.30;1.00;-24.20;-1.30;;;;F;;;;;\n"
 };
 DRILL_RETOUCH_FILE d;
 int ret;
 ssl_set_printf_func(printf);
 printf("test\n");
 ret = parse_drill_retouch_file(buf, strlen(buf), &d);
 printf("ret = %d\n", ret);
 return 0;
}

canonical_original_code:
int main()
{
 char buf[128] = {
  "# drill_retouch V1;001;12345;R\n"
  "DRILLE=B;CF;-26.20;-1.30;1.00;-24.20;-1.30;;;;F;;;;;\n"
 };
 struct drill_retouch_file_rec d;
 int ret;
 ssl_set_printf_func(printf);
 printf("test\n");
 ret = parse_drill_retouch_file(buf, strlen(buf), &d);
 printf("ret = %d\n", ret);
 return 0;
}

variable_types:
buf
char [128]

variable_types: struct drill_retouch_file_rec { int id; char job_no[128]; int side; }

variable_types: ret int


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
puts("bin file transfer V0.1 for POSIX OS");
printf("%s [-p /dev/ttyXXXX] [-s 115200] binfile\n", a1);
puts("\t-p : serial port device file path");
return puts("\t-s : baud rate (115200, 38400, 9600)");
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
puts("bin file transfer V0.1 for POSIX OS");
printf("%s [-p /dev/ttyXXXX] [-s 115200] binfile\n", a1);
puts("\t-p : serial port device file path");
return puts("\t-s : baud rate (115200, 38400, 9600)");
}

original_code:
static void usage(char *path)
{
 printf("bin file transfer V0.1 for POSIX OS\n");
 printf("%s [-p /dev/ttyXXXX] [-s 115200] binfile\n", path);
 printf("\t-p : serial port device file path\n");
 printf("\t-s : baud rate (115200, 38400, 9600)\n");
}

variable_types:
path
char *


decompiled_code:
__int64 __fastcall proc_cmd_options(int argc, char **argv)
{
int v2; // eax
unsigned int v3; // er12

if ( argc <= 1 )
{
v3 = -1;
usage(*argv);
}
else
{
while ( 1 )
{
v2 = getopt(argc, argv, "p:s:");
if ( v2 == -1 )
break;
if ( v2 == 112 )
{
printf("DEBUG port = %s\n", optarg);
strncpy(port_path, optarg, 0x400uLL);
}
else if ( v2 > 112 )
{
if ( v2 == 115 )
{
printf("DEBUG speed = %s\n", optarg);
strncpy(speed_str, optarg, 0x400uLL);
}
}
else
{
if ( v2 == 58 )
{
usage(*argv);
return 4294967294LL;
}
if ( v2 == 63 )
{
v3 = -3;
usage(*argv);
return v3;
}
}
}
v3 = 0;
if ( optind < argc )
{
strncpy(bin_file_path, argv[optind], 0x400uLL);
printf("DEBUG bin_file = %s\n", bin_file_path);
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
int v2; // eax
unsigned int v3; // er12

if ( argc <= 1 )
{
v3 = -1;
func0(*argv);
}
else
{
while ( 1 )
{
v2 = getopt(argc, argv, "p:s:");
if ( v2 == -1 )
break;
if ( v2 == 112 )
{
printf("DEBUG port = %s\n", optarg);
strncpy(port_path, optarg, 0x400uLL);
}
else if ( v2 > 112 )
{
if ( v2 == 115 )
{
printf("DEBUG speed = %s\n", optarg);
strncpy(speed_str, optarg, 0x400uLL);
}
}
else
{
if ( v2 == 58 )
{
func0(*argv);
return 4294967294LL;
}
if ( v2 == 63 )
{
v3 = -3;
func0(*argv);
return v3;
}
}
}
v3 = 0;
if ( optind < argc )
{
strncpy(bin_file_path, argv[optind], 0x400uLL);
printf("DEBUG bin_file = %s\n", bin_file_path);
}
}
return v3;
}

original_code:
int proc_cmd_options(int argc, char *argv[])
{
 int opt;
 if ( argc <= 1 ) {
  usage(argv[0]);
  return -1;
 } else {
  while((opt = getopt(argc, argv, "p:s:")) != -1 ) {
   switch(opt) {
    case 'p':
     printf("DEBUG port =  %s\n", optarg);
     strncpy(port_path, optarg, sizeof(port_path));
     break;
    case 's':
     printf("DEBUG speed =  %s\n", optarg);
     strncpy(speed_str, optarg, sizeof(speed_str));
     break;
    case ':':
     usage(argv[0]);
     return -2;
    case '?':
     usage(argv[0]);
     return -3;
   }
  }
  if ( optind < argc ) {
   strncpy(bin_file_path, argv[optind], sizeof(port_path));
   printf("DEBUG bin_file = %s\n", bin_file_path);
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int


original_code:
void calc_lens_thick_and_type(void)
{
 int i;
 float temp_cen_of_min_thickness = 0.0, temp_cen_of_max_thickness = 0.0;
 for (i = 0; i < 1440 ; i++) {
  feeler_lens_thick[i] = ((float) (feeler_data_front[i]) * (4*3.141592/2000) - (float) (feeler_data_rear[i]) * (4*3.141592/2000)) / 2.0;
  if (half_of_min_thickness > feeler_lens_thick[i] ) {
   half_of_min_thickness = feeler_lens_thick[i];
   temp_cen_of_min_thickness = (feeler_data_front[i] * (4*3.141592/2000) + feeler_data_rear[i] * (4*3.141592/2000)) / 2.0;
  }
  if (half_of_max_thickness < feeler_lens_thick[i]) {
   half_of_max_thickness = feeler_lens_thick[i];
   temp_cen_of_max_thickness = (feeler_data_front[i] * (4*3.141592/2000) + feeler_data_rear[i] * (4*3.141592/2000)) / 2.0;
  }
 }
 if (temp_cen_of_max_thickness > temp_cen_of_min_thickness) {
  lens_type_is_negative = 0;
 } else {
  lens_type_is_negative = 1;
 }
}

canonical_original_code:
void calc_lens_thick_and_type()
{
 int i;
 float temp_cen_of_min_thickness = 0.0; float temp_cen_of_max_thickness = 0.0;
 for (i = 0; i < 1440 ; i++) {
  feeler_lens_thick[i] = ((float) (feeler_data_front[i]) * (4*3.141592/2000) - (float) (feeler_data_rear[i]) * (4*3.141592/2000)) / 2.0;
  if (half_of_min_thickness > feeler_lens_thick[i] ) {
   half_of_min_thickness = feeler_lens_thick[i];
   temp_cen_of_min_thickness = (feeler_data_front[i] * (4*3.141592/2000) + feeler_data_rear[i] * (4*3.141592/2000)) / 2.0;
  }
  if (half_of_max_thickness < feeler_lens_thick[i]) {
   half_of_max_thickness = feeler_lens_thick[i];
   temp_cen_of_max_thickness = (feeler_data_front[i] * (4*3.141592/2000) + feeler_data_rear[i] * (4*3.141592/2000)) / 2.0;
  }
 }
 if (temp_cen_of_max_thickness > temp_cen_of_min_thickness) {
  lens_type_is_negative = 0;
 } else {
  lens_type_is_negative = 1;
 }
}

variable_types: i int


decompiled_code:
__int64 calc_front_mm()
{
float v0; // xmm1_4
__int64 result; // rax
int v2; // xmm0_4

v0 = *(float *)&pos_value;
for ( result = 0LL; result != 1440; ++result )
{
v2 = feeler_lens_thick[result];
if ( *(float *)&v2 > v0 )
*(float *)&v2 = (float)feeler_data_front[result] * 0.006283184000000001 - v0;
designed_pattern_X[result] = v2;
}
return result * 4;
}

canonical_decompiled_code:
__int64 func8()
{
float v0; // xmm1_4
__int64 result; // rax
int v2; // xmm0_4

v0 = *(float *)&pos_value;
for ( result = 0LL; result != 1440; ++result )
{
v2 = feeler_lens_thick[result];
if ( *(float *)&v2 > v0 )
*(float *)&v2 = (float)feeler_data_front[result] * 0.006283184000000001 - v0;
designed_pattern_X[result] = v2;
}
return result * 4;
}

original_code:
void calc_front_mm(void)
{
 int i;
 for ( i = 0 ; i < 1440 ; i++) {
  if ( pos_value < feeler_lens_thick[i]) {
   designed_pattern_X[i] = (float) (feeler_data_front[i]) * (4*3.141592/2000) - pos_value;
  } else {
   designed_pattern_X[i] = feeler_lens_thick[i];
  }
 }
}

canonical_original_code:
void calc_front_mm()
{
 int i;
 for ( i = 0 ; i < 1440 ; i++) {
  if ( pos_value < feeler_lens_thick[i]) {
   designed_pattern_X[i] = (float) (feeler_data_front[i]) * (4*3.141592/2000) - pos_value;
  } else {
   designed_pattern_X[i] = feeler_lens_thick[i];
  }
 }
}


decompiled_code:
__int64 calc_rear_mm()
{
float v0; // xmm1_4
__int64 result; // rax
int v2; // xmm0_4

v0 = *(float *)&pos_value;
for ( result = 0LL; result != 1440; ++result )
{
v2 = feeler_lens_thick[result];
if ( *(float *)&v2 > v0 )
*(float *)&v2 = (double)feeler_data_rear[result] * 0.006283184000000001 + v0;
designed_pattern_X[result] = v2;
}
return result * 4;
}

canonical_decompiled_code:
__int64 func9()
{
float v0; // xmm1_4
__int64 result; // rax
int v2; // xmm0_4

v0 = *(float *)&pos_value;
for ( result = 0LL; result != 1440; ++result )
{
v2 = feeler_lens_thick[result];
if ( *(float *)&v2 > v0 )
*(float *)&v2 = (double)feeler_data_rear[result] * 0.006283184000000001 + v0;
designed_pattern_X[result] = v2;
}
return result * 4;
}

original_code:
void calc_rear_mm(void)
{
 int i;
 for ( i = 0 ; i < 1440 ; i++) {
  if (pos_value < feeler_lens_thick[i] ) {
   designed_pattern_X[i] = (feeler_data_rear[i]) * (4*3.141592/2000) + pos_value ;
  } else {
   designed_pattern_X[i] = feeler_lens_thick[i];
  }
 }
}

canonical_original_code:
void calc_rear_mm()
{
 int i;
 for ( i = 0 ; i < 1440 ; i++) {
  if (pos_value < feeler_lens_thick[i] ) {
   designed_pattern_X[i] = (feeler_data_rear[i]) * (4*3.141592/2000) + pos_value ;
  } else {
   designed_pattern_X[i] = feeler_lens_thick[i];
  }
 }
}


decompiled_code:
__int64 calc_front_percent()
{
double v0; // xmm3_8
__int64 result; // rax
int v2; // xmm0_4

v0 = (float)(*(float *)&pos_value / 100.0);
for ( result = 0LL; result != 1440; dword_40843C[result] = v2 )
{
++result;
*(float *)&v2 = (double)dword_406D7C[result] * 0.006283184000000001 * (1.0 - v0)
+ (double)dword_4056FC[result] * 0.006283184000000001 * v0;
}
return result * 4;
}

original_code:
void calc_front_percent(void)
{
 int i;
 float tmp_percent = pos_value / 100.0;
 for ( i = 0 ; i < 1440 ; i++) {
  designed_pattern_X[i] = (feeler_data_front[i] * (4*3.141592/2000)) * (1.0 - tmp_percent ) + (feeler_data_rear[i] * (4*3.141592/2000)) * tmp_percent;
 }
}

canonical_original_code:
void calc_front_percent()
{
 int i;
 float tmp_percent = pos_value / 100.0;
 for ( i = 0 ; i < 1440 ; i++) {
  designed_pattern_X[i] = (feeler_data_front[i] * (4*3.141592/2000)) * (1.0 - tmp_percent ) + (feeler_data_rear[i] * (4*3.141592/2000)) * tmp_percent;
 }
}


original_code:
void calc_frame_curve(void)
{
 int i;
 float pattern_Z[1440];
 float temp_pattern_Z[1440];
 float max_z = 0.0;
 float alpha = 0.5;
 float alpha_step = 0.25;
 float std_margin_front = 0.6;
 float std_margin_rear = 0.6;
 float temp_margin_front = 0.0;
 float temp_margin_rear = 0.0;
 float min_margin_front = 100.0;
 float min_margin_rear = 100.0;
 for ( i = 0 ; i < 1440 ; i++) {
  pattern_Z[i] = (500.0 / pos_value ) - sqrt(((500.0 / pos_value ) * (500.0 / pos_value)) - (pattern[i] * pattern[i]));
  temp_pattern_Z[i] = -pattern_Z[i];
  if(max_z < temp_pattern_Z[i]) {
   max_z = temp_pattern_Z[i];
  }
 }
 alpha = 0.5;
 alpha_step = 0.25;
 for ( i = 0 ; i < 1440 ; i++) {
  temp_pattern_Z[i] = temp_pattern_Z[i] - max_z;
 }
 while(1) {
  min_margin_front = 100.0;
  min_margin_rear = 100.0;
  for ( i = 0 ; i < 1440 ; i++) {
   designed_pattern_X[i] = (temp_pattern_Z[i] * alpha) + (((feeler_data_front[i]*(4*3.141592/2000) + feeler_data_rear[i]*(4*3.141592/2000)) / 2.0) * (1 - alpha));
   temp_margin_front = feeler_data_front[i]*(4*3.141592/2000) - designed_pattern_X[i];
   temp_margin_rear = designed_pattern_X[i] - feeler_data_rear[i]*(4*3.141592/2000);
   if(min_margin_front > temp_margin_front) {
    min_margin_front = temp_margin_front;
   }
   if(min_margin_rear > temp_margin_rear) {
    min_margin_rear = temp_margin_rear;
   }
  }
  if((std_margin_front + std_margin_rear) > (min_margin_front + min_margin_rear)) {
   alpha = alpha - alpha_step;
  }
  else if((std_margin_front + std_margin_rear) < (min_margin_front + min_margin_rear)) {
   alpha = alpha + alpha_step;
  }
  else {
   break;
  }
  alpha_step = alpha_step / 2.0;
  if(alpha_step < 0.00001)
   break;
 }
 for ( i = 0 ; i < 1440 ; i++) {
  designed_pattern_X[i] = designed_pattern_X[i] + ((min_margin_front - min_margin_rear) / 2.0);
 }
}

canonical_original_code:
void calc_frame_curve()
{
 int i;
 float pattern_Z[1440];
 float temp_pattern_Z[1440];
 float max_z = 0.0;
 float alpha = 0.5;
 float alpha_step = 0.25;
 float std_margin_front = 0.6;
 float std_margin_rear = 0.6;
 float temp_margin_front = 0.0;
 float temp_margin_rear = 0.0;
 float min_margin_front = 100.0;
 float min_margin_rear = 100.0;
 for ( i = 0 ; i < 1440 ; i++) {
  pattern_Z[i] = (500.0 / pos_value ) - sqrt(((500.0 / pos_value ) * (500.0 / pos_value)) - (pattern[i] * pattern[i]));
  temp_pattern_Z[i] = -pattern_Z[i];
  if(max_z < temp_pattern_Z[i]) {
   max_z = temp_pattern_Z[i];
  }
 }
 alpha = 0.5;
 alpha_step = 0.25;
 for ( i = 0 ; i < 1440 ; i++) {
  temp_pattern_Z[i] = temp_pattern_Z[i] - max_z;
 }
 while(1) {
  min_margin_front = 100.0;
  min_margin_rear = 100.0;
  for ( i = 0 ; i < 1440 ; i++) {
   designed_pattern_X[i] = (temp_pattern_Z[i] * alpha) + (((feeler_data_front[i]*(4*3.141592/2000) + feeler_data_rear[i]*(4*3.141592/2000)) / 2.0) * (1 - alpha));
   temp_margin_front = feeler_data_front[i]*(4*3.141592/2000) - designed_pattern_X[i];
   temp_margin_rear = designed_pattern_X[i] - feeler_data_rear[i]*(4*3.141592/2000);
   if(min_margin_front > temp_margin_front) {
    min_margin_front = temp_margin_front;
   }
   if(min_margin_rear > temp_margin_rear) {
    min_margin_rear = temp_margin_rear;
   }
  }
  if((std_margin_front + std_margin_rear) > (min_margin_front + min_margin_rear)) {
   alpha = alpha - alpha_step;
  }
  else if((std_margin_front + std_margin_rear) < (min_margin_front + min_margin_rear)) {
   alpha = alpha + alpha_step;
  }
  else {
   break;
  }
  alpha_step = alpha_step / 2.0;
  if(alpha_step < 0.00001)
   break;
 }
 for ( i = 0 ; i < 1440 ; i++) {
  designed_pattern_X[i] = designed_pattern_X[i] + ((min_margin_front - min_margin_rear) / 2.0);
 }
}

variable_types: alpha float


main

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

v3 = &designed_pattern_X;
v4 = &designed_pattern_X;
puts("percent test");
calc_lens_thick_and_type("percent test", argv);
pos_value = 1112014848;
calc_front_percent();
do
printf("%.2f\n", *((float *)++v4 - 1));
while ( v4 != &half_of_max_thickness );
puts("frame curve test");
pos_value = 1036831949;
calc_frame_curve();
do
printf("%.2f\n", *((float *)++v3 - 1));
while ( v3 != &half_of_max_thickness );
return 0;
}

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

v3 = &designed_pattern_X;
v4 = &designed_pattern_X;
puts("percent test");
func7("percent test", argv);
pos_value = 1112014848;
func10();
do
printf("%.2f\n", *((float *)++v4 - 1));
while ( v4 != &half_of_max_thickness );
puts("frame curve test");
pos_value = 1036831949;
func12();
do
printf("%.2f\n", *((float *)++v3 - 1));
while ( v3 != &half_of_max_thickness );
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 int i;
 printf("percent test\n");
 calc_lens_thick_and_type();
 pos_value = 50.0;
 calc_front_percent();
 for ( i = 0 ; i < 1440; i++) {
  printf("%.2f\n", designed_pattern_X[i]);
 }
 printf("frame curve test\n");
 pos_value = 0.1;
 calc_frame_curve();
 for ( i = 0 ; i < 1440; i++) {
  printf("%.2f\n", designed_pattern_X[i]);
 }
 return 0;
}


unpack

decompiled_code:
__int64 __fastcall unpack(int a1)
{
__int64 result; // rax
__int64 v2; // rdx
char v3; // si
__int64 v4; // rdx
int v5; // esi
unsigned __int8 v6; // dl
__int16 v7; // si

if ( (ftpn & 1) != 0 )
{
v2 = outftp;
v3 = *(_BYTE *)outftp++;
result = v3 & 0xF;
switch ( a1 )
{
case 2:
result = (*(_BYTE *)(v2 + 1) >> 4) | (unsigned int)(16 * result);
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
ftpn += 2;
break;
case 4:
v7 = *(unsigned __int8 *)(v2 + 1);
outftp = v2 + 2;
ftpn += 4;
result = (unsigned __int16)(v7 << 12) | (16 * (_DWORD)result) | ((unsigned __int8)v7 >> 4) | (16 * *(unsigned __int8 *)(v2 + 2)) & 0xF00u;
break;
case 1:
if ( (v3 & 8) != 0 )
result = (unsigned int)(result - 16);
++ftpn;
break;
default:
goto LABEL_5;
}
}
else if ( a1 == 2 )
{
result = *(unsigned __int8 *)outftp++;
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
ftpn += 2;
}
else if ( a1 == 4 )
{
v4 = outftp;
ftpn += 4;
v5 = *(unsigned __int8 *)outftp;
outftp += 2LL;
result = v5 | (*(unsigned __int8 *)(v4 + 1) << 8);
}
else
{
result = 0LL;
if ( a1 != 1 )
{
LABEL_5:
ftpn += a1;
return result;
}
v6 = *(_BYTE *)outftp >> 4;
result = v6;
if ( v6 > 7u )
result = v6 | 0xFFFFFFF0;
++ftpn;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 result; // rax
__int64 v2; // rdx
char v3; // si
__int64 v4; // rdx
int v5; // esi
unsigned __int8 v6; // dl
__int16 v7; // si

if ( (ftpn & 1) != 0 )
{
v2 = outftp;
v3 = *(_BYTE *)outftp++;
result = v3 & 0xF;
switch ( a1 )
{
case 2:
result = (*(_BYTE *)(v2 + 1) >> 4) | (unsigned int)(16 * result);
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
ftpn += 2;
break;
case 4:
v7 = *(unsigned __int8 *)(v2 + 1);
outftp = v2 + 2;
ftpn += 4;
result = (unsigned __int16)(v7 << 12) | (16 * (_DWORD)result) | ((unsigned __int8)v7 >> 4) | (16 * *(unsigned __int8 *)(v2 + 2)) & 0xF00u;
break;
case 1:
if ( (v3 & 8) != 0 )
result = (unsigned int)(result - 16);
++ftpn;
break;
default:
goto LABEL_5;
}
}
else if ( a1 == 2 )
{
result = *(unsigned __int8 *)outftp++;
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
ftpn += 2;
}
else if ( a1 == 4 )
{
v4 = outftp;
ftpn += 4;
v5 = *(unsigned __int8 *)outftp;
outftp += 2LL;
result = v5 | (*(unsigned __int8 *)(v4 + 1) << 8);
}
else
{
result = 0LL;
if ( a1 != 1 )
{
LABEL_5:
ftpn += a1;
return result;
}
v6 = *(_BYTE *)outftp >> 4;
result = v6;
if ( v6 > 7u )
result = v6 | 0xFFFFFFF0;
++ftpn;
}
return result;
}

variable_types: j int


packit

original_code:
static S16 packit(S16 *src, U8 *dst, S16 num)
{
 S16 i;
 S16 state = 16;
 S16 dr, d2r, dr1 = 0;
 outftp = dst;
 for (ftpn = 0, i = 0; i < num; i++) {
  if (!i) {
   dr = src[i];
  } else {
   dr = src[i] - src[i - 1];
  }
  d2r = dr - dr1;
  switch (state) {
  case 16:
   if (dr < 128 && dr > - 128) {
    state = 8;
    pack(0x8000, 4);
    pack(dr, 2);
   } else {
    pack(src[i], 4);
   }
   break;
  case 8:
   if (dr >= 128 || dr <= -127) {
    state = 16;
    pack(0x81, 2);
    pack(src[i], 4);
   } else {
    if (d2r < 8 && d2r > -8) {
     state = 4;
     pack(0x80, 2);
     pack(d2r, 1);
    } else {
     pack(dr, 2);
    }
   }
   break;
  case 4:
   if (d2r >= 8 || d2r <= -8) {
    pack(0x8, 1);
    if (dr >=128 || dr <= -127) {
     state = 16;
     pack(0x81, 2);
     pack(src[i], 4);
    } else {
     state = 8;
     pack(dr, 2);
    }
   } else {
    pack(d2r, 1);
   }
   break;
  default:
   return -99;
  }
  dr1 = dr;
 }
 return ((ftpn + 1) >> 1);
}

canonical_original_code:
static int packit(int *src, unsigned char *dst, int num)
{
 int i;
 int state = 16;
 int dr; int d2r; int dr1 = 0;
 outftp = dst;
 for (ftpn = 0, i = 0; i < num; i++) {
  if (!i) {
   dr = src[i];
  } else {
   dr = src[i] - src[i - 1];
  }
  d2r = dr - dr1;
  switch (state) {
  case 16:
   if (dr < 128 && dr > - 128) {
    state = 8;
    pack(0x8000, 4);
    pack(dr, 2);
   } else {
    pack(src[i], 4);
   }
   break;
  case 8:
   if (dr >= 128 || dr <= -127) {
    state = 16;
    pack(0x81, 2);
    pack(src[i], 4);
   } else {
    if (d2r < 8 && d2r > -8) {
     state = 4;
     pack(0x80, 2);
     pack(d2r, 1);
    } else {
     pack(dr, 2);
    }
   }
   break;
  case 4:
   if (d2r >= 8 || d2r <= -8) {
    pack(0x8, 1);
    if (dr >=128 || dr <= -127) {
     state = 16;
     pack(0x81, 2);
     pack(src[i], 4);
    } else {
     state = 8;
     pack(dr, 2);
    }
   } else {
    pack(d2r, 1);
   }
   break;
  default:
   return -99;
  }
  dr1 = dr;
 }
 return ((ftpn + 1) >> 1);
}

variable_types:
src
int *

variable_types:
dst
unsigned char *

variable_types: num int

variable_types: state int

variable_types: dr int


unpackit

decompiled_code:
__int64 __fastcall unpackit(_WORD *a1, __int64 a2, __int16 a3)
{
int v3; // ebp
int i; // er9
int v5; // er10
int v6; // er11
int j; // er9
int v8; // er10
int v9; // er11
int v10; // eax
__int64 v11; // r9
int v12; // er11
int v14; // eax
int v15; // er9

outftp = a2;
ftpn = 0;
if ( a3 > 0 )
{
v3 = 0;
for ( i = unpack(4); ; i = unpack(4) )
{
if ( i == 0x8000 )
goto LABEL_4;
while ( 1 )
{
a1[v5] = i;
if ( !v5 )
break;
v3 = i - (__int16)a1[v5 - 1];
if ( v5 + 1 >= v6 )
return (unsigned int)((ftpn + 1) >> 1);
LABEL_7:
i = unpack(4);
if ( i == 0x8000 )
{
LABEL_4:
for ( j = unpack(2); ; j = unpack(2) )
{
if ( (_BYTE)j == 0x80 )
goto LABEL_12;
if ( (unsigned __int8)j == 129 )
goto LABEL_7;
if ( !v8 )
{
*a1 = j;
if ( v9 == 1 )
return (unsigned int)((ftpn + 1) >> 1);
v14 = unpack(2);
if ( (_BYTE)v14 == 0x80 )
{
v3 = v15;
LABEL_12:
while ( 1 )
{
v10 = unpack(1);
if ( (v10 & 0xF) == 8 )
break;
v3 += v10;
a1[v11] = v3 + a1[v11 - 1];
if ( v12 <= (int)v11 + 1 )
return (unsigned int)((ftpn + 1) >> 1);
}
goto LABEL_4;
}
if ( (unsigned __int8)v14 == 129 )
{
v3 = v15;
goto LABEL_7;
}
j = v14;
v8 = 1;
}
a1[v8] = j + a1[v8 - 1];
if ( v8 + 1 >= v9 )
return (unsigned int)((ftpn + 1) >> 1);
v3 = j;
}
}
}
if ( v6 == 1 )
return (unsigned int)((ftpn + 1) >> 1);
v3 = i;
}
}
return 0LL;
}

original_code:
static S16 unpackit(S16 *dst, U8 *src, S16 n)
{
 S16 state = 16, size = 4;
 S16 i, dr=0, d2r=0, dr1 = 0;
 S16 x;
 outftp = src;
 ftpn = 0;
 for (i = 0; i < n; i++) {
  AGAIN:
  x = unpack(size);
  switch (state) {
  case 16:
   if (x == 0x8000) {
    state = 8;
    size = 2;
    goto AGAIN;
   }
   dst[i] = x;
   if (i) {
    dr1 = x - dst[i - 1];
   } else {
    dr1 = x;
   }
   break;
  case 8:
   if ((x & 0xff) == 0x80) {
    state = 4;
    size = 1;
    goto AGAIN;
   }
   if ((x & 0xff) == 0x81) {
    state = 16;
    size = 4;
    goto AGAIN;
   }
   dr = x;
   if (i) {
    dst[i] = dst[i - 1] + dr;
   } else {
    dst[i] = dr;
   }
   dr1 = dr;
   break;
  case 4:
   if ((x & 0x0f) == 0x8) {
    state = 8;
    size = 2;
    goto AGAIN;
   }
   d2r = x;
   dr = dr1 + d2r;
   dst[i] = dst[i - 1] + dr;
   dr1 = dr;
   break;
  }
  printf("[%d] %d,  src(%02x, %d)\n", i, dst[i], *outftp, *outftp);
 }
 return (outftp - src);
}

canonical_original_code:
static int unpackit(int *dst, unsigned char *src, int n)
{
 int state = 16; int size = 4;
 int i; int dr = 0; int d2r = 0; int dr1 = 0;
 int x;
 outftp = src;
 ftpn = 0;
 for (i = 0; i < n; i++) {
  AGAIN:
  x = unpack(size);
  switch (state) {
  case 16:
   if (x == 0x8000) {
    state = 8;
    size = 2;
    goto AGAIN;
   }
   dst[i] = x;
   if (i) {
    dr1 = x - dst[i - 1];
   } else {
    dr1 = x;
   }
   break;
  case 8:
   if ((x & 0xff) == 0x80) {
    state = 4;
    size = 1;
    goto AGAIN;
   }
   if ((x & 0xff) == 0x81) {
    state = 16;
    size = 4;
    goto AGAIN;
   }
   dr = x;
   if (i) {
    dst[i] = dst[i - 1] + dr;
   } else {
    dst[i] = dr;
   }
   dr1 = dr;
   break;
  case 4:
   if ((x & 0x0f) == 0x8) {
    state = 8;
    size = 2;
    goto AGAIN;
   }
   d2r = x;
   dr = dr1 + d2r;
   dst[i] = dst[i - 1] + dr;
   dr1 = dr;
   break;
  }
  printf("[%d] %d,  src(%02x, %d)\n", i, dst[i], *outftp, *outftp);
 }
 return (outftp - src);
}

variable_types:
dst
int *

variable_types:
src
unsigned char *

variable_types: n int

variable_types: size int

variable_types: x int


unescape

original_code:
static void unescape(unsigned char *dest, unsigned char *src, int len)
{
 int i, k;
 for ( i = 0, k = 0 ; i < len ; i ++, k++ ) {
  if ( src[i] == 0x1b ) {
   i++;
   dest[k] = src[i] & (~0x80);
  } else {
   dest[k] = src[i];
  }
 }
}

canonical_original_code:
static void unescape(unsigned char *dest, unsigned char *src, int len)
{
 int i; int k;
 for ( i = 0, k = 0 ; i < len ; i ++, k++ ) {
  if ( src[i] == 0x1b ) {
   i++;
   dest[k] = src[i] & (~0x80);
  } else {
   dest[k] = src[i];
  }
 }
}

variable_types:
dest
unsigned char *

variable_types: len int

variable_types: k int


decompiled_code:
__int64 __fastcall open_serial_port(const char *a1, char *a2)
{
int v2; // eax
unsigned int v3; // er12
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

v2 = open(a1, 2306);
serial_fd = v2;
if ( v2 < 0 )
{
printf("open_serial_port(%s, %s) ", a1, a2);
v3 = -1;
perror("open error");
}
else
{
v3 = v2;
tcgetattr(v2, &oldtio);
*(_QWORD *)&v5.c_cc[31] = 0LL;
*(_OWORD *)&v5.c_iflag = 0LL;
*(_OWORD *)&v5.c_line = 0LL;
*(_OWORD *)&v5.c_cc[15] = 0LL;
v5.c_ospeed = 0;
v5.c_iflag = 5;
v5.c_cflag = speed_str_to_brate(a2) | 0x8B0;
*(_WORD *)&v5.c_cc[5] = 8193;
tcflush(v3, 1);
tcflush(v3, 0);
tcsetattr(v3, 0, &v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, char *a2)
{
int v2; // eax
unsigned int v3; // er12
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

v2 = open(a1, 2306);
serial_fd = v2;
if ( v2 < 0 )
{
printf("open_serial_port(%s, %s) ", a1, a2);
v3 = -1;
perror("open error");
}
else
{
v3 = v2;
tcgetattr(v2, &oldtio);
*(_QWORD *)&v5.c_cc[31] = 0LL;
*(_OWORD *)&v5.c_iflag = 0LL;
*(_OWORD *)&v5.c_line = 0LL;
*(_OWORD *)&v5.c_cc[15] = 0LL;
v5.c_ospeed = 0;
v5.c_iflag = 5;
v5.c_cflag = func0(a2) | 0x8B0;
*(_WORD *)&v5.c_cc[5] = 8193;
tcflush(v3, 1);
tcflush(v3, 0);
tcsetattr(v3, 0, &v5);
}
return v3;
}

original_code:
int open_serial_port(char *port, char *baud_str)
{
 struct termios newtio;
 serial_fd = open(port, 02 | 0400 | 04000 );
 if ( serial_fd < 0 ) {
  printf("open_serial_port(%s, %s) ", port, baud_str);
  perror("open error");
  return -1;
 }
 tcgetattr(serial_fd, &oldtio);
 memset(&newtio, 0, sizeof(newtio));
 newtio.c_cflag = speed_str_to_brate(baud_str) | 0000060 | 0004000 | 0000200;
 newtio.c_iflag = 0000004 | 0000001;
 newtio.c_oflag = 0;
 newtio.c_lflag = 0;
 newtio.c_cc[5] = 1;
 newtio.c_cc[6] = 32;
 tcflush(serial_fd, 1);
 tcflush(serial_fd, 0);
 tcsetattr(serial_fd,0, &newtio);
 return serial_fd;
}

variable_types:
port
char *

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


decompiled_code:
int close_serial_port()
{
int v0; // ebp
int result; // eax

v0 = serial_fd;
if ( serial_fd > 2 )
{
tcsetattr(serial_fd, 0, &oldtio);
result = close(v0);
serial_fd = 0;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int v0; // ebp
int result; // eax

v0 = serial_fd;
if ( serial_fd > 2 )
{
tcsetattr(serial_fd, 0, &oldtio);
result = close(v0);
serial_fd = 0;
}
return result;
}

original_code:
void close_serial_port(void)
{
 if ( serial_fd > 2 ) {
  tcsetattr(serial_fd, 0, &oldtio);
  close(serial_fd);
  serial_fd = 0;
 }
}

canonical_original_code:
void close_serial_port()
{
 if ( serial_fd > 2 ) {
  tcsetattr(serial_fd, 0, &oldtio);
  close(serial_fd);
  serial_fd = 0;
 }
}


decompiled_code:
void read_value()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
int read_value( int type )
{
}

canonical_original_code:
int read_value(int type)
{
}

variable_types: type int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printf("%x %d hi(%d %x), lo(%d %x)\n", 107LL, 107LL, 6LL, 6LL, 4294967291LL, -5LL);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 union {
  struct {
   signed lo:4;
   signed hi:4;
  } nibble;
  char b;
 } byte;
 byte.b = 0x6b;
 printf("%x %d hi(%d %x), lo(%d %x)\n", byte.b, byte.b, byte.nibble.hi, byte.nibble.hi, byte.nibble.lo, byte.nibble.lo);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 union { struct { int lo; int hi; } nibble; char b; } byte;
 byte.b = 0x6b;
 printf("%x %d hi(%d %x), lo(%d %x)\n", byte.b, byte.b, byte.nibble.hi, byte.nibble.hi, byte.nibble.lo, byte.nibble.lo);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
static void get_intersection_point(float theta, float sx, float sy, float ex, float ey, float *px, float *py)
{
 double slope_a;
 double x, y;
 if ( sx == ex ) {
  x = sx;
  y = (sy + ey)/2.0;
 } else if ( theta == 3.14159265358979323846/2.0 || theta == 3.14159265358979323846 * 3.0 / 2.0 ) {
  x = 0.0;
  y = sy - (sy-ey)*sx/(sx-ex);
 } else {
  slope_a = tan(theta);
  if ( slope_a == (sy-ey)/(sx-ex)) {
   x = sx;
   y = (sy + ey)/2.0;
  } else {
   x = ( sy - (sy-ey) * sx/(sx-ex)) / (slope_a - ( (sy-ey)/(sx-ex)));
   y = slope_a * x;
  }
 }
 *px = x;
 *py = y;
}

canonical_original_code:
static void get_intersection_point(float theta, float sx, float sy, float ex, float ey, float *px, float *py)
{
 double slope_a;
 double x; double y;
 if ( sx == ex ) {
  x = sx;
  y = (sy + ey)/2.0;
 } else if ( theta == 3.14159265358979323846/2.0 || theta == 3.14159265358979323846 * 3.0 / 2.0 ) {
  x = 0.0;
  y = sy - (sy-ey)*sx/(sx-ex);
 } else {
  slope_a = tan(theta);
  if ( slope_a == (sy-ey)/(sx-ex)) {
   x = sx;
   y = (sy + ey)/2.0;
  } else {
   x = ( sy - (sy-ey) * sx/(sx-ex)) / (slope_a - ( (sy-ey)/(sx-ex)));
   y = slope_a * x;
  }
 }
 *px = x;
 *py = y;
}

variable_types: theta float

variable_types: sx float

variable_types: sy float

variable_types: ex float

variable_types: ey float

variable_types:
px
float *

variable_types:
py
float *

variable_types: x double

variable_types: y double


decompiled_code:
int __fastcall dump_file(const char *a1, int a2)
{
int v2; // eax
int v3; // ebp
int v4; // er14
int v5; // eax
int v6; // ebx
int v8; // eax
bool v9; // r12
__int64 v10; // r15
char v11[1064]; // [rsp+0h] [rbp-428h] BYREF

v2 = open(a1, 0);
if ( v2 < 0 )
return fprintf(stderr, "open error %s\n", a1);
v3 = v2;
v4 = 0;
puts("char data[] = {");
do
{
v5 = read(v3, v11, 0x3E8uLL);
v6 = v5;
if ( v5 <= 0 )
break;
v8 = v4 + v5;
if ( v8 > a2 )
{
v6 = a2 - v4;
if ( a2 - v4 <= 0 )
break;
v4 = a2;
v9 = 0;
}
else
{
v9 = v8 < a2;
v4 = v8;
}
v10 = 0LL;
do
{
while ( 1 )
{
printf("0x%02x, ", (unsigned __int8)v11[v10]);
if ( __ROR4__(-858993459 * (v10 + 1), 1) <= 0x19999999u )
break;
if ( v6 <= (int)++v10 )
goto LABEL_11;
}
++v10;
putchar(10);
}
while ( v6 > (int)v10 );
LABEL_11:
;
}
while ( v9 );
puts("\n};");
return close(v3);
}

original_code:
static void dump_file(char *path, int size)
{
 int fd;
 int ret;
 unsigned char buf[1001];
 int i;
 int sum = 0;
 fd = open(path, 00);
 if ( fd < 0 ) {
  fprintf(stderr, "open error %s\n", path);
  return;
 }
 printf("char data[] = {\n");
 do {
  ret = read(fd, buf, sizeof(buf)-1);
  if ( ret > 0 ) {
   if ( (sum + ret) > size ) {
    ret = size - sum;
   }
   for ( i = 0 ; i < ret ; i++ ) {
    printf("0x%02x, ", buf[i] );
    if ( (i + 1) % 10 == 0 ) {
     printf("\n");
    }
   }
   sum += ret;
  }
 } while (ret > 0 && sum < size );
 printf("\n};\n");
 close(fd);
}

variable_types:
path
char *

variable_types: size int

variable_types: fd int

variable_types: ret int

variable_types:
buf
unsigned char [1001]

variable_types: i int

variable_types: sum int


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

if ( argc <= 1 )
{
printf("%s [filename] [size]\n", *argv);
result = 1;
}
else
{
v3 = atoi(argv[2]);
dump_file(argv[1], v3);
result = 0;
}
return result;
}

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

if ( argc <= 1 )
{
printf("%s [filename] [size]\n", *argv);
result = 1;
}
else
{
v3 = atoi(argv[2]);
func7(argv[1], v3);
result = 0;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
 if ( argc < 2 ) {
  printf("%s [filename] [size]\n", argv[0]);
  return 1;
 }
 dump_file(argv[1], atoi(argv[2]));
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
static int parse_HVBG(EDGING_DATA *dest , char *str)
{
 float first = 0.0;
 float second = 0.0;
 printf("parsing HVBG=%s\n", str);
 set_parse_param_sep_str(",");
 if ( parse_param(str, "iff iff",
   &dest->bevel_type, &first, &second,
   &dest->pgrv_type, &dest->pgrv_start, &dest->pgrv_end) < 2 ) {
  set_parse_param_sep_str(" ");
  return -ERR_INVAL;
 }
 set_parse_param_sep_str(" ");
 if ( dest->bevel_type == 1 ) {
  dest->front_height = first;
  if ( dest->front_height < 0.0 ) {
   dest->front_height = 0.0;
  }
  if ( dest->front_height > 0.8 ) {
   dest->front_height = 0.8;
  }
  dest->rear_height = second;
  if ( dest->rear_height < dest->front_height ) {
   dest->rear_height = dest->front_height;
  }
  if ( dest->rear_height > 2.5 ) {
   dest->rear_height = 2.5;
  }
 } else if ( dest->bevel_type == 2 ) {
  dest->front_height = first;
  if ( dest->front_height < 0.0 ) {
   dest->front_height = 0.0;
  }
  if ( dest->front_height > 0.8 ) {
   dest->front_height = 0.8;
  }
  dest->rear_height = dest->front_height;
 } else if ( dest->bevel_type == 3) {
  dest->semi_u_x_offset = first;
  if ( dest->semi_u_x_offset < 0.0 ) {
   dest->semi_u_x_offset = 0.0;
  }
  if ( dest->semi_u_x_offset > 9.0 ) {
   dest->semi_u_x_offset = 9.0;
  }
  dest->rear_height = second;
  if ( dest->rear_height < 0.0 ) {
   dest->rear_height = 0.0;
  }
  if ( dest->rear_height > (9.0 - dest->semi_u_x_offset) ) {
   dest->rear_height = (9.0 - dest->semi_u_x_offset);
  }
 }
 printf("bevel_type = %d, front_height = %.2f, rear_height = %.2f, semi_u_x_offset = %.2f\n", dest->bevel_type, dest->front_height, dest->rear_height, dest->semi_u_x_offset);
 if ( dest->pgrv_type == 1 || dest->pgrv_type == 2 ) {
  dest->pgrv_start = fmod(dest->pgrv_start, 360);
  dest->pgrv_end = fmod(dest->pgrv_end, 360);
  if ( dest->pgrv_start > 180.0 ) {
   dest->pgrv_start -= 360.0;
  } else if ( dest->pgrv_start < -180.0 ) {
   dest->pgrv_start += 360.0;
  }
  if ( dest->pgrv_end > 180.0 ) {
   dest->pgrv_end -= 360.0;
  } else if ( dest->pgrv_end < -180.0 ) {
   dest->pgrv_end += 360.0;
  }
 }
 printf("pgrv_type = %d, pgrv_start = %.2f, pgrv_end = %.2f\n", dest->pgrv_type, dest->pgrv_start, dest->pgrv_end);
 return 0;
}

canonical_original_code:
static int parse_HVBG(struct EDGING_DATA *dest, char *str)
{
 float first = 0.0;
 float second = 0.0;
 printf("parsing HVBG=%s\n", str);
 set_parse_param_sep_str(",");
 if ( parse_param(str, "iff iff",
   &dest->bevel_type, &first, &second,
   &dest->pgrv_type, &dest->pgrv_start, &dest->pgrv_end) < 2 ) {
  set_parse_param_sep_str(" ");
  return -ERR_INVAL;
 }
 set_parse_param_sep_str(" ");
 if ( dest->bevel_type == 1 ) {
  dest->front_height = first;
  if ( dest->front_height < 0.0 ) {
   dest->front_height = 0.0;
  }
  if ( dest->front_height > 0.8 ) {
   dest->front_height = 0.8;
  }
  dest->rear_height = second;
  if ( dest->rear_height < dest->front_height ) {
   dest->rear_height = dest->front_height;
  }
  if ( dest->rear_height > 2.5 ) {
   dest->rear_height = 2.5;
  }
 } else if ( dest->bevel_type == 2 ) {
  dest->front_height = first;
  if ( dest->front_height < 0.0 ) {
   dest->front_height = 0.0;
  }
  if ( dest->front_height > 0.8 ) {
   dest->front_height = 0.8;
  }
  dest->rear_height = dest->front_height;
 } else if ( dest->bevel_type == 3) {
  dest->semi_u_x_offset = first;
  if ( dest->semi_u_x_offset < 0.0 ) {
   dest->semi_u_x_offset = 0.0;
  }
  if ( dest->semi_u_x_offset > 9.0 ) {
   dest->semi_u_x_offset = 9.0;
  }
  dest->rear_height = second;
  if ( dest->rear_height < 0.0 ) {
   dest->rear_height = 0.0;
  }
  if ( dest->rear_height > (9.0 - dest->semi_u_x_offset) ) {
   dest->rear_height = (9.0 - dest->semi_u_x_offset);
  }
 }
 printf("bevel_type = %d, front_height = %.2f, rear_height = %.2f, semi_u_x_offset = %.2f\n", dest->bevel_type, dest->front_height, dest->rear_height, dest->semi_u_x_offset);
 if ( dest->pgrv_type == 1 || dest->pgrv_type == 2 ) {
  dest->pgrv_start = fmod(dest->pgrv_start, 360);
  dest->pgrv_end = fmod(dest->pgrv_end, 360);
  if ( dest->pgrv_start > 180.0 ) {
   dest->pgrv_start -= 360.0;
  } else if ( dest->pgrv_start < -180.0 ) {
   dest->pgrv_start += 360.0;
  }
  if ( dest->pgrv_end > 180.0 ) {
   dest->pgrv_end -= 360.0;
  } else if ( dest->pgrv_end < -180.0 ) {
   dest->pgrv_end += 360.0;
  }
 }
 printf("pgrv_type = %d, pgrv_start = %.2f, pgrv_end = %.2f\n", dest->pgrv_type, dest->pgrv_start, dest->pgrv_end);
 return 0;
}

variable_types: struct EDGING_DATA { int bevel_type; float front_height; float rear_height; float semi_u_x_offset; int pgrv_type; float pgrv_start; float pgrv_end; } *

variable_types:
str
char *

variable_types: first float

variable_types: second float


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__m128i v5; // [rsp+0h] [rbp-48h] BYREF
char v6[8]; // [rsp+10h] [rbp-38h] BYREF
__int128 v7; // [rsp+20h] [rbp-28h] BYREF
__int64 v8; // [rsp+30h] [rbp-18h]
int v9; // [rsp+38h] [rbp-10h]

strcpy(v6, "3,90.4");
v5 = _mm_load_si128((const __m128i *)&LC18);
puts("test");
v7 = 0LL;
v8 = 0LL;
v9 = 0;
v3 = parse_HVBG((float *)&v7, v5.m128i_i8);
printf("ret = %d\n", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__m128i v5; // [rsp+0h] [rbp-48h] BYREF
char v6[8]; // [rsp+10h] [rbp-38h] BYREF
__int128 v7; // [rsp+20h] [rbp-28h] BYREF
__int64 v8; // [rsp+30h] [rbp-18h]
int v9; // [rsp+38h] [rbp-10h]

strcpy(v6, "3,90.4");
v5 = _mm_load_si128((const __m128i *)&LC18);
puts("test");
v7 = 0LL;
v8 = 0LL;
v9 = 0;
v3 = func0((float *)&v7, v5.m128i_i8);
printf("ret = %d\n", v3);
return 0;
}

original_code:
int main(void)
{
 char buf[] = "1,0.7,1.3,1,-45.3,90.4";
 EDGING_DATA e;
 int ret;
 printf("test\n");
 memset(&e, 0, sizeof(e));
 ret = parse_HVBG(&e, buf);
 printf("ret = %d\n", ret);
 return 0;
}

canonical_original_code:
int main()
{
 char buf[] = "1,0.7,1.3,1,-45.3,90.4";
 struct EDGING_DATA e;
 int ret;
 printf("test\n");
 memset(&e, 0, sizeof(e));
 ret = parse_HVBG(&e, buf);
 printf("ret = %d\n", ret);
 return 0;
}

variable_types:
buf
char []

variable_types: struct EDGING_DATA { int bevel_type; float front_height; float rear_height; float semi_u_x_offset; int pgrv_type; float pgrv_start; float pgrv_end; }


decompiled_code:
__int64 __fastcall read_file(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er13
int v5; // eax

if ( a3 <= 0 )
{
v4 = 0;
}
else
{
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), v3);
if ( v5 <= 0 )
break;
v4 += v5;
v3 -= v5;
if ( v4 >= v3 )
return (unsigned int)v4;
}
if ( v5 )
v4 = v5;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er13
int v5; // eax

if ( a3 <= 0 )
{
v4 = 0;
}
else
{
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), v3);
if ( v5 <= 0 )
break;
v4 += v5;
v3 -= v5;
if ( v4 >= v3 )
return (unsigned int)v4;
}
if ( v5 )
v4 = v5;
}
return (unsigned int)v4;
}

original_code:
static int read_file(int fd, unsigned char *buf, int size)
{
 int rd_idx = 0;
 int ret;
 while( rd_idx < size ) {
  ret = read(fd, &buf[rd_idx], size);
  if ( ret <= 0 ) {
   break;
  }
  rd_idx += ret;
  size -= ret;
 }
 if ( ret < 0 ) {
  return ret;
 }
 return rd_idx;
}

variable_types:
buf
unsigned char *


decompiled_code:
__int64 __fastcall read_bin_file(const char *a1, _QWORD *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er13
char *v5; // rax
char *v6; // rbx
int v7; // er13
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

v2 = open(a1, 0);
if ( v2 < 0 )
{
v7 = -1;
perror("read_file() : open error");
}
else
{
v3 = v2;
if ( fstat(v2, &v9) < 0 )
{
v7 = -2;
perror("read_file() : stat error");
close(v3);
}
else
{
v4 = v9.st_size;
v5 = (char *)malloc(v9.st_size + 8);
if ( v5 )
{
v6 = v5 + 4;
v7 = read_file(v3, (__int64)(v5 + 4), v4);
close(v3);
if ( v7 < 0 )
{
v7 = -4;
perror("read_file() : read error");
}
else if ( v7 == v9.st_size )
{
*a2 = v6;
}
else
{
v7 = -5;
perror("read_file() : size error");
}
}
else
{
v7 = -3;
perror("read_file() : alloc error");
close(v3);
}
}
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, _QWORD *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er13
char *v5; // rax
char *v6; // rbx
int v7; // er13
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

v2 = open(a1, 0);
if ( v2 < 0 )
{
v7 = -1;
perror("read_file() : open error");
}
else
{
v3 = v2;
if ( fstat(v2, &v9) < 0 )
{
v7 = -2;
perror("read_file() : stat error");
close(v3);
}
else
{
v4 = v9.st_size;
v5 = (char *)malloc(v9.st_size + 8);
if ( v5 )
{
v6 = v5 + 4;
v7 = func0(v3, (__int64)(v5 + 4), v4);
close(v3);
if ( v7 < 0 )
{
v7 = -4;
perror("read_file() : read error");
}
else if ( v7 == v9.st_size )
{
*a2 = v6;
}
else
{
v7 = -5;
perror("read_file() : size error");
}
}
else
{
v7 = -3;
perror("read_file() : alloc error");
close(v3);
}
}
}
return (unsigned int)v7;
}

original_code:
int read_bin_file(char *path, unsigned char **p_buf)
{
 int fd;
 struct stat sb;
 int ret;
 unsigned char *buf;
 fd = open(path, 00);
 if ( fd < 0 ) {
  perror("read_file() : open error");
  return -1;
 }
 if ( fstat(fd, &sb) < 0 ) {
  perror("read_file() : stat error");
  close(fd);
  return -2;
 }
 buf = (unsigned char *)malloc(sb.st_size + 8);
 if ( buf == ((void *)0) ) {
  perror("read_file() : alloc error");
  close(fd);
  return -3;
 }
 ret = read_file(fd, &buf[4], sb.st_size);
 close (fd);
 if ( ret < 0 ) {
  perror("read_file() : read error");
  return -4;
 }
 if ( ret != sb.st_size ) {
  perror("read_file() : size error");
  return -5;
 }
 *p_buf = &buf[4];
 return ret;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
float __fastcall degree2radian(float a1)
{
return a1 * 3.141592653589793 / 180.0;
}

original_code:
static float degree2radian(float angle)
{
 return (3.14159265358979323846 * angle) / 180.0;
}

variable_types: angle float

return_type: float


decompiled_code:
float __fastcall radian2degree(float a1)
{
return a1 * 180.0 / 3.141592653589793;
}

original_code:
static float radian2degree(float radian)
{
 return 180.0 * radian / 3.14159265358979323846;
}

variable_types: radian float


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm4_8
double v5; // xmm0_8
double v6; // xmm4_8
unsigned int v7; // eax
__int64 v8; // r12
float v9; // xmm4_4
float v10; // xmm5_4
__int64 v11; // rbp
__int64 v12; // rbx
double v13; // xmm0_8
float v14; // xmm2_4
double v15; // xmm0_8
double v16; // xmm1_8
double v17; // xmm0_8

if ( argc <= 1 )
{
printf("enter angle : ");
__isoc99_scanf();
v4 = 0.0;
}
else
{
v3 = atof(argv[1]);
HIDWORD(v4) = 0;
*(float *)&v4 = v3;
}
v5 = degree2radian("%.2f, %.4f -> start_idx = %d\n", v4);
v7 = ((__int64 (__fastcall *)(double, double, double))pgrv_angle_to_ptn_index_constprop_0)(v6, v6, v5);
v8 = (int)v7;
printf("%.2f, %.4f -> start_idx = %d\n", v9, v10, v7);
v11 = ((int)v8 + 1) % 1440;
v12 = ((int)v8 + 1439) % 1440;
printf("ptn[idx] = %.4f, ptn[idx-1] = %.4f, ptn[idx+1] = %.4f\n", ptn_list[v8], ptn_list[v12], ptn_list[v11]);
v13 = radian2degree("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v11]);
v14 = *(float *)&v13;
v15 = radian2degree("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v12]);
v16 = *(float *)&v15;
v17 = radian2degree("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v8]);
printf("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", *(float *)&v17, v16, v14);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm4_8
double v5; // xmm0_8
double v6; // xmm4_8
unsigned int v7; // eax
__int64 v8; // r12
float v9; // xmm4_4
float v10; // xmm5_4
__int64 v11; // rbp
__int64 v12; // rbx
double v13; // xmm0_8
float v14; // xmm2_4
double v15; // xmm0_8
double v16; // xmm1_8
double v17; // xmm0_8

if ( argc <= 1 )
{
printf("enter angle : ");
__isoc99_scanf();
v4 = 0.0;
}
else
{
v3 = atof(argv[1]);
HIDWORD(v4) = 0;
*(float *)&v4 = v3;
}
v5 = func7("%.2f, %.4f -> start_idx = %d\n", v4);
v7 = ((__int64 (__fastcall *)(double, double, double))pgrv_angle_to_ptn_index_constprop_0)(v6, v6, v5);
v8 = (int)v7;
printf("%.2f, %.4f -> start_idx = %d\n", v9, v10, v7);
v11 = ((int)v8 + 1) % 1440;
v12 = ((int)v8 + 1439) % 1440;
printf("ptn[idx] = %.4f, ptn[idx-1] = %.4f, ptn[idx+1] = %.4f\n", ptn_list[v8], ptn_list[v12], ptn_list[v11]);
v13 = func8("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v11]);
v14 = *(float *)&v13;
v15 = func8("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v12]);
v16 = *(float *)&v15;
v17 = func8("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", ptn_list[v8]);
printf("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", *(float *)&v17, v16, v14);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 extern float ptn_list[];
 int start_idx;
 float angle = 0.0;
 float radian;
 if ( argc < 2 ) {
  printf("enter angle : ");
  scanf("%f", &angle);
 } else {
  angle = atof(argv[1]);
 }
 radian = degree2radian(angle);
 start_idx = pgrv_angle_to_ptn_index(angle, ptn_list, 1440);
 printf("%.2f, %.4f -> start_idx = %d\n", angle, radian, start_idx);
 printf("ptn[idx] = %.4f, ptn[idx-1] = %.4f, ptn[idx+1] = %.4f\n", ptn_list[start_idx], ptn_list[(start_idx - 1 + 1440) % 1440], ptn_list[ (start_idx + 1) % 1440 ]);
 printf("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", radian2degree(ptn_list[start_idx]), radian2degree(ptn_list[(start_idx - 1 + 1440) % 1440]), radian2degree(ptn_list[ (start_idx + 1) % 1440 ]));
 return 0;
}


decompiled_code:
__int64 __fastcall check_trip(unsigned int *a1)
{
unsigned int v1; // er12
int v2; // edx
int v3; // esi
unsigned int v4; // er12
unsigned int v5; // eax
unsigned int v6; // er12
int v7; // ecx

pr_stat_isra_0(*a1, a1[1], a1[2], a1[3]);
v1 = *a1;
if ( *a1 == -10 || (v2 = a1[1], v2 == -10) || a1[3] == -10 || (v3 = a1[2], v3 == -10) )
{
v1 = 0;
puts("Inverter packet CRC error");
return v1;
}
if ( v1 )
{
puts("Inverter Error condition (1)");
goto LABEL_9;
}
if ( v2 >= 30 * dword_404A9C / 100 )
{
if ( low_freq_count_1 <= 0 )
{
if ( v3 || v2 != dword_404A9C )
{
dword_404A88 &= 0xFFFFFFFD;
zero_cur_count_0 = 0;
low_freq_count_1 = 0;
}
else
{
if ( zero_cur_count_0 > 10 )
{
puts("Inverter Error condition (4)");
goto LABEL_9;
}
++zero_cur_count_0;
}
}
else if ( v3 <= 1 && v2 == dword_404A9C )
{
v1 = dword_404A88 & 2;
if ( (dword_404A88 & 2) != 0 )
{
puts("Inverter Error condition (3)");
goto LABEL_9;
}
dword_404A88 |= 2u;
}
return v1;
}
if ( low_freq_count_1 <= 5 )
{
++low_freq_count_1;
return v1;
}
puts("Inverter Error condition (2)");
LABEL_9:
v4 = *a1;
dword_404A88 &= 0xFFFFFFFD;
zero_cur_count_0 = 0;
low_freq_count_1 = 0;
v5 = v4 - 2;
v6 = -v4;
if ( v5 > 3 )
{
v7 = a1[3];
v6 = -5;
if ( v7 >= dword_404AA4 / 2 )
v6 = (v7 > 130 * dword_404AA8 / 100) - 4;
}
printf("Inverter Error %d\n", v6);
pr_stat_isra_0(*a1, a1[1], a1[2], a1[3]);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int *a1)
{
unsigned int v1; // er12
int v2; // edx
int v3; // esi
unsigned int v4; // er12
unsigned int v5; // eax
unsigned int v6; // er12
int v7; // ecx

func7(*a1, a1[1], a1[2], a1[3]);
v1 = *a1;
if ( *a1 == -10 || (v2 = a1[1], v2 == -10) || a1[3] == -10 || (v3 = a1[2], v3 == -10) )
{
v1 = 0;
puts("Inverter packet CRC error");
return v1;
}
if ( v1 )
{
puts("Inverter Error condition (1)");
goto LABEL_9;
}
if ( v2 >= 30 * dword_404A9C / 100 )
{
if ( low_freq_count_1 <= 0 )
{
if ( v3 || v2 != dword_404A9C )
{
dword_404A88 &= 0xFFFFFFFD;
zero_cur_count_0 = 0;
low_freq_count_1 = 0;
}
else
{
if ( zero_cur_count_0 > 10 )
{
puts("Inverter Error condition (4)");
goto LABEL_9;
}
++zero_cur_count_0;
}
}
else if ( v3 <= 1 && v2 == dword_404A9C )
{
v1 = dword_404A88 & 2;
if ( (dword_404A88 & 2) != 0 )
{
puts("Inverter Error condition (3)");
goto LABEL_9;
}
dword_404A88 |= 2u;
}
return v1;
}
if ( low_freq_count_1 <= 5 )
{
++low_freq_count_1;
return v1;
}
puts("Inverter Error condition (2)");
LABEL_9:
v4 = *a1;
dword_404A88 &= 0xFFFFFFFD;
zero_cur_count_0 = 0;
low_freq_count_1 = 0;
v5 = v4 - 2;
v6 = -v4;
if ( v5 > 3 )
{
v7 = a1[3];
v6 = -5;
if ( v7 >= dword_404AA4 / 2 )
v6 = (v7 > 130 * dword_404AA8 / 100) - 4;
}
printf("Inverter Error %d\n", v6);
func7(*a1, a1[1], a1[2], a1[3]);
return v6;
}

original_code:
static int check_trip(INV_INFO *p)
{
 int ret = 0;
 static int low_freq_count = 0;
 static int zero_cur_count = 0;
 pr_stat(p);
 if ( p->trip == -INV_ERR_CRC || p->freq == -INV_ERR_CRC || p->dcv == -INV_ERR_CRC || p->cur == -INV_ERR_CRC ) {
  printf("Inverter packet CRC error\n");
  return 0;
 }
 if ( p->trip ) {
  printf("Inverter Error condition (1)\n");
 } else if ( p->freq < inv_meta.req_freq * 30 / 100) {
  if ( low_freq_count > 5 ) {
   printf("Inverter Error condition (2)\n");
  } else {
   low_freq_count++;
   return 0;
  }
 } else if ( low_freq_count > 0 ) {
  if ( (p->cur < 2) && (p->freq == inv_meta.req_freq) ) {
   if ( inv_meta.flag & 0x02 ) {
    printf("Inverter Error condition (3)\n");
   } else {
    inv_meta.flag |= 0x02;
    return 0;
   }
  } else {
   return 0;
  }
 } else if ((p->cur == 0 ) && (p->freq == inv_meta.req_freq) ) {
  if ( zero_cur_count > 10 ) {
   printf("Inverter Error condition (4)\n");
  } else {
   zero_cur_count++;
   return 0;
  }
 } else {
  zero_cur_count = 0;
  low_freq_count = 0;
  inv_meta.flag &= (~0x02);
  return 0;
 }
 zero_cur_count = 0;
 low_freq_count = 0;
 inv_meta.flag &= (~0x02);
 if ( p->trip >= INV_ERR_OVER_CURRENT && p->trip <= INV_ERR_LOW_VOLTAGE ) {
  ret = -p->trip;
 } else if ( p->dcv < inv_meta.normal_dcv * 50/100 ) {
  ret = -INV_ERR_LOW_VOLTAGE;
 } else if ( p->dcv > inv_meta.start_dcv * 130/100 ) {
  ret = -INV_ERR_OVER_VOLTAGE;
 } else {
  ret = -INV_ERR_OVER_HEAT;
 }
 printf("Inverter Error %d\n", ret);
 pr_stat(p);
 return ret;
}

canonical_original_code:
static int check_trip(struct _inv_info_rec *p)
{
 int ret = 0;
 static int low_freq_count = 0;
 static int zero_cur_count = 0;
 pr_stat(p);
 if ( p->trip == -INV_ERR_CRC || p->freq == -INV_ERR_CRC || p->dcv == -INV_ERR_CRC || p->cur == -INV_ERR_CRC ) {
  printf("Inverter packet CRC error\n");
  return 0;
 }
 if ( p->trip ) {
  printf("Inverter Error condition (1)\n");
 } else if ( p->freq < inv_meta.req_freq * 30 / 100) {
  if ( low_freq_count > 5 ) {
   printf("Inverter Error condition (2)\n");
  } else {
   low_freq_count++;
   return 0;
  }
 } else if ( low_freq_count > 0 ) {
  if ( (p->cur < 2) && (p->freq == inv_meta.req_freq) ) {
   if ( inv_meta.flag & 0x02 ) {
    printf("Inverter Error condition (3)\n");
   } else {
    inv_meta.flag |= 0x02;
    return 0;
   }
  } else {
   return 0;
  }
 } else if ((p->cur == 0 ) && (p->freq == inv_meta.req_freq) ) {
  if ( zero_cur_count > 10 ) {
   printf("Inverter Error condition (4)\n");
  } else {
   zero_cur_count++;
   return 0;
  }
 } else {
  zero_cur_count = 0;
  low_freq_count = 0;
  inv_meta.flag &= (~0x02);
  return 0;
 }
 zero_cur_count = 0;
 low_freq_count = 0;
 inv_meta.flag &= (~0x02);
 if ( p->trip >= INV_ERR_OVER_CURRENT && p->trip <= INV_ERR_LOW_VOLTAGE ) {
  ret = -p->trip;
 } else if ( p->dcv < inv_meta.normal_dcv * 50/100 ) {
  ret = -INV_ERR_LOW_VOLTAGE;
 } else if ( p->dcv > inv_meta.start_dcv * 130/100 ) {
  ret = -INV_ERR_OVER_VOLTAGE;
 } else {
  ret = -INV_ERR_OVER_HEAT;
 }
 printf("Inverter Error %d\n", ret);
 pr_stat(p);
 return ret;
}

variable_types: struct _inv_info_rec { int trip; int freq; int cur; int dcv; } *


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

puts("inverter simulation");
if ( inv_info_list != -1 )
{
v3 = &inv_info_list;
while ( (int)check_trip(v3, argv) >= 0 )
{
v3 += 4;
if ( *v3 == -1 )
return 0;
}
puts("error");
}
return 0;
}

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

puts("inverter simulation");
if ( inv_info_list != -1 )
{
v3 = &inv_info_list;
while ( (int)func8(v3, argv) >= 0 )
{
v3 += 4;
if ( *v3 == -1 )
return 0;
}
puts("error");
}
return 0;
}

original_code:
int main(void)
{
 int i;
 printf("inverter simulation\n");
 for ( i = 0 ; inv_info_list[i].trip != -1 ; i++ ) {
  if ( check_trip(&inv_info_list[i]) < 0 ) {
   printf("error\n");
   break;
  }
 }
 return 0;
}

canonical_original_code:
int main()
{
 int i;
 printf("inverter simulation\n");
 for ( i = 0 ; inv_info_list[i].trip != -1 ; i++ ) {
  if ( check_trip(&inv_info_list[i]) < 0 ) {
   printf("error\n");
   break;
  }
 }
 return 0;
}


decompiled_code:
__int64 send_error_code_list(int a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
int v9; // ebx
unsigned int *v10; // rdx
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF
char s[128]; // [rsp+20h] [rbp-C8h] BYREF

va_start(va, a9);
v9 = 0;
strcpy(tmp_packet, "motorerrlist ");
do
{
if ( va[0].gp_offset <= 0x2F )
{
v10 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
else
{
v10 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
++v9;
sprintf(s, "%d ", *v10);
strcat(tmp_packet, s);
}
while ( a1 != v9 );
return 0LL;
}

canonical_decompiled_code:
__int64 func7(int a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, ...)
{
int v9; // ebx
unsigned int *v10; // rdx
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF
char s[128]; // [rsp+20h] [rbp-C8h] BYREF

va_start(va, a9);
v9 = 0;
strcpy(tmp_packet, "motorerrlist ");
do
{
if ( va[0].gp_offset <= 0x2F )
{
v10 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
else
{
v10 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
++v9;
sprintf(s, "%d ", *v10);
strcat(tmp_packet, s);
}
while ( a1 != v9 );
return 0LL;
}

original_code:
static int send_error_code_list(int cnt, ...)
{
 va_list ap;
 char str[128];
 int i;
 int val;
 sprintf(tmp_packet, "motorerrlist ");
 __builtin_va_start(ap,cnt);
 for ( i = 0 ; i < cnt ; i ++ ) {
  val = (int)__builtin_va_arg(ap,int);
  sprintf(str, "%d ", val);
  strcat(tmp_packet, str);
 }
 __builtin_va_end(ap);
 return 0;
}

canonical_original_code:
static int send_error_code_list(int cnt, ...)
{
 __builtin_va_list ap;
 char str[128];
 int i;
 int val;
 sprintf(tmp_packet, "motorerrlist ");
 __builtin_va_start(ap,cnt);
 for ( i = 0 ; i < cnt ; i ++ ) {
  val = (int)__builtin_va_arg(ap,int);
  sprintf(str, "%d ", val);
  strcat(tmp_packet, str);
 }
 __builtin_va_end(ap);
 return 0;
}

variable_types: cnt int

variable_types:
str
char [128]

variable_types: val int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int v4; // ecx
int v5; // er8
int v6; // er9
int v7; // er8
int v8; // er9
int v9; // er8
int v10; // er9
int v11; // edx
int v12; // ecx
int v13; // er8
int v14; // er9

puts("stdarg test");
send_error_code_list(1, 10, v3, v4, v5, v6);
send_error_code_list(3, 1, 2, 3, v7, v8);
send_error_code_list(10, 18, 28, 38, 1, 2);
send_error_code_list(2, 100, 200, 300, v9, v10);
send_error_code_list(4, 100, v11, v12, v13, v14);
puts("test end");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
int v4; // ecx
int v5; // er8
int v6; // er9
int v7; // er8
int v8; // er9
int v9; // er8
int v10; // er9
int v11; // edx
int v12; // ecx
int v13; // er8
int v14; // er9

puts("stdarg test");
func7(1, 10, v3, v4, v5, v6);
func7(3, 1, 2, 3, v7, v8);
func7(10, 18, 28, 38, 1, 2);
func7(2, 100, 200, 300, v9, v10);
func7(4, 100, v11, v12, v13, v14);
puts("test end");
return 0;
}

original_code:
int main(void)
{
 printf("stdarg test\n");
 send_error_code_list(1, 10);
 send_error_code_list(3, 1, 2, 3);
 send_error_code_list(10, 18, 28, 38, 1, 2, 10, 20, 30, 40, 50);
 send_error_code_list(2, 100, 200, 300);
 send_error_code_list(4, 100);
 printf("test end\n");
 return 0;
}

canonical_original_code:
int main()
{
 printf("stdarg test\n");
 send_error_code_list(1, 10);
 send_error_code_list(3, 1, 2, 3);
 send_error_code_list(10, 18, 28, 38, 1, 2, 10, 20, 30, 40, 50);
 send_error_code_list(2, 100, 200, 300);
 send_error_code_list(4, 100);
 printf("test end\n");
 return 0;
}