// File: auton1[P]viterbi/bgzf.c
// Function: bgzf_read_block
int bgzf_read_block(BGZF *fp)
{
	uint8_t header[BLOCK_HEADER_LENGTH], *compressed_block;
	int count, size = 0, block_length, remaining;
	int64_t block_address;
	block_address = _bgzf_tell((_bgzf_file_t)fp->fp);
	if (fp->cache_size && load_block_from_cache(fp, block_address)) return 0;
	count = _bgzf_read(fp->fp, header, sizeof(header));
	if (count == 0) { // no data read
		fp->block_length = 0;
		return 0;
	}
	if (count != sizeof(header) || !check_header(header)) {
		fp->errcode |= BGZF_ERR_HEADER;
		return -1;
	}
	size = count;
	block_length = unpackInt16((uint8_t*)&header[16]) + 1; // +1 because when writing this number, we used "-1"
	compressed_block = (uint8_t*)fp->compressed_block;
	memcpy(compressed_block, header, BLOCK_HEADER_LENGTH);
	remaining = block_length - BLOCK_HEADER_LENGTH;
	count = _bgzf_read(fp->fp, &compressed_block[BLOCK_HEADER_LENGTH], remaining);
	if (count != remaining) {
		fp->errcode |= BGZF_ERR_IO;
		return -1;
	}
	size += count;
	if ((count = inflate_block(fp, block_length)) < 0) return -1;
	if (fp->block_length != 0) fp->block_offset = 0; // Do not reset offset if this read follows a seek.
	fp->block_address = block_address;
	fp->block_length = count;
	cache_block(fp, size);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x66e0
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x71ebc
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x72050
cmpl $0x0, %eax
je 0x71ebc
movl $0x0, -0x4(%rbp)
jmp 0x7203c
leaq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movl $0x1, %esi
movl $0x12, %edx
callq 0x6840
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x71ef7
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x7203c
movslq -0x3c(%rbp), %rax
cmpq $0x12, %rax
jne 0x71f0f
leaq -0x30(%rbp), %rdi
callq 0x72060
cmpl $0x0, %eax
jne 0x71f3c
movq -0x10(%rbp), %rax
movl (%rax), %edx
shll $0x10, %edx
sarl $0x10, %edx
orl $0x2, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7203c
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
addq $0x10, %rdi
callq 0x72120
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movw -0x20(%rbp), %cx
movw %cx, 0x10(%rax)
movl -0x44(%rbp), %eax
subl $0x12, %eax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
addq $0x12, %rdi
movslq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movl $0x1, %esi
callq 0x6840
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x71fd8
movq -0x10(%rbp), %rax
movl (%rax), %edx
shll $0x10, %edx
sarl $0x10, %edx
orl $0x4, %edx
movl (%rax), %ecx
andl $0xffff, %edx           # imm = 0xFFFF
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
orl %edx, %ecx
movl %ecx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7203c
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x72140
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
jge 0x71ffe
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7203c
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x72013
movq -0x10(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x72280
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)