// File: /workspace/github_c_makefile_star2_O3/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 0x1d5b
leaq 0x13fa(%rip), %rdi      # 0x3150
jmp 0x1f44
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 0x1da0
leaq 0x13bc(%rip), %rdi      # 0x3157
jmp 0x1f44
leaq -0x38(%rbp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x11a0
testl %eax, %eax
jne 0x1f34
movl %ebx, %edi
movl $0x5, %esi
callq 0x1190
testl %eax, %eax
jne 0x1f3d
leaq 0x153f(%rip), %rdi      # 0x3312
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
leaq 0x162f(%rip), %rdi      # 0x3413
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
testl %eax, %eax
js 0x1f2b
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 0x1f53
movb $0x0, -0x3c(%rbp)
movq %r12, %rdi
leaq 0x14ed(%rip), %rsi      # 0x3333
callq 0x1200
movq %rax, %r15
movq %rax, %rdi
leaq 0x14de(%rip), %rsi      # 0x3336
callq 0x1120
testl %eax, %eax
je 0x1ea7
movq %r15, %rdi
leaq 0x14fe(%rip), %rsi      # 0x3369
callq 0x1120
testl %eax, %eax
je 0x1f5c
movl $0xb7, %edx
leaq -0x10f(%rbp), %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x14f9(%rip), %xmm0     # 0x338b
movaps %xmm0, -0x120(%rbp)
movb $0x0, -0x110(%rbp)
movl $0x11, %edx
jmp 0x1efe
movl $0xc8, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq 0x33a3(%rip), %rsi      # 0x5260
leaq -0x190(%rbp), %r15
movq %r15, %rdi
callq 0x1fb7
leaq 0x32a9(%rip), %rax      # 0x517c
movl (%rax), %ecx
leaq 0x329c(%rip), %rax      # 0x5178
movl (%rax), %r8d
movq %r13, %rdi
leaq 0x1453(%rip), %rsi      # 0x333c
movq %r15, %rdx
xorl %eax, %eax
callq 0x1250
movq %r13, %rdi
callq 0x10a0
movq %rax, %rdx
movl %r14d, %edi
movq %r13, %rsi
callq 0x1cdc
leaq 0x1503(%rip), %rdi      # 0x3413
callq 0x1060
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1210
movl %eax, %r14d
testl %eax, %eax
jns 0x1e0a
leaq 0x1275(%rip), %rdi      # 0x31a7
jmp 0x1f44
leaq 0x1227(%rip), %rdi      # 0x3162
jmp 0x1f44
leaq 0x1223(%rip), %rdi      # 0x3167
callq 0x11f0
movl $0x1, %edi
callq 0x1260
leaq 0x1254(%rip), %rdi      # 0x31ae
jmp 0x1f44
leaq -0x107(%rbp), %rbx
movl $0xaf, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
movups 0x13f9(%rip), %xmm0     # 0x3372
movaps %xmm0, -0x19(%rbx)
movups 0x13f7(%rip), %xmm0     # 0x337b
movups %xmm0, -0x10(%rbx)
leaq -0x120(%rbp), %rsi
movl %r14d, %edi
movl $0x19, %edx
callq 0x1cdc
leaq 0x1484(%rip), %rdi      # 0x3427
callq 0x1060
movl %r14d, %edi
callq 0x10f0
xorl %edi, %edi
callq 0x1260