decompiled_code:
void __fastcall __noreturn do_ping(char *name, int a2)
{
struct hostent *v2; // rax
struct hostent *v3; // rbx
__int16 v4; // ax
size_t v5; // rdx
unsigned int v6; // er13
char *v7; // rax
_QWORD *v8; // rax
_QWORD *v9; // rbx
__int16 v10[2]; // [rsp+0h] [rbp-38h] BYREF
struct in_addr dest[13]; // [rsp+4h] [rbp-34h] BYREF

pid = getpid();
signal(14, (__sighandler_t)sig_alrm);
v2 = gethostbyname(name);
if ( v2 )
{
v3 = v2;
v10[0] = 2;
dest[0].s_addr = _bswap_32_constprop_0(dest);
v4 = _bswap_16_constprop_0();
v5 = v3->h_length;
v10[1] = v4;
memcpy(dest, *(const void **)v3->h_addr_list, v5);
v6 = datalen;
v7 = inet_ntoa(dest[0]);
printf("PING %s (%s): %d data bytes\n", v3->h_name, v7, v6);
v8 = malloc(0x18uLL);
*v8 = v10;
v9 = v8;
pr = v8;
v8[1] = calloc(1uLL, 0x10uLL);
v9[2] = 0x100000010LL;
readloop(a2);
free(pr);
exit(0);
}
perror("Host unknown");
exit(3);
}

canonical_decompiled_code:
void __fastcall __noreturn func15(char *name, int a2)
{
struct hostent *v2; // rax
struct hostent *v3; // rbx
__int16 v4; // ax
size_t v5; // rdx
unsigned int v6; // er13
char *v7; // rax
_QWORD *v8; // rax
_QWORD *v9; // rbx
__int16 v10[2]; // [rsp+0h] [rbp-38h] BYREF
struct in_addr dest[13]; // [rsp+4h] [rbp-34h] BYREF

pid = getpid();
signal(14, (__sighandler_t)sig_alrm);
v2 = gethostbyname(name);
if ( v2 )
{
v3 = v2;
v10[0] = 2;
dest[0].s_addr = func7(dest);
v4 = func8();
v5 = v3->h_length;
v10[1] = v4;
memcpy(dest, *(const void **)v3->h_addr_list, v5);
v6 = datalen;
v7 = inet_ntoa(dest[0]);
printf("PING %s (%s): %d data bytes\n", v3->h_name, v7, v6);
v8 = malloc(0x18uLL);
*v8 = v10;
v9 = v8;
pr = v8;
v8[1] = calloc(1uLL, 0x10uLL);
v9[2] = 0x100000010LL;
func14(a2);
free(pr);
exit(0);
}
perror("Host unknown");
exit(3);
}

original_code:
void
do_ping (const char *host, int count)
{
 struct hostent *hp;
 struct sockaddr_in addr;
 pid = getpid ();
 signal (14, sig_alrm);
 if ((hp = gethostbyname (host)) == ((void *)0)) {
  perror ("Host unknown");
  exit (3);
 }
 addr.sin_family = 2;
 addr.sin_addr.s_addr = htonl (((in_addr_t) 0x00000000));
 addr.sin_port = htons (1);
 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
 printf ("PING %s (%s): %d data bytes\n", hp->h_name,
  inet_ntoa (addr.sin_addr), datalen);
 pr = malloc (sizeof (struct proto));
 pr->sasend = (struct sockaddr *) &(addr);
 pr->sarecv = calloc (1, sizeof (addr));
 pr->salen = sizeof (addr);
 pr->icmpproto = IPPROTO_ICMP;
 readloop (count);
 free (pr);
 exit (0);
}

canonical_original_code:
void do_ping(char *host, int count)
{
 struct hostent *hp;
 struct sockaddr_in addr;
 pid = getpid ();
 signal (14, sig_alrm);
 if ((hp = gethostbyname (host)) == ((void *)0)) {
  perror ("Host unknown");
  exit (3);
 }
 addr.sin_family = 2;
 addr.sin_addr.s_addr = htonl (((unsigned int) 0x00000000));
 addr.sin_port = htons (1);
 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
 printf ("PING %s (%s): %d data bytes\n", hp->h_name,
  inet_ntoa (addr.sin_addr), datalen);
 pr = malloc (sizeof (struct proto));
 pr->sasend = (struct sockaddr *) &(addr);
 pr->sarecv = calloc (1, sizeof (addr));
 pr->salen = sizeof (addr);
 pr->icmpproto = IPPROTO_ICMP;
 readloop (count);
 free (pr);
 exit (0);
}

variable_types:
host
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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

user_defined_types: struct proto { struct sockaddr *sasend; struct sockaddr *sarecv; unsigned int salen; int icmpproto; }

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


repo: GNU-Pony/libpassphrase

decompiled_code:
int __fastcall passphrase_disable_echo1(int fd)
{
__m128i v1; // xmm0
__m128i v2; // xmm1
__m128i v3; // xmm2
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

tcgetattr(fd, &v5);
v1 = _mm_loadu_si128((const __m128i *)&v5);
v2 = _mm_loadu_si128((const __m128i *)&v5.c_line);
v3 = _mm_loadu_si128((const __m128i *)&v5.c_cc[15]);
*(_QWORD *)&saved_stty.c_cc[31] = *(_QWORD *)&v5.c_cc[31];
v5.c_lflag &= 0xFFFFFFF5;
*(__m128i *)&saved_stty.c_iflag = v1;
saved_stty.c_ospeed = v5.c_ospeed;
*(__m128i *)&saved_stty.c_line = v2;
*(__m128i *)&saved_stty.c_cc[15] = v3;
return tcsetattr(fd, 2, &v5);
}

canonical_decompiled_code:
int __fastcall func0(int fd)
{
__m128i v1; // xmm0
__m128i v2; // xmm1
__m128i v3; // xmm2
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

tcgetattr(fd, &v5);
v1 = _mm_loadu_si128((const __m128i *)&v5);
v2 = _mm_loadu_si128((const __m128i *)&v5.c_line);
v3 = _mm_loadu_si128((const __m128i *)&v5.c_cc[15]);
*(_QWORD *)&saved_stty.c_cc[31] = *(_QWORD *)&v5.c_cc[31];
v5.c_lflag &= 0xFFFFFFF5;
*(__m128i *)&saved_stty.c_iflag = v1;
saved_stty.c_ospeed = v5.c_ospeed;
*(__m128i *)&saved_stty.c_line = v2;
*(__m128i *)&saved_stty.c_cc[15] = v3;
return tcsetattr(fd, 2, &v5);
}

original_code:
void passphrase_disable_echo1(int fdin)
{
  struct termios stty;
  tcgetattr(fdin, &stty);
  saved_stty = stty;
  stty.c_lflag &= (tcflag_t)~0000010;
  stty.c_lflag &= (tcflag_t)~0000002;
  tcsetattr(fdin, 2, &stty);
}

canonical_original_code:
void passphrase_disable_echo1(int fdin)
{
  struct termios stty;
  tcgetattr(fdin, &stty);
  saved_stty = stty;
  stty.c_lflag &= (unsigned int)~0000010;
  stty.c_lflag &= (unsigned int)~0000002;
  tcsetattr(fdin, 2, &stty);
}

variable_types: fdin int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

return_type: void


decompiled_code:
int passphrase_disable_echo()
{
return passphrase_disable_echo1(0);
}

canonical_decompiled_code:
int func1()
{
return func0(0);
}

original_code:
void passphrase_disable_echo(void)
{
  passphrase_disable_echo1(0);
}

canonical_original_code:
void passphrase_disable_echo()
{
  passphrase_disable_echo1(0);
}


decompiled_code:
int __fastcall passphrase_reenable_echo1(int a1)
{
return tcsetattr(a1, 2, &saved_stty);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
return tcsetattr(a1, 2, &saved_stty);
}

original_code:
void passphrase_reenable_echo1(int fdin)
{
  tcsetattr(fdin, 2, &saved_stty);
}


decompiled_code:
int passphrase_reenable_echo()
{
return passphrase_reenable_echo1(0);
}

canonical_decompiled_code:
int func3()
{
return func2(0);
}

original_code:
void passphrase_reenable_echo(void)
{
  passphrase_reenable_echo1(0);
}

canonical_original_code:
void passphrase_reenable_echo()
{
  passphrase_reenable_echo1(0);
}


fdgetc

decompiled_code:
__int64 __fastcall fdgetc(int a1)
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(a1, buf, 1uLL) <= 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int8)buf[0];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(a1, buf, 1uLL) <= 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int8)buf[0];
return result;
}

original_code:
static int fdgetc(int fd)
{
  unsigned char c;
  if (read(fd, &c, sizeof(c)) <= 0)
    return -1;
  return (int)c;
}

variable_types: fd int

variable_types:
c
unsigned char

return_type: int


