// File: /workspace/github_c_makefile_star2_O0/DReichLab[P]adna/sam.c
// Function: bam_index
hts_idx_t *bam_index(BGZF *fp, int min_shift)
{
	int n_lvls, i, fmt;
	bam1_t *b;
	hts_idx_t *idx;
	bam_hdr_t *h;
	h = bam_hdr_read(fp);
	if (min_shift > 0) {
		int64_t max_len = 0, s;
		for (i = 0; i < h->n_targets; ++i)
			if (max_len < h->target_len[i]) max_len = h->target_len[i];
		max_len += 256;
		for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
		fmt = HTS_FMT_CSI;
	} else min_shift = 14, n_lvls = 5, fmt = HTS_FMT_BAI;
	idx = hts_idx_init(h->n_targets, fmt, bgzf_tell(fp), min_shift, n_lvls);
	bam_hdr_destroy(h);
	b = bam_init1();
	while (bam_read1(fp, b) >= 0) {
		int l, ret;
		l = bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
		if (l == 0) l = 1; // no zero-length records
		ret = hts_idx_push(idx, b->core.tid, b->core.pos, b->core.pos + l, bgzf_tell(fp), !(b->core.flag&BAM_FUNMAP));
		if (ret < 0) break; // unsorted
	}
	hts_idx_finish(idx, bgzf_tell(fp));
	bam_destroy1(b);
	return idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0xc830
movq %rax, -0x30(%rbp)
cmpl $0x0, -0xc(%rbp)
jle 0xd8e9
movq $0x0, -0x38(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xd898
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x14(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
cmpq %rcx, %rax
jge 0xd88b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0x38(%rbp)
jmp 0xd88d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xd855
movq -0x38(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x38(%rbp)
movl $0x0, -0x10(%rbp)
movl -0xc(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0xd8e0
jmp 0xd8c9
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
movq -0x40(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x40(%rbp)
jmp 0xd8bd
movl $0x0, -0x18(%rbp)
jmp 0xd8fe
movl $0xe, -0xc(%rbp)
movl $0x5, -0x10(%rbp)
movl $0x1, -0x18(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %edi
movl -0x18(%rbp), %esi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdx
shlq $0x10, %rdx
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rdx
movl -0xc(%rbp), %ecx
movl -0x10(%rbp), %r8d
callq 0x88d0
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rdi
callq 0xc780
callq 0xce60
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xd090
cmpl $0x0, %eax
jl 0xda0e
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x30, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x18, %rax
andq $0xff, %rax
cltq
addq %rax, %rsi
callq 0xd020
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0xd99e
movl $0x1, -0x44(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %esi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
addl -0x44(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %r8
shlq $0x10, %r8
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %r8
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x9320
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0xda09
jmp 0xda0e
jmp 0xd946
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x10, %rsi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rsi
callq 0x89f0
movq -0x20(%rbp), %rdi
callq 0xce80
movq -0x28(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)