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 *


repo: never-run/spark

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

variable_types:
ctx
void *

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

return_type: void

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

return_type: int


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)
{
int v4; // eax
unsigned int v5; // er12
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

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

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

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

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 v7; // 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 )
{
while ( 1 )
{
v7 = spk_cap_adpt_get_pkts(v5, v8);
if ( v7 != 1 )
break;
printf(
"[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
(unsigned int)v8[0],
(unsigned int)v8[1],
v9,
v10);
}
if ( v7 )
{
if ( v7 == -1 || v7 == -2 )
fwrite("error\n", 1uLL, 6uLL, stderr);
}
else
{
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 v7; // 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 )
{
while ( 1 )
{
v7 = spk_cap_adpt_get_pkts(v5, v8);
if ( v7 != 1 )
break;
printf(
"[len : %5d][cap len : %5d][time : %10lu : %10lu]\n",
(unsigned int)v8[0],
(unsigned int)v8[1],
v9,
v10);
}
if ( v7 )
{
if ( v7 == -1 || v7 == -2 )
fwrite("error\n", 1uLL, 6uLL, stderr);
}
else
{
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 i; // rbx
__int64 *v1; // rbx

for ( i = 8LL; i != 1032; i += 16LL )
{
INIT_LIST_HEAD((char *)&tv5 + i);
INIT_LIST_HEAD((char *)&tv4 + i);
INIT_LIST_HEAD((char *)&tv3 + i);
INIT_LIST_HEAD((char *)&tv2 + i);
}
v1 = &qword_508;
do
{
INIT_LIST_HEAD(v1);
v1 += 2;
}
while ( v1 != (__int64 *)&unk_1508 );
}

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(_QWORD *a1)
{
_BOOL8 result; // rax

result = timer_pending(a1);
if ( result )
{
list_del((__int64)a1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_QWORD *a1)
{
_BOOL8 result; // rax

result = func6(a1);
if ( result )
{
func4((__int64)a1);
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(_QWORD *a1)
{
int result; // eax
const void *retaddr; // [rsp+8h] [rbp+0h]

a1[2] = timer_jiffies + 2LL * a1[2];
init_timer(a1);
pthread_mutex_lock(&timerlist_lock);
if ( timer_pending(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(_QWORD *a1)
{
int result; // eax
const void *retaddr; // [rsp+8h] [rbp+0h]

a1[2] = timer_jiffies + 2LL * a1[2];
func5(a1);
pthread_mutex_lock(&timerlist_lock);
if ( func6(a1) )
{
pthread_mutex_unlock(&timerlist_lock);
result = printf("bug: kernel timer added twice at %p.\n", retaddr);
}
else
{
func8(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(_QWORD *a1, __int64 a2)
{
_BOOL4 v2; // ebp

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(_QWORD *a1, __int64 a2)
{
_BOOL4 v2; // ebp

pthread_mutex_lock(&timerlist_lock);
a1[2] = a2;
v2 = func9(a1);
func8(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(_QWORD *a1)
{
_BOOL4 v1; // ebp

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

canonical_decompiled_code:
_BOOL8 __fastcall func14(_QWORD *a1)
{
_BOOL4 v1; // ebp

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

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; // ebx
int v1; // eax
struct sched_param param; // [rsp+Ch] [rbp-5Ch] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-58h] BYREF
pthread_t newthread[4]; // [rsp+48h] [rbp-20h] BYREF

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

canonical_decompiled_code:
__int64 func15()
{
unsigned int v0; // ebx
int v1; // eax
struct sched_param param; // [rsp+Ch] [rbp-5Ch] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-58h] BYREF
pthread_t newthread[4]; // [rsp+48h] [rbp-20h] BYREF

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


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
__int64 v6; // [rsp+0h] [rbp-118h] BYREF
char s[88]; // [rsp+90h] [rbp-88h] BYREF
__int64 v8; // [rsp+E8h] [rbp-30h]
__int64 v9; // [rsp+F0h] [rbp-28h]
__int64 v10; // [rsp+F8h] [rbp-20h]
__int64 v11; // [rsp+100h] [rbp-18h]
__int64 v12; // [rsp+108h] [rbp-10h]

strcpy(s, "# drill_retouch V1;001;12345;R\nDRILLE=B;CF;-26.20;-1.30;1.00;-24.20;-1.30;;;;F;;;;;\n");
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
ssl_set_printf_func(&printf, argv);
puts("test");
v3 = strlen(s);
v4 = parse_drill_retouch_file(s, v3, (__int64)&v6);
printf("ret = %d\n", v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // eax
__int64 v6; // [rsp+0h] [rbp-118h] BYREF
char s[88]; // [rsp+90h] [rbp-88h] BYREF
__int64 v8; // [rsp+E8h] [rbp-30h]
__int64 v9; // [rsp+F0h] [rbp-28h]
__int64 v10; // [rsp+F8h] [rbp-20h]
__int64 v11; // [rsp+100h] [rbp-18h]
__int64 v12; // [rsp+108h] [rbp-10h]

strcpy(s, "# drill_retouch V1;001;12345;R\nDRILLE=B;CF;-26.20;-1.30;1.00;-24.20;-1.30;;;;F;;;;;\n");
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
ssl_set_printf_func(&printf, argv);
puts("test");
v3 = strlen(s);
v4 = func1(s, v3, (__int64)&v6);
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
__int64 result; // rax

if ( argc <= 1 )
{
usage(*argv);
result = 0xFFFFFFFFLL;
}
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 )
{
usage(*argv);
return 4294967293LL;
}
}
}
result = 0LL;
if ( optind < argc )
{
strncpy(bin_file_path, argv[optind], 0x400uLL);
printf("DEBUG bin_file = %s\n", bin_file_path);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax

if ( argc <= 1 )
{
func0(*argv);
result = 0xFFFFFFFFLL;
}
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 )
{
func0(*argv);
return 4294967293LL;
}
}
}
result = 0LL;
if ( optind < argc )
{
strncpy(bin_file_path, argv[optind], 0x400uLL);
printf("DEBUG bin_file = %s\n", bin_file_path);
result = 0LL;
}
}
return result;
}

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()
{
__int64 result; // rax
double v1; // xmm3_8
int v2; // xmm0_4

result = 0LL;
v1 = (float)(*(float *)&pos_value / 100.0);
do
{
*(float *)&v2 = (double)feeler_data_rear[result] * 0.006283184000000001 * v1
+ (double)feeler_data_front[result] * 0.006283184000000001 * (1.0 - v1);
designed_pattern_X[result++] = v2;
}
while ( result != 1440 );
return result * 4;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 result; // rax
double v1; // xmm3_8
int v2; // xmm0_4

result = 0LL;
v1 = (float)(*(float *)&pos_value / 100.0);
do
{
*(float *)&v2 = (double)feeler_data_rear[result] * 0.006283184000000001 * v1
+ (double)feeler_data_front[result] * 0.006283184000000001 * (1.0 - v1);
designed_pattern_X[result++] = v2;
}
while ( result != 1440 );
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

puts("percent test");
calc_lens_thick_and_type();
pos_value = 1112014848;
calc_front_percent("percent test", argv);
v3 = designed_pattern_X;
v4 = designed_pattern_X;
do
printf("%.2f\n", *(float *)v4++);
while ( v4 != &half_of_max_thickness );
puts("frame curve test");
pos_value = 1036831949;
calc_frame_curve();
do
printf("%.2f\n", *(float *)v3++);
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

puts("percent test");
func7();
pos_value = 1112014848;
func10("percent test", argv);
v3 = designed_pattern_X;
v4 = designed_pattern_X;
do
printf("%.2f\n", *(float *)v4++);
while ( v4 != &half_of_max_thickness );
puts("frame curve test");
pos_value = 1036831949;
func12();
do
printf("%.2f\n", *(float *)v3++);
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;
}


pack

decompiled_code:
__int64 __fastcall pack(unsigned __int16 a1, __int16 a2)
{
__int64 result; // rax
_BYTE *v3; // rdx
__int64 v4; // rcx
_BYTE *v5; // rdx
_BYTE *v6; // rdx

result = (unsigned __int16)ftpn;
if ( (ftpn & 1) != 0 )
{
if ( a2 <= 1 )
{
v6 = (_BYTE *)outftp++;
*v6 |= a1 & 0xF;
}
else if ( a2 == 2 )
{
v5 = (_BYTE *)outftp++;
*v5 |= (unsigned __int8)a1 >> 4;
v5[1] = 16 * a1;
}
else
{
v4 = outftp;
*(_BYTE *)outftp |= (unsigned __int8)a1 >> 4;
outftp = v4 + 2;
*(_BYTE *)(v4 + 1) = (16 * a1) | (a1 >> 12);
*(_BYTE *)(v4 + 2) = ((int)a1 >> 4) & 0xF0;
}
}
else if ( a2 <= 1 )
{
*(_BYTE *)outftp = 16 * a1;
}
else
{
v3 = (_BYTE *)outftp++;
*v3 = a1;
if ( a2 > 2 )
{
outftp = (__int64)(v3 + 2);
v3[1] = HIBYTE(a1);
}
}
ftpn = result + a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int16 a1, __int16 a2)
{
__int64 result; // rax
_BYTE *v3; // rdx
__int64 v4; // rcx
_BYTE *v5; // rdx
_BYTE *v6; // rdx

result = (unsigned __int16)ftpn;
if ( (ftpn & 1) != 0 )
{
if ( a2 <= 1 )
{
v6 = (_BYTE *)outftp++;
*v6 |= a1 & 0xF;
}
else if ( a2 == 2 )
{
v5 = (_BYTE *)outftp++;
*v5 |= (unsigned __int8)a1 >> 4;
v5[1] = 16 * a1;
}
else
{
v4 = outftp;
*(_BYTE *)outftp |= (unsigned __int8)a1 >> 4;
outftp = v4 + 2;
*(_BYTE *)(v4 + 1) = (16 * a1) | (a1 >> 12);
*(_BYTE *)(v4 + 2) = ((int)a1 >> 4) & 0xF0;
}
}
else if ( a2 <= 1 )
{
*(_BYTE *)outftp = 16 * a1;
}
else
{
v3 = (_BYTE *)outftp++;
*v3 = a1;
if ( a2 > 2 )
{
outftp = (__int64)(v3 + 2);
v3[1] = HIBYTE(a1);
}
}
ftpn = result + a2;
return result;
}

variable_types:
i
unsigned int

variable_types: n int


unpack

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

if ( (ftpn & 1) != 0 )
{
v5 = (_BYTE *)outftp++;
result = *v5 & 0xF;
if ( a1 == 2 )
{
result = (v5[1] >> 4) | (unsigned int)(16 * result);
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
}
else if ( a1 == 4 )
{
v6 = (unsigned __int8)v5[1];
outftp = (__int64)(v5 + 2);
result = (unsigned __int16)(v6 << 12) | (16 * (_DWORD)result) | ((unsigned __int8)v6 >> 4) | (16
* (unsigned __int8)v5[2]) & 0xF00u;
}
else if ( a1 == 1 && (*v5 & 0xFu) > 7 )
{
result = (unsigned int)(result - 16);
}
}
else if ( a1 == 2 )
{
v2 = (unsigned __int8 *)outftp++;
result = *v2;
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
}
else if ( a1 == 4 )
{
v3 = outftp;
v4 = *(unsigned __int8 *)outftp;
outftp += 2LL;
result = v4 | (*(unsigned __int8 *)(v3 + 1) << 8);
}
else
{
result = 0LL;
if ( a1 == 1 )
{
result = *(_BYTE *)outftp >> 4;
if ( (unsigned __int8)(*(_BYTE *)outftp >> 4) > 7u )
result = (unsigned int)(result - 16);
}
}
ftpn += a1;
return result;
}

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

if ( (ftpn & 1) != 0 )
{
v5 = (_BYTE *)outftp++;
result = *v5 & 0xF;
if ( a1 == 2 )
{
result = (v5[1] >> 4) | (unsigned int)(16 * result);
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
}
else if ( a1 == 4 )
{
v6 = (unsigned __int8)v5[1];
outftp = (__int64)(v5 + 2);
result = (unsigned __int16)(v6 << 12) | (16 * (_DWORD)result) | ((unsigned __int8)v6 >> 4) | (16
* (unsigned __int8)v5[2]) & 0xF00u;
}
else if ( a1 == 1 && (*v5 & 0xFu) > 7 )
{
result = (unsigned int)(result - 16);
}
}
else if ( a1 == 2 )
{
v2 = (unsigned __int8 *)outftp++;
result = *v2;
if ( (int)result > 127 )
result = (unsigned int)(result - 256);
}
else if ( a1 == 4 )
{
v3 = outftp;
v4 = *(unsigned __int8 *)outftp;
outftp += 2LL;
result = v4 | (*(unsigned __int8 *)(v3 + 1) << 8);
}
else
{
result = 0LL;
if ( a1 == 1 )
{
result = *(_BYTE *)outftp >> 4;
if ( (unsigned __int8)(*(_BYTE *)outftp >> 4) > 7u )
result = (unsigned int)(result - 16);
}
}
ftpn += a1;
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

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: 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; // ebx
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);
perror("open error");
v3 = -1;
}
else
{
v3 = v2;
tcgetattr(v2, &oldtio);
*(_QWORD *)&v5.c_line = 0x2001000000000000LL;
*(_QWORD *)&v5.c_cc[7] = 0LL;
*(_QWORD *)&v5.c_cc[15] = 0LL;
*(_QWORD *)&v5.c_cc[23] = 0LL;
*(_QWORD *)&v5.c_cc[31] = 0LL;
v5.c_ospeed = 0;
*(_QWORD *)&v5.c_cflag = (unsigned int)speed_str_to_brate(a2) | 0x8B0LL;
*(_QWORD *)&v5.c_iflag = 5LL;
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; // ebx
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);
perror("open error");
v3 = -1;
}
else
{
v3 = v2;
tcgetattr(v2, &oldtio);
*(_QWORD *)&v5.c_line = 0x2001000000000000LL;
*(_QWORD *)&v5.c_cc[7] = 0LL;
*(_QWORD *)&v5.c_cc[15] = 0LL;
*(_QWORD *)&v5.c_cc[23] = 0LL;
*(_QWORD *)&v5.c_cc[31] = 0LL;
v5.c_ospeed = 0;
*(_QWORD *)&v5.c_cflag = (unsigned int)func0(a2) | 0x8B0LL;
*(_QWORD *)&v5.c_iflag = 5LL;
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; // ebx
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; // ebx
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; // er14
int v4; // er12
int v6; // ebp
__int64 v7; // rbx
int v8; // eax
char v9[1048]; // [rsp+0h] [rbp-418h] BYREF

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

canonical_decompiled_code:
int __fastcall func7(const char *a1, int a2)
{
int v2; // eax
int v3; // er14
int v4; // er12
int v6; // ebp
__int64 v7; // rbx
int v8; // eax
char v9[1048]; // [rsp+0h] [rbp-418h] BYREF

v2 = open(a1, 0);
if ( v2 < 0 )
return fprintf(stderr, "open error %s\n", a1);
v3 = v2;
puts("char data[] = {");
v4 = 0;
do
{
v8 = read(v3, v9, 0x3E8uLL);
v6 = v8;
if ( v8 <= 0 )
break;
if ( v4 + v8 > a2 )
{
v6 = a2 - v4;
if ( a2 - v4 <= 0 )
break;
}
v7 = 0LL;
do
{
printf("0x%02x, ", (unsigned __int8)v9[v7]);
if ( (_DWORD)v7 + 1 == 10 * (((int)v7 + 1) / 10) )
putchar(10);
++v7;
}
while ( v6 > (int)v7 );
v4 += v6;
if ( v6 <= 0 )
break;
}
while ( a2 > v4 );
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)
{
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)
{
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
__int64 v5[3]; // [rsp+0h] [rbp-48h] BYREF
int v6; // [rsp+18h] [rbp-30h]
char v7[40]; // [rsp+20h] [rbp-28h] BYREF

strcpy(v7, "1,0.7,1.3,1,-45.3,90.4");
puts("test");
v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v6 = 0;
v3 = parse_HVBG((float *)v5, v7);
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
__int64 v5[3]; // [rsp+0h] [rbp-48h] BYREF
int v6; // [rsp+18h] [rbp-30h]
char v7[40]; // [rsp+20h] [rbp-28h] BYREF

strcpy(v7, "1,0.7,1.3,1,-45.3,90.4");
puts("test");
v5[0] = 0LL;
v5[1] = 0LL;
v5[2] = 0LL;
v6 = 0;
v3 = func0((float *)v5, v7);
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 a4)
{
int v4; // ebx
int v5; // ebp
int v6; // eax

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

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __int64 a2, int a3, int a4)
{
int v4; // ebx
int v5; // ebp
int v6; // eax

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

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; // er12
int v4; // ebx
char *v5; // rax
int v6; // ecx
char *v7; // rbp
int v8; // ebx
struct stat v10; // [rsp+0h] [rbp-B8h] BYREF

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

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

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

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)
{
float v3; // xmm0_4
unsigned int v4; // ebp
__int64 v5; // r12
__int64 v6; // rbx
float v7; // xmm0_4
float v9; // [rsp+8h] [rbp-30h]
float v10; // [rsp+Ch] [rbp-2Ch]
float v11; // [rsp+Ch] [rbp-2Ch]
float v12; // [rsp+1Ch] [rbp-1Ch]

v12 = 0.0;
if ( argc <= 1 )
{
printf("enter angle : ");
__isoc99_scanf();
}
else
{
v3 = atof(argv[1]);
v12 = v3;
}
v10 = degree2radian(v12);
v4 = pgrv_angle_to_ptn_index((__int64)ptn_list, 1440, v12);
printf("%.2f, %.4f -> start_idx = %d\n", v12, v10, v4);
v5 = (int)(v4 + 1) % 1440;
v6 = (int)(v4 + 1439) % 1440;
printf("ptn[idx] = %.4f, ptn[idx-1] = %.4f, ptn[idx+1] = %.4f\n", ptn_list[v4], ptn_list[v6], ptn_list[v5]);
v9 = radian2degree(ptn_list[v5]);
v11 = radian2degree(ptn_list[v6]);
v7 = radian2degree(ptn_list[v4]);
printf("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", v7, v11, v9);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm0_4
unsigned int v4; // ebp
__int64 v5; // r12
__int64 v6; // rbx
float v7; // xmm0_4
float v9; // [rsp+8h] [rbp-30h]
float v10; // [rsp+Ch] [rbp-2Ch]
float v11; // [rsp+Ch] [rbp-2Ch]
float v12; // [rsp+1Ch] [rbp-1Ch]

v12 = 0.0;
if ( argc <= 1 )
{
printf("enter angle : ");
__isoc99_scanf();
}
else
{
v3 = atof(argv[1]);
v12 = v3;
}
v10 = func7(v12);
v4 = func9((__int64)ptn_list, 1440, v12);
printf("%.2f, %.4f -> start_idx = %d\n", v12, v10, v4);
v5 = (int)(v4 + 1) % 1440;
v6 = (int)(v4 + 1439) % 1440;
printf("ptn[idx] = %.4f, ptn[idx-1] = %.4f, ptn[idx+1] = %.4f\n", ptn_list[v4], ptn_list[v6], ptn_list[v5]);
v9 = func8(ptn_list[v5]);
v11 = func8(ptn_list[v6]);
v7 = func8(ptn_list[v4]);
printf("angle[idx] = %.2f, angle[idx-1] = %.2f, angle[idx+1] = %.2f\n", v7, v11, v9);
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; // ebp
int v2; // edx
int v3; // esi
int v5; // ecx

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

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int *a1)
{
unsigned int v1; // ebp
int v2; // edx
int v3; // esi
int v5; // ecx

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

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)
{
unsigned int *v3; // rbx

puts("inverter simulation");
if ( inv_info_list != -1 )
{
v3 = (unsigned int *)&inv_info_list;
while ( (int)check_trip(v3) >= 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)
{
unsigned int *v3; // rbx

puts("inverter simulation");
if ( inv_info_list != -1 )
{
v3 = (unsigned int *)&inv_info_list;
while ( (int)func8(v3) >= 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 i; // ebx
unsigned int *v10; // rdx
char v12[136]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-60h] BYREF

va_start(va, a9);
strcpy(tmp_packet, "motorerrlist ");
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
if ( va[0].gp_offset > 0x2F )
{
v10 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v10 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
sprintf(v12, "%d ", *v10);
strcat(tmp_packet, v12);
}
}
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 i; // ebx
unsigned int *v10; // rdx
char v12[136]; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-60h] BYREF

va_start(va, a9);
strcpy(tmp_packet, "motorerrlist ");
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
if ( va[0].gp_offset > 0x2F )
{
v10 = (unsigned int *)va[0].overflow_arg_area;
va[0].overflow_arg_area = (char *)va[0].overflow_arg_area + 8;
}
else
{
v10 = (unsigned int *)((char *)va[0].reg_save_area + va[0].gp_offset);
va[0].gp_offset += 8;
}
sprintf(v12, "%d ", *v10);
strcat(tmp_packet, v12);
}
}
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)
{
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // xmm2_8
double v6; // xmm3_8
double v7; // xmm6_8
double v8; // xmm7_8
double v9; // xmm4_8
double v10; // xmm5_8
double v11; // xmm4_8
double v12; // xmm5_8
double v13; // xmm4_8
double v14; // xmm5_8
double v15; // xmm4_8
double v16; // xmm5_8
double v17; // xmm4_8
double v18; // xmm5_8

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
double v4; // xmm1_8
double v5; // xmm2_8
double v6; // xmm3_8
double v7; // xmm6_8
double v8; // xmm7_8
double v9; // xmm4_8
double v10; // xmm5_8
double v11; // xmm4_8
double v12; // xmm5_8
double v13; // xmm4_8
double v14; // xmm5_8
double v15; // xmm4_8
double v16; // xmm5_8
double v17; // xmm4_8
double v18; // xmm5_8

puts("stdarg test");
func7(1, v3, v4, v5, v6, v9, v10, v7, v8, 10LL);
func7(3, v3, v4, v5, v6, v11, v12, v7, v8, 1LL, 2LL, 3LL);
func7(10, v3, v4, v5, v6, v13, v14, v7, v8, 18LL, 28LL, 38LL, 1LL, 2LL, 10LL, 20LL, 30LL, 40LL, 50LL);
func7(2, v3, v4, v5, v6, v15, v16, v7, v8, 100LL, 200LL, 300LL);
func7(4, v3, v4, v5, v6, v17, v18, v7, v8, 100LL);
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;
}


decompiled_code:
__int64 __fastcall parse_ascii_diff_record(char *src, int a2, int a3, __int64 a4, int a5)
{
unsigned int v6; // er13
__int64 i; // rbx
int v9; // ebp
char *v10; // rdi
char *v11; // rdi
char *v12; // r12
float v13; // [rsp+4h] [rbp-574h]
int v16[3]; // [rsp+14h] [rbp-564h]
char *s2[4]; // [rsp+20h] [rbp-558h]
char s[128]; // [rsp+40h] [rbp-538h] BYREF
char s1[128]; // [rsp+C0h] [rbp-4B8h] BYREF
char dest[8]; // [rsp+140h] [rbp-438h] BYREF
__int64 v21; // [rsp+148h] [rbp-430h]
char v22[1008]; // [rsp+150h] [rbp-428h] BYREF

*(_QWORD *)dest = 0LL;
v21 = 0LL;
memset(v22, 0, sizeof(v22));
s2[0] = (char *)&unk_54E1;
s2[1] = "A";
s2[2] = "Z";
v16[0] = LC3;
v16[1] = 953267991;
v16[2] = LC3;
strncpy(dest, src, 0x3FFuLL);
v6 = 0;
while ( (int)v6 < a5 )
{
if ( dest[0] )
{
if ( (unsigned __int8)(dest[0] - 29) <= 1u )
{
ssl_printf("parse_ascii_diff_record() : End of packet. record count = %d\n", v6);
break;
}
if ( (int)get_key_common(dest, "=", s1) < 0 )
{
ssl_printf("parse_ascii_diff_record() : Wrong record. record count = %d\n", v6);
break;
}
if ( strcmp(s1, s2[a2]) )
{
ssl_printf("parse_ascii_diff_record() : Wrong label. record count = %d\n", v6);
break;
}
get_value2(dest, s);
ssl_printf("#%d value [%s]\n", v6, s);
v11 = strtok(s, ";");
if ( v11 )
{
v9 = atoi(v11);
v13 = *(float *)&v16[a2];
*(float *)(a4 + 4LL * (int)v6) = (float)v9 * v13;
LODWORD(i) = v6 + 1;
if ( a3 )
{
for ( i = (int)i; ; ++i )
{
v6 = i;
v10 = strtok(0LL, ";");
if ( !v10 )
break;
v9 += atoi(v10);
*(float *)(a4 + 4 * i) = (float)v9 * v13;
}
}
else
{
for ( i = (int)i; ; ++i )
{
v6 = i;
v12 = strtok(0LL, ";");
if ( !v12 )
break;
ssl_printf("#%d prev = %d\n", (unsigned int)i, (unsigned int)v9);
v9 -= atoi(v12);
*(float *)(a4 + 4 * i) = (float)v9 * v13;
}
}
}
}
if ( (int)mem_get_line(0LL, dest, 1024LL) <= 0 )
return v6;
}
mem_unget_line(0LL, dest);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src, int a2, int a3, __int64 a4, int a5)
{
unsigned int v6; // er13
__int64 i; // rbx
int v9; // ebp
char *v10; // rdi
char *v11; // rdi
char *v12; // r12
float v13; // [rsp+4h] [rbp-574h]
int v16[3]; // [rsp+14h] [rbp-564h]
char *s2[4]; // [rsp+20h] [rbp-558h]
char s[128]; // [rsp+40h] [rbp-538h] BYREF
char s1[128]; // [rsp+C0h] [rbp-4B8h] BYREF
char dest[8]; // [rsp+140h] [rbp-438h] BYREF
__int64 v21; // [rsp+148h] [rbp-430h]
char v22[1008]; // [rsp+150h] [rbp-428h] BYREF

*(_QWORD *)dest = 0LL;
v21 = 0LL;
memset(v22, 0, sizeof(v22));
s2[0] = (char *)&unk_54E1;
s2[1] = "A";
s2[2] = "Z";
v16[0] = LC3;
v16[1] = 953267991;
v16[2] = LC3;
strncpy(dest, src, 0x3FFuLL);
v6 = 0;
while ( (int)v6 < a5 )
{
if ( dest[0] )
{
if ( (unsigned __int8)(dest[0] - 29) <= 1u )
{
ssl_printf("parse_ascii_diff_record() : End of packet. record count = %d\n", v6);
break;
}
if ( (int)get_key_common(dest, "=", s1) < 0 )
{
ssl_printf("parse_ascii_diff_record() : Wrong record. record count = %d\n", v6);
break;
}
if ( strcmp(s1, s2[a2]) )
{
ssl_printf("parse_ascii_diff_record() : Wrong label. record count = %d\n", v6);
break;
}
func0(dest, s);
ssl_printf("#%d value [%s]\n", v6, s);
v11 = strtok(s, ";");
if ( v11 )
{
v9 = atoi(v11);
v13 = *(float *)&v16[a2];
*(float *)(a4 + 4LL * (int)v6) = (float)v9 * v13;
LODWORD(i) = v6 + 1;
if ( a3 )
{
for ( i = (int)i; ; ++i )
{
v6 = i;
v10 = strtok(0LL, ";");
if ( !v10 )
break;
v9 += atoi(v10);
*(float *)(a4 + 4 * i) = (float)v9 * v13;
}
}
else
{
for ( i = (int)i; ; ++i )
{
v6 = i;
v12 = strtok(0LL, ";");
if ( !v12 )
break;
ssl_printf("#%d prev = %d\n", (unsigned int)i, (unsigned int)v9);
v9 -= atoi(v12);
*(float *)(a4 + 4 * i) = (float)v9 * v13;
}
}
}
}
if ( (int)mem_get_line(0LL, dest, 1024LL) <= 0 )
return v6;
}
mem_unget_line(0LL, dest);
return v6;
}

original_code:
int parse_ascii_diff_record(char *first_line, int type, int rev, float *list, int max)
{
 int count = 0;
 char line[1024] = {0, };
 char key[128];
 char value[128];
 char *ptr;
 int prev;
 int roll_back = 0;
 char *type_str[] = {
  "R",
  "A",
  "Z",
 };
 float convert_val[] = {
  1.0/1000.0,
  1.0/10000.0,
  1.0/1000.0,
 };
 strncpy(line, first_line, sizeof(line)-1);
 do {
  if ( count >= max ) {
   roll_back = 1;
   break;
  }
  if ( line[0] == 0 ) {
   continue;
  }
  if ( line[0] == 0x1E || line[0] == 0x1d) {
   ((void (*)(char *, ...))ssl_printf)("parse_ascii_diff_record() : End of packet. record count = %d\n", count);
   roll_back = 1;
   break;
  }
  if ( get_key_common(line, "=", key) < 0 ) {
   ((void (*)(char *, ...))ssl_printf)("parse_ascii_diff_record() : Wrong record. record count = %d\n", count);
   roll_back = 1;
   break;
  }
  if ( strcmp(key, type_str[type]) != 0 ) {
   ((void (*)(char *, ...))ssl_printf)("parse_ascii_diff_record() : Wrong label. record count = %d\n", count);
   roll_back = 1;
   break;
  }
  get_value2(line, value);
  ((void (*)(char *, ...))ssl_printf)("#%d value [%s]\n", count, value);
  ptr = strtok(value, ";");
  if ( !ptr ) {
   continue;
  }
  prev = atoi(ptr);
  list[count] = (float)prev * convert_val[type];
  count++;
  if ( rev ) {
   while ( (ptr = strtok(((void *)0), ";") ) != ((void *)0) ) {
    prev += atoi(ptr);
    list[count] = (float)prev * convert_val[type];
    count++;
   }
  } else {
   while ( (ptr = strtok(((void *)0), ";") ) != ((void *)0) ) {
    ((void (*)(char *, ...))ssl_printf)("#%d prev = %d\n", count, prev);
    prev -= atoi(ptr);
    list[count] = (float)prev * convert_val[type];
    count++;
   }
  }
 } while ( mem_get_line(((void *)0), line, sizeof(line)) > 0 );
 if ( roll_back) {
  mem_unget_line(((void *)0), line);
 }
 return count;
}

variable_types: type int

variable_types: rev int

variable_types:
list
float *

variable_types: max int

variable_types: count int

variable_types:
line
char [1024]

variable_types:
key
char [128]

variable_types:
value
char [128]

variable_types:
ptr
char *

variable_types: prev int


decompiled_code:
__int64 __fastcall parse_angle(__int64 a1, __int64 a2)
{
int v3; // eax
char dest[4]; // [rsp+Ch] [rbp-82Ch] BYREF
char s1[1024]; // [rsp+10h] [rbp-828h] BYREF
char v6[1036]; // [rsp+410h] [rbp-428h] BYREF
char src[4]; // [rsp+81Ch] [rbp-1Ch] BYREF

*(_DWORD *)src = 13;
set_parse_param_sep_str(";");
strncpy(dest, &mem_get_line_sep_str, 4uLL);
strncpy(&mem_get_line_sep_str, src, 8uLL);
mem_get_line(a1, v6, 0LL);
if ( (int)mem_get_line(0LL, v6, 1024LL) >= 0 && v6[0] != 29 )
{
if ( v6[0] == 28 )
v6[0] = 32;
if ( (int)get_key_common(v6, "=", s1) >= 0 && !strcmp(s1, "A") )
{
v3 = parse_ascii_diff_record(v6, 1, 0, a2, 1440);
if ( v3 < 0 )
ssl_printf("iso16824_to_epc_comm_frame_data() : R error. %d\n", (unsigned int)v3);
else
ssl_printf("record count = %d\n", (unsigned int)v3);
}
}
strncpy(&mem_get_line_sep_str, dest, 8uLL);
return set_parse_param_sep_str(" ");
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
int v3; // eax
char dest[4]; // [rsp+Ch] [rbp-82Ch] BYREF
char s1[1024]; // [rsp+10h] [rbp-828h] BYREF
char v6[1036]; // [rsp+410h] [rbp-428h] BYREF
char src[4]; // [rsp+81Ch] [rbp-1Ch] BYREF

*(_DWORD *)src = 13;
set_parse_param_sep_str(";");
strncpy(dest, &mem_get_line_sep_str, 4uLL);
strncpy(&mem_get_line_sep_str, src, 8uLL);
mem_get_line(a1, v6, 0LL);
if ( (int)mem_get_line(0LL, v6, 1024LL) >= 0 && v6[0] != 29 )
{
if ( v6[0] == 28 )
v6[0] = 32;
if ( (int)get_key_common(v6, "=", s1) >= 0 && !strcmp(s1, "A") )
{
v3 = func1(v6, 1, 0, a2, 1440);
if ( v3 < 0 )
ssl_printf("iso16824_to_epc_comm_frame_data() : R error. %d\n", (unsigned int)v3);
else
ssl_printf("record count = %d\n", (unsigned int)v3);
}
}
strncpy(&mem_get_line_sep_str, dest, 8uLL);
return set_parse_param_sep_str(" ");
}

original_code:
static void parse_angle(char *buf, float *list)
{
 char epc_comm_cr_str[4] = "\x0d";
 char line_buf[1024];
 char key[1024];
 int ret = 0;
 char prev_sep[4] = {0, };
 int i;
 set_parse_param_sep_str( ";" );
 strncpy(prev_sep, (mem_get_line_sep_str), sizeof(prev_sep));
 do { strncpy(mem_get_line_sep_str, epc_comm_cr_str, 8); } while(0);
 mem_get_line(buf, line_buf, 0);
 for ( i = 0 ; i < 1 ; i ++)
 {
  if ( mem_get_line(((void *)0), line_buf, sizeof(line_buf)) < 0 ) {
   break;
  }
  if ( line_buf[0] == 0x1d ) {
   break;
  }
  if ( line_buf[0] == 0x1c ) {
   line_buf[0] = ' ';
  }
  if ( get_key_common(line_buf, "=", key) < 0 ) {
   continue;
  }
  if ( (strcmp(key, "A") == 0 ) ) {
   if ( ( ret = parse_ascii_diff_record(line_buf, 1, 0, list, 1440)) < 0 ) {
    ((void (*)(char *, ...))ssl_printf)("iso16824_to_epc_comm_frame_data() : R error. %d\n", ret);
    continue;
   }
   ((void (*)(char *, ...))ssl_printf)("record count = %d\n", ret);
   continue;
  }
 }
 do { strncpy(mem_get_line_sep_str, prev_sep, 8); } while(0);
 set_parse_param_sep_str(" ");
}

variable_types:
buf
char *

variable_types:
key
char [1024]

variable_types: ret int

variable_types: i int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
float v5[1440]; // [rsp+0h] [rbp-2D08h] BYREF
float v6[1442]; // [rsp+1680h] [rbp-1688h] BYREF

memset(v6, 0, 0x1680uLL);
memset(v5, 0, sizeof(v5));
puts("ASCII diff test");
parse_angle((__int64)normal_angle, (__int64)v6);
parse_angle((__int64)reverse_angle, (__int64)v5);
for ( i = 0LL; i != 1440; ++i )
ssl_printf("[%d] %f %f\n", (unsigned int)i, v6[i], v5[i]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
float v5[1440]; // [rsp+0h] [rbp-2D08h] BYREF
float v6[1442]; // [rsp+1680h] [rbp-1688h] BYREF

memset(v6, 0, 0x1680uLL);
memset(v5, 0, sizeof(v5));
puts("ASCII diff test");
func2((__int64)normal_angle, (__int64)v6);
func2((__int64)reverse_angle, (__int64)v5);
for ( i = 0LL; i != 1440; ++i )
ssl_printf("[%d] %f %f\n", (unsigned int)i, v6[i], v5[i]);
return 0;
}

original_code:
int main(void)
{
 float normal_list[1440] = { 0.0, };
 float reverse_list[1440] = { 0.0, };
 int i;
 printf("ASCII diff test\n");
 parse_angle(normal_angle, normal_list);
 parse_angle(reverse_angle, reverse_list);
 for ( i = 0 ; i < 1440; i++ )
 {
  ((void (*)(char *, ...))ssl_printf)("[%d] %f %f\n", i, normal_list[i], reverse_list[i]);
 }
 return 0;
}

canonical_original_code:
int main()
{
 float normal_list[1440] = { 0.0, };
 float reverse_list[1440] = { 0.0, };
 int i;
 printf("ASCII diff test\n");
 parse_angle(normal_angle, normal_list);
 parse_angle(reverse_angle, reverse_list);
 for ( i = 0 ; i < 1440; i++ )
 {
  ((void (*)(char *, ...))ssl_printf)("[%d] %f %f\n", i, normal_list[i], reverse_list[i]);
 }
 return 0;
}


canonical_decompiled_code:
__int64 __fastcall func9(int fd, __int64 a2, int a3, int a4)
{
int v4; // ebx
int v5; // ebp
int v6; // eax

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

canonical_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: fd int

variable_types:
buf
unsigned char *

variable_types: size int


repo: newbg/romfser

decompiled_code:
__int64 __fastcall extract_inode(__int64 a1, __int64 a2)
{
const char *v2; // r13
unsigned int v3; // er12
int v4; // ebx
const void *v5; // r13
int v6; // eax
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v2 = (const char *)(a2 + 16);
if ( strcmp((const char *)(a2 + 16), (const char *)(a1 + 8)) )
return (unsigned int)-1;
v3 = 0;
snprintf(v8, 0x400uLL, "./%s%s", (const char *)(a1 + 98), v2);
if ( (_bswap_32(*(_DWORD *)a2) & 7) == 1 )
{
if ( mkdir(v8, 0x1EDu) == -1 )
{
perror("mkdir");
v3 = -1;
}
}
else
{
v4 = open(v8, 65);
if ( v4 == -1 && (v4 = open(v2, 65), v4 == -1) )
{
perror("open");
v3 = -1;
}
else
{
v5 = (const void *)(a2 + ((strlen(v2) + 15) & 0xFFFFFFFFFFFFFFF0LL) + 17);
v6 = _bswap_32(*(_DWORD *)(a2 + 8));
if ( write(v4, v5, v6) == -1 )
{
perror("write");
v3 = -1;
}
else
{
close(v4);
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
const char *v2; // r13
unsigned int v3; // er12
int v4; // ebx
const void *v5; // r13
int v6; // eax
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v2 = (const char *)(a2 + 16);
if ( strcmp((const char *)(a2 + 16), (const char *)(a1 + 8)) )
return (unsigned int)-1;
v3 = 0;
snprintf(v8, 0x400uLL, "./%s%s", (const char *)(a1 + 98), v2);
if ( (func7(*(_DWORD *)a2) & 7) == 1 )
{
if ( mkdir(v8, 0x1EDu) == -1 )
{
perror("mkdir");
v3 = -1;
}
}
else
{
v4 = open(v8, 65);
if ( v4 == -1 && (v4 = open(v2, 65), v4 == -1) )
{
perror("open");
v3 = -1;
}
else
{
v5 = (const void *)(a2 + ((strlen(v2) + 15) & 0xFFFFFFFFFFFFFFF0LL) + 17);
v6 = func7(*(_DWORD *)(a2 + 8));
if ( write(v4, v5, v6) == -1 )
{
perror("write");
v3 = -1;
}
else
{
close(v4);
}
}
}
return v3;
}

original_code:
int extract_inode(struct func_desc *func, struct romfs_file *file)
{
 int filefd=0,size=0;
 char *writeptr;
 char fpath[1024];
 if (strcmp(file->name, func->name))
  return -1;
 snprintf(fpath, 1024, "./%s%s",func->parentstr, file->name);
 if ((htonl(file->next)&0x7) == ROMFS_DIR)
 {
  if (mkdir(fpath, 0755) == -1)
  {
   perror("mkdir");
   return -1;
  }
 }
 else
 {
  if ((filefd = open(fpath, 0100|01)) == -1)
  {
   if ((filefd = open(file->name, 0100|01)) == -1)
   {
    perror("open");
    return -1;
   }
  }
  writeptr = (char*)((char*)file + sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1);
  size = htonl(file->size);
  if (write(filefd, writeptr, size) == -1)
  {
   perror("write");
   return -1;
  }
  close(filefd);
 }
 return 0;
}

canonical_original_code:
int extract_inode(struct func_desc *func, struct romfs_file *file)
{
 int filefd = 0; int size = 0;
 char *writeptr;
 char fpath[1024];
 if (strcmp(file->name, func->name))
  return -1;
 snprintf(fpath, 1024, "./%s%s",func->parentstr, file->name);
 if ((htonl(file->next)&0x7) == ROMFS_DIR)
 {
  if (mkdir(fpath, 0755) == -1)
  {
   perror("mkdir");
   return -1;
  }
 }
 else
 {
  if ((filefd = open(fpath, 0100|01)) == -1)
  {
   if ((filefd = open(file->name, 0100|01)) == -1)
   {
    perror("open");
    return -1;
   }
  }
  writeptr = (char *)((char *)file + sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1);
  size = htonl(file->size);
  if (write(filefd, writeptr, size) == -1)
  {
   perror("write");
   return -1;
  }
  close(filefd);
 }
 return 0;
}

variable_types: struct func_desc { int (*func)(); char name[45]; char sname[45]; char parentstr[1024]; } *

variable_types: struct romfs_file { unsigned int next; unsigned int spec; unsigned int size; unsigned int check; char name[]; } *

variable_types: filefd int

variable_types:
writeptr
char *

variable_types:
fpath
char [1024]


decompiled_code:
__int64 __fastcall sub_inode(__int64 a1, __int64 a2)
{
int v3; // er15
char *v4; // r13
unsigned int v5; // eax
unsigned int v6; // er12
size_t v7; // rbp
unsigned int v8; // ebx
size_t v9; // rax
unsigned int v10; // [rsp+4h] [rbp-1044h]
char *s; // [rsp+8h] [rbp-1040h]
char buf[4152]; // [rsp+10h] [rbp-1038h] BYREF

s = (char *)(a2 + 16);
v10 = strcmp((const char *)(a1 + 8), (const char *)(a2 + 16));
if ( v10 )
return 0;
v3 = open((const char *)(a1 + 53), 0);
if ( v3 == -1 )
{
perror("open");
v10 = -1;
}
else
{
v4 = (char *)(a2 + ((strlen(s) + 15) & 0xFFFFFFFFFFFFFFF0LL) + 17);
v5 = _bswap_32(*(_DWORD *)(a2 + 8));
if ( v5 >= 0x1000 )
v5 = 4096;
v6 = 0;
v7 = (int)v5;
while ( 1 )
{
v8 = _bswap_32(*(_DWORD *)(a2 + 8));
if ( v8 <= v6 )
break;
read(v3, buf, v7);
memcpy(v4, buf, v7);
v4 += v7;
v6 += 4096;
}
v9 = strlen(s);
if ( msync((void *)a2, ((v9 + 15) & 0xFFFFFFFFFFFFFFF0LL) + v8 + 17, 4) == -1 )
{
perror("msync");
v10 = -1;
}
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2)
{
int v3; // er15
char *v4; // r13
unsigned int v5; // eax
unsigned int v6; // er12
size_t v7; // rbp
unsigned int v8; // ebx
size_t v9; // rax
unsigned int v10; // [rsp+4h] [rbp-1044h]
char *s; // [rsp+8h] [rbp-1040h]
char buf[4152]; // [rsp+10h] [rbp-1038h] BYREF

s = (char *)(a2 + 16);
v10 = strcmp((const char *)(a1 + 8), (const char *)(a2 + 16));
if ( v10 )
return 0;
v3 = open((const char *)(a1 + 53), 0);
if ( v3 == -1 )
{
perror("open");
v10 = -1;
}
else
{
v4 = (char *)(a2 + ((strlen(s) + 15) & 0xFFFFFFFFFFFFFFF0LL) + 17);
v5 = func7(*(_DWORD *)(a2 + 8));
if ( v5 >= 0x1000 )
v5 = 4096;
v6 = 0;
v7 = (int)v5;
while ( 1 )
{
v8 = func7(*(_DWORD *)(a2 + 8));
if ( v8 <= v6 )
break;
read(v3, buf, v7);
memcpy(v4, buf, v7);
v4 += v7;
v6 += 4096;
}
v9 = strlen(s);
if ( msync((void *)a2, ((v9 + 15) & 0xFFFFFFFFFFFFFFF0LL) + v8 + 17, 4) == -1 )
{
perror("msync");
v10 = -1;
}
}
return v10;
}

original_code:
int sub_inode(struct func_desc *func, struct romfs_file *file)
{
 int filefd,i,readsiz=0;
 char *writeptr;
 char readstring[4096];
 if (strcmp(func->name, file->name))
  return 0;
 if ((filefd = open(func->sname, 00)) == -1)
 {
  perror("open");
  return -1;
 }
 writeptr = (char*)((char*)file + sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1);
 readsiz = htonl(file->size)<4096 ? htonl(file->size) : 4096;
 for(i=0;i<htonl(file->size);i+=4096)
 {
  read(filefd, readstring, readsiz);
  memcpy(writeptr, readstring, readsiz);
  writeptr+=readsiz;
 }
 if (msync(file, htonl(file->size)+sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1, 4) == -1)
 {
  perror("msync");
  return -1;
 }
 return 0;
}

canonical_original_code:
int sub_inode(struct func_desc *func, struct romfs_file *file)
{
 int filefd; int i; int readsiz = 0;
 char *writeptr;
 char readstring[4096];
 if (strcmp(func->name, file->name))
  return 0;
 if ((filefd = open(func->sname, 00)) == -1)
 {
  perror("open");
  return -1;
 }
 writeptr = (char *)((char *)file + sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1);
 readsiz = htonl(file->size)<4096 ? htonl(file->size) : 4096;
 for(i=0;i<htonl(file->size);i+=4096)
 {
  read(filefd, readstring, readsiz);
  memcpy(writeptr, readstring, readsiz);
  writeptr+=readsiz;
 }
 if (msync(file, htonl(file->size)+sizeof(struct romfs_file)+(((strlen(file->name))+15)&~15)+1, 4) == -1)
 {
  perror("msync");
  return -1;
 }
 return 0;
}

variable_types: readsiz int

variable_types:
readstring
char [4096]

user_defined_types: struct romfs_file { unsigned int next; unsigned int spec; unsigned int size; unsigned int check; char name[]; }

user_defined_types: struct func_desc { int (*func)(); char name[45]; char sname[45]; char parentstr[1024]; }