decompiled_code:
__int64 rt_stdin_open()
{
__int64 v0; // r12

v0 = nm_nmalloc(32LL, "iiab/rt_std.c", 80LL, "rt_stdin_open");
*(_DWORD *)v0 = rt_stdin_magic();
*(_QWORD *)(v0 + 8) = rt_stdin_prefix();
*(_QWORD *)(v0 + 16) = rt_stdin_description();
return v0;
}

original_code:
RT_LLD rt_stdin_open (char *p_url, char *comment, char *password, int keep,
        char *basename)
{
     RT_STDD rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",80,__PRETTY_FUNCTION__);
     rt->magic = rt_stdin_magic();
     rt->prefix = rt_stdin_prefix();
     rt->description = rt_stdin_description();
     return rt;
}

canonical_original_code:
void *rt_stdin_open(char *p_url, char *comment, char *password, int keep, char *basename)
{
     struct rt_std_desc *rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",80,__PRETTY_FUNCTION__);
     rt->magic = rt_stdin_magic();
     rt->prefix = rt_stdin_prefix();
     rt->description = rt_stdin_description();
     return rt;
}

variable_types:
comment
char *

variable_types: keep int

variable_types: struct rt_std_desc { int magic; char *prefix; char *description; int fd; } *


repo: SystemGarden/habitat

decompiled_code:
__int64 rt_stdout_open()
{
__int64 v0; // r12

v0 = nm_nmalloc(32LL, "iiab/rt_std.c", 94LL, "rt_stdout_open");
*(_DWORD *)v0 = rt_stdout_magic();
*(_QWORD *)(v0 + 8) = rt_stdout_prefix();
*(_QWORD *)(v0 + 16) = rt_stdout_description();
return v0;
}

original_code:
RT_LLD rt_stdout_open (char *p_url, char *comment, char *password, int keep,
         char *basename)
{
     RT_STDD rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",94,__PRETTY_FUNCTION__);
     rt->magic = rt_stdout_magic();
     rt->prefix = rt_stdout_prefix();
     rt->description = rt_stdout_description();
     return rt;
}

canonical_original_code:
void *rt_stdout_open(char *p_url, char *comment, char *password, int keep, char *basename)
{
     struct rt_std_desc *rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",94,__PRETTY_FUNCTION__);
     rt->magic = rt_stdout_magic();
     rt->prefix = rt_stdout_prefix();
     rt->description = rt_stdout_description();
     return rt;
}

variable_types:
password
char *

variable_types:
basename
char *

return_type: void *

user_defined_types: struct rt_std_desc { int magic; char *prefix; char *description; int fd; }


decompiled_code:
__int64 rt_stderr_open()
{
__int64 v0; // r12

v0 = nm_nmalloc(32LL, "iiab/rt_std.c", 108LL, "rt_stderr_open");
*(_DWORD *)v0 = rt_stderr_magic();
*(_QWORD *)(v0 + 8) = rt_stderr_prefix();
*(_QWORD *)(v0 + 16) = rt_stderr_description();
return v0;
}

original_code:
RT_LLD rt_stderr_open (char *p_url, char *comment, char *password, int keep,
         char *basename)
{
     RT_STDD rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",108,__PRETTY_FUNCTION__);
     rt->magic = rt_stderr_magic();
     rt->prefix = rt_stderr_prefix();
     rt->description = rt_stderr_description();
     return rt;
}

canonical_original_code:
void *rt_stderr_open(char *p_url, char *comment, char *password, int keep, char *basename)
{
     struct rt_std_desc *rt;
     rt = nm_nmalloc(sizeof(struct rt_std_desc),"iiab/rt_std.c",108,__PRETTY_FUNCTION__);
     rt->magic = rt_stderr_magic();
     rt->prefix = rt_stderr_prefix();
     rt->description = rt_stderr_description();
     return rt;
}


decompiled_code:
__int64 rt_std_read()
{
int v0; // eax
int v1; // ebx
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // r12
_BYTE v6[4120]; // [rsp+0h] [rbp-1018h] BYREF

v0 = read(0, v6, 0x1000uLL);
if ( v0 == -1 )
{
v4 = 0LL;
elog_fprintf(5LL, "iiab/rt_std.c", 184LL, "rt_std_read", "unable to read %d bytes from stdin", 4096LL);
}
else
{
v1 = v0;
v2 = nm_xnmalloc(16LL, "iiab/rt_std.c", 190LL, "rt_std_read");
v3 = nm_xnmemdup(v6, v1 + 1, "iiab/rt_std.c", 191LL, "rt_std_read");
*(_DWORD *)v2 = v1;
*(_QWORD *)(v2 + 8) = v3;
v4 = itree_really_create("iiab/rt_std.c", 193LL, "rt_std_read");
itree_append(v4, v2);
}
return v4;
}

canonical_decompiled_code:
__int64 func17()
{
int v0; // eax
int v1; // ebx
__int64 v2; // rbp
__int64 v3; // rax
__int64 v4; // r12
_BYTE v6[4120]; // [rsp+0h] [rbp-1018h] BYREF

v0 = read(0, v6, 0x1000uLL);
if ( v0 == -1 )
{
v4 = 0LL;
elog_fprintf(5LL, "iiab/rt_std.c", 184LL, "rt_std_read", "unable to read %d bytes from stdin", 4096LL);
}
else
{
v1 = v0;
v2 = nm_xnmalloc(16LL, "iiab/rt_std.c", 190LL, "rt_std_read");
v3 = nm_xnmemdup(v6, v1 + 1, "iiab/rt_std.c", 191LL, "rt_std_read");
*(_DWORD *)v2 = v1;
*(_QWORD *)(v2 + 8) = v3;
v4 = itree_really_create("iiab/rt_std.c", 193LL, "rt_std_read");
itree_append(v4, v2);
}
return v4;
}

original_code:
ITREE *rt_std_read (RT_LLD lld, int seq, int offset)
{
     ROUTE_BUF *storebuf;
     char buffer[4096];
     int r;
     ITREE *buflist;
     r = read(0, buffer, 4096);
     if (r == -1) {
   elog_fprintf(ERROR,"iiab/rt_std.c",184,__PRETTY_FUNCTION__,"unable to read %d bytes from stdin", 4096);
   return ((void *)0);
     }
     storebuf = nm_xnmalloc(sizeof(ROUTE_BUF),"iiab/rt_std.c",190,__PRETTY_FUNCTION__);
     storebuf->buffer = nm_xnmemdup(buffer,r+1,"iiab/rt_std.c",191,__PRETTY_FUNCTION__);
     storebuf->buflen = r;
     buflist = itree_really_create("iiab/rt_std.c",193,__PRETTY_FUNCTION__);
     itree_append(buflist, storebuf);
     return buflist;
}

canonical_original_code:
struct itree_head *rt_std_read(void *lld, int seq, int offset)
{
     struct route_buffer *storebuf;
     char buffer[4096];
     int r;
     struct itree_head *buflist;
     r = read(0, buffer, 4096);
     if (r == -1) {
   elog_fprintf(ERROR,"iiab/rt_std.c",184,__PRETTY_FUNCTION__,"unable to read %d bytes from stdin", 4096);
   return ((void *)0);
     }
     storebuf = nm_xnmalloc(sizeof(ROUTE_BUF),"iiab/rt_std.c",190,__PRETTY_FUNCTION__);
     storebuf->buffer = nm_xnmemdup(buffer,r+1,"iiab/rt_std.c",191,__PRETTY_FUNCTION__);
     storebuf->buflen = r;
     buflist = itree_really_create("iiab/rt_std.c",193,__PRETTY_FUNCTION__);
     itree_append(buflist, storebuf);
     return buflist;
}

variable_types:
lld
void *

variable_types: seq int

variable_types: offset int

variable_types: struct route_buffer { int buflen; char *buffer; } *

variable_types:
buffer
char [4096]

variable_types: r int

variable_types: struct itree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *

user_defined_types: struct itree_head { struct rb_node *root; struct rb_node *node; }

user_defined_types: struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; }


decompiled_code:
__int64 rt_std_tread()
{
int v0; // eax
int v1; // ebx
void *v2; // rbp
__int64 v3; // r12
time_t v4; // rax
__int64 v5; // rax
_BYTE v7[4120]; // [rsp+0h] [rbp-1018h] BYREF

v0 = read(0, v7, 0x1000uLL);
if ( v0 == -1 )
{
v3 = 0LL;
elog_fprintf(5LL, "iiab/rt_std.c", 213LL, "rt_std_tread", "unable to read %d bytes from stdin", 4096LL);
}
else
{
v1 = v0;
v2 = (void *)nm_xnmalloc(v0 + 1, "iiab/rt_std.c", 217LL, "rt_std_tread");
memcpy(v2, v7, v1);
v3 = table_create_a(rt_std_tabschema);
table_addemptyrow(v3);
table_replacecurrentcell(v3, "data", v2);
v4 = time(0LL);
v5 = util_i32toa(v4);
table_replacecurrentcell(v3, "_time", v5);
table_freeondestroy(v3, v2);
}
return v3;
}

canonical_decompiled_code:
__int64 func18()
{
int v0; // eax
int v1; // ebx
void *v2; // rbp
__int64 v3; // r12
time_t v4; // rax
__int64 v5; // rax
_BYTE v7[4120]; // [rsp+0h] [rbp-1018h] BYREF

v0 = read(0, v7, 0x1000uLL);
if ( v0 == -1 )
{
v3 = 0LL;
elog_fprintf(5LL, "iiab/rt_std.c", 213LL, "rt_std_tread", "unable to read %d bytes from stdin", 4096LL);
}
else
{
v1 = v0;
v2 = (void *)nm_xnmalloc(v0 + 1, "iiab/rt_std.c", 217LL, "rt_std_tread");
memcpy(v2, v7, v1);
v3 = table_create_a(rt_std_tabschema);
table_addemptyrow(v3);
table_replacecurrentcell(v3, "data", v2);
v4 = time(0LL);
v5 = util_i32toa(v4);
table_replacecurrentcell(v3, "_time", v5);
table_freeondestroy(v3, v2);
}
return v3;
}

original_code:
TABLE rt_std_tread (RT_LLD lld, int seq, int offset)
{
     char buffer[4096], *buf;
     int r;
     TABLE tab;
     r = read(0, buffer, 4096);
     if (r == -1) {
   elog_fprintf(ERROR,"iiab/rt_std.c",213,__PRETTY_FUNCTION__,"unable to read %d bytes from stdin", 4096);
   return ((void *)0);
     }
     buf = nm_xnmalloc(r+1,"iiab/rt_std.c",217,__PRETTY_FUNCTION__);
     memcpy(buf, buffer, r);
     tab = table_create_a(rt_std_tabschema);
     table_addemptyrow(tab);
     table_replacecurrentcell(tab, "data", buf);
     table_replacecurrentcell(tab, "_time", util_i32toa(time(((void *)0))));
     table_freeondestroy(tab, buf);
     return tab;
}

canonical_original_code:
struct table_info *rt_std_tread(void *lld, int seq, int offset)
{
     char buffer[4096]; char *buf;
     int r;
     struct table_info *tab;
     r = read(0, buffer, 4096);
     if (r == -1) {
   elog_fprintf(ERROR,"iiab/rt_std.c",213,__PRETTY_FUNCTION__,"unable to read %d bytes from stdin", 4096);
   return ((void *)0);
     }
     buf = nm_xnmalloc(r+1,"iiab/rt_std.c",217,__PRETTY_FUNCTION__);
     memcpy(buf, buffer, r);
     tab = table_create_a(rt_std_tabschema);
     table_addemptyrow(tab);
     table_replacecurrentcell(tab, "data", buf);
     table_replacecurrentcell(tab, "_time", util_i32toa(time(((void *)0))));
     table_freeondestroy(tab, buf);
     return tab;
}

variable_types:
buf
char *

variable_types: struct table_info { int ncols; int nrows; int minrowkey; int maxrowkey; struct tree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *data; struct itree_head { struct rb_node *root; struct rb_node *node; } *colorder; struct tree_head *info; struct tree_head *infolookup; struct itree_head *tobegarbage; int refcount; struct itree_head *roworder; char separator; } *

user_defined_types: struct table_info { int ncols; int nrows; int minrowkey; int maxrowkey; struct tree_head *data; struct itree_head *colorder; struct tree_head *info; struct tree_head *infolookup; struct itree_head *tobegarbage; int refcount; struct itree_head *roworder; char separator; }

user_defined_types: struct tree_head { struct rb_node *root; struct rb_node *node; }


decompiled_code:
const char **__fastcall rt_std_from_lld(const char **a1)
{
int v1; // eax
const char *v3; // r15
const char *v4; // r14
const char *v5; // r13
const char *v6; // rbp
const char *v7; // rbx
const char *v8; // rax

if ( !a1 )
elog_fdie(6LL, "iiab/rt_std.c", 269LL, "rt_std_from_lld", "passed NULL low level descriptor");
v1 = *(_DWORD *)a1;
if ( v1 != 7822399 && v1 != 4299644 && v1 != 1053976 )
{
if ( !v1 )
elog_fprintf(
6LL,
"iiab/rt_std.c",
275LL,
"rt_std_from_lld",
"magic type of 0 encountered, possibly previously deleted\n");
v3 = rt_stderr_description();
v4 = rt_stderr_prefix();
v5 = rt_stdout_description();
v6 = rt_stdout_prefix();
v7 = rt_stdin_description();
v8 = rt_stdin_prefix();
elog_fdie(
6LL,
"iiab/rt_std.c",
277LL,
"rt_std_from_lld",
"magic type mismatch: we were given %s (%s [magic %d]) but can only handle either %s (%s) or %s (%s) or %s (%s)",
a1[1],
a1[2],
*(unsigned int *)a1,
v8,
v7,
v6,
v5,
v4,
v3);
}
return a1;
}

canonical_decompiled_code:
const char **__fastcall func19(const char **a1)
{
int v1; // eax
const char *v3; // r15
const char *v4; // r14
const char *v5; // r13
const char *v6; // rbp
const char *v7; // rbx
const char *v8; // rax

if ( !a1 )
elog_fdie(6LL, "iiab/rt_std.c", 269LL, "rt_std_from_lld", "passed NULL low level descriptor");
v1 = *(_DWORD *)a1;
if ( v1 != 7822399 && v1 != 4299644 && v1 != 1053976 )
{
if ( !v1 )
elog_fprintf(
6LL,
"iiab/rt_std.c",
275LL,
"rt_std_from_lld",
"magic type of 0 encountered, possibly previously deleted\n");
v3 = func8();
v4 = func7();
v5 = func5();
v6 = func4();
v7 = func2();
v8 = func1();
elog_fdie(
6LL,
"iiab/rt_std.c",
277LL,
"rt_std_from_lld",
"magic type mismatch: we were given %s (%s [magic %d]) but can only handle either %s (%s) or %s (%s) or %s (%s)",
a1[1],
a1[2],
*(unsigned int *)a1,
v8,
v7,
v6,
v5,
v4,
v3);
}
return a1;
}

original_code:
RT_STDD rt_std_from_lld(RT_LLD lld )
{
     if (!lld)
   elog_fdie(FATAL,"iiab/rt_std.c",269,__PRETTY_FUNCTION__,"passed NULL low level descriptor");
     if ( (((RT_STDD)lld)->magic != 4299644) &&
   (((RT_STDD)lld)->magic != 7822399) &&
   (((RT_STDD)lld)->magic != 1053976) ) {
          if (((RT_STDD)lld)->magic == 0)
        elog_fprintf(FATAL,"iiab/rt_std.c",275,__PRETTY_FUNCTION__,"magic type of 0 encountered, possibly " "previously deleted\n");
   elog_fdie(FATAL,"iiab/rt_std.c",277,__PRETTY_FUNCTION__,"magic type mismatch: we were given " "%s (%s [magic %d]) but can only handle either %s (%s) " "or %s (%s) or %s (%s)", ((RT_STDD)lld)->prefix, ((RT_STDD)lld)->description, ((RT_STDD)lld)->magic, rt_stdin_prefix(), rt_stdin_description(), rt_stdout_prefix(), rt_stdout_description(), rt_stderr_prefix(), rt_stderr_description());
     }
     return (RT_STDD) lld;
}

canonical_original_code:
struct rt_std_desc *rt_std_from_lld(void *lld)
{
     if (!lld)
   elog_fdie(FATAL,"iiab/rt_std.c",269,__PRETTY_FUNCTION__,"passed NULL low level descriptor");
     if ( (((struct rt_std_desc *)lld)->magic != 4299644) &&
   (((struct rt_std_desc *)lld)->magic != 7822399) &&
   (((struct rt_std_desc *)lld)->magic != 1053976) ) {
          if (((struct rt_std_desc *)lld)->magic == 0)
        elog_fprintf(FATAL,"iiab/rt_std.c",275,__PRETTY_FUNCTION__,"magic type of 0 encountered, possibly " "previously deleted\n");
   elog_fdie(FATAL,"iiab/rt_std.c",277,__PRETTY_FUNCTION__,"magic type mismatch: we were given " "%s (%s [magic %d]) but can only handle either %s (%s) " "or %s (%s) or %s (%s)", ((struct rt_std_desc *)lld)->prefix, ((struct rt_std_desc *)lld)->description, ((struct rt_std_desc *)lld)->magic, rt_stdin_prefix(), rt_stdin_description(), rt_stdout_prefix(), rt_stdout_description(), rt_stderr_prefix(), rt_stderr_description());
     }
     return (struct rt_std_desc *) lld;
}

return_type: struct rt_std_desc { int magic; char *prefix; char *description; int fd; } *


decompiled_code:
__int64 __fastcall rt_std_close(const char **a1)
{
const char **v1; // rax

v1 = rt_std_from_lld(a1);
*(_DWORD *)v1 = 0;
return nm_nfree(v1, "iiab/rt_std.c", 123LL, "rt_std_close");
}

canonical_decompiled_code:
__int64 __fastcall func20(const char **a1)
{
const char **v1; // rax

v1 = func19(a1);
*(_DWORD *)v1 = 0;
return nm_nfree(v1, "iiab/rt_std.c", 123LL, "rt_std_close");
}

original_code:
void rt_std_close (RT_LLD lld)
{
     RT_STDD rt;
     rt = rt_std_from_lld(lld);
     rt->magic = 0;
     nm_nfree(rt,"iiab/rt_std.c",123,__PRETTY_FUNCTION__);
}

canonical_original_code:
void rt_std_close(void *lld)
{
     struct rt_std_desc *rt;
     rt = rt_std_from_lld(lld);
     rt->magic = 0;
     nm_nfree(rt,"iiab/rt_std.c",123,__PRETTY_FUNCTION__);
}

return_type: void


decompiled_code:
__int64 __fastcall rt_std_write(const char **a1, const void *a2, int a3)
{
unsigned int v4; // er12
int *v6; // rax
char *v7; // rax

if ( *(_DWORD *)rt_std_from_lld(a1) == 1053976 )
{
v4 = write(2, a2, a3);
if ( v4 != -1 )
return v4;
}
else
{
v4 = write(1, a2, a3);
if ( v4 != -1 )
return v4;
}
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "write() system call returns -1: %s", v7);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func21(const char **a1, const void *a2, int a3)
{
unsigned int v4; // er12
int *v6; // rax
char *v7; // rax

if ( *(_DWORD *)func19(a1) == 1053976 )
{
v4 = write(2, a2, a3);
if ( v4 != -1 )
return v4;
}
else
{
v4 = write(1, a2, a3);
if ( v4 != -1 )
return v4;
}
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "write() system call returns -1: %s", v7);
return v4;
}

original_code:
int rt_std_write (RT_LLD lld, const void *buf, int buflen)
{
     int n;
     RT_STDD rt;
     rt = rt_std_from_lld(lld);
     if (rt->magic == 1053976)
   n = write(2, buf, buflen);
     else
   n = write(1, buf, buflen);
     if (n == -1)
   fprintf(stderr, "write() system call returns -1: %s",
    strerror((*__errno_location ())));
     return n;
}

canonical_original_code:
int rt_std_write(void *lld, void *buf, int buflen)
{
     int n;
     struct rt_std_desc *rt;
     rt = rt_std_from_lld(lld);
     if (rt->magic == 1053976)
   n = write(2, buf, buflen);
     else
   n = write(1, buf, buflen);
     if (n == -1)
   fprintf(stderr, "write() system call returns -1: %s",
    strerror((*__errno_location ())));
     return n;
}

variable_types:
buf
void *

variable_types: buflen int

variable_types: n int

return_type: int


