lerChaveValor

decompiled_code:
__int64 __fastcall lerChaveValor(int a1)
{
int v1; // er12
__int64 v2; // rbx
char *v3; // rbp
char *i; // r14
__int64 v5; // rsi
int v6; // eax
char *j; // rdi
char *v8; // r12
char *v9; // rax

v1 = 0;
v2 = 100LL;
v3 = (char *)myMalloc(50LL);
for ( i = v3; ; i = &v3[v1] )
{
v6 = read(*(_DWORD *)(gestor + 28LL * a1 + 8), i, 0x32uLL);
if ( !v6 )
break;
if ( v6 < 0 )
{
*(i - 1) = 0;
break;
}
v1 += v6;
v5 = v2;
v2 += 50LL;
v3 = (char *)myRealloc(v3, v5);
}
if ( !v1 )
return 0xFFFFFFFFLL;
for ( j = v3; ; j = 0LL )
{
v8 = strtok(j, "\t");
v9 = strtok(0LL, "\n");
if ( !v8 || !v9 )
break;
listaInsere(v8);
}
free(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // er12
__int64 v2; // rbx
char *v3; // rbp
char *i; // r14
__int64 v5; // rsi
int v6; // eax
char *j; // rdi
char *v8; // r12
char *v9; // rax

v1 = 0;
v2 = 100LL;
v3 = (char *)myMalloc(50LL);
for ( i = v3; ; i = &v3[v1] )
{
v6 = read(*(_DWORD *)(gestor + 28LL * a1 + 8), i, 0x32uLL);
if ( !v6 )
break;
if ( v6 < 0 )
{
*(i - 1) = 0;
break;
}
v1 += v6;
v5 = v2;
v2 += 50LL;
v3 = (char *)myRealloc(v3, v5);
}
if ( !v1 )
return 0xFFFFFFFFLL;
for ( j = v3; ; j = 0LL )
{
v8 = strtok(j, "\t");
v9 = strtok(0LL, "\n");
if ( !v8 || !v9 )
break;
listaInsere(v8);
}
free(v3);
return 0LL;
}

original_code:
int lerChaveValor(int pos){
    const int tamanhoInicial = 50;
    int tamanho = tamanhoInicial;
    int incrementos = 1;
    char *buffer = (char*)myMalloc(sizeof(char) * tamanho);
    char *i = buffer;
    int lidos;
    int lidosTotal = 0;
    while(lidos = read(gestor[pos].pipeOut[0], i, sizeof(char)*tamanhoInicial)){
        if(lidos < 0)
            lidos = 0;
        lidosTotal += lidos;
        if( lidos <= 0){
            *(i+lidos-1) = '\0';
            break;
        }else{
            incrementos++;
            buffer = (char*)myRealloc(buffer, sizeof(char) * tamanhoInicial * incrementos);
            i = buffer + lidosTotal;
        }
    }
    if(lidosTotal == 0) return -1;
    char *chave, *valor;
    chave = strtok(buffer, "\t");
    valor = strtok(((void *)0), "\n");
    while( chave != ((void *)0) && valor != ((void *)0) ){
        listaInsere(chave, valor);
        chave = strtok(((void *)0), "\t");
        valor = strtok(((void *)0), "\n");
    }
    free(buffer);
    return 0;
}

canonical_original_code:
int lerChaveValor(int pos){
    int tamanhoInicial = 50;
    int tamanho = tamanhoInicial;
    int incrementos = 1;
    char *buffer = (char*)myMalloc(sizeof(char) * tamanho);
    char *i = buffer;
    int lidos;
    int lidosTotal = 0;
    while(lidos = read(gestor[pos].pipeOut[0], i, sizeof(char)*tamanhoInicial)){
        if(lidos < 0)
            lidos = 0;
        lidosTotal += lidos;
        if( lidos <= 0){
            *(i+lidos-1) = '\0';
            break;
        }else{
            incrementos++;
            buffer = (char *)myRealloc(buffer, sizeof(char) * tamanhoInicial * incrementos);
            i = buffer + lidosTotal;
        }
    }
    if(lidosTotal == 0) return -1;
    char *chave; char *valor;
    chave = strtok(buffer, "\t");
    valor = strtok(((void *)0), "\n");
    while( chave != ((void *)0) && valor != ((void *)0) ){
        listaInsere(chave, valor);
        chave = strtok(((void *)0), "\t");
        valor = strtok(((void *)0), "\n");
    }
    free(buffer);
    return 0;
}

variable_types: pos int

variable_types: tamanhoInicial int

variable_types: tamanho int

variable_types: incrementos int

variable_types:
buffer
char *

variable_types:
i
char *

variable_types: lidos int

variable_types: lidosTotal int


esperaPorPipeMap

repo: chalkos/ProjetoSO

decompiled_code:
__int64 esperaPorPipeMap()
{
int v0; // ebx
int v1; // edi
__pid_t v2; // eax
__pid_t v3; // ebx
unsigned int v4; // er12
__int64 v5; // rax
unsigned int v6; // edx
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = 0;
while ( MAXCOM > v0 )
{
if ( zombieFound )
break;
v1 = v0++;
lerChaveValor(v1);
}
v2 = wait((__WAIT_STATUS)&stat_loc);
zombieFound = 0;
v3 = v2;
if ( v2 == -1 )
return (unsigned int)-1;
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
printErrorAndExit(&unk_107E8, "map.c", 61LL);
v4 = MAXCOM;
if ( MAXCOM <= 0 )
{
v4 = 0;
LABEL_14:
printErrorAndExit(&unk_10820, "map.c", 76LL);
return v4;
}
v5 = gestor;
v6 = 0;
while ( *(_DWORD *)(v5 + 16) != v3 )
{
++v6;
v5 += 28LL;
if ( MAXCOM == v6 )
goto LABEL_14;
}
*(_BYTE *)(v5 + 20) = 0;
return v6;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // ebx
int v1; // edi
__pid_t v2; // eax
__pid_t v3; // ebx
unsigned int v4; // er12
__int64 v5; // rax
unsigned int v6; // edx
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = 0;
while ( MAXCOM > v0 )
{
if ( zombieFound )
break;
v1 = v0++;
func0(v1);
}
v2 = wait((__WAIT_STATUS)&stat_loc);
zombieFound = 0;
v3 = v2;
if ( v2 == -1 )
return (unsigned int)-1;
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
printErrorAndExit(&unk_107E8, "map.c", 61LL);
v4 = MAXCOM;
if ( MAXCOM <= 0 )
{
v4 = 0;
LABEL_14:
printErrorAndExit(&unk_10820, "map.c", 76LL);
return v4;
}
v5 = gestor;
v6 = 0;
while ( *(_DWORD *)(v5 + 16) != v3 )
{
++v6;
v5 += 28LL;
if ( MAXCOM == v6 )
goto LABEL_14;
}
*(_BYTE *)(v5 + 20) = 0;
return v6;
}

original_code:
int esperaPorPipeMap(){
    int i=0;
    for(i=0; i<MAXCOM && !zombieFound; i++){
        lerChaveValor(i);
    }
    int resultado;
    int pid = wait(&resultado);
    zombieFound = 0;
    if(pid == -1)
        return -1;
    if(!(((resultado) & 0x7f) == 0)){
        printErrorAndExit("Uma execução do comando map terminou abruptamente.", "map.c", 61);
    }
    bool encontrou = 0;
    for(i=0; i<MAXCOM; i++){
        if(gestor[i].pid == pid){
            encontrou = 1;
            gestor[i].vivo = 0;
            break;
        }
    }
    if(!encontrou)
        printErrorAndExit("O pid do zombie encontrado não estava na lista.", "map.c", 76);
    return i;
}

canonical_original_code:
int esperaPorPipeMap(){
    int i = 0;
    for(i=0; i<MAXCOM && !zombieFound; i++){
        lerChaveValor(i);
    }
    int resultado;
    int pid = wait(&resultado);
    zombieFound = 0;
    if(pid == -1)
        return -1;
    if(!(((resultado) & 0x7f) == 0)){
        printErrorAndExit("Uma execução do comando map terminou abruptamente.", "map.c", 61);
    }
    unsigned char encontrou = 0;
    for(i=0; i<MAXCOM; i++){
        if(gestor[i].pid == pid){
            encontrou = 1;
            gestor[i].vivo = 0;
            break;
        }
    }
    if(!encontrou)
        printErrorAndExit("O pid do zombie encontrado não estava na lista.", "map.c", 76);
    return i;
}

variable_types: i int

variable_types: resultado int

variable_types: pid int

variable_types:
encontrou
unsigned char

return_type: int


lerLinha

decompiled_code:
__int64 __fastcall lerLinha(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebp
char *v4; // rdx
char v5; // al
char v6; // r9
char *v7; // rdx
int v8; // edx
int v9; // edx
int v10; // edx
int v11; // eax
char v12; // cl

result = (unsigned int)stdInBufferPos;
if ( stdInBufferPos != -1 )
{
v3 = 0;
while ( 1 )
{
LABEL_3:
if ( (int)result > 0xFFFF )
{
stdInBufferLidos = read(0, stdInBuffer, (size_t)&unk_10000);
if ( stdInBufferLidos <= 0 )
{
result = 0LL;
*(_BYTE *)(a1 + v3 - 1) = 0;
stdInBufferPos = -1;
return result;
}
stdInBufferPos = 0;
if ( a2 > v3 )
{
LABEL_5:
LODWORD(result) = stdInBufferPos;
v4 = (char *)(a1 + v3);
while ( 1 )
{
if ( (int)result > 0xFFFF || (int)result >= stdInBufferLidos )
{
v9 = 0xFFFF;
if ( (int)result <= 0xFFFF )
v9 = result;
if ( v9 < stdInBufferLidos )
goto LABEL_3;
goto LABEL_27;
}
++v3;
v5 = stdInBuffer[(int)result];
if ( v5 == 10 )
break;
*v4++ = v5;
LODWORD(result) = ++stdInBufferPos;
if ( a2 == v3 )
goto LABEL_30;
}
*v4 = 0;
++stdInBufferPos;
if ( a2 <= v3 )
{
v6 = 1;
goto LABEL_12;
}
v11 = 0xFFFF;
if ( stdInBufferPos <= 0xFFFF )
v11 = stdInBufferPos;
if ( v11 < stdInBufferLidos )
return 0LL;
LABEL_27:
stdInBufferPos = -1;
*(_BYTE *)(a1 + v3) = 0;
return 0LL;
}
}
else if ( a2 > v3 )
{
goto LABEL_5;
}
LABEL_30:
v6 = 0;
LABEL_12:
*(_BYTE *)(a1 + a2 - 1) = 0;
LODWORD(result) = stdInBufferPos;
if ( stdInBuffer[stdInBufferPos - 1] == 10 )
{
LABEL_31:
if ( stdInBufferLidos <= (int)result && stdInBufferLidos <= 0xFFFF )
goto LABEL_27;
return 0LL;
}
v7 = &stdInBuffer[stdInBufferPos];
if ( v6 )
{
v12 = stdInBuffer[stdInBufferPos - 1];
LABEL_18:
if ( v12 == 10 )
goto LABEL_31;
v8 = 0xFFFF;
if ( (int)result <= 0xFFFF )
v8 = result;
if ( stdInBufferLidos <= v8 )
goto LABEL_27;
if ( v6 )
return 0LL;
}
else
{
do
{
v12 = *v7;
LODWORD(result) = result + 1;
if ( (int)result > 0xFFFF || *v7 == 10 )
{
stdInBufferPos = result;
goto LABEL_18;
}
++v7;
}
while ( (int)result < stdInBufferLidos );
v10 = 0xFFFF;
stdInBufferPos = result;
if ( (int)result <= 0xFFFF )
v10 = result;
if ( stdInBufferLidos <= v10 )
goto LABEL_27;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // ebp
char *v4; // rdx
char v5; // al
char v6; // r9
char *v7; // rdx
int v8; // edx
int v9; // edx
int v10; // edx
int v11; // eax
char v12; // cl

result = (unsigned int)stdInBufferPos;
if ( stdInBufferPos != -1 )
{
v3 = 0;
while ( 1 )
{
LABEL_3:
if ( (int)result > 0xFFFF )
{
stdInBufferLidos = read(0, stdInBuffer, (size_t)&unk_10000);
if ( stdInBufferLidos <= 0 )
{
result = 0LL;
*(_BYTE *)(a1 + v3 - 1) = 0;
stdInBufferPos = -1;
return result;
}
stdInBufferPos = 0;
if ( a2 > v3 )
{
LABEL_5:
LODWORD(result) = stdInBufferPos;
v4 = (char *)(a1 + v3);
while ( 1 )
{
if ( (int)result > 0xFFFF || (int)result >= stdInBufferLidos )
{
v9 = 0xFFFF;
if ( (int)result <= 0xFFFF )
v9 = result;
if ( v9 < stdInBufferLidos )
goto LABEL_3;
goto LABEL_27;
}
++v3;
v5 = stdInBuffer[(int)result];
if ( v5 == 10 )
break;
*v4++ = v5;
LODWORD(result) = ++stdInBufferPos;
if ( a2 == v3 )
goto LABEL_30;
}
*v4 = 0;
++stdInBufferPos;
if ( a2 <= v3 )
{
v6 = 1;
goto LABEL_12;
}
v11 = 0xFFFF;
if ( stdInBufferPos <= 0xFFFF )
v11 = stdInBufferPos;
if ( v11 < stdInBufferLidos )
return 0LL;
LABEL_27:
stdInBufferPos = -1;
*(_BYTE *)(a1 + v3) = 0;
return 0LL;
}
}
else if ( a2 > v3 )
{
goto LABEL_5;
}
LABEL_30:
v6 = 0;
LABEL_12:
*(_BYTE *)(a1 + a2 - 1) = 0;
LODWORD(result) = stdInBufferPos;
if ( stdInBuffer[stdInBufferPos - 1] == 10 )
{
LABEL_31:
if ( stdInBufferLidos <= (int)result && stdInBufferLidos <= 0xFFFF )
goto LABEL_27;
return 0LL;
}
v7 = &stdInBuffer[stdInBufferPos];
if ( v6 )
{
v12 = stdInBuffer[stdInBufferPos - 1];
LABEL_18:
if ( v12 == 10 )
goto LABEL_31;
v8 = 0xFFFF;
if ( (int)result <= 0xFFFF )
v8 = result;
if ( stdInBufferLidos <= v8 )
goto LABEL_27;
if ( v6 )
return 0LL;
}
else
{
do
{
v12 = *v7;
LODWORD(result) = result + 1;
if ( (int)result > 0xFFFF || *v7 == 10 )
{
stdInBufferPos = result;
goto LABEL_18;
}
++v7;
}
while ( (int)result < stdInBufferLidos );
v10 = 0xFFFF;
stdInBufferPos = result;
if ( (int)result <= 0xFFFF )
v10 = result;
if ( stdInBufferLidos <= v10 )
goto LABEL_27;
}
}
}
return result;
}

original_code:
int lerLinha(char *buffer, int bufferSize){
    if(stdInBufferPos == -1)
        return -1;
    int lidosParaBuffer = 0;
    bool sair = 0;
    while(!sair){
        if(stdInBufferPos >= 65536){
            if(( stdInBufferLidos = read(0, stdInBuffer, 65536)) <= 0){
                buffer[lidosParaBuffer-1] = '\0';
                stdInBufferPos = -1;
                break;
            }
            stdInBufferPos = 0;
        }
        while( !sair && lidosParaBuffer < bufferSize && stdInBufferPos < 65536 && stdInBufferPos < stdInBufferLidos){
            if(stdInBuffer[stdInBufferPos] == '\n'){
                buffer[lidosParaBuffer++] = '\0';
                sair = 1;
            }else
                buffer[lidosParaBuffer++] = stdInBuffer[stdInBufferPos];
            stdInBufferPos++;
        }
        if(lidosParaBuffer >= bufferSize){
            buffer[bufferSize-1] = '\0';
            if(stdInBuffer[stdInBufferPos-1] == '\n')
                sair = 1;
            while(!sair && stdInBuffer[stdInBufferPos++] != '\n' && stdInBufferPos < 65536 && stdInBufferPos < stdInBufferLidos)
                ;
            if(stdInBuffer[stdInBufferPos-1] == '\n')
                sair = 1;
        }
        if(stdInBufferPos >= stdInBufferLidos && stdInBufferLidos < 65536){
            stdInBufferPos = -1;
            buffer[lidosParaBuffer] = '\0';
            break;
        }
    }
    return 0;
}

canonical_original_code:
int lerLinha(char *buffer, int bufferSize){
    if(stdInBufferPos == -1)
        return -1;
    int lidosParaBuffer = 0;
    unsigned char sair = 0;
    while(!sair){
        if(stdInBufferPos >= 65536){
            if(( stdInBufferLidos = read(0, stdInBuffer, 65536)) <= 0){
                buffer[lidosParaBuffer-1] = '\0';
                stdInBufferPos = -1;
                break;
            }
            stdInBufferPos = 0;
        }
        while( !sair && lidosParaBuffer < bufferSize && stdInBufferPos < 65536 && stdInBufferPos < stdInBufferLidos){
            if(stdInBuffer[stdInBufferPos] == '\n'){
                buffer[lidosParaBuffer++] = '\0';
                sair = 1;
            }else
                buffer[lidosParaBuffer++] = stdInBuffer[stdInBufferPos];
            stdInBufferPos++;
        }
        if(lidosParaBuffer >= bufferSize){
            buffer[bufferSize-1] = '\0';
            if(stdInBuffer[stdInBufferPos-1] == '\n')
                sair = 1;
            while(!sair && stdInBuffer[stdInBufferPos++] != '\n' && stdInBufferPos < 65536 && stdInBufferPos < stdInBufferLidos)
                ;
            if(stdInBuffer[stdInBufferPos-1] == '\n')
                sair = 1;
        }
        if(stdInBufferPos >= stdInBufferLidos && stdInBufferLidos < 65536){
            stdInBufferPos = -1;
            buffer[lidosParaBuffer] = '\0';
            break;
        }
    }
    return 0;
}

variable_types: bufferSize int

variable_types: lidosParaBuffer int

variable_types:
sair
unsigned char


map

decompiled_code:
__int64 __fastcall map(__int64 a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // r12
__int64 v4; // rbx
__int64 v5; // rsi
char **v6; // rax
int v7; // edx
_DWORD *v8; // rax
__int64 v9; // rdx
char v10; // bl
int v11; // er13
__int64 v12; // rbp
__int64 v13; // rdi
__int64 v14; // rbx
__pid_t v15; // eax
int *v16; // rbx
__int64 v17; // rbx
__int64 result; // rax
__int64 v19; // rbx
__int64 v20; // rax
_BYTE *v21; // rax
char **v22; // rax
char **argv; // [rsp+8h] [rbp-1030h] BYREF
char v24[4136]; // [rsp+10h] [rbp-1028h] BYREF

separaStrEmArgumentos(a1, &argv);
if ( *argv )
{
v1 = 1LL;
do
{
v2 = v1;
v3 = v1++;
}
while ( argv[v1 - 1] );
v4 = v1;
v5 = v2 + 2;
}
else
{
v4 = 1LL;
v5 = 2LL;
v3 = 0LL;
}
v6 = (char **)myRealloc(argv, v5);
v7 = MAXCOM;
argv = v6;
v6[v4] = 0LL;
if ( v7 > 0 )
{
v8 = (_DWORD *)(gestor + 16);
v9 = gestor + 28LL * (unsigned int)(v7 - 1) + 44;
do
{
*v8 = -1;
v8 += 7;
*((_BYTE *)v8 - 24) = 0;
}
while ( (_DWORD *)v9 != v8 );
}
v10 = 1;
v11 = 0;
listaInit();
LABEL_9:
while ( (unsigned int)lerLinha((__int64)v24, 4096) != -1 )
{
if ( !v10 )
{
v11 = esperaPorPipeMap();
if ( v11 == -1 )
printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "map.c", 121LL);
}
v12 = 28LL * v11;
v13 = v12 + gestor;
if ( !*(_BYTE *)(v12 + gestor + 20) && *(int *)(v13 + 16) > 0 )
{
lerChaveValor(v11);
close(*(_DWORD *)(gestor + v12 + 8));
v13 = v12 + gestor;
*(_DWORD *)(v12 + gestor + 16) = -1;
}
if ( pipe((int *)(v13 + 8)) == -1 )
{
if ( *_errno_location() == 24 )
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "map.c", 134LL);
sleep(0x3E8u);
}
v14 = gestor;
*(_BYTE *)(gestor + v12 + 20) = 1;
while ( 1 )
{
v15 = fork();
*(_DWORD *)(v14 + v12 + 16) = v15;
if ( v15 != -1 )
break;
v16 = _errno_location();
if ( *v16 == 11 )
{
printWarning(&unk_108D0, "map.c", 146LL);
if ( *v16 != 12 )
goto LABEL_17;
}
else if ( *v16 != 12 )
{
goto LABEL_17;
}
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"map.c",
148LL);
LABEL_17:
sleep(0x3E8u);
v14 = gestor;
}
v20 = v12 + gestor;
if ( !*(_DWORD *)(v12 + gestor + 16) )
{
close(*(_DWORD *)(v20 + 8));
dup2(*(_DWORD *)(gestor + v12 + 12), 1);
v22 = argv;
argv[v3] = v24;
execvp(*v22, v22);
exit(1);
}
close(*(_DWORD *)(v20 + 12));
if ( MAXCOM <= 0 )
{
v10 = 0;
v11 = 0;
}
else
{
v11 = 0;
v21 = (_BYTE *)(gestor + 20);
do
{
if ( !*v21 )
{
v10 = 1;
goto LABEL_9;
}
++v11;
v21 += 28;
}
while ( v11 != MAXCOM );
v10 = 0;
}
}
while ( 1 )
{
result = esperaPorPipeMap();
v19 = (int)result;
if ( (_DWORD)result == -1 )
break;
lerChaveValor(result);
v17 = 28 * v19;
close(*(_DWORD *)(gestor + v17 + 8));
*(_DWORD *)(gestor + v17 + 16) = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // r12
__int64 v4; // rbx
__int64 v5; // rsi
char **v6; // rax
int v7; // edx
_DWORD *v8; // rax
__int64 v9; // rdx
char v10; // bl
int v11; // er13
__int64 v12; // rbp
__int64 v13; // rdi
__int64 v14; // rbx
__pid_t v15; // eax
int *v16; // rbx
__int64 v17; // rbx
__int64 result; // rax
__int64 v19; // rbx
__int64 v20; // rax
_BYTE *v21; // rax
char **v22; // rax
char **argv; // [rsp+8h] [rbp-1030h] BYREF
char v24[4136]; // [rsp+10h] [rbp-1028h] BYREF

separaStrEmArgumentos(a1, &argv);
if ( *argv )
{
v1 = 1LL;
do
{
v2 = v1;
v3 = v1++;
}
while ( argv[v1 - 1] );
v4 = v1;
v5 = v2 + 2;
}
else
{
v4 = 1LL;
v5 = 2LL;
v3 = 0LL;
}
v6 = (char **)myRealloc(argv, v5);
v7 = MAXCOM;
argv = v6;
v6[v4] = 0LL;
if ( v7 > 0 )
{
v8 = (_DWORD *)(gestor + 16);
v9 = gestor + 28LL * (unsigned int)(v7 - 1) + 44;
do
{
*v8 = -1;
v8 += 7;
*((_BYTE *)v8 - 24) = 0;
}
while ( (_DWORD *)v9 != v8 );
}
v10 = 1;
v11 = 0;
listaInit();
LABEL_9:
while ( (unsigned int)func2((__int64)v24, 4096) != -1 )
{
if ( !v10 )
{
v11 = func1();
if ( v11 == -1 )
printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "map.c", 121LL);
}
v12 = 28LL * v11;
v13 = v12 + gestor;
if ( !*(_BYTE *)(v12 + gestor + 20) && *(int *)(v13 + 16) > 0 )
{
func0(v11);
close(*(_DWORD *)(gestor + v12 + 8));
v13 = v12 + gestor;
*(_DWORD *)(v12 + gestor + 16) = -1;
}
if ( pipe((int *)(v13 + 8)) == -1 )
{
if ( *_errno_location() == 24 )
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "map.c", 134LL);
sleep(0x3E8u);
}
v14 = gestor;
*(_BYTE *)(gestor + v12 + 20) = 1;
while ( 1 )
{
v15 = fork();
*(_DWORD *)(v14 + v12 + 16) = v15;
if ( v15 != -1 )
break;
v16 = _errno_location();
if ( *v16 == 11 )
{
printWarning(&unk_108D0, "map.c", 146LL);
if ( *v16 != 12 )
goto LABEL_17;
}
else if ( *v16 != 12 )
{
goto LABEL_17;
}
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"map.c",
148LL);
LABEL_17:
sleep(0x3E8u);
v14 = gestor;
}
v20 = v12 + gestor;
if ( !*(_DWORD *)(v12 + gestor + 16) )
{
close(*(_DWORD *)(v20 + 8));
dup2(*(_DWORD *)(gestor + v12 + 12), 1);
v22 = argv;
argv[v3] = v24;
execvp(*v22, v22);
exit(1);
}
close(*(_DWORD *)(v20 + 12));
if ( MAXCOM <= 0 )
{
v10 = 0;
v11 = 0;
}
else
{
v11 = 0;
v21 = (_BYTE *)(gestor + 20);
do
{
if ( !*v21 )
{
v10 = 1;
goto LABEL_9;
}
++v11;
v21 += 28;
}
while ( v11 != MAXCOM );
v10 = 0;
}
}
while ( 1 )
{
result = func1();
v19 = (int)result;
if ( (_DWORD)result == -1 )
break;
func0(result);
v17 = 28 * v19;
close(*(_DWORD *)(gestor + v17 + 8));
*(_DWORD *)(gestor + v17 + 16) = -1;
}
return result;
}

original_code:
int map(char *comando){
    char **args;
    separaStrEmArgumentos(comando, &args);
    int i=0;
    for(i=0; args[i]; i++);
    args = (char**)myRealloc(args, sizeof(char) * (i+2));
    args[i+1] = ((void *)0);
    int indiceArgsBuffer = i;
    i=0;
    bool livre = 1;
    char buffer[4096];
    for(i=0; i<MAXCOM; i++){
        gestor[i].pid = -1;
        gestor[i].vivo = 0;
    }
    i=0;
    listaInit();
    while(1){
        if(lerLinha(buffer, 4096) == -1)
            break;
        if(!livre){
            i = esperaPorPipeMap();
            if(i == -1)
                printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "map.c", 121);
        }
        if(gestor[i].vivo == 0 && gestor[i].pid > 0){
            lerChaveValor(i);
            close(gestor[i].pipeOut[0]);
            gestor[i].pid = -1;
        }
        if( pipe(gestor[i].pipeOut) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "map.c", 134);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "map.c", 136);
            sleep(1000);
        }
        gestor[i].vivo = 1;
        while( (gestor[i].pid = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "map.c", 146);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "map.c", 148);
            sleep(1000);
        }
        if( gestor[i].pid==0){
            close(gestor[i].pipeOut[0]);
            dup2(gestor[i].pipeOut[1], 1);
            args[indiceArgsBuffer] = buffer;
            execvp(args[0], args);
            exit(1);
        }
        close(gestor[i].pipeOut[1]);
        livre = 0;
        for(i=0; i<MAXCOM; i++){
            if(gestor[i].vivo == 0){
                livre = 1;
                break;
            }
        }
    }
    while((i = esperaPorPipeMap()) != -1){
        lerChaveValor(i);
        close(gestor[i].pipeOut[0]);
        gestor[i].pid = -1;
    }
}

canonical_original_code:
int map(char *comando){
    char **args;
    separaStrEmArgumentos(comando, &args);
    int i = 0;
    for(i=0; args[i]; i++);
    args = (char **)myRealloc(args, sizeof(char) * (i+2));
    args[i+1] = ((void *)0);
    int indiceArgsBuffer = i;
    i=0;
    unsigned char livre = 1;
    char buffer[4096];
    for(i=0; i<MAXCOM; i++){
        gestor[i].pid = -1;
        gestor[i].vivo = 0;
    }
    i=0;
    listaInit();
    while(1){
        if(lerLinha(buffer, 4096) == -1)
            break;
        if(!livre){
            i = esperaPorPipeMap();
            if(i == -1)
                printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "map.c", 121);
        }
        if(gestor[i].vivo == 0 && gestor[i].pid > 0){
            lerChaveValor(i);
            close(gestor[i].pipeOut[0]);
            gestor[i].pid = -1;
        }
        if( pipe(gestor[i].pipeOut) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "map.c", 134);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "map.c", 136);
            sleep(1000);
        }
        gestor[i].vivo = 1;
        while( (gestor[i].pid = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "map.c", 146);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "map.c", 148);
            sleep(1000);
        }
        if( gestor[i].pid==0){
            close(gestor[i].pipeOut[0]);
            dup2(gestor[i].pipeOut[1], 1);
            args[indiceArgsBuffer] = buffer;
            execvp(args[0], args);
            exit(1);
        }
        close(gestor[i].pipeOut[1]);
        livre = 0;
        for(i=0; i<MAXCOM; i++){
            if(gestor[i].vivo == 0){
                livre = 1;
                break;
            }
        }
    }
    while((i = esperaPorPipeMap()) != -1){
        lerChaveValor(i);
        close(gestor[i].pipeOut[0]);
        gestor[i].pid = -1;
    }
}

variable_types:
comando
char *

variable_types:
args
char **

variable_types: indiceArgsBuffer int

variable_types:
livre
unsigned char

variable_types:
buffer
char [4096]


lerReduced

