// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_index.c
// Function: bam_index_core
bam_index_t *bam_index_core(bamFile fp)
{
	bam1_t *b;
	bam_header_t *h;
	int i, ret;
	bam_index_t *idx;
	uint32_t last_bin, save_bin;
	int32_t last_coor, last_tid, save_tid;
	bam1_core_t *c;
	uint64_t save_off, last_off;

	idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
	b = (bam1_t*)calloc(1, sizeof(bam1_t));
	h = bam_header_read(fp);
	c = &b->core;

	idx->n = h->n_targets;
	bam_header_destroy(h);
	idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
	for (i = 0; i < idx->n; ++i) idx->index[i] = kh_init(i);
	idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));

	save_bin = save_tid = last_tid = last_bin = 0xffffffffu;
	save_off = last_off = bam_tell(fp); last_coor = 0xffffffffu;
	while ((ret = bam_read1(fp, b)) >= 0) {
		if (last_tid != c->tid) { // change of chromosomes
			last_tid = c->tid;
			last_bin = 0xffffffffu;
		} else if (last_coor > c->pos) {
			fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %u > %u in %d-th chr\n",
					bam1_qname(b), last_coor, c->pos, c->tid+1);
			exit(1);
		}
		if (b->core.tid >= 0 && b->core.bin < 4681) insert_offset2(&idx->index2[b->core.tid], b, last_off);
		if (c->bin != last_bin) { // then possibly write the binning index
			if (save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
				insert_offset(idx->index[save_tid], save_bin, save_off, last_off);
			save_off = last_off;
			save_bin = last_bin = c->bin;
			save_tid = c->tid;
			if (save_tid < 0) break;
		}
		if (bam_tell(fp) <= last_off) {
			fprintf(stderr, "[bam_index_core] bug in BGZF/RAZF: %llx < %llx\n",
					(unsigned long long)bam_tell(fp), (unsigned long long)last_off);
			exit(1);
		}
		last_off = bam_tell(fp);
		last_coor = b->core.pos;
	}
	if (save_tid >= 0) insert_offset(idx->index[save_tid], save_bin, save_off, bam_tell(fp));
	merge_chunks(idx);
	if (ret < -1) fprintf(stderr, "[bam_index_core] truncated file? Continue anyway. (%d)\n", ret);
	free(b->data); free(b);
	return idx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r13
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, %r14
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, 0x8(%rsp)
movq %r13, %rdi
callq 0x11d21
movslq (%rax), %r15
movl %r15d, (%r14)
movq %rax, %rdi
callq 0x11c9f
movl $0x8, %esi
movq %r15, %rdi
callq 0x3530
movq %rax, %r12
movq %rax, 0x8(%r14)
testq %r15, %r15
jle 0x1571f
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x28, %esi
callq 0x3530
movq %rax, (%r12,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x15704
movq %r12, 0x20(%rsp)
movl $0x10, %esi
movq %r15, 0x40(%rsp)
movq %r15, %rdi
callq 0x3530
movq %r14, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, 0x10(%r14)
movq 0x28(%r13), %rax
shlq $0x10, %rax
movzwl 0x34(%r13), %ebx
orq %rax, %rbx
movq %r13, 0x38(%rsp)
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x1204b
movl %eax, 0x14(%rsp)
testl %eax, %eax
movq %rbx, 0x18(%rsp)
js 0x159b2
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbp
movl (%rbp), %r12d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r12d, %eax
cmpl %r12d, %edx
movq %rbp, %rcx
jne 0x157c3
movl 0x4(%rcx), %r8d
movl %r14d, %r15d
movl %edx, %eax
cmpl %r8d, %r13d
jg 0x15b9f
movl %eax, 0x30(%rsp)
testl %r12d, %r12d
movq 0x38(%rsp), %rbp
js 0x158b0
movq 0x8(%rcx), %rsi
movzwl %si, %eax
cmpl $0x1248, %eax           # imm = 0x1248
ja 0x158b0
shlq $0x4, %r12
movq 0x50(%rsp), %r14
leaq (%r14,%r12), %rax
movq %rax, 0x58(%rsp)
movl 0x4(%rcx), %eax
movl %eax, 0x2c(%rsp)
shrl $0x18, %esi
addq 0x30(%rcx), %rsi
movq %rcx, %rdi
callq 0x11c08
decl %eax
movl %eax, %r13d
shrl $0xe, %r13d
movslq 0x4(%r14,%r12), %r12
movq 0x58(%rsp), %r14
cmpl %r13d, %r12d
jg 0x1587a
shrl $0xf, %eax
orl %r13d, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%r14), %rdi
leal 0x8(,%rax,8), %esi
callq 0x3610
movq %rax, 0x8(%r14)
leaq (%rax,%r12,8), %rdi
movslq 0x4(%r14), %rdx
subq %r12, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x3060
movl 0x2c(%rsp), %ecx
sarl $0xe, %ecx
cmpl %r13d, %ecx
jge 0x158a5
movq 0x8(%r14), %rax
movslq %ecx, %rcx
movl %r13d, %edx
cmpq $0x0, 0x8(%rax,%rcx,8)
jne 0x1589d
movq %rbx, 0x8(%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rdx
jne 0x15890
incl %r13d
movl %r13d, (%r14)
movq 0x8(%rsp), %rcx
movzwl 0x8(%rcx), %eax
cmpl %r15d, %eax
jne 0x158c1
movl %r15d, %r14d
jmp 0x15972
movl (%rsp), %esi
cmpl $-0x1, %esi
je 0x1595a
movslq 0x4(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %r12
movq %r12, %rdi
leaq 0x34(%rsp), %rdx
callq 0x16b94
movl %eax, %r14d
shlq $0x4, %r14
addq 0x20(%r12), %r14
cmpl $0x0, 0x34(%rsp)
je 0x15915
movq $0x1, (%r14)
movl $0x1, %edi
movl $0x10, %esi
callq 0x3530
movq %rax, 0x8(%r14)
movl (%r14), %esi
cmpl %esi, 0x4(%r14)
jne 0x15934
leal (%rsi,%rsi), %eax
movl %eax, (%r14)
movq 0x8(%r14), %rdi
shll $0x5, %esi
callq 0x3610
movq %rax, 0x8(%r14)
movq 0x8(%r14), %rax
movl 0x4(%r14), %ecx
leal 0x1(%rcx), %edx
shlq $0x4, %rcx
movq 0x18(%rsp), %rsi
movq %rsi, (%rax,%rcx)
movl %edx, 0x4(%r14)
movq %rbx, 0x8(%rax,%rcx)
movq 0x8(%rsp), %rcx
movzwl 0x8(%rcx), %r14d
movl (%rcx), %eax
movl %r14d, (%rsp)
movq %rbx, 0x18(%rsp)
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x159c3
movq 0x28(%rbp), %rax
shlq $0x10, %rax
movzwl 0x34(%rbp), %r12d
orq %rax, %r12
cmpq %rbx, %r12
jbe 0x15b94
movl 0x4(%rcx), %r13d
movq %rbp, %rdi
movq %rcx, %rsi
movq %rcx, %rbp
callq 0x1204b
movq %r12, %rbx
movl 0x30(%rsp), %edx
movl %eax, 0x14(%rsp)
testl %eax, %eax
jns 0x1579c
jmp 0x159cc
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jmp 0x159cc
movl %r14d, (%rsp)
movq %rbx, 0x18(%rsp)
movl 0x4(%rsp), %eax
testl %eax, %eax
movq 0x20(%rsp), %r12
js 0x15a7c
movl %eax, %eax
movq (%r12,%rax,8), %rbx
movq 0x38(%rsp), %rax
movq 0x28(%rax), %r14
shlq $0x10, %r14
movzwl 0x34(%rax), %r13d
leaq 0x64(%rsp), %r12
movq %rbx, %rdi
movl (%rsp), %esi
movq %r12, %rdx
callq 0x16b94
movl %eax, %r15d
shlq $0x4, %r15
addq 0x20(%rbx), %r15
cmpl $0x0, (%r12)
je 0x15a34
movq $0x1, (%r15)
movl $0x1, %edi
movl $0x10, %esi
callq 0x3530
movq %rax, 0x8(%r15)
orq %r13, %r14
movl (%r15), %esi
cmpl %esi, 0x4(%r15)
movq 0x18(%rsp), %rbx
jne 0x15a5b
leal (%rsi,%rsi), %eax
movl %eax, (%r15)
movq 0x8(%r15), %rdi
shll $0x5, %esi
callq 0x3610
movq %rax, 0x8(%r15)
movq 0x8(%r15), %rax
movl 0x4(%r15), %ecx
leal 0x1(%rcx), %edx
shlq $0x4, %rcx
movq %rbx, (%rax,%rcx)
movl %edx, 0x4(%r15)
movq %r14, 0x8(%rax,%rcx)
movq 0x20(%rsp), %r12
movq 0x40(%rsp), %r15
testl %r15d, %r15d
movq 0x48(%rsp), %r14
jle 0x15b49
xorl %eax, %eax
movq (%r12,%rax,8), %rdx
cmpl $0x0, (%rdx)
je 0x15b3d
xorl %esi, %esi
movq 0x10(%rdx), %rcx
movl %esi, %edi
shrl $0x4, %edi
movl (%rcx,%rdi,4), %edi
leal (%rsi,%rsi), %ecx
shrl %cl, %edi
testb $0x3, %dil
jne 0x15b33
movq 0x20(%rdx), %r8
movl %esi, %r9d
shlq $0x4, %r9
leaq (%r8,%r9), %rcx
movl $0x1, %edi
cmpl $0x2, 0x4(%r8,%r9)
jb 0x15b30
xorl %edi, %edi
movl $0x1, %r8d
movl $0x10, %r9d
movq 0x8(%rcx), %r10
movslq %edi, %r11
shlq $0x4, %r11
movq (%r10,%r9), %rbx
xorq 0x8(%r10,%r11), %rbx
cmpq $0xffff, %rbx           # imm = 0xFFFF
ja 0x15b0f
addq %r10, %r11
addq $0x8, %r11
movq 0x8(%r10,%r9), %r10
movq %r10, (%r11)
jmp 0x15b1e
leaq (%r10,%r9), %rbx
incl %edi
movups (%rbx), %xmm0
movups %xmm0, 0x10(%r11,%r10)
incq %r8
movl 0x4(%rcx), %r10d
addq $0x10, %r9
cmpq %r10, %r8
jb 0x15ae1
incl %edi
movl %edi, 0x4(%rcx)
incl %esi
cmpl (%rdx), %esi
jne 0x15aa0
incq %rax
cmpq %r15, %rax
jne 0x15a91
movl 0x14(%rsp), %edx
cmpl $-0x2, %edx
jle 0x15b7a
movq 0x8(%rsp), %rbx
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19467(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
leaq 0x10d9d(%rip), %rsi     # 0x26928
xorl %eax, %eax
callq 0x3630
jmp 0x15b52
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4431
movq 0x19442(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
movq 0x30(%rcx), %rdx
incl %r12d
leaq 0x10cf8(%rip), %rsi     # 0x268af
movl %r13d, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x3630
movl $0x1, %edi
callq 0x3130