// File: /workspace/github_cpp_makefile_star2_O1/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 0x19826(%rip), %xmm0    # 0x1f7f0
movups %xmm0, 0x60(%rsp)
movups 0x1982a(%rip), %xmm0    # 0x1f800
movups %xmm0, 0x70(%rsp)
movq 0x1982e(%rip), %rax     # 0x1f810
movq %rax, 0x80(%rsp)
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x41f0
cmpl $0x1, 0x198b3(%rip)     # 0x1f8b0
jne 0x600e
movq %r14, %rdi
movl %ebp, %esi
callq 0x5535
jmp 0x610d
leaq 0x60(%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 0x60ce
movl %r14d, %edi
movl %ebp, %esi
callq 0x5102
leaq 0x88(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x45d0
movslq 0x30(%r15), %r13
movq %rbx, %rdi
callq 0x42b0
movq %rbx, %rdi
callq 0x4530
leaq (,%r13,8), %rdi
callq 0x44b0
movq %rax, %r15
movl $0x8, %edx
movl %r14d, %edi
movq %r15, %rsi
callq 0x4510
movq %rax, %r12
testl %r12d, %r12d
je 0x60e5
movslq %r12d, %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x4130
cmpq %r13, %rax
jne 0x6075
movq %rbx, %rdi
callq 0x42b0
testl %r12d, %r12d
je 0x60ed
movq %rbx, %rdi
callq 0x4530
movq %r15, %rdi
callq 0x4400
leaq 0xf4b4(%rip), %rsi      # 0x15577
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
jmp 0x6105
leaq 0xf46f(%rip), %rsi      # 0x15544
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jmp 0x6100
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 $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq