// File: theuni[P]libbtcnet/src/connectionbase.cpp
// Function: ConnectionBase::read_cb_message(bufferevent*, void*)
void ConnectionBase::read_cb_message(bufferevent* bev, void* ctx)
{
    assert(ctx);
    assert(bev);
    ConnectionBase* base = static_cast<ConnectionBase*>(ctx);
    const CNetworkConfig& netconfig = base->m_connection.GetNetConfig();
    std::list<std::vector<unsigned char> > msgs;
    size_t totalsize = 0;
    bool fTooBig = false;
    bool fBadMsgStart = false;
    {
        BufferEventLocker lock(bev);
        evbuffer* input = bufferevent_get_input(bev);

        uint64_t msgsize = 0;
        bool fComplete = false;
        do {
            msgsize = first_complete_message_size(netconfig, input, fComplete, fBadMsgStart);
            if (netconfig.message_max_size > 0 && msgsize > netconfig.message_max_size) {
                DEBUG_PRINT(LOGWARN, "id:", base->m_id, "Received an oversized message");
                if (fComplete) {
                    evbuffer_drain(input, msgsize);
                }
                fTooBig = true;
                break;
            } else if (fBadMsgStart) {
                DEBUG_PRINT(LOGWARN, "id:", base->m_id, "Received a bad message start");
                break;
            } else if ((msgsize != 0u) && fComplete) {
                msgs.emplace_back(msgsize, 0);
                evbuffer_remove(input, msgs.back().data(), msgsize);
                totalsize += msgsize;
            }
        } while (fComplete);

        if ((msgsize != 0u) && !fBadMsgStart) {
            size_t buflen = evbuffer_get_length(input);
            if (buflen < msgsize + netconfig.header_size && !fTooBig)
                evbuffer_expand(input, msgsize + netconfig.header_size - buflen);
            bufferevent_setwatermark(bev, EV_READ, msgsize, msgsize + netconfig.message_max_size);
            DEBUG_PRINT(LOGVERBOSE, "id:", base->m_id, "watermark set to", msgsize);
        } else if (!fBadMsgStart && !fTooBig) {
            size_t watermark = netconfig.header_msg_size_offset + netconfig.header_msg_size_size;
            bufferevent_setwatermark(bev, EV_READ, watermark, netconfig.message_max_size);
            DEBUG_PRINT(LOGVERBOSE, "id:", base->m_id, "watermark set to", watermark);
        }
    }

    if (totalsize != 0u) {
        DEBUG_PRINT(LOGINFO, "id:", base->m_id, "Received", msgs.size(), "messages");
        base->m_handler.OnReceiveMessages(base->m_id, std::move(msgs), totalsize);
    }

    if (fTooBig || fBadMsgStart) {
        base->m_handler.OnMalformedMessage(base->m_id);
        base->DisconnectInt(0);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1bb0c
jmp 0x1bb2b
leaq 0xb09c(%rip), %rdi      # 0x26baf
leaq 0xae5e(%rip), %rsi      # 0x26978
movl $0x169, %edx            # imm = 0x169
leaq 0xb21f(%rip), %rcx      # 0x26d45
callq 0x52e0
cmpq $0x0, -0x8(%rbp)
je 0x1bb34
jmp 0x1bb53
leaq 0xacea(%rip), %rdi      # 0x26825
leaq 0xae36(%rip), %rsi      # 0x26978
movl $0x16a, %edx            # imm = 0x16A
leaq 0xb1f7(%rip), %rcx      # 0x26d45
callq 0x52e0
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
callq 0xa9e0
movq %rax, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1c910
movq $0x0, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movb $0x0, -0x42(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x1c730
jmp 0x1bb94
movq -0x8(%rbp), %rdi
callq 0x5300
movq %rax, -0x98(%rbp)
jmp 0x1bba6
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x71(%rbp)
movq -0x20(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0x71(%rbp), %rdx
leaq -0x42(%rbp), %rcx
callq 0x1e310
movq %rax, -0xa0(%rbp)
jmp 0x1bbdb
movq -0xa0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jbe 0x1bc76
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x1bc76
movq -0x18(%rbp), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xb172(%rip), %rdi      # 0x26d88
leaq 0xa079(%rip), %rsi      # 0x25c96
leaq 0xb174(%rip), %rcx      # 0x26d98
callq 0x1ca00
jmp 0x1bc2b
testb $0x1, -0x71(%rbp)
je 0x1bc6d
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x5490
jmp 0x1bc40
jmp 0x1bc6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x1bf11
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1c760
jmp 0x1bf11
movb $0x1, -0x41(%rbp)
jmp 0x1bd19
testb $0x1, -0x42(%rbp)
je 0x1bca5
movq -0x18(%rbp), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xb0fa(%rip), %rdi      # 0x26d88
leaq 0xa001(%rip), %rsi      # 0x25c96
leaq 0xb11a(%rip), %rcx      # 0x26db6
callq 0x1ca20
jmp 0x1bca3
jmp 0x1bd19
cmpq $0x0, -0x70(%rbp)
je 0x1bd09
testb $0x1, -0x71(%rbp)
je 0x1bd09
movl $0x0, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x1ca40
jmp 0x1bccc
movq -0x68(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1c970
movq %rax, %rdi
callq 0xcee0
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
movq -0x70(%rbp), %rdx
callq 0x54e0
jmp 0x1bcfd
movq -0x70(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x1bd0b
jmp 0x1bd0d
jmp 0x1bd0f
testb $0x1, -0x71(%rbp)
jne 0x1bbbd
cmpq $0x0, -0x70(%rbp)
je 0x1bdda
testb $0x1, -0x42(%rbp)
jne 0x1bdda
movq -0x68(%rbp), %rdi
callq 0x5440
movq %rax, -0xb0(%rbp)
jmp 0x1bd40
movq -0xb0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x20(%rbp), %rdx
movslq 0x8(%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x1bd8c
testb $0x1, -0x41(%rbp)
jne 0x1bd8c
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x20(%rbp), %rax
movslq 0x8(%rax), %rax
addq %rax, %rsi
movq -0x80(%rbp), %rax
subq %rax, %rsi
callq 0x5200
jmp 0x1bd8a
jmp 0x1bd8c
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movq %rdx, %rcx
addq %rax, %rcx
movl $0x2, %esi
callq 0x50f0
jmp 0x1bdad
movq -0x18(%rbp), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xafc9(%rip), %rdi      # 0x26d88
leaq 0x9ed0(%rip), %rsi      # 0x25c96
leaq 0xb006(%rip), %rcx      # 0x26dd3
leaq -0x70(%rbp), %r8
callq 0x1ca80
jmp 0x1bdd8
jmp 0x1be4c
testb $0x1, -0x42(%rbp)
jne 0x1be4a
testb $0x1, -0x41(%rbp)
jne 0x1be4a
movq -0x20(%rbp), %rcx
movl (%rcx), %eax
movl 0x4(%rcx), %ecx
addl %ecx, %eax
cltq
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rdx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %ecx
movl $0x2, %esi
callq 0x50f0
jmp 0x1be1a
movq -0x18(%rbp), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xaf5c(%rip), %rdi      # 0x26d88
leaq 0x9e63(%rip), %rsi      # 0x25c96
leaq 0xaf99(%rip), %rcx      # 0x26dd3
leaq -0x88(%rbp), %r8
callq 0x1ca80
jmp 0x1be48
jmp 0x1be4a
jmp 0x1be4c
leaq -0x50(%rbp), %rdi
callq 0x1c760
cmpq $0x0, -0x40(%rbp)
je 0x1bece
movq -0x18(%rbp), %rax
addq $0x190, %rax            # imm = 0x190
movq %rax, -0xb8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1c9d0
movq -0xb8(%rbp), %rdx
movq %rax, -0x90(%rbp)
leaq 0xaefd(%rip), %rdi      # 0x26d88
leaq 0x9e04(%rip), %rsi      # 0x25c96
leaq 0xaf4b(%rip), %rcx      # 0x26de4
leaq 0xaf4d(%rip), %r9       # 0x26ded
leaq -0x90(%rbp), %r8
callq 0x1caa0
jmp 0x1beae
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x190(%rax), %rsi
movq -0x40(%rbp), %rcx
leaq -0x38(%rbp), %rdx
callq 0xf9f0
jmp 0x1becc
jmp 0x1bece
testb $0x1, -0x41(%rbp)
jne 0x1beda
testb $0x1, -0x42(%rbp)
je 0x1beff
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x190(%rax), %rsi
callq 0xfb60
jmp 0x1bef0
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x1a770
jmp 0x1befd
jmp 0x1beff
leaq -0x38(%rbp), %rdi
callq 0x11a50
addq $0xc0, %rsp
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x11a50
movq -0x58(%rbp), %rdi
callq 0x5710
nopw %cs:(%rax,%rax)