original_code:
void small_hash__table__fini_destroy(
    small_hash__table *table,
    void (*free_node)(void *, small_hash__node *), void *arg)
{
    while(1) {
        _Bool empty = 1;
        unsigned i;
        for(i = 0; i < table->anchors_count; i++) {
            small_hash__node *first = table->anchors[i].first;
            if(!first) continue;
            empty = 0;
            table->anchors[i].first = first->next;
            free_node(arg, first);
        }
        if(empty) break;
    }
    small_hash__table__fini(table);
}

canonical_original_code:
void small_hash__table__fini_destroy(struct small_hash__table *table, void (*free_node)(void *, struct small_hash__node *), void *arg)
{
    while(1) {
        _Bool empty = 1;
        unsigned int i;
        for(i = 0; i < table->anchors_count; i++) {
            struct small_hash__node *first = table->anchors[i].first;
            if(!first) continue;
            empty = 0;
            table->anchors[i].first = first->next;
            free_node(arg, first);
        }
        if(empty) break;
    }
    small_hash__table__fini(table);
}

variable_types: void (*)(void *, struct small_hash__node *)

variable_types:
arg
void *

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall small_hash__table__find(__int64 a1, unsigned int a2, __int64 a3)
{
unsigned int v3; // eax

if ( !*(_BYTE *)(a1 + 20) || *(_DWORD *)(a1 + 56) )
return find(a1, a2, a3);
v3 = *(_DWORD *)(a1 + 40) + 1;
if ( v3 <= 9 )
{
*(_DWORD *)(a1 + 40) = v3;
return find(a1, a2, a3);
}
*(_DWORD *)(a1 + 40) = 0;
return find_and_report(a1, a2, a3);
}

original_code:
small_hash__node *small_hash__table__find(
    small_hash__table *table,
    small_hash__hash hash, const void *key)
{
    if(table->is_dynamic && 0 == table->prevent_resizes_count &&
       ++table->lookup_count >= 10)
    {
        table->lookup_count = 0;
        return find_and_report(table, hash, key);
    }
    return find(table, hash, key);
}

canonical_original_code:
struct small_hash__node *small_hash__table__find(struct small_hash__table *table, unsigned int hash, void *key)
{
    if(table->is_dynamic && 0 == table->prevent_resizes_count &&
       ++table->lookup_count >= 10)
    {
        table->lookup_count = 0;
        return find_and_report(table, hash, key);
    }
    return find(table, hash, key);
}

variable_types: struct small_hash__table { struct small_hash__funcs { _Bool (*match_key)(void *user_arg, void *key, struct small_hash__node *); unsigned int (*get_hash)(void *user_arg, struct small_hash__node *); } *user_funcs; void *user_arg; unsigned int count; _Bool is_dynamic; unsigned int min_anchors_count; unsigned int low_watermark; unsigned int high_watermark; unsigned int expensive_lookup_count; unsigned int lookup_count; unsigned int anchors_count; struct small_hash__anchor { struct small_hash__node *first; } *anchors; unsigned int prevent_resizes_count; } *

variable_types:
hash
unsigned int

variable_types:
key
void *

return_type: struct small_hash__node { struct small_hash__node *prev; } *

user_defined_types: struct small_hash__table { struct small_hash__funcs *user_funcs; void *user_arg; unsigned int count; _Bool is_dynamic; unsigned int min_anchors_count; unsigned int low_watermark; unsigned int high_watermark; unsigned int expensive_lookup_count; unsigned int lookup_count; unsigned int anchors_count; struct small_hash__anchor *anchors; unsigned int prevent_resizes_count; }


original_code:
void small_hash__iter__init(small_hash__table *table, small_hash__iter *iter)
{
    table->prevent_resizes_count++;
    iter->next_anchor_index = 0;
    iter->next = ((void *)0);
}

canonical_original_code:
void small_hash__iter__init(struct small_hash__table *table, struct small_hash__iter *iter)
{
    table->prevent_resizes_count++;
    iter->next_anchor_index = 0;
    iter->next = ((void *)0);
}

variable_types: struct small_hash__table { struct small_hash__funcs { _Bool (*match_key)(void *user_arg, void *key, struct small_hash__node { struct small_hash__node *prev; } *); unsigned int (*get_hash)(void *user_arg, struct small_hash__node *); } *user_funcs; void *user_arg; unsigned int count; _Bool is_dynamic; unsigned int min_anchors_count; unsigned int low_watermark; unsigned int high_watermark; unsigned int expensive_lookup_count; unsigned int lookup_count; unsigned int anchors_count; struct small_hash__anchor { struct small_hash__node *first; } *anchors; unsigned int prevent_resizes_count; } *

variable_types: struct small_hash__iter { unsigned int next_anchor_index; struct small_hash__node *next; } *

return_type: void

user_defined_types: struct small_hash__funcs { _Bool (*match_key)(void *user_arg, void *key, struct small_hash__node *); unsigned int (*get_hash)(void *user_arg, struct small_hash__node *); }


decompiled_code:
__int64 __fastcall small_hash__iter__fini(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 56);
if ( !v1 )
_assert_fail("table->prevent_resizes_count > 0", "small_hash.c", 0x11Au, "small_hash__iter__fini");
result = (unsigned int)(v1 - 1);
*(_DWORD *)(a1 + 56) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 56);
if ( !v1 )
_assert_fail("table->prevent_resizes_count > 0", "small_hash.c", 0x11Au, "small_hash__iter__fini");
result = (unsigned int)(v1 - 1);
*(_DWORD *)(a1 + 56) = result;
return result;
}

original_code:
void small_hash__iter__fini(small_hash__table *table, small_hash__iter *iter)
{
    ((void) sizeof ((table->prevent_resizes_count > 0) ? 1 : 0), __extension__ ({ if (table->prevent_resizes_count > 0) ; else __assert_fail ("table->prevent_resizes_count > 0", "small_hash.c", 282, __extension__ __PRETTY_FUNCTION__); }));
    table->prevent_resizes_count--;
}

canonical_original_code:
void small_hash__iter__fini(struct small_hash__table *table, struct small_hash__iter *iter)
{
    ((void) sizeof ((table->prevent_resizes_count > 0) ? 1 : 0), __extension__ ({ if (table->prevent_resizes_count > 0) ; else __assert_fail ("table->prevent_resizes_count > 0", "small_hash.c", 282, __extension__ __PRETTY_FUNCTION__); }));
    table->prevent_resizes_count--;
}

user_defined_types: struct small_hash__iter { unsigned int next_anchor_index; struct small_hash__node *next; }

user_defined_types: struct small_hash__anchor { struct small_hash__node *first; }

user_defined_types: struct small_hash__node { struct small_hash__node *prev; }


original_code:
small_hash__node *small_hash__iter__next(small_hash__table *table, small_hash__iter *iter)
{
    small_hash__node *next = iter->next;
    if(next) {
        iter->next = next->next;
        return next;
    }
    if(-1U == iter->next_anchor_index) return ((void *)0);
    unsigned i;
    for(i = iter->next_anchor_index; i < table->anchors_count; i++) {
        small_hash__node *first = table->anchors[i].first;
        if(first) {
            iter->next_anchor_index = i+1;
            iter->next = first->next;
            return first;
        }
    }
    iter->next = ((void *)0);
    iter->next_anchor_index = -1U;
    small_hash__iter__fini(table, iter);
    return ((void *)0);
}

canonical_original_code:
struct small_hash__node *small_hash__iter__next(struct small_hash__table *table, struct small_hash__iter *iter)
{
    struct small_hash__node *next = iter->next;
    if(next) {
        iter->next = next->next;
        return next;
    }
    if(-1U == iter->next_anchor_index) return ((void *)0);
    unsigned int i;
    for(i = iter->next_anchor_index; i < table->anchors_count; i++) {
        struct small_hash__node *first = table->anchors[i].first;
        if(first) {
            iter->next_anchor_index = i+1;
            iter->next = first->next;
            return first;
        }
    }
    iter->next = ((void *)0);
    iter->next_anchor_index = -1U;
    small_hash__iter__fini(table, iter);
    return ((void *)0);
}

variable_types: struct small_hash__node { struct small_hash__node *prev; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v5 = 0LL;
v4 = 0;
lecture(argv[1], &v5, &v4);
printf("capacite : %i\n", v4);
return 0;
}

original_code:
int main (int argc, char *argv[]) {
objet *tabObjets = ((void *)0);
int capacite = 0;
char *filename = argv[1];
lecture(filename, &tabObjets, &capacite);
printf("capacite : %i\n", capacite);
}

