// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x110, %rsp            # imm = 0x110
movl %edx, %ebp
movq %rsi, %r14
movb $0x0, 0x38(%rsp)
movups 0x18916(%rip), %xmm0    # 0x1e7f0
movups %xmm0, 0x58(%rsp)
movups 0x1891a(%rip), %xmm0    # 0x1e800
movups %xmm0, 0x68(%rsp)
movq 0x1891e(%rip), %rax     # 0x1e810
movq %rax, 0x78(%rsp)
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x41f0
cmpl $0x1, 0x189a6(%rip)     # 0x1e8b0
jne 0x5f1b
movq %r14, %rdi
movl %ebp, %esi
callq 0x5534
jmp 0x6011
leaq 0x58(%rsp), %rbx
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x4280
movl %eax, %r14d
movq %rbx, %rdi
callq 0x4530
cmpl $-0x1, %r14d
je 0x5fd2
movl %r14d, %edi
movl %ebp, %esi
callq 0x5101
leaq 0x80(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x45d0
movslq 0x30(%r15), %r12
movq %rbx, %rdi
callq 0x42b0
movq %rbx, %rdi
callq 0x4530
leaq (,%r12,8), %rdi
callq 0x44b0
movq %rax, %r15
movl $0x8, %edx
movl %r14d, %edi
movq %r15, %rsi
callq 0x4510
testl %eax, %eax
je 0x5fe9
movslq %eax, %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x4130
cmpq %r12, %rax
jne 0x5f82
movq %rbx, %rdi
callq 0x42b0
movq %rbx, %rdi
callq 0x4530
movq %r15, %rdi
callq 0x4400
leaq 0xf5b0(%rip), %rsi      # 0x15577
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
jmp 0x6009
leaq 0xf56b(%rip), %rsi      # 0x15544
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jmp 0x6004
movq %rbx, %rdi
callq 0x42b0
movq %rbx, %rdi
callq 0x4530
movq %r15, %rdi
callq 0x4400
movl %r14d, %edi
callq 0x46d0
movq %rbx, %rdi
callq 0x42b0
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq