// File: /workspace/github_c_makefile_star2_O3/niuyuling[P]CProxy/http_request.c
// Function: request_head
char *request_head(conn_t * in, conf * configure)
{
    char *result = NULL;
    char *delim = ",";
    char *saveptr = NULL;
    char *incomplete_head = NULL;
    int incomplete_head_len = 0;
    int return_val = 0;
    struct http_request *http_request;
    http_request = (struct http_request *)malloc(sizeof(struct http_request));
    memset(http_request, 0, sizeof(struct http_request));

    parse_request_head(in->incomplete_data, http_request);

    if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0) {
        sslEncodeCode = configure->https_encode;

        char https_del_copy[configure->https_del_len + 1];

        memset(remote_host, 0, CACHE_SIZE);
        if (configure->https_port > 0) {
            remote_port = configure->https_port;
        }
        if (configure->https_ip != NULL) {
            memcpy(remote_host, configure->https_ip, configure->https_ip_len);
        }

        incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
        if (incomplete_head == NULL) {
            free(incomplete_head);
            perror("malloc");
        }
        memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
        memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
        memmove(https_del_copy, configure->https_del, configure->https_del_len + 1);

        result = strtok_r(https_del_copy, delim, &saveptr);
        while (result != NULL) {
            delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
            result = strtok_r(NULL, delim, &saveptr);
        }

        splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->https_first, configure->https_first_len);

        incomplete_head_len = in->incomplete_data_len; // 更新HTTPS HEADER长度
        incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, https_head_strrep);
        incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, https_head_regrep);

        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->version, http_request->version_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->version, http_request->version_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);

        printf("%s", incomplete_head); // 打印HTTPS HEADER

        char *new_incomplete_data;
        new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
        if (new_incomplete_data == NULL) {
            free(in->incomplete_data);
            perror("realloc");
            return in->incomplete_data;
        }
        in->incomplete_data = new_incomplete_data;
        memset(in->incomplete_data, 0, incomplete_head_len + 1);                // 清空incomplete_data数据
        memmove(in->incomplete_data, incomplete_head, incomplete_head_len);     // 更新incomplete_data数据
        in->incomplete_data_len = incomplete_head_len;                          // 更新incomplete_data长度

        free(incomplete_head);                                                  // 释放incomplete_head内存
    }

    if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0) {
        sslEncodeCode = configure->http_encode;

        char http_del_copy[configure->http_del_len + 1];

        memset(remote_host, 0, CACHE_SIZE);
        if (configure->http_port > 0) {
            remote_port = configure->http_port;
        }
        if (configure->http_ip != NULL) {
            memcpy(remote_host, configure->http_ip, configure->http_ip_len);
        }

        incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
        if (incomplete_head == NULL) {
            perror("malloc");
            free(incomplete_head);
        }

        memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
        memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
        memmove(http_del_copy, configure->http_del, configure->http_del_len + 1);

        result = strtok_r(http_del_copy, delim, &saveptr);
        while (result != NULL) {
            delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
            result = strtok_r(NULL, delim, &saveptr);
        }

        splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->http_first, configure->http_first_len);
        incomplete_head_len = in->incomplete_data_len; // 更新HTTP HEADER长度
        incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, http_head_strrep);
        incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, http_head_regrep);

        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->version, http_request->version_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->version, http_request->version_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[url]", 5, http_request->url, http_request->url_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[uri]", 5, http_request->uri, http_request->uri_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
        incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);

        printf("%s", incomplete_head); // 打印HTTP HEADER

        char *new_incomplete_data;
        new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
        if (new_incomplete_data == NULL) {
            free(in->incomplete_data);
            perror("realloc");
            return in->incomplete_data;
        }
        in->incomplete_data = new_incomplete_data;
        memset(in->incomplete_data, 0, incomplete_head_len + 1);                // 清空incomplete_data数据
        memmove(in->incomplete_data, incomplete_head, incomplete_head_len);     // 更新incomplete_data数据
        in->incomplete_data_len = incomplete_head_len;                          // 更新incomplete_data长度

        free(incomplete_head);                                                  // 释放incomplete_head内存
    }

    free_http_request(http_request);
    free(http_request);
    return in->incomplete_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, -0x30(%rbp)
