// File: /workspace/github_c_makefile_star2_O1/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 0xd38(%rip), %rsi       # 0x213c
pushq $0xb
popq %rdi
callq 0x10f0
movl $0x4e36, %edi           # imm = 0x4E36
callq 0x1b47
movl %eax, %r15d
leaq 0x810(%rsp), %r12
leaq 0x2113(%rip), %r14      # 0x353b
leaq 0x2111(%rip), %rbx      # 0x3540
pushq $0x6
popq %rbp
leaq 0x218b(%rip), %r13      # 0x35c4
movl %eax, 0xc(%rsp)
movl %r15d, %edi
callq 0x1cc7
movl $0x1000, %edx           # imm = 0x1000
movl %eax, 0x8(%rsp)
movl %eax, %edi
movq %r12, %rsi
callq 0x193e
cltq
movb $0x0, 0x810(%rsp,%rax)
movq 0x4b8f(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x20a4(%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 0x161e
movq %r12, %rcx
leaq 0x1810(%rsp), %r12
movq %r12, %rdi
leaq 0x209b(%rip), %rsi      # 0x3546
leaq 0x209a(%rip), %rdx      # 0x354c
xorl %eax, %eax
callq 0x1190
movq 0x4b38(%rip), %rax      # 0x5ff8
movq %rax, %rbx
movq (%rax), %rdi
leaq 0x209f(%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
testq %r12, %r12
je 0x1525
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
cmpq $0x100, %r15            # imm = 0x100
jne 0x14eb
jmp 0x152b
andq $0x0, 0x10(%rsp,%r15,8)
callq 0x1200
testl %eax, %eax
js 0x1632
movl %eax, %r12d
jne 0x1555
movq 0x10(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x11f0
cmpl $-0x1, %eax
je 0x1649
andl $0x0, 0x10(%rsp)
movl %r12d, %edi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x1150
testl %eax, %eax
js 0x163b
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 0x15aa
testl %edx, %edx
movl 0x8(%rsp), %ebx
jne 0x15ca
movw $0x2e, 0x814(%rsp)
movl $0x656e6f64, 0x810(%rsp) # imm = 0x656E6F64
jmp 0x15e2
movabsq $0x2e64656873617263, %rax # imm = 0x2E64656873617263
movq %rax, 0x810(%rsp)
movb $0x0, 0x818(%rsp)
movl 0x8(%rsp), %ebx
jmp 0x15e2
jle 0x15e2
leaq 0x810(%rsp), %rdi
leaq 0x1fcd(%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 0x18ac
movl %ebx, %edi
callq 0x1d4d
leaq 0x1f2e(%rip), %r14      # 0x353b
leaq 0x1f2c(%rip), %rbx      # 0x3540
movl 0xc(%rsp), %r15d
jmp 0x143d
xorl %eax, %eax
addq $0x3818, %rsp           # imm = 0x3818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f8e(%rip), %rdi      # 0x35c7
jmp 0x1642
leaq 0x1fcd(%rip), %rdi      # 0x360f
callq 0x1160
jmp 0x1673
movq (%rbx), %rbx
callq 0x1060
movl (%rax), %edi
callq 0x11d0
leaq 0x1f74(%rip), %rsi      # 0x35d3
leaq 0x1f9c(%rip), %rdx      # 0x3602
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1100
pushq $0x1
popq %rdi
callq 0x11a0