original_code:
int connect_pNet( BinLeaf *leaf, struct Extraction_Params *eparams ) {
 int err=0;
 double source[2], target[2];
 if ( eparams->outgoing ) {
  memcpy( source, eparams->ref, 2*sizeof *source );
  memcpy( target, leaf->x, 2*sizeof *target );
 } else {
  memcpy( source, leaf->x, 2*sizeof *source );
  memcpy( target, eparams->ref, 2*sizeof *target );
 }
 unsigned int i;
 int block=-1, index;
 pNode *source_pnode=0, *target_pnode=0, *n;
 for ( i=0; i<eparams->net->num_nodes; i++ ) {
  index = i % 256;
  if ( ! index )
   block++;
  n = &eparams->net->p_nodes[block][index];
  if ( ! memcmp( source, n->x, 2*sizeof *source ) ) {
   source_pnode = n;
   if ( target_pnode )
    break;
  }
  if ( ! memcmp( target, n->x, 2*sizeof *source ) ) {
   target_pnode = n;
   if ( source_pnode )
    break;
  }
 }
 if ( ! source_pnode ) {
  if ( ! eparams->create_nodes )
   return 0;
  if (( err = add_pNode( eparams->net, source, &source_pnode ) ))
   return err;
 }
 if ( ! target_pnode ) {
  if ( ! eparams->create_nodes )
   return 0;
  if (( err = add_pNode( eparams->net, target, &target_pnode ) ))
   return err;
 }
 pLink *plink;
 if ( eparams->outgoing ) {
  if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
   return err;
 } else {
  unsigned char out_of_bounds=0;
  for ( i=0; i<2; i++ ) {
   if ( target[i] <= -0.5 || target[i] >= 0.5 ) {
    out_of_bounds = 1;
    break;
   }
  }
  if ( out_of_bounds ) {
   if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
    return err;
  } else {
   block=-1;
   pLink *l;
   for ( i=0; i<eparams->net->num_links; i++ ) {
    index = i % 1024;
    if ( ! index )
     block++;
    l = &eparams->net->p_links[block][index];
    if ( l->from == source_pnode && l->to == target_pnode ) {
     return 0;
    }
   }
   if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
    return err;
  }
 }
 memcpy( plink->r, leaf->r, 1*sizeof *plink->r );
 return err;
}

canonical_original_code:
int connect_pNet(struct BinLeaf *leaf, struct Extraction_Params *eparams) {
 int err = 0;
 double source[2]; double target[2];
 if ( eparams->outgoing ) {
  memcpy( source, eparams->ref, 2*sizeof *source );
  memcpy( target, leaf->x, 2*sizeof *target );
 } else {
  memcpy( source, leaf->x, 2*sizeof *source );
  memcpy( target, eparams->ref, 2*sizeof *target );
 }
 unsigned int i;
 int block = -1; int index;
 struct pNode *source_pnode = 0; struct pNode *target_pnode = 0; struct pNode *n;
 for ( i=0; i<eparams->net->num_nodes; i++ ) {
  index = i % 256;
  if ( ! index )
   block++;
  n = &eparams->net->p_nodes[block][index];
  if ( ! memcmp( source, n->x, 2*sizeof *source ) ) {
   source_pnode = n;
   if ( target_pnode )
    break;
  }
  if ( ! memcmp( target, n->x, 2*sizeof *source ) ) {
   target_pnode = n;
   if ( source_pnode )
    break;
  }
 }
 if ( ! source_pnode ) {
  if ( ! eparams->create_nodes )
   return 0;
  if (( err = add_pNode( eparams->net, source, &source_pnode ) ))
   return err;
 }
 if ( ! target_pnode ) {
  if ( ! eparams->create_nodes )
   return 0;
  if (( err = add_pNode( eparams->net, target, &target_pnode ) ))
   return err;
 }
 struct pLink *plink;
 if ( eparams->outgoing ) {
  if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
   return err;
 } else {
  unsigned char out_of_bounds = 0;
  for ( i=0; i<2; i++ ) {
   if ( target[i] <= -0.5 || target[i] >= 0.5 ) {
    out_of_bounds = 1;
    break;
   }
  }
  if ( out_of_bounds ) {
   if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
    return err;
  } else {
   block=-1;
   struct pLink *l;
   for ( i=0; i<eparams->net->num_links; i++ ) {
    index = i % 1024;
    if ( ! index )
     block++;
    l = &eparams->net->p_links[block][index];
    if ( l->from == source_pnode && l->to == target_pnode ) {
     return 0;
    }
   }
   if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
    return err;
  }
 }
 memcpy( plink->r, leaf->r, 1*sizeof *plink->r );
 return err;
}

variable_types: struct BinLeaf { struct BinLeaf *c; double r[1]; double x[2]; int level; } *

variable_types: struct Extraction_Params { struct BinLeaf *root; double ref[2]; unsigned char outgoing; unsigned char create_nodes; struct CPPN { struct CPPN_Node { enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } func; double activation; } *nodes; int num_inputs; struct CPPN_Link { unsigned int innov_id; int from; double weight; int is_disabled; } *links; int num_links; } *cppn; struct NEAT_Params { unsigned int flags; int min_resolution; double variance_threshold; enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } output_funcs[1]; int initially_linked_outputs[1]; enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } *allowed_funcs; int num_allowed_funcs; int num_activations; int population_size; double survival_quota; int stagnation_age_threshold; double stagnation_score_threshold; double add_link_prob; unsigned int innov_counter; unsigned int species_counter; unsigned int individual_counter; } *params; struct pNetwork { unsigned int num_nodes; struct pNode { double x[2]; unsigned char used; unsigned int index; } **p_nodes; int num_inputs; unsigned int num_links; struct pLink { struct pNode *from; double r[1]; unsigned char used; } **p_links; } *net; } *

variable_types:
source
double [2]

variable_types:
target
double [2]

variable_types:
i
unsigned int

variable_types: index int

variable_types: struct pLink { struct pNode { double x[2]; unsigned char used; unsigned int index; } *from; double r[1]; unsigned char used; } *


backtrack

repo: kernfel/fNEAT

original_code:
void backtrack( pNetwork *net, pNode *n ) {
 n->used = 1;
 net->num_used_nodes++;
 unsigned int i;
 int index, block=-1;
 pLink *l;
 for ( i = 0; i < net->num_links; i++ ) {
  index = i % 1024;
  if ( ! index )
   block++;
  l = &net->p_links[block][index];
  if ( l->to == n ) {
   l->used = 1;
   net->num_used_links++;
   if ( ! l->from->used ) {
    backtrack( net, l->from );
   }
  }
 }
}

canonical_original_code:
void backtrack(struct pNetwork *net, struct pNode *n) {
 n->used = 1;
 net->num_used_nodes++;
 unsigned int i;
 int index; int block = -1;
 struct pLink *l;
 for ( i = 0; i < net->num_links; i++ ) {
  index = i % 1024;
  if ( ! index )
   block++;
  l = &net->p_links[block][index];
  if ( l->to == n ) {
   l->used = 1;
   net->num_used_links++;
   if ( ! l->from->used ) {
    backtrack( net, l->from );
   }
  }
 }
}

variable_types: struct pNetwork { unsigned int num_nodes; struct pNode { double x[2]; unsigned char used; unsigned int index; } **p_nodes; int num_inputs; unsigned int num_links; struct pLink { struct pNode *from; double r[1]; unsigned char used; } **p_links; } *

variable_types:
n
struct pNode *

variable_types: block int

return_type: void

user_defined_types: struct pNetwork { unsigned int num_nodes; struct pNode **p_nodes; int num_inputs; unsigned int num_links; struct pLink **p_links; }


original_code:
int build_pNetwork( pNetwork *net, CPPN *cppn, struct NEAT_Params *params, int num_inputs, const pNode *inputs, int num_outputs, const pNode *outputs ) {
 int block, index, err=0;
 unsigned int i;
 pNode *tmp;
 for ( i=0; i<num_inputs; i++ ) {
  if (( err = add_pNode( net, inputs[i].x, &tmp ) ))
   return err;
 }
 for ( i=0; i<num_outputs; i++ ) {
  if (( err = add_pNode( net, outputs[i].x, &tmp ) ))
   return err;
 }
 net->num_inputs = num_inputs;
 net->num_outputs = num_outputs;
 struct Extraction_Params eparams = {0};
 eparams.params = params;
 eparams.cppn = cppn;
 eparams.net = net;
 if ( params->max_network_depth ) {
  eparams.outgoing = 1;
  eparams.create_nodes = 1;
  block = -1;
  for ( i=0; i<num_inputs; i++ ) {
   index = i % 256;
   if ( ! index )
    block++;
   memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
   if (( err = extract_links( &eparams ) ))
    return err;
  }
  int depth;
  unsigned int i_start=num_inputs+num_outputs, i_end;
  block = (num_inputs+num_outputs-1) / 256;
  for ( depth=1; depth < params->max_network_depth; depth++ ) {
   i_end = net->num_nodes;
   for ( i=i_start; i<i_end; i++ ) {
    index = i % 256;
    if ( ! index )
     block++;
    memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
    if (( err = extract_links( &eparams ) ))
     return err;
   }
   i_start = i_end;
  }
 }
 eparams.outgoing = 0;
 eparams.create_nodes = 0;
 block = (num_inputs-1) / 256;
 for ( i=num_inputs; i<num_inputs+num_outputs; i++ ) {
  index = i % 256;
  if ( ! index )
   block++;
  memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
  if (( err = extract_links( &eparams ) ))
   return err;
  if ( ! (params->flags & (1 << 10)) ) {
   backtrack( net, &net->p_nodes[block][index] );
  }
 }
 return err;
}

canonical_original_code:
int build_pNetwork(struct pNetwork *net, struct CPPN *cppn, struct NEAT_Params *params, int num_inputs, struct pNode *inputs, int num_outputs, struct pNode *outputs) {
 int block; int index; int err = 0;
 unsigned int i;
 struct pNode *tmp;
 for ( i=0; i<num_inputs; i++ ) {
  if (( err = add_pNode( net, inputs[i].x, &tmp ) ))
   return err;
 }
 for ( i=0; i<num_outputs; i++ ) {
  if (( err = add_pNode( net, outputs[i].x, &tmp ) ))
   return err;
 }
 net->num_inputs = num_inputs;
 net->num_outputs = num_outputs;
 struct Extraction_Params eparams = {0};
 eparams.params = params;
 eparams.cppn = cppn;
 eparams.net = net;
 if ( params->max_network_depth ) {
  eparams.outgoing = 1;
  eparams.create_nodes = 1;
  block = -1;
  for ( i=0; i<num_inputs; i++ ) {
   index = i % 256;
   if ( ! index )
    block++;
   memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
   if (( err = extract_links( &eparams ) ))
    return err;
  }
  int depth;
  unsigned int i_start = num_inputs+num_outputs; unsigned int i_end;
  block = (num_inputs+num_outputs-1) / 256;
  for ( depth=1; depth < params->max_network_depth; depth++ ) {
   i_end = net->num_nodes;
   for ( i=i_start; i<i_end; i++ ) {
    index = i % 256;
    if ( ! index )
     block++;
    memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
    if (( err = extract_links( &eparams ) ))
     return err;
   }
   i_start = i_end;
  }
 }
 eparams.outgoing = 0;
 eparams.create_nodes = 0;
 block = (num_inputs-1) / 256;
 for ( i=num_inputs; i<num_inputs+num_outputs; i++ ) {
  index = i % 256;
  if ( ! index )
   block++;
  memcpy( eparams.ref, net->p_nodes[block][index].x, 2*sizeof *eparams.ref );
  if (( err = extract_links( &eparams ) ))
   return err;
  if ( ! (params->flags & (1 << 10)) ) {
   backtrack( net, &net->p_nodes[block][index] );
  }
 }
 return err;
}

variable_types:
inputs
struct pNode *

variable_types:
outputs
struct pNode *

variable_types: err int

variable_types: struct Extraction_Params { struct BinLeaf { struct BinLeaf *c; double r[1]; double x[2]; int level; } *root; double ref[2]; unsigned char outgoing; unsigned char create_nodes; struct CPPN { struct CPPN_Node { enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } func; double activation; } *nodes; int num_inputs; struct CPPN_Link { unsigned int innov_id; int from; double weight; int is_disabled; } *links; int num_links; } *cppn; struct NEAT_Params { unsigned int flags; int min_resolution; double variance_threshold; enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } output_funcs[1]; int initially_linked_outputs[1]; enum CPPNFunc { CF_GAUSS=0, CF_SIGMOID=1, CF_SINE=2, CF_LINEAR=3, CF_STEP=4, CF_ABS=5 } *allowed_funcs; int num_allowed_funcs; int num_activations; int population_size; double survival_quota; int stagnation_age_threshold; double stagnation_score_threshold; double add_link_prob; unsigned int innov_counter; unsigned int species_counter; unsigned int individual_counter; } *params; struct pNetwork { unsigned int num_nodes; struct pNode { double x[2]; unsigned char used; unsigned int index; } **p_nodes; int num_inputs; unsigned int num_links; struct pLink { struct pNode *from; double r[1]; unsigned char used; } **p_links; } *net; }

variable_types: depth int

return_type: int

user_defined_types: struct Extraction_Params { struct BinLeaf *root; double ref[2]; unsigned char outgoing; unsigned char create_nodes; struct CPPN *cppn; struct NEAT_Params *params; struct pNetwork *net; }

user_defined_types: struct CPPN { struct CPPN_Node *nodes; int num_inputs; struct CPPN_Link *links; int num_links; }

user_defined_types: struct NEAT_Params { unsigned int flags; int min_resolution; double variance_threshold; enum CPPNFunc output_funcs[1]; int initially_linked_outputs[1]; enum CPPNFunc *allowed_funcs; int num_allowed_funcs; int num_activations; int population_size; double survival_quota; int stagnation_age_threshold; double stagnation_score_threshold; double add_link_prob; unsigned int innov_counter; unsigned int species_counter; unsigned int individual_counter; }

user_defined_types: struct CPPN_Node { enum CPPNFunc func; double activation; }


repo: ketotek/http-web-server

decompiled_code:
__int64 __fastcall is_ctl(unsigned __int8 a1)
{
return (a1 == 127) | (unsigned __int8)(a1 <= 0x1Fu);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
return (a1 == 127) | (unsigned __int8)(a1 <= 0x1Fu);
}

original_code:
static int is_ctl(char c)
{
    return ((int)c >= 0 && (int)c <= 31) || (int)c == 127;
}

variable_types: c char


decompiled_code:
__int64 __fastcall parse_char(_QWORD *a1, char a2)
{
_BYTE *v2; // rcx
unsigned int v3; // edx

if ( !a1 )
return 1;
v2 = (_BYTE *)a1[7];
v3 = 1;
if ( (unsigned __int64)&v2[-*a1] < a1[1] - 1LL && *v2 == a2 )
{
a1[7] = v2 + 1;
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, char a2)
{
_BYTE *v2; // rcx
unsigned int v3; // edx

if ( !a1 )
return 1;
v2 = (_BYTE *)a1[7];
v3 = 1;
if ( (unsigned __int64)&v2[-*a1] < a1[1] - 1LL && *v2 == a2 )
{
a1[7] = v2 + 1;
v3 = 0;
}
return v3;
}

original_code:
static int parse_char(http_parser_t *parser, char c)
{
    if (parser == ((void *)0))
        return 1;
    if (parser->parse_ptr - parser->data >= parser->len - 1)
        return 1;
    if (*parser->parse_ptr == c) {
        parser->parse_ptr++;
        return 0;
    }
    return 1;
}

canonical_original_code:
static int parse_char(struct http_parser *parser, char c)
{
    if (parser == ((void *)0))
        return 1;
    if (parser->parse_ptr - parser->data >= parser->len - 1)
        return 1;
    if (*parser->parse_ptr == c) {
        parser->parse_ptr++;
        return 0;
    }
    return 1;
}

variable_types: struct http_parser { char *data; unsigned long len; struct http_request { int method; char *path; char *params; unsigned long field_count; struct http_header_field { char *name; char *value; int valid; } *fields; } req; char *parse_ptr; int initialized; } *

user_defined_types: struct http_parser { char *data; unsigned long len; struct http_request req; char *parse_ptr; int initialized; }

user_defined_types: struct http_request { int method; char *path; char *params; unsigned long field_count; struct http_header_field *fields; }


decompiled_code:
_BOOL8 __fastcall parse_crlf(_QWORD *a1)
{
int v1; // edx
_BOOL8 result; // rax

v1 = parse_char(a1, 13);
result = 1LL;
if ( !v1 )
result = (unsigned int)parse_char(a1, 10) != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_QWORD *a1)
{
int v1; // edx
_BOOL8 result; // rax

v1 = func1(a1, 13);
result = 1LL;
if ( !v1 )
result = (unsigned int)func1(a1, 10) != 0;
return result;
}

original_code:
static int parse_crlf(http_parser_t *parser)
{
    if (parse_char(parser, '\r'))
        return 1;
    if (parse_char(parser, '\n'))
        return 1;
    return 0;
}

canonical_original_code:
static int parse_crlf(struct http_parser *parser)
{
    if (parse_char(parser, '\r'))
        return 1;
    if (parse_char(parser, '\n'))
        return 1;
    return 0;
}

user_defined_types: struct http_header_field { char *name; char *value; int valid; }


decompiled_code:
__int64 __fastcall parse_string(__int64 a1, const char *a2, size_t a3)
{
size_t v3; // rbp
const char *v4; // rbx
size_t v5; // rax
unsigned int v6; // edx
char *v7; // rax
char v8; // dl

v3 = a3;
if ( a3 == 0 || a2 == 0LL )
return 1;
v4 = a2;
if ( !a1 )
return 1;
v5 = strlen(a2);
v6 = 1;
if ( v5 >= v3 )
{
v7 = *(char **)(a1 + 56);
v8 = *v7;
if ( *v7 )
{
do
{
if ( *v4 != v8 )
break;
if ( !*v4 )
break;
++v7;
++v4;
--v3;
v8 = *v7;
}
while ( *v7 );
v6 = 1;
if ( !v3 )
{
*(_QWORD *)(a1 + 56) = v7;
v6 = 0;
}
}
else
{
v6 = 1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, size_t a3)
{
size_t v3; // rbp
const char *v4; // rbx
size_t v5; // rax
unsigned int v6; // edx
char *v7; // rax
char v8; // dl

v3 = a3;
if ( a3 == 0 || a2 == 0LL )
return 1;
v4 = a2;
if ( !a1 )
return 1;
v5 = strlen(a2);
v6 = 1;
if ( v5 >= v3 )
{
v7 = *(char **)(a1 + 56);
v8 = *v7;
if ( *v7 )
{
do
{
if ( *v4 != v8 )
break;
if ( !*v4 )
break;
++v7;
++v4;
--v3;
v8 = *v7;
}
while ( *v7 );
v6 = 1;
if ( !v3 )
{
*(_QWORD *)(a1 + 56) = v7;
v6 = 0;
}
}
else
{
v6 = 1;
}
}
return v6;
}

original_code:
static int parse_string(http_parser_t *parser, const char *s, size_t len)
{
    char *old_ptr;
    if (parser == ((void *)0) || s == ((void *)0) || len == 0)
        return 1;
    if (len > strlen(s))
        return 1;
    old_ptr = parser->parse_ptr;
    while (len >= 0 && *old_ptr && *s && *old_ptr == *s) {
        old_ptr++;
        s++;
        len--;
    }
    if (len != 0)
        return 1;
    parser->parse_ptr = old_ptr;
    return 0;
}

canonical_original_code:
static int parse_string(struct http_parser *parser, char *s, unsigned long len)
{
    char *old_ptr;
    if (parser == ((void *)0) || s == ((void *)0) || len == 0)
        return 1;
    if (len > strlen(s))
        return 1;
    old_ptr = parser->parse_ptr;
    while (len >= 0 && *old_ptr && *s && *old_ptr == *s) {
        old_ptr++;
        s++;
        len--;
    }
    if (len != 0)
        return 1;
    parser->parse_ptr = old_ptr;
    return 0;
}

variable_types:
s
char *

variable_types:
len
unsigned long


decompiled_code:
char *__fastcall parse_field_content(_QWORD *a1)
{
const char *v1; // rbp
unsigned __int8 *v2; // rax
const char *v3; // r12
size_t v4; // r12
char *v5; // rax
char *v6; // rbx

v1 = (const char *)a1[7];
if ( (unsigned __int64)&v1[-*a1] >= a1[1] )
return 0LL;
v2 = (unsigned __int8 *)a1[7];
do
{
if ( (unsigned int)is_ctl(*v2) )
break;
if ( !parse_crlf(a1) )
break;
v2 = (unsigned __int8 *)(a1[7] + 1LL);
a1[7] = v2;
}
while ( (unsigned __int64)&v2[-*a1] < a1[1] );
v3 = (const char *)a1[7];
if ( v3 == v1 )
return 0LL;
v4 = v3 - v1;
v5 = (char *)malloc(v4 + 1);
v6 = v5;
if ( !v5 )
{
fprintf(stderr, "%s:%d\n", "http.c", 264LL);
perror(&s);
exit(1);
}
strncpy(v5, v1, v4);
v6[v4] = 0;
return v6;
}

canonical_decompiled_code:
char *__fastcall func5(_QWORD *a1)
{
const char *v1; // rbp
unsigned __int8 *v2; // rax
const char *v3; // r12
size_t v4; // r12
char *v5; // rax
char *v6; // rbx

v1 = (const char *)a1[7];
if ( (unsigned __int64)&v1[-*a1] >= a1[1] )
return 0LL;
v2 = (unsigned __int8 *)a1[7];
do
{
if ( (unsigned int)func0(*v2) )
break;
if ( !func2(a1) )
break;
v2 = (unsigned __int8 *)(a1[7] + 1LL);
a1[7] = v2;
}
while ( (unsigned __int64)&v2[-*a1] < a1[1] );
v3 = (const char *)a1[7];
if ( v3 == v1 )
return 0LL;
v4 = v3 - v1;
v5 = (char *)malloc(v4 + 1);
v6 = v5;
if ( !v5 )
{
fprintf(stderr, "%s:%d\n", "http.c", 264LL);
perror(&s);
exit(1);
}
strncpy(v5, v1, v4);
v6[v4] = 0;
return v6;
}

original_code:
static char *parse_field_content(http_parser_t *parser)
{
    char *start, *str, ch;
    start = parser->parse_ptr;
    do {
        if (parser->parse_ptr - parser->data >= parser->len)
            break;
        ch = *parser->parse_ptr;
        if (is_ctl(ch))
            break;
        if (!parse_crlf(parser))
            break;
        parser->parse_ptr++;
    } while (1);
    if (parser->parse_ptr - start == 0)
        return ((void *)0);
    str = malloc(parser->parse_ptr - start + 1);
    do { if (!(str != ((void *)0))) { fprintf(stderr, "%s:%d\n", "http.c", 264); perror(""); exit(1); } } while (0);
    strncpy(str, start, parser->parse_ptr - start);
    str[parser->parse_ptr - start] = '\0';
    return str;
}

canonical_original_code:
static char *parse_field_content(struct http_parser *parser)
{
    char *start; char *str; char ch;
    start = parser->parse_ptr;
    do {
        if (parser->parse_ptr - parser->data >= parser->len)
            break;
        ch = *parser->parse_ptr;
        if (is_ctl(ch))
            break;
        if (!parse_crlf(parser))
            break;
        parser->parse_ptr++;
    } while (1);
    if (parser->parse_ptr - start == 0)
        return ((void *)0);
    str = malloc(parser->parse_ptr - start + 1);
    do { if (!(str != ((void *)0))) { fprintf(stderr, "%s:%d\n", "http.c", 264); perror(""); exit(1); } } while (0);
    strncpy(str, start, parser->parse_ptr - start);
    str[parser->parse_ptr - start] = '\0';
    return str;
}

variable_types:
start
char *

variable_types:
str
char *

variable_types: ch char

return_type: char *


decompiled_code:
_BOOL8 __fastcall is_token(char a1)
{
return strchr("()<>@,;:\\\"/[]?={} \t", a1) == 0LL;
}

original_code:
static int is_token(char c)
{
    if (c > 127)
        return 0;
    return !!!strchr(separator, c);
}


decompiled_code:
char *__fastcall parse_token(__int64 *a1)
{
const char *v1; // r13
__int64 v2; // rbp
unsigned __int64 v3; // r12
char *v4; // rbx
char *v5; // rbp
size_t v6; // rbx
char *v7; // rax

v1 = (const char *)a1[7];
v2 = *a1;
v3 = a1[1];
if ( (unsigned __int64)&v1[-*a1] >= v3 )
return 0LL;
v4 = (char *)a1[7];
while ( is_token(*v4) )
{
if ( (unsigned __int64)&(++v4)[-v2] >= v3 )
return 0LL;
}
a1[7] = (__int64)v4;
v6 = v4 - v1;
v7 = (char *)malloc(v6 + 1);
v5 = v7;
if ( !v7 )
{
fprintf(stderr, "%s:%d\n", "http.c", 110LL);
perror(&s);
exit(1);
}
strncpy(v7, v1, v6);
v5[v6] = 0;
return v5;
}

canonical_decompiled_code:
char *__fastcall func7(__int64 *a1)
{
const char *v1; // r13
__int64 v2; // rbp
unsigned __int64 v3; // r12
char *v4; // rbx
char *v5; // rbp
size_t v6; // rbx
char *v7; // rax

v1 = (const char *)a1[7];
v2 = *a1;
v3 = a1[1];
if ( (unsigned __int64)&v1[-*a1] >= v3 )
return 0LL;
v4 = (char *)a1[7];
while ( func6(*v4) )
{
if ( (unsigned __int64)&(++v4)[-v2] >= v3 )
return 0LL;
}
a1[7] = (__int64)v4;
v6 = v4 - v1;
v7 = (char *)malloc(v6 + 1);
v5 = v7;
if ( !v7 )
{
fprintf(stderr, "%s:%d\n", "http.c", 110LL);
perror(&s);
exit(1);
}
strncpy(v7, v1, v6);
v5[v6] = 0;
return v5;
}

original_code:
static char *parse_token(http_parser_t *parser)
{
    char *old_ptr, *start, *token;
    old_ptr = parser->parse_ptr;
    start = old_ptr;
    do {
        if (old_ptr - parser->data >= parser->len)
            return ((void *)0);
        if (is_token(*old_ptr))
            old_ptr++;
        else
            break;
    } while (1);
    parser->parse_ptr = old_ptr;
    token = malloc(old_ptr - start + 1);
    do { if (!(token != ((void *)0))) { fprintf(stderr, "%s:%d\n", "http.c", 110); perror(""); exit(1); } } while (0);
    strncpy(token, start, old_ptr - start);
    token[old_ptr - start] = '\0';
    return token;
}

canonical_original_code:
static char *parse_token(struct http_parser *parser)
{
    char *old_ptr; char *start; char *token;
    old_ptr = parser->parse_ptr;
    start = old_ptr;
    do {
        if (old_ptr - parser->data >= parser->len)
            return ((void *)0);
        if (is_token(*old_ptr))
            old_ptr++;
        else
            break;
    } while (1);
    parser->parse_ptr = old_ptr;
    token = malloc(old_ptr - start + 1);
    do { if (!(token != ((void *)0))) { fprintf(stderr, "%s:%d\n", "http.c", 110); perror(""); exit(1); } } while (0);
    strncpy(token, start, old_ptr - start);
    token[old_ptr - start] = '\0';
    return token;
}

variable_types:
token
char *


original_code:
static int add_field(http_parser_t *parser)
{
    size_t field;
    for (field = 0; field < parser->req.field_count; field++)
        if (parser->req.fields[field].valid == 0) {
            break;
        }
    if (field == parser->req.field_count) {
        size_t newsz = 2 * parser->req.field_count * sizeof(http_header_field_t);
        newsz = newsz == 0 ? 2 : newsz;
        parser->req.field_count *= 2;
        parser->req.fields = realloc(parser->req.fields, newsz);
        memset(parser->req.fields + field, 0, newsz / 2);
        parser->req.fields[field].valid = 1;
    }
    return field;
}

canonical_original_code:
static int add_field(struct http_parser *parser)
{
    unsigned long field;
    for (field = 0; field < parser->req.field_count; field++)
        if (parser->req.fields[field].valid == 0) {
            break;
        }
    if (field == parser->req.field_count) {
        unsigned long newsz = 2 * parser->req.field_count * sizeof(http_header_field_t);
        newsz = newsz == 0 ? 2 : newsz;
        parser->req.field_count *= 2;
        parser->req.fields = realloc(parser->req.fields, newsz);
        memset(parser->req.fields + field, 0, newsz / 2);
        parser->req.fields[field].valid = 1;
    }
    return field;
}

variable_types:
field
unsigned long

variable_types:
newsz
unsigned long


decompiled_code:
__int64 __fastcall parse_message_header(__int64 *a1)
{
__int64 v1; // r12
unsigned int v2; // ebp
char *v3; // rax

v1 = a1[6] + 24LL * (int)add_field((__int64)a1);
*(_DWORD *)(v1 + 16) = 0;
parse_crlf(a1);
*(_QWORD *)v1 = parse_token(a1);
v2 = parse_char(a1, 58);
if ( v2 )
{
free(*(void **)v1);
v2 = 1;
}
else
{
while ( !(unsigned int)parse_char(a1, 32) )
;
v3 = parse_field_content(a1);
*(_QWORD *)(v1 + 8) = v3;
if ( v3 )
{
while ( !(unsigned int)parse_char(a1, 32) )
;
*(_DWORD *)(v1 + 16) = 1;
}
else
{
v2 = 1;
}
}
return v2;
}

original_code:
static int parse_message_header(http_parser_t *parser)
{
    int field_num;
    http_header_field_t *field;
    field_num = add_field(parser);
    field = &parser->req.fields[field_num];
    field->valid = 0;
    parse_crlf(parser);
    field->name = parse_token(parser);
    if (!field)
        return 1;
    if (parse_char(parser, ':')) {
        free(field->name);
        return 1;
    }
    while (parse_char(parser, ' ') == 0)
        ;
    field->value = parse_field_content(parser);
    if (!field->value)
        return 1;
    while (parse_char(parser, ' ') == 0)
        ;
    field->valid = 1;
    return 0;
}

canonical_original_code:
static int parse_message_header(struct http_parser *parser)
{
    int field_num;
    struct http_header_field *field;
    field_num = add_field(parser);
    field = &parser->req.fields[field_num];
    field->valid = 0;
    parse_crlf(parser);
    field->name = parse_token(parser);
    if (!field)
        return 1;
    if (parse_char(parser, ':')) {
        free(field->name);
        return 1;
    }
    while (parse_char(parser, ' ') == 0)
        ;
    field->value = parse_field_content(parser);
    if (!field->value)
        return 1;
    while (parse_char(parser, ' ') == 0)
        ;
    field->valid = 1;
    return 0;
}

variable_types: struct http_header_field { char *name; char *value; int valid; } *


decompiled_code:
__int64 __fastcall parse_digit(_QWORD *a1)
{
char *v1; // rbp
unsigned int v2; // edx
const unsigned __int16 **v3; // rax

v1 = (char *)a1[7];
v2 = 1;
if ( (unsigned __int64)&v1[-*a1] < a1[1] - 1LL )
{
v3 = _ctype_b_loc();
v2 = 1;
if ( ((*v3)[*v1] & 0x800) != 0 )
{
a1[7] = v1 + 1;
v2 = 0;
}
}
return v2;
}

original_code:
static int parse_digit(http_parser_t *parser)
{
    if (parser->parse_ptr - parser->data >= parser->len - 1)
        return 1;
    if (((*__ctype_b_loc ())[(int) ((*parser->parse_ptr))] & (unsigned short int) _ISdigit)) {
        parser->parse_ptr++;
        return 0;
    }
    return 1;
}

canonical_original_code:
static int parse_digit(struct http_parser *parser)
{
    if (parser->parse_ptr - parser->data >= parser->len - 1)
        return 1;
    if (((*__ctype_b_loc ())[(int) ((*parser->parse_ptr))] & (unsigned short) _ISdigit)) {
        parser->parse_ptr++;
        return 0;
    }
    return 1;
}


original_code:
void http_parser_free(http_parser_t *parser)
{
    size_t i;
    if (!parser || !parser->initialized)
        return;
    for (i = 0; i < parser->req.field_count; i++) {
        if (parser->req.fields[i].valid == 1) {
            free(parser->req.fields[i].name);
            free(parser->req.fields[i].value);
        }
    }
    if (parser->req.fields)
        free(parser->req.fields);
    if (parser->req.path)
        free(parser->req.path);
    if (parser->req.params)
        free(parser->req.params);
}

canonical_original_code:
void http_parser_free(struct http_parser *parser)
{
    unsigned long i;
    if (!parser || !parser->initialized)
        return;
    for (i = 0; i < parser->req.field_count; i++) {
        if (parser->req.fields[i].valid == 1) {
            free(parser->req.fields[i].name);
            free(parser->req.fields[i].value);
        }
    }
    if (parser->req.fields)
        free(parser->req.fields);
    if (parser->req.path)
        free(parser->req.path);
    if (parser->req.params)
        free(parser->req.params);
}

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall sock_create_listener(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 <= 0 )
{
fprintf(stderr, "%s:%d\n", "sock.c", 18LL);
perror(&s);
exit(1);
}
v2 = v1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
optval = 1;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 27LL);
perror(&s);
exit(1);
}
if ( bind(v2, &addr, 0x10u) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 30LL);
perror(&s);
exit(1);
}
if ( listen(v2, 10) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 33LL);
perror(&s);
exit(1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 <= 0 )
{
fprintf(stderr, "%s:%d\n", "sock.c", 18LL);
perror(&s);
exit(1);
}
v2 = v1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
optval = 1;
if ( setsockopt(v2, 1, 2, &optval, 4u) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 27LL);
perror(&s);
exit(1);
}
if ( bind(v2, &addr, 0x10u) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 30LL);
perror(&s);
exit(1);
}
if ( listen(v2, 10) )
{
fprintf(stderr, "%s:%d\n", "sock.c", 33LL);
perror(&s);
exit(1);
}
return v2;
}

