// File: /workspace/github_c_makefile_star2_O3/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 0xd5b(%rip), %rsi       # 0x2134
pushq $0xb
popq %rdi
callq 0x10f0
movl $0x4e36, %edi           # imm = 0x4E36
callq 0x1b3e
movl %eax, %r15d
leaq 0x810(%rsp), %r12
leaq 0x213e(%rip), %r14      # 0x353b
leaq 0x213c(%rip), %rbx      # 0x3540
pushq $0x6
popq %rbp
leaq 0x21b6(%rip), %r13      # 0x35c4
movl %eax, 0xc(%rsp)
movl %r15d, %edi
callq 0x1cbe
movl $0x1000, %edx           # imm = 0x1000
movl %eax, 0x8(%rsp)
movl %eax, %edi
movq %r12, %rsi
callq 0x190e
cltq
movb $0x0, 0x810(%rsp,%rax)
movq 0x4bba(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x20cf(%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 0x15f1
movq %r12, %rcx
leaq 0x1810(%rsp), %r12
movq %r12, %rdi
leaq 0x20c6(%rip), %rsi      # 0x3546
leaq 0x20c5(%rip), %rdx      # 0x354c
xorl %eax, %eax
callq 0x1190
movq 0x4b63(%rip), %rax      # 0x5ff8
movq %rax, %rbx
movq (%rax), %rdi
leaq 0x20ca(%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 0x14fa
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 0x14c0
jmp 0x1500
andq $0x0, 0x10(%rsp,%r15,8)
callq 0x1200
testl %eax, %eax
js 0x1605
movl %eax, %r12d
jne 0x152a
movq 0x10(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x11f0
cmpl $-0x1, %eax
je 0x161c
andl $0x0, 0x10(%rsp)
movl %r12d, %edi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x1150
testl %eax, %eax
js 0x160e
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 0x157f
testl %edx, %edx
movl 0x8(%rsp), %ebx
jne 0x159f
movw $0x2e, 0x814(%rsp)
movl $0x656e6f64, 0x810(%rsp) # imm = 0x656E6F64
jmp 0x15b5
movabsq $0x2e64656873617263, %rax # imm = 0x2E64656873617263
movq %rax, 0x810(%rsp)
movb $0x0, 0x818(%rsp)
movl 0x8(%rsp), %ebx
jmp 0x15b5
leaq 0x810(%rsp), %rdi
leaq 0x1ffa(%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 0x187c
movl %ebx, %edi
callq 0x1d44
leaq 0x1f5b(%rip), %r14      # 0x353b
leaq 0x1f59(%rip), %rbx      # 0x3540
movl 0xc(%rsp), %r15d
jmp 0x1412
xorl %eax, %eax
addq $0x3818, %rsp           # imm = 0x3818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fbb(%rip), %rdi      # 0x35c7
jmp 0x1615
leaq 0x1ffa(%rip), %rdi      # 0x360f
callq 0x1160
jmp 0x1646
movq (%rbx), %rbx
callq 0x1060
movl (%rax), %edi
callq 0x11d0
leaq 0x1fa1(%rip), %rsi      # 0x35d3
leaq 0x1fc9(%rip), %rdx      # 0x3602
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x1100
pushq $0x1
popq %rdi
callq 0x11a0