bitcount

decompiled_code:
__int64 __fastcall bitcount(int a1)
{
unsigned int v1; // edx

if ( !a1 )
return 0LL;
v1 = 0;
do
{
++v1;
a1 &= a1 - 1;
}
while ( a1 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
unsigned int v1; // edx

if ( !a1 )
return 0LL;
v1 = 0;
do
{
++v1;
a1 &= a1 - 1;
}
while ( a1 );
return v1;
}

original_code:
int bitcount(unsigned x)
{
 unsigned b;
 for (b = 0; x != 0U; x &= (x-1))
  ++b;
 return b;
}

canonical_original_code:
int bitcount(unsigned int x)
{
 unsigned int b;
 for (b = 0; x != 0U; x &= (x-1))
  ++b;
 return b;
}

variable_types:
b
unsigned int


main

canonical_name: main

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

v3 = (unsigned int)bitcount(2299LL, argv, envp);
printf("%d\n", v3);
v5 = bitcount(2905LL, v3, v4);
printf("%d\n", v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rsi
__int64 v4; // rdx
unsigned int v5; // eax

v3 = (unsigned int)func7(2299LL, argv, envp);
printf("%d\n", v3);
v5 = func7(2905LL, v3, v4);
printf("%d\n", v5);
return 0;
}

original_code:
int main(void)
{
 unsigned x;
 x = 0x8FB;
 printf("%d\n", bitcount(x));
 x = 0xB59;
 printf("%d\n", bitcount(x));
 return 0;
}

canonical_original_code:
int main()
{
 unsigned int x;
 x = 0x8FB;
 printf("%d\n", bitcount(x));
 x = 0xB59;
 printf("%d\n", bitcount(x));
 return 0;
}

variable_types:
x
unsigned int

return_type: int


trimline

original_code:
void trimline(char line[], int len)
{
 int i;
 for (i = len - 2; line[i] == ' ' || line[i] == '\t'; --i)
  ;
 line[++i] = (line[len - 1] == '\n') ? '\n' : '\0';
 if (line[i] == '\n')
  line[++i] = '\0';
 if (i != 1)
  printf("%s", line);
}

variable_types:
line
char []

variable_types: len int

variable_types: i int

return_type: void


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
_BYTE v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

while ( 1 )
{
v3 = obtenirline(v5, 1000LL, envp);
if ( v3 <= 0 )
break;
if ( v3 != 1 )
trimline(v5, (unsigned int)v3);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
_BYTE v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

while ( 1 )
{
v3 = func7(v5, 1000LL, envp);
if ( v3 <= 0 )
break;
if ( v3 != 1 )
func8(v5, (unsigned int)v3);
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 int len;
 char line[1000];
 while ((len = obtenirline(line, 1000)) > 0)
 {
  if (len > 1)
   trimline(line, len);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
line
char [1000]


getword

original_code:
int getword(char *word, int lim)
{
    int c, getch(void);
    void ungetch(int);
    char *w = word;
    while (((*__ctype_b_loc ())[(int) ((c = getch()))] & (unsigned short int) _ISspace))
        ;
    *w++ = c;
    if (c == '\'')
    {
       if (getch() == '\\')
           getch();
       getch();
    }
    else if (c == '"')
    {
        for(;;)
        {
            if ((c = getch()) == '\\')
                getch();
            else if (c == '"')
                break;
        }
    }
    else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) || c == '_')
    {
        for (; --lim; w++)
            if (!((*__ctype_b_loc ())[(int) ((*w = getch()))] & (unsigned short int) _ISalnum))
            {
                ungetch(*w);
                break;
            }
    }
    else if (c == '/')
    {
        if ((c = getch()) == '*')
        {
            for(;;)
            {
                while ((c = getch()) != '*')
                    ;
                if ((c = getch()) != '/')
                    ungetch(c);
                else
                    break;
            }
        }
        else if (c == '/')
            while (getch() != '\n')
                ;
        else
            ungetch(c);
    }
    *w = '\0';
    return word[0];
}

canonical_original_code:
int getword(char *word, int lim)
{
    int c; int getch();
    void ungetch(int);
    char *w = word;
    while (((*__ctype_b_loc ())[(int) ((c = getch()))] & (unsigned short) _ISspace))
        ;
    *w++ = c;
    if (c == '\'')
    {
       if (getch() == '\\')
           getch();
       getch();
    }
    else if (c == '"')
    {
        for(;;)
        {
            if ((c = getch()) == '\\')
                getch();
            else if (c == '"')
                break;
        }
    }
    else if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) || c == '_')
    {
        for (; --lim; w++)
            if (!((*__ctype_b_loc ())[(int) ((*w = getch()))] & (unsigned short) _ISalnum))
            {
                ungetch(*w);
                break;
            }
    }
    else if (c == '/')
    {
        if ((c = getch()) == '*')
        {
            for(;;)
            {
                while ((c = getch()) != '*')
                    ;
                if ((c = getch()) != '/')
                    ungetch(c);
                else
                    break;
            }
        }
        else if (c == '/')
            while (getch() != '\n')
                ;
        else
            ungetch(c);
    }
    *w = '\0';
    return word[0];
}

variable_types:
word
char *

variable_types: lim int

variable_types: c int

variable_types:
getch
int ()

variable_types:
ungetch
void (int)

variable_types:
w
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const unsigned __int16 **v3; // rax
int v4; // eax
char *v5; // rbx
__int64 v6; // rsi
char v8[120]; // [rsp+0h] [rbp-78h] BYREF

while ( (unsigned int)getword(v8, 100LL, envp) != -1 )
{
v3 = __ctype_b_loc();
envp = (const char **)v8[0];
if ( ((*v3)[v8[0]] & 0x400) != 0 )
{
v4 = binsearch(v8);
if ( v4 >= 0 )
++dword_404088[4 * v4];
}
}
v5 = (char *)&keytab;
do
{
v6 = *((unsigned int *)v5 + 2);
if ( (int)v6 > 0 )
printf("%4d %s\n", v6, *(const char **)v5);
v5 += 16;
}
while ( v5 != &_bss_start );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const unsigned __int16 **v3; // rax
int v4; // eax
char *v5; // rbx
__int64 v6; // rsi
char v8[120]; // [rsp+0h] [rbp-78h] BYREF

while ( (unsigned int)func10(v8, 100LL, envp) != -1 )
{
v3 = __ctype_b_loc();
envp = (const char **)v8[0];
if ( ((*v3)[v8[0]] & 0x400) != 0 )
{
v4 = func7(v8);
if ( v4 >= 0 )
++dword_404088[4 * v4];
}
}
v5 = (char *)&keytab;
do
{
v6 = *((unsigned int *)v5 + 2);
if ( (int)v6 > 0 )
printf("%4d %s\n", v6, *(const char **)v5);
v5 += 16;
}
while ( v5 != &_bss_start );
return 0;
}

original_code:
int main(void)
{
    int n;
    char word[100];
    while (getword(word, 100) != (-1))
    {
        if (((*__ctype_b_loc ())[(int) ((word[0]))] & (unsigned short int) _ISalpha))
            if ((n = binsearch(word, keytab, (sizeof keytab / sizeof(keytab[0])))) >= 0)
                keytab[n].count++;
    }
    for (n = 0; n < (sizeof keytab / sizeof(keytab[0])); n++)
        if (keytab[n].count > 0)
            printf("%4d %s\n", keytab[n].count, keytab[n].word);
    return 0;
}

canonical_original_code:
int main()
{
    int n;
    char word[100];
    while (getword(word, 100) != (-1))
    {
        if (((*__ctype_b_loc ())[(int) ((word[0]))] & (unsigned short) _ISalpha))
            if ((n = binsearch(word, keytab, (sizeof keytab / sizeof(keytab[0])))) >= 0)
                keytab[n].count++;
    }
    for (n = 0; n < (sizeof keytab / sizeof(keytab[0])); n++)
        if (keytab[n].count > 0)
            printf("%4d %s\n", keytab[n].count, keytab[n].word);
    return 0;
}

variable_types: n int

variable_types:
word
char [100]


binsearch

decompiled_code:
__int64 __fastcall binsearch(char *s1, __int64 a2, int a3)
{
int v3; // edx
int v4; // er13
int v5; // er14
int v6; // er12
int v7; // eax

v3 = a3 - 1;
if ( v3 >= 0 )
{
v4 = v3;
v5 = 0;
do
{
while ( 1 )
{
v6 = (v5 + v4) >> 1;
v7 = strcmp(s1, *(const char **)(a2 + 16LL * v6));
if ( v7 >= 0 )
break;
v4 = v6 - 1;
if ( v6 - 1 < v5 )
return (unsigned int)-1;
}
if ( !v7 )
return (unsigned int)v6;
v5 = v6 + 1;
}
while ( v4 >= v6 + 1 );
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s1, __int64 a2, int a3)
{
int v3; // edx
int v4; // er13
int v5; // er14
int v6; // er12
int v7; // eax

v3 = a3 - 1;
if ( v3 >= 0 )
{
v4 = v3;
v5 = 0;
do
{
while ( 1 )
{
v6 = (v5 + v4) >> 1;
v7 = strcmp(s1, *(const char **)(a2 + 16LL * v6));
if ( v7 >= 0 )
break;
v4 = v6 - 1;
if ( v6 - 1 < v5 )
return (unsigned int)-1;
}
if ( !v7 )
return (unsigned int)v6;
v5 = v6 + 1;
}
while ( v4 >= v6 + 1 );
}
return (unsigned int)-1;
}

original_code:
int binsearch(char *word, struct key tab[], int n)
{
    int cond;
    int low, high, mid;
    low = 0;
    high = n - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if ((cond = strcmp(word, tab[mid].word)) < 0)
            high = mid - 1;
        else if (cond > 0)
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}

canonical_original_code:
int binsearch(char *word, struct key tab[], int n)
{
    int cond;
    int low; int high; int mid;
    low = 0;
    high = n - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if ((cond = strcmp(word, tab[mid].word)) < 0)
            high = mid - 1;
        else if (cond > 0)
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}

variable_types: struct key { char *word; int count; } []

variable_types: cond int

variable_types: low int

variable_types: high int

variable_types: mid int


getch

decompiled_code:
int getch()
{
char *v0; // rax

v0 = (char *)bufp;
if ( bufp <= &buf )
return getchar();
bufp = (char *)bufp - 1;
return *(v0 - 1);
}

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

v0 = (char *)bufp;
if ( bufp <= &buf )
return getchar();
bufp = (char *)bufp - 1;
return *(v0 - 1);
}

original_code:
int getch(void)
{
    return bufp > buf ? *--bufp : getchar();
}

canonical_original_code:
int getch()
{
    return bufp > buf ? *--bufp : getchar();
}


ungetch

decompiled_code:
int __fastcall ungetch(char a1)
{
_BYTE *v1; // rax

v1 = bufp;
if ( bufp > &unk_404304 )
{
LODWORD(v1) = puts("ungetch: too many characters");
}
else
{
bufp = (char *)bufp + 1;
*v1 = a1;
}
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func9(char a1)
{
_BYTE *v1; // rax

v1 = bufp;
if ( bufp > &unk_404304 )
{
LODWORD(v1) = puts("ungetch: too many characters");
}
else
{
bufp = (char *)bufp + 1;
*v1 = a1;
}
return (int)v1;
}

original_code:
void ungetch(int c)
{
    if (bufp > buf + 100)
        printf("ungetch: too many characters\n");
    else
        *bufp++ = c;
}


atof

original_code:
double atof(char s[])
{
 double val;
 int power;
 int power_sig;
 int power_exp;
 int sign_sig;
 int sign_exp;
 int i;
 for (i = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISspace); ++i)
  ;
 sign_sig = s[i] == '-' ? -1 : 1;
 if (s[i] == '-' || s[i] == '+')
  ++i;
 for (val = 0.0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit); ++i)
  val = 10.0 * val + (s[i] - '0');
 if (s[i] == '.')
  ++i;
 for (power_sig = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit); ++i)
 {
  val = 10.0 * val + (s[i] - '0');
  ++power_sig;
 }
 if (s[i] == 'e' || s[i] == 'E')
  ++i;
 sign_exp = s[i] == '-' ? -1 : 1;
 if (s[i] == '-' || s[i] == '+')
  ++i;
 for (power_exp = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit); ++i)
  power_exp = 10 * power_exp + (s[i] - '0');
 power = sign_exp * power_exp - power_sig;
 if (power < 0)
 {
  while (power++ < 0)
   val /= 10.0;
 }
 else
 {
  while (power-- > 0)
   val *= 10.0;
 }
 return sign_sig * val;
}

canonical_original_code:
double atof(char s[])
{
 double val;
 int power;
 int power_sig;
 int power_exp;
 int sign_sig;
 int sign_exp;
 int i;
 for (i = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISspace); ++i)
  ;
 sign_sig = s[i] == '-' ? -1 : 1;
 if (s[i] == '-' || s[i] == '+')
  ++i;
 for (val = 0.0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit); ++i)
  val = 10.0 * val + (s[i] - '0');
 if (s[i] == '.')
  ++i;
 for (power_sig = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit); ++i)
 {
  val = 10.0 * val + (s[i] - '0');
  ++power_sig;
 }
 if (s[i] == 'e' || s[i] == 'E')
  ++i;
 sign_exp = s[i] == '-' ? -1 : 1;
 if (s[i] == '-' || s[i] == '+')
  ++i;
 for (power_exp = 0; ((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit); ++i)
  power_exp = 10 * power_exp + (s[i] - '0');
 power = sign_exp * power_exp - power_sig;
 if (power < 0)
 {
  while (power++ < 0)
   val /= 10.0;
 }
 else
 {
  while (power-- > 0)
   val *= 10.0;
 }
 return sign_sig * val;
}

variable_types:
s
char []

variable_types: val double

variable_types: power int

variable_types: i int

return_type: double


advance

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

result = (unsigned int)(a1 + 1);
if ( a1 >= 1000 )
result = 0LL;
return result;
}

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

result = (unsigned int)(a1 + 1);
if ( a1 >= 1000 )
result = 0LL;
return result;
}

original_code:
int advance(int pointer)
{
 if (pointer < 1001 - 1)
  return pointer + 1;
 else
  return 0;
}

variable_types: pointer int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // er13
unsigned int v5; // ebp
__int64 i; // rbx
int v7; // er12
int v8; // er14
char v10[1064]; // [rsp+0h] [rbp-428h]