decompiled_code:
_BOOL8 __fastcall rt_std_twrite(const char **a1, __int64 a2)
{
const char *v2; // rbp
_BOOL8 result; // rax
int v4; // eax
int v5; // er12

rt_std_from_lld(a1);
v2 = (const char *)table_outtable(a2);
result = 1LL;
if ( v2 )
{
v4 = strlen(v2);
v5 = rt_std_write(a1, v2, v4);
nm_nfree(v2, "iiab/rt_std.c", 157LL, "rt_std_twrite");
result = v5 != -1;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func22(const char **a1, __int64 a2)
{
const char *v2; // rbp
_BOOL8 result; // rax
int v4; // eax
int v5; // er12

func19(a1);
v2 = (const char *)table_outtable(a2);
result = 1LL;
if ( v2 )
{
v4 = strlen(v2);
v5 = func21(a1, v2, v4);
nm_nfree(v2, "iiab/rt_std.c", 157LL, "rt_std_twrite");
result = v5 != -1;
}
return result;
}

original_code:
int rt_std_twrite (RT_LLD lld, TABLE tab)
{
     int n;
     RT_STDD rt;
     char *buf;
     rt = rt_std_from_lld(lld);
     buf = table_outtable(tab);
     if ( ! buf)
       return 1;
     n = rt_std_write(lld, buf, strlen(buf));
     nm_nfree(buf,"iiab/rt_std.c",157,__PRETTY_FUNCTION__);
     if (n == -1)
   return 0;
     else
   return 1;
}

canonical_original_code:
int rt_std_twrite(void *lld, struct table_info *tab)
{
     int n;
     struct rt_std_desc *rt;
     char *buf;
     rt = rt_std_from_lld(lld);
     buf = table_outtable(tab);
     if ( ! buf)
       return 1;
     n = rt_std_write(lld, buf, strlen(buf));
     nm_nfree(buf,"iiab/rt_std.c",157,__PRETTY_FUNCTION__);
     if (n == -1)
   return 0;
     else
   return 1;
}

user_defined_types: struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; }


decompiled_code:
_BOOL8 __fastcall rt_std_checkpoint(const char **a1)
{
const char **v1; // rax

v1 = rt_std_from_lld(a1);
return fsync(*((_DWORD *)v1 + 6)) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func23(const char **a1)
{
const char **v1; // rax

v1 = func19(a1);
return fsync(*((_DWORD *)v1 + 6)) == 0;
}

original_code:
int rt_std_checkpoint (RT_LLD lld)
{
     RT_STDD rt;
     int r;
     rt = rt_std_from_lld(lld);
     r = fsync(rt->fd);
     if (r)
          return 0;
     else
   return 1;
}

canonical_original_code:
int rt_std_checkpoint(void *lld)
{
     struct rt_std_desc *rt;
     int r;
     rt = rt_std_from_lld(lld);
     r = fsync(rt->fd);
     if (r)
          return 0;
     else
   return 1;
}


variable_types:
k
char *

variable_types:
length
unsigned int

variable_types:
initval
unsigned int

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types:
c
unsigned int

variable_types:
len
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall hash_str(char *a1)
{
unsigned int v1; // eax

v1 = strlen(a1);
return hash_block(a1, v1, 685570);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1)
{
unsigned int v1; // eax

v1 = strlen(a1);
return func0(a1, v1, 685570);
}

original_code:
unsigned int hash_str(char *str) {
     return hash_block(str, strlen(str), 685570);
}

variable_types:
str
char *


decompiled_code:
__int64 __fastcall cascade_init(unsigned int a1, const char *a2)
{
int v2; // ebx
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rax
int v7; // [rsp+0h] [rbp-38h] BYREF
char v8[4]; // [rsp+4h] [rbp-34h] BYREF
char v9[48]; // [rsp+8h] [rbp-30h] BYREF

v2 = route_stat(a2, 0LL, &v7, v8, v9);
v3 = nm_nmalloc(24LL, "iiab/cascade.c", 102LL, "cascade_init");
*(_DWORD *)v3 = a1;
v4 = v3;
*(_QWORD *)(v3 + 8) = nm_xnstrdup(a2, "iiab/cascade.c", 104LL, "cascade_init");
if ( v2 )
v2 = v7 + 1;
v5 = (unsigned int)(v7 + 1);
*(_DWORD *)(v4 + 16) = v2;
elog_fprintf(1LL, "iiab/cascade.c", 107LL, "cascade_init", "cascade type %d init on %s from seq %d", a1, a2, v5);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, const char *a2)
{
int v2; // ebx
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rax
int v7; // [rsp+0h] [rbp-38h] BYREF
char v8[4]; // [rsp+4h] [rbp-34h] BYREF
char v9[48]; // [rsp+8h] [rbp-30h] BYREF

v2 = route_stat(a2, 0LL, &v7, v8, v9);
v3 = nm_nmalloc(24LL, "iiab/cascade.c", 102LL, "cascade_init");
*(_DWORD *)v3 = a1;
v4 = v3;
*(_QWORD *)(v3 + 8) = nm_xnstrdup(a2, "iiab/cascade.c", 104LL, "cascade_init");
if ( v2 )
v2 = v7 + 1;
v5 = (unsigned int)(v7 + 1);
*(_DWORD *)(v4 + 16) = v2;
elog_fprintf(1LL, "iiab/cascade.c", 107LL, "cascade_init", "cascade type %d init on %s from seq %d", a1, a2, v5);
return v4;
}

original_code:
CASCADE *cascade_init(enum cascade_fn func,
        char *monroute )
{
     struct cascade_info *session;
     int seq, size, r;
     time_t modt;
     r = route_stat(monroute, ((void *)0), &seq, &size, &modt);
     session = nm_nmalloc(sizeof(struct cascade_info),"iiab/cascade.c",102,__PRETTY_FUNCTION__);
     session->fn = func;
     session->purl = nm_xnstrdup(monroute,"iiab/cascade.c",104,__PRETTY_FUNCTION__);
     session->seq = r ? seq+1 : 0;
     elog_fprintf(DEBUG,"iiab/cascade.c",107,__PRETTY_FUNCTION__,"cascade type %d init on %s from seq %d", func, monroute, seq+1);
     return session;
}

canonical_original_code:
struct cascade_info *cascade_init(enum cascade_fn func, char *monroute)
{
     struct cascade_info *session;
     int seq; int size; int r;
     long modt;
     r = route_stat(monroute, ((void *)0), &seq, &size, &modt);
     session = nm_nmalloc(sizeof(struct cascade_info),"iiab/cascade.c",102,__PRETTY_FUNCTION__);
     session->fn = func;
     session->purl = nm_xnstrdup(monroute,"iiab/cascade.c",104,__PRETTY_FUNCTION__);
     session->seq = r ? seq+1 : 0;
     elog_fprintf(DEBUG,"iiab/cascade.c",107,__PRETTY_FUNCTION__,"cascade type %d init on %s from seq %d", func, monroute, seq+1);
     return session;
}

variable_types:
monroute
char *

variable_types: struct cascade_info { enum cascade_fn { CASCADE_AVG=0, CASCADE_MIN=1, CASCADE_MAX=2, CASCADE_SUM=3, CASCADE_LAST=4, CASCADE_FIRST=5, CASCADE_DIFF=6, CASCADE_RATE=7 } fn; char *purl; int seq; } *

variable_types: seq int

variable_types: size int

variable_types: modt long

user_defined_types: struct cascade_info { enum cascade_fn fn; char *purl; int seq; }


decompiled_code:
__int64 __fastcall cascade_fini(__int64 a1)
{
nm_nfree(*(_QWORD *)(a1 + 8), "iiab/cascade.c", 120LL, "cascade_fini");
return nm_nfree(a1, "iiab/cascade.c", 121LL, "cascade_fini");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
nm_nfree(*(_QWORD *)(a1 + 8), "iiab/cascade.c", 120LL, "cascade_fini");
return nm_nfree(a1, "iiab/cascade.c", 121LL, "cascade_fini");
}

original_code:
void cascade_fini(CASCADE *session)
{
     nm_nfree(session->purl,"iiab/cascade.c",120,__PRETTY_FUNCTION__);
     nm_nfree(session,"iiab/cascade.c",121,__PRETTY_FUNCTION__);
}

canonical_original_code:
void cascade_fini(struct cascade_info *session)
{
     nm_nfree(session->purl,"iiab/cascade.c",120,__PRETTY_FUNCTION__);
     nm_nfree(session,"iiab/cascade.c",121,__PRETTY_FUNCTION__);
}

user_defined_types: enum cascade_fn { CASCADE_AVG=0, CASCADE_MIN=1, CASCADE_MAX=2, CASCADE_SUM=3, CASCADE_LAST=4, CASCADE_FIRST=5, CASCADE_DIFF=6, CASCADE_RATE=7 }


decompiled_code:
__int64 __fastcall cascade_aggregate(int a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rbx
__int64 v6; // rbp
__int64 v7; // r14
__int64 v8; // rax
_QWORD *v9; // rdx
__int64 v10; // r15
_QWORD *v11; // rax
__int64 v12; // rax
__int64 v13; // r13
__int64 v14; // rax
__int64 v15; // rax
__int64 v16; // rbp
const char *v17; // rax
__int64 v18; // rbx
const char *v19; // rax
__int64 v20; // r12
__int64 **v21; // rax
__int64 *v22; // rdx
__int64 v23; // r13
_QWORD **v24; // rax
_QWORD *v25; // rdx
_QWORD *v26; // rbx
_QWORD *v27; // rax
const char *v28; // rbp
_QWORD **v29; // r15
const char *v30; // rsi
bool v31; // cf
bool v32; // zf
__int64 v33; // rcx
const char *v34; // rdi
char v35; // al
__int64 v36; // rcx
const char *v37; // rsi
const char *v38; // rdi
bool v39; // al
bool v40; // cf
bool v41; // zf
__int64 v42; // rcx
const char *v43; // rsi
const char *v44; // rdi
const char *v45; // rax
const char *v46; // rdi
double v47; // xmm7_8
double v48; // xmm0_8
__int64 v49; // rax
__int64 v50; // rax
__int64 *v51; // rax
__int64 v52; // rax
__int64 v53; // rax
__int64 v54; // rax
const char *v56; // rdi
_QWORD *v57; // rdx
_QWORD *v58; // rax
const char *v59; // rdi
_QWORD *v60; // rax
_QWORD *v61; // rdx
_QWORD *v62; // rax
const char *v63; // rdi
_QWORD *v64; // rax
_QWORD *v65; // rdx
_QWORD *v66; // rax
const char *v67; // rax
double v68; // xmm0_8
_QWORD *v69; // rax
const char *v70; // rax
const char *v71; // rdi
_QWORD *v72; // rdx
_QWORD *v73; // rax
const char *v74; // rdi
_QWORD *v75; // rax
__int64 v76; // r15
__int64 v77; // rax
__int64 v78; // rax
__int64 v79; // rax
time_t v80; // rax
__int64 v81; // rax
const char *v82; // rax
const char *v83; // rbp
double v84; // [rsp+0h] [rbp-58h]
double v85; // [rsp+0h] [rbp-58h]
double v86; // [rsp+0h] [rbp-58h]
double v87; // [rsp+0h] [rbp-58h]
__int64 v89; // [rsp+10h] [rbp-48h]
int v91; // [rsp+1Ch] [rbp-3Ch]

if ( !a2 )
{
v20 = 0LL;
elog_fprintf(2LL, "iiab/cascade.c", 217LL, "cascade_aggregate", "no dataset given to aggregate");
return v20;
}
if ( !(unsigned int)table_nrows(a2) )
elog_fprintf(2LL, "iiab/cascade.c", 221LL, "cascade_aggregate", "no rows to aggregate in dataset");
if ( !(unsigned int)table_hascol(a2, "_time") )
{
v20 = 0LL;
v83 = (const char *)table_outheader(a2);
elog_fprintf(
5LL,
"iiab/cascade.c",
225LL,
"cascade_aggregate",
"attempting to aggregate a table without _time column (columns: %s)",
v83);
nm_nfree(v83, "iiab/cascade.c", 227LL, "cascade_aggregate");
return v20;
}
v2 = table_getinforow(a2, "key");
v3 = v2;
if ( v2 )
{
v4 = tree_search(v2, "1", 2LL);
v5 = v4;
if ( v4 )
{
v6 = table_uniqcolvals(a2, v4, 0LL);
if ( v6 )
{
v7 = tree_really_create("iiab/cascade.c", 241LL, "cascade_aggregate");
v8 = tableset_create(a2);
v9 = *(_QWORD **)v6;
v10 = v8;
v11 = **(_QWORD ***)v6;
*(_QWORD *)(v6 + 8) = v11;
if ( v9 != v11 )
{
do
{
tableset_reset(v10);
v12 = tree_getkey(v6);
tableset_where(v10, v5, 0LL, v12);
v13 = tableset_into(v10);
v14 = tree_getkey(v6);
tree_add(v7, v14, v13);
v15 = **(_QWORD **)(v6 + 8);
*(_QWORD *)(v6 + 8) = v15;
}
while ( v15 != *(_QWORD *)v6 );
}
tableset_destroy(v10);
tree_really_destroy(v6, "iiab/cascade.c", 253LL, "cascade_aggregate");
tree_really_destroy(v3, "iiab/cascade.c", 255LL, "cascade_aggregate");
v91 = 1;
goto LABEL_14;
}
tree_really_destroy(0LL, "iiab/cascade.c", 253LL, "cascade_aggregate");
}
tree_really_destroy(v3, "iiab/cascade.c", 255LL, "cascade_aggregate");
}
v7 = tree_really_create("iiab/cascade.c", 260LL, "cascade_aggregate");
tree_add(v7, "nokey", a2);
v91 = 0;
LABEL_14:
v16 = 0LL;
table_first(a2);
if ( (unsigned int)table_hascol(a2, "_dur") )
{
v82 = (const char *)table_getcurrentcell(a2, "_dur");
v16 = (int)strtol(v82, 0LL, 10);
}
v17 = (const char *)table_getcurrentcell(a2, "_time");
v18 = strtol(v17, 0LL, 10);
table_last(a2);
v19 = (const char *)table_getcurrentcell(a2, "_time");
v89 = v16 + strtol(v19, 0LL, 10) - v18;
v20 = table_create_fromdonor(a2);
table_addcol(v20, "_seq", 0LL);
table_addcol(v20, "_time", 0LL);
table_addcol(v20, "_dur", 0LL);
v21 = *(__int64 ***)v7;
v22 = **(__int64 ***)v7;
*(_QWORD *)(v7 + 8) = v22;
if ( v21 != (__int64 **)v22 )
{
do
{
table_addemptyrow(v20);
v23 = tree_get(v7);
v24 = (_QWORD **)table_getheader(v23);
v25 = *v24;
v26 = v24;
v27 = (_QWORD *)**v24;
v26[1] = v27;
if ( v25 != v27 )
{
do
{
v28 = (const char *)tree_getkey(v26);
if ( !(unsigned int)table_hascol(v20, v28) )
{
v76 = nm_xnstrdup(v28, "iiab/cascade.c", 288LL, "cascade_aggregate");
table_addcol(v20, v76, 0LL);
table_freeondestroy(v20, v76);
}
v29 = (_QWORD **)table_getcol(v23, v28);
v30 = (const char *)table_getinfocell(v23, "type", v28);
v31 = 0;
v32 = v30 == 0LL;
if ( !v30 )
goto LABEL_25;
v33 = 4LL;
v34 = "str";
do
{
if ( !v33 )
break;
v31 = *v30 < (unsigned int)*v34;
v32 = *v30++ == *v34++;
--v33;
}
while ( v32 );
v35 = (!v31 && !v32) - v31;
v31 = 0;
v32 = v35 == 0;
if ( !v35 )
{
itree_last(v29, v30);
v78 = itree_get(v29);
table_replacecurrentcell(v20, v28, v78);
}
else
{
LABEL_25:
v36 = 5LL;
v37 = v28;
v38 = "_dur";
do
{
if ( !v36 )
break;
v31 = *v37 < (unsigned int)*v38;
v32 = *v37++ == *v38++;
--v36;
}
while ( v32 );
if ( (!v31 && !v32) == v31 )
{
itree_last(v29, v37);
v77 = itree_get(v29);
table_replacecurrentcell(v20, "_dur", v77);
}
else
{
v39 = strcmp(v28, "_seq") != 0;
v40 = 0;
v41 = !v39;
if ( v39 )
{
v42 = 6LL;
v43 = v28;
v44 = "_time";
do
{
if ( !v42 )
break;
v40 = *v43 < (unsigned int)*v44;
v41 = *v43++ == *v44++;
--v42;
}
while ( v41 );
if ( (!v40 && !v41) == v40 )
{
itree_last(v29, v43);
v79 = itree_get(v29);
table_replacecurrentcell(v20, "_time", v79);
}
else
{
switch ( a1 )
{
case 0:
v65 = *v29;
v66 = (_QWORD *)**v29;
v29[1] = v66;
if ( v65 == v66 )
{
v86 = 0.0;
}
else
{
v86 = 0.0;
do
{
v67 = (const char *)itree_get(v29);
v68 = atof(v67);
v69 = (_QWORD *)*v29[1];
v86 = v68 + v86;
v29[1] = v69;
}
while ( v69 != *v29 );
}
HIDWORD(v48) = 0;
v84 = v86 / (double)(int)itree_n(v29, v43);
*(float *)&v48 = v84;
break;
case 1:
v72 = *v29;
v73 = (_QWORD *)**v29;
v29[1] = v73;
if ( v72 != v73 )
{
v84 = *(double *)&LC3;
do
{
v74 = (const char *)itree_get(v29);
v48 = fmin(atof(v74), v84);
v75 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v75;
}
while ( v75 != *v29 );
goto LABEL_66;
}
*(_QWORD *)&v48 = (unsigned int)LC5;
v84 = *(double *)&LC3;
break;
case 2:
v61 = *v29;
v62 = (_QWORD *)**v29;
v29[1] = v62;
if ( v61 != v62 )
{
v84 = *(double *)&LC4;
do
{
v63 = (const char *)itree_get(v29);
v48 = fmax(atof(v63), v84);
v64 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v64;
}
while ( v64 != *v29 );
goto LABEL_66;
}
v48 = 0.0;
v84 = *(double *)&LC4;
break;
case 3:
v57 = *v29;
v58 = (_QWORD *)**v29;
v29[1] = v58;
if ( v57 != v58 )
{
v84 = 0.0;
do
{
v59 = (const char *)itree_get(v29);
v48 = atof(v59) + v84;
v60 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v60;
}
while ( v60 != *v29 );
goto LABEL_66;
}
v48 = 0.0;
v84 = 0.0;
break;
case 4:
itree_last(v29, v43);
goto LABEL_51;
case 5:
itree_first(v29, v43);
LABEL_51:
v56 = (const char *)itree_get(v29);
v48 = atof(v56);
v84 = v48;
*(float *)&v48 = v48;
break;
case 6:
itree_first(v29, v43);
v45 = (const char *)itree_get(v29);
v85 = atof(v45);
itree_last(v29, v43);
v46 = (const char *)itree_get(v29);
v47 = atof(v46) - v85;
HIDWORD(v48) = 0;
v84 = v47;
*(float *)&v48 = v47;
break;
case 7:
itree_first(v29, v43);
v70 = (const char *)itree_get(v29);
v87 = atof(v70);
itree_last(v29, v43);
v71 = (const char *)itree_get(v29);
v48 = (atof(v71) - v87) / (double)(int)v89;
v84 = v48;
LABEL_66:
*(float *)&v48 = v48;
break;
default:
HIDWORD(v48) = 0;
*(float *)&v48 = v84;
break;
}
v49 = util_ftoa(v48);
table_replacecurrentcell_alloc(v20, v28, v49);
}
}
else
{
table_replacecurrentcell(v20, "_seq", "0");
}
}
}
itree_really_destroy(v29, "iiab/cascade.c", 376LL, "cascade_aggregate");
v50 = *(_QWORD *)v26[1];
v26[1] = v50;
}
while ( v50 != *v26 );
}
if ( !(unsigned int)table_hascol(a2, "_time") )
{
v80 = time(0LL);
v81 = util_decdatetime(v80);
table_replacecurrentcell(v20, "_time", v81);
}
if ( !(unsigned int)table_hascol(a2, "_seq") )
table_replacecurrentcell(v20, "_seq", "0");
if ( !(unsigned int)table_hascol(a2, "_dur") )
table_replacecurrentcell(v20, "_dur", "0");
v22 = *(__int64 **)v7;
v51 = **(__int64 ***)(v7 + 8);
*(_QWORD *)(v7 + 8) = v51;
}
while ( v51 != v22 );
}
if ( v91 )
{
v52 = *v22;
*(_QWORD *)(v7 + 8) = *v22;
if ( v52 != *(_QWORD *)v7 )
{
do
{
v53 = tree_get(v7);
table_destroy(v53);
v54 = **(_QWORD **)(v7 + 8);
*(_QWORD *)(v7 + 8) = v54;
}
while ( v54 != *(_QWORD *)v7 );
}
}
tree_really_destroy(v7, "iiab/cascade.c", 395LL, "cascade_aggregate");
return v20;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rbx
__int64 v6; // rbp
__int64 v7; // r14
__int64 v8; // rax
_QWORD *v9; // rdx
__int64 v10; // r15
_QWORD *v11; // rax
__int64 v12; // rax
__int64 v13; // r13
__int64 v14; // rax
__int64 v15; // rax
__int64 v16; // rbp
const char *v17; // rax
__int64 v18; // rbx
const char *v19; // rax
__int64 v20; // r12
__int64 **v21; // rax
__int64 *v22; // rdx
__int64 v23; // r13
_QWORD **v24; // rax
_QWORD *v25; // rdx
_QWORD *v26; // rbx
_QWORD *v27; // rax
const char *v28; // rbp
_QWORD **v29; // r15
const char *v30; // rsi
bool v31; // cf
bool v32; // zf
__int64 v33; // rcx
const char *v34; // rdi
char v35; // al
__int64 v36; // rcx
const char *v37; // rsi
const char *v38; // rdi
bool v39; // al
bool v40; // cf
bool v41; // zf
__int64 v42; // rcx
const char *v43; // rsi
const char *v44; // rdi
const char *v45; // rax
const char *v46; // rdi
double v47; // xmm7_8
double v48; // xmm0_8
__int64 v49; // rax
__int64 v50; // rax
__int64 *v51; // rax
__int64 v52; // rax
__int64 v53; // rax
__int64 v54; // rax
const char *v56; // rdi
_QWORD *v57; // rdx
_QWORD *v58; // rax
const char *v59; // rdi
_QWORD *v60; // rax
_QWORD *v61; // rdx
_QWORD *v62; // rax
const char *v63; // rdi
_QWORD *v64; // rax
_QWORD *v65; // rdx
_QWORD *v66; // rax
const char *v67; // rax
double v68; // xmm0_8
_QWORD *v69; // rax
const char *v70; // rax
const char *v71; // rdi
_QWORD *v72; // rdx
_QWORD *v73; // rax
const char *v74; // rdi
_QWORD *v75; // rax
__int64 v76; // r15
__int64 v77; // rax
__int64 v78; // rax
__int64 v79; // rax
time_t v80; // rax
__int64 v81; // rax
const char *v82; // rax
const char *v83; // rbp
double v84; // [rsp+0h] [rbp-58h]
double v85; // [rsp+0h] [rbp-58h]
double v86; // [rsp+0h] [rbp-58h]
double v87; // [rsp+0h] [rbp-58h]
__int64 v89; // [rsp+10h] [rbp-48h]
int v91; // [rsp+1Ch] [rbp-3Ch]

if ( !a2 )
{
v20 = 0LL;
elog_fprintf(2LL, "iiab/cascade.c", 217LL, "cascade_aggregate", "no dataset given to aggregate");
return v20;
}
if ( !(unsigned int)table_nrows(a2) )
elog_fprintf(2LL, "iiab/cascade.c", 221LL, "cascade_aggregate", "no rows to aggregate in dataset");
if ( !(unsigned int)table_hascol(a2, "_time") )
{
v20 = 0LL;
v83 = (const char *)table_outheader(a2);
elog_fprintf(
5LL,
"iiab/cascade.c",
225LL,
"cascade_aggregate",
"attempting to aggregate a table without _time column (columns: %s)",
v83);
nm_nfree(v83, "iiab/cascade.c", 227LL, "cascade_aggregate");
return v20;
}
v2 = table_getinforow(a2, "key");
v3 = v2;
if ( v2 )
{
v4 = tree_search(v2, "1", 2LL);
v5 = v4;
if ( v4 )
{
v6 = table_uniqcolvals(a2, v4, 0LL);
if ( v6 )
{
v7 = tree_really_create("iiab/cascade.c", 241LL, "cascade_aggregate");
v8 = tableset_create(a2);
v9 = *(_QWORD **)v6;
v10 = v8;
v11 = **(_QWORD ***)v6;
*(_QWORD *)(v6 + 8) = v11;
if ( v9 != v11 )
{
do
{
tableset_reset(v10);
v12 = tree_getkey(v6);
tableset_where(v10, v5, 0LL, v12);
v13 = tableset_into(v10);
v14 = tree_getkey(v6);
tree_add(v7, v14, v13);
v15 = **(_QWORD **)(v6 + 8);
*(_QWORD *)(v6 + 8) = v15;
}
while ( v15 != *(_QWORD *)v6 );
}
tableset_destroy(v10);
tree_really_destroy(v6, "iiab/cascade.c", 253LL, "cascade_aggregate");
tree_really_destroy(v3, "iiab/cascade.c", 255LL, "cascade_aggregate");
v91 = 1;
goto LABEL_14;
}
tree_really_destroy(0LL, "iiab/cascade.c", 253LL, "cascade_aggregate");
}
tree_really_destroy(v3, "iiab/cascade.c", 255LL, "cascade_aggregate");
}
v7 = tree_really_create("iiab/cascade.c", 260LL, "cascade_aggregate");
tree_add(v7, "nokey", a2);
v91 = 0;
LABEL_14:
v16 = 0LL;
table_first(a2);
if ( (unsigned int)table_hascol(a2, "_dur") )
{
v82 = (const char *)table_getcurrentcell(a2, "_dur");
v16 = (int)strtol(v82, 0LL, 10);
}
v17 = (const char *)table_getcurrentcell(a2, "_time");
v18 = strtol(v17, 0LL, 10);
table_last(a2);
v19 = (const char *)table_getcurrentcell(a2, "_time");
v89 = v16 + strtol(v19, 0LL, 10) - v18;
v20 = table_create_fromdonor(a2);
table_addcol(v20, "_seq", 0LL);
table_addcol(v20, "_time", 0LL);
table_addcol(v20, "_dur", 0LL);
v21 = *(__int64 ***)v7;
v22 = **(__int64 ***)v7;
*(_QWORD *)(v7 + 8) = v22;
if ( v21 != (__int64 **)v22 )
{
do
{
table_addemptyrow(v20);
v23 = tree_get(v7);
v24 = (_QWORD **)table_getheader(v23);
v25 = *v24;
v26 = v24;
v27 = (_QWORD *)**v24;
v26[1] = v27;
if ( v25 != v27 )
{
do
{
v28 = (const char *)tree_getkey(v26);
if ( !(unsigned int)table_hascol(v20, v28) )
{
v76 = nm_xnstrdup(v28, "iiab/cascade.c", 288LL, "cascade_aggregate");
table_addcol(v20, v76, 0LL);
table_freeondestroy(v20, v76);
}
v29 = (_QWORD **)table_getcol(v23, v28);
v30 = (const char *)table_getinfocell(v23, "type", v28);
v31 = 0;
v32 = v30 == 0LL;
if ( !v30 )
goto LABEL_25;
v33 = 4LL;
v34 = "str";
do
{
if ( !v33 )
break;
v31 = *v30 < (unsigned int)*v34;
v32 = *v30++ == *v34++;
--v33;
}
while ( v32 );
v35 = (!v31 && !v32) - v31;
v31 = 0;
v32 = v35 == 0;
if ( !v35 )
{
itree_last(v29, v30);
v78 = itree_get(v29);
table_replacecurrentcell(v20, v28, v78);
}
else
{
LABEL_25:
v36 = 5LL;
v37 = v28;
v38 = "_dur";
do
{
if ( !v36 )
break;
v31 = *v37 < (unsigned int)*v38;
v32 = *v37++ == *v38++;
--v36;
}
while ( v32 );
if ( (!v31 && !v32) == v31 )
{
itree_last(v29, v37);
v77 = itree_get(v29);
table_replacecurrentcell(v20, "_dur", v77);
}
else
{
v39 = strcmp(v28, "_seq") != 0;
v40 = 0;
v41 = !v39;
if ( v39 )
{
v42 = 6LL;
v43 = v28;
v44 = "_time";
do
{
if ( !v42 )
break;
v40 = *v43 < (unsigned int)*v44;
v41 = *v43++ == *v44++;
--v42;
}
while ( v41 );
if ( (!v40 && !v41) == v40 )
{
itree_last(v29, v43);
v79 = itree_get(v29);
table_replacecurrentcell(v20, "_time", v79);
}
else
{
switch ( a1 )
{
case 0:
v65 = *v29;
v66 = (_QWORD *)**v29;
v29[1] = v66;
if ( v65 == v66 )
{
v86 = 0.0;
}
else
{
v86 = 0.0;
do
{
v67 = (const char *)itree_get(v29);
v68 = atof(v67);
v69 = (_QWORD *)*v29[1];
v86 = v68 + v86;
v29[1] = v69;
}
while ( v69 != *v29 );
}
HIDWORD(v48) = 0;
v84 = v86 / (double)(int)itree_n(v29, v43);
*(float *)&v48 = v84;
break;
case 1:
v72 = *v29;
v73 = (_QWORD *)**v29;
v29[1] = v73;
if ( v72 != v73 )
{
v84 = *(double *)&LC3;
do
{
v74 = (const char *)itree_get(v29);
v48 = fmin(atof(v74), v84);
v75 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v75;
}
while ( v75 != *v29 );
goto LABEL_66;
}
*(_QWORD *)&v48 = (unsigned int)LC5;
v84 = *(double *)&LC3;
break;
case 2:
v61 = *v29;
v62 = (_QWORD *)**v29;
v29[1] = v62;
if ( v61 != v62 )
{
v84 = *(double *)&LC4;
do
{
v63 = (const char *)itree_get(v29);
v48 = fmax(atof(v63), v84);
v64 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v64;
}
while ( v64 != *v29 );
goto LABEL_66;
}
v48 = 0.0;
v84 = *(double *)&LC4;
break;
case 3:
v57 = *v29;
v58 = (_QWORD *)**v29;
v29[1] = v58;
if ( v57 != v58 )
{
v84 = 0.0;
do
{
v59 = (const char *)itree_get(v29);
v48 = atof(v59) + v84;
v60 = (_QWORD *)*v29[1];
v84 = v48;
v29[1] = v60;
}
while ( v60 != *v29 );
goto LABEL_66;
}
v48 = 0.0;
v84 = 0.0;
break;
case 4:
itree_last(v29, v43);
goto LABEL_51;
case 5:
itree_first(v29, v43);
LABEL_51:
v56 = (const char *)itree_get(v29);
v48 = atof(v56);
v84 = v48;
*(float *)&v48 = v48;
break;
case 6:
itree_first(v29, v43);
v45 = (const char *)itree_get(v29);
v85 = atof(v45);
itree_last(v29, v43);
v46 = (const char *)itree_get(v29);
v47 = atof(v46) - v85;
HIDWORD(v48) = 0;
v84 = v47;
*(float *)&v48 = v47;
break;
case 7:
itree_first(v29, v43);
v70 = (const char *)itree_get(v29);
v87 = atof(v70);
itree_last(v29, v43);
v71 = (const char *)itree_get(v29);
v48 = (atof(v71) - v87) / (double)(int)v89;
v84 = v48;
LABEL_66:
*(float *)&v48 = v48;
break;
default:
HIDWORD(v48) = 0;
*(float *)&v48 = v84;
break;
}
v49 = util_ftoa(v48);
table_replacecurrentcell_alloc(v20, v28, v49);
}
}
else
{
table_replacecurrentcell(v20, "_seq", "0");
}
}
}
itree_really_destroy(v29, "iiab/cascade.c", 376LL, "cascade_aggregate");
v50 = *(_QWORD *)v26[1];
v26[1] = v50;
}
while ( v50 != *v26 );
}
if ( !(unsigned int)table_hascol(a2, "_time") )
{
v80 = time(0LL);
v81 = util_decdatetime(v80);
table_replacecurrentcell(v20, "_time", v81);
}
if ( !(unsigned int)table_hascol(a2, "_seq") )
table_replacecurrentcell(v20, "_seq", "0");
if ( !(unsigned int)table_hascol(a2, "_dur") )
table_replacecurrentcell(v20, "_dur", "0");
v22 = *(__int64 **)v7;
v51 = **(__int64 ***)(v7 + 8);
*(_QWORD *)(v7 + 8) = v51;
}
while ( v51 != v22 );
}
if ( v91 )
{
v52 = *v22;
*(_QWORD *)(v7 + 8) = *v22;
if ( v52 != *(_QWORD *)v7 )
{
do
{
v53 = tree_get(v7);
table_destroy(v53);
v54 = **(_QWORD **)(v7 + 8);
*(_QWORD *)(v7 + 8) = v54;
}
while ( v54 != *(_QWORD *)v7 );
}
}
tree_really_destroy(v7, "iiab/cascade.c", 395LL, "cascade_aggregate");
return v20;
}

original_code:
TABLE cascade_aggregate(enum cascade_fn func,
   TABLE dataset )
{
     TREE *inforow, *keyvals, *databykey, *colnames;
     char *keycol, *colname, *tmpstr, *type;
     int duration, haskey=0;
     TABLE itab, result;
     TABSET tset;
     ITREE *col;
     double val, tmpval1, tmpval2;
     time_t t1, t2, tdiff;
     if ( ! dataset ) {
          elog_fprintf(DIAG,"iiab/cascade.c",217,__PRETTY_FUNCTION__,"no dataset given to aggregate");
          return ((void *)0);
     }
     if (table_nrows(dataset) == 0) {
          elog_fprintf(DIAG,"iiab/cascade.c",221,__PRETTY_FUNCTION__,"no rows to aggregate in dataset");
     }
     if ( ! table_hascol(dataset, "_time")) {
          tmpstr = table_outheader(dataset);
          elog_fprintf(ERROR,"iiab/cascade.c",225,__PRETTY_FUNCTION__,"attempting to aggregate a table without _time " "column (columns: %s)", tmpstr);
   nm_nfree(tmpstr,"iiab/cascade.c",227,__PRETTY_FUNCTION__);
          return ((void *)0);
     }
     inforow = table_getinforow(dataset, "key");
     if (inforow) {
          keycol = tree_search(inforow, "1", 2);
   if (keycol) {
        keyvals = table_uniqcolvals(dataset, keycol, ((void *)0));
        if (keyvals) {
      haskey++;
      databykey = tree_really_create("iiab/cascade.c",241,__PRETTY_FUNCTION__);
      tset = tableset_create(dataset);
      for((keyvals)->node = ((keyvals)->root->c.list.flink); (keyvals)->node != ((keyvals)->root); (keyvals)->node = ((keyvals)->node->c.list.flink)) {
           tableset_reset(tset);
    tableset_where(tset, keycol, eq,
     tree_getkey(keyvals));
    itab = tableset_into(tset);
           tree_add(databykey, tree_getkey(keyvals), itab);
      }
      tableset_destroy(tset);
        }
        tree_really_destroy(keyvals,"iiab/cascade.c",253,__PRETTY_FUNCTION__);
   }
   tree_really_destroy(inforow,"iiab/cascade.c",255,__PRETTY_FUNCTION__);
     }
     if ( ! haskey ) {
          databykey = tree_really_create("iiab/cascade.c",260,__PRETTY_FUNCTION__);
   tree_add(databykey, "nokey", dataset);
     }
     table_first(dataset);
     if (table_hascol(dataset, "_dur"))
          duration = strtol(table_getcurrentcell(dataset, "_dur"), ((void *)0), 10);
     else
          duration = 0;
     t1 = strtol(table_getcurrentcell(dataset, "_time"), ((void *)0), 10);
     table_last(dataset);
     t2 = strtol(table_getcurrentcell(dataset, "_time"), ((void *)0), 10);
     tdiff = t2-t1+duration;
     result = table_create_fromdonor(dataset);
     table_addcol(result, "_seq", ((void *)0));
     table_addcol(result, "_time", ((void *)0));
     table_addcol(result, "_dur", ((void *)0));
     for((databykey)->node = ((databykey)->root->c.list.flink); (databykey)->node != ((databykey)->root); (databykey)->node = ((databykey)->node->c.list.flink)) {
          table_addemptyrow(result);
          itab = tree_get(databykey);
   colnames = table_getheader(itab);
   for((colnames)->node = ((colnames)->root->c.list.flink); (colnames)->node != ((colnames)->root); (colnames)->node = ((colnames)->node->c.list.flink)) {
        colname = tree_getkey(colnames);
        if ( ! table_hascol(result, colname)) {
       tmpstr = nm_xnstrdup(colname,"iiab/cascade.c",288,__PRETTY_FUNCTION__);
       table_addcol(result, tmpstr, ((void *)0));
       table_freeondestroy(result, tmpstr);
        }
        col = table_getcol(itab, colname);
        type = table_getinfocell(itab, "type", colname);
        if (type && strcmp(type, "str") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, colname, itree_get(col));
        } else if (strcmp(colname, "_dur") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, "_dur", itree_get(col));
        } else if (strcmp(colname, "_seq") == 0) {
      table_replacecurrentcell(result, "_seq", "0");
        } else if (strcmp(colname, "_time") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, "_time", itree_get(col));
        } else {
      switch (func) {
      case CASCADE_AVG:
           val = 0.0;
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink))
         val += atof( itree_get(col) );
    val = val / itree_n(col);
    break;
      case CASCADE_MIN:
           val = ((double)1.79769313486231570814527423731704357e+308L);
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink)) {
         tmpval1 = atof( itree_get(col) );
         if (tmpval1 < val)
       val = tmpval1;
    }
    break;
      case CASCADE_MAX:
           val = ((double)2.22507385850720138309023271733240406e-308L);
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink)) {
         tmpval1 = atof( itree_get(col) );
         if (tmpval1 > val)
       val = tmpval1;
    }
    break;
      case CASCADE_SUM:
           val = 0.0;
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink))
         val += atof( itree_get(col) );
    break;
      case CASCADE_LAST:
           itree_last(col);
    val = atof( itree_get(col) );
    break;
      case CASCADE_FIRST:
           itree_first(col);
    val = atof( itree_get(col) );
    break;
      case CASCADE_DIFF:
           itree_first(col);
    tmpval1 = atof( itree_get(col) );
    itree_last(col);
    tmpval2 = atof( itree_get(col) );
    val = tmpval2 - tmpval1;
    break;
      case CASCADE_RATE:
           itree_first(col);
    tmpval1 = atof( itree_get(col) );
    itree_last(col);
    tmpval2 = atof( itree_get(col) );
    val = tmpval2 - tmpval1;
    val = val / tdiff;
    break;
      }
      table_replacecurrentcell_alloc(result, colname,
         util_ftoa(val));
        }
        itree_really_destroy(col,"iiab/cascade.c",376,__PRETTY_FUNCTION__);
   }
   if ( ! table_hascol(dataset, "_time"))
        table_replacecurrentcell(result, "_time",
     util_decdatetime(time(((void *)0))));
   if ( ! table_hascol(dataset, "_seq"))
        table_replacecurrentcell(result, "_seq", "0");
   if ( ! table_hascol(dataset, "_dur"))
        table_replacecurrentcell(result, "_dur", "0");
     }
     if (haskey) {
          for((databykey)->node = ((databykey)->root->c.list.flink); (databykey)->node != ((databykey)->root); (databykey)->node = ((databykey)->node->c.list.flink)) {
        itab = tree_get(databykey);
        table_destroy(itab);
   }
     }
     tree_really_destroy(databykey,"iiab/cascade.c",395,__PRETTY_FUNCTION__);
     return result;
}

canonical_original_code:
struct table_info *cascade_aggregate(enum cascade_fn func, struct table_info *dataset)
{
     struct tree_head *inforow; struct tree_head *keyvals; struct tree_head *databykey; struct tree_head *colnames;
     char *keycol; char *colname; char *tmpstr; char *type;
     int duration; int haskey = 0;
     struct table_info *itab; struct table_info *result;
     struct tableset *tset;
     struct itree_head *col;
     double val; double tmpval1; double tmpval2;
     long t1; long t2; long tdiff;
     if ( ! dataset ) {
          elog_fprintf(DIAG,"iiab/cascade.c",217,__PRETTY_FUNCTION__,"no dataset given to aggregate");
          return ((void *)0);
     }
     if (table_nrows(dataset) == 0) {
          elog_fprintf(DIAG,"iiab/cascade.c",221,__PRETTY_FUNCTION__,"no rows to aggregate in dataset");
     }
     if ( ! table_hascol(dataset, "_time")) {
          tmpstr = table_outheader(dataset);
          elog_fprintf(ERROR,"iiab/cascade.c",225,__PRETTY_FUNCTION__,"attempting to aggregate a table without _time " "column (columns: %s)", tmpstr);
   nm_nfree(tmpstr,"iiab/cascade.c",227,__PRETTY_FUNCTION__);
          return ((void *)0);
     }
     inforow = table_getinforow(dataset, "key");
     if (inforow) {
          keycol = tree_search(inforow, "1", 2);
   if (keycol) {
        keyvals = table_uniqcolvals(dataset, keycol, ((void *)0));
        if (keyvals) {
      haskey++;
      databykey = tree_really_create("iiab/cascade.c",241,__PRETTY_FUNCTION__);
      tset = tableset_create(dataset);
      for((keyvals)->node = ((keyvals)->root->c.list.flink); (keyvals)->node != ((keyvals)->root); (keyvals)->node = ((keyvals)->node->c.list.flink)) {
           tableset_reset(tset);
    tableset_where(tset, keycol, eq,
     tree_getkey(keyvals));
    itab = tableset_into(tset);
           tree_add(databykey, tree_getkey(keyvals), itab);
      }
      tableset_destroy(tset);
        }
        tree_really_destroy(keyvals,"iiab/cascade.c",253,__PRETTY_FUNCTION__);
   }
   tree_really_destroy(inforow,"iiab/cascade.c",255,__PRETTY_FUNCTION__);
     }
     if ( ! haskey ) {
          databykey = tree_really_create("iiab/cascade.c",260,__PRETTY_FUNCTION__);
   tree_add(databykey, "nokey", dataset);
     }
     table_first(dataset);
     if (table_hascol(dataset, "_dur"))
          duration = strtol(table_getcurrentcell(dataset, "_dur"), ((void *)0), 10);
     else
          duration = 0;
     t1 = strtol(table_getcurrentcell(dataset, "_time"), ((void *)0), 10);
     table_last(dataset);
     t2 = strtol(table_getcurrentcell(dataset, "_time"), ((void *)0), 10);
     tdiff = t2-t1+duration;
     result = table_create_fromdonor(dataset);
     table_addcol(result, "_seq", ((void *)0));
     table_addcol(result, "_time", ((void *)0));
     table_addcol(result, "_dur", ((void *)0));
     for((databykey)->node = ((databykey)->root->c.list.flink); (databykey)->node != ((databykey)->root); (databykey)->node = ((databykey)->node->c.list.flink)) {
          table_addemptyrow(result);
          itab = tree_get(databykey);
   colnames = table_getheader(itab);
   for((colnames)->node = ((colnames)->root->c.list.flink); (colnames)->node != ((colnames)->root); (colnames)->node = ((colnames)->node->c.list.flink)) {
        colname = tree_getkey(colnames);
        if ( ! table_hascol(result, colname)) {
       tmpstr = nm_xnstrdup(colname,"iiab/cascade.c",288,__PRETTY_FUNCTION__);
       table_addcol(result, tmpstr, ((void *)0));
       table_freeondestroy(result, tmpstr);
        }
        col = table_getcol(itab, colname);
        type = table_getinfocell(itab, "type", colname);
        if (type && strcmp(type, "str") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, colname, itree_get(col));
        } else if (strcmp(colname, "_dur") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, "_dur", itree_get(col));
        } else if (strcmp(colname, "_seq") == 0) {
      table_replacecurrentcell(result, "_seq", "0");
        } else if (strcmp(colname, "_time") == 0) {
      itree_last(col);
      table_replacecurrentcell(result, "_time", itree_get(col));
        } else {
      switch (func) {
      case CASCADE_AVG:
           val = 0.0;
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink))
         val += atof( itree_get(col) );
    val = val / itree_n(col);
    break;
      case CASCADE_MIN:
           val = ((double)1.79769313486231570814527423731704357e+308L);
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink)) {
         tmpval1 = atof( itree_get(col) );
         if (tmpval1 < val)
       val = tmpval1;
    }
    break;
      case CASCADE_MAX:
           val = ((double)2.22507385850720138309023271733240406e-308L);
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink)) {
         tmpval1 = atof( itree_get(col) );
         if (tmpval1 > val)
       val = tmpval1;
    }
    break;
      case CASCADE_SUM:
           val = 0.0;
    for((col)->node = ((col)->root->c.list.flink); (col)->node != ((col)->root); (col)->node = ((col)->node->c.list.flink))
         val += atof( itree_get(col) );
    break;
      case CASCADE_LAST:
           itree_last(col);
    val = atof( itree_get(col) );
    break;
      case CASCADE_FIRST:
           itree_first(col);
    val = atof( itree_get(col) );
    break;
      case CASCADE_DIFF:
           itree_first(col);
    tmpval1 = atof( itree_get(col) );
    itree_last(col);
    tmpval2 = atof( itree_get(col) );
    val = tmpval2 - tmpval1;
    break;
      case CASCADE_RATE:
           itree_first(col);
    tmpval1 = atof( itree_get(col) );
    itree_last(col);
    tmpval2 = atof( itree_get(col) );
    val = tmpval2 - tmpval1;
    val = val / tdiff;
    break;
      }
      table_replacecurrentcell_alloc(result, colname,
         util_ftoa(val));
        }
        itree_really_destroy(col,"iiab/cascade.c",376,__PRETTY_FUNCTION__);
   }
   if ( ! table_hascol(dataset, "_time"))
        table_replacecurrentcell(result, "_time",
     util_decdatetime(time(((void *)0))));
   if ( ! table_hascol(dataset, "_seq"))
        table_replacecurrentcell(result, "_seq", "0");
   if ( ! table_hascol(dataset, "_dur"))
        table_replacecurrentcell(result, "_dur", "0");
     }
     if (haskey) {
          for((databykey)->node = ((databykey)->root->c.list.flink); (databykey)->node != ((databykey)->root); (databykey)->node = ((databykey)->node->c.list.flink)) {
        itab = tree_get(databykey);
        table_destroy(itab);
   }
     }
     tree_really_destroy(databykey,"iiab/cascade.c",395,__PRETTY_FUNCTION__);
     return result;
}

