// File: /workspace/github_cpp_makefile_star2_O2/Cristian-Castillo[P]Multithreaded-HTTP-Server/./httpserver_definitions.cpp
// Function: worker_thread(void*)
void *worker_thread(void *args){
    
    struct http_object *node_parser,get_http_content;
    node_parser = &get_http_content;

    int id = *((int *)args); 
	int clientSocket;
    


    while(LIVE){
           
        /* -----------------  Locking  Thread In ------------------------------*/
        clientSocket = -1;
		
		pthread_mutex_lock(&mutex);
		{
			while(items.empty()){
				pthread_cond_wait(&client_in_queue,&mutex);
			}
            /* Take 1 client out of the queue */
            clientSocket = items.front();
            items.pop();
		}
		pthread_mutex_unlock(&mutex);

       
        /* -----------------  UnLocking Thread In ---------------------------- */
    
        char buffer[HEADER_BUFFER];
        size_t bytes_read;
        int msgsize = 0;

        /* read the clients message */
        while((bytes_read = read(clientSocket,buffer+msgsize,sizeof(buffer)-msgsize-1)) > 0){
            msgsize += bytes_read;
            if(msgsize > HEADER_BUFFER-1 || buffer[msgsize-1] == '\n')break;
        }
        buffer[msgsize-1] = 0; /* null terminate message and remove the \n */

        char request[4],file[50],protocol[10];
        sscanf(buffer,"%s %s %s",request,file, protocol);
     
        node_parser->buffer = buffer;
        node_parser->protocol = protocol;
        node_parser->request = request;
        node_parser->file_name = file;
        node_parser->client_connect = clientSocket;
        node_parser->id = id;

        /* ------------------------ REGEX SECTION ---------------------------- */

        bool matcha = f_regex_overkill_parse(node_parser->request,node_parser->file_name,node_parser->protocol);
        
        if(matcha != 1){ f_void_400(node_parser->client_connect); return NULL;}

        /*---------------------------------------------------------------------*/

        /* Get the file name - delimiter dash */
     
        char *theFile = file;
        char *restOfString = theFile;
        char *file_name = strtok_r(restOfString,"/", &restOfString);
        node_parser->file_name = file_name;
        node_parser->buffer = buffer;
        node_parser->client_connect = clientSocket;
        

        /* Master Module Check Box - Thread Dies on failure on failure */
        bool die_on_failure = f_bool_check_vitality(node_parser->file_name,node_parser->protocol,node_parser->request,node_parser->client_connect);
        
        if(die_on_failure == false){
            memset(node_parser,0,sizeof(http_object));
            continue;
        }
 
        /* Tokenizes my string please */
        std::string string_to_parse;
        std::stringstream ss;
        ss << node_parser->file_name;
        ss >> string_to_parse;
        
        /* Fancy hashing */
        std::unordered_map<std::string,int>hashing;

        /* hash id [file n] = id & convert too c-str */
        hashing[string_to_parse.c_str()] = node_parser->id;
        
        /* initialize iterator to search up valid existing id keys */
        std::unordered_map<std::string, int>::iterator lookup;

        for(lookup = hashing.begin(); lookup != hashing.end(); lookup++){ 
            

            if(strcmp(node_parser->request,"PUT") == 0 && strcmp(node_parser->request,"GET") != 0){
                f_void_put_module(buffer,node_parser->file_name,node_parser->client_connect);
                memset(node_parser,0,sizeof(http_object));
                return NULL;
            }

            if(strcmp(node_parser->request,"GET") == 0 && strcmp(node_parser->request,"PUT") != 0){
                f_void_get_module(buffer,node_parser->file_name,node_parser->client_connect);
                memset(node_parser,0,sizeof(http_object));
                return NULL;
            }   
        }
    }
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1268, %rsp           # imm = 0x1268
movl (%rdi), %eax
movl %eax, 0xc(%rsp)
leaq 0x19962(%rip), %r14     # 0x1f7b0
leaq 0x199d3(%rip), %r15     # 0x1f828
leaq 0x260(%rsp), %r13
movq %r14, %rdi
callq 0x4570
movq 0x199fc(%rip), %rax     # 0x1f868
cmpq %rax, 0x19a15(%rip)     # 0x1f888
jne 0x5e82
movq %r15, %rdi
movq %r14, %rsi
callq 0x45a0
jmp 0x5e65
movl (%rax), %ebx
leaq 0x199cd(%rip), %rdi     # 0x1f858
callq 0x12166
movq %r14, %rdi
callq 0x42e0
xorl %ebp, %ebp
movslq %ebp, %rax
leaq (%rsp,%rax), %rsi
addq $0x260, %rsi            # imm = 0x260
movl $0xfff, %edx            # imm = 0xFFF
subq %rax, %rdx
movl %ebx, %edi
callq 0x4550
testq %rax, %rax
je 0x5ed1
addl %eax, %ebp
cmpl $0xfff, %ebp            # imm = 0xFFF
jg 0x5ed1
movslq %ebp, %rax
cmpb $0xa, -0x1(%rax,%r13)
jne 0x5e9a
movslq %ebp, %rax
movb $0x0, -0x1(%rax,%r13)
movq %r13, %rdi
leaq 0xd96f(%rip), %rsi      # 0x13853
leaq 0x8(%rsp), %r12
movq %r12, %rdx
leaq 0xa0(%rsp), %rbp
movq %rbp, %rcx
leaq 0x76(%rsp), %r13
movq %r13, %r8
xorl %eax, %eax
callq 0x4370
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4f04
testb %al, %al
je 0x6083
movq %rbp, 0x30(%rsp)
movq %rbp, %rdi
leaq 0xd7e9(%rip), %rsi      # 0x13714
leaq 0x30(%rsp), %rdx
callq 0x4690
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
movl %ebx, %ecx
callq 0x495a
testb %al, %al
je 0x5e55
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x4280
leaq 0x260(%rsp), %r13
leaq 0x80(%rsp), %r12
leaq 0xe8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4380
leaq 0xd8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x44a0
leaq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x1, 0x40(%rsp)
xorps %xmm0, %xmm0
leaq 0x48(%rsp), %rcx
movups %xmm0, (%rcx)
movl $0x3f800000, 0x58(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq 0x10(%rsp), %rsi
movq %r12, %rdi
leaq 0x7(%rsp), %rdx
callq 0x64ea
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x121bc
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movq %r12, %rdi
callq 0x41b0
leaq 0x48(%rsp), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x6057
xorl %eax, %eax
cmpl $0x545550, 0x8(%rsp)    # imm = 0x545550
setne %cl
jne 0x6022
cmpl $0x544547, 0x8(%rsp)    # imm = 0x544547
jne 0x604a
movb %cl, %al
xorl %ecx, %ecx
cmpl $0x544547, 0x8(%rsp)    # imm = 0x544547
setne %dl
testl %eax, %eax
je 0x6000
movb %dl, %cl
testl %ecx, %ecx
jne 0x6000
movq %r13, %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0x5bec
jmp 0x6057
movq %r13, %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0x598f
leaq 0x38(%rsp), %rdi
callq 0x65a8
leaq 0xd8(%rsp), %rdi
callq 0x42c0
leaq 0x10(%rsp), %rdi
callq 0x41b0
testq %r12, %r12
je 0x5e5d
jmp 0x608a
movl %ebx, %edi
callq 0x4ad1
xorl %eax, %eax
addq $0x1268, %rsp           # imm = 0x1268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x60a5
movq %rax, %rbx
jmp 0x60d6
movq %rax, %rbx
jmp 0x60ba
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x41b0
leaq 0x38(%rsp), %rdi
callq 0x65a8
jmp 0x60c9
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x42c0
leaq 0x10(%rsp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x4650