// File: /workspace/github_cpp_makefile_star2_O3/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
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movb $0x0, 0x48(%rsp)
movq %rsi, 0x10(%rsp)
movl %edx, 0x38(%rsp)
movq %rdi, 0x28(%rsp)
movups 0x18b70(%rip), %xmm0    # 0x1e7c8
movups %xmm0, 0x68(%rsp)
movups 0x18b74(%rip), %xmm0    # 0x1e7d8
movups %xmm0, 0x78(%rsp)
movq 0x18b78(%rip), %rax     # 0x1e7e8
movq %rax, 0x88(%rsp)
leaq 0xf8f6(%rip), %rsi      # 0x15575
callq 0x4590
testq %rax, %rax
je 0x5cbb
leaq 0xfb54(%rip), %rbx      # 0x157e4
leaq 0xc(%rsp), %r12
leaq 0xf8d9(%rip), %r13      # 0x15575
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x4340
xorl %edi, %edi
movq %r13, %rsi
callq 0x4590
testq %rax, %rax
jne 0x5c9c
leaq 0x90(%rsp), %rdi
callq 0x4400
cmpl $0x1, 0x18be1(%rip)     # 0x1e8b0
movl 0xc(%rsp), %r12d
jne 0x5ce8
movq %r14, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x527d
jmp 0x5ea6
leaq 0x68(%rsp), %rbx
movl %r12d, -0x24(%rbx)
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x41f0
movq %r14, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x4280
movl %eax, -0x2c(%rbx)
cmpl $-0x1, %eax
je 0x5da6
testl %r12d, %r12d
je 0x5dc5
jle 0x5def
shlq $0x3, %r12
movq %r12, %rdi
callq 0x44b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4530
movq $0x0, 0x50(%rsp)
movl 0x44(%rsp), %ecx
movl 0x38(%rsp), %edi
movslq %ecx, %rdx
movq %r14, %rsi
callq 0x4510
movq %rax, 0x58(%rsp)
testq %rax, %rax
js 0x5d8f
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 0x5d53
movq %rbx, %rdi
callq 0x42b0
movq 0x50(%rsp), %r15
movslq 0x44(%rsp), %r12
jmp 0x5e5a
movq %rbx, %rdi
callq 0x4530
movl 0x38(%rsp), %edi
leaq 0xf78b(%rip), %rsi      # 0x15544
xorl %eax, %eax
callq 0x4200
jmp 0x5ea6
movq %rbx, %rdi
callq 0x4530
movl 0x38(%rsp), %edi
leaq 0xf8e5(%rip), %rsi      # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
movl 0x3c(%rsp), %edi
callq 0x46d0
jmp 0x5ea6
movl $0x20000, %edi          # imm = 0x20000
callq 0x44b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4530
movq $0x0, 0x50(%rsp)
movl 0x38(%rsp), %edi
movl $0x4000, %edx           # imm = 0x4000
movq %r14, %rsi
callq 0x4510
movq %rax, 0x58(%rsp)
testq %rax, %rax
js 0x5e48
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 0x5e0d
movq %rbx, %rdi
callq 0x42b0
movq 0x50(%rsp), %r15
movq 0x58(%rsp), %r12
movq %rbx, %rdi
callq 0x4530
cmpq %r12, %r15
jne 0x5e7d
movl 0x38(%rsp), %edi
leaq 0xf84b(%rip), %rsi      # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
jmp 0x5e8d
leaq 0xf6f3(%rip), %rsi      # 0x15577
movl %ebp, %edi
xorl %eax, %eax
callq 0x4200
movq %r14, %rdi
callq 0x4400
movl 0x3c(%rsp), %edi
callq 0x46d0
movq %rbx, %rdi
callq 0x42b0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq