qstrfmt

decompiled_code:
__int64 __fastcall qstrfmt(unsigned __int8 *a1, unsigned __int8 *a2, int *a3, __int64 a4)
{
unsigned __int8 *v4; // r15
_BYTE *v6; // rbp
unsigned __int64 v7; // rdx
_DWORD *v8; // rax
unsigned int *v9; // rbx
unsigned int *v10; // rax
int v11; // er12
unsigned __int8 *v12; // r13
__int64 result; // rax
unsigned int v14; // eax
char *v15; // rcx
unsigned int *v16; // rsi
char v17; // al
unsigned int v18; // eax
unsigned int v19; // eax
unsigned int v20; // eax
unsigned int v21; // eax
unsigned __int64 v22; // [rsp+8h] [rbp-80h]
unsigned __int64 v23; // [rsp+10h] [rbp-78h]
unsigned __int8 *v24; // [rsp+18h] [rbp-70h]
unsigned __int64 v25; // [rsp+20h] [rbp-68h]
__int64 v26; // [rsp+28h] [rbp-60h]
int v28; // [rsp+3Ch] [rbp-4Ch]
unsigned int v29; // [rsp+48h] [rbp-40h] BYREF
unsigned int v30[15]; // [rsp+4Ch] [rbp-3Ch] BYREF

v4 = a2;
v24 = &a1[a3[2]];
v25 = (unsigned __int64)&a2[4 * a3[1]];
v26 = *(_QWORD *)(a4 + 96);
v28 = 0;
if ( (v26 & 1) != 0 )
v28 = *(_DWORD *)(a4 + 84);
if ( *(_BYTE *)a4 )
{
if ( (v26 & 2) != 0 )
{
v6 = *(_BYTE **)(a4 + 16);
v22 = *(_QWORD *)(a4 + 24);
LABEL_6:
v7 = (unsigned __int64)(v6 + 4);
v23 = v22;
v8 = v6;
if ( v22 < (unsigned __int64)(v6 + 4) )
{
v8 = (_DWORD *)_fmtflush(a4, v6, 4LL);
if ( !v8 )
return 0xFFFFFFFFLL;
v7 = *(_QWORD *)(a4 + 24);
v23 = v7;
}
v9 = v8 + 1;
*v8 = 39;
goto LABEL_15;
}
if ( (int)_rfmtpad(a4, (unsigned int)(v28 - a3[3])) < 0 )
return 0xFFFFFFFFLL;
}
else
{
if ( (v26 & 2) != 0 )
{
v9 = *(unsigned int **)(a4 + 16);
v23 = *(_QWORD *)(a4 + 24);
goto LABEL_13;
}
if ( (int)_fmtpad(a4, (unsigned int)(v28 - a3[4])) < 0 )
return 0xFFFFFFFFLL;
}
v6 = *(_BYTE **)(a4 + 16);
v22 = *(_QWORD *)(a4 + 24);
if ( *(_BYTE *)a4 )
goto LABEL_6;
v23 = *(_QWORD *)(a4 + 24);
v9 = *(unsigned int **)(a4 + 16);
LABEL_13:
v7 = (unsigned __int64)(v9 + 1);
v22 = v23;
v10 = v9;
if ( v23 < (unsigned __int64)(v9 + 1) )
{
v14 = runelen(39LL);
v7 = v14;
v15 = (char *)v9 + (int)v14;
v22 = v23;
v10 = v9;
if ( v23 < (unsigned __int64)v15 )
{
v10 = (unsigned int *)_fmtflush(a4, v9, v7);
if ( !v10 )
return 0xFFFFFFFFLL;
v7 = *(_QWORD *)(a4 + 24);
v22 = v7;
}
}
v6 = (char *)v10 + 1;
*(_BYTE *)v10 = 39;
LABEL_15:
v11 = a3[1];
if ( v11 > 0 )
{
v12 = a1;
do
{
if ( a1 )
{
v30[0] = *v12;
if ( v30[0] > 0x7F )
{
if ( v24 - v12 <= 3 && !(unsigned int)fullrune(v12) )
break;
v12 += (int)chartorune(v30, v12);
}
else
{
++v12;
}
}
else
{
if ( v25 <= (unsigned __int64)v4 )
break;
v30[0] = *v4;
v4 += 4;
}
if ( *(_BYTE *)a4 )
{
if ( v23 < (unsigned __int64)(v9 + 1) )
{
v9 = (unsigned int *)_fmtflush(a4, v9, 4LL);
if ( !v9 )
return 0xFFFFFFFFLL;
v23 = *(_QWORD *)(a4 + 24);
}
v16 = v9 + 1;
v18 = v30[0];
*v9 = v30[0];
if ( v18 == 39 )
{
if ( v23 < (unsigned __int64)(v9 + 2) )
{
v16 = (unsigned int *)_fmtflush(a4, v16, 4LL);
if ( !v16 )
return 0xFFFFFFFFLL;
v23 = *(_QWORD *)(a4 + 24);
}
v9 = v16 + 1;
*v16 = v30[0];
}
else
{
++v9;
}
}
else
{
if ( v22 < (unsigned __int64)(v6 + 4) )
{
v19 = runelen(v30[0]);
v7 = v19;
if ( v22 < (unsigned __int64)&v6[v19] )
{
v6 = (_BYTE *)_fmtflush(a4, v6, v19);
if ( !v6 )
return 0xFFFFFFFFLL;
v22 = *(_QWORD *)(a4 + 24);
}
}
if ( v30[0] > 0x7F )
{
v29 = v30[0];
v6 += (int)runetochar(v6, &v29, v7);
}
else
{
*v6++ = v30[0];
}
v17 = v30[0];
if ( v30[0] == 39 )
{
v7 = (unsigned __int64)(v6 + 4);
if ( v22 >= (unsigned __int64)(v6 + 4) )
goto LABEL_53;
v20 = runelen(39LL);
v7 = v20;
if ( v22 < (unsigned __int64)&v6[v20] )
{
v6 = (_BYTE *)_fmtflush(a4, v6, v20);
if ( !v6 )
return 0xFFFFFFFFLL;
v22 = *(_QWORD *)(a4 + 24);
}
v17 = v30[0];
if ( v30[0] <= 0x7F )
{
LABEL_53:
*v6++ = v17;
}
else
{
v29 = v30[0];
v6 += (int)runetochar(v6, &v29, v7);
}
}
}
--v11;
}
while ( v11 );
}
if ( *(_BYTE *)a4 )
{
if ( v23 >= (unsigned __int64)(v9 + 1) || (v9 = (unsigned int *)_fmtflush(a4, v9, 4LL)) != 0LL )
{
*v9 = 39;
*(_DWORD *)(a4 + 48) += ((__int64)v9 - *(_QWORD *)(a4 + 16) + 4) >> 2;
*(_QWORD *)(a4 + 16) = v9 + 1;
result = 0LL;
if ( (v26 & 2) != 0 )
result = (unsigned int)((int)_rfmtpad(a4, (unsigned int)(v28 - a3[3])) >> 31);
}
else
{
result = 0xFFFFFFFFLL;
}
}
else if ( v22 >= (unsigned __int64)(v6 + 4)
|| (v21 = runelen(39LL), v22 >= (unsigned __int64)&v6[v21])
|| (v6 = (_BYTE *)_fmtflush(a4, v6, v21)) != 0LL )
{
*v6 = 39;
*(_DWORD *)(a4 + 48) += (_DWORD)v6 + 1 - *(_DWORD *)(a4 + 16);
*(_QWORD *)(a4 + 16) = v6 + 1;
result = 0LL;
if ( (v26 & 2) != 0 )
result = (unsigned int)((int)_fmtpad(a4, (unsigned int)(v28 - a3[4])) >> 31);
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, unsigned __int8 *a2, int *a3, __int64 a4)
{
unsigned __int8 *v4; // r15
_BYTE *v6; // rbp
unsigned __int64 v7; // rdx
_DWORD *v8; // rax
unsigned int *v9; // rbx
unsigned int *v10; // rax
int v11; // er12
unsigned __int8 *v12; // r13
__int64 result; // rax
unsigned int v14; // eax
char *v15; // rcx
unsigned int *v16; // rsi
char v17; // al
unsigned int v18; // eax
unsigned int v19; // eax
unsigned int v20; // eax
unsigned int v21; // eax
unsigned __int64 v22; // [rsp+8h] [rbp-80h]
unsigned __int64 v23; // [rsp+10h] [rbp-78h]
unsigned __int8 *v24; // [rsp+18h] [rbp-70h]
unsigned __int64 v25; // [rsp+20h] [rbp-68h]
__int64 v26; // [rsp+28h] [rbp-60h]
int v28; // [rsp+3Ch] [rbp-4Ch]
unsigned int v29; // [rsp+48h] [rbp-40h] BYREF
unsigned int v30[15]; // [rsp+4Ch] [rbp-3Ch] BYREF

v4 = a2;
v24 = &a1[a3[2]];
v25 = (unsigned __int64)&a2[4 * a3[1]];
v26 = *(_QWORD *)(a4 + 96);
v28 = 0;
if ( (v26 & 1) != 0 )
v28 = *(_DWORD *)(a4 + 84);
if ( *(_BYTE *)a4 )
{
if ( (v26 & 2) != 0 )
{
v6 = *(_BYTE **)(a4 + 16);
v22 = *(_QWORD *)(a4 + 24);
LABEL_6:
v7 = (unsigned __int64)(v6 + 4);
v23 = v22;
v8 = v6;
if ( v22 < (unsigned __int64)(v6 + 4) )
{
v8 = (_DWORD *)_fmtflush(a4, v6, 4LL);
if ( !v8 )
return 0xFFFFFFFFLL;
v7 = *(_QWORD *)(a4 + 24);
v23 = v7;
}
v9 = v8 + 1;
*v8 = 39;
goto LABEL_15;
}
if ( (int)_rfmtpad(a4, (unsigned int)(v28 - a3[3])) < 0 )
return 0xFFFFFFFFLL;
}
else
{
if ( (v26 & 2) != 0 )
{
v9 = *(unsigned int **)(a4 + 16);
v23 = *(_QWORD *)(a4 + 24);
goto LABEL_13;
}
if ( (int)_fmtpad(a4, (unsigned int)(v28 - a3[4])) < 0 )
return 0xFFFFFFFFLL;
}
v6 = *(_BYTE **)(a4 + 16);
v22 = *(_QWORD *)(a4 + 24);
if ( *(_BYTE *)a4 )
goto LABEL_6;
v23 = *(_QWORD *)(a4 + 24);
v9 = *(unsigned int **)(a4 + 16);
LABEL_13:
v7 = (unsigned __int64)(v9 + 1);
v22 = v23;
v10 = v9;
if ( v23 < (unsigned __int64)(v9 + 1) )
{
v14 = runelen(39LL);
v7 = v14;
v15 = (char *)v9 + (int)v14;
v22 = v23;
v10 = v9;
if ( v23 < (unsigned __int64)v15 )
{
v10 = (unsigned int *)_fmtflush(a4, v9, v7);
if ( !v10 )
return 0xFFFFFFFFLL;
v7 = *(_QWORD *)(a4 + 24);
v22 = v7;
}
}
v6 = (char *)v10 + 1;
*(_BYTE *)v10 = 39;
LABEL_15:
v11 = a3[1];
if ( v11 > 0 )
{
v12 = a1;
do
{
if ( a1 )
{
v30[0] = *v12;
if ( v30[0] > 0x7F )
{
if ( v24 - v12 <= 3 && !(unsigned int)fullrune(v12) )
break;
v12 += (int)chartorune(v30, v12);
}
else
{
++v12;
}
}
else
{
if ( v25 <= (unsigned __int64)v4 )
break;
v30[0] = *v4;
v4 += 4;
}
if ( *(_BYTE *)a4 )
{
if ( v23 < (unsigned __int64)(v9 + 1) )
{
v9 = (unsigned int *)_fmtflush(a4, v9, 4LL);
if ( !v9 )
return 0xFFFFFFFFLL;
v23 = *(_QWORD *)(a4 + 24);
}
v16 = v9 + 1;
v18 = v30[0];
*v9 = v30[0];
if ( v18 == 39 )
{
if ( v23 < (unsigned __int64)(v9 + 2) )
{
v16 = (unsigned int *)_fmtflush(a4, v16, 4LL);
if ( !v16 )
return 0xFFFFFFFFLL;
v23 = *(_QWORD *)(a4 + 24);
}
v9 = v16 + 1;
*v16 = v30[0];
}
else
{
++v9;
}
}
else
{
if ( v22 < (unsigned __int64)(v6 + 4) )
{
v19 = runelen(v30[0]);
v7 = v19;
if ( v22 < (unsigned __int64)&v6[v19] )
{
v6 = (_BYTE *)_fmtflush(a4, v6, v19);
if ( !v6 )
return 0xFFFFFFFFLL;
v22 = *(_QWORD *)(a4 + 24);
}
}
if ( v30[0] > 0x7F )
{
v29 = v30[0];
v6 += (int)runetochar(v6, &v29, v7);
}
else
{
*v6++ = v30[0];
}
v17 = v30[0];
if ( v30[0] == 39 )
{
v7 = (unsigned __int64)(v6 + 4);
if ( v22 >= (unsigned __int64)(v6 + 4) )
goto LABEL_53;
v20 = runelen(39LL);
v7 = v20;
if ( v22 < (unsigned __int64)&v6[v20] )
{
v6 = (_BYTE *)_fmtflush(a4, v6, v20);
if ( !v6 )
return 0xFFFFFFFFLL;
v22 = *(_QWORD *)(a4 + 24);
}
v17 = v30[0];
if ( v30[0] <= 0x7F )
{
LABEL_53:
*v6++ = v17;
}
else
{
v29 = v30[0];
v6 += (int)runetochar(v6, &v29, v7);
}
}
}
--v11;
}
while ( v11 );
}
if ( *(_BYTE *)a4 )
{
if ( v23 >= (unsigned __int64)(v9 + 1) || (v9 = (unsigned int *)_fmtflush(a4, v9, 4LL)) != 0LL )
{
*v9 = 39;
*(_DWORD *)(a4 + 48) += ((__int64)v9 - *(_QWORD *)(a4 + 16) + 4) >> 2;
*(_QWORD *)(a4 + 16) = v9 + 1;
result = 0LL;
if ( (v26 & 2) != 0 )
result = (unsigned int)((int)_rfmtpad(a4, (unsigned int)(v28 - a3[3])) >> 31);
}
else
{
result = 0xFFFFFFFFLL;
}
}
else if ( v22 >= (unsigned __int64)(v6 + 4)
|| (v21 = runelen(39LL), v22 >= (unsigned __int64)&v6[v21])
|| (v6 = (_BYTE *)_fmtflush(a4, v6, v21)) != 0LL )
{
*v6 = 39;
*(_DWORD *)(a4 + 48) += (_DWORD)v6 + 1 - *(_DWORD *)(a4 + 16);
*(_QWORD *)(a4 + 16) = v6 + 1;
result = 0LL;
if ( (v26 & 2) != 0 )
result = (unsigned int)((int)_fmtpad(a4, (unsigned int)(v28 - a3[4])) >> 31);
}
else
{
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f)
{
 Rune r, *rm, *rme;
 char *t, *s, *m, *me;
 Rune *rt, *rs;
 _fmtulong fl;
 int nc, w;
 m = sin;
 me = m + q->nbytesin;
 rm = rin;
 rme = rm + q->nrunesin;
 fl = f->flags;
 w = 0;
 if(fl & FmtWidth)
  w = f->width;
 if(f->runes){
  if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0)
   return -1;
 }else{
  if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
   return -1;
 }
 t = (char*)f->to;
 s = (char*)f->stop;
 rt = (Rune*)f->to;
 rs = (Rune*)f->stop;
 if(f->runes)
  do{ if(rt + 1 > (Rune*)rs){ rt = (Rune*)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (Rune*)f->stop; else return -1; } *rt++ = '\''; }while(0);
 else
  do{ Rune _rune; int _runelen; if(t + UTFmax > (char*)s && t + (_runelen = runelen('\'')) > (char*)s){ t = (char*)__fmtflush(f, t, _runelen); if(t != 0) s = (char*)f->stop; else return -1; } if('\'' < Runeself) *t++ = '\''; else{ _rune = '\''; t += runetochar(t, &_rune); } }while(0);
 for(nc = q->nrunesin; nc > 0; nc--){
  if(sin){
   r = *(_fmtuchar*)m;
   if(r < Runeself)
    m++;
   else if((me - m) >= UTFmax || fullrune(m, me-m))
    m += chartorune(&r, m);
   else
    break;
  }else{
   if(rm >= rme)
    break;
   r = *(_fmtuchar*)rm++;
  }
  if(f->runes){
   do{ if(rt + 1 > (Rune*)rs){ rt = (Rune*)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (Rune*)f->stop; else return -1; } *rt++ = r; }while(0);
   if(r == '\'')
    do{ if(rt + 1 > (Rune*)rs){ rt = (Rune*)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (Rune*)f->stop; else return -1; } *rt++ = r; }while(0);
  }else{
   do{ Rune _rune; int _runelen; if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){ t = (char*)__fmtflush(f, t, _runelen); if(t != 0) s = (char*)f->stop; else return -1; } if(r < Runeself) *t++ = r; else{ _rune = r; t += runetochar(t, &_rune); } }while(0);
   if(r == '\'')
    do{ Rune _rune; int _runelen; if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){ t = (char*)__fmtflush(f, t, _runelen); if(t != 0) s = (char*)f->stop; else return -1; } if(r < Runeself) *t++ = r; else{ _rune = r; t += runetochar(t, &_rune); } }while(0);
  }
 }
 if(f->runes){
  do{ if(rt + 1 > (Rune*)rs){ rt = (Rune*)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (Rune*)f->stop; else return -1; } *rt++ = '\''; }while(0);
  if(rs);else;
  f->nfmt += rt - (Rune *)f->to;
  f->to = rt;
  if(fl & FmtLeft && __rfmtpad(f, w - q->nrunesout) < 0)
   return -1;
 }else{
  do{ Rune _rune; int _runelen; if(t + UTFmax > (char*)s && t + (_runelen = runelen('\'')) > (char*)s){ t = (char*)__fmtflush(f, t, _runelen); if(t != 0) s = (char*)f->stop; else return -1; } if('\'' < Runeself) *t++ = '\''; else{ _rune = '\''; t += runetochar(t, &_rune); } }while(0);
  if(s);else;
  f->nfmt += t - (char *)f->to;
  f->to = t;
  if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
   return -1;
 }
 return 0;
}

