// File: Cristian-Castillo[P]Multithreaded-HTTP-Server/./httpserver_definitions.cpp
// Function: f_void_get_module(char*, char*, int)
void *f_void_get_module(char *buffer,char *file_name,int clientSocket){

    struct http_object *http_ptr,http_mem_location;
    http_ptr = &http_mem_location;

    struct http_node *child, content_len_mem;
    child = &content_len_mem;
    http_ptr->master_key = lock_get;

    memset(buffer,'\0',HEADER_BUFFER);

    if(Redundency == 1){
        f_check_copy(file_name, clientSocket);
        return NULL;
    }

    /* Open the file for reads only! */
    int fetch_file_fd = f_fetch_file(file_name);

    if(fetch_file_fd == -1){ 
        pthread_mutex_lock(&http_ptr->master_key);
        {
            f_void_client_error_not_found(clientSocket);
            close(fetch_file_fd);
            pthread_mutex_unlock(&http_ptr->master_key);
        }
        return NULL; 
    }
    
    /* 200 OK client file found fetch from server */
    if(fetch_file_fd != -1){ 

        int get_length;
         /* Get file size  & 200 OK */
        pthread_mutex_lock(&http_ptr->master_key);
        {
            f_client_req_found(fetch_file_fd,clientSocket);
            struct stat buf;
            fstat(fetch_file_fd,&buf);
            get_length = buf.st_size;
        }
        pthread_mutex_unlock(&http_ptr->master_key);

     
        pthread_mutex_lock(&http_ptr->master_key);
        char *buff = (char*)malloc(get_length*sizeof(char*));
        ssize_t send_bytes;

        /* Store file contents */
        while((child->content_length = read(fetch_file_fd,buff,sizeof(buff))) != 0){
            send_bytes =  write(clientSocket,buff,child->content_length);
            if(send_bytes == get_length){break;}
        }
        pthread_mutex_unlock(&http_ptr->master_key);

        if(child->content_length == 0){
            pthread_mutex_lock(&http_ptr->master_key);
            {
                free(buff);
                close(fetch_file_fd);
                
            }
            pthread_mutex_unlock(&http_ptr->master_key);
            return NULL;
        }
        else{ 
        /* Internal error of 500 Content failuire */
        pthread_mutex_lock(&http_ptr->master_key);
        {
            free(buff);
            f_void_intr_error(clientSocket);
         
        }
        pthread_mutex_unlock(&http_ptr->master_key);
        return NULL; 
        }
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x8d80
leaq -0xa8(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0xb4(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
leaq 0x47c06(%rip), %rsi     # 0x4f8f0
movl $0x28, %edx
callq 0x53e0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x5260
cmpl $0x1, 0x47ca5(%rip)     # 0x4f9b0
jne 0x7d26
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x6e00
movq $0x0, -0x8(%rbp)
jmp 0x7ee0
movq -0x18(%rbp), %rdi
callq 0x69c0
movl %eax, -0xb8(%rbp)
cmpl $-0x1, -0xb8(%rbp)
jne 0x7d78
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x56d0
movl -0x1c(%rbp), %edi
callq 0x6470
movl -0xb8(%rbp), %edi
callq 0x58b0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x53b0
movq $0x0, -0x8(%rbp)
jmp 0x7ee0
cmpl $-0x1, -0xb8(%rbp)
je 0x7ed8
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x56d0
movl -0xb8(%rbp), %edi
movl -0x1c(%rbp), %esi
callq 0x6700
movl -0xb8(%rbp), %edi
leaq -0x150(%rbp), %rsi
callq 0x57a0
movq -0x120(%rbp), %rax
movl %eax, -0xbc(%rbp)
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x53b0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x56d0
movslq -0xbc(%rbp), %rdi
shlq $0x3, %rdi
callq 0x5640
movq %rax, -0x158(%rbp)
movl -0xb8(%rbp), %edi
movq -0x158(%rbp), %rsi
movl $0x8, %edx
callq 0x56b0
movq -0xb0(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
je 0x7e4c
movl -0x1c(%rbp), %edi
movq -0x158(%rbp), %rsi
movq -0xb0(%rbp), %rax
movslq (%rax), %rdx
callq 0x5170
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rax
movslq -0xbc(%rbp), %rcx
cmpq %rcx, %rax
jne 0x7e4a
jmp 0x7e4c
jmp 0x7df0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x53b0
movq -0xb0(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x7ea0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x56d0
movq -0x158(%rbp), %rdi
callq 0x5560
movl -0xb8(%rbp), %edi
callq 0x58b0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x53b0
movq $0x0, -0x8(%rbp)
jmp 0x7ee0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x56d0
movq -0x158(%rbp), %rdi
callq 0x5560
movl -0x1c(%rbp), %edi
callq 0x64a0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
callq 0x53b0
movq $0x0, -0x8(%rbp)
jmp 0x7ee0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopl (%rax)