// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, %r14
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r13
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x12383
movslq (%rax), %r15
movl %r15d, (%r14)
movq %rax, %rdi
callq 0x122ff
movl $0x8, %esi
movq %r15, %rdi
callq 0x3530
movq %rax, %r12
movq %rax, 0x8(%r14)
testq %r15, %r15
jle 0x15bec
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x28, %esi
callq 0x3530
movq %rax, (%r12,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x15bd1
movq %r12, 0x18(%rsp)
movl $0x10, %esi
movq %r15, 0x40(%rsp)
movq %r15, %rdi
callq 0x3530
movq %r14, 0x48(%rsp)
movq %rax, 0x58(%rsp)
movq %rax, 0x10(%r14)
movq 0x28(%rsp), %rdi
movq 0x28(%rdi), %rbp
movzwl 0x34(%rdi), %ebx
movq %r13, %rsi
callq 0x126b5
movl %eax, 0x14(%rsp)
testl %eax, %eax
js 0x15f30
shlq $0x10, %rbp
orq %rbx, %rbp
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %rbp, 0x20(%rsp)
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %r13, 0x50(%rsp)
movl (%r13), %r14d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %r14d, %eax
cmpl %r14d, %r9d
jne 0x15c89
movl 0x4(%r13), %r8d
cmpl %r8d, %r12d
jg 0x16066
movl %r15d, %ebx
movl %r9d, %eax
movl %eax, 0x38(%rsp)
testl %r14d, %r14d
js 0x15d68
movq 0x8(%r13), %rsi
movzwl %si, %eax
cmpl $0x1248, %eax           # imm = 0x1248
ja 0x15d68
shlq $0x4, %r14
movq 0x58(%rsp), %r12
movq %r13, %rdi
leaq (%r12,%r14), %r13
movl 0x4(%rdi), %eax
movl %eax, 0x34(%rsp)
shrl $0x18, %esi
addq 0x30(%rdi), %rsi
callq 0x12268
decl %eax
movl %eax, %r15d
shrl $0xe, %r15d
movslq 0x4(%r12,%r14), %r14
cmpl %r15d, %r14d
jg 0x15d31
shrl $0xf, %eax
orl %r15d, %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(%r13)
movq 0x8(%r13), %rdi
leal 0x8(,%rax,8), %esi
callq 0x3610
movq %rax, 0x8(%r13)
leaq (%rax,%r14,8), %rdi
movslq 0x4(%r13), %rdx
subq %r14, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x3060
movl 0x34(%rsp), %ecx
sarl $0xe, %ecx
cmpl %r15d, %ecx
jge 0x15d5c
movq 0x8(%r13), %rax
movslq %ecx, %rcx
movl %r15d, %edx
cmpq $0x0, 0x8(%rax,%rcx,8)
jne 0x15d54
movq %rbp, 0x8(%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rdx
jne 0x15d47
incl %r15d
movl %r15d, (%r13)
movq 0x50(%rsp), %r13
movzwl 0x8(%r13), %r15d
cmpl %ebx, %r15d
jne 0x15d7a
movl %ebx, %r15d
jmp 0x15e3a
movl 0x10(%rsp), %esi
cmpl $-0x1, %esi
je 0x15e20
movslq 0xc(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movq %rbx, %rdi
leaq 0x3c(%rsp), %rdx
callq 0x17327
movl %eax, %r14d
shlq $0x4, %r14
addq 0x20(%rbx), %r14
cmpl $0x0, 0x3c(%rsp)
je 0x15dd6
movq $0x1, (%r14)
leaq 0x4(%r14), %rbx
movl $0x1, %edi
movl $0x10, %esi
callq 0x3530
movq %rax, 0x8(%r14)
xorl %esi, %esi
jmp 0x15e03
leaq 0x4(%r14), %rbx
movl 0x4(%r14), %esi
cmpl (%r14), %esi
jne 0x15dff
leal (%rsi,%rsi), %eax
movl %eax, (%r14)
movq 0x8(%r14), %rdi
shll $0x5, %esi
callq 0x3610
movq %rax, 0x8(%r14)
movl 0x4(%r14), %esi
jmp 0x15e03
movq 0x8(%r14), %rax
movl %esi, %ecx
shlq $0x4, %rcx
movq 0x20(%rsp), %rdx
movq %rdx, (%rax,%rcx)
incl %esi
movl %esi, (%rbx)
movq %rbp, 0x8(%rax,%rcx)
movzwl 0x8(%r13), %r15d
movl (%r13), %eax
movl %r15d, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x15f30
movq 0x28(%rsp), %rdi
movq 0x28(%rdi), %rax
shlq $0x10, %rax
movzwl 0x34(%rdi), %ebx
orq %rax, %rbx
cmpq %rbp, %rbx
jbe 0x1605b
movl 0x4(%r13), %r12d
movq %r13, %rsi
callq 0x126b5
movq %rbx, %rbp
movl 0x38(%rsp), %r9d
movl %eax, 0x14(%rsp)
testl %eax, %eax
jns 0x15c65
movl 0xc(%rsp), %eax
testl %eax, %eax
js 0x15f30
movl %eax, %eax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movq 0x28(%rsp), %rcx
movq 0x28(%rcx), %rax
shlq $0x10, %rax
movzwl 0x34(%rcx), %r15d
orq %rax, %r15
leaq 0x64(%rsp), %r12
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movq %r12, %rdx
callq 0x17327
movl %eax, %r14d
shlq $0x4, %r14
addq 0x20(%rbx), %r14
cmpl $0x0, (%r12)
je 0x15eeb
movl $0x1, (%r14)
leaq 0x4(%r14), %rbx
movl $0x1, %edi
movl $0x10, %esi
callq 0x3530
movq %rax, 0x8(%r14)
xorl %esi, %esi
jmp 0x15f18
leaq 0x4(%r14), %rbx
movl 0x4(%r14), %esi
cmpl (%r14), %esi
jne 0x15f14
leal (%rsi,%rsi), %eax
movl %eax, (%r14)
movq 0x8(%r14), %rdi
shll $0x5, %esi
callq 0x3610
movq %rax, 0x8(%r14)
movl 0x4(%r14), %esi
jmp 0x15f18
movq 0x8(%r14), %rax
movl %esi, %ecx
shlq $0x4, %rcx
movq 0x20(%rsp), %rdx
movq %rdx, (%rax,%rcx)
incl %esi
movl %esi, (%rbx)
movq %r15, 0x8(%rax,%rcx)
movq 0x40(%rsp), %r12
testl %r12d, %r12d
movq 0x48(%rsp), %r15
movq 0x18(%rsp), %rbp
jle 0x16015
xorl %eax, %eax
movq (%rbp,%rax,8), %rdx
movl (%rdx), %r8d
testl %r8d, %r8d
je 0x16009
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 0x15ffe
movq 0x20(%rdx), %rdi
movl %esi, %r9d
shlq $0x4, %r9
leaq (%rdi,%r9), %rcx
movl 0x4(%rdi,%r9), %r9d
movl $0x1, %edi
cmpl $0x2, %r9d
jb 0x15ffb
xorl %edi, %edi
movl $0x1, %r8d
movl $0x10, %r10d
movq 0x8(%rcx), %r11
movslq %edi, %rbx
shlq $0x4, %rbx
movq (%r11,%r10), %r14
xorq 0x8(%r11,%rbx), %r14
cmpq $0xffff, %r14           # imm = 0xFFFF
ja 0x15fd3
addq %r11, %rbx
addq $0x8, %rbx
movq 0x8(%r11,%r10), %r11
movq %r11, (%rbx)
jmp 0x15fe7
leaq (%r11,%r10), %r9
incl %edi
movups (%r9), %xmm0
movups %xmm0, 0x10(%rbx,%r11)
movl 0x4(%rcx), %r9d
incq %r8
movl %r9d, %r11d
addq $0x10, %r10
cmpq %r11, %r8
jb 0x15fa5
movl (%rdx), %r8d
incl %edi
movl %edi, 0x4(%rcx)
incl %esi
cmpl %r8d, %esi
jne 0x15f5d
incq %rax
cmpq %r12, %rax
jne 0x15f4a
movl 0x14(%rsp), %edx
cmpl $-0x2, %edx
jle 0x16041
movq 0x30(%r13), %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17fa0(%rip), %rax     # 0x2dfe8
movq (%rax), %rdi
leaq 0xf9c7(%rip), %rsi      # 0x25a19
xorl %eax, %eax
callq 0x3630
jmp 0x1601e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x46d2
movq 0x17f7b(%rip), %rax     # 0x2dfe8
movq (%rax), %rdi
movq 0x30(%r13), %rdx
incl %r9d
leaq 0xf922(%rip), %rsi      # 0x259a0
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3630
movl $0x1, %edi
callq 0x3130