// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3818, %rsp           # imm = 0x3818
leaq -0x9c(%rip), %rsi       # 0x1cfc
pushq $0xb
popq %rdi
callq 0x10f0
movl $0x4e36, %edi           # imm = 0x4E36
callq 0x1677
movl %eax, %r15d
leaq 0x810(%rsp), %r12
leaq 0x177f(%rip), %r14      # 0x353b
leaq 0x177d(%rip), %rbx      # 0x3540
pushq $0x6
popq %rbp
leaq 0x17f7(%rip), %r13      # 0x35c4
movl %eax, 0xc(%rsp)
movl %r15d, %edi
callq 0x1839
movl $0x1000, %edx           # imm = 0x1000
movl %eax, 0x8(%rsp)
movl %eax, %edi
movq %r12, %rsi
callq 0x13d8
cltq
movb $0x0, 0x810(%rsp,%rax)
movq 0x41fb(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1710(%rip), %rsi      # 0x3517
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1100
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1130
testl %eax, %eax
je 0x1fb0
movq %r12, %rcx
leaq 0x1810(%rsp), %r12
movq %r12, %rdi
leaq 0x1707(%rip), %rsi      # 0x3546
leaq 0x1706(%rip), %rdx      # 0x354c
xorl %eax, %eax
callq 0x1190
movq 0x41a4(%rip), %rax      # 0x5ff8
movq %rax, %rbx
movq (%rax), %rdi
leaq 0x170b(%rip), %rsi      # 0x356c
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1100
movq %r12, %rdi
movq %r13, %rsi
callq 0x1170
movq %rax, %r12
xorl %r15d, %r15d
cmpq $0x100, %r15            # imm = 0x100
je 0x1ebf
testq %r12, %r12
je 0x1eb9
movl $0x100, %edi            # imm = 0x100
callq 0x1110
movq %rax, 0x10(%rsp,%r15,8)
movq %rax, %rdi
movq %r12, %rsi
callq 0x1070
xorl %edi, %edi
movq %r13, %rsi
callq 0x1170
movq %rax, %r12
incq %r15
jmp 0x1e7f
andq $0x0, 0x10(%rsp,%r15,8)
callq 0x1200
testl %eax, %eax
js 0x1fc4
movl %eax, %r12d
jne 0x1ee9
movq 0x10(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x11f0
cmpl $-0x1, %eax
je 0x1fdb
andl $0x0, 0x10(%rsp)
movl %r12d, %edi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x1150
testl %eax, %eax
js 0x1fcd
movl 0x10(%rsp), %eax
movzbl %ah, %ecx
andl $0x7f, %eax
negl %eax
movl $0x0, %edx
sbbl %edx, %edx
orl %ecx, %edx
cmpl $-0x1, %edx
je 0x1f3e
testl %edx, %edx
movl 0x8(%rsp), %ebx
jne 0x1f5e
movw $0x2e, 0x814(%rsp)
movl $0x656e6f64, 0x810(%rsp) # imm = 0x656E6F64
jmp 0x1f74
movabsq $0x2e64656873617263, %rax # imm = 0x2E64656873617263
movq %rax, 0x810(%rsp)
movb $0x0, 0x818(%rsp)
movl 0x8(%rsp), %ebx
jmp 0x1f74
leaq 0x810(%rsp), %rdi
leaq 0x163b(%rip), %rsi      # 0x35a8
xorl %eax, %eax
callq 0x1190
leaq 0x810(%rsp), %r12
movq %r12, %rdi
callq 0x10a0
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x131c
movl %ebx, %edi
callq 0x18bd
leaq 0x159c(%rip), %r14      # 0x353b
leaq 0x159a(%rip), %rbx      # 0x3540
movl 0xc(%rsp), %r15d
jmp 0x1dd1
xorl %eax, %eax
addq $0x3818, %rsp           # imm = 0x3818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15fc(%rip), %rdi      # 0x35c7
jmp 0x1fd4
leaq 0x163b(%rip), %rdi      # 0x360f
callq 0x1160
jmp 0x2005
movq (%rbx), %rbx
callq 0x1060
movl (%rax), %edi
callq 0x11d0
leaq 0x15e2(%rip), %rsi      # 0x35d3
leaq 0x160a(%rip), %rdx      # 0x3602
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1100
pushq $0x1
popq %rdi
callq 0x11a0
nopl (%rax)