variable_types: enum cascade_fn { CASCADE_AVG=0, CASCADE_MIN=1, CASCADE_MAX=2, CASCADE_SUM=3, CASCADE_LAST=4, CASCADE_FIRST=5, CASCADE_DIFF=6, CASCADE_RATE=7 }

variable_types:
dataset
struct table_info *

variable_types:
keycol
char *

variable_types:
colname
char *

variable_types:
tmpstr
char *

variable_types:
type
char *

variable_types: duration int

variable_types: haskey int

variable_types: struct tableset { struct table_info { int ncols; int nrows; int minrowkey; int maxrowkey; struct tree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *data; struct itree_head { struct rb_node *root; struct rb_node *node; } *colorder; struct tree_head *info; struct tree_head *infolookup; struct itree_head *tobegarbage; int refcount; struct itree_head *roworder; char separator; } *tab; struct itree_head *cols; struct itree_head *where; int nwhere; int nunless; char *sortby; int sorthow; struct itree_head *rownums; struct tree_head *groupby; struct itree_head *tobegarbage; } *

variable_types: struct itree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *

variable_types: val double

variable_types: tdiff long

user_defined_types: struct tableset { struct table_info *tab; struct itree_head *cols; struct itree_head *where; int nwhere; int nunless; char *sortby; int sorthow; struct itree_head *rownums; struct tree_head *groupby; struct itree_head *tobegarbage; }