decompiled_code:
__int64 __fastcall lerReduced(int a1)
{
__int64 v1; // r12
int v2; // ebx
char *v3; // rbp
char *v4; // r14
__int64 v5; // r13
__int64 v6; // rsi
int v7; // eax
char *v8; // rbp

v1 = 100LL;
v2 = 0;
v3 = (char *)myMalloc(50LL);
v4 = v3;
v5 = 28LL * a1;
while ( 1 )
{
v7 = read(*(_DWORD *)(gestor + v5 + 8), v4, 0x32uLL);
if ( !v7 )
break;
if ( v7 < 0 )
{
*(v4 - 1) = 0;
break;
}
v2 += v7;
v6 = v1;
v1 += 50LL;
v3 = (char *)myRealloc(v3, v6);
v4 = &v3[v2];
}
if ( !v2 )
return 0xFFFFFFFFLL;
v8 = strtok(v3, "\n");
listaReduced(*(unsigned int *)(gestor + v5 + 24), v8);
free(v8);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // r12
int v2; // ebx
char *v3; // rbp
char *v4; // r14
__int64 v5; // r13
__int64 v6; // rsi
int v7; // eax
char *v8; // rbp

v1 = 100LL;
v2 = 0;
v3 = (char *)myMalloc(50LL);
v4 = v3;
v5 = 28LL * a1;
while ( 1 )
{
v7 = read(*(_DWORD *)(gestor + v5 + 8), v4, 0x32uLL);
if ( !v7 )
break;
if ( v7 < 0 )
{
*(v4 - 1) = 0;
break;
}
v2 += v7;
v6 = v1;
v1 += 50LL;
v3 = (char *)myRealloc(v3, v6);
v4 = &v3[v2];
}
if ( !v2 )
return 0xFFFFFFFFLL;
v8 = strtok(v3, "\n");
listaReduced(*(unsigned int *)(gestor + v5 + 24), v8);
free(v8);
return 0LL;
}

original_code:
int lerReduced(int pos){
    const int tamanhoInicial = 50;
    int tamanho = tamanhoInicial;
    int incrementos = 1;
    char *buffer = (char*)myMalloc(sizeof(char) * tamanho);
    char *i = buffer;
    int lidos;
    int lidosTotal = 0;
    while(lidos = read(gestor[pos].pipeOut[0], i, sizeof(char)*tamanhoInicial)){
        if(lidos < 0)
            lidos = 0;
        lidosTotal += lidos;
        if( lidos <= 0){
            *(i+lidos-1) = '\0';
            break;
        }else{
            incrementos++;
            buffer = (char*)myRealloc(buffer, sizeof(char) * tamanhoInicial * incrementos);
            i = buffer + lidosTotal;
        }
    }
    if(lidosTotal == 0) return -1;
    buffer = strtok(buffer, "\n");
    listaReduced(gestor[pos].indiceTabela, buffer);
    free(buffer);
    return 0;
}

canonical_original_code:
int lerReduced(int pos){
    int tamanhoInicial = 50;
    int tamanho = tamanhoInicial;
    int incrementos = 1;
    char *buffer = (char*)myMalloc(sizeof(char) * tamanho);
    char *i = buffer;
    int lidos;
    int lidosTotal = 0;
    while(lidos = read(gestor[pos].pipeOut[0], i, sizeof(char)*tamanhoInicial)){
        if(lidos < 0)
            lidos = 0;
        lidosTotal += lidos;
        if( lidos <= 0){
            *(i+lidos-1) = '\0';
            break;
        }else{
            incrementos++;
            buffer = (char *)myRealloc(buffer, sizeof(char) * tamanhoInicial * incrementos);
            i = buffer + lidosTotal;
        }
    }
    if(lidosTotal == 0) return -1;
    buffer = strtok(buffer, "\n");
    listaReduced(gestor[pos].indiceTabela, buffer);
    free(buffer);
    return 0;
}

variable_types: pos int

variable_types: tamanhoInicial int

variable_types: tamanho int

variable_types: incrementos int


esperaPorPipeReduce

decompiled_code:
__int64 __fastcall esperaPorPipeReduce(__int64 a1, const char *a2)
{
int v2; // ebx
int v3; // edi
__WAIT_STATUS *v4; // rdi
__pid_t v5; // eax
__int64 v6; // rdx
__pid_t v7; // ebx
__int64 result; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 0;
while ( MAXCOM > v2 )
{
if ( zombieFound )
break;
v3 = v2++;
lerReduced(v3);
}
v4 = &stat_loc;
v5 = wait((__WAIT_STATUS)&stat_loc);
zombieFound = 0;
v7 = v5;
if ( v5 == -1 )
return 0xFFFFFFFFLL;
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
a2 = "reduce.c";
v4 = (__WAIT_STATUS *)&unk_5C0;
printErrorAndExit(&unk_5C0, "reduce.c", 54LL);
}
if ( MAXCOM <= 0 )
return esperaPorPipeReduce(v4, a2, v6);
v6 = gestor;
result = 0LL;
while ( *(_DWORD *)(v6 + 16) != v7 )
{
result = (unsigned int)(result + 1);
v6 += 28LL;
if ( MAXCOM == (_DWORD)result )
return esperaPorPipeReduce(v4, a2, v6);
}
*(_BYTE *)(v6 + 20) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
int v2; // ebx
int v3; // edi
__WAIT_STATUS *v4; // rdi
__pid_t v5; // eax
__int64 v6; // rdx
__pid_t v7; // ebx
__int64 result; // rax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

v2 = 0;
while ( MAXCOM > v2 )
{
if ( zombieFound )
break;
v3 = v2++;
func0(v3);
}
v4 = &stat_loc;
v5 = wait((__WAIT_STATUS)&stat_loc);
zombieFound = 0;
v7 = v5;
if ( v5 == -1 )
return 0xFFFFFFFFLL;
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
a2 = "reduce.c";
v4 = (__WAIT_STATUS *)&unk_5C0;
printErrorAndExit(&unk_5C0, "reduce.c", 54LL);
}
if ( MAXCOM <= 0 )
return func1(v4, a2, v6);
v6 = gestor;
result = 0LL;
while ( *(_DWORD *)(v6 + 16) != v7 )
{
result = (unsigned int)(result + 1);
v6 += 28LL;
if ( MAXCOM == (_DWORD)result )
return func1(v4, a2, v6);
}
*(_BYTE *)(v6 + 20) = 0;
return result;
}

original_code:
int esperaPorPipeReduce(){
    int i=0;
    for(i=0; i<MAXCOM && !zombieFound; i++){
        lerReduced(i);
    }
    int resultado;
    int pid = wait(&resultado);
    zombieFound = 0;
    if(pid == -1)
        return -1;
    if(!(((resultado) & 0x7f) == 0)){
        printErrorAndExit("Uma execução do comando reduce terminou abruptamente.", "reduce.c", 54);
    }
    bool encontrou = 0;
    for(i=0; i<MAXCOM; i++){
        if(gestor[i].pid == pid){
            encontrou = 1;
            gestor[i].vivo = 0;
            break;
        }
    }
    if(!encontrou){
        return esperaPorPipeReduce();
    }
    return i;
}

canonical_original_code:
int esperaPorPipeReduce(){
    int i = 0;
    for(i=0; i<MAXCOM && !zombieFound; i++){
        lerReduced(i);
    }
    int resultado;
    int pid = wait(&resultado);
    zombieFound = 0;
    if(pid == -1)
        return -1;
    if(!(((resultado) & 0x7f) == 0)){
        printErrorAndExit("Uma execução do comando reduce terminou abruptamente.", "reduce.c", 54);
    }
    unsigned char encontrou = 0;
    for(i=0; i<MAXCOM; i++){
        if(gestor[i].pid == pid){
            encontrou = 1;
            gestor[i].vivo = 0;
            break;
        }
    }
    if(!encontrou){
        return esperaPorPipeReduce();
    }
    return i;
}


reduce

decompiled_code:
__int64 __fastcall reduce(__int64 a1)
{
const char *v1; // rsi
_DWORD *v2; // rax
__int64 v3; // rdx
int v4; // ebp
char v5; // bl
int v6; // er12
__int64 v7; // rbx
int *v8; // rdi
__pid_t v9; // eax
int *v10; // r12
int v11; // eax
__int64 v12; // rbx
__int64 v14; // r12
__pid_t v15; // eax
int *v16; // r12
__int64 v17; // rax
_BYTE *v18; // rax
char **argv[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = (const char *)argv;
separaStrEmArgumentos(a1, argv);
if ( MAXCOM > 0 )
{
v1 = (const char *)(unsigned int)(MAXCOM - 1);
v2 = (_DWORD *)(gestor + 16);
v3 = gestor + 28LL * (_QWORD)v1 + 44;
do
{
*v2 = -1;
v2 += 7;
*((_BYTE *)v2 - 24) = 0;
}
while ( v2 != (_DWORD *)v3 );
}
v4 = 0;
v5 = 1;
v6 = 0;
while ( (int)listaCount() > v4 )
{
if ( !v5 )
{
v6 = esperaPorPipeReduce(a1, v1);
if ( v6 == -1 )
{
v1 = "reduce.c";
printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "reduce.c", 102LL);
}
}
v7 = 28LL * v6;
v8 = (int *)(v7 + gestor);
if ( !*(_BYTE *)(v7 + gestor + 20) && v8[4] > 0 )
{
lerReduced(v6);
close(*(_DWORD *)(gestor + v7 + 8));
v8 = (int *)(v7 + gestor);
*(_DWORD *)(v7 + gestor + 16) = -1;
}
v8[6] = v4;
if ( pipe(v8) == -1 )
{
if ( *_errno_location() == 24 )
{
v1 = "reduce.c";
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 119LL);
}
sleep(0x3E8u);
}
if ( pipe((int *)(v7 + gestor + 8)) == -1 )
{
if ( *_errno_location() == 24 )
{
v1 = "reduce.c";
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 128LL);
}
goto LABEL_14;
}
while ( 1 )
{
v9 = fork();
if ( v9 != -1 )
break;
v10 = _errno_location();
if ( *v10 == 11 )
{
v1 = "reduce.c";
printWarning(&unk_670, "reduce.c", 138LL);
if ( *v10 != 12 )
goto LABEL_14;
}
else if ( *v10 != 12 )
{
goto LABEL_14;
}
v1 = "reduce.c";
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"reduce.c",
140LL);
LABEL_14:
sleep(0x3E8u);
}
if ( !v9 )
{
close(*(_DWORD *)(gestor + v7));
close(*(_DWORD *)(gestor + v7 + 12));
close(*(_DWORD *)(gestor + v7 + 8));
listaDumpValues(*(unsigned int *)(gestor + v7 + 24), *(unsigned int *)(gestor + v7 + 4));
exit(0);
}
while ( 2 )
{
v14 = gestor;
v15 = fork();
*(_DWORD *)(v7 + v14 + 16) = v15;
if ( v15 == -1 )
{
v16 = _errno_location();
if ( *v16 == 11 )
{
v1 = "reduce.c";
printWarning(&unk_670, "reduce.c", 164LL);
if ( *v16 == 12 )
{
LABEL_29:
v1 = "reduce.c";
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"reduce.c",
166LL);
}
}
else if ( *v16 == 12 )
{
goto LABEL_29;
}
sleep(0x3E8u);
continue;
}
break;
}
v17 = v7 + gestor;
if ( !*(_DWORD *)(v7 + gestor + 16) )
{
close(*(_DWORD *)(v17 + 4));
close(*(_DWORD *)(gestor + v7 + 8));
dup2(*(_DWORD *)(gestor + v7), 0);
dup2(*(_DWORD *)(gestor + v7 + 12), 1);
execvp(*(const char **)argv[0], argv[0]);
exit(1);
}
*(_BYTE *)(v17 + 20) = 1;
close(*(_DWORD *)v17);
close(*(_DWORD *)(gestor + v7 + 4));
a1 = *(unsigned int *)(gestor + v7 + 12);
close(a1);
if ( MAXCOM <= 0 )
{
v6 = 0;
LABEL_38:
v5 = 0;
}
else
{
v6 = 0;
v18 = (_BYTE *)(gestor + 20);
while ( *v18 )
{
++v6;
v18 += 28;
if ( MAXCOM == v6 )
goto LABEL_38;
}
v5 = 1;
}
++v4;
}
while ( 1 )
{
v11 = esperaPorPipeReduce(a1, v1);
v12 = v11;
if ( v11 == -1 )
break;
lerReduced(v11);
a1 = *(unsigned int *)(gestor + 28 * v12);
close(a1);
*(_DWORD *)(gestor + 28 * v12 + 16) = -1;
}
return listaPrintKeyReduced();
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
const char *v1; // rsi
_DWORD *v2; // rax
__int64 v3; // rdx
int v4; // ebp
char v5; // bl
int v6; // er12
__int64 v7; // rbx
int *v8; // rdi
__pid_t v9; // eax
int *v10; // r12
int v11; // eax
__int64 v12; // rbx
__int64 v14; // r12
__pid_t v15; // eax
int *v16; // r12
__int64 v17; // rax
_BYTE *v18; // rax
char **argv[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = (const char *)argv;
separaStrEmArgumentos(a1, argv);
if ( MAXCOM > 0 )
{
v1 = (const char *)(unsigned int)(MAXCOM - 1);
v2 = (_DWORD *)(gestor + 16);
v3 = gestor + 28LL * (_QWORD)v1 + 44;
do
{
*v2 = -1;
v2 += 7;
*((_BYTE *)v2 - 24) = 0;
}
while ( v2 != (_DWORD *)v3 );
}
v4 = 0;
v5 = 1;
v6 = 0;
while ( (int)listaCount() > v4 )
{
if ( !v5 )
{
v6 = func1(a1, v1);
if ( v6 == -1 )
{
v1 = "reduce.c";
printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "reduce.c", 102LL);
}
}
v7 = 28LL * v6;
v8 = (int *)(v7 + gestor);
if ( !*(_BYTE *)(v7 + gestor + 20) && v8[4] > 0 )
{
func0(v6);
close(*(_DWORD *)(gestor + v7 + 8));
v8 = (int *)(v7 + gestor);
*(_DWORD *)(v7 + gestor + 16) = -1;
}
v8[6] = v4;
if ( pipe(v8) == -1 )
{
if ( *_errno_location() == 24 )
{
v1 = "reduce.c";
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 119LL);
}
sleep(0x3E8u);
}
if ( pipe((int *)(v7 + gestor + 8)) == -1 )
{
if ( *_errno_location() == 24 )
{
v1 = "reduce.c";
printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 128LL);
}
goto LABEL_14;
}
while ( 1 )
{
v9 = fork();
if ( v9 != -1 )
break;
v10 = _errno_location();
if ( *v10 == 11 )
{
v1 = "reduce.c";
printWarning(&unk_670, "reduce.c", 138LL);
if ( *v10 != 12 )
goto LABEL_14;
}
else if ( *v10 != 12 )
{
goto LABEL_14;
}
v1 = "reduce.c";
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"reduce.c",
140LL);
LABEL_14:
sleep(0x3E8u);
}
if ( !v9 )
{
close(*(_DWORD *)(gestor + v7));
close(*(_DWORD *)(gestor + v7 + 12));
close(*(_DWORD *)(gestor + v7 + 8));
listaDumpValues(*(unsigned int *)(gestor + v7 + 24), *(unsigned int *)(gestor + v7 + 4));
exit(0);
}
while ( 2 )
{
v14 = gestor;
v15 = fork();
*(_DWORD *)(v7 + v14 + 16) = v15;
if ( v15 == -1 )
{
v16 = _errno_location();
if ( *v16 == 11 )
{
v1 = "reduce.c";
printWarning(&unk_670, "reduce.c", 164LL);
if ( *v16 == 12 )
{
LABEL_29:
v1 = "reduce.c";
printWarning(
"failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.",
"reduce.c",
166LL);
}
}
else if ( *v16 == 12 )
{
goto LABEL_29;
}
sleep(0x3E8u);
continue;
}
break;
}
v17 = v7 + gestor;
if ( !*(_DWORD *)(v7 + gestor + 16) )
{
close(*(_DWORD *)(v17 + 4));
close(*(_DWORD *)(gestor + v7 + 8));
dup2(*(_DWORD *)(gestor + v7), 0);
dup2(*(_DWORD *)(gestor + v7 + 12), 1);
execvp(*(const char **)argv[0], argv[0]);
exit(1);
}
*(_BYTE *)(v17 + 20) = 1;
close(*(_DWORD *)v17);
close(*(_DWORD *)(gestor + v7 + 4));
a1 = *(unsigned int *)(gestor + v7 + 12);
close(a1);
if ( MAXCOM <= 0 )
{
v6 = 0;
LABEL_38:
v5 = 0;
}
else
{
v6 = 0;
v18 = (_BYTE *)(gestor + 20);
while ( *v18 )
{
++v6;
v18 += 28;
if ( MAXCOM == v6 )
goto LABEL_38;
}
v5 = 1;
}
++v4;
}
while ( 1 )
{
v11 = func1(a1, v1);
v12 = v11;
if ( v11 == -1 )
break;
func0(v11);
a1 = *(unsigned int *)(gestor + 28 * v12);
close(a1);
*(_DWORD *)(gestor + 28 * v12 + 16) = -1;
}
return listaPrintKeyReduced();
}

