// File: /workspace/github_c_makefile_star2_O1/devkot[P]SearchEngine/functions.c
// Function: command
void *command(void *ptr){
    char telbuf[20], *command, stats[200], runTime[100];
    struct sockaddr_in myaddr;
    int lsock, csock, command_port;

    command_port = *((int *) ptr);

    if((lsock = socket( AF_INET, SOCK_STREAM, 0)) < 0)
        perror_exit("socket");

    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(command_port);
    myaddr.sin_family = AF_INET;

    // Resolve address already in use
    if (setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &myaddr, sizeof(myaddr)) == -1) {
        perror("setsockopt");
        exit(1);
    }

    if(bind(lsock, (struct sockaddr *)&myaddr, sizeof(myaddr)))
        perror_exit("bind");

    if(listen(lsock, 5) != 0)
        perror_exit("listen");

    printf("Waiting for commands on port %d\n", command_port);
    while(1){
        printf("Waiting for clients\n");
        if((csock = accept(lsock, NULL, NULL)) < 0)
            perror_exit("accept");

        bzero(telbuf, strlen(telbuf));
        if((read(csock, telbuf, 20)) < 0)
            perror_exit("Reading request");

        telbuf[20] = '\0';                      // Append terminating character

        command = strtok(telbuf, "\r\n");       // Remove chars appended by telnet

        if (!strcmp(command, "STATS")){
            bzero(stats, 200);
            sprintf(stats, "Server up for %s, served %d pages, %d bytes\n", getRunTime(runTime, start), pages, bytes);
            customWrite(csock, stats, strlen(stats));
        }
        else if (!strcmp(command, "SHUTDOWN")){
            bzero(stats, 200);
            sprintf(stats, "Server shutting down...\n");
            customWrite(csock, stats, 25);
            printf("Received shutdown command.\n");
            close(csock);
            exit(0);
        }
        else {
            bzero(stats, 200);
            sprintf(stats, "Invalid command\n");
            customWrite(csock, stats, 17);
        }

    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl (%rdi), %r14d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x12a0
testl %eax, %eax
jns 0x1d67
leaq 0x13fe(%rip), %rdi      # 0x3160
jmp 0x1f50
movl %eax, %ebx
leaq -0x38(%rbp), %rcx
movl $0x0, 0x4(%rcx)
movl %r14d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rcx)
movw $0x2, (%rcx)
movl %ebx, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x10, %r8d
callq 0x1070
cmpl $-0x1, %eax
jne 0x1dac
leaq 0x13c0(%rip), %rdi      # 0x3167
jmp 0x1f50
leaq -0x38(%rbp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x11a0
testl %eax, %eax
jne 0x1f40
movl %ebx, %edi
movl $0x5, %esi
callq 0x1190
testl %eax, %eax
jne 0x1f49
leaq 0x1543(%rip), %rdi      # 0x3322
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
leaq 0x1633(%rip), %rdi      # 0x3423
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
testl %eax, %eax
js 0x1f37
movl %eax, %r14d
leaq -0x50(%rbp), %r12
leaq -0x120(%rbp), %r13
movq %r12, %rdi
callq 0x10a0
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x10e0
movl $0x14, %edx
movl %r14d, %edi
movq %r12, %rsi
callq 0x1100
testq %rax, %rax
js 0x1f5f
movb $0x0, -0x3c(%rbp)
movq %r12, %rdi
leaq 0x14f1(%rip), %rsi      # 0x3343
callq 0x1200
movq %rax, %r15
movq %rax, %rdi
leaq 0x14e2(%rip), %rsi      # 0x3346
callq 0x1120
testl %eax, %eax
je 0x1eb3
movq %r15, %rdi
leaq 0x1502(%rip), %rsi      # 0x3379
callq 0x1120
testl %eax, %eax
je 0x1f68
movl $0xb7, %edx
leaq -0x10f(%rbp), %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x14fd(%rip), %xmm0     # 0x339b
movaps %xmm0, -0x120(%rbp)
movb $0x0, -0x110(%rbp)
movl $0x11, %edx
jmp 0x1f0a
movl $0xc8, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq 0x3397(%rip), %rsi      # 0x5260
leaq -0x190(%rbp), %r15
movq %r15, %rdi
callq 0x1fc3
leaq 0x329d(%rip), %rax      # 0x517c
movl (%rax), %ecx
leaq 0x3290(%rip), %rax      # 0x5178
movl (%rax), %r8d
movq %r13, %rdi
leaq 0x1457(%rip), %rsi      # 0x334c
movq %r15, %rdx
xorl %eax, %eax
callq 0x1250
movq %r13, %rdi
callq 0x10a0
movq %rax, %rdx
movl %r14d, %edi
movq %r13, %rsi
callq 0x1ce8
leaq 0x1507(%rip), %rdi      # 0x3423
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
movl %eax, %r14d
testl %eax, %eax
jns 0x1e16
leaq 0x1279(%rip), %rdi      # 0x31b7
jmp 0x1f50
leaq 0x122b(%rip), %rdi      # 0x3172
jmp 0x1f50
leaq 0x1227(%rip), %rdi      # 0x3177
callq 0x11f0
movl $0x1, %edi
callq 0x1260
leaq 0x1258(%rip), %rdi      # 0x31be
jmp 0x1f50
leaq -0x107(%rbp), %rbx
movl $0xaf, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x13fd(%rip), %xmm0     # 0x3382
movaps %xmm0, -0x19(%rbx)
movups 0x13fb(%rip), %xmm0     # 0x338b
movups %xmm0, -0x10(%rbx)
leaq -0x120(%rbp), %rsi
movl %r14d, %edi
movl $0x19, %edx
callq 0x1ce8
leaq 0x1488(%rip), %rdi      # 0x3437
callq 0x1060
movl %r14d, %edi
callq 0x10f0
xorl %edi, %edi
callq 0x1260