// File: /workspace/github_c_makefile_star2_O1/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 0x56af(%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 0x2169(%rip), %rsi      # 0x5188
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 0x2474(%rip), %rdi      # 0x54cf
callq 0x2070
leaq 0x4199(%rip), %r14      # 0x7200
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e35
leaq 0x556f(%rip), %rdi      # 0x85e8
callq 0x2180
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23c0
testl %eax, %eax
js 0x3147
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 0x3153
leaq -0x6c(%rbp), %rsi
movl %r15d, %edi
movl $0x10, %edx
callq 0x2270
testl %eax, %eax
jne 0x315f
movl %r15d, %edi
movl $0x5, %esi
callq 0x2260
testl %eax, %eax
jne 0x316b
leaq 0x23e6(%rip), %rdi      # 0x54e5
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 0x310b
cmpl $0x0, 0x1390(%r14)
jle 0x3136
jmp 0x3134
movl 0x5514(%rip), %eax      # 0x8650
movq -0x38(%rbp), %rbx
cmpl 0x8(%rbx), %eax
jge 0x3177
jmp 0x3145
leaq 0x211d(%rip), %rdi      # 0x526b
jmp 0x33dc
leaq 0x2118(%rip), %rdi      # 0x5272
jmp 0x33dc
leaq 0x2117(%rip), %rdi      # 0x527d
jmp 0x33dc
leaq 0x2113(%rip), %rdi      # 0x5285
jmp 0x33dc
movq 0x20(%rbx), %rsi
leaq 0x210a(%rip), %rdi      # 0x528c
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x34d2
movq 0x20(%rbx), %rsi
leaq 0x2109(%rip), %rdi      # 0x52a5
xorl %eax, %eax
callq 0x20e0
leaq 0x2353(%rip), %rdi      # 0x54fd
callq 0x2070
movl 0x8(%rbx), %esi
leaq 0x20f9(%rip), %rdi      # 0x52b2
leaq -0x2c(%rbp), %rdx
callq 0x3e0c
movq %rax, -0x58(%rbp)
leaq 0x2345(%rip), %rdi      # 0x5512
callq 0x2070
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
testl %eax, %eax
js 0x33cc
movl %eax, %r13d
leaq -0x50(%rbp), %rbx
leaq 0x2375(%rip), %rdi      # 0x5569
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 0x33d5
movb $0x0, 0x78(%rbp)
movq %rbx, %rdi
leaq 0x209c(%rip), %rsi      # 0x52d1
callq 0x22d0
movq %rbx, %rdi
leaq 0x222b(%rip), %rsi      # 0x546f
callq 0x22d0
movq %rax, %r12
movq %rax, %rdi
leaq 0x207e(%rip), %rsi      # 0x52d4
callq 0x2190
testl %eax, %eax
je 0x32c6
movq %r12, %rdi
leaq 0x20a3(%rip), %rsi      # 0x530c
callq 0x2190
testl %eax, %eax
je 0x33eb
movq %r12, %rdi
leaq 0x20ae(%rip), %rsi      # 0x532e
callq 0x2190
testl %eax, %eax
je 0x3331
movl $0xb7, %edx
leaq -0x12f(%rbp), %rdi
xorl %esi, %esi
callq 0x2120
movups 0x20b9(%rip), %xmm0     # 0x5360
movaps %xmm0, -0x140(%rbp)
movb $0x0, -0x130(%rbp)
movl %r13d, %edi
leaq -0x140(%rbp), %rsi
movl $0x11, %edx
jmp 0x3323
movl $0xc8, %edx
leaq -0x140(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2120
movq 0x5375(%rip), %rsi      # 0x8658
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
callq 0x35d8
movl 0x5350(%rip), %ecx      # 0x8648
movl 0x534d(%rip), %r8d      # 0x864c
movq %r12, %rdi
leaq 0x1fd1(%rip), %rsi      # 0x52da
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2320
movq %r12, %rdi
callq 0x20d0
movl %r13d, %edi
movq %r12, %rsi
movl %eax, %edx
callq 0x36b2
leaq -0x50(%rbp), %rbx
jmp 0x33b5
movl $0xc8, %edx
leaq -0x140(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2120
cmpl $0x0, 0x1390(%r14)
leaq -0x50(%rbp), %rbx
je 0x338f
movups 0x1ff4(%rip), %xmm0     # 0x5350
movups %xmm0, -0x125(%rbp)
movups 0x1fdb(%rip), %xmm0     # 0x5345
movaps %xmm0, -0x130(%rbp)
movups 0x1fbd(%rip), %xmm0     # 0x5335
movaps %xmm0, -0x140(%rbp)
movl %r13d, %edi
movq %r12, %rsi
movl $0x2b, %edx
callq 0x36b2
movq 0x52ca(%rip), %rcx      # 0x8660
movq 0x52cb(%rip), %r8       # 0x8668
leaq -0x49(%rbp), %rdi
leaq 0x1de9(%rip), %rsi      # 0x5191
movl $0x4, %edx
movl %r13d, %r9d
callq 0x2b48
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
movl %eax, %r13d
testl %eax, %eax
jns 0x31ed
leaq 0x1ee7(%rip), %rdi      # 0x52ba
jmp 0x33dc
leaq 0x1ee5(%rip), %rdi      # 0x52c1
callq 0x22c0
movl $0x1, %edi
callq 0x2330
leaq -0x127(%rbp), %r14
movl $0xaf, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
movups 0x1f0d(%rip), %xmm0     # 0x5315
movaps %xmm0, -0x19(%r14)
movups 0x1f0a(%rip), %xmm0     # 0x531e
movups %xmm0, -0x10(%r14)
leaq -0x140(%rbp), %rsi
movl %r13d, %edi
movl $0x19, %edx
callq 0x36b2
leaq -0x49(%rbp), %rdi
movq 0x5228(%rip), %rcx      # 0x8660
movq 0x5229(%rip), %r8       # 0x8668
leaq 0x1d62(%rip), %rsi      # 0x51a8
movl $0x4, %edx
movl %r13d, %r9d
callq 0x2b48
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edi
movl -0x2c(%rbp), %edx
movq -0x58(%rbp), %rsi
callq 0x40a2
movl %r13d, %edi
callq 0x2130
xorl %edi, %edi
callq 0x2330