// File: /workspace/github_cpp_makefile_star2_O3/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, 0x1a(%rsp)
leaq 0x19477(%rip), %rdx     # 0x23d9c
callq 0x31a0
testl %eax, %eax
js 0xab68
movl $0x1, %ebp
movq $0x0, 0x70(%rsp)
leaq 0x193d1(%rip), %r15     # 0x23d18
leaq 0x1944e(%rip), %rbx     # 0x23d9c
leaq 0x193eb(%rip), %r14     # 0x23d40
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x0, 0x28(%rsp)
cmpl $0x57, %eax
jle 0xa9c7
leal -0x62(%rax), %ecx
cmpl $0x16, %ecx
ja 0xaa48
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl $0x1, 0x60(%rsp)
jmp 0xab15
leal -0x3f(%rax), %ecx
cmpl $0x9, %ecx
ja 0xa9e5
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movl $0x1, 0x30(%rsp)
jmp 0xab15
cmpl $0x53, %eax
je 0xab13
cmpl $0x54, %eax
jne 0xaff4
movq 0x235e2(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x48(%rsp)
jmp 0xab13
movq 0x235c9(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3400
movl %eax, 0x246db(%rip)     # 0x2f104
jmp 0xab15
movl $0x1, 0x14(%rsp)
jmp 0xab15
movl $0x1, 0x28(%rsp)
jmp 0xab15
cmpl $0x58, %eax
jne 0xaff4
movl $0x2, 0x20(%rsp)
jmp 0xab15
movl $0x1, 0x20(%rsp)
jmp 0xab15
movl $0x1, 0x5c(%rsp)
jmp 0xab15
movq 0x23561(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x40(%rsp)
jmp 0xab15
movq 0x23548(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x24669(%rip)     # 0x2f110
jmp 0xab15
movq 0x23530(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x3410
movl %eax, 0x2464a(%rip)     # 0x2f108
jmp 0xab15
movl $0x1, %eax
movq %rax, 0x70(%rsp)
jmp 0xab15
movq 0x2350d(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3400
movl %eax, 0x2461b(%rip)     # 0x2f100
jmp 0xab15
movq 0x234f2(%rip), %rax     # 0x2dfe0
movq (%rax), %rdi
callq 0x34b0
movq %rax, 0x2461b(%rip)     # 0x2f118
jmp 0xab15
movq 0x234da(%rip), %rax     # 0x2dfe0
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 0xa9a0
cmpl $0x0, 0x5c(%rsp)
sete %r15b
cmpl $0x0, 0x28(%rsp)
sete %r14b
cmpl $0x0, 0x60(%rsp)
sete %al
testl %ebp, %ebp
sete %cl
movq %rcx, 0x28(%rsp)
cmpl $0x0, 0x70(%rsp)
sete %bpl
cmpl $0x0, 0x14(%rsp)
sete 0x14(%rsp)
movl 0x20(%rsp), %ecx
jmp 0xaba6
movq $0x0, 0x28(%rsp)
movb $0x1, %r14b
movb $0x1, %r15b
movb $0x1, %al
movb $0x1, 0x14(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movb $0x1, %bpl
andb %r15b, %al
movw $0x62, %bx
cmpb $0x1, %al
jne 0xabc5
cmpl $0x1, %ecx
je 0xabc1
cmpl $0x2, %ecx
jne 0xabd4
movw $0x58, %bx
jmp 0xabc5
movw $0x78, %bx
leaq 0x1a(%rsp), %rdi
callq 0x32d0
movw %bx, 0x1a(%rsp,%rax)
cmpb $0x0, 0x28(%rsp)
jne 0xabec
leaq 0x3a(%rsp), %rdi
callq 0x32d0
movw $0x62, 0x3a(%rsp,%rax)
testb %bpl, %r14b
movq 0x40(%rsp), %rbx
jne 0xac07
leaq 0x1a(%rsp), %rdi
callq 0x32d0
movw $0x68, 0x1a(%rsp,%rax)
testb %r15b, %r15b
movq 0x50(%rsp), %rbp
jne 0xac22
leaq 0x1a(%rsp), %rdi
callq 0x32d0
movw $0x75, 0x1a(%rsp,%rax)
movq 0x2338f(%rip), %r15     # 0x2dfb8
movl (%r15), %eax
cmpl %r13d, %eax
je 0xaff4
testq %rbp, %rbp
sete %cl
cmpq $0x0, 0x48(%rsp)
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xac5b
movq 0x48(%rsp), %rdi
callq 0x1525b
movq %rax, %rbp
movl (%r15), %eax
cltq
movq (%r12,%rax,8), %rdi
leaq 0x3a(%rsp), %rsi
movq %rbp, 0x50(%rsp)
movq %rbp, %rdx
callq 0x14d06
movq %rax, %rbp
testq %rax, %rax
je 0xb005
movq 0x10(%rbp), %rdx
testq %rdx, %rdx
je 0xb00c
testq %rbx, %rbx
leaq 0x19178(%rip), %rdi     # 0x23e0e
cmovneq %rbx, %rdi
leaq 0x1a(%rsp), %rsi
callq 0x14d06
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xb013
testb %r14b, %r14b
je 0xaf99
movslq (%r15), %rax
leal 0x1(%rax), %ecx
cmpl %r13d, %ecx
jne 0xaf00
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r12
movq %rbp, %rdi
movq %rax, %rsi
callq 0x15122
testl %eax, %eax
js 0xaed7
leaq 0x2442a(%rip), %r14     # 0x2f120
movl $0x7f, %r13d
movb 0x14(%rsp), %r15b
movq 0x8(%r12), %rax
movl %eax, %ecx
shrl $0x10, %ecx
movzbl %cl, %ecx
cmpl 0x243f4(%rip), %ecx     # 0x2f108
jl 0xaebf
shrq $0x20, %rax
movzwl %ax, %eax
movl %eax, %ecx
notl %ecx
testl %ecx, 0x243d5(%rip)     # 0x2f100
jne 0xaebf
testl %eax, 0x243cd(%rip)     # 0x2f104
jne 0xaebf
movq 0x10(%rbp), %rbx
cmpq $0x0, 0x243cf(%rip)     # 0x2f118
je 0xad75
movq %r12, %rdi
leaq 0x191dc(%rip), %rsi     # 0x23f31
callq 0x1175c
testq %rax, %rax
je 0xad75
movq 0x243b2(%rip), %rdi     # 0x2f118
incq %rax
movq %rax, %rsi
callq 0x34e0
testl %eax, %eax
je 0xadaa
cmpq $0x0, 0x24393(%rip)     # 0x2f110
je 0xadaa
movq %rbx, %rdi
movq %r12, %rsi
callq 0x134e4
testq %rax, %rax
je 0xaebf
movq 0x24376(%rip), %rsi     # 0x2f110
movq %rax, %rdi
callq 0x34e0
testl %eax, %eax
jne 0xaebf
testb %r15b, %r15b
jne 0xaeb2
movq 0x8(%r12), %rcx
movq %rcx, %rbx
shrq $0x30, %rbx
shlq $0x2, %rbx
movl 0x10(%r12), %edx
movl %edx, %eax
addq 0x30(%r12), %rbx
cmpl $0x0, 0x243c2(%rip)     # 0x2f198
jne 0xae72
movq %rdx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movl $0xffffffe1, %r13d      # imm = 0xFFFFFFE1
xorl %r15d, %r15d
movsd 0x186e1(%rip), %xmm2    # 0x234d8
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
divsd %xmm2, %xmm1
movapd %xmm2, %xmm0
callq 0x3650
addsd 0x186bc(%rip), %xmm0    # 0x234d0
callq 0x32a0
movsd 0x186b7(%rip), %xmm2    # 0x234d8
mulsd %xmm2, %xmm0
divsd 0x18ee3(%rip), %xmm0    # 0x23d10
addsd 0x186cb(%rip), %xmm0    # 0x23500
cvttsd2si %xmm0, %eax
cmpl $0x5d, %eax
movl $0x5d, %ecx
cmovgel %ecx, %eax
movl %eax, (%r15,%r14)
addq $0x4, %r15
incl %r13d
cmpq $0x200, %r15            # imm = 0x200
jne 0xadf7
movl 0x10(%r12), %eax
movb 0x14(%rsp), %r15b
movl $0x7f, %r13d
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rdx
testl %eax, %eax
jle 0xaeb2
shrl $0x18, %ecx
movl %edx, %eax
incl %eax
shrl $0x1f, %eax
addl %edx, %eax
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rax, %rbx
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
cmpb $0x7f, %cl
cmovael %r13d, %ecx
movzbl %cl, %ecx
movb (%r14,%rcx,4), %cl
movb %cl, (%rbx,%rax)
incq %rax
movslq 0x10(%r12), %rcx
cmpq %rcx, %rax
jl 0xae90
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x15157
movq %rbp, %rdi
movq %r12, %rsi
callq 0x15122
testl %eax, %eax
jns 0xad01
movq 0x40(%rsp), %rbx
cmpl $-0x1, %eax
jne 0xb039
testq %r12, %r12
je 0xaf99
movq 0x30(%r12), %rdi
callq 0x32c0
movq %r12, %rdi
callq 0x32c0
jmp 0xaf99
cmpb $0x0, 0x28(%rsp)
jne 0xb029
movq (%r12,%rax,8), %rdi
callq 0x16892
testq %rax, %rax
je 0xb029
movq %rax, %r14
movslq (%r15), %rbx
leal 0x1(%rbx), %eax
cmpl %r13d, %eax
jge 0xaf8c
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 0x11f47
movl 0x6c(%rsp), %edx
testl %edx, %edx
js 0xaf85
movq 0x8(%rbp), %rdi
movl 0x68(%rsp), %ecx
movl 0x64(%rsp), %r8d
leaq 0xd9(%rip), %rax        # 0xb043
movq %rax, (%rsp)
movq %r14, %rsi
movq 0x20(%rsp), %r9
callq 0x1717f
incq %rbx
cmpl %ebx, %r13d
jne 0xaf33
jmp 0xaf8c
callq 0x392f
jmp 0xaf7b
movq %r14, %rdi
callq 0x16092
movq 0x40(%rsp), %rbx
xorl %r14d, %r14d
movq 0x50(%rsp), %rdi
callq 0x32c0
movq 0x48(%rsp), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq 0x24151(%rip), %rdi     # 0x2f110
callq 0x32c0
movq 0x2414d(%rip), %rdi     # 0x2f118
callq 0x32c0
movq %rbp, %rdi
callq 0x150dc
movq 0x20(%rsp), %rdi
callq 0x150dc
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x30(%rsp), %edi
callq 0x375a
movl $0x1, %r14d
jmp 0xafe2
callq 0x39d9
jmp 0xb018
callq 0x39b7
jmp 0xb018
callq 0x3995
xorl %r14d, %r14d
movq $0x0, 0x20(%rsp)
jmp 0xaf9c
callq 0x3951
movl $0x1, %r14d
jmp 0xaf9c
callq 0x3973
jmp 0xaee0