// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
cmpl $0xc, %edi
jne 0x2812
movq %rsi, %r12
movq $0x0, -0x50(%rbp)
movq $-0x1, %rbx
leaq 0x2b13(%rip), %r13      # 0x50c2
movl $0x0, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movq 0x10(%r12,%rbx,8), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2190
testl %eax, %eax
je 0x263b
movq %r15, %rdi
leaq 0x2ad7(%rip), %rsi      # 0x50c5
callq 0x2190
testl %eax, %eax
je 0x2642
movq %r15, %rdi
leaq 0x2ac7(%rip), %rsi      # 0x50c8
callq 0x2190
testl %eax, %eax
je 0x2652
movq %r15, %rdi
leaq 0x2ab7(%rip), %rsi      # 0x50cb
callq 0x2190
testl %eax, %eax
je 0x2661
movq %r15, %rdi
leaq 0x2aa7(%rip), %rsi      # 0x50ce
callq 0x2190
testl %eax, %eax
je 0x2670
movq 0x58(%r12), %rax
movq %rax, -0x40(%rbp)
jmp 0x2679
movq 0x18(%r12,%rbx,8), %r14
jmp 0x2679
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movq %rax, -0x50(%rbp)
jmp 0x2679
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movl %eax, -0x30(%rbp)
jmp 0x2679
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movl %eax, -0x2c(%rbp)
jmp 0x2679
movq 0x18(%r12,%rbx,8), %rax
movq %rax, -0x38(%rbp)
addq $0x2, %rbx
cmpq $0xa, %rbx
jb 0x25d0
cmpq $0x0, -0x38(%rbp)
movq -0x50(%rbp), %r12
je 0x2824
testl %r12d, %r12d
je 0x2824
cmpl $0x0, -0x30(%rbp)
je 0x2824
cmpl $0x0, -0x2c(%rbp)
je 0x2824
testq %r14, %r14
je 0x2824
cmpq $0x0, -0x40(%rbp)
je 0x2824
movl -0x2c(%rbp), %r15d
movl %r15d, %eax
movq %rsp, %r13
movq %rax, -0x48(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movl $0x28, %edi
callq 0x2240
movq %rax, %rbx
movl -0x30(%rbp), %eax
movl %eax, (%rbx)
movq -0x40(%rbp), %rax
movq %rax, 0x10(%rbx)
movq %r14, 0x18(%rbx)
movl %r12d, 0x4(%rbx)
movl %r15d, 0x8(%rbx)
movq -0x38(%rbp), %rax
movq %rax, 0x20(%rbx)
leaq 0x2a4d(%rip), %rdi      # 0x5169
callq 0x2070
leaq 0x4ad8(%rip), %rdi      # 0x7200
callq 0x2e14
leaq 0x5e64(%rip), %rdi      # 0x8598
xorl %esi, %esi
callq 0x2370
leaq 0x5e7e(%rip), %rdi      # 0x85c0
xorl %esi, %esi
callq 0x2370
leaq 0x5e98(%rip), %rdi      # 0x85e8
xorl %esi, %esi
callq 0x2210
leaq 0x5eba(%rip), %rdi      # 0x8618
xorl %esi, %esi
callq 0x2210
leaq 0x818(%rip), %rdx       # 0x2f84
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2280
testl %r15d, %r15d
jle 0x27a6
leaq 0xf73(%rip), %r14       # 0x36f9
movq -0x48(%rbp), %r12
movq %r13, %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2280
addq $0x8, %r15
decq %r12
jne 0x278d
movq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x2350
cmpl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %r14
jle 0x27d1
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2350
incq %rbx
cmpq %rbx, %r14
jne 0x27bd
leaq 0x5e10(%rip), %rdi      # 0x85e8
callq 0x2290
leaq 0x5e34(%rip), %rdi      # 0x8618
callq 0x2290
leaq 0x5da8(%rip), %rdi      # 0x8598
callq 0x2110
leaq 0x5dc4(%rip), %rdi      # 0x85c0
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 0x27e9(%rip), %rdi      # 0x5004
xorl %eax, %eax
callq 0x20e0
jmp 0x2830
leaq 0x28a6(%rip), %rdi      # 0x50d1
callq 0x2070
callq 0x21a0
movl $0x1, %edi
callq 0x2330
nop