// File: /workspace/github_c_makefile_star2_O2/nh13[P]ksw/src/ksw2/./ksw2.h
// Function: ksw_backtrack
static inline void ksw_backtrack(void *km, int is_rot, int is_rev, int with_N, const uint8_t *p, const int *off, const int *off_end, int n_col, int i0, int j0,
								 int *m_cigar_, int *n_cigar_, uint32_t **cigar_)
{ // p[] - lower 3 bits: which type gets the max; bit
	int n_cigar = 0, m_cigar = *m_cigar_, i = i0, j = j0, r, state = 0;
	uint32_t *cigar = *cigar_, tmp;
	while (i >= 0 && j >= 0) { // at the beginning of the loop, _state_ tells us which state to check
		int force_state = -1;
		if (is_rot) {
			r = i + j;
			if (i < off[r]) force_state = 2;
			if (off_end && i > off_end[r]) force_state = 1;
			tmp = force_state < 0? p[r * n_col + i - off[r]] : 0;
		} else {
			if (j < off[i]) force_state = 2;
			if (off_end && j > off_end[i]) force_state = 1;
			tmp = force_state < 0? p[i * n_col + j - off[i]] : 0;
		}
		if (state == 0) state = tmp & 7; // if requesting the H state, find state one maximizes it.
		else if (!(tmp >> (state + 2) & 1)) state = 0; // if requesting other states, _state_ stays the same if it is a continuation; otherwise, set to H
		if (state == 0) state = tmp & 7; // TODO: probably this line can be merged into the "else if" line right above; not 100% sure
		if (force_state >= 0) state = force_state;
		if (state == 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 0, 1), --i, --j; // match
		else if (state == 1 || (state == 3 && !with_N)) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 2, 1), --i; // deletion
		else if (state == 3 && with_N) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 3, 1), --i; // intron
		else cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 1, 1), --j; // insertion
	}
	if (i >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 2, i + 1); // first deletion
	if (j >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 1, j + 1); // first insertion
	if (!is_rev)
		for (i = 0; i < n_cigar>>1; ++i) // reverse CIGAR
			tmp = cigar[i], cigar[i] = cigar[n_cigar-1-i], cigar[n_cigar-1-i] = tmp;
	*m_cigar_ = m_cigar, *n_cigar_ = n_cigar, *cigar_ = cigar;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r13
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movl %esi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq 0x80(%rsp), %rcx
movq 0x70(%rsp), %rax
movl 0x68(%rsp), %r14d
movq %rsp, %rdx
andl $0x0, (%rdx)
movl 0x60(%rsp), %r12d
movl (%rax), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
movq (%rcx), %rax
movl %r9d, %r15d
notl %r15d
xorl %ebp, %ebp
movl %r9d, 0x14(%rsp)
leal (%r12,%r14), %ebx
imull %r9d, %ebx
addl %r12d, %ebx
testl %r12d, %r12d
js 0x1a2b2b
testl %r14d, %r14d
js 0x1a2b2b
leal (%r14,%r12), %edx
movq 0x20(%rsp), %rcx
movl (%rcx,%rdx,4), %ecx
testq %r13, %r13
je 0x1a2a43
cmpl %ecx, %r12d
setl %dil
xorl %esi, %esi
cmpl (%r13,%rdx,4), %r12d
setg %dl
orb %dl, %dil
je 0x1a2a51
movb %dl, %sil
pushq $0x2
popq %rdi
subl %esi, %edi
xorl %edx, %edx
movb $0x1, %sil
jmp 0x1a2a66
cmpl %ecx, %r12d
jge 0x1a2a51
xorl %edx, %edx
movb $0x1, %sil
pushq $0x2
jmp 0x1a2a65
movl %ebx, %edx
subl %ecx, %edx
movslq %edx, %rcx
movq 0x18(%rsp), %rdx
movzbl (%rdx,%rcx), %edx
xorl %esi, %esi
pushq $-0x1
popq %rdi
testl %ebp, %ebp
je 0x1a2a87
movl %edx, %r8d
movl %ebp, %ecx
shrl %cl, %r8d
shll $0x1d, %r8d
sarl $0x1f, %r8d
andl %ebp, %r8d
andl $0x7, %edx
movl %edx, %ebp
movl %r8d, %edx
jmp 0x1a2a8c
andl $0x7, %edx
movl %edx, %ebp
testl %edx, %edx
cmovnel %edx, %ebp
testb %sil, %sil
cmovnel %edi, %ebp
cmpl $0x1, %ebp
je 0x1a2ab5
cmpl $0x3, %ebp
jne 0x1a2add
movq 0x8(%rsp), %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x3
jmp 0x1a2ac7
movq 0x8(%rsp), %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x2
popq %r8
pushq $0x1
popq %r9
callq 0x1a2be0
decl %r12d
addl %r15d, %ebx
jmp 0x1a29fb
testl %ebp, %ebp
jne 0x1a2b02
movq 0x8(%rsp), %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x1a2be0
decl %r12d
jmp 0x1a2b1e
movq 0x8(%rsp), %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0x1a2be0
decl %r14d
movl 0x14(%rsp), %r9d
jmp 0x1a29f0
testl %r12d, %r12d
js 0x1a2b4f
incl %r12d
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
pushq $0x2
popq %r8
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %r12d, %r9d
callq 0x1a2be0
movq 0x80(%rsp), %rbx
movl 0x10(%rsp), %ebp
testl %r14d, %r14d
js 0x1a2b7f
incl %r14d
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
pushq $0x1
popq %r8
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %r14d, %r9d
callq 0x1a2be0
movq 0x78(%rsp), %r8
movq 0x70(%rsp), %r9
testl %ebp, %ebp
je 0x1a2bac
movl (%rsp), %esi
movl 0x4(%rsp), %ecx
movl %ecx, (%r9)
movl %esi, (%r8)
movq %rax, (%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rcx
xorl %edx, %edx
movl (%rsp), %esi
movl %esi, %edi
sarl %edi
movslq %edi, %rdi
cmpq %rdi, %rdx
jge 0x1a2b90
movl (%rax,%rdx,4), %edi
addl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %esi
movl %esi, (%rax,%rdx,4)
movl (%rsp), %esi
addl %ecx, %esi
movslq %esi, %rsi
movl %edi, (%rax,%rsi,4)
incq %rdx
decl %ecx
jmp 0x1a2bb1