canonical_original_code:
int main(int argc, char *argv[]) {
struct objet *tabObjets = ((void *)0);
int capacite = 0;
char *filename = argv[1];
lecture(filename, &tabObjets, &capacite);
printf("capacite : %i\n", capacite);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct objet { int numero; int poids; int gain; float ratio; } *

variable_types: capacite int

variable_types:
filename
char *

return_type: int


crosstrap

repo: PegasusEpsilon/complexfractals

original_code:
double crosstrap (COMPLEX *const z, COMPLEX *const c, void *data) {
 unsigned long long i, deadline;
 double d = (double)((unsigned long long)-1);
 COMPLEX z2, oz;
 TRAP *trap;
 trap = (TRAP *)data;
 for (
  i = 0, deadline = 1,
  oz.R = oz.I = 100;
  i != (unsigned long long)-1; i++
 ) {
  z2.R = z->R * z->R;
  z2.I = z->I * z->I;
  if (z2.R + z2.I > 16*16) break;
  z->I = 2 * z->R * z->I + c->I;
  z->R = z2.R - z2.I + c->R;
  if (i > trap->start) {
   double v, h, n;
   v = fabs(trap->sin * z->R + trap->cos * z->I);
   h = fabs(trap->cos * z->R - trap->sin * z->I);
   n = (h < v ? h : v) / trap->hyp;
   if (n < d) d = n;
  }
  if (oz.R == z->R && oz.I == z->I) return d / 2;
  if (i > deadline) { oz.R = z->R; oz.I = z->I; deadline <<= 1; }
 }
 maxiter = maxiter < i ? i : maxiter;
 return trap->dist < d ? -1 : trap->dist - d;
}

canonical_original_code:
double crosstrap(struct COMPLEX *z, struct COMPLEX *c, void *data) {
 unsigned long long i; unsigned long long deadline;
 double d = (double)((unsigned long long)-1);
 struct COMPLEX z2; struct COMPLEX oz;
 struct TRAP *trap;
 trap = (struct TRAP *)data;
 for (
  i = 0, deadline = 1,
  oz.R = oz.I = 100;
  i != (unsigned long long)-1; i++
 ) {
  z2.R = z->R * z->R;
  z2.I = z->I * z->I;
  if (z2.R + z2.I > 16*16) break;
  z->I = 2 * z->R * z->I + c->I;
  z->R = z2.R - z2.I + c->R;
  if (i > trap->start) {
   double v; double h; double n;
   v = fabs(trap->sin * z->R + trap->cos * z->I);
   h = fabs(trap->cos * z->R - trap->sin * z->I);
   n = (h < v ? h : v) / trap->hyp;
   if (n < d) d = n;
  }
  if (oz.R == z->R && oz.I == z->I) return d / 2;
  if (i > deadline) { oz.R = z->R; oz.I = z->I; deadline <<= 1; }
 }
 maxiter = maxiter < i ? i : maxiter;
 return trap->dist < d ? -1 : trap->dist - d;
}

variable_types: struct COMPLEX { double R; double I; } *

variable_types:
c
struct COMPLEX *

variable_types:
data
void *

variable_types:
i
unsigned long long

variable_types:
deadline
unsigned long long

variable_types: d double

variable_types:
oz
struct COMPLEX { double R; double I; }

variable_types: struct TRAP { double angle; unsigned int start; } *

variable_types: v double

variable_types: h double

variable_types: n double

return_type: double


renormalized

original_code:
double renormalized (COMPLEX *const z, COMPLEX *const c, void *data) {
 unsigned long long i, deadline;
 COMPLEX z2, oz;
 for (
  i = 0, deadline = 1,
  oz.R = oz.I = 100;
  i != (unsigned long long)-1; i++
 ) {
  z2.R = z->R * z->R;
  z2.I = z->I * z->I;
  if (z2.R + z2.I > 16*16) break;
  z->I = 2 * z->R * z->I + c->I;
  z->R = z2.R - z2.I + c->R;
  if (oz.R == z->R && oz.I == z->I) return -1;
  if (i > deadline) { oz.R = z->R; oz.I = z->I; deadline <<= 1; }
 }
 maxiter = maxiter < i ? i : maxiter;
 return (double)i - log2(log(z2.R + z2.I) / 2);
}

canonical_original_code:
double renormalized(struct COMPLEX *z, struct COMPLEX *c, void *data) {
 unsigned long long i; unsigned long long deadline;
 struct COMPLEX z2; struct COMPLEX oz;
 for (
  i = 0, deadline = 1,
  oz.R = oz.I = 100;
  i != (unsigned long long)-1; i++
 ) {
  z2.R = z->R * z->R;
  z2.I = z->I * z->I;
  if (z2.R + z2.I > 16*16) break;
  z->I = 2 * z->R * z->I + c->I;
  z->R = z2.R - z2.I + c->R;
  if (oz.R == z->R && oz.I == z->I) return -1;
  if (i > deadline) { oz.R = z->R; oz.I = z->I; deadline <<= 1; }
 }
 maxiter = maxiter < i ? i : maxiter;
 return (double)i - log2(log(z2.R + z2.I) / 2);
}


inset

variable_types: tmp double


trapcheck

original_code:
TRAP trapcheck (int *argc, char ***argv) {
 TRAP trap;
 if (3 < *argc) {
  if (!strcmp("--crosstrap", (*argv)[1])) {
   trap.angle = atof((*argv)[2]) * 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421 / 90;
   trap.dist = atof((*argv)[3]);
   trap.start = (uint32_t)atoi((*argv)[4]);
   trap.sin = sin(trap.angle);
   trap.cos = cos(trap.angle);
   trap.hyp = sqrt(trap.sin * trap.sin + trap.cos * trap.cos);
   (*argv)[4] = (*argv)[0]; *argc -= 4; *argv += 4;
   iterate = &crosstrap;
  } else trap.dist = 0;
 }
 return trap;
}

canonical_original_code:
struct TRAP trapcheck(int *argc, char ***argv) {
 struct TRAP trap;
 if (3 < *argc) {
  if (!strcmp("--crosstrap", (*argv)[1])) {
   trap.angle = atof((*argv)[2]) * 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421 / 90;
   trap.dist = atof((*argv)[3]);
   trap.start = (unsigned int)atoi((*argv)[4]);
   trap.sin = sin(trap.angle);
   trap.cos = cos(trap.angle);
   trap.hyp = sqrt(trap.sin * trap.sin + trap.cos * trap.cos);
   (*argv)[4] = (*argv)[0]; *argc -= 4; *argv += 4;
   iterate = &crosstrap;
  } else trap.dist = 0;
 }
 return trap;
}

variable_types:
argc
int *

variable_types:
argv
char ***

variable_types: struct TRAP { double angle; unsigned int start; }


decompiled_code:
double __fastcall pixel2vector_NOTHETA(int *a1, double *a2, double *a3)
{
return (double)*a1 * *a2 - a3[2] + *a3;
}

canonical_decompiled_code:
double __fastcall func0(int *a1, double *a2, double *a3)
{
return (double)*a1 * *a2 - a3[2] + *a3;
}

original_code:
COMPLEX pixel2vector_NOTHETA (
 const PIXEL *const in, const COMPLEX *const size,
 const REGION *const window, const double theta
) {
 COMPLEX out;
 out.R = window->center.R + (in->X * size->R - window->radius.R);
 out.I = -window->center.I + (in->Y * size->I - window->radius.I);
 return out;
}

canonical_original_code:
struct COMPLEX pixel2vector_NOTHETA(struct PIXEL *in, struct COMPLEX *size, struct REGION *window, double theta) {
 struct COMPLEX out;
 out.R = window->center.R + (in->X * size->R - window->radius.R);
 out.I = -window->center.I + (in->Y * size->I - window->radius.I);
 return out;
}

variable_types:
in
struct PIXEL { int X; int Y; } *

variable_types:
size
struct COMPLEX *

variable_types: struct REGION { struct COMPLEX center; struct COMPLEX radius; } *

variable_types: theta double

variable_types:
out
struct COMPLEX { double R; double I; }

return_type: struct COMPLEX { double R; double I; }


decompiled_code:
double __fastcall pixel2vector_START(int *a1, double *a2, double *a3, double a4)
{
__int64 (__fastcall *v4)(); // rax

v4 = (__int64 (__fastcall *)())pixel2vector_NOTHETA;
if ( a4 != 0.0 )
v4 = pixel2vector_THETA;
pixel2vector = v4;
return ((double (__fastcall *)(int *, double *, double *))v4)(a1, a2, a3);
}

canonical_decompiled_code:
double __fastcall func1(int *a1, double *a2, double *a3, double a4)
{
__int64 (__fastcall *v4)(); // rax

v4 = (__int64 (__fastcall *)())pixel2vector_NOTHETA;
if ( a4 != 0.0 )
v4 = pixel2vector_THETA;
pixel2vector = v4;
return ((double (__fastcall *)(int *, double *, double *))v4)(a1, a2, a3);
}

original_code:
COMPLEX pixel2vector_START (
 const PIXEL *const in, const COMPLEX *const size,
 const REGION *const window, const double theta
) {
 pixel2vector = theta ? &pixel2vector_THETA : &pixel2vector_NOTHETA;
 return pixel2vector(in, size, window, theta);
}

canonical_original_code:
struct COMPLEX pixel2vector_START(struct PIXEL *in, struct COMPLEX *size, struct REGION *window, double theta) {
 pixel2vector = theta ? &pixel2vector_THETA : &pixel2vector_NOTHETA;
 return pixel2vector(in, size, window, theta);
}

user_defined_types: struct REGION { struct COMPLEX center; struct COMPLEX radius; }


decompiled_code:
double __fastcall pixel2vector_THETA(int *a1, double *a2, double *a3, double a4)
{
double v6; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a4, sinx, &v6);
return ((double)*a1 * *a2 - a3[2]) * v6 + *a3 - ((double)a1[1] * a2[1] - a3[3]) * sinx[0];
}

canonical_decompiled_code:
double __fastcall func2(int *a1, double *a2, double *a3, double a4)
{
double v6; // [rsp+0h] [rbp-28h] BYREF
double sinx[4]; // [rsp+8h] [rbp-20h] BYREF

sincos(a4, sinx, &v6);
return ((double)*a1 * *a2 - a3[2]) * v6 + *a3 - ((double)a1[1] * a2[1] - a3[3]) * sinx[0];
}

original_code:
COMPLEX pixel2vector_THETA (
 const PIXEL *const in, const COMPLEX *const size,
 const REGION *const window, const double theta
) {
 COMPLEX out;
 out.R = window->center.R
  + (in->X * size->R - window->radius.R) * cos(theta)
  - (in->Y * size->I - window->radius.I) * sin(theta);
 out.I = -window->center.I
  + (in->X * size->R - window->radius.R) * sin(theta)
  + (in->Y * size->I - window->radius.I) * cos(theta);
 return out;
}

canonical_original_code:
struct COMPLEX pixel2vector_THETA(struct PIXEL *in, struct COMPLEX *size, struct REGION *window, double theta) {
 struct COMPLEX out;
 out.R = window->center.R
  + (in->X * size->R - window->radius.R) * cos(theta)
  - (in->Y * size->I - window->radius.I) * sin(theta);
 out.I = -window->center.I
  + (in->X * size->R - window->radius.R) * sin(theta)
  + (in->Y * size->I - window->radius.I) * cos(theta);
 return out;
}


pixelsize

decompiled_code:
double __fastcall pixelsize(_DWORD *a1, __int64 a2)
{
return (*(double *)(a2 + 16) + *(double *)(a2 + 16)) / (double)(*a1 - 1);
}

canonical_decompiled_code:
double __fastcall func3(_DWORD *a1, __int64 a2)
{
return (*(double *)(a2 + 16) + *(double *)(a2 + 16)) / (double)(*a1 - 1);
}

original_code:
COMPLEX pixelsize (const PIXEL *const img, const REGION *const window) {
 COMPLEX out;
 out.R = 2 * window->radius.R / (img->X - 1);
 out.I = 2 * window->radius.I / (img->Y - 1);
 return out;
}

canonical_original_code:
struct COMPLEX pixelsize(struct PIXEL *img, struct REGION *window) {
 struct COMPLEX out;
 out.R = 2 * window->radius.R / (img->X - 1);
 out.I = 2 * window->radius.I / (img->Y - 1);
 return out;
}

variable_types:
img
struct PIXEL { int X; int Y; } *


fail

decompiled_code:
void __fastcall __noreturn fail(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
__attribute__((noreturn))
void fail (const char *msg) {
 perror(msg);
 exit(1);
}

canonical_original_code:
void fail(char *msg) {
 perror(msg);
 exit(1);
}

variable_types:
msg
char *


die

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

va_start(arg, a1);
vprintf(a1, arg);
puts(".");
exit(1);
}

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

va_start(arg, a1);
vprintf(a1, arg);
puts(".");
exit(1);
}

original_code:
__attribute__((noreturn))
void die (const char *fmt, ...) {
 va_list args;
 __builtin_va_start(args,fmt);
 vprintf(fmt, args);
 __builtin_va_end(args);
 puts(".");
 exit(1);
}

canonical_original_code:
void die(char *fmt, ...) {
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 vprintf(fmt, args);
 __builtin_va_end(args);
 puts(".");
 exit(1);
}

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall add_point(__int64 a1, double a2, double a3)
{
double *v3; // rax

v3 = (double *)realloc(*(void **)a1, 16 * (*(_QWORD *)(a1 + 8) + 1LL));
if ( !v3 )
fail("failed to allocate memory");
*(_QWORD *)a1 = v3;
v3[2 * *(_QWORD *)(a1 + 8)] = a2;
*(double *)(*(_QWORD *)a1 + 16LL * (*(_QWORD *)(a1 + 8))++ + 8) = a3;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, double a2, double a3)
{
double *v3; // rax

v3 = (double *)realloc(*(void **)a1, 16 * (*(_QWORD *)(a1 + 8) + 1LL));
if ( !v3 )
func8("failed to allocate memory");
*(_QWORD *)a1 = v3;
v3[2 * *(_QWORD *)(a1 + 8)] = a2;
*(double *)(*(_QWORD *)a1 + 16LL * (*(_QWORD *)(a1 + 8))++ + 8) = a3;
return a1;
}

original_code:
struct CHANNEL *add_point (struct CHANNEL *in, double x, double y) {
 struct POINT *new;
 new = realloc(in->points, (in->length + 1) * sizeof(*(in->points)));
 if (!new)
  fail("failed to allocate memory");
 in->points = new;
 in->points[in->length].x = x;
 in->points[in->length].y = y;
 in->length++;
 return in;
}

canonical_original_code:
struct CHANNEL *add_point(struct CHANNEL *in, double x, double y) {
 struct POINT *new;
 new = realloc(in->points, (in->length + 1) * sizeof(*(in->points)));
 if (!new)
  fail("failed to allocate memory");
 in->points = new;
 in->points[in->length].x = x;
 in->points[in->length].y = y;
 in->length++;
 return in;
}

variable_types:
in
struct CHANNEL *

variable_types: x double

variable_types: y double

variable_types:
new
struct POINT { double x; double y; } *

return_type: struct CHANNEL { struct POINT { double x; double y; } *points; unsigned long length; } *

user_defined_types: struct CHANNEL { struct POINT *points; unsigned long length; }


decompiled_code:
__int64 __fastcall add_syncpoint(__int64 a1, __int64 a2, _QWORD *a3, double a4)
{
__int64 v6; // rbx
__int64 result; // rax
double v8; // [rsp+8h] [rbp-30h]

v6 = a1;
do
{
a2 += *a3;
__isoc99_sscanf();
debug(",%f", "%lf%jn");
result = add_point(v6, a4, v8);
v6 += 16LL;
}
while ( v6 != a1 + 48 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, _QWORD *a3, double a4)
{
__int64 v6; // rbx
__int64 result; // rax
double v8; // [rsp+8h] [rbp-30h]

v6 = a1;
do
{
a2 += *a3;
__isoc99_sscanf();
debug(",%f", "%lf%jn");
result = func11(v6, a4, v8);
v6 += 16LL;
}
while ( v6 != a1 + 48 );
return result;
}

original_code:
void add_syncpoint (struct CHANNEL *channels, double x, char *line, off_t *off) {
 uint8_t c;
 double y;
 for (c = 0; c < 3; c++) {
  line += *off;
  sscanf(line, "%lf%jn", &y, off);
  debug(",%f", y);
  add_point(&(channels[c]), x, y);
 }
}

canonical_original_code:
void add_syncpoint(struct CHANNEL *channels, double x, char *line, long *off) {
 unsigned char c;
 double y;
 for (c = 0; c < 3; c++) {
  line += *off;
  sscanf(line, "%lf%jn", &y, off);
  debug(",%f", y);
  add_point(&(channels[c]), x, y);
 }
}

variable_types: struct CHANNEL { struct POINT { double x; double y; } *points; unsigned long length; } *

variable_types:
line
char *

variable_types:
off
long *

variable_types:
c
unsigned char


printparsed

decompiled_code:
int __fastcall printparsed(__int64 a1)
{
FILE **v1; // r12
unsigned __int64 *v2; // r13
unsigned __int64 i; // rbx
int result; // eax

v1 = (FILE **)&channel;
v2 = (unsigned __int64 *)(a1 + 8);
do
{
for ( i = 0LL; *v2 > i; ++i )
result = printf(
"%s: %f/%f\n",
(const char *)*v1,
*(double *)(*(v2 - 1) + 16 * i),
*(double *)(*(v2 - 1) + 16 * i + 8));
++v1;
v2 += 2;
}
while ( &_bss_start != v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1)
{
FILE **v1; // r12
unsigned __int64 *v2; // r13
unsigned __int64 i; // rbx
int result; // eax

v1 = (FILE **)&channel;
v2 = (unsigned __int64 *)(a1 + 8);
do
{
for ( i = 0LL; *v2 > i; ++i )
result = printf(
"%s: %f/%f\n",
(const char *)*v1,
*(double *)(*(v2 - 1) + 16 * i),
*(double *)(*(v2 - 1) + 16 * i + 8));
++v1;
v2 += 2;
}
while ( &_bss_start != v1 );
return result;
}

original_code:
void printparsed (struct CHANNEL *channels) {
 uint8_t c;
 size_t i;
 for (c = 0; c < 3; c++)
  for (i = 0; i < channels[c].length; i++)
   printf(
     "%s: %f/%f\n",
     channel[c],
     channels[c].points[i].x,
     channels[c].points[i].y
   );
}

canonical_original_code:
void printparsed(struct CHANNEL *channels) {
 unsigned char c;
 unsigned long i;
 for (c = 0; c < 3; c++)
  for (i = 0; i < channels[c].length; i++)
   printf(
     "%s: %f/%f\n",
     channel[c],
     channels[c].points[i].x,
     channels[c].points[i].y
   );
}

variable_types:
i
unsigned long


printoutput

original_code:
void printoutput (struct GRADIENT *gradient) {
 size_t i;
 for (i = 0; i < gradient->length; i++)
  printf(
   "%lf: %d, %d, %d\n",
   (double)i / (double)gradient->length,
   gradient->x[i].y[0],
   gradient->x[i].y[1],
   gradient->x[i].y[2]
  );
}

canonical_original_code:
void printoutput(struct GRADIENT *gradient) {
 unsigned long i;
 for (i = 0; i < gradient->length; i++)
  printf(
   "%lf: %d, %d, %d\n",
   (double)i / (double)gradient->length,
   gradient->x[i].y[0],
   gradient->x[i].y[1],
   gradient->x[i].y[2]
  );
}

variable_types: struct GRADIENT { struct RGB24 { unsigned char y[3]; } *x; unsigned long length; } *

user_defined_types: struct GRADIENT { struct RGB24 *x; unsigned long length; }


fail

canonical_decompiled_code:
void __fastcall __noreturn func10(const char *a1)
{
perror(a1);
exit(1);
}


die

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

va_start(arg, a1);
vprintf(a1, arg);
puts(".");
exit(1);
}


fail

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
perror(a1);
exit(1);
}


rdtsc

decompiled_code:
unsigned __int64 rdtsc()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func7()
{
return __rdtsc();
}

original_code:
static __inline__ unsigned long long rdtsc(void)
{
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

canonical_original_code:
static __inline__ unsigned long long rdtsc()
{
    unsigned int hi; unsigned int lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int

return_type: unsigned long long


decompiled_code:
int __fastcall enumerate_array(_QWORD *a1)
{
int v1; // ebp
unsigned __int64 v2; // rbx
unsigned __int64 v3; // rax
int result; // eax

v1 = 100;
do
{
v2 = rdtsc();
*a1 = 1LL;
v3 = rdtsc();
result = printf("access address %#lx, latency = %lu\n", a1, v3 - v2);
--v1;
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func8(_QWORD *a1)
{
int v1; // ebp
unsigned __int64 v2; // rbx
unsigned __int64 v3; // rax
int result; // eax

v1 = 100;
do
{
v2 = func7();
*a1 = 1LL;
v3 = func7();
result = printf("access address %#lx, latency = %lu\n", a1, v3 - v2);
--v1;
}
while ( v1 );
return result;
}

original_code:
static inline void enumerate_array(unsigned long* array)
{
    int i = 0, j;
    unsigned long t_start, t_end;
    for( j = 0; j < 100; j+=1)
    {
        t_start = rdtsc();
        array[i] = 1;
 t_end = rdtsc();
 printf("access address %#lx, latency = %lu\n",(unsigned long) (array+i), t_end-t_start);
    }
    return;
}

canonical_original_code:
static inline void enumerate_array(unsigned long *array)
{
    int i = 0; int j;
    unsigned long t_start; unsigned long t_end;
    for( j = 0; j < 100; j+=1)
    {
        t_start = rdtsc();
        array[i] = 1;
 t_end = rdtsc();
 printf("access address %#lx, latency = %lu\n",(unsigned long) (array+i), t_end-t_start);
    }
    return;
}

variable_types:
array
unsigned long *

variable_types: i int

variable_types: j int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
_QWORD *v4; // rax
_QWORD *v5; // r13
unsigned int v6; // eax
int i; // er12
__int64 v8; // rax
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rax
__int64 v11; // rax
unsigned int v12; // ebx
float v13; // xmm0_4
unsigned __int64 v14; // r14
__int64 v15; // rax
float v16; // [rsp-1008h] [rbp-1008h]
float v17; // [rsp-1004h] [rbp-1004h]

v3 = 12;
while ( 1 )
{
v4 = malloc(0x10000uLL);
v5 = v4;
if ( v4 )
break;
if ( !--v3 )
{
fprintf(stderr, "Error: cannot allocate array size:%d\n", 0x2000LL);
exit(2);
}
}
v6 = (unsigned __int16)v4 & 0xFFF;
if ( v6 )
v5 = (_QWORD *)((char *)v5 - v6 + 4096);
printf("Array starting address is alligned; array=%#010lx\n", v5);
for ( i = 1; ; ++i )
{
v9 = rdtsc();
enumerate_array(v5);
v10 = rdtsc();
if ( i == 1 )
{
v8 = v10 - v9;
if ( v8 < 0 )
v16 = (float)(v8 & 1 | (unsigned int)((unsigned __int64)v8 >> 1))
+ (float)(v8 & 1 | (unsigned int)((unsigned __int64)v8 >> 1));
else
v16 = (float)(int)v8;
}
else if ( i > 1 )
{
v11 = v10 - v9;
if ( v11 < 0 )
v17 = (float)(v11 & 1 | (unsigned int)((unsigned __int64)v11 >> 1))
+ (float)(v11 & 1 | (unsigned int)((unsigned __int64)v11 >> 1));
else
v17 = (float)(int)v11;
printf("Initialize the L1D array: All array(store miss) takes %.2fus in total\n", v16);
printf("Restore the L1D array: All array(store hit) takes %.2f in total\n", v17);
printf("miss_hit_diff = %.2f; miss_hit_threshhold = %.2f\n", (float)(v16 - v17), 100.0);
sleep(2u);
puts("Start receiving the data");
v12 = 0;
while ( 1 )
{
v14 = rdtsc();
enumerate_array(v5);
v15 = rdtsc() - v14;
if ( v15 < 0 )
v13 = (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1))
+ (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1));
else
v13 = (float)(int)v15;
if ( (float)(v13 - v17) >= 100.0 )
v12 = 1;
printf(
"bit recieved is %d : latency is %.2f, diff is %.2f, miss_hit_threshold is %.2f\n",
v12,
v13,
(float)(v13 - v17),
100.0);
printf("%d\n", v12);
sleep(1u);
sleep(1u);
}
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
_QWORD *v4; // rax
_QWORD *v5; // r13
unsigned int v6; // eax
int i; // er12
__int64 v8; // rax
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rax
__int64 v11; // rax
unsigned int v12; // ebx
float v13; // xmm0_4
unsigned __int64 v14; // r14
__int64 v15; // rax
float v16; // [rsp-1008h] [rbp-1008h]
float v17; // [rsp-1004h] [rbp-1004h]

v3 = 12;
while ( 1 )
{
v4 = malloc(0x10000uLL);
v5 = v4;
if ( v4 )
break;
if ( !--v3 )
{
fprintf(stderr, "Error: cannot allocate array size:%d\n", 0x2000LL);
exit(2);
}
}
v6 = (unsigned __int16)v4 & 0xFFF;
if ( v6 )
v5 = (_QWORD *)((char *)v5 - v6 + 4096);
printf("Array starting address is alligned; array=%#010lx\n", v5);
for ( i = 1; ; ++i )
{
v9 = func7();
func8(v5);
v10 = func7();
if ( i == 1 )
{
v8 = v10 - v9;
if ( v8 < 0 )
v16 = (float)(v8 & 1 | (unsigned int)((unsigned __int64)v8 >> 1))
+ (float)(v8 & 1 | (unsigned int)((unsigned __int64)v8 >> 1));
else
v16 = (float)(int)v8;
}
else if ( i > 1 )
{
v11 = v10 - v9;
if ( v11 < 0 )
v17 = (float)(v11 & 1 | (unsigned int)((unsigned __int64)v11 >> 1))
+ (float)(v11 & 1 | (unsigned int)((unsigned __int64)v11 >> 1));
else
v17 = (float)(int)v11;
printf("Initialize the L1D array: All array(store miss) takes %.2fus in total\n", v16);
printf("Restore the L1D array: All array(store hit) takes %.2f in total\n", v17);
printf("miss_hit_diff = %.2f; miss_hit_threshhold = %.2f\n", (float)(v16 - v17), 100.0);
sleep(2u);
puts("Start receiving the data");
v12 = 0;
while ( 1 )
{
v14 = func7();
func8(v5);
v15 = func7() - v14;
if ( v15 < 0 )
v13 = (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1))
+ (float)(v15 & 1 | (unsigned int)((unsigned __int64)v15 >> 1));
else
v13 = (float)(int)v15;
if ( (float)(v13 - v17) >= 100.0 )
v12 = 1;
printf(
"bit recieved is %d : latency is %.2f, diff is %.2f, miss_hit_threshold is %.2f\n",
v12,
v13,
(float)(v13 - v17),
100.0);
printf("%d\n", v12);
sleep(1u);
sleep(1u);
}
}
}
}

original_code:
void main()
{
    unsigned long* array = ((void *)0), *array_0;
    int set = 0, line = 0, k = 0;
    int start = 0, end = 0;
    struct timeval before, after;
    int bad = 0, try = 0, i = 0, tmp = 0;
    int bit = 0;
    float miss_time_all, miss_time, hit_time_all, hit_time, miss_hit_ratio, miss_hit_diff,miss_hit_threshhold, time_elapsed;
    uint32_t buffer = 0;
    unsigned long temp;
    unsigned long t_start, t_end;
    do
    {
        array_0 = (unsigned long*)malloc(sizeof(unsigned long) * (4096 * 2));
    }while( array_0 == ((void *)0) && try++ <= 10);
    if( array_0 == ((void *)0))
    {
        fprintf( stderr, "Error: cannot allocate array size:%d\n", (4096 * 2));
        exit(2);
    }
    if( ( (unsigned long)array_0 & 0x0FFFUL ) != 0)
    {
        array = (unsigned long *) ((char*)array_0 + ( 0x0FFFUL - ((unsigned long)array_0 & 0x0FFFUL) ) + 1);
    }else{
        array = array_0;
    }
    printf("Array starting address is alligned; array=%#010lx\n", (unsigned long)array);
    for(i = 0; i < 2; i++)
    {
 t_start = rdtsc();
 enumerate_array(array);
 t_end = rdtsc();
        if( i == 0 )
        {
     miss_time_all = t_end - t_start;
        }
        else
        {
     hit_time_all = t_end - t_start;
        }
    }
    miss_hit_diff = miss_time_all - hit_time_all;
    miss_hit_threshhold = 100;
    printf( "Initialize the L1D array: All array(store miss) takes %.2fus in total\n", miss_time_all );
    printf( "Restore the L1D array: All array(store hit) takes %.2f in total\n", hit_time_all );
    printf( "miss_hit_diff = %.2f; miss_hit_threshhold = %.2f\n", miss_hit_diff, miss_hit_threshhold );
    sleep(2);
    printf( "Start receiving the data\n" );
    while(1)
    {
        time_elapsed = 0;
 t_start = rdtsc();
        enumerate_array(array);
 t_end = rdtsc();
 time_elapsed = t_end - t_start;
        if( time_elapsed - hit_time_all >= miss_hit_threshhold )
        {
            bit = 1;
        }
        printf( "bit recieved is %d : latency is %.2f, diff is %.2f,  miss_hit_threshold is %.2f\n", bit, time_elapsed, time_elapsed - hit_time_all, miss_hit_threshhold );
        printf("%d\n", bit);
 sleep(1);
 sleep(1);
    }
    free(array_0);
}

canonical_original_code:
void main()
{
    unsigned long *array = ((void *)0); unsigned long *array_0;
    int set = 0; int line = 0; int k = 0;
    int start = 0; int end = 0;
    struct timeval before; struct timeval after;
    int bad = 0; int try = 0; int i = 0; int tmp = 0;
    int bit = 0;
    float miss_time_all; float miss_time; float hit_time_all; float hit_time; float miss_hit_ratio; float miss_hit_diff; float miss_hit_threshhold; float time_elapsed;
    unsigned int buffer = 0;
    unsigned long temp;
    unsigned long t_start; unsigned long t_end;
    do
    {
        array_0 = (unsigned long *)malloc(sizeof(unsigned long) * (4096 * 2));
    }while( array_0 == ((void *)0) && try++ <= 10);
    if( array_0 == ((void *)0))
    {
        fprintf( stderr, "Error: cannot allocate array size:%d\n", (4096 * 2));
        exit(2);
    }
    if( ( (unsigned long)array_0 & 0x0FFFUL ) != 0)
    {
        array = (unsigned long *) ((char *)array_0 + ( 0x0FFFUL - ((unsigned long)array_0 & 0x0FFFUL) ) + 1);
    }else{
        array = array_0;
    }
    printf("Array starting address is alligned; array=%#010lx\n", (unsigned long)array);
    for(i = 0; i < 2; i++)
    {
 t_start = rdtsc();
 enumerate_array(array);
 t_end = rdtsc();
        if( i == 0 )
        {
     miss_time_all = t_end - t_start;
        }
        else
        {
     hit_time_all = t_end - t_start;
        }
    }
    miss_hit_diff = miss_time_all - hit_time_all;
    miss_hit_threshhold = 100;
    printf( "Initialize the L1D array: All array(store miss) takes %.2fus in total\n", miss_time_all );
    printf( "Restore the L1D array: All array(store hit) takes %.2f in total\n", hit_time_all );
    printf( "miss_hit_diff = %.2f; miss_hit_threshhold = %.2f\n", miss_hit_diff, miss_hit_threshhold );
    sleep(2);
    printf( "Start receiving the data\n" );
    while(1)
    {
        time_elapsed = 0;
 t_start = rdtsc();
        enumerate_array(array);
 t_end = rdtsc();
 time_elapsed = t_end - t_start;
        if( time_elapsed - hit_time_all >= miss_hit_threshhold )
        {
            bit = 1;
        }
        printf( "bit recieved is %d : latency is %.2f, diff is %.2f,  miss_hit_threshold is %.2f\n", bit, time_elapsed, time_elapsed - hit_time_all, miss_hit_threshhold );
        printf("%d\n", bit);
 sleep(1);
 sleep(1);
    }
    free(array_0);
}

variable_types: set int

variable_types: line int

variable_types: k int

variable_types: start int

variable_types: end int

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

variable_types: bad int

variable_types: try int

variable_types: tmp int

variable_types: bit int

variable_types:
buffer
unsigned int

variable_types:
temp
unsigned long


decompiled_code:
int get_rand_char()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return rand();
}

canonical_decompiled_code:
int func7()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return rand();
}

original_code:
static inline char get_rand_char()
{
    srand(time(((void *)0)));
    return (char) rand() & 0x0FF;
}

return_type: char


decompiled_code:
_BOOL8 __fastcall is_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:
static inline int is_file_exist(char* filename)
{
    int err = access(filename, 0);
    if( err == 0 )
    {
        return 1;
    }else{
        return 0;
    }
}

canonical_original_code:
static inline int is_file_exist(char *filename)
{
    int err = access(filename, 0);
    if( err == 0 )
    {
        return 1;
    }else{
        return 0;
    }
}

variable_types:
filename
char *

variable_types: err int


decompiled_code:
__int64 __fastcall get_array_index(int a1, int a2)
{
return (unsigned int)(8 * (a1 + (a2 << 6)));
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return (unsigned int)(8 * (a1 + (a2 << 6)));
}

original_code:
static inline int get_array_index(int set, int line)
{
    return set * (8) + line * (64) * (8);
}


canonical_decompiled_code:
int func9()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return rand();
}

canonical_original_code:
static inline char get_rand_char()
{
    srand(time(((void *)0)));
    return (char) rand() & 0x0FF;
}


canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
return (unsigned int)(8 * (a1 + (a2 << 6)));
}

canonical_original_code:
static inline int get_array_index(int set, int line)
{
    return set * (8) + line * (64) * (8);
}


repo: PerformanceHorizonGroup/csv-split

decompiled_code:
__int64 __fastcall write_gz_file(const char *a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v5; // rax
__int64 v6; // rbx
char v8[296]; // [rsp+0h] [rbp-128h] BYREF

if ( (_DWORD)a4 == -1 )
strncpy(v8, "wb", 0xFFuLL);
else
snprintf(v8, 0xFFuLL, "wb%d", a4);
v5 = gzopen(a1, v8);
v6 = v5;
if ( !v5 )
{
fprintf(stderr, "Error: Unable to open output file '%s'\n", a1);
exit(1);
}
if ( (int)gzwrite(v5, a2, (unsigned int)a3) != a3 )
{
fprintf(stderr, "Error: Unable to write all data to file '%s'\n", a1);
exit(1);
}
return gzclose(v6);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v5; // rax
__int64 v6; // rbx
char v8[296]; // [rsp+0h] [rbp-128h] BYREF

if ( (_DWORD)a4 == -1 )
strncpy(v8, "wb", 0xFFuLL);
else
snprintf(v8, 0xFFuLL, "wb%d", a4);
v5 = gzopen(a1, v8);
v6 = v5;
if ( !v5 )
{
fprintf(stderr, "Error: Unable to open output file '%s'\n", a1);
exit(1);
}
if ( (int)gzwrite(v5, a2, (unsigned int)a3) != a3 )
{
fprintf(stderr, "Error: Unable to write all data to file '%s'\n", a1);
exit(1);
}
return gzclose(v6);
}

original_code:
static void write_gz_file(const char *file, const char *data, size_t len, int level) {
 char mode[255];
 if(level == (-1)) {
  strncpy(mode,"wb",sizeof(mode));
 } else {
  snprintf(mode,sizeof(mode),"wb%d",level);
 }
 gzFile fp = gzopen(file,mode);
 if(!fp) {
  fprintf(stderr, "Error:  Unable to open output file '%s'\n", file);
  exit(1);
 }
 if(gzwrite(fp, data, len) != len) {
  fprintf(stderr, "Error:  Unable to write all data to file '%s'\n", file);
  exit(1);
 }
 gzclose(fp);
}

canonical_original_code:
static void write_gz_file(char *file, char *data, unsigned long len, int level) {
 char mode[255];
 if(level == (-1)) {
  strncpy(mode,"wb",sizeof(mode));
 } else {
  snprintf(mode,sizeof(mode),"wb%d",level);
 }
 struct gzFile_s *fp = gzopen(file,mode);
 if(!fp) {
  fprintf(stderr, "Error:  Unable to open output file '%s'\n", file);
  exit(1);
 }
 if(gzwrite(fp, data, len) != len) {
  fprintf(stderr, "Error:  Unable to write all data to file '%s'\n", file);
  exit(1);
 }
 gzclose(fp);
}

variable_types:
file
char *

variable_types:
data
char *

variable_types:
len
unsigned long

variable_types: level int

variable_types:
mode
char [255]

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *


decompiled_code:
int __fastcall exec_trigger(char *command, char *value, __int64 a3)
{
int result; // eax
int *v4; // rax
int *v5; // rax
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v6, 0x28uLL, "%lu", a3);
if ( setenv("CSV_PAYLOAD_FILE", value, 1) )
{
v4 = _errno_location();
fprintf(stderr, "Couldn't set environment variable: %d\n", (unsigned int)*v4);
}
if ( setenv("CSV_ROWCOUNT", v6, 1) )
{
v5 = _errno_location();
fprintf(stderr, "Couldn't set environment variable: %d\n", (unsigned int)*v5);
}
result = system(command);
if ( result )
result = fprintf(stderr, "Error: Couldn't execute system command \"%s\"\n", command);
return result;
}

canonical_decompiled_code:
int __fastcall func2(char *command, char *value, __int64 a3)
{
int result; // eax
int *v4; // rax
int *v5; // rax
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v6, 0x28uLL, "%lu", a3);
if ( setenv("CSV_PAYLOAD_FILE", value, 1) )
{
v4 = _errno_location();
fprintf(stderr, "Couldn't set environment variable: %d\n", (unsigned int)*v4);
}
if ( setenv("CSV_ROWCOUNT", v6, 1) )
{
v5 = _errno_location();
fprintf(stderr, "Couldn't set environment variable: %d\n", (unsigned int)*v5);
}
result = system(command);
if ( result )
result = fprintf(stderr, "Error: Couldn't execute system command \"%s\"\n", command);
return result;
}

original_code:
static void exec_trigger(const char *trigger_cmd, const char *job_file, unsigned long row_count) {
    char row_count_str[40];
    snprintf(row_count_str, sizeof(row_count_str), "%lu", row_count);
    if(setenv("CSV_PAYLOAD_FILE", job_file, 1) != 0) {
        fprintf(stderr, "Couldn't set environment variable: %d\n", (*__errno_location ()));
    }
    if(setenv("CSV_ROWCOUNT", row_count_str, 1) != 0) {
        fprintf(stderr, "Couldn't set environment variable: %d\n", (*__errno_location ()));
    }
    if(system(trigger_cmd) != 0) {
        fprintf(stderr, "Error:  Couldn't execute system command \"%s\"\n", trigger_cmd);
    }
}

canonical_original_code:
static void exec_trigger(char *trigger_cmd, char *job_file, unsigned long row_count) {
    char row_count_str[40];
    snprintf(row_count_str, sizeof(row_count_str), "%lu", row_count);
    if(setenv("CSV_PAYLOAD_FILE", job_file, 1) != 0) {
        fprintf(stderr, "Couldn't set environment variable: %d\n", (*__errno_location ()));
    }
    if(setenv("CSV_ROWCOUNT", row_count_str, 1) != 0) {
        fprintf(stderr, "Couldn't set environment variable: %d\n", (*__errno_location ()));
    }
    if(system(trigger_cmd) != 0) {
        fprintf(stderr, "Error:  Couldn't execute system command \"%s\"\n", trigger_cmd);
    }
}