v3 = 0;
v4 = 0;
v5 = 0;
LABEL_2:
for ( i = 0LL; ; i = 0LL )
{
v7 = getchar();
if ( v7 == -1 )
break;
while ( v7 != 10 )
{
if ( v7 != 32 && v7 != 9 )
{
while ( (_DWORD)i != v5 )
{
putchar(v10[v5]);
v5 = advance(v5);
}
v3 = 1;
putchar(v7);
goto LABEL_2;
}
v8 = advance((unsigned int)i);
if ( v8 == v5 )
{
v4 = 1;
v3 = 1;
putchar(v10[v5]);
v5 = advance(v5);
}
v10[i] = v7;
i = v8;
v7 = getchar();
if ( v7 == -1 )
return v4;
}
v5 = 0;
if ( v3 )
{
v3 = 0;
putchar(10);
}
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er15
int v4; // er13
unsigned int v5; // ebp
__int64 i; // rbx
int v7; // er12
int v8; // er14
char v10[1064]; // [rsp+0h] [rbp-428h]

v3 = 0;
v4 = 0;
v5 = 0;
LABEL_2:
for ( i = 0LL; ; i = 0LL )
{
v7 = getchar();
if ( v7 == -1 )
break;
while ( v7 != 10 )
{
if ( v7 != 32 && v7 != 9 )
{
while ( (_DWORD)i != v5 )
{
putchar(v10[v5]);
v5 = func7(v5);
}
v3 = 1;
putchar(v7);
goto LABEL_2;
}
v8 = func7((unsigned int)i);
if ( v8 == v5 )
{
v4 = 1;
v3 = 1;
putchar(v10[v5]);
v5 = func7(v5);
}
v10[i] = v7;
i = v8;
v7 = getchar();
if ( v7 == -1 )
return v4;
}
v5 = 0;
if ( v3 )
{
v3 = 0;
putchar(10);
}
}
return v4;
}

original_code:
int main(void)
{
 char blank[1001];
 int head, tail;
 int nonspace;
 int retval;
 int c;
 retval = nonspace = head = tail = 0;
 while ((c = getchar()) != (-1))
 {
  if (c == '\n')
  {
   head = tail = 0;
   if (nonspace)
      putchar('\n');
   nonspace = 0;
  }
  else if (c == ' ' || c == '\t')
  {
   if (advance(head) == tail)
   {
      putchar(blank[tail]);
      tail = advance(tail);
      nonspace = 1;
      retval = 1;
   }
   blank[head] = c;
   head = advance(head);
  }
  else
  {
   while (head != tail)
   {
    putchar(blank[tail]);
    tail = advance(tail);
   }
   putchar(c);
   nonspace = 1;
  }
 }
 return retval;
}

canonical_original_code:
int main()
{
 char blank[1001];
 int head; int tail;
 int nonspace;
 int retval;
 int c;
 retval = nonspace = head = tail = 0;
 while ((c = getchar()) != (-1))
 {
  if (c == '\n')
  {
   head = tail = 0;
   if (nonspace)
      putchar('\n');
   nonspace = 0;
  }
  else if (c == ' ' || c == '\t')
  {
   if (advance(head) == tail)
   {
      putchar(blank[tail]);
      tail = advance(tail);
      nonspace = 1;
      retval = 1;
   }
   blank[head] = c;
   head = advance(head);
  }
  else
  {
   while (head != tail)
   {
    putchar(blank[tail]);
    tail = advance(tail);
   }
   putchar(c);
   nonspace = 1;
  }
 }
 return retval;
}

variable_types:
blank
char [1001]

variable_types: head int

variable_types: tail int

variable_types: nonspace int

variable_types: retval int


htoi

original_code:
unsigned int htoi(const char s[])
{
 unsigned int answer = 0;
 int i = 0;
 int valid = 1;
 if (s[i] == '0')
 {
  ++i;
  if (s[i] == 'x' || s[i] == 'X')
  {
   ++i;
  }
 }
 while (valid && s[i] != '\0')
 {
  answer *= 16;
  if (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISxdigit))
  {
   if (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit))
    answer += s[i] - '0';
   else
    answer += toupper(s[i]) - 'A' + 10;
  }
  else
   valid = 0;
  ++i;
 }
 if (!valid)
  answer = 0;
 return answer;
}

canonical_original_code:
unsigned int htoi(char s[])
{
 unsigned int answer = 0;
 int i = 0;
 int valid = 1;
 if (s[i] == '0')
 {
  ++i;
  if (s[i] == 'x' || s[i] == 'X')
  {
   ++i;
  }
 }
 while (valid && s[i] != '\0')
 {
  answer *= 16;
  if (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISxdigit))
  {
   if (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit))
    answer += s[i] - '0';
   else
    answer += toupper(s[i]) - 'A' + 10;
  }
  else
   valid = 0;
  ++i;
 }
 if (!valid)
  answer = 0;
 return answer;
}

variable_types:
answer
unsigned int

variable_types: valid int

return_type: unsigned int


main

original_code:
int main(void)
{
 char *test[] =
 {
  "F00",
  "bar",
  "0100",
  "0x1",
  "0XA",
  "0X0C0E",
  "abcdef",
  "123456",
  "0x123456",
  "deadbeaf",
  "zog_c"
 };
 size_t numtests = sizeof(test) / sizeof(test[0]);
 size_t thistest;
 for (thistest = 0; thistest < numtests; thistest++)
 {
  printf("%s: %u\n", test[thistest], htoi(test[thistest]));
 }
 return 0;
}

canonical_original_code:
int main()
{
 char *test[] = {
  "F00",
  "bar",
  "0100",
  "0x1",
  "0XA",
  "0X0C0E",
  "abcdef",
  "123456",
  "0x123456",
  "deadbeaf",
  "zog_c"
 };
 unsigned long numtests = sizeof(test) / sizeof(test[0]);
 unsigned long thistest;
 for (thistest = 0; thistest < numtests; thistest++)
 {
  printf("%s: %u\n", test[thistest], htoi(test[thistest]));
 }
 return 0;
}

variable_types:
test
char *[]

variable_types:
numtests
unsigned long

variable_types:
thistest
unsigned long


itob

canonical_original_code:
void itob(int n, char s[], int b)
{
 ((void) sizeof ((2 <= b && b <= 16) ? 1 : 0), __extension__ ({ if (2 <= b && b <= 16) ; else __assert_fail ("2 <= b && b <= 16", "5.c", 49, __extension__ __PRETTY_FUNCTION__); }));
 int i; int sign;
 i = 0;
 sign = n;
 do
 {
  unsigned int t = abs(n % b);
  s[i++] = t < 10 ? t + '0' : t - 10 + 'a';
 } while (n /= b);
 if (sign < 0)
  s[i++] = '-';
 s[i] = '\0';
 reverse(s);
}

variable_types: n int

variable_types: b int

variable_types: sign int

variable_types:
t
unsigned int


reverse

original_code:
void reverse(char s[])
{
 int len;
 int i;
 char c;
 for (len = 0; s[len] != '\0'; ++len)
  ;
 for (i = 0; i < len / 2; ++i)
 {
  c = s[i];
  s[i] = s[len-1-i];
  s[len-1-i] = c;
 }
}

variable_types:
s
char []

variable_types: len int

variable_types: c char


decompiled_code:
__int64 __fastcall discard_newline(__int64 a1, int a2)
{
__int64 result; // rax

result = a2;
if ( *(_BYTE *)(a1 + a2 - 1) == 10 )
{
result = (unsigned int)(a2 - 1);
*(_BYTE *)(a1 + (int)result) = 0;
}
return result;
}

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

result = a2;
if ( *(_BYTE *)(a1 + a2 - 1) == 10 )
{
result = (unsigned int)(a2 - 1);
*(_BYTE *)(a1 + (int)result) = 0;
}
return result;
}

original_code:
int discard_newline(char s[], int len)
{
 if (s[len - 1] == '\n')
  s[--len] = '\0';
 return len;
}


main

original_code:
int main(void)
{
 int len;
 char line[1000];
 int i, j, p, col;
 col = p = 0;
 while ((len = obtenirline(line, 1000)) > 0)
 {
  p = 0;
  if (len == 1)
  {
   printf("\n\n");
   col = 0;
   continue;
  }
  len = discard_newline(line, len);
  while (len - p >= 60 - col)
  {
   for (i = 60 - col - 1; i >= 0 && line[p+i] != ' '; --i)
    ;
   for (j = 60 - col - 1; j < len - p && line[p+j] != ' '; ++j)
    ;
   i = ((i + j) / 2 > 60 - col - 1) ? i : j;
   while (i-- >= 0)
    putchar(line[p++]);
   col = 0;
   putchar('\n');
  }
  for (; p < len; ++p, ++col)
  {
   putchar(line[p]);
  }
 }
 return 0;
}

canonical_original_code:
int main()
{
 int len;
 char line[1000];
 int i; int j; int p; int col;
 col = p = 0;
 while ((len = obtenirline(line, 1000)) > 0)
 {
  p = 0;
  if (len == 1)
  {
   printf("\n\n");
   col = 0;
   continue;
  }
  len = discard_newline(line, len);
  while (len - p >= 60 - col)
  {
   for (i = 60 - col - 1; i >= 0 && line[p+i] != ' '; --i)
    ;
   for (j = 60 - col - 1; j < len - p && line[p+j] != ' '; ++j)
    ;
   i = ((i + j) / 2 > 60 - col - 1) ? i : j;
   while (i-- >= 0)
    putchar(line[p++]);
   col = 0;
   putchar('\n');
  }
  for (; p < len; ++p, ++col)
  {
   putchar(line[p]);
  }
 }
 return 0;
}

variable_types:
line
char [1000]

variable_types: i int

variable_types: j int

variable_types: p int

variable_types: col int


obtenirline

canonical_original_code:
int obtenirline(char s[], int lim)
{
 int i; int c;
 for (i = 0; i < lim - 1 && (c = getchar()) != '\n' && c != (-1); ++i)
  s[i] = c;
 if (c == '\n')
  s[i++] = '\n';
 s[i] = '\0';
 return i;
}

variable_types: lim int

variable_types: c int


strrindex

decompiled_code:
__int64 __fastcall strrindex(char *a1, char *a2)
{
char v2; // dl
char v3; // r11
__int64 v4; // r8
unsigned int v5; // er9
__int64 v6; // rax
char v7; // cl
char v8; // dl

v2 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v3 = *a2;
v4 = 1LL;
v5 = -1;
do
{
v6 = v4;
if ( v3 == v2 )
{
do
{
v7 = a1[v6++];
v8 = a2[v6 - 1];
}
while ( v7 == v8 );
if ( !v8 )
v5 = v4 - 1;
}
v2 = a1[v4];
--a2;
++v4;
}
while ( v2 );
return v5;
}

canonical_original_code:
int strrindex(char s[], char t[])
{
 int i; int j; int k; int kr;
 k = kr = -1;
 for (i = 0; s[i] != '\0'; ++i)
 {
  for (k = i, j = 0; s[k] == t[j]; ++k, ++j)
   ;
  if (j > 0 && t[j] == '\0')
   kr = i;
 }
 return kr;
}

variable_types:
t
char []

variable_types: k int

variable_types: kr int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[5]; // [rsp+Bh] [rbp-4Dh] BYREF
_OWORD v6[3]; // [rsp+10h] [rbp-48h] BYREF
char v7[16]; // [rsp+40h] [rbp-18h] BYREF

strcpy(v7, "ld you?\n");
v6[0] = _mm_load_si128((const __m128i *)&xmmword_402010);
v6[1] = _mm_load_si128((const __m128i *)&xmmword_402020);
strcpy(v5, "ould");
v6[2] = _mm_load_si128((const __m128i *)&xmmword_402030);
v3 = strrindex(v6, v5, envp);
printf("%d\n", v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v5[5]; // [rsp+Bh] [rbp-4Dh] BYREF
_OWORD v6[3]; // [rsp+10h] [rbp-48h] BYREF
char v7[16]; // [rsp+40h] [rbp-18h] BYREF

strcpy(v7, "ld you?\n");
v6[0] = _mm_load_si128((const __m128i *)&xmmword_402010);
v6[1] = _mm_load_si128((const __m128i *)&xmmword_402020);
strcpy(v5, "ould");
v6[2] = _mm_load_si128((const __m128i *)&xmmword_402030);
v3 = func7(v6, v5, envp);
printf("%d\n", v3);
return 0;
}

original_code:
int main(void)
{
 char s[] = "I would forgive you if you could forgive me, could you?\n";
 char t[] = "ould";
 printf("%d\n", strrindex(s, t));
 return 0;
}

canonical_original_code:
int main()
{
 char s[] = "I would forgive you if you could forgive me, could you?\n";
 char t[] = "ould";
 printf("%d\n", strrindex(s, t));
 return 0;
}


getbits

decompiled_code:
__int64 __fastcall getbits(unsigned int a1, char a2, char a3)
{
return (a1 >> (a2 + 1 - a3)) & ~(-1 << a3);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, char a2, char a3)
{
return (a1 >> (a2 + 1 - a3)) & ~(-1 << a3);
}

original_code:
unsigned getbits(unsigned x, int p, int n)
{
 return (x >> (p+1-n)) & ~(~0 << n);
}

canonical_original_code:
unsigned int getbits(unsigned int x, int p, int n)
{
 return (x >> (p+1-n)) & ~(~0 << n);
}

variable_types:
x
unsigned int

variable_types: n int


invert

decompiled_code:
__int64 __fastcall invert(int a1, char a2, char a3)
{
int v3; // eax
char v4; // dl
int v5; // er8

v3 = getbits(~a1, a2, a3);
return (v5 & (unsigned int)~(~(-1 << v4) << (a2 + 1 - v4))) + (v3 << (a2 + 1 - v4));
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, char a2, char a3)
{
int v3; // eax
char v4; // dl
int v5; // er8

v3 = func7(~a1, a2, a3);
return (v5 & (unsigned int)~(~(-1 << v4) << (a2 + 1 - v4))) + (v3 << (a2 + 1 - v4));
}

original_code:
unsigned invert(unsigned x, int p, int n)
{
 unsigned y = getbits(~x, p, n) << (p+1-n);
 x = x & ~(~(~0 << n) << (p+1-n));
 return x + y;
}

canonical_original_code:
unsigned int invert(unsigned int x, int p, int n)
{
 unsigned int y = getbits(~x, p, n) << (p+1-n);
 x = x & ~(~(~0 << n) << (p+1-n));
 return x + y;
}

variable_types:
y
unsigned int


copy