canonical_original_code:
static int qstrfmt(char *sin, unsigned int *rin, struct Quoteinfo *q, struct Fmt *f)
{
 unsigned int r; unsigned int *rm; unsigned int *rme;
 char *t; char *s; char *m; char *me;
 unsigned int *rt; unsigned int *rs;
 unsigned long fl;
 int nc; int w;
 m = sin;
 me = m + q->nbytesin;
 rm = rin;
 rme = rm + q->nrunesin;
 fl = f->flags;
 w = 0;
 if(fl & FmtWidth)
  w = f->width;
 if(f->runes){
  if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0)
   return -1;
 }else{
  if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
   return -1;
 }
 t = (char *)f->to;
 s = (char *)f->stop;
 rt = (unsigned int *)f->to;
 rs = (unsigned int *)f->stop;
 if(f->runes)
  do{ if(rt + 1 > (unsigned int *)rs){ rt = (unsigned int *)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (unsigned int *)f->stop; else return -1; } *rt++ = '\''; }while(0);
 else
  do{ unsigned int _rune; int _runelen; if(t + UTFmax > (char *)s && t + (_runelen = runelen('\'')) > (char *)s){ t = (char *)__fmtflush(f, t, _runelen); if(t != 0) s = (char *)f->stop; else return -1; } if('\'' < Runeself) *t++ = '\''; else{ _rune = '\''; t += runetochar(t, &_rune); } }while(0);
 for(nc = q->nrunesin; nc > 0; nc--){
  if(sin){
   r = *(unsigned char *)m;
   if(r < Runeself)
    m++;
   else if((me - m) >= UTFmax || fullrune(m, me-m))
    m += chartorune(&r, m);
   else
    break;
  }else{
   if(rm >= rme)
    break;
   r = *(unsigned char *)rm++;
  }
  if(f->runes){
   do{ if(rt + 1 > (unsigned int *)rs){ rt = (unsigned int *)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (unsigned int *)f->stop; else return -1; } *rt++ = r; }while(0);
   if(r == '\'')
    do{ if(rt + 1 > (unsigned int *)rs){ rt = (unsigned int *)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (unsigned int *)f->stop; else return -1; } *rt++ = r; }while(0);
  }else{
   do{ unsigned int _rune; int _runelen; if(t + UTFmax > (char *)s && t + (_runelen = runelen(r)) > (char *)s){ t = (char *)__fmtflush(f, t, _runelen); if(t != 0) s = (char *)f->stop; else return -1; } if(r < Runeself) *t++ = r; else{ _rune = r; t += runetochar(t, &_rune); } }while(0);
   if(r == '\'')
    do{ unsigned int _rune; int _runelen; if(t + UTFmax > (char *)s && t + (_runelen = runelen(r)) > (char *)s){ t = (char *)__fmtflush(f, t, _runelen); if(t != 0) s = (char *)f->stop; else return -1; } if(r < Runeself) *t++ = r; else{ _rune = r; t += runetochar(t, &_rune); } }while(0);
  }
 }
 if(f->runes){
  do{ if(rt + 1 > (unsigned int *)rs){ rt = (unsigned int *)__fmtflush(f, rt, sizeof(Rune)); if(rt != 0) rs = (unsigned int *)f->stop; else return -1; } *rt++ = '\''; }while(0);
  if(rs);else;
  f->nfmt += rt - (unsigned int *)f->to;
  f->to = rt;
  if(fl & FmtLeft && __rfmtpad(f, w - q->nrunesout) < 0)
   return -1;
 }else{
  do{ unsigned int _rune; int _runelen; if(t + UTFmax > (char *)s && t + (_runelen = runelen('\'')) > (char *)s){ t = (char *)__fmtflush(f, t, _runelen); if(t != 0) s = (char *)f->stop; else return -1; } if('\'' < Runeself) *t++ = '\''; else{ _rune = '\''; t += runetochar(t, &_rune); } }while(0);
  if(s);else;
  f->nfmt += t - (char *)f->to;
  f->to = t;
  if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
   return -1;
 }
 return 0;
}

variable_types:
sin
char *

variable_types:
rin
unsigned int *

variable_types: struct Quoteinfo { int quoted; int nrunesin; int nbytesin; int nrunesout; int nbytesout; } *

variable_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; } *

variable_types:
r
unsigned int

variable_types:
rm
unsigned int *

variable_types:
rme
unsigned int *

variable_types:
t
char *

variable_types:
s
char *

variable_types:
m
char *

variable_types:
me
char *

variable_types:
rt
unsigned int *

variable_types:
rs
unsigned int *

variable_types:
fl
unsigned long

variable_types: nc int

variable_types: w int


quotestrfmt

decompiled_code:
__int64 __fastcall quotestrfmt(unsigned __int8 *a1)
{
return _quotestrfmt(0, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1)
{
return func2(0, a1);
}

original_code:
int
quotestrfmt(Fmt *f)
{
 return __quotestrfmt(0, f);
}

canonical_original_code:
int quotestrfmt(struct Fmt *f)
{
 return __quotestrfmt(0, f);
}

return_type: int

user_defined_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; }


quoterunestrfmt

