flushBuffer

decompiled_code:
void flushBuffer()
{
if ( coCurrentClient )
{
if ( coBufferPosition != (unsigned int)write(*(_DWORD *)coCurrentClient, &coBuffer, coBufferPosition) )
disconnectClient(coCurrentClient);
coBufferPosition = 0;
}
}

canonical_decompiled_code:
void func4()
{
if ( coCurrentClient )
{
if ( coBufferPosition != (unsigned int)write(*(_DWORD *)coCurrentClient, &coBuffer, coBufferPosition) )
func0(coCurrentClient);
coBufferPosition = 0;
}
}

original_code:
static void flushBuffer(void)
{
    int bytesWritten;
    if(coCurrentClient == ((void *)0)) {
 return;
    }
    bytesWritten = write(coCurrentClient->sockfd, coBuffer, coBufferPosition);
    if(bytesWritten != coBufferPosition) {
        disconnectClient(coCurrentClient);
    }
    coBufferPosition = 0;
}

canonical_original_code:
static void flushBuffer()
{
    int bytesWritten;
    if(coCurrentClient == ((void *)0)) {
 return;
    }
    bytesWritten = write(coCurrentClient->sockfd, coBuffer, coBufferPosition);
    if(bytesWritten != coBufferPosition) {
        disconnectClient(coCurrentClient);
    }
    coBufferPosition = 0;
}

variable_types: bytesWritten int


writeBufferedBytes

repo: waywardgeek/comserial

