// File: /workspace/github_c_makefile_star2_O2/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 0x3acf
movq %rdi, %rbx
movq %rdi, %rax
subq 0x86fc(%rip), %rax      # 0xbfe0
pushq $0x28
popq %rcx
cqto
idivq %rcx
testb $0x1, %al
jne 0x3ac0
movq %rsi, %r15
andl $0x0, 0x48(%rbx)
andl $0x0, 0x20(%rbx)
leaq 0x28(%rbx), %r14
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 0x3b30
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 0x3b35
movl 0x18(%rbx), %ebp
addl %eax, %ebp
movl %ebp, 0x18(%rbx)
movq %r12, %r13
cmpl $0x2000, %eax           # imm = 0x2000
je 0x390a
movslq %ebp, %rax
movb $0x0, (%r12,%rax)
movq %r12, 0x8(%rbx)
movb (%r12), %bpl
cmpb $0x47, %bpl
jne 0x397f
cmpb $0x45, 0x1(%r12)
jne 0x397f
cmpb $0x54, 0x2(%r12)
je 0x3ade
leaq 0x597a(%rip), %rsi      # 0x9300
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x5965(%rip), %rsi      # 0x9305
pushq $0x7
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x5953(%rip), %rsi      # 0x930d
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
cmpb $0x50, %bpl
jne 0x39e7
cmpb $0x55, 0x1(%r12)
jne 0x39e7
cmpb $0x54, 0x2(%r12)
je 0x3ade
leaq 0x5924(%rip), %rsi      # 0x9312
pushq $0x7
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x5912(%rip), %rsi      # 0x931a
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58fd(%rip), %rsi      # 0x931f
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58e8(%rip), %rsi      # 0x9324
pushq $0x5
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58d4(%rip), %rsi      # 0x932a
pushq $0x6
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58c7(%rip), %rsi      # 0x9333
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58af(%rip), %rsi      # 0x9331
pushq $0x6
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x58a0(%rip), %rsi      # 0x9338
pushq $0x5
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3ade
leaq 0x5890(%rip), %rsi      # 0x933e
pushq $0x7
popq %rdx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
setne %al
jmp 0x3ae0
andl $0x0, -0x8(%rbx)
andl $0x0, 0x20(%rbx)
xorl %eax, %eax
cmpl %eax, 0x14(%rbx)
jle 0x3b1a
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 0x3b6f
testb $0x1, %bpl
je 0x3b59
movq %rbx, %rdi
callq 0x3d0b
testb %al, %al
jne 0x3b59
jmp 0x3c11
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3c42
movq %r13, %rdi
jmp 0x3b4f
je 0x3b4c
movl 0x18(%rbx), %ebp
testl %ebp, %ebp
je 0x3b4c
callq 0x2050
cmpl $0xb, (%rax)
je 0x3955
movq %r12, %rdi
callq 0x2030
andq $0x0, 0x8(%rbx)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36c5
leaq 0x5787(%rip), %rsi      # 0x92fd
movq %r12, %rdi
callq 0x2540
testq %rax, %rax
je 0x3acf
testb $0x1, %bpl
jne 0x3bfd
andb $-0x4, %bpl
incb %bpl
movb %bpl, 0x26(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4aec
movq %rax, 0x8(%rbx)
movl 0x15ab3(%rip), %esi     # 0x19660
leaq 0x1599c(%rip), %rdi     # 0x19550
callq 0x37db
movl %eax, 0x38(%rbx)
testl %eax, %eax
js 0x3c31
pushq $0x4
popq %rsi
movl %eax, %edi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20d0
leaq 0x127b4(%rip), %rcx     # 0x1638c
movl $0x80000005, (%rcx)     # imm = 0x80000005
movq %r14, 0x4(%rcx)
leaq 0x1279b(%rip), %rax     # 0x16384
movl (%rax), %edi
movl 0x38(%rbx), %edx
pushq $0x1
popq %rsi
callq 0x2220
cmpq $0x0, 0x8(%rbx)
je 0x3c09
movq %rbx, %rdi
callq 0x3d0b
testb %al, %al
je 0x3c11
movq %r14, %rdi
jmp 0x3b5c
cmpl $0x0, 0x38(%rbx)
js 0x3acf
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3d9e
leaq 0x571b(%rip), %rdi      # 0x9353
callq 0x2090
jmp 0x3b59