// File: /workspace/github_c_makefile_star2_O3/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 $0x8e8, %rsp            # imm = 0x8E8
leaq 0x394a(%rip), %rdi      # 0x5228
callq 0x1110
leaq 0x3896(%rip), %rdi      # 0x5180
callq 0x1709
movl %eax, %r13d
leaq 0x1aed(%rip), %rdi      # 0x33e6
callq 0x1060
leaq -0x910(%rbp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r13d, %edi
callq 0x1100
testl %eax, %eax
jns 0x192c
leaq 0x1891(%rip), %rdi      # 0x31ae
callq 0x11f0
movl $0x1, %edi
callq 0x1260
leaq -0x910(%rbp), %r12
movq $0x0, -0x30(%rbp)
xorl %r15d, %r15d
testl %eax, %eax
je 0x197d
movq %r12, %rdi
leaq 0x1872(%rip), %r14      # 0x31be
movq %r14, %rsi
callq 0x1200
movq %r15, %rbx
movq %rax, %r15
xorl %edi, %edi
movq %r14, %rsi
callq 0x1200
movq %rax, %r14
movq %r15, %rdi
movq %rbx, %r15
leaq 0x184c(%rip), %rsi      # 0x31c0
callq 0x1120
testl %eax, %eax
je 0x19bd
leaq 0x38a4(%rip), %rdi      # 0x5228
callq 0x1110
leaq 0x37f0(%rip), %rdi      # 0x5180
callq 0x1709
movl %eax, %r13d
leaq 0x1a47(%rip), %rdi      # 0x33e6
callq 0x1060
movl $0x3e8, %edx            # imm = 0x3E8
movl %r13d, %edi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
jns 0x193e
jmp 0x1916
movabsq $0x645f746f6f722f2e, %rax # imm = 0x645F746F6F722F2E
movq %rax, -0x130(%rbp)
movl $0x726964, -0x129(%rbp) # imm = 0x726964
leaq -0x130(%rbp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x1240
movq %r14, %rdi
leaq -0xc0(%rbp), %r15
movq %r15, %rsi
callq 0x1140
testl %eax, %eax
je 0x1a4b
callq 0x1bfa
leaq -0x520(%rbp), %r14
movq %r14, %rdi
leaq 0x17b9(%rip), %rsi      # 0x31cf
leaq 0x3853(%rip), %rdx      # 0x5270
movl $0x3e, %ecx
xorl %eax, %eax
callq 0x1250
movq %r14, %rdi
callq 0x10a0
movl %r13d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1080
movq -0x30(%rbp), %r14
movq %rbx, %r15
jmp 0x1b98
movq %r14, %rdi
callq 0x1c3c
callq 0x1bfa
movq %r14, %rdi
movq %r15, %rsi
callq 0x1140
movl $0x0, %ecx
testl %eax, %eax
jne 0x1a72
movl -0x90(%rbp), %ecx
leaq -0x520(%rbp), %rdi
leaq 0x180e(%rip), %rsi      # 0x328e
leaq 0x37e9(%rip), %rdx      # 0x5270
xorl %eax, %eax
callq 0x1250
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11e0
testl %eax, %eax
js 0x1bb9
movl %eax, %ebx
movq %r14, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1140
xorl %ecx, %ecx
testl %eax, %eax
movslq -0x90(%rbp), %rdi
cmovneq %rcx, %rdi
callq 0x1180
movq %rax, %r15
movq %r14, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1140
xorl %edx, %edx
testl %eax, %eax
jne 0x1ae5
movl -0x90(%rbp), %edx
movl %ebx, %edi
movq %r15, %r14
movq %r15, %rsi
callq 0x1c95
testl %eax, %eax
js 0x1bc5
leaq -0x520(%rbp), %rdi
callq 0x10a0
movq %rax, %r15
movq %r14, %rdi
callq 0x10a0
leaq (%r15,%rax), %rdi
incq %rdi
callq 0x1180
movl %ebx, %r15d
movq %rax, %rbx
movq %rax, %rdi
leaq -0x520(%rbp), %rsi
callq 0x1050
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1240
leaq 0x18be(%rip), %rdi      # 0x3402
callq 0x1060
movq %rbx, %rdi
callq 0x10a0
movl %r13d, %edi
movq %rbx, %rsi
movl %eax, %edx
callq 0x1cdc
movl %eax, -0x30(%rbp)
movl %r15d, %edi
callq 0x10f0
leaq 0x3660(%rip), %r15      # 0x51d0
movq %r15, %rdi
callq 0x1290
leaq 0x35f9(%rip), %rax      # 0x5178
movl -0x30(%rbp), %ecx
addl %ecx, (%rax)
leaq 0x35f1(%rip), %rax      # 0x517c
incl (%rax)
movq %r15, %rdi
movq %rbx, %r15
callq 0x1170
movl %r13d, %edi
callq 0x10f0
movq %r15, %rdi
callq 0x1030
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x1030
jmp 0x197d
leaq 0x1748(%rip), %rdi      # 0x3308
jmp 0x191d
leaq 0x1741(%rip), %rdi      # 0x330d
jmp 0x191d