decompiled_code:
__int64 __fastcall cascade_sample(__int64 a1, __int64 a2)
{
__int64 v3; // rax
__int64 v4; // rbp
__int64 v5; // r12
int v6; // edi
__int64 v7; // rax
__int64 v8; // rbp
unsigned int v9; // er13
__int64 result; // rax
int v11; // [rsp+0h] [rbp-38h] BYREF
char v12[4]; // [rsp+4h] [rbp-34h] BYREF
char v13[48]; // [rsp+8h] [rbp-30h] BYREF

v3 = route_open(*(_QWORD *)(a1 + 8), 0LL, 0LL, 0LL);
if ( !v3 )
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 143LL, "cascade_sample", "route does not exist %s", *(const char **)(a1 + 8));
return v9;
}
v4 = v3;
v5 = route_seektread(v3, *(unsigned int *)(a1 + 16), 0xFFFFFFFFLL);
route_tell(v4, &v11, v12, v13);
route_close(v4);
if ( v5 )
{
if ( (unsigned int)table_nrows(v5) )
{
v6 = *(_DWORD *)a1;
*(_DWORD *)(a1 + 16) = v11 + 1;
v7 = cascade_aggregate(v6, v5);
v8 = v7;
if ( v7 )
{
v9 = 0;
if ( !(unsigned int)route_twrite(a2, v7) )
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 173LL, "cascade_sample", "unable to write result");
}
table_destroy(v8);
}
else
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 178LL, "cascade_sample", "no results produced from cascade");
}
table_destroy(v5);
return v9;
}
elog_fprintf(
2LL,
"iiab/cascade.c",
159LL,
"cascade_sample",
"no rows to cascade sample in %s",
*(const char **)(a1 + 8));
table_destroy(v5);
result = 0LL;
}
else
{
elog_fprintf(
2LL,
"iiab/cascade.c",
154LL,
"cascade_sample",
"no data to cascade sample in %s",
*(const char **)(a1 + 8));
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v3; // rax
__int64 v4; // rbp
__int64 v5; // r12
int v6; // edi
__int64 v7; // rax
__int64 v8; // rbp
unsigned int v9; // er13
__int64 result; // rax
int v11; // [rsp+0h] [rbp-38h] BYREF
char v12[4]; // [rsp+4h] [rbp-34h] BYREF
char v13[48]; // [rsp+8h] [rbp-30h] BYREF

v3 = route_open(*(_QWORD *)(a1 + 8), 0LL, 0LL, 0LL);
if ( !v3 )
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 143LL, "cascade_sample", "route does not exist %s", *(const char **)(a1 + 8));
return v9;
}
v4 = v3;
v5 = route_seektread(v3, *(unsigned int *)(a1 + 16), 0xFFFFFFFFLL);
route_tell(v4, &v11, v12, v13);
route_close(v4);
if ( v5 )
{
if ( (unsigned int)table_nrows(v5) )
{
v6 = *(_DWORD *)a1;
*(_DWORD *)(a1 + 16) = v11 + 1;
v7 = func2(v6, v5);
v8 = v7;
if ( v7 )
{
v9 = 0;
if ( !(unsigned int)route_twrite(a2, v7) )
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 173LL, "cascade_sample", "unable to write result");
}
table_destroy(v8);
}
else
{
v9 = -1;
elog_fprintf(5LL, "iiab/cascade.c", 178LL, "cascade_sample", "no results produced from cascade");
}
table_destroy(v5);
return v9;
}
elog_fprintf(
2LL,
"iiab/cascade.c",
159LL,
"cascade_sample",
"no rows to cascade sample in %s",
*(const char **)(a1 + 8));
table_destroy(v5);
result = 0LL;
}
else
{
elog_fprintf(
2LL,
"iiab/cascade.c",
154LL,
"cascade_sample",
"no data to cascade sample in %s",
*(const char **)(a1 + 8));
result = 0LL;
}
return result;
}

original_code:
int cascade_sample(CASCADE *session,
     ROUTE output,
     ROUTE error )
{
     ROUTE rt;
     int r, seq, size, ret=0;
     time_t modt;
     TABLE dataset, result;
     rt = route_open(session->purl, ((void *)0), ((void *)0), 0);
     if ( ! rt ) {
          elog_fprintf(ERROR,"iiab/cascade.c",143,__PRETTY_FUNCTION__,"route does not exist %s", session->purl);
          return -1;
     }
     dataset = route_seektread(rt, session->seq, -1);
     r = route_tell(rt, &seq, &size, &modt);
     route_close(rt);
     if ( ! dataset ) {
          elog_fprintf(DIAG,"iiab/cascade.c",154,__PRETTY_FUNCTION__,"no data to cascade sample in %s", session->purl);
          return 0;
     }
     if ( table_nrows(dataset) == 0 ) {
          elog_fprintf(DIAG,"iiab/cascade.c",159,__PRETTY_FUNCTION__,"no rows to cascade sample in %s", session->purl);
   table_destroy(dataset);
          return 0;
     }
     session->seq = seq+1;
     result = cascade_aggregate(session->fn, dataset);
     if ( result ) {
          if ( ! route_twrite(output, result) ) {
        elog_fprintf(ERROR,"iiab/cascade.c",173,__PRETTY_FUNCTION__,"unable to write result");
        ret = -1;
   }
   table_destroy(result);
     } else {
   elog_fprintf(ERROR,"iiab/cascade.c",178,__PRETTY_FUNCTION__,"no results produced from cascade");
   ret = -1;
     }
     if (dataset)
          table_destroy(dataset);
     return ret;
}

canonical_original_code:
int cascade_sample(struct cascade_info *session, struct route_handle *output, struct route_handle *error)
{
     struct route_handle *rt;
     int r; int seq; int size; int ret = 0;
     long modt;
     struct table_info *dataset; struct table_info *result;
     rt = route_open(session->purl, ((void *)0), ((void *)0), 0);
     if ( ! rt ) {
          elog_fprintf(ERROR,"iiab/cascade.c",143,__PRETTY_FUNCTION__,"route does not exist %s", session->purl);
          return -1;
     }
     dataset = route_seektread(rt, session->seq, -1);
     r = route_tell(rt, &seq, &size, &modt);
     route_close(rt);
     if ( ! dataset ) {
          elog_fprintf(DIAG,"iiab/cascade.c",154,__PRETTY_FUNCTION__,"no data to cascade sample in %s", session->purl);
          return 0;
     }
     if ( table_nrows(dataset) == 0 ) {
          elog_fprintf(DIAG,"iiab/cascade.c",159,__PRETTY_FUNCTION__,"no rows to cascade sample in %s", session->purl);
   table_destroy(dataset);
          return 0;
     }
     session->seq = seq+1;
     result = cascade_aggregate(session->fn, dataset);
     if ( result ) {
          if ( ! route_twrite(output, result) ) {
        elog_fprintf(ERROR,"iiab/cascade.c",173,__PRETTY_FUNCTION__,"unable to write result");
        ret = -1;
   }
   table_destroy(result);
     } else {
   elog_fprintf(ERROR,"iiab/cascade.c",178,__PRETTY_FUNCTION__,"no results produced from cascade");
   ret = -1;
     }
     if (dataset)
          table_destroy(dataset);
     return ret;
}

variable_types: struct route_handle { char *p_url; struct route_lowlevel { int (*ll_magic)(); char *(*ll_prefix)(); char *(*ll_description)(); void (*ll_init)(struct tree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *cf, int debug); void (*ll_fini)(); int (*ll_access)(char *p_url, char *password, char *basename, int flag); void *(*ll_open)(char *p_url, char *comment, char *password, int keep, char *basename); void (*ll_close)(void *lld); int (*ll_write)(void *lld, void *buf, int buflen); int (*ll_twrite)(void *lld, struct table_info { int ncols; int nrows; int minrowkey; int maxrowkey; struct tree_head *data; struct itree_head { struct rb_node *root; struct rb_node *node; } *colorder; struct tree_head *info; struct tree_head *infolookup; struct itree_head *tobegarbage; int refcount; struct itree_head *roworder; char separator; } *tab); int (*ll_tell)(void *lld, int *seq, int *size, long *modt); struct itree_head *(*ll_read)(void *lld, int seq, int offset); struct table_info *(*ll_tread)(void *lld, int seq, int offset); void (*ll_status)(void *lld, char **status, char **info); int (*ll_checkpoint)(void *lld); } *method; void *handle; struct route_buffer { int buflen; char *buffer; } unsent; } *

variable_types:
error
struct route_handle *

variable_types: r int

variable_types: ret int

variable_types: struct table_info { int ncols; int nrows; int minrowkey; int maxrowkey; struct tree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *data; struct itree_head { struct rb_node *root; struct rb_node *node; } *colorder; struct tree_head *info; struct tree_head *infolookup; struct itree_head *tobegarbage; int refcount; struct itree_head *roworder; char separator; } *

user_defined_types: struct route_handle { char *p_url; struct route_lowlevel *method; void *handle; struct route_buffer unsent; }

user_defined_types: struct route_lowlevel { int (*ll_magic)(); char *(*ll_prefix)(); char *(*ll_description)(); void (*ll_init)(struct tree_head *cf, int debug); void (*ll_fini)(); int (*ll_access)(char *p_url, char *password, char *basename, int flag); void *(*ll_open)(char *p_url, char *comment, char *password, int keep, char *basename); void (*ll_close)(void *lld); int (*ll_write)(void *lld, void *buf, int buflen); int (*ll_twrite)(void *lld, struct table_info *tab); int (*ll_tell)(void *lld, int *seq, int *size, long *modt); struct itree_head *(*ll_read)(void *lld, int seq, int offset); struct table_info *(*ll_tread)(void *lld, int seq, int offset); void (*ll_status)(void *lld, char **status, char **info); int (*ll_checkpoint)(void *lld); }


decompiled_code:
__int64 __fastcall timeline_actual(__int64 a1)
{
return timeline_offset + a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return timeline_offset + a1;
}

original_code:
time_t timeline_actual(time_t point)
{
     return point + timeline_offset;
}

canonical_original_code:
long timeline_actual(long point)
{
     return point + timeline_offset;
}

variable_types: point long

return_type: long


