// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, -0x2c(%rbp)
leaq 0x480f(%rip), %rdi      # 0x7658
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 0x12ba(%rip), %rsi      # 0x4179
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 0x15d1(%rip), %rdi      # 0x44cc
callq 0x2070
leaq 0x32f9(%rip), %r14      # 0x6200
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d26
leaq 0x46cf(%rip), %rdi      # 0x75e8
callq 0x2180
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x23c0
testl %eax, %eax
js 0x2fb9
movl %eax, %r15d
leaq -0x6c(%rbp), %rcx
andl $0x0, 0x4(%rcx)
rolw $0x8, %bx
movw %bx, 0x2(%rcx)
movw $0x2, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x10
popq %r8
movl %eax, %edi
callq 0x2080
cmpl $-0x1, %eax
je 0x2fc5
leaq -0x6c(%rbp), %rsi
pushq $0x10
popq %rdx
movl %r15d, %edi
callq 0x2270
testl %eax, %eax
jne 0x2fd9
pushq $0x5
popq %rsi
movl %r15d, %edi
callq 0x2260
testl %eax, %eax
jne 0x2fe5
leaq 0x1558(%rip), %rdi      # 0x44e2
callq 0x2070
movq %r12, %rdi
callq 0x32de
cmpl $-0x1, %eax
je 0x2f8f
cmpl $0x0, 0x1390(%r14)
jle 0x2fa8
jmp 0x2fa6
movl 0x46a2(%rip), %eax      # 0x7650
movq -0x38(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x2ff1
jmp 0x2fb7
leaq 0x12a3(%rip), %rdi      # 0x4263
callq 0x32cd
leaq 0x129e(%rip), %rdi      # 0x426a
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x2330
leaq 0x1295(%rip), %rdi      # 0x4275
callq 0x32cd
leaq 0x1291(%rip), %rdi      # 0x427d
callq 0x32cd
movq 0x20(%rcx), %rsi
movq %rcx, %rbx
leaq 0x1285(%rip), %rdi      # 0x4284
xorl %eax, %eax
callq 0x20e0
movq %r12, %rdi
callq 0x3328
movq 0x20(%rbx), %rsi
leaq 0x1284(%rip), %rdi      # 0x429d
xorl %eax, %eax
callq 0x20e0
leaq 0x14d3(%rip), %rdi      # 0x44fa
callq 0x2070
movl 0x8(%rbx), %esi
leaq 0x1274(%rip), %rdi      # 0x42aa
leaq -0x2c(%rbp), %rdx
callq 0x3bc0
movq %rax, -0x58(%rbp)
leaq 0x14c5(%rip), %rdi      # 0x450f
callq 0x2070
leaq -0x50(%rbp), %rbx
movl %r15d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22e0
testl %eax, %eax
js 0x322f
movl %eax, %r12d
leaq 0x14f5(%rip), %rdi      # 0x4566
callq 0x2070
movq %rbx, %rdi
callq 0x20d0
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x2120
movl %r12d, %edi
movq %rbx, %rsi
pushq $0x14
popq %rdx
callq 0x2160
testq %rax, %rax
js 0x323b
movb $0x0, 0x78(%rbp)
movq %rbx, %rdi
leaq 0x1219(%rip), %rsi      # 0x42c9
callq 0x22d0
movq %rbx, %rdi
leaq 0x13ad(%rip), %rsi      # 0x446c
callq 0x22d0
movq %rax, %r13
movq %rax, %rdi
leaq 0x11fb(%rip), %rsi      # 0x42cc
callq 0x2190
testl %eax, %eax
je 0x31c8
movq %r13, %rdi
leaq 0x121c(%rip), %rsi      # 0x4304
callq 0x2190
testl %eax, %eax
je 0x3247
movq %r13, %rdi
leaq 0x122c(%rip), %rsi      # 0x432b
callq 0x2190
testl %eax, %eax
jne 0x3191
movl $0xc8, %edx
leaq -0x140(%rbp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x2120
cmpl $0x0, 0x1390(%r14)
leaq -0x50(%rbp), %rbx
je 0x3168
movups 0x1216(%rip), %xmm0     # 0x434d
movups %xmm0, -0x125(%rbp)
movups 0x11fd(%rip), %xmm0     # 0x4342
movaps %xmm0, -0x130(%rbp)
movups 0x11df(%rip), %xmm0     # 0x4332
movaps %xmm0, -0x140(%rbp)
movl %r12d, %edi
movq %r13, %rsi
pushq $0x2b
popq %rdx
callq 0x34bb
movq 0x44f1(%rip), %rcx      # 0x7660
movq 0x44f2(%rip), %r8       # 0x7668
leaq -0x49(%rbp), %rdi
leaq 0x1001(%rip), %rsi      # 0x4182
pushq $0x4
popq %rdx
movl %r12d, %r9d
callq 0x2a69
jmp 0x3053
movl $0xb7, %edx
leaq -0x12f(%rbp), %rdi
xorl %esi, %esi
callq 0x2120
movups 0x11b2(%rip), %xmm0     # 0x435d
movaps %xmm0, -0x140(%rbp)
movb $0x0, -0x130(%rbp)
movl %r12d, %edi
leaq -0x140(%rbp), %rsi
pushq $0x11
popq %rdx
jmp 0x3225
movl $0xc8, %edx
leaq -0x140(%rbp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x2120
movq 0x4473(%rip), %rsi      # 0x7658
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3414
movl 0x444e(%rip), %ecx      # 0x7648
movl 0x444b(%rip), %r8d      # 0x764c
movq %r13, %rdi
leaq 0x10c7(%rip), %rsi      # 0x42d2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2320
movq %r13, %rdi
callq 0x20d0
movl %r12d, %edi
movq %r13, %rsi
movl %eax, %edx
callq 0x34bb
jmp 0x304f
leaq 0x107c(%rip), %rdi      # 0x42b2
callq 0x32cd
leaq 0x1077(%rip), %rdi      # 0x42b9
callq 0x32cd
leaq -0x127(%rbp), %r14
movl $0xaf, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
movups 0x10a9(%rip), %xmm0     # 0x430d
movaps %xmm0, -0x19(%r14)
movups 0x10a6(%rip), %xmm0     # 0x4316
movups %xmm0, -0x10(%r14)
leaq -0x140(%rbp), %rsi
pushq $0x19
popq %rdx
movl %r12d, %edi
callq 0x34bb
movq 0x43d2(%rip), %rcx      # 0x7660
movq 0x43d3(%rip), %r8       # 0x7668
leaq 0x108a(%rip), %rsi      # 0x4326
pushq $0x4
popq %rdx
leaq -0x49(%rbp), %rdi
movl %r12d, %r9d
callq 0x2a69
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edi
movl -0x2c(%rbp), %edx
movq -0x58(%rbp), %rsi
callq 0x3e71
movl %r12d, %edi
callq 0x2130
xorl %edi, %edi
callq 0x2330