// File: /workspace/github_c_makefile_star2_O2/devkot[P]SearchEngine/functions.c
// Function: consumer
void *consumer(void *ptr) {
    char temp[1000], *type, *requestPath, rootDir[100], header[1000], *payload = NULL, *packet = NULL;
    int fd, bytesWrote, socketID, getRequest;

    while (1){
        pthread_cond_signal(&cond_nonfull);

        socketID = obtain(&pool);                                               // Pop a fd from the pool

        printf("Consumer received a request\n");

        if((getRequest = read(socketID, temp, 1000)) < 0)                       // Read request
            perror_exit("Reading request");

        if(getRequest == 0)                                                     // If the crawler has closed
            continue;

        type = strtok(temp, " ");                                               // Save GET request
        requestPath = strtok(NULL, " ");                                        // Get the given file path

        if(strcmp(type, "GET") != 0)                                            // If it isn't a GET request
            continue;

        strcpy(rootDir, "./root_dir");
        strcat(rootDir, requestPath);                                           // Concatenate the path

        if(fileExists(rootDir) == 1){                                           // File doesn't exist 404
            sprintf(header,
                    "HTTP/1.1 404 Not Found\nDate: %s\nServer: myhttpd/1.0.0 (Ubuntu64)\nContent-Length: %d\nContent-Type: text/html\nConnection: Closed\n\n<html>The page you were looking for could not be found.</html>"
                    , getFormattedTime(), 62);
            write(socketID, header, strlen(header));
        }
        else if (hasPermissions(rootDir) == 0) {                                // Forbidden 403
            sprintf(header,
                    "HTTP/1.1 403 Forbidden\nDate: %s\nServer: myhttpd/1.0.0 (Ubuntu64)\nContent-Length: %d\nContent-Type: text/html\nConnection: Closed\n\n<html>These are not the droids you are looking for..or you are not allowed to access this page.</html>",
                    getFormattedTime(), 102);
            write(socketID, header, strlen(header));
        }
        else {                                                                  // OK to send
            sprintf(header,
                    "HTTP/1.1 200 OK\nDate: %s\nServer: myhttpd/1.0.0 (Ubuntu64)\nContent-Length: %d\nContent-Type: text/html\nConnection: Closed\n\n",
                    getFormattedTime(), getFilesize(rootDir));

            if((fd = open(rootDir, O_RDONLY)) < 0)                              // Open the file
                perror_exit("open");

            payload = (char *) malloc(sizeof(char) * getFilesize(rootDir));     // Allocate memory for the text

            if((customRead(fd, payload, getFilesize(rootDir))) < 0)             // Custom read function to read the entire doc
                perror_exit("read");

            packet = (char *) malloc(strlen(header) + strlen(payload) + 1);     // Allocate memory for the header + payload

            strcpy(packet, header);
            strcat(packet, payload);                                            // Header + payload

            printf("Serving document\n");
            bytesWrote = customWrite(socketID, packet, strlen(packet));         // Write to socket and save bytes for STATS

            close(fd);

            pthread_mutex_lock(&statistics);                                    // Use mutexes

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

            pthread_mutex_unlock(&statistics);                                  // Release lock

        }
        close(socketID);
        free(packet);
        free(payload);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
leaq -0x880(%rbp), %r12
xorl %r13d, %r13d
movq $0x0, -0x30(%rbp)
leaq 0x2a08(%rip), %rdi      # 0x4228
callq 0x1110
leaq 0x2954(%rip), %rdi      # 0x4180
callq 0x1676
movl %eax, %ebx
leaq 0xbac(%rip), %rdi       # 0x23e6
callq 0x1060
movl $0x3e8, %edx            # imm = 0x3E8
movl %ebx, %edi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
js 0x1a44
je 0x1819
movq %r12, %rdi
leaq 0x95c(%rip), %r14       # 0x21be
movq %r14, %rsi
callq 0x1200
movq %rax, %r15
xorl %edi, %edi
movq %r14, %rsi
callq 0x1200
movq %rax, %r14
movq %r15, %rdi
leaq 0x93c(%rip), %rsi       # 0x21c0
callq 0x1120
testl %eax, %eax
jne 0x1819
movabsq $0x645f746f6f722f2e, %rax # imm = 0x645F746F6F722F2E
movq %rax, -0xa0(%rbp)
movl $0x726964, -0x99(%rbp)  # imm = 0x726964
leaq -0xa0(%rbp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x1240
movq %r14, %rdi
callq 0x1a68
testl %eax, %eax
je 0x190b
callq 0x1a91
leaq -0x490(%rbp), %r14
movq %r14, %rdi
leaq 0x8f3(%rip), %rsi       # 0x21cf
leaq 0x298d(%rip), %rdx      # 0x4270
pushq $0x3e
popq %rcx
xorl %eax, %eax
callq 0x1250
movq %r14, %rdi
callq 0x10a0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1080
movq -0x30(%rbp), %r14
jmp 0x1a24
movq %r14, %rdi
callq 0x1ad3
callq 0x1a91
movq %r14, %rdi
callq 0x1b06
leaq -0x490(%rbp), %rdi
leaq 0x960(%rip), %rsi       # 0x228e
leaq 0x293b(%rip), %rdx      # 0x4270
movl %eax, %ecx
xorl %eax, %eax
callq 0x1250
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11e0
testl %eax, %eax
js 0x1a50
movl %eax, %r13d
movq %r14, %rdi
callq 0x1b06
movslq %eax, %rdi
callq 0x1180
movq %r14, %rdi
movq %rax, %r14
callq 0x1b06
movl %r13d, %edi
movq %r14, %rsi
movl %eax, %edx
callq 0x1b31
testl %eax, %eax
js 0x1a5c
leaq -0x490(%rbp), %rdi
callq 0x10a0
movq %rax, %r15
movq %r14, %rdi
callq 0x10a0
leaq (%r15,%rax), %rdi
incq %rdi
callq 0x1180
movq %rax, %r15
movq %rax, -0x30(%rbp)
movq %rax, %rdi
leaq -0x490(%rbp), %rsi
callq 0x1050
movq %r15, %rdi
movq %r14, %rsi
callq 0x1240
leaq 0xa32(%rip), %rdi       # 0x2402
callq 0x1060
movq %r15, %rdi
callq 0x10a0
movl %ebx, %edi
movq %r15, %rsi
movl %eax, %edx
callq 0x1b7d
movl %eax, -0x34(%rbp)
movl %r13d, %edi
callq 0x10f0
leaq 0x27d5(%rip), %r15      # 0x41d0
movq %r15, %rdi
callq 0x1290
leaq 0x276e(%rip), %rax      # 0x4178
movl -0x34(%rbp), %ecx
addl %ecx, (%rax)
leaq 0x2766(%rip), %rax      # 0x417c
incl (%rax)
movq %r15, %rdi
movq -0x30(%rbp), %r13
callq 0x1170
movl %ebx, %edi
callq 0x10f0
movq %r13, %rdi
callq 0x1030
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x1030
jmp 0x1819
leaq 0x763(%rip), %rdi       # 0x21ae
callq 0x17e2
leaq 0x8b1(%rip), %rdi       # 0x2308
callq 0x17e2
leaq 0x8aa(%rip), %rdi       # 0x230d
callq 0x17e2