// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ebml.c
// Function: get_var_size_int
static int get_var_size_int(dbuf *f, i64 *val, i64 *pos,
	i64 nbytes_avail)
{
	i64 pos1;
	u8 b;
	u8 mask;
	unsigned int k;
	int retval = 0;
	u8 test_bit;
	unsigned int initial_zero_bits;

	pos1 = *pos;
	if(nbytes_avail<1) goto done;

	// This is an unsigned int. In a i64, we can support up to 63
	// bits.
	// For now we'll hope that 8 octets is the most we'll have to support,
	// but it's possible we'll have to support 9 or even more, which will
	// require additional logic.
	//
	//  1xxxxxxx width_nbits=0, octets=1, data_bits=7
	//  01xxxxxx width_nbits=1, octets=2, data_bits=14
	//  ...
	//  00000001 width_nbits=7, octets=8, data_bits=56
	//  00000000 1xxxxxxx width_nbits=8, octets=9, data_bits=63

	b = dbuf_getbyte(f, *pos);
	(*pos)++;

	test_bit = 0x80;
	initial_zero_bits = 0;
	while(1) {
		if(b>=test_bit) {
			break;
		}

		// "Not it". Try the next-larger number of initial 0 bits.
		initial_zero_bits++;
		test_bit >>= 1;
		if(test_bit==0) {
			goto done;
		}
	}

	mask = 0x7f >> initial_zero_bits;

	*val = (i64)(b & mask);

	// Read remaining bytes, if any.
	for(k=0; k<initial_zero_bits; k++) {
		if(*pos >= pos1+nbytes_avail) goto done;
		b = dbuf_getbyte(f, *pos);
		(*pos)++;
		if(*val > 0x07ffffffffffffffLL) {
			goto done;
		}
		*val = ((*val)<<8) | ((i64)b);
	}

	if(initial_zero_bits==0 && (*val)==0x7f) {
		// TODO: Fully handle "reserved" element value of all 1 bits.
		retval = 2;
		goto done;
	}

	retval = 1;

done:
	if(retval!=1) {
		*val = 0;
	}
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x20(%rbp)
jge 0x59a46
jmp 0x59b62
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movb $-0x80, -0x35(%rbp)
movl $0x0, -0x3c(%rbp)
movzbl -0x29(%rbp), %eax
movzbl -0x35(%rbp), %ecx
cmpl %ecx, %eax
jl 0x59a80
jmp 0x59aa2
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movzbl -0x35(%rbp), %eax
sarl %eax
movb %al, -0x35(%rbp)
movzbl -0x35(%rbp), %eax
cmpl $0x0, %eax
jne 0x59aa0
jmp 0x59b62
jmp 0x59a72
movl -0x3c(%rbp), %ecx
movl $0x7f, %eax
sarl %cl, %eax
movb %al, -0x2a(%rbp)
movzbl -0x29(%rbp), %eax
movzbl -0x2a(%rbp), %ecx
andl %ecx, %eax
movslq %eax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x59b42
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
addq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jl 0x59ae8
jmp 0x59b62
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movabsq $0x7ffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFF
cmpq %rcx, (%rax)
jle 0x59b1e
jmp 0x59b62
movq -0x10(%rbp), %rax
movq (%rax), %rcx
shlq $0x8, %rcx
movzbl -0x29(%rbp), %eax
orq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x59aca
cmpl $0x0, -0x3c(%rbp)
jne 0x59b5b
movq -0x10(%rbp), %rax
cmpq $0x7f, (%rax)
jne 0x59b5b
movl $0x2, -0x34(%rbp)
jmp 0x59b62
movl $0x1, -0x34(%rbp)
cmpl $0x1, -0x34(%rbp)
je 0x59b73
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0x34(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)