// File: /workspace/github_c_makefile_star2_O0/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
subq $0x8f0, %rsp            # imm = 0x8F0
movq %rdi, -0x8(%rbp)
movq $0x0, -0x868(%rbp)
movq $0x0, -0x870(%rbp)
leaq 0x387c(%rip), %rdi      # 0x5228
callq 0x1120
leaq 0x37c8(%rip), %rdi      # 0x5180
callq 0x17a0
movl %eax, -0x87c(%rbp)
leaq 0x17f4(%rip), %rdi      # 0x31be
movb $0x0, %al
callq 0x10b0
movl -0x87c(%rbp), %edi
leaq -0x3f0(%rbp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x1110
movl %eax, -0x880(%rbp)
cmpl $0x0, %eax
jge 0x19ff
leaq 0x17e1(%rip), %rdi      # 0x31db
callq 0x1960
cmpl $0x0, -0x880(%rbp)
jne 0x1a0a
jmp 0x19a5
leaq -0x3f0(%rbp), %rdi
leaq 0x17d3(%rip), %rsi      # 0x31eb
callq 0x1210
movq %rax, -0x3f8(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x17bc(%rip), %rsi      # 0x31eb
callq 0x1210
movq %rax, -0x400(%rbp)
movq -0x3f8(%rbp), %rdi
leaq 0x17a4(%rip), %rsi      # 0x31ed
callq 0x1130
cmpl $0x0, %eax
je 0x1a58
jmp 0x19a5
leaq -0x470(%rbp), %rdi
leaq 0x178b(%rip), %rsi      # 0x31f1
callq 0x1050
leaq -0x470(%rbp), %rdi
movq -0x400(%rbp), %rsi
callq 0x1250
leaq -0x470(%rbp), %rdi
callq 0x1d80
cmpl $0x1, %eax
jne 0x1aff
leaq -0x860(%rbp), %rax
movq %rax, -0x898(%rbp)
callq 0x1dc0
movq -0x898(%rbp), %rdi
movq %rax, %rdx
leaq 0x1749(%rip), %rsi      # 0x31fc
movl $0x3e, %ecx
movb $0x0, %al
callq 0x1260
movl -0x87c(%rbp), %eax
movl %eax, -0x88c(%rbp)
leaq -0x860(%rbp), %rax
movq %rax, -0x888(%rbp)
leaq -0x860(%rbp), %rdi
callq 0x1090
movl -0x88c(%rbp), %edi
movq -0x888(%rbp), %rsi
movq %rax, %rdx
callq 0x1070
jmp 0x1d54
leaq -0x470(%rbp), %rdi
callq 0x1e10
cmpl $0x0, %eax
jne 0x1b80
leaq -0x860(%rbp), %rax
movq %rax, -0x8b0(%rbp)
callq 0x1dc0
movq -0x8b0(%rbp), %rdi
movq %rax, %rdx
leaq 0x1787(%rip), %rsi      # 0x32bb
movl $0x66, %ecx
movb $0x0, %al
callq 0x1260
movl -0x87c(%rbp), %eax
movl %eax, -0x8a4(%rbp)
leaq -0x860(%rbp), %rax
movq %rax, -0x8a0(%rbp)
leaq -0x860(%rbp), %rdi
callq 0x1090
movl -0x8a4(%rbp), %edi
movq -0x8a0(%rbp), %rsi
movq %rax, %rdx
callq 0x1070
jmp 0x1d52
leaq -0x860(%rbp), %rax
movq %rax, -0x8c0(%rbp)
callq 0x1dc0
movq %rax, -0x8b8(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x1e70
movq -0x8c0(%rbp), %rdi
movq -0x8b8(%rbp), %rdx
movl %eax, %ecx
leaq 0x17e5(%rip), %rsi      # 0x33a2
movb $0x0, %al
callq 0x1260
leaq -0x470(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x11f0
movl %eax, -0x874(%rbp)
cmpl $0x0, %eax
jge 0x1beb
leaq 0x1836(%rip), %rdi      # 0x341c
callq 0x1960
leaq -0x470(%rbp), %rdi
callq 0x1e70
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x1190
movq %rax, -0x868(%rbp)
movl -0x874(%rbp), %eax
movl %eax, -0x8cc(%rbp)
movq -0x868(%rbp), %rax
movq %rax, -0x8c8(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x1e70
movl -0x8cc(%rbp), %edi
movq -0x8c8(%rbp), %rsi
movl %eax, %edx
callq 0x1eb0
cmpl $0x0, %eax
jge 0x1c55
leaq 0x17d1(%rip), %rdi      # 0x3421
callq 0x1960
leaq -0x860(%rbp), %rdi
callq 0x1090
movq %rax, -0x8e8(%rbp)
movq -0x868(%rbp), %rdi
callq 0x1090
movq -0x8e8(%rbp), %rdi
addq %rax, %rdi
addq $0x1, %rdi
callq 0x1190
movq %rax, -0x870(%rbp)
movq -0x870(%rbp), %rdi
leaq -0x860(%rbp), %rsi
callq 0x1050
movq -0x870(%rbp), %rdi
movq -0x868(%rbp), %rsi
callq 0x1250
leaq 0x176b(%rip), %rdi      # 0x3426
movb $0x0, %al
callq 0x10b0
movl -0x87c(%rbp), %eax
movl %eax, -0x8dc(%rbp)
movq -0x870(%rbp), %rax
movq %rax, -0x8d8(%rbp)
movq -0x870(%rbp), %rdi
callq 0x1090
movl -0x8dc(%rbp), %edi
movq -0x8d8(%rbp), %rsi
movl %eax, %edx
callq 0x1f30
movl %eax, -0x878(%rbp)
movl -0x874(%rbp), %edi
callq 0x1100
leaq 0x34bc(%rip), %rdi      # 0x51d0
callq 0x12a0
movl -0x878(%rbp), %ecx
leaq 0x3452(%rip), %rax      # 0x5178
addl (%rax), %ecx
leaq 0x3449(%rip), %rax      # 0x5178
movl %ecx, (%rax)
leaq 0x3444(%rip), %rax      # 0x517c
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x3438(%rip), %rax      # 0x517c
movl %ecx, (%rax)
leaq 0x3483(%rip), %rdi      # 0x51d0
callq 0x1180
jmp 0x1d54
movl -0x87c(%rbp), %edi
callq 0x1100
movq -0x870(%rbp), %rdi
callq 0x1030
movq -0x868(%rbp), %rdi
callq 0x1030
jmp 0x19a5
nopl (%rax)