// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x88, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movq %rdx, -0x30(%rbp)
movl %ecx, -0x34(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1b8130
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xc6f009(%rip), %rax    # 0xe27120
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b80f9
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1b816e
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xc6efcb(%rip), %rax    # 0xe27120
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b8137
movq -0x40(%rbp), %rax
movl 0x20(%rax), %eax
movq %rax, -0x60(%rbp)
subq $0x3, %rax
ja 0x1b838a
movq -0x60(%rbp), %rax
leaq 0xad51e6(%rip), %rcx    # 0xc8d374
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xc6ee3a(%rip), %rax    # 0xe26fd8
movq (%rax), %rdi
leaq 0xad5690(%rip), %rsi    # 0xc8d838
movb $0x0, %al
callq 0x19e180
movl $0x1, %edi
callq 0x19e130
movq 0xc6ee18(%rip), %rax    # 0xe26fd8
movq (%rax), %rdi
leaq 0xad568b(%rip), %rsi    # 0xc8d855
movb $0x0, %al
callq 0x19e180
movl $0x1, %edi
callq 0x19e130
movl -0x24(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0x58(%rbp), %rax
movq (%rax), %r15
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movb %al, %r14b
movq -0x40(%rbp), %rax
movl 0xc(%rax), %eax
movb %al, %bl
movq -0x40(%rbp), %rax
movl 0x10(%rax), %r11d
movq -0x58(%rbp), %rax
movl 0x8(%rax), %r10d
orl $0x40, %r10d
movq -0x58(%rbp), %rax
addq $0x10, %rax
xorl %edi, %edi
movl $0x5, %r9d
movq %r15, (%rsp)
movsbl %r14b, %r14d
movl %r14d, 0x8(%rsp)
movsbl %bl, %ebx
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movl %r10d, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x1af910
movq -0x58(%rbp), %rax
movl 0x1c(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jge 0x1b829c
movq 0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
jmp 0x1b82ce
movq 0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movq 0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x58(%rbp), %rax
movl 0x14(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x58(%rbp), %rax
movl 0x18(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x1b83ba
movl -0x24(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0x58(%rbp), %rax
movq (%rax), %r15
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movb %al, %r14b
movq -0x40(%rbp), %rax
movl 0xc(%rax), %eax
movb %al, %bl
movq -0x40(%rbp), %rax
movl 0x10(%rax), %r11d
movq -0x58(%rbp), %rax
movl 0x8(%rax), %r10d
movq -0x58(%rbp), %rax
addq $0x10, %rax
xorl %edi, %edi
movl $0x5, %r9d
movq %r15, (%rsp)
movsbl %r14b, %r14d
movl %r14d, 0x8(%rsp)
movsbl %bl, %ebx
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movl %r10d, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x1af910
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq 0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movq 0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x1b83ba
movq 0xc6ec47(%rip), %rax    # 0xe26fd8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x20(%rax), %ecx
leaq 0xad5202(%rip), %rsi    # 0xc8d5a4
leaq 0xad54ca(%rip), %rdx    # 0xc8d873
movb $0x0, %al
callq 0x19e180
movl $0x1, %edi
callq 0x19e130
movq -0x40(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jne 0x1b8454
movq 0x10(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x1b840a
movq -0x58(%rbp), %rax
movl 0x30(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x10(%rbp), %rax
movslq 0x24(%rax), %rsi
shlq $0x2, %rsi
callq 0x19e1f0
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0x1b8446
movq -0x58(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b8411
movq -0x58(%rbp), %rax
movl 0x34(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1b8492
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xad5125(%rip), %rax    # 0xc8d59e
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b845b
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1b84d0
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0xad50e7(%rip), %rax    # 0xc8d59e
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b8499
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop