// File: /workspace/github_c_makefile_star2_O0/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
subq $0x660, %rsp            # imm = 0x660
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x660(%rbp)
movq -0x660(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x61c(%rbp)
movq -0x660(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x660(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x20c0
movq %rax, %rdi
addq $0x4, %rdi
callq 0x2250
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x660(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x2330
movq -0x660(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21c0
movq %rax, -0x650(%rbp)
cmpq $0x0, %rax
jne 0x3ce0
leaq 0x176c(%rip), %rdi      # 0x543d
callq 0x2260
movl $0x1, %edi
callq 0x2350
leaq -0x648(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2130
leaq -0x648(%rbp), %rdi
addq $0x4, %rdi
movq -0x650(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x650(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x2200
movl -0x61c(%rbp), %eax
movzwl %ax, %edi
callq 0x20d0
movw %ax, -0x646(%rbp)
movw $0x2, -0x648(%rbp)
leaq -0x2c(%rbp), %rdi
movl -0x61c(%rbp), %edx
leaq 0x1440(%rip), %rsi      # 0x518b
movb $0x0, %al
callq 0x2340
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x23e0
movl %eax, -0x620(%rbp)
cmpl $-0x1, %eax
jne 0x3d7a
leaq 0x1515(%rip), %rdi      # 0x528a
callq 0x3910
leaq 0x48a7(%rip), %rdi      # 0x8628
callq 0x2190
movl -0x620(%rbp), %edi
leaq -0x648(%rbp), %rsi
movl $0x10, %edx
callq 0x2360
cmpl $0x0, %eax
je 0x3dae
leaq 0x16a2(%rip), %rdi      # 0x544b
callq 0x3910
leaq 0x345b(%rip), %rdi      # 0x7210
callq 0x3290
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x20c0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x2250
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2050
movq -0x10(%rbp), %rdi
movl -0x61c(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x620(%rbp), %ecx
callq 0x3f90
movl -0x620(%rbp), %edi
leaq -0x610(%rbp), %rsi
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x4020
movl %eax, -0x624(%rbp)
cmpl $0x0, %eax
jge 0x3e2b
leaq 0x1567(%rip), %rdi      # 0x538d
callq 0x3910
leaq -0x610(%rbp), %rdi
callq 0x40a0
movl %eax, -0x630(%rbp)
leaq -0x610(%rbp), %rdi
callq 0x4100
movl %eax, -0x628(%rbp)
movl -0x624(%rbp), %eax
subl -0x630(%rbp), %eax
movl %eax, -0x634(%rbp)
movl -0x628(%rbp), %eax
subl -0x634(%rbp), %eax
movl %eax, -0x62c(%rbp)
movslq -0x628(%rbp), %rdi
shlq $0x0, %rdi
addq $0x1, %rdi
callq 0x2250
movq %rax, -0x618(%rbp)
leaq -0x610(%rbp), %rsi
movslq -0x630(%rbp), %rax
addq %rax, %rsi
movq -0x618(%rbp), %rdi
movslq -0x634(%rbp), %rdx
callq 0x22a0
movl -0x620(%rbp), %edi
movq -0x618(%rbp), %rsi
movslq -0x634(%rbp), %rax
addq %rax, %rsi
movl -0x62c(%rbp), %edx
callq 0x4020
movl %eax, -0x638(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4150
movq %rax, -0x658(%rbp)
movq -0x658(%rbp), %rdi
movq -0x618(%rbp), %rdx
leaq 0x1550(%rip), %rsi      # 0x5453
movb $0x0, %al
callq 0x21d0
cmpl $0x0, %eax
jge 0x3f1b
leaq 0x1540(%rip), %rdi      # 0x5456
callq 0x3910
movq -0x658(%rbp), %rdi
callq 0x20a0
movq -0x618(%rbp), %rdi
leaq 0x32db(%rip), %rsi      # 0x7210
callq 0x42d0
movl -0x620(%rbp), %edi
callq 0x2140
leaq 0x4684(%rip), %rdi      # 0x85d0
callq 0x23d0
movl -0x638(%rbp), %eax
addl 0x46ff(%rip), %eax      # 0x865c
movl %eax, 0x46f9(%rip)      # 0x865c
movl 0x46ef(%rip), %eax      # 0x8658
addl $0x1, %eax
movl %eax, 0x46e6(%rip)      # 0x8658
leaq 0x4657(%rip), %rdi      # 0x85d0
callq 0x2240
jmp 0x3d52
nopw %cs:(%rax,%rax)