decompiled_code:
char *__fastcall timeline_label(__int64 a1, __int64 a2, char *a3, int a4)
{
char *result; // rax
time_t timer; // [rsp+8h] [rbp-70h] BYREF
struct tm tp; // [rsp+10h] [rbp-68h] BYREF

timer = timeline_actual(a1);
localtime_r(&timer, &tp);
if ( !a3 )
return a3;
if ( a2 <= 599 )
{
strftime(a3, a4, "%H:%M:%S", &tp);
result = a3;
}
else if ( a2 <= 25199 )
{
strftime(a3, a4, "%H:%M", &tp);
result = a3;
}
else if ( a2 <= 129599 )
{
strftime(a3, a4, "%H:00", &tp);
result = a3;
}
else
{
if ( a2 <= 604799 )
{
strftime(a3, a4, "%a", &tp);
return a3;
}
if ( a2 > 1209599 && a2 > 5356799 )
{
if ( a2 <= 21427199 )
{
strftime(a3, a4, "%d%b%y", &tp);
}
else if ( a2 > 36892799 )
{
strftime(a3, a4, "%Y", &tp);
}
else
{
strftime(a3, a4, "%b%y", &tp);
}
return a3;
}
strftime(a3, a4, "%d%b", &tp);
result = a3;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(__int64 a1, __int64 a2, char *a3, int a4)
{
char *result; // rax
time_t timer; // [rsp+8h] [rbp-70h] BYREF
struct tm tp; // [rsp+10h] [rbp-68h] BYREF

timer = func0(a1);
localtime_r(&timer, &tp);
if ( !a3 )
return a3;
if ( a2 <= 599 )
{
strftime(a3, a4, "%H:%M:%S", &tp);
result = a3;
}
else if ( a2 <= 25199 )
{
strftime(a3, a4, "%H:%M", &tp);
result = a3;
}
else if ( a2 <= 129599 )
{
strftime(a3, a4, "%H:00", &tp);
result = a3;
}
else
{
if ( a2 <= 604799 )
{
strftime(a3, a4, "%a", &tp);
return a3;
}
if ( a2 > 1209599 && a2 > 5356799 )
{
if ( a2 <= 21427199 )
{
strftime(a3, a4, "%d%b%y", &tp);
}
else if ( a2 > 36892799 )
{
strftime(a3, a4, "%Y", &tp);
}
else
{
strftime(a3, a4, "%b%y", &tp);
}
return a3;
}
strftime(a3, a4, "%d%b", &tp);
result = a3;
}
return result;
}

original_code:
char * timeline_label(time_t point, time_t scope, char *label, int length)
{
     time_t t;
     struct tm t_tm;
     t = timeline_actual(point);
     localtime_r(&t, &t_tm);
     if ( ! label)
       return ((void *)0);
     if (scope < 600)
       strftime(label, length, "%H:%M:%S", &t_tm);
     else if (scope < 25200)
       strftime(label, length, "%H:%M", &t_tm);
     else if (scope < 129600)
       strftime(label, length, "%H:00", &t_tm);
     else if (scope < 604800)
       strftime(label, length, "%a", &t_tm);
     else if (scope < 1209600)
       strftime(label, length, "%d%b", &t_tm);
     else if (scope < 5356800)
       strftime(label, length, "%d%b", &t_tm);
     else if (scope < 21427200)
       strftime(label, length, "%d%b%y", &t_tm);
     else if (scope < 36892800)
       strftime(label, length, "%b%y", &t_tm);
     else
       strftime(label, length, "%Y", &t_tm);
     return label;
}

canonical_original_code:
char *timeline_label(long point, long scope, char *label, int length)
{
     long t;
     struct tm t_tm;
     t = timeline_actual(point);
     localtime_r(&t, &t_tm);
     if ( ! label)
       return ((void *)0);
     if (scope < 600)
       strftime(label, length, "%H:%M:%S", &t_tm);
     else if (scope < 25200)
       strftime(label, length, "%H:%M", &t_tm);
     else if (scope < 129600)
       strftime(label, length, "%H:00", &t_tm);
     else if (scope < 604800)
       strftime(label, length, "%a", &t_tm);
     else if (scope < 1209600)
       strftime(label, length, "%d%b", &t_tm);
     else if (scope < 5356800)
       strftime(label, length, "%d%b", &t_tm);
     else if (scope < 21427200)
       strftime(label, length, "%d%b%y", &t_tm);
     else if (scope < 36892800)
       strftime(label, length, "%b%y", &t_tm);
     else
       strftime(label, length, "%Y", &t_tm);
     return label;
}

variable_types: scope long

variable_types:
label
char *

variable_types: length int

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

return_type: char *


decompiled_code:
__int64 __fastcall timeline_free(_QWORD *a1)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 v4; // rbp

while ( *a1 != *(_QWORD *)*a1 )
{
itree_first(a1);
v2 = itree_get(a1);
v3 = *(_QWORD *)(v2 + 8);
v4 = v2;
if ( v3 )
nm_nfree(v3, "iiab/timeline.c", 461LL, "timeline_free");
nm_nfree(v4, "iiab/timeline.c", 462LL, "timeline_free");
itree_rm(a1);
}
return itree_really_destroy(a1, "iiab/timeline.c", 466LL, "timeline_free");
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
__int64 v2; // rax
__int64 v3; // rdi
__int64 v4; // rbp

while ( *a1 != *(_QWORD *)*a1 )
{
itree_first(a1);
v2 = itree_get(a1);
v3 = *(_QWORD *)(v2 + 8);
v4 = v2;
if ( v3 )
nm_nfree(v3, "iiab/timeline.c", 461LL, "timeline_free");
nm_nfree(v4, "iiab/timeline.c", 462LL, "timeline_free");
itree_rm(a1);
}
return itree_really_destroy(a1, "iiab/timeline.c", 466LL, "timeline_free");
}

original_code:
void timeline_free(ITREE *l)
{
     struct timeline_tick *tick;
     while ( ! ((l)->root->c.list.flink == (l)->root) ) {
   itree_first(l);
   tick = itree_get(l);
   if (tick->label)
        nm_nfree(tick->label,"iiab/timeline.c",461,__PRETTY_FUNCTION__);
   nm_nfree(tick,"iiab/timeline.c",462,__PRETTY_FUNCTION__);
   itree_rm(l);
     }
     itree_really_destroy(l,"iiab/timeline.c",466,__PRETTY_FUNCTION__);
}

canonical_original_code:
void timeline_free(struct itree_head *l)
{
     struct timeline_tick *tick;
     while ( ! ((l)->root->c.list.flink == (l)->root) ) {
   itree_first(l);
   tick = itree_get(l);
   if (tick->label)
        nm_nfree(tick->label,"iiab/timeline.c",461,__PRETTY_FUNCTION__);
   nm_nfree(tick,"iiab/timeline.c",462,__PRETTY_FUNCTION__);
   itree_rm(l);
     }
     itree_really_destroy(l,"iiab/timeline.c",466,__PRETTY_FUNCTION__);
}

variable_types: struct timeline_tick { enum timeline_ticktype { TIMELINE_MAJOR=0, TIMELINE_MINOR=1, TIMELINE_NONE=2 } type; char *label; long point; long ptime; } *

user_defined_types: struct timeline_tick { enum timeline_ticktype type; char *label; long point; long ptime; }


decompiled_code:
__int64 __fastcall timeline_rounddown(_DWORD *a1, unsigned int a2)
{
int v2; // eax
__int64 result; // rax

if ( a2 <= 6 )
{
if ( a2 <= 3 )
{
if ( a2 == 2 )
{
LABEL_6:
*a1 = 0;
return result;
}
if ( a2 != 3 )
return result;
LABEL_5:
a1[1] = 0;
goto LABEL_6;
}
LABEL_10:
a1[2] = 0;
goto LABEL_5;
}
if ( a2 - 7 <= 1 )
{
v2 = a1[3];
a1[3] = 1;
result = (unsigned int)(v2 - 1);
a1[7] -= result;
goto LABEL_10;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, unsigned int a2)
{
int v2; // eax
__int64 result; // rax

if ( a2 <= 6 )
{
if ( a2 <= 3 )
{
if ( a2 == 2 )
{
LABEL_6:
*a1 = 0;
return result;
}
if ( a2 != 3 )
return result;
LABEL_5:
a1[1] = 0;
goto LABEL_6;
}
LABEL_10:
a1[2] = 0;
goto LABEL_5;
}
if ( a2 - 7 <= 1 )
{
v2 = a1[3];
a1[3] = 1;
result = (unsigned int)(v2 - 1);
a1[7] -= result;
goto LABEL_10;
}
return result;
}

original_code:
void timeline_rounddown(struct tm *t_tmp, enum timeline_units unit)
{
     switch (unit) {
     case NOUNIT:
   break;
     case YEARS:
     case MONTHS:
   t_tmp->tm_yday -= t_tmp->tm_mday-1;
   t_tmp->tm_mday = 1;
   t_tmp->tm_hour = 0;
     case MDAYS:
     case DAYS:
     case WEEKDAYS:
   t_tmp->tm_hour = 0;
     case HOURS:
   t_tmp->tm_min = 0;
     case MINS:
   t_tmp->tm_sec = 0;
     case SECS:
   break;
     }
}

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: enum timeline_units { NOUNIT=0, SECS=1, MINS=2, HOURS=3, WEEKDAYS=4, DAYS=5, MDAYS=6, MONTHS=7, YEARS=8 }


decompiled_code:
__int64 __fastcall timeline_calc(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // r14
__int64 v5; // rax
int *v6; // rdx
int v7; // ebx
__int64 v8; // rax
time_t v9; // rax
char *v10; // rbx
unsigned int v11; // esi
time_t v12; // rdx
time_t i; // rax
__int64 v14; // rax
time_t v15; // rsi
__int64 v16; // rax
__int64 v17; // r12
time_t v18; // rax
__int64 v20; // [rsp+8h] [rbp-C0h]
__int64 v21; // [rsp+8h] [rbp-C0h]
time_t timer; // [rsp+28h] [rbp-A0h] BYREF
char s[32]; // [rsp+30h] [rbp-98h] BYREF
struct tm tp; // [rsp+50h] [rbp-78h] BYREF

if ( (int)a2 - (int)a1 <= 2 || a1 + 3 >= a2 )
return 0LL;
v4 = itree_really_create("iiab/timeline.c", 156LL, "timeline_calc");
v5 = timeline_tickpoints;
if ( timeline_tickpoints )
{
v6 = (int *)((char *)&timeline_tickpoints + 24);
v7 = 0;
do
{
if ( v5 >= a3 )
break;
v5 = *v6;
v6 += 6;
++v7;
}
while ( (_DWORD)v5 );
}
else
{
v7 = 0;
}
timer = timeline_offset + a1;
localtime_r(&timer, &tp);
strftime(s, 0x14uLL, "%d%b%y", &tp);
v8 = nm_nmalloc(32LL, "iiab/timeline.c", 176LL, "timeline_calc");
*(_DWORD *)v8 = 2;
v20 = v8;
*(_QWORD *)(v8 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 178LL, "timeline_calc");
v9 = timer;
*(_QWORD *)(v20 + 16) = a1;
*(_QWORD *)(v20 + 24) = v9;
itree_add(v4, (unsigned int)a1, v20);
v10 = (char *)&timeline_tickpoints + 24 * v7;
v11 = *((_DWORD *)v10 + 1);
timer -= *((int *)v10 + 2);
timeline_rounddown(&tp, v11);
timer = mktime(&tp);
v12 = timer;
for ( i = timeline_offset + a2; timeline_offset + a2 > timer; i = timeline_offset + a2 )
{
timer = v12 + *((int *)v10 + 2);
localtime_r(&timer, &tp);
switch ( *((_DWORD *)v10 + 1) )
{
case 0:
strftime(s, 0x14uLL, "??", &tp);
break;
case 1:
strftime(s, 0x14uLL, "%H:%M:%S", &tp);
break;
case 2:
strftime(s, 0x14uLL, "%H:%M", &tp);
break;
case 3:
strftime(s, 0x14uLL, "%H:00", &tp);
break;
case 4:
strftime(s, 0x14uLL, "%a", &tp);
break;
case 5:
strftime(s, 0x14uLL, "%d%b", &tp);
break;
case 6:
strftime(s, 0x14uLL, "%d", &tp);
break;
case 7:
strftime(s, 0x14uLL, "%b", &tp);
break;
case 8:
strftime(s, 0x14uLL, "%Y", &tp);
break;
default:
break;
}
v14 = nm_nmalloc(32LL, "iiab/timeline.c", 235LL, "timeline_calc");
*(_DWORD *)v14 = 0;
v21 = v14;
*(_QWORD *)(v14 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 237LL, "timeline_calc");
v15 = timer - timeline_offset;
*(_QWORD *)(v21 + 24) = timer;
*(_QWORD *)(v21 + 16) = v15;
itree_add(v4, v15, v21);
v12 = timer;
}
timer = i;
localtime_r(&timer, &tp);
strftime(s, 0x14uLL, "%d%b%y", &tp);
v16 = nm_nmalloc(32LL, "iiab/timeline.c", 290LL, "timeline_calc");
*(_DWORD *)v16 = 2;
v17 = v16;
*(_QWORD *)(v16 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 292LL, "timeline_calc");
v18 = timer;
*(_QWORD *)(v17 + 16) = a2;
*(_QWORD *)(v17 + 24) = v18;
itree_add(v4, (unsigned int)a2, v17);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // r14
__int64 v5; // rax
int *v6; // rdx
int v7; // ebx
__int64 v8; // rax
time_t v9; // rax
char *v10; // rbx
unsigned int v11; // esi
time_t v12; // rdx
time_t i; // rax
__int64 v14; // rax
time_t v15; // rsi
__int64 v16; // rax
__int64 v17; // r12
time_t v18; // rax
__int64 v20; // [rsp+8h] [rbp-C0h]
__int64 v21; // [rsp+8h] [rbp-C0h]
time_t timer; // [rsp+28h] [rbp-A0h] BYREF
char s[32]; // [rsp+30h] [rbp-98h] BYREF
struct tm tp; // [rsp+50h] [rbp-78h] BYREF

if ( (int)a2 - (int)a1 <= 2 || a1 + 3 >= a2 )
return 0LL;
v4 = itree_really_create("iiab/timeline.c", 156LL, "timeline_calc");
v5 = timeline_tickpoints;
if ( timeline_tickpoints )
{
v6 = (int *)((char *)&timeline_tickpoints + 24);
v7 = 0;
do
{
if ( v5 >= a3 )
break;
v5 = *v6;
v6 += 6;
++v7;
}
while ( (_DWORD)v5 );
}
else
{
v7 = 0;
}
timer = timeline_offset + a1;
localtime_r(&timer, &tp);
strftime(s, 0x14uLL, "%d%b%y", &tp);
v8 = nm_nmalloc(32LL, "iiab/timeline.c", 176LL, "timeline_calc");
*(_DWORD *)v8 = 2;
v20 = v8;
*(_QWORD *)(v8 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 178LL, "timeline_calc");
v9 = timer;
*(_QWORD *)(v20 + 16) = a1;
*(_QWORD *)(v20 + 24) = v9;
itree_add(v4, (unsigned int)a1, v20);
v10 = (char *)&timeline_tickpoints + 24 * v7;
v11 = *((_DWORD *)v10 + 1);
timer -= *((int *)v10 + 2);
func3(&tp, v11);
timer = mktime(&tp);
v12 = timer;
for ( i = timeline_offset + a2; timeline_offset + a2 > timer; i = timeline_offset + a2 )
{
timer = v12 + *((int *)v10 + 2);
localtime_r(&timer, &tp);
switch ( *((_DWORD *)v10 + 1) )
{
case 0:
strftime(s, 0x14uLL, "??", &tp);
break;
case 1:
strftime(s, 0x14uLL, "%H:%M:%S", &tp);
break;
case 2:
strftime(s, 0x14uLL, "%H:%M", &tp);
break;
case 3:
strftime(s, 0x14uLL, "%H:00", &tp);
break;
case 4:
strftime(s, 0x14uLL, "%a", &tp);
break;
case 5:
strftime(s, 0x14uLL, "%d%b", &tp);
break;
case 6:
strftime(s, 0x14uLL, "%d", &tp);
break;
case 7:
strftime(s, 0x14uLL, "%b", &tp);
break;
case 8:
strftime(s, 0x14uLL, "%Y", &tp);
break;
default:
break;
}
v14 = nm_nmalloc(32LL, "iiab/timeline.c", 235LL, "timeline_calc");
*(_DWORD *)v14 = 0;
v21 = v14;
*(_QWORD *)(v14 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 237LL, "timeline_calc");
v15 = timer - timeline_offset;
*(_QWORD *)(v21 + 24) = timer;
*(_QWORD *)(v21 + 16) = v15;
itree_add(v4, v15, v21);
v12 = timer;
}
timer = i;
localtime_r(&timer, &tp);
strftime(s, 0x14uLL, "%d%b%y", &tp);
v16 = nm_nmalloc(32LL, "iiab/timeline.c", 290LL, "timeline_calc");
*(_DWORD *)v16 = 2;
v17 = v16;
*(_QWORD *)(v16 + 8) = nm_xnstrdup(s, "iiab/timeline.c", 292LL, "timeline_calc");
v18 = timer;
*(_QWORD *)(v17 + 16) = a2;
*(_QWORD *)(v17 + 24) = v18;
itree_add(v4, (unsigned int)a2, v17);
return v4;
}

original_code:
ITREE *timeline_calc(time_t min,
       time_t max,
       time_t dispdiff,
       int maxchars )
{
     int diff;
     int maxlabels;
     int tickindex;
     ITREE *results;
     time_t t;
     struct tm t_tm;
     struct timeline_tick *tick;
     char label[20];
     diff = max - min;
     if (min +3 >= max || diff < 3)
   return ((void *)0);
     maxlabels = maxchars / (1+8+1);
     results = itree_really_create("iiab/timeline.c",156,__PRETTY_FUNCTION__);
     for (tickindex = 0; timeline_tickpoints[ tickindex ].threshold;
   tickindex++)
   if (dispdiff <= timeline_tickpoints[ tickindex ].threshold)
        break;
     t = min + timeline_offset;
     localtime_r(&t, &t_tm);
     strftime(label, 20, "%d%b%y", &t_tm);
     tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",176,__PRETTY_FUNCTION__);
     tick->type = TIMELINE_NONE;
     tick->label = nm_xnstrdup(label,"iiab/timeline.c",178,__PRETTY_FUNCTION__);
     tick->point = min;
     tick->ptime = t;
     itree_add(results, tick->point, tick);
     t -= timeline_tickpoints[ tickindex ].major;
     timeline_rounddown(&t_tm, timeline_tickpoints[ tickindex ].base);
     t = mktime(&t_tm);
     while (t < max + timeline_offset) {
   t += timeline_tickpoints[ tickindex ].major;
   localtime_r(&t, &t_tm);
   switch (timeline_tickpoints[ tickindex ].base) {
   case NOUNIT:
        strftime(label, 20, "??", &t_tm);
        break;
   case SECS:
        strftime(label, 20, "%H:%M:%S", &t_tm);
        break;
   case MINS:
        strftime(label, 20, "%H:%M", &t_tm);
        break;
   case HOURS:
        strftime(label, 20, "%H:00", &t_tm);
        break;
   case WEEKDAYS:
        strftime(label, 20, "%a", &t_tm);
        break;
   case DAYS:
        strftime(label, 20, "%d%b", &t_tm);
        break;
   case MDAYS:
        strftime(label, 20, "%d", &t_tm);
        break;
   case MONTHS:
        strftime(label, 20, "%b", &t_tm);
        break;
   case YEARS:
        strftime(label, 20, "%Y", &t_tm);
        break;
   }
   tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",235,__PRETTY_FUNCTION__);
   tick->type = TIMELINE_MAJOR;
   tick->label = nm_xnstrdup(label,"iiab/timeline.c",237,__PRETTY_FUNCTION__);
   tick->point = t - timeline_offset;
   tick->ptime = t;
   itree_add(results, tick->point, tick);
     }
     t = max + timeline_offset;
     localtime_r(&t, &t_tm);
     strftime(label, 20, "%d%b%y", &t_tm);
     tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",290,__PRETTY_FUNCTION__);
     tick->type = TIMELINE_NONE;
     tick->label = nm_xnstrdup(label,"iiab/timeline.c",292,__PRETTY_FUNCTION__);
     tick->point = max;
     tick->ptime = t;
     itree_add(results, max, tick);
     return results;
}

canonical_original_code:
struct itree_head *timeline_calc(long min, long max, long dispdiff, int maxchars)
{
     int diff;
     int maxlabels;
     int tickindex;
     struct itree_head *results;
     long t;
     struct tm t_tm;
     struct timeline_tick *tick;
     char label[20];
     diff = max - min;
     if (min +3 >= max || diff < 3)
   return ((void *)0);
     maxlabels = maxchars / (1+8+1);
     results = itree_really_create("iiab/timeline.c",156,__PRETTY_FUNCTION__);
     for (tickindex = 0; timeline_tickpoints[ tickindex ].threshold;
   tickindex++)
   if (dispdiff <= timeline_tickpoints[ tickindex ].threshold)
        break;
     t = min + timeline_offset;
     localtime_r(&t, &t_tm);
     strftime(label, 20, "%d%b%y", &t_tm);
     tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",176,__PRETTY_FUNCTION__);
     tick->type = TIMELINE_NONE;
     tick->label = nm_xnstrdup(label,"iiab/timeline.c",178,__PRETTY_FUNCTION__);
     tick->point = min;
     tick->ptime = t;
     itree_add(results, tick->point, tick);
     t -= timeline_tickpoints[ tickindex ].major;
     timeline_rounddown(&t_tm, timeline_tickpoints[ tickindex ].base);
     t = mktime(&t_tm);
     while (t < max + timeline_offset) {
   t += timeline_tickpoints[ tickindex ].major;
   localtime_r(&t, &t_tm);
   switch (timeline_tickpoints[ tickindex ].base) {
   case NOUNIT:
        strftime(label, 20, "??", &t_tm);
        break;
   case SECS:
        strftime(label, 20, "%H:%M:%S", &t_tm);
        break;
   case MINS:
        strftime(label, 20, "%H:%M", &t_tm);
        break;
   case HOURS:
        strftime(label, 20, "%H:00", &t_tm);
        break;
   case WEEKDAYS:
        strftime(label, 20, "%a", &t_tm);
        break;
   case DAYS:
        strftime(label, 20, "%d%b", &t_tm);
        break;
   case MDAYS:
        strftime(label, 20, "%d", &t_tm);
        break;
   case MONTHS:
        strftime(label, 20, "%b", &t_tm);
        break;
   case YEARS:
        strftime(label, 20, "%Y", &t_tm);
        break;
   }
   tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",235,__PRETTY_FUNCTION__);
   tick->type = TIMELINE_MAJOR;
   tick->label = nm_xnstrdup(label,"iiab/timeline.c",237,__PRETTY_FUNCTION__);
   tick->point = t - timeline_offset;
   tick->ptime = t;
   itree_add(results, tick->point, tick);
     }
     t = max + timeline_offset;
     localtime_r(&t, &t_tm);
     strftime(label, 20, "%d%b%y", &t_tm);
     tick = nm_nmalloc(sizeof(struct timeline_tick),"iiab/timeline.c",290,__PRETTY_FUNCTION__);
     tick->type = TIMELINE_NONE;
     tick->label = nm_xnstrdup(label,"iiab/timeline.c",292,__PRETTY_FUNCTION__);
     tick->point = max;
     tick->ptime = t;
     itree_add(results, max, tick);
     return results;
}

variable_types: min long

variable_types: max long

variable_types: dispdiff long

variable_types: maxchars int

variable_types: diff int

variable_types: maxlabels int

variable_types: tickindex int

variable_types: struct itree_head { struct rb_node { union { struct { struct rb_node *flink; struct rb_node *blink; } list; struct { struct rb_node *left; struct rb_node *right; } child; } c; union { struct rb_node *parent; struct rb_node *root; } p; struct status { unsigned int red; unsigned int internal; unsigned int left; unsigned int root; unsigned int head; } s; union { unsigned int ikey; char *key; struct rb_node *lext; } k; union { char *val; struct rb_node *rext; } v; } *root; struct rb_node *node; } *

variable_types:
label
char [20]

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

user_defined_types: enum timeline_ticktype { TIMELINE_MAJOR=0, TIMELINE_MINOR=1, TIMELINE_NONE=2 }