// File: /workspace/github_c_makefile_star2_O2/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
callq 0x1a6e71
movq %rax, 0x8(%rsp)
callq 0x1a665a
movq %rax, %rbx
leaq 0x3c18c6(%rip), %r12    # 0x568c53
movq 0x593c2c(%rip), %r14    # 0x73afc0
leaq 0x3c1095(%rip), %r13    # 0x568430
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x19e050
testl %eax, %eax
js 0x1a7491
leal -0x61(%rax), %ecx
cmpl $0x16, %ecx
ja 0x1a73ce
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, (%rbx)
jmp 0x1a739b
cmpl $0x48, %eax
je 0x1a7485
cmpl $0x52, %eax
je 0x1a746d
cmpl $0x4f, %eax
je 0x1a7479
cmpl $0x4d, %eax
jne 0x1a749c
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x20(%rbx)
jmp 0x1a739b
movl $0x1, 0x28(%rbx)
jmp 0x1a739b
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x8(%rbx)
jmp 0x1a739b
movq (%r14), %rax
movq %rax, 0x18(%rbx)
jmp 0x1a739b
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0xc(%rbx)
jmp 0x1a739b
movl $0x1, 0x24(%rbx)
jmp 0x1a739b
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x38(%rbx)
jmp 0x1a739b
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x10(%rbx)
jmp 0x1a739b
movq (%r14), %rdi
callq 0x19e1a0
movl %eax, 0x4(%rbx)
jmp 0x1a739b
movl $0x1, 0x30(%rbx)
jmp 0x1a739b
movl $0x1, 0x34(%rbx)
jmp 0x1a739b
movl $0x1, 0x2c(%rbx)
jmp 0x1a739b
movq 0x593b48(%rip), %rax    # 0x73afe0
cmpl %ebp, (%rax)
jle 0x1a74ac
movq %rbx, %rdi
callq 0x1a7069
pushq $0x1
popq %rax
jmp 0x1a7612
movq %rbx, %rdi
callq 0x1a6d1e
movq %rbx, %rdi
callq 0x1a6679
cmpl $0x0, 0x2c(%rbx)
je 0x1a7530
cmpl $0x1, 0x34(%rbx)
movq 0x593ae3(%rip), %r14    # 0x73afb0
movq (%r14), %rcx
jne 0x1a74dd
leaq 0x3c1791(%rip), %rdi    # 0x568c6a
pushq $0x3b
jmp 0x1a74e6
leaq 0x3c17c2(%rip), %rdi    # 0x568ca6
pushq $0x33
popq %rsi
pushq $0x1
popq %rdx
callq 0x19e200
cmpl $0x1, 0x24(%rbx)
jne 0x1a750a
movq (%r14), %rcx
leaq 0x3c17db(%rip), %rdi    # 0x568cda
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x19e200
cmpl $0x0, 0x28(%rbx)
je 0x1a7525
movq (%r14), %rcx
leaq 0x3c17c7(%rip), %rdi    # 0x568ce1
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x19e200
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x19e140
movq 0x593a99(%rip), %rax    # 0x73afd0
movq (%rax), %rdi
callq 0x19e0e0
movl %eax, %ebp
pushq $0x1
popq %r14
pushq $0x18
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x19e0b0
movq %rax, %r15
movl %ebp, 0x14(%rax)
movl $0x4000, %edi           # imm = 0x4000
callq 0x19e150
movq %rax, (%r15)
movq %r14, %rdi
movq %r12, %rsi
callq 0x19e0b0
movq %rax, %r13
movq %r14, %rdi
movq %r12, %rsi
callq 0x19e0b0
movq %rax, %r12
leaq 0x14(%rsp), %r14
andl $0x0, (%r14)
xorl %ebp, %ebp
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1a7621
testl %eax, %eax
jle 0x1a75cd
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x1a7621
movq 0x10(%r12), %rbp
testl %eax, %eax
jle 0x1a75cd
movq 0x10(%r13), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
callq 0x1a6fb8
jmp 0x1a758e
movq 0x10(%r13), %rdi
callq 0x19e120
movq %r13, %rdi
callq 0x19e120
movq %rbp, %rdi
callq 0x19e120
movq %r12, %rdi
callq 0x19e120
movq (%r15), %rdi
callq 0x19e120
movq %r15, %rdi
callq 0x19e120
movq 0x8(%rsp), %rdi
callq 0x1a6fa2
movq %rbx, %rdi
callq 0x1a6bff
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq