// File: 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
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9c70
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x1d1(%rbp)
jne 0x1f31d
jmp 0x1f333
movq -0x18(%rbp), %rdi
callq 0xa9c0
cmpl $0x2, 0x4(%rax)
sete %al
movb %al, -0x1d1(%rbp)
movb -0x1d1(%rbp), %al
andb $0x1, %al
movb %al, -0x19(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x71a0
movq -0x1e0(%rbp), %rdi
movb $0x5, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
callq 0xceb0
jmp 0x1f364
testb $0x1, -0x19(%rbp)
je 0x1f390
movb $-0x10, -0x4d(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x4d(%rbp), %rsi
callq 0xceb0
jmp 0x1f37d
jmp 0x1f3a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1f9a2
movb $0x1, -0x4e(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x4e(%rbp), %rsi
callq 0xceb0
jmp 0x1f3a3
jmp 0x1f3a5
movb $0x0, -0x4f(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x4f(%rbp), %rsi
callq 0xceb0
jmp 0x1f3b8
movq -0x18(%rbp), %rdi
callq 0x9c70
movb %al, -0x1e1(%rbp)
jmp 0x1f3c9
movb -0x1e1(%rbp), %al
testb $0x1, %al
jne 0x1f40a
jmp 0x1f3d5
movq -0x18(%rbp), %rdi
callq 0xa9a0
movq %rax, -0x1f0(%rbp)
jmp 0x1f3e7
movq -0x1f0(%rbp), %rdi
callq 0x9c50
movl %eax, -0x1f4(%rbp)
jmp 0x1f3fb
movl -0x1f4(%rbp), %eax
cmpl $0x2, %eax
jne 0x1f596
movq -0x18(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x9d90
jmp 0x1f419
leaq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9fb0
movw %ax, -0x1f6(%rbp)
jmp 0x1f433
movw -0x1f6(%rbp), %ax
movw %ax, -0x52(%rbp)
movq -0x60(%rbp), %rdi
callq 0x5330
cmpq $0xff, %rax
jbe 0x1f47c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x1f578
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x52a0
jmp 0x1f9a2
movb $0x3, -0x85(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x85(%rbp), %rsi
callq 0xceb0
jmp 0x1f495
movq -0x60(%rbp), %rdi
callq 0x5330
movb %al, -0x86(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x86(%rbp), %rsi
callq 0xceb0
jmp 0x1f4b6
leaq -0x38(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x74a0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x20390
movq -0x60(%rbp), %rdi
callq 0x53f0
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rdi
callq 0x5790
movq -0x208(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
callq 0x202e0
movq %rax, -0x200(%rbp)
jmp 0x1f52a
movq -0x200(%rbp), %rax
movq %rax, -0xb0(%rbp)
movb -0x51(%rbp), %al
movb %al, -0xb1(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0xb1(%rbp), %rsi
callq 0xceb0
jmp 0x1f553
movb -0x52(%rbp), %al
movb %al, -0xb2(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0xb2(%rbp), %rsi
callq 0xceb0
jmp 0x1f56e
movl $0x0, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x52a0
movl -0x84(%rbp), %eax
testl %eax, %eax
jne 0x1f98b
jmp 0x1f591
jmp 0x1f91a
movq -0x18(%rbp), %rdi
callq 0xa9a0
movq %rax, -0x210(%rbp)
jmp 0x1f5a8
movq -0x210(%rbp), %rdi
callq 0x9c50
movl %eax, -0x214(%rbp)
jmp 0x1f5bc
movl -0x214(%rbp), %eax
cmpl $0x1, %eax
jne 0x1f908
xorps %xmm0, %xmm0
movaps %xmm0, -0x230(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movl $0x80, -0x144(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x144(%rbp), %rdx
callq 0x9cb0
jmp 0x1f630
movzwl -0x140(%rbp), %eax
cmpl $0xa, %eax
jne 0x1f793
movslq -0x144(%rbp), %rax
cmpq $0x1c, %rax
jb 0x1f64f
jmp 0x1f66e
leaq 0x7a87(%rip), %rdi      # 0x270dd
leaq 0x7a37(%rip), %rsi      # 0x27094
movl $0x59, %edx
leaq 0x7a9d(%rip), %rcx      # 0x27106
callq 0x52e0
movups -0x138(%rbp), %xmm0
movaps %xmm0, -0x160(%rbp)
movw -0x13e(%rbp), %ax
movw %ax, -0x52(%rbp)
movb $0x4, -0x161(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x161(%rbp), %rsi
callq 0xceb0
jmp 0x1f6a0
leaq -0x38(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x74a0
movq %rax, -0x178(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x20390
movq -0x240(%rbp), %rdi
leaq -0x150(%rbp), %rcx
movq -0x170(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x203d0
movq %rax, -0x238(%rbp)
jmp 0x1f6f4
movq -0x238(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x250(%rbp)
callq 0x74a0
movq %rax, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x20390
movq -0x250(%rbp), %rdi
leaq -0x50(%rbp), %rcx
movq -0x188(%rbp), %rsi
leaq -0x52(%rbp), %rdx
callq 0x203d0
movq %rax, -0x248(%rbp)
jmp 0x1f750
movq -0x248(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6b00
cmpq $0x16, %rax
jne 0x1f76f
jmp 0x1f78e
leaq 0x79d7(%rip), %rdi      # 0x2714d
leaq 0x7917(%rip), %rsi      # 0x27094
movl $0x5f, %edx
leaq 0x797d(%rip), %rcx      # 0x27106
callq 0x52e0
jmp 0x1f906
movzwl -0x140(%rbp), %eax
cmpl $0x2, %eax
jne 0x1f8f1
movslq -0x144(%rbp), %rax
cmpq $0x10, %rax
jb 0x1f7b2
jmp 0x1f7d1
leaq 0x79a9(%rip), %rdi      # 0x27162
leaq 0x78d4(%rip), %rsi      # 0x27094
movl $0x61, %edx
leaq 0x793a(%rip), %rcx      # 0x27106
callq 0x52e0
movl -0x13c(%rbp), %eax
movl %eax, -0x19c(%rbp)
movw -0x13e(%rbp), %ax
movw %ax, -0x52(%rbp)
movb $0x1, -0x19d(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x19d(%rbp), %rsi
callq 0xceb0
jmp 0x1f801
leaq -0x38(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x74a0
movq %rax, -0x1b0(%rbp)
leaq -0x1a8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x20390
movq -0x260(%rbp), %rdi
leaq -0x198(%rbp), %rcx
movq -0x1a8(%rbp), %rsi
leaq -0x19c(%rbp), %rdx
callq 0x203d0
movq %rax, -0x258(%rbp)
jmp 0x1f855
movq -0x258(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x74a0
movq %rax, -0x1c8(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x20390
movq -0x270(%rbp), %rdi
leaq -0x50(%rbp), %rcx
movq -0x1c0(%rbp), %rsi
leaq -0x52(%rbp), %rdx
callq 0x203d0
movq %rax, -0x268(%rbp)
jmp 0x1f8b1
movq -0x268(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6b00
cmpq $0xa, %rax
jne 0x1f8d0
jmp 0x1f8ef
leaq 0x78b3(%rip), %rdi      # 0x2718a
leaq 0x77b6(%rip), %rsi      # 0x27094
movl $0x67, %edx
leaq 0x781c(%rip), %rcx      # 0x27106
callq 0x52e0
jmp 0x1f904
movb $0x0, -0x1(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x1f98b
jmp 0x1f906
jmp 0x1f918
movb $0x0, -0x1(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x1f98b
jmp 0x1f91a
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x2, %esi
movl $0x8, %edx
callq 0x50f0
jmp 0x1f933
movq -0x10(%rbp), %rax
movq %rax, -0x280(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x1e6a0
movq -0x288(%rbp), %rdi
movq %rax, -0x278(%rbp)
callq 0x6b00
movq -0x280(%rbp), %rdi
movq -0x278(%rbp), %rsi
movq %rax, %rdx
callq 0x5240
jmp 0x1f97d
movb $0x1, -0x1(%rbp)
movl $0x1, -0x84(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x5c60
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x5c60
movq -0x48(%rbp), %rdi
callq 0x5710
nopw %cs:(%rax,%rax)