// File: theuni[P]libbtcnet/src/bareproxy.cpp
// Function: CBareProxy::read_final(bufferevent*, void*)
void CBareProxy::read_final(bufferevent* bev, void* ctx)
{
    CBareProxy* data = static_cast<CBareProxy*>(ctx);
    const CConnection& conn = data->m_proxy_connection;
    evbuffer* input = bufferevent_get_input(bev);
    size_t len = evbuffer_get_length(input);
    int result;
    assert(len >= 8);
    char pchRet2[5] = {};
    result = evbuffer_copyout(input, pchRet2, sizeof(pchRet2));
    assert(result == sizeof(pchRet2));
    (void)result;
    if (pchRet2[0] != 0x05 || pchRet2[1] != 0x00) {
        data->ProxyFailure(0);
        return;
    }
    size_t needSize = 6;
    size_t addrsize;
    int sockaddr_size;
    bool ip = true;
    sockaddr_storage addr;
    memset(&addr, 0, sizeof(addr));
    void* writeip = nullptr;
    switch (pchRet2[3]) {
    case 0x01:
        addrsize = 4;
        addr.ss_family = AF_INET;
        sockaddr_size = sizeof(sockaddr_in);
        reinterpret_cast<sockaddr_in*>(&addr)->sin_port = htons(conn.GetPort());
        writeip = &(reinterpret_cast<sockaddr_in*>(&addr)->sin_addr);
        break;
    case 0x04:
        addrsize = 16;
        addr.ss_family = AF_INET6;
        sockaddr_size = sizeof(sockaddr_in6);
        reinterpret_cast<sockaddr_in6*>(&addr)->sin6_port = htons(conn.GetPort());
        writeip = &(reinterpret_cast<sockaddr_in6*>(&addr)->sin6_addr);
        break;
    case 0x03:
        addrsize = pchRet2[4];
        needSize += 1;
        ip = false;
        sockaddr_size = 0;
        break;
    default:
        data->ProxyFailure(0);
        return;
    }

    needSize += addrsize;

    if (len < needSize) {
        bufferevent_setwatermark(bev, EV_READ, needSize, 0);
        return;
    }

    if (ip) {
        result = evbuffer_drain(input, 4);
        assert(result == 0);
        (void)result;
        result = evbuffer_remove(input, writeip, addrsize);
        assert(result == (int)addrsize);
        (void)result;
        // Throw this away.
        char port[2] = {};
        result = evbuffer_remove(input, port, sizeof(port));
        assert(result == sizeof(port));
        (void)result;
    } else {
        result = evbuffer_drain(input, 5 + addrsize + 2);
        assert(result == 0);
        (void)result;
    }

    CConnection ret = conn;
    if (ip && conn.IsDNS() && conn.GetOptions().doResolve == CConnectionOptions::RESOLVE_ONLY)
        ret = CConnection(conn.GetOptions(), conn.GetNetConfig(), conn.GetProxy(), reinterpret_cast<sockaddr*>(&addr), sockaddr_size);
    data->OnProxySuccess(std::move(data->m_bev), std::move(ret));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x710, %rsp            # imm = 0x710
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5300
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5440
movq %rax, -0x30(%rbp)
cmpq $0x8, -0x30(%rbp)
jb 0x1fc6a
jmp 0x1fc89
leaq 0x752e(%rip), %rdi      # 0x2719f
leaq 0x741c(%rip), %rsi      # 0x27094
movl $0x8a, %edx
leaq 0x7524(%rip), %rcx      # 0x271a8
callq 0x52e0
leaq -0x39(%rbp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x5220
movq -0x28(%rbp), %rdi
leaq -0x39(%rbp), %rsi
movl $0x5, %edx
callq 0x5080
movl %eax, -0x34(%rbp)
movslq -0x34(%rbp), %rax
cmpq $0x5, %rax
jne 0x1fcba
jmp 0x1fcd9
leaq 0x7521(%rip), %rdi      # 0x271e2
leaq 0x73cc(%rip), %rsi      # 0x27094
movl $0x8d, %edx
leaq 0x74d4(%rip), %rcx      # 0x271a8
callq 0x52e0
movsbl -0x39(%rbp), %eax
cmpl $0x5, %eax
jne 0x1fceb
movsbl -0x38(%rbp), %eax
cmpl $0x0, %eax
je 0x1fcfb
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x1f2b0
jmp 0x201b2
movq $0x6, -0x48(%rbp)
movb $0x1, -0x55(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x6a0(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movq $0x0, -0xe8(%rbp)
movsbl -0x36(%rbp), %eax
movl %eax, -0x684(%rbp)
subl $0x1, %eax
je 0x1fd80
jmp 0x1fd5f
movl -0x684(%rbp), %eax
subl $0x3, %eax
je 0x1fe08
jmp 0x1fd70
movl -0x684(%rbp), %eax
subl $0x4, %eax
je 0x1fdc4
jmp 0x1fe2a
movq $0x4, -0x50(%rbp)
movw $0x2, -0xe0(%rbp)
movl $0x10, -0x54(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9fb0
movzwl %ax, %edi
callq 0x5090
movw %ax, -0xde(%rbp)
leaq -0xe0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1fe3a
movq $0x10, -0x50(%rbp)
movw $0xa, -0xe0(%rbp)
movl $0x1c, -0x54(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9fb0
movzwl %ax, %edi
callq 0x5090
movw %ax, -0xde(%rbp)
leaq -0xe0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1fe3a
movsbq -0x35(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movb $0x0, -0x55(%rbp)
movl $0x0, -0x54(%rbp)
jmp 0x1fe3a
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x1f2b0
jmp 0x201b2
movq -0x50(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x1fe6b
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
callq 0x50f0
jmp 0x201b2
testb $0x1, -0x55(%rbp)
je 0x1ff48
movq -0x28(%rbp), %rdi
movl $0x4, %esi
callq 0x5490
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x1fe8e
jmp 0x1fead
leaq 0x59ed(%rip), %rdi      # 0x25882
leaq 0x71f8(%rip), %rsi      # 0x27094
movl $0xbd, %edx
leaq 0x7300(%rip), %rcx      # 0x271a8
callq 0x52e0
movq -0x28(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x54e0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl %ecx, %eax
jne 0x1fed1
jmp 0x1fef0
leaq 0x7324(%rip), %rdi      # 0x271fc
leaq 0x71b5(%rip), %rsi      # 0x27094
movl $0xc0, %edx
leaq 0x72bd(%rip), %rcx      # 0x271a8
callq 0x52e0
leaq -0xea(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x5220
movq -0x28(%rbp), %rdi
leaq -0xea(%rbp), %rsi
movl $0x2, %edx
callq 0x54e0
movl %eax, -0x34(%rbp)
movslq -0x34(%rbp), %rax
cmpq $0x2, %rax
jne 0x1ff27
jmp 0x1ff46
leaq 0x72e6(%rip), %rdi      # 0x27214
leaq 0x715f(%rip), %rsi      # 0x27094
movl $0xc5, %edx
leaq 0x7267(%rip), %rcx      # 0x271a8
callq 0x52e0
jmp 0x1ff89
movq -0x28(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x5, %rsi
addq $0x2, %rsi
callq 0x5490
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x1ff68
jmp 0x1ff87
leaq 0x5913(%rip), %rdi      # 0x25882
leaq 0x711e(%rip), %rsi      # 0x27094
movl $0xc9, %edx
leaq 0x7226(%rip), %rcx      # 0x271a8
callq 0x52e0
jmp 0x1ff89
movq -0x20(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0xc230
testb $0x1, -0x55(%rbp)
je 0x2014a
movq -0x20(%rbp), %rdi
callq 0x9c70
movb %al, -0x6a1(%rbp)
jmp 0x1ffb4
movb -0x6a1(%rbp), %al
testb $0x1, %al
jne 0x1ffc3
jmp 0x2014a
movq -0x20(%rbp), %rdi
callq 0xa9c0
movq %rax, -0x6b0(%rbp)
jmp 0x1ffd5
movq -0x6b0(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0x2014a
movq -0x20(%rbp), %rdi
callq 0xa9c0
movq %rax, -0x6b8(%rbp)
jmp 0x1fff8
movq -0x6b8(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x1c(%rax), %xmm2
movups %xmm2, -0x404(%rbp)
movaps %xmm1, -0x410(%rbp)
movaps %xmm0, -0x420(%rbp)
movq -0x20(%rbp), %rdi
callq 0xa9e0
movq %rax, -0x6c0(%rbp)
jmp 0x20031
movq -0x6c0(%rbp), %rsi
leaq -0x458(%rbp), %rdi
callq 0x6fb0
jmp 0x20046
movq -0x20(%rbp), %rdi
callq 0xa9a0
movq %rax, -0x6c8(%rbp)
jmp 0x20058
movq -0x6c8(%rbp), %rsi
leaq -0x508(%rbp), %rdi
callq 0xc3b0
jmp 0x2006d
movl -0x54(%rbp), %r8d
movups -0x404(%rbp), %xmm0
movq %rsp, %rax
movups %xmm0, 0x1c(%rax)
movaps -0x420(%rbp), %xmm0
movaps -0x410(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x3f0(%rbp), %rdi
leaq -0x458(%rbp), %rsi
leaq -0x508(%rbp), %rdx
leaq -0xe0(%rbp), %rcx
callq 0xa750
jmp 0x200b7
leaq -0x268(%rbp), %rdi
leaq -0x3f0(%rbp), %rsi
callq 0x20470
leaq -0x3f0(%rbp), %rdi
callq 0x68f0
leaq -0x508(%rbp), %rdi
callq 0x6950
leaq -0x458(%rbp), %rdi
callq 0x6930
jmp 0x2014a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x201d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x20139
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x508(%rbp), %rdi
callq 0x6950
leaq -0x458(%rbp), %rdi
callq 0x6930
jmp 0x201d9
movq -0x18(%rbp), %rax
movq %rax, -0x6e0(%rbp)
addq $0x10, %rax
movq %rax, -0x6d8(%rbp)
leaq -0x680(%rbp), %rdi
movq %rdi, -0x6d0(%rbp)
leaq -0x268(%rbp), %rsi
callq 0x11a70
movq -0x6e0(%rbp), %rdi
movq -0x6d8(%rbp), %rsi
movq -0x6d0(%rbp), %rdx
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x2019a
leaq -0x680(%rbp), %rdi
callq 0x68f0
leaq -0x268(%rbp), %rdi
callq 0x68f0
addq $0x710, %rsp            # imm = 0x710
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x680(%rbp), %rdi
callq 0x68f0
leaq -0x268(%rbp), %rdi
callq 0x68f0
movq -0x270(%rbp), %rdi
callq 0x5710
nopw %cs:(%rax,%rax)