// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_md.c
// Function: bam_fillmd
int bam_fillmd(int argc, char *argv[])
{
	int c, flt_flag, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag;
	samfile_t *fp, *fpout = 0;
	faidx_t *fai;
	char *ref = 0, mode_w[8], mode_r[8];
	bam1_t *b;

	flt_flag = UPDATE_NM | UPDATE_MD;
	is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0;
	mode_w[0] = mode_r[0] = 0;
	strcpy(mode_r, "r"); strcpy(mode_w, "w");
	while ((c = getopt(argc, argv, "EqreuNhbSC:n:Ad")) >= 0) {
		switch (c) {
		case 'r': is_realn = 1; break;
		case 'e': flt_flag |= USE_EQUAL; break;
		case 'd': flt_flag |= DROP_TAG; break;
		case 'q': flt_flag |= BIN_QUAL; break;
		case 'h': flt_flag |= HASH_QNM; break;
		case 'N': flt_flag &= ~(UPDATE_MD|UPDATE_NM); break;
		case 'b': is_bam_out = 1; break;
		case 'u': is_uncompressed = is_bam_out = 1; break;
		case 'S': is_sam_in = 1; break;
		case 'n': max_nm = atoi(optarg); break;
		case 'C': capQ = atoi(optarg); break;
		case 'A': baq_flag |= 1; break;
		case 'E': baq_flag |= 2; break;
		default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1;
		}
	}
	if (!is_sam_in) strcat(mode_r, "b");
	if (is_bam_out) strcat(mode_w, "b");
	else strcat(mode_w, "h");
	if (is_uncompressed) strcat(mode_w, "u");
	if (optind + 1 >= argc) {
		fprintf(stderr, "\n");
		fprintf(stderr, "Usage:   samtools fillmd [-eubrS] <aln.bam> <ref.fasta>\n\n");
		fprintf(stderr, "Options: -e       change identical bases to '='\n");
		fprintf(stderr, "         -u       uncompressed BAM output (for piping)\n");
		fprintf(stderr, "         -b       compressed BAM output\n");
		fprintf(stderr, "         -S       the input is SAM with header\n");
		fprintf(stderr, "         -A       modify the quality string\n");
		fprintf(stderr, "         -r       compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n");
		fprintf(stderr, "         -E       extended BAQ for better sensitivity but lower specificity\n\n");
		return 1;
	}
	fp = samopen(argv[optind], mode_r, 0);
	if (fp == 0) return 1;
	if (is_sam_in && (fp->header == 0 || fp->header->n_targets == 0)) {
		fprintf(stderr, "[bam_fillmd] input SAM does not have header. Abort!\n");
		return 1;
	}
	fpout = samopen("-", mode_w, fp->header);
	fai = fai_load(argv[optind+1]);

	b = bam_init1();
	while ((ret = samread(fp, b)) >= 0) {
		if (b->core.tid >= 0) {
			if (tid != b->core.tid) {
				free(ref);
				ref = fai_fetch(fai, fp->header->target_name[b->core.tid], &len);
				tid = b->core.tid;
				if (ref == 0)
					fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n",
							fp->header->target_name[tid]);
			}
			if (is_realn) bam_prob_realn_core(b, ref, baq_flag);
			if (capQ > 10) {
				int q = bam_cap_mapQ(b, ref, capQ);
				if (b->core.qual > q) b->core.qual = q;
			}
			if (ref) bam_fillmd1_core(b, ref, flt_flag, max_nm);
		}
		samwrite(fpout, b);
	}
	bam_destroy1(b);

	free(ref);
	fai_destroy(fai);
	samclose(fp); samclose(fpout);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movl %edi, %r13d
movw $0x72, 0x2c(%rsp)
movw $0x77, 0x10(%rsp)
leaq 0x15edf(%rip), %rdx     # 0x44825
callq 0x3210
testl %eax, %eax
js 0x2ea97
xorl %r14d, %r14d
movl $0x18, %ebx
leaq 0x15ec3(%rip), %r15     # 0x44825
leaq 0x15def(%rip), %rbp     # 0x44758
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
cmpl $0x4d, %eax
jle 0x2e9b7
leal -0x62(%rax), %ecx
cmpl $0x13, %ecx
ja 0x2e9ed
movslq (%rbp,%rcx,4), %rcx
addq %rbp, %rcx
jmpq *%rcx
orl $0x2, %ebx
jmp 0x2ea45
cmpl $0x41, %eax
je 0x2e9d1
cmpl $0x43, %eax
je 0x2e9d8
cmpl $0x45, %eax
jne 0x2ecf8
orl $0x2, 0x4(%rsp)
jmp 0x2ea45
orl $0x1, 0x4(%rsp)
jmp 0x2ea45
movq 0x21601(%rip), %rax     # 0x4ffe0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0x8(%rsp)
jmp 0x2ea45
cmpl $0x4e, %eax
je 0x2ea42
cmpl $0x53, %eax
jne 0x2ecf8
movl $0x1, 0x20(%rsp)
jmp 0x2ea45
movq 0x215d4(%rip), %rax     # 0x4ffe0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0xc(%rsp)
jmp 0x2ea45
orl $0x20, %ebx
jmp 0x2ea45
movl $0x1, 0x18(%rsp)
movl $0x1, %r14d
jmp 0x2ea45
orl $0x1, %ebx
jmp 0x2ea45
movl $0x1, (%rsp)
jmp 0x2ea45
orl $0x4, %ebx
jmp 0x2ea45
andl $-0x19, %ebx
movl %r13d, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3210
testl %eax, %eax
jns 0x2e998
cmpl $0x0, (%rsp)
sete (%rsp)
cmpl $0x0, 0x20(%rsp)
sete %r15b
jne 0x2ea7f
leaq 0x2c(%rsp), %rdi
callq 0x3360
movw $0x62, 0x2c(%rsp,%rax)
leaq 0x10(%rsp), %rdi
testl %r14d, %r14d
je 0x2ead2
callq 0x3360
movw $0x62, 0x10(%rsp,%rax)
jmp 0x2eade
leaq 0x2c(%rsp), %rdi
callq 0x3360
movw $0x62, 0x2c(%rsp,%rax)
movb $0x1, (%rsp)
movl $0x0, 0x4(%rsp)
movl $0x18, %ebx
movw $0x68, %bp
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movb $0x1, %r15b
jmp 0x2eae9
callq 0x3360
movw $0x68, 0x10(%rsp,%rax)
movw $0x75, %bp
cmpl $0x0, 0x18(%rsp)
je 0x2eaf8
leaq 0x10(%rsp), %rdi
callq 0x3360
movw %bp, 0x10(%rsp,%rax)
movq 0x214b9(%rip), %r14     # 0x4ffb8
movslq (%r14), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jge 0x2ece5
movl %r15d, %r13d
movq (%r12,%rax,8), %rdi
leaq 0x2c(%rsp), %rsi
xorl %edx, %edx
callq 0x27ef0
movl $0x1, %ebp
testq %rax, %rax
je 0x2ecd4
movq %rax, %r15
movq 0x10(%rax), %rdx
testb %r13b, %r13b
jne 0x2eb4d
testq %rdx, %rdx
je 0x2ecf1
cmpl $0x0, (%rdx)
je 0x2ecf1
leaq 0x1133c(%rip), %rdi     # 0x3fe90
leaq 0x10(%rsp), %rsi
callq 0x27ef0
movq %rax, %r13
movslq (%r14), %rax
movq 0x8(%r12,%rax,8), %rdi
callq 0x2faa6
movq %rax, 0x18(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x283b1
testl %eax, %eax
js 0x2ec97
movq %r15, %rax
addq $0x10, %rax
movq %rax, 0x20(%rsp)
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
xorl %ebp, %ebp
movl (%r12), %eax
testl %eax, %eax
js 0x2ec63
cmpl %eax, %r14d
je 0x2ebf8
movq %rbp, %rdi
callq 0x3350
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movslq (%r12), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x18(%rsp), %rdi
leaq 0x34(%rsp), %rdx
callq 0x2fb9f
movl (%r12), %r14d
testq %rax, %rax
je 0x2ec83
movq %rax, %rbp
cmpb $0x0, (%rsp)
jne 0x2ec0d
movq %r12, %rdi
movq %rbp, %rsi
movl 0x4(%rsp), %edx
callq 0x2e0ec
cmpl $0xb, 0x8(%rsp)
jl 0x2ec49
movq %r12, %rdi
movq %rbp, %rsi
movl 0x8(%rsp), %edx
callq 0x2de16
movq 0x8(%r12), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl %eax, %edx
jle 0x2ec49
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%r12)
testq %rbp, %rbp
je 0x2ec61
movq %r12, %rdi
movq %rbp, %rsi
movl %ebx, %edx
movl 0xc(%rsp), %ecx
callq 0x2d484
jmp 0x2ec63
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
callq 0x283e6
movq %r15, %rdi
movq %r12, %rsi
callq 0x283b1
testl %eax, %eax
jns 0x2ebac
jmp 0x2ec9e
movq 0x20(%rsp), %rdi
movl %r14d, %esi
callq 0x54d7
xorl %ebp, %ebp
jmp 0x2ebf8
xorl %ebp, %ebp
testq %r12, %r12
je 0x2ecb0
movq 0x30(%r12), %rdi
callq 0x3350
movq %r12, %rdi
callq 0x3350
movq %rbp, %rdi
callq 0x3350
movq 0x18(%rsp), %rdi
callq 0x2f578
movq %r15, %rdi
callq 0x2836b
movq %r13, %rdi
callq 0x2836b
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x53ed
movl $0x1, %ebp
jmp 0x2ecd4
callq 0x54b5
jmp 0x2ecd4
movq 0x212e9(%rip), %rcx     # 0x4ffe8
movq (%rcx), %rdi
leaq 0x15b2c(%rip), %rsi     # 0x44835
movl %eax, %edx
xorl %eax, %eax
callq 0x3750
jmp 0x2ecea