// File: /workspace/github_c_makefile_star2_O1/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, 0x24(%rsp)
leaq 0x15738(%rip), %rdx     # 0x4373d
callq 0x3210
testl %eax, %eax
js 0x2e142
xorl %ebp, %ebp
movl $0x18, %ebx
leaq 0x1571d(%rip), %r14     # 0x4373d
leaq 0x15645(%rip), %r15     # 0x4366c
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
cmpl $0x4d, %eax
jle 0x2e074
leal -0x62(%rax), %ecx
cmpl $0x13, %ecx
ja 0x2e0aa
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
orl $0x2, %ebx
jmp 0x2e101
cmpl $0x41, %eax
je 0x2e08e
cmpl $0x43, %eax
je 0x2e095
cmpl $0x45, %eax
jne 0x2e3b0
orl $0x2, 0x4(%rsp)
jmp 0x2e101
orl $0x1, 0x4(%rsp)
jmp 0x2e101
movq 0x20f44(%rip), %rax     # 0x4efe0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0x8(%rsp)
jmp 0x2e101
cmpl $0x4e, %eax
je 0x2e0fe
cmpl $0x53, %eax
jne 0x2e3b0
movl $0x1, 0x10(%rsp)
jmp 0x2e101
movq 0x20f17(%rip), %rax     # 0x4efe0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0xc(%rsp)
jmp 0x2e101
orl $0x20, %ebx
jmp 0x2e101
movl $0x1, 0x18(%rsp)
movl $0x1, %ebp
jmp 0x2e101
orl $0x1, %ebx
jmp 0x2e101
movl $0x1, (%rsp)
jmp 0x2e101
orl $0x4, %ebx
jmp 0x2e101
andl $-0x19, %ebx
movl %r13d, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3210
testl %eax, %eax
jns 0x2e056
cmpl $0x0, 0x10(%rsp)
sete %r15b
cmpl $0x0, 0x18(%rsp)
sete %r14b
cmpl $0x0, (%rsp)
sete (%rsp)
testl %ebp, %ebp
movl $0x68, %eax
movl $0x62, %ebp
cmovel %eax, %ebp
jmp 0x2e16d
movl $0x18, %ebx
movw $0x68, %bp
movl $0x0, 0xc(%rsp)
movb $0x1, %r15b
movb $0x1, %r14b
movb $0x1, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
testb %r15b, %r15b
je 0x2e183
leaq 0x2c(%rsp), %rdi
callq 0x3360
movw $0x62, 0x2c(%rsp,%rax)
leaq 0x24(%rsp), %rdi
callq 0x3360
movw %bp, 0x24(%rsp,%rax)
testb %r14b, %r14b
jne 0x2e1a8
leaq 0x24(%rsp), %rdi
callq 0x3360
movw $0x75, 0x24(%rsp,%rax)
movq 0x20e09(%rip), %rax     # 0x4efb8
movslq (%rax), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jge 0x2e39d
movq (%r12,%rax,8), %rdi
leaq 0x2c(%rsp), %rsi
xorl %edx, %edx
callq 0x278cd
movl $0x1, %ebp
testq %rax, %rax
je 0x2e38c
movq %rax, %r14
testb %r15b, %r15b
jne 0x2e1fa
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x2e3a9
cmpl $0x0, (%rax)
je 0x2e3a9
movq 0x10(%r14), %rdx
leaq 0x10c53(%rip), %rdi     # 0x3ee58
leaq 0x24(%rsp), %rsi
callq 0x278cd
movq %rax, %r13
movq 0x20d9f(%rip), %rax     # 0x4efb8
movslq (%rax), %rax
movq 0x8(%r12,%rax,8), %rdi
callq 0x2ee9e
movq %rax, 0x18(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x27d67
testl %eax, %eax
js 0x2e34f
movq %r14, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
xorl %ebp, %ebp
movl (%r12), %eax
testl %eax, %eax
js 0x2e31b
cmpl %eax, %r15d
je 0x2e2b0
movq %rbp, %rdi
callq 0x3350
movq 0x10(%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 0x2ef9c
movl (%r12), %r15d
testq %rax, %rax
je 0x2e33b
movq %rax, %rbp
cmpb $0x0, (%rsp)
jne 0x2e2c5
movq %r12, %rdi
movq %rbp, %rsi
movl 0x4(%rsp), %edx
callq 0x2d7a9
cmpl $0xb, 0x8(%rsp)
jl 0x2e301
movq %r12, %rdi
movq %rbp, %rsi
movl 0x8(%rsp), %edx
callq 0x2d466
movq 0x8(%r12), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl %eax, %edx
jle 0x2e301
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%r12)
testq %rbp, %rbp
je 0x2e319
movq %r12, %rdi
movq %rbp, %rsi
movl %ebx, %edx
movl 0xc(%rsp), %ecx
callq 0x2caa8
jmp 0x2e31b
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
callq 0x27d9c
movq %r14, %rdi
movq %r12, %rsi
callq 0x27d67
testl %eax, %eax
jns 0x2e264
jmp 0x2e351
movq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0x51f2
xorl %ebp, %ebp
jmp 0x2e2b0
xorl %ebp, %ebp
testq %r12, %r12
je 0x2e368
movq 0x30(%r12), %rdi
callq 0x3350
movq %r12, %rdi
callq 0x3350
movq %rbp, %rdi
callq 0x3350
movq 0x18(%rsp), %rdi
callq 0x2e970
movq %r14, %rdi
callq 0x27d21
movq %r13, %rdi
callq 0x27d21
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5108
movl $0x1, %ebp
jmp 0x2e38c
callq 0x51d0
jmp 0x2e38c
movq 0x20c31(%rip), %rcx     # 0x4efe8
movq (%rcx), %rdi
leaq 0x1538c(%rip), %rsi     # 0x4374d
movl %eax, %edx
xorl %eax, %eax
callq 0x3750
jmp 0x2e3a2