// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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 0x3610
movq %rax, (%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r14
movl $0x1, %edi
movl $0x20, %esi
callq 0x3610
movq %rax, %rbp
movl $0x1, %edi
movl $0x20, %esi
callq 0x3610
incq (%rax)
movq %rax, 0x10(%rbp)
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0xc8e1
movq 0x18(%rax), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rax)
movq -0x8(%rdx,%rcx,8), %rax
jmp 0xc8f0
movl $0x1, %edi
movl $0x18, %esi
callq 0x3610
movq %rax, 0x8(%rbp)
movq %rax, (%rbp)
movq $0x0, 0x10(%rax)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x27d67
testl %eax, %eax
movq %rbp, 0x8(%rsp)
js 0xcc94
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
movzbl 0xb(%r14), %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x22d1c
movl %eax, %ebx
movslq 0x10(%r14), %rax
movl $0x0, %r12d
testq %rax, %rax
jle 0xc97a
movq 0x8(%r14), %rdx
movq %rdx, %rsi
shrq $0x30, %rsi
shlq $0x2, %rsi
addq 0x30(%r14), %rsi
shrl $0x18, %edx
leal 0x1(%rax), %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 0xc96b
cmpl (%r14), %r15d
jne 0xc996
movl 0x4(%r14), %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq (%rsp), %rcx
callq 0xce5f
jmp 0xc9b4
testl %r15d, %r15d
js 0xc9b1
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rsp), %rcx
callq 0xce5f
movl (%r14), %r15d
movl 0xc(%r14), %eax
testb $0x4, %al
movl %r15d, 0x20(%rsp)
jne 0xc9cd
cmpl $0x0, 0x2c(%rsp)
jne 0xc9e2
andl $0x1, %eax
je 0xc9e2
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xcfa0
jmp 0xcc6d
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x25967
testq %rax, %rax
leaq 0x346ec(%rip), %rcx     # 0x410e9
cmoveq %rcx, %rax
movq (%rsp), %rdi
movq %rax, %rsi
callq 0xd0b8
incq (%rax)
xorl %edx, %edx
movl %ebx, %ebp
movabsq $0x1000000000, %rcx     # imm = 0x1000000000
testq %rcx, 0x8(%r14)
setne %cl
jne 0xca2b
movl 0x4(%r14), %ebp
movb %cl, %dl
movq %rax, 0x30(%rsp)
movq 0x10(%rax,%rdx,8), %r15
movl 0x8(%r15), %eax
cmpl 0xc(%r15), %eax
jb 0xca60
movl (%r15), %eax
movl 0x4(%r15), %ecx
addl %ecx, %ecx
cmpl %eax, %ecx
movl $0x0, %esi
sbbl %esi, %esi
orl $0x1, %esi
addl %eax, %esi
movq %r15, %rdi
callq 0xd6dc
movl (%r15), %r8d
movl %ebp, %eax
xorl %edx, %edx
divl %r8d
movl %edx, %esi
movq 0x10(%r15), %rdi
movl %edx, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %eax
leal (%rsi,%rsi), %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0xcb17
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 0xcaee
testb $0x1, %r11b
jne 0xcac7
movq 0x18(%r15), %r11
movl %r9d, %r13d
cmpl %ebp, (%r11,%r13,4)
je 0xcaee
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 0xca97
jmp 0xcaf1
movl %r8d, %ecx
movl %ecx, %esi
cmpl %r8d, %ecx
jne 0xcb17
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, %r13d
testb $0x2, %dl
jne 0xcc1a
testb $0x1, %dl
jne 0xcc38
movq 0x20(%r15), %rax
movq (%rax,%r13,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 0xcc6d
movabsq $0x1000000000, %rdx     # imm = 0x1000000000
testq %rdx, 0x8(%r14)
jne 0xcc89
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 0x24(%r14), %eax
cmpl %eax, %ebp
jge 0xcbd7
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 0x3720
movq %rax, %rbx
movq 0x30(%r14), %rsi
movslq 0x24(%r14), %rdx
movq %rbx, %rdi
callq 0x37d0
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 0xcc6d
movq 0x18(%r15), %rdx
movl %ebp, (%rdx,%r13,4)
movl $0x3, %edx
shll %cl, %edx
notl %edx
andl %edx, (%rdi,%rax,4)
incl 0x4(%r15)
incl 0x8(%r15)
jmp 0xcc50
movq 0x18(%r15), %rdx
movl %ebp, (%rdx,%r13,4)
movl $0x3, %edx
shll %cl, %edx
notl %edx
andl %edx, (%rdi,%rax,4)
incl 0x4(%r15)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xcfa0
movq 0x20(%r15), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x27d67
testl %eax, %eax
movl 0x20(%rsp), %r15d
jns 0xc920
jmp 0xcc94
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x4(%rax)
jmp 0xcc55
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rsp), %rbx
movq %rbx, %rcx
callq 0xce5f
movl (%rbx), %r13d
testq %r13, %r13
je 0xcd03
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 0xccf1
incq %r12
addl $0x2, %ebx
cmpq %r12, %r13
jne 0xcccf
jmp 0xcd03
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3e89
jmp 0xcce4
movq (%rsp), %rbx
movq 0x18(%rbx), %rdi
callq 0x3350
movq 0x10(%rbx), %rdi
callq 0x3350
movq 0x20(%rbx), %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
testq %r14, %r14
je 0xcd40
movq 0x30(%r14), %rdi
callq 0x3350
movq %r14, %rdi
callq 0x3350
movq 0x8(%rsp), %r13
movq (%r13), %r14
movq 0x8(%r13), %rbx
cmpq %rbx, %r14
je 0xcda6
movq 0x10(%r13), %r15
movl $0x10, %r12d
decq (%r15)
movq 0x10(%r15), %rax
cmpq %rax, 0x8(%r15)
jne 0xcd89
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x10(%r15)
movq 0x18(%r15), %rdi
shlq $0x3, %rsi
callq 0x3720
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 0xcd5c
movq 0x10(%r13), %rbx
decq (%rbx)
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0xcddc
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 0x3720
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 0xce38
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0xce22
movq 0x30(%rax), %rdi
callq 0x3350
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x3350
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0x3350
incq %r14
cmpq 0x8(%rbx), %r14
jb 0xcdf7
movq 0x18(%rbx), %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
movq %r13, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3350