// File: theuni[P]libbtcnet/src/bareproxy.cpp
// Function: CBareProxy::InitProxy(event_type<bufferevent>&&)
void CBareProxy::InitProxy(event_type<bufferevent>&& bev)
{
    assert(bev);
    assert(!m_bev);
    m_bev.swap(bev);
    static const uint8_t pchSocks5Init[] = {0x05, 0x01, 0x00};
    static const uint8_t pchSocks5InitAuth[] = {0x05, 0x02, 0x02, 0x00};

    bufferevent_setcb(m_bev, receive_init, nullptr, event_cb, this);
    bufferevent_setwatermark(m_bev, EV_READ, 2, 0);

    // TODO: Need a proxy-specific timeout here.
    timeval recvTimeout = {m_proxy_connection.GetOptions().nRecvTimeout, 0};
    timeval sendTimeout = {m_proxy_connection.GetOptions().nSendTimeout, 0};
    bufferevent_set_timeouts(m_bev, &recvTimeout, &sendTimeout);

    const CProxy& proxy = m_proxy_connection.GetProxy();
    assert(proxy.IsSet());
    const CProxyAuth& auth = proxy.GetAuth();
    if (auth.IsSet())
        bufferevent_write(m_bev, pchSocks5InitAuth, sizeof(pchSocks5InitAuth));
    else
        bufferevent_write(m_bev, pchSocks5Init, sizeof(pchSocks5Init));

    bufferevent_enable(m_bev, EV_READ | EV_WRITE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x19d80
testb $0x1, %al
jne 0x1ef47
jmp 0x1ef49
jmp 0x1ef68
leaq 0x78d5(%rip), %rdi      # 0x26825
leaq 0x813d(%rip), %rsi      # 0x27094
movl $0x1d, %edx
leaq 0x8143(%rip), %rcx      # 0x270a6
callq 0x52e0
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a0e0
testb $0x1, %al
jne 0x1ef7b
jmp 0x1ef7d
jmp 0x1ef9c
leaq 0x789e(%rip), %rdi      # 0x26822
leaq 0x8109(%rip), %rsi      # 0x27094
movl $0x1e, %edx
leaq 0x810f(%rip), %rcx      # 0x270a6
callq 0x52e0
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
movq -0x10(%rbp), %rsi
callq 0x202b0
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a1a0
movq -0x48(%rbp), %r8
movq %rax, %rdi
leaq 0x158(%rip), %rsi       # 0x1f120
xorl %eax, %eax
movl %eax, %edx
leaq 0x26d(%rip), %rcx       # 0x1f240
callq 0x5410
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a1a0
movq %rax, %rdi
movl $0x2, %esi
movl $0x2, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x50f0
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xa9c0
movq %rax, %rcx
movq -0x48(%rbp), %rax
movslq 0x10(%rcx), %rcx
movq %rcx, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq 0x8(%rax), %rdi
callq 0xa9c0
movq -0x48(%rbp), %rdi
movslq 0x14(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
addq $0x10, %rdi
callq 0x1a1a0
movq %rax, %rdi
leaq -0x20(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x55d0
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xa9a0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xa100
testb $0x1, %al
jne 0x1f075
jmp 0x1f077
jmp 0x1f096
leaq 0x7ffb(%rip), %rdi      # 0x27079
leaq 0x800f(%rip), %rsi      # 0x27094
movl $0x2c, %edx
leaq 0x8015(%rip), %rcx      # 0x270a6
callq 0x52e0
movq -0x38(%rbp), %rdi
callq 0x9c30
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x9400
testb $0x1, %al
jne 0x1f0b2
jmp 0x1f0d5
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a1a0
movq %rax, %rdi
leaq 0x817d(%rip), %rsi      # 0x27246
movl $0x4, %edx
callq 0x5240
jmp 0x1f0f6
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a1a0
movq %rax, %rdi
leaq 0x8157(%rip), %rsi      # 0x27243
movl $0x3, %edx
callq 0x5240
movq -0x48(%rbp), %rdi
addq $0x10, %rdi
callq 0x1a1a0
movq %rax, %rdi
movl $0x6, %esi
callq 0x5750
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)