movq %rdi, %r15
movq $0x0, -0x50(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x2270
movq %rax, %r14
movq 0x8(%r15), %rdi
movq %rax, %rsi
callq 0x4943
movq 0x8(%r15), %rbx
leaq 0x4596(%rip), %rsi      # 0x9305
movl $0x7, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
jne 0x5041
movq %rsp, -0x38(%rbp)
movq -0x30(%rbp), %rbx
movl 0x7c(%rbx), %eax
leaq 0x148ce(%rip), %rcx     # 0x19664
movl %eax, (%rcx)
movl 0x74(%rbx), %eax
incl %eax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0x1479b(%rip), %rdi     # 0x19550
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x50(%rbx), %eax
testl %eax, %eax
jle 0x4dd1
leaq 0x14891(%rip), %rcx     # 0x19660
movl %eax, (%rcx)
movq 0x58(%rbx), %rsi
testq %rsi, %rsi
je 0x4dea
movslq 0x70(%rbx), %rdx
leaq 0x1476b(%rip), %rdi     # 0x19550
callq 0x22e0
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2270
movq %rax, %r13
testq %rax, %rax
je 0x53b2
movq %r14, -0x40(%rbp)
movq 0x8(%r15), %rsi
leaq 0x18(%r15), %rbx
movq %r15, -0x58(%rbp)
movslq 0x18(%r15), %rdx
movq %r13, %rdi
callq 0x2430
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rsi
movslq 0x74(%rax), %rdx
incq %rdx
movq %r12, %rdi
callq 0x2430
leaq 0x4558(%rip), %rsi      # 0x9397
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
callq 0x2230
testq %rax, %rax
je 0x4e7b
leaq 0x4540(%rip), %r12      # 0x9397
leaq -0x50(%rbp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x53c3
xorl %edi, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2230
testq %rax, %rax
jne 0x4e5b
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rdx
movl 0x78(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x549c
movq -0x58(%rbp), %r14
movl 0x18(%r14), %eax
leaq -0x44(%rbp), %r12
movl %eax, (%r12)
leaq 0x14650(%rip), %rax     # 0x194f8
movq (%rax), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x5554
leaq 0x14643(%rip), %rcx     # 0x19500
movq (%rcx), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x5597
movq -0x40(%rbp), %r13
movq (%r13), %rbx
movl 0x40(%r13), %r15d
leaq 0x44bb(%rip), %rdx      # 0x9399
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40c3
leaq 0x44a2(%rip), %rdx      # 0x939d
movq %rax, %rdi
movq %r12, %rsi
movl $0x8, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40c3
movq 0x8(%r13), %r8
movl 0x44(%r13), %r9d
leaq 0x4486(%rip), %rdx      # 0x93a6
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
callq 0x40c3
movq 0x10(%r13), %rbx
movl 0x48(%r13), %r15d
leaq 0x446b(%rip), %rdx      # 0x93aa
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40c3
leaq 0x4452(%rip), %rdx      # 0x93ae
movq %rax, %rdi
movq %r12, %rsi
movl $0x9, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40c3
movq 0x18(%r13), %r8
movl 0x4c(%r13), %r9d
leaq 0x4437(%rip), %rdx      # 0x93b8
movq %rax, %rdi
movq %r12, %rsi
movl $0x6, %ecx
callq 0x40c3
movq 0x20(%r13), %r8
movl 0x50(%r13), %r9d
leaq 0x441f(%rip), %rdx      # 0x93bf
movq %rax, %rdi
movq %r12, %rsi
movl $0x6, %ecx
callq 0x40c3
movq 0x28(%r13), %r8
movl 0x54(%r13), %r9d
leaq 0x4407(%rip), %rdx      # 0x93c6
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
callq 0x40c3
movq %rax, %rbx
leaq 0x40dd(%rip), %rdi      # 0x90b6
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq %r14, %r15
movq 0x8(%r14), %rdi
movl (%r12), %eax
movslq %eax, %r12
leaq 0x1(%r12), %r14
movq %r14, %rsi
callq 0x23d0
testq %rax, %rax
je 0x5382
movq %rax, %r13
movq %rax, 0x8(%r15)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x21b0
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2430
movl %r12d, 0x18(%r15)
movq %rbx, %rdi
callq 0x2030
movq -0x38(%rbp), %rsp
movq 0x8(%r15), %rbx
movq -0x40(%rbp), %r14
cmpb $0x47, (%rbx)
jne 0x5052
cmpb $0x45, 0x1(%rbx)
jne 0x5052
cmpb $0x54, 0x2(%rbx)
je 0x506e
leaq 0x42a7(%rip), %rsi      # 0x9300
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
jne 0x535f
movq %rsp, -0x38(%rbp)
movq -0x30(%rbp), %rbx
movl 0x4c(%rbx), %eax
leaq 0x145e4(%rip), %rcx     # 0x19664
movl %eax, (%rcx)
movl 0x44(%rbx), %eax
incl %eax
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq 0x144b1(%rip), %rdi     # 0x19550
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x20(%rbx), %eax
testl %eax, %eax
jle 0x50bb
leaq 0x145a7(%rip), %rcx     # 0x19660
movl %eax, (%rcx)
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0x50d4
movslq 0x40(%rbx), %rdx
leaq 0x14481(%rip), %rdi     # 0x19550
callq 0x22e0
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2270
movq %rax, %r12
testq %rax, %rax
je 0x53a1
movq %r14, -0x40(%rbp)
movq 0x8(%r15), %rsi
leaq 0x18(%r15), %rbx
movq %r15, -0x58(%rbp)
movslq 0x18(%r15), %rdx
movq %r12, %rdi
callq 0x2430
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rsi
movslq 0x44(%rax), %rdx
incq %rdx
movq %r13, %rdi
callq 0x2430
leaq 0x426e(%rip), %rsi      # 0x9397
leaq -0x50(%rbp), %rdx
movq %r13, %rdi
callq 0x2230
testq %rax, %rax
je 0x5165
leaq 0x4256(%rip), %r13      # 0x9397
leaq -0x50(%rbp), %r15
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x53c3
xorl %edi, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2230
testq %rax, %rax
jne 0x5145
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rdx
movl 0x48(%rax), %ecx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x549c
movq -0x58(%rbp), %r14
movl 0x18(%r14), %eax
leaq -0x44(%rbp), %r13
movl %eax, (%r13)
leaq 0x14356(%rip), %rax     # 0x194e8
movq (%rax), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x5554
leaq 0x14349(%rip), %rcx     # 0x194f0
movq (%rcx), %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x5597
movq -0x40(%rbp), %r15
movq (%r15), %rbx
movl 0x40(%r15), %r12d
leaq 0x41d2(%rip), %rdx      # 0x9399
movq %rax, %rdi
movq %r13, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40c3
leaq 0x41b9(%rip), %rdx      # 0x939d
movq %rax, %rdi
movq %r13, %rsi
movl $0x8, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40c3
movq 0x8(%r15), %r8
movl 0x44(%r15), %r9d
leaq 0x419d(%rip), %rdx      # 0x93a6
movq %rax, %rdi
movq %r13, %rsi
movl $0x3, %ecx
callq 0x40c3
movq 0x10(%r15), %rbx
movl 0x48(%r15), %r12d
leaq 0x4182(%rip), %rdx      # 0x93aa
movq %rax, %rdi
movq %r13, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40c3
leaq 0x4169(%rip), %rdx      # 0x93ae
movq %rax, %rdi
movq %r13, %rsi
movl $0x9, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40c3
movq 0x30(%r15), %r8
movl 0x58(%r15), %r9d
leaq 0x4168(%rip), %rdx      # 0x93d2
movq %rax, %rdi
movq %r13, %rsi
movl $0x5, %ecx
callq 0x40c3
movq 0x38(%r15), %r8
movl 0x5c(%r15), %r9d
leaq 0x414f(%rip), %rdx      # 0x93d8
movq %rax, %rdi
movq %r13, %rsi
movl $0x5, %ecx
callq 0x40c3
movq 0x18(%r15), %r8
movl 0x4c(%r15), %r9d
leaq 0x4110(%rip), %rdx      # 0x93b8
movq %rax, %rdi
movq %r13, %rsi
movl $0x6, %ecx
callq 0x40c3
movq 0x20(%r15), %r8
movl 0x50(%r15), %r9d
leaq 0x40f8(%rip), %rdx      # 0x93bf
movq %rax, %rdi
movq %r13, %rsi
movl $0x6, %ecx
callq 0x40c3
movq 0x28(%r15), %r8
movl 0x54(%r15), %r9d
leaq 0x40e0(%rip), %rdx      # 0x93c6
movq %rax, %rdi
movq %r13, %rsi
movl $0x3, %ecx
callq 0x40c3
movq %rax, %rbx
leaq 0x3db6(%rip), %rdi      # 0x90b6
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq %r14, %r15
movq 0x8(%r14), %rdi
movl (%r13), %eax
movslq %eax, %r14
leaq 0x1(%r14), %r12
movq %r12, %rsi
callq 0x23d0
testq %rax, %rax
je 0x5382
movq %rax, %r13
movq %rax, 0x8(%r15)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x21b0
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2430
movl %r14d, 0x18(%r15)
movq %rbx, %rdi
callq 0x2030
movq -0x38(%rbp), %rsp
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x48cd
movq %r14, %rdi
callq 0x2030
movq 0x8(%r15), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %rdi
callq 0x2030
leaq 0x4038(%rip), %rdi      # 0x93ca
callq 0x2460
movq 0x8(%r15), %rax
movq -0x38(%rbp), %rsp
jmp 0x5373
leaq 0x3fe0(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x50ef
leaq 0x3fcf(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4e05