decompiled_code:
void __fastcall __noreturn pump_loop(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 v5; // r13
__int64 v6; // r12
__int64 v7; // rbx
__int64 v8; // rbx
int v9; // ebx
int buf[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v5 = a3;
v6 = a3 + 16LL * (unsigned int)(a4 - 1) + 16;
while ( 1 )
{
pump_init(a1);
if ( a4 > 0 )
{
v8 = v5;
do
{
pump_add_pair(a1, v8);
v8 += 16LL;
}
while ( v8 != v6 );
}
if ( a2 >= 0 )
pump_add_fd(a1, (unsigned int)a2, 5LL);
while ( (unsigned int)pump_select(a1) == -1 )
{
if ( *_errno_location() != 4 )
{
perror("select");
abort();
}
}
v7 = v5;
if ( a4 > 0 )
{
do
{
pump_pair_copy(v7);
v7 += 16LL;
}
while ( v7 != v6 );
}
if ( (unsigned int)pump_ready(a1, (unsigned int)a2, 5LL) )
{
v9 = read(a2, buf, 4uLL);
if ( v9 >= 0 )
{
if ( a4 > 0 )
{
do
{
pump_pair_copy(v5);
v5 += 16LL;
}
while ( v5 != v6 );
}
if ( v9 )
{
if ( v9 != 4 )
_assert_fail("rv == sizeof(status)", "wsh.c", 0xA2u, "pump_loop");
exit(buf[0]);
}
exit(255);
}
_assert_fail("rv >= 0", "wsh.c", 0x96u, "pump_loop");
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 v5; // r13
__int64 v6; // r12
__int64 v7; // rbx
__int64 v8; // rbx
int v9; // ebx
int buf[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v5 = a3;
v6 = a3 + 16LL * (unsigned int)(a4 - 1) + 16;
while ( 1 )
{
pump_init(a1);
if ( a4 > 0 )
{
v8 = v5;
do
{
pump_add_pair(a1, v8);
v8 += 16LL;
}
while ( v8 != v6 );
}
if ( a2 >= 0 )
pump_add_fd(a1, (unsigned int)a2, 5LL);
while ( (unsigned int)pump_select(a1) == -1 )
{
if ( *_errno_location() != 4 )
{
perror("select");
abort();
}
}
v7 = v5;
if ( a4 > 0 )
{
do
{
pump_pair_copy(v7);
v7 += 16LL;
}
while ( v7 != v6 );
}
if ( (unsigned int)pump_ready(a1, (unsigned int)a2, 5LL) )
{
v9 = read(a2, buf, 4uLL);
if ( v9 >= 0 )
{
if ( a4 > 0 )
{
do
{
pump_pair_copy(v5);
v5 += 16LL;
}
while ( v5 != v6 );
}
if ( v9 )
{
if ( v9 != 4 )
_assert_fail("rv == sizeof(status)", "wsh.c", 0xA2u, "pump_loop");
exit(buf[0]);
}
exit(255);
}
_assert_fail("rv >= 0", "wsh.c", 0x96u, "pump_loop");
}
}
}

original_code:
void pump_loop(pump_t *p, int exit_status_fd, pump_pair_t *pp, int pplen) {
  int i, rv;
  for (;;) {
    pump_init(p);
    for (i = 0; i < pplen; i++) {
      pump_add_pair(p, &pp[i]);
    }
    if (exit_status_fd >= 0) {
      pump_add_fd(p, exit_status_fd, 1 | 4);
    }
    do {
      rv = pump_select(p);
    } while (rv == -1 && (*__errno_location ()) == 4);
    if (rv == -1) {
      perror("select");
      abort();
    }
    for (i = 0; i < pplen; i++) {
      pump_pair_copy(&pp[i]);
    }
    if (pump_ready(p, exit_status_fd, 1 | 4)) {
      int status;
      rv = read(exit_status_fd, &status, sizeof(status));
      ((void) sizeof ((rv >= 0) ? 1 : 0), __extension__ ({ if (rv >= 0) ; else __assert_fail ("rv >= 0", "wsh.c", 150, __extension__ __PRETTY_FUNCTION__); }));
      for (i = 0; i < pplen; i++) {
        pump_pair_copy(&pp[i]);
      }
      if (rv == 0) {
        exit(255);
      }
      ((void) sizeof ((rv == sizeof(status)) ? 1 : 0), __extension__ ({ if (rv == sizeof(status)) ; else __assert_fail ("rv == sizeof(status)", "wsh.c", 162, __extension__ __PRETTY_FUNCTION__); }));
      exit(status);
    }
  }
}

canonical_original_code:
void pump_loop(struct pump_s *p, int exit_status_fd, struct pump_pair_s *pp, int pplen) {
  int i; int rv;
  for (;;) {
    pump_init(p);
    for (i = 0; i < pplen; i++) {
      pump_add_pair(p, &pp[i]);
    }
    if (exit_status_fd >= 0) {
      pump_add_fd(p, exit_status_fd, 1 | 4);
    }
    do {
      rv = pump_select(p);
    } while (rv == -1 && (*__errno_location ()) == 4);
    if (rv == -1) {
      perror("select");
      abort();
    }
    for (i = 0; i < pplen; i++) {
      pump_pair_copy(&pp[i]);
    }
    if (pump_ready(p, exit_status_fd, 1 | 4)) {
      int status;
      rv = read(exit_status_fd, &status, sizeof(status));
      ((void) sizeof ((rv >= 0) ? 1 : 0), __extension__ ({ if (rv >= 0) ; else __assert_fail ("rv >= 0", "wsh.c", 150, __extension__ __PRETTY_FUNCTION__); }));
      for (i = 0; i < pplen; i++) {
        pump_pair_copy(&pp[i]);
      }
      if (rv == 0) {
        exit(255);
      }
      ((void) sizeof ((rv == sizeof(status)) ? 1 : 0), __extension__ ({ if (rv == sizeof(status)) ; else __assert_fail ("rv == sizeof(status)", "wsh.c", 162, __extension__ __PRETTY_FUNCTION__); }));
      exit(status);
    }
  }
}

variable_types: struct pump_s { int nfd; struct fd_set { long fds_bits[-1]; } rfds; struct fd_set wfds; struct fd_set efds; } *

variable_types: struct pump_pair_s { struct pump_s *p; int rfd; int wfd; } *

variable_types: pplen int

variable_types: status int

user_defined_types: struct pump_s { int nfd; struct fd_set rfds; struct fd_set wfds; struct fd_set efds; }


repo: chenyf/wsh

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

result = tcsetattr(pty_local_fd, 0, &told);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xB0u, "tty_reset");
return result;
}

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

result = tcsetattr(pty_local_fd, 0, &told);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xB0u, "tty_reset");
return result;
}

original_code:
void tty_reset(void) {
  int rv;
  rv = tcsetattr(pty_local_fd, 0, &told);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 176, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void tty_reset() {
  int rv;
  rv = tcsetattr(pty_local_fd, 0, &told);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 176, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: rv int

return_type: void


decompiled_code:
void tty__atexit(void)
{
tty_reset();
}

canonical_decompiled_code:
void func4(void)
{
func3();
}

original_code:
void tty__atexit(void) {
  tty_reset();
}

canonical_original_code:
void tty__atexit() {
  tty_reset();
}


decompiled_code:
int tty_raw()
{
int v0; // ebx
int result; // eax

v0 = pty_local_fd;
if ( tcgetattr(pty_local_fd, &told) == -1 )
_assert_fail("rv != -1", "wsh.c", 0xBBu, "tty_raw");
if ( atexit(tty__atexit) == -1 )
_assert_fail("rv != -1", "wsh.c", 0xBEu, "tty_raw");
*(__m128i *)&tnew.c_iflag = _mm_load_si128((const __m128i *)&told);
*(__m128i *)&tnew.c_line = _mm_load_si128((const __m128i *)&told.c_line);
*(__m128i *)&tnew.c_cc[15] = _mm_load_si128((const __m128i *)&told.c_cc[15]);
*(_QWORD *)&tnew.c_cc[31] = *(_QWORD *)&told.c_cc[31];
tnew.c_ospeed = told.c_ospeed;
cfmakeraw(&tnew);
result = tcsetattr(v0, 0, &tnew);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xC4u, "tty_raw");
return result;
}

canonical_decompiled_code:
int func5()
{
int v0; // ebx
int result; // eax

v0 = pty_local_fd;
if ( tcgetattr(pty_local_fd, &told) == -1 )
_assert_fail("rv != -1", "wsh.c", 0xBBu, "tty_raw");
if ( atexit(tty__atexit) == -1 )
_assert_fail("rv != -1", "wsh.c", 0xBEu, "tty_raw");
*(__m128i *)&tnew.c_iflag = _mm_load_si128((const __m128i *)&told);
*(__m128i *)&tnew.c_line = _mm_load_si128((const __m128i *)&told.c_line);
*(__m128i *)&tnew.c_cc[15] = _mm_load_si128((const __m128i *)&told.c_cc[15]);
*(_QWORD *)&tnew.c_cc[31] = *(_QWORD *)&told.c_cc[31];
tnew.c_ospeed = told.c_ospeed;
cfmakeraw(&tnew);
result = tcsetattr(v0, 0, &tnew);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xC4u, "tty_raw");
return result;
}

original_code:
void tty_raw(void) {
  int rv;
  rv = tcgetattr(pty_local_fd, &told);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 187, __extension__ __PRETTY_FUNCTION__); }));
  rv = atexit(tty__atexit);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 190, __extension__ __PRETTY_FUNCTION__); }));
  tnew = told;
  cfmakeraw(&tnew);
  rv = tcsetattr(pty_local_fd, 0, &tnew);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 196, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void tty_raw() {
  int rv;
  rv = tcgetattr(pty_local_fd, &told);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 187, __extension__ __PRETTY_FUNCTION__); }));
  rv = atexit(tty__atexit);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 190, __extension__ __PRETTY_FUNCTION__); }));
  tnew = told;
  cfmakeraw(&tnew);
  rv = tcsetattr(pty_local_fd, 0, &tnew);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 196, __extension__ __PRETTY_FUNCTION__); }));
}


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

result = ioctl(pty_local_fd, 0x5413uLL, &wsz);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xCBu, "tty_gwinsz");
return result;
}

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

result = ioctl(pty_local_fd, 0x5413uLL, &wsz);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xCBu, "tty_gwinsz");
return result;
}

original_code:
void tty_gwinsz(void) {
  int rv;
  rv = ioctl(pty_local_fd, 0x5413, &wsz);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 203, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void tty_gwinsz() {
  int rv;
  rv = ioctl(pty_local_fd, 0x5413, &wsz);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 203, __extension__ __PRETTY_FUNCTION__); }));
}


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

result = ioctl(pty_remote_fd, 0x5414uLL, &wsz);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xD2u, "tty_swinsz");
return result;
}

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

result = ioctl(pty_remote_fd, 0x5414uLL, &wsz);
if ( result == -1 )
_assert_fail("rv != -1", "wsh.c", 0xD2u, "tty_swinsz");
return result;
}

original_code:
void tty_swinsz(void) {
  int rv;
  rv = ioctl(pty_remote_fd, 0x5414, &wsz);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 210, __extension__ __PRETTY_FUNCTION__); }));
}

canonical_original_code:
void tty_swinsz() {
  int rv;
  rv = ioctl(pty_remote_fd, 0x5414, &wsz);
  ((void) sizeof ((rv != -1) ? 1 : 0), __extension__ ({ if (rv != -1) ; else __assert_fail ("rv != -1", "wsh.c", 210, __extension__ __PRETTY_FUNCTION__); }));
}


decompiled_code:
void tty__sigwinch()
{
tty_gwinsz();
tty_swinsz();
}

canonical_decompiled_code:
void func8()
{
func6();
func7();
}

original_code:
void tty__sigwinch(int sig) {
  tty_gwinsz();
  tty_swinsz();
}

variable_types: sig int


decompiled_code:
int tty_winsz()
{
if ( signal(28, (__sighandler_t)tty__sigwinch) == (__sighandler_t)-1LL )
_assert_fail("s != SIG_ERR", "wsh.c", 0xDFu, "tty_winsz");
tty_gwinsz();
return tty_swinsz();
}

canonical_decompiled_code:
int func9()
{
if ( signal(28, (__sighandler_t)tty__sigwinch) == (__sighandler_t)-1LL )
_assert_fail("s != SIG_ERR", "wsh.c", 0xDFu, "tty_winsz");
func6();
return func7();
}

original_code:
void tty_winsz(void) {
  sighandler_t s;
  s = signal(28, tty__sigwinch);
  ((void) sizeof ((s != ((__sighandler_t) -1)) ? 1 : 0), __extension__ ({ if (s != ((__sighandler_t) -1)) ; else __assert_fail ("s != SIG_ERR", "wsh.c", 223, __extension__ __PRETTY_FUNCTION__); }));
  tty_gwinsz();
  tty_swinsz();
}

canonical_original_code:
void tty_winsz() {
  void (*s)(int);
  s = signal(28, tty__sigwinch);
  ((void) sizeof ((s != ((__sighandler_t) -1)) ? 1 : 0), __extension__ ({ if (s != ((__sighandler_t) -1)) ; else __assert_fail ("s != SIG_ERR", "wsh.c", 223, __extension__ __PRETTY_FUNCTION__); }));
  tty_gwinsz();
  tty_swinsz();
}

variable_types:
s
void (*)(int)


decompiled_code:
void __fastcall __noreturn loop_interactive(__int64 a1)
{
int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v4[16]; // [rsp+0h] [rbp-41B8h] BYREF
char v5[16]; // [rsp+10h] [rbp-41A8h] BYREF
char v6[392]; // [rsp+20h] [rbp-4198h] BYREF
int fd[2]; // [rsp+1A8h] [rbp-4010h] BYREF
char v8[16392]; // [rsp+1B0h] [rbp-4008h] BYREF

v1 = un_recv_fds(a1, v8, 0x4000LL, fd, 2LL);
if ( v1 > 0 )
{
if ( v1 != 4 )
_assert_fail("rv == sizeof(res)", "wsh.c", 0xF4u, "loop_interactive");
pty_remote_fd = fd[0];
pty_local_fd = 0;
tty_raw();
tty_winsz();
v2 = dup(fd[0]);
pump_pair_init(v4, v6, 0LL, v2);
v3 = dup(fd[0]);
pump_pair_init(v5, v6, v3, 1LL);
pump_loop((__int64)v6, fd[1], (__int64)v4, 2);
}
perror("recvmsg");
exit(255);
}