original_code:
int sock_create_listener(unsigned short port)
{
    struct sockaddr_in addr;
    int fd, rc, opt;
    fd = socket(2, SOCK_STREAM, 0);
    do { if (!(fd > 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 18); perror(""); exit(1); } } while (0);
    memset(&addr, 0, sizeof addr);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    opt = 1;
    rc = setsockopt(fd, 1, 2, &opt, sizeof opt);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 27); perror(""); exit(1); } } while (0);
    rc = bind(fd, (struct sockaddr*)&addr, sizeof addr);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 30); perror(""); exit(1); } } while (0);
    rc = listen(fd, 10);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 33); perror(""); exit(1); } } while (0);
    return fd;
}

canonical_original_code:
int sock_create_listener(unsigned short port)
{
    struct sockaddr_in addr;
    int fd; int rc; int opt;
    fd = socket(2, SOCK_STREAM, 0);
    do { if (!(fd > 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 18); perror(""); exit(1); } } while (0);
    memset(&addr, 0, sizeof addr);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    opt = 1;
    rc = setsockopt(fd, 1, 2, &opt, sizeof opt);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 27); perror(""); exit(1); } } while (0);
    rc = bind(fd, (struct sockaddr *)&addr, sizeof addr);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 30); perror(""); exit(1); } } while (0);
    rc = listen(fd, 10);
    do { if (!(rc == 0)) { fprintf(stderr, "%s:%d\n", "sock.c", 33); perror(""); exit(1); } } while (0);
    return fd;
}

variable_types:
port
unsigned short

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

variable_types: fd int

variable_types: rc int

variable_types: opt int

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

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


