// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rdi, %r15
movq $0x0, -0x50(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x60, %edi
callq 0x2320
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x50(%rax)
movq 0x8(%r15), %rdi
movq %rax, %rsi
callq 0x496c
movq 0x8(%r15), %rdi
leaq 0x455a(%rip), %rsi      # 0x9309
movl $0x7, %edx
callq 0x2070
testl %eax, %eax
movq %r13, -0x38(%rbp)
jne 0x508f
movq %rsp, -0x48(%rbp)
movl 0x7c(%r13), %eax
leaq 0x14880(%rip), %rcx     # 0x19654
movl %eax, (%rcx)
movl 0x74(%r13), %eax
incl %eax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0x1474c(%rip), %rdi     # 0x19540
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x50(%r13), %eax
testl %eax, %eax
jle 0x4e11
leaq 0x14841(%rip), %rcx     # 0x19650
movl %eax, (%rcx)
movq 0x58(%r13), %rsi
testq %rsi, %rsi
je 0x4e2a
movslq 0x70(%r13), %rdx
leaq 0x1471b(%rip), %rdi     # 0x19540
callq 0x22d0
movl $0x2000, %edi           # imm = 0x2000
callq 0x2320
movq %rax, %r13
testq %rax, %rax
je 0x5421
movl $0x2000, %edx           # imm = 0x2000
movq %r13, %rdi
xorl %esi, %esi
callq 0x21b0
movq 0x8(%r15), %rsi
leaq 0x18(%r15), %rbx
movq %r15, -0x30(%rbp)
movslq 0x18(%r15), %rdx
movq %r13, %rdi
callq 0x2420
movq -0x38(%rbp), %rax
movq 0x60(%rax), %rsi
movslq 0x74(%rax), %rdx
incq %rdx
movq %r12, %rdi
callq 0x2420
leaq 0x451a(%rip), %rsi      # 0x939f
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
callq 0x2230
testq %rax, %rax
je 0x4ec1
leaq 0x4502(%rip), %r12      # 0x939f
leaq -0x50(%rbp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5450
xorl %edi, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2230
testq %rax, %rax
jne 0x4ea1
movq -0x38(%rbp), %rax
movq 0x68(%rax), %rdx
movl 0x78(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5529
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
leaq -0x3c(%rbp), %r12
movl %eax, (%r12)
leaq 0x145fb(%rip), %rax     # 0x194e8
movq (%rax), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x55e1
leaq 0x145ee(%rip), %rcx     # 0x194f0
movq (%rcx), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x5624
movq (%r14), %rbx
movl 0x40(%r14), %r15d
leaq 0x4483(%rip), %rdx      # 0x93a1
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40cf
leaq 0x446a(%rip), %rdx      # 0x93a5
movq %rax, %rdi
movq %r12, %rsi
movl $0x8, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40cf
movq 0x8(%r14), %r8
movl 0x44(%r14), %r9d
leaq 0x444e(%rip), %rdx      # 0x93ae
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
callq 0x40cf
movq 0x10(%r14), %rbx
movl 0x48(%r14), %r15d
leaq 0x4433(%rip), %rdx      # 0x93b2
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40cf
leaq 0x441a(%rip), %rdx      # 0x93b6
movq %rax, %rdi
movq %r12, %rsi
movl $0x9, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x40cf
movq 0x18(%r14), %r8
movl 0x4c(%r14), %r9d
leaq 0x43ff(%rip), %rdx      # 0x93c0
movq %rax, %rdi
movq %r12, %rsi
movl $0x6, %ecx
callq 0x40cf
movq 0x20(%r14), %r8
movl 0x50(%r14), %r9d
leaq 0x43e7(%rip), %rdx      # 0x93c7
movq %rax, %rdi
movq %r12, %rsi
movl $0x6, %ecx
callq 0x40cf
movq 0x28(%r14), %r8
movl 0x54(%r14), %r9d
leaq 0x43cf(%rip), %rdx      # 0x93ce
movq %rax, %rdi
movq %r12, %rsi
movl $0x3, %ecx
callq 0x40cf
leaq 0x40a3(%rip), %rdi      # 0x90b9
movq %rax, -0x58(%rbp)
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq -0x30(%rbp), %rax
movq %rax, %r15
movq 0x8(%rax), %rdi
movl (%r12), %eax
movslq %eax, %r12
leaq 0x1(%r12), %r13
movq %r13, %rsi
callq 0x23c0
movq %rax, %rbx
testq %rax, %rax
je 0x5432
movq %rbx, 0x8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq %rbx, %rdi
movq -0x58(%rbp), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x2420
movl %r12d, 0x18(%r15)
movq %r13, %rdi
callq 0x2030
testq %rbx, %rbx
movq -0x48(%rbp), %rsp
movq -0x38(%rbp), %r13
je 0x53e3
movq 0x8(%r15), %rbx
leaq 0x4266(%rip), %rsi      # 0x9300
movl $0x3, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
je 0x50c7
leaq 0x4252(%rip), %rsi      # 0x9304
movl $0x4, %edx
movq %rbx, %rdi
callq 0x2070
testl %eax, %eax
jne 0x53cf
movq %r12, -0x60(%rbp)
movq %rsp, -0x58(%rbp)
movl 0x4c(%r13), %eax
leaq 0x1457a(%rip), %rcx     # 0x19654
movl %eax, (%rcx)
movl 0x44(%r13), %eax
incl %eax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0x14446(%rip), %rdi     # 0x19540
movl $0x10e, %edx            # imm = 0x10E
xorl %esi, %esi
callq 0x21b0
movl 0x20(%r13), %eax
testl %eax, %eax
jle 0x5117
leaq 0x1453b(%rip), %rcx     # 0x19650
movl %eax, (%rcx)
movq 0x28(%r13), %rsi
testq %rsi, %rsi
je 0x5130
movslq 0x40(%r13), %rdx
leaq 0x14415(%rip), %rdi     # 0x19540
callq 0x22d0
movl $0x2000, %edi           # imm = 0x2000
callq 0x2320
movq %rax, %r13
testq %rax, %rax
je 0x53f5
movq %r14, -0x48(%rbp)
movl $0x2000, %edx           # imm = 0x2000
movq %r13, %rdi
xorl %esi, %esi
callq 0x21b0
movq 0x8(%r15), %rsi
leaq 0x18(%r15), %rbx
movq %r15, -0x30(%rbp)
movslq 0x18(%r15), %rdx
movq %r13, %rdi
callq 0x2420
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rsi
movslq 0x44(%rax), %rdx
incq %rdx
movq %r12, %rdi
callq 0x2420
leaq 0x4210(%rip), %rsi      # 0x939f
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
callq 0x2230
testq %rax, %rax
je 0x51cb
leaq 0x41f8(%rip), %r12      # 0x939f
leaq -0x50(%rbp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5450
xorl %edi, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2230
testq %rax, %rax
jne 0x51ab
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdx
movl 0x48(%rax), %ecx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5529
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
leaq -0x3c(%rbp), %r14
movl %eax, (%r14)
leaq 0x142e2(%rip), %rax     # 0x194d8
movq (%rax), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x55e1
leaq 0x142d5(%rip), %rcx     # 0x194e0
movq (%rcx), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x5624
movq -0x48(%rbp), %r15
movq (%r15), %rbx
movl 0x40(%r15), %r12d
leaq 0x4176(%rip), %rdx      # 0x93a1
movq %rax, %rdi
movq %r14, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40cf
leaq 0x415d(%rip), %rdx      # 0x93a5
movq %rax, %rdi
movq %r14, %rsi
movl $0x8, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40cf
movq 0x8(%r15), %r8
movl 0x44(%r15), %r9d
leaq 0x4141(%rip), %rdx      # 0x93ae
movq %rax, %rdi
movq %r14, %rsi
movl $0x3, %ecx
callq 0x40cf
movq 0x10(%r15), %rbx
movl 0x48(%r15), %r12d
leaq 0x4126(%rip), %rdx      # 0x93b2
movq %rax, %rdi
movq %r14, %rsi
movl $0x3, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40cf
leaq 0x410d(%rip), %rdx      # 0x93b6
movq %rax, %rdi
movq %r14, %rsi
movl $0x9, %ecx
movq %rbx, %r8
movl %r12d, %r9d
callq 0x40cf
movq 0x30(%r15), %r8
movl 0x58(%r15), %r9d
leaq 0x410c(%rip), %rdx      # 0x93da
movq %rax, %rdi
movq %r14, %rsi
movl $0x5, %ecx
callq 0x40cf
movq 0x38(%r15), %r8
movl 0x5c(%r15), %r9d
leaq 0x40f3(%rip), %rdx      # 0x93e0
movq %rax, %rdi
movq %r14, %rsi
movl $0x5, %ecx
callq 0x40cf
movq 0x18(%r15), %r8
movl 0x4c(%r15), %r9d
leaq 0x40b4(%rip), %rdx      # 0x93c0
movq %rax, %rdi
movq %r14, %rsi
movl $0x6, %ecx
callq 0x40cf
movq 0x20(%r15), %r8
movl 0x50(%r15), %r9d
leaq 0x409c(%rip), %rdx      # 0x93c7
movq %rax, %rdi
movq %r14, %rsi
movl $0x6, %ecx
callq 0x40cf
movq 0x28(%r15), %r8
movl 0x54(%r15), %r9d
leaq 0x4084(%rip), %rdx      # 0x93ce
movq %rax, %rdi
movq %r14, %rsi
movl $0x3, %ecx
callq 0x40cf
movq %rax, %r12
leaq 0x3d55(%rip), %rdi      # 0x90b9
movq %rax, %rsi
xorl %eax, %eax
callq 0x2170
movq -0x30(%rbp), %rax
movq %rax, %r15
movq 0x8(%rax), %rdi
movl (%r14), %eax
movslq %eax, %r14
leaq 0x1(%r14), %r13
movq %r13, %rsi
callq 0x23c0
movq %rax, %rbx
testq %rax, %rax
je 0x5406
movq %rbx, 0x8(%r15)
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2420
movl %r14d, 0x18(%r15)
movq %r12, %rdi
callq 0x2030
movq -0x60(%rbp), %r12
testq %rbx, %rbx
movq -0x58(%rbp), %rsp
movq -0x48(%rbp), %r14
je 0x53e3
movq %r14, %rdi
callq 0x48f6
movq %r14, %rdi
callq 0x2030
movq 0x8(%r15), %r12
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f94(%rip), %rdi      # 0x9390
callq 0x2450
jmp 0x5146
movq 0x8(%r15), %rdi
callq 0x2030
leaq 0x3fbc(%rip), %rdi      # 0x93d2
callq 0x2450
movq 0x8(%r15), %r12
jmp 0x53c2
leaq 0x3f68(%rip), %rdi      # 0x9390
callq 0x2450
jmp 0x4e40
movq 0x8(%r15), %rdi
callq 0x2030
leaq 0x3f90(%rip), %rdi      # 0x93d2
callq 0x2450
movq 0x8(%r15), %r12
jmp 0x507e