// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movl $0x1, %edi
movl $0x50, %esi
callq 0x19e0b0
movq %rax, %rbx
movaps 0x3911f5(%rip), %xmm0   # 0x596480
movups %xmm0, (%rax)
movl $0x1fffffff, 0x10(%rax) # imm = 0x1FFFFFFF
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movq $0x0, 0x38(%rax)
leaq 0x3919a4(%rip), %rdx    # 0x596c53
movl %ebp, %edi
movq %r15, %rsi
callq 0x19e050
testl %eax, %eax
js 0x2053cc
movq %rbx, %rcx
addq $0x18, %rcx
movq %rcx, 0x10(%rsp)
movq 0x59ccec(%rip), %r13    # 0x7a1fc0
leaq 0x391978(%rip), %r12    # 0x596c53
leaq 0x39113e(%rip), %r14    # 0x596420
cmpl $0x51, %eax
jle 0x205308
leal -0x61(%rax), %ecx
cmpl $0x16, %ecx
ja 0x205341
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, (%rbx)
jmp 0x2053b7
cmpl $0x48, %eax
je 0x205327
cmpl $0x4d, %eax
je 0x205333
cmpl $0x4f, %eax
jne 0x205580
movl $0x1, 0x34(%rbx)
jmp 0x2053b7
movl $0x1, 0x2c(%rbx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0x20(%rbx)
jmp 0x2053b7
cmpl $0x52, %eax
jne 0x205580
movl $0x1, 0x30(%rbx)
jmp 0x2053b7
movl $0x1, 0x28(%rbx)
jmp 0x2053b7
movq (%r13), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0x4(%rbx)
jmp 0x2053b7
movl $0x1, 0x24(%rbx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0x38(%rbx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0x10(%rbx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0x8(%rbx)
jmp 0x2053b7
movq (%r13), %rdi
callq 0x19e1a0
movl %eax, 0xc(%rbx)
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x19e050
testl %eax, %eax
jns 0x2052e2
movq 0x59cc0d(%rip), %rax    # 0x7a1fe0
cmpl %ebp, (%rax)
jg 0x205580
movq %rbx, %rdi
callq 0x204ee4
movq %rbx, %rdi
callq 0x20489f
cmpl $0x0, 0x2c(%rbx)
je 0x205470
cmpl $0x1, 0x34(%rbx)
movq 0x59cbb4(%rip), %r14    # 0x7a1fb0
movq (%r14), %rcx
jne 0x20540f
leaq 0x391862(%rip), %rdi    # 0x596c6a
movl $0x3b, %esi
jmp 0x20541b
leaq 0x391890(%rip), %rdi    # 0x596ca6
movl $0x33, %esi
movl $0x1, %edx
callq 0x19e200
cmpl $0x1, 0x24(%rbx)
jne 0x205444
movq (%r14), %rcx
leaq 0x3918a5(%rip), %rdi    # 0x596cda
movl $0x6, %esi
movl $0x1, %edx
callq 0x19e200
cmpl $0x0, 0x28(%rbx)
je 0x205463
movq (%r14), %rcx
leaq 0x39188d(%rip), %rdi    # 0x596ce1
movl $0xd, %esi
movl $0x1, %edx
callq 0x19e200
movq (%r14), %rsi
movl $0xa, %edi
callq 0x19e140
movq 0x59cb59(%rip), %rax    # 0x7a1fd0
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, %r13
movl $0x1, %edi
movl $0x18, %esi
callq 0x19e0b0
movq %rax, %r12
leaq 0x4(%rsp), %rdx
movl $0x0, (%rdx)
movq %r15, %rdi
movq %r13, %rsi
callq 0x20558f
testl %eax, %eax
movq 0x8(%rsp), %r14
jle 0x205523
leaq 0x4(%rsp), %rbp
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x20558f
testl %eax, %eax
jle 0x205523
movq 0x10(%r13), %rdi
movq 0x10(%r12), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x205197
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x20558f
testl %eax, %eax
jg 0x2054eb
movq 0x10(%r13), %rdi
callq 0x19e120
movq %r13, %rdi
callq 0x19e120
movq 0x10(%r12), %rdi
callq 0x19e120
movq %r12, %rdi
callq 0x19e120
movq (%r15), %rdi
callq 0x19e120
movq %r15, %rdi
callq 0x19e120
movq 0x18(%r14), %rdi
callq 0x19e120
movq %r14, %rdi
callq 0x19e120
movq %rbx, %rdi
callq 0x204dcb
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 0x205571