// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movl $0x0, -0x1c(%rbp)
leaq 0x530b(%rip), %rdi      # 0x8668
callq 0x2210
movq -0x8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movq -0x1b8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x10(%rbp)
movq -0x1b8(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x20c0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x2250
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x2050
movq -0x1b8(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x20c0
movq %rax, %rdi
addq $0x4, %rdi
callq 0x2250
movq %rax, -0x198(%rbp)
movq -0x198(%rbp), %rdi
leaq 0x1e85(%rip), %rsi      # 0x5270
callq 0x2050
movq -0x198(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x2330
movq -0x198(%rbp), %rdi
leaq 0x1e5c(%rip), %rsi      # 0x5271
callq 0x2330
leaq -0x3c(%rbp), %rdi
movl -0x10(%rbp), %edx
leaq 0x1d63(%rip), %rsi      # 0x518b
movb $0x0, %al
callq 0x2340
movq -0x28(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x23c0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x3c(%rbp), %rdi
callq 0x20c0
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2050
leaq 0x1dfa(%rip), %rdi      # 0x5273
movb $0x0, %al
callq 0x20e0
movq -0x28(%rbp), %rsi
leaq 0x3d85(%rip), %rdi      # 0x7210
callq 0x31e0
leaq 0x5161(%rip), %rdi      # 0x85f8
callq 0x2190
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23e0
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x34c1
leaq 0x1dce(%rip), %rdi      # 0x528a
callq 0x3910
xorl %edi, %edi
callq 0x2120
movl %eax, -0x1a4(%rbp)
movl -0xc(%rbp), %eax
movzwl %ax, %edi
callq 0x20d0
movw %ax, -0x1a6(%rbp)
movw $0x2, -0x1a8(%rbp)
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x1a8(%rbp), %rcx
movl $0x10, %r8d
callq 0x2070
cmpl $-0x1, %eax
jne 0x3523
leaq 0x1d7d(%rip), %rdi      # 0x5291
callq 0x22e0
movl $0x1, %edi
callq 0x2350
movl -0x14(%rbp), %edi
leaq -0x1a8(%rbp), %rsi
movl $0x10, %edx
callq 0x2280
cmpl $0x0, %eax
je 0x3548
leaq 0x1d59(%rip), %rdi      # 0x529c
callq 0x3910
movl -0x14(%rbp), %edi
movl $0x5, %esi
callq 0x2270
cmpl $0x0, %eax
je 0x3566
leaq 0x1d43(%rip), %rdi      # 0x52a4
callq 0x3910
leaq 0x1d3e(%rip), %rdi      # 0x52ab
movb $0x0, %al
callq 0x20e0
movq -0x198(%rbp), %rdi
callq 0x3930
cmpl $-0x1, %eax
jne 0x3587
jmp 0x3574
jmp 0x3589
leaq 0x3c80(%rip), %rax      # 0x7210
cmpl $0x0, 0x1390(%rax)
jle 0x359b
jmp 0x3589
jmp 0x359d
movl 0x50bd(%rip), %eax      # 0x8660
movq -0x1b8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x35b1
jmp 0x359d
movq -0x1b8(%rbp), %rax
movq 0x20(%rax), %rsi
leaq 0x1d01(%rip), %rdi      # 0x52c4
movb $0x0, %al
callq 0x20e0
movq -0x198(%rbp), %rdi
callq 0x3990
movq -0x1b8(%rbp), %rax
movq 0x20(%rax), %rsi
leaq 0x1cf5(%rip), %rdi      # 0x52dd
movb $0x0, %al
callq 0x20e0
leaq 0x1cf4(%rip), %rdi      # 0x52ea
movb $0x0, %al
callq 0x20e0
movq -0x1b8(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x1cf2(%rip), %rdi      # 0x5300
leaq -0x1c(%rbp), %rdx
callq 0x45a0
movq %rax, -0x1b0(%rbp)
leaq 0x1ce3(%rip), %rdi      # 0x5308
movb $0x0, %al
callq 0x20e0
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2300
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jge 0x364f
leaq 0x1d16(%rip), %rdi      # 0x5360
callq 0x3910
leaq 0x1d11(%rip), %rdi      # 0x5367
movb $0x0, %al
callq 0x20e0
leaq -0x50(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x20c0
movq -0x1c8(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x2130
movl -0x18(%rbp), %edi
leaq -0x50(%rbp), %rsi
movl $0x14, %edx
callq 0x2170
cmpq $0x0, %rax
jge 0x36a5
leaq 0x1ced(%rip), %rdi      # 0x538d
callq 0x3910
movb $0x0, 0x78(%rbp)
leaq -0x50(%rbp), %rdi
leaq 0x1ce9(%rip), %rsi      # 0x539d
callq 0x22f0
leaq -0x50(%rbp), %rdi
leaq 0x1e77(%rip), %rsi      # 0x553b
callq 0x22f0
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdi
leaq 0x1cc2(%rip), %rsi      # 0x53a0
callq 0x21a0
cmpl $0x0, %eax
jne 0x3781
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x2130
leaq -0x120(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x190(%rbp), %rdi
movq 0x4f4d(%rip), %rsi      # 0x8668
callq 0x3aa0
movq -0x1e0(%rbp), %rdi
movq %rax, %rdx
movl 0x4f28(%rip), %ecx      # 0x8658
movl 0x4f25(%rip), %r8d      # 0x865c
leaq 0x1c68(%rip), %rsi      # 0x53a6
movb $0x0, %al
callq 0x2340
movl -0x18(%rbp), %eax
movl %eax, -0x1d4(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x20c0
movl -0x1d4(%rbp), %edi
movq -0x1d0(%rbp), %rsi
movl %eax, %edx
callq 0x3bc0
jmp 0x38fd
movq -0x128(%rbp), %rdi
leaq 0x1c49(%rip), %rsi      # 0x53d8
callq 0x21a0
cmpl $0x0, %eax
jne 0x382c
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x2130
leaq -0x120(%rbp), %rdi
leaq 0x1c23(%rip), %rsi      # 0x53e1
movb $0x0, %al
callq 0x2340
movl -0x18(%rbp), %edi
leaq -0x120(%rbp), %rsi
movl $0x19, %edx
callq 0x3bc0
leaq -0x50(%rbp), %rdi
addq $0x7, %rdi
movq 0x4e88(%rip), %rcx      # 0x8670
movq 0x4e89(%rip), %r8       # 0x8678
movl -0x18(%rbp), %r9d
leaq 0x19b1(%rip), %rsi      # 0x51ab
movl $0x4, %edx
callq 0x2d90
movq -0x1b8(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x1b0(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x4930
movl -0x18(%rbp), %edi
callq 0x2140
xorl %edi, %edi
callq 0x2350
movq -0x128(%rbp), %rdi
leaq 0x1bc0(%rip), %rsi      # 0x53fa
callq 0x21a0
cmpl $0x0, %eax
jne 0x38bd
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x2130
leaq 0x39b2(%rip), %rax      # 0x7210
cmpl $0x0, 0x1390(%rax)
je 0x3890
leaq -0x120(%rbp), %rdi
leaq 0x1b8c(%rip), %rsi      # 0x5401
movb $0x0, %al
callq 0x2340
movl -0x18(%rbp), %edi
leaq -0x120(%rbp), %rsi
movl $0x2b, %edx
callq 0x3bc0
leaq -0x50(%rbp), %rdi
addq $0x7, %rdi
movq 0x4dd1(%rip), %rcx      # 0x8670
movq 0x4dd2(%rip), %r8       # 0x8678
movl -0x18(%rbp), %r9d
leaq 0x18e3(%rip), %rsi      # 0x5194
movl $0x4, %edx
callq 0x2d90
jmp 0x38f9
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x2130
leaq -0x120(%rbp), %rdi
leaq 0x1b4e(%rip), %rsi      # 0x542c
movb $0x0, %al
callq 0x2340
movl -0x18(%rbp), %edi
leaq -0x120(%rbp), %rsi
movl $0x11, %edx
callq 0x3bc0
jmp 0x38fb
jmp 0x38fd
jmp 0x362c
nopw %cs:(%rax,%rax)