// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x32f50
movq %rax, 0x58(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x32f50
movq %rax, 0x70(%rsp)
movq 0x78(%rsp), %rdi
callq 0x4691e0
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x68(%rsp), %rax
movl (%rax), %ecx
movq 0x58(%rsp), %rax
movl %ecx, (%rax)
movq 0x68(%rsp), %rdi
callq 0x469100
movq 0x58(%rsp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x32f50
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x4714df
callq 0x474130
movq %rax, %rdx
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x64(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x4714ab
movq 0x58(%rsp), %rax
movslq (%rax), %rdi
movl $0x10, %esi
callq 0x32f50
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x18(%rax)
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x48(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x50(%rsp) # imm = 0xFFFFFFFF
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rax
shlq $0x10, %rax
movq 0x78(%rsp), %rcx
movl 0x3c(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl $0xffffffff, 0x4c(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rax
shlq $0x10, %rax
movq 0x78(%rsp), %rcx
movl 0x3c(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x469710
movl %eax, 0x60(%rsp)
cmpl $0x0, %eax
jl 0x4718ef
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jge 0x4715d0
movq (%rsp), %rax
addq $0x1, %rax
movq %rax, (%rsp)
movl 0x48(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
jl 0x4715ee
cmpl $0x0, 0x48(%rsp)
jl 0x471606
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jge 0x471606
movq 0x38(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x48(%rsp)
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x4716bf
movl 0x48(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
jbe 0x471658
movq 0x18a9ae(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rdx
movl 0x48(%rsp), %ecx
addl $0x1, %ecx
movq 0x38(%rsp), %rax
movl (%rax), %r8d
addl $0x1, %r8d
leaq 0x108aa0(%rip), %rsi    # 0x57a0e0
movb $0x0, %al
callq 0x33960
movq $0x0, 0x80(%rsp)
jmp 0x471a70
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jl 0x4716bb
movl 0x4c(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x4716bb
movq 0x18a951(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rdx
movl 0x4c(%rsp), %ecx
movq 0x38(%rsp), %rax
movl 0x4(%rax), %r8d
movq 0x38(%rsp), %rax
movl (%rax), %r9d
addl $0x1, %r9d
leaq 0x108a87(%rip), %rsi    # 0x57a12a
movb $0x0, %al
callq 0x33960
movq $0x0, 0x80(%rsp)
jmp 0x471a70
jmp 0x4716bd
jmp 0x4716bf
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jl 0x47170b
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x47170b
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x70(%rsp), %rax
movslq (%rax), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x70(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x471a80
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
andq $0xffff, %rax           # imm = 0xFFFF
cmpl 0x54(%rsp), %eax
je 0x47180a
cmpl $-0x1, 0x50(%rsp)
je 0x471750
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl 0x50(%rsp), %esi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0x471c80
cmpl $-0x1, 0x54(%rsp)
jne 0x4717d0
cmpl $-0x1, 0x44(%rsp)
je 0x4717d0
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movl $0x924a, %esi           # imm = 0x924A
callq 0x471c80
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl $0x924a, %esi           # imm = 0x924A
callq 0x471c80
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
andq $0xffff, %rax           # imm = 0xFFFF
movl %eax, 0x54(%rsp)
movl %eax, 0x50(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
jge 0x471808
jmp 0x4718ef
jmp 0x47180a
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rax
shlq $0x10, %rax
movq 0x78(%rsp), %rcx
movl 0x3c(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
cmpq 0x28(%rsp), %rax
ja 0x47187f
movq 0x18a78f(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rdx
shlq $0x10, %rdx
movq 0x78(%rsp), %rax
movl 0x3c(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rdx
movq 0x28(%rsp), %rcx
leaq 0x10890c(%rip), %rsi    # 0x57a173
movb $0x0, %al
callq 0x33960
movq $0x0, 0x80(%rsp)
jmp 0x471a70
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4718aa
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x4718b8
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rax
shlq $0x10, %rax
movq 0x78(%rsp), %rcx
movl 0x3c(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x4c(%rsp)
jmp 0x47159e
cmpl $0x0, 0x44(%rsp)
jl 0x47199f
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl 0x50(%rsp), %esi
movq 0x30(%rsp), %rdx
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rcx
shlq $0x10, %rcx
movq 0x78(%rsp), %rax
movl 0x3c(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rcx
callq 0x471c80
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rdx
movq 0x78(%rsp), %rax
movq 0x30(%rax), %rcx
shlq $0x10, %rcx
movq 0x78(%rsp), %rax
movl 0x3c(%rax), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cltq
orq %rax, %rcx
movl $0x924a, %esi           # imm = 0x924A
callq 0x471c80
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl $0x924a, %esi           # imm = 0x924A
callq 0x471c80
movq 0x58(%rsp), %rdi
callq 0x471da0
movq 0x58(%rsp), %rdi
callq 0x471f70
cmpl $0x0, 0x60(%rsp)
jl 0x471a1b
jmp 0x4719bc
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x469710
movl %eax, 0x60(%rsp)
cmpl $0x0, %eax
jl 0x471a19
movq (%rsp), %rax
addq $0x1, %rax
movq %rax, (%rsp)
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jl 0x471a17
cmpq $0x0, (%rsp)
je 0x471a17
movq 0x18a5d0(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
leaq 0x1087a1(%rip), %rsi    # 0x57a1a3
movb $0x0, %al
callq 0x33960
movq $0x0, 0x80(%rsp)
jmp 0x471a70
jmp 0x4719bc
jmp 0x471a1b
cmpl $-0x1, 0x60(%rsp)
jge 0x471a3e
movq 0x18a59f(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movl 0x60(%rsp), %edx
leaq 0x1087d6(%rip), %rsi    # 0x57a20d
movb $0x0, %al
callq 0x33960
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x34078
movq 0x70(%rsp), %rdi
callq 0x34078
movq (%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq