// File: /workspace/github_c_makefile_star2_O3/devkot[P]SearchEngine/Crawler/support.c
// Function: producer
void *producer(void *ptr) {
    int command_port, serving_port, lsock, csock, nDocs = 0;
    char *url, *trimpath, *path, portString[4], telbuf[20], stats[200], *command, runTime[100], *saveDir;
    struct sockaddr_in myaddr;
    DocumentNode *docArray;

    time (&start);


    struct prod_args* prodArgs = (struct prod_args*) ptr;           // Get arguments
    command_port = prodArgs->port_c;
    serving_port = prodArgs->port_s;
    url = (char *) malloc (strlen(prodArgs->url) + 1);
    strcpy(url, prodArgs->url);
    saveDir = (char *) malloc (strlen(prodArgs->saveDir) + 4);
    strcpy(saveDir, "./");
    strcat(saveDir, prodArgs->saveDir);
    strcat(saveDir, "/");

    sprintf(portString, "%d", serving_port);                        // Port to string
    trimpath = strstr(url, portString);                             // Split string after port
    path = (trimpath+strlen(portString));                           // Set pointer after port
    strcpy(url, path);

    printf("Beginning crawling...\n");
    place(&pool, url);                                              // Place the given URL into the pool

    pthread_cond_signal(&cond_nonempty);


    if((lsock = socket( AF_INET, SOCK_STREAM, 0)) < 0)              // Create command socket
        perror_exit("socket");

    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(command_port);
    myaddr.sin_family = AF_INET;

    // Resolve address already in use
    if (setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &myaddr, sizeof(myaddr)) == -1) {
        perror("setsockopt");
        exit(1);
    }

    if(bind(lsock, (struct sockaddr *)&myaddr, sizeof(myaddr)))
        perror_exit("command");

    if(listen(lsock, 5) != 0)
        perror_exit("listen");
    printf("Crawling in progress...\n");

   while(dirExists(saveDir) == -1){}
   while(pool.count > 0){}
   while(ready < prodArgs->numThreads){}               		// Wait for dir to get created

    printf("Trying to create %s ...\n", prodArgs->saveDir);
    readSaveFolder(saveDir);                                // Populate docfile
    printf("%s created!\n", prodArgs->saveDir);

    printf("Crawling finished...\n");

    docArray = prepareJobExec("docfile", prodArgs->numThreads, &nDocs);

    printf("Crawler is ready to receive commands! <STATS, SHUTDOWN, SEARCH q1 q2...q10> via telnet\n");
    while(1){
        if((csock = accept(lsock, NULL, NULL)) < 0)      // Accept telnet connections
            perror_exit("accept");

        printf("Crawler received a telnet command...\n");
        bzero(telbuf, strlen(telbuf));
        if((read(csock, telbuf, 20)) < 0)
            perror_exit("Reading request");

        telbuf[200] = '\0';                              // Append terminating character

        strtok(telbuf, "\r\n");                          // Remove chars appended by telnet

        command = strtok(telbuf, " ");

        if (!strcmp(command, "STATS")){
            bzero(stats, 200);
            sprintf(stats, "Crawler up for %s, downloaded %d pages, %d bytes\n", getRunTime(runTime, start), pages, bytes);
            customWrite(csock, stats, strlen(stats));
        }
        else if (!strcmp(command, "SHUTDOWN")){ 
            bzero(stats, 200);
            sprintf(stats, "Server shutting down...\n");
            customWrite(csock, stats, 25);
            writeQuery(telbuf + 7, "exit", 4, fdIn, fdOut, csock);
            cleanUp(prodArgs->numThreads, docArray, nDocs);
            close(csock);
            exit(0);
        }
        else if (!strcmp(command, "SEARCH")){
            bzero(stats, 200);
            if (pool.count != 0) {
                sprintf(stats, "Crawling in progress. Please try again...\n");
                customWrite(csock, stats, 43);
            }
            writeQuery(telbuf + 7, "search", 4, fdIn, fdOut, csock);
        }
        else {
            bzero(stats, 200);
            sprintf(stats, "Invalid command\n");
            customWrite(csock, stats, 17);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdi, %r13
movl $0x0, -0x2c(%rbp)
leaq 0x56bf(%rip), %rdi      # 0x8658
callq 0x2200
movl (%r13), %ebx
movl 0x4(%r13), %r14d
movq 0x10(%r13), %r12
movq %r12, %rdi
callq 0x20d0
leaq 0x1(%rax), %rdi
callq 0x2240
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
callq 0x2050
movq %r13, -0x38(%rbp)
movq 0x20(%r13), %r13
movq %r13, %rdi
callq 0x20d0
leaq 0x4(%rax), %rdi
callq 0x2240
movq %rax, %r12
movw $0x2f2e, (%rax)         # imm = 0x2F2E
movb $0x0, 0x2(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2310
movq %r12, %rdi
callq 0x20d0
movw $0x2f, (%r12,%rax)
leaq 0x216a(%rip), %rsi      # 0x5179
leaq -0x5c(%rbp), %r13
movq %r13, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2320
movq %r15, %rdi
movq %r13, %rsi
callq 0x23a0
movq %rax, %r14
movq %r13, %rdi
callq 0x20d0
addq %rax, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x2050
leaq 0x2481(%rip), %rdi      # 0x54cc
callq 0x2070
leaq 0x41a9(%rip), %r14      # 0x7200
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e25
leaq 0x557f(%rip), %rdi      # 0x85e8
callq 0x2180
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23c0
testl %eax, %eax
js 0x3137
movl %eax, %r15d
leaq -0x6c(%rbp), %rcx
movl $0x0, 0x4(%rcx)
rolw $0x8, %bx
movw %bx, 0x2(%rcx)
movw $0x2, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x10, %r8d
callq 0x2080
cmpl $-0x1, %eax
je 0x3143
leaq -0x6c(%rbp), %rsi
movl %r15d, %edi
movl $0x10, %edx
callq 0x2270
testl %eax, %eax
jne 0x314f
movl %r15d, %edi
movl $0x5, %esi
callq 0x2260
testl %eax, %eax
jne 0x315b
leaq 0x23f3(%rip), %rdi      # 0x54e2
callq 0x2070
leaq -0x140(%rbp), %r13
movl $0x90, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x2120
movq %r12, %rdi
movq %r13, %rsi
callq 0x21e0
cmpl $-0x1, %eax
je 0x30fb
cmpl $0x0, 0x1390(%r14)
jle 0x3126
jmp 0x3124
movl 0x5524(%rip), %eax      # 0x8650
movq -0x38(%rbp), %rbx
cmpl 0x8(%rbx), %eax
jge 0x3167
jmp 0x3135
leaq 0x2125(%rip), %rdi      # 0x5263
jmp 0x33cc
leaq 0x2120(%rip), %rdi      # 0x526a
jmp 0x33cc
leaq 0x211f(%rip), %rdi      # 0x5275
jmp 0x33cc
leaq 0x211b(%rip), %rdi      # 0x527d
jmp 0x33cc
movq 0x20(%rbx), %rsi
leaq 0x2112(%rip), %rdi      # 0x5284
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x34c2
movq 0x20(%rbx), %rsi
leaq 0x2111(%rip), %rdi      # 0x529d
xorl %eax, %eax
callq 0x20e0
leaq 0x2360(%rip), %rdi      # 0x54fa
callq 0x2070
movl 0x8(%rbx), %esi
leaq 0x2101(%rip), %rdi      # 0x52aa
leaq -0x2c(%rbp), %rdx
callq 0x3df4
movq %rax, -0x58(%rbp)
leaq 0x2352(%rip), %rdi      # 0x550f
callq 0x2070
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
testl %eax, %eax
js 0x33bc
movl %eax, %r13d
leaq -0x50(%rbp), %rbx
leaq 0x2382(%rip), %rdi      # 0x5566
callq 0x2070
movq %rbx, %rdi
callq 0x20d0
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2120
movl $0x14, %edx
movl %r13d, %edi
movq %rbx, %rsi
callq 0x2160
testq %rax, %rax
js 0x33c5
movb $0x0, 0x78(%rbp)
movq %rbx, %rdi
leaq 0x20a4(%rip), %rsi      # 0x52c9
callq 0x22d0
movq %rbx, %rdi
leaq 0x2238(%rip), %rsi      # 0x546c
callq 0x22d0
movq %rax, %r12
movq %rax, %rdi
leaq 0x2086(%rip), %rsi      # 0x52cc
callq 0x2190
testl %eax, %eax
je 0x32b6
movq %r12, %rdi
leaq 0x20ab(%rip), %rsi      # 0x5304
callq 0x2190
testl %eax, %eax
je 0x33db
movq %r12, %rdi
leaq 0x20bb(%rip), %rsi      # 0x532b
callq 0x2190
testl %eax, %eax
je 0x3321
movl $0xb7, %edx
leaq -0x12f(%rbp), %rdi
xorl %esi, %esi
callq 0x2120
movups 0x20c6(%rip), %xmm0     # 0x535d
movaps %xmm0, -0x140(%rbp)
movb $0x0, -0x130(%rbp)
movl %r13d, %edi
leaq -0x140(%rbp), %rsi
movl $0x11, %edx
jmp 0x3313
movl $0xc8, %edx
leaq -0x140(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2120
movq 0x5385(%rip), %rsi      # 0x8658
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
callq 0x35ba
movl 0x5360(%rip), %ecx      # 0x8648
movl 0x535d(%rip), %r8d      # 0x864c
movq %r12, %rdi
leaq 0x1fd9(%rip), %rsi      # 0x52d2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2320
movq %r12, %rdi
callq 0x20d0
movl %r13d, %edi
movq %r12, %rsi
movl %eax, %edx
callq 0x3694
leaq -0x50(%rbp), %rbx
jmp 0x33a5
movl $0xc8, %edx
leaq -0x140(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2120
cmpl $0x0, 0x1390(%r14)
leaq -0x50(%rbp), %rbx
je 0x337f
movups 0x2001(%rip), %xmm0     # 0x534d
movups %xmm0, -0x125(%rbp)
movups 0x1fe8(%rip), %xmm0     # 0x5342
movaps %xmm0, -0x130(%rbp)
movups 0x1fca(%rip), %xmm0     # 0x5332
movaps %xmm0, -0x140(%rbp)
movl %r13d, %edi
movq %r12, %rsi
movl $0x2b, %edx
callq 0x3694
movq 0x52da(%rip), %rcx      # 0x8660
movq 0x52db(%rip), %r8       # 0x8668
leaq -0x49(%rbp), %rdi
leaq 0x1dea(%rip), %rsi      # 0x5182
movl $0x4, %edx
movl %r13d, %r9d
callq 0x2b2b
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
movl %eax, %r13d
testl %eax, %eax
jns 0x31dd
leaq 0x1eef(%rip), %rdi      # 0x52b2
jmp 0x33cc
leaq 0x1eed(%rip), %rdi      # 0x52b9
callq 0x22c0
movl $0x1, %edi
callq 0x2330
leaq -0x127(%rbp), %r14
movl $0xaf, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
movups 0x1f15(%rip), %xmm0     # 0x530d
movaps %xmm0, -0x19(%r14)
movups 0x1f12(%rip), %xmm0     # 0x5316
movups %xmm0, -0x10(%r14)
leaq -0x140(%rbp), %rsi
movl %r13d, %edi
movl $0x19, %edx
callq 0x3694
movq 0x523c(%rip), %rcx      # 0x8660
movq 0x523d(%rip), %r8       # 0x8668
leaq 0x1ef4(%rip), %rsi      # 0x5326
leaq -0x49(%rbp), %rdi
movl $0x4, %edx
movl %r13d, %r9d
callq 0x2b2b
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edi
movl -0x2c(%rbp), %edx
movq -0x58(%rbp), %rsi
callq 0x409a
movl %r13d, %edi
callq 0x2130
xorl %edi, %edi
callq 0x2330