decompiled_code:
void *__fastcall io_worker(void *a1)
{
char *v2; // rdi
char *v3; // rbx
char v5[1032]; // [rsp+0h] [rbp-428h] BYREF
char *src; // [rsp+408h] [rbp-20h] BYREF

while ( !(unsigned int)fq_get(a1, &src) )
{
v3 = src;
if ( *((_DWORD *)src + 72) )
{
snprintf(v5, 0x400uLL, "%s.gz", src);
write_gz_file(v5, *((_QWORD *)v3 + 34), *((_QWORD *)v3 + 35), *((unsigned int *)v3 + 72));
}
else
{
strncpy(v5, src, 0x400uLL);
write_file(v5, *((const void **)v3 + 34), *((_QWORD *)v3 + 35));
}
v2 = (char *)*((_QWORD *)v3 + 32);
if ( v2 )
exec_trigger(v2, v5, *((_QWORD *)v3 + 33));
free(*((void **)v3 + 34));
free(v3);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1)
{
char *v2; // rdi
char *v3; // rbx
char v5[1032]; // [rsp+0h] [rbp-428h] BYREF
char *src; // [rsp+408h] [rbp-20h] BYREF

while ( !(unsigned int)fq_get(a1, &src) )
{
v3 = src;
if ( *((_DWORD *)src + 72) )
{
snprintf(v5, 0x400uLL, "%s.gz", src);
func0(v5, *((_QWORD *)v3 + 34), *((_QWORD *)v3 + 35), *((unsigned int *)v3 + 72));
}
else
{
strncpy(v5, src, 0x400uLL);
func1(v5, *((const void **)v3 + 34), *((_QWORD *)v3 + 35));
}
v2 = (char *)*((_QWORD *)v3 + 32);
if ( v2 )
func2(v2, v5, *((_QWORD *)v3 + 33));
free(*((void **)v3 + 34));
free(v3);
}
return 0LL;
}

original_code:
void *io_worker(void *arg) {
    fqueue *queue = (fqueue*)arg;
    struct q_flush_item *item;
    void *itm_ptr;
    char out_file[1024];
    while(!fq_get(queue, &itm_ptr)) {
        item = itm_ptr;
        if(item->gzip) {
            snprintf(out_file, sizeof(out_file),"%s.gz", item->out_file);
            write_gz_file(out_file, item->str, item->len, item->gzip);
        } else {
         strncpy(out_file, item->out_file, sizeof(out_file));
         write_file(out_file, item->str, item->len);
        }
        if(item->trigger_cmd) {
            exec_trigger(item->trigger_cmd, out_file, item->row_count);
        }
        free(item->str);
        free(item);
    }
    return ((void *)0);
}

canonical_original_code:
void *io_worker(void *arg) {
    struct _fqueue *queue = (fqueue*)arg;
    struct q_flush_item *item;
    void *itm_ptr;
    char out_file[1024];
    while(!fq_get(queue, &itm_ptr)) {
        item = itm_ptr;
        if(item->gzip) {
            snprintf(out_file, sizeof(out_file),"%s.gz", item->out_file);
            write_gz_file(out_file, item->str, item->len, item->gzip);
        } else {
         strncpy(out_file, item->out_file, sizeof(out_file));
         write_file(out_file, item->str, item->len);
        }
        if(item->trigger_cmd) {
            exec_trigger(item->trigger_cmd, out_file, item->row_count);
        }
        free(item->str);
        free(item);
    }
    return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct _fqueue { struct queue_item { void *data; struct queue_item *next; } *items; struct queue_item *first; unsigned int max_len; unsigned int len; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } full; union pthread_cond_t empty; } *

variable_types: struct q_flush_item { char out_file[255]; char *trigger_cmd; unsigned long row_count; char *str; unsigned long len; int gzip; } *

return_type: void *

user_defined_types: struct _fqueue { struct queue_item *items; struct queue_item *first; unsigned int max_len; unsigned int len; union pthread_mutex_t mutex; union pthread_cond_t full; union pthread_cond_t empty; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


original_code:
void flush_file(struct csv_context *ctx, unsigned int use_ovr) {
    struct q_flush_item *q_item = malloc(sizeof(struct q_flush_item));
    size_t flush_len = use_ovr && ctx->opos ? ctx->opos : (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
    sprintf(q_item->out_file, "%s%s.%05d", ctx->out_path, ctx->in_prefix, ++ctx->on_file);
    if(*ctx->trigger_cmd) {
     q_item->trigger_cmd = (const char *)ctx->trigger_cmd;
    } else {
     q_item->trigger_cmd = ((void *)0);
    }
    q_item->row_count = ctx->row;
    q_item->str = cbuf_duplen(ctx->csv_buf, &flush_len);
    q_item->len = flush_len;
    q_item->gzip = ctx->gzip;
    (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos=ctx->header_len);
    ctx->row = ctx->count_header ? 1 : 0;
    ctx->opos = 0;
    fq_add(&ctx->io_queue, (void*)q_item);
}

canonical_original_code:
void flush_file(struct csv_context *ctx, unsigned int use_ovr) {
    struct q_flush_item *q_item = malloc(sizeof(struct q_flush_item));
    unsigned long flush_len = use_ovr && ctx->opos ? ctx->opos : (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
    sprintf(q_item->out_file, "%s%s.%05d", ctx->out_path, ctx->in_prefix, ++ctx->on_file);
    if(*ctx->trigger_cmd) {
     q_item->trigger_cmd = (char *)ctx->trigger_cmd;
    } else {
     q_item->trigger_cmd = ((void *)0);
    }
    q_item->row_count = ctx->row;
    q_item->str = cbuf_duplen(ctx->csv_buf, &flush_len);
    q_item->len = flush_len;
    q_item->gzip = ctx->gzip;
    (((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos=ctx->header_len);
    ctx->row = ctx->count_header ? 1 : 0;
    ctx->opos = 0;
    fq_add(&ctx->io_queue, (void *)q_item);
}

variable_types: struct csv_context { char in_file[255]; char *in_prefix; char trigger_cmd[255]; int from_stdin; unsigned int on_file; unsigned long row; unsigned long max_rows; unsigned long opos; int gcol; int gzip; unsigned short use_header; unsigned short count_header; unsigned int header_len; unsigned int put_comma; char *gcol_buf; char *csv_buf; struct _fqueue { struct queue_item { void *data; struct queue_item *next; } *items; struct queue_item *first; unsigned int max_len; unsigned int len; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } full; union pthread_cond_t empty; } io_queue; unsigned int thread_count; unsigned long *io_threads; struct csv_parser { int pstate; int quoted; unsigned long spaces; unsigned char *entry_buf; unsigned long entry_pos; unsigned long entry_size; int status; unsigned char options; unsigned char quote_char; unsigned char delim_char; int (*is_space)(unsigned char); int (*is_term)(unsigned char); unsigned long blk_size; void *(*malloc_func)(unsigned long); void *(*realloc_func)(void *, unsigned long); void (*free_func)(void *); } parser; } *

user_defined_types: struct csv_context { char in_file[255]; char *in_prefix; char trigger_cmd[255]; int from_stdin; unsigned int on_file; unsigned long row; unsigned long max_rows; unsigned long opos; int gcol; int gzip; unsigned short use_header; unsigned short count_header; unsigned int header_len; unsigned int put_comma; char *gcol_buf; char *csv_buf; struct _fqueue io_queue; unsigned int thread_count; unsigned long *io_threads; struct csv_parser parser; }

user_defined_types: struct q_flush_item { char out_file[255]; char *trigger_cmd; unsigned long row_count; char *str; unsigned long len; int gzip; }

user_defined_types: struct _cbufhdr { unsigned long size; unsigned long pos; char buf[]; }


original_code:
void cb_row(int c, void *data) {
    struct csv_context *ctx = (struct csv_context*)data;
    ctx->csv_buf = cbuf_putc(ctx->csv_buf, '\n');
    if(ctx->use_header && !ctx->header_len) {
        ctx->header_len = (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
        if(ctx->count_header) {
            ctx->row++;
        }
    } else {
        ctx->row++;
    }
    if(ctx->row >= ctx->max_rows) {
        if(ctx->gcol >= 0) {
            ctx->opos = (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
        } else {
            flush_file(ctx, 0);
        }
    }
    ctx->col=0;
    ctx->put_comma = 0;
}

canonical_original_code:
void cb_row(int c, void *data) {
    struct csv_context *ctx = (struct csv_context*)data;
    ctx->csv_buf = cbuf_putc(ctx->csv_buf, '\n');
    if(ctx->use_header && !ctx->header_len) {
        ctx->header_len = (((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
        if(ctx->count_header) {
            ctx->row++;
        }
    } else {
        ctx->row++;
    }
    if(ctx->row >= ctx->max_rows) {
        if(ctx->gcol >= 0) {
            ctx->opos = (((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos);
        } else {
            flush_file(ctx, 0);
        }
    }
    ctx->col=0;
    ctx->put_comma = 0;
}

variable_types: c int

variable_types:
data
void *

user_defined_types: struct csv_parser { int pstate; int quoted; unsigned long spaces; unsigned char *entry_buf; unsigned long entry_pos; unsigned long entry_size; int status; unsigned char options; unsigned char quote_char; unsigned char delim_char; int (*is_space)(unsigned char); int (*is_term)(unsigned char); unsigned long blk_size; void *(*malloc_func)(unsigned long); void *(*realloc_func)(void *, unsigned long); void (*free_func)(void *); }

user_defined_types: struct queue_item { void *data; struct queue_item *next; }

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


original_code:
static inline void cb_col(void *s, size_t len, void *data) {
    struct csv_context *ctx = (struct csv_context *)data;
    size_t cnt;
    if(ctx->put_comma) {
        ctx->csv_buf = cbuf_putc(ctx->csv_buf, ',');
    }
    ctx->put_comma = 1;
    if(ctx->gcol > -1 && ctx->col == ctx->gcol) {
        if(!ctx->use_header || ctx->header_len) {
            if(ctx->gcol_buf && ctx->opos && memcmp(ctx->gcol_buf, s, len) != 0) {
                flush_file(ctx, 1);
            } else if(!ctx->gcol_buf) {
                ctx->gcol_buf = cbuf_init(len);
            }
            ctx->gcol_buf = cbuf_setlen(ctx->gcol_buf, (const char*)s, len);
        }
    }
    while((cnt = csv_write(((((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->buf)+(((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)), ((((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->size)-(((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)), s, len)) > ((((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->size)-(((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos))) {
        ctx->csv_buf = cbuf_double(ctx->csv_buf);
    }
    (((cbufhdr*)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)+=cnt;
    ctx->col++;
}

canonical_original_code:
static inline void cb_col(void *s, unsigned long len, void *data) {
    struct csv_context *ctx = (struct csv_context *)data;
    unsigned long cnt;
    if(ctx->put_comma) {
        ctx->csv_buf = cbuf_putc(ctx->csv_buf, ',');
    }
    ctx->put_comma = 1;
    if(ctx->gcol > -1 && ctx->col == ctx->gcol) {
        if(!ctx->use_header || ctx->header_len) {
            if(ctx->gcol_buf && ctx->opos && memcmp(ctx->gcol_buf, s, len) != 0) {
                flush_file(ctx, 1);
            } else if(!ctx->gcol_buf) {
                ctx->gcol_buf = cbuf_init(len);
            }
            ctx->gcol_buf = cbuf_setlen(ctx->gcol_buf, (char *)s, len);
        }
    }
    while((cnt = csv_write(((((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->buf)+(((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)), ((((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->size)-(((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)), s, len)) > ((((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->size)-(((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos))) {
        ctx->csv_buf = cbuf_double(ctx->csv_buf);
    }
    (((struct _cbufhdr *)(ctx->csv_buf-(sizeof(cbufhdr))))->pos)+=cnt;
    ctx->col++;
}

variable_types:
s
void *

variable_types:
cnt
unsigned long


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return printf("Usage: %s [options] FILE\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return printf("Usage: %s [options] FILE\n", a1);
}

original_code:
void print_usage(char *exec) {
    printf("Usage:  %s [options] FILE\n", exec);
}

variable_types:
exec
char *


decompiled_code:
__int64 __fastcall parse_args(char *s, int argc, char **argv)
{
int v4; // eax
unsigned __int64 v5; // rax
const char **v6; // rdx
char *v7; // rax
bool v8; // zf
char *v9; // rax
const char *v10; // rsi
size_t v12; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v4 = getopt_long(argc, argv, "g:n:v:i:z::hd::", &g_long_opts, longind);
if ( v4 == -1 )
break;
if ( !v4 && !strcmp("stdin", *(&g_long_opts.name + 4 * longind[0])) )
*((_DWORD *)s + 194) = 1;
}
v5 = *((_QWORD *)s + 100);
if ( !v5 )
{
fwrite("Must specify the --num-rows (-n) argument!\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
if ( *((_WORD *)s + 413) && v5 <= 1 )
{
fwrite("--num-rows must be > 1 if we're counting headers as rows!\n", 1uLL, 0x3AuLL, stderr);
exit(1);
}
v6 = (const char **)&argv[optind];
if ( !*v6 || !**v6 )
{
fwrite("Must specify a file to process or a prefix to use if reading from STDIN!\n", 1uLL, 0x49uLL, stderr);
exit(1);
}
++optind;
strcpy(s, *v6);
v7 = strrchr(s, 47);
v8 = v7 == 0LL;
v9 = v7 + 1;
if ( v8 )
v9 = s;
*((_QWORD *)s + 64) = v9;
v10 = argv[optind];
if ( v10 )
{
if ( *v10 )
{
strcpy(s + 255, v10);
v12 = strlen(s + 255);
if ( s[v12 + 255] != 48 )
*(_WORD *)&s[v12 + 255] = 47;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s, int argc, char **argv)
{
int v4; // eax
unsigned __int64 v5; // rax
const char **v6; // rdx
char *v7; // rax
bool v8; // zf
char *v9; // rax
const char *v10; // rsi
size_t v12; // rax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v4 = getopt_long(argc, argv, "g:n:v:i:z::hd::", &g_long_opts, longind);
if ( v4 == -1 )
break;
if ( !v4 && !strcmp("stdin", *(&g_long_opts.name + 4 * longind[0])) )
*((_DWORD *)s + 194) = 1;
}
v5 = *((_QWORD *)s + 100);
if ( !v5 )
{
fwrite("Must specify the --num-rows (-n) argument!\n", 1uLL, 0x2BuLL, stderr);
exit(1);
}
if ( *((_WORD *)s + 413) && v5 <= 1 )
{
fwrite("--num-rows must be > 1 if we're counting headers as rows!\n", 1uLL, 0x3AuLL, stderr);
exit(1);
}
v6 = (const char **)&argv[optind];
if ( !*v6 || !**v6 )
{
fwrite("Must specify a file to process or a prefix to use if reading from STDIN!\n", 1uLL, 0x49uLL, stderr);
exit(1);
}
++optind;
strcpy(s, *v6);
v7 = strrchr(s, 47);
v8 = v7 == 0LL;
v9 = v7 + 1;
if ( v8 )
v9 = s;
*((_QWORD *)s + 64) = v9;
v10 = argv[optind];
if ( v10 )
{
if ( *v10 )
{
strcpy(s + 255, v10);
v12 = strlen(s + 255);
if ( s[v12 + 255] != 48 )
*(_WORD *)&s[v12 + 255] = 47;
}
}
return 0LL;
}

original_code:
int parse_args(struct csv_context *ctx, int argc, char **argv) {
    int opt, opt_idx, intval;
    char *ptr;
    while((opt = getopt_long(argc, argv, "g:n:v:i:z::hd::", g_long_opts, &opt_idx)) != -1) {
        switch(opt) {
            case 't':
                strncpy(ctx->trigger_cmd, optarg, sizeof(ctx->trigger_cmd));
          break;
            case 'g':
                ctx->gcol = atoi(optarg);
                break;
            case 'n':
                intval = atoi(optarg);
                if(intval < 1) {
                    fprintf(stderr, "Number of lines per file must be a positive integer!\n");
                    exit(1);
                }
                ctx->max_rows = intval;
                break;
            case 'i':
                if(optarg) {
                 intval = atoi(optarg);
                 if(intval < 1 || intval > 10) {
                  fprintf(stderr, "Thread count must be in range %d - %d\n",
                    1, 10);
                  exit(1);
                 }
                 ctx->thread_count = intval;
                }
                break;
            case 'z':
             ctx->gzip = (-1);
             if(optarg) {
              intval = atoi(optarg);
              if(intval >= 1 && intval <= 9) {
               ctx->gzip = intval;
              } else {
               fprintf(stderr, "Unknown compression level: %d\n", intval);
              }
             }
             break;
            case 'd':
                ctx->use_header = 1;
                if(optarg) {
                    intval = atoi(optarg);
                    ctx->count_header = intval != 0;
                }
                break;
            case 'h':
            case '?':
                print_usage(argv[0]);
                exit(1);
            case 'v':
                printf("csv-split " "0.1.1" "\n");
                exit(0);
            case 0:
                if(!strcmp("stdin", g_long_opts[opt_idx].name)) {
                    ctx->from_stdin = 1;
                }
                break;
        }
    }
    if(!ctx->max_rows) {
        fprintf(stderr, "Must specify the --num-rows (-n) argument!\n");
        exit(1);
    }
    if(ctx->count_header && ctx->max_rows < 2) {
        fprintf(stderr, "--num-rows must be > 1 if we're counting headers as rows!\n");
        exit(1);
    }
    if(!argv[optind] || !*argv[optind]) {
        fprintf(stderr, "Must specify a file to process or a prefix to use if reading from STDIN!\n");
        exit(1);
    }
    strcpy(ctx->in_file, argv[optind++]);
    if((ptr = strrchr(ctx->in_file, '/'))) {
        ctx->in_prefix = ptr+1;
    } else {
        ctx->in_prefix = ctx->in_file;
    }
    if(argv[optind] && *argv[optind]) {
     strcpy(ctx->out_path, argv[optind]);
     if(ctx->out_path[strlen(ctx->out_path)]-1 != '/') {
      strcat(ctx->out_path, "/");
     }
    }
    return 0;
}

canonical_original_code:
int parse_args(struct csv_context *ctx, int argc, char **argv) {
    int opt; int opt_idx; int intval;
    char *ptr;
    while((opt = getopt_long(argc, argv, "g:n:v:i:z::hd::", g_long_opts, &opt_idx)) != -1) {
        switch(opt) {
            case 't':
                strncpy(ctx->trigger_cmd, optarg, sizeof(ctx->trigger_cmd));
          break;
            case 'g':
                ctx->gcol = atoi(optarg);
                break;
            case 'n':
                intval = atoi(optarg);
                if(intval < 1) {
                    fprintf(stderr, "Number of lines per file must be a positive integer!\n");
                    exit(1);
                }
                ctx->max_rows = intval;
                break;
            case 'i':
                if(optarg) {
                 intval = atoi(optarg);
                 if(intval < 1 || intval > 10) {
                  fprintf(stderr, "Thread count must be in range %d - %d\n",
                    1, 10);
                  exit(1);
                 }
                 ctx->thread_count = intval;
                }
                break;
            case 'z':
             ctx->gzip = (-1);
             if(optarg) {
              intval = atoi(optarg);
              if(intval >= 1 && intval <= 9) {
               ctx->gzip = intval;
              } else {
               fprintf(stderr, "Unknown compression level: %d\n", intval);
              }
             }
             break;
            case 'd':
                ctx->use_header = 1;
                if(optarg) {
                    intval = atoi(optarg);
                    ctx->count_header = intval != 0;
                }
                break;
            case 'h':
            case '?':
                print_usage(argv[0]);
                exit(1);
            case 'v':
                printf("csv-split " "0.1.1" "\n");
                exit(0);
            case 0:
                if(!strcmp("stdin", g_long_opts[opt_idx].name)) {
                    ctx->from_stdin = 1;
                }
                break;
        }
    }
    if(!ctx->max_rows) {
        fprintf(stderr, "Must specify the --num-rows (-n) argument!\n");
        exit(1);
    }
    if(ctx->count_header && ctx->max_rows < 2) {
        fprintf(stderr, "--num-rows must be > 1 if we're counting headers as rows!\n");
        exit(1);
    }
    if(!argv[optind] || !*argv[optind]) {
        fprintf(stderr, "Must specify a file to process or a prefix to use if reading from STDIN!\n");
        exit(1);
    }
    strcpy(ctx->in_file, argv[optind++]);
    if((ptr = strrchr(ctx->in_file, '/'))) {
        ctx->in_prefix = ptr+1;
    } else {
        ctx->in_prefix = ctx->in_file;
    }
    if(argv[optind] && *argv[optind]) {
     strcpy(ctx->out_path, argv[optind]);
     if(ctx->out_path[strlen(ctx->out_path)]-1 != '/') {
      strcat(ctx->out_path, "/");
     }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: opt int

variable_types: intval int

variable_types:
ptr
char *


decompiled_code:
void __fastcall spool_threads(__int64 a1)
{
__int64 v1; // rbx

if ( *(_DWORD *)(a1 + 1024) )
{
v1 = 0LL;
do
{
if ( pthread_create((pthread_t *)(*(_QWORD *)(a1 + 1032) + 8 * v1), 0LL, io_worker, (void *)(a1 + 856)) )
{
fwrite("Couldn't start background IO threads!\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
++v1;
}
while ( *(_DWORD *)(a1 + 1024) > (unsigned int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1)
{
__int64 v1; // rbx

if ( *(_DWORD *)(a1 + 1024) )
{
v1 = 0LL;
do
{
if ( pthread_create((pthread_t *)(*(_QWORD *)(a1 + 1032) + 8 * v1), 0LL, io_worker, (void *)(a1 + 856)) )
{
fwrite("Couldn't start background IO threads!\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
++v1;
}
while ( *(_DWORD *)(a1 + 1024) > (unsigned int)v1 );
}
}

original_code:
void spool_threads(struct csv_context *ctx) {
    int i;
    for(i=0;i<ctx->thread_count;i++) {
        if(pthread_create(&ctx->io_threads[i], ((void *)0), io_worker, (void*)&ctx->io_queue) != 0) {
            fprintf(stderr, "Couldn't start background IO threads!\n");
            exit(1);
        }
    }
}

canonical_original_code:
void spool_threads(struct csv_context *ctx) {
    int i;
    for(i=0;i<ctx->thread_count;i++) {
        if(pthread_create(&ctx->io_threads[i], ((void *)0), io_worker, (void *)&ctx->io_queue) != 0) {
            fprintf(stderr, "Couldn't start background IO threads!\n");
            exit(1);
        }
    }
}

variable_types: i int


decompiled_code:
int __fastcall join_threads(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

if ( *(_DWORD *)(a1 + 1024) )
{
v1 = 0LL;
do
result = pthread_join(*(_QWORD *)(*(_QWORD *)(a1 + 1032) + 8 * v1++), 0LL);
while ( *(_DWORD *)(a1 + 1024) > (unsigned int)v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
__int64 v1; // rbx
int result; // eax

if ( *(_DWORD *)(a1 + 1024) )
{
v1 = 0LL;
do
result = pthread_join(*(_QWORD *)(*(_QWORD *)(a1 + 1032) + 8 * v1++), 0LL);
while ( *(_DWORD *)(a1 + 1024) > (unsigned int)v1 );
}
return result;
}

original_code:
void join_threads(struct csv_context *ctx) {
    int i=0;
    for(i=0;i<ctx->thread_count;i++) {
        pthread_join(ctx->io_threads[i], ((void *)0));
    }
}

canonical_original_code:
void join_threads(struct csv_context *ctx) {
    int i = 0;
    for(i=0;i<ctx->thread_count;i++) {
        pthread_join(ctx->io_threads[i], ((void *)0));
    }
}


original_code:
void context_init(struct csv_context *ctx) {
    ctx->csv_buf = cbuf_init(1024*1000*10);
    fq_init(&ctx->io_queue, 20);
    if(csv_init(&ctx->parser, 0) != 0) {
        fprintf(stderr, "Couldn't initialize CSV parser!\n");
        exit(1);
    }
    csv_set_blk_size(&ctx->parser, 1024);
    ctx->thread_count = 1;
    ctx->gcol = -1;
    ctx->gzip = 0;
    ctx->use_header = 0;
    ctx->count_header = 0;
    ctx->header_len = 0;
}


original_code:
void context_free(struct csv_context *ctx) {
    cbuf_free(ctx->csv_buf);
    if(ctx->gcol_buf) {
        cbuf_free(ctx->gcol_buf);
    }
    fq_free(&ctx->io_queue);
    csv_free(&ctx->parser);
    free(ctx->io_threads);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4[145]; // [rsp+0h] [rbp-488h] BYREF

memset(v4, 0, 0x470uLL);
context_init((__int64)v4);
parse_args((char *)v4, argc, (char **)argv);
v4[129] = (__int64)malloc(8LL * LODWORD(v4[128]));
if ( !v4[129] )
{
fwrite("Error: Couldn't allocate thread storage.\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
spool_threads((__int64)v4);
process_csv((__int64)v4);
fq_fin(&v4[107]);
join_threads((__int64)v4);
exec_trigger((char *)&v4[65], (char *)&value, 0LL);
context_free((__int64)v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4[145]; // [rsp+0h] [rbp-488h] BYREF

memset(v4, 0, 0x470uLL);
func11((__int64)v4);
func8((char *)v4, argc, (char **)argv);
v4[129] = (__int64)malloc(8LL * LODWORD(v4[128]));
if ( !v4[129] )
{
fwrite("Error: Couldn't allocate thread storage.\n", 1uLL, 0x2AuLL, stderr);
exit(1);
}
func9((__int64)v4);
func13((__int64)v4);
fq_fin(&v4[107]);
func10((__int64)v4);
func2((char *)&v4[65], (char *)&value, 0LL);
func12((__int64)v4);
return 0;
}

original_code:
int main(int argc, char **argv) {
 struct csv_context ctx;
    memset(&ctx, 0, sizeof(struct csv_context));
    context_init(&ctx);
    parse_args(&ctx, argc, argv);
    ctx.io_threads = malloc(ctx.thread_count * sizeof *ctx.io_threads);
    if(!ctx.io_threads) {
        fprintf(stderr, "Error:  Couldn't allocate thread storage.\n");
        exit(1);
    }
    spool_threads(&ctx);
    process_csv(&ctx);
    fq_fin(&ctx.io_queue);
    join_threads(&ctx);
    exec_trigger(ctx.trigger_cmd, "", 0);
    context_free(&ctx);
    return 0;
}

variable_types: struct csv_context { char in_file[255]; char *in_prefix; char trigger_cmd[255]; int from_stdin; unsigned int on_file; unsigned long row; unsigned long max_rows; unsigned long opos; int gcol; int gzip; unsigned short use_header; unsigned short count_header; unsigned int header_len; unsigned int put_comma; char *gcol_buf; char *csv_buf; struct _fqueue { struct queue_item { void *data; struct queue_item *next; } *items; struct queue_item *first; unsigned int max_len; unsigned int len; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } full; union pthread_cond_t empty; } io_queue; unsigned int thread_count; unsigned long *io_threads; struct csv_parser { int pstate; int quoted; unsigned long spaces; unsigned char *entry_buf; unsigned long entry_pos; unsigned long entry_size; int status; unsigned char options; unsigned char quote_char; unsigned char delim_char; int (*is_space)(unsigned char); int (*is_term)(unsigned char); unsigned long blk_size; void *(*malloc_func)(unsigned long); void *(*realloc_func)(void *, unsigned long); void (*free_func)(void *); } parser; }


decompiled_code:
void __fastcall cbuf_free(__int64 a1)
{
if ( a1 )
free((void *)(a1 - 16));
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
if ( a1 )
free((void *)(a1 - 16));
}

original_code:
void cbuf_free(cbuf buf) {
 if(buf == ((void *)0)) return;
 free(buf-sizeof(cbufhdr));
}

canonical_original_code:
void cbuf_free(char *buf) {
 if(buf == ((void *)0)) return;
 free(buf-sizeof(cbufhdr));
}

variable_types:
buf
char *


decompiled_code:
__int64 __fastcall cbuf_size(__int64 a1)
{
return *(_QWORD *)(a1 - 16);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(_QWORD *)(a1 - 16);
}

original_code:
size_t cbuf_size(cbuf buf) {
 cbufhdr *ch = (cbufhdr*)(buf-sizeof(cbufhdr));
 return ch->size;
}

canonical_original_code:
unsigned long cbuf_size(char *buf) {
 struct _cbufhdr *ch = (cbufhdr*)(buf-sizeof(cbufhdr));
 return ch->size;
}

variable_types: struct _cbufhdr { unsigned long size; unsigned long pos; char buf[]; } *

return_type: unsigned long


decompiled_code:
void *__fastcall cbuf_dup(void *src, size_t *a2)
{
size_t v2; // r13
void *v3; // rbp

if ( !src )
return 0LL;
v2 = *((_QWORD *)src - 1);
v3 = malloc(v2);
*a2 = v2;
memcpy(v3, src, *((_QWORD *)src - 1));
return v3;
}

canonical_decompiled_code:
void *__fastcall func2(void *src, size_t *a2)
{
size_t v2; // r13
void *v3; // rbp

if ( !src )
return 0LL;
v2 = *((_QWORD *)src - 1);
v3 = malloc(v2);
*a2 = v2;
memcpy(v3, src, *((_QWORD *)src - 1));
return v3;
}

original_code:
char *cbuf_dup(cbuf buf, size_t *size) {
 if(!buf) return ((void *)0);
 char *ret = (char*)malloc((((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos));
 *size = (((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos);
 memcpy(ret, buf, (((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos));
 return ret;
}

canonical_original_code:
char *cbuf_dup(char *buf, unsigned long *size) {
 if(!buf) return ((void *)0);
 char *ret = (char*)malloc((((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos));
 *size = (((struct _cbufhdr *)(buf-(sizeof(cbufhdr))))->pos);
 memcpy(ret, buf, (((struct _cbufhdr *)(buf-(sizeof(cbufhdr))))->pos));
 return ret;
}

variable_types:
size
unsigned long *

variable_types:
ret
char *

return_type: char *


decompiled_code:
void *__fastcall cbuf_duplen(void *src, size_t *a2)
{
size_t v2; // rax
size_t v3; // r12
void *v4; // rbp

if ( !src )
return 0LL;
v2 = *((_QWORD *)src - 1);
if ( *a2 > v2 )
*a2 = v2;
v3 = *a2;
v4 = malloc(*a2);
memcpy(v4, src, v3);
return v4;
}

canonical_decompiled_code:
void *__fastcall func3(void *src, size_t *a2)
{
size_t v2; // rax
size_t v3; // r12
void *v4; // rbp

if ( !src )
return 0LL;
v2 = *((_QWORD *)src - 1);
if ( *a2 > v2 )
*a2 = v2;
v3 = *a2;
v4 = malloc(*a2);
memcpy(v4, src, v3);
return v4;
}

original_code:
char *cbuf_duplen(cbuf buf, size_t *len) {
    if(!buf) return ((void *)0);
    if(*len > (((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos)) {
        *len = (((cbufhdr*)(buf-(sizeof(cbufhdr))))->pos);
    }
    char *ret = (char*)malloc(*len);
    memcpy(ret, buf, *len);
    return ret;
}

canonical_original_code:
char *cbuf_duplen(char *buf, unsigned long *len) {
    if(!buf) return ((void *)0);
    if(*len > (((struct _cbufhdr *)(buf-(sizeof(cbufhdr))))->pos)) {
        *len = (((struct _cbufhdr *)(buf-(sizeof(cbufhdr))))->pos);
    }
    char *ret = (char*)malloc(*len);
    memcpy(ret, buf, *len);
    return ret;
}

variable_types:
len
unsigned long *


decompiled_code:
__int64 __fastcall fq_init(__int64 a1, int a2)
{
unsigned int v2; // ebp

if ( !a1 )
return 22;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 160) = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 168) >> 3));
*(_DWORD *)(a1 + 24) = a2;
v2 = pthread_cond_init((pthread_cond_t *)(a1 + 120), 0LL);
if ( !v2 )
{
v2 = pthread_cond_init((pthread_cond_t *)(a1 + 72), 0LL);
if ( v2 )
{
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
}
else
{
v2 = pthread_mutex_init((pthread_mutex_t *)(a1 + 32), 0LL);
if ( v2 )
{
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
pthread_cond_destroy((pthread_cond_t *)(a1 + 72));
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
unsigned int v2; // ebp

if ( !a1 )
return 22;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 160) = 0LL;
memset(
(void *)((a1 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 168) >> 3));
*(_DWORD *)(a1 + 24) = a2;
v2 = pthread_cond_init((pthread_cond_t *)(a1 + 120), 0LL);
if ( !v2 )
{
v2 = pthread_cond_init((pthread_cond_t *)(a1 + 72), 0LL);
if ( v2 )
{
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
}
else
{
v2 = pthread_mutex_init((pthread_mutex_t *)(a1 + 32), 0LL);
if ( v2 )
{
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
pthread_cond_destroy((pthread_cond_t *)(a1 + 72));
}
}
}
return v2;
}

original_code:
int fq_init(fqueue *queue, size_t max_len) {
    int ret = 0;
    if(queue == ((void *)0)) {
        return 22;
    }
    memset(queue, 0, sizeof(fqueue));
    queue->max_len = max_len;
    if((ret = pthread_cond_init(&queue->empty, ((void *)0)))) {
        return ret;
    }
    if((ret = pthread_cond_init(&queue->full, ((void *)0)))) {
        pthread_cond_destroy(&queue->empty);
        return ret;
    }
    if((ret = pthread_mutex_init(&queue->mutex, ((void *)0)))) {
        pthread_cond_destroy(&queue->empty);
        pthread_cond_destroy(&queue->full);
        return ret;
    }
    return 0;
}

canonical_original_code:
int fq_init(struct _fqueue *queue, unsigned long max_len) {
    int ret = 0;
    if(queue == ((void *)0)) {
        return 22;
    }
    memset(queue, 0, sizeof(fqueue));
    queue->max_len = max_len;
    if((ret = pthread_cond_init(&queue->empty, ((void *)0)))) {
        return ret;
    }
    if((ret = pthread_cond_init(&queue->full, ((void *)0)))) {
        pthread_cond_destroy(&queue->empty);
        return ret;
    }
    if((ret = pthread_mutex_init(&queue->mutex, ((void *)0)))) {
        pthread_cond_destroy(&queue->empty);
        pthread_cond_destroy(&queue->full);
        return ret;
    }
    return 0;
}

variable_types: struct _fqueue { struct queue_item { void *data; struct queue_item *next; } *items; struct queue_item *first; unsigned int max_len; unsigned int len; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } full; union pthread_cond_t empty; } *

variable_types: ret int


decompiled_code:
__int64 __fastcall fq_add(__int64 a1, __int64 a2)
{
pthread_mutex_t *v2; // rbp
_QWORD *v3; // rax
__int64 v4; // rdx

v2 = (pthread_mutex_t *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 32));
while ( *(_DWORD *)(a1 + 28) == *(_DWORD *)(a1 + 24) )
pthread_cond_wait((pthread_cond_t *)(a1 + 72), v2);
v3 = malloc(0x10uLL);
*v3 = a2;
v3[1] = 0LL;
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
*(_QWORD *)(v4 + 8) = v3;
*(_QWORD *)(a1 + 16) = v3;
}
else
{
*(_QWORD *)(a1 + 16) = v3;
*(_QWORD *)(a1 + 8) = v3;
}
if ( !*(_DWORD *)(a1 + 28) )
pthread_cond_broadcast((pthread_cond_t *)(a1 + 120));
++*(_DWORD *)(a1 + 28);
pthread_mutex_unlock(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
pthread_mutex_t *v2; // rbp
_QWORD *v3; // rax
__int64 v4; // rdx

v2 = (pthread_mutex_t *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 32));
while ( *(_DWORD *)(a1 + 28) == *(_DWORD *)(a1 + 24) )
pthread_cond_wait((pthread_cond_t *)(a1 + 72), v2);
v3 = malloc(0x10uLL);
*v3 = a2;
v3[1] = 0LL;
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
*(_QWORD *)(v4 + 8) = v3;
*(_QWORD *)(a1 + 16) = v3;
}
else
{
*(_QWORD *)(a1 + 16) = v3;
*(_QWORD *)(a1 + 8) = v3;
}
if ( !*(_DWORD *)(a1 + 28) )
pthread_cond_broadcast((pthread_cond_t *)(a1 + 120));
++*(_DWORD *)(a1 + 28);
pthread_mutex_unlock(v2);
return 0LL;
}

original_code:
int fq_add(fqueue *queue, void *data) {
    struct queue_item *item;
    pthread_mutex_lock(&queue->mutex);
    while(queue->len == queue->max_len) {
        pthread_cond_wait(&queue->full, &queue->mutex);
    }
    item = malloc(sizeof(struct queue_item));
    item->data = data;
    item->next = ((void *)0);
    if(queue->last == ((void *)0)) {
        queue->last = item;
        queue->first = item;
    } else {
        queue->last->next = item;
        queue->last = item;
    }
    if(queue->len == 0) {
        pthread_cond_broadcast(&queue->empty);
    }
    queue->len++;
    pthread_mutex_unlock(&queue->mutex);
    return 0;
}

canonical_original_code:
int fq_add(struct _fqueue *queue, void *data) {
    struct queue_item *item;
    pthread_mutex_lock(&queue->mutex);
    while(queue->len == queue->max_len) {
        pthread_cond_wait(&queue->full, &queue->mutex);
    }
    item = malloc(sizeof(struct queue_item));
    item->data = data;
    item->next = ((void *)0);
    if(queue->last == ((void *)0)) {
        queue->last = item;
        queue->first = item;
    } else {
        queue->last->next = item;
        queue->last = item;
    }
    if(queue->len == 0) {
        pthread_cond_broadcast(&queue->empty);
    }
    queue->len++;
    pthread_mutex_unlock(&queue->mutex);
    return 0;
}

variable_types:
data
void *

variable_types: struct queue_item { void *data; struct queue_item *next; } *


original_code:
int fq_get(fqueue *queue, void **data) {
    if(queue == ((void *)0)) {
        return 22;
    }
    pthread_mutex_lock(&queue->mutex);
    while(queue->first == ((void *)0)) {
        pthread_cond_wait(&queue->empty, &queue->mutex);
    }
    if(queue->first->data) {
     *data = queue->first->data;
     struct queue_item *tmp = queue->first;
     queue->first = queue->first->next;
     queue->len--;
     free(tmp);
     if(queue->first == ((void *)0)) {
      queue->last = ((void *)0);
      queue->len = 0;
     }
    } else {
        *data = ((void *)0);
    }
    if(queue->len == queue->max_len - 1) {
     pthread_cond_broadcast(&queue->full);
    }
    pthread_mutex_unlock(&queue->mutex);
    return *data == ((void *)0);
}

canonical_original_code:
int fq_get(struct _fqueue *queue, void **data) {
    if(queue == ((void *)0)) {
        return 22;
    }
    pthread_mutex_lock(&queue->mutex);
    while(queue->first == ((void *)0)) {
        pthread_cond_wait(&queue->empty, &queue->mutex);
    }
    if(queue->first->data) {
     *data = queue->first->data;
     struct queue_item *tmp = queue->first;
     queue->first = queue->first->next;
     queue->len--;
     free(tmp);
     if(queue->first == ((void *)0)) {
      queue->last = ((void *)0);
      queue->len = 0;
     }
    } else {
        *data = ((void *)0);
    }
    if(queue->len == queue->max_len - 1) {
     pthread_cond_broadcast(&queue->full);
    }
    pthread_mutex_unlock(&queue->mutex);
    return *data == ((void *)0);
}

variable_types:
data
void **


decompiled_code:
__int64 __fastcall fq_fin(__int64 a1)
{
return fq_add(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return func1(a1, 0LL);
}

original_code:
int fq_fin(fqueue *queue) {
    return fq_add(queue, ((void *)0));
}

canonical_original_code:
int fq_fin(struct _fqueue *queue) {
    return fq_add(queue, ((void *)0));
}


decompiled_code:
__int64 __fastcall fq_free(__int64 a1)
{
pthread_mutex_t *v2; // r12
_QWORD *v3; // rbx
void *v4; // rdi

if ( !a1 )
return 22LL;
v2 = (pthread_mutex_t *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 32));
v3 = *(_QWORD **)(a1 + 8);
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
pthread_mutex_unlock(v2);
pthread_mutex_destroy(v2);
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
pthread_cond_destroy((pthread_cond_t *)(a1 + 72));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
pthread_mutex_t *v2; // r12
_QWORD *v3; // rbx
void *v4; // rdi

if ( !a1 )
return 22LL;
v2 = (pthread_mutex_t *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 32));
v3 = *(_QWORD **)(a1 + 8);
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
pthread_mutex_unlock(v2);
pthread_mutex_destroy(v2);
pthread_cond_destroy((pthread_cond_t *)(a1 + 120));
pthread_cond_destroy((pthread_cond_t *)(a1 + 72));
return 0LL;
}

original_code:
int fq_free(fqueue *queue) {
    if(queue == ((void *)0)) {
        return 22;
    }
    pthread_mutex_lock(&queue->mutex);
    struct queue_item *cur = queue->first, *next;
    while(cur) {
        next = cur->next;
        free(cur);
        cur = next;
    }
    pthread_mutex_unlock(&queue->mutex);
    pthread_mutex_destroy(&queue->mutex);
    pthread_cond_destroy(&queue->empty);
    pthread_cond_destroy(&queue->full);
    return 0;
}

canonical_original_code:
int fq_free(struct _fqueue *queue) {
    if(queue == ((void *)0)) {
        return 22;
    }
    pthread_mutex_lock(&queue->mutex);
    struct queue_item *cur = queue->first; struct queue_item *next;
    while(cur) {
        next = cur->next;
        free(cur);
        cur = next;
    }
    pthread_mutex_unlock(&queue->mutex);
    pthread_mutex_destroy(&queue->mutex);
    pthread_cond_destroy(&queue->empty);
    pthread_cond_destroy(&queue->full);
    return 0;
}


ChckPreIndex

repo: PerseusDL/morpheus

decompiled_code:
__int64 __fastcall ChckPreIndex(__int64 a1, const char *a2, unsigned int a3, unsigned int a4, __int64 a5)
{
int v8; // eax
__int64 v9; // rdx
char dest[49]; // [rsp+7h] [rbp-31h] BYREF

if ( (int)Xstrlen(a2) > 8 )
a4 = 0;
strncpy(dest, a2, 8uLL);
dest[8] = 0;
v8 = binlook(a1, dest, a3, 24LL, a4, a5);
if ( v8 < 0 )
return -1LL;
v9 = 0LL;
if ( v8 )
v9 = *(_QWORD *)(a1 + 24LL * v8 + 16);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2, unsigned int a3, unsigned int a4, __int64 a5)
{
int v8; // eax
__int64 v9; // rdx
char dest[49]; // [rsp+7h] [rbp-31h] BYREF

if ( (int)Xstrlen(a2) > 8 )
a4 = 0;
strncpy(dest, a2, 8uLL);
dest[8] = 0;
v8 = binlook(a1, dest, a3, 24LL, a4, a5);
if ( v8 < 0 )
return -1LL;
v9 = 0LL;
if ( v8 )
v9 = *(_QWORD *)(a1 + 24LL * v8 + 16);
return v9;
}

original_code:
long
ChckPreIndex(endtags *etags, char *tag, int ntags, int exact_match, int (*scmp )())
{
 int rval;
 long roff;
 char curtag[8 +1];
 if( Xstrlen(tag) > 8 ) exact_match = 0;
 strncpy(curtag,tag,8);
 curtag[8] = 0;
 rval = binlook( (char *)etags , curtag , ntags , (int)sizeof *etags , exact_match ,scmp );
 if( rval < 0 )
  return((long) -1 );
 if( ! rval )
  roff = 0;
 else
  roff = (etags+rval)->tagoffset;
return( roff );
}

canonical_original_code:
long ChckPreIndex(struct endtags *etags, char *tag, int ntags, int exact_match, int (*scmp)())
{
 int rval;
 long roff;
 char curtag[-1];
 if( Xstrlen(tag) > 8 ) exact_match = 0;
 strncpy(curtag,tag,8);
 curtag[8] = 0;
 rval = binlook( (char *)etags , curtag , ntags , (int)sizeof *etags , exact_match ,scmp );
 if( rval < 0 )
  return((long) -1 );
 if( ! rval )
  roff = 0;
 else
  roff = (etags+rval)->tagoffset;
return( roff );
}

variable_types: struct endtags { char tagstring[-1]; long tagoffset; } *

variable_types:
tag
char *

variable_types: ntags int

variable_types:
scmp
int (*)()

variable_types: rval int

variable_types: roff long

variable_types:
curtag
char [-1]

return_type: long


decompiled_code:
__int64 __fastcall new_person(__int64 a1, char a2)
{
__int64 result; // rax

result = 8 * (a2 & 7u);
*(_BYTE *)(a1 + 1) = (8 * (a2 & 7)) | *(_BYTE *)(a1 + 1) & 0xC7;
return result;
}

original_code:
void
new_person(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_person = (val);
}

canonical_original_code:
void new_person(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_person = (val);
}

variable_types: struct gk_string { struct word_form { unsigned int f_voice; unsigned int f_mood; unsigned int f_tense; unsigned int f_person; unsigned int f_number; unsigned int f_case; unsigned int f_degree; unsigned int f_gender; } gs_forminfo; unsigned int gs_steminfo; unsigned int gs_derivtype; short gs_dialect; unsigned int gs_geogregion; unsigned char gs_morphflags[12]; char st_domains[-1]; char gs_gkstring[60]; } *

variable_types:
val
unsigned long

user_defined_types: struct gk_string { struct word_form gs_forminfo; unsigned int gs_steminfo; unsigned int gs_derivtype; short gs_dialect; unsigned int gs_geogregion; unsigned char gs_morphflags[12]; char st_domains[-1]; char gs_gkstring[60]; }


decompiled_code:
__int64 __fastcall new_number(_DWORD *a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 7) << 14;
*a1 = result | *a1 & 0xFFFE3FFF;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 7) << 14;
*a1 = result | *a1 & 0xFFFE3FFF;
return result;
}

original_code:
void
new_number(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_number = (val);
}

canonical_original_code:
void new_number(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_number = (val);
}

user_defined_types: struct word_form { unsigned int f_voice; unsigned int f_mood; unsigned int f_tense; unsigned int f_person; unsigned int f_number; unsigned int f_case; unsigned int f_degree; unsigned int f_gender; }


original_code:
void
new_case(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_case |= val;
}

canonical_original_code:
void new_case(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_case |= val;
}


decompiled_code:
__int64 __fastcall new_tense(_WORD *a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 0xF) << 7;
*a1 = ((a2 & 0xF) << 7) | *a1 & 0xF87F;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_WORD *a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 0xF) << 7;
*a1 = ((a2 & 0xF) << 7) | *a1 & 0xF87F;
return result;
}

original_code:
void
new_tense(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_tense = (val);
}

canonical_original_code:
void new_tense(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_tense = (val);
}


decompiled_code:
__int64 __fastcall new_mood(_BYTE *a1, char a2)
{
__int64 result; // rax

result = 8 * (a2 & 0xFu);
*a1 = (8 * (a2 & 0xF)) | *a1 & 0x87;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_BYTE *a1, char a2)
{
__int64 result; // rax

result = 8 * (a2 & 0xFu);
*a1 = (8 * (a2 & 0xF)) | *a1 & 0x87;
return result;
}

original_code:
void
new_mood(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_mood = (val);
}

canonical_original_code:
void new_mood(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_mood = (val);
}


decompiled_code:
__int64 __fastcall new_degree(__int64 a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 3) << 7;
*(_WORD *)(a1 + 2) = ((a2 & 3) << 7) | *(_WORD *)(a1 + 2) & 0xFE7F;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, char a2)
{
__int64 result; // rax

result = (unsigned __int8)(a2 & 3) << 7;
*(_WORD *)(a1 + 2) = ((a2 & 3) << 7) | *(_WORD *)(a1 + 2) & 0xFE7F;
return result;
}

original_code:
void
new_degree(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_degree = (val);
}

canonical_original_code:
void new_degree(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_degree = (val);
}


original_code:
void
new_gender(gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_gender |= val;
}

canonical_original_code:
void new_gender(struct gk_string *gstr, unsigned long val)
{
  ((gstr)->gs_forminfo).f_gender |= val;
}


decompiled_code:
void __fastcall new_dialect(__int64 a1, __int16 a2)
{
*(_WORD *)(a1 + 12) |= a2;
}

original_code:
void
new_dialect(gk_string *gstr, unsigned long val)
{
  (gstr)->gs_dialect |= (Dialect)val;
}

canonical_original_code:
void new_dialect(struct gk_string *gstr, unsigned long val)
{
  (gstr)->gs_dialect |= (short)val;
}


decompiled_code:
void __fastcall new_region(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 16) |= a2;
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 16) |= a2;
}

original_code:
void
new_region(gk_string *gstr, unsigned long val)
{
  (gstr)->gs_geogregion |= (GeogRegion)val;
}

canonical_original_code:
void new_region(struct gk_string *gstr, unsigned long val)
{
  (gstr)->gs_geogregion |= (unsigned int)val;
}


decompiled_code:
__int64 __fastcall new_morphflags(__int64 a1)
{
return add_morphflag(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return add_morphflag(a1 + 20);
}

original_code:
void
new_morphflags(gk_string *gstr, unsigned long val)
{
  add_morphflag((gstr)->gs_morphflags,(int)val);
}

canonical_original_code:
void new_morphflags(struct gk_string *gstr, unsigned long val)
{
  add_morphflag((gstr)->gs_morphflags,(int)val);
}


decompiled_code:
void __fastcall new_stemtype(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4) = a2;
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4) = a2;
}

original_code:
void
new_stemtype(gk_string *gstr, unsigned long val)
{
  (gstr)->gs_steminfo = (Stemtype)val;
}

canonical_original_code:
void new_stemtype(struct gk_string *gstr, unsigned long val)
{
  (gstr)->gs_steminfo = (unsigned int)val;
}


decompiled_code:
__int64 new_domain()
{
return add_domain();
}

canonical_decompiled_code:
__int64 func12()
{
return add_domain();
}

original_code:
void
new_domain(gk_string *gstr, unsigned long val)
{
  add_domain(gstr,(int)val);
}

canonical_original_code:
void new_domain(struct gk_string *gstr, unsigned long val)
{
  add_domain(gstr,(int)val);
}


decompiled_code:
void __fastcall new_derivtype(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 8) = a2;
}

canonical_decompiled_code:
void __fastcall func13(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 8) = a2;
}

original_code:
void
new_derivtype(gk_string *gstr, unsigned long val)
{
  (gstr)->gs_derivtype = (Derivtype)val;
}

canonical_original_code:
void new_derivtype(struct gk_string *gstr, unsigned long val)
{
  (gstr)->gs_derivtype = (unsigned int)val;
}


parsefield

decompiled_code:
const char *__fastcall parsefield(char *a1, char *a2, int a3, int a4)
{
char *v4; // r13
char v7; // bp
const char *i; // rbx
const unsigned __int16 **v9; // r12
const char *v10; // rbx
unsigned int v12; // eax
char j; // al

v4 = a2;
*a2 = 0;
v7 = *a1;
for ( i = a1; *i; v7 = *i )
{
if ( v7 == a3 )
break;
v9 = _ctype_b_loc();
if ( ((*v9)[v7] & 0x2000) != 0 )
break;
++i;
*v4++ = v7;
if ( a4 <= (int)i + ~(_DWORD)a1 )
{
v12 = Xstrlen(i);
fprintf(stderr, "Hey %d chars; %d s [%s] left!\n", (unsigned int)a4, v12, i);
*v4 = 0;
for ( j = *i; *i; j = *++i )
{
if ( ((*v9)[j] & 0x2000) != 0 )
break;
}
break;
}
}
*v4 = 0;
if ( ((*_ctype_b_loc())[*i] & 0x2000) != 0 )
v10 = (const char *)&unk_8C0;
else
v10 = &i[-(*i == 0) + 1];
return v10;
}

canonical_decompiled_code:
const char *__fastcall func1(char *a1, char *a2, int a3, int a4)
{
char *v4; // r13
char v7; // bp
const char *i; // rbx
const unsigned __int16 **v9; // r12
const char *v10; // rbx
unsigned int v12; // eax
char j; // al

v4 = a2;
*a2 = 0;
v7 = *a1;
for ( i = a1; *i; v7 = *i )
{
if ( v7 == a3 )
break;
v9 = _ctype_b_loc();
if ( ((*v9)[v7] & 0x2000) != 0 )
break;
++i;
*v4++ = v7;
if ( a4 <= (int)i + ~(_DWORD)a1 )
{
v12 = Xstrlen(i);
fprintf(stderr, "Hey %d chars; %d s [%s] left!\n", (unsigned int)a4, v12, i);
*v4 = 0;
for ( j = *i; *i; j = *++i )
{
if ( ((*v9)[j] & 0x2000) != 0 )
break;
}
break;
}
}
*v4 = 0;
if ( ((*_ctype_b_loc())[*i] & 0x2000) != 0 )
v10 = (const char *)&unk_8C0;
else
v10 = &i[-(*i == 0) + 1];
return v10;
}

original_code:
char *
parsefield(char *s, char *buf, int c, int len)
{
 int i;
 *buf = 0;
 for(i=0;*s&&*s!=c&&!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISspace);i++) {
  *buf++ = *s++;
  if( i >= len ) {
   fprintf(stderr,"Hey %d chars; %d s [%s] left!\n", len , Xstrlen(s) ,s );
   *buf = 0;
   while(*s&&!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISspace)) s++;
   break;
  }
 }
 *buf = 0;
 if( ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISspace) ) return("");
 if(*s) s++;
 return(s);
}

canonical_original_code:
char *parsefield(char *s, char *buf, int c, int len)
{
 int i;
 *buf = 0;
 for(i=0;*s&&*s!=c&&!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISspace);i++) {
  *buf++ = *s++;
  if( i >= len ) {
   fprintf(stderr,"Hey %d chars; %d s [%s] left!\n", len , Xstrlen(s) ,s );
   *buf = 0;
   while(*s&&!((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISspace)) s++;
   break;
  }
 }
 *buf = 0;
 if( ((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISspace) ) return("");
 if(*s) s++;
 return(s);
}

variable_types:
s
char *

variable_types:
buf
char *

variable_types: c int

variable_types: len int

variable_types: i int