// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, -0x30(%rbp)
andl $0x0, -0x2c(%rbp)
cmpl $0x9, %edi
jne 0x15c8
movq %rsi, %r14
pushq $0x1
popq %r13
xorl %r15d, %r15d
xorl %ebx, %ebx
movq $0x0, -0x38(%rbp)
xorl %r12d, %r12d
cmpq $0x8, %r13
ja 0x1465
movq (%r14,%r13,8), %rax
cmpb $0x2d, (%rax)
jne 0x145f
cmpb $0x70, 0x1(%rax)
jne 0x1400
cmpb $0x0, 0x2(%rax)
je 0x142f
cmpb $0x63, 0x1(%rax)
jne 0x140c
cmpb $0x0, 0x2(%rax)
je 0x1440
cmpb $0x74, 0x1(%rax)
jne 0x1418
cmpb $0x0, 0x2(%rax)
je 0x1452
cmpb $0x64, 0x1(%rax)
jne 0x145f
cmpb $0x0, 0x2(%rax)
jne 0x145f
movq 0x8(%r14,%r13,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x145f
movq 0x8(%r14,%r13,8), %rdi
callq 0x1230
movl %eax, %ebx
movl %eax, -0x30(%rbp)
jmp 0x145f
movq 0x8(%r14,%r13,8), %rdi
callq 0x1230
movl %eax, %r15d
movl %eax, -0x2c(%rbp)
jmp 0x145f
movq 0x8(%r14,%r13,8), %rdi
callq 0x1230
movl %eax, %r12d
addq $0x2, %r13
jmp 0x13e5
cmpq $0x0, -0x38(%rbp)
je 0x15da
testl %ebx, %ebx
je 0x15da
testl %r15d, %r15d
je 0x15da
testl %r12d, %r12d
je 0x15da
movq %rsp, -0x38(%rbp)
movl %r12d, %eax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq 0xc82(%rip), %rdi       # 0x212f
callq 0x1060
leaq 0x2cc7(%rip), %rdi      # 0x4180
callq 0x15f4
leaq 0x2ce3(%rip), %rdi      # 0x41a8
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x1280
leaq 0x2cfa(%rip), %rdi      # 0x41d0
xorl %esi, %esi
callq 0x1280
leaq 0x2d14(%rip), %rdi      # 0x41f8
xorl %esi, %esi
callq 0x1160
leaq 0x2d36(%rip), %rdi      # 0x4228
xorl %esi, %esi
callq 0x1160
leaq 0x1df(%rip), %rdx       # 0x16df
leaq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
leaq 0x6b3(%rip), %rdx       # 0x1bc9
leaq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rcx
xorl %esi, %esi
callq 0x11b0
testl %r12d, %r12d
cmovlel %r14d, %r12d
leaq 0x2c0(%rip), %r14       # 0x17f3
movq %r13, %r15
movq %r12, %rbx
subq $0x1, %rbx
jb 0x1554
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x11b0
addq $0x8, %r15
jmp 0x1539
movq -0x48(%rbp), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x1270
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x1270
cmpq %r14, %r12
je 0x1583
movq (%r13,%r14,8), %rdi
xorl %esi, %esi
callq 0x1270
incq %r14
jmp 0x156d
leaq 0x2c6e(%rip), %rdi      # 0x41f8
callq 0x11d0
leaq 0x2c92(%rip), %rdi      # 0x4228
callq 0x11d0
leaq 0x2c06(%rip), %rdi      # 0x41a8
callq 0x10d0
leaq 0x2c22(%rip), %rdi      # 0x41d0
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 0xa33(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x10b0
jmp 0x15e6
leaq 0xacb(%rip), %rdi       # 0x20ac
callq 0x1060
callq 0x1130
pushq $0x1
popq %rdi
callq 0x1260
nop