canonical_decompiled_code:
void __fastcall __noreturn func10(__int64 a1)
{
int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
_BYTE v4[16]; // [rsp+0h] [rbp-41B8h] BYREF
char v5[16]; // [rsp+10h] [rbp-41A8h] BYREF
char v6[392]; // [rsp+20h] [rbp-4198h] BYREF
int fd[2]; // [rsp+1A8h] [rbp-4010h] BYREF
char v8[16392]; // [rsp+1B0h] [rbp-4008h] BYREF

v1 = un_recv_fds(a1, v8, 0x4000LL, fd, 2LL);
if ( v1 > 0 )
{
if ( v1 != 4 )
_assert_fail("rv == sizeof(res)", "wsh.c", 0xF4u, "loop_interactive");
pty_remote_fd = fd[0];
pty_local_fd = 0;
func5();
func9();
v2 = dup(fd[0]);
pump_pair_init(v4, v6, 0LL, v2);
v3 = dup(fd[0]);
pump_pair_init(v5, v6, v3, 1LL);
func2((__int64)v6, fd[1], (__int64)v4, 2);
}
perror("recvmsg");
exit(255);
}

original_code:
void loop_interactive(int fd) {
  msg_response_t res;
  char buf[(16 * 1024)];
  size_t buflen = sizeof(buf);
  int fds[2];
  size_t fdslen = sizeof(fds)/sizeof(fds[0]);
  int rv;
  rv = un_recv_fds(fd, buf, buflen, fds, fdslen);
  if (rv <= 0) {
    perror("recvmsg");
    exit(255);
  }
  ((void) sizeof ((rv == sizeof(res)) ? 1 : 0), __extension__ ({ if (rv == sizeof(res)) ; else __assert_fail ("rv == sizeof(res)", "wsh.c", 244, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(&res, buf, sizeof(res));
  pty_remote_fd = fds[0];
  pty_local_fd = 0;
  tty_raw();
  tty_winsz();
  pump_t p;
  pump_pair_t pp[2];
  pump_pair_init(&pp[0], &p, 0, dup(fds[0]));
  pump_pair_init(&pp[1], &p, dup(fds[0]), 1);
  pump_loop(&p, fds[1], pp, 2);
}

canonical_original_code:
void loop_interactive(int fd) {
  struct msg_response_s res;
  char buf[-1];
  unsigned long buflen = sizeof(buf);
  int fds[2];
  unsigned long fdslen = sizeof(fds)/sizeof(fds[0]);
  int rv;
  rv = un_recv_fds(fd, buf, buflen, fds, fdslen);
  if (rv <= 0) {
    perror("recvmsg");
    exit(255);
  }
  ((void) sizeof ((rv == sizeof(res)) ? 1 : 0), __extension__ ({ if (rv == sizeof(res)) ; else __assert_fail ("rv == sizeof(res)", "wsh.c", 244, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(&res, buf, sizeof(res));
  pty_remote_fd = fds[0];
  pty_local_fd = 0;
  tty_raw();
  tty_winsz();
  struct pump_s p;
  struct pump_pair_s pp[2];
  pump_pair_init(&pp[0], &p, 0, dup(fds[0]));
  pump_pair_init(&pp[1], &p, dup(fds[0]), 1);
  pump_loop(&p, fds[1], pp, 2);
}

variable_types: fd int

variable_types:
res
struct msg_response_s { int version; }

variable_types:
buf
char [-1]

variable_types:
buflen
unsigned long

variable_types:
fds
int [2]

variable_types:
fdslen
unsigned long

variable_types: struct pump_pair_s { struct pump_s { int nfd; struct fd_set { long fds_bits[-1]; } rfds; struct fd_set wfds; struct fd_set efds; } *p; int rfd; int wfd; } [2]

user_defined_types: struct pump_pair_s { struct pump_s *p; int rfd; int wfd; }


decompiled_code:
void __fastcall __noreturn loop_noninteractive(__int64 a1)
{
int v1; // eax
_BYTE v2[16]; // [rsp+0h] [rbp-41D8h] BYREF
char v3[16]; // [rsp+10h] [rbp-41C8h] BYREF
char v4[16]; // [rsp+20h] [rbp-41B8h] BYREF
char v5[400]; // [rsp+30h] [rbp-41A8h] BYREF
int v6[4]; // [rsp+1C0h] [rbp-4018h] BYREF
char v7[16392]; // [rsp+1D0h] [rbp-4008h] BYREF

v1 = un_recv_fds(a1, v7, 0x4000LL, v6, 4LL);
if ( v1 > 0 )
{
if ( v1 != 4 )
_assert_fail("rv == sizeof(res)", "wsh.c", 0x115u, "loop_noninteractive");
pump_pair_init(v2, v5, 0LL, (unsigned int)v6[0]);
pump_pair_init(v3, v5, (unsigned int)v6[1], 1LL);
pump_pair_init(v4, v5, (unsigned int)v6[2], 2LL);
pump_loop((__int64)v5, v6[3], (__int64)v2, 3);
}
perror("recvmsg");
exit(255);
}

canonical_decompiled_code:
void __fastcall __noreturn func11(__int64 a1)
{
int v1; // eax
_BYTE v2[16]; // [rsp+0h] [rbp-41D8h] BYREF
char v3[16]; // [rsp+10h] [rbp-41C8h] BYREF
char v4[16]; // [rsp+20h] [rbp-41B8h] BYREF
char v5[400]; // [rsp+30h] [rbp-41A8h] BYREF
int v6[4]; // [rsp+1C0h] [rbp-4018h] BYREF
char v7[16392]; // [rsp+1D0h] [rbp-4008h] BYREF

v1 = un_recv_fds(a1, v7, 0x4000LL, v6, 4LL);
if ( v1 > 0 )
{
if ( v1 != 4 )
_assert_fail("rv == sizeof(res)", "wsh.c", 0x115u, "loop_noninteractive");
pump_pair_init(v2, v5, 0LL, (unsigned int)v6[0]);
pump_pair_init(v3, v5, (unsigned int)v6[1], 1LL);
pump_pair_init(v4, v5, (unsigned int)v6[2], 2LL);
func2((__int64)v5, v6[3], (__int64)v2, 3);
}
perror("recvmsg");
exit(255);
}

original_code:
void loop_noninteractive(int fd) {
  msg_response_t res;
  char buf[(16 * 1024)];
  size_t buflen = sizeof(buf);
  int fds[4];
  size_t fdslen = sizeof(fds)/sizeof(fds[0]);
  int rv;
  rv = un_recv_fds(fd, buf, buflen, fds, fdslen);
  if (rv <= 0) {
    perror("recvmsg");
    exit(255);
  }
  ((void) sizeof ((rv == sizeof(res)) ? 1 : 0), __extension__ ({ if (rv == sizeof(res)) ; else __assert_fail ("rv == sizeof(res)", "wsh.c", 277, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(&res, buf, sizeof(res));
  pump_t p;
  pump_pair_t pp[3];
  pump_pair_init(&pp[0], &p, 0, fds[0]);
  pump_pair_init(&pp[1], &p, fds[1], 1);
  pump_pair_init(&pp[2], &p, fds[2], 2);
  pump_loop(&p, fds[3], pp, 3);
}

canonical_original_code:
void loop_noninteractive(int fd) {
  struct msg_response_s res;
  char buf[-1];
  unsigned long buflen = sizeof(buf);
  int fds[4];
  unsigned long fdslen = sizeof(fds)/sizeof(fds[0]);
  int rv;
  rv = un_recv_fds(fd, buf, buflen, fds, fdslen);
  if (rv <= 0) {
    perror("recvmsg");
    exit(255);
  }
  ((void) sizeof ((rv == sizeof(res)) ? 1 : 0), __extension__ ({ if (rv == sizeof(res)) ; else __assert_fail ("rv == sizeof(res)", "wsh.c", 277, __extension__ __PRETTY_FUNCTION__); }));
  memcpy(&res, buf, sizeof(res));
  struct pump_s p;
  struct pump_pair_s pp[3];
  pump_pair_init(&pp[0], &p, 0, fds[0]);
  pump_pair_init(&pp[1], &p, fds[1], 1);
  pump_pair_init(&pp[2], &p, fds[2], 2);
  pump_loop(&p, fds[3], pp, 3);
}

variable_types:
fds
int [4]

variable_types: struct pump_pair_s { struct pump_s { int nfd; struct fd_set { long fds_bits[-1]; } rfds; struct fd_set wfds; struct fd_set efds; } *p; int rfd; int wfd; } [3]


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rax
_QWORD *v4; // rbx
unsigned int v5; // ebp
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
_BYTE v10[4]; // [rsp+0h] [rbp-21D8h] BYREF
_BOOL4 v11; // [rsp+4h] [rbp-21D4h]
char v12[8200]; // [rsp+8h] [rbp-21D0h] BYREF
char v13[392]; // [rsp+2010h] [rbp-1C8h] BYREF
char v14[64]; // [rsp+2198h] [rbp-40h] BYREF

v3 = calloc(1uLL, 0x20uLL);
if ( v3 )
{
v4 = v3;
*(_DWORD *)v3 = argc;
v3[1] = argv;
if ( (unsigned int)wsh__getopt((__int64)v3) != -1 )
{
if ( !v4[2] )
v4[2] = "run/wshd.sock";
v5 = un_connect(v4[2]);
if ( (v5 & 0x80000000) == 0 )
{
msg_request_init(v10);
v11 = isatty(0) != 0;
if ( (unsigned int)msg_array_import(v12, *(unsigned int *)v4, v4[1]) != -1 )
{
if ( (unsigned int)msg_rlimit_import(v13) != -1 )
{
if ( (unsigned int)msg_user_import(v14, v4[3]) != -1 )
{
if ( (int)un_send_fds(v5, v10, 8632LL, 0LL, 0LL) > 0 )
{
if ( v11 )
loop_interactive(v5);
loop_noninteractive(v5);
}
perror("sendmsg");
exit(255);
}
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "msg_user_import: %s\n", v9);
exit(255);
}
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "msg_rlimit_import: %s\n", v7);
exit(255);
}
fwrite("msg_import_array: Too much data\n", 1uLL, 0x20uLL, stderr);
exit(255);
}
perror("connect");
exit(255);
}
exit(1);
}
_assert_fail("w != NULL", "wsh.c", 0x129u, "main");
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
_QWORD *v3; // rax
_QWORD *v4; // rbx
unsigned int v5; // ebp
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
_BYTE v10[4]; // [rsp+0h] [rbp-21D8h] BYREF
_BOOL4 v11; // [rsp+4h] [rbp-21D4h]
char v12[8200]; // [rsp+8h] [rbp-21D0h] BYREF
char v13[392]; // [rsp+2010h] [rbp-1C8h] BYREF
char v14[64]; // [rsp+2198h] [rbp-40h] BYREF

v3 = calloc(1uLL, 0x20uLL);
if ( v3 )
{
v4 = v3;
*(_DWORD *)v3 = argc;
v3[1] = argv;
if ( (unsigned int)func1((__int64)v3) != -1 )
{
if ( !v4[2] )
v4[2] = "run/wshd.sock";
v5 = un_connect(v4[2]);
if ( (v5 & 0x80000000) == 0 )
{
msg_request_init(v10);
v11 = isatty(0) != 0;
if ( (unsigned int)msg_array_import(v12, *(unsigned int *)v4, v4[1]) != -1 )
{
if ( (unsigned int)msg_rlimit_import(v13) != -1 )
{
if ( (unsigned int)msg_user_import(v14, v4[3]) != -1 )
{
if ( (int)un_send_fds(v5, v10, 8632LL, 0LL, 0LL) > 0 )
{
if ( v11 )
func10(v5);
func11(v5);
}
perror("sendmsg");
exit(255);
}
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "msg_user_import: %s\n", v9);
exit(255);
}
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "msg_rlimit_import: %s\n", v7);
exit(255);
}
fwrite("msg_import_array: Too much data\n", 1uLL, 0x20uLL, stderr);
exit(255);
}
perror("connect");
exit(255);
}
exit(1);
}
_assert_fail("w != NULL", "wsh.c", 0x129u, "main");
}

original_code:
int main(int argc, char **argv) {
  wsh_t *w;
  int rv;
  int fd;
  msg_request_t req;
  w = calloc(1, sizeof(*w));
  ((void) sizeof ((w != ((void *)0)) ? 1 : 0), __extension__ ({ if (w != ((void *)0)) ; else __assert_fail ("w != NULL", "wsh.c", 297, __extension__ __PRETTY_FUNCTION__); }));
  w->argc = argc;
  w->argv = argv;
  rv = wsh__getopt(w);
  if (rv == -1) {
    exit(1);
  }
  if (w->socket_path == ((void *)0)) {
    w->socket_path = "run/wshd.sock";
  }
  rv = un_connect(w->socket_path);
  if (rv < 0) {
    perror("connect");
    exit(255);
  }
  fd = rv;
  msg_request_init(&req);
  if (isatty(0)) {
    req.tty = 1;
  } else {
    req.tty = 0;
  }
  rv = msg_array_import(&req.arg, w->argc, (const char **)w->argv);
  if (rv == -1) {
    fprintf(stderr, "msg_import_array: Too much data\n");
    exit(255);
  }
  rv = msg_rlimit_import(&req.rlim);
  if (rv == -1) {
    fprintf(stderr, "msg_rlimit_import: %s\n", strerror((*__errno_location ())));
    exit(255);
  }
  rv = msg_user_import(&req.user, w->user);
  if (rv == -1) {
    fprintf(stderr, "msg_user_import: %s\n", strerror((*__errno_location ())));
    exit(255);
  }
  rv = un_send_fds(fd, (char *)&req, sizeof(req), ((void *)0), 0);
  if (rv <= 0) {
    perror("sendmsg");
    exit(255);
  }
  if (req.tty) {
    loop_interactive(fd);
  } else {
    loop_noninteractive(fd);
  }
  perror("unreachable");
  exit(255);
}

canonical_original_code:
int main(int argc, char **argv) {
  struct wsh_s *w;
  int rv;
  int fd;
  struct msg_request_s req;
  w = calloc(1, sizeof(*w));
  ((void) sizeof ((w != ((void *)0)) ? 1 : 0), __extension__ ({ if (w != ((void *)0)) ; else __assert_fail ("w != NULL", "wsh.c", 297, __extension__ __PRETTY_FUNCTION__); }));
  w->argc = argc;
  w->argv = argv;
  rv = wsh__getopt(w);
  if (rv == -1) {
    exit(1);
  }
  if (w->socket_path == ((void *)0)) {
    w->socket_path = "run/wshd.sock";
  }
  rv = un_connect(w->socket_path);
  if (rv < 0) {
    perror("connect");
    exit(255);
  }
  fd = rv;
  msg_request_init(&req);
  if (isatty(0)) {
    req.tty = 1;
  } else {
    req.tty = 0;
  }
  rv = msg_array_import(&req.arg, w->argc, (char **)w->argv);
  if (rv == -1) {
    fprintf(stderr, "msg_import_array: Too much data\n");
    exit(255);
  }
  rv = msg_rlimit_import(&req.rlim);
  if (rv == -1) {
    fprintf(stderr, "msg_rlimit_import: %s\n", strerror((*__errno_location ())));
    exit(255);
  }
  rv = msg_user_import(&req.user, w->user);
  if (rv == -1) {
    fprintf(stderr, "msg_user_import: %s\n", strerror((*__errno_location ())));
    exit(255);
  }
  rv = un_send_fds(fd, (char *)&req, sizeof(req), ((void *)0), 0);
  if (rv <= 0) {
    perror("sendmsg");
    exit(255);
  }
  if (req.tty) {
    loop_interactive(fd);
  } else {
    loop_noninteractive(fd);
  }
  perror("unreachable");
  exit(255);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct wsh_s { int argc; char **argv; char *socket_path; char *user; } *

variable_types: struct msg_request_s { int version; int tty; struct msg__array_s { int count; char buf[-1]; } arg; struct msg__rlimit_s { int count; struct { int id; struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; } rlim; } rlim[-1]; } rlim; struct msg__user_s { char name[32]; } user; }

return_type: int

user_defined_types: struct msg_request_s { int version; int tty; struct msg__array_s arg; struct msg__rlimit_s rlim; struct msg__user_s user; }

user_defined_types: struct msg__rlimit_s { int count; struct { int id; struct rlimit rlim; } rlim[-1]; }


decompiled_code:
__int64 __fastcall pump_init(_DWORD *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
memset(a1 + 2, 0, 0x80uLL);
memset(a1 + 34, 0, 0x80uLL);
memset(a1 + 66, 0, 0x80uLL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
__int64 result; // rax

*a1 = 0;
result = 0LL;
memset(a1 + 2, 0, 0x80uLL);
memset(a1 + 34, 0, 0x80uLL);
memset(a1 + 66, 0, 0x80uLL);
return result;
}

variable_types: struct pump_s { int nfd; struct fd_set { long fds_bits[-1]; } rfds; struct fd_set wfds; struct fd_set efds; } *


decompiled_code:
__int64 __fastcall pump_pair_init(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
*(_QWORD *)a1 = a2;
*(_DWORD *)(a1 + 8) = a3;
*(_DWORD *)(a1 + 12) = a4;
fcntl_set_nonblock(a3, 0LL);
return fcntl_set_nonblock(a4, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
*(_QWORD *)a1 = a2;
*(_DWORD *)(a1 + 8) = a3;
*(_DWORD *)(a1 + 12) = a4;
fcntl_set_nonblock(a3, 0LL);
return fcntl_set_nonblock(a4, 0LL);
}

original_code:
void pump_pair_init(pump_pair_t *pp, pump_t *p, int rfd, int wfd) {
  pp->p = p;
  pp->rfd = rfd;
  pp->wfd = wfd;
  fcntl_set_nonblock(rfd, 0);
  fcntl_set_nonblock(wfd, 0);
}

canonical_original_code:
void pump_pair_init(struct pump_pair_s *pp, struct pump_s *p, int rfd, int wfd) {
  pp->p = p;
  pp->rfd = rfd;
  pp->wfd = wfd;
  fcntl_set_nonblock(rfd, 0);
  fcntl_set_nonblock(wfd, 0);
}

variable_types: struct pump_pair_s { struct pump_s { int nfd; struct fd_set { long fds_bits[-1]; } rfds; struct fd_set wfds; struct fd_set efds; } *p; int rfd; int wfd; } *

variable_types:
p
struct pump_s *

variable_types: rfd int

variable_types: wfd int


decompiled_code:
int __fastcall pump_pair_close(__int64 a1)
{
int result; // eax

close(*(_DWORD *)(a1 + 8));
*(_DWORD *)(a1 + 8) = -1;
result = close(*(_DWORD *)(a1 + 12));
*(_DWORD *)(a1 + 12) = -1;
return result;
}

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

close(*(_DWORD *)(a1 + 8));
*(_DWORD *)(a1 + 8) = -1;
result = close(*(_DWORD *)(a1 + 12));
*(_DWORD *)(a1 + 12) = -1;
return result;
}

original_code:
void pump_pair_close(pump_pair_t *pp) {
  close(pp->rfd);
  pp->rfd = -1;
  close(pp->wfd);
  pp->wfd = -1;
}

canonical_original_code:
void pump_pair_close(struct pump_pair_s *pp) {
  close(pp->rfd);
  pp->rfd = -1;
  close(pp->wfd);
  pp->wfd = -1;
}


original_code:
int pump_add_fd(pump_t *p, int fd, int mode) {
  if (fd < 0) {
    return 0;
  }
  if (fd > p->nfd) {
    p->nfd = fd;
  }
  if (mode & 1) {
    ((void) (((&p->rfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if (mode & 2) {
    ((void) (((&p->wfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if (mode & 4) {
    ((void) (((&p->efds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  return 1;
}

canonical_original_code:
int pump_add_fd(struct pump_s *p, int fd, int mode) {
  if (fd < 0) {
    return 0;
  }
  if (fd > p->nfd) {
    p->nfd = fd;
  }
  if (mode & 1) {
    ((void) (((&p->rfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if (mode & 2) {
    ((void) (((&p->wfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  if (mode & 4) {
    ((void) (((&p->efds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
  }
  return 1;
}

variable_types: fd int

variable_types: mode int


decompiled_code:
__int64 __fastcall pump_add_pair(int *a1, __int64 a2)
{
int v2; // ebx

v2 = pump_add_fd(a1, *(_DWORD *)(a2 + 8), 5);
return v2 + (unsigned int)pump_add_fd(a1, *(_DWORD *)(a2 + 12), 6);
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1, __int64 a2)
{
int v2; // ebx

v2 = func3(a1, *(_DWORD *)(a2 + 8), 5);
return v2 + (unsigned int)func3(a1, *(_DWORD *)(a2 + 12), 6);
}

original_code:
int pump_add_pair(pump_t *p, pump_pair_t *pp) {
  int j = 0;
  j += pump_add_fd(p, pp->rfd, 1 | 4);
  j += pump_add_fd(p, pp->wfd, 2 | 4);
  return j;
}

canonical_original_code:
int pump_add_pair(struct pump_s *p, struct pump_pair_s *pp) {
  int j = 0;
  j += pump_add_fd(p, pp->rfd, 1 | 4);
  j += pump_add_fd(p, pp->wfd, 2 | 4);
  return j;
}

variable_types: struct pump_pair_s { struct pump_s *p; int rfd; int wfd; } *

variable_types: j int


canonical_original_code:
int pump_ready(struct pump_s *p, int fd, int mode) {
  int rv = 0;
  if (mode & 1) {
    rv |= ((((&p->rfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0);
  }
  if (mode & 2) {
    rv |= ((((&p->wfds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0);
  }
  if (mode & 4) {
    rv |= ((((&p->efds)->fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0);
  }
  return rv;
}

variable_types: rv int


decompiled_code:
int __fastcall pump_select(__int64 a1)
{
return select(1024, (fd_set *)(a1 + 8), 0LL, (fd_set *)(a1 + 264), 0LL);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
return select(1024, (fd_set *)(a1 + 8), 0LL, (fd_set *)(a1 + 264), 0LL);
}

original_code:
int pump_select(pump_t *p) {
  return select(1024, &p->rfds, ((void *)0), &p->efds, ((void *)0));
}

canonical_original_code:
int pump_select(struct pump_s *p) {
  return select(1024, &p->rfds, ((void *)0), &p->efds, ((void *)0));
}


original_code:
int pump_pair_splice(pump_pair_t *pp) {
  int rv;
  if (!((((&pp->p->rfds)->fds_bits)[((pp->rfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->rfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  if (!((((&pp->p->wfds)->fds_bits)[((pp->wfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->wfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  rv = splice(pp->rfd, ((void *)0), pp->wfd, ((void *)0), 64 * 1024, 2);
  if (rv == -1) {
    perror("splice");
    exit(1);
  } else if (rv == 0) {
    pump_pair_close(pp);
  }
  return rv;
}

canonical_original_code:
int pump_pair_splice(struct pump_pair_s *pp) {
  int rv;
  if (!((((&pp->p->rfds)->fds_bits)[((pp->rfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->rfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  if (!((((&pp->p->wfds)->fds_bits)[((pp->wfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->wfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  rv = splice(pp->rfd, ((void *)0), pp->wfd, ((void *)0), 64 * 1024, 2);
  if (rv == -1) {
    perror("splice");
    exit(1);
  } else if (rv == 0) {
    pump_pair_close(pp);
  }
  return rv;
}


decompiled_code:
__int64 __fastcall pump_pair_copy(__int64 a1)
{
unsigned int v1; // er13
int v2; // eax
_BYTE *v3; // rbp
int v4; // eax
int v5; // er14
_BYTE v7[65576]; // [rsp+0h] [rbp-10028h] BYREF

v1 = 0;
if ( (*(_QWORD *)(*(_QWORD *)a1 + 8LL * (*(_DWORD *)(a1 + 8) / 64) + 8) & (1LL << (*(_DWORD *)(a1 + 8) % 64))) != 0 )
{
while ( 1 )
{
v2 = read(*(_DWORD *)(a1 + 8), v7, 0x10000uLL);
v1 = v2;
if ( v2 != -1 )
break;
if ( *_errno_location() != 4 )
goto LABEL_4;
}
if ( v2 <= 0 )
{
LABEL_4:
pump_pair_close(a1);
return v1;
}
v3 = v7;
do
{
while ( 1 )
{
v4 = write(*(_DWORD *)(a1 + 12), v3, (int)v1);
v5 = v4;
if ( v4 == -1 )
break;
if ( v4 <= 0 )
goto LABEL_11;
v3 += v4;
v1 -= v4;
if ( !v1 )
return v1;
}
}
while ( *_errno_location() == 4 );
LABEL_11:
pump_pair_close(a1);
v1 = v5;
}
return v1;
}

original_code:
int pump_pair_copy(pump_pair_t *pp) {
  char buf[64 * 1024];
  char *ptr = buf;
  int nr, nw;
  if (!((((&pp->p->rfds)->fds_bits)[((pp->rfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->rfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  do {
    nr = read(pp->rfd, buf, sizeof(buf));
  } while (nr == -1 && (*__errno_location ()) == 4);
  if (nr <= 0) {
    pump_pair_close(pp);
    return nr;
  }
  while (nr) {
    do {
      nw = write(pp->wfd, ptr, nr);
    } while (nw == -1 && (*__errno_location ()) == 4);
    if (nw <= 0) {
      pump_pair_close(pp);
      return nw;
    }
    ptr += nw;
    nr -= nw;
  }
  return 0;
}

canonical_original_code:
int pump_pair_copy(struct pump_pair_s *pp) {
  char buf[-1];
  char *ptr = buf;
  int nr; int nw;
  if (!((((&pp->p->rfds)->fds_bits)[((pp->rfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((pp->rfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    return 0;
  }
  do {
    nr = read(pp->rfd, buf, sizeof(buf));
  } while (nr == -1 && (*__errno_location ()) == 4);
  if (nr <= 0) {
    pump_pair_close(pp);
    return nr;
  }
  while (nr) {
    do {
      nw = write(pp->wfd, ptr, nr);
    } while (nw == -1 && (*__errno_location ()) == 4);
    if (nw <= 0) {
      pump_pair_close(pp);
      return nw;
    }
    ptr += nw;
    nr -= nw;
  }
  return 0;
}

variable_types:
buf
char [-1]

variable_types:
ptr
char *

variable_types: nr int

variable_types: nw int


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

result = socket(1, 1, 0);
if ( result == -1 )
{
perror("socket");
exit(1);
}
return result;
}

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

result = socket(1, 1, 0);
if ( result == -1 )
{
perror("socket");
exit(1);
}
return result;
}

original_code:
int un__socket() {
  int fd;
  if ((fd = socket(1, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  return fd;
}


decompiled_code:
__int64 __fastcall un_listen(char *src)
{
unsigned int v1; // ebx
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = un__socket();
v3[0].sa_family = 1;
if ( strlen(src) > 0x6B )
_assert_fail("strlen(path) < sizeof(sa.sun_path)", "un.c", 0x20u, "un_listen");
strcpy(v3[0].sa_data, src);
unlink(v3[0].sa_data);
if ( bind(v1, v3, 0x6Eu) == -1 )
{
perror("bind");
exit(1);
}
if ( listen(v1, 5) == -1 )
{
perror("listen");
exit(1);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src)
{
unsigned int v1; // ebx
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = func0();
v3[0].sa_family = 1;
if ( strlen(src) > 0x6B )
_assert_fail("strlen(path) < sizeof(sa.sun_path)", "un.c", 0x20u, "un_listen");
strcpy(v3[0].sa_data, src);
unlink(v3[0].sa_data);
if ( bind(v1, v3, 0x6Eu) == -1 )
{
perror("bind");
exit(1);
}
if ( listen(v1, 5) == -1 )
{
perror("listen");
exit(1);
}
return v1;
}

original_code:
int un_listen(const char *path) {
  int fd;
  struct sockaddr_un sa;
  fd = un__socket();
  sa.sun_family = 1;
  ((void) sizeof ((strlen(path) < sizeof(sa.sun_path)) ? 1 : 0), __extension__ ({ if (strlen(path) < sizeof(sa.sun_path)) ; else __assert_fail ("strlen(path) < sizeof(sa.sun_path)", "un.c", 32, __extension__ __PRETTY_FUNCTION__); }));
  strcpy(sa.sun_path, path);
  unlink(sa.sun_path);
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(fd, 5) == -1) {
    perror("listen");
    exit(1);
  }
  return fd;
}

canonical_original_code:
int un_listen(char *path) {
  int fd;
  struct sockaddr_un sa;
  fd = un__socket();
  sa.sun_family = 1;
  ((void) sizeof ((strlen(path) < sizeof(sa.sun_path)) ? 1 : 0), __extension__ ({ if (strlen(path) < sizeof(sa.sun_path)) ; else __assert_fail ("strlen(path) < sizeof(sa.sun_path)", "un.c", 32, __extension__ __PRETTY_FUNCTION__); }));
  strcpy(sa.sun_path, path);
  unlink(sa.sun_path);
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(fd, 5) == -1) {
    perror("listen");
    exit(1);
  }
  return fd;
}

variable_types:
path
char *

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

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


decompiled_code:
__int64 __fastcall un_connect(char *src)
{
unsigned int v1; // ebx
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = un__socket();
v3[0].sa_family = 1;
if ( strlen(src) > 0x6B )
_assert_fail("strlen(path) < sizeof(sa.sun_path)", "un.c", 0x39u, "un_connect");
strcpy(v3[0].sa_data, src);
if ( connect(v1, v3, 0x6Eu) == -1 )
{
close(v1);
v1 = -1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *src)
{
unsigned int v1; // ebx
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

v1 = func0();
v3[0].sa_family = 1;
if ( strlen(src) > 0x6B )
_assert_fail("strlen(path) < sizeof(sa.sun_path)", "un.c", 0x39u, "un_connect");
strcpy(v3[0].sa_data, src);
if ( connect(v1, v3, 0x6Eu) == -1 )
{
close(v1);
v1 = -1;
}
return v1;
}

original_code:
int un_connect(const char *path) {
  int fd;
  struct sockaddr_un sa;
  int rv;
  fd = un__socket();
  sa.sun_family = 1;
  ((void) sizeof ((strlen(path) < sizeof(sa.sun_path)) ? 1 : 0), __extension__ ({ if (strlen(path) < sizeof(sa.sun_path)) ; else __assert_fail ("strlen(path) < sizeof(sa.sun_path)", "un.c", 57, __extension__ __PRETTY_FUNCTION__); }));
  strcpy(sa.sun_path, path);
  rv = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
  if (rv == -1) {
    close(fd);
    return rv;
  }
  return fd;
}

canonical_original_code:
int un_connect(char *path) {
  int fd;
  struct sockaddr_un sa;
  int rv;
  fd = un__socket();
  sa.sun_family = 1;
  ((void) sizeof ((strlen(path) < sizeof(sa.sun_path)) ? 1 : 0), __extension__ ({ if (strlen(path) < sizeof(sa.sun_path)) ; else __assert_fail ("strlen(path) < sizeof(sa.sun_path)", "un.c", 57, __extension__ __PRETTY_FUNCTION__); }));
  strcpy(sa.sun_path, path);
  rv = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
  if (rv == -1) {
    close(fd);
    return rv;
  }
  return fd;
}

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall un_send_fds(int fd, __int64 a2, int a3, const void *a4, int a5)
{
unsigned __int64 v6; // rax
char *v7; // rdi
size_t v8; // rbp
unsigned int v9; // ebp
__int64 v11[2]; // [rsp+0h] [rbp-868h] BYREF
char v12[2048]; // [rsp+10h] [rbp-858h] BYREF
struct msghdr message; // [rsp+810h] [rbp-58h] BYREF

v6 = ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16;
if ( v6 > 0x800 )
_assert_fail("sizeof(buf) >= buflen", "un.c", 0x4Cu, "un_send_fds");
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
message.msg_controllen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_control = v12;
message.msg_iov = (iovec *)v11;
message.msg_iovlen = 1LL;
v11[0] = a2;
v11[1] = a3;
v7 = v12;
if ( v6 <= 0xF )
v7 = 0LL;
*((_DWORD *)v7 + 2) = 1;
*((_DWORD *)v7 + 3) = 1;
v8 = 4LL * a5 + 16;
*(_QWORD *)v7 = v8;
memcpy(v7 + 16, a4, 4LL * a5);
message.msg_controllen = v8;
do
v9 = sendmsg(fd, &message, 0);
while ( v9 == -1 && *_errno_location() == 4 );
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 a2, int a3, const void *a4, int a5)
{
unsigned __int64 v6; // rax
char *v7; // rdi
size_t v8; // rbp
unsigned int v9; // ebp
__int64 v11[2]; // [rsp+0h] [rbp-868h] BYREF
char v12[2048]; // [rsp+10h] [rbp-858h] BYREF
struct msghdr message; // [rsp+810h] [rbp-58h] BYREF

v6 = ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16;
if ( v6 > 0x800 )
_assert_fail("sizeof(buf) >= buflen", "un.c", 0x4Cu, "un_send_fds");
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
message.msg_controllen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_control = v12;
message.msg_iov = (iovec *)v11;
message.msg_iovlen = 1LL;
v11[0] = a2;
v11[1] = a3;
v7 = v12;
if ( v6 <= 0xF )
v7 = 0LL;
*((_DWORD *)v7 + 2) = 1;
*((_DWORD *)v7 + 3) = 1;
v8 = 4LL * a5 + 16;
*(_QWORD *)v7 = v8;
memcpy(v7 + 16, a4, 4LL * a5);
message.msg_controllen = v8;
do
v9 = sendmsg(fd, &message, 0);
while ( v9 == -1 && *_errno_location() == 4 );
return v9;
}

original_code:
int un_send_fds(int fd, char *data, int datalen, int *fds, int fdslen) {
  struct msghdr mh;
  struct cmsghdr *cmh = ((void *)0);
  char buf[2048];
  size_t buflen = ((((sizeof(int) * fdslen) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  struct iovec iov[1];
  ((void) sizeof ((sizeof(buf) >= buflen) ? 1 : 0), __extension__ ({ if (sizeof(buf) >= buflen) ; else __assert_fail ("sizeof(buf) >= buflen", "un.c", 76, __extension__ __PRETTY_FUNCTION__); }));
  memset(&mh, 0, sizeof(mh));
  mh.msg_control = buf;
  mh.msg_controllen = buflen;
  mh.msg_iov = iov;
  mh.msg_iovlen = 1;
  iov[0].iov_base = data;
  iov[0].iov_len = datalen;
  cmh = ((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0);
  cmh->cmsg_level = 1;
  cmh->cmsg_type = SCM_RIGHTS;
  cmh->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int) * fdslen));
  memcpy(((cmh)->__cmsg_data), fds, sizeof(int) * fdslen);
  mh.msg_controllen = cmh->cmsg_len;
  int rv;
  do {
    rv = sendmsg(fd, &mh, 0);
  } while (rv == -1 && (*__errno_location ()) == 4);
  return rv;
}

canonical_original_code:
int un_send_fds(int fd, char *data, int datalen, int *fds, int fdslen) {
  struct msghdr mh;
  struct cmsghdr *cmh = ((void *)0);
  char buf[2048];
  unsigned long buflen = ((((sizeof(int) * fdslen) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  struct iovec iov[1];
  ((void) sizeof ((sizeof(buf) >= buflen) ? 1 : 0), __extension__ ({ if (sizeof(buf) >= buflen) ; else __assert_fail ("sizeof(buf) >= buflen", "un.c", 76, __extension__ __PRETTY_FUNCTION__); }));
  memset(&mh, 0, sizeof(mh));
  mh.msg_control = buf;
  mh.msg_controllen = buflen;
  mh.msg_iov = iov;
  mh.msg_iovlen = 1;
  iov[0].iov_base = data;
  iov[0].iov_len = datalen;
  cmh = ((unsigned long) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0);
  cmh->cmsg_level = 1;
  cmh->cmsg_type = SCM_RIGHTS;
  cmh->cmsg_len = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int) * fdslen));
  memcpy(((cmh)->__cmsg_data), fds, sizeof(int) * fdslen);
  mh.msg_controllen = cmh->cmsg_len;
  int rv;
  do {
    rv = sendmsg(fd, &mh, 0);
  } while (rv == -1 && (*__errno_location ()) == 4);
  return rv;
}

variable_types:
data
char *

variable_types: datalen int

variable_types:
fds
int *

variable_types: fdslen int

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types:
buf
char [2048]

variable_types:
buflen
unsigned long

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } [1]

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }


decompiled_code:
__int64 __fastcall un_recv_fds(int fd, __int64 a2, int a3, __int64 a4, int a5)
{
__int64 v5; // r13
int v8; // eax
unsigned int v9; // er14
int v10; // eax
_DWORD *v12; // rcx
__int64 v13; // rax
__int64 v14[2]; // [rsp+0h] [rbp-878h] BYREF
char v15; // [rsp+10h] [rbp-868h] BYREF
struct msghdr message; // [rsp+810h] [rbp-68h] BYREF

v5 = 4LL * a5 + 8;
if ( ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16 > 0x800 )
_assert_fail("sizeof(buf) >= buflen", "un.c", 0x6Eu, "un_recv_fds");
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_control = &v15;
message.msg_controllen = ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16;
message.msg_iov = (iovec *)v14;
message.msg_iovlen = 1LL;
v14[0] = a2;
v14[1] = a3;
while ( 1 )
{
v8 = recvmsg(fd, &message, 0);
v9 = v8;
if ( v8 != -1 )
break;
v10 = *_errno_location();
if ( v10 != 4 && v10 != 11 )
return v9;
}
if ( a4 && v8 > 0 )
{
if ( message.msg_controllen <= 0xF || (v12 = message.msg_control) == 0LL )
_assert_fail("cmh != NULL", "un.c", 0x85u, "un_recv_fds");
if ( *((_DWORD *)message.msg_control + 2) != 1 )
_assert_fail("cmh->cmsg_level == SOL_SOCKET", "un.c", 0x86u, "un_recv_fds");
if ( *((_DWORD *)message.msg_control + 3) != 1 )
_assert_fail("cmh->cmsg_type == SCM_RIGHTS", "un.c", 0x87u, "un_recv_fds");
if ( *(_QWORD *)message.msg_control != v5 + 8 )
_assert_fail("cmh->cmsg_len == CMSG_LEN(sizeof(int) * fdslen)", "un.c", 0x88u, "un_recv_fds");
if ( a5 > 0 )
{
v13 = 0LL;
do
{
*(_DWORD *)(a4 + 4 * v13) = v12[v13 + 4];
++v13;
}
while ( v13 != a5 );
}
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, __int64 a2, int a3, __int64 a4, int a5)
{
__int64 v5; // r13
int v8; // eax
unsigned int v9; // er14
int v10; // eax
_DWORD *v12; // rcx
__int64 v13; // rax
__int64 v14[2]; // [rsp+0h] [rbp-878h] BYREF
char v15; // [rsp+10h] [rbp-868h] BYREF
struct msghdr message; // [rsp+810h] [rbp-68h] BYREF

v5 = 4LL * a5 + 8;
if ( ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16 > 0x800 )
_assert_fail("sizeof(buf) >= buflen", "un.c", 0x6Eu, "un_recv_fds");
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_control = &v15;
message.msg_controllen = ((4LL * a5 + 7) & 0xFFFFFFFFFFFFFFF8LL) + 16;
message.msg_iov = (iovec *)v14;
message.msg_iovlen = 1LL;
v14[0] = a2;
v14[1] = a3;
while ( 1 )
{
v8 = recvmsg(fd, &message, 0);
v9 = v8;
if ( v8 != -1 )
break;
v10 = *_errno_location();
if ( v10 != 4 && v10 != 11 )
return v9;
}
if ( a4 && v8 > 0 )
{
if ( message.msg_controllen <= 0xF || (v12 = message.msg_control) == 0LL )
_assert_fail("cmh != NULL", "un.c", 0x85u, "un_recv_fds");
if ( *((_DWORD *)message.msg_control + 2) != 1 )
_assert_fail("cmh->cmsg_level == SOL_SOCKET", "un.c", 0x86u, "un_recv_fds");
if ( *((_DWORD *)message.msg_control + 3) != 1 )
_assert_fail("cmh->cmsg_type == SCM_RIGHTS", "un.c", 0x87u, "un_recv_fds");
if ( *(_QWORD *)message.msg_control != v5 + 8 )
_assert_fail("cmh->cmsg_len == CMSG_LEN(sizeof(int) * fdslen)", "un.c", 0x88u, "un_recv_fds");
if ( a5 > 0 )
{
v13 = 0LL;
do
{
*(_DWORD *)(a4 + 4 * v13) = v12[v13 + 4];
++v13;
}
while ( v13 != a5 );
}
}
return v9;
}

original_code:
int un_recv_fds(int fd, char *data, int datalen, int *fds, int fdslen) {
  struct msghdr mh;
  struct cmsghdr *cmh = ((void *)0);
  char buf[2048];
  size_t buflen = ((((sizeof(int) * fdslen) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  struct iovec iov[1];
  ((void) sizeof ((sizeof(buf) >= buflen) ? 1 : 0), __extension__ ({ if (sizeof(buf) >= buflen) ; else __assert_fail ("sizeof(buf) >= buflen", "un.c", 110, __extension__ __PRETTY_FUNCTION__); }));
  memset(&mh, 0, sizeof(mh));
  mh.msg_control = buf;
  mh.msg_controllen = buflen;
  mh.msg_iov = iov;
  mh.msg_iovlen = 1;
  iov[0].iov_base = data;
  iov[0].iov_len = datalen;
  int rv;
  do {
    rv = recvmsg(fd, &mh, 0);
  } while (rv == -1 && ((*__errno_location ()) == 4 || (*__errno_location ()) == 11));
  if (rv <= 0) {
    goto done;
  }
  if (fds != ((void *)0)) {
    cmh = ((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0);
    ((void) sizeof ((cmh != ((void *)0)) ? 1 : 0), __extension__ ({ if (cmh != ((void *)0)) ; else __assert_fail ("cmh != NULL", "un.c", 133, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_level == 1) ? 1 : 0), __extension__ ({ if (cmh->cmsg_level == 1) ; else __assert_fail ("cmh->cmsg_level == SOL_SOCKET", "un.c", 134, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_type == SCM_RIGHTS) ? 1 : 0), __extension__ ({ if (cmh->cmsg_type == SCM_RIGHTS) ; else __assert_fail ("cmh->cmsg_type == SCM_RIGHTS", "un.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int) * fdslen))) ? 1 : 0), __extension__ ({ if (cmh->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(int) * fdslen))) ; else __assert_fail ("cmh->cmsg_len == CMSG_LEN(sizeof(int) * fdslen)", "un.c", 136, __extension__ __PRETTY_FUNCTION__); }));
    int *fds_ = (int *)((cmh)->__cmsg_data);
    int i;
    for (i = 0; i < fdslen; i++) {
      fds[i] = fds_[i];
    }
  }
done:
  return rv;
}

canonical_original_code:
int un_recv_fds(int fd, char *data, int datalen, int *fds, int fdslen) {
  struct msghdr mh;
  struct cmsghdr *cmh = ((void *)0);
  char buf[2048];
  unsigned long buflen = ((((sizeof(int) * fdslen) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)));
  struct iovec iov[1];
  ((void) sizeof ((sizeof(buf) >= buflen) ? 1 : 0), __extension__ ({ if (sizeof(buf) >= buflen) ; else __assert_fail ("sizeof(buf) >= buflen", "un.c", 110, __extension__ __PRETTY_FUNCTION__); }));
  memset(&mh, 0, sizeof(mh));
  mh.msg_control = buf;
  mh.msg_controllen = buflen;
  mh.msg_iov = iov;
  mh.msg_iovlen = 1;
  iov[0].iov_base = data;
  iov[0].iov_len = datalen;
  int rv;
  do {
    rv = recvmsg(fd, &mh, 0);
  } while (rv == -1 && ((*__errno_location ()) == 4 || (*__errno_location ()) == 11));
  if (rv <= 0) {
    goto done;
  }
  if (fds != ((void *)0)) {
    cmh = ((unsigned long) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0);
    ((void) sizeof ((cmh != ((void *)0)) ? 1 : 0), __extension__ ({ if (cmh != ((void *)0)) ; else __assert_fail ("cmh != NULL", "un.c", 133, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_level == 1) ? 1 : 0), __extension__ ({ if (cmh->cmsg_level == 1) ; else __assert_fail ("cmh->cmsg_level == SOL_SOCKET", "un.c", 134, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_type == SCM_RIGHTS) ? 1 : 0), __extension__ ({ if (cmh->cmsg_type == SCM_RIGHTS) ; else __assert_fail ("cmh->cmsg_type == SCM_RIGHTS", "un.c", 135, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((cmh->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int) * fdslen))) ? 1 : 0), __extension__ ({ if (cmh->cmsg_len == ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (sizeof(int) * fdslen))) ; else __assert_fail ("cmh->cmsg_len == CMSG_LEN(sizeof(int) * fdslen)", "un.c", 136, __extension__ __PRETTY_FUNCTION__); }));
    int *fds_ = (int *)((cmh)->__cmsg_data);
    int i;
    for (i = 0; i < fdslen; i++) {
      fds[i] = fds_[i];
    }
  }
done:
  return rv;
}

variable_types: fd int

variable_types: i int

user_defined_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; }

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }


decompiled_code:
__int64 __fastcall barrier_open(_DWORD *a1)
{
unsigned int v1; // ebx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

pipedes[0] = -1;
pipedes[1] = -1;
if ( pipe(pipedes) == -1 )
{
v1 = -1;
perror("pipe");
if ( pipedes[0] >= 0 )
close(pipedes[0]);
if ( pipedes[1] >= 0 )
close(pipedes[1]);
}
else
{
*a1 = pipedes[0];
a1[1] = pipedes[1];
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
unsigned int v1; // ebx
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

pipedes[0] = -1;
pipedes[1] = -1;
if ( pipe(pipedes) == -1 )
{
v1 = -1;
perror("pipe");
if ( pipedes[0] >= 0 )
close(pipedes[0]);
if ( pipedes[1] >= 0 )
close(pipedes[1]);
}
else
{
*a1 = pipedes[0];
a1[1] = pipedes[1];
v1 = 0;
}
return v1;
}

original_code:
int barrier_open(barrier_t *bar) {
  int rv;
  int aux[2] = { -1, -1 };
  rv = pipe(aux);
  if (rv == -1) {
    perror("pipe");
    goto err;
  }
  bar->fd[0] = aux[0];
  bar->fd[1] = aux[1];
  return 0;
err:
  if (aux[0] >= 0) close(aux[0]);
  if (aux[1] >= 0) close(aux[1]);
  return -1;
}

canonical_original_code:
int barrier_open(struct barrier_s *bar) {
  int rv;
  int aux[2] = { -1, -1 };
  rv = pipe(aux);
  if (rv == -1) {
    perror("pipe");
    goto err;
  }
  bar->fd[0] = aux[0];
  bar->fd[1] = aux[1];
  return 0;
err:
  if (aux[0] >= 0) close(aux[0]);
  if (aux[1] >= 0) close(aux[1]);
  return -1;
}

variable_types:
bar
struct barrier_s { int fd[2]; } *

variable_types: rv int

variable_types:
aux
int [2]


decompiled_code:
int __fastcall barrier_close(int *a1)
{
close(*a1);
return close(a1[1]);
}

canonical_decompiled_code:
int __fastcall func1(int *a1)
{
close(*a1);
return close(a1[1]);
}

original_code:
void barrier_close(barrier_t *bar) {
  close(bar->fd[0]);
  close(bar->fd[1]);
}

canonical_original_code:
void barrier_close(struct barrier_s *bar) {
  close(bar->fd[0]);
  close(bar->fd[1]);
}


decompiled_code:
__int64 __fastcall barrier_mix_cloexec(unsigned int *a1)
{
fcntl_set_cloexec(*a1, 1LL);
return fcntl_set_cloexec(a1[1], 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1)
{
fcntl_set_cloexec(*a1, 1LL);
return fcntl_set_cloexec(a1[1], 1LL);
}

original_code:
void barrier_mix_cloexec(barrier_t *bar) {
  fcntl_set_cloexec((bar->fd[0]), 1);
  fcntl_set_cloexec((bar->fd[1]), 1);
}

canonical_original_code:
void barrier_mix_cloexec(struct barrier_s *bar) {
  fcntl_set_cloexec((bar->fd[0]), 1);
  fcntl_set_cloexec((bar->fd[1]), 1);
}


decompiled_code:
int __fastcall barrier_close_wait(int *a1)
{
int result; // eax

result = close(*a1);
*a1 = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func3(int *a1)
{
int result; // eax

result = close(*a1);
*a1 = -1;
return result;
}

original_code:
void barrier_close_wait(barrier_t *bar) {
  close(bar->fd[0]);
  bar->fd[0] = -1;
}

canonical_original_code:
void barrier_close_wait(struct barrier_s *bar) {
  close(bar->fd[0]);
  bar->fd[0] = -1;
}


decompiled_code:
int __fastcall barrier_close_signal(__int64 a1)
{
int result; // eax

result = close(*(_DWORD *)(a1 + 4));
*(_DWORD *)(a1 + 4) = -1;
return result;
}

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

result = close(*(_DWORD *)(a1 + 4));
*(_DWORD *)(a1 + 4) = -1;
return result;
}

original_code:
void barrier_close_signal(barrier_t *bar) {
  close(bar->fd[1]);
  bar->fd[1] = -1;
}

canonical_original_code:
void barrier_close_signal(struct barrier_s *bar) {
  close(bar->fd[1]);
  bar->fd[1] = -1;
}


decompiled_code:
__int64 __fastcall barrier_wait(int *a1)
{
unsigned int v1; // ebp
unsigned int v2; // er12
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

barrier_close_signal((__int64)a1);
v1 = read(*a1, buf, 1uLL);
v2 = v1;
barrier_close_wait(a1);
if ( v1 != -1 )
return (unsigned int)-(v1 == 0);
perror("read");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1)
{
unsigned int v1; // ebp
unsigned int v2; // er12
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

func4((__int64)a1);
v1 = read(*a1, buf, 1uLL);
v2 = v1;
func3(a1);
if ( v1 != -1 )
return (unsigned int)-(v1 == 0);
perror("read");
return v2;
}

original_code:
int barrier_wait(barrier_t *bar) {
  int nread;
  char buf[1];
  barrier_close_signal(bar);
  nread = read(bar->fd[0], buf, sizeof(buf));
  barrier_close_wait(bar);
  if (nread == -1) {
    perror("read");
    return -1;
  } else if (nread == 0) {
    return -1;
  }
  return 0;
}

canonical_original_code:
int barrier_wait(struct barrier_s *bar) {
  int nread;
  char buf[1];
  barrier_close_signal(bar);
  nread = read(bar->fd[0], buf, sizeof(buf));
  barrier_close_wait(bar);
  if (nread == -1) {
    perror("read");
    return -1;
  } else if (nread == 0) {
    return -1;
  }
  return 0;
}

variable_types: nread int

variable_types:
buf
char [1]


decompiled_code:
__int64 __fastcall barrier_signal(int *a1)
{
unsigned int v1; // er12
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

buf[0] = 0;
barrier_close_wait(a1);
v1 = write(a1[1], buf, 1uLL);
barrier_close_signal((__int64)a1);
if ( v1 != -1 )
return 0;
perror("write");
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(int *a1)
{
unsigned int v1; // er12
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

buf[0] = 0;
func3(a1);
v1 = write(a1[1], buf, 1uLL);
func4((__int64)a1);
if ( v1 != -1 )
return 0;
perror("write");
return v1;
}

original_code:
int barrier_signal(barrier_t *bar) {
  int nwritten;
  char byte = '\0';
  barrier_close_wait(bar);
  nwritten = write(bar->fd[1], &byte, 1);
  barrier_close_signal(bar);
  if (nwritten == -1) {
    perror("write");
    return -1;
  }
  return 0;
}

canonical_original_code:
int barrier_signal(struct barrier_s *bar) {
  int nwritten;
  char byte = '\0';
  barrier_close_wait(bar);
  nwritten = write(bar->fd[1], &byte, 1);
  barrier_close_signal(bar);
  if (nwritten == -1) {
    perror("write");
    return -1;
  }
  return 0;
}

variable_types: nwritten int

variable_types: byte char


decompiled_code:
__int64 __fastcall mount__compare_proc_mounts(const void *a1, const void *a2)
{
return (unsigned int)-strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)-strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
static int mount__compare_proc_mounts(const void *a, const void *b) {
  return -strcmp(*((char**)a), *((char**)b));
}

canonical_original_code:
static int mount__compare_proc_mounts(void *a, void *b) {
  return -strcmp(*((char **)a), *((char **)b));
}

variable_types:
a
void *

variable_types:
b
void *


decompiled_code:
void __fastcall mount__filter_proc_mounts(_QWORD *a1, const char *a2)
{
unsigned __int64 v2; // rbx
size_t v3; // rax

if ( a1[1] )
{
v2 = 0LL;
do
{
if ( v2 && !strcmp(*(const char **)(*a1 + 8 * v2), *(const char **)(*a1 + 8 * v2 - 8))
|| (v3 = strlen(a2), strncmp(*(const char **)(*a1 + 8 * v2), a2, v3)) )
{
mount__remove_line((__int64)a1, v2--);
}
++v2;
}
while ( a1[1] > v2 );
}
}

canonical_decompiled_code:
void __fastcall func2(_QWORD *a1, const char *a2)
{
unsigned __int64 v2; // rbx
size_t v3; // rax

if ( a1[1] )
{
v2 = 0LL;
do
{
if ( v2 && !strcmp(*(const char **)(*a1 + 8 * v2), *(const char **)(*a1 + 8 * v2 - 8))
|| (v3 = strlen(a2), strncmp(*(const char **)(*a1 + 8 * v2), a2, v3)) )
{
func1((__int64)a1, v2--);
}
++v2;
}
while ( a1[1] > v2 );
}
}

original_code:
static void mount__filter_proc_mounts(mount_lines_t *dst, const char *prefix) {
  size_t i;
  for (i = 0; i < dst->mount_len; i++) {
    if (i > 0 && strcmp(dst->mount_lines[i], dst->mount_lines[i - 1]) == 0) {
      mount__remove_line(dst, i);
      i--;
      continue;
    }
    if (strncmp(dst->mount_lines[i], prefix, strlen(prefix)) != 0) {
      mount__remove_line(dst, i);
      i--;
      continue;
    }
  }
}

canonical_original_code:
static void mount__filter_proc_mounts(struct mount_lines_s *dst, char *prefix) {
  unsigned long i;
  for (i = 0; i < dst->mount_len; i++) {
    if (i > 0 && strcmp(dst->mount_lines[i], dst->mount_lines[i - 1]) == 0) {
      mount__remove_line(dst, i);
      i--;
      continue;
    }
    if (strncmp(dst->mount_lines[i], prefix, strlen(prefix)) != 0) {
      mount__remove_line(dst, i);
      i--;
      continue;
    }
  }
}

variable_types: struct mount_lines_s { char **mount_lines; unsigned long mount_len; } *

variable_types:
prefix
char *

variable_types:
i
unsigned long


decompiled_code:
void __fastcall mount__umount_paths_for_pivoted_root(void **a1, const char *a2)
{
char v2[4120]; // [rsp+0h] [rbp-1018h] BYREF

if ( (unsigned int)snprintf(v2, 0x1000uLL, "%s/proc/mounts", a2) > 0xFFF )
_assert_fail("rv < sizeof(proc_mounts_path)", "mount.c", 0x70u, "mount__umount_paths_for_pivoted_root");
mount__load_proc_mounts(a1, v2);
qsort(*a1, (size_t)a1[1], 8uLL, (__compar_fn_t)mount__compare_proc_mounts);
mount__filter_proc_mounts(a1, a2);
}

canonical_decompiled_code:
void __fastcall func3(void **a1, const char *a2)
{
char v2[4120]; // [rsp+0h] [rbp-1018h] BYREF

if ( (unsigned int)snprintf(v2, 0x1000uLL, "%s/proc/mounts", a2) > 0xFFF )
_assert_fail("rv < sizeof(proc_mounts_path)", "mount.c", 0x70u, "mount__umount_paths_for_pivoted_root");
mount__load_proc_mounts(a1, v2);
qsort(*a1, (size_t)a1[1], 8uLL, (__compar_fn_t)mount__compare_proc_mounts);
func2(a1, a2);
}

original_code:
static void mount__umount_paths_for_pivoted_root(mount_lines_t *dst, const char *path) {
  char proc_mounts_path[4096];
  int rv;
  rv = snprintf(proc_mounts_path, sizeof(proc_mounts_path), "%s/proc/mounts", path);
  ((void) sizeof ((rv < sizeof(proc_mounts_path)) ? 1 : 0), __extension__ ({ if (rv < sizeof(proc_mounts_path)) ; else __assert_fail ("rv < sizeof(proc_mounts_path)", "mount.c", 112, __extension__ __PRETTY_FUNCTION__); }));
  mount__load_proc_mounts(dst, proc_mounts_path);
  qsort(dst->mount_lines, dst->mount_len, sizeof(char*), &mount__compare_proc_mounts);
  mount__filter_proc_mounts(dst, path);
}

canonical_original_code:
static void mount__umount_paths_for_pivoted_root(struct mount_lines_s *dst, char *path) {
  char proc_mounts_path[4096];
  int rv;
  rv = snprintf(proc_mounts_path, sizeof(proc_mounts_path), "%s/proc/mounts", path);
  ((void) sizeof ((rv < sizeof(proc_mounts_path)) ? 1 : 0), __extension__ ({ if (rv < sizeof(proc_mounts_path)) ; else __assert_fail ("rv < sizeof(proc_mounts_path)", "mount.c", 112, __extension__ __PRETTY_FUNCTION__); }));
  mount__load_proc_mounts(dst, proc_mounts_path);
  qsort(dst->mount_lines, dst->mount_len, sizeof(char *), &mount__compare_proc_mounts);
  mount__filter_proc_mounts(dst, path);
}

variable_types:
path
char *

user_defined_types: struct mount_lines_s { char **mount_lines; unsigned long mount_len; }


decompiled_code:
__int64 __fastcall mount_umount_pivoted_root(const char *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int *v3; // rax
char *v4; // rax
const char **v6; // [rsp+0h] [rbp-28h] BYREF
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

mount__umount_paths_for_pivoted_root((void **)&v6, a1);
if ( !v7 )
return 0;
v1 = 0LL;
while ( 1 )
{
v2 = umount(v6[v1]);
if ( v2 == -1 )
break;
if ( v7 <= ++v1 )
return 0;
}
v3 = _errno_location();
v4 = strerror(*v3);
fprintf(stderr, "umount(%s): %s\n", v6[v1], v4);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int *v3; // rax
char *v4; // rax
const char **v6; // [rsp+0h] [rbp-28h] BYREF
unsigned __int64 v7; // [rsp+8h] [rbp-20h]

func3((void **)&v6, a1);
if ( !v7 )
return 0;
v1 = 0LL;
while ( 1 )
{
v2 = umount(v6[v1]);
if ( v2 == -1 )
break;
if ( v7 <= ++v1 )
return 0;
}
v3 = _errno_location();
v4 = strerror(*v3);
fprintf(stderr, "umount(%s): %s\n", v6[v1], v4);
return v2;
}

original_code:
int mount_umount_pivoted_root(const char *path) {
  mount_lines_t dst;
  size_t i;
  int rv;
  mount__umount_paths_for_pivoted_root(&dst, path);
  for (i = 0; i < dst.mount_len; i++) {
    rv = umount(dst.mount_lines[i]);
    if (rv == -1) {
      fprintf(stderr, "umount(%s): %s\n", dst.mount_lines[i], strerror((*__errno_location ())));
      return rv;
    }
  }
  return 0;
}

canonical_original_code:
int mount_umount_pivoted_root(char *path) {
  struct mount_lines_s dst;
  unsigned long i;
  int rv;
  mount__umount_paths_for_pivoted_root(&dst, path);
  for (i = 0; i < dst.mount_len; i++) {
    rv = umount(dst.mount_lines[i]);
    if (rv == -1) {
      fprintf(stderr, "umount(%s): %s\n", dst.mount_lines[i], strerror((*__errno_location ())));
      return rv;
    }
  }
  return 0;
}

variable_types: struct mount_lines_s { char **mount_lines; unsigned long mount_len; }


decompiled_code:
int __fastcall fcntl_set_cloexec(int fd, int a2)
{
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // eax
int result; // eax

v2 = fcntl(fd, 1);
if ( v2 == -1 )
{
perror("fcntl");
abort();
}
v3 = v2 | 1u;
v4 = v2 & 0xFFFFFFFE;
if ( !a2 )
v3 = v4;
result = fcntl(fd, 2, v3);
if ( result == -1 )
{
perror("fcntl");
abort();
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd, int a2)
{
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // eax
int result; // eax

v2 = fcntl(fd, 1);
if ( v2 == -1 )
{
perror("fcntl");
abort();
}
v3 = v2 | 1u;
v4 = v2 & 0xFFFFFFFE;
if ( !a2 )
v3 = v4;
result = fcntl(fd, 2, v3);
if ( result == -1 )
{
perror("fcntl");
abort();
}
return result;
}

original_code:
void fcntl_set_cloexec(int fd, int on) {
  int rv;
  int fl;
  rv = fcntl(fd, 1);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
  fl = rv;
  if (on) {
    fl |= 1;
  } else {
    fl &= ~1;
  }
  rv = fcntl(fd, 2, fl);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
}

variable_types: on int

variable_types: fl int


decompiled_code:
int __fastcall fcntl_set_nonblock(int fd, int a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
int result; // eax

v2 = fcntl(fd, 3);
if ( v2 == -1 )
{
perror("fcntl");
abort();
}
v3 = v2;
BYTE1(v3) = BYTE1(v2) | 8;
BYTE1(v2) &= 0xF7u;
if ( !a2 )
v3 = v2;
result = fcntl(fd, 4, v3);
if ( result == -1 )
{
perror("fcntl");
abort();
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int fd, int a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
int result; // eax

v2 = fcntl(fd, 3);
if ( v2 == -1 )
{
perror("fcntl");
abort();
}
v3 = v2;
BYTE1(v3) = BYTE1(v2) | 8;
BYTE1(v2) &= 0xF7u;
if ( !a2 )
v3 = v2;
result = fcntl(fd, 4, v3);
if ( result == -1 )
{
perror("fcntl");
abort();
}
return result;
}

original_code:
void fcntl_set_nonblock(int fd, int on) {
  int rv;
  int fl;
  rv = fcntl(fd, 3);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
  fl = rv;
  if (on) {
    fl |= 04000;
  } else {
    fl &= ~04000;
  }
  rv = fcntl(fd, 4, fl);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
}

variable_types: fd int


run

decompiled_code:
__int64 __fastcall run(const char *a1, const char *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-101Ch] BYREF
char *file[2]; // [rsp+10h] [rbp-1018h] BYREF
char s[4104]; // [rsp+20h] [rbp-1008h] BYREF

if ( (unsigned int)snprintf(s, 0x1000uLL, "%s/%s", a1, a2) > 0xFFF )
_assert_fail("rv < sizeof(path)", "util.c", 0x48u, "run");
file[0] = s;
file[1] = 0LL;
v2 = fork();
if ( v2 == -1 )
{
perror("fork");
abort();
}
if ( !v2 )
{
execvp(file[0], file);
perror("execvp");
abort();
}
if ( waitpid(v2, &stat_loc, 0) == -1 )
{
perror("waitpid");
abort();
}
result = BYTE1(stat_loc);
if ( BYTE1(stat_loc) )
{
fprintf(stderr, "Process for \"%s\" exited with %d\n", s, BYTE1(stat_loc));
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-101Ch] BYREF
char *file[2]; // [rsp+10h] [rbp-1018h] BYREF
char s[4104]; // [rsp+20h] [rbp-1008h] BYREF

if ( (unsigned int)snprintf(s, 0x1000uLL, "%s/%s", a1, a2) > 0xFFF )
_assert_fail("rv < sizeof(path)", "util.c", 0x48u, "run");
file[0] = s;
file[1] = 0LL;
v2 = fork();
if ( v2 == -1 )
{
perror("fork");
abort();
}
if ( !v2 )
{
execvp(file[0], file);
perror("execvp");
abort();
}
if ( waitpid(v2, &stat_loc, 0) == -1 )
{
perror("waitpid");
abort();
}
result = BYTE1(stat_loc);
if ( BYTE1(stat_loc) )
{
fprintf(stderr, "Process for \"%s\" exited with %d\n", s, BYTE1(stat_loc));
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int run(const char *p1, const char *p2) {
  char path[4096];
  int rv;
  char *argv[2];
  int status;
  rv = snprintf(path, sizeof(path), "%s/%s", p1, p2);
  ((void) sizeof ((rv < sizeof(path)) ? 1 : 0), __extension__ ({ if (rv < sizeof(path)) ; else __assert_fail ("rv < sizeof(path)", "util.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  argv[0] = path;
  argv[1] = ((void *)0);
  rv = fork();
  if (rv == -1) {
    perror("fork");
    abort();
  }
  if (rv == 0) {
    execvp(argv[0], argv);
    perror("execvp");
    abort();
  } else {
    rv = waitpid(rv, &status, 0);
    if (rv == -1) {
      perror("waitpid");
      abort();
    }
    if ((((status) & 0xff00) >> 8) != 0) {
      fprintf(stderr, "Process for \"%s\" exited with %d\n", path, (((status) & 0xff00) >> 8));
      return -1;
    }
  }
  return 0;
}

canonical_original_code:
int run(char *p1, char *p2) {
  char path[4096];
  int rv;
  char *argv[2];
  int status;
  rv = snprintf(path, sizeof(path), "%s/%s", p1, p2);
  ((void) sizeof ((rv < sizeof(path)) ? 1 : 0), __extension__ ({ if (rv < sizeof(path)) ; else __assert_fail ("rv < sizeof(path)", "util.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  argv[0] = path;
  argv[1] = ((void *)0);
  rv = fork();
  if (rv == -1) {
    perror("fork");
    abort();
  }
  if (rv == 0) {
    execvp(argv[0], argv);
    perror("execvp");
    abort();
  } else {
    rv = waitpid(rv, &status, 0);
    if (rv == -1) {
      perror("waitpid");
      abort();
    }
    if ((((status) & 0xff00) >> 8) != 0) {
      fprintf(stderr, "Process for \"%s\" exited with %d\n", path, (((status) & 0xff00) >> 8));
      return -1;
    }
  }
  return 0;
}

variable_types:
path
char [4096]

variable_types:
argv
char *[2]

variable_types: status int


setproctitle

decompiled_code:
void *__fastcall setproctitle(__int64 a1, const char *a2)
{
char *v3; // rbp
__int64 v4; // r14
const char *v5; // rbx
const char *v6; // rdi
size_t v7; // rbx
size_t v8; // rax
size_t v9; // r14

v3 = *(char **)a1;
v4 = a1 + 8;
v5 = *(const char **)a1;
v6 = *(const char **)a1;
do
{
v5 += strlen(v6) + 1;
v4 += 8LL;
v6 = *(const char **)(v4 - 8);
}
while ( v6 == v5 );
v7 = v5 - v3;
v8 = strlen(a2);
v9 = v8;
if ( v7 <= v8 )
_assert_fail("len > n", "util.c", 0x78u, "setproctitle");
*(_QWORD *)(a1 + 8) = 0LL;
strncpy(v3, a2, v8);
return memset(&v3[v9], 0, v7 - v9);
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, const char *a2)
{
char *v3; // rbp
__int64 v4; // r14
const char *v5; // rbx
const char *v6; // rdi
size_t v7; // rbx
size_t v8; // rax
size_t v9; // r14

v3 = *(char **)a1;
v4 = a1 + 8;
v5 = *(const char **)a1;
v6 = *(const char **)a1;
do
{
v5 += strlen(v6) + 1;
v4 += 8LL;
v6 = *(const char **)(v4 - 8);
}
while ( v6 == v5 );
v7 = v5 - v3;
v8 = strlen(a2);
v9 = v8;
if ( v7 <= v8 )
_assert_fail("len > n", "util.c", 0x78u, "setproctitle");
*(_QWORD *)(a1 + 8) = 0LL;
strncpy(v3, a2, v8);
return memset(&v3[v9], 0, v7 - v9);
}

original_code:
void setproctitle(char **argv, const char *title) {
  char *last;
  int i;
  size_t len;
  char *p;
  size_t n;
  last = argv[0];
  i = 0;
  while (last == argv[i]) {
    last += strlen(argv[i++]) + 1;
  }
  len = last - argv[0];
  p = argv[0];
  n = strlen(title);
  ((void) sizeof ((len > n) ? 1 : 0), __extension__ ({ if (len > n) ; else __assert_fail ("len > n", "util.c", 120, __extension__ __PRETTY_FUNCTION__); }));
  argv[1] = ((void *)0);
  strncpy(p, title, n);
  len -= n;
  p += n;
  memset(p, '\0', len);
}

canonical_original_code:
void setproctitle(char **argv, char *title) {
  char *last;
  int i;
  unsigned long len;
  char *p;
  unsigned long n;
  last = argv[0];
  i = 0;
  while (last == argv[i]) {
    last += strlen(argv[i++]) + 1;
  }
  len = last - argv[0];
  p = argv[0];
  n = strlen(title);
  ((void) sizeof ((len > n) ? 1 : 0), __extension__ ({ if (len > n) ; else __assert_fail ("len > n", "util.c", 120, __extension__ __PRETTY_FUNCTION__); }));
  argv[1] = ((void *)0);
  strncpy(p, title, n);
  len -= n;
  p += n;
  memset(p, '\0', len);
}

variable_types:
argv
char **

variable_types:
title
char *

variable_types:
last
char *

variable_types: i int

variable_types:
len
unsigned long

variable_types:
p
char *

variable_types:
n
unsigned long


main

repo: chenyukang/dsC

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
unsigned int v5; // ebx
unsigned int i; // er12
unsigned int j; // er12

v3 = bitmap_new(10000000LL, argv, envp);
if ( !v3 )
__assert_fail("bm", "bitmap_test.c", 0x11u, "main");
v4 = (void *)v3;
bitmap_set(v3, 1LL);
if ( !(unsigned int)bitmap_tst(v4, 1LL) )
__assert_fail("bitmap_tst(bm, 1)", "bitmap_test.c", 0x13u, "main");
bitmap_clr(v4, 1LL);
if ( (unsigned int)bitmap_tst(v4, 1LL) )
__assert_fail("bitmap_tst(bm, 1) == 0", "bitmap_test.c", 0x15u, "main");
v5 = bitmap_tst(v4, 2LL);
if ( v5 )
__assert_fail("bitmap_tst(bm, 2) == 0", "bitmap_test.c", 0x16u, "main");
if ( (unsigned int)bitmap_first0(v4) )
__assert_fail("bitmap_first0(bm) == 0", "bitmap_test.c", 0x17u, "main");
bitmap_set(v4, 1LL);
if ( (unsigned int)bitmap_first1(v4) != 1 )
__assert_fail("bitmap_first1(bm) == 1", "bitmap_test.c", 0x19u, "main");
if ( (unsigned int)bitmap_first0(v4) )
__assert_fail("bitmap_first0(bm) == 0", "bitmap_test.c", 0x1Au, "main");
for ( i = v5; i != 10000000; ++i )
bitmap_set(v4, i);
if ( (unsigned int)bitmap_first0(v4) != -1 )
__assert_fail("bitmap_first0(bm) == -1", "bitmap_test.c", 0x21u, "main");
for ( j = v5; j != 10000000; ++j )
bitmap_clr(v4, j);
if ( (unsigned int)bitmap_first1(v4) != -1 )
__assert_fail("bitmap_first1(bm) == -1", "bitmap_test.c", 0x27u, "main");
do
{
bitmap_set(v4, v5);
if ( !(unsigned int)bitmap_tst(v4, v5) )
__assert_fail("bitmap_tst(bm, k)", "bitmap_test.c", 0x2Cu, "main");
bitmap_clr(v4, v5);
if ( (unsigned int)bitmap_tst(v4, v5) )
__assert_fail("bitmap_tst(bm, k) == 0", "bitmap_test.c", 0x2Eu, "main");
++v5;
}
while ( v5 != 10000000 );
bitmap_del(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
void *v4; // rbp
unsigned int v5; // ebx
unsigned int i; // er12
unsigned int j; // er12

v3 = bitmap_new(10000000LL, argv, envp);
if ( !v3 )
__assert_fail("bm", "bitmap_test.c", 0x11u, "main");
v4 = (void *)v3;
func10(v3, 1LL);
if ( !(unsigned int)func12(v4, 1LL) )
__assert_fail("bitmap_tst(bm, 1)", "bitmap_test.c", 0x13u, "main");
func11(v4, 1LL);
if ( (unsigned int)func12(v4, 1LL) )
__assert_fail("bitmap_tst(bm, 1) == 0", "bitmap_test.c", 0x15u, "main");
v5 = func12(v4, 2LL);
if ( v5 )
__assert_fail("bitmap_tst(bm, 2) == 0", "bitmap_test.c", 0x16u, "main");
if ( (unsigned int)func13(v4) )
__assert_fail("bitmap_first0(bm) == 0", "bitmap_test.c", 0x17u, "main");
func10(v4, 1LL);
if ( (unsigned int)func14(v4) != 1 )
__assert_fail("bitmap_first1(bm) == 1", "bitmap_test.c", 0x19u, "main");
if ( (unsigned int)func13(v4) )
__assert_fail("bitmap_first0(bm) == 0", "bitmap_test.c", 0x1Au, "main");
for ( i = v5; i != 10000000; ++i )
func10(v4, i);
if ( (unsigned int)func13(v4) != -1 )
__assert_fail("bitmap_first0(bm) == -1", "bitmap_test.c", 0x21u, "main");
for ( j = v5; j != 10000000; ++j )
func11(v4, j);
if ( (unsigned int)func14(v4) != -1 )
__assert_fail("bitmap_first1(bm) == -1", "bitmap_test.c", 0x27u, "main");
do
{
func10(v4, v5);
if ( !(unsigned int)func12(v4, v5) )
__assert_fail("bitmap_tst(bm, k)", "bitmap_test.c", 0x2Cu, "main");
func11(v4, v5);
if ( (unsigned int)func12(v4, v5) )
__assert_fail("bitmap_tst(bm, k) == 0", "bitmap_test.c", 0x2Eu, "main");
++v5;
}
while ( v5 != 10000000 );
func9(v4);
return 0;
}

original_code:
int main()
{
    bitmap* bm = bitmap_new(10000000);
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap_test.c", 17, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_set(bm, 1);
    ((void) sizeof ((bitmap_tst(bm, 1)) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 1)) ; else __assert_fail ("bitmap_tst(bm, 1)", "bitmap_test.c", 19, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_clr(bm, 1);
    ((void) sizeof ((bitmap_tst(bm, 1) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 1) == 0) ; else __assert_fail ("bitmap_tst(bm, 1) == 0", "bitmap_test.c", 21, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_tst(bm, 2) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 2) == 0) ; else __assert_fail ("bitmap_tst(bm, 2) == 0", "bitmap_test.c", 22, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_first0(bm) == 0) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == 0) ; else __assert_fail ("bitmap_first0(bm) == 0", "bitmap_test.c", 23, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_set(bm, 1);
    ((void) sizeof ((bitmap_first1(bm) == 1) ? 1 : 0), __extension__ ({ if (bitmap_first1(bm) == 1) ; else __assert_fail ("bitmap_first1(bm) == 1", "bitmap_test.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_first0(bm) == 0) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == 0) ; else __assert_fail ("bitmap_first0(bm) == 0", "bitmap_test.c", 26, __extension__ __PRETTY_FUNCTION__); }));
    int k;
    for(k=0; k<10000000; k++)
    {
        bitmap_set(bm, k);
    }
    ((void) sizeof ((bitmap_first0(bm) == -1) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == -1) ; else __assert_fail ("bitmap_first0(bm) == -1", "bitmap_test.c", 33, __extension__ __PRETTY_FUNCTION__); }));
    for(k=0; k<10000000; k++)
    {
        bitmap_clr(bm, k);
    }
    ((void) sizeof ((bitmap_first1(bm) == -1) ? 1 : 0), __extension__ ({ if (bitmap_first1(bm) == -1) ; else __assert_fail ("bitmap_first1(bm) == -1", "bitmap_test.c", 39, __extension__ __PRETTY_FUNCTION__); }));
    for(k=0; k<10000000; k++)
    {
        bitmap_set(bm, k);
        ((void) sizeof ((bitmap_tst(bm, k)) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, k)) ; else __assert_fail ("bitmap_tst(bm, k)", "bitmap_test.c", 44, __extension__ __PRETTY_FUNCTION__); }));
        bitmap_clr(bm, k);
        ((void) sizeof ((bitmap_tst(bm, k) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, k) == 0) ; else __assert_fail ("bitmap_tst(bm, k) == 0", "bitmap_test.c", 46, __extension__ __PRETTY_FUNCTION__); }));
    }
    bitmap_del(bm);
    return 0;
}

canonical_original_code:
int main()
{
    struct _bitmap *bm = bitmap_new(10000000);
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap_test.c", 17, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_set(bm, 1);
    ((void) sizeof ((bitmap_tst(bm, 1)) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 1)) ; else __assert_fail ("bitmap_tst(bm, 1)", "bitmap_test.c", 19, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_clr(bm, 1);
    ((void) sizeof ((bitmap_tst(bm, 1) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 1) == 0) ; else __assert_fail ("bitmap_tst(bm, 1) == 0", "bitmap_test.c", 21, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_tst(bm, 2) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, 2) == 0) ; else __assert_fail ("bitmap_tst(bm, 2) == 0", "bitmap_test.c", 22, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_first0(bm) == 0) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == 0) ; else __assert_fail ("bitmap_first0(bm) == 0", "bitmap_test.c", 23, __extension__ __PRETTY_FUNCTION__); }));
    bitmap_set(bm, 1);
    ((void) sizeof ((bitmap_first1(bm) == 1) ? 1 : 0), __extension__ ({ if (bitmap_first1(bm) == 1) ; else __assert_fail ("bitmap_first1(bm) == 1", "bitmap_test.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((bitmap_first0(bm) == 0) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == 0) ; else __assert_fail ("bitmap_first0(bm) == 0", "bitmap_test.c", 26, __extension__ __PRETTY_FUNCTION__); }));
    int k;
    for(k=0; k<10000000; k++)
    {
        bitmap_set(bm, k);
    }
    ((void) sizeof ((bitmap_first0(bm) == -1) ? 1 : 0), __extension__ ({ if (bitmap_first0(bm) == -1) ; else __assert_fail ("bitmap_first0(bm) == -1", "bitmap_test.c", 33, __extension__ __PRETTY_FUNCTION__); }));
    for(k=0; k<10000000; k++)
    {
        bitmap_clr(bm, k);
    }
    ((void) sizeof ((bitmap_first1(bm) == -1) ? 1 : 0), __extension__ ({ if (bitmap_first1(bm) == -1) ; else __assert_fail ("bitmap_first1(bm) == -1", "bitmap_test.c", 39, __extension__ __PRETTY_FUNCTION__); }));
    for(k=0; k<10000000; k++)
    {
        bitmap_set(bm, k);
        ((void) sizeof ((bitmap_tst(bm, k)) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, k)) ; else __assert_fail ("bitmap_tst(bm, k)", "bitmap_test.c", 44, __extension__ __PRETTY_FUNCTION__); }));
        bitmap_clr(bm, k);
        ((void) sizeof ((bitmap_tst(bm, k) == 0) ? 1 : 0), __extension__ ({ if (bitmap_tst(bm, k) == 0) ; else __assert_fail ("bitmap_tst(bm, k) == 0", "bitmap_test.c", 46, __extension__ __PRETTY_FUNCTION__); }));
    }
    bitmap_del(bm);
    return 0;
}

variable_types: struct _bitmap { unsigned int size; unsigned int cnt; unsigned int *table; } *

variable_types: k int


INIT

decompiled_code:
__int64 __fastcall INIT(unsigned int a1, _DWORD *a2, int *a3)
{
__int64 result; // rax

result = a1 >> 5;
*a2 = result;
*a3 = a1 & 0x1F;
return result;
}

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

result = a1 >> 5;
*a2 = result;
*a3 = a1 & 0x1F;
return result;
}

original_code:
inline static
void INIT(u32 val, u32* index, u32* offset)
{
    *index = ((val)/(sizeof(u32)*8));
    *offset = ((val)%(sizeof(u32)*8));
}

canonical_original_code:
inline static void INIT(unsigned int val, unsigned int *index, unsigned int *offset)
{
    *index = ((val)/(sizeof(u32)*8));
    *offset = ((val)%(sizeof(u32)*8));
}

variable_types:
val
unsigned int

variable_types:
index
unsigned int *

variable_types:
offset
unsigned int *


decompiled_code:
void __fastcall bitmap_del(void *ptr)
{
if ( ptr )
{
free(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
if ( ptr )
{
free(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
void bitmap_del(bitmap* bm)
{
    if(bm == ((void *)0))
        return;
    free(bm->table);
    free(bm);
}

canonical_original_code:
void bitmap_del(struct _bitmap *bm)
{
    if(bm == ((void *)0))
        return;
    free(bm->table);
    free(bm);
}

user_defined_types: struct _bitmap { unsigned int size; unsigned int cnt; unsigned int *table; }


decompiled_code:
__int64 __fastcall bitmap_tst(__int64 a1, unsigned int a2)
{
int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( *(_DWORD *)a1 < a2 )
__assert_fail("val>=0 && val <= bm->size && \"val out of range\"", "bitmap.c", 0x72u, "bitmap_tst");
INIT(a2, v4, &v3);
return *(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * v4[0]) & (unsigned int)(1 << v3);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, unsigned int a2)
{
int v3; // [rsp+8h] [rbp-10h] BYREF
unsigned int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( *(_DWORD *)a1 < a2 )
__assert_fail("val>=0 && val <= bm->size && \"val out of range\"", "bitmap.c", 0x72u, "bitmap_tst");
func8(a2, v4, &v3);
return *(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * v4[0]) & (unsigned int)(1 << v3);
}

original_code:
int bitmap_tst(bitmap* bm, u32 val)
{
    ((void) sizeof ((val>=0 && val <= bm->size && "val out of range") ? 1 : 0), __extension__ ({ if (val>=0 && val <= bm->size && "val out of range") ; else __assert_fail ("val>=0 && val <= bm->size && \"val out of range\"", "bitmap.c", 114, __extension__ __PRETTY_FUNCTION__); }));
    u32 index, offset;
    INIT(val, &index, &offset);
    return bm->table[index] & (0x1 << offset );
}

canonical_original_code:
int bitmap_tst(struct _bitmap *bm, unsigned int val)
{
    ((void) sizeof ((val>=0 && val <= bm->size && "val out of range") ? 1 : 0), __extension__ ({ if (val>=0 && val <= bm->size && "val out of range") ; else __assert_fail ("val>=0 && val <= bm->size && \"val out of range\"", "bitmap.c", 114, __extension__ __PRETTY_FUNCTION__); }));
    unsigned int index; unsigned int offset;
    INIT(val, &index, &offset);
    return bm->table[index] & (0x1 << offset );
}

variable_types:
index
unsigned int

variable_types:
offset
unsigned int


decompiled_code:
__int64 __fastcall bitmap_first0(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // rdx
__int64 v3; // rcx
__int64 result; // rax

if ( !a1 )
__assert_fail("bm", "bitmap.c", 0x7Du, "bitmap_first0");
v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = 0LL;
while ( 1 )
{
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 4 * v3);
if ( (_DWORD)result != -1 )
break;
if ( ++v3 == v2 )
return result;
}
return 32 * (_DWORD)v3 + (unsigned int)first_onebit(~(_DWORD)result) - 1;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // rdx
__int64 v3; // rcx
__int64 result; // rax

if ( !a1 )
__assert_fail("bm", "bitmap.c", 0x7Du, "bitmap_first0");
v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v3 = 0LL;
while ( 1 )
{
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 4 * v3);
if ( (_DWORD)result != -1 )
break;
if ( ++v3 == v2 )
return result;
}
return 32 * (_DWORD)v3 + (unsigned int)func7(~(_DWORD)result) - 1;
}

original_code:
u32 bitmap_first0(bitmap* bm)
{
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap.c", 125, __extension__ __PRETTY_FUNCTION__); }));
    u32 i;
    for( i = 0; i < bm->cnt; i++) {
        if( bm->table[i] == 0xFFFFFFFF )
            continue;
        u32 v = bm->table[i];
        return 32*i + (_first_onebit(~(v))) - 1;
    }
    return -1;
}

canonical_original_code:
unsigned int bitmap_first0(struct _bitmap *bm)
{
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap.c", 125, __extension__ __PRETTY_FUNCTION__); }));
    unsigned int i;
    for( i = 0; i < bm->cnt; i++) {
        if( bm->table[i] == 0xFFFFFFFF )
            continue;
        unsigned int v = bm->table[i];
        return 32*i + (_first_onebit(~(v))) - 1;
    }
    return -1;
}

variable_types:
i
unsigned int

variable_types:
v
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall bitmap_first1(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // rcx
__int64 v3; // rdx
__int64 i; // rax
unsigned int v5; // edi

if ( !a1 )
__assert_fail("bm", "bitmap.c", 0x8Du, "bitmap_first1");
v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 8);
v3 = v1;
for ( i = 0LL; i != v3; ++i )
{
v5 = *(_DWORD *)(v2 + 4 * i);
if ( v5 )
return 32 * (_DWORD)i + (unsigned int)first_onebit(v5) - 1;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // rcx
__int64 v3; // rdx
__int64 i; // rax
unsigned int v5; // edi

if ( !a1 )
__assert_fail("bm", "bitmap.c", 0x8Du, "bitmap_first1");
v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 8);
v3 = v1;
for ( i = 0LL; i != v3; ++i )
{
v5 = *(_DWORD *)(v2 + 4 * i);
if ( v5 )
return 32 * (_DWORD)i + (unsigned int)func7(v5) - 1;
}
return 0xFFFFFFFFLL;
}

original_code:
u32 bitmap_first1(bitmap* bm)
{
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap.c", 141, __extension__ __PRETTY_FUNCTION__); }));
    u32 i;
    for( i = 0; i < bm->cnt; i++ ) {
        if( bm->table[i] == 0x0 )
            continue;
        u32 v = bm->table[i];
        return 32*i + (_first_onebit(v)) - 1;
    }
    return -1;
}

canonical_original_code:
unsigned int bitmap_first1(struct _bitmap *bm)
{
    ((void) sizeof ((bm) ? 1 : 0), __extension__ ({ if (bm) ; else __assert_fail ("bm", "bitmap.c", 141, __extension__ __PRETTY_FUNCTION__); }));
    unsigned int i;
    for( i = 0; i < bm->cnt; i++ ) {
        if( bm->table[i] == 0x0 )
            continue;
        unsigned int v = bm->table[i];
        return 32*i + (_first_onebit(v)) - 1;
    }
    return -1;
}


sarycmp

repo: cherry-wb/strmat

decompiled_code:
__int64 __fastcall sarycmp(const void *a1, const void *a2)
{
int v2; // ecx
int v3; // er9
__int64 v4; // r8
int v5; // esi
__int64 v6; // rax
__int64 v7; // rdx
int v8; // edi
int v9; // edi
__int64 result; // rax

v2 = *(_DWORD *)a1;
v3 = *(_DWORD *)a2;
v4 = *(_QWORD *)sarystruct;
v5 = *(_DWORD *)(sarystruct + 8);
if ( *(_DWORD *)a1 > v5 )
{
v8 = 0;
LABEL_13:
*(_DWORD *)(sarystruct + 40) += v8 + 1;
return (unsigned int)-(v3 + v8 <= v5);
}
if ( v3 > v5 )
{
v8 = 0;
LABEL_7:
*(_DWORD *)(sarystruct + 40) += v8 + 1;
}
else
{
v6 = v2 + 1;
v7 = v3;
v8 = 0;
while ( 1 )
{
v2 = v6 - 1;
if ( *(_BYTE *)(v4 + v6 - 1) != *(_BYTE *)(v4 + v7) )
break;
++v8;
v2 = v6;
if ( v5 < (int)v6 )
goto LABEL_13;
++v6;
if ( v5 < (int)++v7 )
goto LABEL_7;
}
*(_DWORD *)(sarystruct + 40) += v8 + 1;
if ( v5 < v2 )
return (unsigned int)-(v3 + v8 <= v5);
}
v9 = v3 + v8;
result = 0xFFFFFFFFLL;
if ( v9 <= v5 )
result = 2 * (unsigned int)(*(_BYTE *)(v4 + v2) > *(_BYTE *)(v4 + v9)) - 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
int v2; // ecx
int v3; // er9
__int64 v4; // r8
int v5; // esi
__int64 v6; // rax
__int64 v7; // rdx
int v8; // edi
int v9; // edi
__int64 result; // rax

v2 = *(_DWORD *)a1;
v3 = *(_DWORD *)a2;
v4 = *(_QWORD *)sarystruct;
v5 = *(_DWORD *)(sarystruct + 8);
if ( *(_DWORD *)a1 > v5 )
{
v8 = 0;
LABEL_13:
*(_DWORD *)(sarystruct + 40) += v8 + 1;
return (unsigned int)-(v3 + v8 <= v5);
}
if ( v3 > v5 )
{
v8 = 0;
LABEL_7:
*(_DWORD *)(sarystruct + 40) += v8 + 1;
}
else
{
v6 = v2 + 1;
v7 = v3;
v8 = 0;
while ( 1 )
{
v2 = v6 - 1;
if ( *(_BYTE *)(v4 + v6 - 1) != *(_BYTE *)(v4 + v7) )
break;
++v8;
v2 = v6;
if ( v5 < (int)v6 )
goto LABEL_13;
++v6;
if ( v5 < (int)++v7 )
goto LABEL_7;
}
*(_DWORD *)(sarystruct + 40) += v8 + 1;
if ( v5 < v2 )
return (unsigned int)-(v3 + v8 <= v5);
}
v9 = v3 + v8;
result = 0xFFFFFFFFLL;
if ( v9 <= v5 )
result = 2 * (unsigned int)(*(_BYTE *)(v4 + v2) > *(_BYTE *)(v4 + v9)) - 1;
return result;
}

original_code:
static int sarycmp(int *a, int *b)
{
  int i, j, k, M;
  char *S;
  i = *a;
  j = *b;
  S = sarystruct->S;
  M = sarystruct->M;
  for (k=0; i + k <= M && j + k <= M && S[i+k] == S[j+k]; k++) ;
  sarystruct->num_compares += k + 1;
  if (i + k > M)
    return (j + k > M ? 0 : -1);
  else if (j + k <= M && S[i+k] > S[j+k])
    return 1;
  else
    return -1;
}

canonical_original_code:
static int sarycmp(int *a, int *b)
{
  int i; int j; int k; int M;
  char *S;
  i = *a;
  j = *b;
  S = sarystruct->S;
  M = sarystruct->M;
  for (k=0; i + k <= M && j + k <= M && S[i+k] == S[j+k]; k++) ;
  sarystruct->num_compares += k + 1;
  if (i + k > M)
    return (j + k > M ? 0 : -1);
  else if (j + k <= M && S[i+k] > S[j+k])
    return 1;
  else
    return -1;
}

variable_types:
a
int *

variable_types:
b
int *

variable_types: j int

variable_types: M int

variable_types:
S
char *


decompiled_code:
__int64 __fastcall compute_lcp_values(__int64 a1, __int64 a2, unsigned int a3, int a4)
{
unsigned int v7; // er13
int v8; // er14
int v9; // er15
int v10; // eax

if ( a3 - (_DWORD)a2 == 1 )
{
*(_DWORD *)(*(_QWORD *)(a1 + 24) + 4LL * a4) = *(_DWORD *)(*(_QWORD *)(a1 + 32) + 4LL * (int)a3);
}
else
{
v7 = (int)(a3 + a2) / 2;
v8 = 2 * a4;
v9 = compute_lcp_values(a1, a2, v7, (unsigned int)(2 * a4));
v10 = compute_lcp_values(a1, v7, a3, (unsigned int)(v8 + 1));
if ( v9 > v10 )
v9 = v10;
*(_DWORD *)(*(_QWORD *)(a1 + 24) + 4LL * a4) = v9;
++*(_DWORD *)(a1 + 48);
}
return *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL * a4);
}

original_code:
static int compute_lcp_values(SARY_STRUCT *sary, int min, int max, int index)
{
  int midpoint, value1, value2;
  if (max - min == 1)
    sary->lcp[index] = sary->lcp_leaves[max];
  else {
    midpoint = (min + max) / 2;
    value1 = compute_lcp_values(sary, min, midpoint, index * 2);
    value2 = compute_lcp_values(sary, midpoint, max, index * 2 + 1);
    sary->lcp[index] = (value1 <= value2 ? value1 : value2);
    sary->num_lcp_ops++;
  }
  return sary->lcp[index];
}

canonical_original_code:
static int compute_lcp_values(struct SARY_STRUCT *sary, int min, int max, int index)
{
  int midpoint; int value1; int value2;
  if (max - min == 1)
    sary->lcp[index] = sary->lcp_leaves[max];
  else {
    midpoint = (min + max) / 2;
    value1 = compute_lcp_values(sary, min, midpoint, index * 2);
    value2 = compute_lcp_values(sary, midpoint, max, index * 2 + 1);
    sary->lcp[index] = (value1 <= value2 ? value1 : value2);
    sary->num_lcp_ops++;
  }
  return sary->lcp[index];
}

variable_types: struct SARY_STRUCT { char *S; int M; int *Pos; int num_compares; } *

variable_types: min int

variable_types: max int

variable_types: index int

variable_types: midpoint int


decompiled_code:
void __fastcall sary_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
if ( *((_DWORD *)ptr + 3) && *(_QWORD *)ptr )
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 2);
if ( v4 )
free(v4);
if ( *((_DWORD *)ptr + 3) && *(_QWORD *)ptr )
free(*(void **)ptr);
free(ptr);
}

original_code:
void sary_free(SARY_STRUCT *sary)
{
  if (sary->lcp_leaves != ((void *)0))
    free(sary->lcp_leaves);
  if (sary->lcp != ((void *)0))
    free(sary->lcp);
  if (sary->Pos != ((void *)0))
    free(sary->Pos);
  if (sary->copyflag && sary->S != ((void *)0))
    free(sary->S);
  free(sary);
}

canonical_original_code:
void sary_free(struct SARY_STRUCT *sary)
{
  if (sary->lcp_leaves != ((void *)0))
    free(sary->lcp_leaves);
  if (sary->lcp != ((void *)0))
    free(sary->lcp);
  if (sary->Pos != ((void *)0))
    free(sary->Pos);
  if (sary->copyflag && sary->S != ((void *)0))
    free(sary->S);
  free(sary);
}

user_defined_types: struct SARY_STRUCT { char *S; int M; int *Pos; int num_compares; }


original_code:
char *z_search(Z_STRUCT *node, char *T, int N, int initmatch)
{
  int j, l, r, k, beta, kprime, M, *Z, ZT;
  char *P;
  T--;
  P = node->S;
  M = node->M;
  Z = node->Z;
  l = 0;
  r = (!initmatch ? 0 : M - 1);
  k = (!initmatch ? 1 : 2);
  for ( ; k + M <= N; k++) {
    if (k > r) {
      j = 0;
      while (1 + j <= M && P[1+j] == T[k+j]) {
        j++;
        node->num_compares++;
      }
      node->num_compares++;
      ZT = j;
      r = k + j - 1;
      l = k;
    }
    else {
      beta = r - k + 1;
      kprime = k - l + 1;
      if (Z[kprime] < beta)
        ZT = Z[kprime];
      else {
        j = 1;
        while (beta + j <= M && r + j <= N && P[beta+j] == T[r+j]) {
          j++;
          node->num_compares++;
        }
        node->num_compares++;
        ZT = r + j - k;
        r = r + j - 1;
        l = k;
      }
    }
    if (ZT == M)
      return &T[k];
  }
  return ((void *)0);
}

canonical_original_code:
char *z_search(struct Z_STRUCT *node, char *T, int N, int initmatch)
{
  int j; int l; int r; int k; int beta; int kprime; int M; int *Z; int ZT;
  char *P;
  T--;
  P = node->S;
  M = node->M;
  Z = node->Z;
  l = 0;
  r = (!initmatch ? 0 : M - 1);
  k = (!initmatch ? 1 : 2);
  for ( ; k + M <= N; k++) {
    if (k > r) {
      j = 0;
      while (1 + j <= M && P[1+j] == T[k+j]) {
        j++;
        node->num_compares++;
      }
      node->num_compares++;
      ZT = j;
      r = k + j - 1;
      l = k;
    }
    else {
      beta = r - k + 1;
      kprime = k - l + 1;
      if (Z[kprime] < beta)
        ZT = Z[kprime];
      else {
        j = 1;
        while (beta + j <= M && r + j <= N && P[beta+j] == T[r+j]) {
          j++;
          node->num_compares++;
        }
        node->num_compares++;
        ZT = r + j - k;
        r = r + j - 1;
        l = k;
      }
    }
    if (ZT == M)
      return &T[k];
  }
  return ((void *)0);
}

variable_types: struct Z_STRUCT { char *S; int M; int *Z; int prep_compares; } *

variable_types:
T
char *

variable_types: N int

variable_types: initmatch int

variable_types: l int

variable_types: r int

variable_types: k int

variable_types: beta int

variable_types: kprime int

variable_types:
Z
int *

variable_types: ZT int

variable_types:
P
char *

return_type: char *


decompiled_code:
void __fastcall z_free(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
if ( *((_DWORD *)ptr + 3) )
{
if ( *(_QWORD *)ptr )
free(*(void **)ptr);
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
if ( *((_DWORD *)ptr + 3) )
{
if ( *(_QWORD *)ptr )
free(*(void **)ptr);
}
free(ptr);
}
}

original_code:
void z_free(Z_STRUCT *node)
{
  if (node == ((void *)0))
    return;
  if (node->Z != ((void *)0))
    free(node->Z);
  if (node->copyflag && node->S != ((void *)0))
    free(node->S);
  free(node);
}

canonical_original_code:
void z_free(struct Z_STRUCT *node)
{
  if (node == ((void *)0))
    return;
  if (node->Z != ((void *)0))
    free(node->Z);
  if (node->copyflag && node->S != ((void *)0))
    free(node->S);
  free(node);
}

user_defined_types: struct Z_STRUCT { char *S; int M; int *Z; int prep_compares; }