decompiled_code:
__int64 __fastcall get_peer_address(int a1, char *a2, size_t a3)
{
unsigned __int16 v4; // r12
char *v5; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
if ( getpeername(a1, &addr, &len) < 0 )
return 0xFFFFFFFFLL;
v4 = _bswap_16(*(__int16 *)addr.sa_data);
v5 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
snprintf(a2, a3, "%s:%d", v5, v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, char *a2, size_t a3)
{
unsigned __int16 v4; // r12
char *v5; // rax
socklen_t len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

len = 16;
if ( getpeername(a1, &addr, &len) < 0 )
return 0xFFFFFFFFLL;
v4 = func0(*(__int16 *)addr.sa_data);
v5 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
snprintf(a2, a3, "%s:%d", v5, v4);
return 0LL;
}

original_code:
int get_peer_address(int sockfd, char *buf, size_t len)
{
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    if (getpeername(sockfd, (struct sockaddr *) &addr, &addrlen) < 0)
        return -1;
    snprintf(buf, len, "%s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
    return 0;
}

canonical_original_code:
int get_peer_address(int sockfd, char *buf, unsigned long len)
{
    struct sockaddr_in addr;
    unsigned int addrlen = sizeof(struct sockaddr_in);
    if (getpeername(sockfd, (struct sockaddr *) &addr, &addrlen) < 0)
        return -1;
    snprintf(buf, len, "%s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
    return 0;
}

variable_types: sockfd int

variable_types:
buf
char *

variable_types:
addrlen
unsigned int


clean

decompiled_code:
void __noreturn clean()
{
int v0; // ebx

puts("Terminated by sinal, Clean process will exit.");
v0 = access(newname, 0);
sync();
if ( unlink(newname) && v0 >= 0 )
printf("Can not remove the temporary file:%s", newname);
if ( !unlink(newname) )
printf("The temporary file:%s was delete.\n", newname);
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
int v0; // ebx

puts("Terminated by sinal, Clean process will exit.");
v0 = access(newname, 0);
sync();
if ( unlink(newname) && v0 >= 0 )
printf("Can not remove the temporary file:%s", newname);
if ( !unlink(newname) )
printf("The temporary file:%s was delete.\n", newname);
exit(1);
}

original_code:
void clean(int signal)
{
 int file_exist;
 printf("Terminated by sinal, Clean process will exit.\n");
 if(access(newname, 0) < 0)
 file_exist = 0;
 else
 file_exist = 1;
 sync();
 if((unlink(newname) != 0) && file_exist)
 {
  printf("Can not remove the temporary file:%s", newname);
 }
 int result = unlink(newname);
 if(result == 0)
 printf("The temporary file:%s was delete.\n", newname);
 exit(1);
}

variable_types: signal int

variable_types: result int


decompiled_code:
_BOOL8 __fastcall decide_file_exist(const char *a1)
{
return access(a1, 0) >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(const char *a1)
{
return access(a1, 0) >= 0;
}

original_code:
int decide_file_exist(char* filename)
{
 if(access(filename, 0) < 0)
 return 0;
 else
 return 1;
}

canonical_original_code:
int decide_file_exist(char *filename)
{
 if(access(filename, 0) < 0)
 return 0;
 else
 return 1;
}

variable_types:
filename
char *


decompiled_code:
int __fastcall create_temp_file(char *name)
{
size_t v1; // rax
size_t v2; // rax
char *v3; // rax
struct stat64 v5; // [rsp+0h] [rbp-98h] BYREF

strcpy(newname, name);
if ( opendir(name) )
{
v1 = strlen(newname);
if ( byte_40417F[v1] != 47 )
*(_WORD *)&newname[v1] = 47;
v2 = strlen(newname);
*(_QWORD *)&newname[v2] = 0x5858585858585858LL;
*(int *)((char *)&dword_404188 + v2) = 1482184750;
*(__int16 *)((char *)&word_40418C + v2) = 88;
if ( (int)lstat64(newname, &v5) < 0 )
v3 = newname;
else
LODWORD(v3) = printf("The file:%s is alredy exist.\n", newname);
}
else
{
LODWORD(v3) = printf("can not open: %s\n", name);
}
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func9(char *name)
{
size_t v1; // rax
size_t v2; // rax
char *v3; // rax
struct stat64 v5; // [rsp+0h] [rbp-98h] BYREF

strcpy(newname, name);
if ( opendir(name) )
{
v1 = strlen(newname);
if ( byte_40417F[v1] != 47 )
*(_WORD *)&newname[v1] = 47;
v2 = strlen(newname);
*(_QWORD *)&newname[v2] = 0x5858585858585858LL;
*(int *)((char *)&dword_404188 + v2) = 1482184750;
*(__int16 *)((char *)&word_40418C + v2) = 88;
if ( (int)func17(newname, &v5) < 0 )
v3 = newname;
else
LODWORD(v3) = printf("The file:%s is alredy exist.\n", newname);
}
else
{
LODWORD(v3) = printf("can not open: %s\n", name);
}
return (int)v3;
}

original_code:
char* create_temp_file(char* name)
{
 struct stat file_stat;
 strcpy(newname, name);
 if(opendir(name) == ((void *)0))
 {
  printf("can not open: %s\n", name);
  return;
 }
 if(newname[strlen(newname) - 1] != '/') strcat(newname, "/");
 strcat(newname, "XXXXXXXX.XXXX");
 int result = lstat(newname, &file_stat);
 if(result < 0)
  return newname;
 else
 printf("The file:%s is alredy exist.\n", newname);
}

canonical_original_code:
char *create_temp_file(char *name)
{
 struct stat file_stat;
 strcpy(newname, name);
 if(opendir(name) == ((void *)0))
 {
  printf("can not open: %s\n", name);
  return;
 }
 if(newname[strlen(newname) - 1] != '/') strcat(newname, "/");
 strcat(newname, "XXXXXXXX.XXXX");
 int result = lstat(newname, &file_stat);
 if(result < 0)
  return newname;
 else
 printf("The file:%s is alredy exist.\n", newname);
}

variable_types:
name
char *

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:
unsigned __int64 __fastcall xad_fill_buf(_BYTE *a1, unsigned __int64 a2, _BYTE *a3)
{
unsigned __int64 result; // rax

result = 0xAAAAAAAAAAAAAAABLL * a2;
if ( a2 > 2 )
{
result = 0LL;
do
{
*a3 = *a1;
a3[1] = a1[1];
a3 += 3;
*(a3 - 1) = a1[2];
++result;
}
while ( result < a2 / 3 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(_BYTE *a1, unsigned __int64 a2, _BYTE *a3)
{
unsigned __int64 result; // rax

result = 0xAAAAAAAAAAAAAAABLL * a2;
if ( a2 > 2 )
{
result = 0LL;
do
{
*a3 = *a1;
a3[1] = a1[1];
a3 += 3;
*(a3 - 1) = a1[2];
++result;
}
while ( result < a2 / 3 );
}
return result;
}

original_code:
void xad_fill_buf(char pattern[3], unsigned long bufsize, char *buf)
{
    int loop;
    for (loop = 0; loop < (bufsize / 3); loop++)
   {
 *buf++ = pattern[0];
 *buf++ = pattern[1];
 *buf++ = pattern[2];
    }
}

variable_types:
pattern
char [3]

variable_types:
bufsize
unsigned long

variable_types: loop int


help

decompiled_code:
void __noreturn help()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-l(1|2) standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}

canonical_decompiled_code:
void __noreturn func12()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-l(1|2) standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}

original_code:
void help ()
{
    printf("Syntax: %s [-svh] file1 file2 etc.\n\n", parameter);
    printf("\t-l(1|2)  standard name\n");
    printf("\t-v  is verbose mode.\n");
    printf("Default is fast mode (fast writes 0).\n");
    exit(1);
}

canonical_original_code:
void help()
{
    printf("Syntax: %s [-svh] file1 file2 etc.\n\n", parameter);
    printf("\t-l(1|2)  standard name\n");
    printf("\t-v  is verbose mode.\n");
    printf("Default is fast mode (fast writes 0).\n");
    exit(1);
}


decompiled_code:
__int64 __fastcall decide_level(const char *a1)
{
bool v1; // zf
__int64 result; // rax

v1 = strcmp(a1, "2") == 0;
result = (unsigned int)v1 + 1;
level = v1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(const char *a1)
{
bool v1; // zf
__int64 result; // rax

v1 = strcmp(a1, "2") == 0;
result = (unsigned int)v1 + 1;
level = v1 + 1;
return result;
}

original_code:
int decide_level(char* standard_name)
{
 if(strcmp(standard_name, "2") == 0) level = 2;
 else level = 1;
 return level;
}

canonical_original_code:
int decide_level(char *standard_name)
{
 if(strcmp(standard_name, "2") == 0) level = 2;
 else level = 1;
 return level;
}


decompiled_code:
void standard_erase()
{
;
}

canonical_decompiled_code:
void func14()
{
;
}

original_code:
void standard_erase(char* pathname)
{
}

canonical_original_code:
void standard_erase(char *pathname)
{
}

variable_types:
pathname
char *


decompiled_code:
void __noreturn help()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-s standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}

canonical_decompiled_code:
void __noreturn func13()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-s standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}


canonical_decompiled_code:
_BOOL8 __fastcall func18(const char *a1)
{
return access(a1, 0) >= 0;
}


canonical_decompiled_code:
void __noreturn func11()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-s standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}


canonical_decompiled_code:
_BOOL8 __fastcall func16(const char *a1)
{
return access(a1, 0) >= 0;
}


canonical_decompiled_code:
void __noreturn func10()
{
printf("Syntax: %s [-svh] file1 file2 etc.\n\n", (const char *)parameter);
puts("\t-s standard name");
puts("\t-v is verbose mode.");
puts("Default is fast mode (fast writes 0).");
exit(1);
}


canonical_decompiled_code:
_BOOL8 __fastcall func14(const char *a1)
{
return access(a1, 0) >= 0;
}


decompiled_code:
__int64 __fastcall show_column_number(__int64 a1)
{
int v1; // ebx
int v3; // ebx

if ( *(int *)(a1 + 4) > 10 )
{
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v3 = 1;
do
{
ft_putchar((unsigned int)(char)(v3 / 10 % 10 + 48));
++v3;
}
while ( *(_DWORD *)(a1 + 4) >= v3 );
}
ft_putchar(10LL);
}
ft_putstr(" ");
if ( *(int *)(a1 + 4) > 0 )
{
v1 = 1;
do
{
ft_putchar((unsigned int)(char)(v1 % 10 + 48));
++v1;
}
while ( *(_DWORD *)(a1 + 4) >= v1 );
}
return ft_putchar(10LL);
}

original_code:
void show_column_number(t_size *size)
{
 int i;
 i = 1;
 if (size->l > 10)
 {
  ft_putstr("    ");
  while (i <= size->l)
  {
   ft_putchar('0' + (i / 10) % 10);
   i++;
  }
  ft_putchar('\n');
  i = 1;
 }
 ft_putstr("    ");
 while (i <= size->l)
 {
  ft_putchar('0' + i % 10);
  i++;
 }
 ft_putchar('\n');
}

canonical_original_code:
void show_column_number(struct s_size *size)
{
 int i;
 i = 1;
 if (size->l > 10)
 {
  ft_putstr("    ");
  while (i <= size->l)
  {
   ft_putchar('0' + (i / 10) % 10);
   i++;
  }
  ft_putchar('\n');
  i = 1;
 }
 ft_putstr("    ");
 while (i <= size->l)
 {
  ft_putchar('0' + i % 10);
  i++;
 }
 ft_putchar('\n');
}

variable_types: struct s_size { int h; int l; int m; } *

variable_types: i int


decompiled_code:
__int64 __fastcall show_carac(char a1, int a2, int a3)
{
__int64 v4; // rax
char v5; // dl
int v6; // ecx
int v8; // eax
__int64 v9; // rcx
char *v10; // rbx
__int64 v11; // rcx
char *v12; // rbx

v4 = 0LL;
v5 = 45;
while ( 1 )
{
v6 = v4;
if ( a1 == v5 )
break;
++v4;
v5 = g_show[16 * v4];
if ( !v5 )
{
if ( a2 == 1 )
ft_putstr("\x1B[47m");
ft_putchar(32LL);
return ft_putstr("\x1B[0m");
}
}
v8 = v4 + 14;
if ( a2 == 1 )
v6 = v8;
if ( a3 )
{
if ( a3 == 1 )
{
v11 = 2LL * v6;
v12 = (char *)&g_finish + v11 * 8;
ft_putstr((&off_4E8)[v11]);
ft_putchar((unsigned int)v12[1]);
}
}
else
{
v9 = 2LL * v6;
v10 = &g_show[v9 * 8];
ft_putstr((&off_5C8)[v9]);
ft_putchar((unsigned int)v10[1]);
}
return ft_putstr("\x1B[0m");
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, int a2, int a3)
{
__int64 v4; // rax
char v5; // dl
int v6; // ecx
int v8; // eax
__int64 v9; // rcx
char *v10; // rbx
__int64 v11; // rcx
char *v12; // rbx

v4 = 0LL;
v5 = 45;
while ( 1 )
{
v6 = v4;
if ( a1 == v5 )
break;
++v4;
v5 = g_show[16 * v4];
if ( !v5 )
{
if ( a2 == 1 )
ft_putstr("\x1B[47m");
ft_putchar(32LL);
return ft_putstr("\x1B[0m");
}
}
v8 = v4 + 14;
if ( a2 == 1 )
v6 = v8;
if ( a3 )
{
if ( a3 == 1 )
{
v11 = 2LL * v6;
v12 = (char *)&g_finish + v11 * 8;
ft_putstr((&off_4E8)[v11]);
ft_putchar((unsigned int)v12[1]);
}
}
else
{
v9 = 2LL * v6;
v10 = &g_show[v9 * 8];
ft_putstr((&off_5C8)[v9]);
ft_putchar((unsigned int)v10[1]);
}
return ft_putstr("\x1B[0m");
}

original_code:
void show_carac(char c, int r, int f)
{
 int i;
 i = 0;
 while (g_show[i].c)
    {
  if (g_show[i].c == c)
  {
   if (r == 1)
    i += 14;
   if (f == 0)
   {
    ft_putstr(g_show[i].color);
    ft_putchar(g_show[i].show_c);
   }
   if (f == 1)
   {
    ft_putstr(g_finish[i].color);
    ft_putchar(g_finish[i].show_c);
   }
   ft_putstr("\033[0m");
   return ;
  }
  i++;
 }
 if (g_show[i].c == 0)
 {
  if (r == 1)
   ft_putstr("\033[47m");
  ft_putchar(' ');
  ft_putstr("\033[0m");
    }
}

variable_types: c char

variable_types: r int

variable_types: f int


decompiled_code:
__int64 __fastcall show_beg_of_line(int a1)
{
__int64 result; // rax

ft_putnbr((unsigned int)(char)a1);
if ( a1 > 9 )
result = ft_putstr(" |");
else
result = ft_putstr(" |");
return result;
}

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

ft_putnbr((unsigned int)(char)a1);
if ( a1 > 9 )
result = ft_putstr(" |");
else
result = ft_putstr(" |");
return result;
}

original_code:
void show_beg_of_line(int line)
{
 ft_putnbr(line);
 if (line < 10)
  ft_putstr("  |");
 else
  ft_putstr(" |");
}

variable_types: line int


decompiled_code:
__int64 __fastcall show_border_line(__int64 a1, char a2)
{
int v2; // ebx

ft_putstr(" *");
ft_putstr("\x1B[1;37m");
if ( *(int *)(a1 + 4) > 0 )
{
v2 = 0;
do
{
ft_putchar((unsigned int)a2);
++v2;
}
while ( *(_DWORD *)(a1 + 4) > v2 );
}
ft_putstr("\x1B[0m");
return ft_putstr("*\n");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char a2)
{
int v2; // ebx

ft_putstr(" *");
ft_putstr("\x1B[1;37m");
if ( *(int *)(a1 + 4) > 0 )
{
v2 = 0;
do
{
ft_putchar((unsigned int)a2);
++v2;
}
while ( *(_DWORD *)(a1 + 4) > v2 );
}
ft_putstr("\x1B[0m");
return ft_putstr("*\n");
}

original_code:
void show_border_line(t_size *size, char c)
{
  int i;
  i = 0;
  ft_putstr("   *");
  ft_putstr("\033[1;37m");
  while (i < size->l)
    {
      ft_putchar(c);
      i++;
    }
  ft_putstr("\033[0m");
  ft_putstr("*\n");
}

canonical_original_code:
void show_border_line(struct s_size *size, char c)
{
  int i;
  i = 0;
  ft_putstr("   *");
  ft_putstr("\033[1;37m");
  while (i < size->l)
    {
      ft_putchar(c);
      i++;
    }
  ft_putstr("\033[0m");
  ft_putstr("*\n");
}


decompiled_code:
__int64 __fastcall show_tab(_QWORD *a1, int *a2, _DWORD *a3)
{
int v5; // er12
__int64 v6; // rbx

show_column_number((__int64)a2);
show_border_line((__int64)a2, 95);
if ( *a2 > 0 )
{
v5 = 0;
do
{
show_beg_of_line(v5 + 1);
if ( a2[1] > 0 )
{
v6 = 0LL;
do
{
if ( *a3 == v5 && a3[1] == (_DWORD)v6 )
show_carac(*(_BYTE *)(*a1 + v6), 1, 0);
else
show_carac(*(_BYTE *)(*a1 + v6), 0, 0);
++v6;
}
while ( a2[1] > (int)v6 );
}
ft_putstr("|\n");
++v5;
++a1;
}
while ( *a2 > v5 );
}
return show_border_line((__int64)a2, 45);
}

original_code:
void show_tab(char **map, t_size *size, t_cursor *cursor)
{
 int i;
 int j;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
 {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   if (cursor->x == i && cursor->y == j)
    show_carac(map[i][j], 1 , 0);
   else
    show_carac(map[i][j], 0, 0);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
}

canonical_original_code:
void show_tab(char **map, struct s_size *size, struct s_cursor *cursor)
{
 int i;
 int j;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
 {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   if (cursor->x == i && cursor->y == j)
    show_carac(map[i][j], 1 , 0);
   else
    show_carac(map[i][j], 0, 0);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
}

variable_types:
map
char **

variable_types:
cursor
struct s_cursor { int x; int y; } *

variable_types: j int

user_defined_types:
struct s_size { int h; int l; int m; }
struct s_cursor { int x; int y; }


decompiled_code:
__int64 __fastcall show_finish(_QWORD *a1, int *a2, int a3)
{
int v5; // er13
__int64 v6; // rbx
time_t timer; // [rsp+8h] [rbp-30h] BYREF

timer = time(&timer);
ft_putstr("\x1B[H\x1B[2J");
show_column_number((__int64)a2);
show_border_line((__int64)a2, 95);
if ( *a2 > 0 )
{
v5 = 0;
do
{
show_beg_of_line(++v5);
if ( a2[1] > 0 )
{
v6 = 0LL;
do
show_carac(*(_BYTE *)(*a1 + v6++), 0, 1);
while ( a2[1] > (int)v6 );
}
ft_putstr("|\n");
++a1;
}
while ( v5 < *a2 );
}
show_border_line((__int64)a2, 45);
if ( a3 )
ft_putstr_color("You lose the GAME... 2 times!\n", "\x1B[31m");
else
ft_putstr_color("You win the GAME!\n\n", "\x1B[32m");
ft_putstr("Made By Jchichep alias Bueno\n");
return quit();
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, int *a2, int a3)
{
int v5; // er13
__int64 v6; // rbx
time_t timer; // [rsp+8h] [rbp-30h] BYREF

timer = time(&timer);
ft_putstr("\x1B[H\x1B[2J");
func0((__int64)a2);
func3((__int64)a2, 95);
if ( *a2 > 0 )
{
v5 = 0;
do
{
func2(++v5);
if ( a2[1] > 0 )
{
v6 = 0LL;
do
func1(*(_BYTE *)(*a1 + v6++), 0, 1);
while ( a2[1] > (int)v6 );
}
ft_putstr("|\n");
++a1;
}
while ( v5 < *a2 );
}
func3((__int64)a2, 45);
if ( a3 )
ft_putstr_color("You lose the GAME... 2 times!\n", "\x1B[31m");
else
ft_putstr_color("You win the GAME!\n\n", "\x1B[32m");
ft_putstr("Made By Jchichep alias Bueno\n");
return quit();
}

original_code:
void show_finish(char **map, t_size *size, int win, time_t *t)
{
 int i;
 int j;
 time_t res;
 double timer;
 res = time(&res);
 timer = difftime(res, *t);
 timer = timer;
 ft_putstr("\033[H\033[2J");
 win = win;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
    {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   show_carac(map[i][j], 0, 1);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
 if (win == 0)
 {
  ft_putstr_color("You win the GAME!\n\n", "\033[32m");
 }
 else
  ft_putstr_color("You lose the GAME... 2 times!\n", "\033[31m");
 ft_putstr("Made By Jchichep alias Bueno\n");
 quit();
}

canonical_original_code:
void show_finish(char **map, struct s_size *size, int win, long *t)
{
 int i;
 int j;
 long res;
 double timer;
 res = time(&res);
 timer = difftime(res, *t);
 timer = timer;
 ft_putstr("\033[H\033[2J");
 win = win;
 i = 0;
 j = 0;
 show_column_number(size);
 show_border_line(size, '_');
 while (i < size->h)
    {
  show_beg_of_line(i + 1);
  while (j < size->l)
  {
   show_carac(map[i][j], 0, 1);
   j++;
  }
  ft_putstr("|\n");
  j = 0;
  i++;
 }
 show_border_line(size, '-');
 if (win == 0)
 {
  ft_putstr_color("You win the GAME!\n\n", "\033[32m");
 }
 else
  ft_putstr_color("You lose the GAME... 2 times!\n", "\033[31m");
 ft_putstr("Made By Jchichep alias Bueno\n");
 quit();
}

variable_types: win int

variable_types:
t
long *

variable_types: res long

variable_types: timer double


original_code:
void easy_level()
{
 t_size size;
 size.h = 10;
 size.l = 10;
 size.m = 10;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void easy_level()
{
 struct s_size size;
 size.h = 10;
 size.l = 10;
 size.m = 10;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

variable_types:
size
struct s_size { int h; int l; int m; }


original_code:
void med_level()
{
 t_size size;
 size.h = 16;
 size.l = 16;
 size.m = 40;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void med_level()
{
 struct s_size size;
 size.h = 16;
 size.l = 16;
 size.m = 40;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}


original_code:
void hard_level()
{
 t_size size;
 size.h = 16;
 size.l = 30;
 size.m = 99;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void hard_level()
{
 struct s_size size;
 size.h = 16;
 size.l = 30;
 size.m = 99;
 ft_putstr("\033[?25l\n");
 start_game(&size);
}


decompiled_code:
_BOOL8 __fastcall verify_arg(_DWORD *a1)
{
_BOOL4 v1; // edx
int v2; // ecx

v1 = 0;
if ( (unsigned int)(*a1 - 1) <= 0x27 )
{
v2 = a1[1];
if ( (unsigned int)(v2 - 1) <= 0x4F )
v1 = a1[2] < v2 * *a1;
}
return v1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_DWORD *a1)
{
_BOOL4 v1; // edx
int v2; // ecx

v1 = 0;
if ( (unsigned int)(*a1 - 1) <= 0x27 )
{
v2 = a1[1];
if ( (unsigned int)(v2 - 1) <= 0x4F )
v1 = a1[2] < v2 * *a1;
}
return v1;
}

original_code:
int verify_arg(t_size *size)
{
 if (size->h < 1 || size->h > 40)
  return (0);
 if (size->l < 1 || size->l > 80)
  return (0);
 if (size->m >= size->h * size->l)
  return (0);
 return (1);
}

canonical_original_code:
int verify_arg(struct s_size *size)
{
 if (size->h < 1 || size->h > 40)
  return (0);
 if (size->l < 1 || size->l > 80)
  return (0);
 if (size->m >= size->h * size->l)
  return (0);
 return (1);
}


decompiled_code:
__int64 custom_level()
{
void *v0; // rbx
_BOOL4 v1; // eax
int v3[5]; // [rsp+4h] [rbp-14h] BYREF

unset_raw_mode();
v0 = malloc(0x400uLL);
while ( 1 )
{
ft_bzero(v0, 1024LL);
ft_putstr("Quelle hauteur? (maximum 40)\n");
read(0, v0, 0x3FFuLL);
v3[0] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Quelle longueur? (maximum 80)\n");
read(0, v0, 0x3FFuLL);
v3[1] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
read(0, v0, 0x3FFuLL);
v3[2] = ft_atoi(v0);
v1 = verify_arg(v3);
if ( v1 )
break;
if ( !v1 )
ft_putstr("Valeur incorrecte \n");
}
set_raw_mode();
ft_putstr("\x1B[?25l\n");
return start_game(v3);
}

canonical_decompiled_code:
__int64 func4()
{
void *v0; // rbx
_BOOL4 v1; // eax
int v3[5]; // [rsp+4h] [rbp-14h] BYREF

unset_raw_mode();
v0 = malloc(0x400uLL);
while ( 1 )
{
ft_bzero(v0, 1024LL);
ft_putstr("Quelle hauteur? (maximum 40)\n");
read(0, v0, 0x3FFuLL);
v3[0] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Quelle longueur? (maximum 80)\n");
read(0, v0, 0x3FFuLL);
v3[1] = ft_atoi(v0);
ft_bzero(v0, 1023LL);
ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
read(0, v0, 0x3FFuLL);
v3[2] = ft_atoi(v0);
v1 = func3(v3);
if ( v1 )
break;
if ( !v1 )
ft_putstr("Valeur incorrecte \n");
}
set_raw_mode();
ft_putstr("\x1B[?25l\n");
return start_game(v3);
}

original_code:
void custom_level()
{
 t_size size;
 int verify;
 char *str;
 unset_raw_mode();
 str = malloc(sizeof(*str) * 1024);
 verify = -1;
 while (verify != 1)
 {
  if (verify == 0)
   ft_putstr("Valeur incorrecte \n");
  ft_bzero(str, 1024);
  ft_putstr("Quelle hauteur? (maximum 40)\n");
  read(0, str, 1023);
  size.h = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Quelle longueur? (maximum 80)\n");
  read(0, str, 1023);
  size.l = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
  read(0, str, 1023);
  size.m = ft_atoi(str);
  verify = verify_arg(&size);
 }
 set_raw_mode();
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

canonical_original_code:
void custom_level()
{
 struct s_size size;
 int verify;
 char *str;
 unset_raw_mode();
 str = malloc(sizeof(*str) * 1024);
 verify = -1;
 while (verify != 1)
 {
  if (verify == 0)
   ft_putstr("Valeur incorrecte \n");
  ft_bzero(str, 1024);
  ft_putstr("Quelle hauteur? (maximum 40)\n");
  read(0, str, 1023);
  size.h = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Quelle longueur? (maximum 80)\n");
  read(0, str, 1023);
  size.l = ft_atoi(str);
  ft_bzero(str, 1023);
  ft_putstr("Combien de mines? (maximum (hauteur * longueur) - 1\n");
  read(0, str, 1023);
  size.m = ft_atoi(str);
  verify = verify_arg(&size);
 }
 set_raw_mode();
 ft_putstr("\033[?25l\n");
 start_game(&size);
}

variable_types: verify int

variable_types:
str
char *


quit

decompiled_code:
void __noreturn quit()
{
unset_raw_mode();
ft_putstr("\x1B[?12l\x1B[?25h");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
unset_raw_mode();
ft_putstr("\x1B[?12l\x1B[?25h");
exit(0);
}


main

canonical_name: main

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

_sysv_signal(2, (__sighandler_t)&catch_sigint);
v3 = show_menu();
get_option(v3, &g_option);
unset_raw_mode();
return 0;
}

original_code:
int main()
{
 int lvl;
 signal(2, catch_sigint);
 lvl = show_menu();
 get_option(lvl, g_option);
 unset_raw_mode();
 return (0);
}

variable_types: lvl int


original_code:
int check_case(char **map, t_cursor *cursor, t_size *size)
{
 int ret;
 int i;
 int j;
 ret = 0;
 i = cursor->x - 1;
 while (i < cursor->x + 2)
 {
  j = cursor->y - 1;
  while (j < cursor->y + 2)
  {
   if (!(i < 0 || j < 0 || i >= size->h || j >= size->l)
    && (map[i][j] == 'X' || map[i][j] == 'F'))
    ret++;
   j++;
  }
  i++;
 }
 return (ret);
}

canonical_original_code:
int check_case(char **map, struct s_cursor *cursor, struct s_size *size)
{
 int ret;
 int i;
 int j;
 ret = 0;
 i = cursor->x - 1;
 while (i < cursor->x + 2)
 {
  j = cursor->y - 1;
  while (j < cursor->y + 2)
  {
   if (!(i < 0 || j < 0 || i >= size->h || j >= size->l)
    && (map[i][j] == 'X' || map[i][j] == 'F'))
    ret++;
   j++;
  }
  i++;
 }
 return (ret);
}

variable_types: struct s_size { int h; int l; int m; } *

variable_types: ret int

user_defined_types:
struct s_cursor { int x; int y; }
struct s_size { int h; int l; int m; }


finish

original_code:
int finish(char **map, t_size *size)
{
 int i;
 int j;
 i = 0;
 j = 0;
 while (i < size->h)
 {
  while (j < size->l)
  {
   if (map[i][j] == ' ' || map[i][j] == 'W')
    return (0);
   j++;
  }
  j = 0;
  i++;
 }
 return (1);
}

canonical_original_code:
int finish(char **map, struct s_size *size)
{
 int i;
 int j;
 i = 0;
 j = 0;
 while (i < size->h)
 {
  while (j < size->l)
  {
   if (map[i][j] == ' ' || map[i][j] == 'W')
    return (0);
   j++;
  }
  j = 0;
  i++;
 }
 return (1);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ft_atoi(_BYTE *a1)
{
char *v1; // rdi
char v2; // dl
__int64 result; // rax

v1 = &a1[*a1 == 45];
v2 = *v1;
if ( (unsigned __int8)(*v1 - 48) > 9u )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(10 * result - v2 + 48);
v2 = *++v1;
}
while ( (unsigned __int8)(v2 - 48) <= 9u );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
char *v1; // rdi
char v2; // dl
__int64 result; // rax

v1 = &a1[*a1 == 45];
v2 = *v1;
if ( (unsigned __int8)(*v1 - 48) > 9u )
return 0LL;
LODWORD(result) = 0;
do
{
result = (unsigned int)(10 * result - v2 + 48);
v2 = *++v1;
}
while ( (unsigned __int8)(v2 - 48) <= 9u );
return result;
}

original_code:
int ft_atoi(char *str)
{
 int ret;
 int i;
 int neg;
 i = 0;
 if (str[0] == '-')
 {
  neg = 1;
  i = 1;
 }
 ret = 0;
 while (str[i] >= '0' && str[i] <= '9')
 {
  if (neg == 1)
   ret = ret * 10 - str[i] + '0';
  else
   ret = ret * 10 + str[i] - '0';
  i++;
 }
 return (ret);
}

variable_types: neg int


decompiled_code:
ssize_t __fastcall ft_putchar(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func1(char a1)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a1;
return write(1, buf, 1uLL);
}

original_code:
void ft_putchar(char c)
{
 (void)write(1, &c, 1);
}

variable_types: c char


decompiled_code:
void __fastcall ft_bzero(_BYTE *a1, int a2)
{
_BYTE *v2; // rax

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

original_code:
void ft_bzero(char *str, int nb)
{
 int i;
 i = 0;
 while (i < nb)
 {
  str[i++] = 0;
 }
}

variable_types: nb int


decompiled_code:
__int64 __fastcall ft_strlen(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

if ( !*a1 )
return 0;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

if ( !*a1 )
return 0;
v1 = 1LL;
do
v2 = v1++;
while ( a1[v1 - 1] );
return v2;
}

original_code:
int ft_strlen(char *str)
{
 int i;
 i = 0;
 while (str[i])
 {
  i++;
 }
 return (i);
}


decompiled_code:
ssize_t __fastcall ft_putstr(void *buf)
{
int v1; // eax

v1 = ft_strlen(buf);
return write(1, buf, v1);
}

canonical_decompiled_code:
ssize_t __fastcall func4(void *buf)
{
int v1; // eax

v1 = func3(buf);
return write(1, buf, v1);
}

original_code:
void ft_putstr(char *str)
{
 (void)write(1, str, ft_strlen(str));
}


decompiled_code:
ssize_t __fastcall ft_putnbr(int a1)
{
int v1; // ebx

v1 = a1;
if ( a1 == 0x80000000 )
{
ft_putstr("-2147483648");
}
else if ( a1 >= 0 )
{
goto LABEL_3;
}
ft_putchar(45);
v1 = -a1;
LABEL_3:
if ( v1 > 9 )
ft_putnbr((unsigned int)(v1 / 10));
return ft_putchar((unsigned __int8)(v1 % 10) + 48);
}


decompiled_code:
ssize_t __fastcall ft_putstr_color(void *buf, void *a2)
{
ft_putstr(a2);
ft_putstr(buf);
return ft_putstr("\x1B[0m");
}

canonical_decompiled_code:
ssize_t __fastcall func6(void *buf, void *a2)
{
func4(a2);
func4(buf);
return func4("\x1B[0m");
}

original_code:
void ft_putstr_color(char *str, char *color)
{
 ft_putstr(color);
 ft_putstr(str);
 ft_putstr("\033[0m");
}

variable_types:
color
char *


original_code:
int check_map(char **map, t_cursor *cursor, t_size *size)
{
 char nb;
 t_cursor temp;
 temp.x = cursor->x - 1;
 temp.y = cursor->y - 1;
 if (cursor->x < 0 || cursor->y < 0
  || cursor->x >= size->h || cursor->y >= size->l)
  return (0);
 if (map[cursor->x][cursor->y] != ' ' && map[cursor->x][cursor->y] != 'X')
  return (0);
 nb = '0';
 if (map[cursor->x][cursor->y] == 'X')
 {
  map[cursor->x][cursor->y] = 'B';
  return (1);
 }
 nb += check_case(map, cursor, size);
 map[cursor->x][cursor->y] = nb;
 if (nb == '0')
 {
  map[cursor->x][cursor->y] = '-';
  while (temp.x < cursor->x + 2)
  {
   temp.y = cursor->y - 1;
   while (temp.y < cursor->y + 2)
   {
    check_map(map, &temp, size);
    temp.y++;
   }
   temp.x++;
  }
 }
 return (0);
}

canonical_original_code:
int check_map(char **map, struct s_cursor *cursor, struct s_size *size)
{
 char nb;
 struct s_cursor temp;
 temp.x = cursor->x - 1;
 temp.y = cursor->y - 1;
 if (cursor->x < 0 || cursor->y < 0
  || cursor->x >= size->h || cursor->y >= size->l)
  return (0);
 if (map[cursor->x][cursor->y] != ' ' && map[cursor->x][cursor->y] != 'X')
  return (0);
 nb = '0';
 if (map[cursor->x][cursor->y] == 'X')
 {
  map[cursor->x][cursor->y] = 'B';
  return (1);
 }
 nb += check_case(map, cursor, size);
 map[cursor->x][cursor->y] = nb;
 if (nb == '0')
 {
  map[cursor->x][cursor->y] = '-';
  while (temp.x < cursor->x + 2)
  {
   temp.y = cursor->y - 1;
   while (temp.y < cursor->y + 2)
   {
    check_map(map, &temp, size);
    temp.y++;
   }
   temp.x++;
  }
 }
 return (0);
}

variable_types:
map
char **

variable_types:
cursor
struct s_cursor { int x; int y; } *

variable_types: nb char

variable_types:
temp
struct s_cursor { int x; int y; }


move

original_code:
int move(int key, t_cursor *cursor, t_size *size, char **map)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (i == 4 && g_option[i].key == key)
   quit();
  else if (i == 5 && g_option[i].key == key)
   flag(map, cursor);
  else if (g_option[i].key == key)
  {
   g_option[i].ptr_func(cursor, size);
   return (0);
  }
  i++;
 }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

canonical_original_code:
int move(int key, struct s_cursor *cursor, struct s_size *size, char **map)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (i == 4 && g_option[i].key == key)
   quit();
  else if (i == 5 && g_option[i].key == key)
   flag(map, cursor);
  else if (g_option[i].key == key)
  {
   g_option[i].ptr_func(cursor, size);
   return (0);
  }
  i++;
 }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

variable_types: key int


decompiled_code:
void __fastcall set_mines(__int64 a1, _DWORD *a2)
{
unsigned int v2; // eax
int v3; // ebp
int v4; // er13
_BYTE *v5; // rdx

v2 = time(0LL);
srand(v2);
v3 = 0;
while ( v3 < a2[2] )
{
v4 = rand() % *a2;
v5 = (_BYTE *)(*(_QWORD *)(a1 + 8LL * v4) + rand() % a2[1]);
if ( *v5 == 32 )
{
*v5 = 88;
++v3;
}
}
}

original_code:
void set_mines(char **map, t_size *size)
{
 int mh;
 int mw;
 int monboard;
 srand(time(0));
 monboard = 0;
 while (monboard < size->m)
 {
  mh = rand() % size->h;
  mw = rand() % size->l;
  if (map[mh][mw] == ' ')
  {
   map[mh][mw] = 'X';
   monboard++;
  }
 }
}

canonical_original_code:
void set_mines(char **map, struct s_size *size)
{
 int mh;
 int mw;
 int monboard;
 srand(time(0));
 monboard = 0;
 while (monboard < size->m)
 {
  mh = rand() % size->h;
  mw = rand() % size->l;
  if (map[mh][mw] == ' ')
  {
   map[mh][mw] = 'X';
   monboard++;
  }
 }
}

variable_types: mh int

variable_types: mw int

variable_types: monboard int


original_code:
void start_game(t_size *size)
{
 char **tab;
 t_cursor cursor;
 int key;
 int setmines;
 time_t t;
 time(&t);
 setmines = 0;
 cursor.x = 0;
 cursor.y = 0;
 tab = setmap(size);
 while (finish(tab, size) == 0)
 {
  ft_putstr("\033[H\033[2J");
  show_tab(tab, size, &cursor);
  key = get_key();
  if (key == '\n')
  {
   if (setmines == 0)
   {
    tab[cursor.x][cursor.y] = '0';
    set_mines(tab, size);
    tab[cursor.x][cursor.y] = ' ';
    setmines = 1;
   }
   check_map(tab, &cursor, size);
   if (tab[cursor.x][cursor.y] == 'B')
    show_finish(tab, size, 1, &t);
  }
  move(key, &cursor, size, tab);
 }
 show_finish(tab, size, 0, &t);
}

canonical_original_code:
void start_game(struct s_size *size)
{
 char **tab;
 struct s_cursor cursor;
 int key;
 int setmines;
 long t;
 time(&t);
 setmines = 0;
 cursor.x = 0;
 cursor.y = 0;
 tab = setmap(size);
 while (finish(tab, size) == 0)
 {
  ft_putstr("\033[H\033[2J");
  show_tab(tab, size, &cursor);
  key = get_key();
  if (key == '\n')
  {
   if (setmines == 0)
   {
    tab[cursor.x][cursor.y] = '0';
    set_mines(tab, size);
    tab[cursor.x][cursor.y] = ' ';
    setmines = 1;
   }
   check_map(tab, &cursor, size);
   if (tab[cursor.x][cursor.y] == 'B')
    show_finish(tab, size, 1, &t);
  }
  move(key, &cursor, size, tab);
 }
 show_finish(tab, size, 0, &t);
}

variable_types:
tab
char **

variable_types:
cursor
struct s_cursor { int x; int y; }

variable_types: setmines int

variable_types: t long


decompiled_code:
void catch_sigint()
{
ft_putstr("Please use q to quit the program\n");
_sysv_signal(2, catch_sigint);
}

canonical_decompiled_code:
void func0()
{
ft_putstr("Please use q to quit the program\n");
_sysv_signal(2, catch_sigint);
}

original_code:
void catch_sigint(int sig)
{
 sig = sig;
 ft_putstr("Please use q to quit the program\n");
 signal(2, catch_sigint);
}

variable_types: sig int


decompiled_code:
__int64 __fastcall get_option(int a1, void (**a2)(void))
{
int v2; // eax

v2 = *(_DWORD *)a2;
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
while ( a1 != v2 )
{
a2 += 2;
v2 = *(_DWORD *)a2;
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
}
a2[1]();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, void (**a2)(void))
{
int v2; // eax

v2 = *(_DWORD *)a2;
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
while ( a1 != v2 )
{
a2 += 2;
v2 = *(_DWORD *)a2;
if ( !*(_DWORD *)a2 )
return 0xFFFFFFFFLL;
}
a2[1]();
return 0LL;
}

original_code:
int get_option(int key, t_opt *g_option)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (g_option[i].key == key)
  {
   g_option[i].ptr_func();
   return (0);
  }
  i++;
    }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

canonical_original_code:
int get_option(int key, struct s_option *g_option)
{
 int i;
 i = 0;
 while (g_option[i].key)
 {
  if (g_option[i].key == key)
  {
   g_option[i].ptr_func();
   return (0);
  }
  i++;
    }
 if (g_option[i].key == 0)
 {
  return (-1);
 }
 return (0);
}

variable_types: struct s_option { int key; void (*ptr_func)(); } *

variable_types: i int


decompiled_code:
int unset_raw_mode()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag += 10;
v1.c_cc[6] = 1;
v1.c_cc[5] = 0;
return tcsetattr(0, 0, &v1);
}

canonical_decompiled_code:
int func2()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag += 10;
v1.c_cc[6] = 1;
v1.c_cc[5] = 0;
return tcsetattr(0, 0, &v1);
}

original_code:
void unset_raw_mode()
{
 struct termios t;
 tcgetattr(0, &t);
 t.c_lflag += 0000002;
 t.c_lflag += 0000010;
 t.c_cc[6] = 1;
 t.c_cc[5] = 0;
 tcsetattr(0, 0, &t);
}

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 set_raw_mode()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag &= 0xFFFFFFF5;
v1.c_cc[6] = 1;
v1.c_cc[5] = 0;
return tcsetattr(0, 0, &v1);
}

canonical_decompiled_code:
int func3()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &v1);
v1.c_lflag &= 0xFFFFFFF5;
v1.c_cc[6] = 1;
v1.c_cc[5] = 0;
return tcsetattr(0, 0, &v1);
}

original_code:
void set_raw_mode()
{
 struct termios t;
 tcgetattr(0, &t);
 t.c_lflag &= ~0000002;
 t.c_lflag &= ~0000010;
 t.c_cc[6] = 1;
 t.c_cc[5] = 0;
 tcsetattr(0, 0, &t);
}

user_defined_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:
__int64 get_key()
{
char v0; // al
char buf; // [rsp+Dh] [rbp-Bh] BYREF
char v3; // [rsp+Eh] [rbp-Ah] BYREF
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, &buf, 1uLL);
v0 = buf;
if ( buf != 27 )
return (unsigned int)v0;
read(0, &v3, 1uLL);
if ( v3 == 91 )
read(0, v4, 1uLL);
v0 = buf;
if ( buf != 27 || v3 != 91 )
return (unsigned int)v0;
if ( v4[0] == 66 )
return 256LL;
if ( v4[0] == 65 )
return 257LL;
if ( v4[0] != 68 )
{
if ( v4[0] == 67 )
return 259LL;
return (unsigned int)v0;
}
return 258LL;
}

canonical_decompiled_code:
__int64 func0()
{
char v0; // al
char buf; // [rsp+Dh] [rbp-Bh] BYREF
char v3; // [rsp+Eh] [rbp-Ah] BYREF
char v4[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, &buf, 1uLL);
v0 = buf;
if ( buf != 27 )
return (unsigned int)v0;
read(0, &v3, 1uLL);
if ( v3 == 91 )
read(0, v4, 1uLL);
v0 = buf;
if ( buf != 27 || v3 != 91 )
return (unsigned int)v0;
if ( v4[0] == 66 )
return 256LL;
if ( v4[0] == 65 )
return 257LL;
if ( v4[0] != 68 )
{
if ( v4[0] == 67 )
return 259LL;
return (unsigned int)v0;
}
return 258LL;
}

variable_types:
tab
char [3]


decompiled_code:
__int64 show_menu()
{
int v0; // eax

ft_putstr_color("Press 1 for easy\n", "\x1B[32m");
ft_putstr_color("Press 2 for medium\n", "\x1B[33m");
ft_putstr_color("Press 3 for hard\n", "\x1B[31m");
ft_putstr_color("Press 4 for custom\n", "\x1B[34m");
set_raw_mode();
do
v0 = get_key();
while ( (unsigned int)(v0 - 49) > 3 && v0 != 113 );
return (unsigned int)(v0 - 48);
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // eax

ft_putstr_color("Press 1 for easy\n", "\x1B[32m");
ft_putstr_color("Press 2 for medium\n", "\x1B[33m");
ft_putstr_color("Press 3 for hard\n", "\x1B[31m");
ft_putstr_color("Press 4 for custom\n", "\x1B[34m");
set_raw_mode();
do
v0 = func0();
while ( (unsigned int)(v0 - 49) > 3 && v0 != 113 );
return (unsigned int)(v0 - 48);
}

original_code:
int show_menu()
{
 int key;
 key = 0;
 ft_putstr_color("Press 1 for easy\n", "\033[32m");
 ft_putstr_color("Press 2 for medium\n", "\033[33m");
 ft_putstr_color("Press 3 for hard\n", "\033[31m");
 ft_putstr_color("Press 4 for custom\n", "\033[34m");
 set_raw_mode();
 while ((key < '1' || key > '4') && key != 'q')
    {
  key = get_key();
    }
 return (key - '0');
}


repo: kevinriehm/evt-black-box

decompiled_code:
__int64 is_alive()
{
int v0; // edi
FILE *v2; // rax

pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 258);
if ( pidfd < 0 )
pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 256);
pidfp = fdopen(pidfd, "w");
if ( pidfp )
{
v0 = pidfd;
if ( pidfd >= 0 )
return (unsigned int)flock(v0, 6) >> 31;
}
else
{
v2 = fdopen(pidfd, "r");
pidfp = v2;
v0 = pidfd;
if ( pidfd >= 0 && v2 )
return (unsigned int)flock(v0, 6) >> 31;
}
syslog(4, "cannot open PID file (%m)");
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // edi
FILE *v2; // rax

pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 258);
if ( pidfd < 0 )
pidfd = open("/var/opt/staevt.com/angel/.angeld.pid", 256);
pidfp = fdopen(pidfd, "w");
if ( pidfp )
{
v0 = pidfd;
if ( pidfd >= 0 )
return (unsigned int)flock(v0, 6) >> 31;
}
else
{
v2 = fdopen(pidfd, "r");
pidfp = v2;
v0 = pidfd;
if ( pidfd >= 0 && v2 )
return (unsigned int)flock(v0, 6) >> 31;
}
syslog(4, "cannot open PID file (%m)");
return 0LL;
}

original_code:
int is_alive() {
 pidfd = open("/var/opt/staevt.com/angel""/.angeld.pid",02 | 0400);
 if(pidfd < 0) pidfd = open("/var/opt/staevt.com/angel""/.angeld.pid",00 | 0400);
 pidfp = fdopen(pidfd,"w");
 if(!pidfp) pidfp = fdopen(pidfd,"r");
 if(pidfd < 0 || !pidfp) {
  syslog(4,"cannot open PID file (%m)");
  return 0;
 }
 return flock(pidfd,2 | 4) < 0;
}


decompiled_code:
__int64 __fastcall try_open(const char *a1, int a2)
{
int v2; // ebx
struct termios termios_p; // [rsp+0h] [rbp-50h] BYREF

v2 = open(a1, a2);
if ( v2 < 0 )
{
syslog(4, "cannot open '%s' (%m)", a1);
}
else
{
syslog(5, "opening '%s'", a1);
*(_QWORD *)&termios_p.c_iflag = 0LL;
*(_QWORD *)&termios_p.c_cflag = 0LL;
*(_QWORD *)&termios_p.c_line = 0LL;
*(_QWORD *)&termios_p.c_cc[7] = 0LL;
*(_QWORD *)&termios_p.c_cc[15] = 0LL;
*(_QWORD *)&termios_p.c_cc[23] = 0LL;
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0xDu);
cfsetispeed(&termios_p, 0xDu);
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
termios_p.c_iflag |= 1u;
tcsetattr(v2, 0, &termios_p);
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2)
{
int v2; // ebx
struct termios termios_p; // [rsp+0h] [rbp-50h] BYREF

v2 = open(a1, a2);
if ( v2 < 0 )
{
syslog(4, "cannot open '%s' (%m)", a1);
}
else
{
syslog(5, "opening '%s'", a1);
*(_QWORD *)&termios_p.c_iflag = 0LL;
*(_QWORD *)&termios_p.c_cflag = 0LL;
*(_QWORD *)&termios_p.c_line = 0LL;
*(_QWORD *)&termios_p.c_cc[7] = 0LL;
*(_QWORD *)&termios_p.c_cc[15] = 0LL;
*(_QWORD *)&termios_p.c_cc[23] = 0LL;
*(_QWORD *)&termios_p.c_cc[31] = 0LL;
termios_p.c_ospeed = 0;
cfsetospeed(&termios_p, 0xDu);
cfsetispeed(&termios_p, 0xDu);
*(_QWORD *)&termios_p.c_cflag |= 0x8000000030uLL;
termios_p.c_iflag |= 1u;
tcsetattr(v2, 0, &termios_p);
}
return (unsigned int)v2;
}

