// File: /workspace/github_c_makefile_star2_O2/devkot[P]SearchEngine/Crawler/crawler.c
// Function: main
int main(int argc, char **argv) {
    int serving_port = 0, command_port = 0, numThreads = 0, i = 0;
    char *saveDir = NULL, *host_or_ip = NULL, *starting_URL = NULL;

    typedef struct{
        int port;
        char *url;
    }prod_args;


    // Check there are 12 arguments in total
    if (argc != 12){
        printf("Wrong number of arguments. Expected (12). Actual (%d)\n"
               "Correct Usage: ./mycrawler -h host_or_IP -p port -c command_port -t num_of_threads -d save_dir starting_URL\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], "-h")){            // if this is "-h" flag
            host_or_ip = argv[i+1];             // next arg is the server ip
            continue;
        }
        else 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
            saveDir = (argv[i+1]);              // next arg is root dir
            continue;
        }
        else {
            starting_URL = argv[11];            // last argument is the starting URL
            continue;
        }
    }

    // Check they are all ok
    if (!saveDir || !serving_port || !command_port || !numThreads || !host_or_ip || !starting_URL){
        printf("Wrong arguments.\n"
               "Correct Usage: ./mycrawler -h host_or_IP -p port -c command_port -t num_of_threads -d save_dir starting_URL\n"
               "Press <ENTER> to continue.\n");
        getchar();
        exit(EXIT_FAILURE);
    }

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

    struct prod_args *prodArgs = malloc(sizeof(struct prod_args));  // Pass thread args as a struct
    prodArgs->port_c        = command_port;
    prodArgs->url           = starting_URL;
    prodArgs->domain        = host_or_ip;
    prodArgs->port_s        = serving_port;
    prodArgs->numThreads    = numThreads;
    prodArgs->saveDir       = saveDir;

    printf("Crawler is starting up...\n");

    initializePool(&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 *) prodArgs);          // Args must be cast to void *
    for(i = 0; i < numThreads; i++)
        pthread_create(&cons[i], 0, consumer, (void *) prodArgs);

    pthread_join(prod, 0);                                          // Join terminating threads
    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 $0x18, %rsp
cmpl $0xc, %edi
jne 0x2729
movq %rsi, %r12
xorl %ebx, %ebx
pushq $0x1
popq %r14
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpq $0xb, %r14
ja 0x25a3
movq (%r12,%r14,8), %rax
cmpb $0x2d, (%rax)
jne 0x255a
cmpb $0x68, 0x1(%rax)
jne 0x252a
cmpb $0x0, 0x2(%rax)
je 0x2565
cmpb $0x70, 0x1(%rax)
jne 0x2536
cmpb $0x0, 0x2(%rax)
je 0x2570
cmpb $0x63, 0x1(%rax)
jne 0x2542
cmpb $0x0, 0x2(%rax)
je 0x257f
cmpb $0x74, 0x1(%rax)
jne 0x254e
cmpb $0x0, 0x2(%rax)
je 0x258e
cmpb $0x64, 0x1(%rax)
jne 0x255a
cmpb $0x0, 0x2(%rax)
je 0x259c
movq 0x58(%r12), %r15
addq $0x2, %r14
jmp 0x250b
movq 0x8(%r12,%r14,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x255f
movq 0x8(%r12,%r14,8), %rdi
callq 0x2300
movl %eax, -0x30(%rbp)
jmp 0x255f
movq 0x8(%r12,%r14,8), %rdi
callq 0x2300
movl %eax, -0x2c(%rbp)
jmp 0x255f
movq 0x8(%r12,%r14,8), %rdi
callq 0x2300
movl %eax, %ebx
jmp 0x255f
movq 0x8(%r12,%r14,8), %r13
jmp 0x255f
testq %r13, %r13
je 0x273b
cmpl $0x0, -0x30(%rbp)
je 0x273b
cmpl $0x0, -0x2c(%rbp)
je 0x273b
testl %ebx, %ebx
je 0x273b
cmpq $0x0, -0x38(%rbp)
je 0x273b
testq %r15, %r15
je 0x273b
movq %r13, %r14
movl %ebx, %eax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
pushq $0x28
popq %rdi
callq 0x2240
movq %rax, %r12
movl -0x2c(%rbp), %eax
movl %eax, (%r12)
movq %r15, 0x10(%r12)
movq -0x38(%rbp), %rax
movq %rax, 0x18(%r12)
movl -0x30(%rbp), %eax
movl %eax, 0x4(%r12)
movl %ebx, 0x8(%r12)
movq %r14, 0x20(%r12)
leaq 0x1b2b(%rip), %rdi      # 0x415a
callq 0x2070
leaq 0x3bc5(%rip), %rdi      # 0x6200
callq 0x2d08
leaq 0x4f51(%rip), %rdi      # 0x7598
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x2370
leaq 0x4f68(%rip), %rdi      # 0x75c0
xorl %esi, %esi
callq 0x2370
leaq 0x4f82(%rip), %rdi      # 0x75e8
xorl %esi, %esi
callq 0x2210
leaq 0x4fa4(%rip), %rdi      # 0x7618
xorl %esi, %esi
callq 0x2210
leaq 0x7a5(%rip), %rdx       # 0x2e27
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0x2280
testl %ebx, %ebx
cmovlel %r14d, %ebx
leaq 0xe6a(%rip), %r14       # 0x3507
movq %r13, %r15
movq %rbx, -0x38(%rbp)
subq $0x1, %rbx
jb 0x26c0
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
callq 0x2280
addq $0x8, %r15
jmp 0x26a4
movq -0x40(%rbp), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x2350
movq -0x38(%rbp), %rbx
cmpq %r14, %rbx
je 0x26e8
movq (%r13,%r14,8), %rdi
xorl %esi, %esi
callq 0x2350
incq %r14
jmp 0x26d2
leaq 0x4ef9(%rip), %rdi      # 0x75e8
callq 0x2290
leaq 0x4f1d(%rip), %rdi      # 0x7618
callq 0x2290
leaq 0x4e91(%rip), %rdi      # 0x7598
callq 0x2110
leaq 0x4ead(%rip), %rdi      # 0x75c0
callq 0x2110
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, %esi
leaq 0x18d2(%rip), %rdi      # 0x4004
xorl %eax, %eax
callq 0x20e0
jmp 0x2747
leaq 0x1980(%rip), %rdi      # 0x40c2
callq 0x2070
callq 0x21a0
pushq $0x1
popq %rdi
callq 0x2330