// File: theuni[P]libbtcnet/src/message.cpp
// Function: first_complete_message_size(CNetworkConfig const&, evbuffer*, bool&, bool&)
uint64_t first_complete_message_size(const CNetworkConfig& config, evbuffer* input, bool& fComplete, bool& fBadMsgStart)
{
    size_t nTotal = evbuffer_get_length(input);
    uint64_t nMessageSize;
    const int size_needed = config.header_msg_size_offset + config.header_msg_size_size;
    fComplete = false;
    fBadMsgStart = false;

    // Assume 4-bytes until there's a reason not to.
    assert(config.header_msg_size_size == 4);

    if (nTotal < static_cast<size_t>(size_needed))
        return 0;
    evbuffer_iovec v;
    if (evbuffer_peek(input, size_needed, nullptr, &v, 1) == 1) {
        const unsigned char* ptr = static_cast<const unsigned char*>(v.iov_base);
        if (!config.message_start.empty() && memcmp(ptr, &config.message_start[0], config.message_start.size()) != 0) {
            fBadMsgStart = true;
            return 0;
        }
        nMessageSize = get_message_length(ptr + config.header_msg_size_offset) + config.header_size;
    } else {
        std::vector<unsigned char> partial_header(size_needed);
        int ret = evbuffer_copyout(input, &partial_header[0], size_needed);
        assert(ret == size_needed);
        (void)ret;
        if (!config.message_start.empty() && memcmp(&partial_header[0], &config.message_start[0], config.message_start.size()) != 0) {
            fBadMsgStart = true;
            return 0;
        }
        nMessageSize = get_message_length(&partial_header[0] + config.header_msg_size_offset) + config.header_size;
    }
    if (nTotal >= nMessageSize)
        fComplete = true;
    return nMessageSize;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5440
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
addl 0x4(%rcx), %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x4, 0x4(%rax)
jne 0x1e362
jmp 0x1e381
leaq 0x8c3e(%rip), %rdi      # 0x26fa7
leaq 0x8c58(%rip), %rsi      # 0x26fc8
movl $0x23, %edx
leaq 0x8c5c(%rip), %rcx      # 0x26fd8
callq 0x52e0
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1e39b
movq $0x0, -0x8(%rbp)
jmp 0x1e63c
movq -0x18(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq -0x50(%rbp), %rcx
movl $0x1, %r8d
callq 0x57a0
cmpl $0x1, %eax
jne 0x1e45e
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0xaf30
testb $0x1, %al
jne 0x1e437
movq -0x58(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xaf70
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x6b00
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %rax, %rdx
callq 0x51e0
cmpl $0x0, %eax
je 0x1e437
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x1e63c
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdi
callq 0x1e660
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x1e623
movslq -0x3c(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x5ba0
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x1dc40
jmp 0x1e492
leaq -0x71(%rbp), %rdi
callq 0x5c40
movq -0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x1e6a0
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
movslq -0x3c(%rbp), %rdx
callq 0x5080
movq %rax, -0xb8(%rbp)
jmp 0x1e4cf
movq -0xb8(%rbp), %rax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0x1e4e9
jmp 0x1e542
leaq 0x8b41(%rip), %rdi      # 0x27031
leaq 0x8ad1(%rip), %rsi      # 0x26fc8
movl $0x32, %edx
leaq 0x8ad5(%rip), %rcx      # 0x26fd8
callq 0x52e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x5c40
jmp 0x1e649
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c60
jmp 0x1e649
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0xaf30
testb $0x1, %al
jne 0x1e5c2
leaq -0x70(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1e6a0
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xaf70
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x6b00
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq %rax, %rdx
callq 0x51e0
cmpl $0x0, %eax
je 0x1e5c2
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
movq $0x0, -0x8(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x1e60a
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x1e6a0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdi
callq 0x1e660
movq %rax, -0xd8(%rbp)
jmp 0x1e5ea
movq -0xd8(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x8c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c60
movl -0x8c(%rbp), %eax
testl %eax, %eax
je 0x1e621
jmp 0x1e61f
jmp 0x1e63c
jmp 0x1e623
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x1e634
movq -0x20(%rbp), %rax
movb $0x1, (%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x5710
nopw %cs:(%rax,%rax)