decompiled_code:
__int64 __fastcall quoterunestrfmt(unsigned __int8 *a1)
{
return _quotestrfmt(1, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 *a1)
{
return func2(1, a1);
}

original_code:
int
quoterunestrfmt(Fmt *f)
{
 return __quotestrfmt(1, f);
}

canonical_original_code:
int quoterunestrfmt(struct Fmt *f)
{
 return __quotestrfmt(1, f);
}


quotefmtinstall

decompiled_code:
__int64 quotefmtinstall()
{
fmtinstall(113LL, quotestrfmt);
return fmtinstall(81LL, quoterunestrfmt);
}

canonical_decompiled_code:
__int64 func5()
{
fmtinstall(113LL, quotestrfmt);
return fmtinstall(81LL, quoterunestrfmt);
}

original_code:
void
quotefmtinstall(void)
{
 fmtinstall('q', quotestrfmt);
 fmtinstall('Q', quoterunestrfmt);
}

canonical_original_code:
void quotefmtinstall()
{
 fmtinstall('q', quotestrfmt);
 fmtinstall('Q', quoterunestrfmt);
}

return_type: void


utfecpy

decompiled_code:
char *__fastcall utfecpy(char *a1, unsigned __int64 a2, const void *a3)
{
char *v3; // rbp
char *v5; // rax
char *v6; // rsi

v3 = a1;
if ( (unsigned __int64)a1 < a2 )
{
v5 = (char *)memccpy(a1, a3, 0, a2 - (_QWORD)a1);
if ( v5 )
{
v3 = v5 - 1;
}
else
{
v6 = (char *)(a2 - 1);
while ( v6 != a1 )
{
if ( (*--v6 & 0xC0) != 128 )
{
v3 = v6;
break;
}
}
*v3 = 0;
}
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func0(char *a1, unsigned __int64 a2, const void *a3)
{
char *v3; // rbp
char *v5; // rax
char *v6; // rsi

v3 = a1;
if ( (unsigned __int64)a1 < a2 )
{
v5 = (char *)memccpy(a1, a3, 0, a2 - (_QWORD)a1);
if ( v5 )
{
v3 = v5 - 1;
}
else
{
v6 = (char *)(a2 - 1);
while ( v6 != a1 )
{
if ( (*--v6 & 0xC0) != 128 )
{
v3 = v6;
break;
}
}
*v3 = 0;
}
}
return v3;
}

original_code:
char*
utfecpy(char *to, char *e, char *from)
{
 char *end;
 if(to >= e)
  return to;
 end = memccpy(to, from, '\0', e - to);
 if(end == 0){
  end = e-1;
  while(end>to && (*--end&0xC0)==0x80)
   ;
  *end = '\0';
 }else{
  end--;
 }
 return end;
}

canonical_original_code:
char *utfecpy(char *to, char *e, char *from)
{
 char *end;
 if(to >= e)
  return to;
 end = memccpy(to, from, '\0', e - to);
 if(end == 0){
  end = e-1;
  while(end>to && (*--end&0xC0)==0x80)
   ;
  *end = '\0';
 }else{
  end--;
 }
 return end;
}

variable_types:
to
char *

variable_types:
e
char *

variable_types:
from
char *

variable_types:
end
char *

return_type: char *


runestrstr

decompiled_code:
__int64 __fastcall runestrstr(__int64 i, unsigned int *a2)
{
unsigned int v2; // ebp
unsigned int v4; // edx
__int64 v5; // rax

v2 = *a2;
if ( *a2 )
{
for ( i = runestrchr(i, v2); i; i = runestrchr(i + 4, v2) )
{
v4 = a2[1];
if ( !v4 )
break;
v5 = 0LL;
while ( *(_DWORD *)(i + v5 * 4 + 4) == v4 )
{
v4 = a2[++v5 + 1];
if ( !v4 )
return i;
}
}
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 i, unsigned int *a2)
{
unsigned int v2; // ebp
unsigned int v4; // edx
__int64 v5; // rax

v2 = *a2;
if ( *a2 )
{
for ( i = runestrchr(i, v2); i; i = runestrchr(i + 4, v2) )
{
v4 = a2[1];
if ( !v4 )
break;
v5 = 0LL;
while ( *(_DWORD *)(i + v5 * 4 + 4) == v4 )
{
v4 = a2[++v5 + 1];
if ( !v4 )
return i;
}
}
}
return i;
}

original_code:
Rune*
runestrstr(Rune *s1, Rune *s2)
{
 Rune *p, *pa, *pb;
 int c0, c;
 c0 = *s2;
 if(c0 == 0)
  return s1;
 s2++;
 for(p=runestrchr(s1, c0); p; p=runestrchr(p+1, c0)) {
  pa = p;
  for(pb=s2;; pb++) {
   c = *pb;
   if(c == 0)
    return p;
   if(c != *++pa)
    break;
  }
 }
 return 0;
}

canonical_original_code:
unsigned int *runestrstr(unsigned int *s1, unsigned int *s2)
{
 unsigned int *p; unsigned int *pa; unsigned int *pb;
 int c0; int c;
 c0 = *s2;
 if(c0 == 0)
  return s1;
 s2++;
 for(p=runestrchr(s1, c0); p; p=runestrchr(p+1, c0)) {
  pa = p;
  for(pb=s2;; pb++) {
   c = *pb;
   if(c == 0)
    return p;
   if(c != *++pa)
    break;
  }
 }
 return 0;
}

variable_types:
p
unsigned int *

variable_types:
pa
unsigned int *

variable_types:
pb
unsigned int *

variable_types: c int

return_type: unsigned int *


getsyserr

decompiled_code:
void *getsyserr()
{
void *result; // rax

result = &xsyserr;
if ( syserrstr )
{
result = (void *)syserrstr();
if ( !result )
result = &xsyserr;
}
return result;
}

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

result = &xsyserr;
if ( syserrstr )
{
result = (void *)syserrstr();
if ( !result )
result = &xsyserr;
}
return result;
}

original_code:
static char*
getsyserr(void)
{
 char *s;
 s = ((void*)0);
 if(_syserrstr)
  s = (*_syserrstr)();
 if(s == ((void*)0))
  s = xsyserr;
 return s;
}

canonical_original_code:
static char *getsyserr()
{
 char *s;
 s = ((void *)0);
 if(_syserrstr)
  s = (*_syserrstr)();
 if(s == ((void *)0))
  s = xsyserr;
 return s;
}


rerrstr

decompiled_code:
__int64 __fastcall rerrstr(__int64 a1, unsigned int a2)
{
char *v3; // r12
int v4; // edi
char *v6; // rax

v3 = (char *)getsyserr();
v4 = *_errno_location();
if ( v4 == 4 )
{
strcpy(v3, "interrupted");
}
else if ( v4 != 422065989 )
{
v6 = strerror(v4);
strcpy(v3, v6);
}
return strecpy(a1, a1 + a2, v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
char *v3; // r12
int v4; // edi
char *v6; // rax

v3 = (char *)func0();
v4 = *_errno_location();
if ( v4 == 4 )
{
strcpy(v3, "interrupted");
}
else if ( v4 != 422065989 )
{
v6 = strerror(v4);
strcpy(v3, v6);
}
return strecpy(a1, a1 + a2, v3);
}

original_code:
void
rerrstr(char *err, uint n)
{
 char *syserr;
 syserr = getsyserr();
 if((*__errno_location ()) == 4)
  strcpy(syserr, "interrupted");
 else if((*__errno_location ()) != EPLAN9)
  strcpy(syserr, strerror((*__errno_location ())));
 strecpy(err, err+n, syserr);
}

canonical_original_code:
void rerrstr(char *err, unsigned int n)
{
 char *syserr;
 syserr = getsyserr();
 if((*__errno_location ()) == 4)
  strcpy(syserr, "interrupted");
 else if((*__errno_location ()) != EPLAN9)
  strcpy(syserr, strerror((*__errno_location ())));
 strecpy(err, err+n, syserr);
}

variable_types:
err
char *

variable_types:
n
unsigned int

variable_types:
syserr
char *


errstr

decompiled_code:
__int64 __fastcall errstr(__int64 a1, unsigned int a2)
{
char *v2; // rax
_BYTE v4[128]; // [rsp+0h] [rbp-98h] BYREF
char v5[24]; // [rsp+80h] [rbp-18h] BYREF

strecpy(v4, v5, a1);
rerrstr(a1, a2);
v2 = (char *)getsyserr();
strecpy(v2, v2 + 128, v4);
*_errno_location() = 422065989;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2)
{
char *v2; // rax
_BYTE v4[128]; // [rsp+0h] [rbp-98h] BYREF
char v5[24]; // [rsp+80h] [rbp-18h] BYREF

strecpy(v4, v5, a1);
func1(a1, a2);
v2 = (char *)func0();
strecpy(v2, v2 + 128, v4);
*_errno_location() = 422065989;
return 0LL;
}

original_code:
int
errstr(char *err, uint n)
{
 char tmp[128];
 char *syserr;
 strecpy(tmp, tmp+128, err);
 rerrstr(err, n);
 syserr = getsyserr();
 strecpy(syserr, syserr+128, tmp);
 (*__errno_location ()) = EPLAN9;
 return 0;
}

canonical_original_code:
int errstr(char *err, unsigned int n)
{
 char tmp[128];
 char *syserr;
 strecpy(tmp, tmp+128, err);
 rerrstr(err, n);
 syserr = getsyserr();
 strecpy(syserr, syserr+128, tmp);
 (*__errno_location ()) = EPLAN9;
 return 0;
}

variable_types:
tmp
char [128]


werrstr

decompiled_code:
__int64 werrstr(__int64 a1, ...)
{
_BYTE v2[128]; // [rsp+0h] [rbp-158h] BYREF
char v3[8]; // [rsp+80h] [rbp-D8h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-D0h] BYREF

va_start(va, a1);
vseprint(v2, v3, a1, va);
return errstr((__int64)v2, 0x80u);
}

canonical_decompiled_code:
__int64 func4(__int64 a1, ...)
{
_BYTE v2[128]; // [rsp+0h] [rbp-158h] BYREF
char v3[8]; // [rsp+80h] [rbp-D8h] BYREF
gcc_va_list va; // [rsp+88h] [rbp-D0h] BYREF

va_start(va, a1);
vseprint(v2, v3, a1, va);
return func2((__int64)v2, 0x80u);
}

original_code:
void
werrstr(char *fmt, ...)
{
 va_list arg;
 char buf[128];
 __builtin_va_start(arg,fmt);
 vseprint(buf, buf+128, fmt, arg);
 __builtin_va_end(arg);
 errstr(buf, 128);
}

canonical_original_code:
void werrstr(char *fmt, ...)
{
 __builtin_va_list arg;
 char buf[128];
 __builtin_va_start(arg,fmt);
 vseprint(buf, buf+128, fmt, arg);
 __builtin_va_end(arg);
 errstr(buf, 128);
}

variable_types:
fmt
char *

variable_types:
buf
char [128]


decompiled_code:
__int64 __fastcall regerror9(char *src)
{
size_t v1; // rax
char v3[152]; // [rsp+0h] [rbp-98h] BYREF

strcpy(v3, "regerror: ");
strcat(v3, src);
*(_WORD *)&v3[strlen(v3)] = 10;
v1 = strlen(v3);
p9write(2LL, v3, v1);
return exits("regerr");
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src)
{
size_t v1; // rax
char v3[152]; // [rsp+0h] [rbp-98h] BYREF

strcpy(v3, "regerror: ");
strcat(v3, src);
*(_WORD *)&v3[strlen(v3)] = 10;
v1 = strlen(v3);
p9write(2LL, v3, v1);
return exits("regerr");
}

original_code:
void
regerror9(char *s)
{
 char buf[132];
 strcpy(buf, "regerror: ");
 strcat(buf, s);
 strcat(buf, "\n");
 p9write(2, buf, strlen(buf));
 exits("regerr");
}

variable_types:
s
char *

variable_types:
buf
char [132]


decompiled_code:
__int64 __fastcall p9srand(__int64 a1)
{
lock(&lk);
isrand(a1);
return unlock(&lk);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
lock(&lk);
func0(a1);
return unlock(&lk);
}

original_code:
void
p9srand(long seed)
{
 lock(&lk);
 isrand(seed);
 unlock(&lk);
}

variable_types: seed long


decompiled_code:
__int64 p9lrand()
{
_DWORD *v0; // rax
__int64 v1; // rbx

lock(&lk);
if ( --rng_tap < rng_vec )
{
if ( !rng_feed )
{
isrand(1LL);
--rng_tap;
}
rng_tap += 607;
}
v0 = (_DWORD *)(rng_feed + 4848);
if ( rng_feed - 8 >= (unsigned __int64)rng_vec )
v0 = (_DWORD *)(rng_feed - 8);
rng_feed = (__int64)v0;
v1 = (*(_DWORD *)rng_tap + *v0) & 0x7FFFFFFF;
*(_QWORD *)v0 = v1;
unlock(&lk);
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
_DWORD *v0; // rax
__int64 v1; // rbx

lock(&lk);
if ( --rng_tap < rng_vec )
{
if ( !rng_feed )
{
func0(1LL);
--rng_tap;
}
rng_tap += 607;
}
v0 = (_DWORD *)(rng_feed + 4848);
if ( rng_feed - 8 >= (unsigned __int64)rng_vec )
v0 = (_DWORD *)(rng_feed - 8);
rng_feed = (__int64)v0;
v1 = (*(_DWORD *)rng_tap + *v0) & 0x7FFFFFFF;
*(_QWORD *)v0 = v1;
unlock(&lk);
return v1;
}

original_code:
long
p9lrand(void)
{
 ulong x;
 lock(&lk);
 rng_tap--;
 if(rng_tap < rng_vec) {
  if(rng_feed == 0) {
   isrand(1);
   rng_tap--;
  }
  rng_tap += 607;
 }
 rng_feed--;
 if(rng_feed < rng_vec)
  rng_feed += 607;
 x = (*rng_feed + *rng_tap) & 0x7fffffffL;
 *rng_feed = x;
 unlock(&lk);
 return x;
}

canonical_original_code:
long p9lrand()
{
 unsigned long x;
 lock(&lk);
 rng_tap--;
 if(rng_tap < rng_vec) {
  if(rng_feed == 0) {
   isrand(1);
   rng_tap--;
  }
  rng_tap += 607;
 }
 rng_feed--;
 if(rng_feed < rng_vec)
  rng_feed += 607;
 x = (*rng_feed + *rng_tap) & 0x7fffffffL;
 *rng_feed = x;
 unlock(&lk);
 return x;
}

variable_types:
x
unsigned long

return_type: long


snprint

decompiled_code:
__int64 snprint(__int64 a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return vsnprint(a1, a2, a3, va);
}

canonical_decompiled_code:
__int64 func0(__int64 a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return vsnprint(a1, a2, a3, va);
}

original_code:
int
snprint(char *buf, int len, char *fmt, ...)
{
 int n;
 va_list args;
 __builtin_va_start(args,fmt);
 n = vsnprint(buf, len, fmt, args);
 __builtin_va_end(args);
 return n;
}

canonical_original_code:
int snprint(char *buf, int len, char *fmt, ...)
{
 int n;
 __builtin_va_list args;
 __builtin_va_start(args,fmt);
 n = vsnprint(buf, len, fmt, args);
 __builtin_va_end(args);
 return n;
}

variable_types:
buf
char *

variable_types: len int

variable_types: n int


rwx

decompiled_code:
char *__fastcall rwx(__int64 a1, char *a2)
{
return strncpy(a2, (const char *)*(&modes + a1), 3uLL);
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, char *a2)
{
return strncpy(a2, (const char *)*(&modes + a1), 3uLL);
}

original_code:
static void
rwx(long m, char *s)
{
 strncpy(s, modes[m], 3);
}

variable_types: m long


dirmodefmt

variable_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; } *

variable_types:
buf
char [16]

variable_types:
m
unsigned long


runeFmtStrFlush

original_code:
static int
runeFmtStrFlush(Fmt *f)
{
 Rune *s;
 int n;
 if(f->start == 0)
  return 0;
 n = (_fmtuintptr)f->farg;
 n *= 2;
 s = (Rune*)f->start;
 f->start = realloc(s, sizeof(Rune)*n);
 if(f->start == 0){
  f->farg = 0;
  f->to = 0;
  f->stop = 0;
  free(s);
  return 0;
 }
 f->farg = (void*)(_fmtuintptr)n;
 f->to = (Rune*)f->start + ((Rune*)f->to - s);
 f->stop = (Rune*)f->start + n - 1;
 return 1;
}

canonical_original_code:
static int runeFmtStrFlush(struct Fmt *f)
{
 unsigned int *s;
 int n;
 if(f->start == 0)
  return 0;
 n = (unsigned long)f->farg;
 n *= 2;
 s = (unsigned int *)f->start;
 f->start = realloc(s, sizeof(Rune)*n);
 if(f->start == 0){
  f->farg = 0;
  f->to = 0;
  f->stop = 0;
  free(s);
  return 0;
 }
 f->farg = (void *)(unsigned long)n;
 f->to = (unsigned int *)f->start + ((unsigned int *)f->to - s);
 f->stop = (unsigned int *)f->start + n - 1;
 return 1;
}

variable_types:
s
unsigned int *


runefmtstrinit

decompiled_code:
__int64 __fastcall runefmtstrinit(_QWORD *a1)
{
char *v1; // rax

*a1 = 0LL;
a1[15] = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 128) >> 3));
*(_BYTE *)a1 = 1;
v1 = (char *)malloc(0x80uLL);
a1[1] = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
a1[2] = v1;
a1[3] = v1 + 124;
a1[4] = runeFmtStrFlush;
a1[5] = 32LL;
fmtlocaleinit(a1, 0LL, 0LL, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
char *v1; // rax

*a1 = 0LL;
a1[15] = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 128) >> 3));
*(_BYTE *)a1 = 1;
v1 = (char *)malloc(0x80uLL);
a1[1] = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
a1[2] = v1;
a1[3] = v1 + 124;
a1[4] = runeFmtStrFlush;
a1[5] = 32LL;
fmtlocaleinit(a1, 0LL, 0LL, 0LL);
return 0LL;
}

original_code:
int
runefmtstrinit(Fmt *f)
{
 int n;
 memset(f, 0, sizeof *f);
 f->runes = 1;
 n = 32;
 f->start = malloc(sizeof(Rune)*n);
 if(f->start == 0)
  return -1;
 f->to = f->start;
 f->stop = (Rune*)f->start + n - 1;
 f->flush = runeFmtStrFlush;
 f->farg = (void*)(_fmtuintptr)n;
 f->nfmt = 0;
 fmtlocaleinit(f, 0, 0, 0);
 return 0;
}

canonical_original_code:
int runefmtstrinit(struct Fmt *f)
{
 int n;
 memset(f, 0, sizeof *f);
 f->runes = 1;
 n = 32;
 f->start = malloc(sizeof(Rune)*n);
 if(f->start == 0)
  return -1;
 f->to = f->start;
 f->stop = (unsigned int *)f->start + n - 1;
 f->flush = runeFmtStrFlush;
 f->farg = (void *)(unsigned long)n;
 f->nfmt = 0;
 fmtlocaleinit(f, 0, 0, 0);
 return 0;
}


runevsmprint

decompiled_code:
void *__fastcall runevsmprint(__int64 a1, const __m128i *a2)
{
int v2; // edx
void *result; // rax
int v4; // edx
__int64 v5; // [rsp+0h] [rbp-98h] BYREF
void *ptr; // [rsp+8h] [rbp-90h]
_DWORD *v7; // [rsp+10h] [rbp-88h]
__m128i v8; // [rsp+38h] [rbp-60h]
__int64 v9; // [rsp+48h] [rbp-50h]

v2 = runefmtstrinit(&v5);
result = 0LL;
if ( v2 >= 0 )
{
v8 = _mm_loadu_si128(a2);
v9 = a2[1].m128i_i64[0];
v4 = dofmt(&v5, a1);
result = ptr;
if ( ptr )
{
if ( v4 < 0 )
{
free(ptr);
result = 0LL;
}
else
{
*v7 = 0;
result = ptr;
}
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1, const __m128i *a2)
{
int v2; // edx
void *result; // rax
int v4; // edx
__int64 v5; // [rsp+0h] [rbp-98h] BYREF
void *ptr; // [rsp+8h] [rbp-90h]
_DWORD *v7; // [rsp+10h] [rbp-88h]
__m128i v8; // [rsp+38h] [rbp-60h]
__int64 v9; // [rsp+48h] [rbp-50h]

v2 = func1(&v5);
result = 0LL;
if ( v2 >= 0 )
{
v8 = _mm_loadu_si128(a2);
v9 = a2[1].m128i_i64[0];
v4 = dofmt(&v5, a1);
result = ptr;
if ( ptr )
{
if ( v4 < 0 )
{
free(ptr);
result = 0LL;
}
else
{
*v7 = 0;
result = ptr;
}
}
}
return result;
}

original_code:
Rune*
runevsmprint(char *fmt, va_list args)
{
 Fmt f;
 int n;
 if(runefmtstrinit(&f) < 0)
  return 0;
 __builtin_va_copy(f.args,args);
 n = dofmt(&f, fmt);
 __builtin_va_end(f.args);
 if(f.start == 0)
  return 0;
 if(n < 0){
  free(f.start);
  return 0;
 }
 *(Rune*)f.to = '\0';
 return (Rune*)f.start;
}

canonical_original_code:
unsigned int *runevsmprint(char *fmt, __builtin_va_list args)
{
 struct Fmt f;
 int n;
 if(runefmtstrinit(&f) < 0)
  return 0;
 __builtin_va_copy(f.args,args);
 n = dofmt(&f, fmt);
 __builtin_va_end(f.args);
 if(f.start == 0)
  return 0;
 if(n < 0){
  free(f.start);
  return 0;
 }
 *(unsigned int *)f.to = '\0';
 return (unsigned int *)f.start;
}

variable_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; }


Bflush

original_code:
int
Bflush(Biobuf *bp)
{
 int n, c;
 switch(bp->state) {
 case Bwactive:
  n = bp->bsize+bp->ocount;
  if(n == 0)
   return 0;
  c = p9write(bp->fid, bp->bbuf, n);
  if(n == c) {
   bp->offset += n;
   bp->ocount = -bp->bsize;
   return 0;
  }
  bp->state = Binactive;
  bp->ocount = 0;
  break;
 case Bracteof:
  bp->state = Bractive;
 case Bractive:
  bp->icount = 0;
  bp->gbuf = bp->ebuf;
  return 0;
 }
 return Beof;
}

canonical_original_code:
int Bflush(struct Biobuf *bp)
{
 int n; int c;
 switch(bp->state) {
 case Bwactive:
  n = bp->bsize+bp->ocount;
  if(n == 0)
   return 0;
  c = p9write(bp->fid, bp->bbuf, n);
  if(n == c) {
   bp->offset += n;
   bp->ocount = -bp->bsize;
   return 0;
  }
  bp->state = Binactive;
  bp->ocount = 0;
  break;
 case Bracteof:
  bp->state = Bractive;
 case Bractive:
  bp->icount = 0;
  bp->gbuf = bp->ebuf;
  return 0;
 }
 return Beof;
}

variable_types: struct Biobuf { int icount; int ocount; int rdline; int runesize; int state; int fid; int flag; long long offset; int bsize; unsigned char *bbuf; unsigned char *ebuf; unsigned char *gbuf; unsigned char b[-1]; } *

variable_types: c int


decompiled_code:
__int64 __fastcall p9times(__suseconds_t *a1)
{
__suseconds_t v1; // rcx
__suseconds_t v2; // r9
__suseconds_t v3; // r8
__time_t v4; // rsi
struct rusage v6; // [rsp+0h] [rbp-128h] BYREF
struct rusage usage; // [rsp+90h] [rbp-98h] BYREF

if ( getrusage(RUSAGE_SELF, &usage) < 0 )
return -1LL;
if ( getrusage(RUSAGE_CHILDREN, &v6) < 0 )
return -1LL;
v1 = usage.ru_utime.tv_usec / 1000 + 1000 * usage.ru_utime.tv_sec;
*a1 = v1;
v2 = usage.ru_stime.tv_usec / 1000 + 1000 * usage.ru_stime.tv_sec;
a1[1] = v2;
v3 = v6.ru_utime.tv_usec / 1000 + 1000 * v6.ru_utime.tv_sec;
a1[2] = v3;
v4 = 1000 * v6.ru_stime.tv_sec + v6.ru_stime.tv_usec / 1000;
a1[3] = v4;
return v4 + v3 + v1 + v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__suseconds_t *a1)
{
__suseconds_t v1; // rcx
__suseconds_t v2; // r9
__suseconds_t v3; // r8
__time_t v4; // rsi
struct rusage v6; // [rsp+0h] [rbp-128h] BYREF
struct rusage usage; // [rsp+90h] [rbp-98h] BYREF

if ( getrusage(RUSAGE_SELF, &usage) < 0 )
return -1LL;
if ( getrusage(RUSAGE_CHILDREN, &v6) < 0 )
return -1LL;
v1 = usage.ru_utime.tv_usec / 1000 + 1000 * usage.ru_utime.tv_sec;
*a1 = v1;
v2 = usage.ru_stime.tv_usec / 1000 + 1000 * usage.ru_stime.tv_sec;
a1[1] = v2;
v3 = v6.ru_utime.tv_usec / 1000 + 1000 * v6.ru_utime.tv_sec;
a1[2] = v3;
v4 = 1000 * v6.ru_stime.tv_sec + v6.ru_stime.tv_usec / 1000;
a1[3] = v4;
return v4 + v3 + v1 + v2;
}

original_code:
long
p9times(long *t)
{
 struct rusage ru, cru;
 if(getrusage(0, &ru) < 0 || getrusage(-1, &cru) < 0)
  return -1;
 t[0] = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
 t[1] = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
 t[2] = cru.ru_utime.tv_sec*1000 + cru.ru_utime.tv_usec/1000;
 t[3] = cru.ru_stime.tv_sec*1000 + cru.ru_stime.tv_usec/1000;
 return t[0]+t[1]+t[2]+t[3];
}

canonical_original_code:
long p9times(long *t)
{
 struct rusage ru; struct rusage cru;
 if(getrusage(0, &ru) < 0 || getrusage(-1, &cru) < 0)
  return -1;
 t[0] = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
 t[1] = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
 t[2] = cru.ru_utime.tv_sec*1000 + cru.ru_utime.tv_usec/1000;
 t[3] = cru.ru_stime.tv_sec*1000 + cru.ru_stime.tv_usec/1000;
 return t[0]+t[1]+t[2]+t[3];
}

variable_types:
t
long *

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


decompiled_code:
double p9cputime()
{
double result; // xmm0_8
__suseconds_t v1; // [rsp+0h] [rbp-28h] BYREF
__int64 v2; // [rsp+8h] [rbp-20h]
__int64 v3; // [rsp+10h] [rbp-18h]
__int64 v4; // [rsp+18h] [rbp-10h]

result = *(double *)&LC0;
if ( p9times(&v1) >= 0 )
result = ((double)(int)v1 + (double)(int)v2 + (double)(int)v3 + (double)(int)v4) / 1000.0;
return result;
}

canonical_decompiled_code:
double func1()
{
double result; // xmm0_8
__suseconds_t v1; // [rsp+0h] [rbp-28h] BYREF
__int64 v2; // [rsp+8h] [rbp-20h]
__int64 v3; // [rsp+10h] [rbp-18h]
__int64 v4; // [rsp+18h] [rbp-10h]

result = *(double *)&LC0;
if ( func0(&v1) >= 0 )
result = ((double)(int)v1 + (double)(int)v2 + (double)(int)v3 + (double)(int)v4) / 1000.0;
return result;
}

original_code:
double
p9cputime(void)
{
 long t[4];
 double d;
 if(p9times(t) < 0)
  return -1.0;
 d = (double)t[0]+(double)t[1]+(double)t[2]+(double)t[3];
 return d/1000.0;
}

canonical_original_code:
double p9cputime()
{
 long t[4];
 double d;
 if(p9times(t) < 0)
  return -1.0;
 d = (double)t[0]+(double)t[1]+(double)t[2]+(double)t[3];
 return d/1000.0;
}

variable_types:
t
long [4]

variable_types: d double

return_type: double


decompiled_code:
__suseconds_t p9nsec()
{
__suseconds_t result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) < 0 )
result = -1LL;
else
result = 1000 * v1.tv_usec + 1000000000 * v1.tv_sec;
return result;
}

