// File: /workspace/github_c_makefile_star2_O1/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, %ebp
leaq 0xb802(%rip), %rdx      # 0xe018
callq 0x2380
xorl %r13d, %r13d
testl %eax, %eax
js 0x2899
movl $0x2, 0xc(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0xb7e1(%rip), %r15      # 0xe018
movq 0xf79a(%rip), %r12      # 0x11fd8
cmpl $0x53, %eax
jg 0x2855
cmpl $0x42, %eax
je 0x286d
cmpl $0x44, %eax
jne 0x287f
movl $0x1, %r13d
jmp 0x287f
cmpl $0x54, %eax
je 0x2877
cmpl $0x6c, %eax
jne 0x287f
movq (%r12), %rdi
callq 0x2390
movl %eax, %r14d
jmp 0x287f
movl $0x0, 0xc(%rsp)
jmp 0x287f
movl $0x1, 0xc(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2380
testl %eax, %eax
jns 0x283e
testl %r13d, %r13d
setne %r13b
jmp 0x28a7
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x2, 0xc(%rsp)
movq 0xf722(%rip), %rax      # 0x11fd0
movslq (%rax), %rax
cmpl %ebp, %eax
jne 0x28c4
callq 0x2430
movl $0x1, %eax
jmp 0x2da2
movq (%rbx,%rax,8), %rbx
leaq 0xb83c(%rip), %rsi      # 0xe10b
movq %rbx, %rdi
callq 0x2200
testl %eax, %eax
jne 0x28fa
movq 0xf6e6(%rip), %rax      # 0x11fc8
movq (%rax), %rdi
callq 0x2260
leaq 0xc01f(%rip), %rsi      # 0xe910
movl %eax, %edi
callq 0x3c4f
jmp 0x2909
leaq 0xc00f(%rip), %rsi      # 0xe910
movq %rbx, %rdi
callq 0x3abc
movq %rax, %r15
movq %rax, %rdi
callq 0x89a0
movq %rax, 0x38(%rsp)
cmpl $0x9, %r14d
movl %r13d, 0x24(%rsp)
ja 0x2953
leaq 0xb7e2(%rip), %rsi      # 0xe10d
leaq 0x55(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x23a0
movq 0xf67c(%rip), %rax      # 0x11fc0
movq (%rax), %rdi
callq 0x2260
movl %eax, %edi
movq %rbx, %rsi
jmp 0x296b
movq 0xf666(%rip), %rax      # 0x11fc0
movq (%rax), %rdi
callq 0x2260
leaq 0xb7a8(%rip), %rsi      # 0xe111
movl %eax, %edi
callq 0x3c4f
movq %rax, %rbx
movabsq $0x3ffffffffffffff, %r14 # imm = 0x3FFFFFFFFFFFFFF
movq %rax, %rdi
movl $0x3, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3f7d
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x8b81
movl $0x1, %edi
movl $0x28, %esi
callq 0x21f0
movq %rax, 0x18(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x21f0
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x21f0
movq %rax, %r13
movq (%rax), %rax
andq %r14, %rax
btsq $0x3b, %rax
movq %rax, (%r13)
movq $0x3, 0x10(%r13)
movl $0x40, %edi
callq 0x22a0
movq %rax, 0x18(%r13)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x8d74
movq %rax, %r14
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %r15, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x8e9f
movl %eax, 0x28(%rsp)
testl %eax, %eax
js 0x2c41
andb $-0x5, 0xd(%r14)
cmpl %r12d, (%r14)
jne 0x2a4b
cmpl %ebp, 0x4(%r14)
jne 0x2a4b
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
jmp 0x2a80
orl %ebp, %r12d
js 0x2a6f
movq %r13, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0x2db1
movq %rax, 0x18(%rsp)
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
movl (%r14), %r12d
movl 0x4(%r14), %ebp
movl (%r14), %eax
testl %eax, %eax
js 0x2c41
movl %eax, 0x2c(%rsp)
movl %r12d, 0x30(%rsp)
movl %ebp, 0x34(%rsp)
movq (%r13), %rax
movq 0x8(%r13), %r15
shrq $0x3a, %rax
movl $0x1, %r12d
movl %eax, %ecx
shlq %cl, %r12
cmpq %r12, %r15
jne 0x2bed
leal 0x1(%rax), %ebp
movl $0x1, %ebx
movl %ebp, %ecx
shlq %cl, %rbx
cmpq %r15, %rbx
jae 0x2af1
movq $-0x1, %rbp
incq %rbp
movq %r15, %rdx
movl %ebp, %ecx
shrq %cl, %rdx
cmpq $0x3f, %rbp
ja 0x2ae7
testq %rdx, %rdx
jne 0x2ad1
movl $0x1, %ebx
movl %ebp, %ecx
shlq %cl, %rbx
cmpl %eax, %ebp
je 0x2bed
jle 0x2b1c
movq 0x18(%r13), %rdi
movl $0x10, %esi
movl %ebp, %ecx
shlq %cl, %rsi
callq 0x22e0
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
movq %rax, 0x18(%r13)
movq (%r13), %rax
movq %rax, %rsi
andq %rdi, %rsi
addq %rsi, %r15
cmpq %r12, %r15
jbe 0x2b81
movq %rax, %r15
movq 0x18(%r13), %rax
movq %r12, %rdx
subq %rsi, %rdx
movq %rbx, %rdi
subq %rdx, %rdi
shlq $0x4, %rdi
addq %rax, %rdi
shlq $0x4, %rsi
addq %rax, %rsi
shlq $0x4, %rdx
callq 0x2310
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
subq %r12, %rbx
addq %r15, %rbx
andq %rdi, %rbx
movabsq $-0x400000000000000, %rax # imm = 0xFC00000000000000
andq %rax, %r15
orq %rbx, %r15
movq %r15, (%r13)
jmp 0x2baa
subq %rbx, %r15
jbe 0x2baa
movq 0x18(%r13), %rdi
shlq $0x4, %rbx
addq %rdi, %rbx
shlq $0x4, %r15
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2310
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
movq %rbp, %rax
shlq $0x3a, %rax
movq (%r13), %rdx
andq %rdi, %rdx
orq %rax, %rdx
movq $-0x1, %rax
movl %ebp, %ecx
shlq %cl, %rax
movq %rdx, (%r13)
notq %rax
movq %rax, 0x10(%r13)
testl %ebp, %ebp
jns 0x2bed
movq 0x18(%r13), %rdi
callq 0x22e0
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
movq %rax, 0x18(%r13)
movq 0x8(%r13), %rax
movq 0x18(%r13), %rbx
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r13)
movq (%r13), %r15
andq %rdi, %r15
addq %rax, %r15
andq 0x10(%r13), %r15
shlq $0x4, %r15
callq 0x8d74
movq %rax, (%rbx,%r15)
movq %rax, %rdi
movq %r14, %rsi
callq 0x8d9f
cmpl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %rbx
movl 0x34(%rsp), %ebp
movl 0x30(%rsp), %r12d
jns 0x2a18
movq %r13, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0x2db1
movq %rax, %rbp
cmpl $0x0, 0x28(%rsp)
sets %al
orb 0x24(%rsp), %al
testb $0x1, %al
jne 0x2c88
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9204
movq %r15, %rdi
movq %r14, %rsi
callq 0x8e9f
testl %eax, %eax
jns 0x2c6e
movq %r14, %rdi
callq 0x8d83
movq 0x18(%r13), %rdi
callq 0x2050
movq %r13, %rdi
callq 0x2050
movq 0xf350(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movq 0xf69e(%rip), %rcx      # 0x12350
movq 0xf69f(%rip), %r8       # 0x12358
movq 0xf6a0(%rip), %r9       # 0x12360
movq 0x10(%rsp), %r14
movl 0x4(%r14), %r10d
leaq 0xb443(%rip), %rsi      # 0xe113
leaq 0xb47e(%rip), %rdx      # 0xe155
xorl %ebx, %ebx
xorl %eax, %eax
pushq %r10
pushq 0xf685(%rip)            # 0x12368
callq 0x2220
addq $0x10, %rsp
movl (%r14), %r14d
testq %r14, %r14
je 0x2d32
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r15
xorl %r12d, %r12d
movl %r12d, %eax
shrl $0x4, %eax
movl (%r15,%rax,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x2d27
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r12,8), %rdi
callq 0x2050
incq %r12
addl $0x2, %ebx
cmpq %r12, %r14
jne 0x2d00
movq 0x10(%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
testq %rbp, %rbp
je 0x2d82
movq 0x18(%rbp), %rdi
callq 0x2050
movq 0x10(%rbp), %rdi
callq 0x2050
movq 0x20(%rbp), %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq 0x40(%rsp), %rdi
callq 0x4433
movq 0x38(%rsp), %rdi
callq 0x892b
movq 0x48(%rsp), %rdi
callq 0x4433
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq