// File: /workspace/github_c_makefile_star2_O1/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 0x3ab7
movq %rdi, %rbx
leal 0x89ce(%rip), %eax      # 0xc410
movl %ebx, %ecx
subl %eax, %ecx
btl $0x3, %ecx
jb 0x3aaa
movq %rsi, %r15
leaq 0x28(%rbx), %r14
xorl %eax, %eax
movl %eax, 0x48(%rbx)
movl %eax, 0x20(%rbx)
movq 0x8(%rbx), %r13
movslq 0x18(%rbx), %rsi
addq $0x2001, %rsi           # imm = 0x2001
movq %r13, %rdi
callq 0x23c0
testq %rax, %rax
je 0x3adc
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 0x3ae1
addl %eax, 0x18(%rbx)
movq %r12, %r13
cmpl $0x2000, %eax           # imm = 0x2000
je 0x3a5f
movslq 0x18(%rbx), %rax
movb $0x0, (%r12,%rax)
jmp 0x3afe
xorl %eax, %eax
movl %eax, -0x8(%rbx)
movl %eax, 0x20(%rbx)
cmpl %eax, 0x14(%rbx)
jle 0x3ac6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3da6
movq %r13, %rdi
jmp 0x3af6
je 0x3af3
cmpl $0x0, 0x18(%rbx)
je 0x3af3
callq 0x2050
cmpl $0xb, (%rax)
je 0x3a9f
movq %r12, %rdi
callq 0x2030
xorl %r12d, %r12d
movq %r12, 0x8(%rbx)
testq %r12, %r12
je 0x3cb9
leaq 0x57ee(%rip), %rsi      # 0x9300
movl $0x3, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x57d6(%rip), %rsi      # 0x9304
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x57bf(%rip), %rsi      # 0x9309
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x57ab(%rip), %rsi      # 0x9311
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x5794(%rip), %rsi      # 0x9316
movl $0x3, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x577c(%rip), %rsi      # 0x931a
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x5768(%rip), %rsi      # 0x9322
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x5751(%rip), %rsi      # 0x9327
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x573a(%rip), %rsi      # 0x932c
movl $0x5, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x5728(%rip), %rsi      # 0x9332
movl $0x6, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x5719(%rip), %rsi      # 0x933b
movl $0x4, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x56ff(%rip), %rsi      # 0x9339
movl $0x6, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x56ee(%rip), %rsi      # 0x9340
movl $0x5, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x3c7e
leaq 0x56dc(%rip), %rsi      # 0x9346
movl $0x7, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
setne %al
jmp 0x3c80
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 0x3ccf
testb $0x1, %bpl
je 0x3cb9
movq %rbx, %rdi
callq 0x3e83
testb %al, %al
je 0x3d75
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3822
leaq 0x5627(%rip), %rsi      # 0x92fd
movq %r12, %rdi
callq 0x2530
testq %rax, %rax
je 0x3ab7
testb $0x1, %bpl
jne 0x3d5a
andb $-0x4, %bpl
incb %bpl
movb %bpl, 0x26(%rbx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4d4b
movq %rax, 0x8(%rbx)
movl 0x15943(%rip), %esi     # 0x19650
leaq 0x1582c(%rip), %rdi     # 0x19540
callq 0x393b
movl %eax, 0x38(%rbx)
testl %eax, %eax
js 0x3d95
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20d0
leaq 0x12642(%rip), %rcx     # 0x1637c
movl $0x80000005, (%rcx)     # imm = 0x80000005
movq %r14, 0x4(%rcx)
leaq 0x12629(%rip), %rax     # 0x16374
movl (%rax), %edi
movl 0x38(%rbx), %edx
movl $0x1, %esi
callq 0x2220
cmpq $0x0, 0x8(%rbx)
je 0x3d6d
movq %rbx, %rdi
callq 0x3e83
testb %al, %al
je 0x3d75
movq %r14, %rdi
jmp 0x3cbc
cmpl $0x0, 0x38(%rbx)
js 0x3ab7
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3f2d
leaq 0x55bf(%rip), %rdi      # 0x935b
callq 0x2090
jmp 0x3cb9