// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
leaq 0x1957f(%rip), %r15     # 0x1f7a0
leaq 0x195f0(%rip), %rbx     # 0x1f818
leaq 0x270(%rsp), %rbp
leaq 0xf(%rsp), %r13
movq %r15, %rdi
callq 0x4530
movq 0x19614(%rip), %rax     # 0x1f858
cmpq %rax, 0x1962d(%rip)     # 0x1f878
jne 0x625a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4560
jmp 0x623d
movl (%rax), %r14d
leaq 0x195e4(%rip), %rdi     # 0x1f848
callq 0x140e2
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 0x62aa
addl %eax, %ebx
cmpl $0xfff, %ebx            # imm = 0xFFF
jg 0x62aa
movslq %ebx, %rax
cmpb $0xa, -0x1(%rax,%rbp)
jne 0x6273
movslq %ebx, %rax
movb $0x0, -0x1(%rax,%rbp)
movq %rbp, %rdi
leaq 0xf597(%rip), %rsi      # 0x15853
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 0x519d
testb %al, %al
je 0x6379
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
leaq 0xf413(%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
movl $0x2, %r13d
testb %al, %al
je 0x64f4
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
leaq 0x40(%rsp), %r12
je 0x6395
movq %rbx, %rdi
callq 0x4190
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x43d0
jmp 0x63bc
movl %r14d, %edi
leaq 0xf163(%rip), %rsi      # 0x154e6
xorl %eax, %eax
callq 0x4200
movl $0x1, %r13d
jmp 0x64f4
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 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 0x13(%rsp), %rdx
callq 0x6a82
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x14130
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x643b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4360
movq 0x70(%rsp), %rax
xorl %r13d, %r13d
testq %rax, %rax
je 0x64ad
xorl %ecx, %ecx
cmpl $0x545550, 0xf(%rsp)    # imm = 0x545550
setne %cl
testl %ecx, %ecx
jne 0x6463
cmpl $0x544547, 0xf(%rsp)    # imm = 0x544547
jne 0x6499
testl %ecx, %ecx
sete %dl
cmpl $0x544547, 0xf(%rsp)    # imm = 0x544547
setne %sil
orb %dl, %sil
je 0x6483
movq (%rax), %rax
testq %rax, %rax
jne 0x6455
jmp 0x64ad
movl $0x1, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5fa8
jmp 0x64ad
movl $0x1, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5d19
leaq 0x60(%rsp), %rdi
callq 0x6b1a
leaq 0xa8(%rsp), %rdi
movq 0x18ac2(%rip), %rsi     # 0x1ef88
callq 0x4290
leaq 0x128(%rsp), %rdi
callq 0x4100
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x64f4
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4360
testb $0x1, %r13b
leaq 0xf(%rsp), %r13
leaq 0x19314(%rip), %rbx     # 0x1f818
je 0x6235
xorl %eax, %eax
addq $0x1278, %rsp           # imm = 0x1278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6546
movq %rax, %rbx
jmp 0x6579
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x6549
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4360
jmp 0x6549
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x6b1a
jmp 0x6558
movq %rax, %rbx
movq 0x18a29(%rip), %rsi     # 0x1ef88
leaq 0xa8(%rsp), %rdi
callq 0x4290
leaq 0x128(%rsp), %rdi
callq 0x4100
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6595
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4360
movq %rbx, %rdi
callq 0x4610