decompiled_code:
__int64 __fastcall copy(_BYTE *a1, unsigned __int8 *a2)
{
__int64 result; // rax
unsigned __int8 v3; // dl

result = *a2;
*a1 = result;
if ( (_BYTE)result )
{
result = 1LL;
do
{
v3 = a2[result];
a1[result++] = v3;
}
while ( v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_BYTE *a1, unsigned __int8 *a2)
{
__int64 result; // rax
unsigned __int8 v3; // dl

result = *a2;
*a1 = result;
if ( (_BYTE)result )
{
result = 1LL;
do
{
v3 = a2[result];
a1[result++] = v3;
}
while ( v3 );
}
return result;
}

original_code:
void copy(char to[], char from[])
{
    int i;
    for (i = 0; (to[i] = from[i]) != '\0'; i++)
      ;
}

variable_types:
to
char []

variable_types:
from
char []


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v4[1046]; // [rsp+2h] [rbp-416h] BYREF

strcpy(v4, "Hello\tWorld!\n");
escape(&v4[14], v4, envp);
puts(&v4[14]);
unescape(v4, &v4[14]);
puts(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v4[1046]; // [rsp+2h] [rbp-416h] BYREF

strcpy(v4, "Hello\tWorld!\n");
func7(&v4[14], v4, envp);
puts(&v4[14]);
func8(v4, &v4[14]);
puts(v4);
return 0;
}

original_code:
int main(void)
{
 char t[] = "Hello\tWorld!\n";
 char s[1024];
 escape(s, t);
 printf("%s\n", s);
 unescape(t, s);
 printf("%s\n", t);
 return 0;
}

canonical_original_code:
int main()
{
 char t[] = "Hello\tWorld!\n";
 char s[1024];
 escape(s, t);
 printf("%s\n", s);
 unescape(t, s);
 printf("%s\n", t);
 return 0;
}


outputError

decompiled_code:
int __fastcall outputError(int a1, int a2, int a3, const char *a4, void *a5)
{
char *v6; // rax
const char *v7; // rcx
char v9[512]; // [rsp+0h] [rbp-618h] BYREF
char s[512]; // [rsp+200h] [rbp-418h] BYREF
char v11[536]; // [rsp+400h] [rbp-218h] BYREF

vsnprintf(s, 0x1F4uLL, a4, a5);
if ( a1 )
{
v6 = strerror(a2);
v7 = "?UNKNOWN?";
if ( (unsigned int)(a2 - 1) <= 0x84 )
v7 = (const char *)*(&ename + a2);
snprintf(v11, 0x1F4uLL, " [%s %s]", v7, v6);
}
else
{
strcpy(v11, ":");
}
snprintf(v9, 0x1F4uLL, "ERROR%s %s\n", v11, s);
if ( a3 )
fflush(stdout);
fputs(v9, stderr);
return fflush(stderr);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, int a3, const char *a4, void *a5)
{
char *v6; // rax
const char *v7; // rcx
char v9[512]; // [rsp+0h] [rbp-618h] BYREF
char s[512]; // [rsp+200h] [rbp-418h] BYREF
char v11[536]; // [rsp+400h] [rbp-218h] BYREF

vsnprintf(s, 0x1F4uLL, a4, a5);
if ( a1 )
{
v6 = strerror(a2);
v7 = "?UNKNOWN?";
if ( (unsigned int)(a2 - 1) <= 0x84 )
v7 = (const char *)*(&ename + a2);
snprintf(v11, 0x1F4uLL, " [%s %s]", v7, v6);
}
else
{
strcpy(v11, ":");
}
snprintf(v9, 0x1F4uLL, "ERROR%s %s\n", v11, s);
if ( a3 )
fflush(stdout);
fputs(v9, stderr);
return fflush(stderr);
}

original_code:
static void
outputError(Boolean useErr, int err, Boolean flushStdout,
        const char *format, va_list ap)
{
    char buf[500], userMsg[500], errText[500];
    vsnprintf(userMsg, 500, format, ap);
    if (useErr)
        snprintf(errText, 500, " [%s %s]",
                (err > 0 && err <= 133) ?
                ename[err] : "?UNKNOWN?", strerror(err));
    else
        snprintf(errText, 500, ":");
    snprintf(buf, 500, "ERROR%s %s\n", errText, userMsg);
    if (flushStdout)
        fflush(stdout);
    fputs(buf, stderr);
    fflush(stderr);
}

canonical_original_code:
static void outputError(enum Boolean useErr, int err, enum Boolean flushStdout, char *format, __builtin_va_list ap)
{
    char buf[500]; char userMsg[500]; char errText[500];
    vsnprintf(userMsg, 500, format, ap);
    if (useErr)
        snprintf(errText, 500, " [%s %s]",
                (err > 0 && err <= 133) ?
                ename[err] : "?UNKNOWN?", strerror(err));
    else
        snprintf(errText, 500, ":");
    snprintf(buf, 500, "ERROR%s %s\n", errText, userMsg);
    if (flushStdout)
        fflush(stdout);
    fputs(buf, stderr);
    fflush(stderr);
}

variable_types:
useErr
enum Boolean { FALSE=0, TRUE=1 }

variable_types: err int

variable_types:
flushStdout
enum Boolean { FALSE=0, TRUE=1 }

variable_types:
format
char *

variable_types:
buf
char [500]

variable_types:
userMsg
char [500]

variable_types:
errText
char [500]


terminate

decompiled_code:
void __fastcall __noreturn terminate(int a1)
{
char *v1; // rax

v1 = getenv("EF_DUMPCORE");
if ( !v1 || !*v1 )
{
if ( a1 )
exit(1);
exit(1);
}
terminate_cold();
}

canonical_decompiled_code:
void __fastcall __noreturn func1(int a1)
{
char *v1; // rax

v1 = getenv("EF_DUMPCORE");
if ( !v1 || !*v1 )
{
if ( a1 )
exit(1);
exit(1);
}
func7();
}

original_code:
__attribute__ ((__noreturn__))
static void
terminate(Boolean useExit3)
{
    char *s;
    s = getenv("EF_DUMPCORE");
    if (s != ((void *)0) && *s != '\0')
        abort();
    else if (useExit3)
        exit(1);
    else
        _exit(1);
}

canonical_original_code:
static void terminate(enum Boolean useExit3)
{
    char *s;
    s = getenv("EF_DUMPCORE");
    if (s != ((void *)0) && *s != '\0')
        abort();
    else if (useExit3)
        exit(1);
    else
        _exit(1);
}

variable_types:
s
char *


errMsg

decompiled_code:
int errMsg(const char *a1, ...)
{
int *v1; // rax
int v2; // ebp
int *v3; // rbx
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a1);
v1 = _errno_location();
v2 = *v1;
v3 = v1;
result = outputError(1, *v1, 1, a1, va);
*v3 = v2;
return result;
}

canonical_decompiled_code:
int func2(const char *a1, ...)
{
int *v1; // rax
int v2; // ebp
int *v3; // rbx
int result; // eax
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

va_start(va, a1);
v1 = _errno_location();
v2 = *v1;
v3 = v1;
result = func0(1, *v1, 1, a1, va);
*v3 = v2;
return result;
}

original_code:
void
errMsg(const char *format, ...)
{
    va_list argList;
    int savedErrno;
    savedErrno = (*__errno_location ());
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), TRUE, format, argList);
    __builtin_va_end(argList);
    (*__errno_location ()) = savedErrno;
}

canonical_original_code:
void errMsg(char *format, ...)
{
    __builtin_va_list argList;
    int savedErrno;
    savedErrno = (*__errno_location ());
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), TRUE, format, argList);
    __builtin_va_end(argList);
    (*__errno_location ()) = savedErrno;
}

variable_types: savedErrno int


errExit

decompiled_code:
void __noreturn errExit(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
outputError(1, *v1, 1, a1, va);
terminate(1);
}

canonical_decompiled_code:
void __noreturn func3(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
func0(1, *v1, 1, a1, va);
func1(1);
}

original_code:
void
errExit(const char *format, ...)
{
    va_list argList;
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), TRUE, format, argList);
    __builtin_va_end(argList);
    terminate(TRUE);
}

canonical_original_code:
void errExit(char *format, ...)
{
    __builtin_va_list argList;
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), TRUE, format, argList);
    __builtin_va_end(argList);
    terminate(TRUE);
}


decompiled_code:
void __noreturn err_exit(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
outputError(1, *v1, 0, a1, va);
terminate(0);
}

canonical_decompiled_code:
void __noreturn func4(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
func0(1, *v1, 0, a1, va);
func1(0);
}

original_code:
void
err_exit(const char *format, ...)
{
    va_list argList;
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), FALSE, format, argList);
    __builtin_va_end(argList);
    terminate(FALSE);
}

canonical_original_code:
void err_exit(char *format, ...)
{
    __builtin_va_list argList;
    __builtin_va_start(argList,format);
    outputError(TRUE, (*__errno_location ()), FALSE, format, argList);
    __builtin_va_end(argList);
    terminate(FALSE);
}


fatal

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

va_start(va, a1);
outputError(0, 0, 1, a1, va);
terminate(1);
}

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

va_start(va, a1);
func0(0, 0, 1, a1, va);
func1(1);
}

original_code:
void
fatal(const char *format, ...)
{
    va_list argList;
    __builtin_va_start(argList,format);
    outputError(FALSE, 0, TRUE, format, argList);
    __builtin_va_end(argList);
    terminate(TRUE);
}

canonical_original_code:
void fatal(char *format, ...)
{
    __builtin_va_list argList;
    __builtin_va_start(argList,format);
    outputError(FALSE, 0, TRUE, format, argList);
    __builtin_va_end(argList);
    terminate(TRUE);
}


usageErr

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

va_start(arg, format);
fflush(stdout);
fwrite("Usage: ", 1uLL, 7uLL, stderr);
vfprintf(stderr, format, arg);
fflush(stderr);
exit(1);
}

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

va_start(arg, format);
fflush(stdout);
fwrite("Usage: ", 1uLL, 7uLL, stderr);
vfprintf(stderr, format, arg);
fflush(stderr);
exit(1);
}

original_code:
void
usageErr(const char *format, ...)
{
    va_list argList;
    fflush(stdout);
    fprintf(stderr, "Usage: ");
    __builtin_va_start(argList,format);
    vfprintf(stderr, format, argList);
    __builtin_va_end(argList);
    fflush(stderr);
    exit(1);
}

canonical_original_code:
void usageErr(char *format, ...)
{
    __builtin_va_list argList;
    fflush(stdout);
    fprintf(stderr, "Usage: ");
    __builtin_va_start(argList,format);
    vfprintf(stderr, format, argList);
    __builtin_va_end(argList);
    fflush(stderr);
    exit(1);
}


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

if ( signal(2, (__sighandler_t)((char *)init_shell + 1)) == (__sighandler_t)-1LL )
{
errExit("signal");
result = teardown_job();
}
else
{
puts("Tiny Shell v1.0 Created by Yanzhe Chen, 2013.03");
puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>");
puts("This is free software: you are free to change and redistribute it.");
puts("There is NO WARRANTY, to the extent permitted by law.");
result = puts("For bug reporting, please email: <0x2fdev0x2fnull@gmail.com>");
}
return result;
}

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

if ( signal(2, (__sighandler_t)((char *)init_shell + 1)) == (__sighandler_t)-1LL )
{
errExit("signal");
result = func1();
}
else
{
puts("Tiny Shell v1.0 Created by Yanzhe Chen, 2013.03");
puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>");
puts("This is free software: you are free to change and redistribute it.");
puts("There is NO WARRANTY, to the extent permitted by law.");
result = puts("For bug reporting, please email: <0x2fdev0x2fnull@gmail.com>");
}
return result;
}

original_code:
void init_shell(void)
{
    if (signal(2, ((__sighandler_t) 1)) == ((__sighandler_t) -1))
        errExit("signal");
    printf("Tiny Shell v1.0 Created by Yanzhe Chen, 2013.03\n");
    printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
    printf("This is free software: you are free to change and redistribute it.\n");
    printf("There is NO WARRANTY, to the extent permitted by law.\n");
    printf("For bug reporting, please email: <0x2fdev0x2fnull@gmail.com>\n");
}

canonical_original_code:
void init_shell()
{
    if (signal(2, ((void (*)(int)) 1)) == ((__sighandler_t) -1))
        errExit("signal");
    printf("Tiny Shell v1.0 Created by Yanzhe Chen, 2013.03\n");
    printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
    printf("This is free software: you are free to change and redistribute it.\n");
    printf("There is NO WARRANTY, to the extent permitted by law.\n");
    printf("For bug reporting, please email: <0x2fdev0x2fnull@gmail.com>\n");
}


decompiled_code:
void teardown_job()
{
_QWORD *v0; // rdi
void **i; // rdi
void **v2; // rbp
void *v3; // rax

v0 = j;
if ( j )
{
if ( !*(_QWORD *)j )
goto LABEL_6;
for ( i = *(void ***)j; ; i = v2 )
{
free(i);
v0 = j;
LABEL_6:
v2 = (void **)v0[1];
if ( !v2 )
break;
v3 = v2[1];
v0[1] = v3;
if ( !v3 )
v0[2] = v0 + 1;
free(*v2);
}
free(v0);
}
}

original_code:
void teardown_job(void)
{
    if (j != ((void *)0))
    {
        if (j->cmd != ((void *)0))
            free(j->cmd);
        process *p;
        while (!(((&j->head)->sqh_first) == ((void *)0)))
        {
            p = ((&j->head)->sqh_first);
            do { if (((&j->head)->sqh_first = (&j->head)->sqh_first->next.sqe_next) == ((void *)0)) (&j->head)->sqh_last = &(&j->head)->sqh_first; } while (0);
            free(p->argv);
            free(p);
        }
        free(j);
    }
}

canonical_original_code:
void teardown_job()
{
    if (j != ((void *)0))
    {
        if (j->cmd != ((void *)0))
            free(j->cmd);
        struct process *p;
        while (!(((&j->head)->sqh_first) == ((void *)0)))
        {
            p = ((&j->head)->sqh_first);
            do { if (((&j->head)->sqh_first = (&j->head)->sqh_first->next.sqe_next) == ((void *)0)) (&j->head)->sqh_last = &(&j->head)->sqh_first; } while (0);
            free(p->argv);
            free(p);
        }
        free(j);
    }
}

variable_types: struct process { char **argv; struct { struct process *sqe_next; } next; } *


decompiled_code:
void __fastcall __noreturn launch_process(const char ***a1, const char **a2, int a3)
{
if ( (_DWORD)a2 )
goto LABEL_7;
while ( a3 == 1 )
{
LABEL_6:
a2 = *a1;
execvp(**a1, (char *const *)*a1);
errExit("execvp");
LABEL_7:
if ( dup2((int)a2, 0) )
goto LABEL_4;
close((int)a2);
}
if ( dup2(a3, 1) != 1 )
LABEL_4:
errExit("dup2");
close(a3);
goto LABEL_6;
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const char ***a1, const char **a2, int a3)
{
if ( (_DWORD)a2 )
goto LABEL_7;
while ( a3 == 1 )
{
LABEL_6:
a2 = *a1;
execvp(**a1, (char *const *)*a1);
errExit("execvp");
LABEL_7:
if ( dup2((int)a2, 0) )
goto LABEL_4;
close((int)a2);
}
if ( dup2(a3, 1) != 1 )
LABEL_4:
errExit("dup2");
close(a3);
goto LABEL_6;
}

