// File: /workspace/github_c_makefile_star2_O3/devkot[P]SearchEngine/main.c
// Function: main
int main(int argc, char **argv) {
    int serving_port = 0, command_port = 0, numThreads = 0, i = 0;
    char *rootDir = NULL;

    // Check there are 5 arguments in total
    if (argc != 9){
        printf("Wrong number of arguments. Expected (9). Actual (%d)\n"
               "Correct Usage: ./myhttpd -p serving_port -c command_port -t num_of_threads -d root_dir\n"
               "Press <ENTER> to continue.\n", argc);
        getchar();
        exit(EXIT_FAILURE);
    }

    // Get arguments
    for (i=1;i<argc;i+=2){                      // iterate all flag (odd) arguments
        if (!strcmp(argv[i],"-p")){             // if this is "-p" flag
            serving_port = atoi(argv[i+1]);     // next arg is serving port
            continue;
        }
        else if (!strcmp(argv[i],"-c")){        // if this is "-d" flag
            command_port = atoi(argv[i+1]);     // next arg is command port
            continue;
        }
        else if (!strcmp(argv[i], "-t")){       // if this is "-t" flag
            numThreads = atoi(argv[i+1]);       // next arg is num of threads
            continue;
        }
        else if (!strcmp(argv[i], "-d")){       // if this is "-d" flag
            rootDir = (argv[i+1]);              // next arg is root dir
            continue;
        }
    }

    // Check they are all ok
    if (!rootDir || !serving_port || !command_port || !numThreads ){
        printf("Wrong arguments.\n"
               "Correct Usage: ./myhttpd -p serving_port -c command_port -t num_of_threads -d root_dir\n"
               "Press <ENTER> to continue.\n");
        getchar();
        exit(EXIT_FAILURE);
    }

    pthread_t cons[numThreads], prod, comm;                     // declare threads

    printf("Server is booting up...\n");

    initialize(&pool);                                          // Initialize pool and mutexes
    pthread_mutex_init(&mtx, 0);
    pthread_mutex_init(&statistics, 0);
    pthread_cond_init(&cond_nonempty, 0);
    pthread_cond_init(&cond_nonfull, 0);

    pthread_create(&prod, 0, producer, (void *) &serving_port); // Args must be cast to void *
    pthread_create(&comm, 0, command, (void *) &command_port);
    for(i = 0; i < numThreads; i++)
        pthread_create(&cons[i], 0, consumer, 0);

    pthread_join(prod, 0);                                      // Join terminating threads
    pthread_join(comm, 0);
    for(i = 0; i < numThreads; i++)
        pthread_join(cons[i], 0);

    pthread_cond_destroy(&cond_nonempty);                       // Destroy conditionals and mutexes
    pthread_cond_destroy(&cond_nonfull);

    pthread_mutex_destroy(&mtx);
    pthread_mutex_destroy(&statistics);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %r14d, %r14d
movl %r14d, -0x34(%rbp)
movl %r14d, -0x30(%rbp)
cmpl $0x9, %edi
jne 0x162e
movq %rsi, %r15
movq $-0x1, %r13
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
xorl %ebx, %ebx
movq 0x10(%r15,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x14a9
cmpb $0x70, 0x1(%rax)
jne 0x144d
cmpb $0x0, 0x2(%rax)
je 0x1478
cmpb $0x63, 0x1(%rax)
jne 0x1459
cmpb $0x0, 0x2(%rax)
je 0x148a
cmpb $0x74, 0x1(%rax)
jne 0x1465
cmpb $0x0, 0x2(%rax)
je 0x149c
cmpb $0x64, 0x1(%rax)
jne 0x14a9
cmpb $0x0, 0x2(%rax)
jne 0x14a9
movq 0x18(%r15,%r13,8), %rbx
jmp 0x14a9
movq 0x18(%r15,%r13,8), %rdi
callq 0x1230
movl %eax, %r12d
movl %eax, -0x34(%rbp)
jmp 0x14a9
movq 0x18(%r15,%r13,8), %rdi
callq 0x1230
movl %eax, %r14d
movl %eax, -0x30(%rbp)
jmp 0x14a9
movq 0x18(%r15,%r13,8), %rdi
callq 0x1230
movl %eax, -0x2c(%rbp)
addq $0x2, %r13
cmpq $0x7, %r13
jb 0x1437
testq %rbx, %rbx
movl -0x2c(%rbp), %ebx
je 0x1640
testl %r12d, %r12d
je 0x1640
testl %r14d, %r14d
je 0x1640
testl %ebx, %ebx
je 0x1640
movq %rsp, %r14
movl %ebx, %r13d
movq %rsp, %r12
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0x1c34(%rip), %rdi      # 0x312f
callq 0x1060
leaq 0x3c79(%rip), %rdi      # 0x5180
callq 0x165c
leaq 0x3c95(%rip), %rdi      # 0x51a8
xorl %esi, %esi
callq 0x1280
leaq 0x3caf(%rip), %rdi      # 0x51d0
xorl %esi, %esi
callq 0x1280
leaq 0x3cc9(%rip), %rdi      # 0x51f8
xorl %esi, %esi
callq 0x1160
leaq 0x3ceb(%rip), %rdi      # 0x5228
xorl %esi, %esi
callq 0x1160
leaq 0x248(%rip), %rdx       # 0x1793
leaq -0x48(%rbp), %rdi
leaq -0x34(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
leaq 0x7c2(%rip), %rdx       # 0x1d23
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
testl %ebx, %ebx
jle 0x15d4
movq %r14, -0x50(%rbp)
leaq 0x344(%rip), %r14       # 0x18c3
movq %r13, %rbx
movq %r12, %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x11b0
addq $0x8, %r15
decq %rbx
jne 0x1585
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
cmpl $0x0, -0x2c(%rbp)
movq -0x50(%rbp), %r14
jle 0x15ea
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1270
incq %rbx
cmpq %rbx, %r13
jne 0x15bf
jmp 0x15ea
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
leaq 0x3c07(%rip), %rdi      # 0x51f8
callq 0x11d0
leaq 0x3c2b(%rip), %rdi      # 0x5228
callq 0x11d0
leaq 0x3b9f(%rip), %rdi      # 0x51a8
callq 0x10d0
leaq 0x3bbb(%rip), %rdi      # 0x51d0
callq 0x10d0
movq %r14, %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, %esi
leaq 0x19cd(%rip), %rdi      # 0x3004
xorl %eax, %eax
callq 0x10b0
jmp 0x164c
leaq 0x1a65(%rip), %rdi      # 0x30ac
callq 0x1060
callq 0x1130
movl $0x1, %edi
callq 0x1260
nop