decompiled_code:
__int64 __fastcall writeBufferedBytes(__int64 *a1, int a2)
{
int v2; // eax
char *v3; // rdx
__int64 result; // rax

if ( a2 > 4096 )
{
perror("Write size too large.");
exit(1);
}
if ( coBufferPosition + a2 > 4095 )
flushBuffer();
v2 = coBufferPosition;
v3 = (char *)&coBuffer + coBufferPosition;
if ( (unsigned __int64)a2 >= 8 )
{
*(__int64 *)((char *)&coBuffer + coBufferPosition) = *a1;
*(_QWORD *)&v3[a2 - 8] = *(__int64 *)((char *)a1 + a2 - 8);
qmemcpy(
(void *)((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)a1 - &v3[-((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8 * ((unsigned __int64)&v3[a2 - ((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL)] >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)((char *)&coBuffer + coBufferPosition) = *(_DWORD *)a1;
*(_DWORD *)&v3[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
*((_BYTE *)&coBuffer + coBufferPosition) = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&v3[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
result = (unsigned int)(a2 + v2);
coBufferPosition = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, int a2)
{
int v2; // eax
char *v3; // rdx
__int64 result; // rax

if ( a2 > 4096 )
{
perror("Write size too large.");
exit(1);
}
if ( coBufferPosition + a2 > 4095 )
func4();
v2 = coBufferPosition;
v3 = (char *)&coBuffer + coBufferPosition;
if ( (unsigned __int64)a2 >= 8 )
{
*(__int64 *)((char *)&coBuffer + coBufferPosition) = *a1;
*(_QWORD *)&v3[a2 - 8] = *(__int64 *)((char *)a1 + a2 - 8);
qmemcpy(
(void *)((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)a1 - &v3[-((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL)]),
8 * ((unsigned __int64)&v3[a2 - ((unsigned __int64)(v3 + 8) & 0xFFFFFFFFFFFFFFF8LL)] >> 3));
}
else if ( (a2 & 4) != 0 )
{
*(_DWORD *)((char *)&coBuffer + coBufferPosition) = *(_DWORD *)a1;
*(_DWORD *)&v3[a2 - 4] = *(_DWORD *)((char *)a1 + a2 - 4);
}
else if ( a2 )
{
*((_BYTE *)&coBuffer + coBufferPosition) = *(_BYTE *)a1;
if ( (a2 & 2) != 0 )
*(_WORD *)&v3[a2 - 2] = *(_WORD *)((char *)a1 + a2 - 2);
}
result = (unsigned int)(a2 + v2);
coBufferPosition = result;
return result;
}

original_code:
static void writeBufferedBytes(
    char *buffer,
    int length)
{
    if(length > 4096) {
        perror("Write size too large.");
        exit(1);
    }
    if(coBufferPosition + length >= 4096) {
 flushBuffer();
    }
    memcpy(coBuffer + coBufferPosition, buffer, length);
    coBufferPosition += length;
}

canonical_original_code:
static void writeBufferedBytes(char *buffer, int length)
{
    if(length > 4096) {
        perror("Write size too large.");
        exit(1);
    }
    if(coBufferPosition + length >= 4096) {
 flushBuffer();
    }
    memcpy(coBuffer + coBufferPosition, buffer, length);
    coBufferPosition += length;
}

variable_types:
buffer
char *

variable_types: length int

return_type: void


coStartServer

decompiled_code:
void *__fastcall coStartServer(char *s)
{
int v1; // ebp
int v2; // eax
size_t v3; // rax
void *result; // rax
struct sockaddr v5[6]; // [rsp+0h] [rbp-88h] BYREF
char v6; // [rsp+6Dh] [rbp-1Bh]

unlink(s);
v1 = socket(1, 1, 0);
coServerSockfd = v1;
v5[0].sa_family = 1;
strncpy(v5[0].sa_data, s, 0x6BuLL);
v6 = 0;
v2 = strlen(v5[0].sa_data);
if ( bind(v1, v5, v2 + 2) )
{
perror("failed to bind socket");
exit(1);
}
listen(v1, 5);
v3 = strlen(s);
coSocketPath = (char *)calloc(v3 + 1, 1uLL);
strcpy(coSocketPath, s);
result = calloc((unsigned int)coClientTableSize, 8uLL);
coClientTable = result;
coServerStarted = 1;
return result;
}

canonical_decompiled_code:
void *__fastcall func6(char *s)
{
int v1; // ebp
int v2; // eax
size_t v3; // rax
void *result; // rax
struct sockaddr v5[6]; // [rsp+0h] [rbp-88h] BYREF
char v6; // [rsp+6Dh] [rbp-1Bh]

unlink(s);
v1 = socket(1, 1, 0);
coServerSockfd = v1;
v5[0].sa_family = 1;
strncpy(v5[0].sa_data, s, 0x6BuLL);
v6 = 0;
v2 = strlen(v5[0].sa_data);
if ( bind(v1, v5, v2 + 2) )
{
perror("failed to bind socket");
exit(1);
}
listen(v1, 5);
v3 = strlen(s);
coSocketPath = (char *)calloc(v3 + 1, 1uLL);
strcpy(coSocketPath, s);
result = calloc((unsigned int)coClientTableSize, 8uLL);
coClientTable = result;
coServerStarted = 1;
return result;
}

original_code:
void coStartServer(
    char *fileSocketPath)
{
    int serverLen;
    struct sockaddr_un serverAddress;
    unlink(fileSocketPath);
    coServerSockfd = socket(1, SOCK_STREAM, 0);
    serverAddress.sun_family = 1;
    strncpy(serverAddress.sun_path, fileSocketPath, 108);
    serverAddress.sun_path[108 - 1] = '\0';
    serverLen = strlen(serverAddress.sun_path) + 2;
    if(bind(coServerSockfd, (struct sockaddr*)&serverAddress, serverLen)) {
        perror("failed to bind socket");
        exit(1);
    }
    listen(coServerSockfd, 5);
    coSocketPath = calloc(strlen(fileSocketPath) + 1, sizeof(char));
    strcpy(coSocketPath, fileSocketPath);
    coClientTable = (coClient *)calloc(coClientTableSize, sizeof(coClient));
    coServerStarted = 1;
}

canonical_original_code:
void coStartServer(char *fileSocketPath)
{
    int serverLen;
    struct sockaddr_un serverAddress;
    unlink(fileSocketPath);
    coServerSockfd = socket(1, SOCK_STREAM, 0);
    serverAddress.sun_family = 1;
    strncpy(serverAddress.sun_path, fileSocketPath, 108);
    serverAddress.sun_path[108 - 1] = '\0';
    serverLen = strlen(serverAddress.sun_path) + 2;
    if(bind(coServerSockfd, (struct sockaddr *)&serverAddress, serverLen)) {
        perror("failed to bind socket");
        exit(1);
    }
    listen(coServerSockfd, 5);
    coSocketPath = calloc(strlen(fileSocketPath) + 1, sizeof(char));
    strcpy(coSocketPath, fileSocketPath);
    coClientTable = (struct coClientStruct **)calloc(coClientTableSize, sizeof(coClient));
    coServerStarted = 1;
}

variable_types:
fileSocketPath
char *

variable_types: serverLen int

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

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

user_defined_types: struct coClientStruct { int sockfd; char *message; unsigned int messageSize; unsigned int messagePos; char *sessionID; struct coClientStruct *nextReadyClient; }


coStopServer

decompiled_code:
int coStopServer()
{
char *v0; // rbx

v0 = coSocketPath;
unlink(coSocketPath);
free(v0);
free(coClientTable);
return close(coServerSockfd);
}

canonical_decompiled_code:
int func7()
{
char *v0; // rbx

v0 = coSocketPath;
unlink(coSocketPath);
free(v0);
free(coClientTable);
return close(coServerSockfd);
}

original_code:
void coStopServer(void)
{
    unlink(coSocketPath);
    free(coSocketPath);
    free(coClientTable);
    close(coServerSockfd);
}

canonical_original_code:
void coStopServer()
{
    unlink(coSocketPath);
    free(coSocketPath);
    free(coClientTable);
    close(coServerSockfd);
}


coStartResponse

decompiled_code:
const char *coStartResponse()
{
_QWORD *i; // rax
int v1; // ecx
int v2; // edx
int **v3; // rsi
__int64 v4; // rdi
__int64 v5; // r8
__int64 v6; // r9
__int64 v7; // rdx
__int64 v8; // rsi
__int64 v9; // rcx
int **v10; // rdi
struct timeval v12; // [rsp+0h] [rbp-A8h] BYREF
fd_set v13; // [rsp+10h] [rbp-98h] BYREF

if ( !coServerStarted )
return "sessionID";
for ( i = (_QWORD *)firstReadyClient; !firstReadyClient; i = (_QWORD *)firstReadyClient )
{
v2 = coServerSockfd;
memset(&v13, 0, sizeof(v13));
if ( (_DWORD)coClientTableSize )
{
v3 = (int **)coClientTable;
v10 = (int **)((char *)coClientTable + 8 * (unsigned int)(coClientTableSize - 1) + 8);
do
{
if ( *v3 )
{
v1 = **v3;
if ( v2 < v1 )
v2 = **v3;
v13.fds_bits[v1 / 64] |= 1LL << (v1 % 64);
}
++v3;
}
while ( v3 != v10 );
}
v13.fds_bits[coServerSockfd / 64] |= 1LL << (coServerSockfd % 64);
v12.tv_sec = 2LL;
v12.tv_usec = 0LL;
v4 = (unsigned int)(v2 + 1);
if ( select(v4, &v13, 0LL, 0LL, &v12) < 0 )
{
perror("Select error");
exit(1);
}
v7 = coServerSockfd / 64;
v8 = (unsigned int)(coServerSockfd >> 31) >> 26;
v9 = (unsigned int)(coServerSockfd % 64);
if ( (v13.fds_bits[v7] & (1LL << v9)) != 0 )
acceptNewClient();
readMessage(v4, v8, v7, v9, v5, v6, v13.fds_bits[0]);
}
coCurrentClient = i;
firstReadyClient = i[5];
return (const char *)i[4];
}

canonical_decompiled_code:
const char *func8()
{
_QWORD *i; // rax
int v1; // ecx
int v2; // edx
int **v3; // rsi
__int64 v4; // rdi
__int64 v5; // r8
__int64 v6; // r9
__int64 v7; // rdx
__int64 v8; // rsi
__int64 v9; // rcx
int **v10; // rdi
struct timeval v12; // [rsp+0h] [rbp-A8h] BYREF
fd_set v13; // [rsp+10h] [rbp-98h] BYREF

if ( !coServerStarted )
return "sessionID";
for ( i = (_QWORD *)firstReadyClient; !firstReadyClient; i = (_QWORD *)firstReadyClient )
{
v2 = coServerSockfd;
memset(&v13, 0, sizeof(v13));
if ( (_DWORD)coClientTableSize )
{
v3 = (int **)coClientTable;
v10 = (int **)((char *)coClientTable + 8 * (unsigned int)(coClientTableSize - 1) + 8);
do
{
if ( *v3 )
{
v1 = **v3;
if ( v2 < v1 )
v2 = **v3;
v13.fds_bits[v1 / 64] |= 1LL << (v1 % 64);
}
++v3;
}
while ( v3 != v10 );
}
v13.fds_bits[coServerSockfd / 64] |= 1LL << (coServerSockfd % 64);
v12.tv_sec = 2LL;
v12.tv_usec = 0LL;
v4 = (unsigned int)(v2 + 1);
if ( select(v4, &v13, 0LL, 0LL, &v12) < 0 )
{
perror("Select error");
exit(1);
}
v7 = coServerSockfd / 64;
v8 = (unsigned int)(coServerSockfd >> 31) >> 26;
v9 = (unsigned int)(coServerSockfd % 64);
if ( (v13.fds_bits[v7] & (1LL << v9)) != 0 )
func2();
func3(v4, v8, v7, v9, v5, v6, v13.fds_bits[0]);
}
coCurrentClient = i;
firstReadyClient = i[5];
return (const char *)i[4];
}

original_code:
char *coStartResponse(void)
{
    coClient client;
    fd_set readSockets;
    struct timeval tv;
    int maxSocket;
    int numActiveSockets;
    int xClient;
    if(!coServerStarted) {
        return "sessionID";
    }
    while(firstReadyClient == ((void *)0)) {
        maxSocket = coServerSockfd;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readSockets)->__fds_bits)[0]) : "memory"); } while (0);
        for(xClient = 0; xClient < coClientTableSize; xClient++) {
            client = coClientTable[xClient];
            if(client != ((void *)0)) {
                if(client->sockfd > maxSocket) {
                    maxSocket = client->sockfd;
                }
                ((void) (((&readSockets)->__fds_bits)[((client->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((client->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
            }
        }
        ((void) (((&readSockets)->__fds_bits)[((coServerSockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((coServerSockfd) % (8 * (int) sizeof (__fd_mask)))))));
        tv.tv_sec = 2;
        tv.tv_usec = 0;
        numActiveSockets = select(maxSocket + 1, &readSockets, ((void *)0), ((void *)0), &tv);
        if(numActiveSockets < 0) {
            perror("Select error");
            exit(1);
        }
        if(((((&readSockets)->__fds_bits)[((coServerSockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((coServerSockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            acceptNewClient();
        }
        readMessage(readSockets);
    }
    coCurrentClient = firstReadyClient;
    firstReadyClient = firstReadyClient->nextReadyClient;
    return coCurrentClient->sessionID;
}

canonical_original_code:
char *coStartResponse()
{
    struct coClientStruct *client;
    struct fd_set readSockets;
    struct timeval tv;
    int maxSocket;
    int numActiveSockets;
    int xClient;
    if(!coServerStarted) {
        return "sessionID";
    }
    while(firstReadyClient == ((void *)0)) {
        maxSocket = coServerSockfd;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readSockets)->__fds_bits)[0]) : "memory"); } while (0);
        for(xClient = 0; xClient < coClientTableSize; xClient++) {
            client = coClientTable[xClient];
            if(client != ((void *)0)) {
                if(client->sockfd > maxSocket) {
                    maxSocket = client->sockfd;
                }
                ((void) (((&readSockets)->__fds_bits)[((client->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((client->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
            }
        }
        ((void) (((&readSockets)->__fds_bits)[((coServerSockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((coServerSockfd) % (8 * (int) sizeof (__fd_mask)))))));
        tv.tv_sec = 2;
        tv.tv_usec = 0;
        numActiveSockets = select(maxSocket + 1, &readSockets, ((void *)0), ((void *)0), &tv);
        if(numActiveSockets < 0) {
            perror("Select error");
            exit(1);
        }
        if(((((&readSockets)->__fds_bits)[((coServerSockfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((coServerSockfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            acceptNewClient();
        }
        readMessage(readSockets);
    }
    coCurrentClient = firstReadyClient;
    firstReadyClient = firstReadyClient->nextReadyClient;
    return coCurrentClient->sessionID;
}

variable_types: struct coClientStruct { int sockfd; char *message; unsigned int messageSize; unsigned int messagePos; char *sessionID; struct coClientStruct *nextReadyClient; } *

variable_types:
readSockets
struct fd_set { long __fds_bits[-1]; }

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

variable_types: maxSocket int

variable_types: numActiveSockets int

variable_types: xClient int

return_type: char *


coGetc

decompiled_code:
int coGetc()
{
unsigned int v0; // eax
__int64 v1; // rcx

if ( !coServerStarted )
return getchar();
v0 = *((_DWORD *)coCurrentClient + 6);
if ( v0 == *((_DWORD *)coCurrentClient + 5) )
return -1;
v1 = *((_QWORD *)coCurrentClient + 1);
*((_DWORD *)coCurrentClient + 6) = v0 + 1;
return *(char *)(v1 + v0);
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // eax
__int64 v1; // rcx

if ( !coServerStarted )
return getchar();
v0 = *((_DWORD *)coCurrentClient + 6);
if ( v0 == *((_DWORD *)coCurrentClient + 5) )
return -1;
v1 = *((_QWORD *)coCurrentClient + 1);
*((_DWORD *)coCurrentClient + 6) = v0 + 1;
return *(char *)(v1 + v0);
}

original_code:
int coGetc(void)
{
    if(!coServerStarted) {
        return getchar();
    }
    if(coCurrentClient->messagePos == coCurrentClient->messageLength) {
        return (-1);
    }
    return coCurrentClient->message[(coCurrentClient->messagePos)++];
}

canonical_original_code:
int coGetc()
{
    if(!coServerStarted) {
        return getchar();
    }
    if(coCurrentClient->messagePos == coCurrentClient->messageLength) {
        return (-1);
    }
    return coCurrentClient->message[(coCurrentClient->messagePos)++];
}

return_type: int


coPrintf

decompiled_code:
__int64 coPrintf(char *format, ...)
{
int v1; // eax
unsigned int v2; // ebx
__int64 v4[513]; // [rsp+0h] [rbp-10D8h] BYREF
gcc_va_list arg; // [rsp+1008h] [rbp-D0h] BYREF

va_start(arg, format);
v1 = vsnprintf((char *)v4, 0x1000uLL, format, arg);
v2 = v1;
if ( coServerStarted )
writeBufferedBytes(v4, v1);
else
printf("%s", (const char *)v4);
return v2;
}

canonical_decompiled_code:
__int64 func10(char *format, ...)
{
int v1; // eax
unsigned int v2; // ebx
__int64 v4[513]; // [rsp+0h] [rbp-10D8h] BYREF
gcc_va_list arg; // [rsp+1008h] [rbp-D0h] BYREF

va_start(arg, format);
v1 = vsnprintf((char *)v4, 0x1000uLL, format, arg);
v2 = v1;
if ( coServerStarted )
func5(v4, v1);
else
printf("%s", (const char *)v4);
return v2;
}

original_code:
int coPrintf(
    char *format,
    ...)
{
    va_list ap;
    char buffer[4096];
    int length;
    __builtin_va_start(ap,format);
    length = vsnprintf(buffer, 4096, format, ap);
    __builtin_va_end(ap);
    if(coServerStarted) {
 writeBufferedBytes(buffer, length);
    } else {
        printf("%s", buffer);
    }
    return length;
}

canonical_original_code:
int coPrintf(char *format, ...)
{
    __builtin_va_list ap;
    char buffer[4096];
    int length;
    __builtin_va_start(ap,format);
    length = vsnprintf(buffer, 4096, format, ap);
    __builtin_va_end(ap);
    if(coServerStarted) {
 writeBufferedBytes(buffer, length);
    } else {
        printf("%s", buffer);
    }
    return length;
}

variable_types:
format
char *

variable_types:
buffer
char [4096]


coCompleteResponse

decompiled_code:
_DWORD *coCompleteResponse()
{
_DWORD *result; // rax

if ( coServerStarted )
{
writeBufferedBytes((__int64 *)&unk_1916, 1);
flushBuffer();
fsync(*(_DWORD *)coCurrentClient);
result = coCurrentClient;
*((_DWORD *)coCurrentClient + 5) = 0;
result[6] = 0;
coCurrentClient = 0LL;
}
return result;
}

canonical_decompiled_code:
_DWORD *func11()
{
_DWORD *result; // rax

if ( coServerStarted )
{
func5((__int64 *)&unk_1916, 1);
func4();
fsync(*(_DWORD *)coCurrentClient);
result = coCurrentClient;
*((_DWORD *)coCurrentClient + 5) = 0;
result[6] = 0;
coCurrentClient = 0LL;
}
return result;
}

original_code:
void coCompleteResponse(void)
{
    if(coServerStarted) {
        writeBufferedBytes("", 1);
 flushBuffer();
        fsync(coCurrentClient->sockfd);
        coCurrentClient->messageLength = 0;
        coCurrentClient->messagePos = 0;
        coCurrentClient = ((void *)0);
    }
}

canonical_original_code:
void coCompleteResponse()
{
    if(coServerStarted) {
        writeBufferedBytes("", 1);
 flushBuffer();
        fsync(coCurrentClient->sockfd);
        coCurrentClient->messageLength = 0;
        coCurrentClient->messagePos = 0;
        coCurrentClient = ((void *)0);
    }
}


readChar

decompiled_code:
__int64 readChar()
{
int v0; // eax
__int64 result; // rax
int v2; // eax
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

fds.fd = coSockfd;
fds.events = 1;
fds.revents = 0;
v0 = coBufferNextCharPos;
if ( coBufferNextCharPos >= coBufferLength )
{
poll(&fds, 1uLL, -1);
coBufferNextCharPos = 0;
coBufferLength = read(coSockfd, coBuffer, 0x1000uLL);
if ( coBufferLength <= 0 )
{
perror("Cannot read from server");
exit(1);
}
v2 = coBufferNextCharPos++;
result = (unsigned __int8)coBuffer[v2];
}
else
{
++coBufferNextCharPos;
result = (unsigned __int8)coBuffer[v0];
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
__int64 result; // rax
int v2; // eax
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

fds.fd = coSockfd;
fds.events = 1;
fds.revents = 0;
v0 = coBufferNextCharPos;
if ( coBufferNextCharPos >= coBufferLength )
{
poll(&fds, 1uLL, -1);
coBufferNextCharPos = 0;
coBufferLength = read(coSockfd, coBuffer, 0x1000uLL);
if ( coBufferLength <= 0 )
{
perror("Cannot read from server");
exit(1);
}
v2 = coBufferNextCharPos++;
result = (unsigned __int8)coBuffer[v2];
}
else
{
++coBufferNextCharPos;
result = (unsigned __int8)coBuffer[v0];
}
return result;
}

original_code:
static char readChar(void)
{
    struct pollfd pstruct = {coSockfd, 0x001, 0};
    if(coBufferNextCharPos < coBufferLength) {
 return coBuffer[coBufferNextCharPos++];
    }
    poll(&pstruct, 1, -1);
    coBufferNextCharPos = 0;
    coBufferLength = read(coSockfd, coBuffer, 4096);
    if(coBufferLength <= 0) {
 perror("Cannot read from server");
 exit(1);
    }
    return coBuffer[coBufferNextCharPos++];
}

canonical_original_code:
static char readChar()
{
    struct pollfd pstruct = {coSockfd, 0x001, 0};
    if(coBufferNextCharPos < coBufferLength) {
 return coBuffer[coBufferNextCharPos++];
    }
    poll(&pstruct, 1, -1);
    coBufferNextCharPos = 0;
    coBufferLength = read(coSockfd, coBuffer, 4096);
    if(coBufferLength <= 0) {
 perror("Cannot read from server");
 exit(1);
    }
    return coBuffer[coBufferNextCharPos++];
}

variable_types: struct pollfd { int fd; short events; short revents; }

return_type: char


coStopClient

decompiled_code:
int coStopClient()
{
return close(coSockfd);
}

canonical_decompiled_code:
int func1()
{
return close(coSockfd);
}

original_code:
void coStopClient(void)
{
    close(coSockfd);
}

canonical_original_code:
void coStopClient()
{
    close(coSockfd);
}


coSendMessage

decompiled_code:
int coSendMessage(char *format, ...)
{
int v1; // eax
char v3[4104]; // [rsp+0h] [rbp-10D8h] BYREF
gcc_va_list arg; // [rsp+1008h] [rbp-D0h] BYREF

va_start(arg, format);
v1 = vsnprintf(v3, 0x1000uLL, format, arg);
write(coSockfd, v3, v1 + 1);
return fsync(coSockfd);
}

canonical_decompiled_code:
int func2(char *format, ...)
{
int v1; // eax
char v3[4104]; // [rsp+0h] [rbp-10D8h] BYREF
gcc_va_list arg; // [rsp+1008h] [rbp-D0h] BYREF

va_start(arg, format);
v1 = vsnprintf(v3, 0x1000uLL, format, arg);
write(coSockfd, v3, v1 + 1);
return fsync(coSockfd);
}

original_code:
void coSendMessage(
    char *format,
    ...)
{
    va_list ap;
    char buffer[4096];
    int length;
    __builtin_va_start(ap,format);
    length = vsnprintf(buffer, 4096, format, ap);
    __builtin_va_end(ap);
    length = write(coSockfd, buffer, length + 1);
    fsync(coSockfd);
}

canonical_original_code:
void coSendMessage(char *format, ...)
{
    __builtin_va_list ap;
    char buffer[4096];
    int length;
    __builtin_va_start(ap,format);
    length = vsnprintf(buffer, 4096, format, ap);
    __builtin_va_end(ap);
    length = write(coSockfd, buffer, length + 1);
    fsync(coSockfd);
}


coReadMessage

decompiled_code:
void *coReadMessage()
{
__int64 v0; // rbp
void *result; // rax
char v2; // bl
int v3; // esi

v0 = 0LL;
do
{
v2 = readChar();
v3 = coMessageSize;
if ( coMessageSize == (_DWORD)v0 + 1 )
{
coMessageSize *= 2;
coMessage = realloc(coMessage, 2 * v3);
}
result = coMessage;
*((_BYTE *)coMessage + v0++) = v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
void *func3()
{
__int64 v0; // rbp
void *result; // rax
char v2; // bl
int v3; // esi

v0 = 0LL;
do
{
v2 = func0();
v3 = coMessageSize;
if ( coMessageSize == (_DWORD)v0 + 1 )
{
coMessageSize *= 2;
coMessage = realloc(coMessage, 2 * v3);
}
result = coMessage;
*((_BYTE *)coMessage + v0++) = v2;
}
while ( v2 );
return result;
}

original_code:
char *coReadMessage(void)
{
    char c;
    int messagePos = 0;
    do {
 c = readChar();
        if(messagePos + 1 == coMessageSize) {
            coMessageSize <<= 1;
            coMessage = (char *)realloc(coMessage, coMessageSize*sizeof(char));
        }
        coMessage[messagePos++] = c;
    } while(c != '\0');
    return coMessage;
}

canonical_original_code:
char *coReadMessage()
{
    char c;
    int messagePos = 0;
    do {
 c = readChar();
        if(messagePos + 1 == coMessageSize) {
            coMessageSize <<= 1;
            coMessage = (char *)realloc(coMessage, coMessageSize*sizeof(char));
        }
        coMessage[messagePos++] = c;
    } while(c != '\0');
    return coMessage;
}

variable_types: c char

variable_types: messagePos int


usage

repo: waywardgeek/sonic

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"Usage: sonic [OPTION]... infile outfile\n"
" -c -- Modify pitch by emulating vocal chords vibrating\n"
" faster or slower.\n"
" -o -- Override the sample rate of the output. -o 44200\n"
" on an input file at 22100 KHz will play twice as fast\n"
" and have twice the pitch.\n"
" -p pitch -- Set pitch scaling factor. 1.3 means 30%% higher.\n"
" -q -- Disable speed-up heuristics. May increase quality.\n"
" -r rate -- Set playback rate. 2.0 means 2X faster, and 2X pitch.\n"
" -s speed -- Set speed up factor. 2.0 means 2X faster.\n"
" -v volume -- Scale volume by a constant factor.\n");
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fprintf(
stderr,
"Usage: sonic [OPTION]... infile outfile\n"
" -c -- Modify pitch by emulating vocal chords vibrating\n"
" faster or slower.\n"
" -o -- Override the sample rate of the output. -o 44200\n"
" on an input file at 22100 KHz will play twice as fast\n"
" and have twice the pitch.\n"
" -p pitch -- Set pitch scaling factor. 1.3 means 30%% higher.\n"
" -q -- Disable speed-up heuristics. May increase quality.\n"
" -r rate -- Set playback rate. 2.0 means 2X faster, and 2X pitch.\n"
" -s speed -- Set speed up factor. 2.0 means 2X faster.\n"
" -v volume -- Scale volume by a constant factor.\n");
exit(1);
}

original_code:
static void usage(void) {
  fprintf(
      stderr,
      "Usage: sonic [OPTION]... infile outfile\n"
      "    -c         -- Modify pitch by emulating vocal chords vibrating\n"
      "                  faster or slower.\n"
      "    -o         -- Override the sample rate of the output.  -o 44200\n"
      "                  on an input file at 22100 KHz will play twice as fast\n"
      "                  and have twice the pitch.\n"
      "    -p pitch   -- Set pitch scaling factor.  1.3 means 30%% higher.\n"
      "    -q         -- Disable speed-up heuristics.  May increase quality.\n"
      "    -r rate    -- Set playback rate.  2.0 means 2X faster, and 2X "
      "pitch.\n"
      "    -s speed   -- Set speed up factor.  2.0 means 2X faster.\n"
      "    -v volume  -- Scale volume by a constant factor.\n");
  exit(1);
}

canonical_original_code:
static void usage() {
  fprintf(
      stderr,
      "Usage: sonic [OPTION]... infile outfile\n"
      "    -c         -- Modify pitch by emulating vocal chords vibrating\n"
      "                  faster or slower.\n"
      "    -o         -- Override the sample rate of the output.  -o 44200\n"
      "                  on an input file at 22100 KHz will play twice as fast\n"
      "                  and have twice the pitch.\n"
      "    -p pitch   -- Set pitch scaling factor.  1.3 means 30%% higher.\n"
      "    -q         -- Disable speed-up heuristics.  May increase quality.\n"
      "    -r rate    -- Set playback rate.  2.0 means 2X faster, and 2X "
      "pitch.\n"
      "    -s speed   -- Set speed up factor.  2.0 means 2X faster.\n"
      "    -v volume  -- Scale volume by a constant factor.\n");
  exit(1);
}


main

canonical_name: main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
const char **v4; // r12
int v5; // ebx
__int64 v7; // r13
const char *v8; // rax
bool v9; // cf
bool v10; // zf
__int64 v11; // rcx
const char *v12; // rdi
char v13; // dl
bool v14; // cf
bool v15; // zf
__int64 v16; // rcx
char v17; // dl
bool v18; // cf
bool v19; // zf
__int64 v20; // rcx
char v21; // dl
bool v22; // cf
bool v23; // zf
float v24; // xmm5_4
__int64 v25; // rcx
const char *v26; // rdi
char v27; // dl
bool v28; // cf
bool v29; // zf
__int64 v30; // rcx
char v31; // dl
bool v32; // cf
bool v33; // zf
const char *v34; // rdi
float v35; // xmm6_4
__int64 v36; // rcx
float v37; // xmm7_4
float v38; // xmm7_4
unsigned int v39; // [rsp+4h] [rbp-54h]
unsigned int v40; // [rsp+8h] [rbp-50h]
float v41; // [rsp+Ch] [rbp-4Ch]
float v42; // [rsp+10h] [rbp-48h]
unsigned int v43; // [rsp+14h] [rbp-44h]
float v44; // [rsp+18h] [rbp-40h]
float v45; // [rsp+1Ch] [rbp-3Ch]

v3 = argc;
v4 = argv;
if ( argc <= 1 )
{
v5 = 1;
v43 = 0;
v39 = 0;
v40 = 0;
v45 = *(float *)&LC3;
v41 = *(float *)&LC3;
v42 = *(float *)&LC3;
v44 = *(float *)&LC3;
}
else
{
v5 = 1;
v43 = 0;
v39 = 0;
v40 = 0;
v45 = *(float *)&LC3;
v41 = *(float *)&LC3;
v42 = *(float *)&LC3;
v44 = *(float *)&LC3;
do
{
v7 = v5;
v8 = v4[v7];
v9 = *v8 < 0x2Du;
v10 = *v8 == 45;
if ( *v8 != 45 )
break;
v11 = 3LL;
argv = (const char **)v4[v5];
v12 = "-c";
do
{
if ( !v11 )
break;
v9 = *(_BYTE *)argv < *v12;
v10 = *(_BYTE *)argv == *v12;
argv = (const char **)((char *)argv + 1);
++v12;
--v11;
}
while ( v10 );
v13 = (!v9 && !v10) - v9;
v14 = 0;
v15 = v13 == 0;
if ( v13 )
{
v16 = 3LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "-o";
do
{
if ( !v16 )
break;
v14 = *(_BYTE *)argv < **(_BYTE **)&argc;
v15 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v16;
}
while ( v15 );
v17 = (!v14 && !v15) - v14;
v18 = 0;
v19 = v17 == 0;
if ( v17 )
{
v20 = 3LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "-p";
do
{
if ( !v20 )
break;
v18 = *(_BYTE *)argv < **(_BYTE **)&argc;
v19 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v20;
}
while ( v19 );
v21 = (!v18 && !v19) - v18;
v22 = 0;
v23 = v21 == 0;
if ( v21 )
{
v25 = 3LL;
argv = (const char **)v4[v5];
v26 = "-q";
do
{
if ( !v25 )
break;
v22 = *(_BYTE *)argv < *v26;
v23 = *(_BYTE *)argv == *v26;
argv = (const char **)((char *)argv + 1);
++v26;
--v25;
}
while ( v23 );
v27 = (!v22 && !v23) - v22;
v28 = 0;
v29 = v27 == 0;
if ( v27 )
{
v30 = 3LL;
*(_QWORD *)&argc = "-r";
argv = (const char **)v4[v5];
do
{
if ( !v30 )
break;
v28 = *(_BYTE *)argv < **(_BYTE **)&argc;
v29 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v30;
}
while ( v29 );
v31 = (!v28 && !v29) - v28;
v32 = 0;
v33 = v31 == 0;
if ( v31 )
{
v36 = 3LL;
*(_QWORD *)&argc = "-s";
argv = (const char **)v4[v5];
do
{
if ( !v36 )
break;
v32 = *(_BYTE *)argv < **(_BYTE **)&argc;
v33 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v36;
}
while ( v33 );
if ( (!v32 && !v33) == v32 )
{
if ( v3 > ++v5 )
{
v37 = atof(v4[v7 + 1]);
v44 = v37;
*(_QWORD *)&argc = "Setting speed to %0.2fX\n";
printf("Setting speed to %0.2fX\n", v37);
}
}
else
{
argv = (const char **)"-v";
*(_QWORD *)&argc = v4[v5];
if ( !strcmp(*(const char **)&argc, "-v") && v3 > ++v5 )
{
v38 = atof(v4[v7 + 1]);
v45 = v38;
*(_QWORD *)&argc = "Setting volume to %0.2f\n";
printf("Setting volume to %0.2f\n", v38);
}
}
}
else if ( v3 > ++v5 )
{
v34 = v4[v7 + 1];
v35 = atof(v34);
v41 = v35;
if ( v35 == 0.0 )
usage(v34, argv);
*(_QWORD *)&argc = "Setting rate to %0.2fX\n";
printf("Setting rate to %0.2fX\n", v35);
}
}
else
{
*(_QWORD *)&argc = "Disabling speed-up heuristics";
puts("Disabling speed-up heuristics");
v43 = 1;
}
}
else if ( v3 > ++v5 )
{
v24 = atof(v4[v7 + 1]);
v42 = v24;
*(_QWORD *)&argc = "Setting pitch to %0.2fX\n";
printf("Setting pitch to %0.2fX\n", v24);
}
}
else if ( v3 > ++v5 )
{
v40 = atoi(v4[v7 + 1]);
argv = (const char **)v40;
*(_QWORD *)&argc = "Setting output sample rate to %d\n";
printf("Setting output sample rate to %d\n", v40);
}
}
else
{
*(_QWORD *)&argc = "Scaling pitch linearly.";
puts("Scaling pitch linearly.");
v39 = 1;
}
++v5;
}
while ( v3 > v5 );
}
if ( v3 - v5 != 2 )
usage(*(_QWORD *)&argc, argv);
runSonic(v4[v5], v4[v5 + 1], v40, v39, v43, 0, v44, v42, v41, v45);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
const char **v4; // r12
int v5; // ebx
__int64 v7; // r13
const char *v8; // rax
bool v9; // cf
bool v10; // zf
__int64 v11; // rcx
const char *v12; // rdi
char v13; // dl
bool v14; // cf
bool v15; // zf
__int64 v16; // rcx
char v17; // dl
bool v18; // cf
bool v19; // zf
__int64 v20; // rcx
char v21; // dl
bool v22; // cf
bool v23; // zf
float v24; // xmm5_4
__int64 v25; // rcx
const char *v26; // rdi
char v27; // dl
bool v28; // cf
bool v29; // zf
__int64 v30; // rcx
char v31; // dl
bool v32; // cf
bool v33; // zf
const char *v34; // rdi
float v35; // xmm6_4
__int64 v36; // rcx
float v37; // xmm7_4
float v38; // xmm7_4
unsigned int v39; // [rsp+4h] [rbp-54h]
unsigned int v40; // [rsp+8h] [rbp-50h]
float v41; // [rsp+Ch] [rbp-4Ch]
float v42; // [rsp+10h] [rbp-48h]
unsigned int v43; // [rsp+14h] [rbp-44h]
float v44; // [rsp+18h] [rbp-40h]
float v45; // [rsp+1Ch] [rbp-3Ch]

v3 = argc;
v4 = argv;
if ( argc <= 1 )
{
v5 = 1;
v43 = 0;
v39 = 0;
v40 = 0;
v45 = *(float *)&LC3;
v41 = *(float *)&LC3;
v42 = *(float *)&LC3;
v44 = *(float *)&LC3;
}
else
{
v5 = 1;
v43 = 0;
v39 = 0;
v40 = 0;
v45 = *(float *)&LC3;
v41 = *(float *)&LC3;
v42 = *(float *)&LC3;
v44 = *(float *)&LC3;
do
{
v7 = v5;
v8 = v4[v7];
v9 = *v8 < 0x2Du;
v10 = *v8 == 45;
if ( *v8 != 45 )
break;
v11 = 3LL;
argv = (const char **)v4[v5];
v12 = "-c";
do
{
if ( !v11 )
break;
v9 = *(_BYTE *)argv < *v12;
v10 = *(_BYTE *)argv == *v12;
argv = (const char **)((char *)argv + 1);
++v12;
--v11;
}
while ( v10 );
v13 = (!v9 && !v10) - v9;
v14 = 0;
v15 = v13 == 0;
if ( v13 )
{
v16 = 3LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "-o";
do
{
if ( !v16 )
break;
v14 = *(_BYTE *)argv < **(_BYTE **)&argc;
v15 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v16;
}
while ( v15 );
v17 = (!v14 && !v15) - v14;
v18 = 0;
v19 = v17 == 0;
if ( v17 )
{
v20 = 3LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "-p";
do
{
if ( !v20 )
break;
v18 = *(_BYTE *)argv < **(_BYTE **)&argc;
v19 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v20;
}
while ( v19 );
v21 = (!v18 && !v19) - v18;
v22 = 0;
v23 = v21 == 0;
if ( v21 )
{
v25 = 3LL;
argv = (const char **)v4[v5];
v26 = "-q";
do
{
if ( !v25 )
break;
v22 = *(_BYTE *)argv < *v26;
v23 = *(_BYTE *)argv == *v26;
argv = (const char **)((char *)argv + 1);
++v26;
--v25;
}
while ( v23 );
v27 = (!v22 && !v23) - v22;
v28 = 0;
v29 = v27 == 0;
if ( v27 )
{
v30 = 3LL;
*(_QWORD *)&argc = "-r";
argv = (const char **)v4[v5];
do
{
if ( !v30 )
break;
v28 = *(_BYTE *)argv < **(_BYTE **)&argc;
v29 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v30;
}
while ( v29 );
v31 = (!v28 && !v29) - v28;
v32 = 0;
v33 = v31 == 0;
if ( v31 )
{
v36 = 3LL;
*(_QWORD *)&argc = "-s";
argv = (const char **)v4[v5];
do
{
if ( !v36 )
break;
v32 = *(_BYTE *)argv < **(_BYTE **)&argc;
v33 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v36;
}
while ( v33 );
if ( (!v32 && !v33) == v32 )
{
if ( v3 > ++v5 )
{
v37 = atof(v4[v7 + 1]);
v44 = v37;
*(_QWORD *)&argc = "Setting speed to %0.2fX\n";
printf("Setting speed to %0.2fX\n", v37);
}
}
else
{
argv = (const char **)"-v";
*(_QWORD *)&argc = v4[v5];
if ( !strcmp(*(const char **)&argc, "-v") && v3 > ++v5 )
{
v38 = atof(v4[v7 + 1]);
v45 = v38;
*(_QWORD *)&argc = "Setting volume to %0.2f\n";
printf("Setting volume to %0.2f\n", v38);
}
}
}
else if ( v3 > ++v5 )
{
v34 = v4[v7 + 1];
v35 = atof(v34);
v41 = v35;
if ( v35 == 0.0 )
func0(v34, argv);
*(_QWORD *)&argc = "Setting rate to %0.2fX\n";
printf("Setting rate to %0.2fX\n", v35);
}
}
else
{
*(_QWORD *)&argc = "Disabling speed-up heuristics";
puts("Disabling speed-up heuristics");
v43 = 1;
}
}
else if ( v3 > ++v5 )
{
v24 = atof(v4[v7 + 1]);
v42 = v24;
*(_QWORD *)&argc = "Setting pitch to %0.2fX\n";
printf("Setting pitch to %0.2fX\n", v24);
}
}
else if ( v3 > ++v5 )
{
v40 = atoi(v4[v7 + 1]);
argv = (const char **)v40;
*(_QWORD *)&argc = "Setting output sample rate to %d\n";
printf("Setting output sample rate to %d\n", v40);
}
}
else
{
*(_QWORD *)&argc = "Scaling pitch linearly.";
puts("Scaling pitch linearly.");
v39 = 1;
}
++v5;
}
while ( v3 > v5 );
}
if ( v3 - v5 != 2 )
func0(*(_QWORD *)&argc, argv);
func1(v4[v5], v4[v5 + 1], v40, v39, v43, 0, v44, v42, v41, v45);
return 0;
}

original_code:
int main(int argc, char** argv) {
  char* inFileName;
  char* outFileName;
  float speed = 1.0f;
  float pitch = 1.0f;
  float rate = 1.0f;
  float volume = 1.0f;
  int outputSampleRate = 0;
  int emulateChordPitch = 0;
  int quality = 0;
  int xArg = 1;
  int computeSpectrogram = 0;
  int numRows = 0, numCols = 0;
  while (xArg < argc && *(argv[xArg]) == '-') {
    if (!strcmp(argv[xArg], "-c")) {
      emulateChordPitch = 1;
      printf("Scaling pitch linearly.\n");
    } else if (!strcmp(argv[xArg], "-o")) {
      xArg++;
      if (xArg < argc) {
        outputSampleRate = atoi(argv[xArg]);
        printf("Setting output sample rate to %d\n", outputSampleRate);
      }
    } else if (!strcmp(argv[xArg], "-p")) {
      xArg++;
      if (xArg < argc) {
        pitch = atof(argv[xArg]);
        printf("Setting pitch to %0.2fX\n", pitch);
      }
    } else if (!strcmp(argv[xArg], "-q")) {
      quality = 1;
      printf("Disabling speed-up heuristics\n");
    } else if (!strcmp(argv[xArg], "-r")) {
      xArg++;
      if (xArg < argc) {
        rate = atof(argv[xArg]);
        if (rate == 0.0f) {
          usage();
        }
        printf("Setting rate to %0.2fX\n", rate);
      }
    } else if (!strcmp(argv[xArg], "-s")) {
      xArg++;
      if (xArg < argc) {
        speed = atof(argv[xArg]);
        printf("Setting speed to %0.2fX\n", speed);
      }
    } else if (!strcmp(argv[xArg], "-v")) {
      xArg++;
      if (xArg < argc) {
        volume = atof(argv[xArg]);
        printf("Setting volume to %0.2f\n", volume);
      }
    }
    xArg++;
  }
  if (argc - xArg != 2) {
    usage();
  }
  inFileName = argv[xArg];
  outFileName = argv[xArg + 1];
  runSonic(inFileName, outFileName, speed, pitch, rate, volume,
           outputSampleRate, emulateChordPitch, quality,
           computeSpectrogram, numRows, numCols);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  char *inFileName;
  char *outFileName;
  float speed = 1.0f;
  float pitch = 1.0f;
  float rate = 1.0f;
  float volume = 1.0f;
  int outputSampleRate = 0;
  int emulateChordPitch = 0;
  int quality = 0;
  int xArg = 1;
  int computeSpectrogram = 0;
  int numRows = 0; int numCols = 0;
  while (xArg < argc && *(argv[xArg]) == '-') {
    if (!strcmp(argv[xArg], "-c")) {
      emulateChordPitch = 1;
      printf("Scaling pitch linearly.\n");
    } else if (!strcmp(argv[xArg], "-o")) {
      xArg++;
      if (xArg < argc) {
        outputSampleRate = atoi(argv[xArg]);
        printf("Setting output sample rate to %d\n", outputSampleRate);
      }
    } else if (!strcmp(argv[xArg], "-p")) {
      xArg++;
      if (xArg < argc) {
        pitch = atof(argv[xArg]);
        printf("Setting pitch to %0.2fX\n", pitch);
      }
    } else if (!strcmp(argv[xArg], "-q")) {
      quality = 1;
      printf("Disabling speed-up heuristics\n");
    } else if (!strcmp(argv[xArg], "-r")) {
      xArg++;
      if (xArg < argc) {
        rate = atof(argv[xArg]);
        if (rate == 0.0f) {
          usage();
        }
        printf("Setting rate to %0.2fX\n", rate);
      }
    } else if (!strcmp(argv[xArg], "-s")) {
      xArg++;
      if (xArg < argc) {
        speed = atof(argv[xArg]);
        printf("Setting speed to %0.2fX\n", speed);
      }
    } else if (!strcmp(argv[xArg], "-v")) {
      xArg++;
      if (xArg < argc) {
        volume = atof(argv[xArg]);
        printf("Setting volume to %0.2f\n", volume);
      }
    }
    xArg++;
  }
  if (argc - xArg != 2) {
    usage();
  }
  inFileName = argv[xArg];
  outFileName = argv[xArg + 1];
  runSonic(inFileName, outFileName, speed, pitch, rate, volume,
           outputSampleRate, emulateChordPitch, quality,
           computeSpectrogram, numRows, numCols);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
inFileName
char *

variable_types:
outFileName
char *

variable_types: speed float

variable_types: pitch float

variable_types: rate float

variable_types: volume float

variable_types: outputSampleRate int

variable_types: emulateChordPitch int

variable_types: quality int

variable_types: xArg int

variable_types: computeSpectrogram int

variable_types: numRows int

variable_types: numCols int


cmp

repo: wazedix/algorithms

decompiled_code:
__int64 __fastcall cmp(const void *a1, const void *a2)
{
return (unsigned int)(*((_DWORD *)a1 + 2) - *((_DWORD *)a2 + 2));
}

canonical_decompiled_code:
__int64 __fastcall func7(const void *a1, const void *a2)
{
return (unsigned int)(*((_DWORD *)a1 + 2) - *((_DWORD *)a2 + 2));
}

original_code:
int cmp(const void *x, const void *y)
{
    return (*(Edge *)x).value - (*(Edge *)y).value;
}

canonical_original_code:
int cmp(void *x, void *y)
{
    return (*(struct Edge *)x).value - (*(struct Edge *)y).value;
}

variable_types:
x
void *

variable_types:
y
void *

user_defined_types: struct Edge { int x; int y; int value; }


decompiled_code:
__int64 __fastcall get_father(int a1)
{
if ( father[a1] != a1 )
father[a1] = get_father();
return (unsigned int)father[a1];
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
if ( father[a1] != a1 )
father[a1] = func8();
return (unsigned int)father[a1];
}

original_code:
int get_father(int x)
{
    if (father[x] != x)
        father[x] = get_father(father[x]);
    return father[x];
}

variable_types: x int


decompiled_code:
__int64 __fastcall set_union(int a1, int a2)
{
int v2; // ebx
__int64 result; // rax

v2 = get_father(a1);
result = get_father(a2);
if ( v2 != (_DWORD)result )
father[v2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
int v2; // ebx
__int64 result; // rax

v2 = func8(a1);
result = func8(a2);
if ( v2 != (_DWORD)result )
father[v2] = result;
return result;
}

original_code:
void set_union(int x, int y)
{
    x = get_father(x);
    y = get_father(y);
    if (x != y)
        father[x] = y;
}

variable_types: y int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
int v4; // er12
int *v5; // rbp
int i; // ebx
int v7; // eax
__int64 v8; // r13
unsigned __int64 v9; // rbx
int v10; // er12
int v11; // ebp
int v13; // [rsp+8h] [rbp-30h]
int v14; // [rsp+Ch] [rbp-2Ch]

__isoc99_scanf();
if ( v14 <= 0 )
{
qsort(edge, 0LL, 0xCuLL, (__compar_fn_t)cmp);
}
else
{
v3 = 1LL;
v4 = 0;
do
{
father[v3] = v3;
v5 = &edge[3 * v4];
for ( i = 1; i <= v14; ++i )
{
__isoc99_scanf();
*v5 = v3;
v5[1] = i;
v5[2] = v13;
v7 = v4 + i;
v5 += 3;
}
v4 = v7;
++v3;
}
while ( v14 >= (int)v3 );
qsort(edge, v7, 0xCuLL, (__compar_fn_t)cmp);
if ( v4 > 0 )
{
v8 = 4 * (3LL * (unsigned int)(v4 - 1) + 3);
v9 = 0LL;
v10 = 0;
do
{
v11 = get_father(edge[v9 / 4]);
if ( v11 != (unsigned int)get_father(dword_4049C4[v9 / 4]) )
{
set_union(edge[v9 / 4], dword_4049C4[v9 / 4]);
++v10;
}
if ( v14 - 1 <= v10 )
break;
v9 += 12LL;
}
while ( v8 != v9 );
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
int v4; // er12
int *v5; // rbp
int i; // ebx
int v7; // eax
__int64 v8; // r13
unsigned __int64 v9; // rbx
int v10; // er12
int v11; // ebp
int v13; // [rsp+8h] [rbp-30h]
int v14; // [rsp+Ch] [rbp-2Ch]

__isoc99_scanf();
if ( v14 <= 0 )
{
qsort(edge, 0LL, 0xCuLL, (__compar_fn_t)cmp);
}
else
{
v3 = 1LL;
v4 = 0;
do
{
father[v3] = v3;
v5 = &edge[3 * v4];
for ( i = 1; i <= v14; ++i )
{
__isoc99_scanf();
*v5 = v3;
v5[1] = i;
v5[2] = v13;
v7 = v4 + i;
v5 += 3;
}
v4 = v7;
++v3;
}
while ( v14 >= (int)v3 );
qsort(edge, v7, 0xCuLL, (__compar_fn_t)cmp);
if ( v4 > 0 )
{
v8 = 4 * (3LL * (unsigned int)(v4 - 1) + 3);
v9 = 0LL;
v10 = 0;
do
{
v11 = func8(edge[v9 / 4]);
if ( v11 != (unsigned int)func8(dword_4049C4[v9 / 4]) )
{
func9(edge[v9 / 4], dword_4049C4[v9 / 4]);
++v10;
}
if ( v14 - 1 <= v10 )
break;
v9 += 12LL;
}
while ( v8 != v9 );
}
}
return 0;
}

original_code:
int main(void)
{
    int n, i, j, value;
    int length = 0;
    int max = 0;
    int num_E = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i ++)
    {
        father[i] = i;
        for (j = 1; j <=n; j ++)
        {
            scanf("%d", &value);
            edge[length].x = i;
            edge[length].y = j;
            edge[length].value = value;
            length ++;
        }
    }
    qsort(edge, length, sizeof(Edge), cmp);
    for (i = 0; i < length; i ++)
    {
        if (get_father(edge[i].x) != get_father(edge[i].y))
        {
            set_union(edge[i].x, edge[i].y);
            num_E ++;
        }
        if (num_E >= n-1)
            break;
    }
    return 0;
}

canonical_original_code:
int main()
{
    int n; int i; int j; int value;
    int length = 0;
    int max = 0;
    int num_E = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i ++)
    {
        father[i] = i;
        for (j = 1; j <=n; j ++)
        {
            scanf("%d", &value);
            edge[length].x = i;
            edge[length].y = j;
            edge[length].value = value;
            length ++;
        }
    }
    qsort(edge, length, sizeof(Edge), cmp);
    for (i = 0; i < length; i ++)
    {
        if (get_father(edge[i].x) != get_father(edge[i].y))
        {
            set_union(edge[i].x, edge[i].y);
            num_E ++;
        }
        if (num_E >= n-1)
            break;
    }
    return 0;
}

variable_types: n int

variable_types: i int

variable_types: j int

variable_types: value int

variable_types: length int

variable_types: max int


sort

original_code:
void sort(int *begin, int *end, int n)
{
    int i, j, temp;
    for (i = 0; i < n; i ++)
        for (j = i+1; j < n; j ++)
            if (end[j] < end[i])
            {
                temp = end[j];
                end[j] = end[i];
                end[i] = temp;
                temp = begin[j];
                begin[j] = begin[i];
                begin[i] = temp;
            }
}

canonical_original_code:
void sort(int *begin, int *end, int n)
{
    int i; int j; int temp;
    for (i = 0; i < n; i ++)
        for (j = i+1; j < n; j ++)
            if (end[j] < end[i])
            {
                temp = end[j];
                end[j] = end[i];
                end[i] = temp;
                temp = begin[j];
                begin[j] = begin[i];
                begin[i] = temp;
            }
}

variable_types:
begin
int *

variable_types:
end
int *

variable_types: temp int


select

original_code:
void select(int *begin, int *end, int n)
{
    int index = 0;
    int ans[MAXN], length = 0;
    int last_time = 0;
    while (index < n)
    {
        if (begin[index] > last_time)
        {
            ans[length ++] = index;
            last_time = end[index];
        }
        index ++;
    }
    for (index = 0; index < length; index ++)
        printf("%d ", ans[index] + 1);
    printf("\n");
}

canonical_original_code:
void select(int *begin, int *end, int n)
{
    int index = 0;
    int ans[-1]; int length = 0;
    int last_time = 0;
    while (index < n)
    {
        if (begin[index] > last_time)
        {
            ans[length ++] = index;
            last_time = end[index];
        }
        index ++;
    }
    for (index = 0; index < length; index ++)
        printf("%d ", ans[index] + 1);
    printf("\n");
}

variable_types: index int

variable_types:
ans
int [-1]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
_DWORD *v4; // rbp
__int64 v5; // rbx
int v7; // [rsp+Ch] [rbp-7Ch]
_DWORD v8[12]; // [rsp+10h] [rbp-78h] BYREF
_DWORD v9[18]; // [rsp+40h] [rbp-48h] BYREF

__isoc99_scanf();
v3 = v7;
if ( v7 > 0 )
{
v4 = v9;
v5 = 0LL;
do
{
__isoc99_scanf();
v3 = v7;
++v5;
++v4;
}
while ( v7 > (int)v5 );
}
sort(v9, v8, v3);
select((__int64)v9, (__int64)v8, v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // edx
_DWORD *v4; // rbp
__int64 v5; // rbx
int v7; // [rsp+Ch] [rbp-7Ch]
_DWORD v8[12]; // [rsp+10h] [rbp-78h] BYREF
_DWORD v9[18]; // [rsp+40h] [rbp-48h] BYREF

__isoc99_scanf();
v3 = v7;
if ( v7 > 0 )
{
v4 = v9;
v5 = 0LL;
do
{
__isoc99_scanf();
v3 = v7;
++v5;
++v4;
}
while ( v7 > (int)v5 );
}
func7(v9, v8, v3);
func8((__int64)v9, (__int64)v8, v7);
return 0;
}

original_code:
int main(void)
{
    int begin[MAXN], end[MAXN];
    int n, i;
    scanf("%d\n", &n);
    for (i = 0; i < n; i ++)
        scanf("%d %d\n", &begin[i], &end[i]);
    sort(begin, end, n);
    select(begin, end, n);
    return 0;
}

canonical_original_code:
int main()
{
    int begin[-1]; int end[-1];
    int n; int i;
    scanf("%d\n", &n);
    for (i = 0; i < n; i ++)
        scanf("%d %d\n", &begin[i], &end[i]);
    sort(begin, end, n);
    select(begin, end, n);
    return 0;
}

variable_types:
begin
int [-1]

variable_types:
end
int [-1]


output

decompiled_code:
int __fastcall output(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // r13

if ( (_DWORD)a2 == a3 )
return printf("A%d", a2);
putchar(40);
v4 = a1 + 28LL * (int)a2;
output(a1, (unsigned int)a2, *(unsigned int *)(v4 + 4LL * (int)a3));
output(a1, (unsigned int)(*(_DWORD *)(v4 + 4LL * (int)a3) + 1), a3);
return putchar(41);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // r13

if ( (_DWORD)a2 == a3 )
return printf("A%d", a2);
putchar(40);
v4 = a1 + 28LL * (int)a2;
func7(a1, (unsigned int)a2, *(unsigned int *)(v4 + 4LL * (int)a3));
func7(a1, (unsigned int)(*(_DWORD *)(v4 + 4LL * (int)a3) + 1), a3);
return putchar(41);
}

original_code:
void output(int s[][MAXN], int i, int j)
{
    if( i == j)
        printf("A%d", i);
    else
    {
        printf("(");
        output(s, i, s[i][j]);
        output(s, s[i][j] + 1, j);
        printf(")");
    }
}

canonical_original_code:
void output(int s[][-1], int i, int j)
{
    if( i == j)
        printf("A%d", i);
    else
    {
        printf("(");
        output(s, i, s[i][j]);
        output(s, s[i][j] + 1, j);
        printf(")");
    }
}


matrix

original_code:
int matrix(int *list)
{
    int result[MAXN][MAXN], record[MAXN][MAXN];
    memset(result, 0, MAXN * MAXN * sizeof(int));
    memset(record, 0, MAXN * MAXN * sizeof(int));
    int i, j, k, q, temp;
    for (q = 2; q <= n; q ++)
        for (i = 1; i <= n - q + 1; i ++)
        {
            j = i + q - 1;
            result[i][j] = 0x7fffffff;
            for (k = i; k <= j - 1; k ++)
            {
                temp = result[i][k] + result[k+1][j] +
                    list[i-1] * list[k] * list[j];
                if (temp < result[i][j])
                {
                    result[i][j] = temp;
                    record[i][j] = k;
                }
            }
        }
    output(record, 1, n);
    return result[1][n];
}

canonical_original_code:
int matrix(int *list)
{
    int result[-1][-1]; int record[-1][-1];
    memset(result, 0, MAXN * MAXN * sizeof(int));
    memset(record, 0, MAXN * MAXN * sizeof(int));
    int i; int j; int k; int q; int temp;
    for (q = 2; q <= n; q ++)
        for (i = 1; i <= n - q + 1; i ++)
        {
            j = i + q - 1;
            result[i][j] = 0x7fffffff;
            for (k = i; k <= j - 1; k ++)
            {
                temp = result[i][k] + result[k+1][j] +
                    list[i-1] * list[k] * list[j];
                if (temp < result[i][j])
                {
                    result[i][j] = temp;
                    record[i][j] = k;
                }
            }
        }
    output(record, 1, n);
    return result[1][n];
}

variable_types:
list
int *

variable_types:
result
int [-1][-1]

variable_types:
record
int [-1][-1]

variable_types: k int

variable_types: q int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE *v3; // rbp
int v4; // ebx
unsigned int v5; // eax
_BYTE v7[56]; // [rsp+0h] [rbp-38h] BYREF

__isoc99_scanf();
if ( n >= 0 )
{
v3 = v7;
v4 = 0;
do
{
__isoc99_scanf();
++v4;
v3 += 4;
}
while ( n >= v4 );
}
v5 = matrix((__int64)v7);
printf("\nans:%d\n", v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE *v3; // rbp
int v4; // ebx
unsigned int v5; // eax
_BYTE v7[56]; // [rsp+0h] [rbp-38h] BYREF

__isoc99_scanf();
if ( n >= 0 )
{
v3 = v7;
v4 = 0;
do
{
__isoc99_scanf();
++v4;
v3 += 4;
}
while ( n >= v4 );
}
v5 = func8((__int64)v7);
printf("\nans:%d\n", v5);
return 0;
}

original_code:
int main(void)
{
    int list[MAXN];
    int i;
    scanf("%d", &n);
    for (i = 0; i <= n; i ++)
        scanf("%d", &list[i]);
    int ans = matrix(list);
    printf("\nans:%d\n", ans);
    return 0;
}

canonical_original_code:
int main()
{
    int list[-1];
    int i;
    scanf("%d", &n);
    for (i = 0; i <= n; i ++)
        scanf("%d", &list[i]);
    int ans = matrix(list);
    printf("\nans:%d\n", ans);
    return 0;
}

variable_types:
list
int [-1]

variable_types: i int

variable_types: ans int


output

decompiled_code:
int __fastcall output(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func15(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

variable_types: j int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 i; // rbx
__int64 j; // rbx
int *v6; // rbx
__int64 *v7; // rax

v3 = time(0LL);
srand(v3);
for ( i = 0LL; i != 5; ++i )
LIST[i] = rand() % 100;
output(LIST, 5LL);
for ( j = 0LL; j != 5; ++j )
tree_insert(LIST[j]);
tree_walk((unsigned int *)ROOT);
putchar(10);
v6 = LIST;
do
{
v7 = (__int64 *)tree_search(*v6);
if ( v7 )
tree_delete(v7);
tree_walk((unsigned int *)ROOT);
putchar(10);
++v6;
}
while ( v6 != (int *)&unk_4040CC );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 i; // rbx
__int64 j; // rbx
int *v6; // rbx
__int64 *v7; // rax

v3 = time(0LL);
srand(v3);
for ( i = 0LL; i != 5; ++i )
LIST[i] = rand() % 100;
func15(LIST, 5LL);
for ( j = 0LL; j != 5; ++j )
func12(LIST[j]);
func7((unsigned int *)ROOT);
putchar(10);
v6 = LIST;
do
{
v7 = (__int64 *)func10(*v6);
if ( v7 )
func14(v7);
func7((unsigned int *)ROOT);
putchar(10);
++v6;
}
while ( v6 != (int *)&unk_4040CC );
return 0;
}


repo: wcharczuk/urlencode

decompiled_code:
__int64 __fastcall is_non_symbol(int a1, __int64 a2, int a3)
{
__int64 result; // rax
unsigned int v4; // eax

result = 1LL;
if ( (_BYTE)a1 )
{
v4 = (a1 & 0xFFFFFFDF) - 65;
LOBYTE(v4) = (unsigned __int8)v4 <= 0x19u;
LOBYTE(a3) = (unsigned int)((char)a1 - 48) <= 9;
result = a3 | v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, __int64 a2, int a3)
{
__int64 result; // rax
unsigned int v4; // eax

result = 1LL;
if ( (_BYTE)a1 )
{
v4 = (a1 & 0xFFFFFFDF) - 65;
LOBYTE(v4) = (unsigned __int8)v4 <= 0x19u;
LOBYTE(a3) = (unsigned int)((char)a1 - 48) <= 9;
result = a3 | v4;
}
return result;
}

original_code:
_Bool is_non_symbol(char c)
{
    if(c == '\0') return 1;
    int c_int = (int)c;
    return (c_int >= 48 && c_int <= 57) || (c_int >= 65 && c_int <= 90) || (c_int >= 97 && c_int <= 122);
}

variable_types: c char


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbx
char *v6; // [rsp+0h] [rbp-18h] BYREF
size_t n[2]; // [rsp+8h] [rbp-10h] BYREF

if ( argc > 1 )
{
v3 = url_encode(argv[1], (__int64)argv);
printf("%s", v3);
free(v3);
}
else
{
v6 = 0LL;
while ( getline(&v6, n, stdin) != -1 )
{
v4 = url_encode(v6, (__int64)n);
printf("%s", v4);
free(v4);
}
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
    if(argc > 1)
    {
        char *input = argv[1];
        char *encoded = url_encode(input);
        printf("%s", encoded);
        free(encoded);
    }
    else
    {
        char *line = ((void *)0);
        size_t size;
        while(getline(&line, &size, stdin) != -1)
        {
            char *encoded = url_encode(line);
            printf("%s", encoded);
            free(encoded);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    if(argc > 1)
    {
        char *input = argv[1];
        char *encoded = url_encode(input);
        printf("%s", encoded);
        free(encoded);
    }
    else
    {
        char *line = ((void *)0);
        unsigned long size;
        while(getline(&line, &size, stdin) != -1)
        {
            char *encoded = url_encode(line);
            printf("%s", encoded);
            free(encoded);
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
input
char *

variable_types:
encoded
char *

variable_types:
line
char *

variable_types:
size
unsigned long


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbx
char *v6; // [rsp+0h] [rbp-18h] BYREF
size_t n[2]; // [rsp+8h] [rbp-10h] BYREF

if ( argc > 1 )
{
v3 = url_decode(argv[1]);
printf("%s", v3);
free(v3);
}
else
{
v6 = 0LL;
while ( getline(&v6, n, stdin) != -1 )
{
v4 = url_decode(v6);
printf("%s", v4);
free(v4);
}
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
    if(argc > 1)
    {
        char *input = argv[1];
        char *decoded = url_decode(input);
        printf("%s",decoded);
     free(decoded);
    }
    else
    {
        char *line = ((void *)0);
        size_t size;
        while(getline(&line, &size, stdin) != -1)
        {
         char *decoded = url_decode(line);
            printf("%s", decoded);
            free(decoded);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    if(argc > 1)
    {
        char *input = argv[1];
        char *decoded = url_decode(input);
        printf("%s",decoded);
     free(decoded);
    }
    else
    {
        char *line = ((void *)0);
        unsigned long size;
        while(getline(&line, &size, stdin) != -1)
        {
         char *decoded = url_decode(line);
            printf("%s", decoded);
            free(decoded);
        }
    }
    return 0;
}

variable_types:
decoded
char *


repo: wconrad/brazzle

decompiled_code:
__int64 __fastcall memcpy_forward(__int64 a1, __int64 a2, int a3)
{
__int64 i; // rax

if ( a3 )
{
for ( i = 0LL; i != a3; ++i )
*(_BYTE *)(a1 + i) = *(_BYTE *)(a2 + i);
}
return a1;
}

original_code:
static void *
memcpy_forward(void * dst, const void * src, unsigned size) {
  for(unsigned i = 0; i < size; i++)
    ((char *)dst)[i] = ((const char *)src)[i];
  return dst;
}

canonical_original_code:
static void *memcpy_forward(void *dst, void *src, unsigned int size) {
  for(unsigned int i = 0; i < size; i++)
    ((char *)dst)[i] = ((char *)src)[i];
  return dst;
}

variable_types:
dst
void *

variable_types:
src
void *

variable_types:
size
unsigned int

variable_types:
i
unsigned int

return_type: void *


decompiled_code:
__int64 __fastcall memcpy_backward(__int64 a1, __int64 a2, int a3)
{
unsigned int v3; // edx

if ( a3 )
{
v3 = a3 - 1;
do
{
*(_BYTE *)(a1 + v3) = *(_BYTE *)(a2 + v3);
--v3;
}
while ( v3 != -1 );
}
return a1;
}

original_code:
static void *
memcpy_backward(void * dst, const void * src, unsigned size) {
  for(unsigned i = 0; i < size; i++)
    ((char *)dst)[(size - 1) - i] = ((const char *)src)[(size - 1) - i];
  return dst;
}

canonical_original_code:
static void *memcpy_backward(void *dst, void *src, unsigned int size) {
  for(unsigned int i = 0; i < size; i++)
    ((char *)dst)[(size - 1) - i] = ((char *)src)[(size - 1) - i];
  return dst;
}


memcpy

decompiled_code:
// attributes: thunk
__int64 __fastcall memcpy(__int64 a1, __int64 a2, int a3)
{
return memcpy_forward(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int a3)
{
return func0(a1, a2, a3);
}

original_code:
void *
memcpy(void * dst, const void * src, unsigned size) {
  return memcpy_forward(dst, src, size);
}

canonical_original_code:
void *memcpy(void *dst, void *src, unsigned int size) {
  return memcpy_forward(dst, src, size);
}


memmove

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

if ( a2 < a1 )
result = memcpy_backward(a1, a2, a3);
else
result = memcpy_forward(a1, a2, a3);
return result;
}

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

if ( a2 < a1 )
result = func1(a1, a2, a3);
else
result = func0(a1, a2, a3);
return result;
}

original_code:
void *
memmove(void * dst, const void * src, unsigned size) {
  if(src >= dst)
    return memcpy_forward(dst, src, size);
  else
    return memcpy_backward(dst, src, size);
}

canonical_original_code:
void *memmove(void *dst, void *src, unsigned int size) {
  if(src >= dst)
    return memcpy_forward(dst, src, size);
  else
    return memcpy_backward(dst, src, size);
}


strrev

original_code:
char *
strrev(char * s) {
  char * a = s;
  char * b = s;
  while(*b != '\0')
    b++;
  b--;
  while (a < b) {
    char temp = *a;
    *a++ = *b;
    *b-- = temp;
  }
  return s;
}

canonical_original_code:
char *strrev(char *s) {
  char *a = s;
  char *b = s;
  while(*b != '\0')
    b++;
  b--;
  while (a < b) {
    char temp = *a;
    *a++ = *b;
    *b-- = temp;
  }
  return s;
}

variable_types:
s
char *

variable_types:
a
char *

variable_types:
b
char *

variable_types: temp char


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // ecx

if ( !*a1 )
return 0;
v1 = a1 + 1;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // ecx

if ( !*a1 )
return 0;
v1 = a1 + 1;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

original_code:
unsigned strlen(const char * s) {
  unsigned length = 0;
  while (*s++ != '\0')
    length++;
  return length;
}

canonical_original_code:
unsigned int strlen(char *s) {
  unsigned int length = 0;
  while (*s++ != '\0')
    length++;
  return length;
}

variable_types:
length
unsigned int

return_type: unsigned int


decompiled_code:
void __fastcall apply_bios_map_if(unsigned __int8 a1)
{
unsigned int v1; // ebx
unsigned int *v2; // rax

if ( loader_data[0] )
{
v1 = 0;
do
{
v2 = &loader_data[6 * v1 + 1];
if ( (v2[4] == 1) == a1 && !v2[1] && !v2[3] )
pmmap_mark_region(loader_data[6 * v1 + 1], v2[2], a1 ^ 1u);
++v1;
}
while ( loader_data[0] > v1 );
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned __int8 a1)
{
unsigned int v1; // ebx
unsigned int *v2; // rax

if ( loader_data[0] )
{
v1 = 0;
do
{
v2 = &loader_data[6 * v1 + 1];
if ( (v2[4] == 1) == a1 && !v2[1] && !v2[3] )
pmmap_mark_region(loader_data[6 * v1 + 1], v2[2], a1 ^ 1u);
++v1;
}
while ( loader_data[0] > v1 );
}
}

original_code:
static void apply_bios_map_if(_Bool avail) {
  for(unsigned i = 0; i < loader_data.bios_memory_map.count; i++) {
    BiosMemoryMapEntry * entry = loader_data.bios_memory_map.entries + i;
    if(avail != (entry->type == 1))
      continue;
    if(entry->base_addr_high != 0 || entry->length_high != 0)
      continue;
    pmmap_mark_region((PhysicalAddress) entry->base_addr_low,
                      entry->length_low,
                      avail ? BLOCK_AVAIL : BLOCK_UNAVAIL);
  }
}

canonical_original_code:
static void apply_bios_map_if(_Bool avail) {
  for(unsigned int i = 0; i < loader_data.bios_memory_map.count; i++) {
    struct BiosMemoryMapEntry *entry = loader_data.bios_memory_map.entries + i;
    if(avail != (entry->type == 1))
      continue;
    if(entry->base_addr_high != 0 || entry->length_high != 0)
      continue;
    pmmap_mark_region((void *) entry->base_addr_low,
                      entry->length_low,
                      avail ? BLOCK_AVAIL : BLOCK_UNAVAIL);
  }
}

variable_types: struct BiosMemoryMapEntry { unsigned int base_addr_low; unsigned int base_addr_high; unsigned int length_low; unsigned int length_high; unsigned int type; unsigned int reserved; } *


decompiled_code:
void apply_bios_map()
{
pmmap_mark_all_used();
apply_bios_map_if(1u);
apply_bios_map_if(0);
}

canonical_decompiled_code:
void func1()
{
pmmap_mark_all_used();
func0(1u);
func0(0);
}

original_code:
static void apply_bios_map() {
  pmmap_mark_all_used();
  apply_bios_map_if(1);
  apply_bios_map_if(0);
}


decompiled_code:
__int64 mark_kernel_memory_used()
{
return pmmap_mark_region(kernel_phys_addr_var, kernel_max_bytes_var, 1LL);
}

canonical_decompiled_code:
__int64 func2()
{
return pmmap_mark_region(kernel_phys_addr_var, kernel_max_bytes_var, 1LL);
}

original_code:
static void mark_kernel_memory_used() {
  pmmap_mark_region((PhysicalAddress) kernel_phys_addr_var,
                    kernel_max_bytes_var,
                    BLOCK_UNAVAIL);
}

canonical_original_code:
static void mark_kernel_memory_used() {
  pmmap_mark_region((void *) kernel_phys_addr_var,
                    kernel_max_bytes_var,
                    BLOCK_UNAVAIL);
}


decompiled_code:
__int64 mark_page_tables_used()
{
unsigned int i; // ebx
__int64 result; // rax

for ( i = 0; i != 1024; ++i )
{
result = page_table_phys_addr(i);
if ( result )
result = pmmap_mark_region(result, 4096LL, 1LL);
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int i; // ebx
__int64 result; // rax

for ( i = 0; i != 1024; ++i )
{
result = page_table_phys_addr(i);
if ( result )
result = pmmap_mark_region(result, 4096LL, 1LL);
}
return result;
}

original_code:
static void mark_page_tables_used() {
  for(int i = 0; i < 0x400; i++) {
    PhysicalAddress phys_addr = page_table_phys_addr(i);
    if(phys_addr != 0)
      pmmap_mark_region(phys_addr, (0x400 * sizeof(uint32_t)), BLOCK_UNAVAIL);
  }
}

canonical_original_code:
static void mark_page_tables_used() {
  for(int i = 0; i < 0x400; i++) {
    void *phys_addr = page_table_phys_addr(i);
    if(phys_addr != 0)
      pmmap_mark_region(phys_addr, (0x400 * sizeof(unsigned int)), BLOCK_UNAVAIL);
  }
}

variable_types: i int


decompiled_code:
__int64 mem_init()
{
pmmap_init();
apply_bios_map();
mark_kernel_memory_used();
return mark_page_tables_used();
}

canonical_decompiled_code:
__int64 func4()
{
pmmap_init();
func1();
func2();
return func3();
}

original_code:
void mem_init() {
  pmmap_init();
  apply_bios_map();
  mark_kernel_memory_used();
  mark_page_tables_used();
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init(argc, argv, envp);
vty_puts("Hello, world...\n");
bmmap_print();
pmmap_print();
return halt();
}

original_code:
void main() {
  init();
  vty_puts("Hello, world...\n");
  bmmap_print();
  pmmap_print();
  halt();
}


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

result = memmove(&loader_data, loader_data_p, 0x244uLL);
loader_data_p = 0LL;
return result;
}

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

result = memmove(&loader_data, loader_data_p, 0x244uLL);
loader_data_p = 0LL;
return result;
}

original_code:
void loader_data_init() {
  memmove(&loader_data, loader_data_p, sizeof(loader_data));
  loader_data_p = ((void *)0);
}


decompiled_code:
__int64 __fastcall print_registers(unsigned int *a1, unsigned int *a2)
{
vty_printf(" eflags=%08x", a2[2]);
vty_puts("\n");
vty_printf(" eax=%08x", a1[12]);
vty_printf(" ebx=%08x", a1[9]);
vty_printf(" ecx=%08x", a1[11]);
vty_printf(" edx=%08x", a1[10]);
vty_puts("\n");
vty_printf(" ebp=%08x", a1[7]);
vty_printf(" esi=%08x", a1[6]);
vty_printf(" edi=%08x", a1[5]);
vty_puts("\n");
vty_printf(" cs:eip=%04x:%08x", a2[1], *a2);
vty_printf(" ss:esp=%04x:%08x", a1[3], a1[8]);
vty_puts("\n");
vty_printf(" ds=%04x", a1[4]);
vty_printf(" es=%04x", a1[2]);
vty_printf(" fs=%04x", a1[1]);
vty_printf(" gs=%04x", *a1);
return vty_puts("\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, unsigned int *a2)
{
vty_printf(" eflags=%08x", a2[2]);
vty_puts("\n");
vty_printf(" eax=%08x", a1[12]);
vty_printf(" ebx=%08x", a1[9]);
vty_printf(" ecx=%08x", a1[11]);
vty_printf(" edx=%08x", a1[10]);
vty_puts("\n");
vty_printf(" ebp=%08x", a1[7]);
vty_printf(" esi=%08x", a1[6]);
vty_printf(" edi=%08x", a1[5]);
vty_puts("\n");
vty_printf(" cs:eip=%04x:%08x", a2[1], *a2);
vty_printf(" ss:esp=%04x:%08x", a1[3], a1[8]);
vty_puts("\n");
vty_printf(" ds=%04x", a1[4]);
vty_printf(" es=%04x", a1[2]);
vty_printf(" fs=%04x", a1[1]);
vty_printf(" gs=%04x", *a1);
return vty_puts("\n");
}

original_code:
static void print_registers(const StubRegisters * stub_registers,
                            const TrapRegisters * trap_registers) {
  vty_printf(" eflags=%08x", trap_registers->eflags);
  vty_puts("\n");
  vty_printf(" eax=%08x", stub_registers->eax);
  vty_printf(" ebx=%08x", stub_registers->ebx);
  vty_printf(" ecx=%08x", stub_registers->ecx);
  vty_printf(" edx=%08x", stub_registers->edx);
  vty_puts("\n");
  vty_printf(" ebp=%08x", stub_registers->ebp);
  vty_printf(" esi=%08x", stub_registers->esi);
  vty_printf(" edi=%08x", stub_registers->edi);
  vty_puts("\n");
  vty_printf(" cs:eip=%04x:%08x", trap_registers->cs, trap_registers->eip);
  vty_printf(" ss:esp=%04x:%08x", stub_registers->ss, stub_registers->esp);
  vty_puts("\n");
  vty_printf(" ds=%04x", stub_registers->ds);
  vty_printf(" es=%04x", stub_registers->es);
  vty_printf(" fs=%04x", stub_registers->fs);
  vty_printf(" gs=%04x", stub_registers->gs);
  vty_puts("\n");
}

canonical_original_code:
static void print_registers(struct StubRegisters *stub_registers, struct TrapRegisters *trap_registers) {
  vty_printf(" eflags=%08x", trap_registers->eflags);
  vty_puts("\n");
  vty_printf(" eax=%08x", stub_registers->eax);
  vty_printf(" ebx=%08x", stub_registers->ebx);
  vty_printf(" ecx=%08x", stub_registers->ecx);
  vty_printf(" edx=%08x", stub_registers->edx);
  vty_puts("\n");
  vty_printf(" ebp=%08x", stub_registers->ebp);
  vty_printf(" esi=%08x", stub_registers->esi);
  vty_printf(" edi=%08x", stub_registers->edi);
  vty_puts("\n");
  vty_printf(" cs:eip=%04x:%08x", trap_registers->cs, trap_registers->eip);
  vty_printf(" ss:esp=%04x:%08x", stub_registers->ss, stub_registers->esp);
  vty_puts("\n");
  vty_printf(" ds=%04x", stub_registers->ds);
  vty_printf(" es=%04x", stub_registers->es);
  vty_printf(" fs=%04x", stub_registers->fs);
  vty_printf(" gs=%04x", stub_registers->gs);
  vty_puts("\n");
}

variable_types: struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } *

variable_types: struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } *


decompiled_code:
__int64 __fastcall print_trap_with_error(const char *a1, unsigned int *a2)
{
vty_printf("\n%s fault %04x\n", a1, a2[13]);
return print_registers(a2, a2 + 14);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, unsigned int *a2)
{
vty_printf("\n%s fault %04x\n", a1, a2[13]);
return func0(a2, a2 + 14);
}

original_code:
static void print_trap_with_error(const char * name,
                           TrapParamsWithError * params) {
  vty_printf("\n%s fault %04x\n", name, params->error_code);
  print_registers(&params->stub_registers, &params->trap_registers);
}

canonical_original_code:
static void print_trap_with_error(char *name, struct TrapParamsWithError *params) {
  vty_printf("\n%s fault %04x\n", name, params->error_code);
  print_registers(&params->stub_registers, &params->trap_registers);
}

variable_types:
name
char *

variable_types: struct TrapParamsWithError { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; unsigned int error_code; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; } *

user_defined_types: struct TrapParamsWithError { struct StubRegisters stub_registers; unsigned int error_code; struct TrapRegisters trap_registers; }


decompiled_code:
__int64 load_idt()
{
__int16 v1; // [rsp+6h] [rbp-12h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1 = 135;
v2 = get_physaddr(&idt);
return lidt(&v1);
}

canonical_decompiled_code:
__int64 func3()
{
__int16 v1; // [rsp+6h] [rbp-12h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1 = 135;
v2 = get_physaddr(&idt);
return lidt(&v1);
}

original_code:
static void load_idt() {
  IdtAddr idt_addr = {
    .addr = get_physaddr(idt),
    .size = sizeof(idt) - 1,
  };
  lidt(&idt_addr);
}

canonical_original_code:
static void load_idt() {
  struct idt_addr idt_addr = {
    .addr = get_physaddr(idt),
    .size = sizeof(idt) - 1,
  };
  lidt(&idt_addr);
}

variable_types: struct idt_addr { unsigned short size; void *addr; }


decompiled_code:
__int64 __fastcall int3_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap("BP", (unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func1("BP", (unsigned int *)&a7);
return halt();
}

original_code:
void int3_handler(TrapParams params) {
  print_trap("BP", &params);
  halt();
}

canonical_original_code:
void int3_handler(struct TrapParams params) {
  print_trap("BP", &params);
  halt();
}

variable_types: struct TrapParams { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; }

user_defined_types: struct TrapParams { struct StubRegisters stub_registers; struct TrapRegisters trap_registers; }


decompiled_code:
__int64 __fastcall int8_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap_with_error("DF", (unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func2("DF", (unsigned int *)&a7);
return halt();
}

original_code:
void int8_handler(TrapParamsWithError params) {
  print_trap_with_error("DF", &params);
  halt();
}

canonical_original_code:
void int8_handler(struct TrapParamsWithError params) {
  print_trap_with_error("DF", &params);
  halt();
}

variable_types: struct TrapParamsWithError { struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; } stub_registers; unsigned int error_code; struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; } trap_registers; }


decompiled_code:
__int64 __fastcall int13_handler(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
print_trap_with_error("GP", (unsigned int *)&a7);
return halt();
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7)
{
func2("GP", (unsigned int *)&a7);
return halt();
}

original_code:
void int13_handler(TrapParamsWithError params) {
  print_trap_with_error("GP", &params);
  halt();
}

canonical_original_code:
void int13_handler(struct TrapParamsWithError params) {
  print_trap_with_error("GP", &params);
  halt();
}

user_defined_types: struct StubRegisters { unsigned int gs; unsigned int fs; unsigned int es; unsigned int ss; unsigned int ds; unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int esp; unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; }

user_defined_types: struct TrapRegisters { unsigned int eip; unsigned int cs; unsigned int eflags; }


decompiled_code:
__int64 int_init()
{
set_idt_entry(3LL, &int3_stub, 143LL);
set_idt_entry(8LL, &int8_stub, 143LL);
set_idt_entry(13LL, &int13_stub, 143LL);
return load_idt();
}

canonical_decompiled_code:
__int64 func7()
{
set_idt_entry(3LL, &int3_stub, 143LL);
set_idt_entry(8LL, &int8_stub, 143LL);
set_idt_entry(13LL, &int13_stub, 143LL);
return func3();
}


utoa

decompiled_code:
__int64 __fastcall utoa(__int64 *a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // ebx
__int64 v5; // r14
unsigned int v6; // eax

v3 = a2;
v5 = *a1;
if ( a2 )
{
do
{
buffwrite(a1, (unsigned int)byte_160[v3 % a3]);
v6 = v3;
v3 /= a3;
}
while ( a3 <= v6 );
}
else
{
buffwrite(a1, 48LL);
}
buffwrite(a1, 0LL);
return strrev(v5);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // ebx
__int64 v5; // r14
unsigned int v6; // eax

v3 = a2;
v5 = *a1;
if ( a2 )
{
do
{
buffwrite(a1, (unsigned int)byte_160[v3 % a3]);
v6 = v3;
v3 /= a3;
}
while ( a3 <= v6 );
}
else
{
buffwrite(a1, 48LL);
}
buffwrite(a1, 0LL);
return strrev(v5);
}

original_code:
static void utoa(BufferCursor * cursor,
                 unsigned n,
                 int radix) {
  char * start = cursor->p;
  if(n == 0)
    buffwrite(cursor, '0');
  else
    while(n != 0) {
      char c = "0123456789abcdef"[n % radix];
      buffwrite(cursor, c);
      n /= radix;
    }
  buffwrite(cursor, '\0');
  strrev(start);
}

canonical_original_code:
static void utoa(struct BufferCursor *cursor, unsigned int n, int radix) {
  char *start = cursor->p;
  if(n == 0)
    buffwrite(cursor, '0');
  else
    while(n != 0) {
      char c = "0123456789abcdef"[n % radix];
      buffwrite(cursor, c);
      n /= radix;
    }
  buffwrite(cursor, '\0');
  strrev(start);
}

variable_types: struct BufferCursor { char *p; int remaining; } *

variable_types:
n
unsigned int

variable_types: radix int

variable_types:
start
char *

variable_types: c char


utodec

decompiled_code:
__int64 __fastcall utodec(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 0xAu);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 0xAu);
return a1;
}

original_code:
char * utodec(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 10);
  return buffer;
}

canonical_original_code:
char *utodec(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 10);
  return buffer;
}

variable_types:
buffer
char *

variable_types:
buflen
unsigned long

variable_types: struct BufferCursor { char *p; int remaining; }


utohex

decompiled_code:
__int64 __fastcall utohex(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 0x10u);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 0x10u);
return a1;
}

original_code:
char * utohex(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 16);
  return buffer;
}

canonical_original_code:
char *utohex(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 16);
  return buffer;
}

user_defined_types: struct BufferCursor { char *p; int remaining; }


utobin

decompiled_code:
__int64 __fastcall utobin(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
utoa(&v4, a3, 2u);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned int a3)
{
__int64 v4; // [rsp+0h] [rbp-18h] BYREF
int v5; // [rsp+8h] [rbp-10h]

v4 = a1;
v5 = a2;
func0(&v4, a3, 2u);
return a1;
}

original_code:
char * utobin(char * buffer, size_t buflen, unsigned n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 2);
  return buffer;
}

canonical_original_code:
char *utobin(char *buffer, unsigned long buflen, unsigned int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  utoa(&cursor, n, 2);
  return buffer;
}


itodec

decompiled_code:
__int64 __fastcall itodec(__int64 a1, int a2, int a3)
{
unsigned int v3; // esi
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
int v7; // [rsp+8h] [rbp-20h]

v6 = a1;
v7 = a2;
v3 = a3;
if ( a3 < 0 )
{
buffwrite(&v6, 45LL);
v3 = -a3;
}
utoa(&v6, v3, 0xAu);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, int a3)
{
unsigned int v3; // esi
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
int v7; // [rsp+8h] [rbp-20h]

v6 = a1;
v7 = a2;
v3 = a3;
if ( a3 < 0 )
{
buffwrite(&v6, 45LL);
v3 = -a3;
}
func0(&v6, v3, 0xAu);
return a1;
}

original_code:
char * itodec(char * buffer, size_t buflen, int n) {
  BufferCursor cursor = {.p = buffer, .remaining = buflen};
  unsigned u;
  if(n < 0) {
    buffwrite(&cursor, '-');
    u = -n;
  }
  else
    u = n;
  utoa(&cursor, u, 10);
  return buffer;
}

canonical_original_code:
char *itodec(char *buffer, unsigned long buflen, int n) {
  struct BufferCursor cursor = {.p = buffer, .remaining = buflen};
  unsigned int u;
  if(n < 0) {
    buffwrite(&cursor, '-');
    u = -n;
  }
  else
    u = n;
  utoa(&cursor, u, 10);
  return buffer;
}

variable_types: n int

variable_types:
u
unsigned int


dectou

decompiled_code:
__int64 __fastcall dectou(char *a1, char **a2)
{
char v2; // al
unsigned int v3; // edx

v2 = *a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
{
v3 = 0;
}
else
{
v3 = 0;
do
{
++a1;
v3 = v2 + 10 * v3 - 48;
v2 = *a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
}
*a2 = a1;
return v3;
}

original_code:
unsigned dectou(const char *buffer, const char **endptr) {
  const char * p = buffer;
  unsigned n = 0;
  for(;;) {
    char c = *p;
    if(c < '0' || c > '9')
      break;
    p++;
    n = 10 * n + (c - '0');
  }
  *endptr = p;
  return n;
}

canonical_original_code:
unsigned int dectou(char *buffer, char **endptr) {
  char *p = buffer;
  unsigned int n = 0;
  for(;;) {
    char c = *p;
    if(c < '0' || c > '9')
      break;
    p++;
    n = 10 * n + (c - '0');
  }
  *endptr = p;
  return n;
}

variable_types:
endptr
char **

variable_types:
p
char *


decompiled_code:
__int64 __fastcall page_table(int a1)
{
return 4LL * (a1 << 10) + 4290772992LL;
}

original_code:
static uint32_t *page_table(int n) {
  return ((uint32_t *) ((VirtualAddress) 0xffc00000)) + (0x400 * n);
}

canonical_original_code:
static unsigned int *page_table(int n) {
  return ((unsigned int *) ((void *) 0xffc00000)) + (0x400 * n);
}

return_type: unsigned int *


decompiled_code:
__int64 __fastcall page_directory_index(unsigned int a1)
{
return a1 >> 22;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return a1 >> 22;
}

original_code:
inline unsigned page_directory_index(VirtualAddress addr) {
  return (unsigned) addr >> 22;
}

canonical_original_code:
inline unsigned int page_directory_index(void *addr) {
  return (unsigned int) addr >> 22;
}

variable_types:
addr
void *


decompiled_code:
__int64 __fastcall page_table_index(unsigned int a1)
{
return (a1 >> 12) & 0x3FF;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
return (a1 >> 12) & 0x3FF;
}

original_code:
inline unsigned page_table_index(VirtualAddress addr) {
  return (unsigned) addr >> 12 & 0x03FF;
}

canonical_original_code:
inline unsigned int page_table_index(void *addr) {
  return (unsigned int) addr >> 12 & 0x03FF;
}


decompiled_code:
__int64 __fastcall page_offset(__int16 a1)
{
return a1 & 0xFFF;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1)
{
return a1 & 0xFFF;
}

original_code:
inline unsigned page_offset(VirtualAddress addr) {
  return (unsigned) addr & 0xfff;
}

canonical_original_code:
inline unsigned int page_offset(void *addr) {
  return (unsigned int) addr & 0xfff;
}


decompiled_code:
__int64 __fastcall get_physaddr(__int16 a1)
{
unsigned int v1; // er12
unsigned int v2; // ebp
int v3; // ebp
__int64 result; // rax

v1 = page_directory_index(a1);
v2 = page_table_index(a1);
if ( (*(_BYTE *)(4LL * v1 + 0xFFFFF000LL) & 1) == 0 )
return 0LL;
v3 = *(_DWORD *)(page_table(v1) + 4LL * v2);
if ( (v3 & 1) != 0 )
result = (v3 & 0xFFFFF000) + (unsigned int)page_offset(a1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1)
{
unsigned int v1; // er12
unsigned int v2; // ebp
int v3; // ebp
__int64 result; // rax

v1 = func1(a1);
v2 = func2(a1);
if ( (*(_BYTE *)(4LL * v1 + 0xFFFFF000LL) & 1) == 0 )
return 0LL;
v3 = *(_DWORD *)(func0(v1) + 4LL * v2);
if ( (v3 & 1) != 0 )
result = (v3 & 0xFFFFF000) + (unsigned int)func3(a1);
else
result = 0LL;
return result;
}

original_code:
PhysicalAddress get_physaddr(VirtualAddress virt_addr)
{
  unsigned pdindex = page_directory_index(virt_addr);
  unsigned ptindex = page_table_index(virt_addr);
  unsigned offset = page_offset(virt_addr);
  unsigned * pd = ((VirtualAddress) 0xfffff000);
  if(!(pd[pdindex] & (1 << 0)))
     return 0;
  uint32_t * pt = page_table(pdindex);
  if(!(pt[ptindex] & (1 << 0)))
     return 0;
  uint32_t page_address = pt[ptindex] & 0xfffff000;
  return (void *) (page_address + offset);
}

canonical_original_code:
void *get_physaddr(void *virt_addr)
{
  unsigned int pdindex = page_directory_index(virt_addr);
  unsigned int ptindex = page_table_index(virt_addr);
  unsigned int offset = page_offset(virt_addr);
  unsigned int *pd = ((VirtualAddress) 0xfffff000);
  if(!(pd[pdindex] & (1 << 0)))
     return 0;
  unsigned int *pt = page_table(pdindex);
  if(!(pt[ptindex] & (1 << 0)))
     return 0;
  unsigned int page_address = pt[ptindex] & 0xfffff000;
  return (void *) (page_address + offset);
}

variable_types:
pdindex
unsigned int

variable_types:
ptindex
unsigned int

variable_types:
offset
unsigned int

variable_types:
pd
unsigned int *

variable_types:
pt
unsigned int *


decompiled_code:
__int64 __fastcall page_table_phys_addr(unsigned int a1)
{
__int16 v1; // ax

if ( a1 > 0x3FF )
return 0LL;
v1 = page_table(a1);
return get_physaddr(v1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
__int16 v1; // ax

if ( a1 > 0x3FF )
return 0LL;
v1 = func0(a1);
return func4(v1);
}

original_code:
PhysicalAddress page_table_phys_addr(int n) {
  if (n < 0 || n >= 0x400)
    return 0;
  return get_physaddr(page_table(n));
}

canonical_original_code:
void *page_table_phys_addr(int n) {
  if (n < 0 || n >= 0x400)
    return 0;
  return get_physaddr(page_table(n));
}


decompiled_code:
bool __fastcall valid_block_number(unsigned int a1)
{
return a1 <= 0xFFFFF;
}

canonical_decompiled_code:
bool __fastcall func0(unsigned int a1)
{
return a1 <= 0xFFFFF;
}

original_code:
static _Bool valid_block_number(int block_number) {
  return block_number >= 0 && block_number < 0x00100000;
}


decompiled_code:
__int64 __fastcall pmmap_index(int a1)
{
return (unsigned int)(a1 / 32);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return (unsigned int)(a1 / 32);
}

original_code:
static int pmmap_index(int block_number) {
  return block_number / 32;
}


decompiled_code:
__int64 __fastcall pmmap_bitnum(int a1)
{
return (unsigned int)(a1 % 32);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return (unsigned int)(a1 % 32);
}

original_code:
static int pmmap_bitnum(int block_number) {
  return block_number % 32;
}


decompiled_code:
char __fastcall pmmap_set(unsigned int a1)
{
int v1; // eax
char v2; // bp

LOBYTE(v1) = valid_block_number(a1);
if ( (_BYTE)v1 )
{
v2 = pmmap_bitnum(a1);
v1 = pmmap_index(a1);
pmmap[v1] |= 1 << v2;
}
return v1;
}

canonical_decompiled_code:
char __fastcall func3(unsigned int a1)
{
int v1; // eax
char v2; // bp

LOBYTE(v1) = func0(a1);
if ( (_BYTE)v1 )
{
v2 = func2(a1);
v1 = func1(a1);
pmmap[v1] |= 1 << v2;
}
return v1;
}

original_code:
static void pmmap_set(int block_number) {
  if(!valid_block_number(block_number))
    return;
  pmmap[pmmap_index(block_number)] |= (1 << pmmap_bitnum(block_number));
}


decompiled_code:
char __fastcall pmmap_reset(unsigned int a1)
{
int v1; // eax
char v2; // bp

LOBYTE(v1) = valid_block_number(a1);
if ( (_BYTE)v1 )
{
v2 = pmmap_bitnum(a1);
v1 = pmmap_index(a1);
pmmap[v1] &= __ROL4__(-2, v2);
}
return v1;
}

canonical_decompiled_code:
char __fastcall func4(unsigned int a1)
{
int v1; // eax
char v2; // bp

LOBYTE(v1) = func0(a1);
if ( (_BYTE)v1 )
{
v2 = func2(a1);
v1 = func1(a1);
pmmap[v1] &= __ROL4__(-2, v2);
}
return v1;
}

original_code:
static void pmmap_reset(int block_number) {
  if(!valid_block_number(block_number))
    return;
  pmmap[pmmap_index(block_number)] &= ~(1 << pmmap_bitnum(block_number));
}


decompiled_code:
char __fastcall pmmap_mark_block(unsigned int a1, int a2)
{
char result; // al

if ( !a2 )
return pmmap_reset(a1);
if ( a2 == 1 )
result = pmmap_set(a1);
return result;
}

canonical_decompiled_code:
char __fastcall func5(unsigned int a1, int a2)
{
char result; // al

if ( !a2 )
return func4(a1);
if ( a2 == 1 )
result = func3(a1);
return result;
}

original_code:
static void pmmap_mark_block(int block_number, BlockStatus block_status) {
  switch(block_status) {
  case BLOCK_AVAIL:
    pmmap_reset(block_number);
    break;
  case BLOCK_UNAVAIL:
    pmmap_set(block_number);
    break;
  }
}

canonical_original_code:
static void pmmap_mark_block(int block_number, enum BlockStatus block_status) {
  switch(block_status) {
  case BLOCK_AVAIL:
    pmmap_reset(block_number);
    break;
  case BLOCK_UNAVAIL:
    pmmap_set(block_number);
    break;
  }
}

variable_types: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
_BOOL8 __fastcall pmmap_get_block_status(unsigned int a1)
{
bool v1; // dl
_BOOL8 result; // rax
int v3; // ebp

v1 = valid_block_number(a1);
result = 1LL;
if ( v1 )
{
v3 = pmmap_index(a1);
result = (pmmap[v3] & (1 << pmmap_bitnum(a1))) != 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(unsigned int a1)
{
bool v1; // dl
_BOOL8 result; // rax
int v3; // ebp

v1 = func0(a1);
result = 1LL;
if ( v1 )
{
v3 = func1(a1);
result = (pmmap[v3] & (1 << func2(a1))) != 0;
}
return result;
}

original_code:
static BlockStatus pmmap_get_block_status(int block_number) {
  if(!valid_block_number(block_number))
    return 1;
  if (pmmap[pmmap_index(block_number)] & (1 << pmmap_bitnum(block_number)))
    return BLOCK_UNAVAIL;
  else
    return BLOCK_AVAIL;
}

canonical_original_code:
static enum BlockStatus pmmap_get_block_status(int block_number) {
  if(!valid_block_number(block_number))
    return 1;
  if (pmmap[pmmap_index(block_number)] & (1 << pmmap_bitnum(block_number)))
    return BLOCK_UNAVAIL;
  else
    return BLOCK_AVAIL;
}

return_type: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
__int64 __fastcall pmmap_run_length(unsigned int a1)
{
bool v1; // dl
__int64 result; // rax
_BOOL4 v3; // ebp
unsigned int v4; // ebx

v1 = valid_block_number(a1);
result = 0LL;
if ( v1 )
{
v3 = pmmap_get_block_status(a1);
v4 = a1 + 1;
if ( (int)(a1 + 1) <= 0xFFFFF )
{
do
{
if ( pmmap_get_block_status(v4) != v3 )
break;
++v4;
}
while ( v4 != 0x100000 );
}
result = v4 - a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
bool v1; // dl
__int64 result; // rax
_BOOL4 v3; // ebp
unsigned int v4; // ebx

v1 = func0(a1);
result = 0LL;
if ( v1 )
{
v3 = func6(a1);
v4 = a1 + 1;
if ( (int)(a1 + 1) <= 0xFFFFF )
{
do
{
if ( func6(v4) != v3 )
break;
++v4;
}
while ( v4 != 0x100000 );
}
result = v4 - a1;
}
return result;
}

original_code:
static int pmmap_run_length(int first_block_number) {
  if(!valid_block_number(first_block_number))
    return 0;
  BlockStatus first_status = pmmap_get_block_status(first_block_number);
  int next_block_number = first_block_number + 1;
  while(next_block_number < 0x00100000 &&
        pmmap_get_block_status(next_block_number) == first_status)
    next_block_number++;
  return next_block_number - first_block_number;
}

canonical_original_code:
static int pmmap_run_length(int first_block_number) {
  if(!valid_block_number(first_block_number))
    return 0;
  enum BlockStatus first_status = pmmap_get_block_status(first_block_number);
  int next_block_number = first_block_number + 1;
  while(next_block_number < 0x00100000 &&
        pmmap_get_block_status(next_block_number) == first_status)
    next_block_number++;
  return next_block_number - first_block_number;
}

user_defined_types: enum BlockStatus { BLOCK_AVAIL=0, BLOCK_UNAVAIL=1 }


decompiled_code:
__int64 __fastcall pmmap_addr(int a1)
{
return a1 << 12;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
return a1 << 12;
}

original_code:
static inline PhysicalAddress pmmap_addr(int block_number) {
  return (PhysicalAddress) (0x1000 * block_number);
}

canonical_original_code:
static inline void *pmmap_addr(int block_number) {
  return (PhysicalAddress) (0x1000 * block_number);
}


decompiled_code:
__int64 __fastcall pmmap_bytes(int a1)
{
return (unsigned int)(a1 << 12);
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
return (unsigned int)(a1 << 12);
}

original_code:
static unsigned pmmap_bytes(int num_blocks) {
  return 0x1000 * num_blocks;
}

canonical_original_code:
static unsigned int pmmap_bytes(int num_blocks) {
  return 0x1000 * num_blocks;
}


decompiled_code:
const char *__fastcall pmmap_status_str(int a1)
{
const char *result; // rax

result = "AVAIL";
if ( a1 )
{
result = (const char *)&unk_202E0;
if ( a1 == 1 )
result = "UNAVAIL";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func10(int a1)
{
const char *result; // rax

result = "AVAIL";
if ( a1 )
{
result = (const char *)&unk_202E0;
if ( a1 == 1 )
result = "UNAVAIL";
}
return result;
}

original_code:
static const char * pmmap_status_str(BlockStatus status) {
  switch(status) {
  case BLOCK_AVAIL:
    return "AVAIL";
  case BLOCK_UNAVAIL:
    return "UNAVAIL";
  default:
    return "?";
  }
}

canonical_original_code:
static char *pmmap_status_str(enum BlockStatus status) {
  switch(status) {
  case BLOCK_AVAIL:
    return "AVAIL";
  case BLOCK_UNAVAIL:
    return "UNAVAIL";
  default:
    return "?";
  }
}


decompiled_code:
void *__fastcall pmmap_memset(unsigned __int8 a1)
{
return memset(pmmap, a1, (size_t)&unk_20000);
}

canonical_decompiled_code:
void *__fastcall func11(unsigned __int8 a1)
{
return memset(pmmap, a1, (size_t)&unk_20000);
}

original_code:
static void pmmap_memset(unsigned char value) {
  memset(pmmap, value, sizeof(pmmap));
}

variable_types:
value
unsigned char


decompiled_code:
void *pmmap_mark_all_unused()
{
return pmmap_memset(0);
}

canonical_decompiled_code:
void *func12()
{
return func11(0);
}

original_code:
static void pmmap_mark_all_unused() {
  pmmap_memset(0);
}


decompiled_code:
void *pmmap_mark_all_used()
{
return pmmap_memset(0xFFu);
}

canonical_decompiled_code:
void *func13()
{
return func11(0xFFu);
}

original_code:
void pmmap_mark_all_used() {
  pmmap_memset(0xff);
}


decompiled_code:
unsigned __int64 __fastcall pmmap_mark_region(unsigned int a1, int a2, int a3)
{
int v4; // ebx
unsigned __int64 result; // rax

v4 = a1 >> 12;
do
{
pmmap_mark_block(v4++, a3);
result = pmmap_addr(v4);
}
while ( result < a2 + a1 );
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func14(unsigned int a1, int a2, int a3)
{
int v4; // ebx
unsigned __int64 result; // rax

v4 = a1 >> 12;
do
{
func5(v4++, a3);
result = func8(v4);
}
while ( result < a2 + a1 );
return result;
}

original_code:
void pmmap_mark_region(PhysicalAddress addr,
                       unsigned length,
                       BlockStatus status) {
  uint32_t addr_value = (uint32_t) addr;
  int block_number = addr_value / 0x1000;
  do {
    pmmap_mark_block(block_number, status);
    block_number++;
  } while(pmmap_addr(block_number) < (PhysicalAddress) (addr_value + length));
}

canonical_original_code:
void pmmap_mark_region(void *addr, unsigned int length, enum BlockStatus status) {
  unsigned int addr_value = (uint32_t) addr;
  int block_number = addr_value / 0x1000;
  do {
    pmmap_mark_block(block_number, status);
    block_number++;
  } while(pmmap_addr(block_number) < (PhysicalAddress) (addr_value + length));
}

variable_types:
length
unsigned int


decompiled_code:
void *pmmap_init()
{
return pmmap_mark_all_unused();
}

canonical_decompiled_code:
void *func15()
{
return func12();
}

original_code:
void pmmap_init() {
  pmmap_mark_all_unused();
}


decompiled_code:
__int64 pmmap_print()
{
int i; // ebx
unsigned int v1; // ebp
int v2; // eax
const char *v3; // r13
unsigned int v4; // er12
__int64 v5; // rax
__int64 result; // rax

vty_puts("Physical memory map:\n");
vty_puts(" BLK # BLOCKS ADDR BYTES STATUS\n");
for ( i = 0; i <= 0xFFFFF; i += v1 )
{
v1 = pmmap_run_length(i);
v2 = pmmap_get_block_status(i);
v3 = pmmap_status_str(v2);
v4 = pmmap_bytes(v1);
v5 = pmmap_addr(i);
result = vty_printf("%7d %7d %08x %08x %s\n", (unsigned int)i, v1, v5, v4, v3);
}
return result;
}

canonical_decompiled_code:
__int64 func16()
{
int i; // ebx
unsigned int v1; // ebp
int v2; // eax
const char *v3; // r13
unsigned int v4; // er12
__int64 v5; // rax
__int64 result; // rax

vty_puts("Physical memory map:\n");
vty_puts(" BLK # BLOCKS ADDR BYTES STATUS\n");
for ( i = 0; i <= 0xFFFFF; i += v1 )
{
v1 = func7(i);
v2 = func6(i);
v3 = func10(v2);
v4 = func9(v1);
v5 = func8(i);
result = vty_printf("%7d %7d %08x %08x %s\n", (unsigned int)i, v1, v5, v4, v3);
}
return result;
}

original_code:
void pmmap_print() {
  vty_puts("Physical memory map:\n");
  vty_puts("  BLK #   BLOCKS      ADDR     BYTES  STATUS\n");
  int block_number = 0;
  while(block_number < 0x00100000) {
    int run_length = pmmap_run_length(block_number);
    vty_printf("%7d  %7d  %08x  %08x  %s\n", block_number,
               run_length,
               pmmap_addr(block_number),
               pmmap_bytes(run_length),
               pmmap_status_str(pmmap_get_block_status(block_number)));
    block_number += run_length;
  }
}