// File: /workspace/github_c_makefile_star2_O1/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
movl %r9d, 0x1c(%rsp)
movq %rcx, %r9
movq %rdx, 0x20(%rsp)
movl %esi, 0x18(%rsp)
movq %rdi, %r15
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
movl 0x68(%rsp), %ebp
movl 0x60(%rsp), %r12d
movl (%rcx), %r10d
movq (%rax), %rax
testl %r12d, %r12d
setns %cl
testl %ebp, %ebp
setns %r13b
movl %r12d, %edx
orl %ebp, %edx
js 0x2006eb
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r8, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movq %r15, (%rsp)
leal (%r12,%rbp), %ecx
movl (%r9,%rcx,4), %edi
xorl %edx, %edx
cmpl %edi, %r12d
setl %dl
leal (%rdx,%rdx,2), %edx
decl %edx
testq %r8, %r8
je 0x2004d9
cmpl (%r8,%rcx,4), %r12d
movl $0x1, %esi
cmovgl %esi, %edx
xorl %esi, %esi
testl %edx, %edx
jns 0x2004f5
imull 0x1c(%rsp), %ecx
addl %r12d, %ecx
subl %edi, %ecx
movslq %ecx, %rcx
movq 0x20(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
movl %esi, %edi
testl %ebx, %ebx
je 0x200509
movl %ebx, %ecx
shrl %cl, %edi
shll $0x1d, %edi
sarl $0x1f, %edi
andl %ebx, %edi
jmp 0x20050c
andl $0x7, %edi
andl $0x7, %esi
testl %edi, %edi
cmovnel %edi, %esi
testl %edx, %edx
movl %esi, %ebx
cmovnsl %edx, %ebx
cmpl $0x3, %ebx
je 0x2005f0
cmpl $0x1, %ebx
je 0x200590
testl %ebx, %ebx
jne 0x20060f
testl %r14d, %r14d
je 0x200546
movslq %r14d, %rcx
movl -0x4(%rax,%rcx,4), %edx
testb $0xf, %dl
je 0x2006c7
cmpl %r10d, %r14d
jne 0x20057e
leal (%r10,%r10), %r13d
testl %r10d, %r10d
movl $0x4, %ecx
cmovel %ecx, %r13d
leal (,%r13,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1fbaa3
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movl %r13d, %r10d
movslq %r14d, %rcx
incl %r14d
movl $0x10, (%rax,%rcx,4)
jmp 0x2006ce
testl %r14d, %r14d
je 0x2005a6
movslq %r14d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
je 0x200606
cmpl %r10d, %r14d
jne 0x2005de
leal (%r10,%r10), %r13d
testl %r10d, %r10d
movl $0x4, %ecx
cmovel %ecx, %r13d
leal (,%r13,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1fbaa3
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movl %r13d, %r10d
movslq %r14d, %rcx
incl %r14d
movl $0x12, (%rax,%rcx,4)
jmp 0x200678
testl %r14d, %r14d
je 0x200633
movslq %r14d, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x3, %esi
jne 0x200633
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x200678
movslq %r14d, %r13
testl %r14d, %r14d
je 0x20067d
movl -0x4(%rax,%r13,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x20067d
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r13,4)
jmp 0x2006d1
cmpl %r10d, %r14d
jne 0x20066b
leal (%r10,%r10), %r13d
testl %r10d, %r10d
movl $0x4, %ecx
cmovel %ecx, %r13d
leal (,%r13,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1fbaa3
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movl %r13d, %r10d
movslq %r14d, %rcx
incl %r14d
movl $0x13, (%rax,%rcx,4)
decl %r12d
jmp 0x2006d3
cmpl %r10d, %r14d
jne 0x2006ba
leal (%r10,%r10), %r15d
testl %r10d, %r10d
movl $0x4, %ecx
cmovel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa3
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movl %r15d, %r10d
movq (%rsp), %r15
incl %r14d
movl $0x11, (%rax,%r13,4)
jmp 0x2006d1
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
decl %r12d
decl %ebp
testl %ebp, %ebp
setns %r13b
testl %r12d, %r12d
setns %cl
js 0x2006ee
testl %ebp, %ebp
jns 0x2004b3
jmp 0x2006ee
xorl %r14d, %r14d
testb %cl, %cl
je 0x20075f
incl %r12d
movslq %r14d, %rbx
testl %r14d, %r14d
je 0x200718
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x2, %edx
jne 0x200718
shll $0x4, %r12d
addl %r12d, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x20075f
cmpl %r10d, %r14d
jne 0x200750
leal (%r10,%r10), %ecx
testl %r10d, %r10d
movq %r15, (%rsp)
movl $0x4, %r15d
cmovnel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x1fbaa3
movq %r15, %r10
movq (%rsp), %r15
shll $0x4, %r12d
orl $0x2, %r12d
incl %r14d
movl %r12d, (%rax,%rbx,4)
testb %r13b, %r13b
je 0x2007c1
incl %ebp
movslq %r14d, %rbx
testl %r14d, %r14d
je 0x200787
movl -0x4(%rax,%rbx,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x200787
shll $0x4, %ebp
addl %ebp, %ecx
movl %ecx, -0x4(%rax,%rbx,4)
jmp 0x2007c1
cmpl %r10d, %r14d
jne 0x2007b6
leal (%r10,%r10), %ecx
testl %r10d, %r10d
movq %r15, %rdi
movl $0x4, %r15d
cmovnel %ecx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq %rax, %rsi
callq 0x1fbaa3
movq %r15, %r10
shll $0x4, %ebp
incl %ebp
incl %r14d
movl %ebp, (%rax,%rbx,4)
movq 0x78(%rsp), %rcx
cmpl $0x0, 0x18(%rsp)
jne 0x2007fb
movl %r14d, %edx
sarl %edx
testl %edx, %edx
jle 0x2007fb
movl %edx, %edx
leal -0x1(%r14), %esi
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
movslq %esi, %rsi
movl (%rax,%rsi,4), %r9d
movl %r9d, (%rax,%rdi,4)
movl %r8d, (%rax,%rsi,4)
incq %rdi
decl %esi
cmpq %rdi, %rdx
jne 0x2007de
movq 0x70(%rsp), %rdx
movl %r10d, (%rdx)
movl %r14d, (%rcx)
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq