// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_rmdupse.c
// Function: bam_rmdupse_core
void bam_rmdupse_core(samfile_t *in, samfile_t *out, int force_se)
{
	bam1_t *b;
	queue_t *queue;
	khint_t k;
	int last_tid = -2;
	khash_t(lib) *aux;

	aux = kh_init(lib);
	b = bam_init1();
	queue = kl_init(q);
	while (samread(in, b) >= 0) {
		bam1_core_t *c = &b->core;
		int endpos = bam_calend(c, bam1_cigar(b));
		int score = sum_qual(b);
		
		if (last_tid != c->tid) {
			if (last_tid >= 0) dump_alignment(out, queue, MAX_POS, aux);
			last_tid = c->tid;
		} else dump_alignment(out, queue, c->pos, aux);
		if ((c->flag&BAM_FUNMAP) || ((c->flag&BAM_FPAIRED) && !force_se)) {
			push_queue(queue, b, endpos, score);
		} else {
			const char *lib;
			lib_aux_t *q;
			besthash_t *h;
			uint32_t key;
			int ret;
			lib = bam_get_library(in->header, b);
			q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
			++q->n_checked;
			h = (c->flag&BAM_FREVERSE)? q->rght : q->left;
			key = (c->flag&BAM_FREVERSE)? endpos : c->pos;
			k = kh_put(best, h, key, &ret);
			if (ret == 0) { // in the hash table
				elem_t *p = kh_val(h, k);
				++q->n_removed;
				if (p->score < score) {
					if (c->flag&BAM_FREVERSE) { // mark "discarded" and push the queue
						p->discarded = 1;
						kh_val(h, k) = push_queue(queue, b, endpos, score);
					} else { // replace
						p->score = score; p->endpos = endpos;
						bam_copy1(p->b, b);
					}
				} // otherwise, discard the alignment
			} else kh_val(h, k) = push_queue(queue, b, endpos, score);
		}
	}
	dump_alignment(out, queue, MAX_POS, aux);

	for (k = kh_begin(aux); k != kh_end(aux); ++k) {
		if (kh_exist(aux, k)) {
			lib_aux_t *q = &kh_val(aux, k);
			fprintf(stderr, "[bam_rmdupse_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
					(long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
			kh_destroy(best, q->left); kh_destroy(best, q->rght);
			free((char*)kh_key(aux, k));
		}
	}
	kh_destroy(lib, aux);
	bam_destroy1(b);
	kl_destroy(q, queue);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, 0x2c(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x3530
movq %rax, (%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r14
movl $0x1, %edi
movl $0x20, %esi
callq 0x3530
movq %rax, %rbp
movl $0x1, %edi
movl $0x20, %esi
callq 0x3530
incq (%rax)
movq %rax, 0x10(%rbp)
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xc5e9
movq 0x18(%rax), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rax)
movq -0x8(%rdx,%rcx,8), %rax
jmp 0xc5f8
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, 0x8(%rbp)
movq %rax, (%rbp)
movq $0x0, 0x10(%rax)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x14c5e
testl %eax, %eax
movq %rbp, 0x8(%rsp)
js 0xc9a5
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
movzbl 0xb(%r14), %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x11c08
movl %eax, %ebx
movslq 0x10(%r14), %rax
movl $0x0, %r12d
testq %rax, %rax
jle 0xc689
movq 0x8(%r14), %rdx
movq %rdx, %rsi
shrq $0x30, %rsi
shlq $0x2, %rsi
addq 0x30(%r14), %rsi
shrl $0x18, %edx
leal 0x1(%rax), %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
incl %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rdx, %rcx
addq %rsi, %rcx
xorl %edx, %edx
xorl %r12d, %r12d
movzbl (%rcx,%rdx), %esi
addl %esi, %r12d
incq %rdx
cmpq %rdx, %rax
jne 0xc67a
cmpl (%r14), %r15d
jne 0xc6a5
movl 0x4(%r14), %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq (%rsp), %rcx
callq 0xcb70
jmp 0xc6c3
testl %r15d, %r15d
js 0xc6c0
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rsp), %rcx
callq 0xcb70
movl (%r14), %r15d
movl 0xc(%r14), %eax
testb $0x4, %al
movl %r15d, 0x20(%rsp)
jne 0xc6dc
cmpl $0x0, 0x2c(%rsp)
jne 0xc6f1
andl $0x1, %eax
je 0xc6f1
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xccb1
jmp 0xc97e
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x12e37
testq %rax, %rax
leaq 0x18559(%rip), %rcx     # 0x24c65
cmoveq %rcx, %rax
movq (%rsp), %rdi
movq %rax, %rsi
callq 0xcdc9
incq (%rax)
xorl %edx, %edx
movl %ebx, %ebp
movabsq $0x1000000000, %rcx     # imm = 0x1000000000
testq %rcx, 0x8(%r14)
setne %cl
jne 0xc73a
movl 0x4(%r14), %ebp
movb %cl, %dl
movq %rax, 0x30(%rsp)
movq 0x10(%rax,%rdx,8), %r13
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
jb 0xc770
movl (%r13), %eax
movl 0x4(%r13), %ecx
addl %ecx, %ecx
cmpl %eax, %ecx
movl $0x0, %esi
sbbl %esi, %esi
orl $0x1, %esi
addl %eax, %esi
movq %r13, %rdi
callq 0xd3ed
movl (%r13), %r8d
movl %ebp, %eax
xorl %edx, %edx
divl %r8d
movl %edx, %esi
movq 0x10(%r13), %rdi
movl %edx, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %eax
leal (%rsi,%rsi), %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0xc828
leal -0x1(%r8), %ecx
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
incl %edx
movl %esi, %r9d
movl %r8d, %eax
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rdi,%rcx,4), %r10d
leal (%r9,%r9), %ecx
andl $0x1e, %ecx
movl %r10d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0xc7ff
testb $0x1, %r11b
jne 0xc7d8
movq 0x18(%r13), %r11
movl %r9d, %r15d
cmpl %ebp, (%r11,%r15,4)
je 0xc7ff
btl %ecx, %r10d
cmovbl %r9d, %eax
addl %edx, %r9d
cmpl %r8d, %r9d
movl %r8d, %ecx
movl $0x0, %r10d
cmovbl %r10d, %ecx
subl %ecx, %r9d
movl %eax, %ecx
cmpl %esi, %r9d
jne 0xc7a8
jmp 0xc802
movl %r8d, %ecx
movl %ecx, %esi
cmpl %r8d, %ecx
jne 0xc828
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rdi,%rcx,4), %edx
leal (%r9,%r9), %ecx
shrl %cl, %edx
cmpl %r8d, %eax
cmovel %r9d, %eax
testb $0x2, %dl
cmovel %r9d, %eax
movl %eax, %esi
movl %esi, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %edx
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
shrl %cl, %edx
movl %esi, %r15d
testb $0x2, %dl
jne 0xc92b
testb $0x1, %dl
jne 0xc949
movq 0x20(%r13), %rax
movq (%rax,%r15,8), %rax
movq 0x30(%rsp), %rcx
incq 0x8(%rcx)
movl 0x4(%rax), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpl %r12d, %edx
movq 0x8(%rsp), %rbp
jge 0xc97e
movabsq $0x1000000000, %rdx     # imm = 0x1000000000
testq %rdx, 0x8(%r14)
jne 0xc99a
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %r12d, %ecx
movl %ecx, 0x4(%rax)
movl %ebx, (%rax)
movq 0x8(%rax), %r15
movq 0x30(%r15), %rbx
movl 0x28(%r15), %ebp
movl 0x28(%r14), %eax
cmpl %eax, %ebp
jge 0xc8e8
decl %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ebp
sarl $0x10, %ebp
orl %eax, %ebp
incl %ebp
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x3610
movq %rax, %rbx
movq 0x30(%r14), %rsi
movslq 0x24(%r14), %rdx
movq %rbx, %rdi
callq 0x3680
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%r15)
movq 0x30(%r14), %rax
movq %rax, 0x30(%r15)
movups %xmm0, (%r15)
movups %xmm1, 0x10(%r15)
movl %ebp, 0x28(%r15)
movq %rbx, 0x30(%r15)
movq 0x8(%rsp), %rbp
jmp 0xc97e
movq 0x18(%r13), %rdx
movl %ebp, (%rdx,%r15,4)
movl $0x3, %edx
shll %cl, %edx
notl %edx
andl %edx, (%rdi,%rax,4)
incl 0x4(%r13)
incl 0x8(%r13)
jmp 0xc961
movq 0x18(%r13), %rdx
movl %ebp, (%rdx,%r15,4)
movl $0x3, %edx
shll %cl, %edx
notl %edx
andl %edx, (%rdi,%rax,4)
incl 0x4(%r13)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xccb1
movq 0x20(%r13), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x14c5e
testl %eax, %eax
movl 0x20(%rsp), %r15d
jns 0xc628
jmp 0xc9a5
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x4(%rax)
jmp 0xc966
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rsp), %rbx
movq %rbx, %rcx
callq 0xcb70
movl (%rbx), %r13d
testq %r13, %r13
je 0xca14
movq (%rsp), %r15
movq 0x10(%r15), %rbp
leaq 0x20(%r15), %rax
movq %rax, 0x20(%rsp)
addq $0x18, %r15
xorl %ebx, %ebx
xorl %r12d, %r12d
movl %r12d, %eax
shrl $0x4, %eax
movl (%rbp,%rax,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
je 0xca02
incq %r12
addl $0x2, %ebx
cmpq %r12, %r13
jne 0xc9e0
jmp 0xca14
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3ad9
jmp 0xc9f5
movq (%rsp), %rbx
movq 0x18(%rbx), %rdi
callq 0x32c0
movq 0x10(%rbx), %rdi
callq 0x32c0
movq 0x20(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
testq %r14, %r14
je 0xca51
movq 0x30(%r14), %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x32c0
movq 0x8(%rsp), %r13
movq (%r13), %r14
movq 0x8(%r13), %rbx
cmpq %rbx, %r14
je 0xcab7
movq 0x10(%r13), %r15
movl $0x10, %r12d
decq (%r15)
movq 0x10(%r15), %rax
cmpq %rax, 0x8(%r15)
jne 0xca9a
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x10(%r15)
movq 0x18(%r15), %rdi
shlq $0x3, %rsi
callq 0x3610
movq %rax, 0x18(%r15)
movq 0x8(%r15), %rax
movq 0x18(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r15)
movq %r14, (%rcx,%rax,8)
movq 0x10(%r14), %r14
cmpq %rbx, %r14
jne 0xca6d
movq 0x10(%r13), %rbx
decq (%rbx)
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0xcaed
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x10, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%rbx)
movq 0x18(%rbx), %rdi
shlq $0x3, %rsi
callq 0x3610
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
movq %rax, %rdx
incq %rdx
movq %rdx, 0x8(%rbx)
movq %r14, (%rcx,%rax,8)
je 0xcb49
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0xcb33
movq 0x30(%rax), %rdi
callq 0x32c0
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x32c0
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0x32c0
incq %r14
cmpq 0x8(%rbx), %r14
jb 0xcb08
movq 0x18(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq %r13, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0