// File: /workspace/github_c_makefile_star2_O1/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 %eax, %eax
movl %eax, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movl %eax, -0x2c(%rbp)
cmpl $0x9, %edi
jne 0x1639
movq %rsi, %r14
movq $-0x1, %r13
leaq 0x1c75(%rip), %r15      # 0x30ac
leaq 0x1c71(%rip), %r12      # 0x30af
movq $0x0, -0x38(%rbp)
movq 0x10(%r14,%r13,8), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1120
testl %eax, %eax
je 0x149a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1120
testl %eax, %eax
je 0x14a9
movq %rbx, %rdi
leaq 0x1c3f(%rip), %rsi      # 0x30b2
callq 0x1120
testl %eax, %eax
je 0x14b8
movq %rbx, %rdi
leaq 0x1c2f(%rip), %rsi      # 0x30b5
callq 0x1120
testl %eax, %eax
jne 0x14c6
movq 0x18(%r14,%r13,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x14c6
movq 0x18(%r14,%r13,8), %rdi
callq 0x1230
movl %eax, -0x30(%rbp)
jmp 0x14c6
movq 0x18(%r14,%r13,8), %rdi
callq 0x1230
movl %eax, -0x2c(%rbp)
jmp 0x14c6
movq 0x18(%r14,%r13,8), %rdi
callq 0x1230
movq %rax, -0x40(%rbp)
addq $0x2, %r13
cmpq $0x7, %r13
jb 0x1446
cmpq $0x0, -0x38(%rbp)
je 0x164b
cmpl $0x0, -0x30(%rbp)
je 0x164b
cmpl $0x0, -0x2c(%rbp)
je 0x164b
cmpl $0x0, -0x40(%rbp)
je 0x164b
movq %rsp, -0x38(%rbp)
movq -0x40(%rbp), %rbx
movl %ebx, %r15d
movq %rsp, %r13
leaq 0xf(,%r15,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq 0x1c17(%rip), %rdi      # 0x313b
callq 0x1060
leaq 0x3c50(%rip), %rdi      # 0x5180
callq 0x1668
leaq 0x3c6c(%rip), %rdi      # 0x51a8
xorl %esi, %esi
callq 0x1280
leaq 0x3c86(%rip), %rdi      # 0x51d0
xorl %esi, %esi
callq 0x1280
leaq 0x3ca0(%rip), %rdi      # 0x51f8
xorl %esi, %esi
callq 0x1160
leaq 0x3cc2(%rip), %rdi      # 0x5228
xorl %esi, %esi
callq 0x1160
leaq 0x22b(%rip), %rdx       # 0x179f
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
leaq 0x7a5(%rip), %rdx       # 0x1d2f
leaq -0x48(%rbp), %rdi
leaq -0x2c(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
testl %ebx, %ebx
jle 0x15c2
leaq 0x32b(%rip), %rbx       # 0x18cf
movq %r15, %r12
movq %r13, %r14
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x11b0
addq $0x8, %r14
decq %r12
jne 0x15aa
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
movq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
cmpl $0x0, -0x40(%rbp)
jle 0x15f4
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1270
incq %rbx
cmpq %rbx, %r15
jne 0x15e0
leaq 0x3bfd(%rip), %rdi      # 0x51f8
callq 0x11d0
leaq 0x3c21(%rip), %rdi      # 0x5228
callq 0x11d0
leaq 0x3b95(%rip), %rdi      # 0x51a8
callq 0x10d0
leaq 0x3bb1(%rip), %rdi      # 0x51d0
callq 0x10d0
movq -0x38(%rbp), %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 0x19c2(%rip), %rdi      # 0x3004
xorl %eax, %eax
callq 0x10b0
jmp 0x1657
leaq 0x1a66(%rip), %rdi      # 0x30b8
callq 0x1060
callq 0x1130
movl $0x1, %edi
callq 0x1260
nop