// File: /workspace/github_c_makefile_star2_O1/sszllzss[P]webSocket_Libevent_ThreadPool/ev_httpd.cpp
// Function: accept_request(httpChilent_t*)
int accept_request(struct httpChilent_t * client)
{
    size_t numchars;
    char * buf;
    size_t i, j;
    struct evbuffer * evbuff = NULL;
    if(client == NULL)
        return REQUEST_ERROR;
    pthread_mutex_lock(&client->lock);
    gettimeofday(&client->final_optime, NULL);
    evbuff = client->receive_evbuff; 
    evbuffer_lock(client->receive_evbuff);
    buf = evbuffer_readln(evbuff, &numchars, EVBUFFER_EOL_CRLF);
    evbuffer_unlock(client->receive_evbuff);
    if(buf == NULL)
    {
        pthread_mutex_unlock(&client->lock);
        return REQUEST_NOCONTENT;
    }
    client->request.method = (char *)malloc(METHOD_MAX_LEN + 1);
    client->request.url = (char *)malloc(URL_MAX_LEN + 1);
    client->request.http_version = (char *)malloc(HTTPVERSION_MAX_LEN + 1);
    if(client->request.method == NULL || client->request.url == NULL || client->request.http_version == NULL)
    {
        pthread_mutex_unlock(&client->lock);
        return REQUEST_INTERNAL;
    }


    i = 0; j = 0;
    while (!ISspace(buf[j]) && (i < METHOD_MAX_LEN) && (j < numchars))
    {
        client->request.method[i] = buf[j];
        i++; j++;
    }
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }


    client->request.method[i] = '\0';

    if (strcasecmp(client->request.method, "GET") && strcasecmp(client->request.method, "POST"))
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_NOTIMPLEMENTED;
    }




    i = 0;
    while (ISspace(buf[j]) && (j < numchars) )
        j++;
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    while (!ISspace(buf[j]) && (i < URL_MAX_LEN) && (j < numchars))
    {
        client->request.url[i] = buf[j];
        i++; j++;
    }
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }


    client->request.url[i] = '\0';

    if (strcasecmp(client->request.method, "GET") == 0)
    {
        client->request.query_string = client->request.url;
        while ((*client->request.query_string != '?') && (*client->request.query_string != '\0'))
            client->request.query_string++;
        if (*client->request.query_string == '?')
        {
            *client->request.query_string = '\0';
            client->request.query_string++;
        }
    }
    else
    {
        if(strstr(client->request.url, "?"))
        {
            http_request_free(&client->request);
            pthread_mutex_unlock(&client->lock);
            return REQUEST_BADREQUEST;        
        }
    }



    i = 0;
    while (ISspace(buf[j]) && (j < numchars) )
        j++;
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    while (!ISspace(buf[j]) && (i < HTTPVERSION_MAX_LEN) && (j < numchars))
    {
        client->request.http_version[i] = buf[j];
        i++; j++;
    }
    if(strlen(client->request.http_version) < 8)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    free(buf);
    while(1)
    {
        evbuffer_lock(client->receive_evbuff);
        buf = evbuffer_readln(evbuff, &numchars, EVBUFFER_EOL_CRLF);
        evbuffer_unlock(client->receive_evbuff);
        if(buf == NULL)
            break;
        else if(strcasecmp(client->request.method, "POST") == 0 &&  numchars == 0)
        {
            client->request.request_data_len = evbuffer_get_length(evbuff);
            client->request.request_data = (char *)malloc(client->request.request_data_len);
            if(client->request.request_data == NULL)
            {
                http_request_free(&client->request);
                pthread_mutex_unlock(&client->lock);
                return REQUEST_INTERNAL;        
            }
            evbuffer_lock(client->receive_evbuff);
            evbuffer_remove(evbuff, client->request.request_data, client->request.request_data_len);
            evbuffer_unlock(client->receive_evbuff);
            break;
        }
        else if(numchars == 0)
        {
            break;
        }
        if( client->request.HeadParameter == NULL)
            client->request.HeadParameter = new HeadParameterMap();
        char * key = buf;
        char * v = strchr(buf,':');
        if(v == NULL)
        {
            http_request_free(&client->request);
            pthread_mutex_unlock(&client->lock);
            return REQUEST_BADREQUEST;        
        }
        *v = '\0';
        v++;
        i = 0;j = 0;
        while (ISspace(*key) && (j < numchars) )
        {
            j++;
            key++;
        }
        while (!ISspace(key[i]) && (i < strlen(key)))
        {
            i++; j++;
        }
        key[i] = '\0';
        i = 0;j = 0;
        numchars = v - buf;
        while (ISspace(*v) && (j < numchars) )
        {
            j++;
            v++;
        }
        while (!ISspace(v[i]) && (i < strlen(v)))
        {
            i++; j++;
        }
        v[i] = '\0';

        client->request.HeadParameter->insert(std::pair<STRING, STRING>(key, v));    
    }

    if(strcasecmp(client->request.method, "POST") == 0 && client->request.request_data == NULL)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    pthread_mutex_unlock(&client->lock);

    return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
testq %rdi, %rdi
je 0x6486
movq %rdi, %rbx
callq 0x3620
leaq 0xb8(%rbx), %rdi
xorl %esi, %esi
callq 0x3690
movq 0x98(%rbx), %r14
movq %r14, %rdi
callq 0x32d0
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0x34b0
movq %rax, %r12
movq 0x98(%rbx), %rdi
callq 0x3780
testq %r12, %r12
je 0x6490
movl $0x7e, %edi
callq 0x35a0
movq %rax, %r13
movq %rax, 0x40(%rbx)
movl $0x7e, %edi
callq 0x35a0
movq %rax, %r15
movq %rax, 0x50(%rbx)
movl $0xb, %edi
callq 0x35a0
movq %rax, 0x58(%rbx)
testq %r13, %r13
je 0x6474
testq %r15, %r15
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x64a2
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
jmp 0x6683
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x6683
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x6683
movq %r14, 0x28(%rsp)
leaq 0x40(%rbx), %r13
movb (%r12), %r14b
movsbl %r14b, %edi
callq 0x30a0
testl %eax, %eax
sete %al
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x6505
xorl %r15d, %r15d
movq (%r13), %rax
movb %r14b, (%rax,%r15)
leaq 0x1(%r15), %rbp
movb 0x1(%r12,%r15), %r14b
movsbl %r14b, %edi
callq 0x30a0
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x6507
cmpq $0x7b, %r15
ja 0x6507
movq %rbp, %r15
cmpq %rcx, %rbp
jb 0x64d2
jmp 0x6507
xorl %ebp, %ebp
cmpq %rcx, %rbp
jne 0x6514
movq %r13, %rdi
jmp 0x6671
movq (%r13), %rax
movb $0x0, (%rax,%rbp)
movq %r13, 0x10(%rsp)
movq (%r13), %r13
leaq 0x656f(%rip), %rsi      # 0xca9b
movq %r13, %rdi
callq 0x34a0
testl %eax, %eax
je 0x6567
leaq 0x6560(%rip), %rsi      # 0xca9f
movq %r13, %rdi
callq 0x34a0
testl %eax, %eax
je 0x6567
movq 0x10(%rsp), %rdi
callq 0x6a06
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffa, %eax       # imm = 0xFFFFFFFA
jmp 0x6683
movq 0x8(%rsp), %r15
movsbl (%r12,%rbp), %edi
callq 0x30a0
leaq 0x1(%rbp), %r14
testl %eax, %eax
je 0x6586
cmpq %r15, %rbp
movq %r14, %rbp
jb 0x656c
incq %r15
cmpq %r14, %r15
je 0x666c
leaq -0x1(%r14), %r13
movb -0x1(%r12,%r14), %r15b
movsbl %r15b, %edi
callq 0x30a0
xorl %ebp, %ebp
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x65f5
cmpq %rcx, %r13
jae 0x65f5
xorl %r13d, %r13d
movq 0x50(%rbx), %rax
movb %r15b, (%rax,%r13)
leaq 0x1(%r13), %rbp
leaq (%r12,%r13), %rax
movb (%r14,%rax), %r15b
movsbl %r15b, %edi
callq 0x30a0
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x65ee
cmpq $0x7b, %r13
ja 0x65ee
addq %r14, %r13
cmpq %rcx, %r13
movq %rbp, %r13
jb 0x65b7
leaq (%r14,%rbp), %r13
decq %r13
cmpq %rcx, %r13
je 0x666c
movq 0x50(%rbx), %rax
movb $0x0, (%rax,%rbp)
movq 0x40(%rbx), %rdi
leaq 0x648e(%rip), %rsi      # 0xca9b
callq 0x34a0
movq 0x50(%rbx), %rdi
testl %eax, %eax
je 0x662e
movl $0x3f, %esi
callq 0x31c0
testq %rax, %rax
jne 0x666c
jmp 0x6645
incq %rdi
movq %rdi, 0x48(%rbx)
movzbl (%rdi), %eax
testl %eax, %eax
je 0x6645
cmpl $0x3f, %eax
jne 0x662b
movb $0x0, (%rdi)
incq 0x48(%rbx)
movq 0x8(%rsp), %r15
movsbl (%r12,%r13), %edi
callq 0x30a0
leaq 0x1(%r13), %r14
testl %eax, %eax
je 0x6664
cmpq %r15, %r13
movq %r14, %r13
jb 0x664a
incq %r15
cmpq %r14, %r15
jne 0x6692
movq 0x10(%rsp), %rdi
callq 0x6a06
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb -0x1(%r12,%r14), %bpl
movsbl %bpl, %edi
callq 0x30a0
testl %eax, %eax
movq 0x10(%rsp), %r13
jne 0x66e8
leaq -0x1(%r14), %rax
cmpq 0x8(%rsp), %rax
jae 0x66e8
xorl %r15d, %r15d
movq 0x58(%rbx), %rax
movb %bpl, (%rax,%r15)
leaq (%r12,%r15), %rax
movb (%r14,%rax), %bpl
movsbl %bpl, %edi
callq 0x30a0
testl %eax, %eax
jne 0x66e8
cmpq $0x8, %r15
ja 0x66e8
leaq (%r14,%r15), %rax
incq %r15
cmpq 0x8(%rsp), %rax
jb 0x66b7
movq 0x58(%rbx), %rdi
callq 0x31f0
cmpq $0x7, %rax
jbe 0x650c
movq %r12, %rdi
callq 0x3500
movq 0x28(%rsp), %r14
movq 0x98(%rbx), %rdi
callq 0x32d0
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x34b0
movq %rax, %r12
movq 0x98(%rbx), %rdi
callq 0x3780
testq %r12, %r12
je 0x69b6
movq (%r13), %rdi
leaq 0x6356(%rip), %rsi      # 0xca9f
callq 0x34a0
testl %eax, %eax
sete %cl
movq 0x8(%rsp), %rax
testq %rax, %rax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x6974
testq %rax, %rax
je 0x69b6
cmpq $0x0, 0x60(%rbx)
jne 0x67a9
movl $0x30, %edi
callq 0x3440
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x60(%rbx)
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movl $0x3a, %esi
callq 0x31c0
movq %rax, %r15
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x683d
movq %r15, 0x30(%rsp)
movb $0x0, (%r15)
incq 0x18(%rsp)
movq 0x8(%rsp), %r14
movq 0x20(%rsp), %rbp
movsbl (%rbp), %edi
callq 0x30a0
testl %eax, %eax
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x682b
movl $0x1, %r15d
leaq (%r15,%rbp), %rax
movq %rax, 0x20(%rsp)
movsbl (%rbp,%r15), %edi
callq 0x30a0
leaq 0x1(%r15), %rcx
testl %eax, %eax
je 0x6825
cmpq %r14, %r15
movq %rcx, %r15
jb 0x6801
addq %rcx, %rbp
decq %rbp
movsbl (%rbp), %edi
callq 0x30a0
testl %eax, %eax
je 0x6852
xorl %r14d, %r14d
jmp 0x687c
movq %r13, %rdi
callq 0x6a06
movq %rbx, %rdi
callq 0x3310
jmp 0x6961
movq %rbp, %rdi
callq 0x31f0
movq %rax, %r13
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x6879
movsbl 0x1(%rbp,%r14), %edi
incq %r14
callq 0x30a0
testl %eax, %eax
je 0x6860
jmp 0x687c
movq %r13, %r14
movb $0x0, (%rbp,%r14)
movq 0x18(%rsp), %rbp
movq %rbp, %r14
subq %r12, %r14
movq %r14, 0x8(%rsp)
movsbl (%rbp), %edi
setne %r15b
callq 0x30a0
testl %eax, %eax
setne %al
andb %r15b, %al
cmpb $0x1, %al
jne 0x68e2
movl $0x1, %r15d
movq 0x10(%rsp), %r13
leaq (%r15,%rbp), %rax
movq %rax, 0x18(%rsp)
movsbl (%rbp,%r15), %edi
callq 0x30a0
leaq 0x1(%r15), %rcx
testl %eax, %eax
je 0x68da
cmpq %r14, %r15
movq %rcx, %r15
jb 0x68b6
addq %rcx, %rbp
decq %rbp
jmp 0x68e7
movq 0x10(%rsp), %r13
movsbl (%rbp), %edi
callq 0x30a0
testl %eax, %eax
je 0x68f9
xorl %r14d, %r14d
jmp 0x6923
movq %rbp, %rdi
callq 0x31f0
movq %rax, %r12
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x6920
movsbl 0x1(%rbp,%r14), %edi
incq %r14
callq 0x30a0
testl %eax, %eax
je 0x6907
jmp 0x6923
movq %r12, %r14
movb $0x0, (%rbp,%r14)
movq 0x60(%rbx), %r12
leaq 0x38(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x7dd2
movq %r12, %rdi
movq %r14, %rsi
callq 0x8268
movq %r14, %rdi
callq 0x7e24
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r15
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
testq %r15, %r15
jne 0x6708
jmp 0x6683
movq %r14, %rdi
callq 0x3490
movl %eax, 0x70(%rbx)
movl %eax, %edi
callq 0x35a0
movq %rax, 0x68(%rbx)
testq %rax, %rax
je 0x69e4
movq 0x98(%rbx), %rdi
callq 0x32d0
movq 0x68(%rbx), %rsi
movl 0x70(%rbx), %edx
movq %r14, %rdi
callq 0x3540
movq 0x98(%rbx), %rdi
callq 0x3780
movq (%r13), %rdi
leaq 0x60de(%rip), %rsi      # 0xca9f
callq 0x34a0
testl %eax, %eax
jne 0x69d5
cmpq $0x0, 0x68(%rbx)
je 0x650c
movq %rbx, %rdi
callq 0x3310
xorl %eax, %eax
jmp 0x6683
movq %r13, %rdi
callq 0x6a06
jmp 0x6474
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7e24
movq %rbx, %rdi
callq 0x3740