// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
cmpl $0xc, %edi
jne 0x27f1
movq %rsi, %r12
movq $0x0, -0x38(%rbp)
movq $-0x1, %rbx
movl $0x0, -0x2c(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movq $0x0, -0x40(%rbp)
xorl %r13d, %r13d
movq 0x10(%r12,%rbx,8), %rax
cmpb $0x2d, (%rax)
jne 0x2606
cmpb $0x68, 0x1(%rax)
jne 0x25d6
cmpb $0x0, 0x2(%rax)
je 0x2617
cmpb $0x70, 0x1(%rax)
jne 0x25e2
cmpb $0x0, 0x2(%rax)
je 0x261e
cmpb $0x63, 0x1(%rax)
jne 0x25ee
cmpb $0x0, 0x2(%rax)
je 0x262e
cmpb $0x74, 0x1(%rax)
jne 0x25fa
cmpb $0x0, 0x2(%rax)
je 0x263d
cmpb $0x64, 0x1(%rax)
jne 0x2606
cmpb $0x0, 0x2(%rax)
je 0x264c
movq 0x58(%r12), %r15
addq $0x2, %rbx
cmpq $0xa, %rbx
jb 0x25c0
jmp 0x2657
movq 0x18(%r12,%rbx,8), %r14
jmp 0x260b
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movq %rax, -0x38(%rbp)
jmp 0x260b
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movl %eax, -0x2c(%rbp)
jmp 0x260b
movq 0x18(%r12,%rbx,8), %rdi
callq 0x2300
movl %eax, %r13d
jmp 0x260b
movq 0x18(%r12,%rbx,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x260b
cmpq $0x0, -0x40(%rbp)
je 0x2803
cmpl $0x0, -0x38(%rbp)
je 0x2803
cmpl $0x0, -0x2c(%rbp)
je 0x2803
testl %r13d, %r13d
je 0x2803
testq %r14, %r14
je 0x2803
testq %r15, %r15
je 0x2803
movl %r13d, %eax
movl %r13d, %ebx
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, %r12
movl -0x2c(%rbp), %eax
movl %eax, (%r12)
movq %r15, 0x10(%r12)
movq %r14, 0x18(%r12)
movq -0x38(%rbp), %rax
movl %eax, 0x4(%r12)
movl %ebx, 0x8(%r12)
movq -0x40(%rbp), %rax
movq %rax, 0x20(%r12)
leaq 0x2a6e(%rip), %rdi      # 0x515a
callq 0x2070
leaq 0x4b08(%rip), %rdi      # 0x7200
callq 0x2e04
leaq 0x5e94(%rip), %rdi      # 0x8598
xorl %esi, %esi
callq 0x2370
leaq 0x5eae(%rip), %rdi      # 0x85c0
xorl %esi, %esi
callq 0x2370
leaq 0x5ec8(%rip), %rdi      # 0x85e8
xorl %esi, %esi
callq 0x2210
leaq 0x5eea(%rip), %rdi      # 0x8618
xorl %esi, %esi
callq 0x2210
leaq 0x838(%rip), %rdx       # 0x2f74
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0x2280
movl %ebx, -0x38(%rbp)
testl %ebx, %ebx
jle 0x27a5
leaq 0xf83(%rip), %r14       # 0x36db
movq -0x48(%rbp), %rbx
movq %r13, %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
callq 0x2280
addq $0x8, %r15
decq %rbx
jne 0x275f
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2350
cmpl $0x0, -0x38(%rbp)
movq -0x48(%rbp), %r14
jle 0x27b0
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2350
incq %rbx
cmpq %rbx, %r14
jne 0x278f
jmp 0x27b0
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2350
leaq 0x5e31(%rip), %rdi      # 0x85e8
callq 0x2290
leaq 0x5e55(%rip), %rdi      # 0x8618
callq 0x2290
leaq 0x5dc9(%rip), %rdi      # 0x8598
callq 0x2110
leaq 0x5de5(%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 0x280a(%rip), %rdi      # 0x5004
xorl %eax, %eax
callq 0x20e0
jmp 0x280f
leaq 0x28b8(%rip), %rdi      # 0x50c2
callq 0x2070
callq 0x21a0
movl $0x1, %edi
callq 0x2330
nop