// File: /workspace/github_c_makefile_star2_O3/nh13[P]ksw/src/main.c
// Function: main
int main(int argc, char *argv[])
{
	main_opt_t * opt = NULL;
	int c;
	ksw_extz_t ez;
	alignment_t *alignment = alignment_init();

	memset(&ez, 0, sizeof(ksw_extz_t));

	opt = main_opt_init();

	// FIXME: for local or glocal we don't the query/target starts unless we output the cigar
	while ((c = getopt(argc, argv, "M:a:b:q:r:w:m:csHROl:h")) >= 0) {
		switch (c) {
			case 'M': opt->alignment_mode = atoi(optarg); break;
			case 'a': opt->match_score = atoi(optarg); break;
			case 'b': opt->mismatch_score = atoi(optarg); break;
			case 'q': opt->gap_open = atoi(optarg); break;
			case 'r': opt->gap_extend = atoi(optarg); break;
			case 'w': opt->band_width = atoi(optarg); break;
			case 'm': opt->matrix_fn = optarg; break; 
			case 'c': opt->add_cigar = 1; break;
			case 's': opt->add_seq = 1; break;
			case 'H': opt->add_header = 1; break;
			case 'R': opt->right_align_gaps = 1; break;
			case 'O': opt->offset_and_length = 1; break;
			case 'l': opt->library = atoi(optarg); break;
			case 'h': usage(opt); return 1;
			default: usage(opt); return 1;
		}
	}
	if (optind > argc) {
		usage(opt);
		return 1;
	}

	// validate args
	main_opt_validate(opt);
	
	// set the library data **after** setting the scoring matrix
	main_opt_init_library(opt);

	// output the header
	if (opt->add_header) {
		// based output format
		if (opt->offset_and_length == 1) fprintf(stdout, "score\tquery_offset\tquery_length\ttarget_offset\ttarget_length");
		else fprintf(stdout, "score\tquery_start\tquery_end\ttarget_start\ttarget_end");
		// append the cigar
		if (opt->add_cigar == 1) fprintf(stdout, "\tcigar");
		// append the query and target sequence
		if (opt->add_seq) fprintf(stdout, "\tquery\ttarget");
		fputc('\n', stdout);
	}

	// read a query and target at a time
	kstream_t *fp     = ks_init(fileno(stdin));
	kstring_t *query  = (kstring_t*)calloc(1, sizeof(kstring_t));
	kstring_t *target = (kstring_t*)calloc(1, sizeof(kstring_t));
	int retval = 0;
	while (ks_getuntil(fp, 0, query, &retval) > 0 && ks_getuntil(fp, 0, target, &retval) > 0) {
		align(query->s, target->s, opt, alignment);
	}
	free(query->s);
	free(query);
	free(target->s);
	free(target);
	ks_destroy(fp);

	// clean up
	alignment_destroy(alignment);
	main_opt_destroy(opt);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movl %edi, %ebp
movl $0x1, %edi
movl $0x28, %esi
callq 0x19e0b0
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x50, %esi
callq 0x19e0b0
movq %rax, %rbx
movaps 0x3c503d(%rip), %xmm0   # 0x5ca4a0
movups %xmm0, (%rax)
movl $0x1fffffff, 0x10(%rax) # imm = 0x1FFFFFFF
leaq 0x3c57df(%rip), %rdx    # 0x5cac53
movl %ebp, %edi
movq %r15, %rsi
callq 0x19e050
testl %eax, %eax
js 0x205577
movq 0x5d2b33(%rip), %r14    # 0x7d7fc0
leaq 0x3c57bf(%rip), %r12    # 0x5cac53
leaq 0x3c4fa5(%rip), %r13    # 0x5ca440
cmpl $0x51, %eax
jle 0x2054c1
leal -0x61(%rax), %ecx
cmpl $0x16, %ecx
ja 0x2054f6
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, (%rbx)
jmp 0x205562
cmpl $0x48, %eax
je 0x2054e0
cmpl $0x4d, %eax
je 0x2054e9
cmpl $0x4f, %eax
jne 0x205732
movl $0x1, 0x34(%rbx)
jmp 0x205562
movl $0x1, 0x2c(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x20(%rbx)
jmp 0x205562
cmpl $0x52, %eax
jne 0x205732
movl $0x1, 0x30(%rbx)
jmp 0x205562
movl $0x1, 0x28(%rbx)
jmp 0x205562
movq (%r14), %rax
movq %rax, 0x18(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x4(%rbx)
jmp 0x205562
movl $0x1, 0x24(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x38(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x10(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x8(%rbx)
jmp 0x205562
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0xc(%rbx)
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x19e050
testl %eax, %eax
jns 0x20549b
movq 0x5d2a62(%rip), %rax    # 0x7d7fe0
cmpl %ebp, (%rax)
jg 0x205732
movq %rbx, %rdi
callq 0x2050bb
movq %rbx, %rdi
callq 0x204a7f
cmpl $0x0, 0x2c(%rbx)
je 0x20561b
cmpl $0x1, 0x34(%rbx)
movq 0x5d2a09(%rip), %r14    # 0x7d7fb0
movq (%r14), %rcx
jne 0x2055ba
leaq 0x3c56b7(%rip), %rdi    # 0x5cac6a
movl $0x3b, %esi
jmp 0x2055c6
leaq 0x3c56e5(%rip), %rdi    # 0x5caca6
movl $0x33, %esi
movl $0x1, %edx
callq 0x19e200
cmpl $0x1, 0x24(%rbx)
jne 0x2055ef
movq (%r14), %rcx
leaq 0x3c56fa(%rip), %rdi    # 0x5cacda
movl $0x6, %esi
movl $0x1, %edx
callq 0x19e200
cmpl $0x0, 0x28(%rbx)
je 0x20560e
movq (%r14), %rcx
leaq 0x3c56e2(%rip), %rdi    # 0x5cace1
movl $0xd, %esi
movl $0x1, %edx
callq 0x19e200
movq (%r14), %rsi
movl $0xa, %edi
callq 0x19e140
movq 0x5d29ae(%rip), %rax    # 0x7d7fd0
movq (%rax), %rdi
callq 0x19e0e0
movl %eax, %ebp
movl $0x1, %edi
movl $0x18, %esi
callq 0x19e0b0
movq %rax, %r15
movl %ebp, 0x14(%rax)
movl $0x4000, %edi           # imm = 0x4000
callq 0x19e150
movq %rax, (%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x19e0b0
movq %rax, %rbp
movl $0x1, %edi
movl $0x18, %esi
callq 0x19e0b0
movq %rax, %r13
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x205741
testl %eax, %eax
jle 0x2056cf
leaq 0xc(%rsp), %r14
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x205741
movq 0x10(%r13), %r12
testl %eax, %eax
jle 0x2056d2
movq 0x10(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
callq 0x20536f
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x205741
testl %eax, %eax
jg 0x205691
jmp 0x2056d2
xorl %r12d, %r12d
movq 0x10(%rbp), %rdi
callq 0x19e120
movq %rbp, %rdi
callq 0x19e120
movq %r12, %rdi
callq 0x19e120
movq %r13, %rdi
callq 0x19e120
movq (%r15), %rdi
callq 0x19e120
movq %r15, %rdi
callq 0x19e120
movq 0x10(%rsp), %r14
movq 0x18(%r14), %rdi
callq 0x19e120
movq %r14, %rdi
callq 0x19e120
movq %rbx, %rdi
callq 0x204fa2
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x19e31c
movl $0x1, %eax
jmp 0x205723