original_code:
int reduce(char *comando){
    char **args;
    separaStrEmArgumentos(comando, &args);
    int i=0;
    bool livre = 1;
    int pidAlimentador;
    for(i=0; i<MAXCOM; i++){
        gestor[i].pid = -1;
        gestor[i].vivo = 0;
    }
    i=0;
    int indiceChaveActual = 0;
    while(1){
        if( indiceChaveActual > listaCount()-1 )
            break;
        if(!livre){
            i = esperaPorPipeReduce();
            if(i == -1)
                printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "reduce.c", 102);
        }
        if(gestor[i].vivo == 0 && gestor[i].pid > 0){
            lerReduced(i);
            close(gestor[i].pipeOut[0]);
            gestor[i].pid = -1;
        }
        gestor[i].indiceTabela = indiceChaveActual;
        if( pipe(gestor[i].pipeIn) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 119);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "reduce.c", 121);
            sleep(1000);
        }
        if( pipe(gestor[i].pipeOut) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 128);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "reduce.c", 130);
            sleep(1000);
        }
        while( (pidAlimentador = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "reduce.c", 138);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "reduce.c", 140);
            sleep(1000);
        }
        if( pidAlimentador == 0 ){
            close(gestor[i].pipeIn[0]);
            close(gestor[i].pipeOut[1]);
            close(gestor[i].pipeOut[0]);
            listaDumpValues(gestor[i].indiceTabela,gestor[i].pipeIn[1]);
            exit(0);
        }
        while( (gestor[i].pid = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "reduce.c", 164);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "reduce.c", 166);
            sleep(1000);
        }
        if( gestor[i].pid==0){
            close(gestor[i].pipeIn[1]);
            close(gestor[i].pipeOut[0]);
            dup2(gestor[i].pipeIn[0], 0);
            dup2(gestor[i].pipeOut[1], 1);
            execvp(args[0], args);
            exit(1);
        }
        gestor[i].vivo = 1;
        close(gestor[i].pipeIn[0]);
        close(gestor[i].pipeIn[1]);
        close(gestor[i].pipeOut[1]);
        livre = 0;
        for(i=0; i<MAXCOM; i++){
            if(gestor[i].vivo == 0){
                livre = 1;
                break;
            }
        }
        indiceChaveActual++;
    }
    while((i = esperaPorPipeReduce()) != -1){
        lerReduced(i);
        close(gestor[i].pipeIn[0]);
        gestor[i].pid = -1;
    }
    listaPrintKeyReduced();
}

canonical_original_code:
int reduce(char *comando){
    char **args;
    separaStrEmArgumentos(comando, &args);
    int i = 0;
    unsigned char livre = 1;
    int pidAlimentador;
    for(i=0; i<MAXCOM; i++){
        gestor[i].pid = -1;
        gestor[i].vivo = 0;
    }
    i=0;
    int indiceChaveActual = 0;
    while(1){
        if( indiceChaveActual > listaCount()-1 )
            break;
        if(!livre){
            i = esperaPorPipeReduce();
            if(i == -1)
                printErrorAndExit("Ocorreu um ECHILD no wait antes do tempo.", "reduce.c", 102);
        }
        if(gestor[i].vivo == 0 && gestor[i].pid > 0){
            lerReduced(i);
            close(gestor[i].pipeOut[0]);
            gestor[i].pid = -1;
        }
        gestor[i].indiceTabela = indiceChaveActual;
        if( pipe(gestor[i].pipeIn) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 119);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "reduce.c", 121);
            sleep(1000);
        }
        if( pipe(gestor[i].pipeOut) == -1 ){
            if( (*__errno_location ()) == 24)
               printWarning("Too many file descriptors in use by the process. Retrying in a while.", "reduce.c", 128);
            else if( (*__errno_location ()) == 24)
               printWarning("The system limit on the total number of open files has been reached. Retrying in a while.", "reduce.c", 130);
            sleep(1000);
        }
        while( (pidAlimentador = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "reduce.c", 138);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "reduce.c", 140);
            sleep(1000);
        }
        if( pidAlimentador == 0 ){
            close(gestor[i].pipeIn[0]);
            close(gestor[i].pipeOut[1]);
            close(gestor[i].pipeOut[0]);
            listaDumpValues(gestor[i].indiceTabela,gestor[i].pipeIn[1]);
            exit(0);
        }
        while( (gestor[i].pid = fork()) == -1 ){
            if( (*__errno_location ()) == 11 )
                printWarning("Não foi possível alocar memória para o filho ou o máximo de filhos permitido foi atingido. Tentando outra vez.", "reduce.c", 164);
            if( (*__errno_location ()) == 12)
                printWarning("failed to allocate the necessary kernel structures because memory is tight. Retrying in a while.", "reduce.c", 166);
            sleep(1000);
        }
        if( gestor[i].pid==0){
            close(gestor[i].pipeIn[1]);
            close(gestor[i].pipeOut[0]);
            dup2(gestor[i].pipeIn[0], 0);
            dup2(gestor[i].pipeOut[1], 1);
            execvp(args[0], args);
            exit(1);
        }
        gestor[i].vivo = 1;
        close(gestor[i].pipeIn[0]);
        close(gestor[i].pipeIn[1]);
        close(gestor[i].pipeOut[1]);
        livre = 0;
        for(i=0; i<MAXCOM; i++){
            if(gestor[i].vivo == 0){
                livre = 1;
                break;
            }
        }
        indiceChaveActual++;
    }
    while((i = esperaPorPipeReduce()) != -1){
        lerReduced(i);
        close(gestor[i].pipeIn[0]);
        gestor[i].pid = -1;
    }
    listaPrintKeyReduced();
}

variable_types: pidAlimentador int

variable_types: indiceChaveActual int


printErrorAndExit

decompiled_code:
void __fastcall __noreturn printErrorAndExit(void *buf, void *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax

write(2, "[ERRO] ", 7uLL);
v4 = strlen((const char *)buf);
write(2, buf, v4);
write(2, "(", 1uLL);
v5 = strlen((const char *)a2);
write(2, a2, v5);
write(2, "@line", 5uLL);
snprintf(errorLineAsString, 0xAuLL, "%d", a3);
write(2, errorLineAsString, 0xAuLL);
write(2, &unk_1B5, 0x1DuLL);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *buf, void *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax

write(2, "[ERRO] ", 7uLL);
v4 = strlen((const char *)buf);
write(2, buf, v4);
write(2, "(", 1uLL);
v5 = strlen((const char *)a2);
write(2, a2, v5);
write(2, "@line", 5uLL);
snprintf(errorLineAsString, 0xAuLL, "%d", a3);
write(2, errorLineAsString, 0xAuLL);
write(2, &unk_1B5, 0x1DuLL);
exit(1);
}

original_code:
void printErrorAndExit(const char* erro, const char* file, int line){
    write(2, "[ERRO] ", 7);
    write(2, erro, strlen(erro));
    write(2, "(", 1);
    write(2, file, strlen(file));
    write(2, "@line", 5);
    snprintf(errorLineAsString, 10, "%d", line);
    write(2, errorLineAsString, 10);
    write(2, ")\nA aplicaçao vai terminar.\n", 29);
    exit(1);
}

canonical_original_code:
void printErrorAndExit(char *erro, char *file, int line){
    write(2, "[ERRO] ", 7);
    write(2, erro, strlen(erro));
    write(2, "(", 1);
    write(2, file, strlen(file));
    write(2, "@line", 5);
    snprintf(errorLineAsString, 10, "%d", line);
    write(2, errorLineAsString, 10);
    write(2, ")\nA aplicaçao vai terminar.\n", 29);
    exit(1);
}

variable_types:
erro
char *

variable_types:
file
char *

variable_types: line int

return_type: void


printWarning

decompiled_code:
ssize_t __fastcall printWarning(char *s, char *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax

write(2, "[WARN] ", 7uLL);
v4 = strlen(s);
write(2, s, v4);
write(2, "(", 1uLL);
v5 = strlen(a2);
write(2, a2, v5);
write(2, "@line", 5uLL);
snprintf(errorLineAsString, 0xAuLL, "%d", a3);
write(2, errorLineAsString, 0xAuLL);
return write(2, ")\n", 2uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func1(char *s, char *a2, unsigned int a3)
{
size_t v4; // rax
size_t v5; // rax

write(2, "[WARN] ", 7uLL);
v4 = strlen(s);
write(2, s, v4);
write(2, "(", 1uLL);
v5 = strlen(a2);
write(2, a2, v5);
write(2, "@line", 5uLL);
snprintf(errorLineAsString, 0xAuLL, "%d", a3);
write(2, errorLineAsString, 0xAuLL);
return write(2, ")\n", 2uLL);
}

original_code:
void printWarning(const char* erro, const char* file, int line){
    write(2, "[WARN] ", 7);
    write(2, erro, strlen(erro));
    write(2, "(", 1);
    write(2, file, strlen(file));
    write(2, "@line", 5);
    snprintf(errorLineAsString, 10, "%d", line);
    write(2, errorLineAsString, 10);
    write(2, ")\n", 2);
}

canonical_original_code:
void printWarning(char *erro, char *file, int line){
    write(2, "[WARN] ", 7);
    write(2, erro, strlen(erro));
    write(2, "(", 1);
    write(2, file, strlen(file));
    write(2, "@line", 5);
    snprintf(errorLineAsString, 10, "%d", line);
    write(2, errorLineAsString, 10);
    write(2, ")\n", 2);
}


myMalloc

decompiled_code:
void *__fastcall myMalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
printErrorAndExit(&unk_178, "comum.c", 11LL);
result = 0LL;
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
printErrorAndExit(&unk_178, "comum.c", 11LL);
result = 0LL;
}
return result;
}

original_code:
void *myMalloc(size_t size){
    void *ptr = malloc(size);
    if( ptr == ((void *)0) )
        printErrorAndExit("Falha ao alocar memória.", "comum.c", 11);
    return ptr;
}

canonical_original_code:
void *myMalloc(unsigned long size){
    void *ptr = malloc(size);
    if( ptr == ((void *)0) )
        printErrorAndExit("Falha ao alocar memória.", "comum.c", 11);
    return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


myRealloc

decompiled_code:
void *__fastcall myRealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
printErrorAndExit(&unk_192, "comum.c", 18LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
printErrorAndExit(&unk_192, "comum.c", 18LL);
result = 0LL;
}
return result;
}

original_code:
void *myRealloc(void *ptr, size_t size){
    ptr = realloc(ptr, size);
    if( ptr == ((void *)0) )
        printErrorAndExit("Falha ao realocar memória.", "comum.c", 18);
    return ptr;
}

canonical_original_code:
void *myRealloc(void *ptr, unsigned long size){
    ptr = realloc(ptr, size);
    if( ptr == ((void *)0) )
        printErrorAndExit("Falha ao realocar memória.", "comum.c", 18);
    return ptr;
}


filhoMorreu

decompiled_code:
void filhoMorreu()
{
zombieFound = 1;
}

canonical_decompiled_code:
void func2()
{
zombieFound = 1;
}

original_code:
void filhoMorreu(){
    zombieFound = 1;
}

canonical_original_code:
void filhoMorreu(){
    zombieFound = 1;
}


repo: challinan/pscanner

decompiled_code:
__int64 __fastcall timeval_subtract(_QWORD *a1, _QWORD *a2)
{
return a1[1] + 1000000LL * *a1 - (a2[1] + 1000000LL * *a2);
}

original_code:
long int timeval_subtract(struct timeval *t2, struct timeval *t1)
{
    long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
    return (diff);
}

canonical_original_code:
long timeval_subtract(struct timeval *t2, struct timeval *t1)
{
    long diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
    return (diff);
}

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

variable_types: diff long

return_type: long


