// File: /workspace/github_cpp_makefile_star2_O3/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 0x6156
testb %al, %al
je 0xf86b
movq %r14, %rdi
callq 0x6fa8
cmpl $0x2, 0x4(%rax)
sete %bpl
jmp 0xf86d
xorl %ebp, %ebp
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x50(%rsp), %rdx
movb $0x5, (%rdx)
xorl %esi, %esi
callq 0x7ea6
testb %bpl, %bpl
je 0xf8aa
movb $-0x10, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xf8c5
movb $-0x10, (%rsi)
jmp 0xf8be
movb $0x1, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xf8d6
movb $0x1, (%rsi)
incq 0x18(%rsp)
jmp 0xf8e5
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7ea6
jmp 0xf8e5
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7ea6
movb $0x0, 0x50(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xf900
movb $0x0, (%rsi)
incq 0x18(%rsp)
jmp 0xf90f
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x7ea6
movq %r14, %rdi
callq 0x6156
testb %al, %al
jne 0xf930
movq %r14, %rdi
callq 0x6fa2
movq %rax, %rdi
callq 0x614e
cmpl $0x2, %eax
jne 0xf99b
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x61b6
movq %r14, %rdi
callq 0x62ec
movw %ax, 0xc(%rsp)
cmpq $0x100, 0x58(%rsp)      # imm = 0x100
jae 0xf97f
movb $0x3, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfa51
movb $0x3, (%rsi)
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfa65
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfa4a
callq 0x4340
jmp 0xfa4a
movq %r14, %rdi
callq 0x6fa2
movq %rax, %rdi
callq 0x614e
cmpl $0x1, %eax
jne 0xfa4a
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 0x615e
movzwl 0x50(%rsp), %eax
cmpl $0x2, %eax
je 0xfb6c
cmpl $0xa, %eax
jne 0xfa4a
cmpl $0x1b, 0x4c(%rsp)
jbe 0xfc99
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 0xfbb3
movb $0x4, (%rsi)
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfbc7
xorl %ebp, %ebp
jmp 0xfb4f
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7ea6
movq 0x18(%rsp), %rsi
movb 0x58(%rsp), %al
movb %al, 0x30(%rsp)
cmpq 0x20(%rsp), %rsi
je 0xfa85
movb %al, (%rsi)
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfa99
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7ea6
movq 0x18(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
addq %rdx, %rcx
leaq 0x10(%rsp), %rdi
callq 0x108de
movb 0xd(%rsp), %al
movb %al, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfad5
movb %al, (%rsi)
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfae9
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7ea6
movq 0x18(%rsp), %rsi
movb 0xc(%rsp), %al
movb %al, 0x30(%rsp)
cmpq 0x20(%rsp), %rsi
je 0xfb01
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0xfb10
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x7ea6
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfb23
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 0xfb5e
callq 0x4340
movl %ebp, %eax
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
cmpl $0xf, 0x4c(%rsp)
jbe 0xfcb8
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 0xfc26
movb $0x1, (%rsi)
movq 0x18(%rsp), %rsi
incq %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfc3a
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x7ea6
movq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %rcx
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x10b3e
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %rdx
callq 0x10b3e
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0x16, %rax
je 0xfb23
leaq 0x43a2(%rip), %rdi      # 0x13fb0
leaq 0x42e2(%rip), %rsi      # 0x13ef7
leaq 0x434d(%rip), %rcx      # 0x13f69
movl $0x5f, %edx
callq 0x4260
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x7ea6
movq 0x18(%rsp), %rsi
leaq 0x34(%rsp), %rcx
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x10b3e
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xe(%rsp), %rcx
leaq 0xc(%rsp), %rdx
callq 0x10b3e
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0xa, %rax
je 0xfb23
leaq 0x436c(%rip), %rdi      # 0x13fed
leaq 0x426f(%rip), %rsi      # 0x13ef7
leaq 0x42da(%rip), %rcx      # 0x13f69
movl $0x67, %edx
callq 0x4260
leaq 0x42a0(%rip), %rdi      # 0x13f40
leaq 0x4250(%rip), %rsi      # 0x13ef7
leaq 0x42bb(%rip), %rcx      # 0x13f69
movl $0x59, %edx
callq 0x4260
leaq 0x4306(%rip), %rdi      # 0x13fc5
leaq 0x4231(%rip), %rsi      # 0x13ef7
leaq 0x429c(%rip), %rcx      # 0x13f69
movl $0x61, %edx
callq 0x4260
jmp 0xfd11
jmp 0xfd11
jmp 0xfd11
jmp 0xfd11
jmp 0xfd11
jmp 0xfd11
jmp 0xfd11
jmp 0xfcf5
jmp 0xfcf5
jmp 0xfcf5
jmp 0xfcf5
jmp 0xfd11
jmp 0xfd11
jmp 0xfcf5
jmp 0xfd11
movq %rax, %rbx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xfd14
callq 0x4340
jmp 0xfd14
jmp 0xfd11
jmp 0xfd11
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xfd23
callq 0x4340
movq %rbx, %rdi
callq 0x4640
nop