// File: /workspace/github_c_makefile_star2_O3/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 0x204bb0
xorl %eax, %eax
leaq 0x5d3584(%rip), %rdi    # 0x7d8120
movsbq (%r15,%rax), %rdx
movb (%rdx,%rdi), %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x204b9c
movl %ecx, %r12d
testl %ecx, %ecx
jle 0x204bd3
xorl %eax, %eax
leaq 0x5d3560(%rip), %rdi    # 0x7d8120
movsbq (%rbx,%rax), %rdx
movb (%rdx,%rdi), %dl
movb %dl, (%rbx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x204bc0
movl 0x20(%rbp), %edi
cmpq $0x3, %rdi
ja 0x204dbf
leaq 0x3c5839(%rip), %rax    # 0x5ca420
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 0x201394
addq $0x40, %rsp
movl 0x1c(%r13), %eax
movq 0x60(%rsp), %rdi
movl %eax, (%rdi)
movl 0x14(%r13), %eax
testl %eax, %eax
js 0x204cf0
movq $0x0, 0x4(%rdi)
movl %eax, 0xc(%rdi)
movl 0x18(%r13), %eax
movl %eax, 0x10(%rdi)
jmp 0x204cfc
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 0x201394
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 0x204d06
leaq 0x4(%rdi), %rax
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rax)
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r9
cmpl $0x1, 0x24(%rbp)
jne 0x204d63
movl 0x30(%r13), %eax
cmpl %eax, 0x24(%rdi)
jge 0x204d3c
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 0x204d60
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 0x204d4e
movl %edx, 0x20(%rdi)
testl %r9d, %r9d
jle 0x204d85
xorl %eax, %eax
leaq 0x3c5829(%rip), %rcx    # 0x5ca59a
movsbq (%r15,%rax), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x204d71
testl %r8d, %r8d
jle 0x204da6
xorl %eax, %eax
leaq 0x3c5807(%rip), %rcx    # 0x5ca59a
movsbq (%rbx,%rax), %rdx
movb (%rdx,%rcx), %dl
movb %dl, (%rbx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x204d93
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x19e740
callq 0x19e71b
callq 0x19e765