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

            struct http_node *child, content_len_mem;
            struct http_object *http_ptr,http_mem_location;
            http_ptr = &http_mem_location;
            child = &content_len_mem;
            http_ptr->file_name = file_name;
            http_ptr->client_connect = clientSocket;
            http_ptr->buffer = buffer;
            http_ptr->master_key = lock_put;

            int content_length;
            char *char_parse_content = strtok(buffer,"\n");
          
            while(char_parse_content != NULL){
                 sscanf(char_parse_content,"Content-Length: %d",&content_length);
                 char_parse_content = strtok (NULL, "\n");
            }
            free(&char_parse_content);

            /* Redundency */
            if(Redundency == 1){
                f_make_copy(file_name, content_length, clientSocket);
                return NULL;
            }

            http_ptr->content_length = content_length;

            /* Reset the buffer so that child procees may reuse */
            memset(http_ptr->buffer,'\0',HEADER_BUFFER); 
            
            http_ptr->key_fd = open(http_ptr->file_name,O_CREAT | O_WRONLY | O_TRUNC,0664);

            if(http_ptr->key_fd == -1 ){
                pthread_mutex_lock(&http_ptr->master_key);
                {
                    f_void_client_error_not_found(http_ptr->client_connect);return NULL;
                    close(http_ptr->key_fd);
                }
                pthread_mutex_unlock(&http_ptr->master_key);
                return NULL;
            }

            /* ---------------------------- Content Length = 0 ------------------------------------------- */

            if(http_ptr->content_length == 0){ 

                pthread_mutex_lock(&http_ptr->master_key);
                {
                    f_client_req_created(http_ptr->client_connect);
                    close(http_ptr->key_fd); return NULL;
                }
                pthread_mutex_unlock(&http_ptr->master_key);
            }
 
            if(http_ptr->content_length > 0){
         
            /* ---------------------------- Content Length = n -------------------------------------------- */
           
                char *buff = (char*)malloc(http_ptr->content_length*sizeof(char*));
             
                pthread_mutex_lock(&http_ptr->master_key);
                {
                        http_ptr->write_bytes = 0;
                        while((http_ptr->recv_bytes = read(http_ptr->client_connect,buff,http_ptr->content_length)) >= 0){
                            http_ptr->write_bytes += write(http_ptr->key_fd,buff,http_ptr->recv_bytes);
                            if(http_ptr->write_bytes == http_ptr->content_length)break;
                        }
                }
                pthread_mutex_unlock(&http_ptr->master_key);

                if(http_ptr->write_bytes == http_ptr->content_length){
                    
                    pthread_mutex_lock(&http_ptr->master_key);
                    {
                        f_client_req_created(http_ptr->client_connect);
                        free(buff);
                        close(http_ptr->key_fd);
                    }
                    pthread_mutex_unlock(&http_ptr->master_key);
                    return NULL;
                }
                else{

                    pthread_mutex_lock(&http_ptr->master_key);
                    {
                        f_void_intr_error(clientSocket);
                        free(buff);
                        close(http_ptr->key_fd);
                    }
                    pthread_mutex_unlock(&http_ptr->master_key);
                    return NULL;
                }
            }
            /*------------------------- Content Length = ? -> Assume 16 Kib allottted Buffer ---------------------------------*/
            else{ 

                char *buff = (char*)malloc(THREAD_BUFFER*sizeof(char*));
                
                 pthread_mutex_lock(&http_ptr->master_key);
                {
                        http_ptr->write_bytes = 0;

                        while((http_ptr->recv_bytes = read(http_ptr->client_connect,buff,THREAD_BUFFER)) >= 0){
                            http_ptr->write_bytes += write(http_ptr->key_fd,buff,http_ptr->recv_bytes);
                            if(http_ptr->write_bytes == http_ptr->recv_bytes)break;
                        }
                }
                pthread_mutex_unlock(&http_ptr->master_key);

                if(http_ptr->write_bytes == http_ptr->recv_bytes){
                    
                    pthread_mutex_lock(&http_ptr->master_key);
                    {
                        f_client_req_created(http_ptr->client_connect);
                        free(buff);
                        close(http_ptr->key_fd);
                    }
                    pthread_mutex_unlock(&http_ptr->master_key);
                    return NULL;
                }
                else{

                    pthread_mutex_lock(&http_ptr->master_key);
                    {
                        f_void_intr_error(clientSocket);
                        free(buff);
                        close(http_ptr->key_fd);
                    }
                    pthread_mutex_unlock(&http_ptr->master_key);
                    return NULL;
                }
            }
            return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movb $0x0, 0x40(%rsp)
movq %rsi, 0x8(%rsp)
movl %edx, 0x30(%rsp)
movq %rdi, 0x20(%rsp)
movups 0x19a81(%rip), %xmm0    # 0x1f7c8
movups %xmm0, 0x60(%rsp)
movups 0x19a85(%rip), %xmm0    # 0x1f7d8
movups %xmm0, 0x70(%rsp)
movq 0x19a89(%rip), %rax     # 0x1f7e8
movq %rax, 0x80(%rsp)
leaq 0xf807(%rip), %rsi      # 0x15575
callq 0x4590
testq %rax, %rax
je 0x5daa
leaq 0xfa65(%rip), %r14      # 0x157e4
leaq 0x4(%rsp), %r15
leaq 0xf7ea(%rip), %r12      # 0x15575
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4340
xorl %edi, %edi
movq %r12, %rsi
callq 0x4590
testq %rax, %rax
jne 0x5d8b
leaq 0x88(%rsp), %rdi
movq %rax, (%rdi)
callq 0x4400
cmpl $0x1, 0x19aef(%rip)     # 0x1f8b0
movl 0x4(%rsp), %esi
jne 0x5dd6
movq %rbx, %rdi
movl %ebp, %edx
callq 0x527e
jmp 0x5f96
leaq 0x60(%rsp), %rbx
movl %esi, -0x24(%rbx)
movq -0x40(%rbx), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x41f0
movq -0x58(%rbx), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x4280
movl %eax, -0x2c(%rbx)
cmpl $-0x1, %eax
je 0x5e96
movl 0x3c(%rsp), %edi
testq %rdi, %rdi
je 0x5eb5
testl %edi, %edi
jle 0x5edf
shlq $0x3, %rdi
callq 0x44b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4530
movq $0x0, 0x48(%rsp)
movl 0x30(%rsp), %edi
movslq 0x3c(%rsp), %rdx
movq %r14, %rsi
callq 0x4510
movq %rax, 0x50(%rsp)
testq %rax, %rax
js 0x5e7f
movl 0x34(%rsp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
movslq 0x3c(%rsp), %rcx
cmpq %rcx, %rax
jne 0x5e41
movq %rbx, %rdi
callq 0x42b0
movq 0x48(%rsp), %r15
movslq 0x3c(%rsp), %r12
jmp 0x5f4a
movq %rbx, %rdi
callq 0x4530
movl 0x30(%rsp), %edi
leaq 0xf69b(%rip), %rsi      # 0x15544
xorl %eax, %eax
callq 0x4200
jmp 0x5f96
movq %rbx, %rdi
callq 0x4530
movl 0x30(%rsp), %edi
leaq 0xf7f5(%rip), %rsi      # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
movl 0x34(%rsp), %edi
callq 0x46d0
jmp 0x5f96
movl $0x20000, %edi          # imm = 0x20000
callq 0x44b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4530
movq $0x0, 0x48(%rsp)
movl 0x30(%rsp), %edi
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rsi
callq 0x4510
movq %rax, 0x50(%rsp)
testq %rax, %rax
js 0x5f38
movl 0x34(%rsp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq 0x50(%rsp), %rax
jne 0x5efd
movq %rbx, %rdi
callq 0x42b0
movq 0x48(%rsp), %r15
movq 0x50(%rsp), %r12
movq %rbx, %rdi
callq 0x4530
cmpq %r12, %r15
jne 0x5f6d
movl 0x30(%rsp), %edi
leaq 0xf75b(%rip), %rsi      # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
jmp 0x5f7d
leaq 0xf603(%rip), %rsi      # 0x15577
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
movq %r14, %rdi
callq 0x4400
movl 0x34(%rsp), %edi
callq 0x46d0
movq %rbx, %rdi
callq 0x42b0
xorl %eax, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq