// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %edx, %ebp
movq %rsi, %r14
movb $0x0, 0x40(%rsp)
movups 0x19bf2(%rip), %xmm0    # 0x1f800
movups %xmm0, 0x60(%rsp)
movups 0x19bf6(%rip), %xmm0    # 0x1f810
movups %xmm0, 0x70(%rsp)
movq 0x19bfa(%rip), %rax     # 0x1f820
movq %rax, 0x80(%rsp)
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x4220
cmpl $0x1, 0x19c7f(%rip)     # 0x1f8c0
jne 0x5c52
movq %r14, %rdi
movl %ebp, %esi
callq 0x521e
jmp 0x5d32
leaq 0x60(%rsp), %rbx
movq %r14, %rdi
callq 0x4f84
movl %eax, %r14d
movq %rbx, %rdi
callq 0x4570
cmpl $-0x1, %r14d
je 0x5cfe
movl %r14d, %edi
movl %ebp, %esi
callq 0x4e6f
leaq 0x88(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x4610
movslq 0x30(%r15), %r13
movq %rbx, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x4570
leaq (,%r13,8), %rdi
callq 0x44f0
movq %rax, %r15
pushq $0x8
popq %r12
movl %r14d, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4550
testl %eax, %eax
je 0x5d0a
movslq %eax, %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x4130
cmpq %r13, %rax
jne 0x5cb9
movq %rbx, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x4570
movq %r15, %rdi
callq 0x4430
movl %ebp, %edi
callq 0x4d86
jmp 0x5d2a
movl %ebp, %edi
callq 0x4d78
pushq $-0x1
popq %rdi
jmp 0x5d25
movq %rbx, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x4570
movq %r15, %rdi
callq 0x4430
movl %r14d, %edi
callq 0x4700
movq %rbx, %rdi
callq 0x42e0
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq