main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx

for ( i = dup(0); ; readshow(i) )
readshow(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int i; // ebx

for ( i = dup(0); ; func7(i) )
func7(0);
}

original_code:
int
main(int argc, char *argv[])
{
    int newfd;
    newfd = dup(0);
    for (;;) {
        readshow(0);
        readshow(newfd);
    }
    exit(0);
}

variable_types:
argv
char *[]

variable_types: newfd int


showsig

decompiled_code:
void __fastcall showsig(unsigned int a1)
{
printf("signal=%d\n", a1);
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
printf("signal=%d\n", a1);
}

original_code:
void
showsig(int sig)
{
    printf("signal=%d\n", sig);
}

variable_types: sig int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
signal(2, (__sighandler_t)showsig);
pause();
exit(0);
}

original_code:
int
main(int argc, char *argv[])
{
    signal(2, showsig);
    pause();
    exit(0);
}

variable_types: argc int

return_type: int


decompiled_code:
unsigned int msg_sleep()
{
puts("got SIGINT, sleep...");
return sleep(3u);
}

canonical_decompiled_code:
unsigned int func7()
{
puts("got SIGINT, sleep...");
return sleep(3u);
}

original_code:
void
msg_sleep(int sig)
{
    puts("got SIGINT, sleep...");
    sleep(3);
}


decompiled_code:
__sighandler_t __fastcall trap_signal(int sig, void (*a2)(int))
{
bool v2; // sf
__sighandler_t result; // rax
struct sigaction v4; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v5; // [rsp+A0h] [rbp-A8h] BYREF

v5.sa_handler = a2;
sigemptyset(&v5.sa_mask);
v5.sa_flags = 0x10000000;
v2 = sigaction(sig, &v5, &v4) < 0;
result = 0LL;
if ( !v2 )
result = v4.sa_handler;
return result;
}

canonical_decompiled_code:
__sighandler_t __fastcall func8(int sig, void (*a2)(int))
{
bool v2; // sf
__sighandler_t result; // rax
struct sigaction v4; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v5; // [rsp+A0h] [rbp-A8h] BYREF

v5.sa_handler = a2;
sigemptyset(&v5.sa_mask);
v5.sa_flags = 0x10000000;
v2 = sigaction(sig, &v5, &v4) < 0;
result = 0LL;
if ( !v2 )
result = v4.sa_handler;
return result;
}

original_code:
sighandler_t
trap_signal(int sig, sighandler_t handler)
{
    struct sigaction act, old;
    act.__sigaction_handler.sa_handler = handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0x10000000;
    if (sigaction(sig, &act, &old) < 0)
        return ((void *)0);
    return old.__sigaction_handler.sa_handler;
}

canonical_original_code:
void (*trap_signal(int sig, void (*handler)(int)))(int)
{
    struct sigaction act; struct sigaction old;
    act.__sigaction_handler.sa_handler = handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0x10000000;
    if (sigaction(sig, &act, &old) < 0)
        return ((void *)0);
    return old.__sigaction_handler.sa_handler;
}

variable_types:
handler
void (*)(int)

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

return_type: void (*)(int)

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
trap_signal(2, (void (*)(int))msg_sleep);
pause();
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
func8(2, (void (*)(int))msg_sleep);
pause();
exit(0);
}

original_code:
int
main(int argc, char *argv[])
{
    trap_signal(2, msg_sleep);
    pause();
    exit(0);
}


decompiled_code:
__int64 gfile_null_on_read()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
static size_t gfile_null_on_read(void *ud, void *ptr, size_t n, jmp_buf env) {
  ((void)ud);
  ((void)ptr);
  ((void)n);
  ((void)env);
  return 0;
}

canonical_original_code:
static unsigned long gfile_null_on_read(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag env[1]) {
  ((void)ud);
  ((void)ptr);
  ((void)n);
  ((void)env);
  return 0;
}

variable_types:
ud
void *

variable_types:
ptr
void *

variable_types:
n
unsigned long

variable_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1]

return_type: unsigned long

user_defined_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t __saved_mask; }


decompiled_code:
void gfile_null_on_close()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static void gfile_null_on_close(void *ud) {
  ((void)ud);
}


offmin

decompiled_code:
unsigned __int64 __fastcall offmin(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int64 result; // rax

result = a1;
if ( a2 <= a1 )
result = a2;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int64 result; // rax

result = a1;
if ( a2 <= a1 )
result = a2;
return result;
}

original_code:
static gar_off_t offmin(gar_off_t x, gar_off_t y) {
  return (x < y) ? x : y;
}

canonical_original_code:
static unsigned long long offmin(unsigned long long x, unsigned long long y) {
  return (x < y) ? x : y;
}

variable_types:
x
unsigned long long

variable_types:
y
unsigned long long

return_type: unsigned long long


decompiled_code:
__int64 __fastcall check_off(unsigned __int64 a1, unsigned __int64 a2, __int64 a3)
{
__int64 result; // rax

if ( a1 > a2 )
{
gar_error(a3, 0LL, "out-of-range seek offset");
result = gfile_null_on_seek();
}
return result;
}

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

if ( a1 > a2 )
{
gar_error(a3, 0LL, "out-of-range seek offset");
result = func4();
}
return result;
}

original_code:
void check_off(gar_off_t off, gar_off_t len, jmp_buf env) {
  if (off > len) {
    _gar_error(env, ((void *)0), "out-of-range seek offset");
  }
}

canonical_original_code:
void check_off(unsigned long long off, unsigned long long len, struct __jmp_buf_tag env[1]) {
  if (off > len) {
    _gar_error(env, ((void *)0), "out-of-range seek offset");
  }
}

variable_types:
off
unsigned long long

variable_types:
len
unsigned long long

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
__int64 __fastcall gfile_null_on_seek(__int64 a1, unsigned __int64 a2, __int64 a3)
{
return check_off(a2, 0LL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int64 a2, __int64 a3)
{
return func3(a2, 0LL, a3);
}

original_code:
static void gfile_null_on_seek(void *ud, gar_off_t off, jmp_buf env) {
  ((void)ud);
  ((void)off);
  check_off(off, 0, env);
}

canonical_original_code:
static void gfile_null_on_seek(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]) {
  ((void)ud);
  ((void)off);
  check_off(off, 0, env);
}


decompiled_code:
void __fastcall gfile_null_on_dup(__int64 a1, _QWORD *a2)
{
gar_gfile_null(a2);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, _QWORD *a2)
{
func5(a2);
}

original_code:
static void gfile_null_on_dup(void *ud, gar_gfile_t *dst, jmp_buf env) {
  ((void)ud);
  ((void)env);
  gar_gfile_null(dst);
}

canonical_original_code:
static void gfile_null_on_dup(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]) {
  ((void)ud);
  ((void)env);
  gar_gfile_null(dst);
}

variable_types: struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); } *

variable_types:
env
struct __jmp_buf_tag [1]

user_defined_types: struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); }


decompiled_code:
__int64 __fastcall gar_gfile_read(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD))(a1 + 8))(*(_QWORD *)a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD))(a1 + 8))(*(_QWORD *)a1);
}

original_code:
size_t gar_gfile_read(const gar_gfile_t *gf, void *ptr, size_t n, jmp_buf env) {
  return gf->read(gf->ud, ptr, n, env);
}

canonical_original_code:
unsigned long gar_gfile_read(struct gar_gfile *gf, void *ptr, unsigned long n, struct __jmp_buf_tag env[1]) {
  return gf->read(gf->ud, ptr, n, env);
}


decompiled_code:
__int64 __fastcall gfile_part_on_read(__int64 a1, __int64 a2, unsigned __int64 a3)
{
__int64 result; // rax

offmin(a3, *(_QWORD *)(a1 + 56) - *(_QWORD *)(a1 + 40));
result = gar_gfile_read(a1);
*(_QWORD *)(a1 + 40) += result;
return result;
}

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

func2(a3, *(_QWORD *)(a1 + 56) - *(_QWORD *)(a1 + 40));
result = func7(a1);
*(_QWORD *)(a1 + 40) += result;
return result;
}

original_code:
static size_t gfile_part_on_read(void *ud, void *ptr, size_t n, jmp_buf env) {
  gfile_part_ud_t *pud = (gfile_part_ud_t *)ud;
  size_t m = (size_t)offmin(n, pud->len - pud->pos);
  size_t nread = gar_gfile_read(&pud->gf, ptr, m, env);
  pud->pos += nread;
  return nread;
}

canonical_original_code:
static unsigned long gfile_part_on_read(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag env[1]) {
  struct gfile_part_ud *pud = (gfile_part_ud_t *)ud;
  unsigned long m = (size_t)offmin(n, pud->len - pud->pos);
  unsigned long nread = gar_gfile_read(&pud->gf, ptr, m, env);
  pud->pos += nread;
  return nread;
}

variable_types: struct gfile_part_ud { struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); } gf; unsigned long long pos; unsigned long long off; unsigned long long len; } *

variable_types:
m
unsigned long

variable_types:
nread
unsigned long

user_defined_types: struct gfile_part_ud { struct gar_gfile gf; unsigned long long pos; unsigned long long off; unsigned long long len; }


decompiled_code:
__int64 __fastcall gar_gfile_seek(__int64 a1)
{
return (*(__int64 (__fastcall **)(_QWORD))(a1 + 16))(*(_QWORD *)a1);
}

original_code:
void gar_gfile_seek(const gar_gfile_t *gf, gar_off_t off, jmp_buf env) {
  gf->seek(gf->ud, off, env);
}

canonical_original_code:
void gar_gfile_seek(struct gar_gfile *gf, unsigned long long off, struct __jmp_buf_tag env[1]) {
  gf->seek(gf->ud, off, env);
}


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

original_code:
void gar_gfile_dup(const gar_gfile_t *gf, gar_gfile_t *dst, jmp_buf env) {
  gf->dup(gf->ud, dst, env);
}

canonical_original_code:
void gar_gfile_dup(struct gar_gfile *gf, struct gar_gfile *dst, struct __jmp_buf_tag env[1]) {
  gf->dup(gf->ud, dst, env);
}

variable_types:
dst
struct gar_gfile *


decompiled_code:
__int64 __fastcall gfile_part_on_close(_QWORD *a1)
{
gar_gfile_close(a1);
return gar_free(a1);
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD *a1)
{
func12(a1);
return gar_free(a1);
}

original_code:
static void gfile_part_on_close(void *ud) {
  gfile_part_ud_t *pud = (gfile_part_ud_t *)ud;
  gar_gfile_close(&pud->gf);
  _gar_free(pud);
}

canonical_original_code:
static void gfile_part_on_close(void *ud) {
  struct gfile_part_ud *pud = (gfile_part_ud_t *)ud;
  gar_gfile_close(&pud->gf);
  _gar_free(pud);
}


decompiled_code:
__int64 __fastcall on_out_of_memory(__int64 a1)
{
gar_error(a1, 0LL, "out of memory");
return gar_malloc();
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
gar_error(a1, 0LL, "out of memory");
return func1();
}

original_code:
static void on_out_of_memory(jmp_buf env) {
  _gar_error(env, ((void *)0), "out of memory");
}

canonical_original_code:
static void on_out_of_memory(struct __jmp_buf_tag env[1]) {
  _gar_error(env, ((void *)0), "out of memory");
}


decompiled_code:
unsigned __int64 __fastcall decode_u32_le(unsigned __int8 *a1, unsigned __int64 *a2)
{
unsigned __int64 result; // rax

result = ((unsigned __int64)a1[3] << 24) | *a1 | ((unsigned __int64)a1[2] << 16) | ((unsigned __int64)a1[1] << 8);
*a2 = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int8 *a1, unsigned __int64 *a2)
{
unsigned __int64 result; // rax

result = ((unsigned __int64)a1[3] << 24) | *a1 | ((unsigned __int64)a1[2] << 16) | ((unsigned __int64)a1[1] << 8);
*a2 = result;
return result;
}

canonical_original_code:
static void decode_u32_le(unsigned char s[4], unsigned long *t) {
  unsigned long a = s[0];
  unsigned long b = s[1];
  unsigned long c = s[2];
  unsigned long d = s[3];
  *t = a | (b << 8) | (c << 16) | (d << 24);
}

variable_types:
s
unsigned char [4]

variable_types:
t
unsigned long *

variable_types:
a
unsigned long

variable_types:
b
unsigned long

variable_types:
c
unsigned long

variable_types:
d
unsigned long


decompiled_code:
__int64 __fastcall read_pk0304_header(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v3; // rdx
__int64 result; // rax
int v5; // edx
unsigned __int8 v6[4]; // [rsp+0h] [rbp-28h] BYREF
unsigned __int16 v7; // [rsp+4h] [rbp-24h] BYREF
unsigned __int16 v8; // [rsp+6h] [rbp-22h] BYREF
unsigned __int16 v9; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v10; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v11; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int8 v12[4]; // [rsp+Eh] [rbp-1Ah] BYREF
unsigned __int8 v13[4]; // [rsp+12h] [rbp-16h] BYREF
unsigned __int8 v14[4]; // [rsp+16h] [rbp-12h] BYREF
unsigned __int16 v15; // [rsp+1Ah] [rbp-Eh] BYREF
unsigned __int16 v16[6]; // [rsp+1Ch] [rbp-Ch] BYREF

v3 = gar_gfile_read(a1, v6, 30LL, a3);
result = 0LL;
if ( v3 > 0x1D )
{
v5 = memcmp(v6, &unk_590, 4uLL);
result = 0LL;
if ( !v5 )
{
decode_u32_le(v6, (unsigned __int64 *)a2);
decode_u16_le(&v7, (_WORD *)(a2 + 8));
decode_u16_le(&v8, (_WORD *)(a2 + 10));
decode_u16_le(&v9, (_WORD *)(a2 + 12));
decode_u16_le(&v10, (_WORD *)(a2 + 14));
decode_u16_le(&v11, (_WORD *)(a2 + 16));
decode_u32_le(v12, (unsigned __int64 *)(a2 + 24));
decode_u32_le(v13, (unsigned __int64 *)(a2 + 32));
decode_u32_le(v14, (unsigned __int64 *)(a2 + 40));
decode_u16_le(&v15, (_WORD *)(a2 + 48));
decode_u16_le(v16, (_WORD *)(a2 + 50));
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v3; // rdx
__int64 result; // rax
int v5; // edx
unsigned __int8 v6[4]; // [rsp+0h] [rbp-28h] BYREF
unsigned __int16 v7; // [rsp+4h] [rbp-24h] BYREF
unsigned __int16 v8; // [rsp+6h] [rbp-22h] BYREF
unsigned __int16 v9; // [rsp+8h] [rbp-20h] BYREF
unsigned __int16 v10; // [rsp+Ah] [rbp-1Eh] BYREF
unsigned __int16 v11; // [rsp+Ch] [rbp-1Ch] BYREF
unsigned __int8 v12[4]; // [rsp+Eh] [rbp-1Ah] BYREF
unsigned __int8 v13[4]; // [rsp+12h] [rbp-16h] BYREF
unsigned __int8 v14[4]; // [rsp+16h] [rbp-12h] BYREF
unsigned __int16 v15; // [rsp+1Ah] [rbp-Eh] BYREF
unsigned __int16 v16[6]; // [rsp+1Ch] [rbp-Ch] BYREF

v3 = gar_gfile_read(a1, v6, 30LL, a3);
result = 0LL;
if ( v3 > 0x1D )
{
v5 = memcmp(v6, &unk_590, 4uLL);
result = 0LL;
if ( !v5 )
{
func0(v6, (unsigned __int64 *)a2);
func1(&v7, (_WORD *)(a2 + 8));
func1(&v8, (_WORD *)(a2 + 10));
func1(&v9, (_WORD *)(a2 + 12));
func1(&v10, (_WORD *)(a2 + 14));
func1(&v11, (_WORD *)(a2 + 16));
func0(v12, (unsigned __int64 *)(a2 + 24));
func0(v13, (unsigned __int64 *)(a2 + 32));
func0(v14, (unsigned __int64 *)(a2 + 40));
func1(&v15, (_WORD *)(a2 + 48));
func1(v16, (_WORD *)(a2 + 50));
result = 1LL;
}
}
return result;
}

original_code:
static int read_pk0304_header(const gar_gfile_t *gf, pk0304_header_t *hdr,
                              jmp_buf env) {
  byte_t s[30];
  size_t n;
  n = gar_gfile_read(gf, s, sizeof(s), env);
  if (n < sizeof(s) || memcmp(s, "PK\3\4", 4)) {
    return 0;
  }
  decode_u32_le(&s[0], &hdr->sig);
  decode_u16_le(&s[4], &hdr->need_ver);
  decode_u16_le(&s[6], &hdr->flags);
  decode_u16_le(&s[8], &hdr->comp_method);
  decode_u16_le(&s[10], &hdr->last_mod_time);
  decode_u16_le(&s[12], &hdr->last_mod_date);
  decode_u32_le(&s[14], &hdr->crc32);
  decode_u32_le(&s[18], &hdr->comp_size);
  decode_u32_le(&s[22], &hdr->uncomp_size);
  decode_u16_le(&s[26], &hdr->fname_len);
  decode_u16_le(&s[28], &hdr->extra_len);
  return 1;
}

canonical_original_code:
static int read_pk0304_header(struct gar_gfile *gf, struct pk0304_header *hdr, struct __jmp_buf_tag env[1]) {
  unsigned char s[30];
  unsigned long n;
  n = gar_gfile_read(gf, s, sizeof(s), env);
  if (n < sizeof(s) || memcmp(s, "PK\3\4", 4)) {
    return 0;
  }
  decode_u32_le(&s[0], &hdr->sig);
  decode_u16_le(&s[4], &hdr->need_ver);
  decode_u16_le(&s[6], &hdr->flags);
  decode_u16_le(&s[8], &hdr->comp_method);
  decode_u16_le(&s[10], &hdr->last_mod_time);
  decode_u16_le(&s[12], &hdr->last_mod_date);
  decode_u32_le(&s[14], &hdr->crc32);
  decode_u32_le(&s[18], &hdr->comp_size);
  decode_u32_le(&s[22], &hdr->uncomp_size);
  decode_u16_le(&s[26], &hdr->fname_len);
  decode_u16_le(&s[28], &hdr->extra_len);
  return 1;
}

variable_types: struct pk0304_header { unsigned long sig; unsigned short need_ver; unsigned short flags; unsigned short comp_method; unsigned short last_mod_time; unsigned short last_mod_date; unsigned long crc32; unsigned long comp_size; unsigned long uncomp_size; unsigned short fname_len; unsigned short extra_len; } *

variable_types:
s
unsigned char [30]

variable_types:
n
unsigned long


original_code:
static int on_find(const gar_fstat_t *fstat, void *ud, jmp_buf env) {
  const gar_zstat_t *zstat = (const gar_zstat_t *)fstat;
  find_ud_t *fud = (find_ud_t *)ud;
  ((void)env);
  if (strcmp(fud->fname, zstat->fstat.fname) == 0) {
    *(fud->zstat) = *zstat;
    fud->zstat->fstat.fname = ((void *)0);
    return 1;
  } else {
    return 0;
  }
}

canonical_original_code:
static int on_find(struct gar_fstat *fstat, void *ud, struct __jmp_buf_tag env[1]) {
  struct gar_zstat *zstat = (const gar_zstat_t *)fstat;
  struct find_ud *fud = (find_ud_t *)ud;
  ((void)env);
  if (strcmp(fud->fname, zstat->fstat.fname) == 0) {
    *(fud->zstat) = *zstat;
    fud->zstat->fstat.fname = ((void *)0);
    return 1;
  } else {
    return 0;
  }
}

variable_types: struct gar_fstat { char *fname; unsigned long fsize; } *

variable_types:
ud
void *

variable_types: struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } [1]

variable_types: struct find_ud { char *fname; struct gar_zstat { struct gar_fstat { char *fname; unsigned long fsize; } fstat; unsigned short comp_method; unsigned long long data_off; unsigned long long data_len; } *zstat; } *

user_defined_types: struct gar_zstat { struct gar_fstat fstat; unsigned short comp_method; unsigned long long data_off; unsigned long long data_len; }

user_defined_types: struct find_ud { char *fname; struct gar_zstat *zstat; }


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

if ( a1 )
{
gar_gfile_close();
result = gar_free(a1);
}
return result;
}

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

if ( a1 )
{
gar_gfile_close();
result = gar_free(a1);
}
return result;
}

original_code:
void gar_archive_close(gar_t *G) {
  if (G != ((void *)0)) {
    gar_gfile_close(&G->gf);
    _gar_free(G);
  }
}

canonical_original_code:
void gar_archive_close(struct gar *G) {
  if (G != ((void *)0)) {
    gar_gfile_close(&G->gf);
    _gar_free(G);
  }
}

variable_types: struct gar { struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); } gf; } *


original_code:
int gar_enum(gar_t *G, gar_enum_t fn, void *ud, jmp_buf _env) {
  jmp_buf env;
  char *volatile fname = ((void *)0);
  size_t fname_cap;
  const size_t initial_fname_cap = 128;
  pk0304_header_t hdr;
  gar_zstat_t zstat;
  gar_off_t off;
  int result;
  if (_setjmp (env)) {
    _gar_free(fname);
    longjmp(_env, 1);
  }
  fname = _gar_malloc(initial_fname_cap, env);
  fname_cap = initial_fname_cap;
  off = 0;
  result = 0;
  for (;;) {
    gar_gfile_seek(&G->gf, off, env);
    if (!read_pk0304_header(&G->gf, &hdr, env)) break;
    if (fname_cap < hdr.fname_len+1U) {
      fname = _gar_realloc(fname, hdr.fname_len+1U, env);
      fname_cap = hdr.fname_len+1U;
    }
    if (gar_gfile_read(&G->gf, fname, hdr.fname_len, env) < hdr.fname_len) {
      break;
    }
    fname[hdr.fname_len] = 0;
    zstat.fstat.fname = fname;
    zstat.fstat.fsize = hdr.uncomp_size;
    zstat.comp_method = hdr.comp_method;
    zstat.data_off = off + 30 + hdr.fname_len + hdr.extra_len;
    zstat.data_len = hdr.comp_size;
    result = (*fn)(&zstat.fstat, ud, env);
    if (result != 0) break;
    off += 30 + hdr.fname_len + hdr.extra_len + hdr.comp_size;
  }
  _gar_free(fname);
  return result;
}

canonical_original_code:
int gar_enum(struct gar *G, int (*fn)(struct gar_fstat *fstat, void *ud, struct __jmp_buf_tag env[1]), void *ud, struct __jmp_buf_tag _env[1]) {
  struct __jmp_buf_tag env[1];
  char *fname = ((void *)0);
  unsigned long fname_cap;
  unsigned long initial_fname_cap = 128;
  struct pk0304_header hdr;
  struct gar_zstat zstat;
  unsigned long long off;
  int result;
  if (_setjmp (env)) {
    _gar_free(fname);
    longjmp(_env, 1);
  }
  fname = _gar_malloc(initial_fname_cap, env);
  fname_cap = initial_fname_cap;
  off = 0;
  result = 0;
  for (;;) {
    gar_gfile_seek(&G->gf, off, env);
    if (!read_pk0304_header(&G->gf, &hdr, env)) break;
    if (fname_cap < hdr.fname_len+1U) {
      fname = _gar_realloc(fname, hdr.fname_len+1U, env);
      fname_cap = hdr.fname_len+1U;
    }
    if (gar_gfile_read(&G->gf, fname, hdr.fname_len, env) < hdr.fname_len) {
      break;
    }
    fname[hdr.fname_len] = 0;
    zstat.fstat.fname = fname;
    zstat.fstat.fsize = hdr.uncomp_size;
    zstat.comp_method = hdr.comp_method;
    zstat.data_off = off + 30 + hdr.fname_len + hdr.extra_len;
    zstat.data_len = hdr.comp_size;
    result = (*fn)(&zstat.fstat, ud, env);
    if (result != 0) break;
    off += 30 + hdr.fname_len + hdr.extra_len + hdr.comp_size;
  }
  _gar_free(fname);
  return result;
}

variable_types: int (*)(struct gar_fstat { char *fname; unsigned long fsize; } *fstat, void *ud, struct __jmp_buf_tag env[1])

variable_types:
fname
char *

variable_types: struct pk0304_header { unsigned long sig; unsigned short need_ver; unsigned short flags; unsigned short comp_method; unsigned short last_mod_time; unsigned short last_mod_date; unsigned long crc32; unsigned long comp_size; unsigned long uncomp_size; unsigned short fname_len; unsigned short extra_len; }

variable_types: struct gar_zstat { struct gar_fstat { char *fname; unsigned long fsize; } fstat; unsigned short comp_method; unsigned long long data_off; unsigned long long data_len; }

variable_types:
off
unsigned long long

variable_types: result int


decompiled_code:
__int64 __fastcall gar_zstat(__int64 a1, __int64 a2, __int64 a3, struct __jmp_buf_tag *a4)
{
__int64 v5[3]; // [rsp+0h] [rbp-18h] BYREF

v5[0] = a2;
v5[1] = a3;
return gar_enum(a1, (__int64 (__fastcall *)(__int64 *, __int64, __jmp_buf_tag *))on_find, (__int64)v5, a4);
}

original_code:
static int gar_zstat(gar_t *G, const char *fname, gar_zstat_t *zstat,
                     jmp_buf env) {
  find_ud_t fud;
  fud.fname = fname;
  fud.zstat = zstat;
  return gar_enum(G, &on_find, &fud, env);
}

canonical_original_code:
static int gar_zstat(struct gar *G, char *fname, struct gar_zstat *zstat, struct __jmp_buf_tag env[1]) {
  struct find_ud fud;
  fud.fname = fname;
  fud.zstat = zstat;
  return gar_enum(G, &on_find, &fud, env);
}

variable_types:
env
struct __jmp_buf_tag [1]

variable_types: struct find_ud { char *fname; struct gar_zstat { struct gar_fstat { char *fname; unsigned long fsize; } fstat; unsigned short comp_method; unsigned long long data_off; unsigned long long data_len; } *zstat; }


decompiled_code:
__int64 __fastcall gar_stat(__int64 a1, __int64 a2, __m128i *a3, struct __jmp_buf_tag *a4)
{
__int64 result; // rax
__m128i v6[4]; // [rsp+0h] [rbp-48h] BYREF

result = gar_zstat(a1, a2, (__int64)v6, a4);
if ( (_DWORD)result )
{
*a3 = _mm_loadu_si128(v6);
a3->m128i_i64[0] = a2;
result = 1LL;
}
else
{
a3->m128i_i64[0] = 0LL;
a3->m128i_i64[1] = 0LL;
}
return result;
}

original_code:
int gar_stat(gar_t *G, const char *fname, gar_fstat_t *fstat, jmp_buf env) {
  gar_zstat_t zstat;
  if (gar_zstat(G, fname, &zstat, env)) {
    *fstat = zstat.fstat;
    fstat->fname = fname;
    return 1;
  } else {
    fstat->fname = ((void *)0);
    fstat->fsize = 0;
    return 0;
  }
}

canonical_original_code:
int gar_stat(struct gar *G, char *fname, struct gar_fstat *fstat, struct __jmp_buf_tag env[1]) {
  struct gar_zstat zstat;
  if (gar_zstat(G, fname, &zstat, env)) {
    *fstat = zstat.fstat;
    fstat->fname = fname;
    return 1;
  } else {
    fstat->fname = ((void *)0);
    fstat->fsize = 0;
    return 0;
  }
}

user_defined_types: struct gar_fstat { char *fname; unsigned long fsize; }


decompiled_code:
__int64 __fastcall gar_read(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
result = gar_gfile_read(a1, a2, a3, a4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0LL;
if ( a1 )
result = gar_gfile_read(a1, a2, a3, a4);
return result;
}

original_code:
size_t gar_read(gar_fdata_t *fd, void *ptr, size_t n, jmp_buf env) {
  if (fd != ((void *)0)) {
    return gar_gfile_read(&fd->gf, ptr, n, env);
  } else {
    return 0;
  }
}

canonical_original_code:
unsigned long gar_read(struct gar_fdata *fd, void *ptr, unsigned long n, struct __jmp_buf_tag env[1]) {
  if (fd != ((void *)0)) {
    return gar_gfile_read(&fd->gf, ptr, n, env);
  } else {
    return 0;
  }
}

variable_types: struct gar_fdata { struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); } gf; } *

variable_types:
ptr
void *

user_defined_types: struct gar_fdata { struct gar_gfile gf; }


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

if ( a1 )
{
gar_gfile_close();
result = gar_free(a1);
}
return result;
}

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

if ( a1 )
{
gar_gfile_close();
result = gar_free(a1);
}
return result;
}

original_code:
void gar_close(gar_fdata_t *fd) {
  if (fd != ((void *)0)) {
    gar_gfile_close(&fd->gf);
    _gar_free(fd);
  }
}

canonical_original_code:
void gar_close(struct gar_fdata *fd) {
  if (fd != ((void *)0)) {
    gar_gfile_close(&fd->gf);
    _gar_free(fd);
  }
}


decompiled_code:
__int64 __fastcall open_fdata(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3)
{
__int64 v5; // [rsp+28h] [rbp-E0h]
jmp_buf env; // [rsp+30h] [rbp-D8h] BYREF

if ( setjmp(env) )
{
gar_close(0LL);
longjmp(a3, 1);
}
v5 = gar_malloc(40LL, env);
gar_gfile_null(v5);
gar_gfile_dup(a1, v5, env);
gar_gfile_open_part(v5, *(_QWORD *)(a2 + 24), *(_QWORD *)(a2 + 32), env);
if ( *(_WORD *)(a2 + 16) == 8 )
gar_inflate(v5, env);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3)
{
__int64 v5; // [rsp+28h] [rbp-E0h]
jmp_buf env; // [rsp+30h] [rbp-D8h] BYREF

if ( setjmp(env) )
{
func9(0LL);
longjmp(a3, 1);
}
v5 = gar_malloc(40LL, env);
gar_gfile_null(v5);
gar_gfile_dup(a1, v5, env);
gar_gfile_open_part(v5, *(_QWORD *)(a2 + 24), *(_QWORD *)(a2 + 32), env);
if ( *(_WORD *)(a2 + 16) == 8 )
gar_inflate(v5, env);
return v5;
}

original_code:
static gar_fdata_t *open_fdata(gar_t *G, const gar_zstat_t *zstat,
                               jmp_buf _env) {
  jmp_buf env;
  gar_fdata_t *volatile fd = ((void *)0);
  if (_setjmp (env)) {
    gar_close(fd);
    longjmp(_env, 1);
  }
  fd = _gar_malloc(sizeof(gar_fdata_t), env);
  gar_gfile_null(&fd->gf);
  gar_gfile_dup(&G->gf, &fd->gf, env);
  gar_gfile_open_part(&fd->gf, zstat->data_off, zstat->data_len, env);
  if (zstat->comp_method == 8) {
    gar_inflate(&fd->gf, env);
  }
  return fd;
}

canonical_original_code:
static struct gar_fdata *open_fdata(struct gar *G, struct gar_zstat *zstat, struct __jmp_buf_tag _env[1]) {
  struct __jmp_buf_tag env[1];
  struct gar_fdata *fd = ((void *)0);
  if (_setjmp (env)) {
    gar_close(fd);
    longjmp(_env, 1);
  }
  fd = _gar_malloc(sizeof(gar_fdata_t), env);
  gar_gfile_null(&fd->gf);
  gar_gfile_dup(&G->gf, &fd->gf, env);
  gar_gfile_open_part(&fd->gf, zstat->data_off, zstat->data_len, env);
  if (zstat->comp_method == 8) {
    gar_inflate(&fd->gf, env);
  }
  return fd;
}

variable_types:
G
struct gar { struct gar_gfile gf; } *

variable_types: struct gar_zstat { struct gar_fstat { char *fname; unsigned long fsize; } fstat; unsigned short comp_method; unsigned long long data_off; unsigned long long data_len; } *

return_type: struct gar_fdata { struct gar_gfile { void *ud; unsigned long (*read)(void *ud, void *ptr, unsigned long n, struct __jmp_buf_tag { long __jmpbuf[8]; int __mask_was_saved; struct __sigset_t { unsigned long __val[-1]; } __saved_mask; } env[1]); void (*seek)(void *ud, unsigned long long off, struct __jmp_buf_tag env[1]); void (*dup)(void *ud, struct gar_gfile *dst, struct __jmp_buf_tag env[1]); void (*close)(void *ud); } gf; } *


decompiled_code:
__int64 __fastcall gar_open(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3)
{
int v4; // edx
__int64 result; // rax
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = gar_zstat(a1, a2, (__int64)v6, a3);
result = 0LL;
if ( v4 )
result = open_fdata(a1, (__int64)v6, a3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, struct __jmp_buf_tag *a3)
{
int v4; // edx
__int64 result; // rax
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = func6(a1, a2, (__int64)v6, a3);
result = 0LL;
if ( v4 )
result = func10(a1, (__int64)v6, a3);
return result;
}

original_code:
gar_fdata_t *gar_open(gar_t *G, const char *fname, jmp_buf env) {
  gar_zstat_t zstat;
  if (gar_zstat(G, fname, &zstat, env)) {
    return open_fdata(G, &zstat, env);
  } else {
    return ((void *)0);
  }
}

canonical_original_code:
struct gar_fdata *gar_open(struct gar *G, char *fname, struct __jmp_buf_tag env[1]) {
  struct gar_zstat zstat;
  if (gar_zstat(G, fname, &zstat, env)) {
    return open_fdata(G, &zstat, env);
  } else {
    return ((void *)0);
  }
}


decompiled_code:
__int64 __fastcall on_list(const char **a1)
{
puts(*a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char **a1)
{
puts(*a1);
return 0LL;
}

original_code:
static int on_list(const gar_fstat_t *fstat, void *ud, jmp_buf env) {
  ((void)ud);
  ((void)env);
  printf("%s\n", fstat->fname);
  return 0;
}

canonical_original_code:
static int on_list(struct gar_fstat *fstat, void *ud, struct __jmp_buf_tag env[1]) {
  ((void)ud);
  ((void)env);
  printf("%s\n", fstat->fname);
  return 0;
}

variable_types: struct gar_fstat { char *fname; unsigned long fsize; } *


main

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

if ( fork() )
{
v5 = getpid();
printf("parent pid=%d\n", v5);
}
else
{
v3 = getpid();
printf("child pid=%d\n", v3);
}
sleep(0xB4u);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 if (fork() == 0) {
  printf("child pid=%d\n", (int)getpid());
  sleep(180);
 }
 else {
  printf("parent pid=%d\n", (int)getpid());
  sleep(180);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


repo: yoheie/isftoasc

decompiled_code:
__int64 init_isf_header()
{
char *v0; // rbx
__int64 result; // rax
char *v2; // rbp

v0 = strtok(&isf_header_str, " ");
if ( v0 )
{
do
{
v2 = strtok(0LL, ";");
if ( !strcmp(v0, "BYT_NR") )
{
isf_header = atoi(v2);
}
else if ( !strcmp(v0, "BIT_NR") )
{
dword_4040C4 = atoi(v2);
}
else if ( !strcmp(v0, "ENCDG") )
{
s1 = v2;
}
else if ( !strcmp(v0, "BN_FMT") )
{
off_4040D0 = v2;
}
else if ( !strcmp(v0, "BYT_OR") )
{
off_4040D8 = v2;
}
else if ( !strcmp(v0, "NR_PT") )
{
qword_4040E0 = atol(v2);
}
else if ( !strcmp(v0, "WFID") )
{
off_4040E8 = v2;
}
else if ( !strcmp(v0, "PT_FMT") )
{
off_4040F0 = v2;
}
else if ( !strcmp(v0, "XINCR") )
{
qword_4040F8 = atof(v2);
}
else if ( !strcmp(v0, "PT_OFF") )
{
qword_404100 = atol(v2);
}
else if ( !strcmp(v0, "XZERO") )
{
qword_404108 = atof(v2);
}
else if ( !strcmp(v0, "XUNIT") )
{
off_404110 = v2;
}
else if ( !strcmp(v0, "YMULT") )
{
qword_404118 = atof(v2);
}
else if ( !strcmp(v0, "YZERO") )
{
qword_404120 = atof(v2);
}
else if ( !strcmp(v0, "YOFF") )
{
qword_404128 = atof(v2);
}
else if ( !strcmp(v0, "YUNIT") )
{
off_404130 = v2;
}
else
{
fprintf(stderr, " Warning : Unsupported Header %s\n", v0);
}
printf("# %s %s\n", v0, v2);
v0 = strtok(0LL, " ");
}
while ( v0 );
result = 0LL;
}
else
{
fwrite("Error : No headers found\n", 1uLL, 0x19uLL, stderr);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
char *v0; // rbx
__int64 result; // rax
char *v2; // rbp

v0 = strtok(&isf_header_str, " ");
if ( v0 )
{
do
{
v2 = strtok(0LL, ";");
if ( !strcmp(v0, "BYT_NR") )
{
isf_header = atoi(v2);
}
else if ( !strcmp(v0, "BIT_NR") )
{
dword_4040C4 = atoi(v2);
}
else if ( !strcmp(v0, "ENCDG") )
{
s1 = v2;
}
else if ( !strcmp(v0, "BN_FMT") )
{
off_4040D0 = v2;
}
else if ( !strcmp(v0, "BYT_OR") )
{
off_4040D8 = v2;
}
else if ( !strcmp(v0, "NR_PT") )
{
qword_4040E0 = atol(v2);
}
else if ( !strcmp(v0, "WFID") )
{
off_4040E8 = v2;
}
else if ( !strcmp(v0, "PT_FMT") )
{
off_4040F0 = v2;
}
else if ( !strcmp(v0, "XINCR") )
{
qword_4040F8 = atof(v2);
}
else if ( !strcmp(v0, "PT_OFF") )
{
qword_404100 = atol(v2);
}
else if ( !strcmp(v0, "XZERO") )
{
qword_404108 = atof(v2);
}
else if ( !strcmp(v0, "XUNIT") )
{
off_404110 = v2;
}
else if ( !strcmp(v0, "YMULT") )
{
qword_404118 = atof(v2);
}
else if ( !strcmp(v0, "YZERO") )
{
qword_404120 = atof(v2);
}
else if ( !strcmp(v0, "YOFF") )
{
qword_404128 = atof(v2);
}
else if ( !strcmp(v0, "YUNIT") )
{
off_404130 = v2;
}
else
{
fprintf(stderr, " Warning : Unsupported Header %s\n", v0);
}
printf("# %s %s\n", v0, v2);
v0 = strtok(0LL, " ");
}
while ( v0 );
result = 0LL;
}
else
{
fwrite("Error : No headers found\n", 1uLL, 0x19uLL, stderr);
result = 1LL;
}
return result;
}

original_code:
int init_isf_header(void)
{
 char *header_name;
 char *header_value;
 if ((header_name = strtok(isf_header_str, " ")) == ((void *)0)) {
  fprintf(stderr, "Error : No headers found\n");
  return 1;
 }
 do {
  header_value = strtok(((void *)0), ";");
  if (strcmp(header_name, "BYT_NR") == 0) {
   isf_header.byt_nr = atoi(header_value);
  }
  else if (strcmp(header_name, "BIT_NR") == 0) {
   isf_header.bit_nr = atoi(header_value);
  }
  else if (strcmp(header_name, "ENCDG") == 0) {
   isf_header.encdg = header_value;
  }
  else if (strcmp(header_name, "BN_FMT") == 0) {
   isf_header.bn_fmt = header_value;
  }
  else if (strcmp(header_name, "BYT_OR") == 0) {
   isf_header.byt_or = header_value;
  }
  else if (strcmp(header_name, "NR_PT") == 0) {
   isf_header.nr_pt = atol(header_value);
  }
  else if (strcmp(header_name, "WFID") == 0) {
   isf_header.wfid = header_value;
  }
  else if (strcmp(header_name, "PT_FMT") == 0) {
   isf_header.pt_fmt = header_value;
  }
  else if (strcmp(header_name, "XINCR") == 0) {
   isf_header.xincr = atof(header_value);
  }
  else if (strcmp(header_name, "PT_OFF") == 0) {
   isf_header.pt_off = atol(header_value);
  }
  else if (strcmp(header_name, "XZERO") == 0) {
   isf_header.xzero = atof(header_value);
  }
  else if (strcmp(header_name, "XUNIT") == 0) {
   isf_header.xunit = header_value;
  }
  else if (strcmp(header_name, "YMULT") == 0) {
   isf_header.ymult = atof(header_value);
  }
  else if (strcmp(header_name, "YZERO") == 0) {
   isf_header.yzero = atof(header_value);
  }
  else if (strcmp(header_name, "YOFF") == 0) {
   isf_header.yoff = atof(header_value);
  }
  else if (strcmp(header_name, "YUNIT") == 0) {
   isf_header.yunit = header_value;
  }
  else {
   fprintf(stderr, " Warning : Unsupported Header %s\n", header_name);
  }
  printf("# %s %s\n", header_name, header_value);
 } while ((header_name = strtok(((void *)0), " ")) != ((void *)0));
 return 0;
}

canonical_original_code:
int init_isf_header()
{
 char *header_name;
 char *header_value;
 if ((header_name = strtok(isf_header_str, " ")) == ((void *)0)) {
  fprintf(stderr, "Error : No headers found\n");
  return 1;
 }
 do {
  header_value = strtok(((void *)0), ";");
  if (strcmp(header_name, "BYT_NR") == 0) {
   isf_header.byt_nr = atoi(header_value);
  }
  else if (strcmp(header_name, "BIT_NR") == 0) {
   isf_header.bit_nr = atoi(header_value);
  }
  else if (strcmp(header_name, "ENCDG") == 0) {
   isf_header.encdg = header_value;
  }
  else if (strcmp(header_name, "BN_FMT") == 0) {
   isf_header.bn_fmt = header_value;
  }
  else if (strcmp(header_name, "BYT_OR") == 0) {
   isf_header.byt_or = header_value;
  }
  else if (strcmp(header_name, "NR_PT") == 0) {
   isf_header.nr_pt = atol(header_value);
  }
  else if (strcmp(header_name, "WFID") == 0) {
   isf_header.wfid = header_value;
  }
  else if (strcmp(header_name, "PT_FMT") == 0) {
   isf_header.pt_fmt = header_value;
  }
  else if (strcmp(header_name, "XINCR") == 0) {
   isf_header.xincr = atof(header_value);
  }
  else if (strcmp(header_name, "PT_OFF") == 0) {
   isf_header.pt_off = atol(header_value);
  }
  else if (strcmp(header_name, "XZERO") == 0) {
   isf_header.xzero = atof(header_value);
  }
  else if (strcmp(header_name, "XUNIT") == 0) {
   isf_header.xunit = header_value;
  }
  else if (strcmp(header_name, "YMULT") == 0) {
   isf_header.ymult = atof(header_value);
  }
  else if (strcmp(header_name, "YZERO") == 0) {
   isf_header.yzero = atof(header_value);
  }
  else if (strcmp(header_name, "YOFF") == 0) {
   isf_header.yoff = atof(header_value);
  }
  else if (strcmp(header_name, "YUNIT") == 0) {
   isf_header.yunit = header_value;
  }
  else {
   fprintf(stderr, " Warning : Unsupported Header %s\n", header_name);
  }
  printf("# %s %s\n", header_name, header_value);
 } while ((header_name = strtok(((void *)0), " ")) != ((void *)0));
 return 0;
}


decompiled_code:
_BOOL8 check_isf_header()
{
bool v0; // zf
_BOOL8 result; // rax

if ( (unsigned int)(isf_header - 1) > 1 )
{
fprintf(stderr, " Error : Wrong BYT_NR (%d)\n", (unsigned int)isf_header);
result = 1LL;
}
else if ( ((dword_4040C4 - 8) & 0xFFFFFFF7) != 0 )
{
fprintf(stderr, " Error : Wrong BIT_NR (%d)\n", (unsigned int)dword_4040C4);
result = 1LL;
}
else if ( !strcmp(s1, "ASC") || !strcmp(s1, "BIN") || !strcmp(s1, "BINARY") )
{
if ( !strcmp((const char *)off_4040D0, "RI") || !strcmp((const char *)off_4040D0, "RP") )
{
if ( !strcmp((const char *)off_4040D8, "LSB") || !strcmp((const char *)off_4040D8, "MSB") )
{
if ( !strcmp((const char *)off_4040F0, "ENV") )
{
fwrite(" Error : Sorry, PT_FMT = ENV not supported\n", 1uLL, 0x2BuLL, stderr);
result = 1LL;
}
else
{
v0 = memcmp(off_4040F0, "Y", 2uLL) == 0;
result = !v0;
if ( !v0 )
{
fprintf(stderr, " Error : Wrong PT_FMT (%s)\n", (const char *)off_4040F0);
result = 1LL;
}
}
}
else
{
fprintf(stderr, " Error : Wrong BYT_OR (%s)\n", (const char *)off_4040D8);
result = 1LL;
}
}
else
{
fprintf(stderr, " Error : Wrong BN_FMT (%s)\n", (const char *)off_4040D0);
result = 1LL;
}
}
else
{
fprintf(stderr, " Error : Wrong ENCDG (%s)\n", s1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 func8()
{
bool v0; // zf
_BOOL8 result; // rax

if ( (unsigned int)(isf_header - 1) > 1 )
{
fprintf(stderr, " Error : Wrong BYT_NR (%d)\n", (unsigned int)isf_header);
result = 1LL;
}
else if ( ((dword_4040C4 - 8) & 0xFFFFFFF7) != 0 )
{
fprintf(stderr, " Error : Wrong BIT_NR (%d)\n", (unsigned int)dword_4040C4);
result = 1LL;
}
else if ( !strcmp(s1, "ASC") || !strcmp(s1, "BIN") || !strcmp(s1, "BINARY") )
{
if ( !strcmp((const char *)off_4040D0, "RI") || !strcmp((const char *)off_4040D0, "RP") )
{
if ( !strcmp((const char *)off_4040D8, "LSB") || !strcmp((const char *)off_4040D8, "MSB") )
{
if ( !strcmp((const char *)off_4040F0, "ENV") )
{
fwrite(" Error : Sorry, PT_FMT = ENV not supported\n", 1uLL, 0x2BuLL, stderr);
result = 1LL;
}
else
{
v0 = memcmp(off_4040F0, "Y", 2uLL) == 0;
result = !v0;
if ( !v0 )
{
fprintf(stderr, " Error : Wrong PT_FMT (%s)\n", (const char *)off_4040F0);
result = 1LL;
}
}
}
else
{
fprintf(stderr, " Error : Wrong BYT_OR (%s)\n", (const char *)off_4040D8);
result = 1LL;
}
}
else
{
fprintf(stderr, " Error : Wrong BN_FMT (%s)\n", (const char *)off_4040D0);
result = 1LL;
}
}
else
{
fprintf(stderr, " Error : Wrong ENCDG (%s)\n", s1);
result = 1LL;
}
return result;
}

original_code:
int check_isf_header(void)
{
 if ((isf_header.byt_nr != 1) && (isf_header.byt_nr != 2)) {
  fprintf(stderr, " Error : Wrong BYT_NR (%d)\n", isf_header.byt_nr);
  return 1;
 }
 if ((isf_header.bit_nr != 8) && (isf_header.bit_nr != 16)) {
  fprintf(stderr, " Error : Wrong BIT_NR (%d)\n", isf_header.bit_nr);
  return 1;
 }
 if ((strcmp(isf_header.encdg,"ASC") != 0) && (strcmp(isf_header.encdg,"BIN") != 0) && (strcmp(isf_header.encdg,"BINARY") != 0)) {
  fprintf(stderr, " Error : Wrong ENCDG (%s)\n", isf_header.encdg);
  return 1;
 }
 if ((strcmp(isf_header.bn_fmt,"RI") != 0) && (strcmp(isf_header.bn_fmt,"RP") != 0)) {
  fprintf(stderr, " Error : Wrong BN_FMT (%s)\n", isf_header.bn_fmt);
  return 1;
 }
 if ((strcmp(isf_header.byt_or,"LSB") != 0) && (strcmp(isf_header.byt_or,"MSB") != 0)) {
  fprintf(stderr, " Error : Wrong BYT_OR (%s)\n", isf_header.byt_or);
  return 1;
 }
 if ((strcmp(isf_header.pt_fmt,"ENV") != 0) && (strcmp(isf_header.pt_fmt,"Y") != 0)) {
  fprintf(stderr, " Error : Wrong PT_FMT (%s)\n", isf_header.pt_fmt);
  return 1;
 }
 else if (strcmp(isf_header.pt_fmt,"ENV") == 0) {
  fprintf(stderr, " Error : Sorry, PT_FMT = ENV not supported\n");
  return 1;
 }
 return 0;
}

canonical_original_code:
int check_isf_header()
{
 if ((isf_header.byt_nr != 1) && (isf_header.byt_nr != 2)) {
  fprintf(stderr, " Error : Wrong BYT_NR (%d)\n", isf_header.byt_nr);
  return 1;
 }
 if ((isf_header.bit_nr != 8) && (isf_header.bit_nr != 16)) {
  fprintf(stderr, " Error : Wrong BIT_NR (%d)\n", isf_header.bit_nr);
  return 1;
 }
 if ((strcmp(isf_header.encdg,"ASC") != 0) && (strcmp(isf_header.encdg,"BIN") != 0) && (strcmp(isf_header.encdg,"BINARY") != 0)) {
  fprintf(stderr, " Error : Wrong ENCDG (%s)\n", isf_header.encdg);
  return 1;
 }
 if ((strcmp(isf_header.bn_fmt,"RI") != 0) && (strcmp(isf_header.bn_fmt,"RP") != 0)) {
  fprintf(stderr, " Error : Wrong BN_FMT (%s)\n", isf_header.bn_fmt);
  return 1;
 }
 if ((strcmp(isf_header.byt_or,"LSB") != 0) && (strcmp(isf_header.byt_or,"MSB") != 0)) {
  fprintf(stderr, " Error : Wrong BYT_OR (%s)\n", isf_header.byt_or);
  return 1;
 }
 if ((strcmp(isf_header.pt_fmt,"ENV") != 0) && (strcmp(isf_header.pt_fmt,"Y") != 0)) {
  fprintf(stderr, " Error : Wrong PT_FMT (%s)\n", isf_header.pt_fmt);
  return 1;
 }
 else if (strcmp(isf_header.pt_fmt,"ENV") == 0) {
  fprintf(stderr, " Error : Sorry, PT_FMT = ENV not supported\n");
  return 1;
 }
 return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
fwrite("Hello World!\n", 1uLL, 0xDuLL, stderr);
return 0;
}

original_code:
int main(){
 fprintf(stderr, "Hello World!\n");
 return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 3 )
{
fprintf(stderr, "usage %s hostname port-client port-server\n", *argv);
exit(1);
}
setHostname(argv[1]);
setPort_Client(argv[2]);
setPort_Server(argv[3]);
pthread_create(&client_thread, 0LL, (void *(*)(void *))&client, 0LL);
pthread_create(&server_handler_thread, 0LL, (void *(*)(void *))&server_handler, 0LL);
pthread_join(server_handler_thread, 0LL);
pthread_join(client_thread, 0LL);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 if (argc < 4) {
    fprintf(stderr, "usage %s hostname port-client port-server\n", argv[0]);
    exit(1);
 }
 setHostname(argv[1]);
 setPort_Client(argv[2]);
 setPort_Server(argv[3]);
 pthread_create(&client_thread, ((void *)0), &client, ((void *)0));
 pthread_create(&server_handler_thread, ((void *)0), &server_handler, ((void *)0));
 pthread_join(server_handler_thread, ((void *)0));
 pthread_join(client_thread, ((void *)0));
 return 0;
}


closeConnection

decompiled_code:
int closeConnection()
{
close(socket_sockfd);
puts("Server-Handler: socket closed");
return fflush(stdout);
}

canonical_decompiled_code:
int func0()
{
close(socket_sockfd);
puts("Server-Handler: socket closed");
return fflush(stdout);
}

original_code:
static void closeConnection() {
 close(socket_sockfd);
 printf("Server-Handler: socket closed\n");fflush(stdout);
}


createSocket

decompiled_code:
int createSocket()
{
int v0; // eax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v0 = socket(2, 1, 0);
socket_sockfd = v0;
if ( v0 < 0 )
{
perror("Server-Handler: ERROR, can't create socket");
exit(1);
}
setsockopt(v0, 1, 2, optval, 4u);
puts("Server-Handler: socket created");
return fflush(stdout);
}

canonical_decompiled_code:
int func1()
{
int v0; // eax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
v0 = socket(2, 1, 0);
socket_sockfd = v0;
if ( v0 < 0 )
{
perror("Server-Handler: ERROR, can't create socket");
exit(1);
}
setsockopt(v0, 1, 2, optval, 4u);
puts("Server-Handler: socket created");
return fflush(stdout);
}

original_code:
static void createSocket() {
 int sock_opt = 1;
 socket_sockfd = socket(2, SOCK_STREAM, 0);
 if(socket_sockfd < 0) {
  perror("Server-Handler: ERROR, can't create socket");
  exit(1);
 }
 setsockopt(socket_sockfd, 1, 2, &sock_opt, sizeof(sock_opt));
 printf("Server-Handler: socket created\n");fflush(stdout);
}


prepareConnect

decompiled_code:
__int16 prepareConnect()
{
__int16 result; // ax

*(_QWORD *)&serv_addr.sa_family = 2LL;
*(_QWORD *)&serv_addr.sa_data[6] = 0LL;
result = _bswap_16(portno);
*(_WORD *)serv_addr.sa_data = result;
return result;
}

canonical_decompiled_code:
__int16 func3()
{
__int16 result; // ax

*(_QWORD *)&serv_addr.sa_family = 2LL;
*(_QWORD *)&serv_addr.sa_data[6] = 0LL;
result = func2(portno);
*(_WORD *)serv_addr.sa_data = result;
return result;
}

original_code:
static void prepareConnect() {
 bzero((char *) &serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 serv_addr.sin_port = htons(portno);
 serv_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
}

canonical_original_code:
static void prepareConnect() {
 bzero((char *) &serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 serv_addr.sin_port = htons(portno);
 serv_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
}


bindSocket

decompiled_code:
int bindSocket()
{
if ( bind(socket_sockfd, &serv_addr, 0x10u) < 0 )
{
perror("Server-Handler: ERROR, can't bind");
exit(1);
}
puts("Server-Handler: socket bound");
return fflush(stdout);
}

canonical_decompiled_code:
int func4()
{
if ( bind(socket_sockfd, &serv_addr, 0x10u) < 0 )
{
perror("Server-Handler: ERROR, can't bind");
exit(1);
}
puts("Server-Handler: socket bound");
return fflush(stdout);
}

original_code:
static void bindSocket() {
 if(bind(socket_sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
  perror("Server-Handler: ERROR, can't bind");
  exit(1);
 }
 printf("Server-Handler: socket bound\n");fflush(stdout);
}

canonical_original_code:
static void bindSocket() {
 if(bind(socket_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  perror("Server-Handler: ERROR, can't bind");
  exit(1);
 }
 printf("Server-Handler: socket bound\n");fflush(stdout);
}

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


listenOnSocket

decompiled_code:
int listenOnSocket()
{
if ( listen(socket_sockfd, 5) < 0 )
{
perror("Server-Handler: ERROR, can't listen");
exit(1);
}
puts("Server-Handler: listening");
return fflush(stdout);
}

canonical_decompiled_code:
int func5()
{
if ( listen(socket_sockfd, 5) < 0 )
{
perror("Server-Handler: ERROR, can't listen");
exit(1);
}
puts("Server-Handler: listening");
return fflush(stdout);
}

original_code:
static void listenOnSocket() {
 if(listen(socket_sockfd, 5) < 0) {
  perror("Server-Handler: ERROR, can't listen");
  exit(1);
 }
 printf("Server-Handler: listening\n");fflush(stdout);
}


decompiled_code:
int __fastcall setPort_Server(const char *a1)
{
portno = atoi(a1);
printf("Client-Handler: Servers reachable on port: %s\n", a1);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func6(const char *a1)
{
portno = atoi(a1);
printf("Client-Handler: Servers reachable on port: %s\n", a1);
return fflush(stdout);
}

original_code:
void setPort_Server(char port[]) {
 portno = atoi(port);
 if(port == 0) {
  perror("Client-Handler: ERROR, no such port");
  exit(1);
 }
 printf("Client-Handler: Servers reachable on port: %s\n", port);fflush(stdout);
}

variable_types:
port
char []


decompiled_code:
void __noreturn server_handler()
{
int v0; // ebx
_DWORD *v1; // rax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

puts("Server-Handler: is running!");
fflush(stdout);
if ( pthread_mutex_init(&num_servers_lock, 0LL) )
{
perror("Server-Handler: ERROR, failed to init mutex");
exit(1);
}
createSocket();
prepareConnect();
bindSocket();
listenOnSocket();
while ( run )
{
client_len = 16;
do
{
v0 = accept(socket_sockfd, &cli_addr, &client_len);
accept_sockfd = v0;
}
while ( *_errno_location() == 4 && v0 < 0 );
if ( v0 < 0 )
{
perror("Server-Handler: ERROR, during accept");
exit(1);
}
puts("Server-Handler: received incoming connection");
fflush(stdout);
if ( pthread_mutex_lock(&num_servers_lock) )
{
perror("Server-Handler: ERROR, can't lock mutex");
exit(1);
}
printf("Server-handler: current amount of running servers = %d, maximum = %d\n", (unsigned int)num_servers, 4LL);
fflush(stdout);
if ( num_servers > 3 )
{
puts("Server-Handler: max amount of running servers already reached, closing connection");
fflush(stdout);
close(accept_sockfd);
}
else
{
puts("Server-Handler: creating new server");
fflush(stdout);
v1 = malloc(4uLL);
*v1 = accept_sockfd;
pthread_create(newthread, 0LL, (void *(*)(void *))&server, v1);
pthread_detach(newthread[0]);
++num_servers;
}
if ( pthread_mutex_unlock(&num_servers_lock) )
{
perror("Server-Handler: ERROR, can't unlock mutex");
exit(1);
}
}
closeConnection();
if ( pthread_mutex_destroy(&num_servers_lock) )
{
perror("Server-Handler: ERROR, failed to init mutex");
exit(1);
}
puts("Client-Handler: is exiting!");
fflush(stdout);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __noreturn func7()
{
int v0; // ebx
_DWORD *v1; // rax
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

puts("Server-Handler: is running!");
fflush(stdout);
if ( pthread_mutex_init(&num_servers_lock, 0LL) )
{
perror("Server-Handler: ERROR, failed to init mutex");
exit(1);
}
func1();
func3();
func4();
func5();
while ( run )
{
client_len = 16;
do
{
v0 = accept(socket_sockfd, &cli_addr, &client_len);
accept_sockfd = v0;
}
while ( *_errno_location() == 4 && v0 < 0 );
if ( v0 < 0 )
{
perror("Server-Handler: ERROR, during accept");
exit(1);
}
puts("Server-Handler: received incoming connection");
fflush(stdout);
if ( pthread_mutex_lock(&num_servers_lock) )
{
perror("Server-Handler: ERROR, can't lock mutex");
exit(1);
}
printf("Server-handler: current amount of running servers = %d, maximum = %d\n", (unsigned int)num_servers, 4LL);
fflush(stdout);
if ( num_servers > 3 )
{
puts("Server-Handler: max amount of running servers already reached, closing connection");
fflush(stdout);
close(accept_sockfd);
}
else
{
puts("Server-Handler: creating new server");
fflush(stdout);
v1 = malloc(4uLL);
*v1 = accept_sockfd;
pthread_create(newthread, 0LL, (void *(*)(void *))&server, v1);
pthread_detach(newthread[0]);
++num_servers;
}
if ( pthread_mutex_unlock(&num_servers_lock) )
{
perror("Server-Handler: ERROR, can't unlock mutex");
exit(1);
}
}
func0();
if ( pthread_mutex_destroy(&num_servers_lock) )
{
perror("Server-Handler: ERROR, failed to init mutex");
exit(1);
}
puts("Client-Handler: is exiting!");
fflush(stdout);
pthread_exit(0LL);
}

original_code:
void* server_handler(void* arg) {
 int* new_sockfd;
 printf("Server-Handler: is running!\n");fflush(stdout);
 if(pthread_mutex_init(&num_servers_lock, ((void *)0)) != 0) {
  perror("Server-Handler: ERROR, failed to init mutex");
  exit(1);
 }
 createSocket();
 prepareConnect();
 bindSocket();
 listenOnSocket();
 while(run) {
  client_len = sizeof(cli_addr);
  do {
   accept_sockfd = accept(socket_sockfd, (struct sockaddr*) &cli_addr, &client_len);
  } while(((*__errno_location ()) == 4) && accept_sockfd < 0);
  if(accept_sockfd < 0) {
   perror("Server-Handler: ERROR, during accept");
   exit(1);
  }
  printf("Server-Handler: received incoming connection\n");fflush(stdout);
  if(pthread_mutex_lock(&num_servers_lock) != 0) {
   perror("Server-Handler: ERROR, can't lock mutex");
   exit(1);
  }
  printf("Server-handler: current amount of running servers = %d, maximum = %d\n", num_servers, 4);fflush(stdout);
  if(num_servers < 4) {
   printf("Server-Handler: creating new server\n");fflush(stdout);
   new_sockfd = (int*)malloc(sizeof(accept_sockfd));
   *new_sockfd = accept_sockfd;
   pthread_t server_thread;
   pthread_create(&server_thread, ((void *)0), &server, (void*)new_sockfd);
   pthread_detach(server_thread);
   num_servers++;
  } else {
   printf("Server-Handler: max amount of running servers already reached, closing connection\n");fflush(stdout);
   close(accept_sockfd);
  }
  if(pthread_mutex_unlock(&num_servers_lock) != 0) {
   perror("Server-Handler: ERROR, can't unlock mutex");
   exit(1);
  }
 }
 closeConnection();
 if(pthread_mutex_destroy(&num_servers_lock) != 0) {
  perror("Server-Handler: ERROR, failed to init mutex");
  exit(1);
 }
 printf("Client-Handler: is exiting!\n");fflush(stdout);
 pthread_exit(((void *)0));
}

canonical_original_code:
void *server_handler(void *arg) {
 int *new_sockfd;
 printf("Server-Handler: is running!\n");fflush(stdout);
 if(pthread_mutex_init(&num_servers_lock, ((void *)0)) != 0) {
  perror("Server-Handler: ERROR, failed to init mutex");
  exit(1);
 }
 createSocket();
 prepareConnect();
 bindSocket();
 listenOnSocket();
 while(run) {
  client_len = sizeof(cli_addr);
  do {
   accept_sockfd = accept(socket_sockfd, (struct sockaddr *) &cli_addr, &client_len);
  } while(((*__errno_location ()) == 4) && accept_sockfd < 0);
  if(accept_sockfd < 0) {
   perror("Server-Handler: ERROR, during accept");
   exit(1);
  }
  printf("Server-Handler: received incoming connection\n");fflush(stdout);
  if(pthread_mutex_lock(&num_servers_lock) != 0) {
   perror("Server-Handler: ERROR, can't lock mutex");
   exit(1);
  }
  printf("Server-handler: current amount of running servers = %d, maximum = %d\n", num_servers, 4);fflush(stdout);
  if(num_servers < 4) {
   printf("Server-Handler: creating new server\n");fflush(stdout);
   new_sockfd = (int *)malloc(sizeof(accept_sockfd));
   *new_sockfd = accept_sockfd;
   unsigned long server_thread;
   pthread_create(&server_thread, ((void *)0), &server, (void *)new_sockfd);
   pthread_detach(server_thread);
   num_servers++;
  } else {
   printf("Server-Handler: max amount of running servers already reached, closing connection\n");fflush(stdout);
   close(accept_sockfd);
  }
  if(pthread_mutex_unlock(&num_servers_lock) != 0) {
   perror("Server-Handler: ERROR, can't unlock mutex");
   exit(1);
  }
 }
 closeConnection();
 if(pthread_mutex_destroy(&num_servers_lock) != 0) {
  perror("Server-Handler: ERROR, failed to init mutex");
  exit(1);
 }
 printf("Client-Handler: is exiting!\n");fflush(stdout);
 pthread_exit(((void *)0));
}

variable_types:
arg
void *

return_type: void *


decompiled_code:
int init_ImageBuffer()
{
int result; // eax

dword_8CA340 = 0;
dword_8CA344 = 0;
dword_8CA348 = 0;
result = pthread_mutex_init(&mutex, 0LL);
if ( result )
{
perror("Buffer: init_ImageBuffer(): ERROR, failed to init mutex");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

dword_8CA340 = 0;
dword_8CA344 = 0;
dword_8CA348 = 0;
result = pthread_mutex_init(&mutex, 0LL);
if ( result )
{
perror("Buffer: init_ImageBuffer(): ERROR, failed to init mutex");
exit(1);
}
return result;
}

original_code:
static void init_ImageBuffer() {
 imageBuffer.newest_image_idx = 0;
 imageBuffer.oldest_image_idx = 0;
 imageBuffer.count = 0;
 if(pthread_mutex_init(&imageBuffer.lock, ((void *)0)) != 0) {
  perror("Buffer: init_ImageBuffer(): ERROR, failed to init mutex");
  exit(1);
 }
}


decompiled_code:
int __fastcall write_Image(void *src)
{
int v1; // ebx
int result; // eax

if ( pthread_mutex_lock(&mutex) )
{
perror("Buffer: write_Image(): ERROR, failed to lock mutex");
exit(1);
}
if ( !init_done )
{
init_ImageBuffer();
init_done = 1;
}
v1 = dword_8CA340;
if ( dword_8CA340 == dword_8CA344 && (unsigned int)dword_8CA348 > 0x27 )
dword_8CA344 = (dword_8CA344 + 1) % 0x28u;
memcpy((char *)&imageBuffer + 230400 * (unsigned int)dword_8CA340, src, (size_t)&unk_38400);
dword_8CA340 = (v1 + 1) % 0x28u;
if ( dword_8CA348 != 40 )
++dword_8CA348;
result = pthread_mutex_unlock(&mutex);
if ( result )
{
perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(void *src)
{
int v1; // ebx
int result; // eax

if ( pthread_mutex_lock(&mutex) )
{
perror("Buffer: write_Image(): ERROR, failed to lock mutex");
exit(1);
}
if ( !init_done )
{
func0();
init_done = 1;
}
v1 = dword_8CA340;
if ( dword_8CA340 == dword_8CA344 && (unsigned int)dword_8CA348 > 0x27 )
dword_8CA344 = (dword_8CA344 + 1) % 0x28u;
memcpy((char *)&imageBuffer + 230400 * (unsigned int)dword_8CA340, src, (size_t)&unk_38400);
dword_8CA340 = (v1 + 1) % 0x28u;
if ( dword_8CA348 != 40 )
++dword_8CA348;
result = pthread_mutex_unlock(&mutex);
if ( result )
{
perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
exit(1);
}
return result;
}

original_code:
void write_Image(rawImage_t* image) {
 if(pthread_mutex_lock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to lock mutex");
  exit(1);
 }
 if(!init_done) {
  init_ImageBuffer();
  init_done = 1;
 }
 if(imageBuffer.newest_image_idx == imageBuffer.oldest_image_idx && imageBuffer.count >= 40) {
  imageBuffer.oldest_image_idx = (imageBuffer.oldest_image_idx + 1) % 40;
 }
 imageBuffer.data[imageBuffer.newest_image_idx] = *image;
 imageBuffer.newest_image_idx = (imageBuffer.newest_image_idx + 1) % 40;
 if(imageBuffer.count != 40) {
  imageBuffer.count++;
 }
 if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
  exit(1);
 }
}

canonical_original_code:
void write_Image(struct rawImage_t *image) {
 if(pthread_mutex_lock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to lock mutex");
  exit(1);
 }
 if(!init_done) {
  init_ImageBuffer();
  init_done = 1;
 }
 if(imageBuffer.newest_image_idx == imageBuffer.oldest_image_idx && imageBuffer.count >= 40) {
  imageBuffer.oldest_image_idx = (imageBuffer.oldest_image_idx + 1) % 40;
 }
 imageBuffer.data[imageBuffer.newest_image_idx] = *image;
 imageBuffer.newest_image_idx = (imageBuffer.newest_image_idx + 1) % 40;
 if(imageBuffer.count != 40) {
  imageBuffer.count++;
 }
 if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
  exit(1);
 }
}

variable_types:
image
struct rawImage_t { char data[-1]; } *


decompiled_code:
int __fastcall read_Image(void *dest, int *a2)
{
int v2; // ebx
int v3; // er12
unsigned int v4; // eax
int result; // eax

if ( pthread_mutex_lock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to lock mutex");
exit(1);
}
if ( init_done )
{
v3 = dword_8CA344;
v4 = *a2;
if ( dword_8CA344 == *a2 )
{
v2 = a2[1];
}
else if ( dword_8CA344 <= v4 )
{
if ( dword_8CA344 < v4 )
v2 = (int)(a2[1] + v4 - dword_8CA344) % 40;
}
else
{
v2 = (int)(a2[1] - (dword_8CA344 - v4)) % 40;
}
if ( !a2[2] && dword_8CA340 == (v2 + dword_8CA344) % 40 )
{
if ( pthread_mutex_unlock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
exit(1);
}
result = 1;
}
else
{
memcpy(dest, (char *)&imageBuffer + 230400 * ((v2 + dword_8CA344) % 40), (size_t)&unk_38400);
*a2 = v3;
*(_QWORD *)(a2 + 1) = (unsigned int)((v2 + 1) % 40);
result = pthread_mutex_unlock(&mutex);
if ( result )
{
perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
exit(1);
}
}
}
else
{
if ( pthread_mutex_unlock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
exit(1);
}
result = 1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *dest, int *a2)
{
int v2; // ebx
int v3; // er12
unsigned int v4; // eax
int result; // eax

if ( pthread_mutex_lock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to lock mutex");
exit(1);
}
if ( init_done )
{
v3 = dword_8CA344;
v4 = *a2;
if ( dword_8CA344 == *a2 )
{
v2 = a2[1];
}
else if ( dword_8CA344 <= v4 )
{
if ( dword_8CA344 < v4 )
v2 = (int)(a2[1] + v4 - dword_8CA344) % 40;
}
else
{
v2 = (int)(a2[1] - (dword_8CA344 - v4)) % 40;
}
if ( !a2[2] && dword_8CA340 == (v2 + dword_8CA344) % 40 )
{
if ( pthread_mutex_unlock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
exit(1);
}
result = 1;
}
else
{
memcpy(dest, (char *)&imageBuffer + 230400 * ((v2 + dword_8CA344) % 40), (size_t)&unk_38400);
*a2 = v3;
*(_QWORD *)(a2 + 1) = (unsigned int)((v2 + 1) % 40);
result = pthread_mutex_unlock(&mutex);
if ( result )
{
perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
exit(1);
}
}
}
else
{
if ( pthread_mutex_unlock(&mutex) )
{
perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
exit(1);
}
result = 1;
}
return result;
}

original_code:
int read_Image(rawImage_t* image, bufferEntry_t* entry) {
 int tmp_oldest_image_idx;
 int tmp_offset;
 int oldest_image_idx_diff;
 int read_idx;
 if(pthread_mutex_lock(&imageBuffer.lock) != 0) {
  perror("Buffer: read_Image(): ERROR, failed to lock mutex");
  exit(1);
 }
 if(!init_done) {
  if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
   perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
   exit(1);
  }
  return 1;
 }
 if(imageBuffer.oldest_image_idx != entry->last_oldest_image_idx) {
  if(imageBuffer.oldest_image_idx > entry->last_oldest_image_idx) {
   oldest_image_idx_diff = imageBuffer.oldest_image_idx - entry->last_oldest_image_idx;
   tmp_offset = (entry->offset - oldest_image_idx_diff) % 40;
  } else if(imageBuffer.oldest_image_idx < entry->last_oldest_image_idx) {
   oldest_image_idx_diff = entry->last_oldest_image_idx - imageBuffer.oldest_image_idx;
   tmp_offset = (entry->offset + oldest_image_idx_diff) % 40;
  }
 } else {
  tmp_offset = entry->offset;
 }
 tmp_oldest_image_idx = imageBuffer.oldest_image_idx;
 read_idx = (tmp_oldest_image_idx + tmp_offset) % 40;
 if((entry->first_run == 0) && (read_idx == imageBuffer.newest_image_idx)) {
  if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
   perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
   exit(1);
  }
  return 1;
 } else {
  *image = imageBuffer.data[read_idx];
  entry->last_oldest_image_idx = tmp_oldest_image_idx;
  entry->offset = (tmp_offset + 1) % 40;
  entry->first_run = 0;
 }
 if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
  exit(1);
 }
 return 0;
}

canonical_original_code:
int read_Image(struct rawImage_t *image, struct bufferEntry_t *entry) {
 int tmp_oldest_image_idx;
 int tmp_offset;
 int oldest_image_idx_diff;
 int read_idx;
 if(pthread_mutex_lock(&imageBuffer.lock) != 0) {
  perror("Buffer: read_Image(): ERROR, failed to lock mutex");
  exit(1);
 }
 if(!init_done) {
  if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
   perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
   exit(1);
  }
  return 1;
 }
 if(imageBuffer.oldest_image_idx != entry->last_oldest_image_idx) {
  if(imageBuffer.oldest_image_idx > entry->last_oldest_image_idx) {
   oldest_image_idx_diff = imageBuffer.oldest_image_idx - entry->last_oldest_image_idx;
   tmp_offset = (entry->offset - oldest_image_idx_diff) % 40;
  } else if(imageBuffer.oldest_image_idx < entry->last_oldest_image_idx) {
   oldest_image_idx_diff = entry->last_oldest_image_idx - imageBuffer.oldest_image_idx;
   tmp_offset = (entry->offset + oldest_image_idx_diff) % 40;
  }
 } else {
  tmp_offset = entry->offset;
 }
 tmp_oldest_image_idx = imageBuffer.oldest_image_idx;
 read_idx = (tmp_oldest_image_idx + tmp_offset) % 40;
 if((entry->first_run == 0) && (read_idx == imageBuffer.newest_image_idx)) {
  if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
   perror("Buffer: read_Image(): ERROR, failed to unlock mutex");
   exit(1);
  }
  return 1;
 } else {
  *image = imageBuffer.data[read_idx];
  entry->last_oldest_image_idx = tmp_oldest_image_idx;
  entry->offset = (tmp_offset + 1) % 40;
  entry->first_run = 0;
 }
 if(pthread_mutex_unlock(&imageBuffer.lock) != 0) {
  perror("Buffer: write_Image(): ERROR, failed to unlock mutex");
  exit(1);
 }
 return 0;
}

variable_types: struct bufferEntry_t { int last_oldest_image_idx; int offset; int first_run; } *


server

decompiled_code:
void __fastcall __noreturn server(int *a1)
{
int i; // eax
int v2; // ebx
int v3; // eax
int v4[3]; // [rsp+4h] [rbp-38424h] BYREF
char v5[230424]; // [rsp+10h] [rbp-38418h] BYREF

puts("Server: is running!");
fflush(stdout);
v4[0] = 0;
v4[1] = 0;
v4[2] = 1;
for ( i = 0; ; i = 1 )
{
if ( !run || i )
{
LABEL_7:
close(*a1);
free(a1);
if ( pthread_mutex_lock(&num_servers_lock) )
{
perror("Server: ERROR, can't lock mutex");
exit(1);
}
--num_servers;
if ( pthread_mutex_unlock(&num_servers_lock) )
{
perror("Server: ERROR, can't unlock mutex");
exit(1);
}
puts("Server: is exiting!");
fflush(stdout);
pthread_exit(0LL);
}
LABEL_3:
while ( 1 )
{
v2 = 230400;
if ( !(unsigned int)read_Image(v5, v4) )
break;
usleep(0x1388u);
}
while ( 1 )
{
v3 = send(*a1, &v5[230400 - v2], v2, 0x4000);
if ( v3 <= 0 )
break;
v2 -= v3;
if ( !v2 )
{
if ( !run )
goto LABEL_7;
goto LABEL_3;
}
}
puts("Server: ERROR, can't write to socket, exciting!");
fflush(stdout);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int *a1)
{
int i; // eax
int v2; // ebx
int v3; // eax
int v4[3]; // [rsp+4h] [rbp-38424h] BYREF
char v5[230424]; // [rsp+10h] [rbp-38418h] BYREF

puts("Server: is running!");
fflush(stdout);
v4[0] = 0;
v4[1] = 0;
v4[2] = 1;
for ( i = 0; ; i = 1 )
{
if ( !run || i )
{
LABEL_7:
close(*a1);
free(a1);
if ( pthread_mutex_lock(&num_servers_lock) )
{
perror("Server: ERROR, can't lock mutex");
exit(1);
}
--num_servers;
if ( pthread_mutex_unlock(&num_servers_lock) )
{
perror("Server: ERROR, can't unlock mutex");
exit(1);
}
puts("Server: is exiting!");
fflush(stdout);
pthread_exit(0LL);
}
LABEL_3:
while ( 1 )
{
v2 = 230400;
if ( !(unsigned int)read_Image(v5, v4) )
break;
usleep(0x1388u);
}
while ( 1 )
{
v3 = send(*a1, &v5[230400 - v2], v2, 0x4000);
if ( v3 <= 0 )
break;
v2 -= v3;
if ( !v2 )
{
if ( !run )
goto LABEL_7;
goto LABEL_3;
}
}
puts("Server: ERROR, can't write to socket, exciting!");
fflush(stdout);
}
}

original_code:
void* server(void* new_sockfd) {
 rawImage_t output_image;
 bufferEntry_t buffer_entry;
 int exit_server = 0;
 int cBytes = 0;
 int res = 0;
 printf("Server: is running!\n");fflush(stdout);
 buffer_entry.last_oldest_image_idx = 0;
 buffer_entry.offset = 0;
 buffer_entry.first_run = 1;
 while(run && !exit_server) {
  do {
   res = read_Image(&output_image, &buffer_entry);
   if(res != 0) {
    usleep(5000);
   }
  } while(res != 0);
  cBytes = (320 * 240 * 3);
  while(cBytes != 0 && !exit_server) {
   res = send(*(int*)new_sockfd, &output_image.data[(320 * 240 * 3) - cBytes], cBytes, MSG_NOSIGNAL);
   if(res <= 0) {
    printf("Server: ERROR, can't write to socket, exciting!\n");fflush(stdout);
    exit_server = 1;
    break;
   }
   cBytes -= res;
  }
 }
 close(*(int*)new_sockfd);
 free(new_sockfd);
 if(pthread_mutex_lock(&num_servers_lock) != 0) {
  perror("Server: ERROR, can't lock mutex");
  exit(1);
 }
 num_servers--;
 if(pthread_mutex_unlock(&num_servers_lock) != 0) {
  perror("Server: ERROR, can't unlock mutex");
  exit(1);
 }
 printf("Server: is exiting!\n");fflush(stdout);
 pthread_exit(((void *)0));
}

canonical_original_code:
void *server(void *new_sockfd) {
 struct rawImage_t output_image;
 struct bufferEntry_t buffer_entry;
 int exit_server = 0;
 int cBytes = 0;
 int res = 0;
 printf("Server: is running!\n");fflush(stdout);
 buffer_entry.last_oldest_image_idx = 0;
 buffer_entry.offset = 0;
 buffer_entry.first_run = 1;
 while(run && !exit_server) {
  do {
   res = read_Image(&output_image, &buffer_entry);
   if(res != 0) {
    usleep(5000);
   }
  } while(res != 0);
  cBytes = (320 * 240 * 3);
  while(cBytes != 0 && !exit_server) {
   res = send(*(int *)new_sockfd, &output_image.data[(320 * 240 * 3) - cBytes], cBytes, MSG_NOSIGNAL);
   if(res <= 0) {
    printf("Server: ERROR, can't write to socket, exciting!\n");fflush(stdout);
    exit_server = 1;
    break;
   }
   cBytes -= res;
  }
 }
 close(*(int *)new_sockfd);
 free(new_sockfd);
 if(pthread_mutex_lock(&num_servers_lock) != 0) {
  perror("Server: ERROR, can't lock mutex");
  exit(1);
 }
 num_servers--;
 if(pthread_mutex_unlock(&num_servers_lock) != 0) {
  perror("Server: ERROR, can't unlock mutex");
  exit(1);
 }
 printf("Server: is exiting!\n");fflush(stdout);
 pthread_exit(((void *)0));
}

variable_types: struct bufferEntry_t { int last_oldest_image_idx; int offset; int first_run; }

variable_types: cBytes int

variable_types: res int


repo: yosefk/checkedthreads

decompiled_code:
__int64 __fastcall ct_dispatch_task(int a1, __int64 a2)
{
return (*(__int64 (__fastcall **)(_QWORD))(16LL * a1 + a2))(*(_QWORD *)(16LL * a1 + a2 + 8));
}

original_code:
void ct_dispatch_task(int index, void* context) {
    const ct_task* tasks = (const ct_task*)context;
    const ct_task* t = tasks + index;
    t->func(t->arg);
}

canonical_original_code:
void ct_dispatch_task(int index, void *context) {
    struct ct_task *tasks = (const ct_task*)context;
    struct ct_task *t = tasks + index;
    t->func(t->arg);
}

variable_types: index int

variable_types:
context
void *

variable_types: struct ct_task { void (*func)(void *arg); void *arg; } *


decompiled_code:
__int64 __fastcall ct_verbose_ind_func(unsigned int a1, __int64 a2)
{
printf("checkedthreads: i=%d\n", a1);
return (*(__int64 (__fastcall **)(_QWORD, _QWORD))a2)(a1, *(_QWORD *)(a2 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2)
{
printf("checkedthreads: i=%d\n", a1);
return (*(__int64 (__fastcall **)(_QWORD, _QWORD))a2)(a1, *(_QWORD *)(a2 + 8));
}

original_code:
void ct_verbose_ind_func(int index, void* context) {
    ct_wrapped_func_context* wc = (ct_wrapped_func_context*)context;
    printf("checkedthreads: i=%d\n", index);
    wc->next_func(index, wc->next_context);
}

canonical_original_code:
void ct_verbose_ind_func(int index, void *context) {
    struct ct_wrapped_func_context *wc = (ct_wrapped_func_context*)context;
    printf("checkedthreads: i=%d\n", index);
    wc->next_func(index, wc->next_context);
}

variable_types: struct ct_wrapped_func_context { void (*next_func)(int ind, void *context); void *next_context; } *


decompiled_code:
char *__fastcall ct_getenv(const char **a1, const char *a2, char *a3)
{
const char **v4; // rbx
const char *v5; // rdi
char *result; // rax

if ( a1 && (v4 = a1, (v5 = *a1) != 0LL) )
{
while ( strcmp(v5, a2) )
{
v4 += 2;
v5 = *v4;
if ( !*v4 )
goto LABEL_5;
}
result = (char *)v4[1];
}
else
{
LABEL_5:
result = getenv(a2);
if ( !result )
result = a3;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(const char **a1, const char *a2, char *a3)
{
const char **v4; // rbx
const char *v5; // rdi
char *result; // rax

if ( a1 && (v4 = a1, (v5 = *a1) != 0LL) )
{
while ( strcmp(v5, a2) )
{
v4 += 2;
v5 = *v4;
if ( !*v4 )
goto LABEL_5;
}
result = (char *)v4[1];
}
else
{
LABEL_5:
result = getenv(a2);
if ( !result )
result = a3;
}
return result;
}

original_code:
const char* ct_getenv(const ct_env_var* env, const char* name, const char* default_value) {
    int i=0;
    const char* got=0;
    if(env) {
        while(env[i].name) {
            if(strcmp(env[i].name, name) == 0) {
                return env[i].value;
            }
            ++i;
        }
    }
    got = getenv(name);
    return got ? got : default_value;
}

canonical_original_code:
char *ct_getenv(struct ct_env_var *env, char *name, char *default_value) {
    int i = 0;
    char *got = 0;
    if(env) {
        while(env[i].name) {
            if(strcmp(env[i].name, name) == 0) {
                return env[i].value;
            }
            ++i;
        }
    }
    got = getenv(name);
    return got ? got : default_value;
}

variable_types: struct ct_env_var { char *name; char *value; } *

variable_types:
name
char *

variable_types: i int

variable_types:
got
char *

return_type: char *


decompiled_code:
const char **__fastcall ct_sched(char *s2)
{
const char **v1; // rbx
void **v2; // rbp

v1 = (const char **)g_ct_imps;
if ( g_ct_imps )
{
v2 = &g_ct_imps + 1;
do
{
if ( *v1 && !strcmp(*v1, s2) )
break;
v1 = (const char **)*v2++;
}
while ( v1 );
}
return v1;
}

canonical_decompiled_code:
const char **__fastcall func3(char *s2)
{
const char **v1; // rbx
void **v2; // rbp

v1 = (const char **)g_ct_imps;
if ( g_ct_imps )
{
v2 = &g_ct_imps + 1;
do
{
if ( *v1 && !strcmp(*v1, s2) )
break;
v1 = (const char **)*v2++;
}
while ( v1 );
}
return v1;
}

original_code:
ct_imp* ct_sched(const char* name) {
    int i=0;
    while(g_ct_imps[i]) {
        const char* namei = g_ct_imps[i]->name;
        if(namei && strcmp(namei, name) == 0) {
            return g_ct_imps[i];
        }
        ++i;
    }
    return 0;
}

canonical_original_code:
struct ct_imp *ct_sched(char *name) {
    int i = 0;
    while(g_ct_imps[i]) {
        char *namei = g_ct_imps[i]->name;
        if(namei && strcmp(namei, name) == 0) {
            return g_ct_imps[i];
        }
        ++i;
    }
    return 0;
}

variable_types:
namei
char *

return_type: struct ct_imp { char *name; void (*imp_init)(struct ct_env_var { char *name; char *value; } *env); void (*imp_fini)(); void (*imp_for)(int n, void (*f)(int ind, void *context), void *context, struct ct_canceller { int cancelled; void *sched_data; } *c); void (*imp_canceller_init)(struct ct_canceller *c); void (*imp_canceller_fini)(struct ct_canceller *c); void (*imp_cancel)(struct ct_canceller *c); } *

user_defined_types: struct ct_imp { char *name; void (*imp_init)(struct ct_env_var *env); void (*imp_fini)(); void (*imp_for)(int n, void (*f)(int ind, void *context), void *context, struct ct_canceller *c); void (*imp_canceller_init)(struct ct_canceller *c); void (*imp_canceller_fini)(struct ct_canceller *c); void (*imp_cancel)(struct ct_canceller *c); }


decompiled_code:
const char *ct_default_sched()
{
const char *v0; // r12
const char **v1; // r13
const char **v2; // r14
void **v3; // rbx
const char **v4; // rax
const char *v5; // rbp
__int64 v7[9]; // [rsp+0h] [rbp-48h] BYREF

v0 = "pthreads";
v7[0] = (__int64)"pthreads";
v7[1] = (__int64)"tbb";
v7[2] = (__int64)"openmp";
v7[3] = 0LL;
v1 = (const char **)g_ct_imps;
v2 = (const char **)v7;
while ( !v1 )
{
LABEL_6:
v0 = *++v2;
if ( !*v2 )
return "serial";
}
v3 = &g_ct_imps + 1;
v4 = v1;
while ( 1 )
{
v5 = *v4;
if ( *v4 )
{
if ( !strcmp(*v4, v0) )
return v5;
}
v4 = (const char **)*v3++;
if ( !v4 )
goto LABEL_6;
}
}

canonical_decompiled_code:
const char *func4()
{
const char *v0; // r12
const char **v1; // r13
const char **v2; // r14
void **v3; // rbx
const char **v4; // rax
const char *v5; // rbp
__int64 v7[9]; // [rsp+0h] [rbp-48h] BYREF

v0 = "pthreads";
v7[0] = (__int64)"pthreads";
v7[1] = (__int64)"tbb";
v7[2] = (__int64)"openmp";
v7[3] = 0LL;
v1 = (const char **)g_ct_imps;
v2 = (const char **)v7;
while ( !v1 )
{
LABEL_6:
v0 = *++v2;
if ( !*v2 )
return "serial";
}
v3 = &g_ct_imps + 1;
v4 = v1;
while ( 1 )
{
v5 = *v4;
if ( *v4 )
{
if ( !strcmp(*v4, v0) )
return v5;
}
v4 = (const char **)*v3++;
if ( !v4 )
goto LABEL_6;
}
}

original_code:
const char* ct_default_sched() {
    const char* prefs[] = {"pthreads","tbb","openmp",0};
    int j=0;
    while(prefs[j]) {
        int i=0;
        while(g_ct_imps[i]) {
            const char* namei = g_ct_imps[i]->name;
            if(namei && strcmp(namei,prefs[j]) == 0) {
                return namei;
            }
            ++i;
        }
        ++j;
    }
    return "serial";
}

canonical_original_code:
char *ct_default_sched() {
    char *prefs[] = {"pthreads","tbb","openmp",0};
    int j = 0;
    while(prefs[j]) {
        int i = 0;
        while(g_ct_imps[i]) {
            char *namei = g_ct_imps[i]->name;
            if(namei && strcmp(namei,prefs[j]) == 0) {
                return namei;
            }
            ++i;
        }
        ++j;
    }
    return "serial";
}

variable_types:
prefs
char *[]

variable_types: j int


decompiled_code:
_DWORD *ct_alloc_canceller()
{
_DWORD *v0; // rbx
void (__fastcall *v1)(_DWORD *); // rax

v0 = malloc(0x10uLL);
*v0 = 0;
v1 = *(void (__fastcall **)(_DWORD *))(g_ct_pimpl + 32LL);
if ( v1 )
v1(v0);
return v0;
}

canonical_decompiled_code:
_DWORD *func5()
{
_DWORD *v0; // rbx
void (__fastcall *v1)(_DWORD *); // rax

v0 = malloc(0x10uLL);
*v0 = 0;
v1 = *(void (__fastcall **)(_DWORD *))(g_ct_pimpl + 32LL);
if ( v1 )
v1(v0);
return v0;
}

original_code:
ct_canceller* ct_alloc_canceller(void) {
    ct_canceller* c = (ct_canceller*)malloc(sizeof(ct_canceller));
    c->cancelled = 0;
    if(g_ct_pimpl->imp_canceller_init) {
        g_ct_pimpl->imp_canceller_init(c);
    }
    return c;
}

canonical_original_code:
struct ct_canceller *ct_alloc_canceller() {
    struct ct_canceller *c = (ct_canceller*)malloc(sizeof(ct_canceller));
    c->cancelled = 0;
    if(g_ct_pimpl->imp_canceller_init) {
        g_ct_pimpl->imp_canceller_init(c);
    }
    return c;
}

variable_types: struct ct_canceller { int cancelled; void *sched_data; } *


decompiled_code:
int __fastcall ct_init(const char **a1)
{
char *v1; // rbp
char *v2; // r12
char *v3; // rax
_DWORD *v4; // rax

v1 = (char *)ct_default_sched();
v2 = ct_getenv(a1, "CT_SCHED", v1);
g_ct_pimpl = ct_sched(v2);
if ( !g_ct_pimpl )
{
printf("checkedthreads - WARNING: unknown scheduler (%s) specified, using %s instead\n", v2, v1);
g_ct_pimpl = ct_sched(v1);
}
v3 = ct_getenv(a1, "CT_VERBOSE", "0");
g_ct_verbose = atoi(v3);
(*(void (__fastcall **)(const char **))(g_ct_pimpl + 8LL))(a1);
v4 = ct_alloc_canceller();
g_ct_default_canceller = v4;
if ( g_ct_verbose )
LODWORD(v4) = puts("checkedthreads: initialized");
return (int)v4;
}

canonical_decompiled_code:
int __fastcall func6(const char **a1)
{
char *v1; // rbp
char *v2; // r12
char *v3; // rax
_DWORD *v4; // rax

v1 = (char *)func4();
v2 = func2(a1, "CT_SCHED", v1);
g_ct_pimpl = func3(v2);
if ( !g_ct_pimpl )
{
printf("checkedthreads - WARNING: unknown scheduler (%s) specified, using %s instead\n", v2, v1);
g_ct_pimpl = func3(v1);
}
v3 = func2(a1, "CT_VERBOSE", "0");
g_ct_verbose = atoi(v3);
(*(void (__fastcall **)(const char **))(g_ct_pimpl + 8LL))(a1);
v4 = func5();
g_ct_default_canceller = v4;
if ( g_ct_verbose )
LODWORD(v4) = puts("checkedthreads: initialized");
return (int)v4;
}

original_code:
void ct_init(const ct_env_var* env) {
    const char* default_sched = ct_default_sched();
    const char* sched = ct_getenv(env, "CT_SCHED", default_sched);
    g_ct_pimpl = ct_sched(sched);
    if(!g_ct_pimpl) {
        printf("checkedthreads - WARNING: unknown scheduler (%s) specified, using %s instead\n",
                sched, default_sched);
        g_ct_pimpl = ct_sched(default_sched);
    }
    g_ct_verbose = atoi(ct_getenv(env, "CT_VERBOSE", "0"));
    g_ct_pimpl->imp_init(env);
    g_ct_default_canceller = ct_alloc_canceller();
    if(g_ct_verbose) {
        printf("checkedthreads: initialized\n");
    }
}

canonical_original_code:
void ct_init(struct ct_env_var *env) {
    char *default_sched = ct_default_sched();
    char *sched = ct_getenv(env, "CT_SCHED", default_sched);
    g_ct_pimpl = ct_sched(sched);
    if(!g_ct_pimpl) {
        printf("checkedthreads - WARNING: unknown scheduler (%s) specified, using %s instead\n",
                sched, default_sched);
        g_ct_pimpl = ct_sched(default_sched);
    }
    g_ct_verbose = atoi(ct_getenv(env, "CT_VERBOSE", "0"));
    g_ct_pimpl->imp_init(env);
    g_ct_default_canceller = ct_alloc_canceller();
    if(g_ct_verbose) {
        printf("checkedthreads: initialized\n");
    }
}

variable_types:
sched
char *

user_defined_types: struct ct_env_var { char *name; char *value; }


decompiled_code:
void __fastcall ct_free_canceller(void *ptr)
{
void (*v1)(void); // rax

v1 = *(void (**)(void))(g_ct_pimpl + 40LL);
if ( v1 )
v1();
free(ptr);
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
void (*v1)(void); // rax

v1 = *(void (**)(void))(g_ct_pimpl + 40LL);
if ( v1 )
v1();
free(ptr);
}

original_code:
void ct_free_canceller(ct_canceller* c) {
    if(g_ct_pimpl->imp_canceller_fini) {
        g_ct_pimpl->imp_canceller_fini(c);
    }
    free(c);
}

canonical_original_code:
void ct_free_canceller(struct ct_canceller *c) {
    if(g_ct_pimpl->imp_canceller_fini) {
        g_ct_pimpl->imp_canceller_fini(c);
    }
    free(c);
}

user_defined_types: struct ct_canceller { int cancelled; void *sched_data; }


decompiled_code:
int ct_fini()
{
_DWORD *v0; // rax

ct_free_canceller(g_ct_default_canceller);
(*(void (**)(void))(g_ct_pimpl + 16LL))();
g_ct_pimpl = 0LL;
v0 = g_ct_verbose;
if ( g_ct_verbose[0] )
LODWORD(v0) = puts("checkedthreads: finalized");
return (int)v0;
}

canonical_decompiled_code:
int func8()
{
_DWORD *v0; // rax

func7(g_ct_default_canceller);
(*(void (**)(void))(g_ct_pimpl + 16LL))();
g_ct_pimpl = 0LL;
v0 = g_ct_verbose;
if ( g_ct_verbose[0] )
LODWORD(v0) = puts("checkedthreads: finalized");
return (int)v0;
}

original_code:
void ct_fini(void) {
    ct_free_canceller(g_ct_default_canceller);
    g_ct_pimpl->imp_fini();
    g_ct_pimpl = 0;
    if(g_ct_verbose) {
        printf("checkedthreads: finalized\n");
    }
}

canonical_original_code:
void ct_fini() {
    ct_free_canceller(g_ct_default_canceller);
    g_ct_pimpl->imp_fini();
    g_ct_pimpl = 0;
    if(g_ct_verbose) {
        printf("checkedthreads: finalized\n");
    }
}


decompiled_code:
__int64 __fastcall ct_cancelled(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int *a1)
{
return *a1;
}

original_code:
int ct_cancelled(ct_canceller* c) {
    return c->cancelled;
}

canonical_original_code:
int ct_cancelled(struct ct_canceller *c) {
    return c->cancelled;
}


decompiled_code:
int __fastcall ct_for(unsigned int a1, __int64 a2, __int64 *a3, int *a4)
{
__int64 (__fastcall *v4)(unsigned int, __int64); // r12
__int64 *v5; // r13
int *v6; // rbx
int result; // eax
__int64 v8[7]; // [rsp+0h] [rbp-38h] BYREF

v4 = (__int64 (__fastcall *)(unsigned int, __int64))a2;
v5 = a3;
if ( a4 )
{
v6 = a4;
result = *a4;
if ( *a4 )
return result;
}
else
{
v6 = (int *)g_ct_default_canceller;
}
if ( g_ct_verbose[0] <= 0 )
return (*(__int64 (__fastcall **)(_QWORD, __int64, __int64 *, int *))(g_ct_pimpl + 24LL))(a1, a2, a3, v6);
printf("checkedthreads: ct_for(%d) entered\n", a1);
if ( g_ct_verbose[0] > 1 )
{
v8[0] = a2;
v8[1] = (__int64)v5;
v5 = v8;
v4 = ct_verbose_ind_func;
}
(*(void (__fastcall **)(_QWORD, __int64 (__fastcall *)(unsigned int, __int64), __int64 *, int *))(g_ct_pimpl + 24LL))(
a1,
v4,
v5,
v6);
return printf("checkedthreads: ct_for(%d) ended\n", a1);
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1, __int64 a2, __int64 *a3, int *a4)
{
__int64 (__fastcall *v4)(unsigned int, __int64); // r12
__int64 *v5; // r13
int *v6; // rbx
int result; // eax
__int64 v8[7]; // [rsp+0h] [rbp-38h] BYREF

v4 = (__int64 (__fastcall *)(unsigned int, __int64))a2;
v5 = a3;
if ( a4 )
{
v6 = a4;
result = *a4;
if ( *a4 )
return result;
}
else
{
v6 = (int *)g_ct_default_canceller;
}
if ( g_ct_verbose[0] <= 0 )
return (*(__int64 (__fastcall **)(_QWORD, __int64, __int64 *, int *))(g_ct_pimpl + 24LL))(a1, a2, a3, v6);
printf("checkedthreads: ct_for(%d) entered\n", a1);
if ( g_ct_verbose[0] > 1 )
{
v8[0] = a2;
v8[1] = (__int64)v5;
v5 = v8;
v4 = ct_verbose_ind_func;
}
(*(void (__fastcall **)(_QWORD, __int64 (__fastcall *)(unsigned int, __int64), __int64 *, int *))(g_ct_pimpl + 24LL))(
a1,
v4,
v5,
v6);
return printf("checkedthreads: ct_for(%d) ended\n", a1);
}

original_code:
void ct_for(int n, ct_ind_func f, void* context, ct_canceller* c) {
    if(c == 0) {
        c = g_ct_default_canceller;
    }
    else {
        if(c->cancelled) {
            return;
        }
    }
    if(g_ct_verbose>0) {
        ct_wrapped_func_context wc;
        printf("checkedthreads: ct_for(%d) entered\n",n);
        if(g_ct_verbose>1) {
            wc.next_func = f;
            wc.next_context = context;
            f = ct_verbose_ind_func;
            context = &wc;
        }
        g_ct_pimpl->imp_for(n, f, context, c);
        printf("checkedthreads: ct_for(%d) ended\n",n);
    }
    else {
        g_ct_pimpl->imp_for(n, f, context, c);
    }
}

canonical_original_code:
void ct_for(int n, void (*f)(int ind, void *context), void *context, struct ct_canceller *c) {
    if(c == 0) {
        c = g_ct_default_canceller;
    }
    else {
        if(c->cancelled) {
            return;
        }
    }
    if(g_ct_verbose>0) {
        struct ct_wrapped_func_context wc;
        printf("checkedthreads: ct_for(%d) entered\n",n);
        if(g_ct_verbose>1) {
            wc.next_func = f;
            wc.next_context = context;
            f = ct_verbose_ind_func;
            context = &wc;
        }
        g_ct_pimpl->imp_for(n, f, context, c);
        printf("checkedthreads: ct_for(%d) ended\n",n);
    }
    else {
        g_ct_pimpl->imp_for(n, f, context, c);
    }
}

variable_types: n int

variable_types:
f
void (*)(int ind, void *context)

variable_types: struct ct_wrapped_func_context { void (*next_func)(int ind, void *context); void *next_context; }


decompiled_code:
int __fastcall ct_invoke(__int64 *a1, int *a2)
{
__int64 *v3; // rax
unsigned int v4; // edi

if ( *a1 )
{
v3 = a1 + 2;
v4 = 0;
do
{
++v4;
v3 += 2;
}
while ( *(v3 - 2) );
}
else
{
v4 = 0;
}
return ct_for(v4, (__int64)ct_dispatch_task, a1, a2);
}

canonical_decompiled_code:
int __fastcall func11(__int64 *a1, int *a2)
{
__int64 *v3; // rax
unsigned int v4; // edi

if ( *a1 )
{
v3 = a1 + 2;
v4 = 0;
do
{
++v4;
v3 += 2;
}
while ( *(v3 - 2) );
}
else
{
v4 = 0;
}
return func10(v4, (__int64)ct_dispatch_task, a1, a2);
}

original_code:
void ct_invoke(const ct_task tasks[], ct_canceller* c) {
    int i;
    for(i=0; tasks[i].func; ++i);
    ct_for(i, ct_dispatch_task, (void*)tasks, c);
}

canonical_original_code:
void ct_invoke(struct ct_task tasks[], struct ct_canceller *c) {
    int i;
    for(i=0; tasks[i].func; ++i);
    ct_for(i, ct_dispatch_task, (void *)tasks, c);
}

variable_types: struct ct_task { void (*func)(void *arg); void *arg; } []


decompiled_code:
__int64 __fastcall ct_locked_enqueue(pthread_mutex_t *mutex, __int64 a2, int a3)
{
unsigned int v3; // er13
int v7; // edi
int v8; // eax
int v9; // edx

v3 = 0;
if ( a3 + mutex[1].__spins <= mutex[1].__owner )
{
pthread_mutex_lock(mutex);
v7 = mutex[1].__owner;
if ( a3 + mutex[1].__spins <= v7 )
{
v8 = mutex[1].__kind;
if ( a3 > 0 )
{
v9 = 0;
do
{
*(_QWORD *)(mutex[1].__align + 8LL * v8++) = a2;
if ( v7 <= v8 )
v8 = 0;
++v9;
}
while ( a3 != v9 );
}
mutex[1].__kind = v8;
mutex[1].__spins += a3;
v3 = 1;
}
pthread_mutex_unlock(mutex);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(pthread_mutex_t *mutex, __int64 a2, int a3)
{
unsigned int v3; // er13
int v7; // edi
int v8; // eax
int v9; // edx

v3 = 0;
if ( a3 + mutex[1].__spins <= mutex[1].__owner )
{
pthread_mutex_lock(mutex);
v7 = mutex[1].__owner;
if ( a3 + mutex[1].__spins <= v7 )
{
v8 = mutex[1].__kind;
if ( a3 > 0 )
{
v9 = 0;
do
{
*(_QWORD *)(mutex[1].__align + 8LL * v8++) = a2;
if ( v7 <= v8 )
v8 = 0;
++v9;
}
while ( a3 != v9 );
}
mutex[1].__kind = v8;
mutex[1].__spins += a3;
v3 = 1;
}
pthread_mutex_unlock(mutex);
}
return v3;
}

original_code:
int ct_locked_enqueue(ct_locked_queue* q, ct_work_item* item, int reps) {
    int ret = 0, capacity;
    if(q->size + reps > q->capacity) {
        return ret;
    }
    pthread_mutex_lock(&q->mutex);
    capacity = q->capacity;
    if(q->size + reps <= capacity) {
        int i, w=q->write_ind;
        for(i=0; i<reps; ++i) {
            q->work_items[w] = item;
            w++;
            if(w >= capacity) {
                w = 0;
            }
        }
        q->write_ind = w;
        q->size += reps;
        ret = 1;
    }
    pthread_mutex_unlock(&q->mutex);
    return ret;
}

canonical_original_code:
int ct_locked_enqueue(struct ct_locked_queue *q, struct ct_work_item *item, int reps) {
    int ret = 0; int capacity;
    if(q->size + reps > q->capacity) {
        return ret;
    }
    pthread_mutex_lock(&q->mutex);
    capacity = q->capacity;
    if(q->size + reps <= capacity) {
        int i; int w = q->write_ind;
        for(i=0; i<reps; ++i) {
            q->work_items[w] = item;
            w++;
            if(w >= capacity) {
                w = 0;
            }
        }
        q->write_ind = w;
        q->size += reps;
        ret = 1;
    }
    pthread_mutex_unlock(&q->mutex);
    return ret;
}

variable_types: struct ct_locked_queue { 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; struct ct_work_item { int next_ind; int to_do; int n; int ref_cnt; void (*f)(int ind, void *context); void *context; struct ct_canceller { int cancelled; void *sched_data; } *canceller; } **work_items; int capacity; int read_ind; int write_ind; int size; } *

variable_types:
item
struct ct_work_item *

variable_types: reps int

variable_types: ret int

variable_types: capacity int

variable_types: w int

user_defined_types: struct ct_locked_queue { union pthread_mutex_t mutex; struct ct_work_item **work_items; int capacity; int read_ind; int write_ind; int size; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; 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; }

user_defined_types: struct ct_work_item { int next_ind; int to_do; int n; int ref_cnt; void (*f)(int ind, void *context); void *context; struct ct_canceller *canceller; }


decompiled_code:
__int64 __fastcall ct_locked_dequeue(pthread_mutex_t *mutex)
{
__int64 v1; // rbp
signed int v2; // eax
int v3; // eax

v1 = 0LL;
if ( mutex[1].__spins )
{
pthread_mutex_lock(mutex);
if ( mutex[1].__spins > 0 )
{
v2 = mutex[1].__nusers;
v1 = *(_QWORD *)(mutex[1].__align + 8LL * v2);
v3 = v2 + 1;
if ( mutex[1].__owner <= v3 )
v3 = 0;
mutex[1].__nusers = v3;
--mutex[1].__spins;
}
pthread_mutex_unlock(mutex);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(pthread_mutex_t *mutex)
{
__int64 v1; // rbp
signed int v2; // eax
int v3; // eax

v1 = 0LL;
if ( mutex[1].__spins )
{
pthread_mutex_lock(mutex);
if ( mutex[1].__spins > 0 )
{
v2 = mutex[1].__nusers;
v1 = *(_QWORD *)(mutex[1].__align + 8LL * v2);
v3 = v2 + 1;
if ( mutex[1].__owner <= v3 )
v3 = 0;
mutex[1].__nusers = v3;
--mutex[1].__spins;
}
pthread_mutex_unlock(mutex);
}
return v1;
}

original_code:
ct_work_item* ct_locked_dequeue(ct_locked_queue* q) {
    ct_work_item* ret = 0;
    if(q->size == 0) {
        return ret;
    }
    pthread_mutex_lock(&q->mutex);
    if(q->size > 0) {
        int r = q->read_ind;
        ret = q->work_items[r];
        r++;
        if(r >= q->capacity) {
            r = 0;
        }
        q->read_ind = r;
        q->size -= 1;
    }
    pthread_mutex_unlock(&q->mutex);
    return ret;
}

canonical_original_code:
struct ct_work_item *ct_locked_dequeue(struct ct_locked_queue *q) {
    struct ct_work_item *ret = 0;
    if(q->size == 0) {
        return ret;
    }
    pthread_mutex_lock(&q->mutex);
    if(q->size > 0) {
        int r = q->read_ind;
        ret = q->work_items[r];
        r++;
        if(r >= q->capacity) {
            r = 0;
        }
        q->read_ind = r;
        q->size -= 1;
    }
    pthread_mutex_unlock(&q->mutex);
    return ret;
}

variable_types: struct ct_locked_queue { 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; struct ct_work_item **work_items; int capacity; int read_ind; int write_ind; int size; } *

variable_types: struct ct_work_item { int next_ind; int to_do; int n; int ref_cnt; void (*f)(int ind, void *context); void *context; struct ct_canceller { int cancelled; void *sched_data; } *canceller; } *

variable_types: r int


decompiled_code:
void ct_valgrind_fini()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void ct_valgrind_fini(void) {
}

canonical_original_code:
void ct_valgrind_fini() {
}


decompiled_code:
__int64 ct_valgrind_init()
{
return ct_shuffle_init();
}

canonical_decompiled_code:
__int64 func1()
{
return ct_shuffle_init();
}

original_code:
void ct_valgrind_init(const ct_env_var* env) {
    ct_shuffle_init(env);
}

canonical_original_code:
void ct_valgrind_init(struct ct_env_var *env) {
    ct_shuffle_init(env);
}

variable_types: struct ct_env_var { char *name; char *value; } *


decompiled_code:
void *__fastcall ct_valgrind_ptr(int a1, __int64 a2)
{
void *result; // rax

result = &g_ct_valgrind_cmd;
*(_QWORD *)((char *)&g_ct_valgrind_cmd + a1 + 20) = a2;
return result;
}

canonical_decompiled_code:
void *__fastcall func2(int a1, __int64 a2)
{
void *result; // rax

result = &g_ct_valgrind_cmd;
*(_QWORD *)((char *)&g_ct_valgrind_cmd + a1 + 20) = a2;
return result;
}

original_code:
void ct_valgrind_ptr(int oft, volatile const void* ptr) {
    *(volatile const void**)&g_ct_valgrind_cmd.payload[oft] = ptr;
}

canonical_original_code:
void ct_valgrind_ptr(int oft, void *ptr) {
    *(void **)&g_ct_valgrind_cmd.payload[oft] = ptr;
}

variable_types: oft int

variable_types:
ptr
void *


decompiled_code:
void *__fastcall ct_valgrind_cmd(char *a1)
{
char v1; // dl
__int64 v2; // rax
void *result; // rax

v1 = *a1;
if ( *a1 )
{
v2 = 0LL;
do
{
*((_BYTE *)&g_ct_valgrind_cmd + (int)v2++ + 20) = v1;
v1 = a1[v2];
}
while ( v1 );
}
result = &g_ct_valgrind_cmd;
g_ct_valgrind_cmd = 305419896;
return result;
}

canonical_decompiled_code:
void *__fastcall func3(char *a1)
{
char v1; // dl
__int64 v2; // rax
void *result; // rax

v1 = *a1;
if ( *a1 )
{
v2 = 0LL;
do
{
*((_BYTE *)&g_ct_valgrind_cmd + (int)v2++ + 20) = v1;
v1 = a1[v2];
}
while ( v1 );
}
result = &g_ct_valgrind_cmd;
g_ct_valgrind_cmd = 305419896;
return result;
}

original_code:
void ct_valgrind_cmd(const char* str) {
    int i;
    for(i=0; str[i]; ++i) {
        g_ct_valgrind_cmd.payload[i] = str[i];
    }
    g_ct_valgrind_cmd.stored_magic = 0x12345678;
}

canonical_original_code:
void ct_valgrind_cmd(char *str) {
    int i;
    for(i=0; str[i]; ++i) {
        g_ct_valgrind_cmd.payload[i] = str[i];
    }
    g_ct_valgrind_cmd.stored_magic = 0x12345678;
}

variable_types:
str
char *

variable_types: i int