// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl $0x9, %r12d
leaq -0x29(%rbp), %r13
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2180
movl %r12d, %r14d
subl $0x1, %r14d
setb %cl
movb -0x29(%rbp), %al
cmpb $0x1, %al
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x4266
movq (%r15), %rcx
movq -0x18(%rcx), %rcx
cmpl $0x0, 0x20(%r15,%rcx)
jne 0x42b3
andb $0x7f, %al
movb %al, -0x2a(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x4240
movb %al, (%rsi)
incq -0x48(%rbp)
jmp 0x424d
leaq -0x50(%rbp), %rdi
leaq -0x2a(%rbp), %rdx
callq 0x47ac
cmpb $0x0, -0x29(%rbp)
movb $0x1, %cl
jns 0x4300
testl %r12d, %r12d
movl %r14d, %r12d
jne 0x41ed
jmp 0x4300
movq 0x3d83(%rip), %r14      # 0x7ff0
leaq 0x1324(%rip), %rsi      # 0x5598
movl $0x1d, %edx
movq %r14, %rdi
callq 0x2160
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x3d51(%rip), %rdi      # 0x7ff0
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x42fe
movq 0x3d36(%rip), %r14      # 0x7ff0
leaq 0x12f5(%rip), %rsi      # 0x55b6
movl $0x26, %edx
movq %r14, %rdi
callq 0x2160
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x3d04(%rip), %rdi      # 0x7ff0
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
xorl %ecx, %ecx
testb %cl, %cl
je 0x434e
movq $0x0, (%rbx)
movq -0x50(%rbp), %r8
movq -0x48(%rbp), %rdx
subq %r8, %rdx
xorl %eax, %eax
testl %edx, %edx
jle 0x434e
movq (%rbx), %rcx
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
decq %rdi
shlq $0x7, %rcx
movq %rcx, (%rbx)
movl %edi, %esi
cmpq %rsi, %rdx
jbe 0x436f
movzbl (%r8,%rsi), %esi
orq %rsi, %rcx
movq %rcx, (%rbx)
decq %rdi
leal 0x2(%rdi), %esi
cmpl $0x1, %esi
jg 0x432a
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x4360
movl %eax, %ebx
callq 0x2110
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x130e(%rip), %rdi      # 0x5684
xorl %eax, %eax
callq 0x21c0
jmp 0x4383
jmp 0x4383
jmp 0x4383
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x4394
callq 0x2110
movq %rbx, %rdi
callq 0x2220