// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %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)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpl $0xc, -0x8(%rbp)
je 0x2560
movl -0x8(%rbp), %esi
leaq 0x2aba(%rip), %rdi      # 0x5004
movb $0x0, %al
callq 0x20e0
callq 0x21b0
movl $0x1, %edi
callq 0x2350
movl $0x1, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x26a3
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2b3c(%rip), %rsi      # 0x50c2
callq 0x21a0
cmpl $0x0, %eax
jne 0x25aa
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
jmp 0x2695
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2b08(%rip), %rsi      # 0x50c5
callq 0x21a0
cmpl $0x0, %eax
jne 0x25e5
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2320
movl %eax, -0x14(%rbp)
jmp 0x2695
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2ad0(%rip), %rsi      # 0x50c8
callq 0x21a0
cmpl $0x0, %eax
jne 0x261d
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2320
movl %eax, -0x18(%rbp)
jmp 0x2695
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2a9b(%rip), %rsi      # 0x50cb
callq 0x21a0
cmpl $0x0, %eax
jne 0x2655
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2320
movl %eax, -0x1c(%rbp)
jmp 0x2695
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2a66(%rip), %rsi      # 0x50ce
callq 0x21a0
cmpl $0x0, %eax
jne 0x2689
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x2695
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x20(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x20(%rbp)
jmp 0x2567
cmpq $0x0, -0x28(%rbp)
je 0x26ca
cmpl $0x0, -0x14(%rbp)
je 0x26ca
cmpl $0x0, -0x18(%rbp)
je 0x26ca
cmpl $0x0, -0x1c(%rbp)
je 0x26ca
cmpq $0x0, -0x30(%rbp)
je 0x26ca
cmpq $0x0, -0x38(%rbp)
jne 0x26e7
leaq 0x2a00(%rip), %rdi      # 0x50d1
movb $0x0, %al
callq 0x20e0
callq 0x21b0
movl $0x1, %edi
callq 0x2350
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x28, %edi
callq 0x2250
movq %rax, -0x58(%rbp)
movl -0x18(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x1c(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq 0x2a06(%rip), %rdi      # 0x516a
movb $0x0, %al
callq 0x20e0
leaq 0x4a9e(%rip), %rdi      # 0x7210
callq 0x31a0
leaq 0x5e2a(%rip), %rdi      # 0x85a8
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
leaq 0x5e42(%rip), %rdi      # 0x85d0
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
leaq 0x5e5a(%rip), %rdi      # 0x85f8
xorl %eax, %eax
movl %eax, %esi
callq 0x2220
leaq 0x5e7a(%rip), %rdi      # 0x8628
xorl %eax, %eax
movl %eax, %esi
callq 0x2220
movq -0x58(%rbp), %rcx
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0xb76(%rip), %rdx       # 0x3340
callq 0x2290
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x280c
movq -0x60(%rbp), %rdi
movslq -0x20(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x58(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0x1444(%rip), %rdx      # 0x3c40
callq 0x2290
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x27d6
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2370
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2848
movq -0x60(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2370
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2820
leaq 0x5da9(%rip), %rdi      # 0x85f8
callq 0x22b0
leaq 0x5dcd(%rip), %rdi      # 0x8628
callq 0x22b0
leaq 0x5d41(%rip), %rdi      # 0x85a8
callq 0x2110
leaq 0x5d5d(%rip), %rdi      # 0x85d0
callq 0x2110
movl $0x0, -0x4(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nop