// File: /workspace/github_c_makefile_star2_O1/devkot[P]SearchEngine/Crawler/support.c
// Function: consumer
void *consumer(void *ptr) {
    char *domain, *url, *path, *saveDir;
    char portString[4], dummyBuffer[1500], *dummyFile;
    int port, sock, bytesReadFirst, contLength, bytesToRead, headerSize, bytesRead, bytesWrote;
    struct sockaddr_in servadd;                                                 // Server address
    struct hostent *hp;                                                         // Resolve server IP
    FILE *file;

    struct prod_args* prodArgs = (struct prod_args*) ptr;                       // Get arguments
    port = prodArgs->port_s;                                                    // Avoid redundant parsing
    domain = prodArgs->domain;
    saveDir = (char *) malloc (strlen(prodArgs->saveDir) + 4);
    strcat(saveDir, prodArgs->saveDir);

    if((hp = gethostbyname(prodArgs->domain)) == NULL){
        herror("gethostbyname");
        exit(1);
    }

    memset(&servadd, '\0', sizeof(servadd));
    memcpy(&servadd.sin_addr, hp->h_addr, hp->h_length);
    servadd.sin_port = htons(port);
    servadd.sin_family = AF_INET;

    sprintf(portString, "%d", port);

    while (1){
        if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)                      // Get a socket
            perror_exit("socket");

        pthread_cond_signal(&cond_nonfull);
        if(connect(sock, (struct sockaddr*) &servadd, sizeof(servadd)) != 0)    // Server loops on accept
            perror_exit("connect");

        url = obtain(&pool);                                                    // Pop a URL from the pool

        path = (char *) malloc(strlen(url) + 1);

        strcpy(path, url);

        prepareReq(domain, port, path, sock);                                   // Create and write request

        if((bytesReadFirst = customRead(sock, dummyBuffer, 1500)) < 0)          // Read buffer + some data arbitrarily
            perror_exit("Reading request");

        headerSize  = getHeaderSize(dummyBuffer);                               // Get header size

        contLength  = getContentLength(dummyBuffer);                            // Get content length from header

        bytesRead   = bytesReadFirst - headerSize;                              // Body bytes read

        bytesToRead = contLength - bytesRead;                                   // Body bytes remaining

        dummyFile = (char *) malloc((sizeof(char) * contLength) + 1);

        bcopy(dummyBuffer + headerSize, dummyFile, bytesRead);                  // Copy remaining to temp buffer

        bytesWrote = customRead(sock, dummyFile + bytesRead, bytesToRead);      // Read and save the rest of the contents

        file = createSaveFile(path, saveDir);

        if((fprintf(file, "%s", dummyFile) < 0))                                // Hard save the text
            perror_exit("fprintf");

        fclose(file);                                                           // Close file pointer

        searchUrl(dummyFile, &pool);
        close(sock);

        pthread_mutex_lock(&statistics);                                        // Use mutexes

        bytes += bytesWrote;                                                    // Update stats
        pages++;

        pthread_mutex_unlock(&statistics);                                      // Release lock
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movl 0x4(%rdi), %ebx
movq 0x18(%rdi), %r15
movq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x20d0
leaq 0x4(%rax), %rdi
callq 0x2240
movq %rax, -0x38(%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2310
movq %r15, -0x40(%rbp)
movq %r15, %rdi
callq 0x21b0
testq %rax, %rax
jne 0x3757
leaq 0x1c21(%rip), %rdi      # 0x5371
callq 0x2250
jmp 0x37be
xorps %xmm0, %xmm0
leaq -0x5c(%rbp), %r14
movaps %xmm0, -0x4(%r14)
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
movq %r14, %rdi
callq 0x21f0
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, -0x2(%r14)
movw $0x2, -0x4(%r14)
leaq 0x19f9(%rip), %rsi      # 0x5188
leaq -0x44(%rbp), %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2320
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23c0
cmpl $-0x1, %eax
jne 0x37c8
leaq 0x1ab2(%rip), %rdi      # 0x526b
callq 0x22c0
movl $0x1, %edi
callq 0x2330
movl %eax, %r15d
leaq 0x3a2e(%rip), %rbx      # 0x7200
leaq -0x640(%rbp), %r13
leaq 0x4e38(%rip), %rdi      # 0x8618
callq 0x2180
movl %r15d, %edi
leaq -0x60(%rbp), %rsi
movl $0x10, %edx
callq 0x2340
testl %eax, %eax
jne 0x395b
movq %rbx, %rdi
callq 0x2ed8
movq %rax, %r12
movq %rax, %rdi
callq 0x20d0
leaq 0x1(%rax), %rdi
callq 0x2240
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x2050
movq -0x40(%rbp), %rdi
movq %r14, %rdx
movl %r15d, %ecx
callq 0x397f
movl %r15d, %edi
movq %r13, %rsi
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x39d2
testl %eax, %eax
js 0x3967
movl %eax, %r12d
movq %r13, %rdi
callq 0x3a19
movl %r15d, -0x2c(%rbp)
movl %eax, %r15d
movq %r13, %rdi
leaq 0x1bf5(%rip), %rsi      # 0x5460
callq 0x23a0
leaq 0x10(%rax), %rdi
callq 0x2300
movl %eax, %ebx
subl %r15d, %r12d
movslq %eax, %rdi
subl %r12d, %ebx
incq %rdi
callq 0x2240
movq %rax, %r13
movslq %r15d, %rax
leaq (%rax,%rbp), %rsi
addq $-0x640, %rsi           # imm = 0xF9C0
movslq %r12d, %r15
movq %r13, %rdi
movq %r15, %rdx
callq 0x21f0
addq %r13, %r15
movl -0x2c(%rbp), %edi
movl %edi, %r12d
movq %r15, %rsi
movl %ebx, %edx
callq 0x39d2
movl %eax, %r15d
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x3a67
movq %rax, %r14
movq %rax, %rdi
leaq 0x1aad(%rip), %rsi      # 0x5387
movq %r13, %rdx
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x3973
movq %r14, %rdi
callq 0x20b0
movq %r13, %rdi
leaq 0x3902(%rip), %rbx      # 0x7200
movq %rbx, %rsi
callq 0x3bb2
movl %r12d, %edi
callq 0x2130
leaq 0x4cab(%rip), %r14      # 0x85c0
movq %r14, %rdi
callq 0x23b0
addl %r15d, 0x4d28(%rip)     # 0x864c
incl 0x4d1e(%rip)            # 0x8648
movq %r14, %rdi
callq 0x2230
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23c0
movl %eax, %r15d
cmpl $-0x1, %eax
leaq -0x640(%rbp), %r13
jne 0x37d9
jmp 0x37b2
leaq 0x1a1d(%rip), %rdi      # 0x537f
jmp 0x37b9
leaq 0x1953(%rip), %rdi      # 0x52c1
jmp 0x37b9
leaq 0x1a10(%rip), %rdi      # 0x538a
jmp 0x37b9