decompiled_code:
void __fastcall passcheck_start(__int64 a1, int a2)
{
int v3; // eax
char *v4; // rax
const char *v5; // rbp
char *v6; // rax
int v7; // edi
int v8; // edi
__pid_t v9; // eax
int v10; // er15
__pid_t v11; // er12
_BOOL8 v12; // r13
__int64 v13; // r14
int v14; // edi
int v15; // eax
ssize_t v16; // rax
__m128i v17; // xmm0
__m128i v18; // xmm1
__m128i v19; // xmm2
__gid_t v20; // er13
__uid_t v21; // er12
__uid_t v22; // er14
int v23; // er8
int v24; // er15
int v25; // eax
int v26; // eax
int v27; // eax
ssize_t v28; // rax
__gid_t v29; // [rsp+8h] [rbp-E0h]
int buf; // [rsp+1Ch] [rbp-CCh] BYREF
int fd[2]; // [rsp+20h] [rbp-C8h] BYREF
int pipedes[2]; // [rsp+28h] [rbp-C0h] BYREF
struct termios termios_p; // [rsp+30h] [rbp-B8h] BYREF
struct termios v34; // [rsp+70h] [rbp-78h] BYREF

*(_DWORD *)(a1 + 16) = -1;
*(_QWORD *)fd = -1LL;
*(_QWORD *)pipedes = -1LL;
buf = 0;
if ( (a2 & 1) == 0 )
{
*(_DWORD *)(a1 + 20) = 0;
return;
}
v3 = a2 ^ 1;
*(_DWORD *)(a1 + 20) = a2 ^ 1;
if ( a2 != 1 )
{
if ( (v3 & 4) != 0 )
*(_DWORD *)(a1 + 20) = v3 & 0xFFFFFFFD;
v4 = getenv("LIBPASSPHRASE_METER");
v5 = v4;
if ( v4 )
{
if ( !*v4 )
v5 = "passcheck";
}
else
{
v5 = "passcheck";
}
v6 = getenv("LIBPASSPHRASE_STRENGTH_LABEL");
*(_QWORD *)a1 = v6;
if ( !v6 || !*v6 )
*(_QWORD *)a1 = "Strength:";
if ( pipe((int *)(a1 + 8)) )
{
*(_QWORD *)(a1 + 8) = -1LL;
LABEL_15:
if ( fd[0] >= 0 )
close(fd[0]);
if ( fd[1] >= 0 )
close(fd[1]);
if ( pipedes[0] >= 0 )
close(pipedes[0]);
if ( pipedes[1] >= 0 )
close(pipedes[1]);
*(_QWORD *)(a1 + 16) = 0xFFFFFFFFLL;
return;
}
if ( pipe(fd) )
{
*(_QWORD *)fd = -1LL;
LABEL_26:
v7 = *(_DWORD *)(a1 + 8);
if ( v7 >= 0 )
close(v7);
v8 = *(_DWORD *)(a1 + 12);
if ( v8 >= 0 )
close(v8);
goto LABEL_15;
}
if ( pipe(pipedes) )
{
*(_QWORD *)pipedes = -1LL;
goto LABEL_26;
}
if ( fcntl(pipedes[1], 2, 1LL) == -1 )
goto LABEL_26;
v9 = fork();
v10 = v9;
v11 = v9;
if ( v9 == -1 )
{
*(_DWORD *)(a1 + 20) = 0;
goto LABEL_26;
}
v12 = v9 != 0;
v13 = a1 + 4 * v12;
close(pipedes[v12]);
v14 = *(_DWORD *)(v13 + 8);
pipedes[v12] = -1;
close(v14);
*(_DWORD *)(v13 + 8) = -1;
close(fd[v10 == 0]);
fd[v10 == 0] = -1;
v15 = fd[v12];
fd[v12] = -1;
*(_DWORD *)(v13 + 8) = v15;
if ( v10 )
{
while ( 1 )
{
v16 = read(pipedes[0], &buf, 4uLL);
if ( v16 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_44;
}
if ( v16 )
{
LABEL_44:
while ( waitpid(v11, &buf, 0) == -1 && *_errno_location() == 4 )
;
goto LABEL_26;
}
if ( (*(_BYTE *)(a1 + 20) & 2) != 0 )
{
tcgetattr(2, &termios_p);
v17 = _mm_loadu_si128((const __m128i *)&termios_p);
v18 = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
v19 = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v34.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
*(__m128i *)&v34.c_iflag = v17;
*(__m128i *)&v34.c_line = v18;
*(__m128i *)&v34.c_cc[15] = v19;
termios_p.c_oflag &= 0xFFFFFFFB;
v34.c_ospeed = termios_p.c_ospeed;
tcsetattr(2, 2, &termios_p);
fwrite("\n\x1B[A", 1uLL, 4uLL, stderr);
fflush(stderr);
tcsetattr(2, 2, &v34);
}
close(pipedes[0]);
*(_DWORD *)(a1 + 16) = v11;
return;
}
v20 = getgid();
v29 = getegid();
v21 = getuid();
v22 = geteuid();
if ( *(_DWORD *)(a1 + 8) && !*(_DWORD *)(a1 + 12) )
{
v26 = dup(0);
if ( v26 == -1 )
goto LABEL_64;
*(_DWORD *)(a1 + 12) = v26;
}
buf = 0;
do
{
if ( *(_DWORD *)(a1 + 4LL * v10 + 8) == v10 )
{
v23 = buf;
}
else
{
close(v10);
v24 = dup2(*(_DWORD *)(a1 + 4LL * buf + 8), buf);
if ( v24 == -1 )
goto LABEL_64;
close(*(_DWORD *)(a1 + 4LL * buf + 8));
v25 = buf;
*(_DWORD *)(a1 + 4LL * buf + 8) = v24;
v23 = v25;
}
v10 = v23 + 1;
buf = v23 + 1;
}
while ( v23 + 1 <= 1 );
close(2);
if ( v20 == v29 || (v27 = setregid(v20, v20), !v20) || !v27 )
{
if ( v21 == v22 || !setreuid(v21, v21) || !v21 )
execlp(v5, v5, "-r", 0LL);
}
LABEL_64:
v28 = write(pipedes[1], &buf, 4uLL);
exit(v28 != 0);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, int a2)
{
int v3; // eax
char *v4; // rax
const char *v5; // rbp
char *v6; // rax
int v7; // edi
int v8; // edi
__pid_t v9; // eax
int v10; // er15
__pid_t v11; // er12
_BOOL8 v12; // r13
__int64 v13; // r14
int v14; // edi
int v15; // eax
ssize_t v16; // rax
__m128i v17; // xmm0
__m128i v18; // xmm1
__m128i v19; // xmm2
__gid_t v20; // er13
__uid_t v21; // er12
__uid_t v22; // er14
int v23; // er8
int v24; // er15
int v25; // eax
int v26; // eax
int v27; // eax
ssize_t v28; // rax
__gid_t v29; // [rsp+8h] [rbp-E0h]
int buf; // [rsp+1Ch] [rbp-CCh] BYREF
int fd[2]; // [rsp+20h] [rbp-C8h] BYREF
int pipedes[2]; // [rsp+28h] [rbp-C0h] BYREF
struct termios termios_p; // [rsp+30h] [rbp-B8h] BYREF
struct termios v34; // [rsp+70h] [rbp-78h] BYREF

*(_DWORD *)(a1 + 16) = -1;
*(_QWORD *)fd = -1LL;
*(_QWORD *)pipedes = -1LL;
buf = 0;
if ( (a2 & 1) == 0 )
{
*(_DWORD *)(a1 + 20) = 0;
return;
}
v3 = a2 ^ 1;
*(_DWORD *)(a1 + 20) = a2 ^ 1;
if ( a2 != 1 )
{
if ( (v3 & 4) != 0 )
*(_DWORD *)(a1 + 20) = v3 & 0xFFFFFFFD;
v4 = getenv("LIBPASSPHRASE_METER");
v5 = v4;
if ( v4 )
{
if ( !*v4 )
v5 = "passcheck";
}
else
{
v5 = "passcheck";
}
v6 = getenv("LIBPASSPHRASE_STRENGTH_LABEL");
*(_QWORD *)a1 = v6;
if ( !v6 || !*v6 )
*(_QWORD *)a1 = "Strength:";
if ( pipe((int *)(a1 + 8)) )
{
*(_QWORD *)(a1 + 8) = -1LL;
LABEL_15:
if ( fd[0] >= 0 )
close(fd[0]);
if ( fd[1] >= 0 )
close(fd[1]);
if ( pipedes[0] >= 0 )
close(pipedes[0]);
if ( pipedes[1] >= 0 )
close(pipedes[1]);
*(_QWORD *)(a1 + 16) = 0xFFFFFFFFLL;
return;
}
if ( pipe(fd) )
{
*(_QWORD *)fd = -1LL;
LABEL_26:
v7 = *(_DWORD *)(a1 + 8);
if ( v7 >= 0 )
close(v7);
v8 = *(_DWORD *)(a1 + 12);
if ( v8 >= 0 )
close(v8);
goto LABEL_15;
}
if ( pipe(pipedes) )
{
*(_QWORD *)pipedes = -1LL;
goto LABEL_26;
}
if ( fcntl(pipedes[1], 2, 1LL) == -1 )
goto LABEL_26;
v9 = fork();
v10 = v9;
v11 = v9;
if ( v9 == -1 )
{
*(_DWORD *)(a1 + 20) = 0;
goto LABEL_26;
}
v12 = v9 != 0;
v13 = a1 + 4 * v12;
close(pipedes[v12]);
v14 = *(_DWORD *)(v13 + 8);
pipedes[v12] = -1;
close(v14);
*(_DWORD *)(v13 + 8) = -1;
close(fd[v10 == 0]);
fd[v10 == 0] = -1;
v15 = fd[v12];
fd[v12] = -1;
*(_DWORD *)(v13 + 8) = v15;
if ( v10 )
{
while ( 1 )
{
v16 = read(pipedes[0], &buf, 4uLL);
if ( v16 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_44;
}
if ( v16 )
{
LABEL_44:
while ( waitpid(v11, &buf, 0) == -1 && *_errno_location() == 4 )
;
goto LABEL_26;
}
if ( (*(_BYTE *)(a1 + 20) & 2) != 0 )
{
tcgetattr(2, &termios_p);
v17 = _mm_loadu_si128((const __m128i *)&termios_p);
v18 = _mm_loadu_si128((const __m128i *)&termios_p.c_line);
v19 = _mm_loadu_si128((const __m128i *)&termios_p.c_cc[15]);
*(_QWORD *)&v34.c_cc[31] = *(_QWORD *)&termios_p.c_cc[31];
*(__m128i *)&v34.c_iflag = v17;
*(__m128i *)&v34.c_line = v18;
*(__m128i *)&v34.c_cc[15] = v19;
termios_p.c_oflag &= 0xFFFFFFFB;
v34.c_ospeed = termios_p.c_ospeed;
tcsetattr(2, 2, &termios_p);
fwrite("\n\x1B[A", 1uLL, 4uLL, stderr);
fflush(stderr);
tcsetattr(2, 2, &v34);
}
close(pipedes[0]);
*(_DWORD *)(a1 + 16) = v11;
return;
}
v20 = getgid();
v29 = getegid();
v21 = getuid();
v22 = geteuid();
if ( *(_DWORD *)(a1 + 8) && !*(_DWORD *)(a1 + 12) )
{
v26 = dup(0);
if ( v26 == -1 )
goto LABEL_64;
*(_DWORD *)(a1 + 12) = v26;
}
buf = 0;
do
{
if ( *(_DWORD *)(a1 + 4LL * v10 + 8) == v10 )
{
v23 = buf;
}
else
{
close(v10);
v24 = dup2(*(_DWORD *)(a1 + 4LL * buf + 8), buf);
if ( v24 == -1 )
goto LABEL_64;
close(*(_DWORD *)(a1 + 4LL * buf + 8));
v25 = buf;
*(_DWORD *)(a1 + 4LL * buf + 8) = v24;
v23 = v25;
}
v10 = v23 + 1;
buf = v23 + 1;
}
while ( v23 + 1 <= 1 );
close(2);
if ( v20 == v29 || (v27 = setregid(v20, v20), !v20) || !v27 )
{
if ( v21 == v22 || !setreuid(v21, v21) || !v21 )
execlp(v5, v5, "-r", 0LL);
}
LABEL_64:
v28 = write(pipedes[1], &buf, 4uLL);
exit(v28 != 0);
}
}

original_code:
static void passcheck_start(struct passcheck_state* state, int flags)
{
  const char* command;
  int pipe_rw[2] = { -1, -1 };
  int exec_rw[2] = { -1, -1 };
  pid_t pid;
  ssize_t n;
  int i = 0;
  state->pid = -1;
  state->flags = (flags & 1) ? (flags ^ 1) : 0;
  if (state->flags == 0)
    return;
  if (state->flags & 4)
    state->flags &= ~2;
  command = getenv("LIBPASSPHRASE_METER");
  if (!command || !*command)
    command = "passcheck";
  state->label = getenv("LIBPASSPHRASE_STRENGTH_LABEL");
  if (!(state->label) || !*(state->label))
    state->label = "Strength:";
  do { if (pipe(state->pipe_rw)) { state->pipe_rw[0] = state->pipe_rw[1] = -1; goto fail; } } while (0);
  do { if (pipe(pipe_rw)) { pipe_rw[0] = pipe_rw[1] = -1; goto fail; } } while (0);
  do { if (pipe(exec_rw)) { exec_rw[0] = exec_rw[1] = -1; goto fail; } } while (0);
  if (fcntl(exec_rw[1], 2, 1) == -1)
    goto fail;
  pid = fork();
  if (pid == -1)
    {
      state->flags = 0;
      goto fail;
    }
  close(exec_rw[!!pid]), exec_rw[!!pid] = -1;
  close(state->pipe_rw[!!pid]), state->pipe_rw[!!pid] = -1;
  close(pipe_rw[!pid]), pipe_rw[!pid] = -1;
  state->pipe_rw[!!pid] = pipe_rw[!!pid], pipe_rw[!!pid] = -1;
  if (pid == 0)
    {
      gid_t gid = getgid(), egid = getegid();
      uid_t uid = getuid(), euid = geteuid();
      int fd;
      if ((state->pipe_rw[0] != 0) && (state->pipe_rw[1] == 0))
 {
   fd = dup(state->pipe_rw[1]);
   if (fd == -1)
     goto child_fail;
   state->pipe_rw[1] = fd;
 }
      for (i = 0; i <= 1; i++)
 if (state->pipe_rw[i] != i)
   {
     close(i);
     fd = dup2(state->pipe_rw[i], i);
     if (fd == -1)
       goto child_fail;
     close(state->pipe_rw[i]);
     state->pipe_rw[i] = fd;
   }
      close(2);
      if (egid != gid)
 if (setregid(gid, gid) && gid)
   goto child_fail;
      if (euid != uid)
 if (setreuid(uid, uid) && uid)
   goto child_fail;
      execlp(command, command, "-r", ((void *)0));
    child_fail:
      n = write(exec_rw[1], &i, sizeof(i));
      _exit(!!n);
    }
 rewait:
  n = read(exec_rw[0], &i, sizeof(i));
  if ((n < 0) && ((*__errno_location ()) == 4))
    goto rewait;
  if (n)
    {
    rereap:
      if ((waitpid(pid, &i, 0) == -1) && ((*__errno_location ()) == 4))
 goto rereap;
      goto fail;
    }
  if (state->flags & 2)
    {
      struct termios stty;
      struct termios saved_stty;
      tcgetattr(2, &stty);
      saved_stty = stty;
      stty.c_oflag &= (tcflag_t)~0000004;
      tcsetattr(2, 2, &stty);
      fprintf(stderr, "\n\033[A");
      fflush(stderr);
      tcsetattr(2, 2, &saved_stty);
    }
  close(exec_rw[0]);
  state->pid = pid;
  return;
 fail:
  if (state->pipe_rw[0] >= 0) close(state->pipe_rw[0]);
  if (state->pipe_rw[1] >= 0) close(state->pipe_rw[1]);
  if (pipe_rw[0] >= 0) close(pipe_rw[0]);
  if (pipe_rw[1] >= 0) close(pipe_rw[1]);
  if (exec_rw[0] >= 0) close(exec_rw[0]);
  if (exec_rw[1] >= 0) close(exec_rw[1]);
  state->pid = -1;
  state->flags = 0;
}

canonical_original_code:
static void passcheck_start(struct passcheck_state *state, int flags)
{
  char *command;
  int pipe_rw[2] = { -1, -1 };
  int exec_rw[2] = { -1, -1 };
  int pid;
  long n;
  int i = 0;
  state->pid = -1;
  state->flags = (flags & 1) ? (flags ^ 1) : 0;
  if (state->flags == 0)
    return;
  if (state->flags & 4)
    state->flags &= ~2;
  command = getenv("LIBPASSPHRASE_METER");
  if (!command || !*command)
    command = "passcheck";
  state->label = getenv("LIBPASSPHRASE_STRENGTH_LABEL");
  if (!(state->label) || !*(state->label))
    state->label = "Strength:";
  do { if (pipe(state->pipe_rw)) { state->pipe_rw[0] = state->pipe_rw[1] = -1; goto fail; } } while (0);
  do { if (pipe(pipe_rw)) { pipe_rw[0] = pipe_rw[1] = -1; goto fail; } } while (0);
  do { if (pipe(exec_rw)) { exec_rw[0] = exec_rw[1] = -1; goto fail; } } while (0);
  if (fcntl(exec_rw[1], 2, 1) == -1)
    goto fail;
  pid = fork();
  if (pid == -1)
    {
      state->flags = 0;
      goto fail;
    }
  close(exec_rw[!!pid]), exec_rw[!!pid] = -1;
  close(state->pipe_rw[!!pid]), state->pipe_rw[!!pid] = -1;
  close(pipe_rw[!pid]), pipe_rw[!pid] = -1;
  state->pipe_rw[!!pid] = pipe_rw[!!pid], pipe_rw[!!pid] = -1;
  if (pid == 0)
    {
      unsigned int gid = getgid(); unsigned int egid = getegid();
      unsigned int uid = getuid(); unsigned int euid = geteuid();
      int fd;
      if ((state->pipe_rw[0] != 0) && (state->pipe_rw[1] == 0))
 {
   fd = dup(state->pipe_rw[1]);
   if (fd == -1)
     goto child_fail;
   state->pipe_rw[1] = fd;
 }
      for (i = 0; i <= 1; i++)
 if (state->pipe_rw[i] != i)
   {
     close(i);
     fd = dup2(state->pipe_rw[i], i);
     if (fd == -1)
       goto child_fail;
     close(state->pipe_rw[i]);
     state->pipe_rw[i] = fd;
   }
      close(2);
      if (egid != gid)
 if (setregid(gid, gid) && gid)
   goto child_fail;
      if (euid != uid)
 if (setreuid(uid, uid) && uid)
   goto child_fail;
      execlp(command, command, "-r", ((void *)0));
    child_fail:
      n = write(exec_rw[1], &i, sizeof(i));
      _exit(!!n);
    }
 rewait:
  n = read(exec_rw[0], &i, sizeof(i));
  if ((n < 0) && ((*__errno_location ()) == 4))
    goto rewait;
  if (n)
    {
    rereap:
      if ((waitpid(pid, &i, 0) == -1) && ((*__errno_location ()) == 4))
 goto rereap;
      goto fail;
    }
  if (state->flags & 2)
    {
      struct termios stty;
      struct termios saved_stty;
      tcgetattr(2, &stty);
      saved_stty = stty;
      stty.c_oflag &= (unsigned int)~0000004;
      tcsetattr(2, 2, &stty);
      fprintf(stderr, "\n\033[A");
      fflush(stderr);
      tcsetattr(2, 2, &saved_stty);
    }
  close(exec_rw[0]);
  state->pid = pid;
  return;
 fail:
  if (state->pipe_rw[0] >= 0) close(state->pipe_rw[0]);
  if (state->pipe_rw[1] >= 0) close(state->pipe_rw[1]);
  if (pipe_rw[0] >= 0) close(pipe_rw[0]);
  if (pipe_rw[1] >= 0) close(pipe_rw[1]);
  if (exec_rw[0] >= 0) close(exec_rw[0]);
  if (exec_rw[1] >= 0) close(exec_rw[1]);
  state->pid = -1;
  state->flags = 0;
}

variable_types: struct passcheck_state { char *label; int pipe_rw[2]; int pid; int flags; } *

variable_types: flags int

variable_types:
command
char *

variable_types: pid int

variable_types: n long

variable_types: i int

variable_types:
gid
unsigned int

variable_types:
egid
unsigned int

variable_types:
uid
unsigned int

variable_types:
euid
unsigned int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int __fastcall passcheck_stop(int *a1)
{
int result; // eax
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = a1[5];
if ( result )
{
close(a1[2]);
close(a1[3]);
free(strength);
strength = 0LL;
strength_size = 0LL;
while ( waitpid(a1[4], stat_loc, 0) == -1 && *_errno_location() == 4 )
;
if ( (a1[5] & 2) != 0 )
fwrite("\x1B[s\x1B[E\x1B[0K\x1B[u", 1uLL, 0xDuLL, stderr);
else
fwrite("\x1B[B\x1B[0K\x1B[A", 1uLL, 0xAuLL, stderr);
result = fflush(stderr);
a1[5] = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int *a1)
{
int result; // eax
int stat_loc[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = a1[5];
if ( result )
{
close(a1[2]);
close(a1[3]);
free(strength);
strength = 0LL;
strength_size = 0LL;
while ( waitpid(a1[4], stat_loc, 0) == -1 && *_errno_location() == 4 )
;
if ( (a1[5] & 2) != 0 )
fwrite("\x1B[s\x1B[E\x1B[0K\x1B[u", 1uLL, 0xDuLL, stderr);
else
fwrite("\x1B[B\x1B[0K\x1B[A", 1uLL, 0xAuLL, stderr);
result = fflush(stderr);
a1[5] = 0;
}
return result;
}

original_code:
static void passcheck_stop(struct passcheck_state* state)
{
  int _status;
  if (state->flags == 0)
    return;
  close(state->pipe_rw[0]);
  close(state->pipe_rw[1]);
  free(strength), strength = ((void *)0);
  strength_size = 0;
rereap:
  if ((waitpid(state->pid, &_status, 0) == -1) && ((*__errno_location ()) == 4))
    goto rereap;
  if (state->flags & 2)
    fprintf(stderr, "\033[s\033[E\033[0K\033[u");
  else
    fprintf(stderr, "\033[B\033[0K\033[A");
  fflush(stderr);
  state->flags = 0;
}

canonical_original_code:
static void passcheck_stop(struct passcheck_state *state)
{
  int _status;
  if (state->flags == 0)
    return;
  close(state->pipe_rw[0]);
  close(state->pipe_rw[1]);
  free(strength), strength = ((void *)0);
  strength_size = 0;
rereap:
  if ((waitpid(state->pid, &_status, 0) == -1) && ((*__errno_location ()) == 4))
    goto rereap;
  if (state->flags & 2)
    fprintf(stderr, "\033[s\033[E\033[0K\033[u");
  else
    fprintf(stderr, "\033[B\033[0K\033[A");
  fflush(stderr);
  state->flags = 0;
}

user_defined_types: struct passcheck_state { char *label; int pipe_rw[2]; int pid; int flags; }


decompiled_code:
int __fastcall passcheck_update(int *a1, const char *a2, size_t a3)
{
int result; // eax
int v4; // er13
size_t v6; // rbx
ssize_t v7; // rax
char *v8; // r15
size_t v9; // r12
size_t v10; // rbx
char *v11; // rax
ssize_t v12; // rax
char *v13; // rax
char *v14; // rax
const char *v15; // r13
int *v16; // rax
int *v17; // rbx
unsigned __int64 v18; // r13
ssize_t v19; // rax
const char *v20; // rdx

result = a1[5];
if ( result )
{
v4 = 0;
v6 = a3;
if ( !a3 )
goto LABEL_45;
while ( 1 )
{
do
{
while ( 1 )
{
v7 = write(a1[3], a2, v6);
if ( v7 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_22;
}
a2 += v7;
v6 -= v7;
}
while ( v6 );
if ( v4 == 1 )
break;
LABEL_45:
v4 = 1;
v6 = 1LL;
a2 = "\n";
}
v8 = strength;
v9 = 0LL;
LABEL_7:
while ( 2 )
{
v10 = strength_size;
if ( strength_size != v9 )
goto LABEL_12;
v10 = 2 * v9;
if ( !v9 )
v10 = 8LL;
LABEL_10:
strength_size = v10;
v11 = (char *)realloc(v8, v10);
v8 = v11;
if ( v11 )
{
strength = v11;
LABEL_12:
while ( 1 )
{
v12 = read(a1[2], &v8[v9], v10 + ~v9);
if ( v12 <= 0 )
break;
v8 = strength;
v9 += v12;
strength[v9] = 0;
v13 = strpbrk(v8, LC15);
if ( !v13 )
goto LABEL_7;
if ( *v13 == 27 )
{
v14 = strpbrk(v8, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm~");
if ( !v14 )
goto LABEL_22;
v15 = v14 + 1;
}
else
{
v15 = v8;
}
*strpbrk(v15, LC17) = 0;
v16 = _errno_location();
*v16 = 0;
v17 = v16;
v18 = strtoull(v15, 0LL, 10);
if ( !v18 && *v17 )
{
if ( *v17 == 34 )
{
v18 = -1LL;
goto LABEL_25;
}
goto LABEL_22;
}
while ( 1 )
{
LABEL_25:
if ( memchr(v8, 10, v9) )
{
v20 = "1;31mWell-known common password";
if ( v18 )
{
v20 = "31mExtremely week";
if ( v18 > 0x96 )
{
v20 = "33mWeek";
if ( v18 > 0xC8 )
{
v20 = "32mGood";
if ( v18 > 0xFA )
{
v20 = "1;32mStrong";
if ( v18 >= 0x15F )
v20 = "1;7;32mPerfect";
}
}
}
}
if ( (a1[5] & 2) != 0 )
fprintf(stderr, "\x1B[s\x1B[E\x1B[0K%s \x1B[%s\x1B[m (%lli)\x1B[u", *(const char **)a1, v20, v18);
else
fprintf(stderr, "\x1B[B\x1B[s\x1B[0K\x1B[%s\x1B[m (%lli)\x1B[u\x1B[A", v20, v18);
return fflush(stderr);
}
v19 = read(a1[2], v8, strength_size);
v9 = v19;
if ( v19 <= 0 )
break;
v8 = strength;
}
if ( !v19 || *v17 != 4 )
goto LABEL_22;
v9 = strength_size;
v8 = strength;
v10 = 8LL;
if ( !strength_size )
goto LABEL_10;
v10 = strength_size;
v9 = 0LL;
}
if ( v12 && *_errno_location() == 4 )
{
v8 = strength;
continue;
}
}
break;
}
LABEL_22:
result = passcheck_stop(a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int *a1, const char *a2, size_t a3)
{
int result; // eax
int v4; // er13
size_t v6; // rbx
ssize_t v7; // rax
char *v8; // r15
size_t v9; // r12
size_t v10; // rbx
char *v11; // rax
ssize_t v12; // rax
char *v13; // rax
char *v14; // rax
const char *v15; // r13
int *v16; // rax
int *v17; // rbx
unsigned __int64 v18; // r13
ssize_t v19; // rax
const char *v20; // rdx

result = a1[5];
if ( result )
{
v4 = 0;
v6 = a3;
if ( !a3 )
goto LABEL_45;
while ( 1 )
{
do
{
while ( 1 )
{
v7 = write(a1[3], a2, v6);
if ( v7 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_22;
}
a2 += v7;
v6 -= v7;
}
while ( v6 );
if ( v4 == 1 )
break;
LABEL_45:
v4 = 1;
v6 = 1LL;
a2 = "\n";
}
v8 = strength;
v9 = 0LL;
LABEL_7:
while ( 2 )
{
v10 = strength_size;
if ( strength_size != v9 )
goto LABEL_12;
v10 = 2 * v9;
if ( !v9 )
v10 = 8LL;
LABEL_10:
strength_size = v10;
v11 = (char *)realloc(v8, v10);
v8 = v11;
if ( v11 )
{
strength = v11;
LABEL_12:
while ( 1 )
{
v12 = read(a1[2], &v8[v9], v10 + ~v9);
if ( v12 <= 0 )
break;
v8 = strength;
v9 += v12;
strength[v9] = 0;
v13 = strpbrk(v8, LC15);
if ( !v13 )
goto LABEL_7;
if ( *v13 == 27 )
{
v14 = strpbrk(v8, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm~");
if ( !v14 )
goto LABEL_22;
v15 = v14 + 1;
}
else
{
v15 = v8;
}
*strpbrk(v15, LC17) = 0;
v16 = _errno_location();
*v16 = 0;
v17 = v16;
v18 = strtoull(v15, 0LL, 10);
if ( !v18 && *v17 )
{
if ( *v17 == 34 )
{
v18 = -1LL;
goto LABEL_25;
}
goto LABEL_22;
}
while ( 1 )
{
LABEL_25:
if ( memchr(v8, 10, v9) )
{
v20 = "1;31mWell-known common password";
if ( v18 )
{
v20 = "31mExtremely week";
if ( v18 > 0x96 )
{
v20 = "33mWeek";
if ( v18 > 0xC8 )
{
v20 = "32mGood";
if ( v18 > 0xFA )
{
v20 = "1;32mStrong";
if ( v18 >= 0x15F )
v20 = "1;7;32mPerfect";
}
}
}
}
if ( (a1[5] & 2) != 0 )
fprintf(stderr, "\x1B[s\x1B[E\x1B[0K%s \x1B[%s\x1B[m (%lli)\x1B[u", *(const char **)a1, v20, v18);
else
fprintf(stderr, "\x1B[B\x1B[s\x1B[0K\x1B[%s\x1B[m (%lli)\x1B[u\x1B[A", v20, v18);
return fflush(stderr);
}
v19 = read(a1[2], v8, strength_size);
v9 = v19;
if ( v19 <= 0 )
break;
v8 = strength;
}
if ( !v19 || *v17 != 4 )
goto LABEL_22;
v9 = strength_size;
v8 = strength;
v10 = 8LL;
if ( !strength_size )
goto LABEL_10;
v10 = strength_size;
v9 = 0LL;
}
if ( v12 && *_errno_location() == 4 )
{
v8 = strength;
continue;
}
}
break;
}
LABEL_22:
result = func2(a1);
}
return result;
}

original_code:
static void passcheck_update(struct passcheck_state* state, const char* passphrase, size_t len)
{
  size_t strength_ptr = 0;
  ssize_t n;
  int i;
  void* new;
  char* p;
  unsigned long long int value;
  const char* desc;
  if (state->flags == 0)
    return;
  for (i = 0; i < 2; i++, passphrase = "\n", len = 1)
    while (len)
      {
 n = write(state->pipe_rw[1], passphrase, len);
 if (n < 0)
   {
     if ((*__errno_location ()) == 4)
       continue;
     goto fail;
   }
 passphrase += (size_t)n;
 len -= (size_t)n;
      }
 again:
  if (strength_ptr == strength_size)
    {
      strength_size = strength_size ? (strength_size << 1) : 8;
      new = realloc(strength, strength_size * sizeof(char));
      if (new == ((void *)0))
 goto fail;
      strength = new;
    }
  n = read(state->pipe_rw[0], strength + strength_ptr, strength_size - 1 - strength_ptr);
  if (n <= 0)
    {
      if (n && ((*__errno_location ()) == 4))
 goto again;
      goto fail;
    }
  strength_ptr += (size_t)n;
  strength[strength_ptr] = '\0';
  p = strpbrk(strength, " \t\r\f\n\v");
  if (p == ((void *)0))
    goto again;
  if (*p == '\033')
    {
      p = strpbrk(strength, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm~");
      if (p == ((void *)0))
 goto fail;
      p++;
    }
  else
    p = strength;
  *(strpbrk(p, " \t\r\f\n\v\033")) = '\0';
  (*__errno_location ()) = 0;
  value = strtoull(p, ((void *)0), 10);
  if ((value == 0) && (*__errno_location ()))
    {
      if ((*__errno_location ()) != 34)
 goto fail;
      value = (0x7fffffffffffffffLL * 2ULL + 1ULL);
    }
  while (memchr(strength, '\n', strength_ptr) == ((void *)0))
    {
      strength_ptr = 0;
      n = read(state->pipe_rw[0], strength, strength_size);
      if (n <= 0)
 {
   if (n && ((*__errno_location ()) == 4))
     goto again;
   goto fail;
 }
      strength_ptr = (size_t)n;
    }
  strength_ptr = 0;
  if (0);
  else if (value == 0) desc = "1;31""m""Well-known common password"; else if (value <= 150) desc = "31""m""Extremely week"; else if (value <= 200) desc = "33""m""Week"; else if (value <= 250) desc = "32""m""Good"; else if (value <= 350) desc = "1;32""m""Strong"; else if (1) desc = "1;7;32""m""Perfect";
  if (state->flags & 2)
    fprintf(stderr, "\033[s\033[E\033[0K%s \033[%s\033[m (%lli)\033[u", state->label, desc, value);
  else
    fprintf(stderr, "\033[B\033[s\033[0K\033[%s\033[m (%lli)\033[u\033[A", desc, value);
  fflush(stderr);
  return;
 fail:
  passcheck_stop(state);
}

canonical_original_code:
static void passcheck_update(struct passcheck_state *state, char *passphrase, unsigned long len)
{
  unsigned long strength_ptr = 0;
  long n;
  int i;
  void *new;
  char *p;
  unsigned long long value;
  char *desc;
  if (state->flags == 0)
    return;
  for (i = 0; i < 2; i++, passphrase = "\n", len = 1)
    while (len)
      {
 n = write(state->pipe_rw[1], passphrase, len);
 if (n < 0)
   {
     if ((*__errno_location ()) == 4)
       continue;
     goto fail;
   }
 passphrase += (unsigned long)n;
 len -= (unsigned long)n;
      }
 again:
  if (strength_ptr == strength_size)
    {
      strength_size = strength_size ? (strength_size << 1) : 8;
      new = realloc(strength, strength_size * sizeof(char));
      if (new == ((void *)0))
 goto fail;
      strength = new;
    }
  n = read(state->pipe_rw[0], strength + strength_ptr, strength_size - 1 - strength_ptr);
  if (n <= 0)
    {
      if (n && ((*__errno_location ()) == 4))
 goto again;
      goto fail;
    }
  strength_ptr += (unsigned long)n;
  strength[strength_ptr] = '\0';
  p = strpbrk(strength, " \t\r\f\n\v");
  if (p == ((void *)0))
    goto again;
  if (*p == '\033')
    {
      p = strpbrk(strength, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm~");
      if (p == ((void *)0))
 goto fail;
      p++;
    }
  else
    p = strength;
  *(strpbrk(p, " \t\r\f\n\v\033")) = '\0';
  (*__errno_location ()) = 0;
  value = strtoull(p, ((void *)0), 10);
  if ((value == 0) && (*__errno_location ()))
    {
      if ((*__errno_location ()) != 34)
 goto fail;
      value = (0x7fffffffffffffffLL * 2ULL + 1ULL);
    }
  while (memchr(strength, '\n', strength_ptr) == ((void *)0))
    {
      strength_ptr = 0;
      n = read(state->pipe_rw[0], strength, strength_size);
      if (n <= 0)
 {
   if (n && ((*__errno_location ()) == 4))
     goto again;
   goto fail;
 }
      strength_ptr = (unsigned long)n;
    }
  strength_ptr = 0;
  if (0);
  else if (value == 0) desc = "1;31""m""Well-known common password"; else if (value <= 150) desc = "31""m""Extremely week"; else if (value <= 200) desc = "33""m""Week"; else if (value <= 250) desc = "32""m""Good"; else if (value <= 350) desc = "1;32""m""Strong"; else if (1) desc = "1;7;32""m""Perfect";
  if (state->flags & 2)
    fprintf(stderr, "\033[s\033[E\033[0K%s \033[%s\033[m (%lli)\033[u", state->label, desc, value);
  else
    fprintf(stderr, "\033[B\033[s\033[0K\033[%s\033[m (%lli)\033[u\033[A", desc, value);
  fflush(stderr);
  return;
 fail:
  passcheck_stop(state);
}

variable_types:
passphrase
char *

variable_types:
len
unsigned long

variable_types:
new
void *

variable_types:
p
char *

variable_types:
value
unsigned long long

variable_types:
desc
char *


decompiled_code:
char *__fastcall passphrase_read2(int a1, int a2)
{
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
size_t v5; // r13
int v6; // eax
int v8[22]; // [rsp+0h] [rbp-58h] BYREF

v2 = (char *)malloc(0x20uLL);
if ( !v2 )
return 0LL;
v3 = v2;
v4 = 0LL;
v5 = 32LL;
passcheck_start((__int64)v8, a2);
LABEL_3:
v6 = fdgetc(a1);
if ( v6 >= 0 )
{
while ( v6 != 10 )
{
if ( !v6 )
goto LABEL_3;
v3[v4] = v6;
passcheck_update(v8, v3, v4 + 1);
fflush(stderr);
if ( v5 == v4 + 1 )
{
v3 = (char *)xrealloc(v3, v5, 2 * v5);
if ( !v3 )
return 0LL;
v5 *= 2LL;
}
++v4;
v6 = fdgetc(a1);
if ( v6 < 0 )
break;
}
}
passcheck_stop(v8);
v3[v4] = 0;
fputc(10, stderr);
return v3;
}

canonical_decompiled_code:
char *__fastcall func5(int a1, int a2)
{
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
size_t v5; // r13
int v6; // eax
int v8[22]; // [rsp+0h] [rbp-58h] BYREF

v2 = (char *)malloc(0x20uLL);
if ( !v2 )
return 0LL;
v3 = v2;
v4 = 0LL;
v5 = 32LL;
func1((__int64)v8, a2);
LABEL_3:
v6 = func0(a1);
if ( v6 >= 0 )
{
while ( v6 != 10 )
{
if ( !v6 )
goto LABEL_3;
v3[v4] = v6;
func3(v8, v3, v4 + 1);
fflush(stderr);
if ( v5 == v4 + 1 )
{
v3 = (char *)func4(v3, v5, 2 * v5);
if ( !v3 )
return 0LL;
v5 *= 2LL;
}
++v4;
v6 = func0(a1);
if ( v6 < 0 )
break;
}
}
func2(v8);
v3[v4] = 0;
fputc(10, stderr);
return v3;
}

original_code:
char* passphrase_read2(int fdin, int flags)
{
  char* rc = malloc(32 * sizeof(char));
  size_t size = 32;
  size_t len = 0;
  int c;
  struct passcheck_state passcheck;
  if (rc == ((void *)0))
    return ((void *)0);
  passcheck_start(&passcheck, flags);
  for (;;)
    {
      c = fdgetc(fdin);
      if ((c < 0) || (c == '\n'))
 {
   passcheck_stop(&passcheck);
   break;
 }
      if (c == 0)
 continue;
      do { do { ; } while (0); *(rc + len++) = (char)c; do { ; } while (0); } while (0);
      passcheck_update(&passcheck, rc, len);
      do { fflush(stderr); } while (0);
      if (len == size)
 {
   if ((rc = xrealloc(rc, (size_t)size, (size_t)size << 1)) == ((void *)0))
     return ((void *)0);
   size <<= 1L;
 }
    }
  *(rc + len) = 0;
  fprintf(stderr, "\n");
  return rc;
}

canonical_original_code:
char *passphrase_read2(int fdin, int flags)
{
  char *rc = malloc(32 * sizeof(char));
  unsigned long size = 32;
  unsigned long len = 0;
  int c;
  struct passcheck_state passcheck;
  if (rc == ((void *)0))
    return ((void *)0);
  passcheck_start(&passcheck, flags);
  for (;;)
    {
      c = fdgetc(fdin);
      if ((c < 0) || (c == '\n'))
 {
   passcheck_stop(&passcheck);
   break;
 }
      if (c == 0)
 continue;
      do { do { ; } while (0); *(rc + len++) = (char)c; do { ; } while (0); } while (0);
      passcheck_update(&passcheck, rc, len);
      do { fflush(stderr); } while (0);
      if (len == size)
 {
   if ((rc = xrealloc(rc, (unsigned long)size, (unsigned long)size << 1)) == ((void *)0))
     return ((void *)0);
   size <<= 1L;
 }
    }
  *(rc + len) = 0;
  fprintf(stderr, "\n");
  return rc;
}

variable_types: fdin int

variable_types:
rc
char *

variable_types:
size
unsigned long

variable_types: c int

variable_types: struct passcheck_state { char *label; int pipe_rw[2]; int pid; int flags; }

return_type: char *


decompiled_code:
char *passphrase_read()
{
return passphrase_read2(0, 0);
}

canonical_decompiled_code:
char *func6()
{
return func5(0, 0);
}

original_code:
char* passphrase_read(void)
{
  return passphrase_read2(0, 0);
}


decompiled_code:
__int64 __fastcall passphrase_wipe(__int64 a1, __int64 a2)
{
return passphrase_explicit_memset________________(a1, 0LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return passphrase_explicit_memset________________(a1, 0LL, a2);
}

original_code:
__attribute__((optimize("-O0")))
void passphrase_wipe(char* ptr, size_t n)
{
  passphrase_explicit_memset________________(ptr, 0, n);
}

canonical_original_code:
void passphrase_wipe(char *ptr, unsigned long n)
{
  passphrase_explicit_memset________________(ptr, 0, n);
}

variable_types:
ptr
char *

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall passphrase_wipe1(const char *a1)
{
__int64 (__fastcall *v1)(const char *, _QWORD, size_t); // rbx
size_t v2; // rax

v1 = (__int64 (__fastcall *)(const char *, _QWORD, size_t))passphrase_explicit_memset________________;
v2 = strlen(a1);
return v1(a1, 0LL, v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
__int64 (__fastcall *v1)(const char *, _QWORD, size_t); // rbx
size_t v2; // rax

v1 = (__int64 (__fastcall *)(const char *, _QWORD, size_t))passphrase_explicit_memset________________;
v2 = strlen(a1);
return v1(a1, 0LL, v2);
}

original_code:
__attribute__((optimize("-O0")))
void passphrase_wipe1(char* ptr)
{
  passphrase_explicit_memset________________(ptr, 0, strlen(ptr));
}

canonical_original_code:
void passphrase_wipe1(char *ptr)
{
  passphrase_explicit_memset________________(ptr, 0, strlen(ptr));
}


cmalloc

repo: GRAVITYLab/OSUFlow

decompiled_code:
void *__fastcall cmalloc(int a1)
{
void *result; // rax

if ( !a1 )
return 0LL;
result = malloc(a1);
if ( !result )
_assert_fail("buf != NULL", "util.c", 0xFu, "cmalloc");
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
void *result; // rax

if ( !a1 )
return 0LL;
result = malloc(a1);
if ( !result )
_assert_fail("buf != NULL", "util.c", 0xFu, "cmalloc");
return result;
}

original_code:
void* cmalloc(int size) {
  if (size == 0) {
    return ((void *)0);
  }
  void* buf = malloc(size);
  ((void) sizeof ((buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (buf != ((void *)0)) ; else __assert_fail ("buf != NULL", "util.c", 15, __extension__ __PRETTY_FUNCTION__); }));
  return buf;
}

canonical_original_code:
void *cmalloc(int size) {
  if (size == 0) {
    return ((void *)0);
  }
  void *buf = malloc(size);
  ((void) sizeof ((buf != ((void *)0)) ? 1 : 0), __extension__ ({ if (buf != ((void *)0)) ; else __assert_fail ("buf != NULL", "util.c", 15, __extension__ __PRETTY_FUNCTION__); }));
  return buf;
}

variable_types: size int

variable_types:
buf
void *

return_type: void *


cfree

decompiled_code:
void __fastcall cfree(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void cfree(void* buf) {
  if (buf != ((void *)0)) {
    free(buf);
  }
}

canonical_original_code:
void cfree(void *buf) {
  if (buf != ((void *)0)) {
    free(buf);
  }
}


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

result = (unsigned int)(int)a1;
if ( (float)(a1 - (float)(int)result) > 0.0 )
result = (unsigned int)(int)(float)(a1 + 1.0);
return result;
}

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

result = (unsigned int)(int)a1;
if ( (float)(a1 - (float)(int)result) > 0.0 )
result = (unsigned int)(int)(float)(a1 + 1.0);
return result;
}

original_code:
int ceil_pos(float val) {
  return (val - (int)val > 0) ? (int)(val + 1) : (int)val;
}

variable_types: val float


bsearchx

original_code:
int bsearchx(void *key, void *item_data, int num_items, int item_size,
             int (*compare)(const void *, const void *)) {
  int cmp;
  char* base = (char*)item_data;
  size_t lim;
  char* item;
  if (num_items > 1 && compare(key, item_data + item_size) < 0) {
    int cmp = compare(key, item_data);
    if (cmp < 0) {
      return -1;
    } else if (cmp > 0) {
      return 1;
    } else {
      return 0;
    }
  }
  for (lim = num_items - 1; lim > 0; lim >>= 1) {
    item = base + (lim >> 1) * item_size;
    cmp = compare(key, item);
    if (cmp == 0) {
      return ((item - (char*)item_data) / item_size) * 2;
    }
    if (cmp > 0) {
      if (compare(key, item + item_size) < 0) {
        return (((item - (char*)item_data) / item_size) * 2) + 1;
      }
      base = item + item_size;
      lim--;
    }
  }
  cmp = compare(key, item_data + (item_size * (num_items - 1)));
  if (cmp > 0) {
    return (num_items * 2) - 1;
  } else if (cmp == 0) {
    return (num_items * 2) - 2;
  } else {
    return -1;
  }
}

canonical_original_code:
int bsearchx(void *key, void *item_data, int num_items, int item_size, int (*compare)(void *, void *)) {
  int cmp;
  char *base = (char*)item_data;
  unsigned long lim;
  char *item;
  if (num_items > 1 && compare(key, item_data + item_size) < 0) {
    int cmp = compare(key, item_data);
    if (cmp < 0) {
      return -1;
    } else if (cmp > 0) {
      return 1;
    } else {
      return 0;
    }
  }
  for (lim = num_items - 1; lim > 0; lim >>= 1) {
    item = base + (lim >> 1) * item_size;
    cmp = compare(key, item);
    if (cmp == 0) {
      return ((item - (char *)item_data) / item_size) * 2;
    }
    if (cmp > 0) {
      if (compare(key, item + item_size) < 0) {
        return (((item - (char *)item_data) / item_size) * 2) + 1;
      }
      base = item + item_size;
      lim--;
    }
  }
  cmp = compare(key, item_data + (item_size * (num_items - 1)));
  if (cmp > 0) {
    return (num_items * 2) - 1;
  } else if (cmp == 0) {
    return (num_items * 2) - 2;
  } else {
    return -1;
  }
}

variable_types:
key
void *

variable_types:
compare
int (*)(void *, void *)

variable_types: cmp int

variable_types:
base
char *

variable_types:
lim
unsigned long

variable_types:
item
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
const char *v4; // rcx
__int64 v5; // rdx
const char *v6; // rdx
unsigned int v7; // eax
const char *v8; // rdx
unsigned int v9; // eax
const char *v10; // rdx
void **v11; // rbp
int v12; // eax
const char *v13; // rdx
void *v14; // rax
void *v15; // r14
const char *v16; // rdx
void *v17; // r12
int v18; // eax
const char *v19; // rdx
void *v20; // rax
const char *v21; // rdx
unsigned int v22; // eax
int v23; // er14
__int64 v24; // r8
unsigned int i; // ebx
__int64 v26; // rsi
const char *v27; // rdx
unsigned int v28; // eax
int v29; // er14
__int64 v30; // r8
unsigned int j; // ebx
__int64 v32; // rsi
int result; // eax
char v34[168]; // [rsp+0h] [rbp-A8h] BYREF

v3 = 0LL;
if ( argc == 5 )
{
do
{
v4 = argv[v3];
v5 = (unsigned int)v3++;
printf("\n argc = %d , i = %d , argv[i] = %s ", 5LL, v5, v4);
}
while ( v3 != 5 );
v7 = calc_total_numb_of_elemts(argv[3], "rgv[i] = %s ", v6);
printf("\n\nTotal number of elements of set A = %d \n", v7);
v9 = calc_total_numb_of_elemts(argv[4], "rgv[i] = %s ", v8);
printf("Total number of elements of set B = %d \n", v9);
v11 = (void **)malloc(0x18uLL);
if ( v11 )
{
v12 = calc_total_numb_of_elemts(argv[3], "rgv[i] = %s ", v10);
v13 = argv[3];
*(_DWORD *)v11 = v12;
v11[1] = (void *)v13;
v14 = calloc(v12, 0x10uLL);
v11[2] = v14;
v15 = v14;
if ( v14 )
{
v17 = malloc(0x18uLL);
if ( v17 )
{
v18 = calc_total_numb_of_elemts(argv[4], "rgv[i] = %s ", v16);
v19 = argv[4];
*(_DWORD *)v17 = v18;
*((_QWORD *)v17 + 1) = v19;
v20 = calloc(v18, 0x10uLL);
*((_QWORD *)v17 + 2) = v20;
if ( v20 )
{
v22 = parse_elmt_of_set(v11, "rgv[i] = %s ", v21);
printf("\nparse_elmt_of_set for set A return : %d \n", v22);
v23 = *(_DWORD *)v11;
v34[0] = 0;
puts("\n Elements of set A : ");
if ( v23 > 0 )
{
for ( i = 0; i != v23; ++i )
{
while ( (unsigned int)get_element(v11, i, v34, "B = %d \n", v24) )
{
if ( v23 == ++i )
goto LABEL_12;
}
v26 = i;
printf("\nElement N %d = %s", v26, v34);
}
}
LABEL_12:
search_twin_elemts_into_set(v11);
puts("\n\nList of Twin elements for each element into set A:");
print_twin_elements(v11);
v28 = parse_elmt_of_set(v17, "rgv[i] = %s ", v27);
printf("\n\nparse_elmt_of_set for set B return : %d \n", v28);
v29 = *(_DWORD *)v17;
v34[0] = 0;
puts("\n Elements of set B : ");
if ( v29 > 0 )
{
for ( j = 0; j != v29; ++j )
{
while ( (unsigned int)get_element(v17, j, v34, "B = %d \n", v30) )
{
if ( v29 == ++j )
goto LABEL_17;
}
v32 = j;
printf("\nElement N %d = %s", v32, v34);
}
}
LABEL_17:
search_twin_elemts_into_set(v17);
puts("\n\nList of Twin elements for each element into set B:");
print_twin_elements(v17);
puts("\n A union B = ");
operation_union(v11, v17, stdout);
puts("\n A intersection B = ");
operation_intersection(v11, v17, stdout);
puts("\n A difference B = ");
operation_difference(v11, v17, stdout);
free(*((void **)v17 + 2));
free(v17);
free(v11[2]);
free(v11);
puts("\n");
result = 0;
}
else
{
free(v17);
free(v11[2]);
free(v11);
perror("\n ERROR : Bad memory allocation (phase 2) for secondary set ! \n ");
result = 1;
}
}
else
{
free(v15);
free(v11);
perror("\n ERROR : Bad memory allocation (phase 1) for secondary set ! \n ");
result = 1;
}
}
else
{
free(v11);
perror("\n ERROR : Bad memory allocation (phase 2) for primary set ! \n ");
result = 1;
}
}
else
{
perror("\n ERROR : Bad memory allocation (phase 1) for primary set ! \n ");
result = 1;
}
}
else
{
print_help(argc, argv, envp);
result = 1;
}
return result;
}

original_code:
int main( int argc, char* argv[], char* envp[] )
{
    int i = 0;
    int j = 0;
    char buf[128];
    int res = 0;
    struct tabl_elemts_of_set* tbl_elem_A = ((void *)0);
    struct tabl_elemts_of_set* tbl_elem_B = ((void *)0);
    if( argc != 5 )
    {
        print_help();
        return 1;
    }
    for( i = 0; i < argc; i++)
    {
        printf("\n argc = %d , i = %d , argv[i] = %s ", argc, i, *(argv +i) );
    }
    res = calc_total_numb_of_elemts( *(argv+3), ' ' );
    printf("\n\nTotal number of elements of set A = %d \n", res);
    res = calc_total_numb_of_elemts( *(argv+4), ' ' );
    printf("Total number of elements of set B = %d \n", res);
    tbl_elem_A = (tabl_elemts_of_set*) malloc( sizeof(tabl_elemts_of_set) );
    if( tbl_elem_A == ((void *)0) )
    {
        perror("\n ERROR : Bad memory allocation (phase 1) for primary set ! \n ");
        return 1;
    }
    tbl_elem_A->total_numb = calc_total_numb_of_elemts( *(argv+3), ' ' );
    tbl_elem_A->str_of_set = *(argv+3);
    tbl_elem_A->arr_elemts = ((void *)0);
    tbl_elem_A->arr_elemts = (some_elemt_in_str*) calloc(
                                                          (tbl_elem_A->total_numb),
                                                           sizeof(some_elemt_in_str) );
    if( (tbl_elem_A->arr_elemts) == ((void *)0) )
    {
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 2) for primary set ! \n ");
        return 1;
    }
    tbl_elem_B = (tabl_elemts_of_set*) malloc( sizeof(tabl_elemts_of_set) );
    if( tbl_elem_B == ((void *)0) )
    {
        free( (void*) (tbl_elem_A->arr_elemts) );
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 1) for secondary set ! \n ");
        return 1;
    }
    tbl_elem_B->total_numb = calc_total_numb_of_elemts( *(argv+4), ' ' );
    tbl_elem_B->str_of_set = *(argv+4);
    tbl_elem_B->arr_elemts = ((void *)0);
    tbl_elem_B->arr_elemts = (some_elemt_in_str*) calloc(
                                                         (tbl_elem_B->total_numb),
                                                           sizeof(some_elemt_in_str) );
    if( (tbl_elem_B->arr_elemts) == ((void *)0) )
    {
        free( tbl_elem_B );
        free( (void*) (tbl_elem_A->arr_elemts) );
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 2) for secondary set ! \n ");
        return 1;
    }
    res = parse_elmt_of_set( tbl_elem_A, ' ' );
    printf("\nparse_elmt_of_set  for set A return : %d \n", res);
    res = tbl_elem_A->total_numb;
    buf[0] = 0;
    printf("\n Elements of set A : \n");
    for( i = 0 ; i < res; i++)
    {
        j = get_element( tbl_elem_A, i, buf, 128 );
        if( j == 0 )
        {
            printf("\nElement N %d = %s", i, buf);
        }
    }
    res = search_twin_elemts_into_set( tbl_elem_A );
    printf("\n\nList of Twin elements for each element into set A:\n");
    print_twin_elements( tbl_elem_A );
    res = parse_elmt_of_set( tbl_elem_B, ' ' );
    printf("\n\nparse_elmt_of_set  for set B return : %d \n", res);
    res = tbl_elem_B->total_numb;
    buf[0] = 0;
    printf("\n Elements of set B : \n");
    for( i = 0 ; i < res; i++)
    {
        j = get_element( tbl_elem_B, i, buf, 128 );
        if( j == 0 )
        {
            printf("\nElement N %d = %s", i, buf);
        }
    }
    res = search_twin_elemts_into_set( tbl_elem_B );
    printf("\n\nList of Twin elements for each element into set B:\n");
    print_twin_elements( tbl_elem_B );
    printf("\n A union B = \n");
    res = operation_union( tbl_elem_A, tbl_elem_B, stdout );
    printf("\n A intersection B = \n");
    res = operation_intersection( tbl_elem_A, tbl_elem_B, stdout );
    printf("\n A difference B = \n");
    res = operation_difference( tbl_elem_A, tbl_elem_B, stdout );
    free( (void*) (tbl_elem_B->arr_elemts) );
    free( tbl_elem_B );
    free( (void*) (tbl_elem_A->arr_elemts) );
    free( tbl_elem_A );
    printf("\n\n");
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[])
{
    int i = 0;
    int j = 0;
    char buf[128];
    int res = 0;
    struct tabl_elemts_of_set *tbl_elem_A = ((void *)0);
    struct tabl_elemts_of_set *tbl_elem_B = ((void *)0);
    if( argc != 5 )
    {
        print_help();
        return 1;
    }
    for( i = 0; i < argc; i++)
    {
        printf("\n argc = %d , i = %d , argv[i] = %s ", argc, i, *(argv +i) );
    }
    res = calc_total_numb_of_elemts( *(argv+3), ' ' );
    printf("\n\nTotal number of elements of set A = %d \n", res);
    res = calc_total_numb_of_elemts( *(argv+4), ' ' );
    printf("Total number of elements of set B = %d \n", res);
    tbl_elem_A = (struct tabl_elemts_of_set *) malloc( sizeof(tabl_elemts_of_set) );
    if( tbl_elem_A == ((void *)0) )
    {
        perror("\n ERROR : Bad memory allocation (phase 1) for primary set ! \n ");
        return 1;
    }
    tbl_elem_A->total_numb = calc_total_numb_of_elemts( *(argv+3), ' ' );
    tbl_elem_A->str_of_set = *(argv+3);
    tbl_elem_A->arr_elemts = ((void *)0);
    tbl_elem_A->arr_elemts = (struct some_elemt_in_str *) calloc(
                                                          (tbl_elem_A->total_numb),
                                                           sizeof(some_elemt_in_str) );
    if( (tbl_elem_A->arr_elemts) == ((void *)0) )
    {
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 2) for primary set ! \n ");
        return 1;
    }
    tbl_elem_B = (struct tabl_elemts_of_set *) malloc( sizeof(tabl_elemts_of_set) );
    if( tbl_elem_B == ((void *)0) )
    {
        free( (void *) (tbl_elem_A->arr_elemts) );
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 1) for secondary set ! \n ");
        return 1;
    }
    tbl_elem_B->total_numb = calc_total_numb_of_elemts( *(argv+4), ' ' );
    tbl_elem_B->str_of_set = *(argv+4);
    tbl_elem_B->arr_elemts = ((void *)0);
    tbl_elem_B->arr_elemts = (struct some_elemt_in_str *) calloc(
                                                         (tbl_elem_B->total_numb),
                                                           sizeof(some_elemt_in_str) );
    if( (tbl_elem_B->arr_elemts) == ((void *)0) )
    {
        free( tbl_elem_B );
        free( (void *) (tbl_elem_A->arr_elemts) );
        free( tbl_elem_A );
        perror("\n ERROR : Bad memory allocation (phase 2) for secondary set ! \n ");
        return 1;
    }
    res = parse_elmt_of_set( tbl_elem_A, ' ' );
    printf("\nparse_elmt_of_set  for set A return : %d \n", res);
    res = tbl_elem_A->total_numb;
    buf[0] = 0;
    printf("\n Elements of set A : \n");
    for( i = 0 ; i < res; i++)
    {
        j = get_element( tbl_elem_A, i, buf, 128 );
        if( j == 0 )
        {
            printf("\nElement N %d = %s", i, buf);
        }
    }
    res = search_twin_elemts_into_set( tbl_elem_A );
    printf("\n\nList of Twin elements for each element into set A:\n");
    print_twin_elements( tbl_elem_A );
    res = parse_elmt_of_set( tbl_elem_B, ' ' );
    printf("\n\nparse_elmt_of_set  for set B return : %d \n", res);
    res = tbl_elem_B->total_numb;
    buf[0] = 0;
    printf("\n Elements of set B : \n");
    for( i = 0 ; i < res; i++)
    {
        j = get_element( tbl_elem_B, i, buf, 128 );
        if( j == 0 )
        {
            printf("\nElement N %d = %s", i, buf);
        }
    }
    res = search_twin_elemts_into_set( tbl_elem_B );
    printf("\n\nList of Twin elements for each element into set B:\n");
    print_twin_elements( tbl_elem_B );
    printf("\n A union B = \n");
    res = operation_union( tbl_elem_A, tbl_elem_B, stdout );
    printf("\n A intersection B = \n");
    res = operation_intersection( tbl_elem_A, tbl_elem_B, stdout );
    printf("\n A difference B = \n");
    res = operation_difference( tbl_elem_A, tbl_elem_B, stdout );
    free( (void *) (tbl_elem_B->arr_elemts) );
    free( tbl_elem_B );
    free( (void *) (tbl_elem_A->arr_elemts) );
    free( tbl_elem_A );
    printf("\n\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
envp
char *[]

variable_types: i int

variable_types: j int

variable_types:
buf
char [128]

variable_types: res int

variable_types: struct tabl_elemts_of_set { int total_numb; char *str_of_set; struct some_elemt_in_str { int pos_begin; int quantity_symb; int idx_prev_twin; int idx_next_twin; } *arr_elemts; } *

user_defined_types: struct tabl_elemts_of_set { int total_numb; char *str_of_set; struct some_elemt_in_str *arr_elemts; }


original_code:
int get_element( const tabl_elemts_of_set* tbl_elemts, int idx_elmt,
                                     char* val_elmt, const size_t size_val_el )
{
    int i = 0;
    int j = 0;
    int tot_numb_elm = 0;
    int beg_elm = 0;
    int end_elm = 0;
    int quant_symb = 0;
    int size_buf = 0;
    if( tbl_elemts == ((void *)0) ) return -1;
    tot_numb_elm = tbl_elemts->total_numb;
    if( tot_numb_elm == 0 ) return 0;
    if( (tbl_elemts->str_of_set) == ((void *)0) ) return -2;
    if( idx_elmt > (tot_numb_elm - 1) ) return -3;
    if( val_elmt == ((void *)0) ) return -4;
    if( size_val_el == 0 ) return -5;
    beg_elm = ((tbl_elemts->arr_elemts) + idx_elmt)->pos_begin;
    quant_symb = ((tbl_elemts->arr_elemts) + idx_elmt)->quantity_symb;
    end_elm = beg_elm + quant_symb;
    size_buf = size_val_el - 1;
    j = 0;
    for( i = beg_elm; i < end_elm; i++ )
    {
        if( j < size_buf )
        {
            *(val_elmt + j++) = *((tbl_elemts->str_of_set) + i);
        }
    }
    *(val_elmt + j) = 0;
    return 0;
}

canonical_original_code:
int get_element(struct tabl_elemts_of_set *tbl_elemts, int idx_elmt, char *val_elmt, unsigned long size_val_el)
{
    int i = 0;
    int j = 0;
    int tot_numb_elm = 0;
    int beg_elm = 0;
    int end_elm = 0;
    int quant_symb = 0;
    int size_buf = 0;
    if( tbl_elemts == ((void *)0) ) return -1;
    tot_numb_elm = tbl_elemts->total_numb;
    if( tot_numb_elm == 0 ) return 0;
    if( (tbl_elemts->str_of_set) == ((void *)0) ) return -2;
    if( idx_elmt > (tot_numb_elm - 1) ) return -3;
    if( val_elmt == ((void *)0) ) return -4;
    if( size_val_el == 0 ) return -5;
    beg_elm = ((tbl_elemts->arr_elemts) + idx_elmt)->pos_begin;
    quant_symb = ((tbl_elemts->arr_elemts) + idx_elmt)->quantity_symb;
    end_elm = beg_elm + quant_symb;
    size_buf = size_val_el - 1;
    j = 0;
    for( i = beg_elm; i < end_elm; i++ )
    {
        if( j < size_buf )
        {
            *(val_elmt + j++) = *((tbl_elemts->str_of_set) + i);
        }
    }
    *(val_elmt + j) = 0;
    return 0;
}

user_defined_types: struct some_elemt_in_str { int pos_begin; int quantity_symb; int idx_prev_twin; int idx_next_twin; }


original_code:
int parse_elmt_of_set( const tabl_elemts_of_set* tbl_elemts,
                                                     const char separator )
{
    int i, j;
    int len_str = 0;
    int count_elem = 0;
    const char* str = ((void *)0);
    char current = 0;
    enum { CHARS_OF_SEPARATOR, CHARS_OF_ELEM } state = CHARS_OF_SEPARATOR;
    if( tbl_elemts == ((void *)0) ) return -1;
    if( (tbl_elemts->total_numb) == 0 ) return -2;
    len_str = strlen( tbl_elemts->str_of_set );
    str = tbl_elemts->str_of_set;
    for( i = 0; i < len_str; i++ )
    {
        current = *(str + i);
        switch( state )
        {
            case CHARS_OF_SEPARATOR :
            {
                if( current != separator )
                {
                    state = CHARS_OF_ELEM;
                    ((tbl_elemts->arr_elemts) + count_elem)->pos_begin = i;
                    j = 1;
                }
                break;
            }
            case CHARS_OF_ELEM :
            {
                if( current == separator )
                {
                    state = CHARS_OF_SEPARATOR;
                    ((tbl_elemts->arr_elemts) + count_elem)->quantity_symb = j;
                    count_elem++;
                }
                else
                {
                    j++;
                }
                break;
            }
        }
    }
    if( state == CHARS_OF_ELEM )
    {
        ((tbl_elemts->arr_elemts) + count_elem)->quantity_symb = j;
        count_elem++;
    }
    return count_elem;
}

canonical_original_code:
int parse_elmt_of_set(struct tabl_elemts_of_set *tbl_elemts, char separator)
{
    int i; int j;
    int len_str = 0;
    int count_elem = 0;
    char *str = ((void *)0);
    char current = 0;
    enum { CHARS_OF_SEPARATOR=0, CHARS_OF_ELEM=1 } state = CHARS_OF_SEPARATOR;
    if( tbl_elemts == ((void *)0) ) return -1;
    if( (tbl_elemts->total_numb) == 0 ) return -2;
    len_str = strlen( tbl_elemts->str_of_set );
    str = tbl_elemts->str_of_set;
    for( i = 0; i < len_str; i++ )
    {
        current = *(str + i);
        switch( state )
        {
            case CHARS_OF_SEPARATOR :
            {
                if( current != separator )
                {
                    state = CHARS_OF_ELEM;
                    ((tbl_elemts->arr_elemts) + count_elem)->pos_begin = i;
                    j = 1;
                }
                break;
            }
            case CHARS_OF_ELEM :
            {
                if( current == separator )
                {
                    state = CHARS_OF_SEPARATOR;
                    ((tbl_elemts->arr_elemts) + count_elem)->quantity_symb = j;
                    count_elem++;
                }
                else
                {
                    j++;
                }
                break;
            }
        }
    }
    if( state == CHARS_OF_ELEM )
    {
        ((tbl_elemts->arr_elemts) + count_elem)->quantity_symb = j;
        count_elem++;
    }
    return count_elem;
}

variable_types: separator char

variable_types:
str
char *

variable_types: current char


decompiled_code:
__int64 __fastcall calc_total_numb_of_elemts(const char *a1, char a2)
{
int v3; // eax
int v4; // esi
int v5; // ecx
unsigned int v6; // er8
int i; // eax
char v8; // dl
__int64 result; // rax

v3 = strlen(a1);
if ( v3 <= 0 )
return 0LL;
v4 = v3;
v5 = 0;
v6 = 0;
for ( i = 0; i < v4; ++i )
{
v8 = a1[i];
if ( v5 != 1 )
{
if ( a2 == v8 )
goto LABEL_13;
goto LABEL_5;
}
while ( 1 )
{
v5 = 1;
if ( a2 != v8 )
goto LABEL_8;
++i;
++v6;
if ( v4 <= i )
goto LABEL_10;
if ( a2 == a1[i] )
break;
LABEL_5:
if ( v4 <= ++i )
goto LABEL_14;
v8 = a1[i];
}
LABEL_13:
v5 = 0;
LABEL_8:
;
}
if ( v5 == 1 )
LABEL_14:
result = v6 + 1;
else
LABEL_10:
result = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, char a2)
{
int v3; // eax
int v4; // esi
int v5; // ecx
unsigned int v6; // er8
int i; // eax
char v8; // dl
__int64 result; // rax

v3 = strlen(a1);
if ( v3 <= 0 )
return 0LL;
v4 = v3;
v5 = 0;
v6 = 0;
for ( i = 0; i < v4; ++i )
{
v8 = a1[i];
if ( v5 != 1 )
{
if ( a2 == v8 )
goto LABEL_13;
goto LABEL_5;
}
while ( 1 )
{
v5 = 1;
if ( a2 != v8 )
goto LABEL_8;
++i;
++v6;
if ( v4 <= i )
goto LABEL_10;
if ( a2 == a1[i] )
break;
LABEL_5:
if ( v4 <= ++i )
goto LABEL_14;
v8 = a1[i];
}
LABEL_13:
v5 = 0;
LABEL_8:
;
}
if ( v5 == 1 )
LABEL_14:
result = v6 + 1;
else
LABEL_10:
result = v6;
return result;
}

original_code:
int calc_total_numb_of_elemts( const char* const str_of_set,
                                                         const char separator )
{
    int i;
    int len_str;
    int tot_numb_of_elemts = 0;
    char current = 0;
    enum { CHARS_OF_SEPARATOR, CHARS_OF_ELEM } state = CHARS_OF_SEPARATOR;
    len_str = strlen( str_of_set );
    for( i = 0; i < len_str; i++ )
    {
        current = *(str_of_set + i);
        switch( state )
        {
            case CHARS_OF_SEPARATOR :
            {
                if( current != separator )
                {
                    state = CHARS_OF_ELEM;
                }
                break;
            }
            case CHARS_OF_ELEM :
            {
                if( current == separator )
                {
                    state = CHARS_OF_SEPARATOR;
                    tot_numb_of_elemts++;
                }
                break;
            }
        }
    }
    if( state == CHARS_OF_ELEM ) tot_numb_of_elemts++;
    return tot_numb_of_elemts;
}

canonical_original_code:
int calc_total_numb_of_elemts(char *str_of_set, char separator)
{
    int i;
    int len_str;
    int tot_numb_of_elemts = 0;
    char current = 0;
    enum { CHARS_OF_SEPARATOR=0, CHARS_OF_ELEM=1 } state = CHARS_OF_SEPARATOR;
    len_str = strlen( str_of_set );
    for( i = 0; i < len_str; i++ )
    {
        current = *(str_of_set + i);
        switch( state )
        {
            case CHARS_OF_SEPARATOR :
            {
                if( current != separator )
                {
                    state = CHARS_OF_ELEM;
                }
                break;
            }
            case CHARS_OF_ELEM :
            {
                if( current == separator )
                {
                    state = CHARS_OF_SEPARATOR;
                    tot_numb_of_elemts++;
                }
                break;
            }
        }
    }
    if( state == CHARS_OF_ELEM ) tot_numb_of_elemts++;
    return tot_numb_of_elemts;
}


decompiled_code:
int print_help()
{
return puts("\nUsage : wrksets -p <union | intersection | difference> \"set A\" \"set B\" \n");
}

canonical_decompiled_code:
int func0()
{
return puts("\nUsage : wrksets -p <union | intersection | difference> \"set A\" \"set B\" \n");
}

original_code:
void print_help()
{
    printf("\nUsage :  wrksets -p <union | intersection | " "difference> \"set A\" \"set B\" \n\n");
    return;
}


original_code:
void print_twin_elements( tabl_elemts_of_set* tbl_elemts )
{
    int i = 0;
    int j = 0;
    int k = 0;
    int N = 0;
    int beg_elm = 0;
    int len_elm = 0;
    const char* elemt = ((void *)0);
    N = tbl_elemts->total_numb;
    if( !N ) return;
    for( i = 0; i < N; i++ )
    {
 if( ((tbl_elemts->arr_elemts + i)->idx_prev_twin) == -1 )
 {
     printf("\n");
     j = i;
     beg_elm = (tbl_elemts->arr_elemts + j)->pos_begin;
     elemt = tbl_elemts->str_of_set + beg_elm;
            len_elm = (tbl_elemts->arr_elemts + j)->quantity_symb;
     print_element_by_chars( elemt, len_elm, stdout);
     printf(" : ");
     while( (tbl_elemts->arr_elemts + j)->idx_next_twin != -1 )
     {
  k = (tbl_elemts->arr_elemts + j)->idx_next_twin;
  j = k;
  beg_elm = (tbl_elemts->arr_elemts + j)->pos_begin;
         elemt = tbl_elemts->str_of_set + beg_elm;
                len_elm = (tbl_elemts->arr_elemts + j)->quantity_symb;
  print_element_by_chars( elemt, len_elm, stdout);
         printf(" ; ");
     }
 }
    }
    printf("\n");
    return;
}

canonical_original_code:
void print_twin_elements(struct tabl_elemts_of_set *tbl_elemts)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int N = 0;
    int beg_elm = 0;
    int len_elm = 0;
    char *elemt = ((void *)0);
    N = tbl_elemts->total_numb;
    if( !N ) return;
    for( i = 0; i < N; i++ )
    {
 if( ((tbl_elemts->arr_elemts + i)->idx_prev_twin) == -1 )
 {
     printf("\n");
     j = i;
     beg_elm = (tbl_elemts->arr_elemts + j)->pos_begin;
     elemt = tbl_elemts->str_of_set + beg_elm;
            len_elm = (tbl_elemts->arr_elemts + j)->quantity_symb;
     print_element_by_chars( elemt, len_elm, stdout);
     printf(" : ");
     while( (tbl_elemts->arr_elemts + j)->idx_next_twin != -1 )
     {
  k = (tbl_elemts->arr_elemts + j)->idx_next_twin;
  j = k;
  beg_elm = (tbl_elemts->arr_elemts + j)->pos_begin;
         elemt = tbl_elemts->str_of_set + beg_elm;
                len_elm = (tbl_elemts->arr_elemts + j)->quantity_symb;
  print_element_by_chars( elemt, len_elm, stdout);
         printf(" ; ");
     }
 }
    }
    printf("\n");
    return;
}

variable_types: k int

variable_types: N int

variable_types:
elemt
char *


original_code:
int search_twin_elemts_into_set( tabl_elemts_of_set* tbl_elemts )
{
    int i = 0;
    int j = 0;
    int N = 0;
    int res = 0;
    int len_elm_1 = 0;
    int len_elm_2 = 0;
    int beg_elm_1 = 0;
    int beg_elm_2 = 0;
    int adjac_twin = 0;
    const char* elemt_1 = ((void *)0);
    const char* elemt_2 = ((void *)0);
    N = tbl_elemts->total_numb;
    if( N == 0 ) return 0;
    for( i = 0; i < N; i++ )
    {
        if( ((tbl_elemts->arr_elemts + i)->idx_prev_twin) ==
     ((tbl_elemts->arr_elemts + i)->idx_next_twin)
   )
 {
     adjac_twin = i;
     (tbl_elemts->arr_elemts + i)->idx_prev_twin = -1;
     (tbl_elemts->arr_elemts + i)->idx_next_twin = -1;
     beg_elm_1 = (tbl_elemts->arr_elemts + i)->pos_begin;
     elemt_1 = tbl_elemts->str_of_set + beg_elm_1;
            len_elm_1 = (tbl_elemts->arr_elemts + i)->quantity_symb;
     for( j = (i + 1); j < N; j++ )
     {
  len_elm_2 = (tbl_elemts->arr_elemts + j)->quantity_symb;
         if( len_elm_1 == len_elm_2 )
         {
             beg_elm_2 = (tbl_elemts->arr_elemts + j)->pos_begin;
             elemt_2 = tbl_elemts->str_of_set + beg_elm_2;
             res = strncmp( elemt_1, elemt_2, len_elm_1 );
             if( !res )
             {
          (tbl_elemts->arr_elemts + j)->idx_prev_twin = adjac_twin;
   (tbl_elemts->arr_elemts + j)->idx_next_twin = -1;
   (tbl_elemts->arr_elemts + adjac_twin)->idx_next_twin = j;
   adjac_twin = j;
             }
         }
     }
 }
    }
    return 0;
}

canonical_original_code:
int search_twin_elemts_into_set(struct tabl_elemts_of_set *tbl_elemts)
{
    int i = 0;
    int j = 0;
    int N = 0;
    int res = 0;
    int len_elm_1 = 0;
    int len_elm_2 = 0;
    int beg_elm_1 = 0;
    int beg_elm_2 = 0;
    int adjac_twin = 0;
    char *elemt_1 = ((void *)0);
    char *elemt_2 = ((void *)0);
    N = tbl_elemts->total_numb;
    if( N == 0 ) return 0;
    for( i = 0; i < N; i++ )
    {
        if( ((tbl_elemts->arr_elemts + i)->idx_prev_twin) ==
     ((tbl_elemts->arr_elemts + i)->idx_next_twin)
   )
 {
     adjac_twin = i;
     (tbl_elemts->arr_elemts + i)->idx_prev_twin = -1;
     (tbl_elemts->arr_elemts + i)->idx_next_twin = -1;
     beg_elm_1 = (tbl_elemts->arr_elemts + i)->pos_begin;
     elemt_1 = tbl_elemts->str_of_set + beg_elm_1;
            len_elm_1 = (tbl_elemts->arr_elemts + i)->quantity_symb;
     for( j = (i + 1); j < N; j++ )
     {
  len_elm_2 = (tbl_elemts->arr_elemts + j)->quantity_symb;
         if( len_elm_1 == len_elm_2 )
         {
             beg_elm_2 = (tbl_elemts->arr_elemts + j)->pos_begin;
             elemt_2 = tbl_elemts->str_of_set + beg_elm_2;
             res = strncmp( elemt_1, elemt_2, len_elm_1 );
             if( !res )
             {
          (tbl_elemts->arr_elemts + j)->idx_prev_twin = adjac_twin;
   (tbl_elemts->arr_elemts + j)->idx_next_twin = -1;
   (tbl_elemts->arr_elemts + adjac_twin)->idx_next_twin = j;
   adjac_twin = j;
             }
         }
     }
 }
    }
    return 0;
}


RunErrorHandler

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

v1 = *(_DWORD *)(a1 + 272);
if ( v1 )
{
*(_DWORD *)(a1 + 272) = v1 - 1;
syslog(3, "Errors encountered");
result = 0LL;
}
else
{
syslog(3, "Too many errors, closing connection");
result = 0xFFFFFFFFLL;
}
return result;
}

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

v1 = *(_DWORD *)(a1 + 272);
if ( v1 )
{
*(_DWORD *)(a1 + 272) = v1 - 1;
syslog(3, "Errors encountered");
result = 0LL;
}
else
{
syslog(3, "Too many errors, closing connection");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int RunErrorHandler(struct Transaction *t)
{
  if ( t->errors ) {
    t->errors--;
    syslog(3,"Errors encountered");
    return 0;
  } else {
    syslog(3,"Too many errors, closing connection");
    return -1;
  }
}

variable_types: struct Transaction { int socket; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *address; char client_name[256]; int errors; int file; long start_time; int byte_count; } *

user_defined_types: struct Transaction { int socket; struct sockaddr_storage *address; char client_name[256]; int errors; int file; long start_time; int byte_count; }


decompiled_code:
__int64 clean_up_child_process()
{
__int64 result; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

wait((__WAIT_STATUS)&stat_loc);
result = LODWORD(stat_loc.__uptr);
child_exit_status = (int)stat_loc.__uptr;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

wait((__WAIT_STATUS)&stat_loc);
result = LODWORD(stat_loc.__uptr);
child_exit_status = (int)stat_loc.__uptr;
return result;
}

original_code:
void clean_up_child_process (int signal_number)
{
  int status;
  wait (&status);
  child_exit_status = status;
}

canonical_original_code:
void clean_up_child_process(int signal_number)
{
  int status;
  wait (&status);
  child_exit_status = status;
}

variable_types: status int


PrintTransactionTime

decompiled_code:
void __fastcall PrintTransactionTime(__int64 a1)
{
int v1; // ebx
const char *v2; // rsi
int v3; // eax

v1 = time(0LL) - *(_DWORD *)(a1 + 280);
if ( !v1 )
v1 = 1;
syslog(3, "%s: Transfer complete, %d bytes in %d seconds", a1 + 16, *(unsigned int *)(a1 + 288), (unsigned int)v1);
v2 = "%d MB/s";
v3 = *(_DWORD *)(a1 + 288) / v1;
if ( v3 <= 1000000 )
{
v2 = "%d kB/s";
if ( v3 <= 1000 )
v2 = "%d B/s";
}
syslog(3, v2, (unsigned int)v3);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
int v1; // ebx
const char *v2; // rsi
int v3; // eax

v1 = time(0LL) - *(_DWORD *)(a1 + 280);
if ( !v1 )
v1 = 1;
syslog(3, "%s: Transfer complete, %d bytes in %d seconds", a1 + 16, *(unsigned int *)(a1 + 288), (unsigned int)v1);
v2 = "%d MB/s";
v3 = *(_DWORD *)(a1 + 288) / v1;
if ( v3 <= 1000000 )
{
v2 = "%d kB/s";
if ( v3 <= 1000 )
v2 = "%d B/s";
}
syslog(3, v2, (unsigned int)v3);
}

original_code:
static void PrintTransactionTime(struct Transaction *t)
{
  int diff = time(((void *)0)) - t->start_time;
  if ( diff == 0 )
    diff = 1;
  syslog(3,"%s: Transfer complete, %d bytes in %d seconds", t->client_name, t->byte_count, diff);
  int rate = t->byte_count / diff;
  if ( rate > 1000000 ) {
    syslog(3,"%d MB/s", rate);
  } else if ( rate > 1000 ) {
    syslog(3,"%d kB/s", rate);
  } else {
    syslog(3,"%d B/s", rate);
  }
}

variable_types: diff int

variable_types: rate int

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }


decompiled_code:
__int64 __fastcall Trans_SetupSocket(__int64 a1, _WORD *a2)
{
int v2; // eax
unsigned int v3; // er12
_WORD *v4; // rax
socklen_t v5; // ecx

*(_QWORD *)(a1 + 8) = a2;
v2 = socket(2, 2, 0);
*(_DWORD *)a1 = v2;
if ( v2 == -1 )
{
v3 = -1;
syslog(3, "listner: socket");
}
else
{
v3 = 0;
v4 = get_in_addr(a2);
inet_ntop((unsigned __int16)*a2, v4, (char *)(a1 + 16), v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _WORD *a2)
{
int v2; // eax
unsigned int v3; // er12
_WORD *v4; // rax
socklen_t v5; // ecx

*(_QWORD *)(a1 + 8) = a2;
v2 = socket(2, 2, 0);
*(_DWORD *)a1 = v2;
if ( v2 == -1 )
{
v3 = -1;
syslog(3, "listner: socket");
}
else
{
v3 = 0;
v4 = get_in_addr(a2);
inet_ntop((unsigned __int16)*a2, v4, (char *)(a1 + 16), v5);
}
return v3;
}

original_code:
int Trans_SetupSocket(struct Transaction *t, struct sockaddr_storage *address)
{
  t->address = address;
  if ((t->socket = socket(2, SOCK_DGRAM, IPPROTO_IP)) == -1) {
    syslog(3,"listner: socket");
    return -1;
  }
  inet_ntop(address->ss_family,get_in_addr((struct sockaddr *)address),t->client_name, sizeof (t->client_name));
  return 0;
}

variable_types:
address
struct sockaddr_storage *


decompiled_code:
ssize_t __fastcall TFTP_Send_Error(__int64 a1, int a2)
{
const char *v2; // rsi
const struct sockaddr *v3; // rbp
size_t v4; // rax
__int16 v6; // [rsp+0h] [rbp-228h] BYREF
char v7; // [rsp+2h] [rbp-226h]
char v8; // [rsp+3h] [rbp-225h]
char dest[548]; // [rsp+4h] [rbp-224h] BYREF

v8 = a2;
v2 = (&ErrorMsg)[a2];
v6 = 768;
v7 = 0;
strcpy(dest, v2);
v3 = *(const struct sockaddr **)(a1 + 8);
v4 = strlen(dest);
return sendto(*(_DWORD *)a1, &v6, v4 + 5, 0, v3, 0x10u);
}

canonical_decompiled_code:
ssize_t __fastcall func5(__int64 a1, int a2)
{
const char *v2; // rsi
const struct sockaddr *v3; // rbp
size_t v4; // rax
__int16 v6; // [rsp+0h] [rbp-228h] BYREF
char v7; // [rsp+2h] [rbp-226h]
char v8; // [rsp+3h] [rbp-225h]
char dest[548]; // [rsp+4h] [rbp-224h] BYREF

v8 = a2;
v2 = (&ErrorMsg)[a2];
v6 = 768;
v7 = 0;
strcpy(dest, v2);
v3 = *(const struct sockaddr **)(a1 + 8);
v4 = strlen(dest);
return sendto(*(_DWORD *)a1, &v6, v4 + 5, 0, v3, 0x10u);
}

original_code:
void TFTP_Send_Error(struct Transaction *t, int error_type)
{
  char buf[(512+2+2)];
  buf[0] = 0;
  buf[1] = 3;
  buf[2] = 0;
  buf[3] = error_type;
  strcpy(buf+4, ErrorMsg[error_type]);
  sendto(t->socket, buf, strlen(buf+4)+5, 0, (const struct sockaddr *) t->address, sizeof(struct sockaddr));
}

canonical_original_code:
void TFTP_Send_Error(struct Transaction *t, int error_type)
{
  char buf[-1];
  buf[0] = 0;
  buf[1] = 3;
  buf[2] = 0;
  buf[3] = error_type;
  strcpy(buf+4, ErrorMsg[error_type]);
  sendto(t->socket, buf, strlen(buf+4)+5, 0, (struct sockaddr *) t->address, sizeof(struct sockaddr));
}

variable_types:
buf
char [-1]


decompiled_code:
__int64 __fastcall TFTP_NewReadRequest(__int64 a1)
{
__int16 v2; // r12
__int16 v3; // bp
int v4; // ebx
int v5; // edi
int v6; // eax
int v7; // eax
int v8; // edi
int v9; // edi
int v10; // eax
socklen_t addr_len; // [rsp+8h] [rbp-350h] BYREF
char v13; // [rsp+Ch] [rbp-34Ch] BYREF
char v14; // [rsp+Dh] [rbp-34Bh]
char v15; // [rsp+Eh] [rbp-34Ah]
char v16; // [rsp+Fh] [rbp-349h]
struct timeval timeout; // [rsp+10h] [rbp-348h] BYREF
fd_set readfds; // [rsp+20h] [rbp-338h] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-2B8h] BYREF
__int16 v20[2]; // [rsp+120h] [rbp-238h] BYREF
char buf[564]; // [rsp+124h] [rbp-234h] BYREF

v2 = 0;
v3 = 1;
v4 = 0;
while ( v2 == v3 )
{
LABEL_6:
v7 = sendto(*(_DWORD *)a1, v20, v4, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
if ( v7 < 0 )
{
syslog(3, "%s: sendto: %d", (const char *)(a1 + 16), (unsigned int)v7);
return 0LL;
}
memset(&readfds, 0, sizeof(readfds));
v8 = *(_DWORD *)a1;
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v8 / 64] |= 1LL << (v8 % 64);
if ( select(v8 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_2;
v9 = *(_DWORD *)a1;
v10 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v10 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v10 >> 6] & (1LL << (*(_DWORD *)a1 % 64))) != 0 )
{
addr_len = 16;
recvfrom(v9, &v13, 4uLL, 0, &addr, &addr_len);
if ( v14 != 4 )
{
if ( v14 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v16, (const char *)&timeout);
return 0LL;
}
goto LABEL_2;
}
if ( v4 <= 515 )
{
PrintTransactionTime(a1);
return 0LL;
}
v2 = v16 | (unsigned __int16)(v15 << 8);
}
else
{
LABEL_2:
if ( (int)RunErrorHandler(a1) < 0 )
return 0LL;
}
}
v5 = *(_DWORD *)(a1 + 276);
v20[0] = 768;
v20[1] = __ROL2__(v3, 8);
v6 = read(v5, buf, 0x200uLL);
if ( v6 >= 0 )
{
*(_DWORD *)(a1 + 288) += v6;
v4 = v6 + 4;
++v3;
goto LABEL_6;
}
syslog(3, "%s: Read error: %d", (const char *)(a1 + 16), (unsigned int)v6);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int16 v2; // r12
__int16 v3; // bp
int v4; // ebx
int v5; // edi
int v6; // eax
int v7; // eax
int v8; // edi
int v9; // edi
int v10; // eax
socklen_t addr_len; // [rsp+8h] [rbp-350h] BYREF
char v13; // [rsp+Ch] [rbp-34Ch] BYREF
char v14; // [rsp+Dh] [rbp-34Bh]
char v15; // [rsp+Eh] [rbp-34Ah]
char v16; // [rsp+Fh] [rbp-349h]
struct timeval timeout; // [rsp+10h] [rbp-348h] BYREF
fd_set readfds; // [rsp+20h] [rbp-338h] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-2B8h] BYREF
__int16 v20[2]; // [rsp+120h] [rbp-238h] BYREF
char buf[564]; // [rsp+124h] [rbp-234h] BYREF

v2 = 0;
v3 = 1;
v4 = 0;
while ( v2 == v3 )
{
LABEL_6:
v7 = sendto(*(_DWORD *)a1, v20, v4, 0, *(const struct sockaddr **)(a1 + 8), 0x10u);
if ( v7 < 0 )
{
syslog(3, "%s: sendto: %d", (const char *)(a1 + 16), (unsigned int)v7);
return 0LL;
}
memset(&readfds, 0, sizeof(readfds));
v8 = *(_DWORD *)a1;
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v8 / 64] |= 1LL << (v8 % 64);
if ( select(v8 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_2;
v9 = *(_DWORD *)a1;
v10 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v10 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v10 >> 6] & (1LL << (*(_DWORD *)a1 % 64))) != 0 )
{
addr_len = 16;
recvfrom(v9, &v13, 4uLL, 0, &addr, &addr_len);
if ( v14 != 4 )
{
if ( v14 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v16, (const char *)&timeout);
return 0LL;
}
goto LABEL_2;
}
if ( v4 <= 515 )
{
func2(a1);
return 0LL;
}
v2 = v16 | (unsigned __int16)(v15 << 8);
}
else
{
LABEL_2:
if ( (int)func0(a1) < 0 )
return 0LL;
}
}
v5 = *(_DWORD *)(a1 + 276);
v20[0] = 768;
v20[1] = __ROL2__(v3, 8);
v6 = read(v5, buf, 0x200uLL);
if ( v6 >= 0 )
{
*(_DWORD *)(a1 + 288) += v6;
v4 = v6 + 4;
++v3;
goto LABEL_6;
}
syslog(3, "%s: Read error: %d", (const char *)(a1 + 16), (unsigned int)v6);
return 0LL;
}

original_code:
int TFTP_NewReadRequest(struct Transaction *trans, char *data)
{
  fd_set readFD;
  struct timeval timeout;
  char rec_buff[(2+2)], send_buff[(512+2+2)];
  uint16_t packet_block = 0, last_block = 1;
  int packet_length = 0;
  do {
    if ( last_block != packet_block ) {
      send_buff[0] = 0;
      send_buff[1] = 3;
      send_buff[2] = (last_block / 256);
      send_buff[3] = (last_block % 256);
      packet_length = read(trans->file, send_buff+4, (512));
      if ( packet_length < 0 ) {
        syslog(3,"%s: Read error: %d", trans->client_name, packet_length );
        break;
      }
      trans->byte_count += packet_length;
      packet_length += 4;
      last_block++;
    }
    int rv = sendto(trans->socket, send_buff, packet_length, 0, (const struct sockaddr *) trans->address, sizeof(struct sockaddr));
    if ( rv < 0 ) {
      syslog(3,"%s: sendto: %d", trans->client_name, rv );
      break;
    }
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        socklen_t addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        rv = recvfrom(trans->socket, rec_buff, (2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        int opcode = rec_buff[1];
        if ( opcode == 4 ) {
          packet_block = (rec_buff[2] * 256) | rec_buff[3];
          if ( packet_length < (512+2+2) ) {
            PrintTransactionTime(trans);
            break;
          }
          continue;
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, rec_buff[3], rec_buff+4);
          break;
        }
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      break;
    }
  } while ( 1 );
  return 0;
}

canonical_original_code:
int TFTP_NewReadRequest(struct Transaction *trans, char *data)
{
  struct fd_set readFD;
  struct timeval timeout;
  char rec_buff[-1]; char send_buff[-1];
  unsigned short packet_block = 0; unsigned short last_block = 1;
  int packet_length = 0;
  do {
    if ( last_block != packet_block ) {
      send_buff[0] = 0;
      send_buff[1] = 3;
      send_buff[2] = (last_block / 256);
      send_buff[3] = (last_block % 256);
      packet_length = read(trans->file, send_buff+4, (512));
      if ( packet_length < 0 ) {
        syslog(3,"%s: Read error: %d", trans->client_name, packet_length );
        break;
      }
      trans->byte_count += packet_length;
      packet_length += 4;
      last_block++;
    }
    int rv = sendto(trans->socket, send_buff, packet_length, 0, (const struct sockaddr *) trans->address, sizeof(struct sockaddr));
    if ( rv < 0 ) {
      syslog(3,"%s: sendto: %d", trans->client_name, rv );
      break;
    }
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        unsigned int addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        rv = recvfrom(trans->socket, rec_buff, (2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        int opcode = rec_buff[1];
        if ( opcode == 4 ) {
          packet_block = (rec_buff[2] * 256) | rec_buff[3];
          if ( packet_length < (512+2+2) ) {
            PrintTransactionTime(trans);
            break;
          }
          continue;
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, rec_buff[3], rec_buff+4);
          break;
        }
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      break;
    }
  } while ( 1 );
  return 0;
}

variable_types:
data
char *

variable_types:
readFD
struct fd_set { long fds_bits[-1]; }

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

variable_types: rv int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types: opcode int


decompiled_code:
ssize_t __fastcall TFTP_SendAck(__int64 a1, int a2)
{
const struct sockaddr *v2; // r8
__int16 v3; // ax
int v4; // edi
__int16 buf; // [rsp+Ch] [rbp-Ch] BYREF
char v7; // [rsp+Eh] [rbp-Ah]
char v8; // [rsp+Fh] [rbp-9h]

v2 = *(const struct sockaddr **)(a1 + 8);
buf = 1024;
v3 = a2 + 255;
v4 = *(_DWORD *)a1;
if ( a2 >= 0 )
HIBYTE(v3) = BYTE1(a2);
v7 = HIBYTE(v3);
v8 = a2;
return sendto(v4, &buf, 4uLL, 0, v2, 0x10u);
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1, int a2)
{
const struct sockaddr *v2; // r8
__int16 v3; // ax
int v4; // edi
__int16 buf; // [rsp+Ch] [rbp-Ch] BYREF
char v7; // [rsp+Eh] [rbp-Ah]
char v8; // [rsp+Fh] [rbp-9h]

v2 = *(const struct sockaddr **)(a1 + 8);
buf = 1024;
v3 = a2 + 255;
v4 = *(_DWORD *)a1;
if ( a2 >= 0 )
HIBYTE(v3) = BYTE1(a2);
v7 = HIBYTE(v3);
v8 = a2;
return sendto(v4, &buf, 4uLL, 0, v2, 0x10u);
}

original_code:
void TFTP_SendAck(struct Transaction *t, int block_number)
{
  char buf[4];
  buf[0] = 0;
  buf[1] = 4;
  buf[2] = (block_number / 256);
  buf[3] = (block_number % 256);
  sendto(t->socket, buf, 4, 0, (const struct sockaddr *) t->address, sizeof(struct sockaddr));
}

canonical_original_code:
void TFTP_SendAck(struct Transaction *t, int block_number)
{
  char buf[4];
  buf[0] = 0;
  buf[1] = 4;
  buf[2] = (block_number / 256);
  buf[3] = (block_number % 256);
  sendto(t->socket, buf, 4, 0, (struct sockaddr *) t->address, sizeof(struct sockaddr));
}

variable_types:
buf
char [4]


decompiled_code:
__int64 __fastcall TFTP_NewWriteRequest(__int64 a1)
{
unsigned __int16 v1; // r13
int v3; // eax
int v4; // er14
int v5; // edi
int v6; // edi
int v7; // eax
int v9; // eax
int v10; // [rsp+8h] [rbp-370h]
socklen_t addr_len; // [rsp+1Ch] [rbp-35Ch] BYREF
struct timeval timeout; // [rsp+20h] [rbp-358h] BYREF
fd_set readfds; // [rsp+30h] [rbp-348h] BYREF
struct sockaddr addr; // [rsp+B0h] [rbp-2C8h] BYREF
char buf; // [rsp+130h] [rbp-248h] BYREF
char v16; // [rsp+131h] [rbp-247h]
char v17; // [rsp+132h] [rbp-246h]
char v18; // [rsp+133h] [rbp-245h]
char v19[580]; // [rsp+134h] [rbp-244h] BYREF

v1 = 0;
TFTP_SendAck(a1, 0);
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)a1;
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v5 / 64] |= 1LL << (v5 % 64);
if ( select(v5 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_5;
v6 = *(_DWORD *)a1;
v7 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v7 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v7 >> 6] & (1LL << (*(_DWORD *)a1 % 64))) != 0 )
break;
syslog(3, "%s: Timeout: Block %d", (const char *)(a1 + 16), v1);
TFTP_SendAck(a1, v1);
if ( (int)RunErrorHandler(a1) < 0 )
return 0xFFFFFFFFLL;
LABEL_6:
TFTP_SendAck(a1, v1);
}
addr_len = 16;
v3 = recvfrom(v6, &buf, 0x204uLL, 0, &addr, &addr_len);
v4 = v3;
if ( v3 <= 0 )
break;
if ( v16 != 3 )
{
if ( v16 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v18, v19);
return 0xFFFFFFFFLL;
}
LABEL_5:
if ( (int)RunErrorHandler(a1) < 0 )
return 0xFFFFFFFFLL;
goto LABEL_6;
}
if ( v1 == ((unsigned __int16)v18 | (unsigned __int16)(v17 << 8)) )
goto LABEL_5;
++v1;
v9 = write(*(_DWORD *)(a1 + 276), v19, v3 - 4);
*(_DWORD *)(a1 + 288) += v4 - 4;
v10 = v9;
TFTP_SendAck(a1, v1);
if ( v4 <= 515 || v10 < 0 )
{
PrintTransactionTime(a1);
return 0xFFFFFFFFLL;
}
}
syslog(3, "%s: recvfrom: %d", (const char *)(a1 + 16), (unsigned int)v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned __int16 v1; // r13
int v3; // eax
int v4; // er14
int v5; // edi
int v6; // edi
int v7; // eax
int v9; // eax
int v10; // [rsp+8h] [rbp-370h]
socklen_t addr_len; // [rsp+1Ch] [rbp-35Ch] BYREF
struct timeval timeout; // [rsp+20h] [rbp-358h] BYREF
fd_set readfds; // [rsp+30h] [rbp-348h] BYREF
struct sockaddr addr; // [rsp+B0h] [rbp-2C8h] BYREF
char buf; // [rsp+130h] [rbp-248h] BYREF
char v16; // [rsp+131h] [rbp-247h]
char v17; // [rsp+132h] [rbp-246h]
char v18; // [rsp+133h] [rbp-245h]
char v19[580]; // [rsp+134h] [rbp-244h] BYREF

v1 = 0;
func7(a1, 0);
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
v5 = *(_DWORD *)a1;
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
readfds.fds_bits[v5 / 64] |= 1LL << (v5 % 64);
if ( select(v5 + 1, &readfds, 0LL, 0LL, &timeout) <= 0 )
goto LABEL_5;
v6 = *(_DWORD *)a1;
v7 = *(_DWORD *)a1 + 63;
if ( *(int *)a1 >= 0 )
v7 = *(_DWORD *)a1;
if ( (readfds.fds_bits[v7 >> 6] & (1LL << (*(_DWORD *)a1 % 64))) != 0 )
break;
syslog(3, "%s: Timeout: Block %d", (const char *)(a1 + 16), v1);
func7(a1, v1);
if ( (int)func0(a1) < 0 )
return 0xFFFFFFFFLL;
LABEL_6:
func7(a1, v1);
}
addr_len = 16;
v3 = recvfrom(v6, &buf, 0x204uLL, 0, &addr, &addr_len);
v4 = v3;
if ( v3 <= 0 )
break;
if ( v16 != 3 )
{
if ( v16 == 5 )
{
syslog(3, "%s: Error %d: %s", (const char *)(a1 + 16), (unsigned int)v18, v19);
return 0xFFFFFFFFLL;
}
LABEL_5:
if ( (int)func0(a1) < 0 )
return 0xFFFFFFFFLL;
goto LABEL_6;
}
if ( v1 == ((unsigned __int16)v18 | (unsigned __int16)(v17 << 8)) )
goto LABEL_5;
++v1;
v9 = write(*(_DWORD *)(a1 + 276), v19, v3 - 4);
*(_DWORD *)(a1 + 288) += v4 - 4;
v10 = v9;
func7(a1, v1);
if ( v4 <= 515 || v10 < 0 )
{
func2(a1);
return 0xFFFFFFFFLL;
}
}
syslog(3, "%s: recvfrom: %d", (const char *)(a1 + 16), (unsigned int)v3);
return 0LL;
}

original_code:
int TFTP_NewWriteRequest(struct Transaction *trans, char *data)
{
  fd_set readFD;
  struct timeval timeout;
  uint16_t packet_block, last_block = 0;
  char packet_buff[(512+2+2)];
  TFTP_SendAck(trans, last_block);
  while ( 1 ) {
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        socklen_t addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        int bytes = recvfrom(trans->socket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes <= 0 ) {
          syslog(3,"%s: recvfrom: %d", trans->client_name, bytes);
          break;
        }
        int opcode = packet_buff[1];
        if ( opcode == 3 ) {
          packet_block = (packet_buff[2] * 256) | packet_buff[3];
          if ( packet_block != last_block ) {
            int rv = write( trans->file, packet_buff+4, bytes -4);
            trans->byte_count += bytes - 4;
            last_block++;
            TFTP_SendAck(trans, last_block);
            if (( bytes < (512+2+2)) || ( rv < 0 )) {
              PrintTransactionTime(trans);
              return -1;
            }
            continue;
          }
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, packet_buff[3], packet_buff+4);
          return -1;
        }
      } else {
        syslog(3,"%s: Timeout: Block %d", trans->client_name, last_block);
        TFTP_SendAck(trans, last_block);
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      return -1;
    }
    TFTP_SendAck(trans, last_block);
  }
  return 0;
}

canonical_original_code:
int TFTP_NewWriteRequest(struct Transaction *trans, char *data)
{
  struct fd_set readFD;
  struct timeval timeout;
  unsigned short packet_block; unsigned short last_block = 0;
  char packet_buff[-1];
  TFTP_SendAck(trans, last_block);
  while ( 1 ) {
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readFD)->fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))));
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    if ( select(trans->socket+1, &readFD, ((void *)0), ((void *)0), &timeout) > 0 ) {
      if ( ((((&readFD)->fds_bits)[((trans->socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((trans->socket) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
        unsigned int addr_len = sizeof(struct sockaddr);
        struct sockaddr_storage their_addr;
        int bytes = recvfrom(trans->socket, packet_buff, (512+2+2), 0,(struct sockaddr *)&their_addr, &addr_len);
        if ( bytes <= 0 ) {
          syslog(3,"%s: recvfrom: %d", trans->client_name, bytes);
          break;
        }
        int opcode = packet_buff[1];
        if ( opcode == 3 ) {
          packet_block = (packet_buff[2] * 256) | packet_buff[3];
          if ( packet_block != last_block ) {
            int rv = write( trans->file, packet_buff+4, bytes -4);
            trans->byte_count += bytes - 4;
            last_block++;
            TFTP_SendAck(trans, last_block);
            if (( bytes < (512+2+2)) || ( rv < 0 )) {
              PrintTransactionTime(trans);
              return -1;
            }
            continue;
          }
        } else if ( opcode == 5 ) {
          syslog(3,"%s: Error %d: %s", trans->client_name, packet_buff[3], packet_buff+4);
          return -1;
        }
      } else {
        syslog(3,"%s: Timeout: Block %d", trans->client_name, last_block);
        TFTP_SendAck(trans, last_block);
      }
    }
    if ( RunErrorHandler(trans) < 0 ) {
      return -1;
    }
    TFTP_SendAck(trans, last_block);
  }
  return 0;
}

variable_types: bytes int

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