// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x12a0
testl %eax, %eax
jns 0x1bfd
leaq 0x558(%rip), %rdi       # 0x2150
callq 0x17e2
movl %eax, %ebx
leaq -0x38(%rbp), %rcx
andl $0x0, 0x4(%rcx)
movl %r14d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rcx)
movw $0x2, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x10
popq %r8
movl %ebx, %edi
callq 0x1070
cmpl $-0x1, %eax
jne 0x1c41
leaq 0x523(%rip), %rdi       # 0x2157
callq 0x11f0
pushq $0x1
popq %rdi
callq 0x1260
leaq -0x38(%rbp), %rsi
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x11a0
testl %eax, %eax
jne 0x1da9
pushq $0x5
popq %rsi
movl %ebx, %edi
callq 0x1190
testl %eax, %eax
jne 0x1db5
leaq 0x6a2(%rip), %rdi       # 0x2312
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
leaq -0x50(%rbp), %r12
leaq -0x120(%rbp), %r15
leaq 0x787(%rip), %rdi       # 0x2413
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
testl %eax, %eax
js 0x1dc1
movl %eax, %r14d
movq %r12, %rdi
callq 0x10a0
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x10e0
movl %r14d, %edi
movq %r12, %rsi
pushq $0x14
popq %rdx
callq 0x1100
testq %rax, %rax
js 0x1dcd
movb $0x0, -0x3c(%rbp)
movq %r12, %rdi
leaq 0x652(%rip), %rsi       # 0x2333
callq 0x1200
movq %rax, %r13
movq %rax, %rdi
leaq 0x643(%rip), %rsi       # 0x2336
callq 0x1120
testl %eax, %eax
je 0x1d42
movq %r13, %rdi
leaq 0x663(%rip), %rsi       # 0x2369
callq 0x1120
testl %eax, %eax
je 0x1dd9
movl $0xb7, %edx
leaq -0x10f(%rbp), %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x65e(%rip), %xmm0      # 0x238b
movaps %xmm0, -0x120(%rbp)
movb $0x0, -0x110(%rbp)
pushq $0x11
popq %rax
movl %eax, %edx
jmp 0x1d99
movl $0xc8, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x10e0
movq 0x2508(%rip), %rsi      # 0x4260
leaq -0x190(%rbp), %r13
movq %r13, %rdi
callq 0x1e32
leaq 0x240e(%rip), %rax      # 0x417c
movl (%rax), %ecx
leaq 0x2401(%rip), %rax      # 0x4178
movl (%rax), %r8d
movq %r15, %rdi
leaq 0x5b8(%rip), %rsi       # 0x233c
movq %r13, %rdx
xorl %eax, %eax
callq 0x1250
movq %r15, %rdi
callq 0x10a0
movq %rax, %rdx
movl %r14d, %edi
movq %r15, %rsi
callq 0x1b7d
jmp 0x1c85
leaq 0x3b2(%rip), %rdi       # 0x2162
callq 0x17e2
leaq 0x3ab(%rip), %rdi       # 0x2167
callq 0x17e2
leaq 0x3df(%rip), %rdi       # 0x21a7
callq 0x17e2
leaq 0x3da(%rip), %rdi       # 0x21ae
callq 0x17e2
leaq -0x107(%rbp), %rbx
movl $0xaf, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x57c(%rip), %xmm0      # 0x2372
movaps %xmm0, -0x19(%rbx)
movups 0x57a(%rip), %xmm0      # 0x237b
movups %xmm0, -0x10(%rbx)
leaq -0x120(%rbp), %rsi
pushq $0x19
popq %rdx
movl %r14d, %edi
callq 0x1b7d
leaq 0x609(%rip), %rdi       # 0x2427
callq 0x1060
movl %r14d, %edi
callq 0x10f0
xorl %edi, %edi
callq 0x1260