// File: /workspace/github_c_makefile_star2_O3/DReichLab[P]adna/ldup.c
// Function: main
int main(int argc, char *argv[])
{
	int c, clevel = -1, ret, bc_type = AD_USE_NAME, drop_unmapped = 0;
	int last_tid = -1, last_pos = -1;
	BGZF *fpr, *fpw;
	bam_hdr_t *h;
	bam1_t *b;
	khash_t(s) *marked;
	khash_t(64) *aux;
	kdq_t(elem_t) *q;
	khint_t k;

	while ((c = getopt(argc, argv, "l:TBD")) >= 0) {
		if (c == 'l') clevel = atoi(optarg);
		else if (c == 'T') bc_type = AD_USE_BC;
		else if (c == 'B') bc_type = AD_NO_BC;
		else if (c == 'D') drop_unmapped = 1;
	}
	if (optind == argc) {
		fprintf(stderr, "Usage: adna-ldup [options] <aln.bam>\n");
		fprintf(stderr, "Options:\n");
		fprintf(stderr, "  -l INT    zlib compression level [zlib default]\n");
		fprintf(stderr, "  -T        acquire barcode from BC tag [from read name]\n");
		fprintf(stderr, "  -B        ignore barcode\n");
		fprintf(stderr, "  -D        drop reads without mapping coordinates\n");
		return 1;
	}

	fpr = strcmp(argv[optind], "-")? bgzf_open(argv[optind], "r") : bgzf_dopen(fileno(stdin), "r");
	h = bam_hdr_read(fpr);
	if (clevel >= 0 && clevel <= 9) {
		char mode[3];
		sprintf(mode, "w%d", clevel);
		fpw = bgzf_dopen(fileno(stdout), mode);
	} else fpw = bgzf_dopen(fileno(stdout), "w");
	bgzf_mt(fpw, 3, 256);
	bam_hdr_write(fpw, h);
	aux = kh_init(64);
	marked = kh_init(s);
	q = kdq_init(elem_t);

	b = bam_init1();
	while ((ret = bam_read1(fpr, b)) >= 0) {
		elem_t *e;
		b->core.flag &= ~BAM_FDUP;
		if (b->core.tid != last_tid || b->core.pos != last_pos) {
			if (last_tid >= 0 && last_pos >= 0)
				aux = process(q, fpw, marked, aux, bc_type);
			last_tid = b->core.tid, last_pos = b->core.pos;
		}
		if (b->core.tid < 0) break;
		e = kdq_pushp(elem_t, q);
		e->b = bam_init1();
		bam_copy1(e->b, b);
	}
	aux = process(q, fpw, marked, aux, bc_type);
	if (ret >= 0 && !drop_unmapped) {
		do {
			bam_write1(fpw, b);
		} while (bam_read1(fpr, b) >= 0);
	}
	bam_destroy1(b);

	kdq_destroy(elem_t, q);
	fprintf(stderr, "[M::%s] %ld+%ld fragments; %ld+%ld duplicates; %d unpaired reads\n", __func__,
			(long)lt_n_frags_BC, (long)lt_n_frags_noBC, (long)lt_n_dups_BC, (long)lt_n_dups_noBC, kh_size(marked));
	for (k = 0; k < kh_end(marked); ++k) 
		if (kh_exist(marked, k)) free((char*)kh_key(marked, k));
	kh_destroy(s, marked);
	kh_destroy(64, aux);
	bgzf_close(fpw);
	bam_hdr_destroy(h);
	bgzf_close(fpr);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %r15d
leaq 0xb849(%rip), %rdx      # 0xe030
callq 0x2380
xorl %ebp, %ebp
testl %eax, %eax
js 0x286a
xorl %ebp, %ebp
movl $0x2, 0xc(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0xb827(%rip), %r12      # 0xe030
movq 0xf7c8(%rip), %r13      # 0x11fd8
cmpl $0x53, %eax
jg 0x2826
cmpl $0x42, %eax
je 0x283e
cmpl $0x44, %eax
jne 0x2850
movl $0x1, %ebp
jmp 0x2850
cmpl $0x54, %eax
je 0x2848
cmpl $0x6c, %eax
jne 0x2850
movq (%r13), %rdi
callq 0x2390
movl %eax, %r14d
jmp 0x2850
movl $0x0, 0xc(%rsp)
jmp 0x2850
movl $0x1, 0xc(%rsp)
movl %r15d, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2380
testl %eax, %eax
jns 0x2810
testl %ebp, %ebp
setne %bpl
jmp 0x2878
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x2, 0xc(%rsp)
movq 0xf751(%rip), %rax      # 0x11fd0
movslq (%rax), %rax
cmpl %r15d, %eax
jne 0x2896
callq 0x2430
movl $0x1, %eax
jmp 0x2d6d
movq (%rbx,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x28c4
cmpb $0x0, 0x1(%rdi)
jne 0x28c4
movq 0xf71c(%rip), %rax      # 0x11fc8
movq (%rax), %rdi
callq 0x2260
leaq 0xc0cf(%rip), %rsi      # 0xe98a
movl %eax, %edi
callq 0x3c2b
jmp 0x28d0
leaq 0xc0bf(%rip), %rsi      # 0xe98a
callq 0x3ad0
movq %rax, %r15
movq %rax, %rdi
callq 0x8bb2
movq %rax, %r12
cmpl $0x9, %r14d
movl %ebp, 0x38(%rsp)
ja 0x2917
leaq 0xb834(%rip), %rsi      # 0xe123
leaq 0x45(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x23a0
movq 0xf6b8(%rip), %rax      # 0x11fc0
movq (%rax), %rdi
callq 0x2260
movl %eax, %edi
movq %rbx, %rsi
jmp 0x292f
movq 0xf6a2(%rip), %rax      # 0x11fc0
movq (%rax), %rdi
callq 0x2260
leaq 0xb7fa(%rip), %rsi      # 0xe127
movl %eax, %edi
callq 0x3c2b
movq %rax, %rbx
movq %rax, %rdi
movl $0x3, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3f4c
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r12, 0x48(%rsp)
movq %r12, %rsi
callq 0x8d94
movl $0x1, %edi
movl $0x28, %esi
callq 0x21f0
movq %rax, 0x28(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x21f0
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x21f0
movq %rax, %r13
movabsq $0x800000000000000, %rax # imm = 0x800000000000000
movq %rax, (%r13)
movq $0x3, 0x10(%r13)
movl $0x40, %edi
callq 0x22a0
movq %rax, 0x18(%r13)
callq 0x8f83
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x90b0
testl %eax, %eax
movq %r15, 0x30(%rsp)
js 0x2beb
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r14, 0x50(%rsp)
movq %r13, 0x20(%rsp)
andb $-0x5, 0xd(%r14)
movl (%r14), %eax
cmpl %ecx, %eax
jne 0x2a0c
cmpl %edx, 0x4(%r14)
je 0x2a3a
orl %edx, %ecx
js 0x2a34
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0x2d7c
movq %rax, 0x28(%rsp)
movl (%r14), %eax
movl 0x4(%r14), %edx
movl %eax, %ecx
testl %ecx, %ecx
js 0x2c0f
movl %edx, 0x3c(%rsp)
movl %ecx, 0x40(%rsp)
movq (%r13), %rbx
movq 0x8(%r13), %rbp
movq %rbx, %rax
shrq $0x3a, %rax
movl $0x1, %edx
movl %eax, %ecx
shlq %cl, %rdx
cmpq %rdx, %rbp
jne 0x2b9d
leal 0x1(%rax), %r15d
movl $0x1, %r12d
movl %r15d, %ecx
shlq %cl, %r12
cmpq %rbp, %r12
jae 0x2ac9
movq $-0x1, %r15
incq %r15
movq %rbp, %rdx
movl %r15d, %ecx
shrq %cl, %rdx
cmpq $0x3f, %r15
ja 0x2a9f
testq %rdx, %rdx
jne 0x2a88
cmpq %r15, %rax
jne 0x2ab8
movq 0x30(%rsp), %r15
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
jmp 0x2b9d
movl $0x1, %r12d
movl %r15d, %ecx
shlq %cl, %r12
cmpq %rax, %r15
jbe 0x2ae1
movq 0x18(%r13), %rdi
movl $0x10, %esi
movl %r15d, %ecx
shlq %cl, %rsi
callq 0x22e0
movq %rax, 0x18(%r13)
movq %rbx, %r14
movabsq $0x3ffffffffffffff, %r13 # imm = 0x3FFFFFFFFFFFFFF
andq %r13, %r14
leaq (%r14,%rbp), %rdx
cmpq %rbp, %rdx
jbe 0x2b3a
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq %rbp, %rdx
subq %r14, %rdx
movq %r12, %rdi
subq %rdx, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %r14
addq %rax, %r14
shlq $0x4, %rdx
movq %r14, %rsi
callq 0x2310
subq %rbp, %r12
addq %rbx, %r12
andq %r13, %r12
movq %r12, %r14
movq %r13, %r12
jmp 0x2b71
subq %r12, %rdx
jbe 0x2b67
movq 0x20(%rsp), %r13
movq 0x18(%r13), %rdi
shlq $0x4, %r12
addq %rdi, %r12
shlq $0x4, %rdx
movq %r12, %rsi
callq 0x2310
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
jmp 0x2b76
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
movq 0x20(%rsp), %r13
movq %r15, %rbx
shlq $0x3a, %rbx
movq $-0x1, %rax
movl %r15d, %ecx
shlq %cl, %rax
orq %r14, %rbx
movq %rbx, (%r13)
notq %rax
movq %rax, 0x10(%r13)
movq 0x30(%rsp), %r15
movq 0x18(%r13), %r14
leaq 0x1(%rbp), %rax
movq %rax, 0x8(%r13)
andq %r12, %rbx
addq %rbp, %rbx
andq 0x10(%r13), %rbx
shlq $0x4, %rbx
callq 0x8f83
movq %rax, (%r14,%rbx)
movq %rax, %rdi
movq 0x50(%rsp), %r14
movq %r14, %rsi
callq 0x8fae
movq %r15, %rdi
movq %r14, %rsi
callq 0x90b0
testl %eax, %eax
movl 0x40(%rsp), %ecx
movl 0x3c(%rsp), %edx
jns 0x29fa
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0x2d7c
movq %rax, %r12
jmp 0x2c59
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0x2d7c
movq %rax, %r12
cmpb $0x0, 0x38(%rsp)
jne 0x2c54
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x93f4
movq %r15, %rdi
movq %r14, %rsi
callq 0x90b0
testl %eax, %eax
jns 0x2c3a
movq 0x10(%rsp), %rbx
movq %r14, %rdi
callq 0x8f92
movq 0x18(%r13), %rdi
callq 0x2050
movq %r13, %rdi
callq 0x2050
movq 0xf37f(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movq 0xf6cd(%rip), %rcx      # 0x12350
movq 0xf6ce(%rip), %r8       # 0x12358
movq 0xf6cf(%rip), %r9       # 0x12360
movl 0x4(%rbx), %r10d
leaq 0xb48d(%rip), %rsi      # 0xe129
leaq 0xb4c8(%rip), %rdx      # 0xe16b
xorl %r14d, %r14d
xorl %eax, %eax
pushq %r10
pushq 0xf6b8(%rip)            # 0x12368
callq 0x2220
addq $0x10, %rsp
movl (%rbx), %r15d
movq 0x10(%rbx), %rbx
testq %r15, %r15
je 0x2cfb
xorl %r13d, %r13d
movl %r13d, %eax
shrl $0x4, %eax
movl (%rbx,%rax,4), %eax
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x2cef
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r13,8), %rdi
callq 0x2050
incq %r13
addl $0x2, %r14d
cmpq %r13, %r15
jne 0x2cc8
movq 0x10(%rsp), %r14
movq 0x18(%r14), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2050
movq 0x20(%r14), %rdi
callq 0x2050
movq %r14, %rdi
callq 0x2050
testq %r12, %r12
je 0x2d4d
movq 0x18(%r12), %rdi
callq 0x2050
movq 0x10(%r12), %rdi
callq 0x2050
movq 0x20(%r12), %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movq 0x18(%rsp), %rdi
callq 0x44e0
movq 0x48(%rsp), %rdi
callq 0x8b3b
movq 0x30(%rsp), %rdi
callq 0x44e0
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq