// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/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, n_mapped, n_unmapped, off_beg, off_end, n_no_coor;

	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;
	n_mapped = n_unmapped = n_no_coor = off_end = 0;
	off_beg = off_end = bam_tell(fp);
	while ((ret = bam_read1(fp, b)) >= 0) {
		if (c->tid < 0) ++n_no_coor;
		if (last_tid < c->tid || (last_tid >= 0 && c->tid < 0)) { // change of chromosomes
			last_tid = c->tid;
			last_bin = 0xffffffffu;
		} else if ((uint32_t)last_tid > (uint32_t)c->tid) {
			fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %d-th chr > %d-th chr\n",
					bam1_qname(b), last_tid+1, c->tid+1);
			return NULL;
		} else if ((int32_t)c->tid >= 0 && 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);
			return NULL;
		}
		if (c->tid >= 0 && !(c->flag & BAM_FUNMAP)) 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);
			if (last_bin == 0xffffffffu && save_tid != 0xffffffffu) { // write the meta element
				off_end = last_off;
				insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, off_end);
				insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
				n_mapped = n_unmapped = 0;
				off_beg = off_end;
			}
			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);
			return NULL;
		}
		if (c->flag & BAM_FUNMAP) ++n_unmapped;
		else ++n_mapped;
		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));
		insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, bam_tell(fp));
		insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
	}
	merge_chunks(idx);
	fill_missing(idx);
	if (ret >= 0) {
		while ((ret = bam_read1(fp, b)) >= 0) {
			++n_no_coor;
			if (c->tid >= 0 && n_no_coor) {
				fprintf(stderr, "[bam_index_core] the alignment is not sorted: reads without coordinates prior to reads with coordinates.\n");
				return NULL;
			}
		}
	}
	if (ret < -1) fprintf(stderr, "[bam_index_core] truncated file? Continue anyway. (%d)\n", ret);
	free(b->data); free(b);
	idx->n_no_coor = n_no_coor;
	return idx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r13
pushq $0x1
popq %rbx
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x31f40
movq %rax, %r15
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0x31f40
movq %rax, 0x60(%rsp)
movq %r13, %rdi
callq 0x294cb4
movslq (%rax), %r12
movl %r12d, (%r15)
movq %rax, %rdi
callq 0x294c39
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x31f40
movq %rax, 0x28(%rsp)
movq %rax, 0x10(%r15)
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
cmpq %rbx, %r14
je 0x298da5
callq 0x29a69c
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0x298d8d
pushq $0x10
popq %rsi
movq %r12, 0x40(%rsp)
movq %r12, %rdi
callq 0x31f40
movq %r15, 0x70(%rsp)
movq %rax, 0x68(%rsp)
movq %rax, 0x18(%r15)
movq 0x30(%r13), %rax
shlq $0x10, %rax
movzwl 0x3c(%r13), %ecx
orq %rax, %rcx
pushq $-0x1
popq %r15
xorl %ebx, %ebx
movl %r15d, 0x8(%rsp)
movl %r15d, 0x1c(%rsp)
movl %r15d, %ebp
movl %r15d, 0xc(%rsp)
movq %rcx, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
movq %rcx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x60(%rsp), %r14
movq %r13, 0x20(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x294ff7
movl %eax, %r12d
movl %r15d, %r8d
testl %eax, %eax
js 0x2990cf
movq %r8, 0x80(%rsp)
movl (%r14), %r13d
movl %r13d, %eax
shrl $0x1f, %eax
addq %rax, 0x30(%rsp)
movl 0xc(%rsp), %ecx
cmpl %r13d, %ecx
jge 0x298e6d
movl %r13d, %ecx
pushq $-0x1
popq %rdi
testl %r13d, %r13d
jns 0x298eba
movl %r13d, 0xc(%rsp)
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r8
jmp 0x298fe5
testl %r13d, %r13d
sets %al
testl %ecx, %ecx
movl %ecx, %edx
setns %cl
testb %al, %cl
movq 0x10(%rsp), %r8
je 0x298e95
pushq $-0x1
popq %rdi
movl %r13d, 0xc(%rsp)
movq 0x20(%rsp), %r13
jmp 0x298fe5
cmpl %r13d, %edx
movl %edx, %ecx
ja 0x299309
testl %r13d, %r13d
js 0x298fcd
movl 0x4(%r14), %r8d
cmpl %r8d, %ebp
movl 0x8(%rsp), %edi
jg 0x299331
movl %ecx, 0xc(%rsp)
movq 0x8(%r14), %rsi
btq $0x22, %rsi
jb 0x298f99
movl %edi, 0x8(%rsp)
movq %rbx, 0x78(%rsp)
shlq $0x4, %r13
movq %r14, %rdi
movq 0x68(%rsp), %rbp
movq %rbp, %r14
addq %r13, %r14
movl 0x4(%rdi), %eax
movl %eax, 0x5c(%rsp)
shrl $0x18, %esi
addq 0x30(%rdi), %rsi
callq 0x294bb0
decl %eax
movl %eax, %ebx
shrl $0xe, %ebx
movslq 0x4(%rbp,%r13), %r13
cmpl %ebx, %r13d
jg 0x298f5e
shrl $0xf, %eax
orl %ebx, %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 0x330a8
movq %rax, 0x8(%r14)
leaq (%rax,%r13,8), %rdi
movslq 0x4(%r14), %rdx
subq %r13, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2e4b0
movl 0x5c(%rsp), %ecx
sarl $0xe, %ecx
cmpl %ebx, %ecx
movl 0x8(%rsp), %edi
jne 0x298fa5
movq 0x8(%r14), %rax
movl %ecx, %ecx
cmpq $0x0, (%rax,%rcx,8)
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r8
jne 0x298f88
movq %r8, (%rax,%rcx,8)
incl %ebx
movl %ebx, (%r14)
movq 0x60(%rsp), %r14
movq 0x78(%rsp), %rbx
jmp 0x298fd6
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r8
jmp 0x298fd6
movslq %ecx, %rax
movl %ebx, %ecx
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r8
cmpq %rcx, %rax
jg 0x298f88
movq 0x8(%r14), %rdx
cmpq $0x0, (%rdx,%rax,8)
jne 0x298fc8
movq %r8, (%rdx,%rax,8)
incq %rax
jmp 0x298fb4
movq 0x20(%rsp), %r13
movl 0x8(%rsp), %edi
movq 0x8(%r14), %rax
movzwl %ax, %ecx
cmpl %edi, %ecx
je 0x29908e
movl 0x1c(%rsp), %esi
cmpl $-0x1, %esi
je 0x299010
movslq %r15d, %rax
movl %edi, %ebp
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x50(%rsp), %rdx
movq %r8, %rcx
callq 0x29935c
movl %ebp, %edi
movq 0x10(%rsp), %r8
cmpl $-0x1, %edi
sete %al
cmpl $-0x1, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x299072
movq 0x28(%rsp), %r15
movq 0x80(%rsp), %rbp
movq (%r15,%rbp,8), %rdi
movl $0x924a, %esi           # imm = 0x924A
movq 0x48(%rsp), %rdx
movq %r8, %rcx
callq 0x29935c
movq (%r15,%rbp,8), %rdi
movl $0x924a, %esi           # imm = 0x924A
movq %rbx, %rdx
movq 0x38(%rsp), %rcx
callq 0x29935c
movq 0x10(%rsp), %r8
xorl %ebx, %ebx
movq $0x0, 0x38(%rsp)
movq %r8, 0x48(%rsp)
movl (%r14), %r15d
testl %r15d, %r15d
js 0x299137
movq 0x8(%r14), %rax
movzwl %ax, %edi
movl %edi, 0x1c(%rsp)
movq %r8, 0x50(%rsp)
movq 0x30(%r13), %rcx
shlq $0x10, %rcx
movzwl 0x3c(%r13), %edx
orq %rcx, %rdx
cmpq %r8, %rdx
jbe 0x2992ea
movl %edi, 0x8(%rsp)
shrq $0x22, %rax
andl $0x1, %eax
movq %rax, %rcx
xorq $0x1, %rcx
addq %rcx, %rbx
addq %rax, 0x38(%rsp)
movl 0x4(%r14), %ebp
movq %rdx, 0x10(%rsp)
jmp 0x298e16
testl %r15d, %r15d
js 0x299137
movq 0x28(%rsp), %r15
movq (%r15,%r8,8), %rdi
movq 0x30(%r13), %rax
shlq $0x10, %rax
movzwl 0x3c(%r13), %ecx
orq %rax, %rcx
movl 0x1c(%rsp), %esi
movq 0x50(%rsp), %rdx
movq %r8, %rbp
callq 0x29935c
movq (%r15,%rbp,8), %rdi
movq 0x30(%r13), %rax
shlq $0x10, %rax
movzwl 0x3c(%r13), %ecx
orq %rax, %rcx
movl $0x924a, %esi           # imm = 0x924A
movq 0x48(%rsp), %rdx
callq 0x29935c
movq (%r15,%rbp,8), %rdi
movl $0x924a, %esi           # imm = 0x924A
movq %rbx, %rdx
movq 0x38(%rsp), %rcx
callq 0x29935c
movq 0x40(%rsp), %r15
movq 0x30(%rsp), %rbp
xorl %eax, %eax
testq %r15, %r15
cmovleq %rax, %r15
cmpq %r15, %rax
je 0x299201
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %rdx
xorl %esi, %esi
cmpl (%rdx), %esi
je 0x2991f9
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 0x2991f2
movq 0x18(%rdx), %rdi
movl %esi, %ecx
cmpl $0x924a, (%rdi,%rcx,4)  # imm = 0x924A
je 0x2991f2
shlq $0x4, %rcx
addq 0x20(%rdx), %rcx
xorl %edi, %edi
pushq $0x10
popq %r8
pushq $0x1
popq %r9
movl 0x4(%rcx), %r10d
cmpq %r10, %r9
jae 0x2991ed
movq 0x8(%rcx), %r10
movslq %edi, %r11
shlq $0x4, %r11
movq (%r10,%r8), %rbx
xorq 0x8(%r10,%r11), %rbx
cmpq $0xffff, %rbx           # imm = 0xFFFF
ja 0x2991d5
addq %r10, %r11
addq $0x8, %r11
movq 0x8(%r10,%r8), %r10
movq %r10, (%r11)
jmp 0x2991e4
leaq (%r10,%r8), %rbx
incl %edi
movups (%rbx), %xmm0
movups %xmm0, 0x10(%r11,%r10)
incq %r9
addq $0x10, %r8
jmp 0x29919e
incl %edi
movl %edi, 0x4(%rcx)
incl %esi
jmp 0x29915e
incq %rax
jmp 0x29914a
xorl %eax, %eax
pushq $0x1
popq %rcx
cmpq %r15, %rax
je 0x299245
movq %rax, %rsi
shlq $0x4, %rsi
movq 0x68(%rsp), %rdi
leaq (%rdi,%rsi), %rdx
movslq (%rdi,%rsi), %rsi
movq %rcx, %rdi
cmpq %rsi, %rdi
jge 0x299240
movq 0x8(%rdx), %r8
cmpq $0x0, (%r8,%rdi,8)
jne 0x29923b
movq -0x8(%r8,%rdi,8), %r9
movq %r9, (%r8,%rdi,8)
incq %rdi
jmp 0x299222
incq %rax
jmp 0x299206
testl %r12d, %r12d
js 0x299298
negq %rbp
movq %rbp, %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x294ff7
testl %eax, %eax
js 0x29928f
leaq -0x1(%rbx), %rbp
cmpl $0x0, (%r14)
js 0x29924d
cmpq $0x1, %rbx
je 0x29924d
movq 0x171d3a(%rip), %rax    # 0x40afb0
movq (%rax), %rcx
leaq 0xc1e73(%rip), %rdi     # 0x35b0f3
pushq $0x69
popq %rsi
pushq $0x1
popq %rdx
callq 0x32760
xorl %ebx, %ebx
jmp 0x2992b8
movl %eax, %r12d
negq %rbx
movq %rbx, %rbp
cmpl $-0x1, %r12d
jne 0x2992cd
movq 0x30(%r14), %rdi
callq 0x33058
movq %r14, %rdi
callq 0x33058
movq 0x70(%rsp), %rbx
movq %rbp, 0x8(%rbx)
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x171cdc(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xc1e7f(%rip), %rsi     # 0x35b15d
movl %r12d, %edx
xorl %eax, %eax
callq 0x32960
jmp 0x29929e
movq 0x171cbf(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xc1dc8(%rip), %rsi     # 0x35b0c3
xorl %ebx, %ebx
movq %r8, %rcx
xorl %eax, %eax
callq 0x32960
jmp 0x2992b8
movq 0x171ca0(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x30(%r14), %rdx
incl %ecx
incl %r13d
leaq 0xc1d0d(%rip), %rsi     # 0x35b030
xorl %ebx, %ebx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x32960
jmp 0x2992b8
movq 0x171c78(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x30(%r14), %rdx
incl %r13d
leaq 0xc1d31(%rip), %rsi     # 0x35b07a
xorl %ebx, %ebx
movl %ebp, %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0x32960
jmp 0x2992b8