original_code:
int try_open(char *path, int flags) {
 int fd;
 struct termios serialios;
 fd = open(path,flags);
 if(fd < 0) syslog(4,"cannot open '%s' (%m)",path);
 else {
  syslog(5,"opening '%s'",path);
  memset(&serialios,0,sizeof(serialios));
  cfsetospeed(&serialios,0000015);
  cfsetispeed(&serialios,0000015);
  serialios.c_cflag |= 0000060;
  serialios.c_iflag |= 0000001;
  serialios.c_lflag |= 0000200;
  tcsetattr(fd,0,&serialios);
 }
 return fd;
}

variable_types:
path
char *

variable_types: flags int

variable_types: fd int


decompiled_code:
int find_serial()
{
int v0; // er12
__int64 v1; // rbx
int result; // eax
char dest[4112]; // [rsp+0h] [rbp-2090h] BYREF
char buf[4]; // [rsp+1010h] [rbp-1080h] BYREF
int v5; // [rsp+1014h] [rbp-107Ch]
char src[4104]; // [rsp+1020h] [rbp-1070h] BYREF
struct pollfd fds; // [rsp+2028h] [rbp-68h] BYREF
glob_t pglob; // [rsp+2030h] [rbp-60h] BYREF

v0 = inotify_add_watch(inotifyfd, "/dev", 0x100u);
syslog(5, "looking for serial device");
glob("/dev/ttyACM*", 0, 0LL, &pglob);
if ( pglob.gl_pathc )
{
v1 = 0LL;
while ( 1 )
{
result = try_open(pglob.gl_pathv[v1], 256);
serialfd = result;
if ( result >= 0 )
break;
if ( pglob.gl_pathc <= ++v1 )
goto LABEL_5;
}
}
else
{
LABEL_5:
globfree(&pglob);
syslog(6, "nothing there, using inotify");
fds.fd = inotifyfd;
fds.events = 1;
while ( serialfd < 0 )
{
poll(&fds, 1uLL, -1);
if ( (int)read(inotifyfd, buf, 0x1010uLL) < 0 )
{
syslog(4, "read error (%m)");
}
else if ( !((memcmp(src, "ttyACM", 6uLL) != 0) | v5 & 0x40000000) )
{
strcpy(dest, "/dev/");
strcat(dest, src);
serialfd = try_open(dest, 256);
}
}
result = inotify_rm_watch(inotifyfd, v0);
}
return result;
}

canonical_decompiled_code:
int func3()
{
int v0; // er12
__int64 v1; // rbx
int result; // eax
char dest[4112]; // [rsp+0h] [rbp-2090h] BYREF
char buf[4]; // [rsp+1010h] [rbp-1080h] BYREF
int v5; // [rsp+1014h] [rbp-107Ch]
char src[4104]; // [rsp+1020h] [rbp-1070h] BYREF
struct pollfd fds; // [rsp+2028h] [rbp-68h] BYREF
glob_t pglob; // [rsp+2030h] [rbp-60h] BYREF

v0 = inotify_add_watch(inotifyfd, "/dev", 0x100u);
syslog(5, "looking for serial device");
glob("/dev/ttyACM*", 0, 0LL, &pglob);
if ( pglob.gl_pathc )
{
v1 = 0LL;
while ( 1 )
{
result = func2(pglob.gl_pathv[v1], 256);
serialfd = result;
if ( result >= 0 )
break;
if ( pglob.gl_pathc <= ++v1 )
goto LABEL_5;
}
}
else
{
LABEL_5:
globfree(&pglob);
syslog(6, "nothing there, using inotify");
fds.fd = inotifyfd;
fds.events = 1;
while ( serialfd < 0 )
{
poll(&fds, 1uLL, -1);
if ( (int)read(inotifyfd, buf, 0x1010uLL) < 0 )
{
syslog(4, "read error (%m)");
}
else if ( !((memcmp(src, "ttyACM", 6uLL) != 0) | v5 & 0x40000000) )
{
strcpy(dest, "/dev/");
strcat(dest, src);
serialfd = func2(dest, 256);
}
}
result = inotify_rm_watch(inotifyfd, v0);
}
return result;
}

original_code:
void find_serial() {
 int wd;
 glob_t g;
 int nread, i;
 struct pollfd pollfd;
 struct inotify_event *e;
 char buf[sizeof(struct inotify_event) + 4096 + 1],
  path[4096 + 1];
 wd = inotify_add_watch(inotifyfd,"/dev",0x00000100);
 e = (struct inotify_event *) buf;
 syslog(5,"looking for serial device");
 glob("/dev/""ttyACM""*",0,((void *)0),&g);
 for(i = 0; i < g.gl_pathc; i++) {
  serialfd = try_open(g.gl_pathv[i],00 | 0400);
  if(serialfd >= 0) return;
 }
 globfree(&g);
 syslog(6,"nothing there, using inotify");
 pollfd.fd = inotifyfd;
 pollfd.events = 0x001;
 while(serialfd < 0) {
  poll(&pollfd,1,-1);
  nread = read(inotifyfd,e,sizeof(struct inotify_event)
   + 4096);
  if(nread < 0) syslog(4,"read error (%m)");
  else if(strncmp(e->name,"ttyACM",strlen("ttyACM")) == 0
    && !(e->mask & 0x40000000)) {
   strcpy(path,"/dev/");
   strcat(path,e->name);
   serialfd = try_open(path,00 | 0400);
  }
 }
 inotify_rm_watch(inotifyfd,wd);
}

canonical_original_code:
void find_serial() {
 int wd;
 struct glob_t g;
 int nread; int i;
 struct pollfd pollfd;
 struct inotify_event *e;
 char buf[-1]; char path[-1];
 wd = inotify_add_watch(inotifyfd,"/dev",0x00000100);
 e = (struct inotify_event *) buf;
 syslog(5,"looking for serial device");
 glob("/dev/""ttyACM""*",0,((void *)0),&g);
 for(i = 0; i < g.gl_pathc; i++) {
  serialfd = try_open(g.gl_pathv[i],00 | 0400);
  if(serialfd >= 0) return;
 }
 globfree(&g);
 syslog(6,"nothing there, using inotify");
 pollfd.fd = inotifyfd;
 pollfd.events = 0x001;
 while(serialfd < 0) {
  poll(&pollfd,1,-1);
  nread = read(inotifyfd,e,sizeof(struct inotify_event)
   + 4096);
  if(nread < 0) syslog(4,"read error (%m)");
  else if(strncmp(e->name,"ttyACM",strlen("ttyACM")) == 0
    && !(e->mask & 0x40000000)) {
   strcpy(path,"/dev/");
   strcat(path,e->name);
   serialfd = try_open(path,00 | 0400);
  }
 }
 inotify_rm_watch(inotifyfd,wd);
}

variable_types: wd int

variable_types: struct glob_t { unsigned long gl_pathc; char **gl_pathv; unsigned long gl_offs; int gl_flags; void (*gl_closedir)(void *); void *(*gl_readdir)(void *); void *(*gl_opendir)(char *); int (*gl_lstat)(char *, void *); int (*gl_stat)(char *, void *); }

variable_types: nread int

variable_types: struct pollfd { int fd; short events; short revents; }

variable_types: struct inotify_event { int wd; unsigned int mask; unsigned int cookie; unsigned int len; char name[]; } *

variable_types:
buf
char [-1]

variable_types:
path
char [-1]


decompiled_code:
void kill_daemon()
{
__pid_t pid; // [rsp+Ch] [rbp-4h] BYREF

if ( pidfp )
{
_isoc99_fscanf(pidfp, "%i", &pid);
if ( pid > 0 )
{
syslog(6, "sending SIGTERM to old instance");
kill(pid, 15);
}
}
else
{
syslog(4, "PID file not open");
}
}

canonical_decompiled_code:
void func4()
{
__pid_t pid; // [rsp+Ch] [rbp-4h] BYREF

if ( pidfp )
{
_isoc99_fscanf(pidfp, "%i", &pid);
if ( pid > 0 )
{
syslog(6, "sending SIGTERM to old instance");
kill(pid, 15);
}
}
else
{
syslog(4, "PID file not open");
}
}

original_code:
void kill_daemon() {
 pid_t pid;
 if(!pidfp) {
  syslog(4,"PID file not open");
  return;
 }
 fscanf(pidfp,"%i",&pid);
 if(pid > 0) {
  syslog(6,"sending SIGTERM to old instance");
  kill(pid,15);
 }
}

canonical_original_code:
void kill_daemon() {
 int pid;
 if(!pidfp) {
  syslog(4,"PID file not open");
  return;
 }
 fscanf(pidfp,"%i",&pid);
 if(pid > 0) {
  syslog(6,"sending SIGTERM to old instance");
  kill(pid,15);
 }
}

variable_types: pid int


decompiled_code:
int make_daemon()
{
__pid_t v0; // eax
unsigned int v1; // eax
int v2; // ebx

v0 = fork();
if ( v0 < 0 )
{
syslog(4, "cannot fork");
}
else if ( v0 > 0 )
{
exit(0);
}
syslog(6, "now operating as a daemon");
v1 = getpid();
v2 = fprintf(pidfp, "%i\n", v1);
if ( v2 <= 0 )
syslog(4, "cannot write PID to file (%m)");
ftruncate(pidfd, v2);
fflush(pidfp);
fsync(pidfd);
if ( setsid() < 0 )
syslog(4, "cannot set sid");
if ( chdir("/") < 0 )
syslog(4, "cannot change directory");
close(0);
close(1);
return close(2);
}

canonical_decompiled_code:
int func5()
{
__pid_t v0; // eax
unsigned int v1; // eax
int v2; // ebx

v0 = fork();
if ( v0 < 0 )
{
syslog(4, "cannot fork");
}
else if ( v0 > 0 )
{
exit(0);
}
syslog(6, "now operating as a daemon");
v1 = getpid();
v2 = fprintf(pidfp, "%i\n", v1);
if ( v2 <= 0 )
syslog(4, "cannot write PID to file (%m)");
ftruncate(pidfd, v2);
fflush(pidfp);
fsync(pidfd);
if ( setsid() < 0 )
syslog(4, "cannot set sid");
if ( chdir("/") < 0 )
syslog(4, "cannot change directory");
close(0);
close(1);
return close(2);
}

original_code:
void make_daemon() {
 int n;
 pid_t pid, sid;
 pid = fork();
 if(pid < 0) syslog(4,"cannot fork");
 if(pid > 0) exit(0);
 syslog(6,"now operating as a daemon");
 n = fprintf(pidfp,"%i\n",getpid());
 if(n <= 0) syslog(4,"cannot write PID to file (%m)");
 ftruncate(pidfd,n);
 fflush(pidfp);
 fsync(pidfd);
 sid = setsid();
 if(sid < 0) syslog(4,"cannot set sid");
 if(chdir("/") < 0) syslog(4,"cannot change directory");
 close(0);
 close(1);
 close(2);
}

canonical_original_code:
void make_daemon() {
 int n;
 int pid; int sid;
 pid = fork();
 if(pid < 0) syslog(4,"cannot fork");
 if(pid > 0) exit(0);
 syslog(6,"now operating as a daemon");
 n = fprintf(pidfp,"%i\n",getpid());
 if(n <= 0) syslog(4,"cannot write PID to file (%m)");
 ftruncate(pidfd,n);
 fflush(pidfp);
 fsync(pidfd);
 sid = setsid();
 if(sid < 0) syslog(4,"cannot set sid");
 if(chdir("/") < 0) syslog(4,"cannot change directory");
 close(0);
 close(1);
 close(2);
}

variable_types: n int

variable_types: sid int


decompiled_code:
int init_com()
{
char *v0; // rbx
char *v1; // rbx
int v2; // eax
int v3; // er12
size_t v4; // rbx
void *v5; // rsi
struct stat buf; // [rsp+0h] [rbp-B0h] BYREF
const char *v8; // [rsp+98h] [rbp-18h] BYREF

umask(0);
inotifyfd = inotify_init();
find_serial();
v0 = getenv("ANGEL_DB");
if ( !v0 )
v0 = "/var/opt/staevt.com/angel/angel.sqlite3";
if ( (unsigned int)sqlite3_open(v0, &db) )
die("cannot open database '%s'", v0);
sqlite3_exec(db, "PRAGMA journal_mode=WAL", 0LL, 0LL, &v8);
if ( v8 )
die(v8);
sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS cars (car TEXT, time_received TIMESTAMP DEFAULT (strftime('%s','now')),time INTEGER, ampe"
"rage REAL, voltage REAL, latitude REAL,longitude REAL, speed REAL, PRIMARY KEY (car, time_received))",
0LL);
if ( v8 )
die(v8);
v1 = getenv("ANGEL_KEY");
if ( !v1 )
v1 = "/var/opt/staevt.com/angel/hmac.key";
v2 = open(v1, 0);
v3 = v2;
if ( v2 < 0 )
die("cannot open HMAC key file '%s'", v1);
fstat(v2, &buf);
hmackeysize = buf.st_size;
v4 = SLODWORD(buf.st_size);
v5 = malloc(SLODWORD(buf.st_size));
hmackey = (__int64)v5;
if ( !v5 )
die("cannot allocate memory for HMAC key");
read(v3, v5, v4);
return close(v3);
}

canonical_decompiled_code:
int func6()
{
char *v0; // rbx
char *v1; // rbx
int v2; // eax
int v3; // er12
size_t v4; // rbx
void *v5; // rsi
struct stat buf; // [rsp+0h] [rbp-B0h] BYREF
const char *v8; // [rsp+98h] [rbp-18h] BYREF

umask(0);
inotifyfd = inotify_init();
func3();
v0 = getenv("ANGEL_DB");
if ( !v0 )
v0 = "/var/opt/staevt.com/angel/angel.sqlite3";
if ( (unsigned int)sqlite3_open(v0, &db) )
func0("cannot open database '%s'", v0);
sqlite3_exec(db, "PRAGMA journal_mode=WAL", 0LL, 0LL, &v8);
if ( v8 )
func0(v8);
sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS cars (car TEXT, time_received TIMESTAMP DEFAULT (strftime('%s','now')),time INTEGER, ampe"
"rage REAL, voltage REAL, latitude REAL,longitude REAL, speed REAL, PRIMARY KEY (car, time_received))",
0LL);
if ( v8 )
func0(v8);
v1 = getenv("ANGEL_KEY");
if ( !v1 )
v1 = "/var/opt/staevt.com/angel/hmac.key";
v2 = open(v1, 0);
v3 = v2;
if ( v2 < 0 )
func0("cannot open HMAC key file '%s'", v1);
fstat(v2, &buf);
hmackeysize = buf.st_size;
v4 = SLODWORD(buf.st_size);
v5 = malloc(SLODWORD(buf.st_size));
hmackey = (__int64)v5;
if ( !v5 )
func0("cannot allocate memory for HMAC key");
read(v3, v5, v4);
return close(v3);
}

original_code:
void init_com() {
 char *msg;
 int err, fd;
 struct stat stat;
 char *dbname, *keyname;
 umask(0);
 inotifyfd = inotify_init();
 find_serial();
 dbname = getenv("ANGEL_DB");
 if(!dbname) dbname = "/var/opt/staevt.com/angel/angel.sqlite3";
 err = sqlite3_open(dbname,&db);
 if(err != 0) die("cannot open database '%s'",dbname);
 sqlite3_exec(db,"PRAGMA journal_mode=WAL",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS cars ("
  "car TEXT, time_received TIMESTAMP"
   " DEFAULT (strftime('%s','now')),"
  "time INTEGER, amperage REAL, voltage REAL, latitude REAL,"
  "longitude REAL, speed REAL, PRIMARY KEY (car, time_received)"
 ")",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 keyname = getenv("ANGEL_KEY");
 if(!keyname) keyname = "/var/opt/staevt.com/angel/hmac.key";
 fd = open(keyname,00);
 if(fd < 0) die("cannot open HMAC key file '%s'",keyname);
 fstat(fd,&stat);
 hmackeysize = stat.st_size;
 hmackey = malloc(hmackeysize);
 if(!hmackey) die("cannot allocate memory for HMAC key");
 read(fd,hmackey,hmackeysize);
 close(fd);
}

canonical_original_code:
void init_com() {
 char *msg;
 int err; int fd;
 struct stat stat;
 char *dbname; char *keyname;
 umask(0);
 inotifyfd = inotify_init();
 find_serial();
 dbname = getenv("ANGEL_DB");
 if(!dbname) dbname = "/var/opt/staevt.com/angel/angel.sqlite3";
 err = sqlite3_open(dbname,&db);
 if(err != 0) die("cannot open database '%s'",dbname);
 sqlite3_exec(db,"PRAGMA journal_mode=WAL",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS cars ("
  "car TEXT, time_received TIMESTAMP"
   " DEFAULT (strftime('%s','now')),"
  "time INTEGER, amperage REAL, voltage REAL, latitude REAL,"
  "longitude REAL, speed REAL, PRIMARY KEY (car, time_received)"
 ")",((void *)0),((void *)0),&msg);
 if(msg) die(msg);
 keyname = getenv("ANGEL_KEY");
 if(!keyname) keyname = "/var/opt/staevt.com/angel/hmac.key";
 fd = open(keyname,00);
 if(fd < 0) die("cannot open HMAC key file '%s'",keyname);
 fstat(fd,&stat);
 hmackeysize = stat.st_size;
 hmackey = malloc(hmackeysize);
 if(!hmackey) die("cannot allocate memory for HMAC key");
 read(fd,hmackey,hmackeysize);
 close(fd);
}

variable_types:
msg
char *

variable_types: err int

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

variable_types:
dbname
char *

variable_types:
keyname
char *


decompiled_code:
__int64 __fastcall tolower_ct(int a1)
{
int v1; // eax

v1 = a1 - 65;
LOBYTE(v1) = (unsigned __int8)(a1 - 65) < 0x1Au;
return (unsigned int)(a1 + 32 * v1);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
int v1; // eax

v1 = a1 - 65;
LOBYTE(v1) = (unsigned __int8)(a1 - 65) < 0x1Au;
return (unsigned int)(a1 + 32 * v1);
}

original_code:
char tolower_ct(char c) {
 return c + (c >= 'A')*(c <= 'Z')*0x20;
}

variable_types: c char

return_type: char


decompiled_code:
__int64 __fastcall strieq_ct(const char *a1, const char *a2)
{
char *v2; // rbx
const char *v3; // r12
size_t v4; // r14
unsigned int v5; // er13
char v6; // di
char v7; // r14

v2 = (char *)a1;
v3 = a2;
if ( !a1 || !a2 )
return 0;
v4 = strlen(a1);
v5 = 0;
if ( v4 == strlen(a2) )
{
v6 = *a1;
if ( *v2 )
{
do
{
v7 = tolower_ct(v6);
v5 |= v7 == (char)tolower_ct(*v3);
++v2;
++v3;
v6 = *v2;
}
while ( *v2 );
}
else
{
v5 = 0;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, const char *a2)
{
char *v2; // rbx
const char *v3; // r12
size_t v4; // r14
unsigned int v5; // er13
char v6; // di
char v7; // r14

v2 = (char *)a1;
v3 = a2;
if ( !a1 || !a2 )
return 0;
v4 = strlen(a1);
v5 = 0;
if ( v4 == strlen(a2) )
{
v6 = *a1;
if ( *v2 )
{
do
{
v7 = func9(v6);
v5 |= v7 == (char)func9(*v3);
++v2;
++v3;
v6 = *v2;
}
while ( *v2 );
}
else
{
v5 = 0;
}
}
return v5;
}

original_code:
int strieq_ct(char *a, char *b) {
 int equal = 0;
 if(!a || !b || strlen(a) != strlen(b)) return 0;
 while(*a) {
  equal |= tolower_ct(*a) == tolower_ct(*b);
  a++, b++;
 }
 return equal;
}

variable_types:
a
char *

variable_types:
b
char *

variable_types: equal int


original_code:
void reset_datum(struct datum *datum) {
 datum->milliseconds = 0;
 datum->amperage = -1;
 datum->voltage = -1;
 datum->latitude = 0;
 datum->longitude = 0;
 datum->speed = -1;
}

variable_types: struct datum { long milliseconds; float amperage; float voltage; float latitude; float speed; } *


parse

decompiled_code:
void __fastcall parse(char *s)
{
char *v1; // rbx
_BOOL4 v2; // er12
char v3; // al
float v4; // [rsp+0h] [rbp-30h] BYREF
char v5; // [rsp+8h] [rbp-28h] BYREF
char v6[36]; // [rsp+Ch] [rbp-24h] BYREF

v1 = s;
reset_datum((__int64)&v4);
v2 = 0;
if ( s )
{
while ( 1 )
{
v3 = *v1;
if ( !*v1 )
break;
if ( v2 )
{
switch ( v3 )
{
case 'a':
_isoc99_sscanf(v1, "a:%f;", &v5);
goto LABEL_9;
case 'g':
_isoc99_sscanf(v1, "g:%f,%f;", &v6[4], &v6[8]);
goto LABEL_9;
case 's':
_isoc99_sscanf(v1, "s:%f;", &v6[12]);
goto LABEL_9;
case 't':
_isoc99_sscanf(v1, "t:%li;", &v4);
goto LABEL_9;
case 'v':
_isoc99_sscanf(v1, "v:%f;", v6);
goto LABEL_9;
case '}':
store(&v4);
reset_datum((__int64)&v4);
v2 = 0;
break;
default:
LABEL_9:
v1 = strchr(v1, 59);
break;
}
}
else
{
v2 = v3 == 123;
}
++v1;
}
}
}

canonical_decompiled_code:
void __fastcall func12(char *s)
{
char *v1; // rbx
_BOOL4 v2; // er12
char v3; // al
float v4; // [rsp+0h] [rbp-30h] BYREF
char v5; // [rsp+8h] [rbp-28h] BYREF
char v6[36]; // [rsp+Ch] [rbp-24h] BYREF

v1 = s;
func11((__int64)&v4);
v2 = 0;
if ( s )
{
while ( 1 )
{
v3 = *v1;
if ( !*v1 )
break;
if ( v2 )
{
switch ( v3 )
{
case 'a':
_isoc99_sscanf(v1, "a:%f;", &v5);
goto LABEL_9;
case 'g':
_isoc99_sscanf(v1, "g:%f,%f;", &v6[4], &v6[8]);
goto LABEL_9;
case 's':
_isoc99_sscanf(v1, "s:%f;", &v6[12]);
goto LABEL_9;
case 't':
_isoc99_sscanf(v1, "t:%li;", &v4);
goto LABEL_9;
case 'v':
_isoc99_sscanf(v1, "v:%f;", v6);
goto LABEL_9;
case '}':
func7(&v4);
func11((__int64)&v4);
v2 = 0;
break;
default:
LABEL_9:
v1 = strchr(v1, 59);
break;
}
}
else
{
v2 = v3 == 123;
}
++v1;
}
}
}

original_code:
void parse(char *block) {
 struct datum datum;
 enum { START, FIELD } state;
 reset_datum(&datum);
 for(state = START; block && *block; block++) {
  switch(state) {
  case START: if(*block == '{') state = FIELD; break;
  case FIELD:
   switch(*block) {
   case 'a': sscanf(block,"a:%f;",&datum.amperage); break;
   case 'g': sscanf(block,"g:%f,%f;",&datum.latitude,
    &datum.longitude); break;
   case 's': sscanf(block,"s:%f;",&datum.speed); break;
   case 't': sscanf(block,"t:%""l" "i"";",
    &datum.milliseconds); break;
   case 'v': sscanf(block,"v:%f;",&datum.voltage); break;
   case '}':
    store(&datum);
    reset_datum(&datum);
    state = START;
    break;
   default: break;
   }
   if(state == FIELD)
    block = strchr(block,';');
   break;
  default: state = START; break;
  }
 }
}

canonical_original_code:
void parse(char *block) {
 struct datum datum;
 enum { START=0, FIELD=1 } state;
 reset_datum(&datum);
 for(state = START; block && *block; block++) {
  switch(state) {
  case START: if(*block == '{') state = FIELD; break;
  case FIELD:
   switch(*block) {
   case 'a': sscanf(block,"a:%f;",&datum.amperage); break;
   case 'g': sscanf(block,"g:%f,%f;",&datum.latitude,
    &datum.longitude); break;
   case 's': sscanf(block,"s:%f;",&datum.speed); break;
   case 't': sscanf(block,"t:%""l" "i"";",
    &datum.milliseconds); break;
   case 'v': sscanf(block,"v:%f;",&datum.voltage); break;
   case '}':
    store(&datum);
    reset_datum(&datum);
    state = START;
    break;
   default: break;
   }
   if(state == FIELD)
    block = strchr(block,';');
   break;
  default: state = START; break;
  }
 }
}

variable_types:
block
char *

variable_types: struct datum { long milliseconds; float amperage; float voltage; float latitude; float speed; }

variable_types:
state
enum { START=0, FIELD=1 }


monitor

decompiled_code:
void __noreturn monitor()
{
int v0; // er12
__int64 v1; // rbx
int i; // er14
char v3[1032]; // [rsp+0h] [rbp-440h] BYREF
struct pollfd fds; // [rsp+408h] [rbp-38h] BYREF

fds.fd = serialfd;
fds.events = 17;
while ( 1 )
{
v0 = poll(&fds, 1uLL, -1);
if ( v0 < 0 )
syslog(4, "poll error (%m)");
if ( (fds.revents & 1) != 0 )
{
v3[0] = 0;
v1 = 0LL;
for ( i = 0; !strchr(v3, 123) || !strchr(v3, 125); i = 1 )
{
v1 += read(serialfd, &v3[v1], v0);
if ( !i && !strchr(v3, 123) )
break;
if ( v1 > 1023 )
{
v3[1023] = 0;
goto LABEL_16;
}
if ( v1 < 0 )
goto LABEL_18;
v3[v1] = 0;
v0 = 1;
}
if ( v1 < 0 )
LABEL_18:
syslog(4, "read error (%m)");
LABEL_16:
parse(v3);
}
if ( (fds.revents & 0x10) != 0 )
{
syslog(5, "serial device removed");
close(serialfd);
find_serial();
fds.fd = serialfd;
}
}
}

canonical_decompiled_code:
void __noreturn func13()
{
int v0; // er12
__int64 v1; // rbx
int i; // er14
char v3[1032]; // [rsp+0h] [rbp-440h] BYREF
struct pollfd fds; // [rsp+408h] [rbp-38h] BYREF

fds.fd = serialfd;
fds.events = 17;
while ( 1 )
{
v0 = poll(&fds, 1uLL, -1);
if ( v0 < 0 )
syslog(4, "poll error (%m)");
if ( (fds.revents & 1) != 0 )
{
v3[0] = 0;
v1 = 0LL;
for ( i = 0; !strchr(v3, 123) || !strchr(v3, 125); i = 1 )
{
v1 += read(serialfd, &v3[v1], v0);
if ( !i && !strchr(v3, 123) )
break;
if ( v1 > 1023 )
{
v3[1023] = 0;
goto LABEL_16;
}
if ( v1 < 0 )
goto LABEL_18;
v3[v1] = 0;
v0 = 1;
}
if ( v1 < 0 )
LABEL_18:
syslog(4, "read error (%m)");
LABEL_16:
func12(v3);
}
if ( (fds.revents & 0x10) != 0 )
{
syslog(5, "serial device removed");
close(serialfd);
func3();
fds.fd = serialfd;
}
}
}

original_code:
void monitor() {
 static const int bufsize = 1024;
 int nready;
 int reading;
 ssize_t nread;
 char buf[bufsize];
 struct pollfd pollfd;
 pollfd.fd = serialfd;
 pollfd.events = 0x001 | 0x010;
 while(1) {
  nready = poll(&pollfd,1,-1);
  if(nready < 0) syslog(4,"poll error (%m)");
  if(pollfd.revents & 0x001) {
   buf[0] = '\0';
   reading = nread = 0;
   while(nread >= 0
    && (!strchr(buf,'{') || !strchr(buf,'}'))) {
    nread += read(serialfd,buf + nread,nready);
    if(!reading && !strchr(buf,'{')) break;
    else reading = 1;
    if(nread >= bufsize) {
     buf[bufsize - 1] = '\0';
     break;
    }
    if(nread >= 0) buf[nread] = '\0';
    nready = 1;
   }
   if(nread < 0) syslog(4,"read error (%m)");
   parse(buf);
  }
  if(pollfd.revents & 0x010) {
   syslog(5,"serial device removed");
   close(serialfd);
   find_serial();
   pollfd.fd = serialfd;
  }
 }
}

canonical_original_code:
void monitor() {
 static int bufsize = 1024;
 int nready;
 int reading;
 long nread;
 char buf[-1];
 struct pollfd pollfd;
 pollfd.fd = serialfd;
 pollfd.events = 0x001 | 0x010;
 while(1) {
  nready = poll(&pollfd,1,-1);
  if(nready < 0) syslog(4,"poll error (%m)");
  if(pollfd.revents & 0x001) {
   buf[0] = '\0';
   reading = nread = 0;
   while(nread >= 0
    && (!strchr(buf,'{') || !strchr(buf,'}'))) {
    nread += read(serialfd,buf + nread,nready);
    if(!reading && !strchr(buf,'{')) break;
    else reading = 1;
    if(nread >= bufsize) {
     buf[bufsize - 1] = '\0';
     break;
    }
    if(nread >= 0) buf[nread] = '\0';
    nready = 1;
   }
   if(nread < 0) syslog(4,"read error (%m)");
   parse(buf);
  }
  if(pollfd.revents & 0x010) {
   syslog(5,"serial device removed");
   close(serialfd);
   find_serial();
   pollfd.fd = serialfd;
  }
 }
}

variable_types: bufsize int

variable_types: nready int

variable_types: reading int

variable_types: nread long

variable_types:
buf
char [-1]

variable_types: struct pollfd { int fd; short events; short revents; }


die

decompiled_code:
void __noreturn die(const char *a1, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-C8h] BYREF

va_start(ap, a1);
vsyslog(2, a1, ap);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0(const char *a1, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-C8h] BYREF

va_start(ap, a1);
vsyslog(2, a1, ap);
exit(1);
}

original_code:
void die(char *format, ...) {
 va_list ap;
 __builtin_va_start(ap,format);
 vsyslog(2,format,ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void die(char *format, ...) {
 __builtin_va_list ap;
 __builtin_va_start(ap,format);
 vsyslog(2,format,ap);
 __builtin_va_end(ap);
 exit(1);
}

variable_types:
format
char *