// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/main.c
// Function: align_with_ksw2
void align_with_ksw2(char *query, int query_length, char *target, int target_length, main_opt_t *opt, void* library_data, alignment_t *alignment) {
	int i;
	ksw2_data_t *ksw2_data = (ksw2_data_t*)library_data;

	// convert to bases in integer format
	for (i = 0; i < query_length; ++i) query[i] = seq_nt4_table[(int)query[i]];
	for (i = 0; i < target_length; ++i) target[i] = seq_nt4_table[(int)target[i]];

	switch (opt->alignment_mode) {
		case Local: fprintf(stderr, "KSW2 does not support local\n"); exit(1);
		case Glocal: fprintf(stderr, "KSW2 does not support glocal\n"); exit(1);
		case Extension: // extend
			ksw_extz2_sse(0, query_length, (uint8_t*)query, target_length, (uint8_t*)target, 5, ksw2_data->matrix, opt->gap_open, opt->gap_extend, opt->band_width, -1, ksw2_data->ksw2_flags | KSW_EZ_EXTZ_ONLY, &ksw2_data->ez);
			alignment->score   = ksw2_data->ez.mqe; // maximum score when we reach the end of the query
			if (ksw2_data->ez.max_q < 0) {
				alignment->qlb = -1;
				alignment->qle = -1;
				alignment->tlb = -1;
				alignment->tle = -1;
			}
			else {
				alignment->qlb = 0;
				alignment->tlb = 0;
				alignment->qle = ksw2_data->ez.max_q;
				alignment->tle = ksw2_data->ez.max_t;
			}
			break;
		case Global: // global
			ksw_extz2_sse(0, query_length, (uint8_t*)query, target_length, (uint8_t*)target, 5, ksw2_data->matrix, opt->gap_open, opt->gap_extend, opt->band_width, -1, ksw2_data->ksw2_flags, &ksw2_data->ez);
			alignment->score = ksw2_data->ez.score;
			alignment->qlb = 0;
			alignment->tlb = 0;
			alignment->qle   = query_length-1;
			alignment->tle   = target_length-1;
			break;
		default:
			fprintf(stderr, "Unknown alignment mode in %s: %d\n", __func__, opt->alignment_mode); 
			exit(1);
	}

	// copy cigar
	if (opt->add_cigar == 1) {
		if (alignment->m_cigar < ksw2_data->ez.m_cigar) {
			alignment->m_cigar = ksw2_data->ez.m_cigar;
			alignment->cigar = (uint32_t*)realloc(alignment->cigar, alignment->m_cigar*sizeof(uint32_t));
		}
		for (i = 0; i < ksw2_data->ez.n_cigar; i++) {
			alignment->cigar[i] = ksw2_data->ez.cigar[i];
		}
		alignment->n_cigar = ksw2_data->ez.n_cigar;
	}

	// convert back to bases
	for (i = 0; i < query_length; ++i) query[i] = "ACGTN"[(int)query[i]];
	for (i = 0; i < target_length; ++i) target[i] = "ACGTN"[(int)target[i]];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r13
movq %r8, %rbp
movq %rdx, %rbx
movq %rdi, %r15
movl %esi, %r14d
testl %esi, %esi
jle 0x2049cb
xorl %eax, %eax
leaq 0x59d769(%rip), %rdi    # 0x7a2120
movsbq (%r15,%rax), %rdx
movb (%rdx,%rdi), %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2049b7
movl %ecx, %r12d
testl %ecx, %ecx
jle 0x2049ee
xorl %eax, %eax
leaq 0x59d745(%rip), %rdi    # 0x7a2120
movsbq (%rbx,%rax), %rdx
movb (%rdx,%rdi), %dl
movb %dl, (%rbx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x2049db
movl 0x20(%rbp), %edi
cmpq $0x3, %rdi
ja 0x204bda
leaq 0x391a0e(%rip), %rax    # 0x596410
movslq (%rax,%rdi,4), %rdx
addq %rax, %rdx
movq %rcx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
jmpq *%rdx
movsbl 0x8(%rbp), %eax
movq %rax, 0x20(%rsp)
movsbl 0xc(%rbp), %eax
movq %rax, 0x18(%rsp)
movl 0x10(%rbp), %r11d
movl 0x8(%r13), %eax
orl $0x40, %eax
leaq 0x10(%r13), %r10
subq $0x8, %rsp
xorl %edi, %edi
movq %r15, %rdx
movq %rbx, %r8
movl $0x5, %r9d
pushq %r10
pushq %rax
pushq $-0x1
pushq %r11
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq (%r13)
callq 0x2012dc
addq $0x40, %rsp
movl 0x1c(%r13), %eax
movq 0x60(%rsp), %rdi
movl %eax, (%rdi)
movl 0x14(%r13), %eax
testl %eax, %eax
js 0x204b0b
movq $0x0, 0x4(%rdi)
movl %eax, 0xc(%rdi)
movl 0x18(%r13), %eax
movl %eax, 0x10(%rdi)
jmp 0x204b17
movsbl 0x8(%rbp), %eax
movq %rax, 0x20(%rsp)
movsbl 0xc(%rbp), %eax
movq %rax, 0x18(%rsp)
movl 0x10(%rbp), %r11d
movl 0x8(%r13), %eax
leaq 0x10(%r13), %r10
subq $0x8, %rsp
xorl %edi, %edi
movq %r15, %rdx
movq %rbx, %r8
movl $0x5, %r9d
pushq %r10
pushq %rax
pushq $-0x1
pushq %r11
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq (%r13)
callq 0x2012dc
movq 0x48(%rsp), %r9
movq 0x50(%rsp), %r8
addq $0x40, %rsp
movl 0x2c(%r13), %eax
movq 0x60(%rsp), %rdi
movl %eax, (%rdi)
movq $0x0, 0x4(%rdi)
leal -0x1(%r9), %eax
movl %eax, 0xc(%rdi)
leal -0x1(%r8), %eax
movl %eax, 0x10(%rdi)
jmp 0x204b21
leaq 0x4(%rdi), %rax
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rax)
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r9
cmpl $0x1, 0x24(%rbp)
jne 0x204b7e
movl 0x30(%r13), %eax
cmpl %eax, 0x24(%rdi)
jge 0x204b57
movslq %eax, %rsi
movl %esi, 0x24(%rdi)
movq %rdi, %rbp
movq 0x18(%rdi), %rdi
shlq $0x2, %rsi
callq 0x19e1e0
movq %rbp, %rdi
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movq %rax, 0x18(%rbp)
movl 0x34(%r13), %edx
testl %edx, %edx
jle 0x204b7b
movq 0x38(%r13), %rax
movq 0x18(%rdi), %rcx
xorl %esi, %esi
movl (%rax,%rsi,4), %edx
movl %edx, (%rcx,%rsi,4)
incq %rsi
movslq 0x34(%r13), %rdx
cmpq %rdx, %rsi
jl 0x204b69
movl %edx, 0x20(%rdi)
testl %r9d, %r9d
jle 0x204ba0
xorl %eax, %eax
leaq 0x391a0e(%rip), %rcx    # 0x59659a
movsbq (%r15,%rax), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x204b8c
testl %r8d, %r8d
jle 0x204bc1
xorl %eax, %eax
leaq 0x3919ec(%rip), %rcx    # 0x59659a
movsbq (%rbx,%rax), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%rbx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x204bae
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x19e70d
callq 0x19e6e8
callq 0x19e732