// File: /workspace/github_cpp_makefile_star2_O3/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, 0x2c(%rsp)
movw $0x77, 0x18(%rsp)
leaq 0xbfb9(%rip), %rdx      # 0x25fd0
callq 0x31a0
testl %eax, %eax
js 0x1a14d
movl $0x0, 0x14(%rsp)
leaq 0xbf9d(%rip), %rbp      # 0x25fd0
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
xorl %r14d, %r14d
cmpl $0x64, %eax
jg 0x1a088
cmpl $0x52, %eax
jg 0x1a0b0
cmpl $0x41, %eax
je 0x1a0dd
cmpl $0x43, %eax
jne 0x1a3a3
movq 0x13f66(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x3410
movl %eax, 0x8(%rsp)
jmp 0x1a0fe
cmpl $0x71, %eax
jg 0x1a0bf
cmpl $0x65, %eax
je 0x1a0e7
cmpl $0x6e, %eax
jne 0x1a3a3
movq 0x13f3e(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x3410
movl %eax, 0x10(%rsp)
jmp 0x1a0fe
cmpl $0x53, %eax
je 0x1a0f1
cmpl $0x62, %eax
je 0x1a0d5
jmp 0x1a3a3
cmpl $0x72, %eax
je 0x1a0f9
cmpl $0x75, %eax
jne 0x1a3a3
movl $0x1, 0x20(%rsp)
movl $0x1, %r15d
jmp 0x1a0fe
movl $0x1, 0xc(%rsp)
jmp 0x1a0fe
movl $0x1, 0x14(%rsp)
jmp 0x1a0fe
movl $0x1, %r14d
jmp 0x1a0fe
movl $0x1, %ebx
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x31a0
testl %eax, %eax
jns 0x1a05b
testl %ebx, %ebx
sete 0x7(%rsp)
testl %r14d, %r14d
sete %bpl
jne 0x1a135
leaq 0x2c(%rsp), %rdi
callq 0x32d0
movw $0x62, 0x2c(%rsp,%rax)
leaq 0x18(%rsp), %rdi
testl %r15d, %r15d
je 0x1a18c
callq 0x32d0
movw $0x62, 0x18(%rsp,%rax)
jmp 0x1a198
leaq 0x2c(%rsp), %rdi
callq 0x32d0
movw $0x62, 0x2c(%rsp,%rax)
movb $0x1, 0x7(%rsp)
movl $0x0, 0x14(%rsp)
movw $0x68, %bx
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movb $0x1, %bpl
jmp 0x1a1a3
callq 0x32d0
movw $0x68, 0x18(%rsp,%rax)
movw $0x75, %bx
cmpl $0x0, 0x20(%rsp)
je 0x1a1b2
leaq 0x18(%rsp), %rdi
callq 0x32d0
movw %bx, 0x18(%rsp,%rax)
movq 0x13dff(%rip), %r14     # 0x2dfb8
movslq (%r14), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jge 0x1a390
movq (%r12,%rax,8), %rdi
leaq 0x2c(%rsp), %rsi
xorl %edx, %edx
callq 0x14d06
movl $0x1, %ebx
testq %rax, %rax
je 0x1a37f
movq %rax, %r13
movq 0x10(%rax), %rdx
testb %bpl, %bpl
jne 0x1a204
testq %rdx, %rdx
je 0x1a39c
cmpl $0x0, (%rdx)
je 0x1a39c
leaq 0x9c03(%rip), %rdi      # 0x23e0e
leaq 0x18(%rsp), %rsi
callq 0x14d06
movq %rax, %rbp
movslq (%r14), %rax
movq 0x8(%r12,%rax,8), %rdi
callq 0x1b819
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x15122
testl %eax, %eax
js 0x1a342
movq %r13, %r15
addq $0x10, %r15
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
xorl %r12d, %r12d
movl (%rbx), %eax
testl %eax, %eax
js 0x1a30f
cmpl %eax, %r14d
je 0x1a2a2
movq %r12, %rdi
callq 0x32c0
movq (%r15), %rax
movq 0x8(%rax), %rax
movslq (%rbx), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rdx
callq 0x1b912
movl (%rbx), %r14d
testq %rax, %rax
je 0x1a32f
movq %rax, %r12
cmpb $0x0, 0x7(%rsp)
jne 0x1a2b8
movq %rbx, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
callq 0x19a8d
cmpl $0xb, 0x8(%rsp)
jl 0x1a2f2
movq %rbx, %rdi
movq %r12, %rsi
movl 0x8(%rsp), %edx
callq 0x197a0
movq 0x8(%rbx), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl %eax, %edx
jle 0x1a2f2
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%rbx)
testq %r12, %r12
je 0x1a30c
movq %rbx, %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %ecx
callq 0x190a0
jmp 0x1a30f
xorl %r12d, %r12d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x15157
movq %r13, %rdi
movq %rbx, %rsi
callq 0x15122
testl %eax, %eax
jns 0x1a25f
jmp 0x1a34a
movq %r15, %rdi
movl %r14d, %esi
callq 0x4a8a
xorl %r12d, %r12d
jmp 0x1a2a2
xorl %r12d, %r12d
testq %rbx, %rbx
je 0x1a35b
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x32c0
movq 0x20(%rsp), %rdi
callq 0x1b2fd
movq %r13, %rdi
callq 0x150dc
movq %rbp, %rdi
callq 0x150dc
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x49a0
movl $0x1, %ebx
jmp 0x1a37f
callq 0x4a68
jmp 0x1a37f
movq 0x13c3e(%rip), %rcx     # 0x2dfe8
movq (%rcx), %rdi
leaq 0xbc27(%rip), %rsi      # 0x25fdb
movl %eax, %edx
xorl %eax, %eax
callq 0x3630
jmp 0x1a395
nop