// File: /workspace/github_c_makefile_star2_O3/niuyuling[P]CProxy/http_proxy.c
// Function: tcp_in
void tcp_in(conn_t * in, conf * configure)
{
    conn_t *server;
    char *headerEnd;

    if (in->fd < 0)
        return;
    //如果in - cts是奇数，那么是服务端触发事件
    if ((in - cts) & 1) {
        in->timer = (in - 1)->timer = 0;
        if (in->ready_data_len <= 0)
            serverToClient(in);
        return;
    }

    in->timer = (in + 1)->timer = 0;
    in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len);
    if (in->incomplete_data == NULL) {
        close_connection(in);
        return;
    }

    server = in + 1;
    server->request_type = in->request_type = request_type(in->incomplete_data);
    if (in->request_type == OTHER_TYPE) {
        //如果是第一次读取数据，并且不是HTTP请求的，关闭连接。复制数据失败的也关闭连接
        if (in->reread_data == 0 || copy_data(in) != 0) {
            close_connection(in);
            return;
        }
        goto handle_data_complete;
    }

    headerEnd = strstr(in->incomplete_data, "\n\r");
    //请求头不完整，等待下次读取
    if (headerEnd == NULL)
        return;

    if (in->reread_data == 0) {
        in->reread_data = 1;

        in->incomplete_data = request_head(in, configure);
        server->fd = create_connection6(remote_host, remote_port);

        if (server->fd < 0) {
            printf("remote->fd ERROR!\n");
            close_connection(in);
            return;
        }
        fcntl(server->fd, F_SETFL, O_NONBLOCK);
        ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
        ev.data.ptr = server;
        epoll_ctl(epollfd, EPOLL_CTL_ADD, server->fd, &ev);

    }

    if (in->incomplete_data == NULL || copy_data(in) != 0) {
        close_connection(server);
        return;
    }
    // 数据处理完毕，可以发送
handle_data_complete:
    // 这个判断是防止 多次读取客户端数据，但是没有和服务端建立连接，导致报错
    if (server->fd >= 0)
        tcp_out(server);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x0, 0x10(%rdi)
js 0x3c22
movq %rdi, %rbx
leal 0x89fd(%rip), %eax      # 0xc420
movl %ebx, %ecx
subl %eax, %ecx
btl $0x3, %ecx
jb 0x3c15
movq %rsi, %r15
leaq 0x28(%rbx), %r14
xorl %eax, %eax
movl %eax, 0x48(%rbx)
movl %eax, 0x20(%rbx)
movq 0x8(%rbx), %r13
movl 0x18(%rbx), %ebp
addl $0x2001, %ebp           # imm = 0x2001
movslq %ebp, %rsi
movq %r13, %rdi
callq 0x23d0
testq %rax, %rax
je 0x3c83
movq %rax, %r12
movl 0x10(%rbx), %edi
movslq 0x18(%rbx), %rsi
addq %rax, %rsi
movl $0x2000, %edx           # imm = 0x2000
callq 0x2240
testl %eax, %eax
jle 0x3c88
movl 0x18(%rbx), %ebp
addl %eax, %ebp
movl %ebp, 0x18(%rbx)
movq %r12, %r13
cmpl $0x2000, %eax           # imm = 0x2000
je 0x3a47
movslq %ebp, %rax
movb $0x0, (%r12,%rax)
movq %r12, 0x8(%rbx)
movb (%r12), %bpl
cmpb $0x47, %bpl
jne 0x3abc
cmpb $0x45, 0x1(%r12)
jne 0x3abc
cmpb $0x54, 0x2(%r12)
je 0x3c31
leaq 0x583d(%rip), %rsi      # 0x9300
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x5826(%rip), %rsi      # 0x9305
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x5812(%rip), %rsi      # 0x930d
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
cmpb $0x50, %bpl
jne 0x3b2a
cmpb $0x55, 0x1(%r12)
jne 0x3b2a
cmpb $0x54, 0x2(%r12)
je 0x3c31
leaq 0x57e1(%rip), %rsi      # 0x9312
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x57cd(%rip), %rsi      # 0x931a
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x57b6(%rip), %rsi      # 0x931f
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x579f(%rip), %rsi      # 0x9324
movl $0x5, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x5789(%rip), %rsi      # 0x932a
movl $0x6, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x577a(%rip), %rsi      # 0x9333
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x5760(%rip), %rsi      # 0x9331
movl $0x6, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x574f(%rip), %rsi      # 0x9338
movl $0x5, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c31
leaq 0x573d(%rip), %rsi      # 0x933e
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
setne %al
jmp 0x3c33
xorl %eax, %eax
movl %eax, -0x8(%rbx)
movl %eax, 0x20(%rbx)
cmpl %eax, 0x14(%rbx)
jle 0x3c6d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
movb 0x26(%rbx), %bpl
movb 0x4e(%rbx), %cl
leal (%rax,%rax), %edx
movl %ebp, %esi
andb $-0x3, %sil
orb %dl, %sil
movb %sil, 0x26(%rbx)
andb $-0x3, %cl
orb %dl, %cl
movb %cl, 0x4e(%rbx)
testb %al, %al
je 0x3cc5
testb $0x1, %bpl
je 0x3caf
movq %rbx, %rdi
callq 0x3e78
testb %al, %al
jne 0x3caf
jmp 0x3d6b
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3d9c
movq %r13, %rdi
jmp 0x3ca2
je 0x3c9f
movl 0x18(%rbx), %ebp
testl %ebp, %ebp
je 0x3c9f
callq 0x2050
cmpl $0xb, (%rax)
je 0x3a92
movq %r12, %rdi
callq 0x2030
movq $0x0, 0x8(%rbx)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3806
leaq 0x5631(%rip), %rsi      # 0x92fd
movq %r12, %rdi
callq 0x2540
testq %rax, %rax
je 0x3c22
testb $0x1, %bpl
jne 0x3d57
andb $-0x4, %bpl
incb %bpl
movb %bpl, 0x26(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4d1f
movq %rax, 0x8(%rbx)
movl 0x1595d(%rip), %esi     # 0x19660
leaq 0x15846(%rip), %rdi     # 0x19550
callq 0x3918
movl %eax, 0x38(%rbx)
testl %eax, %eax
js 0x3d8b
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20d0
leaq 0x1265c(%rip), %rcx     # 0x1638c
movl $0x80000005, (%rcx)     # imm = 0x80000005
movq %r14, 0x4(%rcx)
leaq 0x12643(%rip), %rax     # 0x16384
movl (%rax), %edi
movl 0x38(%rbx), %edx
movl $0x1, %esi
callq 0x2220
cmpq $0x0, 0x8(%rbx)
je 0x3d63
movq %rbx, %rdi
callq 0x3e78
testb %al, %al
je 0x3d6b
movq %r14, %rdi
jmp 0x3cb2
cmpl $0x0, 0x38(%rbx)
js 0x3c22
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3f22
leaq 0x55c1(%rip), %rdi      # 0x9353
callq 0x2090
jmp 0x3caf