decompiled_code:
__int64 __fastcall active_list_add(int a1)
{
_DWORD *v1; // rax
__int64 v2; // rcx
__int64 result; // rax
__int64 v4; // rdx

v1 = calloc(0x18uLL, 1uLL);
if ( !v1 )
{
fwrite("malloc failed - bailing\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
*v1 = a1;
v2 = (__int64)v1;
result = active_list_head;
if ( active_list_head )
{
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
}
while ( result );
*(_QWORD *)(v4 + 16) = v2;
++diag_count;
*(_QWORD *)(v2 + 8) = v4;
}
else
{
++diag_count;
active_list_head = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
_DWORD *v1; // rax
__int64 v2; // rcx
__int64 result; // rax
__int64 v4; // rdx

v1 = calloc(0x18uLL, 1uLL);
if ( !v1 )
{
fwrite("malloc failed - bailing\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
*v1 = a1;
v2 = (__int64)v1;
result = active_list_head;
if ( active_list_head )
{
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
}
while ( result );
*(_QWORD *)(v4 + 16) = v2;
++diag_count;
*(_QWORD *)(v2 + 8) = v4;
}
else
{
++diag_count;
active_list_head = v2;
}
return result;
}

original_code:
void active_list_add(int pid) {
 active_pids_t *ptmp, *pprev;
 active_pids_t *ap = malloc(sizeof(active_pids_t));
 if ( ap == ((void *)0) ) {
  fprintf(stderr, "malloc failed - bailing\n");
  exit(2);
 }
 memset(ap, 0, sizeof(active_pids_t));
 ap->pid = pid;
 if (active_list_head ) {
  ptmp = active_list_head;
  while ( ptmp->next != ((void *)0) ) {
   pprev = ptmp;
   ptmp = ptmp->next;
  }
  ptmp->next = ap;
  ap->prev = ptmp;
 }
 else {
  active_list_head = ap;
 }
 diag_count++;
}

canonical_original_code:
void active_list_add(int pid) {
 struct active_pids_s *ptmp; struct active_pids_s *pprev;
 struct active_pids_s *ap = malloc(sizeof(active_pids_t));
 if ( ap == ((void *)0) ) {
  fprintf(stderr, "malloc failed - bailing\n");
  exit(2);
 }
 memset(ap, 0, sizeof(active_pids_t));
 ap->pid = pid;
 if (active_list_head ) {
  ptmp = active_list_head;
  while ( ptmp->next != ((void *)0) ) {
   pprev = ptmp;
   ptmp = ptmp->next;
  }
  ptmp->next = ap;
  ap->prev = ptmp;
 }
 else {
  active_list_head = ap;
 }
 diag_count++;
}

variable_types: pid int

variable_types: struct active_pids_s { int pid; struct active_pids_s *prev; struct active_pids_s *next; } *


decompiled_code:
__int64 __fastcall active_list_delete(unsigned int a1)
{
_QWORD *v2; // rdi
_QWORD *v3; // rcx
int i; // edx
_QWORD *v5; // rax
__int64 result; // rax

v2 = (_QWORD *)active_list_head;
if ( !active_list_head )
{
fwrite("active_list_delete: Error list is empty (active_list_head == NULL\n", 1uLL, 0x42uLL, stderr);
v2 = (_QWORD *)active_list_head;
if ( !active_list_head )
{
LABEL_10:
fprintf(stderr, "Error removing item from active list (%d)\n", a1);
return 1LL;
}
}
v3 = 0LL;
for ( i = diag_count + 1; ; ++i )
{
v5 = (_QWORD *)v2[2];
if ( *(_DWORD *)v2 == a1 )
break;
if ( !v5 )
{
diag_count = i;
goto LABEL_10;
}
LABEL_4:
v3 = v2;
v2 = v5;
}
mydentry[39 * (int)a1] = 0LL;
dword_405290[78 * a1] = 0;
if ( !v3 )
{
if ( !v5 )
{
diag_count = i;
free(v2);
result = 0LL;
active_list_head = 0LL;
return result;
}
goto LABEL_4;
}
diag_count = i;
if ( v5 )
{
v3[2] = v5;
v5[1] = v3;
}
else
{
v3[2] = 0LL;
}
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
_QWORD *v2; // rdi
_QWORD *v3; // rcx
int i; // edx
_QWORD *v5; // rax
__int64 result; // rax

v2 = (_QWORD *)active_list_head;
if ( !active_list_head )
{
fwrite("active_list_delete: Error list is empty (active_list_head == NULL\n", 1uLL, 0x42uLL, stderr);
v2 = (_QWORD *)active_list_head;
if ( !active_list_head )
{
LABEL_10:
fprintf(stderr, "Error removing item from active list (%d)\n", a1);
return 1LL;
}
}
v3 = 0LL;
for ( i = diag_count + 1; ; ++i )
{
v5 = (_QWORD *)v2[2];
if ( *(_DWORD *)v2 == a1 )
break;
if ( !v5 )
{
diag_count = i;
goto LABEL_10;
}
LABEL_4:
v3 = v2;
v2 = v5;
}
mydentry[39 * (int)a1] = 0LL;
dword_405290[78 * a1] = 0;
if ( !v3 )
{
if ( !v5 )
{
diag_count = i;
free(v2);
result = 0LL;
active_list_head = 0LL;
return result;
}
goto LABEL_4;
}
diag_count = i;
if ( v5 )
{
v3[2] = v5;
v5[1] = v3;
}
else
{
v3[2] = 0LL;
}
free(v2);
return 0LL;
}

original_code:
int active_list_delete(int pid) {
 active_pids_t *ptmp, *pprev;
 if ( active_list_head == ((void *)0) ) {
  fprintf(stderr, "active_list_delete: Error list is empty (active_list_head == NULL\n");
 }
 ptmp = active_list_head;
 pprev = ((void *)0);
 while ( ptmp != ((void *)0) ) {
  diag_count++;
  if ( ptmp->pid == pid ) {
   mydentry[ptmp->pid].direntry1.d_ino = 0;
   mydentry[ptmp->pid].reported = 0;
   if ( pprev == ((void *)0) ) {
    if ( ptmp->next == ((void *)0) ) {
     free(ptmp);
     active_list_head = ((void *)0);
     return 0;
    }
   }
   else {
    if ( ptmp->next == ((void *)0) ) {
     pprev->next = ((void *)0);
     free(ptmp);
     return 0;
    }
    else {
     active_pids_t *pnext = ptmp->next;
     pprev->next = ptmp->next;
     pnext->prev = pprev;
     free(ptmp);
     return 0;
    }
   }
  }
  pprev = ptmp;
  ptmp = ptmp->next;
 }
 fprintf(stderr, "Error removing item from active list (%d)\n", pid);
 return 1;
}

canonical_original_code:
int active_list_delete(int pid) {
 struct active_pids_s *ptmp; struct active_pids_s *pprev;
 if ( active_list_head == ((void *)0) ) {
  fprintf(stderr, "active_list_delete: Error list is empty (active_list_head == NULL\n");
 }
 ptmp = active_list_head;
 pprev = ((void *)0);
 while ( ptmp != ((void *)0) ) {
  diag_count++;
  if ( ptmp->pid == pid ) {
   mydentry[ptmp->pid].direntry1.d_ino = 0;
   mydentry[ptmp->pid].reported = 0;
   if ( pprev == ((void *)0) ) {
    if ( ptmp->next == ((void *)0) ) {
     free(ptmp);
     active_list_head = ((void *)0);
     return 0;
    }
   }
   else {
    if ( ptmp->next == ((void *)0) ) {
     pprev->next = ((void *)0);
     free(ptmp);
     return 0;
    }
    else {
     struct active_pids_s *pnext = ptmp->next;
     pprev->next = ptmp->next;
     pnext->prev = pprev;
     free(ptmp);
     return 0;
    }
   }
  }
  pprev = ptmp;
  ptmp = ptmp->next;
 }
 fprintf(stderr, "Error removing item from active list (%d)\n", pid);
 return 1;
}

user_defined_types: struct active_pids_s { int pid; struct active_pids_s *prev; struct active_pids_s *next; }


decompiled_code:
__int64 __fastcall count_direntries(_QWORD a1, const char *a2)
{
__int64 *v2; // rax
unsigned int v3; // er8

v2 = mydentry;
v3 = 0;
do
{
v3 -= (*v2 == 0) - 1;
v2 += 39;
}
while ( v2 != (__int64 *)((char *)&unk_137FEC8 + (_QWORD)mydentry) );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD a1, const char *a2)
{
__int64 *v2; // rax
unsigned int v3; // er8

v2 = mydentry;
v3 = 0;
do
{
v3 -= (*v2 == 0) - 1;
v2 += 39;
}
while ( v2 != (__int64 *)((char *)&unk_137FEC8 + (_QWORD)mydentry) );
return v3;
}

original_code:
int count_direntries() {
 int i, c = 0;
 struct dirent *pdirent;
 for ( i=0; i<65535; i++) {
  pdirent = &mydentry[i].direntry1;
  if ( pdirent->d_ino ) {
   c++;
  }
 }
 return c;
}

canonical_original_code:
int count_direntries() {
 int i; int c = 0;
 struct dirent *pdirent;
 for ( i=0; i<65535; i++) {
  pdirent = &mydentry[i].direntry1;
  if ( pdirent->d_ino ) {
   c++;
  }
 }
 return c;
}

variable_types: i int

variable_types: c int

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


decompiled_code:
char *__fastcall get_cmdline(unsigned int a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 i; // rax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

snprintf(v6, 0x20uLL, "/proc/%u/cmdline", a1);
v1 = open(v6, 0);
if ( v1 == -1 )
return 0LL;
v2 = v1;
v3 = read(v1, cbuf_0, 0x2000uLL);
if ( v3 == -1 )
{
close(v2);
result = 0LL;
}
else
{
if ( v3 - strlen(cbuf_0) > 1 )
{
if ( v3 > 0 )
{
for ( i = 0LL; ; ++i )
{
if ( !cbuf_0[i] )
cbuf_0[i] = 32;
if ( i == v3 - 1 )
break;
}
}
cbuf_0[v3] = 0;
}
close(v2);
result = cbuf_0;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func12(unsigned int a1)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
__int64 i; // rax
char *result; // rax
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

snprintf(v6, 0x20uLL, "/proc/%u/cmdline", a1);
v1 = open(v6, 0);
if ( v1 == -1 )
return 0LL;
v2 = v1;
v3 = read(v1, cbuf_0, 0x2000uLL);
if ( v3 == -1 )
{
close(v2);
result = 0LL;
}
else
{
if ( v3 - strlen(cbuf_0) > 1 )
{
if ( v3 > 0 )
{
for ( i = 0LL; ; ++i )
{
if ( !cbuf_0[i] )
cbuf_0[i] = 32;
if ( i == v3 - 1 )
break;
}
}
cbuf_0[v3] = 0;
}
close(v2);
result = cbuf_0;
}
return result;
}

original_code:
char *get_cmdline(int pid) {
 int fd, rc, i;
 char cmdline_filename[32];
 static char cbuf[8192];
 snprintf(cmdline_filename, sizeof(cmdline_filename), "/proc/%u/cmdline", pid);
 fd = open(cmdline_filename, 00);
 if ( fd == -1 ) {
  return(((void *)0));
 }
 rc = read(fd, cbuf, sizeof(cbuf));
 if ( rc == -1 ) {
  close(fd);
  return(((void *)0));
 }
 if ( rc - strlen(cbuf) > 1) {
  for ( i=0; i<rc; i++)
   if ( cbuf[i] == '\0' )
    cbuf[i] = ' ';
  cbuf[rc] = '\0';
 }
 close(fd);
 return cbuf;
}

canonical_original_code:
char *get_cmdline(int pid) {
 int fd; int rc; int i;
 char cmdline_filename[32];
 static char cbuf[8192];
 snprintf(cmdline_filename, sizeof(cmdline_filename), "/proc/%u/cmdline", pid);
 fd = open(cmdline_filename, 00);
 if ( fd == -1 ) {
  return(((void *)0));
 }
 rc = read(fd, cbuf, sizeof(cbuf));
 if ( rc == -1 ) {
  close(fd);
  return(((void *)0));
 }
 if ( rc - strlen(cbuf) > 1) {
  for ( i=0; i<rc; i++)
   if ( cbuf[i] == '\0' )
    cbuf[i] = ' ';
  cbuf[rc] = '\0';
 }
 close(fd);
 return cbuf;
}

variable_types: fd int

variable_types: rc int

variable_types:
cbuf
char [8192]

return_type: char *


decompiled_code:
int __fastcall print_process(unsigned int a1, int a2)
{
char *v3; // rax
const char *v4; // r13
int v5; // eax
FILE *v6; // rdi

v3 = get_cmdline(a1);
if ( v3 )
{
v4 = v3;
v5 = strlen(v3);
v6 = outfd;
dword_405278[78 * a1] = v5;
if ( a2 )
fprintf(v6, "%d: %s (T)\n", a1, v4);
else
fprintf(v6, "%d: %s\n", a1, v4);
}
return fflush(outfd);
}

canonical_decompiled_code:
int __fastcall func13(unsigned int a1, int a2)
{
char *v3; // rax
const char *v4; // r13
int v5; // eax
FILE *v6; // rdi

v3 = func12(a1);
if ( v3 )
{
v4 = v3;
v5 = strlen(v3);
v6 = outfd;
dword_405278[78 * a1] = v5;
if ( a2 )
fprintf(v6, "%d: %s (T)\n", a1, v4);
else
fprintf(v6, "%d: %s\n", a1, v4);
}
return fflush(outfd);
}

original_code:
void print_process(int pid, int timeout) {
 char *pbuf;
 pbuf = get_cmdline(pid);
 if ( pbuf ) {
  mydentry[pid].cmdline_length = strlen(pbuf);
  if ( timeout )
   fprintf(outfd, "%d: %s (T)\n", pid, pbuf);
  else
   fprintf(outfd, "%d: %s\n", pid, pbuf);
 }
 fflush(outfd);
}

variable_types: timeout int

variable_types:
pbuf
char *


decompiled_code:
__int64 check_active_pids()
{
unsigned int *v0; // rbx
unsigned int v1; // er12
char *v2; // rdi
int v3; // edx
__int64 v4; // rax
unsigned int v5; // edi
unsigned int v6; // er8
int v7; // eax
__int64 v9; // rax
int v10; // edx
struct timeval v11; // [rsp+0h] [rbp-28h] BYREF

v0 = (unsigned int *)active_list_head;
if ( !active_list_head )
return 0LL;
delete_index = 0;
v1 = 0;
do
{
++v1;
v2 = get_cmdline(*v0);
if ( v2 )
{
v3 = strlen(v2);
v4 = (int)*v0;
v5 = *v0;
if ( dword_405278[78 * v4] < v3 )
{
dword_405278[78 * v4] = v3;
print_process(v5, 0);
v4 = (int)*v0;
}
if ( !dword_405290[78 * v4] )
{
gettimeofday(&v11, 0LL);
if ( timeval_subtract(&v11, (_QWORD *)(312LL * (int)*v0 + 4215424)) > 300000 )
{
print_process(v6, 1);
dword_405290[78 * *v0] = 1;
}
}
v0 = (unsigned int *)*((_QWORD *)v0 + 2);
}
else
{
v9 = delete_index++;
v10 = *v0;
v0 = (unsigned int *)*((_QWORD *)v0 + 2);
delete_these_pids[v9] = v10;
}
}
while ( v0 );
while ( 1 )
{
v7 = delete_index - 1;
delete_index = v7;
if ( v7 <= 0 )
break;
active_list_delete(delete_these_pids[v7]);
}
return v1;
}

canonical_decompiled_code:
__int64 func14()
{
unsigned int *v0; // rbx
unsigned int v1; // er12
char *v2; // rdi
int v3; // edx
__int64 v4; // rax
unsigned int v5; // edi
unsigned int v6; // er8
int v7; // eax
__int64 v9; // rax
int v10; // edx
struct timeval v11; // [rsp+0h] [rbp-28h] BYREF

v0 = (unsigned int *)active_list_head;
if ( !active_list_head )
return 0LL;
delete_index = 0;
v1 = 0;
do
{
++v1;
v2 = func12(*v0);
if ( v2 )
{
v3 = strlen(v2);
v4 = (int)*v0;
v5 = *v0;
if ( dword_405278[78 * v4] < v3 )
{
dword_405278[78 * v4] = v3;
func13(v5, 0);
v4 = (int)*v0;
}
if ( !dword_405290[78 * v4] )
{
gettimeofday(&v11, 0LL);
if ( func7(&v11, (_QWORD *)(312LL * (int)*v0 + 4215424)) > 300000 )
{
func13(v6, 1);
dword_405290[78 * *v0] = 1;
}
}
v0 = (unsigned int *)*((_QWORD *)v0 + 2);
}
else
{
v9 = delete_index++;
v10 = *v0;
v0 = (unsigned int *)*((_QWORD *)v0 + 2);
delete_these_pids[v9] = v10;
}
}
while ( v0 );
while ( 1 )
{
v7 = delete_index - 1;
delete_index = v7;
if ( v7 <= 0 )
break;
func9(delete_these_pids[v7]);
}
return v1;
}

original_code:
int check_active_pids(void) {
 active_pids_t *ptmp;
 char *pcmd;
 int len, active_pid_count;
 struct timeval timenow;
 long int time_diff;
 if ( active_list_head == ((void *)0) )
  return 0;
 active_pid_count = 0;
 delete_index = 0;
 ptmp = active_list_head;
 while ( ptmp ) {
  active_pid_count++;
  pcmd = get_cmdline(ptmp->pid);
   if ( pcmd == ((void *)0) ) {
    delete_these_pids[delete_index++] = ptmp->pid;
    ptmp = ptmp->next;
    continue;
   }
  len = strlen(pcmd);
  if ( len > mydentry[ptmp->pid].cmdline_length ) {
   mydentry[ptmp->pid].cmdline_length = len;
   print_process(ptmp->pid, 0);
  }
  if ( mydentry[ptmp->pid].reported == 0 ) {
   gettimeofday(&timenow, ((void *)0));
   time_diff = timeval_subtract(&timenow, &mydentry[ptmp->pid].timestamp);
   if ( time_diff > 300000 ) {
    print_process(ptmp->pid, 1);
    mydentry[ptmp->pid].reported = 1;
   }
  }
  ptmp = ptmp->next;
 }
 for ( delete_index = delete_index - 1; delete_index > 0; delete_index--) {
  active_list_delete(delete_these_pids[delete_index]);
 }
 return active_pid_count;
}

canonical_original_code:
int check_active_pids() {
 struct active_pids_s *ptmp;
 char *pcmd;
 int len; int active_pid_count;
 struct timeval timenow;
 long time_diff;
 if ( active_list_head == ((void *)0) )
  return 0;
 active_pid_count = 0;
 delete_index = 0;
 ptmp = active_list_head;
 while ( ptmp ) {
  active_pid_count++;
  pcmd = get_cmdline(ptmp->pid);
   if ( pcmd == ((void *)0) ) {
    delete_these_pids[delete_index++] = ptmp->pid;
    ptmp = ptmp->next;
    continue;
   }
  len = strlen(pcmd);
  if ( len > mydentry[ptmp->pid].cmdline_length ) {
   mydentry[ptmp->pid].cmdline_length = len;
   print_process(ptmp->pid, 0);
  }
  if ( mydentry[ptmp->pid].reported == 0 ) {
   gettimeofday(&timenow, ((void *)0));
   time_diff = timeval_subtract(&timenow, &mydentry[ptmp->pid].timestamp);
   if ( time_diff > 300000 ) {
    print_process(ptmp->pid, 1);
    mydentry[ptmp->pid].reported = 1;
   }
  }
  ptmp = ptmp->next;
 }
 for ( delete_index = delete_index - 1; delete_index > 0; delete_index--) {
  active_list_delete(delete_these_pids[delete_index]);
 }
 return active_pid_count;
}

variable_types:
pcmd
char *

variable_types: len int

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


main

canonical_name: main

repo: chandangoopta/combo

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
size_t v4; // rbx
void *v5; // rbp
int result; // eax

if ( argc )
{
v3 = argv[1];
v4 = strlen(v3);
v5 = calloc(1uLL, (int)v4 + 1);
memcpy(v5, v3, v4 + 1);
findcombo(v5);
free(v5);
putchar(10);
result = fac((unsigned int)v4, 0LL);
}
else
{
puts("Error: missing argument, string ");
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
size_t v4; // rbx
void *v5; // rbp
int result; // eax

if ( argc )
{
v3 = argv[1];
v4 = strlen(v3);
v5 = calloc(1uLL, (int)v4 + 1);
memcpy(v5, v3, v4 + 1);
func9(v5);
free(v5);
putchar(10);
result = func7((unsigned int)v4, 0LL);
}
else
{
puts("Error: missing argument, string ");
result = 1;
}
return result;
}

original_code:
int main (int argc, char const* argv[])
{
 if(argc==0) {
     printf("Error: missing argument, string \n");
     return 1;
    }
 int len=strlen(argv[1]);
 char *str=calloc(sizeof(char), len+1);
 strcpy(str, argv[1]);
 findcombo(str,0);
 free(str);
 printf("\n");
 return fac(len);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 if(argc==0) {
     printf("Error: missing argument, string \n");
     return 1;
    }
 int len = strlen(argv[1]);
 char *str = calloc(sizeof(char), len+1);
 strcpy(str, argv[1]);
 findcombo(str,0);
 free(str);
 printf("\n");
 return fac(len);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
str
char *


fac

decompiled_code:
__int64 __fastcall fac(int a1)
{
__int64 v1; // rdx
__int64 v2; // r8
__int64 i; // rax

if ( a1 <= 1 )
return 1LL;
v1 = a1;
v2 = 1LL;
for ( i = a1 - 1LL; ; --i )
{
v2 *= v1;
v1 = i;
if ( i == a1 - 1LL - (unsigned int)(a1 - 2) )
break;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 v1; // rdx
__int64 v2; // r8
__int64 i; // rax

if ( a1 <= 1 )
return 1LL;
v1 = a1;
v2 = 1LL;
for ( i = a1 - 1LL; ; --i )
{
v2 *= v1;
v1 = i;
if ( i == a1 - 1LL - (unsigned int)(a1 - 2) )
break;
}
return v2;
}

original_code:
long fac(int n) {
 long f;
 for(f=1; n>1; n--) f*=n;
 return f;
}

variable_types: n int

variable_types: f long


swap

decompiled_code:
__int64 __fastcall swap(__int64 a1, int a2, int a3)
{
char *v3; // rsi
char *v4; // rdi
char v5; // al

v3 = (char *)(a1 + a2);
v4 = (char *)(a3 + a1);
v5 = *v3;
*v3 = *v4;
*v4 = v5;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, int a3)
{
char *v3; // rsi
char *v4; // rdi
char v5; // al

v3 = (char *)(a1 + a2);
v4 = (char *)(a3 + a1);
v5 = *v3;
*v3 = *v4;
*v4 = v5;
return 0LL;
}

original_code:
int swap(char *tekst, int pos, int npos){
 char tmpchr=tekst[pos];
 tekst[pos]=tekst[npos];
 tekst[npos]=tmpchr;
 return 0;
}

canonical_original_code:
int swap(char *tekst, int pos, int npos){
 char tmpchr = tekst[pos];
 tekst[pos]=tekst[npos];
 tekst[npos]=tmpchr;
 return 0;
}

variable_types:
tekst
char *

variable_types: pos int

variable_types: npos int

variable_types: tmpchr char


findcombo

decompiled_code:
__int64 __fastcall findcombo(void *dest, int a2)
{
int v3; // edi
void *v4; // r15
int v5; // ebp
int v6; // er12
size_t v7; // rax
size_t n; // [rsp+8h] [rbp-50h]
int v10; // [rsp+18h] [rbp-40h]

v3 = strlen((const char *)dest) - a2;
v10 = fac(v3);
v4 = calloc(1uLL, v3);
if ( v10 > 0 )
{
v5 = a2;
v6 = 1;
do
{
v7 = strlen((const char *)dest);
v5 += v5 < v7 - 1;
if ( v10 <= 2 )
{
++v6;
swap((__int64)dest, a2, v5);
printf("%s ", (const char *)dest);
}
else
{
n = v7 + 1;
memcpy(v4, dest, v7 + 1);
v6 += findcombo(dest);
memcpy(dest, v4, n);
swap((__int64)dest, a2, v5);
}
}
while ( v6 <= v10 );
}
free(v4);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func9(void *dest, int a2)
{
int v3; // edi
void *v4; // r15
int v5; // ebp
int v6; // er12
size_t v7; // rax
size_t n; // [rsp+8h] [rbp-50h]
int v10; // [rsp+18h] [rbp-40h]

v3 = strlen((const char *)dest) - a2;
v10 = func7(v3);
v4 = calloc(1uLL, v3);
if ( v10 > 0 )
{
v5 = a2;
v6 = 1;
do
{
v7 = strlen((const char *)dest);
v5 += v5 < v7 - 1;
if ( v10 <= 2 )
{
++v6;
func8((__int64)dest, a2, v5);
printf("%s ", (const char *)dest);
}
else
{
n = v7 + 1;
memcpy(v4, dest, v7 + 1);
v6 += func9(dest);
memcpy(dest, v4, n);
func8((__int64)dest, a2, v5);
}
}
while ( v6 <= v10 );
}
free(v4);
return (unsigned int)v10;
}

original_code:
int findcombo(char *tekst, int pos){
 int counter=1;
 int npos=pos;
 int len=strlen(tekst);
 int fact=fac(len-pos);
 char *tmpstr=calloc(sizeof(char), len-pos);
 while(counter<=fact) {
  if(pos<(strlen(tekst)-1)) pos++;
  if(fact>2){
   strcpy(tmpstr,tekst);
   counter+=findcombo(tekst,npos+1);
   strcpy(tekst,tmpstr);
   swap(tekst, npos, pos);
  }
  else {
   counter++;
   swap(tekst,npos,pos);
   printf("%s ", tekst);
  }
 }
 free(tmpstr);
 return fact;
}

canonical_original_code:
int findcombo(char *tekst, int pos){
 int counter = 1;
 int npos = pos;
 int len = strlen(tekst);
 int fact = fac(len-pos);
 char *tmpstr = calloc(sizeof(char), len-pos);
 while(counter<=fact) {
  if(pos<(strlen(tekst)-1)) pos++;
  if(fact>2){
   strcpy(tmpstr,tekst);
   counter+=findcombo(tekst,npos+1);
   strcpy(tekst,tmpstr);
   swap(tekst, npos, pos);
  }
  else {
   counter++;
   swap(tekst,npos,pos);
   printf("%s ", tekst);
  }
 }
 free(tmpstr);
 return fact;
}

variable_types: counter int

variable_types: fact int

variable_types:
tmpstr
char *


repo: chansen/c-timestamp

variable_types:
dst
char *

variable_types:
len
unsigned long

variable_types: struct timestamp_t { long sec; int nsec; short offset; } *

variable_types: precision int

variable_types:
p
unsigned char *

variable_types:
sec
unsigned long

variable_types:
rdn
unsigned int

variable_types:
v
unsigned int

variable_types:
y
unsigned short

variable_types:
m
unsigned short

variable_types:
d
unsigned short

variable_types:
dlen
unsigned long

return_type: unsigned long


decompiled_code:
unsigned __int64 __fastcall timestamp_format(__int64 a1, unsigned __int64 a2, __int64 a3)
{
int v4; // eax
int v5; // ecx

if ( !(unsigned __int8)timestamp_valid(a3) )
return 0LL;
v4 = *(_DWORD *)(a3 + 8);
v5 = 0;
if ( v4 )
{
v5 = 3;
if ( __ROR4__(1757569337 * v4, 6) > 0x10C6u )
v5 = __ROR4__(652835029 * v4, 3) < 0x418938u ? 6 : 9;
}
return timestamp_format_internal(a1, a2, a3, v5);
}

original_code:
size_t
timestamp_format(char *dst, size_t len, const timestamp_t *tsp) {
    uint32_t f;
    int precision;
    if (!timestamp_valid(tsp))
        return 0;
    f = tsp->nsec;
    if (!f)
        precision = 0;
    else {
        if ((f % 1000000) == 0) precision = 3;
        else if ((f % 1000) == 0) precision = 6;
        else precision = 9;
    }
    return timestamp_format_internal(dst, len, tsp, precision);
}

canonical_original_code:
unsigned long timestamp_format(char *dst, unsigned long len, struct timestamp_t *tsp) {
    unsigned int f;
    int precision;
    if (!timestamp_valid(tsp))
        return 0;
    f = tsp->nsec;
    if (!f)
        precision = 0;
    else {
        if ((f % 1000000) == 0) precision = 3;
        else if ((f % 1000) == 0) precision = 6;
        else precision = 9;
    }
    return timestamp_format_internal(dst, len, tsp, precision);
}

variable_types:
f
unsigned int

user_defined_types: struct timestamp_t { long sec; int nsec; short offset; }


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

if ( (unsigned __int8)timestamp_valid(a3) && a4 <= 9 )
result = timestamp_format_internal(a1, a2, a3, a4);
else
result = 0LL;
return result;
}

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

if ( (unsigned __int8)timestamp_valid(a3) && a4 <= 9 )
result = func1(a1, a2, a3, a4);
else
result = 0LL;
return result;
}

original_code:
size_t
timestamp_format_precision(char *dst, size_t len, const timestamp_t *tsp, int precision) {
    if (!timestamp_valid(tsp) || precision < 0 || precision > 9)
        return 0;
    return timestamp_format_internal(dst, len, tsp, precision);
}

canonical_original_code:
unsigned long timestamp_format_precision(char *dst, unsigned long len, struct timestamp_t *tsp, int precision) {
    if (!timestamp_valid(tsp) || precision < 0 || precision > 9)
        return 0;
    return timestamp_format_internal(dst, len, tsp, precision);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int8 v3; // al
__int64 v4; // rdx
unsigned __int8 v5; // al
unsigned __int8 v6; // al
__int64 v7; // rdx
unsigned __int8 v8; // al
__int64 v9; // rdx
unsigned __int8 v10; // al
__int64 v11; // rdx
unsigned __int8 v12; // al
__int64 v14; // [rsp+0h] [rbp-18h] BYREF
int v15; // [rsp+8h] [rbp-10h]
__int16 v16; // [rsp+Ch] [rbp-Ch]

v14 = 0LL;
v16 = 0;
v15 = -1;
v3 = timestamp_valid(&v14, argv, envp);
ok_at_loc("t/valid.c", 12LL, v3 ^ 1u, "nsec out of range", 0LL);
v15 = 1000000000;
v5 = timestamp_valid(&v14, 12LL, v4);
ok_at_loc("t/valid.c", 15LL, v5 ^ 1u, "nsec out of range", 0LL);
v15 = 0;
v16 = -1440;
v6 = timestamp_valid(&v14, 15LL, 4294965856LL);
ok_at_loc("t/valid.c", 19LL, v6 ^ 1u, "offset out of range", 0LL);
v16 = 1440;
v8 = timestamp_valid(&v14, 19LL, v7);
ok_at_loc("t/valid.c", 22LL, v8 ^ 1u, "offset out of range", 0LL);
v16 = 0;
v14 = 0xFFFFFFF1886E08FFLL;
v10 = timestamp_valid(&v14, 0LL, v9);
ok_at_loc("t/valid.c", 26LL, v10 ^ 1u, "sec out of range", 0LL);
v14 = 0x3AFFF592C4LL;
v12 = timestamp_valid(&v14, 26LL, v11);
ok_at_loc("t/valid.c", 28LL, v12 ^ 1u, "sec out of range", 0LL);
return exit_status();
}

original_code:
int
main() {
    timestamp_t ts;
    ts.sec = 0;
    ts.offset = 0;
    ts.nsec = -1;
    ok_at_loc("t/valid.c", 12, !timestamp_valid(&ts), "nsec out of range", ((void *)0));
    ts.nsec = 1000000000;
    ok_at_loc("t/valid.c", 15, !timestamp_valid(&ts), "nsec out of range", ((void *)0));
    ts.nsec = 0;
    ts.offset = -23 * 60 - 60;
    ok_at_loc("t/valid.c", 19, !timestamp_valid(&ts), "offset out of range", ((void *)0));
    ts.offset = +23 * 60 + 60;
    ok_at_loc("t/valid.c", 22, !timestamp_valid(&ts), "offset out of range", ((void *)0));
    ts.offset = 0;
    ts.sec = -62135596801L;
    ok_at_loc("t/valid.c", 26, !timestamp_valid(&ts), "sec out of range", ((void *)0));
    ts.sec = 253402387140L;
    ok_at_loc("t/valid.c", 28, !timestamp_valid(&ts), "sec out of range", ((void *)0));
    return exit_status();
}

canonical_original_code:
int main() {
    struct timestamp_t ts;
    ts.sec = 0;
    ts.offset = 0;
    ts.nsec = -1;
    ok_at_loc("t/valid.c", 12, !timestamp_valid(&ts), "nsec out of range", ((void *)0));
    ts.nsec = 1000000000;
    ok_at_loc("t/valid.c", 15, !timestamp_valid(&ts), "nsec out of range", ((void *)0));
    ts.nsec = 0;
    ts.offset = -23 * 60 - 60;
    ok_at_loc("t/valid.c", 19, !timestamp_valid(&ts), "offset out of range", ((void *)0));
    ts.offset = +23 * 60 + 60;
    ok_at_loc("t/valid.c", 22, !timestamp_valid(&ts), "offset out of range", ((void *)0));
    ts.offset = 0;
    ts.sec = -62135596801L;
    ok_at_loc("t/valid.c", 26, !timestamp_valid(&ts), "sec out of range", ((void *)0));
    ts.sec = 253402387140L;
    ok_at_loc("t/valid.c", 28, !timestamp_valid(&ts), "sec out of range", ((void *)0));
    return exit_status();
}

variable_types: struct timestamp_t { long sec; int nsec; short offset; }


decompiled_code:
__int64 __fastcall timestamp_compare(__int64 a1, __int64 a2)
{
__int64 result; // rax
int v3; // eax
bool v4; // cc

result = 0xFFFFFFFFLL;
if ( *(_QWORD *)a1 >= *(_QWORD *)a2 )
{
result = 1LL;
if ( *(_QWORD *)a1 <= *(_QWORD *)a2 )
{
v3 = *(_DWORD *)(a2 + 8);
v4 = *(_DWORD *)(a1 + 8) < v3;
result = *(_DWORD *)(a1 + 8) > v3;
if ( v4 )
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 result; // rax
int v3; // eax
bool v4; // cc

result = 0xFFFFFFFFLL;
if ( *(_QWORD *)a1 >= *(_QWORD *)a2 )
{
result = 1LL;
if ( *(_QWORD *)a1 <= *(_QWORD *)a2 )
{
v3 = *(_DWORD *)(a2 + 8);
v4 = *(_DWORD *)(a1 + 8) < v3;
result = *(_DWORD *)(a1 + 8) > v3;
if ( v4 )
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int
timestamp_compare(const timestamp_t *t1, const timestamp_t *t2) {
    if (t1->sec < t2->sec)
        return -1;
    if (t1->sec > t2->sec)
        return 1;
    if (t1->nsec < t2->nsec)
        return -1;
    if (t1->nsec > t2->nsec)
        return 1;
    return 0;
}

canonical_original_code:
int timestamp_compare(struct timestamp_t *t1, struct timestamp_t *t2) {
    if (t1->sec < t2->sec)
        return -1;
    if (t1->sec > t2->sec)
        return 1;
    if (t1->nsec < t2->nsec)
        return -1;
    if (t1->nsec > t2->nsec)
        return 1;
    return 0;
}


decompiled_code:
unsigned int *__fastcall timestamp_to_tm(__int64 a1, unsigned int *a2, char a3)
{
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rbp

if ( !(unsigned __int8)timestamp_valid() )
return 0LL;
v4 = *(_QWORD *)a1 + 0xE77934880LL;
if ( a3 )
v4 += 60 * *(__int16 *)(a1 + 12);
v5 = v4 % 0x15180;
rdn_to_struct_tm(v4 / 0x15180, a2);
*a2 = (unsigned int)v5 % 0x3C;
a2[2] = (unsigned int)v5 / 0x3C / 0x3C;
a2[1] = (unsigned int)v5 / 0x3C % 0x3C;
return a2;
}

canonical_decompiled_code:
unsigned int *__fastcall func1(__int64 a1, unsigned int *a2, char a3)
{
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rbp

if ( !(unsigned __int8)timestamp_valid() )
return 0LL;
v4 = *(_QWORD *)a1 + 0xE77934880LL;
if ( a3 )
v4 += 60 * *(__int16 *)(a1 + 12);
v5 = v4 % 0x15180;
func0(v4 / 0x15180, a2);
*a2 = (unsigned int)v5 % 0x3C;
a2[2] = (unsigned int)v5 / 0x3C / 0x3C;
a2[1] = (unsigned int)v5 / 0x3C % 0x3C;
return a2;
}

original_code:
static struct tm *
timestamp_to_tm(const timestamp_t *tsp, struct tm *tmp, const _Bool local) {
    uint64_t sec;
    uint32_t rdn, sod;
    if (!timestamp_valid(tsp))
        return ((void *)0);
    sec = tsp->sec + 62135683200L;
    if (local)
        sec += tsp->offset * 60;
    rdn = sec / 86400;
    sod = sec % 86400;
    rdn_to_struct_tm(rdn, tmp);
    tmp->tm_sec = sod % 60; sod /= 60;
    tmp->tm_min = sod % 60; sod /= 60;
    tmp->tm_hour = sod;
    return tmp;
}

canonical_original_code:
static struct tm *timestamp_to_tm(struct timestamp_t *tsp, struct tm *tmp, _Bool local) {
    unsigned long sec;
    unsigned int rdn; unsigned int sod;
    if (!timestamp_valid(tsp))
        return ((void *)0);
    sec = tsp->sec + 62135683200L;
    if (local)
        sec += tsp->offset * 60;
    rdn = sec / 86400;
    sod = sec % 86400;
    rdn_to_struct_tm(rdn, tmp);
    tmp->tm_sec = sod % 60; sod /= 60;
    tmp->tm_min = sod % 60; sod /= 60;
    tmp->tm_hour = sod;
    return tmp;
}

variable_types:
tmp
struct tm *

variable_types:
sod
unsigned int

return_type: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
unsigned int *__fastcall timestamp_to_tm_local(__int64 a1, unsigned int *a2)
{
return timestamp_to_tm(a1, a2, 1);
}

canonical_decompiled_code:
unsigned int *__fastcall func2(__int64 a1, unsigned int *a2)
{
return func1(a1, a2, 1);
}

original_code:
struct tm *
timestamp_to_tm_local(const timestamp_t *tsp, struct tm *tmp) {
    return timestamp_to_tm(tsp, tmp, 1);
}

canonical_original_code:
struct tm *timestamp_to_tm_local(struct timestamp_t *tsp, struct tm *tmp) {
    return timestamp_to_tm(tsp, tmp, 1);
}

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


decompiled_code:
unsigned int *__fastcall timestamp_to_tm_utc(__int64 a1, unsigned int *a2)
{
return timestamp_to_tm(a1, a2, 0);
}

canonical_decompiled_code:
unsigned int *__fastcall func3(__int64 a1, unsigned int *a2)
{
return func1(a1, a2, 0);
}

original_code:
struct tm *
timestamp_to_tm_utc(const timestamp_t *tsp, struct tm *tmp) {
    return timestamp_to_tm(tsp, tmp, 0);
}

canonical_original_code:
struct tm *timestamp_to_tm_utc(struct timestamp_t *tsp, struct tm *tmp) {
    return timestamp_to_tm(tsp, tmp, 0);
}


decompiled_code:
_BOOL8 __fastcall leap_year(__int16 a1)
{
_BOOL8 result; // rax
__int16 v2; // di

result = 0LL;
if ( (a1 & 3) == 0 )
{
v2 = 23593 * a1;
result = 1LL;
if ( __ROR2__(v2, 2) <= 0x28Fu )
result = __ROR2__(v2, 4) <= 0xA3u;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int16 a1)
{
_BOOL8 result; // rax
__int16 v2; // di

result = 0LL;
if ( (a1 & 3) == 0 )
{
v2 = 23593 * a1;
result = 1LL;
if ( __ROR2__(v2, 2) <= 0x28Fu )
result = __ROR2__(v2, 4) <= 0xA3u;
}
return result;
}

canonical_original_code:
static int leap_year(unsigned short y) {
    return ((y & 3) == 0 && (y % 100 != 0 || y % 400 == 0));
}


decompiled_code:
__int64 __fastcall month_days(__int16 a1, unsigned __int16 a2)
{
_BOOL8 v2; // rax

v2 = 0LL;
if ( a2 == 2 )
v2 = leap_year(a1);
return days_0[13 * v2 + a2];
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, unsigned __int16 a2)
{
_BOOL8 v2; // rax

v2 = 0LL;
if ( a2 == 2 )
v2 = func0(a1);
return days_0[13 * v2 + a2];
}

variable_types:
days
unsigned char [2][13]

return_type: unsigned char


original_code:
static int
parse_2d(const unsigned char * const p, size_t i, uint16_t *vp) {
    unsigned char d0, d1;
    if (((d0 = p[i + 0] - '0') > 9) ||
        ((d1 = p[i + 1] - '0') > 9))
        return 1;
    *vp = d0 * 10 + d1;
    return 0;
}

canonical_original_code:
static int parse_2d(unsigned char *p, unsigned long i, unsigned short *vp) {
    unsigned char d0; unsigned char d1;
    if (((d0 = p[i + 0] - '0') > 9) ||
        ((d1 = p[i + 1] - '0') > 9))
        return 1;
    *vp = d0 * 10 + d1;
    return 0;
}

variable_types:
i
unsigned long

variable_types:
vp
unsigned short *


decompiled_code:
__int64 __fastcall timestamp_parse(_BYTE *a1, unsigned __int64 a2)
{
unsigned int v3; // er11
__int64 v5; // r9
__int64 v6; // r10
int v7; // er14
int v8; // ecx
int v9; // er12
unsigned __int16 v10; // bx
unsigned __int16 v11; // r8
char v12; // r13
unsigned __int64 v13; // r9
__int64 v14; // rdi
int v15; // ebp
__int64 v16; // r15
char *v17; // rdx
int i; // ecx
unsigned __int8 v19; // al
unsigned __int16 v20; // [rsp+0h] [rbp-46h]
unsigned __int16 v21; // [rsp+2h] [rbp-44h]
unsigned __int16 v22; // [rsp+4h] [rbp-42h]
unsigned __int16 v23; // [rsp+Ah] [rbp-3Ch] BYREF
unsigned __int16 v24; // [rsp+Ch] [rbp-3Ah] BYREF
unsigned __int16 v25; // [rsp+Eh] [rbp-38h] BYREF
unsigned __int16 v26; // [rsp+10h] [rbp-36h] BYREF
unsigned __int16 v27; // [rsp+12h] [rbp-34h] BYREF
unsigned __int16 v28[25]; // [rsp+14h] [rbp-32h] BYREF

if ( a2 > 0x13 )
{
if ( a1[4] != 45 || a1[7] != 45 || a1[13] != 58 || a1[16] != 58 || (a1[10] & 0xDF) != 84 && a1[10] != 32 )
return 1;
if ( (unsigned int)parse_4d_constprop_0(a1, &v23) )
return 1;
if ( !v23 )
return 1;
if ( (unsigned int)parse_2d((__int64)a1, 5LL, &v24) )
return 1;
if ( (unsigned __int16)(v24 - 1) > 0xBu )
return 1;
if ( (unsigned int)parse_2d((__int64)a1, 8LL, &v25) )
return 1;
if ( (unsigned __int16)(v25 - 1) > 0x1Eu )
return 1;
if ( (unsigned int)parse_2d((__int64)a1, 11LL, &v26) )
return 1;
if ( v26 > 0x17u )
return 1;
if ( (unsigned int)parse_2d((__int64)a1, 14LL, &v27) )
return 1;
if ( v27 > 0x3Bu )
return 1;
v3 = parse_2d((__int64)a1, 17LL, v28);
if ( v3 )
return 1;
v7 = v28[0];
if ( v28[0] > 0x3Bu )
return 1;
v8 = v24;
v9 = v25;
v10 = v23;
v11 = v24;
if ( v25 > 0x1Cu && (unsigned __int16)v9 > (unsigned __int8)month_days(v23, v24) )
return 1;
if ( v11 <= 2u )
v23 = --v10;
v12 = a1[19];
v13 = (unsigned __int64)&a1[v5];
v14 = (__int64)(a1 + 20);
v20 = DayOffset[v8];
v21 = v26;
v22 = v27;
if ( v12 == 46 )
{
v17 = a1 + 20;
for ( i = 0; ; i = v19 + 10 * i )
{
if ( (unsigned __int64)v17 >= v13 )
{
if ( (unsigned __int64)&v17[-v14 - 1] <= 8 && v17 != (char *)v13 )
{
v15 = i * Pow10[9LL - (_QWORD)&v17[-v14]];
LABEL_49:
v12 = *v17;
v14 = (__int64)(v17 + 1);
goto LABEL_28;
}
return 1;
}
v19 = *v17 - 48;
if ( v19 > 9u )
break;
++v17;
}
if ( (unsigned __int64)&v17[-v14 - 1] <= 8 )
{
v15 = Pow10[9LL - (_QWORD)&v17[-v14]] * i;
goto LABEL_49;
}
return 1;
}
v15 = 0;
LABEL_28:
if ( (v12 & 0xDF) == 90 )
{
v16 = v14;
LOWORD(v14) = 0;
}
else
{
v16 = v14 + 5;
if ( v14 + 5 < v13
|| ((v12 - 43) & 0xFD) != 0
|| *(_BYTE *)(v14 + 2) != 58
|| (unsigned int)parse_2d(v14, 0LL, &v26)
|| v26 > 0x17u
|| (unsigned int)parse_2d(v14, 3LL, &v27)
|| v27 > 0x3Bu )
{
return 1;
}
LOWORD(v14) = 60 * v26;
LODWORD(v14) = v27 + (_DWORD)v14;
if ( v12 == 45 )
LODWORD(v14) = -(int)v14;
}
if ( v16 == v13 )
{
*(_DWORD *)(v6 + 8) = v15;
*(_WORD *)(v6 + 12) = v14;
*(_QWORD *)v6 = 86400 * (v20 + ((1461 * v10) >> 2) - v10 / 0x64u + v10 / 0x190u + v9 - 306 - 719163LL)
+ v7
+ 60 * v22
+ 3600 * v21
- 60 * (__int16)v14;
return v3;
}
return 1;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, unsigned __int64 a2)
{
unsigned int v3; // er11
__int64 v5; // r9
__int64 v6; // r10
int v7; // er14
int v8; // ecx
int v9; // er12
unsigned __int16 v10; // bx
unsigned __int16 v11; // r8
char v12; // r13
unsigned __int64 v13; // r9
__int64 v14; // rdi
int v15; // ebp
__int64 v16; // r15
char *v17; // rdx
int i; // ecx
unsigned __int8 v19; // al
unsigned __int16 v20; // [rsp+0h] [rbp-46h]
unsigned __int16 v21; // [rsp+2h] [rbp-44h]
unsigned __int16 v22; // [rsp+4h] [rbp-42h]
unsigned __int16 v23; // [rsp+Ah] [rbp-3Ch] BYREF
unsigned __int16 v24; // [rsp+Ch] [rbp-3Ah] BYREF
unsigned __int16 v25; // [rsp+Eh] [rbp-38h] BYREF
unsigned __int16 v26; // [rsp+10h] [rbp-36h] BYREF
unsigned __int16 v27; // [rsp+12h] [rbp-34h] BYREF
unsigned __int16 v28[25]; // [rsp+14h] [rbp-32h] BYREF

if ( a2 > 0x13 )
{
if ( a1[4] != 45 || a1[7] != 45 || a1[13] != 58 || a1[16] != 58 || (a1[10] & 0xDF) != 84 && a1[10] != 32 )
return 1;
if ( (unsigned int)func3(a1, &v23) )
return 1;
if ( !v23 )
return 1;
if ( (unsigned int)func2((__int64)a1, 5LL, &v24) )
return 1;
if ( (unsigned __int16)(v24 - 1) > 0xBu )
return 1;
if ( (unsigned int)func2((__int64)a1, 8LL, &v25) )
return 1;
if ( (unsigned __int16)(v25 - 1) > 0x1Eu )
return 1;
if ( (unsigned int)func2((__int64)a1, 11LL, &v26) )
return 1;
if ( v26 > 0x17u )
return 1;
if ( (unsigned int)func2((__int64)a1, 14LL, &v27) )
return 1;
if ( v27 > 0x3Bu )
return 1;
v3 = func2((__int64)a1, 17LL, v28);
if ( v3 )
return 1;
v7 = v28[0];
if ( v28[0] > 0x3Bu )
return 1;
v8 = v24;
v9 = v25;
v10 = v23;
v11 = v24;
if ( v25 > 0x1Cu && (unsigned __int16)v9 > (unsigned __int8)func1(v23, v24) )
return 1;
if ( v11 <= 2u )
v23 = --v10;
v12 = a1[19];
v13 = (unsigned __int64)&a1[v5];
v14 = (__int64)(a1 + 20);
v20 = DayOffset[v8];
v21 = v26;
v22 = v27;
if ( v12 == 46 )
{
v17 = a1 + 20;
for ( i = 0; ; i = v19 + 10 * i )
{
if ( (unsigned __int64)v17 >= v13 )
{
if ( (unsigned __int64)&v17[-v14 - 1] <= 8 && v17 != (char *)v13 )
{
v15 = i * Pow10[9LL - (_QWORD)&v17[-v14]];
LABEL_49:
v12 = *v17;
v14 = (__int64)(v17 + 1);
goto LABEL_28;
}
return 1;
}
v19 = *v17 - 48;
if ( v19 > 9u )
break;
++v17;
}
if ( (unsigned __int64)&v17[-v14 - 1] <= 8 )
{
v15 = Pow10[9LL - (_QWORD)&v17[-v14]] * i;
goto LABEL_49;
}
return 1;
}
v15 = 0;
LABEL_28:
if ( (v12 & 0xDF) == 90 )
{
v16 = v14;
LOWORD(v14) = 0;
}
else
{
v16 = v14 + 5;
if ( v14 + 5 < v13
|| ((v12 - 43) & 0xFD) != 0
|| *(_BYTE *)(v14 + 2) != 58
|| (unsigned int)func2(v14, 0LL, &v26)
|| v26 > 0x17u
|| (unsigned int)func2(v14, 3LL, &v27)
|| v27 > 0x3Bu )
{
return 1;
}
LOWORD(v14) = 60 * v26;
LODWORD(v14) = v27 + (_DWORD)v14;
if ( v12 == 45 )
LODWORD(v14) = -(int)v14;
}
if ( v16 == v13 )
{
*(_DWORD *)(v6 + 8) = v15;
*(_WORD *)(v6 + 12) = v14;
*(_QWORD *)v6 = 86400 * (v20 + ((1461 * v10) >> 2) - v10 / 0x64u + v10 / 0x190u + v9 - 306 - 719163LL)
+ v7
+ 60 * v22
+ 3600 * v21
- 60 * (__int16)v14;
return v3;
}
return 1;
}
return 1LL;
}

original_code:
int
timestamp_parse(const char *str, size_t len, timestamp_t *tsp) {
    const unsigned char *cur, *end;
    unsigned char ch;
    uint16_t year, month, day, hour, min, sec;
    uint32_t rdn, sod, nsec;
    int16_t offset;
    cur = (const unsigned char *)str;
    if (len < 20 ||
        cur[4] != '-' || cur[7] != '-' ||
        cur[13] != ':' || cur[16] != ':')
        return 1;
    ch = cur[10];
    if (!(ch == 'T' || ch == ' ' || ch == 't'))
        return 1;
    if (parse_4d(cur, 0, &year) || year < 1 ||
        parse_2d(cur, 5, &month) || month < 1 || month > 12 ||
        parse_2d(cur, 8, &day) || day < 1 || day > 31 ||
        parse_2d(cur, 11, &hour) || hour > 23 ||
        parse_2d(cur, 14, &min) || min > 59 ||
        parse_2d(cur, 17, &sec) || sec > 59)
        return 1;
    if (day > 28 && day > month_days(year, month))
        return 1;
    if (month < 3)
        year--;
    rdn = (1461 * year)/4 - year/100 + year/400 + DayOffset[month] + day - 306;
    sod = hour * 3600 + min * 60 + sec;
    end = cur + len;
    cur = cur + 19;
    offset = nsec = 0;
    ch = *cur++;
    if (ch == '.') {
        const unsigned char *start;
        size_t ndigits;
        start = cur;
        for (; cur < end; cur++) {
            const unsigned char digit = *cur - '0';
            if (digit > 9)
                break;
            nsec = nsec * 10 + digit;
        }
        ndigits = cur - start;
        if (ndigits < 1 || ndigits > 9)
            return 1;
        nsec *= Pow10[9 - ndigits];
        if (cur == end)
            return 1;
        ch = *cur++;
    }
    if (!(ch == 'Z' || ch == 'z')) {
        if (cur + 5 < end || !(ch == '+' || ch == '-') || cur[2] != ':')
            return 1;
        if (parse_2d(cur, 0, &hour) || hour > 23 ||
            parse_2d(cur, 3, &min) || min > 59)
            return 1;
        offset = hour * 60 + min;
        if (ch == '-')
            offset *= -1;
        cur += 5;
    }
    if (cur != end)
        return 1;
    tsp->sec = ((int64_t)rdn - 719163) * 86400 + sod - offset * 60;
    tsp->nsec = nsec;
    tsp->offset = offset;
    return 0;
}

canonical_original_code:
int timestamp_parse(char *str, unsigned long len, struct timestamp_t *tsp) {
    unsigned char *cur; unsigned char *end;
    unsigned char ch;
    unsigned short year; unsigned short month; unsigned short day; unsigned short hour; unsigned short min; unsigned short sec;
    unsigned int rdn; unsigned int sod; unsigned int nsec;
    short offset;
    cur = (unsigned char *)str;
    if (len < 20 ||
        cur[4] != '-' || cur[7] != '-' ||
        cur[13] != ':' || cur[16] != ':')
        return 1;
    ch = cur[10];
    if (!(ch == 'T' || ch == ' ' || ch == 't'))
        return 1;
    if (parse_4d(cur, 0, &year) || year < 1 ||
        parse_2d(cur, 5, &month) || month < 1 || month > 12 ||
        parse_2d(cur, 8, &day) || day < 1 || day > 31 ||
        parse_2d(cur, 11, &hour) || hour > 23 ||
        parse_2d(cur, 14, &min) || min > 59 ||
        parse_2d(cur, 17, &sec) || sec > 59)
        return 1;
    if (day > 28 && day > month_days(year, month))
        return 1;
    if (month < 3)
        year--;
    rdn = (1461 * year)/4 - year/100 + year/400 + DayOffset[month] + day - 306;
    sod = hour * 3600 + min * 60 + sec;
    end = cur + len;
    cur = cur + 19;
    offset = nsec = 0;
    ch = *cur++;
    if (ch == '.') {
        unsigned char *start;
        unsigned long ndigits;
        start = cur;
        for (; cur < end; cur++) {
            unsigned char digit = *cur - '0';
            if (digit > 9)
                break;
            nsec = nsec * 10 + digit;
        }
        ndigits = cur - start;
        if (ndigits < 1 || ndigits > 9)
            return 1;
        nsec *= Pow10[9 - ndigits];
        if (cur == end)
            return 1;
        ch = *cur++;
    }
    if (!(ch == 'Z' || ch == 'z')) {
        if (cur + 5 < end || !(ch == '+' || ch == '-') || cur[2] != ':')
            return 1;
        if (parse_2d(cur, 0, &hour) || hour > 23 ||
            parse_2d(cur, 3, &min) || min > 59)
            return 1;
        offset = hour * 60 + min;
        if (ch == '-')
            offset *= -1;
        cur += 5;
    }
    if (cur != end)
        return 1;
    tsp->sec = ((long)rdn - 719163) * 86400 + sod - offset * 60;
    tsp->nsec = nsec;
    tsp->offset = offset;
    return 0;
}

variable_types:
str
char *

variable_types:
len
unsigned long

variable_types: struct timestamp_t { long sec; int nsec; short offset; } *

variable_types:
cur
unsigned char *

variable_types:
end
unsigned char *

variable_types:
ch
unsigned char

variable_types:
year
unsigned short

variable_types:
month
unsigned short

variable_types:
day
unsigned short

variable_types:
hour
unsigned short

variable_types:
min
unsigned short

variable_types:
sec
unsigned short

variable_types:
nsec
unsigned int

variable_types: offset short

variable_types:
start
unsigned char *

variable_types:
ndigits
unsigned long

variable_types:
digit
unsigned char


original_code:
_Bool
timestamp_valid(const timestamp_t *tsp) {
    const int64_t sec = tsp->sec + tsp->offset * 60;
    if (sec < -62135596800L || sec > 253402300799L ||
        tsp->nsec < 0 || tsp->nsec > 999999999 ||
        tsp->offset < -1439 || tsp->offset > 1439)
        return 0;
    return 1;
}

canonical_original_code:
_Bool timestamp_valid(struct timestamp_t *tsp) {
    long sec = tsp->sec + tsp->offset * 60;
    if (sec < -62135596800L || sec > 253402300799L ||
        tsp->nsec < 0 || tsp->nsec > 999999999 ||
        tsp->offset < -1439 || tsp->offset > 1439)
        return 0;
    return 1;
}

variable_types: sec long


repo: chaozh/c-tools

decompiled_code:
__int64 __fastcall ut_2_exp(char a1)
{
return 1 << a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
return 1 << a1;
}

original_code:
static inline ulong ut_2_exp(ulong n){
 return (ulong)(1<<n);
}

canonical_original_code:
static inline unsigned long ut_2_exp(unsigned long n){
 return (ulong)(1<<n);
}

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall ut_2_log(__int64 a1)
{
unsigned __int64 v1; // rdi
__int64 result; // rax
unsigned __int64 v3; // rdx

v1 = a1 - 1;
result = 0LL;
do
{
v3 = v1;
++result;
v1 >>= 1;
}
while ( v3 > 1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned __int64 v1; // rdi
__int64 result; // rax
unsigned __int64 v3; // rdx

v1 = a1 - 1;
result = 0LL;
do
{
v3 = v1;
++result;
v1 >>= 1;
}
while ( v3 > 1 );
return result;
}

original_code:
static inline ulong ut_2_log(ulong n){
 ulong ret = 0;
 n = n - 1;
 for(;;){
  ret++;
  n = n / 2;
  if(n == 0)
   break;
 }
 return ret;
}

canonical_original_code:
static inline unsigned long ut_2_log(unsigned long n){
 unsigned long ret = 0;
 n = n - 1;
 for(;;){
  ret++;
  n = n / 2;
  if(n == 0)
   break;
 }
 return ret;
}

variable_types:
ret
unsigned long


decompiled_code:
void __fastcall list_del_init(__int64 a1)
{
_QWORD *v1; // rax

_list_del(*(_QWORD **)(a1 + 8), *(_QWORD *)a1);
*v1 = v1;
v1[1] = v1;
}

original_code:
static inline void list_del_init(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    do { (entry)->next = (entry); (entry)->prev = (entry); } while (0);
}

variable_types: struct list_head { struct list_head *next; } *


decompiled_code:
_BOOL8 __fastcall list_empty(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

original_code:
static inline int list_empty(struct list_head *head)
{
    return head->next == head;
}

user_defined_types: struct list_head { struct list_head *next; }


decompiled_code:
__int64 __fastcall list_count(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

v1 = *a1;
for ( i = 0; a1 != v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

v1 = *a1;
for ( i = 0; a1 != v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

original_code:
static inline int list_count(struct list_head *list)
{
    struct list_head *pos;
    int count = 0;
    pos = list->next;
    while(pos != list)
    {
        ++count;
        pos = pos->next;
    }
    return count;
}

variable_types: count int