// File: /workspace/github_c_makefile_star2_O2/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, %r14d
pushq $-0x1
popq %rbp
pushq $0x2
popq %rax
movl %eax, 0x14(%rsp)
movl $0x0, 0x24(%rsp)
leaq 0x9ad7(%rip), %r15      # 0xc030
pushq $0x1
popq %r12
movq 0xea74(%rip), %r13      # 0x10fd8
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2390
testl %eax, %eax
js 0x25b0
cmpl $0x42, %eax
je 0x2597
cmpl $0x44, %eax
je 0x25a1
cmpl $0x54, %eax
je 0x25a9
cmpl $0x6c, %eax
jne 0x2564
movq (%r13), %rdi
callq 0x23a0
movl %eax, %ebp
jmp 0x2564
movl $0x0, 0x14(%rsp)
jmp 0x2564
movb $0x1, %al
movl %eax, 0x24(%rsp)
jmp 0x2564
movl %r12d, 0x14(%rsp)
jmp 0x2564
movq 0xea19(%rip), %rax      # 0x10fd0
movslq (%rax), %rax
cmpl %r14d, %eax
jne 0x2653
movq 0xea2e(%rip), %r14      # 0x10ff8
movq (%r14), %rcx
leaq 0x9a62(%rip), %rdi      # 0xc036
pushq $0x25
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x23c0
movq (%r14), %rcx
leaq 0x9a70(%rip), %rdi      # 0xc05c
pushq $0x9
popq %rsi
movq %rbx, %rdx
callq 0x23c0
movq (%r14), %rcx
leaq 0x9a65(%rip), %rdi      # 0xc066
pushq $0x32
popq %rsi
movq %rbx, %rdx
callq 0x23c0
movq (%r14), %rcx
leaq 0x9a83(%rip), %rdi      # 0xc099
pushq $0x39
popq %rsi
movq %rbx, %rdx
callq 0x23c0
movq (%r14), %rcx
leaq 0x9aa8(%rip), %rdi      # 0xc0d3
pushq $0x1b
popq %rsi
movq %rbx, %rdx
callq 0x23c0
movq (%r14), %rcx
leaq 0x9aaf(%rip), %rdi      # 0xc0ef
pushq $0x33
popq %rsi
movq %rbx, %rdx
callq 0x23c0
pushq $0x1
popq %rax
jmp 0x2aa6
movq (%rbx,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x2681
cmpb $0x0, 0x1(%rdi)
jne 0x2681
movq 0xe95f(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x2260
leaq 0xa296(%rip), %rsi      # 0xc90e
movl %eax, %edi
callq 0x3827
jmp 0x268d
leaq 0xa286(%rip), %rsi      # 0xc90e
callq 0x36d8
movq %rax, %r13
movq %rax, %rdi
callq 0x7b51
movq %rax, %rbx
cmpl $0x9, %ebp
ja 0x26ce
leaq 0x9a7c(%rip), %rsi      # 0xc123
leaq 0x45(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x23b0
movq 0xe901(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2260
movl %eax, %edi
movq %r14, %rsi
jmp 0x26e6
movq 0xe8eb(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2260
leaq 0x9a43(%rip), %rsi      # 0xc127
movl %eax, %edi
callq 0x3827
movq %rax, %r14
pushq $0x3
popq %rsi
movq %rax, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x3b2c
movq %r14, 0x38(%rsp)
movq %r14, %rdi
movq %rbx, 0x48(%rsp)
movq %rbx, %rsi
callq 0x7d4f
callq 0x368f
movq %rax, 0x28(%rsp)
pushq $0x1
popq %r12
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x21f0
movq %rax, 0x18(%rsp)
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x21f0
movq %rax, %rbx
movabsq $0x800000000000000, %rax # imm = 0x800000000000000
movq %rax, (%rbx)
movq $0x3, 0x10(%rbx)
pushq $0x40
popq %rdi
callq 0x22a0
movq %rbx, 0x8(%rsp)
movq %rax, 0x18(%rbx)
pushq $-0x1
popq %rbx
callq 0x7f20
movq %rax, %r14
movl %ebx, %r15d
movq %r13, 0x50(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x804b
movl %eax, %ebp
testl %eax, %eax
js 0x2961
andb $-0x5, 0xd(%r14)
movl (%r14), %eax
cmpl %r15d, %eax
movq 0x8(%rsp), %rdi
jne 0x27a5
cmpl %ebx, 0x4(%r14)
je 0x27dc
orl %ebx, %r15d
js 0x27d5
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl 0x14(%rsp), %r8d
callq 0x2ab5
movq 0x8(%rsp), %rdi
movq %rax, 0x28(%rsp)
movl (%r14), %eax
movl 0x4(%r14), %ebx
movl %eax, %r15d
testl %r15d, %r15d
js 0x2966
movl %r15d, 0x30(%rsp)
movl %ebx, 0x34(%rsp)
movq (%rdi), %r15
movq 0x8(%rdi), %rbx
movq %r15, %rax
shrq $0x3a, %rax
movq %r12, %rdx
movl %eax, %ecx
shlq %cl, %rdx
cmpq %rdx, %rbx
jne 0x2834
leal 0x1(%rax), %ebp
movl %ebp, %ecx
shlq %cl, %r12
cmpq %rbx, %r12
jae 0x2851
pushq $-0x1
popq %rbp
incq %rbp
movq %rbx, %rdx
movl %ebp, %ecx
shrq %cl, %rdx
cmpq $0x3f, %rbp
ja 0x282f
testq %rdx, %rdx
jne 0x2819
cmpq %rbp, %rax
jne 0x2843
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
jmp 0x2921
pushq $0x1
popq %r12
movl %ebp, %ecx
shlq %cl, %r12
cmpq %rax, %rbp
jbe 0x2870
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
pushq $0x10
popq %rsi
movl %ebp, %ecx
shlq %cl, %rsi
callq 0x22f0
movq 0x8(%rsp), %rdi
movq %rax, 0x18(%rdi)
movabsq $0x3ffffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFFF
movq %r15, %r13
andq %rax, %r13
leaq (%rbx,%r13), %rdx
cmpq %rbx, %rdx
jbe 0x28d0
movq 0x18(%rdi), %rax
movq %rbx, %rdx
subq %r13, %rdx
movq %r12, %rdi
subq %rdx, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %r13
addq %rax, %r13
shlq $0x4, %rdx
movq %r13, %rsi
callq 0x2320
movq 0x8(%rsp), %rdi
subq %rbx, %r12
addq %r15, %r12
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
andq %rdx, %r12
movq %r12, %r13
jmp 0x2900
subq %r12, %rdx
jbe 0x28f6
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
shlq $0x4, %r12
addq %rdi, %r12
shlq $0x4, %rdx
movq %r12, %rsi
callq 0x2320
movq 0x8(%rsp), %rdi
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
movq %rbp, %r15
shlq $0x3a, %r15
pushq $-0x1
popq %rax
movl %ebp, %ecx
shlq %cl, %rax
orq %r13, %r15
movq %r15, (%rdi)
notq %rax
movq %rax, 0x10(%rdi)
movq 0x50(%rsp), %r13
movq 0x18(%rdi), %r12
leaq 0x1(%rbx), %rax
movq %rax, 0x8(%rdi)
andq %rdx, %r15
addq %rbx, %r15
andq 0x10(%rdi), %r15
shlq $0x4, %r15
callq 0x7f20
movq %rax, (%r12,%r15)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7f47
pushq $0x1
popq %r12
movl 0x34(%rsp), %ebx
movl 0x30(%rsp), %r15d
jmp 0x2778
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl 0x14(%rsp), %r8d
callq 0x2ab5
movq %rax, %r12
testl %ebp, %ebp
movq %rbx, %rbp
sets %al
orb 0x24(%rsp), %al
testb $0x1, %al
jne 0x29af
movq %rbp, %rdi
movq %r14, %rsi
callq 0x835a
movq %r13, %rdi
movq %r14, %rsi
callq 0x804b
testl %eax, %eax
jns 0x2995
movq %r14, %rdi
callq 0x7f2b
movq 0x8(%rsp), %rbx
movq 0x18(%rbx), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2050
movq 0xe624(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movq 0xe982(%rip), %rcx      # 0x11360
movq 0xe983(%rip), %r8       # 0x11368
movq 0xe984(%rip), %r9       # 0x11370
movq 0x18(%rsp), %r14
movl 0x4(%r14), %r10d
leaq 0x972d(%rip), %rsi      # 0xc129
leaq 0x9768(%rip), %rdx      # 0xc16b
xorl %ebx, %ebx
xorl %eax, %eax
pushq %r10
pushq 0xe969(%rip)            # 0x11378
callq 0x2220
addq $0x10, %rsp
movl (%r14), %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x2a5a
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl %r15d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x2a52
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rdi
callq 0x2050
incq %r15
addl $0x2, %ebx
jmp 0x2a1e
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rdi
callq 0x2050
movq 0x10(%rbx), %rdi
callq 0x2050
movq 0x20(%rbx), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2050
movq %r12, %rdi
callq 0x3661
movq %rbp, %rdi
callq 0x3fbb
movq 0x48(%rsp), %rdi
callq 0x7ae3
movq %r13, %rdi
callq 0x3fbb
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq