// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/sam_view.c
// Function: main_samview
int main_samview(int argc, char *argv[])
{
	int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0, slx2sngr = 0;
	int of_type = BAM_OFDEC, is_long_help = 0;
	samfile_t *in = 0, *out = 0;
	char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;

	/* parse command-line options */
	strcpy(in_mode, "r"); strcpy(out_mode, "w");
	while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:ul:r:xX?T:C")) >= 0) {
		switch (c) {
		case 'C': slx2sngr = 1; break;
		case 'S': is_bamin = 0; break;
		case 'b': is_bamout = 1; break;
		case 't': fn_list = strdup(optarg); is_bamin = 0; break;
		case 'h': is_header = 1; break;
		case 'H': is_header_only = 1; break;
		case 'o': fn_out = strdup(optarg); break;
		case 'f': g_flag_on = strtol(optarg, 0, 0); break;
		case 'F': g_flag_off = strtol(optarg, 0, 0); break;
		case 'q': g_min_mapQ = atoi(optarg); break;
		case 'u': is_uncompressed = 1; break;
		case 'l': g_library = strdup(optarg); break;
		case 'r': g_rg = strdup(optarg); break;
		case 'x': of_type = BAM_OFHEX; break;
		case 'X': of_type = BAM_OFSTR; break;
		case '?': is_long_help = 1; break;
		case 'T': fn_ref = strdup(optarg); is_bamin = 0; break;
		default: return usage(is_long_help);
		}
	}
	if (is_uncompressed) is_bamout = 1;
	if (is_header_only) is_header = 1;
	if (is_bamout) strcat(out_mode, "b");
	else {
		if (of_type == BAM_OFHEX) strcat(out_mode, "x");
		else if (of_type == BAM_OFSTR) strcat(out_mode, "X");
	}
	if (is_bamin) strcat(in_mode, "b");
	if (is_header) strcat(out_mode, "h");
	if (is_uncompressed) strcat(out_mode, "u");
	if (argc == optind) return usage(is_long_help);

	// generate the fn_list if necessary
	if (fn_list == 0 && fn_ref) fn_list = samfaipath(fn_ref);
	// open file handlers
	if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
		fprintf(stderr, "[main_samview] fail to open file for reading.\n");
		goto view_end;
	}
	if (in->header == 0) {
		fprintf(stderr, "[main_samview] fail to read the header.\n");
		goto view_end;
	}
	if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) {
		fprintf(stderr, "[main_samview] fail to open file for writing.\n");
		goto view_end;
	}
	if (is_header_only) goto view_end; // no need to print alignments

	if (argc == optind + 1) { // convert/print the entire file
		bam1_t *b = bam_init1();
		int r;
		while ((r = samread(in, b)) >= 0) { // read one alignment from `in'
			if (!__g_skip_aln(in->header, b)) {
				if (slx2sngr) sol2sanger(b);
				samwrite(out, b); // write the alignment to `out'
			}
		}
		if (r < -1) fprintf(stderr, "[main_samview] truncated file.\n");
		bam_destroy1(b);
	} else { // retrieve alignments in specified regions
		int i;
		bam_index_t *idx = 0;
		if (is_bamin) idx = bam_index_load(argv[optind]); // load BAM index
		if (idx == 0) { // index is unavailable
			fprintf(stderr, "[main_samview] random alignment retrieval only works for indexed BAM files.\n");
			ret = 1;
			goto view_end;
		}
		for (i = optind + 1; i < argc; ++i) {
			int tid, beg, end;
			bam_parse_region(in->header, argv[i], &tid, &beg, &end); // parse a region in the format like `chr2:100-200'
			if (tid < 0) { // reference name is not found
				fprintf(stderr, "[main_samview] fail to get the reference name. Continue anyway.\n");
				continue;
			}
			bam_fetch(in->x.bam, idx, tid, beg, end, out, view_func); // fetch alignments
		}
		bam_index_destroy(idx); // destroy the BAM index
	}

view_end:
	// close files, free and return
	free(fn_list); free(fn_ref); free(fn_out); free(g_library); free(g_rg);
	samclose(in);
	samclose(out);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movl %edi, %r13d
movw $0x72, 0x3a(%rsp)
movw $0x77, 0x16(%rsp)
leaq 0x1a7d3(%rip), %rdx     # 0x24d44
callq 0x31a0
testl %eax, %eax
js 0xa7b3
movl $0x1, %ebp
movq $0x0, 0x70(%rsp)
leaq 0x1a72d(%rip), %r15     # 0x24cc0
leaq 0x1a7aa(%rip), %rbx     # 0x24d44
leaq 0x1a747(%rip), %r14     # 0x24ce8
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x0, 0x28(%rsp)
cmpl $0x57, %eax
jle 0xa613
leal -0x62(%rax), %ecx
cmpl $0x16, %ecx
ja 0xa694
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl $0x1, 0x60(%rsp)
jmp 0xa761
leal -0x3f(%rax), %ecx
cmpl $0x9, %ecx
ja 0xa631
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl $0x1, 0x30(%rsp)
jmp 0xa761
cmpl $0x53, %eax
je 0xa75f
cmpl $0x54, %eax
jne 0xabf2
movq 0x24996(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x48(%rsp)
jmp 0xa75f
movq 0x2497d(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3400
movl %eax, 0x25a8f(%rip)     # 0x30104
jmp 0xa761
movl $0x1, 0x1c(%rsp)
jmp 0xa761
movl $0x1, 0x28(%rsp)
jmp 0xa761
cmpl $0x58, %eax
jne 0xabf2
movl $0x2, 0x20(%rsp)
jmp 0xa761
movl $0x1, 0x20(%rsp)
jmp 0xa761
movl $0x1, 0x5c(%rsp)
jmp 0xa761
movq 0x24915(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x40(%rsp)
jmp 0xa761
movq 0x248fc(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x25a1d(%rip)     # 0x30110
jmp 0xa761
movq 0x248e4(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x3410
movl %eax, 0x259fe(%rip)     # 0x30108
jmp 0xa761
movl $0x1, %eax
movq %rax, 0x70(%rsp)
jmp 0xa761
movq 0x248c1(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3400
movl %eax, 0x259cf(%rip)     # 0x30100
jmp 0xa761
movq 0x248a6(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x259cf(%rip)     # 0x30118
jmp 0xa761
movq 0x2488e(%rip), %rax     # 0x2efe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
movl %r13d, %edi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x31a0
testl %eax, %eax
jns 0xa5ec
cmpl $0x0, 0x5c(%rsp)
sete %r14b
cmpl $0x0, 0x28(%rsp)
sete %r15b
cmpl $0x0, 0x60(%rsp)
sete %al
testl %ebp, %ebp
sete %cl
movq %rcx, 0x28(%rsp)
cmpl $0x0, 0x70(%rsp)
sete %bl
cmpl $0x0, 0x1c(%rsp)
sete 0x1c(%rsp)
movl 0x20(%rsp), %ecx
jmp 0xa7f0
movq $0x0, 0x28(%rsp)
movb $0x1, %r15b
movb $0x1, %r14b
movb $0x1, %al
movb $0x1, 0x1c(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movb $0x1, %bl
andb %r14b, %al
movw $0x62, %bp
cmpb $0x1, %al
jne 0xa80f
cmpl $0x1, %ecx
je 0xa80b
cmpl $0x2, %ecx
jne 0xa81e
movw $0x58, %bp
jmp 0xa80f
movw $0x78, %bp
leaq 0x16(%rsp), %rdi
callq 0x32d0
movw %bp, 0x16(%rsp,%rax)
cmpb $0x0, 0x28(%rsp)
jne 0xa836
leaq 0x3a(%rsp), %rdi
callq 0x32d0
movw $0x62, 0x3a(%rsp,%rax)
testb %bl, %r15b
movq 0x50(%rsp), %rbp
jne 0xa851
leaq 0x16(%rsp), %rdi
callq 0x32d0
movw $0x68, 0x16(%rsp,%rax)
testb %r14b, %r14b
jne 0xa867
leaq 0x16(%rsp), %rdi
callq 0x32d0
movw $0x75, 0x16(%rsp,%rax)
movq 0x2474a(%rip), %rbx     # 0x2efb8
cmpl %r13d, (%rbx)
movq 0x40(%rsp), %r14
je 0xabf2
testq %rbp, %rbp
sete %al
cmpq $0x0, 0x48(%rsp)
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa89f
movq 0x48(%rsp), %rdi
callq 0x14d97
movq %rax, %rbp
movslq (%rbx), %rax
movq (%r12,%rax,8), %rdi
leaq 0x3a(%rsp), %rsi
movq %rbp, 0x50(%rsp)
movq %rbp, %rdx
callq 0x1485f
movq %rax, %rbp
testq %rax, %rax
je 0xac02
movq 0x10(%rbp), %rdx
testq %rdx, %rdx
je 0xac09
testq %r14, %r14
leaq 0x1a4db(%rip), %rdi     # 0x24db6
cmovneq %r14, %rdi
leaq 0x16(%rsp), %rsi
callq 0x1485f
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xac10
testb %r15b, %r15b
je 0xab42
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jne 0xab49
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r12
movq %rbp, %rdi
movq %rax, %rsi
callq 0x14c5e
testl %eax, %eax
js 0xab22
leaq 0x257e5(%rip), %rbx     # 0x30120
leaq 0x1a597(%rip), %r13     # 0x24ed9
movq 0x8(%r12), %rax
movl %eax, %ecx
shrl $0x10, %ecx
movzbl %cl, %ecx
cmpl 0x257b3(%rip), %ecx     # 0x30108
jl 0xab0f
shrq $0x20, %rax
movzwl %ax, %eax
movl %eax, %ecx
notl %ecx
testl %ecx, 0x25794(%rip)     # 0x30100
jne 0xab0f
testl %eax, 0x2578c(%rip)     # 0x30104
jne 0xab0f
movq 0x10(%rbp), %r15
cmpq $0x0, 0x2578e(%rip)     # 0x30118
je 0xa9bc
movq %r12, %rdi
movq %r13, %rsi
callq 0x1110a
testq %rax, %rax
je 0xa9b2
movq 0x25775(%rip), %rdi     # 0x30118
incq %rax
movq %rax, %rsi
callq 0x34e0
testl %eax, %eax
je 0xa9b6
xorl %eax, %eax
jmp 0xa9b8
movb $0x1, %al
testb %al, %al
jne 0xa9f1
cmpq $0x0, 0x2574c(%rip)     # 0x30110
je 0xa9f1
movq %r15, %rdi
movq %r12, %rsi
callq 0x12e37
testq %rax, %rax
je 0xab0f
movq 0x2572f(%rip), %rsi     # 0x30110
movq %rax, %rdi
callq 0x34e0
testl %eax, %eax
jne 0xab0f
cmpb $0x0, 0x1c(%rsp)
jne 0xab02
movq 0x8(%r12), %r15
movq %r15, 0x28(%rsp)
shrq $0x30, %r15
shlq $0x2, %r15
movl 0x10(%r12), %eax
movq %rax, 0x30(%rsp)
addq 0x30(%r12), %r15
cmpl $0x0, 0x25774(%rip)     # 0x30198
movsd 0x19a4c(%rip), %xmm2    # 0x24478
movq %rbx, %r13
jne 0xaaa0
movl $0xffffffe1, %ebx       # imm = 0xFFFFFFE1
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
divsd %xmm2, %xmm1
movapd %xmm2, %xmm0
callq 0x3650
addsd 0x19a1b(%rip), %xmm0    # 0x24470
callq 0x32a0
movl $0x5d, %ecx
movsd 0x1a251(%rip), %xmm1    # 0x24cb8
movsd 0x19a09(%rip), %xmm2    # 0x24478
mulsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd 0x19a21(%rip), %xmm1    # 0x244a0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cmpl $0x5d, %eax
cmovgel %ecx, %eax
movl %eax, (%r14,%r13)
addq $0x4, %r14
incl %ebx
cmpq $0x200, %r14            # imm = 0x200
jne 0xaa39
cmpl $0x0, 0x10(%r12)
movq 0x40(%rsp), %r14
movq %r13, %rbx
movl $0x7f, %edx
leaq 0x1a41f(%rip), %r13     # 0x24ed9
jle 0xab02
movq 0x28(%rsp), %rcx
shrl $0x18, %ecx
movq 0x30(%rsp), %rsi
movl %esi, %eax
incl %eax
shrl $0x1f, %eax
addl %esi, %eax
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rax, %r15
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
cmpb $0x7f, %cl
cmovael %edx, %ecx
movzbl %cl, %ecx
movb (%rbx,%rcx,4), %cl
movb %cl, (%r15,%rax)
incq %rax
movslq 0x10(%r12), %rcx
cmpq %rcx, %rax
jl 0xaae0
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x14c93
movq %rbp, %rdi
movq %r12, %rsi
callq 0x14c5e
testl %eax, %eax
jns 0xa942
cmpl $-0x1, %eax
jne 0xac77
testq %r12, %r12
je 0xab42
movq 0x30(%r12), %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x32c0
xorl %ebx, %ebx
jmp 0xac20
cmpb $0x0, 0x28(%rsp)
je 0xab55
xorl %r14d, %r14d
jmp 0xab61
movq (%r12,%rax,8), %rdi
callq 0x163cd
movq %rax, %r14
testq %r14, %r14
je 0xac81
movslq (%rbx), %rbx
leal 0x1(%rbx), %eax
cmpl %r13d, %eax
jge 0xabd6
decl %r13d
leaq 0x64(%rsp), %r15
movq 0x10(%rbp), %rdi
movq 0x8(%r12,%rbx,8), %rsi
leaq 0x6c(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq %r15, %r8
callq 0x11668
movl 0x6c(%rsp), %edx
testl %edx, %edx
js 0xabcf
movq 0x8(%rbp), %rdi
movl 0x68(%rsp), %ecx
movl 0x64(%rsp), %r8d
leaq 0xe1(%rip), %rax        # 0xac95
movq %rax, (%rsp)
movq %r14, %rsi
movq 0x20(%rsp), %r9
callq 0x169e0
incq %rbx
cmpl %ebx, %r13d
jne 0xab7d
jmp 0xabd6
callq 0x392f
jmp 0xabc5
movq %r14, %rdi
callq 0x15bce
xorl %ebx, %ebx
xorl %eax, %eax
cmpl $0x5, %eax
movq 0x40(%rsp), %r14
je 0xac20
testl %eax, %eax
je 0xac20
jmp 0xac66
movl 0x30(%rsp), %edi
callq 0x375a
movl $0x1, %ebx
jmp 0xac66
callq 0x39d9
jmp 0xac15
callq 0x39b7
jmp 0xac15
callq 0x3995
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq 0x50(%rsp), %rdi
callq 0x32c0
movq 0x48(%rsp), %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x32c0
movq 0x254cd(%rip), %rdi     # 0x30110
callq 0x32c0
movq 0x254c9(%rip), %rdi     # 0x30118
callq 0x32c0
movq %rbp, %rdi
callq 0x14c18
movq 0x20(%rsp), %rdi
callq 0x14c18
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3973
jmp 0xab2b
callq 0x3951
movl $0x5, %eax
movl $0x1, %ebx
jmp 0xabe2