original_code:
void launch_process(process *p, int infile, int outfile, int bg)
{
    if (infile != 0)
    {
        if (dup2(infile, 0) != 0)
            errExit("dup2");
        close(infile);
    }
    if (outfile != 1)
    {
        if (dup2(outfile, 1) != 1)
            errExit("dup2");
        close(outfile);
    }
    execvp(p->argv[0], p->argv);
    errExit("execvp");
}

canonical_original_code:
void launch_process(struct process *p, int infile, int outfile, int bg)
{
    if (infile != 0)
    {
        if (dup2(infile, 0) != 0)
            errExit("dup2");
        close(infile);
    }
    if (outfile != 1)
    {
        if (dup2(outfile, 1) != 1)
            errExit("dup2");
        close(outfile);
    }
    execvp(p->argv[0], p->argv);
    errExit("execvp");
}

variable_types: infile int

variable_types: outfile int

variable_types: bg int

user_defined_types: struct process { char **argv; struct { struct process *sqe_next; } next; }


decompiled_code:
__pid_t __fastcall launch_job(int a1)
{
__pid_t result; // eax
__int64 v2; // rbp
unsigned int v3; // er13
int v4; // er12
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

result = (int)j;
v2 = *((_QWORD *)j + 1);
v3 = *((_DWORD *)j + 6);
if ( v2 )
{
while ( 1 )
{
if ( *(_QWORD *)(v2 + 8) )
{
if ( pipe(pipedes) == -1 )
goto LABEL_20;
v4 = pipedes[1];
}
else
{
v4 = *((_DWORD *)j + 7);
}
result = fork();
if ( result == -1 )
break;
if ( !result )
launch_process((const char ***)v2, (const char **)v3, v4);
if ( !a1 )
{
result = waitpid(result, 0LL, 0);
if ( result == -1 )
return errExit("waitpid");
}
if ( v3 )
result = close(v3);
if ( v4 != 1 )
result = close(v4);
v2 = *(_QWORD *)(v2 + 8);
v3 = pipedes[0];
if ( !v2 )
return result;
}
errExit("fork");
LABEL_20:
errExit("pipe");
result = create_process();
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func3(int a1)
{
__pid_t result; // eax
__int64 v2; // rbp
unsigned int v3; // er13
int v4; // er12
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

result = (int)j;
v2 = *((_QWORD *)j + 1);
v3 = *((_DWORD *)j + 6);
if ( v2 )
{
while ( 1 )
{
if ( *(_QWORD *)(v2 + 8) )
{
if ( pipe(pipedes) == -1 )
goto LABEL_20;
v4 = pipedes[1];
}
else
{
v4 = *((_DWORD *)j + 7);
}
result = fork();
if ( result == -1 )
break;
if ( !result )
func2((const char ***)v2, (const char **)v3, v4);
if ( !a1 )
{
result = waitpid(result, 0LL, 0);
if ( result == -1 )
return errExit("waitpid");
}
if ( v3 )
result = close(v3);
if ( v4 != 1 )
result = close(v4);
v2 = *(_QWORD *)(v2 + 8);
v3 = pipedes[0];
if ( !v2 )
return result;
}
errExit("fork");
LABEL_20:
errExit("pipe");
result = func4();
}
return result;
}

original_code:
void launch_job(int bg)
{
    process *p;
    pid_t pid;
    int mypipe[2], infile, outfile;
    infile = j->in;
    for((p) = ((&j->head)->sqh_first); (p) != ((void *)0); (p) = ((p)->next.sqe_next))
    {
        if (((p)->next.sqe_next))
        {
            if (pipe(mypipe) == -1)
                errExit("pipe");
            outfile = mypipe[1];
        }
        else
            outfile = j->out;
        switch(pid = fork())
        {
        case -1:
            errExit("fork");
        case 0:
            launch_process(p, infile, outfile, bg);
            break;
        default:
            if (!bg)
                if (waitpid(pid, ((void *)0), 0) == -1)
                    errExit("waitpid");
            break;
        }
        if (infile != 0)
            close(infile);
        if (outfile != 1)
            close(outfile);
        infile = mypipe[0];
    }
}

canonical_original_code:
void launch_job(int bg)
{
    struct process *p;
    int pid;
    int mypipe[2]; int infile; int outfile;
    infile = j->in;
    for((p) = ((&j->head)->sqh_first); (p) != ((void *)0); (p) = ((p)->next.sqe_next))
    {
        if (((p)->next.sqe_next))
        {
            if (pipe(mypipe) == -1)
                errExit("pipe");
            outfile = mypipe[1];
        }
        else
            outfile = j->out;
        switch(pid = fork())
        {
        case -1:
            errExit("fork");
        case 0:
            launch_process(p, infile, outfile, bg);
            break;
        default:
            if (!bg)
                if (waitpid(pid, ((void *)0), 0) == -1)
                    errExit("waitpid");
            break;
        }
        if (infile != 0)
            close(infile);
        if (outfile != 1)
            close(outfile);
        infile = mypipe[0];
    }
}

variable_types: pid int

variable_types:
mypipe
int [2]


decompiled_code:
__int64 __fastcall create_process(char *a1, unsigned __int64 a2)
{
_QWORD *v3; // rax
_QWORD *v4; // r13
_QWORD *v5; // rax
void *v6; // rdi
unsigned __int64 v7; // r15
unsigned __int64 v8; // r14
const unsigned __int16 **v9; // r8
const unsigned __int16 *v10; // rax
__int64 v11; // rdx
char *v12; // rcx
char *v13; // rbp
_QWORD **v14; // rax
__int64 result; // rax
bool v16; // cc
void *v17; // rax
const unsigned __int16 **v18; // [rsp+8h] [rbp-40h]

v3 = malloc(0x10uLL);
if ( v3 && (v4 = v3, v5 = malloc(0x48uLL), *v4 = v5, (v6 = v5) != 0LL) )
{
v5[8] = 0LL;
if ( (unsigned __int64)a1 >= a2 )
{
*v5 = 0LL;
}
else
{
v7 = 8LL;
v8 = 0LL;
v6 = v5;
v9 = _ctype_b_loc();
while ( 2 )
{
v10 = *v9;
while ( 1 )
{
v11 = *a1;
v12 = a1++;
if ( (v10[v11] & 0x2000) == 0 )
break;
if ( a2 <= (unsigned __int64)a1 )
{
v13 = v12 + 2;
if ( a2 <= (unsigned __int64)(v12 + 2) )
goto LABEL_15;
goto LABEL_11;
}
}
v16 = a2 <= (unsigned __int64)a1;
v13 = a1;
a1 = v12;
if ( v16 )
goto LABEL_12;
do
{
LABEL_11:
if ( (v10[*v13] & 0x2000) != 0 )
break;
++v13;
}
while ( a2 > (unsigned __int64)v13 );
LABEL_12:
if ( a2 > (unsigned __int64)a1 )
{
if ( v8 >= v7 )
{
v18 = v9;
v17 = realloc(v6, 16 * v7 + 8);
*v4 = v17;
v6 = v17;
if ( !v17 )
goto LABEL_24;
v9 = v18;
*((_QWORD *)v17 + 2 * v7) = 0LL;
v7 *= 2LL;
}
*((_QWORD *)v6 + v8++) = a1;
*v13 = 0;
}
LABEL_15:
a1 = v13 + 1;
if ( a2 > (unsigned __int64)(v13 + 1) )
continue;
break;
}
*((_QWORD *)v6 + v8) = 0LL;
if ( v8 )
{
v14 = (_QWORD **)j;
v4[1] = 0LL;
*v14[2] = v4;
v14[2] = v4 + 1;
return 0LL;
}
}
free(v6);
free(v4);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_24:
errExit("malloc");
result = create_job();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, unsigned __int64 a2)
{
_QWORD *v3; // rax
_QWORD *v4; // r13
_QWORD *v5; // rax
void *v6; // rdi
unsigned __int64 v7; // r15
unsigned __int64 v8; // r14
const unsigned __int16 **v9; // r8
const unsigned __int16 *v10; // rax
__int64 v11; // rdx
char *v12; // rcx
char *v13; // rbp
_QWORD **v14; // rax
__int64 result; // rax
bool v16; // cc
void *v17; // rax
const unsigned __int16 **v18; // [rsp+8h] [rbp-40h]

v3 = malloc(0x10uLL);
if ( v3 && (v4 = v3, v5 = malloc(0x48uLL), *v4 = v5, (v6 = v5) != 0LL) )
{
v5[8] = 0LL;
if ( (unsigned __int64)a1 >= a2 )
{
*v5 = 0LL;
}
else
{
v7 = 8LL;
v8 = 0LL;
v6 = v5;
v9 = _ctype_b_loc();
while ( 2 )
{
v10 = *v9;
while ( 1 )
{
v11 = *a1;
v12 = a1++;
if ( (v10[v11] & 0x2000) == 0 )
break;
if ( a2 <= (unsigned __int64)a1 )
{
v13 = v12 + 2;
if ( a2 <= (unsigned __int64)(v12 + 2) )
goto LABEL_15;
goto LABEL_11;
}
}
v16 = a2 <= (unsigned __int64)a1;
v13 = a1;
a1 = v12;
if ( v16 )
goto LABEL_12;
do
{
LABEL_11:
if ( (v10[*v13] & 0x2000) != 0 )
break;
++v13;
}
while ( a2 > (unsigned __int64)v13 );
LABEL_12:
if ( a2 > (unsigned __int64)a1 )
{
if ( v8 >= v7 )
{
v18 = v9;
v17 = realloc(v6, 16 * v7 + 8);
*v4 = v17;
v6 = v17;
if ( !v17 )
goto LABEL_24;
v9 = v18;
*((_QWORD *)v17 + 2 * v7) = 0LL;
v7 *= 2LL;
}
*((_QWORD *)v6 + v8++) = a1;
*v13 = 0;
}
LABEL_15:
a1 = v13 + 1;
if ( a2 > (unsigned __int64)(v13 + 1) )
continue;
break;
}
*((_QWORD *)v6 + v8) = 0LL;
if ( v8 )
{
v14 = (_QWORD **)j;
v4[1] = 0LL;
*v14[2] = v4;
v14[2] = v4 + 1;
return 0LL;
}
}
free(v6);
free(v4);
result = 0xFFFFFFFFLL;
}
else
{
LABEL_24:
errExit("malloc");
result = func5();
}
return result;
}

original_code:
int create_process(char *sp, char *ep)
{
    char *sa, *ea;
    process *p;
    size_t argc, size;
    sa = sp;
    ea = ((void *)0);
    argc = 0;
    size = 8;
    if ((p = (process *) malloc(sizeof(process))) == ((void *)0))
        errExit("malloc");
    if ((p->argv = (char **) malloc((size + 1) * sizeof(char *))) == ((void *)0))
        errExit("malloc");
    p->argv[size] = ((void *)0);
    while (sa < ep)
    {
        while (sa < ep && ((*__ctype_b_loc ())[(int) ((*sa))] & (unsigned short int) _ISspace))
            sa++;
        for (ea = sa + 1; ea < ep && !((*__ctype_b_loc ())[(int) ((*ea))] & (unsigned short int) _ISspace); ea++)
            ;
        if (sa < ep)
        {
            if (argc >= size)
            {
                size *= 2;
                p->argv = (char **) realloc(p->argv, (size + 1) * sizeof(char *));
                if (p->argv == ((void *)0))
                    errExit("malloc");
                p->argv[size] = ((void *)0);
            }
            p->argv[argc++] = sa;
            *ea = '\0';
        }
        sa = ea + 1;
    }
    p->argv[argc] = ((void *)0);
    if (argc == 0)
    {
        free(p->argv);
        free(p);
        return -1;
    }
    else
        do { (p)->next.sqe_next = ((void *)0); *(&j->head)->sqh_last = (p); (&j->head)->sqh_last = &(p)->next.sqe_next; } while (0);
    return 0;
}

canonical_original_code:
int create_process(char *sp, char *ep)
{
    char *sa; char *ea;
    struct process *p;
    unsigned long argc; unsigned long size;
    sa = sp;
    ea = ((void *)0);
    argc = 0;
    size = 8;
    if ((p = (struct process *) malloc(sizeof(process))) == ((void *)0))
        errExit("malloc");
    if ((p->argv = (char **) malloc((size + 1) * sizeof(char *))) == ((void *)0))
        errExit("malloc");
    p->argv[size] = ((void *)0);
    while (sa < ep)
    {
        while (sa < ep && ((*__ctype_b_loc ())[(int) ((*sa))] & (unsigned short) _ISspace))
            sa++;
        for (ea = sa + 1; ea < ep && !((*__ctype_b_loc ())[(int) ((*ea))] & (unsigned short) _ISspace); ea++)
            ;
        if (sa < ep)
        {
            if (argc >= size)
            {
                size *= 2;
                p->argv = (char **) realloc(p->argv, (size + 1) * sizeof(char *));
                if (p->argv == ((void *)0))
                    errExit("malloc");
                p->argv[size] = ((void *)0);
            }
            p->argv[argc++] = sa;
            *ea = '\0';
        }
        sa = ea + 1;
    }
    p->argv[argc] = ((void *)0);
    if (argc == 0)
    {
        free(p->argv);
        free(p);
        return -1;
    }
    else
        do { (p)->next.sqe_next = ((void *)0); *(&j->head)->sqh_last = (p); (&j->head)->sqh_last = &(p)->next.sqe_next; } while (0);
    return 0;
}

variable_types:
sp
char *

variable_types:
ep
char *

variable_types:
sa
char *

variable_types:
ea
char *

variable_types:
argc
unsigned long

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall create_job()
{
char *i; // rbx
char *v1; // rbp
const char *v2; // r12
__ssize_t v3; // r13
char *v4; // r14
char *v5; // r15
_QWORD *v6; // rax
FILE *v7; // rdx
_DWORD *v8; // r12
char *v9; // rax
char *v10; // r15
char *v11; // rbx
char *v12; // r14
const unsigned __int16 *v13; // rdx
char v14; // al
char *v15; // rax
char *v16; // rax
unsigned int v17; // er12
char *v18; // rax
char *v19; // rax
char *v20; // rbx
__int64 v22; // rbx
const unsigned __int16 *v23; // rdx
__int64 v24; // rax
char *v25; // rax
size_t v26; // rdx
char *v27; // r8
char *v28; // rax
size_t v29; // rdx
int v30; // eax
__int64 v31; // rbx
const unsigned __int16 *v32; // rdx
__int64 v33; // rax
char *v34; // rax
size_t v35; // rdx
_DWORD *v36; // r12
int v37; // eax
size_t v38; // rax
size_t v39; // [rsp+8h] [rbp-50h]
char *v40; // [rsp+8h] [rbp-50h]
size_t v41; // [rsp+8h] [rbp-50h]
size_t v42; // [rsp+8h] [rbp-50h]
size_t n[8]; // [rsp+18h] [rbp-40h] BYREF

n[0] = 0LL;
v6 = malloc(0x28uLL);
j = v6;
if ( !v6 )
goto LABEL_65;
v6[1] = 0LL;
v7 = stdin;
v6[2] = v6 + 1;
v6[3] = 0x100000000LL;
v3 = getline((char **)v6, n, v7);
if ( v3 == -1 )
{
fatal("getline");
LABEL_65:
errExit("malloc");
goto LABEL_66;
}
v8 = j;
v1 = *(char **)j;
if ( **(_BYTE **)j == 10 )
return (unsigned int)-1;
v9 = strrchr(*(const char **)j, 38);
v10 = v9;
if ( v9 )
{
v11 = v9 + 1;
v12 = strchr(v1, 10);
if ( v12 > v11 )
{
v13 = *_ctype_b_loc();
while ( (v13[*v11] & 0x2000) != 0 )
{
if ( v12 == ++v11 )
{
v8[8] = 1;
goto LABEL_27;
}
}
}
v8[8] = v12 == v11;
if ( v12 == v11 )
{
LABEL_27:
*v10 = 0;
v8 = j;
v1 = *(char **)j;
v14 = **(_BYTE **)j;
if ( v14 != 99 )
goto LABEL_11;
goto LABEL_28;
}
}
else
{
v8[8] = 0;
}
v14 = *v1;
if ( *v1 == 99 )
{
LABEL_28:
if ( v1[1] != 100 || ((*_ctype_b_loc())[v1[2]] & 0x2000) == 0 )
goto LABEL_13;
v1[v3 - 1] = 0;
if ( chdir((const char *)(*(_QWORD *)j + 3LL)) == -1 )
{
errExit("chdir");
LABEL_32:
errMsg("missing filename after '<'");
}
return (unsigned int)-1;
}
LABEL_11:
if ( v14 == 113 && ((*_ctype_b_loc())[v1[1]] & 0x2000) != 0 )
return 1;
LABEL_13:
v5 = strchr(v1, 60);
if ( v5 )
{
v15 = strchr(v1, 124);
if ( !v15 || v5 < v15 )
{
v22 = v5[1];
v4 = v5 + 1;
if ( (_BYTE)v22 )
{
v23 = *_ctype_b_loc();
do
{
if ( (v23[v22] & 0x2000) == 0 )
{
v24 = *v4;
for ( i = v4; (_BYTE)v24; v24 = *++i )
{
if ( (v23[v24] & 0x2000) != 0 )
break;
}
n[0] = i - v4;
v25 = (char *)malloc(i - v4 + 1);
v26 = i - v4;
v27 = v25;
if ( !v25 )
goto LABEL_68;
goto LABEL_45;
}
v22 = *++v4;
}
while ( (_BYTE)v22 );
}
goto LABEL_32;
}
}
while ( 1 )
{
v4 = strchr(v1, 62);
if ( !v4 )
goto LABEL_22;
v16 = strrchr(v1, 124);
if ( v16 )
{
if ( v4 <= v16 )
goto LABEL_22;
}
v31 = v4[1];
v2 = v4 + 1;
if ( !(_BYTE)v31 )
{
LABEL_63:
v17 = -1;
errMsg("missing filename after '>'");
return v17;
}
v32 = *_ctype_b_loc();
while ( (v32[v31] & 0x2000) != 0 )
{
v31 = *++v2;
if ( !(_BYTE)v31 )
goto LABEL_63;
}
v33 = *v2;
for ( i = (char *)v2; (_BYTE)v33; v33 = *++i )
{
if ( (v32[v33] & 0x2000) != 0 )
break;
}
n[0] = i - v2;
v34 = (char *)malloc(i - v2 + 1);
v35 = i - v2;
v5 = v34;
if ( !v34 )
{
LABEL_66:
errMsg("malloc");
v35 = n[0];
}
v41 = v35;
strncpy(v5, v2, v35);
v36 = j;
v5[v41] = 0;
v37 = open(v5, 577, 432LL);
v36[7] = v37;
if ( v37 != -1 )
break;
do
{
errExit("open");
LABEL_68:
v42 = (size_t)v27;
errMsg("malloc");
v26 = n[0];
v27 = (char *)v42;
v8 = j;
LABEL_45:
v39 = v26;
v28 = strncpy(v27, v4, v26);
v29 = v39;
v40 = v28;
v28[v29] = 0;
v30 = open(v28, 0);
v8[6] = v30;
}
while ( v30 == -1 );
free(v40);
if ( v5 < i )
memset(v5, 32, i - v5);
}
free(v5);
if ( v4 < i )
memset(v4, 32, i - v4);
LABEL_22:
while ( 1 )
{
v19 = strchr(v1, 124);
v20 = v19;
if ( !v19 )
break;
v17 = create_process(v1, (unsigned __int64)v19);
if ( v17 == -1 )
{
errMsg("empty pipeline");
return v17;
}
v18 = (char *)(v20 - v1);
v1 = v20 + 1;
v3 -= (__int64)(v18 + 1);
}
if ( v3 <= 0 )
return 0;
v38 = strlen(v1);
v17 = create_process(v1, (unsigned __int64)&v1[v38]);
if ( v17 != -1 )
return 0;
errMsg("empty command");
return v17;
}

canonical_decompiled_code:
__int64 __fastcall func5()
{
char *i; // rbx
char *v1; // rbp
const char *v2; // r12
__ssize_t v3; // r13
char *v4; // r14
char *v5; // r15
_QWORD *v6; // rax
FILE *v7; // rdx
_DWORD *v8; // r12
char *v9; // rax
char *v10; // r15
char *v11; // rbx
char *v12; // r14
const unsigned __int16 *v13; // rdx
char v14; // al
char *v15; // rax
char *v16; // rax
unsigned int v17; // er12
char *v18; // rax
char *v19; // rax
char *v20; // rbx
__int64 v22; // rbx
const unsigned __int16 *v23; // rdx
__int64 v24; // rax
char *v25; // rax
size_t v26; // rdx
char *v27; // r8
char *v28; // rax
size_t v29; // rdx
int v30; // eax
__int64 v31; // rbx
const unsigned __int16 *v32; // rdx
__int64 v33; // rax
char *v34; // rax
size_t v35; // rdx
_DWORD *v36; // r12
int v37; // eax
size_t v38; // rax
size_t v39; // [rsp+8h] [rbp-50h]
char *v40; // [rsp+8h] [rbp-50h]
size_t v41; // [rsp+8h] [rbp-50h]
size_t v42; // [rsp+8h] [rbp-50h]
size_t n[8]; // [rsp+18h] [rbp-40h] BYREF

n[0] = 0LL;
v6 = malloc(0x28uLL);
j = v6;
if ( !v6 )
goto LABEL_65;
v6[1] = 0LL;
v7 = stdin;
v6[2] = v6 + 1;
v6[3] = 0x100000000LL;
v3 = getline((char **)v6, n, v7);
if ( v3 == -1 )
{
fatal("getline");
LABEL_65:
errExit("malloc");
goto LABEL_66;
}
v8 = j;
v1 = *(char **)j;
if ( **(_BYTE **)j == 10 )
return (unsigned int)-1;
v9 = strrchr(*(const char **)j, 38);
v10 = v9;
if ( v9 )
{
v11 = v9 + 1;
v12 = strchr(v1, 10);
if ( v12 > v11 )
{
v13 = *_ctype_b_loc();
while ( (v13[*v11] & 0x2000) != 0 )
{
if ( v12 == ++v11 )
{
v8[8] = 1;
goto LABEL_27;
}
}
}
v8[8] = v12 == v11;
if ( v12 == v11 )
{
LABEL_27:
*v10 = 0;
v8 = j;
v1 = *(char **)j;
v14 = **(_BYTE **)j;
if ( v14 != 99 )
goto LABEL_11;
goto LABEL_28;
}
}
else
{
v8[8] = 0;
}
v14 = *v1;
if ( *v1 == 99 )
{
LABEL_28:
if ( v1[1] != 100 || ((*_ctype_b_loc())[v1[2]] & 0x2000) == 0 )
goto LABEL_13;
v1[v3 - 1] = 0;
if ( chdir((const char *)(*(_QWORD *)j + 3LL)) == -1 )
{
errExit("chdir");
LABEL_32:
errMsg("missing filename after '<'");
}
return (unsigned int)-1;
}
LABEL_11:
if ( v14 == 113 && ((*_ctype_b_loc())[v1[1]] & 0x2000) != 0 )
return 1;
LABEL_13:
v5 = strchr(v1, 60);
if ( v5 )
{
v15 = strchr(v1, 124);
if ( !v15 || v5 < v15 )
{
v22 = v5[1];
v4 = v5 + 1;
if ( (_BYTE)v22 )
{
v23 = *_ctype_b_loc();
do
{
if ( (v23[v22] & 0x2000) == 0 )
{
v24 = *v4;
for ( i = v4; (_BYTE)v24; v24 = *++i )
{
if ( (v23[v24] & 0x2000) != 0 )
break;
}
n[0] = i - v4;
v25 = (char *)malloc(i - v4 + 1);
v26 = i - v4;
v27 = v25;
if ( !v25 )
goto LABEL_68;
goto LABEL_45;
}
v22 = *++v4;
}
while ( (_BYTE)v22 );
}
goto LABEL_32;
}
}
while ( 1 )
{
v4 = strchr(v1, 62);
if ( !v4 )
goto LABEL_22;
v16 = strrchr(v1, 124);
if ( v16 )
{
if ( v4 <= v16 )
goto LABEL_22;
}
v31 = v4[1];
v2 = v4 + 1;
if ( !(_BYTE)v31 )
{
LABEL_63:
v17 = -1;
errMsg("missing filename after '>'");
return v17;
}
v32 = *_ctype_b_loc();
while ( (v32[v31] & 0x2000) != 0 )
{
v31 = *++v2;
if ( !(_BYTE)v31 )
goto LABEL_63;
}
v33 = *v2;
for ( i = (char *)v2; (_BYTE)v33; v33 = *++i )
{
if ( (v32[v33] & 0x2000) != 0 )
break;
}
n[0] = i - v2;
v34 = (char *)malloc(i - v2 + 1);
v35 = i - v2;
v5 = v34;
if ( !v34 )
{
LABEL_66:
errMsg("malloc");
v35 = n[0];
}
v41 = v35;
strncpy(v5, v2, v35);
v36 = j;
v5[v41] = 0;
v37 = open(v5, 577, 432LL);
v36[7] = v37;
if ( v37 != -1 )
break;
do
{
errExit("open");
LABEL_68:
v42 = (size_t)v27;
errMsg("malloc");
v26 = n[0];
v27 = (char *)v42;
v8 = j;
LABEL_45:
v39 = v26;
v28 = strncpy(v27, v4, v26);
v29 = v39;
v40 = v28;
v28[v29] = 0;
v30 = open(v28, 0);
v8[6] = v30;
}
while ( v30 == -1 );
free(v40);
if ( v5 < i )
memset(v5, 32, i - v5);
}
free(v5);
if ( v4 < i )
memset(v4, 32, i - v4);
LABEL_22:
while ( 1 )
{
v19 = strchr(v1, 124);
v20 = v19;
if ( !v19 )
break;
v17 = func4(v1, (unsigned __int64)v19);
if ( v17 == -1 )
{
errMsg("empty pipeline");
return v17;
}
v18 = (char *)(v20 - v1);
v1 = v20 + 1;
v3 -= (__int64)(v18 + 1);
}
if ( v3 <= 0 )
return 0;
v38 = strlen(v1);
v17 = func4(v1, (unsigned __int64)&v1[v38]);
if ( v17 != -1 )
return 0;
errMsg("empty command");
return v17;
}

original_code:
int create_job(void)
{
    size_t len = 0;
    ssize_t read;
    char *sp, *ep;
    char *r, *f, *sf, *ef;
    j = (job *) malloc(sizeof(job));
    if (j == ((void *)0))
        errExit("malloc");
    do { (&j->head)->sqh_first = ((void *)0); (&j->head)->sqh_last = &(&j->head)->sqh_first; } while (0);
    j->in = 0;
    j->out = 1;
    if ((read = getline(&j->cmd, &len, stdin)) == -1)
        fatal("getline");
    if (j->cmd[0] == '\n')
        return -1;
    sp = strrchr(j->cmd, '&');
    ep = strchr(j->cmd, '\n');
    if (sp)
    {
        for (r = sp + 1; r < ep; r++)
            if (!((*__ctype_b_loc ())[(int) ((*r))] & (unsigned short int) _ISspace))
                break;
        j->bg = (r == ep) ? 1 : 0;
    }
    else
        j->bg = 0;
    if (j->bg)
        *sp = '\0';
    if (j->cmd[0] == 'c' && j->cmd[1] == 'd' && ((*__ctype_b_loc ())[(int) ((j->cmd[2]))] & (unsigned short int) _ISspace))
    {
        j->cmd[read-1] = '\0';
        if (chdir(j->cmd+3) == -1)
            errExit("chdir");
        return -1;
    }
    if (j->cmd[0] == 'q' && ((*__ctype_b_loc ())[(int) ((j->cmd[1]))] & (unsigned short int) _ISspace))
        return 1;
    sp = j->cmd;
    ep = ((void *)0);
    r = sf = ef = ((void *)0);
    r = strchr(sp, '<');
    ep = strchr(sp, '|');
    if (r && (!ep || r < ep))
    {
        for (sf = r + 1; *sf != '\0' && ((*__ctype_b_loc ())[(int) ((*sf))] & (unsigned short int) _ISspace); sf++)
            ;
        for (ef = sf; *ef != '\0' && !((*__ctype_b_loc ())[(int) ((*ef))] & (unsigned short int) _ISspace); ef++)
            ;
        if (*sf == '\0')
        {
            errMsg("missing filename after '<'");
            return -1;
        }
        len = ef - sf;
        f = (char *) malloc((len + 1) * sizeof(char));
        if (f == ((void *)0))
            errMsg("malloc");
        strncpy(f, sf, len);
        f[len] = '\0';
        if ((j->in = open(f, 00)) == -1)
            errExit("open");
        free(f);
        for(; r < ef; r++)
            *r = ' ';
    }
    r = strchr(sp, '>');
    ep = strrchr(sp, '|');
    if (r && (!ep || ep < r))
    {
        for (sf = r + 1; *sf != '\0' && ((*__ctype_b_loc ())[(int) ((*sf))] & (unsigned short int) _ISspace); sf++)
            ;
        for (ef = sf; *ef != '\0' && !((*__ctype_b_loc ())[(int) ((*ef))] & (unsigned short int) _ISspace); ef++)
            ;
        if (*sf == '\0')
        {
            errMsg("missing filename after '>'");
            return -1;
        }
        len = ef - sf;
        f = (char *) malloc((len + 1) * sizeof(char));
        if (f == ((void *)0))
            errMsg("malloc");
        strncpy(f, sf, len);
        f[len] = '\0';
        if ((j->out = open(f, 01 | 01000 | 0100,
                    0400 | (0400 >> 3) | (0200 >> 3) | 0200)) == -1)
            errExit("open");
        free(f);
        for(; r < ef; r++)
            *r = ' ';
    }
    while ((ep = strchr(sp, '|')) != ((void *)0))
    {
        if (create_process(sp, ep) == -1)
        {
            errMsg("empty pipeline");
            return -1;
        }
        else
        {
            read -= ep - sp + 1;
            sp = ep + 1;
        }
    }
    if (read > 0)
    {
        if (create_process(sp, sp + strlen(sp)) == -1)
        {
            errMsg("empty command");
            return -1;
        }
    }
    return 0;
}

canonical_original_code:
int create_job()
{
    unsigned long len = 0;
    long read;
    char *sp; char *ep;
    char *r; char *f; char *sf; char *ef;
    j = (struct job *) malloc(sizeof(job));
    if (j == ((void *)0))
        errExit("malloc");
    do { (&j->head)->sqh_first = ((void *)0); (&j->head)->sqh_last = &(&j->head)->sqh_first; } while (0);
    j->in = 0;
    j->out = 1;
    if ((read = getline(&j->cmd, &len, stdin)) == -1)
        fatal("getline");
    if (j->cmd[0] == '\n')
        return -1;
    sp = strrchr(j->cmd, '&');
    ep = strchr(j->cmd, '\n');
    if (sp)
    {
        for (r = sp + 1; r < ep; r++)
            if (!((*__ctype_b_loc ())[(int) ((*r))] & (unsigned short) _ISspace))
                break;
        j->bg = (r == ep) ? 1 : 0;
    }
    else
        j->bg = 0;
    if (j->bg)
        *sp = '\0';
    if (j->cmd[0] == 'c' && j->cmd[1] == 'd' && ((*__ctype_b_loc ())[(int) ((j->cmd[2]))] & (unsigned short) _ISspace))
    {
        j->cmd[read-1] = '\0';
        if (chdir(j->cmd+3) == -1)
            errExit("chdir");
        return -1;
    }
    if (j->cmd[0] == 'q' && ((*__ctype_b_loc ())[(int) ((j->cmd[1]))] & (unsigned short) _ISspace))
        return 1;
    sp = j->cmd;
    ep = ((void *)0);
    r = sf = ef = ((void *)0);
    r = strchr(sp, '<');
    ep = strchr(sp, '|');
    if (r && (!ep || r < ep))
    {
        for (sf = r + 1; *sf != '\0' && ((*__ctype_b_loc ())[(int) ((*sf))] & (unsigned short) _ISspace); sf++)
            ;
        for (ef = sf; *ef != '\0' && !((*__ctype_b_loc ())[(int) ((*ef))] & (unsigned short) _ISspace); ef++)
            ;
        if (*sf == '\0')
        {
            errMsg("missing filename after '<'");
            return -1;
        }
        len = ef - sf;
        f = (char *) malloc((len + 1) * sizeof(char));
        if (f == ((void *)0))
            errMsg("malloc");
        strncpy(f, sf, len);
        f[len] = '\0';
        if ((j->in = open(f, 00)) == -1)
            errExit("open");
        free(f);
        for(; r < ef; r++)
            *r = ' ';
    }
    r = strchr(sp, '>');
    ep = strrchr(sp, '|');
    if (r && (!ep || ep < r))
    {
        for (sf = r + 1; *sf != '\0' && ((*__ctype_b_loc ())[(int) ((*sf))] & (unsigned short) _ISspace); sf++)
            ;
        for (ef = sf; *ef != '\0' && !((*__ctype_b_loc ())[(int) ((*ef))] & (unsigned short) _ISspace); ef++)
            ;
        if (*sf == '\0')
        {
            errMsg("missing filename after '>'");
            return -1;
        }
        len = ef - sf;
        f = (char *) malloc((len + 1) * sizeof(char));
        if (f == ((void *)0))
            errMsg("malloc");
        strncpy(f, sf, len);
        f[len] = '\0';
        if ((j->out = open(f, 01 | 01000 | 0100,
                    0400 | (0400 >> 3) | (0200 >> 3) | 0200)) == -1)
            errExit("open");
        free(f);
        for(; r < ef; r++)
            *r = ' ';
    }
    while ((ep = strchr(sp, '|')) != ((void *)0))
    {
        if (create_process(sp, ep) == -1)
        {
            errMsg("empty pipeline");
            return -1;
        }
        else
        {
            read -= ep - sp + 1;
            sp = ep + 1;
        }
    }
    if (read > 0)
    {
        if (create_process(sp, sp + strlen(sp)) == -1)
        {
            errMsg("empty command");
            return -1;
        }
    }
    return 0;
}

variable_types:
len
unsigned long

variable_types: read long

variable_types:
r
char *

variable_types:
f
char *

variable_types:
sf
char *

variable_types:
ef
char *

user_defined_types: struct job { char *cmd; struct { struct process *sqh_first; struct process **sqh_last; } head; int in; int bg; }


main

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

init_shell();
while ( 1 )
{
while ( 1 )
{
printf("(tsh) ");
v3 = create_job();
if ( v3 )
break;
launch_job(*((_DWORD *)j + 8));
teardown_job();
}
if ( v3 == 1 )
break;
if ( v3 == -1 )
teardown_job();
}
teardown_job();
return 0;
}

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

func0();
while ( 1 )
{
while ( 1 )
{
printf("(tsh) ");
v3 = func5();
if ( v3 )
break;
func3(*((_DWORD *)j + 8));
func1();
}
if ( v3 == 1 )
break;
if ( v3 == -1 )
func1();
}
func1();
return 0;
}

original_code:
int main(void)
{
    int rc;
    init_shell();
    while (1)
    {
        printf("(tsh) ");
        rc = create_job();
        if(rc == 0)
        {
            launch_job(j->bg);
            teardown_job();
        }
        else if (rc == 1)
        {
            teardown_job();
            break;
        }
        else if (rc == -1)
        {
            teardown_job();
            continue;
        }
    }
    return 0;
}

canonical_original_code:
int main()
{
    int rc;
    init_shell();
    while (1)
    {
        printf("(tsh) ");
        rc = create_job();
        if(rc == 0)
        {
            launch_job(j->bg);
            teardown_job();
        }
        else if (rc == 1)
        {
            teardown_job();
            break;
        }
        else if (rc == -1)
        {
            teardown_job();
            continue;
        }
    }
    return 0;
}

variable_types: rc int


decompiled_code:
__int64 __fastcall is_existing_blk_or_reg_file(const char *a1)
{
__int64 result; // rax
int v2; // edx
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v3) >= 0 )
return (((v3.st_mode & 0xF000) - 24576) & 0xFFFFD000) == 0;
v2 = *_errno_location();
result = (unsigned int)-v2;
if ( v2 == 2 )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
__int64 result; // rax
int v2; // edx
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v3) >= 0 )
return (((v3.st_mode & 0xF000) - 24576) & 0xFFFFD000) == 0;
v2 = *_errno_location();
result = (unsigned int)-v2;
if ( v2 == 2 )
result = 0LL;
return result;
}

