// File: /workspace/github_cpp_makefile_star2_O3/adamjanovsky[P]PA193_test_parser_monero/src/tools.cpp
// Function: tools::read_varint(std::istream&, unsigned long&)
int tools::read_varint(std::istream & in, unsigned long & val) {
    
    // We will load the varint into a vector and then deserialize it
    std::vector<unsigned char> ser_vect;
    unsigned char c;
    
    // 10 bytes is maximum for the varint to fit in 64 bit int
    // and the last serialized val must be 0x01 so that the int does not overflow
    // because each byte contains 7 bits and 9 * 7 + 1 = 63 + 1 = 64
    for (int i = 0; i < 10; i++) {
        in.read((char *)&c, 1);
        
        // Check for overflow
        if (i == 9 and c != 0x01) {
            std::cerr << "Read varint: Varint overflow." << std::endl;
            return ERR_READV_OVERFLOW;
        }
        
        // Check for EOF
        if (!in.good()) {
            std::cerr << "Read varint: Unexpected end of stream." << std::endl;
            return ERR_READV_UNEXPECTED_EOF;
        }
        
        // Strip the first bit and save it into the vector
        ser_vect.push_back(c & 0x7f);
        
        if ((c & 0x80) == 0x00) {
            // we have read the last byte
            break;
        }

    }
    
    val = 0;
    int last_index = static_cast<int>(ser_vect.size()) - 1;
    
    for (int i = last_index; i >= 0; i--) {
        val <<= 7;
        val |= ser_vect.at(i);
    }
    
    return OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl $0x9, %r15d
leaq -0x29(%rbp), %r12
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2180
movl %r15d, %r13d
subl $0x1, %r13d
setb %cl
movb -0x29(%rbp), %al
cmpb $0x1, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x42c8
movq (%r14), %rcx
movq -0x18(%rcx), %rcx
cmpl $0x0, 0x20(%r14,%rcx)
jne 0x4315
andb $0x7f, %al
movb %al, -0x2a(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x425c
movb %al, (%rsi)
incq -0x48(%rbp)
jmp 0x4269
leaq -0x50(%rbp), %rdi
leaq -0x2a(%rbp), %rdx
callq 0x47ac
cmpb $0x0, -0x29(%rbp)
jns 0x4277
testl %r15d, %r15d
movl %r13d, %r15d
jne 0x4205
movq $0x0, (%rbx)
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rdx
subq %rax, %rdx
testl %edx, %edx
jle 0x42c1
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
decq %rcx
xorl %edi, %edi
shlq $0x7, %rdi
movq %rdi, (%rbx)
movl %ecx, %esi
cmpq %rsi, %rdx
jbe 0x437f
movzbl (%rax,%rsi), %esi
orq %rsi, %rdi
movq %rdi, (%rbx)
decq %rcx
leal 0x2(%rcx), %esi
cmpl $0x1, %esi
jg 0x429a
xorl %ebx, %ebx
jmp 0x4360
movq 0x3d21(%rip), %rbx      # 0x7ff0
leaq 0x12c2(%rip), %rsi      # 0x5598
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x2160
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x3cef(%rip), %rdi      # 0x7ff0
callq 0x2040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x20d0
jmp 0x4360
movq 0x3cd4(%rip), %rbx      # 0x7ff0
leaq 0x1293(%rip), %rsi      # 0x55b6
movl $0x26, %edx
movq %rbx, %rdi
callq 0x2160
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x3ca2(%rip), %rdi      # 0x7ff0
callq 0x2040
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movq %rax, %rdi
callq 0x20d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x436e
callq 0x2110
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12fe(%rip), %rdi      # 0x5684
xorl %eax, %eax
callq 0x21c0
jmp 0x4393
jmp 0x4393
jmp 0x4393
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x43a4
callq 0x2110
movq %rbx, %rdi
callq 0x2220