// File: /workspace/github_c_makefile_star2_O3/niuyuling[P]CProxy/http_request.c
// Function: parse_request_head
void parse_request_head(char *http_request_line, struct http_request *http_request)
{
    char *p, *head, *m, *u;
    size_t head_len;
    int host_len, port_len, uri_len;
    host_len = port_len = uri_len = 0;

    p = strstr(http_request_line, "\r\n"); // 查找"\r\n"
    if (p == NULL) {
        return;
    }

    head_len = p - http_request_line;
    head = (char *)malloc(sizeof(char) * head_len + 1);
    if (head == NULL)
        free(head);
    memset(head, 0, sizeof(char) * head_len + 1);
    memcpy(head, http_request_line, head_len);

    http_request->method = (char *)malloc(sizeof(char) * 8);
    http_request->U = (char *)malloc(sizeof(char) * head_len);
    http_request->version = (char *)malloc(10);
    if (http_request->method == NULL)
        perror("malloc");
    if (http_request->U == NULL)
        perror("malloc");
    if (http_request->version == NULL)
        perror("malloc");
    memset(http_request->method, 0, 8);
    memset(http_request->U, 0, sizeof(char) * head_len);
    memset(http_request->version, 0, 10);

    m = strchr(head, ' ');
    http_request->method_len = m - head;
    memmove(http_request->method, head, http_request->method_len);

    u = strchr(m + 1, ' ');
    memmove(http_request->U, m + 1, u - (m + 1));
    http_request->U_len = u - (m + 1);

    memmove(http_request->version, u + 1, 8);
    http_request->version_len = 8;

    // 获取Host、Port长度
    get_http_host_port_len(http_request_line, &host_len, &port_len);

    // URI LENGTH
    char *_p0 = strstr(http_request->U, "http://");
    if (_p0) {                  // 标准头
        char *_p1 = strchr(http_request->U + 7, '/');
        char *_p2 = strchr(http_request->U + 7, '\0');
        if (_p1) {
            uri_len = _p2 - _p1;
        }
    } else {                    // 非标准头
        char *_p1 = strchr(http_request->U, '/');
        char *_p2 = strchr(http_request->U, '\0');
        if (_p1) {
            uri_len = _p2 - _p1;
        } else {
            uri_len = 1;        // 没有uri时
        }
    }

    http_request->host = (char *)malloc(sizeof(char) * host_len + 1);
    if (http_request->host == NULL)
        perror("malloc");
    http_request->port = (char *)malloc(sizeof(char) * port_len + 1);
    if (http_request->port == NULL)
        perror("malloc");
    http_request->url = (char *)malloc(sizeof(char) * http_request->U_len + 1);
    if (http_request->url == NULL)
        perror("malloc");
    http_request->uri = (char *)malloc(sizeof(char) * uri_len + 1);
    if (http_request->uri == NULL)
        perror("malloc");
    http_request->H = (char *)malloc(sizeof(char) * host_len + port_len + 1);
    if (http_request->H == NULL)
        perror("malloc");

    memset(http_request->host, 0, host_len + 1);
    memset(http_request->port, 0, port_len + 1);
    memset(http_request->url, 0, http_request->U_len + 1);
    memset(http_request->uri, 0, uri_len + 1);
    memset(http_request->H, 0, host_len + port_len + 1);
    if (extract_host(http_request_line, http_request->host, http_request->port) == -1)
        return;
    http_request->host_len = (int)strlen(http_request->host);
    http_request->port_len = (int)strlen(http_request->port);
    memcpy(http_request->H, http_request->host, http_request->host_len);
    strncat(http_request->H, ":", 2);
    strncat(http_request->H, http_request->port, http_request->port_len);
    memcpy(http_request->url, http_request->U, http_request->U_len);
    http_request->url_len = http_request->U_len;
    if (get_http_path(http_request->url, http_request->url_len, http_request->uri) == NULL)
        return;

    http_request->uri_len = uri_len;
    http_request->H_len = http_request->host_len + http_request->port_len + 1;

/* 
   // 调试
   printf("%s %d\n", http_request->method, http_request->method_len);
   printf("%s %d\n", http_request->U, http_request->U_len);
   printf("%s %d\n", http_request->version, http_request->version_len);
   printf("%s %d\n", http_request->host, http_request->host_len);
   printf("%s %d\n", http_request->port, http_request->port_len);
   printf("%s %d\n", http_request->H, http_request->H_len);
   printf("%s %d\n", http_request->url, http_request->url_len);
   printf("%s %d\n", http_request->uri, http_request->uri_len);
*/

    free(head);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
xorl %eax, %eax
movl %eax, (%rsp)
movl %eax, 0x4(%rsp)
leaq 0x4a1e(%rip), %rsi      # 0x9385
callq 0x2540
testq %rax, %rax
je 0x4c79
movq %rax, %r13
subq %r15, %r13
leaq 0x1(%r13), %rsi
movl $0x1, %edi
callq 0x2270
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x22e0
movl $0x8, %edi
callq 0x2330
movq %rax, %rbp
movq %rax, (%rbx)
movq %r13, %rdi
callq 0x2330
movq %rax, %r12
movq %rax, 0x8(%rbx)
movl $0xa, %edi
callq 0x2330
movq %rax, 0x10(%rbx)
testq %rbp, %rbp
je 0x4c93
testq %r12, %r12
je 0x4ca8
cmpq $0x0, 0x10(%rbx)
je 0x4cb9
movq (%rbx), %rax
xorl %r12d, %r12d
movq %r12, (%rax)
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq 0x10(%rbx), %rax
movq %r12, (%rax)
movw $0x0, 0x8(%rax)
movq %r14, %rdi
movl $0x20, %esi
callq 0x2160
movq %rax, %r13
subq %r14, %rax
movl %eax, 0x40(%rbx)
movq (%rbx), %rdi
movslq %eax, %rdx
movq %r14, 0x8(%rsp)
movq %r14, %rsi
callq 0x2430
incq %r13
movq %r13, %rdi
movl $0x20, %esi
callq 0x2160
movq %rax, %r14
movq 0x8(%rbx), %rdi
movq %rax, %rbp
subq %r13, %rbp
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2430
movl %ebp, 0x44(%rbx)
movq 0x10(%rbx), %rax
movq 0x1(%r14), %rcx
movq %rcx, (%rax)
movl $0x8, 0x48(%rbx)
leaq 0x4(%rsp), %rsi
movq %rsp, %rdx
movq %r15, %rdi
callq 0x47ee
movq 0x8(%rbx), %r13
leaq 0x4906(%rip), %rsi      # 0x938f
movq %r13, %rdi
callq 0x2540
testq %rax, %rax
je 0x4aae
addq $0x7, %r13
movq %r13, %rdi
movl $0x2f, %esi
callq 0x2160
testq %rax, %rax
jne 0x4ac4
jmp 0x4ad8
movq %r13, %rdi
movl $0x2f, %esi
callq 0x2160
testq %rax, %rax
je 0x4c88
movq %rax, %rbp
movq %r13, %rdi
callq 0x2140
movq %rax, %r12
addl %r13d, %r12d
subl %ebp, %r12d
movq %r15, 0x10(%rsp)
movslq 0x4(%rsp), %r14
leaq 0x1(%r14), %rbp
movq %rbp, %rdi
callq 0x2330
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x4cca
movslq (%rsp), %r15
leaq 0x1(%r15), %r13
movq %r13, %rdi
callq 0x2330
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4cdb
movslq 0x44(%rbx), %rdi
incq %rdi
callq 0x2330
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x4cec
movslq %r12d, %rdi
incq %rdi
callq 0x2330
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x4cfd
addq %r15, %rbp
movq %rbp, %rdi
callq 0x2330
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x4d0e
movq 0x18(%rbx), %rdi
leal 0x1(%r14), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x20(%rbx), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq 0x30(%rbx), %rdi
movslq 0x44(%rbx), %rdx
incq %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x38(%rbx), %rdi
leal 0x1(%r12), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x28(%rbx), %rdi
leal (%r14,%r15), %eax
incl %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x10(%rsp), %rdi
callq 0x4315
cmpl $-0x1, %eax
je 0x4c79
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x2140
movq %rax, %r15
movl %r15d, 0x4c(%rbx)
movq 0x20(%rbx), %rdi
callq 0x2140
movl %eax, 0x50(%rbx)
movq 0x28(%rbx), %rdi
movslq %r15d, %rdx
movq %r14, %rsi
callq 0x22e0
movq 0x28(%rbx), %r14
movq %r14, %rdi
callq 0x2140
movw $0x3a, (%r14,%rax)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdi
movslq 0x50(%rbx), %rdx
callq 0x21d0
movq 0x8(%rbx), %rsi
movq 0x30(%rbx), %rdi
movslq 0x44(%rbx), %rdx
callq 0x22e0
movl 0x44(%rbx), %esi
movl %esi, 0x58(%rbx)
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rdx
callq 0x4787
testq %rax, %rax
je 0x4c79
movl %r12d, 0x5c(%rbx)
movl 0x4c(%rbx), %eax
movl 0x50(%rbx), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x54(%rbx)
movq 0x8(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r12d
jmp 0x4ad8
leaq 0x46ee(%rip), %rdi      # 0x9388
callq 0x2460
movq 0x8(%rbx), %r12
jmp 0x49d0
leaq 0x46d9(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x49d9
leaq 0x46c8(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x49e4
leaq 0x46b7(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4afb
leaq 0x46a6(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4b18
leaq 0x4695(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4b31
leaq 0x4684(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4b49
leaq 0x4673(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4b61