original_code:
int is_existing_blk_or_reg_file(const char* filename)
{
 struct stat st_buf;
 if(stat(filename, &st_buf) < 0) {
  if((*__errno_location ()) == 2)
   return 0;
  else
   return -(*__errno_location ());
 }
 return (((((st_buf.st_mode)) & 0170000) == (0060000)) || ((((st_buf.st_mode)) & 0170000) == (0100000)));
}

canonical_original_code:
int is_existing_blk_or_reg_file(char *filename)
{
 struct stat st_buf;
 if(stat(filename, &st_buf) < 0) {
  if((*__errno_location ()) == 2)
   return 0;
  else
   return -(*__errno_location ());
 }
 return (((((st_buf.st_mode)) & 0170000) == (0060000)) || ((((st_buf.st_mode)) & 0170000) == (0100000)));
}

variable_types:
filename
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall is_loop_device(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
return (unsigned int)-*_errno_location();
if ( (v2.st_mode & 0xF000) == 24576 )
return v2.st_rdev >> 8 == 7;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
return (unsigned int)-*_errno_location();
if ( (v2.st_mode & 0xF000) == 24576 )
return v2.st_rdev >> 8 == 7;
return 0LL;
}

original_code:
int is_loop_device (const char* device)
{
 struct stat statbuf;
 if(stat(device, &statbuf) < 0)
  return -(*__errno_location ());
 return (((((statbuf.st_mode)) & 0170000) == (0060000)) &&
  ((statbuf.st_rdev)>>8) == 7);
}

canonical_original_code:
int is_loop_device(char *device)
{
 struct stat statbuf;
 if(stat(device, &statbuf) < 0)
  return -(*__errno_location ());
 return (((((statbuf.st_mode)) & 0170000) == (0060000)) &&
  ((statbuf.st_rdev)>>8) == 7);
}

variable_types:
device
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall is_same_blk_file(char *file, char *name)
{
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-2138h] BYREF
struct stat v4; // [rsp+90h] [rbp-20A8h] BYREF
char resolved[4096]; // [rsp+120h] [rbp-2018h] BYREF
char s2[4120]; // [rsp+1120h] [rbp-1018h] BYREF

if ( !realpath(file, resolved) || !realpath(name, s2) )
return (unsigned int)-*_errno_location();
if ( !strcmp(resolved, s2) )
return 1LL;
if ( stat(file, &v3) < 0 || stat(name, &v4) < 0 )
return (unsigned int)-*_errno_location();
if ( (v3.st_mode & 0xF000) == 24576 && (v4.st_mode & 0xF000) == 24576 && v3.st_rdev == v4.st_rdev )
return 1LL;
result = 0LL;
if ( v3.st_dev == v4.st_dev )
result = v3.st_ino == v4.st_ino;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *file, char *name)
{
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-2138h] BYREF
struct stat v4; // [rsp+90h] [rbp-20A8h] BYREF
char resolved[4096]; // [rsp+120h] [rbp-2018h] BYREF
char s2[4120]; // [rsp+1120h] [rbp-1018h] BYREF

if ( !realpath(file, resolved) || !realpath(name, s2) )
return (unsigned int)-*_errno_location();
if ( !strcmp(resolved, s2) )
return 1LL;
if ( stat(file, &v3) < 0 || stat(name, &v4) < 0 )
return (unsigned int)-*_errno_location();
if ( (v3.st_mode & 0xF000) == 24576 && (v4.st_mode & 0xF000) == 24576 && v3.st_rdev == v4.st_rdev )
return 1LL;
result = 0LL;
if ( v3.st_dev == v4.st_dev )
result = v3.st_ino == v4.st_ino;
return result;
}

original_code:
int is_same_blk_file(const char* a, const char* b)
{
 struct stat st_buf_a, st_buf_b;
 char real_a[4096];
 char real_b[4096];
 if(!realpath(a, real_a) ||
    !realpath(b, real_b))
 {
  return -(*__errno_location ());
 }
 if(strcmp(real_a, real_b) == 0)
  return 1;
 if(stat(a, &st_buf_a) < 0 ||
    stat(b, &st_buf_b) < 0)
 {
  return -(*__errno_location ());
 }
 if(((((st_buf_a.st_mode)) & 0170000) == (0060000)) &&
    ((((st_buf_b.st_mode)) & 0170000) == (0060000)) &&
    st_buf_a.st_rdev == st_buf_b.st_rdev)
 {
  return 1;
 }
 if (st_buf_a.st_dev == st_buf_b.st_dev &&
     st_buf_a.st_ino == st_buf_b.st_ino)
 {
  return 1;
 }
 return 0;
}

canonical_original_code:
int is_same_blk_file(char *a, char *b)
{
 struct stat st_buf_a; struct stat st_buf_b;
 char real_a[4096];
 char real_b[4096];
 if(!realpath(a, real_a) ||
    !realpath(b, real_b))
 {
  return -(*__errno_location ());
 }
 if(strcmp(real_a, real_b) == 0)
  return 1;
 if(stat(a, &st_buf_a) < 0 ||
    stat(b, &st_buf_b) < 0)
 {
  return -(*__errno_location ());
 }
 if(((((st_buf_a.st_mode)) & 0170000) == (0060000)) &&
    ((((st_buf_b.st_mode)) & 0170000) == (0060000)) &&
    st_buf_a.st_rdev == st_buf_b.st_rdev)
 {
  return 1;
 }
 if (st_buf_a.st_dev == st_buf_b.st_dev &&
     st_buf_a.st_ino == st_buf_b.st_ino)
 {
  return 1;
 }
 return 0;
}

variable_types:
a
char *

variable_types:
b
char *


decompiled_code:
__int64 __fastcall resolve_loop_device(const char *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
unsigned int v6; // er12
__int64 v8; // [rsp+0h] [rbp-D8h] BYREF
char src[168]; // [rsp+30h] [rbp-A8h] BYREF

v4 = open(a1, 0);
if ( v4 < 0 )
return (unsigned int)-*_errno_location();
v5 = v4;
v6 = ioctl(v4, 0x4C03uLL, &v8);
close(v5);
if ( v6 )
return (unsigned int)-*_errno_location();
strncpy(a2, src, a3);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
unsigned int v6; // er12
__int64 v8; // [rsp+0h] [rbp-D8h] BYREF
char src[168]; // [rsp+30h] [rbp-A8h] BYREF

v4 = open(a1, 0);
if ( v4 < 0 )
return (unsigned int)-*_errno_location();
v5 = v4;
v6 = ioctl(v4, 0x4C03uLL, &v8);
close(v5);
if ( v6 )
return (unsigned int)-*_errno_location();
strncpy(a2, src, a3);
return v6;
}

original_code:
int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
{
 int loop_fd;
 int ret_ioctl;
 struct loop_info loopinfo;
 if ((loop_fd = open(loop_dev, 00)) < 0)
  return -(*__errno_location ());
 ret_ioctl = ioctl(loop_fd, 0x4C03, &loopinfo);
 close(loop_fd);
 if (ret_ioctl == 0)
  strncpy(loop_file, loopinfo.lo_name, max_len);
 else
  return -(*__errno_location ());
 return 0;
}

canonical_original_code:
int resolve_loop_device(char *loop_dev, char *loop_file, int max_len)
{
 int loop_fd;
 int ret_ioctl;
 struct loop_info loopinfo;
 if ((loop_fd = open(loop_dev, 00)) < 0)
  return -(*__errno_location ());
 ret_ioctl = ioctl(loop_fd, 0x4C03, &loopinfo);
 close(loop_fd);
 if (ret_ioctl == 0)
  strncpy(loop_file, loopinfo.lo_name, max_len);
 else
  return -(*__errno_location ());
 return 0;
}

variable_types: struct loop_info { int lo_number; unsigned long lo_device; unsigned long lo_inode; unsigned long lo_rdevice; int lo_offset; int lo_encrypt_type; int lo_encrypt_key_size; int lo_flags; char lo_name[64]; unsigned char lo_encrypt_key[32]; unsigned long lo_init[2]; char reserved[4]; }


decompiled_code:
__int64 __fastcall is_same_loop_file(char *file, char *name)
{
char *v2; // r12
char *v3; // rbp
__int64 result; // rax
char v5[4096]; // [rsp+0h] [rbp-2018h] BYREF
char v6[4120]; // [rsp+1000h] [rbp-1018h] BYREF

v2 = name;
v3 = file;
result = is_loop_device(file);
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
result = resolve_loop_device(file, v5, 4096);
if ( (int)result < 0 )
return result;
v3 = v5;
}
result = is_loop_device(name);
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
result = resolve_loop_device(name, v6, 4096);
if ( (int)result < 0 )
return result;
v2 = v6;
}
result = is_same_blk_file(v3, v2);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *file, char *name)
{
char *v2; // r12
char *v3; // rbp
__int64 result; // rax
char v5[4096]; // [rsp+0h] [rbp-2018h] BYREF
char v6[4120]; // [rsp+1000h] [rbp-1018h] BYREF

v2 = name;
v3 = file;
result = func1(file);
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
result = func3(file, v5, 4096);
if ( (int)result < 0 )
return result;
v3 = v5;
}
result = func1(name);
if ( (int)result >= 0 )
{
if ( (_DWORD)result )
{
result = func3(name, v6, 4096);
if ( (int)result < 0 )
return result;
v2 = v6;
}
result = func2(v3, v2);
}
}
return result;
}

original_code:
int is_same_loop_file(const char* a, const char* b)
{
 char res_a[4096];
 char res_b[4096];
 const char* final_a;
 const char* final_b;
 int ret;
 if((ret = is_loop_device(a)) < 0) {
    return ret;
 } else if(ret) {
  if((ret = resolve_loop_device(a, res_a, sizeof(res_a))) < 0)
   return ret;
  final_a = res_a;
 } else {
  final_a = a;
 }
 if((ret = is_loop_device(b)) < 0) {
    return ret;
 } else if(ret) {
  if((ret = resolve_loop_device(b, res_b, sizeof(res_b))) < 0)
   return ret;
  final_b = res_b;
 } else {
  final_b = b;
 }
 return is_same_blk_file(final_a, final_b);
}

canonical_original_code:
int is_same_loop_file(char *a, char *b)
{
 char res_a[4096];
 char res_b[4096];
 char *final_a;
 char *final_b;
 int ret;
 if((ret = is_loop_device(a)) < 0) {
    return ret;
 } else if(ret) {
  if((ret = resolve_loop_device(a, res_a, sizeof(res_a))) < 0)
   return ret;
  final_a = res_a;
 } else {
  final_a = a;
 }
 if((ret = is_loop_device(b)) < 0) {
    return ret;
 } else if(ret) {
  if((ret = resolve_loop_device(b, res_b, sizeof(res_b))) < 0)
   return ret;
  final_b = res_b;
 } else {
  final_b = b;
 }
 return is_same_blk_file(final_a, final_b);
}

variable_types: ret int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
unsigned int v4; // er12
int v5; // ebp
__int64 v7; // rdx
__int64 v8; // [rsp+0h] [rbp-48h] BYREF
__int16 v9; // [rsp+8h] [rbp-40h]
_DWORD v10[4]; // [rsp+Ah] [rbp-3Eh] BYREF
__int64 v11; // [rsp+1Ah] [rbp-2Eh]

if ( argc != 2 )
{
v4 = -1;
fwrite("give a partition\n", 1uLL, 0x11uLL, stderr);
goto LABEL_7;
}
v3 = (char *)argv[1];
fprintf(stdout, "Format %s , check: \n", v3);
v4 = check_mounted(v3);
if ( v4 )
{
LABEL_7:
v7 = v4;
v4 = -1;
fprintf(stderr, "Fomat err %d\n", v7);
return v4;
}
fprintf(stdout, "Format %s , begin format: \n", v3);
v5 = open(v3, 2);
if ( v5 < 0 )
{
fprintf(stderr, "unable to open %s\n", v3);
exit(1);
}
fwrite("Write Super Block \n", 1uLL, 0x13uLL, stdout);
strcpy((char *)v10, "e^(PIi)+1=0");
v8 = 0x1234567012345LL;
v9 = 4096;
v10[3] = 0;
v11 = 0x100000LL;
fprintf(stdout, "magic %s %lu\n", (const char *)v10, 34LL);
if ( (unsigned int)pwrite(v5, &v8, 0x22uLL, 0x10000LL) != 34 )
{
v4 = 18;
fwrite("write super fail.\n", 1uLL, 0x12uLL, stderr);
goto LABEL_7;
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r13
unsigned int v4; // er12
int v5; // ebp
__int64 v7; // rdx
__int64 v8; // [rsp+0h] [rbp-48h] BYREF
__int16 v9; // [rsp+8h] [rbp-40h]
_DWORD v10[4]; // [rsp+Ah] [rbp-3Eh] BYREF
__int64 v11; // [rsp+1Ah] [rbp-2Eh]

if ( argc != 2 )
{
v4 = -1;
fwrite("give a partition\n", 1uLL, 0x11uLL, stderr);
goto LABEL_7;
}
v3 = (char *)argv[1];
fprintf(stdout, "Format %s , check: \n", v3);
v4 = func5(v3);
if ( v4 )
{
LABEL_7:
v7 = v4;
v4 = -1;
fprintf(stderr, "Fomat err %d\n", v7);
return v4;
}
fprintf(stdout, "Format %s , begin format: \n", v3);
v5 = open(v3, 2);
if ( v5 < 0 )
{
fprintf(stderr, "unable to open %s\n", v3);
exit(1);
}
fwrite("Write Super Block \n", 1uLL, 0x13uLL, stdout);
strcpy((char *)v10, "e^(PIi)+1=0");
v8 = 0x1234567012345LL;
v9 = 4096;
v10[3] = 0;
v11 = 0x100000LL;
fprintf(stdout, "magic %s %lu\n", (const char *)v10, 34LL);
if ( (unsigned int)pwrite(v5, &v8, 0x22uLL, 0x10000LL) != 34 )
{
v4 = 18;
fwrite("write super fail.\n", 1uLL, 0x12uLL, stderr);
goto LABEL_7;
}
return v4;
}

original_code:
int main(int argc, char *argv[])
{
 struct epii_super_block super;
 char *file;
 int ret;
 int fd;
 if (argc != 2) {
  fprintf(stderr, "give a partition\n");
  ret = -1;
  goto error;
 }
 file = argv[argc - 1];
 fprintf(stdout, "Format %s , check: \n", file);
 ret = check_mounted(file);
 if (ret)
  goto error;
 fprintf(stdout, "Format %s , begin format: \n", file);
 fd = open(file, 02);
 if (fd < 0) {
  fprintf(stderr, "unable to open %s\n", file);
  exit(1);
 }
 fprintf(stdout, "Write Super Block \n");
 memset(&super, 0, sizeof(super));
 strncpy(super.s_magic, "e^(PIi)+1=0", sizeof("e^(PIi)+1=0"));
 super.s_uuid = 0x1234567012345;
 super.s_blocksize = 4096;
 super.s_root = 1 << 20;
 fprintf(stdout, "magic %s %lu\n", super.s_magic, sizeof(super));
 ret = pwrite(fd, &super, sizeof(super), (64 * 1024));
 if (ret != sizeof(super)) {
  fprintf(stderr, "write super fail.\n");
  ret = 0x12;
  goto error;
 }
 return 0;
error:
 fprintf(stderr, "Fomat err %d\n", ret);
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct epii_super_block { unsigned long long s_uuid; unsigned short s_blocksize; char s_magic[16]; unsigned long long s_root; }

variable_types:
file
char *

variable_types: fd int


decompiled_code:
__int64 __fastcall encoder_encode_headers(__int64 a1)
{
_DWORD *v1; // rdx
__int64 result; // rax
char v3[4]; // [rsp+4h] [rbp-14h] BYREF
_DWORD *v4; // [rsp+8h] [rbp-10h] BYREF

if ( (int)x264_encoder_headers(encoder, &v4, v3) < 0 )
{
fwrite("x264_encoder_headers failed\n", 1uLL, 0x1CuLL, stderr);
result = 0LL;
}
else
{
v1 = v4;
*(_DWORD *)(a1 + 8) = v4[25] + v4[5] + v4[15];
*(_QWORD *)a1 = *((_QWORD *)v1 + 3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
_DWORD *v1; // rdx
__int64 result; // rax
char v3[4]; // [rsp+4h] [rbp-14h] BYREF
_DWORD *v4; // [rsp+8h] [rbp-10h] BYREF

if ( (int)x264_encoder_headers(encoder, &v4, v3) < 0 )
{
fwrite("x264_encoder_headers failed\n", 1uLL, 0x1CuLL, stderr);
result = 0LL;
}
else
{
v1 = v4;
*(_DWORD *)(a1 + 8) = v4[25] + v4[5] + v4[15];
*(_QWORD *)a1 = *((_QWORD *)v1 + 3);
result = 1LL;
}
return result;
}

original_code:
int encoder_encode_headers(struct encoded_pic_t *headers_out)
{
 x264_nal_t *headers;
 int i_nal;
 if(x264_encoder_headers( encoder, &headers, &i_nal ) < 0){
  fprintf(stderr,"x264_encoder_headers failed\n");
  return 0;
 }
 headers_out->length = headers[0].i_payload + headers[1].i_payload + headers[2].i_payload;
 headers_out->buffer = headers[0].p_payload;
 return 1;
}

canonical_original_code:
int encoder_encode_headers(struct encoded_pic_t *headers_out)
{
 struct x264_nal_t *headers;
 int i_nal;
 if(x264_encoder_headers( encoder, &headers, &i_nal ) < 0){
  fprintf(stderr,"x264_encoder_headers failed\n");
  return 0;
 }
 headers_out->length = headers[0].i_payload + headers[1].i_payload + headers[2].i_payload;
 headers_out->buffer = headers[0].p_payload;
 return 1;
}

variable_types: struct encoded_pic_t { unsigned char *buffer; int length; unsigned long long timepoint; enum H264_FRAME_TYPE { FRAME_TYPE_I=0, FRAME_TYPE_P=1, FRAME_TYPE_B=2 } frame_type; void *usr_def; } *

variable_types: struct x264_nal_t { int i_ref_idc; int i_type; int b_long_startcode; int i_first_mb; int i_last_mb; int i_payload; unsigned char *p_payload; int i_padding; } *

user_defined_types: struct encoded_pic_t { unsigned char *buffer; int length; unsigned long long timepoint; enum H264_FRAME_TYPE frame_type; void *usr_def; }


decompiled_code:
__int64 __fastcall encoder_encode_frame(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // edx
int v4; // ecx
int v5; // ecx
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rax
__int64 result; // rax
char v10[4]; // [rsp+4h] [rbp-14h] BYREF
__int64 v11; // [rsp+8h] [rbp-10h] BYREF

if ( start_timestamp == -1 )
{
start_timestamp = *(_QWORD *)(a1 + 16) + 1000000LL * *(_QWORD *)(a1 + 8);
v2 = 0LL;
}
else
{
v2 = *(_QWORD *)(a1 + 16) + 1000000LL * *(_QWORD *)(a1 + 8) - start_timestamp;
}
v3 = *(_DWORD *)(a1 + 24);
v4 = *(_DWORD *)(a1 + 28);
qword_450 = v2;
v5 = v3 * v4;
qword_480 = *(_QWORD *)a1;
qword_488 = v5 + qword_480;
qword_490 = v5 / 4 + v5 + qword_480;
v6 = x264_encoder_encode(encoder, &v11, v10, &pic_in, &pic_out);
if ( v6 < 0 )
{
fwrite("x264_encoder_encode failed\n", 1uLL, 0x1BuLL, stderr);
result = 0LL;
}
else if ( v6 )
{
v7 = *(_QWORD *)(v11 + 24);
*(_DWORD *)(a2 + 8) = v6;
v8 = qword_350;
*(_QWORD *)a2 = v7;
*(_QWORD *)(a2 + 16) = v8;
if ( pic_out == 3 )
{
applog(", frame_type=P");
*(_DWORD *)(a2 + 24) = 1;
}
else if ( (unsigned int)(pic_out - 4) <= 1 )
{
applog(", frame_type=B");
*(_DWORD *)(a2 + 24) = 2;
}
else
{
applog(", frame_type=I");
*(_DWORD *)(a2 + 24) = 0;
}
applog(", timepoint=%lld", *(_QWORD *)(a2 + 16));
result = 1LL;
}
else
{
*(_DWORD *)(a2 + 8) = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // edx
int v4; // ecx
int v5; // ecx
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rax
__int64 result; // rax
char v10[4]; // [rsp+4h] [rbp-14h] BYREF
__int64 v11; // [rsp+8h] [rbp-10h] BYREF

if ( start_timestamp == -1 )
{
start_timestamp = *(_QWORD *)(a1 + 16) + 1000000LL * *(_QWORD *)(a1 + 8);
v2 = 0LL;
}
else
{
v2 = *(_QWORD *)(a1 + 16) + 1000000LL * *(_QWORD *)(a1 + 8) - start_timestamp;
}
v3 = *(_DWORD *)(a1 + 24);
v4 = *(_DWORD *)(a1 + 28);
qword_450 = v2;
v5 = v3 * v4;
qword_480 = *(_QWORD *)a1;
qword_488 = v5 + qword_480;
qword_490 = v5 / 4 + v5 + qword_480;
v6 = x264_encoder_encode(encoder, &v11, v10, &pic_in, &pic_out);
if ( v6 < 0 )
{
fwrite("x264_encoder_encode failed\n", 1uLL, 0x1BuLL, stderr);
result = 0LL;
}
else if ( v6 )
{
v7 = *(_QWORD *)(v11 + 24);
*(_DWORD *)(a2 + 8) = v6;
v8 = qword_350;
*(_QWORD *)a2 = v7;
*(_QWORD *)(a2 + 16) = v8;
if ( pic_out == 3 )
{
applog(", frame_type=P");
*(_DWORD *)(a2 + 24) = 1;
}
else if ( (unsigned int)(pic_out - 4) <= 1 )
{
applog(", frame_type=B");
*(_DWORD *)(a2 + 24) = 2;
}
else
{
applog(", frame_type=I");
*(_DWORD *)(a2 + 24) = 0;
}
applog(", timepoint=%lld", *(_QWORD *)(a2 + 16));
result = 1LL;
}
else
{
*(_DWORD *)(a2 + 8) = 0;
result = 1LL;
}
return result;
}

original_code:
int encoder_encode_frame(struct picture_t *raw_pic, struct encoded_pic_t *output)
{
 x264_nal_t *nal;
 int i_nal;
 int i_frame_size = 0;
 int64_t timestamp;
 timestamp = raw_pic->timestamp.tv_sec*1000000ull+raw_pic->timestamp.tv_usec;
 if(start_timestamp == -1){
  start_timestamp = timestamp;
 }
 pic_in.i_pts = timestamp-start_timestamp;
 pic_in.img.plane[0] = raw_pic->buffer;
 pic_in.img.plane[1] = pic_in.img.plane[0] + raw_pic->width * raw_pic->height;
 pic_in.img.plane[2] = pic_in.img.plane[1] + raw_pic->width * raw_pic->height / 4;
 i_frame_size = x264_encoder_encode( encoder, &nal, &i_nal, &pic_in, &pic_out );
 if(i_frame_size < 0){
  fprintf(stderr,"x264_encoder_encode failed\n");
  return 0;
 }
 if( i_frame_size )
 {
  output->buffer = nal[0].p_payload;
  output->length = i_frame_size;
  output->timepoint = pic_out.i_pts;
  if(pic_out.i_type == 0x0003){
   applog(", frame_type=P");
   output->frame_type = FRAME_TYPE_P;
  }else if(((pic_out.i_type)==0x0005 || (pic_out.i_type)==0x0004)){
   applog(", frame_type=B");
   output->frame_type = FRAME_TYPE_B;
  }else{
   applog(", frame_type=I");
   output->frame_type = FRAME_TYPE_I;
  }
  applog(", timepoint=%lld", output->timepoint);
 }else{
  output->length = 0;
 }
 return 1;
}

canonical_original_code:
int encoder_encode_frame(struct picture_t *raw_pic, struct encoded_pic_t *output)
{
 struct x264_nal_t *nal;
 int i_nal;
 int i_frame_size = 0;
 long timestamp;
 timestamp = raw_pic->timestamp.tv_sec*1000000ull+raw_pic->timestamp.tv_usec;
 if(start_timestamp == -1){
  start_timestamp = timestamp;
 }
 pic_in.i_pts = timestamp-start_timestamp;
 pic_in.img.plane[0] = raw_pic->buffer;
 pic_in.img.plane[1] = pic_in.img.plane[0] + raw_pic->width * raw_pic->height;
 pic_in.img.plane[2] = pic_in.img.plane[1] + raw_pic->width * raw_pic->height / 4;
 i_frame_size = x264_encoder_encode( encoder, &nal, &i_nal, &pic_in, &pic_out );
 if(i_frame_size < 0){
  fprintf(stderr,"x264_encoder_encode failed\n");
  return 0;
 }
 if( i_frame_size )
 {
  output->buffer = nal[0].p_payload;
  output->length = i_frame_size;
  output->timepoint = pic_out.i_pts;
  if(pic_out.i_type == 0x0003){
   applog(", frame_type=P");
   output->frame_type = FRAME_TYPE_P;
  }else if(((pic_out.i_type)==0x0005 || (pic_out.i_type)==0x0004)){
   applog(", frame_type=B");
   output->frame_type = FRAME_TYPE_B;
  }else{
   applog(", frame_type=I");
   output->frame_type = FRAME_TYPE_I;
  }
  applog(", timepoint=%lld", output->timepoint);
 }else{
  output->length = 0;
 }
 return 1;
}

variable_types: struct picture_t { unsigned char *buffer; struct timeval { long tv_sec; long tv_usec; } timestamp; int width; } *

variable_types: timestamp long

user_defined_types: struct picture_t { unsigned char *buffer; struct timeval timestamp; int width; }

user_defined_types: struct x264_nal_t { int i_ref_idc; int i_type; int b_long_startcode; int i_first_mb; int i_last_mb; int i_payload; unsigned char *p_payload; int i_padding; }

user_defined_types: enum H264_FRAME_TYPE { FRAME_TYPE_I=0, FRAME_TYPE_P=1, FRAME_TYPE_B=2 }


decompiled_code:
void encoder_release()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void encoder_release(struct encoded_pic_t *output)
{
}


decompiled_code:
__int64 encoder_close()
{
return x264_encoder_close(encoder);
}

canonical_decompiled_code:
__int64 func4()
{
return x264_encoder_close(encoder);
}

original_code:
void encoder_close()
{
 x264_encoder_close(encoder);
}


decompiled_code:
void stop_recording()
{
recording = 0;
}

canonical_decompiled_code:
void func0()
{
recording = 0;
}

original_code:
void stop_recording(int param)
{
 recording = 0;
}

variable_types: param int


decompiled_code:
size_t gen_osd_info()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
return strftime(osd_string, 0x14uLL, "%Y-%m-%d %H:%M:%S", v0);
}

canonical_decompiled_code:
size_t func1()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
return strftime(osd_string, 0x14uLL, "%Y-%m-%d %H:%M:%S", v0);
}

original_code:
static void gen_osd_info()
{
 time_t t = time(0);
 strftime(osd_string, 20, "%Y-%m-%d %H:%M:%S",localtime(&t));
}

canonical_original_code:
static void gen_osd_info()
{
 long t = time(0);
 strftime(osd_string, 20, "%Y-%m-%d %H:%M:%S",localtime(&t));
}

variable_types: t long


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // ebp
_BYTE v5[32]; // [rsp+0h] [rbp-58h] BYREF
char v6[56]; // [rsp+20h] [rbp-38h] BYREF

*_errno_location() = 0;
if ( (unsigned int)camera_init(v5, argv) )
{
if ( (unsigned int)encoder_init(v5) )
{
if ( (unsigned int)preview_init(v5) )
{
if ( (unsigned int)output_init(v5)
&& (unsigned int)encoder_encode_headers(v6)
&& (unsigned int)output_write_headers(v6) )
{
encoder_release(v6);
if ( (unsigned int)camera_on() )
{
if ( signal(2, (__sighandler_t)stop_recording) == (__sighandler_t)-1LL )
{
fwrite("signal() failed\n", 1uLL, 0x10uLL, stderr);
}
else
{
v4 = 0;
puts("Press ctrl-c to stop recording...");
recording = 1;
do
{
if ( !(unsigned int)camera_get_frame(v5) )
break;
gen_osd_info();
osd_print(v5, osd_string);
if ( (v4 & 7) == 0 )
preview_display(v5);
if ( !(unsigned int)encoder_encode_frame(v5, v6) )
break;
applog_flush();
if ( !(unsigned int)output_write_frame(v6) )
break;
++v4;
encoder_release(v6);
}
while ( recording );
printf("\n%d frames recorded\n", v4);
}
camera_off();
}
output_close();
}
preview_close();
}
encoder_close();
}
else
{
fwrite("failed to initialize encoder\n", 1uLL, 0x1DuLL, stderr);
}
camera_close();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // ebp
_BYTE v5[32]; // [rsp+0h] [rbp-58h] BYREF
char v6[56]; // [rsp+20h] [rbp-38h] BYREF

*_errno_location() = 0;
if ( (unsigned int)camera_init(v5, argv) )
{
if ( (unsigned int)encoder_init(v5) )
{
if ( (unsigned int)preview_init(v5) )
{
if ( (unsigned int)output_init(v5)
&& (unsigned int)encoder_encode_headers(v6)
&& (unsigned int)output_write_headers(v6) )
{
encoder_release(v6);
if ( (unsigned int)camera_on() )
{
if ( signal(2, (__sighandler_t)stop_recording) == (__sighandler_t)-1LL )
{
fwrite("signal() failed\n", 1uLL, 0x10uLL, stderr);
}
else
{
v4 = 0;
puts("Press ctrl-c to stop recording...");
recording = 1;
do
{
if ( !(unsigned int)camera_get_frame(v5) )
break;
func1();
osd_print(v5, osd_string);
if ( (v4 & 7) == 0 )
preview_display(v5);
if ( !(unsigned int)encoder_encode_frame(v5, v6) )
break;
applog_flush();
if ( !(unsigned int)output_write_frame(v6) )
break;
++v4;
encoder_release(v6);
}
while ( recording );
printf("\n%d frames recorded\n", v4);
}
camera_off();
}
output_close();
}
preview_close();
}
encoder_close();
}
else
{
fwrite("failed to initialize encoder\n", 1uLL, 0x1DuLL, stderr);
}
camera_close();
}
return 0;
}

original_code:
int main()
{
 int i;
 struct picture_t pic;
 struct encoded_pic_t encoded_pic;
 (*__errno_location ()) = 0;
 if(!camera_init(&pic))
  goto error_cam;
 if(!encoder_init(&pic)){
  fprintf(stderr,"failed to initialize encoder\n");
  goto error_encoder;
 }
 if(!preview_init(&pic))
  goto error_preview;
 if(!output_init(&pic))
  goto error_output;
 if(!encoder_encode_headers(&encoded_pic))
  goto error_output;
 if(!output_write_headers(&encoded_pic))
  goto error_output;
 encoder_release(&encoded_pic);
 if(!camera_on())
  goto error_cam_on;
 if(signal(2, stop_recording) == ((__sighandler_t) -1)){
  fprintf(stderr,"signal() failed\n");
  goto error_signal;
 }
 printf("Press ctrl-c to stop recording...\n");
 recording = 1;
 for(i=0; recording; i++){
  if(!camera_get_frame(&pic))
   break;
  gen_osd_info();
  osd_print(&pic, osd_string);
  if((i&7)==0)
   preview_display(&pic);
  if(!encoder_encode_frame(&pic, &encoded_pic))
   break;
  applog_flush();
  if(!output_write_frame(&encoded_pic))
   break;
  encoder_release(&encoded_pic);
 }
 printf("\n%d frames recorded\n", i);
error_signal:
 camera_off();
error_cam_on:
 output_close();
error_output:
 preview_close();
error_preview:
 encoder_close();
error_encoder:
 camera_close();
error_cam:
 return 0;
}

variable_types: i int

variable_types: struct picture_t { unsigned char *buffer; struct timeval { long tv_sec; long tv_usec; } timestamp; int width; }

variable_types: struct encoded_pic_t { unsigned char *buffer; int length; unsigned long long timepoint; enum H264_FRAME_TYPE { FRAME_TYPE_I=0, FRAME_TYPE_P=1, FRAME_TYPE_B=2 } frame_type; void *usr_def; }


decompiled_code:
_BOOL8 __fastcall write_frame_data(void *src, signed int a2)
{
int v2; // er13
_DWORD *v3; // rbp

v2 = a2 + 4;
v3 = cache;
if ( a2 + 3 >= cache_size )
{
cache_size = a2 + 4;
cache = realloc(cache, v2);
v3 = cache;
if ( !cache )
return 0LL;
}
*v3 = _byteswap_ulong(a2);
memcpy(v3 + 1, src, a2);
return (int)mk_add_frame_data(writer, v3, (unsigned int)v2) >= 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(void *src, signed int a2)
{
int v2; // er13
_DWORD *v3; // rbp

v2 = a2 + 4;
v3 = cache;
if ( a2 + 3 >= cache_size )
{
cache_size = a2 + 4;
cache = realloc(cache, v2);
v3 = cache;
if ( !cache )
return 0LL;
}
*v3 = _byteswap_ulong(a2);
memcpy(v3 + 1, src, a2);
return (int)mk_add_frame_data(writer, v3, (unsigned int)v2) >= 0;
}

original_code:
static int write_frame_data(uint8_t *ptr, int size)
{
 if(size+4>cache_size){
  cache_size=size+4;
  cache = realloc(cache, cache_size);
  if(!cache)
   return 0;
 }
  *cache = (size>>24);
  *(cache+1) = (size>>16)&255;
  *(cache+2) = (size>>8)&255;
  *(cache+3) = (size)&255;
 memcpy(cache+4, ptr, size);
 if(mk_add_frame_data(writer, cache, size+4) < 0)
  return 0;
 return 1;
}

canonical_original_code:
static int write_frame_data(unsigned char *ptr, int size)
{
 if(size+4>cache_size){
  cache_size=size+4;
  cache = realloc(cache, cache_size);
  if(!cache)
   return 0;
 }
  *cache = (size>>24);
  *(cache+1) = (size>>16)&255;
  *(cache+2) = (size>>8)&255;
  *(cache+3) = (size)&255;
 memcpy(cache+4, ptr, size);
 if(mk_add_frame_data(writer, cache, size+4) < 0)
  return 0;
 return 1;
}

variable_types:
ptr
unsigned char *

variable_types: size int


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

writer = mk_create_writer("output.mkv");
if ( writer )
{
width = *(_DWORD *)(a1 + 24);
height = *(_DWORD *)(a1 + 28);
result = 1LL;
}
else
{
fwrite("mk_create_writer failed\n", 1uLL, 0x18uLL, stderr);
result = 0LL;
}
return result;
}

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

writer = mk_create_writer("output.mkv");
if ( writer )
{
width = *(_DWORD *)(a1 + 24);
height = *(_DWORD *)(a1 + 28);
result = 1LL;
}
else
{
fwrite("mk_create_writer failed\n", 1uLL, 0x18uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int output_init(struct picture_t *info)
{
 if((writer=mk_create_writer("output.mkv")) == 0){
  fprintf(stderr,"mk_create_writer failed\n");
  return 0;
 }
 width = info->width;
 height = info->height;
 return 1;
}

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