// File: /workspace/github_c_makefile_star2_O3/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 $0x608, %rsp            # imm = 0x608
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 0x3739
leaq 0x1c3c(%rip), %rdi      # 0x536e
callq 0x2250
jmp 0x37a7
leaq -0x50(%rbp), %r14
movl $0x0, 0x8(%r14)
movq $0x0, (%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 0x1a01(%rip), %rsi      # 0x5179
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 0x37b1
leaq 0x1ac1(%rip), %rdi      # 0x5263
callq 0x22c0
movl $0x1, %edi
callq 0x2330
movl %eax, %r15d
leaq 0x3a45(%rip), %rbx      # 0x7200
leaq -0x630(%rbp), %r13
leaq 0x4e4f(%rip), %rdi      # 0x8618
callq 0x2180
movl %r15d, %edi
leaq -0x54(%rbp), %rsi
movl $0x10, %edx
callq 0x2340
testl %eax, %eax
jne 0x3944
movq %rbx, %rdi
callq 0x2ec8
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 0x3968
movl %r15d, %edi
movq %r13, %rsi
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x39bb
testl %eax, %eax
js 0x3950
movl %eax, %r12d
movq %r13, %rdi
callq 0x3a02
movl %r15d, -0x2c(%rbp)
movl %eax, %r15d
movq %r13, %rdi
leaq 0x1c09(%rip), %rsi      # 0x545d
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 $-0x630, %rsi           # imm = 0xF9D0
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 0x39bb
movl %eax, %r15d
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x3a50
movq %rax, %r14
movq %rax, %rdi
leaq 0x1ac1(%rip), %rsi      # 0x5384
movq %r13, %rdx
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x395c
movq %r14, %rdi
callq 0x20b0
movq %r13, %rdi
leaq 0x3919(%rip), %rbx      # 0x7200
movq %rbx, %rsi
callq 0x3b9b
movl %r12d, %edi
callq 0x2130
leaq 0x4cc2(%rip), %r14      # 0x85c0
movq %r14, %rdi
callq 0x23b0
addl %r15d, 0x4d3f(%rip)     # 0x864c
incl 0x4d35(%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 -0x630(%rbp), %r13
jne 0x37c2
jmp 0x379b
leaq 0x1a31(%rip), %rdi      # 0x537c
jmp 0x37a2
leaq 0x1962(%rip), %rdi      # 0x52b9
jmp 0x37a2
leaq 0x1a24(%rip), %rdi      # 0x5387
jmp 0x37a2