canonical_decompiled_code:
__suseconds_t func2()
{
__suseconds_t result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) < 0 )
result = -1LL;
else
result = 1000 * v1.tv_usec + 1000000000 * v1.tv_sec;
return result;
}

original_code:
vlong
p9nsec(void)
{
 struct timeval tv;
 if(gettimeofday(&tv, 0) < 0)
  return -1;
 return (vlong)tv.tv_sec*1000*1000*1000 + tv.tv_usec*1000;
}

canonical_original_code:
long long p9nsec()
{
 struct timeval tv;
 if(gettimeofday(&tv, 0) < 0)
  return -1;
 return (long long)tv.tv_sec*1000*1000*1000 + tv.tv_usec*1000;
}

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

return_type: long long


decompiled_code:
time_t __fastcall p9time(time_t *a1)
{
time_t result; // rax

result = time(0LL);
if ( a1 )
*a1 = result;
return result;
}

canonical_decompiled_code:
time_t __fastcall func3(time_t *a1)
{
time_t result; // rax

result = time(0LL);
if ( a1 )
*a1 = result;
return result;
}

original_code:
long
p9time(long *tt)
{
 long t;
 t = time(0);
 if(tt)
  *tt = t;
 return t;
}

variable_types:
tt
long *

variable_types: t long


putfd

decompiled_code:
__int64 __fastcall putfd(__int64 a1, unsigned int a2)
{
return snprint(a1, 40LL, "/dev/fd/%d", a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
return snprint(a1, 40LL, "/dev/fd/%d", a2);
}

original_code:
static void
putfd(char *dir, int fd)
{
 snprint(dir, 40, "/dev/fd/%d", fd);
}

variable_types:
dir
char *

variable_types: fd int


decompiled_code:
__int64 __fastcall p9announce(const char *a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbx
int v4; // ebx
socklen_t v5; // eax
int *v6; // r12
socklen_t v7; // eax
socklen_t v8; // eax
int v9; // er12
int v10; // eax
socklen_t v11; // eax
struct sockaddr v13[8]; // [rsp+0h] [rbp-B8h] BYREF
socklen_t optlen; // [rsp+84h] [rbp-34h] BYREF
int optval; // [rsp+88h] [rbp-30h] BYREF
int v16; // [rsp+8Ch] [rbp-2Ch] BYREF
const char *v17; // [rsp+90h] [rbp-28h] BYREF
char *filename; // [rsp+98h] [rbp-20h] BYREF

v2 = strdup(a1);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
if ( (int)p9dialparse(v2, &v17, &filename, v13, &v16) < 0 )
{
free(v3);
return (unsigned int)-1;
}
if ( !strcmp(v17, "tcp") )
{
v9 = 1;
LABEL_18:
free(v3);
v10 = socket(v13[0].sa_family, v9, 0);
v4 = v10;
if ( v10 < 0 )
return (unsigned int)-1;
optlen = 4;
if ( v16 && getsockopt(v10, 1, 3, &optval, &optlen) >= 0 && optval == 1 )
setsockopt(v4, 1, 2, &optval, 4u);
v11 = addrlen((__int16 *)v13);
if ( bind(v4, v13, v11) < 0 )
goto LABEL_27;
if ( v9 == 1 )
{
LABEL_22:
listen(v4, 8);
putfd(a2, v4);
return (unsigned int)v4;
}
return (unsigned int)v4;
}
if ( !strcmp(v17, "udp") )
{
v9 = 2;
goto LABEL_18;
}
if ( !strcmp(v17, "unix") )
{
v4 = socket(v13[0].sa_family, 1, 0);
if ( v4 < 0 )
return (unsigned int)-1;
v5 = addrlen((__int16 *)v13);
if ( bind(v4, v13, v5) >= 0 )
goto LABEL_22;
v6 = _errno_location();
if ( *v6 == 98 )
{
v7 = addrlen((__int16 *)v13);
if ( connect(v4, v13, v7) < 0 && *v6 == 111 )
{
remove(filename);
close(v4);
v4 = socket(v13[0].sa_family, 1, 0);
if ( v4 < 0 )
return (unsigned int)-1;
v8 = addrlen((__int16 *)v13);
if ( bind(v4, v13, v8) >= 0 )
goto LABEL_22;
}
}
LABEL_27:
close(v4);
return (unsigned int)-1;
}
werrstr("can only handle tcp, udp, and unix: not %s", v17);
free(v3);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbx
int v4; // ebx
socklen_t v5; // eax
int *v6; // r12
socklen_t v7; // eax
socklen_t v8; // eax
int v9; // er12
int v10; // eax
socklen_t v11; // eax
struct sockaddr v13[8]; // [rsp+0h] [rbp-B8h] BYREF
socklen_t optlen; // [rsp+84h] [rbp-34h] BYREF
int optval; // [rsp+88h] [rbp-30h] BYREF
int v16; // [rsp+8Ch] [rbp-2Ch] BYREF
const char *v17; // [rsp+90h] [rbp-28h] BYREF
char *filename; // [rsp+98h] [rbp-20h] BYREF

v2 = strdup(a1);
if ( !v2 )
return (unsigned int)-1;
v3 = v2;
if ( (int)p9dialparse(v2, &v17, &filename, v13, &v16) < 0 )
{
free(v3);
return (unsigned int)-1;
}
if ( !strcmp(v17, "tcp") )
{
v9 = 1;
LABEL_18:
free(v3);
v10 = socket(v13[0].sa_family, v9, 0);
v4 = v10;
if ( v10 < 0 )
return (unsigned int)-1;
optlen = 4;
if ( v16 && getsockopt(v10, 1, 3, &optval, &optlen) >= 0 && optval == 1 )
setsockopt(v4, 1, 2, &optval, 4u);
v11 = func0((__int16 *)v13);
if ( bind(v4, v13, v11) < 0 )
goto LABEL_27;
if ( v9 == 1 )
{
LABEL_22:
listen(v4, 8);
func1(a2, v4);
return (unsigned int)v4;
}
return (unsigned int)v4;
}
if ( !strcmp(v17, "udp") )
{
v9 = 2;
goto LABEL_18;
}
if ( !strcmp(v17, "unix") )
{
v4 = socket(v13[0].sa_family, 1, 0);
if ( v4 < 0 )
return (unsigned int)-1;
v5 = func0((__int16 *)v13);
if ( bind(v4, v13, v5) >= 0 )
goto LABEL_22;
v6 = _errno_location();
if ( *v6 == 98 )
{
v7 = func0((__int16 *)v13);
if ( connect(v4, v13, v7) < 0 && *v6 == 111 )
{
remove(filename);
close(v4);
v4 = socket(v13[0].sa_family, 1, 0);
if ( v4 < 0 )
return (unsigned int)-1;
v8 = func0((__int16 *)v13);
if ( bind(v4, v13, v8) >= 0 )
goto LABEL_22;
}
}
LABEL_27:
close(v4);
return (unsigned int)-1;
}
werrstr("can only handle tcp, udp, and unix: not %s", v17);
free(v3);
return (unsigned int)-1;
}

original_code:
int
p9announce(char *addr, char *dir)
{
 int proto;
 char *buf, *sockunix;
 char *net;
 int port, s;
 int n;
 socklen_t sn;
 struct sockaddr_storage ss;
 buf = strdup(addr);
 if(buf == ((void*)0))
  return -1;
 if(p9dialparse(buf, &net, &sockunix, &ss, &port) < 0){
  free(buf);
  return -1;
 }
 if(strcmp(net, "tcp") == 0)
  proto = SOCK_STREAM;
 else if(strcmp(net, "udp") == 0)
  proto = SOCK_DGRAM;
 else if(strcmp(net, "unix") == 0)
  goto Unix;
 else{
  werrstr("can only handle tcp, udp, and unix: not %s", net);
  free(buf);
  return -1;
 }
 free(buf);
 if((s = socket(ss.ss_family, proto, 0)) < 0)
  return -1;
 sn = sizeof n;
 if(port && getsockopt(s, 1, 3, (void*)&n, &sn) >= 0
 && n == SOCK_STREAM){
  n = 1;
  setsockopt(s, 1, 2, (char*)&n, sizeof n);
 }
 if(bind(s, (struct sockaddr*)&ss, addrlen(&ss)) < 0){
  close(s);
  return -1;
 }
 if(proto == SOCK_STREAM){
  listen(s, 8);
  putfd(dir, s);
 }
 return s;
Unix:
 if((s = socket(ss.ss_family, SOCK_STREAM, 0)) < 0)
  return -1;
 if(bind(s, (struct sockaddr*)&ss, addrlen(&ss)) < 0){
  if((*__errno_location ()) == 98
  && connect(s, (struct sockaddr*)&ss, addrlen(&ss)) < 0
  && (*__errno_location ()) == 111){
   remove(sockunix);
   close(s);
   if((s = socket(ss.ss_family, SOCK_STREAM, 0)) < 0)
    return -1;
   if(bind(s, (struct sockaddr*)&ss, addrlen(&ss)) >= 0)
    goto Success;
  }
  close(s);
  return -1;
 }
Success:
 listen(s, 8);
 putfd(dir, s);
 return s;
}

canonical_original_code:
int p9announce(char *addr, char *dir)
{
 int proto;
 char *buf; char *sockunix;
 char *net;
 int port; int s;
 int n;
 unsigned int sn;
 struct sockaddr_storage ss;
 buf = strdup(addr);
 if(buf == ((void *)0))
  return -1;
 if(p9dialparse(buf, &net, &sockunix, &ss, &port) < 0){
  free(buf);
  return -1;
 }
 if(strcmp(net, "tcp") == 0)
  proto = SOCK_STREAM;
 else if(strcmp(net, "udp") == 0)
  proto = SOCK_DGRAM;
 else if(strcmp(net, "unix") == 0)
  goto Unix;
 else{
  werrstr("can only handle tcp, udp, and unix: not %s", net);
  free(buf);
  return -1;
 }
 free(buf);
 if((s = socket(ss.ss_family, proto, 0)) < 0)
  return -1;
 sn = sizeof n;
 if(port && getsockopt(s, 1, 3, (void *)&n, &sn) >= 0
 && n == SOCK_STREAM){
  n = 1;
  setsockopt(s, 1, 2, (char *)&n, sizeof n);
 }
 if(bind(s, (struct sockaddr *)&ss, addrlen(&ss)) < 0){
  close(s);
  return -1;
 }
 if(proto == SOCK_STREAM){
  listen(s, 8);
  putfd(dir, s);
 }
 return s;
Unix:
 if((s = socket(ss.ss_family, SOCK_STREAM, 0)) < 0)
  return -1;
 if(bind(s, (struct sockaddr *)&ss, addrlen(&ss)) < 0){
  if((*__errno_location ()) == 98
  && connect(s, (struct sockaddr *)&ss, addrlen(&ss)) < 0
  && (*__errno_location ()) == 111){
   remove(sockunix);
   close(s);
   if((s = socket(ss.ss_family, SOCK_STREAM, 0)) < 0)
    return -1;
   if(bind(s, (struct sockaddr *)&ss, addrlen(&ss)) >= 0)
    goto Success;
  }
  close(s);
  return -1;
 }
Success:
 listen(s, 8);
 putfd(dir, s);
 return s;
}

variable_types:
addr
char *

variable_types: proto int

variable_types:
buf
char *

variable_types:
sockunix
char *

variable_types:
net
char *

variable_types: port int

variable_types: s int

variable_types: n int

variable_types:
sn
unsigned int

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

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


decompiled_code:
__int64 __fastcall p9listen(const char *a1, __int64 a2)
{
int v2; // eax
int v3; // eax
unsigned int v4; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = p9netfd((__int64)a1);
if ( v2 < 0 )
{
werrstr("bad 'directory' in listen: %s", a1);
v4 = -1;
}
else
{
v3 = accept(v2, 0LL, 0LL);
v4 = v3;
if ( v3 < 0 )
{
v4 = -1;
}
else
{
optval[0] = 1;
setsockopt(v3, 6, 1, optval, 4u);
putfd(a2, v4);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, __int64 a2)
{
int v2; // eax
int v3; // eax
unsigned int v4; // ebx
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = func2((__int64)a1);
if ( v2 < 0 )
{
werrstr("bad 'directory' in listen: %s", a1);
v4 = -1;
}
else
{
v3 = accept(v2, 0LL, 0LL);
v4 = v3;
if ( v3 < 0 )
{
v4 = -1;
}
else
{
optval[0] = 1;
setsockopt(v3, 6, 1, optval, 4u);
func1(a2, v4);
}
}
return v4;
}

original_code:
int
p9listen(char *dir, char *newdir)
{
 int fd, one;
 if((fd = _p9netfd(dir)) < 0){
  werrstr("bad 'directory' in listen: %s", dir);
  return -1;
 }
 if((fd = accept(fd, ((void*)0), ((void*)0))) < 0)
  return -1;
 one = 1;
 setsockopt(fd, IPPROTO_TCP, 1, (char*)&one, sizeof one);
 putfd(newdir, fd);
 return fd;
}

canonical_original_code:
int p9listen(char *dir, char *newdir)
{
 int fd; int one;
 if((fd = _p9netfd(dir)) < 0){
  werrstr("bad 'directory' in listen: %s", dir);
  return -1;
 }
 if((fd = accept(fd, ((void *)0), ((void *)0))) < 0)
  return -1;
 one = 1;
 setsockopt(fd, IPPROTO_TCP, 1, (char *)&one, sizeof one);
 putfd(newdir, fd);
 return fd;
}

variable_types:
newdir
char *

variable_types: one int


decompiled_code:
int __fastcall p9accept(__int64 a1, __int64 a2)
{
int v2; // eax

v2 = p9netfd(a2);
if ( v2 >= 0 )
return dup(v2);
werrstr("bad 'directory' in accept");
return -1;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2)
{
int v2; // eax

v2 = func2(a2);
if ( v2 >= 0 )
return dup(v2);
werrstr("bad 'directory' in accept");
return -1;
}

original_code:
int
p9accept(int cfd, char *dir)
{
 int fd;
 if((fd = _p9netfd(dir)) < 0){
  werrstr("bad 'directory' in accept");
  return -1;
 }
 return dup(fd);
}

variable_types: cfd int


decompiled_code:
void *__fastcall p9malloc(size_t size)
{
size_t v2; // rdi
void *v3; // rbx

lock(&malloclock);
v2 = 1LL;
if ( size )
v2 = size;
v3 = malloc(v2);
unlock(&malloclock);
return v3;
}

canonical_decompiled_code:
void *__fastcall func0(size_t size)
{
size_t v2; // rdi
void *v3; // rbx

lock(&malloclock);
v2 = 1LL;
if ( size )
v2 = size;
v3 = malloc(v2);
unlock(&malloclock);
return v3;
}

original_code:
void*
p9malloc(ulong n)
{
 void *v;
 if(n == 0)
  n++;
 lock(&malloclock);
 v = malloc(n);
 unlock(&malloclock);
 return v;
}

canonical_original_code:
void *p9malloc(unsigned long n)
{
 void *v;
 if(n == 0)
  n++;
 lock(&malloclock);
 v = malloc(n);
 unlock(&malloclock);
 return v;
}

variable_types:
n
unsigned long

variable_types:
v
void *

return_type: void *


decompiled_code:
__int64 __fastcall p9free(void *ptr)
{
__int64 result; // rax

if ( ptr )
{
lock(&malloclock);
free(ptr);
result = unlock(&malloclock);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
__int64 result; // rax

if ( ptr )
{
lock(&malloclock);
free(ptr);
result = unlock(&malloclock);
}
return result;
}

original_code:
void
p9free(void *v)
{
 if(v == ((void*)0))
  return;
 lock(&malloclock);
 free(v);
 unlock(&malloclock);
}

canonical_original_code:
void p9free(void *v)
{
 if(v == ((void *)0))
  return;
 lock(&malloclock);
 free(v);
 unlock(&malloclock);
}


decompiled_code:
void *__fastcall p9calloc(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
void *v4; // rbx

v2 = a1;
v3 = a2;
if ( !(a2 * a1) )
v3 = 1LL;
if ( !(a2 * a1) )
v2 = 1LL;
lock(&malloclock);
v4 = calloc(v3 * v2, 1uLL);
unlock(&malloclock);
return v4;
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rbp
void *v4; // rbx

v2 = a1;
v3 = a2;
if ( !(a2 * a1) )
v3 = 1LL;
if ( !(a2 * a1) )
v2 = 1LL;
lock(&malloclock);
v4 = calloc(v3 * v2, 1uLL);
unlock(&malloclock);
return v4;
}

original_code:
void*
p9calloc(ulong a, ulong b)
{
 void *v;
 if(a*b == 0)
  a = b = 1;
 lock(&malloclock);
 v = calloc(a*b, 1);
 unlock(&malloclock);
 return v;
}

canonical_original_code:
void *p9calloc(unsigned long a, unsigned long b)
{
 void *v;
 if(a*b == 0)
  a = b = 1;
 lock(&malloclock);
 v = calloc(a*b, 1);
 unlock(&malloclock);
 return v;
}

variable_types:
a
unsigned long

variable_types:
b
unsigned long


decompiled_code:
void *__fastcall p9realloc(void *ptr, size_t size)
{
void *v2; // rbx

lock(&malloclock);
v2 = realloc(ptr, size);
unlock(&malloclock);
return v2;
}

canonical_decompiled_code:
void *__fastcall func3(void *ptr, size_t size)
{
void *v2; // rbx

lock(&malloclock);
v2 = realloc(ptr, size);
unlock(&malloclock);
return v2;
}

original_code:
void*
p9realloc(void *v, ulong n)
{
 lock(&malloclock);
 v = realloc(v, n);
 unlock(&malloclock);
 return v;
}

canonical_original_code:
void *p9realloc(void *v, unsigned long n)
{
 lock(&malloclock);
 v = realloc(v, n);
 unlock(&malloclock);
 return v;
}


original_code:
int
p9open(char *name, int mode)
{
 int cexec, rclose;
 int fd, umode, lock, rdwr;
 struct flock fl;
 rdwr = mode&3;
 umode = rdwr;
 cexec = mode&32;
 rclose = mode&64;
 lock = mode&0x2000;
 mode &= ~(3|32|64|0x2000);
 if(mode&16){
  umode |= 01000;
  mode ^= 16;
 }
 if(mode&128){
  umode |= 040000;
  mode ^= 128;
 }
 if(mode&256){
  umode |= 04000;
  mode ^= 256;
 }
 if(mode&0x4000){
  umode |= 02000;
  mode ^= 0x4000;
 }
 if(mode){
  werrstr("mode 0x%x not supported", mode);
  return -1;
 }
 fd = open(name, umode);
 if(fd >= 0){
  if(lock){
   fl.l_type = (rdwr==0) ? 0 : 1;
   fl.l_whence = 0;
   fl.l_start = 0;
   fl.l_len = 0;
   if(fcntl(fd, 6, &fl) < 0){
    close(fd);
    werrstr("lock: %r");
    return -1;
   }
  }
  if(cexec)
   fcntl(fd, 4, 1);
  if(rclose)
   remove(name);
 }
 return fd;
}

canonical_original_code:
int p9open(char *name, int mode)
{
 int cexec; int rclose;
 int fd; int umode; int lock; int rdwr;
 struct flock fl;
 rdwr = mode&3;
 umode = rdwr;
 cexec = mode&32;
 rclose = mode&64;
 lock = mode&0x2000;
 mode &= ~(3|32|64|0x2000);
 if(mode&16){
  umode |= 01000;
  mode ^= 16;
 }
 if(mode&128){
  umode |= 040000;
  mode ^= 128;
 }
 if(mode&256){
  umode |= 04000;
  mode ^= 256;
 }
 if(mode&0x4000){
  umode |= 02000;
  mode ^= 0x4000;
 }
 if(mode){
  werrstr("mode 0x%x not supported", mode);
  return -1;
 }
 fd = open(name, umode);
 if(fd >= 0){
  if(lock){
   fl.l_type = (rdwr==0) ? 0 : 1;
   fl.l_whence = 0;
   fl.l_start = 0;
   fl.l_len = 0;
   if(fcntl(fd, 6, &fl) < 0){
    close(fd);
    werrstr("lock: %r");
    return -1;
   }
  }
  if(cexec)
   fcntl(fd, 4, 1);
  if(rclose)
   remove(name);
 }
 return fd;
}

variable_types:
name
char *

variable_types: mode int

variable_types: cexec int

variable_types: rclose int

variable_types: fd int

variable_types: umode int

variable_types: lock int

variable_types: rdwr int

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


utfrrune

decompiled_code:
char *__fastcall utfrrune(const char *a1, __int64 a2)
{
const char *v2; // rbx
const char *v3; // r12
int v5; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = a1;
if ( a2 <= 127 )
return strrchr(a1, a2);
v3 = 0LL;
while ( 1 )
{
while ( *(unsigned __int8 *)v2 > 0x7FuLL )
{
v5 = chartorune(v6, v2);
if ( v6[0] == a2 )
v3 = v2;
v2 += v5;
}
if ( !*v2 )
break;
++v2;
}
return (char *)v3;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1, __int64 a2)
{
const char *v2; // rbx
const char *v3; // r12
int v5; // eax
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = a1;
if ( a2 <= 127 )
return strrchr(a1, a2);
v3 = 0LL;
while ( 1 )
{
while ( *(unsigned __int8 *)v2 > 0x7FuLL )
{
v5 = chartorune(v6, v2);
if ( v6[0] == a2 )
v3 = v2;
v2 += v5;
}
if ( !*v2 )
break;
++v2;
}
return (char *)v3;
}

original_code:
char*
utfrrune(char *s, long c)
{
 long c1;
 Rune r;
 char *s1;
 if(c < Runesync)
  return strrchr(s, c);
 s1 = 0;
 for(;;) {
  c1 = *(_utfuchar*)s;
  if(c1 < Runeself) {
   if(c1 == 0)
    return s1;
   if(c1 == c)
    s1 = s;
   s++;
   continue;
  }
  c1 = chartorune(&r, s);
  if(r == c)
   s1 = s;
  s += c1;
 }
}

canonical_original_code:
char *utfrrune(char *s, long c)
{
 long c1;
 unsigned int r;
 char *s1;
 if(c < Runesync)
  return strrchr(s, c);
 s1 = 0;
 for(;;) {
  c1 = *(unsigned char *)s;
  if(c1 < Runeself) {
   if(c1 == 0)
    return s1;
   if(c1 == c)
    s1 = s;
   s++;
   continue;
  }
  c1 = chartorune(&r, s);
  if(r == c)
   s1 = s;
  s += c1;
 }
}

variable_types:
s
char *

variable_types: c long

variable_types:
r
unsigned int


yrsize

decompiled_code:
void *__fastcall yrsize(int a1)
{
void *v1; // rdx

v1 = &dmsize;
if ( (a1 & 3) == 0 )
{
v1 = &ldmsize;
if ( a1 == 100 * (a1 / 100) )
{
v1 = &dmsize;
if ( a1 == 400 * (a1 / 400) )
v1 = &ldmsize;
}
}
return v1;
}

original_code:
static int *
yrsize(int y)
{
 if((y%4) == 0 && ((y%100) != 0 || (y%400) == 0))
  return ldmsize;
 else
  return dmsize;
}

canonical_original_code:
static int *yrsize(int y)
{
 if((y%4) == 0 && ((y%100) != 0 || (y%400) == 0))
  return ldmsize;
 else
  return dmsize;
}

variable_types: y int

return_type: int *


original_code:
long
p9tm2sec(Tm *tm)
{
 Tinfo ti0, ti1, *ti;
 long secs;
 int i, yday, year, *d2m;
 secs = 0;
 year = tm->year + 1900;
 for(i = 1970; i < year; i++){
  d2m = yrsize(i);
  secs += d2m[0] * (24L*(60L*60L));
 }
 if(tm->mday){
  yday = 0;
  d2m = yrsize(year);
  for(i=0; i<tm->mon; i++)
   yday += d2m[i+1];
  yday += tm->mday-1;
 }else{
  yday = tm->yday;
 }
 secs += yday * (24L*(60L*60L));
 secs += tm->hour * (60L*60L);
 secs += tm->min * 60L;
 secs += tm->sec;
 if(strcmp(tm->zone, "GMT") != 0) {
  i = _p9zonelookuptinfo(&ti0, secs);
  ti = &ti0;
  if (i != -1)
  if (ti->tzoff!=0) {
   if (ti->tzoff>0) {
    if (ti->t+ti->tzoff > secs)
    if (_p9zonetinfo(&ti1, i-1)!=-1)
     ti = &ti1;
   } else
    if (_p9zonetinfo(&ti1, i+1))
    if (ti1.t+ti->tzoff < secs)
     ti = &ti1;
   secs -= ti->tzoff;
  }
 }
 if(secs < 0)
  secs = 0;
 return secs;
}

canonical_original_code:
long p9tm2sec(struct Tm *tm)
{
 struct Tinfo ti0; struct Tinfo ti1; struct Tinfo *ti;
 long secs;
 int i; int yday; int year; int *d2m;
 secs = 0;
 year = tm->year + 1900;
 for(i = 1970; i < year; i++){
  d2m = yrsize(i);
  secs += d2m[0] * (24L*(60L*60L));
 }
 if(tm->mday){
  yday = 0;
  d2m = yrsize(year);
  for(i=0; i<tm->mon; i++)
   yday += d2m[i+1];
  yday += tm->mday-1;
 }else{
  yday = tm->yday;
 }
 secs += yday * (24L*(60L*60L));
 secs += tm->hour * (60L*60L);
 secs += tm->min * 60L;
 secs += tm->sec;
 if(strcmp(tm->zone, "GMT") != 0) {
  i = _p9zonelookuptinfo(&ti0, secs);
  ti = &ti0;
  if (i != -1)
  if (ti->tzoff!=0) {
   if (ti->tzoff>0) {
    if (ti->t+ti->tzoff > secs)
    if (_p9zonetinfo(&ti1, i-1)!=-1)
     ti = &ti1;
   } else
    if (_p9zonetinfo(&ti1, i+1))
    if (ti1.t+ti->tzoff < secs)
     ti = &ti1;
   secs -= ti->tzoff;
  }
 }
 if(secs < 0)
  secs = 0;
 return secs;
}

variable_types: struct Tm { int sec; int min; int hour; int mday; int mon; int year; int wday; int yday; char zone[4]; int tzoff; } *

variable_types: struct Tinfo { long t; int tzoff; int dlflag; char *zone; } *

variable_types: secs long

variable_types: i int

variable_types: yday int

variable_types: year int


unquotestrdup

original_code:
char*
unquotestrdup(char *s)
{
 char *t, *ret;
 int quoting;
 ret = s = p9strdup(s);
 if(ret == ((void*)0))
  return ret;
 quoting = 0;
 t = s;
 while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){
  if(*t != '\''){
   *s++ = *t++;
   continue;
  }
  if(!quoting){
   quoting = 1;
   t++;
   continue;
  }
  if(t[1] != '\''){
   t++;
   quoting = 0;
   continue;
  }
  t++;
  *s++ = *t++;
 }
 if(t != s)
  memmove(s, t, strlen(t)+1);
 return ret;
}

canonical_original_code:
char *unquotestrdup(char *s)
{
 char *t; char *ret;
 int quoting;
 ret = s = p9strdup(s);
 if(ret == ((void *)0))
  return ret;
 quoting = 0;
 t = s;
 while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){
  if(*t != '\''){
   *s++ = *t++;
   continue;
  }
  if(!quoting){
   quoting = 1;
   t++;
   continue;
  }
  if(t[1] != '\''){
   t++;
   quoting = 0;
   continue;
  }
  t++;
  *s++ = *t++;
 }
 if(t != s)
  memmove(s, t, strlen(t)+1);
 return ret;
}

variable_types:
t
char *

variable_types:
ret
char *

variable_types: quoting int


decompiled_code:
__int64 NaN()
{
return _NaN();
}

canonical_decompiled_code:
__int64 func0()
{
return _NaN();
}

original_code:
double
NaN(void)
{
 return __NaN();
}

canonical_original_code:
double NaN()
{
 return __NaN();
}


Inf

decompiled_code:
__int64 Inf()
{
return _Inf();
}

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

original_code:
double
Inf(int sign)
{
 return __Inf(sign);
}

variable_types: sign int


isNaN

decompiled_code:
__int64 isNaN()
{
return _isNaN();
}

canonical_decompiled_code:
__int64 func2()
{
return _isNaN();
}

original_code:
int
isNaN(double x)
{
 return __isNaN(x);
}

variable_types: x double


isInf

decompiled_code:
__int64 isInf()
{
return _isInf();
}

canonical_decompiled_code:
__int64 func3()
{
return _isInf();
}

original_code:
int
isInf(double x, int sign)
{
 return __isInf(x, sign);
}


decompiled_code:
__int64 __fastcall p9atoi(const char *a1)
{
return strtol(a1, 0LL, 0);
}

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

original_code:
int
p9atoi(char *s)
{
 return strtol(s, 0, 0);
}


needsrcquote

decompiled_code:
_BOOL8 __fastcall needsrcquote(int c)
{
_BOOL8 result; // rax

result = 1LL;
if ( c > 32 )
result = strchr("`^#*[]=|\\?${}()'<>&;", c) != 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(int c)
{
_BOOL8 result; // rax

result = 1LL;
if ( c > 32 )
result = strchr("`^#*[]=|\\?${}()'<>&;", c) != 0LL;
return result;
}

original_code:
int
needsrcquote(int c)
{
 if(c <= ' ')
  return 1;
 if(strchr("`^#*[]=|\\?${}()'<>&;", c))
  return 1;
 return 0;
}

variable_types: c int


pqid

variable_types:
p
unsigned char *

variable_types: struct Qid { unsigned long long path; unsigned long vers; unsigned char type; } *

return_type: unsigned char *


stringsz

decompiled_code:
__int64 __fastcall stringsz(const char *a1)
{
__int64 result; // rax

if ( a1 )
result = (unsigned int)strlen(a1) + 2;
else
result = 2LL;
return result;
}

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

if ( a1 )
result = (unsigned int)strlen(a1) + 2;
else
result = 2LL;
return result;
}

original_code:
static
uint
stringsz(char *s)
{
 if(s == ((void*)0))
  return 2;
 return 2 +strlen(s);
}

canonical_original_code:
static unsigned int stringsz(char *s)
{
 if(s == ((void *)0))
  return 2;
 return 2 +strlen(s);
}

variable_types:
s
char *

return_type: unsigned int


decompiled_code:
__int64 __fastcall sizeS2M(__int64 a1)
{
unsigned int v1; // ebx
unsigned __int16 v2; // ax
const char **v3; // rbp
__int64 v4; // r12
int v6; // ebp
int v7; // ebp

switch ( *(_BYTE *)a1 )
{
case 'b':
case 'p':
v1 = 12;
break;
case 'c':
v1 = 28;
break;
case 'd':
case 'e':
v1 = stringsz(*(const char **)(a1 + 16)) + 11;
break;
case 'f':
v6 = stringsz(*(const char **)(a1 + 104));
v1 = v6 + stringsz(*(const char **)(a1 + 112)) + 11;
break;
case 'g':
v1 = 20;
break;
case 'h':
v7 = stringsz(*(const char **)(a1 + 104));
v1 = v7 + stringsz(*(const char **)(a1 + 112)) + 15;
break;
case 'i':
v1 = 20;
break;
case 'k':
v1 = stringsz(*(const char **)(a1 + 32)) + 7;
break;
case 'l':
v1 = 9;
break;
case 'm':
case 'y':
case '{':
case '\x7F':
v1 = 7;
break;
case 'n':
v2 = *(_WORD *)(a1 + 144);
if ( v2 )
{
v3 = (const char **)(a1 + 152);
v4 = a1 + 8LL * ((unsigned int)v2 - 1) + 160;
v1 = 17;
do
v1 += stringsz(*v3++);
while ( v3 != (const char **)v4 );
}
else
{
v1 = 17;
}
break;
case 'o':
v1 = 13 * *(unsigned __int16 *)(a1 + 280) + 9;
break;
case 'q':
case 's':
v1 = 24;
break;
case 'r':
v1 = stringsz(*(const char **)(a1 + 128)) + 16;
break;
case 't':
v1 = 23;
break;
case 'u':
v1 = *(_DWORD *)(a1 + 680) + 11;
break;
case 'v':
v1 = *(_DWORD *)(a1 + 680) + 23;
break;
case 'w':
v1 = 11;
break;
case 'x':
case 'z':
v1 = 11;
break;
case '|':
v1 = 11;
break;
case '}':
v1 = *(unsigned __int16 *)(a1 + 696) + 9;
break;
case '~':
v1 = *(unsigned __int16 *)(a1 + 696) + 13;
break;
default:
v1 = 0;
break;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // ebx
unsigned __int16 v2; // ax
const char **v3; // rbp
__int64 v4; // r12
int v6; // ebp
int v7; // ebp

switch ( *(_BYTE *)a1 )
{
case 'b':
case 'p':
v1 = 12;
break;
case 'c':
v1 = 28;
break;
case 'd':
case 'e':
v1 = func1(*(const char **)(a1 + 16)) + 11;
break;
case 'f':
v6 = func1(*(const char **)(a1 + 104));
v1 = v6 + func1(*(const char **)(a1 + 112)) + 11;
break;
case 'g':
v1 = 20;
break;
case 'h':
v7 = func1(*(const char **)(a1 + 104));
v1 = v7 + func1(*(const char **)(a1 + 112)) + 15;
break;
case 'i':
v1 = 20;
break;
case 'k':
v1 = func1(*(const char **)(a1 + 32)) + 7;
break;
case 'l':
v1 = 9;
break;
case 'm':
case 'y':
case '{':
case '\x7F':
v1 = 7;
break;
case 'n':
v2 = *(_WORD *)(a1 + 144);
if ( v2 )
{
v3 = (const char **)(a1 + 152);
v4 = a1 + 8LL * ((unsigned int)v2 - 1) + 160;
v1 = 17;
do
v1 += func1(*v3++);
while ( v3 != (const char **)v4 );
}
else
{
v1 = 17;
}
break;
case 'o':
v1 = 13 * *(unsigned __int16 *)(a1 + 280) + 9;
break;
case 'q':
case 's':
v1 = 24;
break;
case 'r':
v1 = func1(*(const char **)(a1 + 128)) + 16;
break;
case 't':
v1 = 23;
break;
case 'u':
v1 = *(_DWORD *)(a1 + 680) + 11;
break;
case 'v':
v1 = *(_DWORD *)(a1 + 680) + 23;
break;
case 'w':
v1 = 11;
break;
case 'x':
case 'z':
v1 = 11;
break;
case '|':
v1 = 11;
break;
case '}':
v1 = *(unsigned __int16 *)(a1 + 696) + 9;
break;
case '~':
v1 = *(unsigned __int16 *)(a1 + 696) + 13;
break;
default:
v1 = 0;
break;
}
return v1;
}

original_code:
uint
sizeS2M(Fcall *f)
{
 uint n;
 int i;
 n = 0;
 n += 4;
 n += 1;
 n += 2;
 switch(f->type)
 {
 default:
  return 0;
 case Tversion:
  n += 4;
  n += stringsz(f->version);
  break;
 case Tflush:
  n += 2;
  break;
 case Tauth:
  n += 4;
  n += stringsz(f->uname);
  n += stringsz(f->aname);
  break;
 case Tattach:
  n += 4;
  n += 4;
  n += stringsz(f->uname);
  n += stringsz(f->aname);
  break;
 case Twalk:
  n += 4;
  n += 4;
  n += 2;
  for(i=0; i<f->nwname; i++)
   n += stringsz(f->wname[i]);
  break;
 case Topen:
 case Topenfd:
  n += 4;
  n += 1;
  break;
 case Tcreate:
  n += 4;
  n += stringsz(f->name);
  n += 4;
  n += 1;
  break;
 case Tread:
  n += 4;
  n += 8;
  n += 4;
  break;
 case Twrite:
  n += 4;
  n += 8;
  n += 4;
  n += f->count;
  break;
 case Tclunk:
 case Tremove:
  n += 4;
  break;
 case Tstat:
  n += 4;
  break;
 case Twstat:
  n += 4;
  n += 2;
  n += f->nstat;
  break;
 case Rversion:
  n += 4;
  n += stringsz(f->version);
  break;
 case Rerror:
  n += stringsz(f->ename);
  break;
 case Rflush:
  break;
 case Rauth:
  n += (1 +4 +8);
  break;
 case Rattach:
  n += (1 +4 +8);
  break;
 case Rwalk:
  n += 2;
  n += f->nwqid*(1 +4 +8);
  break;
 case Ropen:
 case Rcreate:
  n += (1 +4 +8);
  n += 4;
  break;
 case Ropenfd:
  n += (1 +4 +8);
  n += 4;
  n += 4;
  break;
 case Rread:
  n += 4;
  n += f->count;
  break;
 case Rwrite:
  n += 4;
  break;
 case Rclunk:
  break;
 case Rremove:
  break;
 case Rstat:
  n += 2;
  n += f->nstat;
  break;
 case Rwstat:
  break;
 }
 return n;
}

canonical_original_code:
unsigned int sizeS2M(struct Fcall *f)
{
 unsigned int n;
 int i;
 n = 0;
 n += 4;
 n += 1;
 n += 2;
 switch(f->type)
 {
 default:
  return 0;
 case Tversion:
  n += 4;
  n += stringsz(f->version);
  break;
 case Tflush:
  n += 2;
  break;
 case Tauth:
  n += 4;
  n += stringsz(f->uname);
  n += stringsz(f->aname);
  break;
 case Tattach:
  n += 4;
  n += 4;
  n += stringsz(f->uname);
  n += stringsz(f->aname);
  break;
 case Twalk:
  n += 4;
  n += 4;
  n += 2;
  for(i=0; i<f->nwname; i++)
   n += stringsz(f->wname[i]);
  break;
 case Topen:
 case Topenfd:
  n += 4;
  n += 1;
  break;
 case Tcreate:
  n += 4;
  n += stringsz(f->name);
  n += 4;
  n += 1;
  break;
 case Tread:
  n += 4;
  n += 8;
  n += 4;
  break;
 case Twrite:
  n += 4;
  n += 8;
  n += 4;
  n += f->count;
  break;
 case Tclunk:
 case Tremove:
  n += 4;
  break;
 case Tstat:
  n += 4;
  break;
 case Twstat:
  n += 4;
  n += 2;
  n += f->nstat;
  break;
 case Rversion:
  n += 4;
  n += stringsz(f->version);
  break;
 case Rerror:
  n += stringsz(f->ename);
  break;
 case Rflush:
  break;
 case Rauth:
  n += (1 +4 +8);
  break;
 case Rattach:
  n += (1 +4 +8);
  break;
 case Rwalk:
  n += 2;
  n += f->nwqid*(1 +4 +8);
  break;
 case Ropen:
 case Rcreate:
  n += (1 +4 +8);
  n += 4;
  break;
 case Ropenfd:
  n += (1 +4 +8);
  n += 4;
  n += 4;
  break;
 case Rread:
  n += 4;
  n += f->count;
  break;
 case Rwrite:
  n += 4;
  break;
 case Rclunk:
  break;
 case Rremove:
  break;
 case Rstat:
  n += 2;
  n += f->nstat;
  break;
 case Rwstat:
  break;
 }
 return n;
}

variable_types: struct Fcall { unsigned char type; unsigned int fid; unsigned short tag; unsigned int msize; char *version; unsigned short oldtag; char *ename; struct Qid { unsigned long long path; unsigned long vers; unsigned char type; } qid; unsigned int iounit; struct Qid aqid; unsigned int afid; char *uname; char *aname; unsigned int perm; char *name; unsigned char mode; unsigned int newfid; unsigned short nwname; char *wname[16]; unsigned short nwqid; struct Qid wqid[16]; long long offset; unsigned int count; char *data; unsigned short nstat; unsigned char *stat; int unixfd; int errornum; int uidnum; char *extension; } *

variable_types:
n
unsigned int

user_defined_types: struct Fcall { unsigned char type; unsigned int fid; unsigned short tag; unsigned int msize; char *version; unsigned short oldtag; char *ename; struct Qid qid; unsigned int iounit; struct Qid aqid; unsigned int afid; char *uname; char *aname; unsigned int perm; char *name; unsigned char mode; unsigned int newfid; unsigned short nwname; char *wname[16]; unsigned short nwqid; struct Qid wqid[16]; long long offset; unsigned int count; char *data; unsigned short nstat; unsigned char *stat; int unixfd; int errornum; int uidnum; char *extension; }


variable_types:
ap
unsigned char *

variable_types:
nap
unsigned int

variable_types:
i
unsigned int

variable_types:
size
unsigned int

user_defined_types: struct Qid { unsigned long long path; unsigned long vers; unsigned char type; }


vsnprint

original_code:
int
vsnprint(char *buf, int len, char *fmt, va_list args)
{
 Fmt f;
 if(len <= 0)
  return -1;
 f.runes = 0;
 f.start = buf;
 f.to = buf;
 f.stop = buf + len - 1;
 f.flush = 0;
 f.farg = 0;
 f.nfmt = 0;
 __builtin_va_copy(f.args,args);
 fmtlocaleinit(&f, 0, 0, 0);
 dofmt(&f, fmt);
 __builtin_va_end(f.args);
 *(char*)f.to = '\0';
 return (char*)f.to - buf;
}

canonical_original_code:
int vsnprint(char *buf, int len, char *fmt, __builtin_va_list args)
{
 struct Fmt f;
 if(len <= 0)
  return -1;
 f.runes = 0;
 f.start = buf;
 f.to = buf;
 f.stop = buf + len - 1;
 f.flush = 0;
 f.farg = 0;
 f.nfmt = 0;
 __builtin_va_copy(f.args,args);
 fmtlocaleinit(&f, 0, 0, 0);
 dofmt(&f, fmt);
 __builtin_va_end(f.args);
 *(char *)f.to = '\0';
 return (char *)f.to - buf;
}

variable_types:
buf
char *

variable_types: len int

variable_types:
fmt
char *

variable_types: struct Fmt { unsigned char runes; void *start; void *to; void *stop; int (*flush)(struct Fmt *); void *farg; int nfmt; __builtin_va_list args; unsigned int r; int width; int prec; unsigned long flags; char *decimal; char *thousands; char *grouping; }


gstring

decompiled_code:
unsigned __int64 __fastcall gstring(unsigned __int16 *a1, unsigned __int64 a2, _QWORD *a3)
{
unsigned __int64 v4; // r13
size_t v5; // rbp

if ( a2 < (unsigned __int64)(a1 + 1) )
return 0LL;
v4 = (unsigned __int64)a1 + (unsigned int)*a1 + 2;
if ( v4 > a2 )
return 0LL;
v5 = *a1;
memmove((char *)a1 + 1, a1 + 1, v5);
*((_BYTE *)a1 + v5 + 1) = 0;
*a3 = (char *)a1 + 1;
return v4;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int16 *a1, unsigned __int64 a2, _QWORD *a3)
{
unsigned __int64 v4; // r13
size_t v5; // rbp

if ( a2 < (unsigned __int64)(a1 + 1) )
return 0LL;
v4 = (unsigned __int64)a1 + (unsigned int)*a1 + 2;
if ( v4 > a2 )
return 0LL;
v5 = *a1;
memmove((char *)a1 + 1, a1 + 1, v5);
*((_BYTE *)a1 + v5 + 1) = 0;
*a3 = (char *)a1 + 1;
return v4;
}

original_code:
static
uchar*
gstring(uchar *p, uchar *ep, char **s)
{
 uint n;
 if(p+2 > ep)
  return ((void*)0);
 n = ((p)[0]|((p)[1]<<8));
 p += 2 - 1;
 if(p+n+1 > ep)
  return ((void*)0);
 memmove(p, p + 1, n);
 p[n] = '\0';
 *s = (char*)p;
 p += n+1;
 return p;
}

canonical_original_code:
static unsigned char *gstring(unsigned char *p, unsigned char *ep, char **s)
{
 unsigned int n;
 if(p+2 > ep)
  return ((void *)0);
 n = ((p)[0]|((p)[1]<<8));
 p += 2 - 1;
 if(p+n+1 > ep)
  return ((void *)0);
 memmove(p, p + 1, n);
 p[n] = '\0';
 *s = (char *)p;
 p += n+1;
 return p;
}

variable_types:
ep
unsigned char *

variable_types:
s
char **


countde

original_code:
static int
countde(char *p, int n)
{
 char *e;
 int m;
 struct dirent *de;
 e = p+n;
 m = 0;
 while(p < e){
  de = (struct dirent*)p;
  if(de->d_reclen <= 4+2+2+1 || p+de->d_reclen > e)
   break;
  if(de->d_name[0]=='.' && de->d_name[1]==0)
   de->d_name[0] = 0;
  else if(de->d_name[0]=='.' && de->d_name[1]=='.' && de->d_name[2]==0)
   de->d_name[0] = 0;
  m++;
  p += de->d_reclen;
 }
 return m;
}

canonical_original_code:
static int countde(char *p, int n)
{
 char *e;
 int m;
 struct dirent *de;
 e = p+n;
 m = 0;
 while(p < e){
  de = (struct dirent *)p;
  if(de->d_reclen <= 4+2+2+1 || p+de->d_reclen > e)
   break;
  if(de->d_name[0]=='.' && de->d_name[1]==0)
   de->d_name[0] = 0;
  else if(de->d_name[0]=='.' && de->d_name[1]=='.' && de->d_name[2]==0)
   de->d_name[0] = 0;
  m++;
  p += de->d_reclen;
 }
 return m;
}

variable_types:
p
char *

variable_types: n int

variable_types:
e
char *

variable_types: m int

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *


mygetdents

decompiled_code:
__ssize_t __fastcall mygetdents(__int64 fd, char *buf, int a3)
{
__off64_t basep[4]; // [rsp+8h] [rbp-20h] BYREF

basep[0] = p9seek(fd, 0LL, 1LL);
return getdirentries64(fd, buf, a3, basep);
}

canonical_decompiled_code:
__ssize_t __fastcall func1(__int64 fd, char *buf, int a3)
{
__off64_t basep[4]; // [rsp+8h] [rbp-20h] BYREF

basep[0] = p9seek(fd, 0LL, 1LL);
return getdirentries64(fd, buf, a3, basep);
}

original_code:
static int
mygetdents(int fd, struct dirent *buf, int n)
{
 off_t off;
 int nn;
 off = p9seek(fd, 0, 1);
 nn = getdirentries(fd, (void*)buf, n, &off);
 return nn;
}

canonical_original_code:
static int mygetdents(int fd, struct dirent *buf, int n)
{
 long off;
 int nn;
 off = p9seek(fd, 0, 1);
 nn = getdirentries(fd, (void *)buf, n, &off);
 return nn;
}

variable_types: fd int

variable_types: off long

variable_types: nn int

user_defined_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; }


dirpackage

original_code:
static int
dirpackage(int fd, char *buf, int n, Dir **dp)
{
 int oldwd;
 char *p, *str, *estr;
 int i, nstr, m;
 struct dirent *de;
 struct stat st, lst;
 Dir *d;
 n = countde(buf, n);
 if(n <= 0)
  return n;
 if((oldwd = open(".", 00)) < 0)
  return -1;
 if(fchdir(fd) < 0)
  return -1;
 p = buf;
 nstr = 0;
 for(i=0; i<n; i++){
  de = (struct dirent*)p;
  memset(&lst, 0, sizeof lst);
  if(de->d_name[0] == 0)
                 {}
  else if(lstat(de->d_name, &lst) < 0)
   de->d_name[0] = 0;
  else{
   st = lst;
   if(((((lst.st_mode)) & 0170000) == (0120000)))
    stat(de->d_name, &st);
   nstr += _p9dir(&lst, &st, de->d_name, ((void*)0), ((void*)0), ((void*)0));
  }
  p += de->d_reclen;
 }
 d = malloc(sizeof(Dir)*n+nstr);
 if(d == ((void*)0)){
  fchdir(oldwd);
  close(oldwd);
  return -1;
 }
 str = (char*)&d[n];
 estr = str+nstr;
 p = buf;
 m = 0;
 for(i=0; i<n; i++){
  de = (struct dirent*)p;
  if(de->d_name[0] != 0 && lstat(de->d_name, &lst) >= 0){
   st = lst;
   if((lst.st_mode&0170000) == 0120000)
    stat(de->d_name, &st);
   _p9dir(&lst, &st, de->d_name, &d[m++], &str, estr);
  }
  p += de->d_reclen;
 }
 fchdir(oldwd);
 close(oldwd);
 *dp = d;
 return m;
}

canonical_original_code:
static int dirpackage(int fd, char *buf, int n, struct Dir **dp)
{
 int oldwd;
 char *p; char *str; char *estr;
 int i; int nstr; int m;
 struct dirent *de;
 struct stat st; struct stat lst;
 struct Dir *d;
 n = countde(buf, n);
 if(n <= 0)
  return n;
 if((oldwd = open(".", 00)) < 0)
  return -1;
 if(fchdir(fd) < 0)
  return -1;
 p = buf;
 nstr = 0;
 for(i=0; i<n; i++){
  de = (struct dirent *)p;
  memset(&lst, 0, sizeof lst);
  if(de->d_name[0] == 0)
                 {}
  else if(lstat(de->d_name, &lst) < 0)
   de->d_name[0] = 0;
  else{
   st = lst;
   if(((((lst.st_mode)) & 0170000) == (0120000)))
    stat(de->d_name, &st);
   nstr += _p9dir(&lst, &st, de->d_name, ((void *)0), ((void *)0), ((void *)0));
  }
  p += de->d_reclen;
 }
 d = malloc(sizeof(Dir)*n+nstr);
 if(d == ((void *)0)){
  fchdir(oldwd);
  close(oldwd);
  return -1;
 }
 str = (char *)&d[n];
 estr = str+nstr;
 p = buf;
 m = 0;
 for(i=0; i<n; i++){
  de = (struct dirent *)p;
  if(de->d_name[0] != 0 && lstat(de->d_name, &lst) >= 0){
   st = lst;
   if((lst.st_mode&0170000) == 0120000)
    stat(de->d_name, &st);
   _p9dir(&lst, &st, de->d_name, &d[m++], &str, estr);
  }
  p += de->d_reclen;
 }
 fchdir(oldwd);
 close(oldwd);
 *dp = d;
 return m;
}

variable_types: struct Dir { unsigned short type; unsigned int dev; struct Qid { unsigned long long path; unsigned long vers; unsigned char type; } qid; unsigned long mode; unsigned long atime; unsigned long mtime; long long length; char *name; char *uid; char *gid; char *muid; unsigned int uidnum; unsigned int gidnum; unsigned int muidnum; char *ext; } **

variable_types: oldwd int

variable_types:
str
char *

variable_types:
estr
char *

variable_types: i int

variable_types: nstr int

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; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

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; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct Dir { unsigned short type; unsigned int dev; struct Qid qid; unsigned long mode; unsigned long atime; unsigned long mtime; long long length; char *name; char *uid; char *gid; char *muid; unsigned int uidnum; unsigned int gidnum; unsigned int muidnum; char *ext; }


dirread

decompiled_code:
__int64 __fastcall dirread(int fd, _QWORD *a2)
{
int v2; // er13
char *v3; // rax
char *v4; // rbx
int v5; // edx
int v6; // ebp
__int64 result; // rax
struct stat64 v8; // [rsp+0h] [rbp-B8h] BYREF

*a2 = 0LL;
if ( fstat64(fd, &v8) < 0 )
return -1LL;
if ( v8.st_blksize <= 0x1FFF )
v8.st_blksize = 0x2000LL;
v2 = v8.st_blksize;
v3 = (char *)malloc(v8.st_blksize);
v4 = v3;
if ( !v3 )
return -1LL;
v5 = mygetdents((unsigned int)fd, v3, v2);
if ( v5 < 0 )
{
free(v4);
result = -1LL;
}
else
{
v6 = dirpackage(fd, (unsigned __int64)v4, v5, a2);
free(v4);
result = v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, _QWORD *a2)
{
int v2; // er13
char *v3; // rax
char *v4; // rbx
int v5; // edx
int v6; // ebp
__int64 result; // rax
struct stat64 v8; // [rsp+0h] [rbp-B8h] BYREF

*a2 = 0LL;
if ( fstat64(fd, &v8) < 0 )
return -1LL;
if ( v8.st_blksize <= 0x1FFF )
v8.st_blksize = 0x2000LL;
v2 = v8.st_blksize;
v3 = (char *)malloc(v8.st_blksize);
v4 = v3;
if ( !v3 )
return -1LL;
v5 = func1((unsigned int)fd, v3, v2);
if ( v5 < 0 )
{
free(v4);
result = -1LL;
}
else
{
v6 = func2(fd, (unsigned __int64)v4, v5, a2);
free(v4);
result = v6;
}
return result;
}

original_code:
long
dirread(int fd, Dir **dp)
{
 char *buf;
 struct stat st;
 int n;
 *dp = 0;
 if(fstat(fd, &st) < 0)
  return -1;
 if(st.st_blksize < 8192)
  st.st_blksize = 8192;
 buf = malloc(st.st_blksize);
 if(buf == ((void*)0))
  return -1;
 n = mygetdents(fd, (void*)buf, st.st_blksize);
 if(n < 0){
  free(buf);
  return -1;
 }
 n = dirpackage(fd, buf, n, dp);
 free(buf);
 return n;
}

canonical_original_code:
long dirread(int fd, struct Dir **dp)
{
 char *buf;
 struct stat st;
 int n;
 *dp = 0;
 if(fstat(fd, &st) < 0)
  return -1;
 if(st.st_blksize < 8192)
  st.st_blksize = 8192;
 buf = malloc(st.st_blksize);
 if(buf == ((void *)0))
  return -1;
 n = mygetdents(fd, (void *)buf, st.st_blksize);
 if(n < 0){
  free(buf);
  return -1;
 }
 n = dirpackage(fd, buf, n, dp);
 free(buf);
 return n;
}

variable_types:
buf
char *

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


dirreadall

decompiled_code:
__int64 __fastcall dirreadall(int fd, _QWORD *a2)
{
__int64 v2; // r12
void *i; // r15
int v4; // ebp
void *v5; // rax
void *v6; // rbx
int v7; // eax
__int64 v8; // rbp
struct stat64 v10; // [rsp+0h] [rbp-C8h] BYREF

if ( fstat64(fd, &v10) < 0 )
return -1LL;
if ( v10.st_blksize <= 0x1FFF )
v10.st_blksize = 0x2000LL;
v2 = 0LL;
for ( i = 0LL; ; i = v6 )
{
v4 = v10.st_blksize;
v5 = realloc(i, v10.st_blksize + v2);
v6 = v5;
if ( !v5 )
{
free(i);
return -1LL;
}
v7 = mygetdents((unsigned int)fd, (char *)v5 + v2, v4);
v8 = v7;
if ( v7 <= 0 )
break;
v2 += v7;
}
if ( v2 >= 0 )
{
v2 = (int)dirpackage(fd, (unsigned __int64)v6, v2, a2);
free(v6);
if ( !v2 && v8 < 0 )
v2 = -1LL;
}
else
{
free(v6);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, _QWORD *a2)
{
__int64 v2; // r12
void *i; // r15
int v4; // ebp
void *v5; // rax
void *v6; // rbx
int v7; // eax
__int64 v8; // rbp
struct stat64 v10; // [rsp+0h] [rbp-C8h] BYREF

if ( fstat64(fd, &v10) < 0 )
return -1LL;
if ( v10.st_blksize <= 0x1FFF )
v10.st_blksize = 0x2000LL;
v2 = 0LL;
for ( i = 0LL; ; i = v6 )
{
v4 = v10.st_blksize;
v5 = realloc(i, v10.st_blksize + v2);
v6 = v5;
if ( !v5 )
{
free(i);
return -1LL;
}
v7 = func1((unsigned int)fd, (char *)v5 + v2, v4);
v8 = v7;
if ( v7 <= 0 )
break;
v2 += v7;
}
if ( v2 >= 0 )
{
v2 = (int)func2(fd, (unsigned __int64)v6, v2, a2);
free(v6);
if ( !v2 && v8 < 0 )
v2 = -1LL;
}
else
{
free(v6);
}
return v2;
}

original_code:
long
dirreadall(int fd, Dir **d)
{
 uchar *buf, *nbuf;
 long n, ts;
 struct stat st;
 if(fstat(fd, &st) < 0)
  return -1;
 if(st.st_blksize < 8192)
  st.st_blksize = 8192;
 buf = ((void*)0);
 ts = 0;
 for(;;){
  nbuf = realloc(buf, ts+st.st_blksize);
  if(nbuf == ((void*)0)){
   free(buf);
   return -1;
  }
  buf = nbuf;
  n = mygetdents(fd, (void*)(buf+ts), st.st_blksize);
  if(n <= 0)
   break;
  ts += n;
 }
 if(ts >= 0)
  ts = dirpackage(fd, (char*)buf, ts, d);
 free(buf);
 if(ts == 0 && n < 0)
  return -1;
 return ts;
}

canonical_original_code:
long dirreadall(int fd, struct Dir **d)
{
 unsigned char *buf; unsigned char *nbuf;
 long n; long ts;
 struct stat st;
 if(fstat(fd, &st) < 0)
  return -1;
 if(st.st_blksize < 8192)
  st.st_blksize = 8192;
 buf = ((void *)0);
 ts = 0;
 for(;;){
  nbuf = realloc(buf, ts+st.st_blksize);
  if(nbuf == ((void *)0)){
   free(buf);
   return -1;
  }
  buf = nbuf;
  n = mygetdents(fd, (void *)(buf+ts), st.st_blksize);
  if(n <= 0)
   break;
  ts += n;
 }
 if(ts >= 0)
  ts = dirpackage(fd, (char *)buf, ts, d);
 free(buf);
 if(ts == 0 && n < 0)
  return -1;
 return ts;
}

variable_types:
buf
unsigned char *

variable_types:
nbuf
unsigned char *

variable_types: n long

variable_types: ts long


isme

decompiled_code:
_BOOL8 __fastcall isme(char *s2)
{
int v1; // ebp
const char *v2; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v1 = strtol(s2, &endptr, 10);
if ( !*endptr && endptr > s2 )
return getuid() == v1;
v2 = (const char *)getuser();
return strcmp(v2, s2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(char *s2)
{
int v1; // ebp
const char *v2; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v1 = strtol(s2, &endptr, 10);
if ( !*endptr && endptr > s2 )
return getuid() == v1;
v2 = (const char *)getuser();
return strcmp(v2, s2) == 0;
}

original_code:
static int
isme(char *uid)
{
 int n;
 char *p;
 n = strtol(uid, &p, 10);
 if(*p == 0 && p > uid)
  return n == getuid();
 return strcmp(getuser(), uid) == 0;
}

variable_types:
uid
char *

variable_types: n int

variable_types:
p
char *


nsfromdisplay

decompiled_code:
const char *nsfromdisplay()
{
const char *v0; // rax
const char *v1; // rbp
char *v2; // rax
const char *v3; // rbx
const unsigned __int16 *v4; // rdx
const char *v5; // rbx
char v6; // dl
char *v7; // rax
const char *v8; // rax
int v9; // eax
__int64 v10; // rax
__int64 v11; // rbp

v0 = (const char *)p9getenv("DISPLAY");
v1 = v0;
if ( v0 )
{
v2 = strrchr(v0, 58);
if ( v2 )
{
v3 = v2 + 1;
v4 = *_ctype_b_loc();
while ( (v4[*(unsigned __int8 *)v3] & 0x800) != 0 )
++v3;
if ( !strcmp(v3, ".0") )
*v3 = 0;
}
v6 = *v1;
if ( *v1 )
{
v7 = (char *)v1;
do
{
if ( v6 == 47 )
*v7 = 95;
v6 = *++v7;
}
while ( *v7 );
}
v8 = (const char *)getuser();
v5 = (const char *)smprint("/tmp/ns.%s.%s", v8, v1);
p9free(v1);
if ( v5 )
{
v9 = p9create(v5, 0LL, 2147484096LL);
if ( v9 >= 0 )
{
close(v9);
}
else
{
v10 = dirstat(v5);
v11 = v10;
if ( v10 )
{
if ( (*(_QWORD *)(v10 + 32) & 0x1FFLL) == 448 && isme(*(char **)(v10 + 72)) )
{
p9free(v11);
}
else
{
werrstr("bad name space dir %s", v5);
p9free(v5);
p9free(v11);
v5 = 0LL;
}
}
else
{
p9free(0LL);
werrstr("stat %s: %r", v5);
p9free(v5);
v5 = 0LL;
}
}
}
else
{
werrstr("out of memory");
}
}
else
{
werrstr("$DISPLAY not set");
v5 = 0LL;
}
return v5;
}

canonical_decompiled_code:
const char *func1()
{
const char *v0; // rax
const char *v1; // rbp
char *v2; // rax
const char *v3; // rbx
const unsigned __int16 *v4; // rdx
const char *v5; // rbx
char v6; // dl
char *v7; // rax
const char *v8; // rax
int v9; // eax
__int64 v10; // rax
__int64 v11; // rbp

v0 = (const char *)p9getenv("DISPLAY");
v1 = v0;
if ( v0 )
{
v2 = strrchr(v0, 58);
if ( v2 )
{
v3 = v2 + 1;
v4 = *_ctype_b_loc();
while ( (v4[*(unsigned __int8 *)v3] & 0x800) != 0 )
++v3;
if ( !strcmp(v3, ".0") )
*v3 = 0;
}
v6 = *v1;
if ( *v1 )
{
v7 = (char *)v1;
do
{
if ( v6 == 47 )
*v7 = 95;
v6 = *++v7;
}
while ( *v7 );
}
v8 = (const char *)getuser();
v5 = (const char *)smprint("/tmp/ns.%s.%s", v8, v1);
p9free(v1);
if ( v5 )
{
v9 = p9create(v5, 0LL, 2147484096LL);
if ( v9 >= 0 )
{
close(v9);
}
else
{
v10 = dirstat(v5);
v11 = v10;
if ( v10 )
{
if ( (*(_QWORD *)(v10 + 32) & 0x1FFLL) == 448 && func0(*(char **)(v10 + 72)) )
{
p9free(v11);
}
else
{
werrstr("bad name space dir %s", v5);
p9free(v5);
p9free(v11);
v5 = 0LL;
}
}
else
{
p9free(0LL);
werrstr("stat %s: %r", v5);
p9free(v5);
v5 = 0LL;
}
}
}
else
{
werrstr("out of memory");
}
}
else
{
werrstr("$DISPLAY not set");
v5 = 0LL;
}
return v5;
}

original_code:
static char*
nsfromdisplay(void)
{
 int fd;
 Dir *d;
 char *disp, *p;
 if((disp = p9getenv("DISPLAY")) == ((void*)0)){
  werrstr("$DISPLAY not set");
  return ((void*)0);
 }
 p = strrchr(disp, ':');
 if(p){
  p++;
  while(((*__ctype_b_loc ())[(int) (((uchar)*p))] & (unsigned short int) _ISdigit))
   p++;
  if(strcmp(p, ".0") == 0)
   *p = 0;
 }
 for(p=disp; *p; p++)
  if(*p == '/')
   *p = '_';
 p = smprint("/tmp/ns.%s.%s", getuser(), disp);
 p9free(disp);
 if(p == ((void*)0)){
  werrstr("out of memory");
  return p;
 }
 if((fd=p9create(p, 0, 0x80000000|0700)) >= 0){
  close(fd);
  return p;
 }
 if((d = dirstat(p)) == ((void*)0)){
  p9free(d);
  werrstr("stat %s: %r", p);
  p9free(p);
  return ((void*)0);
 }
 if((d->mode&0777) != 0700 || !isme(d->uid)){
  werrstr("bad name space dir %s", p);
  p9free(p);
  p9free(d);
  return ((void*)0);
 }
 p9free(d);
 return p;
}

canonical_original_code:
static char *nsfromdisplay()
{
 int fd;
 struct Dir *d;
 char *disp; char *p;
 if((disp = p9getenv("DISPLAY")) == ((void *)0)){
  werrstr("$DISPLAY not set");
  return ((void *)0);
 }
 p = strrchr(disp, ':');
 if(p){
  p++;
  while(((*__ctype_b_loc ())[(int) (((uchar)*p))] & (unsigned short) _ISdigit))
   p++;
  if(strcmp(p, ".0") == 0)
   *p = 0;
 }
 for(p=disp; *p; p++)
  if(*p == '/')
   *p = '_';
 p = smprint("/tmp/ns.%s.%s", getuser(), disp);
 p9free(disp);
 if(p == ((void *)0)){
  werrstr("out of memory");
  return p;
 }
 if((fd=p9create(p, 0, 0x80000000|0700)) >= 0){
  close(fd);
  return p;
 }
 if((d = dirstat(p)) == ((void *)0)){
  p9free(d);
  werrstr("stat %s: %r", p);
  p9free(p);
  return ((void *)0);
 }
 if((d->mode&0777) != 0700 || !isme(d->uid)){
  werrstr("bad name space dir %s", p);
  p9free(p);
  p9free(d);
  return ((void *)0);
 }
 p9free(d);
 return p;
}

variable_types: fd int

variable_types: struct Dir { unsigned short type; unsigned int dev; struct Qid { unsigned long long path; unsigned long vers; unsigned char type; } qid; unsigned long mode; unsigned long atime; unsigned long mtime; long long length; char *name; char *uid; char *gid; char *muid; unsigned int uidnum; unsigned int gidnum; unsigned int muidnum; char *ext; } *

variable_types:
disp
char *


getns

decompiled_code:
const char *getns()
{
const char *v0; // rbx

v0 = (const char *)p9getenv("NAMESPACE");
if ( !v0 )
{
v0 = nsfromdisplay();
if ( !v0 )
werrstr("$NAMESPACE not set, %r");
}
return v0;
}

canonical_decompiled_code:
const char *func2()
{
const char *v0; // rbx

v0 = (const char *)p9getenv("NAMESPACE");
if ( !v0 )
{
v0 = func1();
if ( !v0 )
werrstr("$NAMESPACE not set, %r");
}
return v0;
}

original_code:
char*
getns(void)
{
 char *ns;
 ns = p9getenv("NAMESPACE");
 if(ns == ((void*)0))
  ns = nsfromdisplay();
 if(ns == ((void*)0)){
  werrstr("$NAMESPACE not set, %r");
  return ((void*)0);
 }
 return ns;
}

canonical_original_code:
char *getns()
{
 char *ns;
 ns = p9getenv("NAMESPACE");
 if(ns == ((void *)0))
  ns = nsfromdisplay();
 if(ns == ((void *)0)){
  werrstr("$NAMESPACE not set, %r");
  return ((void *)0);
 }
 return ns;
}

variable_types:
ns
char *


Bgetrune

decompiled_code:
__int64 __fastcall Bgetrune(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
int v3; // er12
int v5; // eax
char v6; // [rsp+7h] [rbp-21h]
char v7[4]; // [rsp+8h] [rbp-20h] BYREF
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

LODWORD(result) = ((__int64 (*)(void))Bgetc)();
if ( (int)result <= 127 )
{
*(_DWORD *)(a1 + 12) = 1;
result = (int)result;
}
else
{
v7[0] = result;
v2 = 2LL;
do
{
LODWORD(result) = Bgetc(a1);
if ( (int)result < 0 )
return (int)result;
v3 = v2;
*(&v6 + v2) = result;
}
while ( !(unsigned int)fullrune(v7, (unsigned int)v2++) );
v5 = chartorune(v8, v7);
for ( *(_DWORD *)(a1 + 12) = v5; *(_DWORD *)(a1 + 12) < v3; --v3 )
Bungetc(a1);
result = v8[0];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rbx
int v3; // er12
int v5; // eax
char v6; // [rsp+7h] [rbp-21h]
char v7[4]; // [rsp+8h] [rbp-20h] BYREF
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

LODWORD(result) = ((__int64 (*)(void))Bgetc)();
if ( (int)result <= 127 )
{
*(_DWORD *)(a1 + 12) = 1;
result = (int)result;
}
else
{
v7[0] = result;
v2 = 2LL;
do
{
LODWORD(result) = Bgetc(a1);
if ( (int)result < 0 )
return (int)result;
v3 = v2;
*(&v6 + v2) = result;
}
while ( !(unsigned int)fullrune(v7, (unsigned int)v2++) );
v5 = chartorune(v8, v7);
for ( *(_DWORD *)(a1 + 12) = v5; *(_DWORD *)(a1 + 12) < v3; --v3 )
Bungetc(a1);
result = v8[0];
}
return result;
}

original_code:
long
Bgetrune(Biobuf *bp)
{
 int c, i;
 Rune rune;
 char str[UTFmax];
 c = Bgetc(bp);
 if(c < Runeself) {
  bp->runesize = 1;
  return c;
 }
 str[0] = c;
 for(i=1;;) {
  c = Bgetc(bp);
  if(c < 0)
   return c;
  str[i++] = c;
  if(fullrune(str, i)) {
   bp->runesize = chartorune(&rune, str);
   while(i > bp->runesize) {
    Bungetc(bp);
    i--;
   }
   return rune;
  }
 }
}

canonical_original_code:
long Bgetrune(struct Biobuf *bp)
{
 int c; int i;
 unsigned int rune;
 char str[-1];
 c = Bgetc(bp);
 if(c < Runeself) {
  bp->runesize = 1;
  return c;
 }
 str[0] = c;
 for(i=1;;) {
  c = Bgetc(bp);
  if(c < 0)
   return c;
  str[i++] = c;
  if(fullrune(str, i)) {
   bp->runesize = chartorune(&rune, str);
   while(i > bp->runesize) {
    Bungetc(bp);
    i--;
   }
   return rune;
  }
 }
}

variable_types: struct Biobuf { int icount; int ocount; int rdline; int runesize; int state; int fid; int flag; long long offset; int bsize; unsigned char *bbuf; unsigned char *ebuf; unsigned char *gbuf; unsigned char b[-1]; } *

variable_types: c int

variable_types:
rune
unsigned int

variable_types:
str
char [-1]


Bungetrune

decompiled_code:
__int64 __fastcall Bungetrune(_DWORD *a1)
{
int v1; // eax
unsigned int v2; // edx

v1 = a1[4];
if ( v1 == 3 )
{
a1[4] = 1;
goto LABEL_5;
}
v2 = -1;
if ( v1 == 1 )
{
LABEL_5:
*a1 -= a1[3];
a1[3] = 0;
v2 = 1;
}
return v2;
}

original_code:
int
Bungetrune(Biobuf *bp)
{
 if(bp->state == Bracteof)
  bp->state = Bractive;
 if(bp->state != Bractive)
  return Beof;
 bp->icount -= bp->runesize;
 bp->runesize = 0;
 return 1;
}

canonical_original_code:
int Bungetrune(struct Biobuf *bp)
{
 if(bp->state == Bracteof)
  bp->state = Bractive;
 if(bp->state != Bractive)
  return Beof;
 bp->icount -= bp->runesize;
 bp->runesize = 0;
 return 1;
}

user_defined_types: struct Biobuf { int icount; int ocount; int rdline; int runesize; int state; int fid; int flag; long long offset; int bsize; unsigned char *bbuf; unsigned char *ebuf; unsigned char *gbuf; unsigned char b[-1]; }


searchpath

decompiled_code:
char *__fastcall searchpath(char *src)
{
char *v1; // rbp
char v3; // al
const char *v4; // rbx
const char *v5; // r13
char *v6; // rax
int v7; // er14
int v8; // er14
__int64 v9; // rax
int v10; // [rsp+4h] [rbp-44h]
__int64 v11; // [rsp+8h] [rbp-40h]

if ( *src == 46 )
{
v3 = src[1];
if ( v3 == 47 || v3 == 46 && src[2] == 47 )
{
LABEL_3:
v1 = 0LL;
if ( access(src, 1) >= 0 )
v1 = (char *)p9strdup(src);
return v1;
}
}
else if ( *src == 47 )
{
goto LABEL_3;
}
v11 = p9getenv("PATH");
v4 = (const char *)v11;
v10 = 0;
v1 = 0LL;
if ( v11 )
{
while ( 1 )
{
if ( !*v4 )
goto LABEL_10;
v6 = strchr(v4, 58);
if ( v6 )
{
v5 = v6 + 1;
*v6 = 0;
if ( !*v4 )
{
if ( access(src, 1) >= 0 )
{
p9free(v1);
p9free(v11);
return (char *)p9strdup(src);
}
goto LABEL_19;
}
}
else
{
v5 = 0LL;
}
v7 = strlen(v4);
v8 = v7 + strlen(src) + 2;
if ( v8 > v10 )
{
v9 = p9realloc(v1, v8);
if ( !v9 )
goto LABEL_10;
v10 = v8;
v1 = (char *)v9;
}
strcpy(v1, v4);
*(_WORD *)&v1[strlen(v1)] = 47;
strcat(v1, src);
if ( access(v1, 1) >= 0 )
{
p9free(v11);
return v1;
}
if ( !v5 )
goto LABEL_10;
LABEL_19:
v4 = v5;
}
}
v1 = 0LL;
LABEL_10:
p9free(v1);
p9free(v11);
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func0(char *src)
{
char *v1; // rbp
char v3; // al
const char *v4; // rbx
const char *v5; // r13
char *v6; // rax
int v7; // er14
int v8; // er14
__int64 v9; // rax
int v10; // [rsp+4h] [rbp-44h]
__int64 v11; // [rsp+8h] [rbp-40h]

if ( *src == 46 )
{
v3 = src[1];
if ( v3 == 47 || v3 == 46 && src[2] == 47 )
{
LABEL_3:
v1 = 0LL;
if ( access(src, 1) >= 0 )
v1 = (char *)p9strdup(src);
return v1;
}
}
else if ( *src == 47 )
{
goto LABEL_3;
}
v11 = p9getenv("PATH");
v4 = (const char *)v11;
v10 = 0;
v1 = 0LL;
if ( v11 )
{
while ( 1 )
{
if ( !*v4 )
goto LABEL_10;
v6 = strchr(v4, 58);
if ( v6 )
{
v5 = v6 + 1;
*v6 = 0;
if ( !*v4 )
{
if ( access(src, 1) >= 0 )
{
p9free(v1);
p9free(v11);
return (char *)p9strdup(src);
}
goto LABEL_19;
}
}
else
{
v5 = 0LL;
}
v7 = strlen(v4);
v8 = v7 + strlen(src) + 2;
if ( v8 > v10 )
{
v9 = p9realloc(v1, v8);
if ( !v9 )
goto LABEL_10;
v10 = v8;
v1 = (char *)v9;
}
strcpy(v1, v4);
*(_WORD *)&v1[strlen(v1)] = 47;
strcat(v1, src);
if ( access(v1, 1) >= 0 )
{
p9free(v11);
return v1;
}
if ( !v5 )
goto LABEL_10;
LABEL_19:
v4 = v5;
}
}
v1 = 0LL;
LABEL_10:
p9free(v1);
p9free(v11);
return 0LL;
}

original_code:
char*
searchpath(char *name)
{
 char *path, *p, *next;
 char *s, *ss;
 int ns, l;
 s = ((void*)0);
 ns = 0;
 if((name[0] == '.' && name[1] == '/')
 || (name[0] == '.' && name[1] == '.' && name[2] == '/')
 || (name[0] == '/')){
  if(access(name, 1) >= 0)
   return p9strdup(name);
  return ((void*)0);
 }
 path = p9getenv("PATH");
 for(p=path; p && *p; p=next){
  if((next = strchr(p, ':')) != ((void*)0))
   *next++ = 0;
  if(*p == 0){
   if(access(name, 1) >= 0){
    p9free(s);
    p9free(path);
    return p9strdup(name);
   }
  }else{
   l = strlen(p)+1+strlen(name)+1;
   if(l > ns){
    ss = p9realloc(s, l);
    if(ss == ((void*)0)){
     p9free(s);
     p9free(path);
     return ((void*)0);
    }
    s = ss;
    ns = l;
   }
   strcpy(s, p);
   strcat(s, "/");
   strcat(s, name);
   if(access(s, 1) >= 0){
    p9free(path);
    return s;
   }
  }
 }
 p9free(s);
 p9free(path);
 return ((void*)0);
}

canonical_original_code:
char *searchpath(char *name)
{
 char *path; char *p; char *next;
 char *s; char *ss;
 int ns; int l;
 s = ((void *)0);
 ns = 0;
 if((name[0] == '.' && name[1] == '/')
 || (name[0] == '.' && name[1] == '.' && name[2] == '/')
 || (name[0] == '/')){
  if(access(name, 1) >= 0)
   return p9strdup(name);
  return ((void *)0);
 }
 path = p9getenv("PATH");
 for(p=path; p && *p; p=next){
  if((next = strchr(p, ':')) != ((void *)0))
   *next++ = 0;
  if(*p == 0){
   if(access(name, 1) >= 0){
    p9free(s);
    p9free(path);
    return p9strdup(name);
   }
  }else{
   l = strlen(p)+1+strlen(name)+1;
   if(l > ns){
    ss = p9realloc(s, l);
    if(ss == ((void *)0)){
     p9free(s);
     p9free(path);
     return ((void *)0);
    }
    s = ss;
    ns = l;
   }
   strcpy(s, p);
   strcat(s, "/");
   strcat(s, name);
   if(access(s, 1) >= 0){
    p9free(path);
    return s;
   }
  }
 }
 p9free(s);
 p9free(path);
 return ((void *)0);
}

variable_types:
name
char *

variable_types:
path
char *

variable_types:
next
char *

variable_types:
s
char *

variable_types:
ss
char *

variable_types: ns int

variable_types: l int