// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x17c(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x12b0
movl %eax, -0x174(%rbp)
cmpl $0x0, %eax
jge 0x1ff3
leaq 0x1172(%rip), %rdi      # 0x3160
callq 0x1960
xorl %edi, %edi
callq 0x10e0
movl %eax, -0x16c(%rbp)
movl -0x17c(%rbp), %eax
movzwl %ax, %edi
callq 0x10a0
movw %ax, -0x16e(%rbp)
movw $0x2, -0x170(%rbp)
movl -0x174(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x170(%rbp), %rcx
movl $0x10, %r8d
callq 0x1060
cmpl $-0x1, %eax
jne 0x205b
leaq 0x111b(%rip), %rdi      # 0x3167
callq 0x1200
movl $0x1, %edi
callq 0x1270
movl -0x174(%rbp), %edi
leaq -0x170(%rbp), %rsi
movl $0x10, %edx
callq 0x11b0
cmpl $0x0, %eax
je 0x2083
leaq 0x10f4(%rip), %rdi      # 0x3172
callq 0x1960
movl -0x174(%rbp), %edi
movl $0x5, %esi
callq 0x11a0
cmpl $0x0, %eax
je 0x20a4
leaq 0x10d8(%rip), %rdi      # 0x3177
callq 0x1960
movl -0x17c(%rbp), %esi
leaq 0x1387(%rip), %rdi      # 0x3438
movb $0x0, %al
callq 0x10b0
leaq 0x139a(%rip), %rdi      # 0x3459
movb $0x0, %al
callq 0x10b0
movl -0x174(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x1220
movl %eax, -0x178(%rbp)
cmpl $0x0, %eax
jge 0x20ef
leaq 0x10cd(%rip), %rdi      # 0x31b7
callq 0x1960
leaq -0x20(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x1090
movq -0x188(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x10f0
movl -0x178(%rbp), %edi
leaq -0x20(%rbp), %rsi
movl $0x14, %edx
callq 0x1110
cmpq $0x0, %rax
jge 0x213a
leaq 0x10a6(%rip), %rdi      # 0x31db
callq 0x1960
movb $0x0, -0xc(%rbp)
leaq -0x20(%rbp), %rdi
leaq 0x1325(%rip), %rsi      # 0x346e
callq 0x1210
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x1314(%rip), %rsi      # 0x3471
callq 0x1130
cmpl $0x0, %eax
jne 0x2209
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x10f0
leaq -0xf0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x160(%rbp), %rdi
movq 0x30c6(%rip), %rsi      # 0x5260
callq 0x22d0
movq -0x1a0(%rbp), %rdi
movq %rax, %rdx
leaq 0x2fcc(%rip), %rax      # 0x517c
movl (%rax), %ecx
leaq 0x2fbf(%rip), %rax      # 0x5178
movl (%rax), %r8d
leaq 0x12b4(%rip), %rsi      # 0x3477
movb $0x0, %al
callq 0x1260
movl -0x178(%rbp), %eax
movl %eax, -0x194(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x1090
movl -0x194(%rbp), %edi
movq -0x190(%rbp), %rsi
movl %eax, %edx
callq 0x1f30
jmp 0x22be
movq -0x28(%rbp), %rdi
leaq 0x1290(%rip), %rsi      # 0x34a4
callq 0x1130
cmpl $0x0, %eax
jne 0x227d
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x10f0
leaq -0xf0(%rbp), %rdi
leaq 0x126e(%rip), %rsi      # 0x34ad
movb $0x0, %al
callq 0x1260
movl -0x178(%rbp), %edi
leaq -0xf0(%rbp), %rsi
movl $0x19, %edx
callq 0x1f30
leaq 0x1262(%rip), %rdi      # 0x34c6
movb $0x0, %al
callq 0x10b0
movl -0x178(%rbp), %edi
callq 0x1100
xorl %edi, %edi
callq 0x1270
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x10f0
leaq -0xf0(%rbp), %rdi
leaq 0x1244(%rip), %rsi      # 0x34e2
movb $0x0, %al
callq 0x1260
movl -0x178(%rbp), %edi
leaq -0xf0(%rbp), %rsi
movl $0x11, %edx
callq 0x1f30
jmp 0x22be
jmp 0x20b8
nopw %cs:(%rax,%rax)