// File: /workspace/github_c_makefile_star2_O2/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, %r14
andq $0x0, -0x58(%rbp)
movq %rdi, %r13
andl $0x0, -0x2c(%rbp)
pushq $0x1
popq %rdi
pushq $0x60
popq %rsi
callq 0x2270
movq 0x8(%r13), %rdi
movq %rax, -0x50(%rbp)
movq %rax, %rsi
callq 0x471e
movq 0x8(%r13), %rbx
leaq 0x47d3(%rip), %rsi      # 0x9305
pushq $0x7
popq %rdx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
movq %r13, -0x40(%rbp)
jne 0x4ddf
movq %rsp, -0x38(%rbp)
movl 0x7c(%r14), %eax
leaq 0x14b0c(%rip), %rcx     # 0x19664
movl %eax, (%rcx)
movl 0x74(%r14), %eax
incl %eax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0x149d8(%rip), %rdi     # 0x19550
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x50(%r14), %eax
testl %eax, %eax
jle 0x4b95
leaq 0x14acd(%rip), %rcx     # 0x19660
movl %eax, (%rcx)
movq 0x58(%r14), %rsi
testq %rsi, %rsi
je 0x4bae
movslq 0x70(%r14), %rdx
leaq 0x149a7(%rip), %rdi     # 0x19550
callq 0x22e0
pushq $0x1
popq %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2270
movq %rax, %r13
testq %rax, %rax
je 0x5136
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x18(%rax), %rbx
movslq 0x18(%rax), %rdx
movq %r13, %rdi
callq 0x2430
movq 0x60(%r14), %rsi
movq %r14, -0x48(%rbp)
movslq 0x74(%r14), %rdx
incq %rdx
movq %r12, %rdi
callq 0x2430
leaq 0x479a(%rip), %r15      # 0x9397
leaq -0x58(%rbp), %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2230
testq %rax, %rax
je 0x4c26
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5147
xorl %edi, %edi
jmp 0x4c04
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rdx
movl 0x78(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x521e
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
leaq -0x2c(%rbp), %r12
movl %eax, (%r12)
leaq 0x148a6(%rip), %rax     # 0x194f8
movq (%rax), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x52d4
leaq 0x14899(%rip), %rcx     # 0x19500
movq (%rcx), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x5314
movq -0x50(%rbp), %r15
movq (%r15), %rbx
movl 0x40(%r15), %r14d
leaq 0x4712(%rip), %rdx      # 0x9399
pushq $0x3
popq %r13
movq %rax, %rdi
movq %r12, %rsi
movl %r13d, %ecx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
leaq 0x46f7(%rip), %rdx      # 0x939d
pushq $0x8
popq %rcx
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
movq 0x8(%r15), %r8
movl 0x44(%r15), %r9d
leaq 0x46dd(%rip), %rdx      # 0x93a6
movq %rax, %rdi
movq %r12, %rsi
movl %r13d, %ecx
callq 0x3f39
movq 0x10(%r15), %rbx
movl 0x48(%r15), %r14d
leaq 0x46c4(%rip), %rdx      # 0x93aa
movq %rax, %rdi
movq %r12, %rsi
movl %r13d, %ecx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
leaq 0x46ad(%rip), %rdx      # 0x93ae
pushq $0x9
popq %rcx
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
movq 0x18(%r15), %r8
movl 0x4c(%r15), %r9d
leaq 0x4694(%rip), %rdx      # 0x93b8
pushq $0x6
popq %rbx
movq %rax, %rdi
movq %r12, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x20(%r15), %r8
movl 0x50(%r15), %r9d
leaq 0x467c(%rip), %rdx      # 0x93bf
movq %rax, %rdi
movq %r12, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x28(%r15), %r8
movl 0x54(%r15), %r9d
leaq 0x4667(%rip), %rdx      # 0x93c6
movq %rax, %rdi
movq %r12, %rsi
movl %r13d, %ecx
movq -0x40(%rbp), %r13
callq 0x3f39
movq %rax, %rbx
leaq 0x433b(%rip), %rdi      # 0x90b6
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq 0x8(%r13), %rdi
movl (%r12), %eax
movslq %eax, %r14
leaq 0x1(%r14), %r15
movq %r15, %rsi
callq 0x23d0
testq %rax, %rax
je 0x5106
movq %rax, %r12
movq %rax, 0x8(%r13)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21b0
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2430
movl %r14d, 0x18(%r13)
movq %rbx, %rdi
callq 0x2030
movq -0x38(%rbp), %rsp
movq 0x8(%r13), %rbx
movq -0x48(%rbp), %r14
cmpb $0x47, (%rbx)
jne 0x4df0
cmpb $0x45, 0x1(%rbx)
jne 0x4df0
cmpb $0x54, 0x2(%rbx)
je 0x4e0a
leaq 0x4509(%rip), %rsi      # 0x9300
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
jne 0x50df
movq %rsp, -0x38(%rbp)
movl 0x4c(%r14), %eax
leaq 0x1484b(%rip), %rcx     # 0x19664
movl %eax, (%rcx)
movl 0x44(%r14), %eax
incl %eax
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq 0x14717(%rip), %rdi     # 0x19550
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x20(%r14), %eax
testl %eax, %eax
jle 0x4e56
leaq 0x1480c(%rip), %rcx     # 0x19660
movl %eax, (%rcx)
movq 0x28(%r14), %rsi
testq %rsi, %rsi
je 0x4e6f
movslq 0x40(%r14), %rdx
leaq 0x146e6(%rip), %rdi     # 0x19550
callq 0x22e0
pushq $0x1
popq %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2270
movq %rax, %r12
testq %rax, %rax
je 0x5125
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x18(%rax), %rbx
movslq 0x18(%rax), %rdx
movq %r12, %rdi
callq 0x2430
movq 0x30(%r14), %rsi
movq %r14, -0x48(%rbp)
movslq 0x44(%r14), %rdx
incq %rdx
movq %r13, %rdi
callq 0x2430
leaq 0x44d9(%rip), %r15      # 0x9397
leaq -0x58(%rbp), %r14
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2230
testq %rax, %rax
je 0x4ee7
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5147
xorl %edi, %edi
jmp 0x4ec5
movq -0x48(%rbp), %rax
movq 0x38(%rax), %rdx
movl 0x48(%rax), %ecx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x521e
movq -0x40(%rbp), %r13
movl 0x18(%r13), %eax
leaq -0x2c(%rbp), %r15
movl %eax, (%r15)
leaq 0x145d5(%rip), %rax     # 0x194e8
movq (%rax), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x52d4
leaq 0x145c8(%rip), %rcx     # 0x194f0
movq (%rcx), %rdx
movq %rax, %rdi
movq %r15, %rsi
callq 0x5314
movq -0x50(%rbp), %r12
movq (%r12), %rbx
movl 0x40(%r12), %r14d
leaq 0x444f(%rip), %rdx      # 0x9399
pushq $0x3
popq %rcx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
leaq 0x4438(%rip), %rdx      # 0x939d
pushq $0x8
popq %rcx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
movq 0x8(%r12), %r8
movl 0x44(%r12), %r9d
leaq 0x441c(%rip), %rdx      # 0x93a6
movq %rax, %rdi
movq %r15, %rsi
pushq $0x3
popq %r15
movl %r15d, %ecx
callq 0x3f39
movq 0x10(%r12), %rbx
movl 0x48(%r12), %r14d
leaq 0x43fd(%rip), %rdx      # 0x93aa
movq %rax, %rdi
leaq -0x2c(%rbp), %rsi
movl %r15d, %ecx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
leaq 0x43e5(%rip), %rdx      # 0x93ae
pushq $0x9
popq %rcx
movq %rax, %rdi
leaq -0x2c(%rbp), %rsi
movq %rsi, %r15
movq %rbx, %r8
movl %r14d, %r9d
callq 0x3f39
movq 0x30(%r12), %r8
movl 0x58(%r12), %r9d
leaq 0x43e0(%rip), %rdx      # 0x93d2
pushq $0x5
popq %rbx
movq %rax, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x38(%r12), %r8
movl 0x5c(%r12), %r9d
leaq 0x43c5(%rip), %rdx      # 0x93d8
movq %rax, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x18(%r12), %r8
movl 0x4c(%r12), %r9d
leaq 0x4387(%rip), %rdx      # 0x93b8
pushq $0x6
popq %rbx
movq %rax, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x20(%r12), %r8
movl 0x50(%r12), %r9d
leaq 0x436d(%rip), %rdx      # 0x93bf
movq %rax, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x3f39
movq 0x28(%r12), %r8
movl 0x54(%r12), %r9d
leaq 0x4356(%rip), %rdx      # 0x93c6
movq %rax, %rdi
movq %r15, %rsi
pushq $0x3
popq %rcx
callq 0x3f39
movq %rax, %rbx
leaq 0x402e(%rip), %rdi      # 0x90b6
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq 0x8(%r13), %rdi
movl (%r15), %eax
movslq %eax, %r14
leaq 0x1(%r14), %r15
movq %r15, %rsi
callq 0x23d0
testq %rax, %rax
je 0x5106
movq %rax, %r12
movq %rax, 0x8(%r13)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x21b0
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2430
movl %r14d, 0x18(%r13)
movq %rbx, %rdi
callq 0x2030
movq -0x38(%rbp), %rsp
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x46d2
movq %rbx, %rdi
callq 0x2030
movq 0x8(%r13), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r13), %rdi
callq 0x2030
leaq 0x42b4(%rip), %rdi      # 0x93ca
callq 0x2460
movq 0x8(%r13), %rax
movq -0x38(%rbp), %rsp
jmp 0x50f7
leaq 0x425c(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4e88
leaq 0x424b(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4bc7