// File: /workspace/github_c_makefile_star2_O2/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 0x356b
leaq 0xe10(%rip), %rdi       # 0x436e
callq 0x2250
pushq $0x1
popq %rdi
callq 0x2330
leaq -0x50(%rbp), %r14
andl $0x0, 0x8(%r14)
andq $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 0xbd5(%rip), %rsi       # 0x4179
leaq -0x44(%rbp), %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2320
leaq 0x2c48(%rip), %rbx      # 0x6200
leaq -0x630(%rbp), %r13
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x23c0
cmpl $-0x1, %eax
je 0x3720
movl %eax, %r15d
leaq 0x4039(%rip), %rdi      # 0x7618
callq 0x2180
movl %r15d, %edi
leaq -0x54(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x2340
testl %eax, %eax
jne 0x372c
movq %rbx, %rdi
callq 0x2da3
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 0x3750
movl %r15d, %edi
movq %r13, %rsi
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x37a3
testl %eax, %eax
js 0x3738
movl %eax, %r12d
movq %r13, %rdi
callq 0x37ef
movl %r15d, -0x2c(%rbp)
movl %eax, %r15d
movq %r13, %rdi
callq 0x3823
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 0x37a3
movl %eax, %r15d
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x383d
movq %rax, %r14
movq %rax, %rdi
leaq 0xcbd(%rip), %rsi       # 0x4384
movq %r13, %rdx
xorl %eax, %eax
callq 0x21c0
testl %eax, %eax
js 0x3744
movq %r14, %rdi
callq 0x20b0
movq %r13, %rdi
leaq 0x2b19(%rip), %rbx      # 0x6200
movq %rbx, %rsi
callq 0x398f
movl %r12d, %edi
callq 0x2130
leaq 0x3ec2(%rip), %r14      # 0x75c0
movq %r14, %rdi
callq 0x23b0
addl %r15d, 0x3f3f(%rip)     # 0x764c
incl 0x3f35(%rip)            # 0x7648
movq %r14, %rdi
callq 0x2230
jmp 0x35b8
leaq 0xb3c(%rip), %rdi       # 0x4263
callq 0x32cd
leaq 0xc49(%rip), %rdi       # 0x437c
callq 0x32cd
leaq 0xb7a(%rip), %rdi       # 0x42b9
callq 0x32cd
leaq 0xc3c(%rip), %rdi       # 0x4387
callq 0x32cd