// File: /workspace/github_cpp_makefile_star2_O1/theuni[P]libbtcnet/src/bareproxy.cpp
// Function: CBareProxy::writeproto(bufferevent*, CConnection const&)
bool CBareProxy::writeproto(bufferevent* bev, const CConnection& conn)
{
    bool resolve = conn.IsDNS() && conn.GetOptions().doResolve == CConnectionOptions::RESOLVE_ONLY;
    std::vector<uint8_t> vSocks5;
    vSocks5.push_back(0x05); // VER protocol version
    if (resolve) {
        vSocks5.push_back(0xf0); // CMD RESOLVE
    } else
        vSocks5.push_back(0x01); // CMD CONNECT
    vSocks5.push_back(0x00);     // RSV Reserved

    unsigned short port;

    if (conn.IsDNS() || conn.GetProxy().GetType() == CProxy::SOCKS5_FORCE_DOMAIN_TYPE) {
        const std::string& host = conn.GetHost();
        port = conn.GetPort();
        if (host.size() > 255)
            return false;
        vSocks5.push_back(0x03); // ATYP DOMAINNAME
        vSocks5.push_back(host.size());
        vSocks5.insert(vSocks5.end(), host.begin(), host.end());
        vSocks5.push_back((port >> 8) & 0xFF);
        vSocks5.push_back((port >> 0) & 0xFF);
    } else if (conn.GetProxy().GetType() == CProxy::SOCKS5) {
        sockaddr_storage sock;
        memset(&sock, 0, sizeof(sock));
        int socksize = sizeof(sock);
        conn.GetSockAddr(reinterpret_cast<sockaddr*>(&sock), &socksize);
        if (sock.ss_family == AF_INET6) {
            assert((size_t)socksize >= sizeof(sockaddr_in6));
            in6_addr addr = reinterpret_cast<sockaddr_in6*>(&sock)->sin6_addr;
            port = reinterpret_cast<sockaddr_in6*>(&sock)->sin6_port;
            vSocks5.push_back(0x04); // ATYP IPV6
            vSocks5.insert(vSocks5.end(), addr.s6_addr, addr.s6_addr + sizeof(addr.s6_addr));
            vSocks5.insert(vSocks5.end(), reinterpret_cast<unsigned char*>(&port), reinterpret_cast<unsigned char*>(&port) + sizeof(port));
            assert(vSocks5.size() == 22);
        } else if (sock.ss_family == AF_INET) {
            assert((size_t)socksize >= sizeof(sockaddr_in));
            in_addr addr = reinterpret_cast<sockaddr_in*>(&sock)->sin_addr;
            port = reinterpret_cast<sockaddr_in*>(&sock)->sin_port;
            vSocks5.push_back(0x01); // ATYP IPV4
            vSocks5.insert(vSocks5.end(), reinterpret_cast<unsigned char*>(&addr), reinterpret_cast<unsigned char*>(&addr) + sizeof(addr));
            vSocks5.insert(vSocks5.end(), reinterpret_cast<unsigned char*>(&port), reinterpret_cast<unsigned char*>(&port) + sizeof(port));
            assert(vSocks5.size() == 10);
        } else
            return false;
    } else
        return false;
    bufferevent_setwatermark(bev, EV_READ, 8, 0);
    bufferevent_write(bev, &vSocks5[0], vSocks5.size());
    return true;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xd0, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x6186
testb %al, %al
je 0xfa25
movq %r14, %rdi
callq 0x701a
cmpl $0x2, 0x4(%rax)
sete %bpl
jmp 0xfa27
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movb $0x5, 0x50(%rsp)
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0xfa51
movb $0x5, (%rsi)
incq 0x18(%rsp)
jmp 0xfa60
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7f08
testb %bpl, %bpl
je 0xfa7b
movb $-0x10, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfa96
movb $-0x10, (%rsi)
jmp 0xfa8f
movb $0x1, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfaa7
movb $0x1, (%rsi)
incq 0x18(%rsp)
jmp 0xfab6
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7f08
jmp 0xfab6
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7f08
movb $0x0, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfad1
movb $0x0, (%rsi)
incq 0x18(%rsp)
jmp 0xfae0
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7f08
movq %r14, %rdi
callq 0x6186
testb %al, %al
jne 0xfb01
movq %r14, %rdi
callq 0x7014
movq %rax, %rdi
callq 0x617e
cmpl $0x2, %eax
jne 0xfb64
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x61e6
movq %r14, %rdi
callq 0x631c
movw %ax, 0xc(%rsp)
cmpq $0x100, 0x58(%rsp)      # imm = 0x100
jae 0xfb48
movb $0x3, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfc12
movb $0x3, (%rsi)
incq 0x18(%rsp)
jmp 0xfc21
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfc0b
callq 0x4340
jmp 0xfc0b
movq %r14, %rdi
callq 0x7014
movq %rax, %rdi
callq 0x617e
cmpl $0x1, %eax
jne 0xfc0b
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
leaq 0x4c(%rsp), %rdx
movl $0x80, (%rdx)
movq %r14, %rdi
callq 0x618e
movzwl 0x50(%rsp), %eax
cmpl $0x2, %eax
je 0xfd1c
cmpl $0xa, %eax
jne 0xfc0b
cmpl $0x1b, 0x4c(%rsp)
jbe 0xfe3b
movups 0x58(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movzwl 0x52(%rsp), %eax
movw %ax, 0xc(%rsp)
movb $0x4, 0xf(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfd57
movb $0x4, (%rsi)
incq 0x18(%rsp)
jmp 0xfd66
xorl %ebp, %ebp
jmp 0xfcff
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7f08
movb 0x58(%rsp), %al
movb %al, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfc3e
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xfc4d
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7f08
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
addq %rdx, %rcx
callq 0x10ac2
movb 0xd(%rsp), %al
movb %al, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfc85
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xfc94
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7f08
movb 0xc(%rsp), %al
movb %al, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfcb1
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xfcc0
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7f08
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfcd3
callq 0x4340
movl $0x8, %edx
movq %rbx, %rdi
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x40c0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
subq %rsi, %rdx
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x41f0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xfd0e
callq 0x4340
movl %ebp, %eax
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
cmpl $0xf, 0x4c(%rsp)
jbe 0xfe5a
movl 0x54(%rsp), %eax
movl %eax, 0x30(%rsp)
movzwl 0x52(%rsp), %eax
movw %ax, 0xc(%rsp)
movb $0x1, 0xf(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfdc9
movb $0x1, (%rsi)
incq 0x18(%rsp)
jmp 0xfdd8
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x7f08
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0x40(%rsp), %rcx
leaq 0x30(%rsp), %rdx
callq 0x10cee
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %rdx
callq 0x10cee
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0x16, %rax
je 0xfcd3
leaq 0x4207(%rip), %rdi      # 0x13fb8
leaq 0x4147(%rip), %rsi      # 0x13eff
leaq 0x41b2(%rip), %rcx      # 0x13f71
movl $0x5f, %edx
callq 0x4260
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x7f08
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0x34(%rsp), %rcx
leaq 0x30(%rsp), %rdx
callq 0x10cee
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %rdx
callq 0x10cee
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0xa, %rax
je 0xfcd3
leaq 0x41d2(%rip), %rdi      # 0x13ff5
leaq 0x40d5(%rip), %rsi      # 0x13eff
leaq 0x4140(%rip), %rcx      # 0x13f71
movl $0x67, %edx
callq 0x4260
leaq 0x4106(%rip), %rdi      # 0x13f48
leaq 0x40b6(%rip), %rsi      # 0x13eff
leaq 0x4121(%rip), %rcx      # 0x13f71
movl $0x59, %edx
callq 0x4260
leaq 0x416c(%rip), %rdi      # 0x13fcd
leaq 0x4097(%rip), %rsi      # 0x13eff
leaq 0x4102(%rip), %rcx      # 0x13f71
movl $0x61, %edx
callq 0x4260
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfe99
jmp 0xfe99
jmp 0xfe99
jmp 0xfe99
jmp 0xfeb3
jmp 0xfeb3
jmp 0xfe99
jmp 0xfeb3
jmp 0xfeb3
movq %rax, %rbx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfeb6
callq 0x4340
jmp 0xfeb6
jmp 0xfeb3
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xfec5
callq 0x4340
movq %rbx, %rdi
callq 0x4640
nop