// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x68(%rsp), %rbx
movb $0x0, -0x20(%rbx)
movq %rsi, -0x58(%rbx)
movl %edx, 0x8(%rsp)
movl %edx, -0x30(%rbx)
movq %rdi, -0x40(%rbx)
movups 0x19e13(%rip), %xmm0    # 0x1f7d8
movups %xmm0, (%rbx)
movups 0x19e19(%rip), %xmm0    # 0x1f7e8
movups %xmm0, 0x10(%rbx)
movq 0x19e1e(%rip), %rax     # 0x1f7f8
movq %rax, 0x20(%rbx)
leaq 0xdb90(%rip), %r12      # 0x13575
movq %r12, %rsi
callq 0x45d0
leaq 0xddf0(%rip), %r13      # 0x137e4
leaq 0xc(%rsp), %rbp
testq %rax, %rax
je 0x5a1a
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x4370
xorl %edi, %edi
movq %r12, %rsi
callq 0x45d0
jmp 0x59f9
leaq 0x90(%rsp), %rdi
callq 0x4430
cmpl $0x1, 0x19e92(%rip)     # 0x1f8c0
movl 0xc(%rsp), %r12d
jne 0x5a49
movq %r14, %rdi
movl %r12d, %esi
movl 0x8(%rsp), %edx
callq 0x4f8d
jmp 0x5bd8
movl %r12d, 0x44(%rsp)
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x4220
movq %r14, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x42b0
movl %eax, 0x3c(%rsp)
cmpl $-0x1, %eax
je 0x5b01
testl %r12d, %r12d
je 0x5b17
jle 0x5b36
shlq $0x3, %r12
movq %r12, %rdi
callq 0x44f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4570
andq $0x0, 0x50(%rsp)
movl 0x44(%rsp), %ecx
movl 0x38(%rsp), %edi
movslq %ecx, %rdx
movq %r14, %rsi
callq 0x4550
movq %rax, 0x58(%rsp)
testq %rax, %rax
js 0x5aea
movl 0x3c(%rsp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
movslq 0x44(%rsp), %rcx
cmpq %rcx, %rax
jne 0x5aae
movq %rbx, %rdi
callq 0x42e0
movq 0x50(%rsp), %r15
movslq 0x44(%rsp), %r12
jmp 0x5b9e
movq %rbx, %rdi
callq 0x4570
movl 0x38(%rsp), %edi
callq 0x4d78
jmp 0x5bd8
movq %rbx, %rdi
callq 0x4570
movl 0x38(%rsp), %edi
callq 0x4ef4
movl 0x3c(%rsp), %edi
callq 0x4700
jmp 0x5bd8
movl $0x20000, %edi          # imm = 0x20000
callq 0x44f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4570
andq $0x0, 0x50(%rsp)
movl 0x38(%rsp), %edi
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rsi
callq 0x4550
movq %rax, 0x58(%rsp)
testq %rax, %rax
js 0x5b8c
movl 0x3c(%rsp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq 0x58(%rsp), %rax
jne 0x5b51
movq %rbx, %rdi
callq 0x42e0
movq 0x50(%rsp), %r15
movq 0x58(%rsp), %r12
movq %rbx, %rdi
callq 0x4570
cmpq %r12, %r15
jne 0x5bb6
movl 0x38(%rsp), %edi
callq 0x4ef4
jmp 0x5bbf
movl 0x8(%rsp), %edi
callq 0x4d86
movq %r14, %rdi
callq 0x4430
movl 0x3c(%rsp), %edi
callq 0x4700
movq %rbx, %rdi
callq 0x42e0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq