// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1278, %rsp           # imm = 0x1278
movl (%rdi), %eax
movl %eax, 0x10(%rsp)
leaq 0x18689(%rip), %r15     # 0x1e7a0
leaq 0x186fa(%rip), %rbx     # 0x1e818
leaq 0x18723(%rip), %rbp     # 0x1e848
leaq 0x270(%rsp), %r13
leaq 0x40(%rsp), %r12
movq %r15, %rdi
callq 0x4530
movq 0x18717(%rip), %rax     # 0x1e858
cmpq %rax, 0x18730(%rip)     # 0x1e878
jne 0x6157
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4560
jmp 0x613a
movl (%rax), %r14d
movq %rbp, %rdi
callq 0x13f8c
movq %r15, %rdi
callq 0x42b0
xorl %ebx, %ebx
movslq %ebx, %rax
leaq (%rsp,%rax), %rsi
addq $0x270, %rsi            # imm = 0x270
movl $0xfff, %edx            # imm = 0xFFF
subq %rax, %rdx
movl %r14d, %edi
callq 0x4510
testq %rax, %rax
je 0x61a4
addl %eax, %ebx
cmpl $0xfff, %ebx            # imm = 0xFFF
jg 0x61a4
movslq %ebx, %rax
cmpb $0xa, -0x1(%rax,%r13)
jne 0x616c
movq %r12, %r15
movslq %ebx, %rax
movb $0x0, -0x1(%rax,%r13)
movq %r13, %rdi
leaq 0xf699(%rip), %rsi      # 0x15853
leaq 0x14(%rsp), %r13
movq %r13, %rdx
leaq 0x230(%rsp), %rbx
movq %rbx, %rcx
leaq 0x9e(%rsp), %r12
movq %r12, %r8
xorl %eax, %eax
callq 0x4340
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x519c
testb %al, %al
je 0x6414
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
leaq 0xf510(%rip), %rsi      # 0x15714
leaq 0x38(%rsp), %rdx
callq 0x4650
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x4b56
testb %al, %al
je 0x6277
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x4250
testq %rbx, %rbx
movq %r15, %r12
je 0x6295
movq %rbx, %rdi
callq 0x4190
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x43d0
leaq 0x1852b(%rip), %r15     # 0x1e7a0
jmp 0x62c3
leaq 0x1859a(%rip), %rbx     # 0x1e818
movq %r15, %r12
leaq 0x270(%rsp), %r13
leaq 0x18510(%rip), %r15     # 0x1e7a0
jmp 0x6132
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0xb8(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0xd8(%rsp,%rax), %esi
orl $0x1, %esi
callq 0x45f0
leaq 0x184dd(%rip), %r15     # 0x1e7a0
leaq 0xa8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4470
leaq 0x90(%rsp), %rax
movq %rax, 0x60(%rsp)
movq $0x1, 0x68(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0x80(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq 0x18(%rsp), %rsi
movq %r12, %rdi
leaq 0xf(%rsp), %rdx
callq 0x696c
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x13fda
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x6342
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4360
movq 0x70(%rsp), %rax
movb $0x1, %r13b
testq %rax, %rax
je 0x63ac
movl 0x14(%rsp), %esi
xorl %ecx, %ecx
cmpl $0x545550, %esi         # imm = 0x545550
setne %cl
xorl %edx, %edx
cmpl $0x544547, %esi         # imm = 0x544547
setne %dl
testl %ecx, %ecx
je 0x6373
testl %edx, %edx
jne 0x6377
jmp 0x6381
testl %edx, %edx
jne 0x6396
movq (%rax), %rax
testq %rax, %rax
jne 0x6369
jmp 0x63ac
leaq 0x270(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5eba
jmp 0x63a9
leaq 0x270(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5c25
xorl %r13d, %r13d
leaq 0x60(%rsp), %rdi
callq 0x6a0c
leaq 0xa8(%rsp), %rdi
movq 0x17bc3(%rip), %rsi     # 0x1df88
callq 0x4290
leaq 0x128(%rsp), %rdi
callq 0x4100
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x63f3
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4360
testb %r13b, %r13b
leaq 0x1844b(%rip), %rbp     # 0x1e848
leaq 0x18414(%rip), %rbx     # 0x1e818
leaq 0x270(%rsp), %r13
jne 0x6132
jmp 0x6425
leaq 0xf0cb(%rip), %rsi      # 0x154e6
movl %r14d, %edi
xorl %eax, %eax
callq 0x4200
xorl %eax, %eax
addq $0x1278, %rsp           # imm = 0x1278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6440
movq %rax, %rbx
jmp 0x6494
movq %rax, %rbx
jmp 0x6464
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x6464
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4360
leaq 0x60(%rsp), %rdi
callq 0x6a0c
jmp 0x6473
movq %rax, %rbx
movq 0x17b0e(%rip), %rsi     # 0x1df88
leaq 0xa8(%rsp), %rdi
callq 0x4290
leaq 0x128(%rsp), %rdi
callq 0x4100
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x64b0
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4360
movq %rbx, %rdi
callq 0x4610