// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movl $0x2, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x10a8e(%rip), %rdx     # 0x13018
callq 0x2390
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jl 0x25e4
cmpl $0x6c, -0x14(%rbp)
jne 0x25b1
movq 0x14a34(%rip), %rax     # 0x16fd8
movq (%rax), %rdi
callq 0x23a0
movl %eax, -0x18(%rbp)
jmp 0x25e2
cmpl $0x54, -0x14(%rbp)
jne 0x25c0
movl $0x1, -0x20(%rbp)
jmp 0x25e0
cmpl $0x42, -0x14(%rbp)
jne 0x25cf
movl $0x0, -0x20(%rbp)
jmp 0x25de
cmpl $0x44, -0x14(%rbp)
jne 0x25dc
movl $0x1, -0x24(%rbp)
jmp 0x25de
jmp 0x25e0
jmp 0x25e2
jmp 0x257c
movq 0x149e5(%rip), %rax     # 0x16fd0
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x2692
movq 0x149fb(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a17(%rip), %rsi     # 0x1301e
movb $0x0, %al
callq 0x2230
movq 0x149e3(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a25(%rip), %rsi     # 0x13044
movb $0x0, %al
callq 0x2230
movq 0x149cb(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a17(%rip), %rsi     # 0x1304e
movb $0x0, %al
callq 0x2230
movq 0x149b3(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a32(%rip), %rsi     # 0x13081
movb $0x0, %al
callq 0x2230
movq 0x1499b(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a54(%rip), %rsi     # 0x130bb
movb $0x0, %al
callq 0x2230
movq 0x14983(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0x10a58(%rip), %rsi     # 0x130d7
movb $0x0, %al
callq 0x2230
movl $0x1, -0x4(%rbp)
jmp 0x29d1
movq -0x10(%rbp), %rax
movq 0x14933(%rip), %rcx     # 0x16fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x10a60(%rip), %rsi     # 0x1310b
callq 0x2210
cmpl $0x0, %eax
je 0x26d9
movq -0x10(%rbp), %rax
movq 0x14910(%rip), %rcx     # 0x16fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x11190(%rip), %rsi     # 0x1385e
callq 0x4790
movq %rax, -0x80(%rbp)
jmp 0x26fa
movq 0x148e8(%rip), %rax     # 0x16fc8
movq (%rax), %rdi
callq 0x2270
movl %eax, %edi
leaq 0x1116d(%rip), %rsi     # 0x1385e
callq 0x4a40
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xc830
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x18(%rbp)
jl 0x2750
cmpl $0x9, -0x18(%rbp)
jg 0x2750
leaq -0x6f(%rbp), %rdi
movl -0x18(%rbp), %edx
leaq 0x109e4(%rip), %rsi     # 0x1310d
movb $0x0, %al
callq 0x23c0
movq 0x14889(%rip), %rax     # 0x16fc0
movq (%rax), %rdi
callq 0x2270
movl %eax, %edi
leaq -0x6f(%rbp), %rsi
callq 0x4a40
movq %rax, -0x40(%rbp)
jmp 0x2771
movq 0x14869(%rip), %rax     # 0x16fc0
movq (%rax), %rdi
callq 0x2270
movl %eax, %edi
leaq 0x109a9(%rip), %rsi     # 0x13111
callq 0x4a40
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl $0x3, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x5150
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xcb30
callq 0x3180
movq %rax, -0x60(%rbp)
callq 0x31a0
movq %rax, -0x58(%rbp)
callq 0x31c0
movq %rax, -0x68(%rbp)
callq 0xce60
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xd090
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jl 0x289d
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x20, %rcx
andq $0xffff, %rcx           # imm = 0xFFFF
andl $0xfffffbff, %ecx       # imm = 0xFFFFFBFF
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xffff, %rdx           # imm = 0xFFFF
shlq $0x20, %rdx
movabsq $-0xffff00000001, %rsi  # imm = 0xFFFF0000FFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movl (%rax), %eax
cmpl -0x28(%rbp), %eax
jne 0x2825
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x2c(%rbp), %eax
je 0x2861
cmpl $0x0, -0x28(%rbp)
jl 0x284e
cmpl $0x0, -0x2c(%rbp)
jl 0x284e
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl -0x20(%rbp), %r8d
callq 0x29e0
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x286c
jmp 0x289d
movq -0x68(%rbp), %rdi
callq 0x3010
movq %rax, -0x78(%rbp)
callq 0xce60
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0xcec0
jmp 0x27b5
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl -0x20(%rbp), %r8d
callq 0x29e0
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x28e9
cmpl $0x0, -0x24(%rbp)
jne 0x28e9
jmp 0x28c8
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xd650
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xd090
cmpl $0x0, %eax
jge 0x28c8
jmp 0x28e9
movq -0x50(%rbp), %rdi
callq 0xce80
movq -0x68(%rbp), %rdi
callq 0x30a0
movq 0x146f6(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
movq 0x14a64(%rip), %rcx     # 0x17370
movq 0x14a65(%rip), %r8      # 0x17378
movq 0x14a66(%rip), %r9      # 0x17380
movq 0x14a67(%rip), %r10     # 0x17388
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
leaq 0x107e4(%rip), %rsi     # 0x13113
leaq 0x1081f(%rip), %rdx     # 0x13155
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2230
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x299d
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x6c(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x6c(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x2990
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x2040
jmp 0x2992
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x294c
movq -0x58(%rbp), %rdi
callq 0x30e0
movq -0x60(%rbp), %rdi
callq 0x3130
movq -0x40(%rbp), %rdi
callq 0x5a10
movq -0x48(%rbp), %rdi
callq 0xc780
movq -0x38(%rbp), %rdi
callq 0x5a10
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)