// File: /workspace/github_c_makefile_star2_O3/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 0x64f7
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 0x6501
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 0x64e5
testq %r15, %r15
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x6513
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
jmp 0x6607
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x6607
movq %rbx, %rdi
callq 0x3310
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x6607
movq %r14, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x40(%rbx), %r15
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 0x6579
xorl %ebx, %ebx
movq (%r15), %rax
movb %r14b, (%rax,%rbx)
leaq 0x1(%rbx), %rbp
movb 0x1(%r12,%rbx), %r14b
movsbl %r14b, %edi
callq 0x30a0
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x657b
cmpq $0x7b, %rbx
ja 0x657b
movq %rbp, %rbx
cmpq %rcx, %rbp
jb 0x6547
jmp 0x657b
xorl %ebp, %ebp
cmpq %rcx, %rbp
je 0x65f0
movq (%r15), %rax
movb $0x0, (%rax,%rbp)
movq (%r15), %r13
leaq 0x650a(%rip), %rsi      # 0xca9b
movq %r13, %rdi
callq 0x34a0
testl %eax, %eax
je 0x65c9
leaq 0x64fb(%rip), %rsi      # 0xca9f
movq %r13, %rdi
callq 0x34a0
testl %eax, %eax
je 0x65c9
movq %r15, %rdi
callq 0x6a62
movq 0x10(%rsp), %rdi
callq 0x3310
movl $0xfffffffa, %eax       # imm = 0xFFFFFFFA
jmp 0x6607
movq 0x8(%rsp), %r13
decq %rbp
movb 0x1(%r12,%rbp), %r14b
incq %rbp
movsbl %r14b, %edi
callq 0x30a0
testl %eax, %eax
je 0x65eb
cmpq %r13, %rbp
jb 0x65d1
cmpq %rbp, %r13
jne 0x6616
movq %r15, %rdi
callq 0x6a62
movq 0x10(%rsp), %rdi
callq 0x3310
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
testl %eax, %eax
jne 0x6672
cmpq %r13, %rbp
jae 0x6672
movq %r15, 0x18(%rsp)
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movb %r14b, (%rax,%r15)
leaq 0x1(%r15), %rbx
leaq (%r12,%r15), %rax
movb 0x1(%rbp,%rax), %r14b
movsbl %r14b, %edi
callq 0x30a0
movq 0x8(%rsp), %r13
testl %eax, %eax
jne 0x666a
cmpq $0x7b, %r15
ja 0x666a
leaq (%r15,%rbp), %rax
incq %rax
movq %rbx, %r15
cmpq %r13, %rax
jb 0x6629
addq %rbx, %rbp
movq 0x18(%rsp), %r15
cmpq %r13, %rbp
movq 0x10(%rsp), %r14
je 0x66ab
movq 0x50(%r14), %rax
movb $0x0, (%rax,%rbx)
movq 0x40(%r14), %rdi
leaq 0x640c(%rip), %rsi      # 0xca9b
callq 0x34a0
movq 0x50(%r14), %rdi
testl %eax, %eax
je 0x66be
movl $0x3f, %esi
callq 0x31c0
testq %rax, %rax
je 0x66d5
movq %r15, %rdi
callq 0x6a62
movq %r14, %rdi
jmp 0x65fd
incq %rdi
movq %rdi, 0x48(%r14)
movzbl (%rdi), %eax
testl %eax, %eax
je 0x66d5
cmpl $0x3f, %eax
jne 0x66bb
movb $0x0, (%rdi)
incq 0x48(%r14)
movq %r15, 0x18(%rsp)
movq 0x8(%rsp), %rbx
movq %rbp, %r15
movb (%r12,%rbp), %r14b
movsbl %r14b, %edi
callq 0x30a0
incq %rbp
testl %eax, %eax
je 0x66fb
cmpq %rbx, %r15
jb 0x66df
leaq 0x1(%rbx), %rcx
cmpq %rbp, %rcx
jne 0x670e
movq 0x18(%rsp), %rdi
jmp 0x65f3
testl %eax, %eax
jne 0x6751
cmpq %rbx, %r15
jae 0x6751
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rax
movb %r14b, (%rax,%r15)
leaq (%r12,%r15), %rax
movb (%rbp,%rax), %r14b
movsbl %r14b, %edi
callq 0x30a0
testl %eax, %eax
jne 0x6751
cmpq $0x8, %r15
ja 0x6751
leaq (%r15,%rbp), %rax
incq %r15
cmpq 0x8(%rsp), %rax
jb 0x671a
movq 0x10(%rsp), %r14
movq 0x58(%r14), %rdi
callq 0x31f0
cmpq $0x7, %rax
ja 0x676f
movq 0x18(%rsp), %rdi
jmp 0x66ae
movq %r12, %rdi
callq 0x3500
movq 0x98(%r14), %rdi
callq 0x32d0
leaq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdi
movl $0x1, %edx
callq 0x34b0
movq %rax, %rbp
movq 0x98(%r14), %rdi
callq 0x3780
testq %rbp, %rbp
movq 0x18(%rsp), %r15
je 0x6a10
movq (%r15), %rdi
leaq 0x62e1(%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 0x69c7
testq %rax, %rax
je 0x6a10
cmpq $0x0, 0x60(%r14)
jne 0x681a
movl $0x30, %edi
callq 0x3440
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x60(%r14)
movq %rbp, 0x30(%rsp)
movq %rbp, %rdi
movl $0x3a, %esi
callq 0x31c0
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x66ab
movb $0x0, (%rax)
incq 0x28(%rsp)
movq 0x8(%rsp), %r14
movq 0x30(%rsp), %r12
movsbl (%r12), %r13d
movl %r13d, %edi
callq 0x30a0
testl %eax, %eax
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x689c
movl $0x1, %ebx
leaq (%r12,%rbx), %rax
movq %rax, 0x30(%rsp)
movsbl (%r12,%rbx), %r13d
movl %r13d, %edi
callq 0x30a0
leaq 0x1(%rbx), %rcx
testl %eax, %eax
je 0x6896
cmpq %r14, %rbx
movq %rcx, %rbx
jb 0x6870
addq %rcx, %r12
decq %r12
movl %r13d, %edi
callq 0x30a0
testl %eax, %eax
je 0x68ac
xorl %ebx, %ebx
jmp 0x68d5
movq %r12, %rdi
callq 0x31f0
movq %rax, %r13
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x68d2
movsbl 0x1(%r12,%rbx), %edi
incq %rbx
callq 0x30a0
testl %eax, %eax
je 0x68b9
jmp 0x68d5
movq %r13, %rbx
movb $0x0, (%r12,%rbx)
movq 0x28(%rsp), %r12
movq %r12, %r14
subq %rbp, %r14
movq %r14, 0x8(%rsp)
movsbl (%r12), %ebp
setne %bl
movl %ebp, %edi
callq 0x30a0
testl %eax, %eax
setne %al
andb %bl, %al
cmpb $0x1, %al
jne 0x6934
movl $0x1, %ebx
leaq (%r12,%rbx), %rax
movq %rax, 0x28(%rsp)
movsbl (%r12,%rbx), %ebp
movl %ebp, %edi
callq 0x30a0
leaq 0x1(%rbx), %rcx
testl %eax, %eax
je 0x692e
cmpq %r14, %rbx
movq %rcx, %rbx
jb 0x6909
addq %rcx, %r12
decq %r12
movl %ebp, %edi
callq 0x30a0
testl %eax, %eax
movq 0x10(%rsp), %r14
je 0x694d
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
jmp 0x697b
movq %r12, %rdi
callq 0x31f0
movq %rax, %r13
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
cmpq %rbx, %r13
je 0x6978
movsbl 0x1(%r12,%rbx), %edi
incq %rbx
callq 0x30a0
testl %eax, %eax
je 0x695f
jmp 0x697b
movq %r13, %rbx
movb $0x0, (%r12,%rbx)
movq 0x60(%r14), %r12
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x7d6e
movq %r12, %rdi
movq %rbx, %rsi
callq 0x8204
movq %rbx, %rdi
callq 0x7dc0
movq 0x98(%r14), %rdi
callq 0x32d0
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
jmp 0x678d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x3490
movl %eax, 0x70(%r14)
movl %eax, %edi
callq 0x35a0
movq %rax, 0x68(%r14)
testq %rax, %rax
je 0x6a3d
movq 0x98(%r14), %rdi
callq 0x32d0
movq 0x68(%r14), %rsi
movl 0x70(%r14), %edx
movq %rbx, %rdi
callq 0x3540
movq 0x98(%r14), %rdi
callq 0x3780
movq (%r15), %rdi
leaq 0x6085(%rip), %rsi      # 0xca9f
callq 0x34a0
testl %eax, %eax
jne 0x6a2e
cmpq $0x0, 0x68(%r14)
je 0x66ab
movq %r14, %rdi
callq 0x3310
xorl %eax, %eax
jmp 0x6607
movq %r15, %rdi
callq 0x6a62
movq %r14, %rdi
jmp 0x64e8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7dc0
movq %rbx, %rdi
callq 0x3740