// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jge 0x42cf
jmp 0x453a
movq -0x8(%rbp), %rax
leaq 0xb196(%rip), %rcx      # 0xf470
subq %rcx, %rax
movl $0x28, %ecx
cqto
idivq %rcx
andq $0x1, %rax
cmpq $0x0, %rax
je 0x431f
movq -0x8(%rbp), %rax
movl $0x0, -0x8(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jg 0x431a
movq -0x8(%rbp), %rdi
callq 0x4540
jmp 0x453a
movq -0x8(%rbp), %rax
movl $0x0, 0x48(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x8(%rbp), %rdx
addq $0x18, %rdx
callq 0x46a0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x4372
movq -0x8(%rbp), %rdi
callq 0x3f20
jmp 0x453a
movq -0x8(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4790
movsbl %al, %eax
movq -0x8(%rbp), %rcx
movb 0x26(%rcx), %dl
andb $0x1, %al
movb %al, %sil
shlb %sil
andb $-0x3, %dl
orb %sil, %dl
movb %dl, 0x26(%rcx)
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movb %cl, %dl
movb 0x26(%rax), %cl
andb $0x1, %dl
shlb %dl
andb $-0x3, %cl
orb %dl, %cl
movb %cl, 0x26(%rax)
movq -0x8(%rbp), %rax
movb 0x26(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x4407
movq -0x8(%rbp), %rax
movb 0x26(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x43f4
movq -0x8(%rbp), %rdi
callq 0x4940
movsbl %al, %eax
cmpl $0x0, %eax
je 0x4402
movq -0x8(%rbp), %rdi
callq 0x3f20
jmp 0x453a
jmp 0x4527
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x7f95(%rip), %rsi      # 0xc3ab
callq 0x2570
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x442b
jmp 0x453a
movq -0x8(%rbp), %rax
movb 0x26(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x44fe
movq -0x8(%rbp), %rax
movb 0x26(%rax), %cl
andb $-0x2, %cl
orb $0x1, %cl
movb %cl, 0x26(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x60e0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movl 0x18242(%rip), %esi     # 0x1c6b0
leaq 0x1812b(%rip), %rdi     # 0x1c5a0
callq 0x40e0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jge 0x44a9
leaq 0x7f1a(%rip), %rdi      # 0xc3ae
movb $0x0, %al
callq 0x2180
movq -0x8(%rbp), %rdi
callq 0x3f20
jmp 0x453a
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
movb $0x0, %al
callq 0x20c0
leaq 0x14f14(%rip), %rax     # 0x193dc
movl $0x80000005, (%rax)     # imm = 0x80000005
movq -0x18(%rbp), %rcx
leaq 0x14f03(%rip), %rax     # 0x193dc
movq %rcx, 0x4(%rax)
leaq 0x14ef0(%rip), %rax     # 0x193d4
movl (%rax), %edi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
movl $0x1, %esi
leaq 0x14ee3(%rip), %rcx     # 0x193dc
callq 0x2240
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x451a
movq -0x8(%rbp), %rdi
callq 0x4940
movsbl %al, %eax
cmpl $0x0, %eax
je 0x4525
movq -0x18(%rbp), %rdi
callq 0x3f20
jmp 0x453a
jmp 0x4527
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jl 0x453a
movq -0x18(%rbp), %rdi
callq 0x4a40
addq $0x20, %rsp
popq %rbp
retq