// File: /workspace/github_c_makefile_star2_O1/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 0x393e(%rip), %rdi      # 0x5228
callq 0x1110
leaq 0x388a(%rip), %rdi      # 0x5180
callq 0x1715
movl %eax, %r13d
leaq 0x1af1(%rip), %rdi      # 0x33f6
callq 0x1060
leaq -0x910(%rbp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r13d, %edi
callq 0x1100
testl %eax, %eax
jns 0x1938
leaq 0x1895(%rip), %rdi      # 0x31be
callq 0x11f0
movl $0x1, %edi
callq 0x1260
leaq -0x910(%rbp), %r12
movq $0x0, -0x30(%rbp)
xorl %r15d, %r15d
testl %eax, %eax
je 0x1989
movq %r12, %rdi
leaq 0x1876(%rip), %r14      # 0x31ce
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 0x1850(%rip), %rsi      # 0x31d0
callq 0x1120
testl %eax, %eax
je 0x19c9
leaq 0x3898(%rip), %rdi      # 0x5228
callq 0x1110
leaq 0x37e4(%rip), %rdi      # 0x5180
callq 0x1715
movl %eax, %r13d
leaq 0x1a4b(%rip), %rdi      # 0x33f6
callq 0x1060
movl $0x3e8, %edx            # imm = 0x3E8
movl %r13d, %edi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
jns 0x194a
jmp 0x1922
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 0x1a57
callq 0x1c06
leaq -0x520(%rbp), %r14
movq %r14, %rdi
leaq 0x17bd(%rip), %rsi      # 0x31df
leaq 0x3847(%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 0x1ba4
movq %r14, %rdi
callq 0x1c48
callq 0x1c06
movq %r14, %rdi
movq %r15, %rsi
callq 0x1140
movl $0x0, %ecx
testl %eax, %eax
jne 0x1a7e
movl -0x90(%rbp), %ecx
leaq -0x520(%rbp), %rdi
leaq 0x1812(%rip), %rsi      # 0x329e
leaq 0x37dd(%rip), %rdx      # 0x5270
xorl %eax, %eax
callq 0x1250
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11e0
testl %eax, %eax
js 0x1bc5
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 0x1af1
movl -0x90(%rbp), %edx
movl %ebx, %edi
movq %r15, %r14
movq %r15, %rsi
callq 0x1ca1
testl %eax, %eax
js 0x1bd1
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 0x18c2(%rip), %rdi      # 0x3412
callq 0x1060
movq %rbx, %rdi
callq 0x10a0
movl %r13d, %edi
movq %rbx, %rsi
movl %eax, %edx
callq 0x1ce8
movl %eax, -0x30(%rbp)
movl %r15d, %edi
callq 0x10f0
leaq 0x3654(%rip), %r15      # 0x51d0
movq %r15, %rdi
callq 0x1290
leaq 0x35ed(%rip), %rax      # 0x5178
movl -0x30(%rbp), %ecx
addl %ecx, (%rax)
leaq 0x35e5(%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 0x1989
leaq 0x174c(%rip), %rdi      # 0x3318
jmp 0x1929
leaq 0x1745(%rip), %rdi      # 0x331d
jmp 0x1929