// File: /workspace/github_c_makefile_star2_O0/hasindu2008[P]f5p/f5pd.c
// Function: main
int main(int argc, char* argv[]) {
    signal(SIGSEGV, sig_handler);

    //buffer for communication
    char buffer[BUFFER_SIZE];

    //create a listening socket on port
    int listenfd = TCP_server_init(PORT);

    while (1) {
        //accept a client connection
        int connectfd = TCP_server_accept_client(listenfd);

        //get message from client
        int received = recv_full_msg(connectfd, buffer, BUFFER_SIZE);

        //print the message
        buffer[received] = '\0'; //null character before printing the string
        INFO("Recieved %s.", buffer);

        //some hidden quit method
        if (strcmp(buffer, "quit!") == 0) {
            return 0;
        }

        //execute the script
        char command[PATH_MAX * 2];
        sprintf(command, "%s %s", SCRIPT, buffer);
        INFO("Command to be run %s.", command);
        int pid = system_async(command);
        int status = wait_async(pid);

        //Copy a string to buffer
        if (status == 0) { //successful execution
            strcpy(buffer, "done.");
        } else if (status == -1) { //terminated due to signal
            strcpy(buffer, "crashed.");
        } else if (status > 0) {
            sprintf(buffer, "failed with exit status %d.", status);
        }

        //send a message to the client
        send_full_msg(connectfd, buffer, strlen(buffer));

        //close down the client connection
        TCP_server_disconnect_client(connectfd);
    }

    //close the down the listening socket
    TCP_server_shutdown(listenfd);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3040, %rsp           # imm = 0x3040
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xb, %edi
leaq -0xe5(%rip), %rsi       # 0x1ed0
callq 0x1120
movl $0x4e36, %edi           # imm = 0x4E36
callq 0x1770
movl %eax, -0x1014(%rbp)
movl -0x1014(%rbp), %edi
callq 0x1950
movl %eax, -0x1018(%rbp)
movl -0x1018(%rbp), %edi
leaq -0x1010(%rbp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x1420
movl %eax, -0x101c(%rbp)
movslq -0x101c(%rbp), %rax
movb $0x0, -0x1010(%rbp,%rax)
movq 0x2fea(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq -0x1010(%rbp), %rcx
leaq 0x14f8(%rip), %rsi      # 0x3517
leaq 0x1515(%rip), %rdx      # 0x353b
movb $0x0, %al
callq 0x1130
leaq -0x1010(%rbp), %rdi
leaq 0x1505(%rip), %rsi      # 0x3540
callq 0x1110
cmpl $0x0, %eax
jne 0x2050
xorl %eax, %eax
addq $0x3040, %rsp           # imm = 0x3040
popq %rbp
retq
leaq -0x3020(%rbp), %rdi
leaq -0x1010(%rbp), %rcx
leaq 0x14e1(%rip), %rsi      # 0x3546
leaq 0x14e0(%rip), %rdx      # 0x354c
movb $0x0, %al
callq 0x11b0
movq 0x2f7e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq -0x3020(%rbp), %rcx
leaq 0x14e1(%rip), %rsi      # 0x356c
leaq 0x14a9(%rip), %rdx      # 0x353b
movb $0x0, %al
callq 0x1130
leaq -0x3020(%rbp), %rdi
callq 0x2170
movl %eax, -0x3024(%rbp)
movl -0x3024(%rbp), %edi
callq 0x22d0
movl %eax, -0x3028(%rbp)
cmpl $0x0, -0x3028(%rbp)
jne 0x20da
leaq -0x1010(%rbp), %rdi
leaq 0x14c6(%rip), %rsi      # 0x3599
callq 0x1070
jmp 0x2120
cmpl $-0x1, -0x3028(%rbp)
jne 0x20f8
leaq -0x1010(%rbp), %rdi
leaq 0x14ae(%rip), %rsi      # 0x359f
callq 0x1070
jmp 0x211e
cmpl $0x0, -0x3028(%rbp)
jle 0x211c
leaq -0x1010(%rbp), %rdi
movl -0x3028(%rbp), %edx
leaq 0x1493(%rip), %rsi      # 0x35a8
movb $0x0, %al
callq 0x11b0
jmp 0x211e
jmp 0x2120
movl -0x1018(%rbp), %eax
movl %eax, -0x3034(%rbp)
leaq -0x1010(%rbp), %rax
movq %rax, -0x3030(%rbp)
leaq -0x1010(%rbp), %rdi
callq 0x10a0
movl -0x3034(%rbp), %edi
movq -0x3030(%rbp), %rsi
movq %rax, %rdx
callq 0x1330
movl -0x1018(%rbp), %edi
callq 0x19e0
jmp 0x1fca
nopl (%rax,%rax)