// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpl $0x9, -0x8(%rbp)
je 0x1420
movl -0x8(%rbp), %esi
leaq 0x1bfa(%rip), %rdi      # 0x3004
movb $0x0, %al
callq 0x10b0
callq 0x1140
movl $0x1, %edi
callq 0x1270
movl $0x1, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1528
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1c66(%rip), %rsi      # 0x30ac
callq 0x1130
cmpl $0x0, %eax
jne 0x146e
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1240
movl %eax, -0x14(%rbp)
jmp 0x151a
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1c2e(%rip), %rsi      # 0x30af
callq 0x1130
cmpl $0x0, %eax
jne 0x14a6
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1240
movl %eax, -0x18(%rbp)
jmp 0x151a
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1bf9(%rip), %rsi      # 0x30b2
callq 0x1130
cmpl $0x0, %eax
jne 0x14de
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1240
movl %eax, -0x1c(%rbp)
jmp 0x151a
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1bc4(%rip), %rsi      # 0x30b5
callq 0x1130
cmpl $0x0, %eax
jne 0x1512
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x151a
jmp 0x1514
jmp 0x1516
jmp 0x1518
jmp 0x151a
movl -0x20(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x20(%rbp)
jmp 0x1427
cmpq $0x0, -0x28(%rbp)
je 0x1541
cmpl $0x0, -0x14(%rbp)
je 0x1541
cmpl $0x0, -0x18(%rbp)
je 0x1541
cmpl $0x0, -0x1c(%rbp)
jne 0x155e
leaq 0x1b70(%rip), %rdi      # 0x30b8
movb $0x0, %al
callq 0x10b0
callq 0x1140
movl $0x1, %edi
callq 0x1270
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
leaq 0x1bb0(%rip), %rdi      # 0x313c
movb $0x0, %al
callq 0x10b0
leaq 0x3be6(%rip), %rdi      # 0x5180
callq 0x16e0
leaq 0x3c02(%rip), %rdi      # 0x51a8
xorl %eax, %eax
movl %eax, %esi
callq 0x1290
leaq 0x3c1a(%rip), %rdi      # 0x51d0
xorl %eax, %eax
movl %eax, %esi
callq 0x1290
leaq 0x3c32(%rip), %rdi      # 0x51f8
xorl %eax, %eax
movl %eax, %esi
callq 0x1170
leaq 0x3c52(%rip), %rdi      # 0x5228
xorl %eax, %eax
movl %eax, %esi
callq 0x1170
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x242(%rip), %rdx       # 0x1830
leaq -0x14(%rbp), %rcx
callq 0x11c0
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x9aa(%rip), %rdx       # 0x1fb0
leaq -0x18(%rbp), %rcx
callq 0x11c0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x164b
movq -0x50(%rbp), %rdi
movslq -0x20(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x348(%rip), %rdx       # 0x1980
movq %rcx, %rsi
callq 0x11c0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1616
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1280
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1280
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1694
movq -0x50(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1280
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x166c
leaq 0x3b5d(%rip), %rdi      # 0x51f8
callq 0x11e0
leaq 0x3b81(%rip), %rdi      # 0x5228
callq 0x11e0
leaq 0x3af5(%rip), %rdi      # 0x51a8
callq 0x10d0
leaq 0x3b11(%rip), %rdi      # 0x51d0
callq 0x10d0
movl $0x0, -0x4(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)