// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_md.c
// Function: bam_fillmd
int bam_fillmd(int argc, char *argv[])
{
	int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, apply_baq;
	samfile_t *fp, *fpout = 0;
	faidx_t *fai;
	char *ref = 0, mode_w[8], mode_r[8];
	bam1_t *b;

	is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = apply_baq = capQ = 0;
	mode_w[0] = mode_r[0] = 0;
	strcpy(mode_r, "r"); strcpy(mode_w, "w");
	while ((c = getopt(argc, argv, "reubSC:n:A")) >= 0) {
		switch (c) {
		case 'r': is_realn = 1; break;
		case 'e': is_equal = 1; 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': apply_baq = 1; 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] [-C <INT>] <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       read-independent local realignment\n");
		fprintf(stderr, "         -C INT   coefficient for adjusting mapQ of poor mappings [0]\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, apply_baq);
			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, is_equal, 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, 0x20(%rsp)
movw $0x77, 0x18(%rsp)
leaq 0xd533(%rip), %rdx      # 0x26ed0
callq 0x31a0
testl %eax, %eax
js 0x19ac2
movl $0x0, 0xc(%rsp)
leaq 0xd517(%rip), %rbp      # 0x26ed0
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
xorl %r15d, %r15d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
cmpl $0x64, %eax
jg 0x19a0c
cmpl $0x52, %eax
jg 0x19a34
cmpl $0x41, %eax
je 0x19a5e
cmpl $0x43, %eax
jne 0x19d24
movq 0x155e1(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x3410
movl %eax, %r15d
jmp 0x19a83
cmpl $0x71, %eax
jg 0x19a43
cmpl $0x65, %eax
je 0x19a68
cmpl $0x6e, %eax
jne 0x19d24
movq 0x155ba(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x3410
movl %eax, 0x8(%rsp)
jmp 0x19a83
cmpl $0x53, %eax
je 0x19a72
cmpl $0x62, %eax
je 0x19a57
jmp 0x19d24
cmpl $0x72, %eax
je 0x19a7c
cmpl $0x75, %eax
jne 0x19d24
movl $0x1, %r14d
movl $0x1, %ebx
jmp 0x19a83
movl $0x1, 0x4(%rsp)
jmp 0x19a83
movl $0x1, 0xc(%rsp)
jmp 0x19a83
movl $0x1, 0x10(%rsp)
jmp 0x19a83
movl $0x1, (%rsp)
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x31a0
testl %eax, %eax
jns 0x199e0
cmpl $0x0, 0x10(%rsp)
sete %bpl
testl %r14d, %r14d
sete %r14b
cmpl $0x0, (%rsp)
sete (%rsp)
testl %ebx, %ebx
movl $0x68, %eax
movl $0x62, %ebx
cmovel %eax, %ebx
jmp 0x19aeb
movw $0x68, %bx
movl $0x0, 0x8(%rsp)
movb $0x1, %bpl
movb $0x1, %r14b
movb $0x1, (%rsp)
xorl %r15d, %r15d
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
testb %bpl, %bpl
je 0x19b01
leaq 0x20(%rsp), %rdi
callq 0x32d0
movw $0x62, 0x20(%rsp,%rax)
leaq 0x18(%rsp), %rdi
callq 0x32d0
movw %bx, 0x18(%rsp,%rax)
testb %r14b, %r14b
jne 0x19b26
leaq 0x18(%rsp), %rdi
callq 0x32d0
movw $0x75, 0x18(%rsp,%rax)
movq 0x1548b(%rip), %r14     # 0x2efb8
movslq (%r14), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jge 0x19d11
movq (%r12,%rax,8), %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x1485f
movl $0x1, %ebx
testq %rax, %rax
je 0x19d00
movq %rax, %r13
testb %bpl, %bpl
jne 0x19b78
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x19d1d
cmpl $0x0, (%rax)
je 0x19d1d
movq 0x10(%r13), %rdx
leaq 0xb233(%rip), %rdi      # 0x24db6
leaq 0x18(%rsp), %rsi
callq 0x1485f
movq %rax, %rbp
movslq (%r14), %rax
movq 0x8(%r12,%rax,8), %rdi
callq 0x1af55
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x14c5e
testl %eax, %eax
js 0x19cc3
movq %r13, %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
xorl %r12d, %r12d
movl (%rbx), %eax
testl %eax, %eax
js 0x19c8e
cmpl %eax, %r14d
je 0x19c24
movq %r12, %rdi
callq 0x32c0
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movslq (%rbx), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x10(%rsp), %rdi
leaq 0x34(%rsp), %rdx
callq 0x1b053
movl (%rbx), %r14d
testq %rax, %rax
je 0x19cae
movq %rax, %r12
cmpb $0x0, (%rsp)
jne 0x19c39
movq %rbx, %rdi
movq %r12, %rsi
movl 0x4(%rsp), %edx
callq 0x193e4
cmpl $0xb, %r15d
jl 0x19c71
movq %rbx, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x190aa
movq 0x8(%rbx), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl %eax, %edx
jle 0x19c71
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%rbx)
testq %r12, %r12
je 0x19c8b
movq %rbx, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
callq 0x1894c
jmp 0x19c8e
xorl %r12d, %r12d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x14c93
movq %r13, %rdi
movq %rbx, %rsi
callq 0x14c5e
testl %eax, %eax
jns 0x19bdc
jmp 0x19cc6
movq 0x28(%rsp), %rdi
movl %r14d, %esi
callq 0x47de
xorl %r12d, %r12d
jmp 0x19c24
xorl %r12d, %r12d
testq %rbx, %rbx
je 0x19cdc
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x32c0
movq 0x10(%rsp), %rdi
callq 0x1aa39
movq %r13, %rdi
callq 0x14c18
movq %rbp, %rdi
callq 0x14c18
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x46f4
movl $0x1, %ebx
jmp 0x19d00
callq 0x47bc
jmp 0x19d00
movq 0x152bd(%rip), %rcx     # 0x2efe8
movq (%rcx), %rdi
leaq 0xd1a6(%rip), %rsi      # 0x26edb
movl %eax, %edx
xorl %eax, %eax
callq 0x3630
jmp 0x19d16