// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movq %rcx, %r11
movq %rdx, 0x20(%rsp)
movl %esi, 0x10(%rsp)
movq %rdi, %r15
movq 0x80(%rsp), %rbx
movq 0x70(%rsp), %rax
movl 0x68(%rsp), %r13d
movl 0x60(%rsp), %r12d
movl (%rax), %r10d
movq (%rbx), %rax
testl %r13d, %r13d
setns %r14b
movl %r12d, %ecx
orl %r13d, %ecx
js 0x2007a9
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r8, 0x8(%rsp)
movq %r11, (%rsp)
leal (%r12,%r13), %ecx
movl (%r11,%rcx,4), %edx
testq %r8, %r8
je 0x20053b
cmpl %edx, %r12d
setl %r9b
xorl %esi, %esi
cmpl (%r8,%rcx,4), %r12d
setg %dil
orb %dil, %r9b
je 0x20054c
movb %dil, %sil
movl $0x2, %edx
subl %esi, %edx
xorl %esi, %esi
jmp 0x200547
cmpl %edx, %r12d
jge 0x20054c
xorl %esi, %esi
movl $0x2, %edx
movb $0x1, %dil
jmp 0x200569
imull 0x14(%rsp), %ecx
addl %r12d, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x20(%rsp), %rdx
movzbl (%rdx,%rcx), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %edi, %edi
testl %ebx, %ebx
je 0x20058a
movl %esi, %r9d
movl %ebx, %ecx
shrl %cl, %r9d
shll $0x1d, %r9d
sarl $0x1f, %r9d
andl %ebx, %r9d
andl $0x7, %esi
movl %esi, %ebx
movl %r9d, %esi
jmp 0x20058f
andl $0x7, %esi
movl %esi, %ebx
testl %esi, %esi
cmovnel %esi, %ebx
testb %dil, %dil
cmovnel %edx, %ebx
cmpl $0x3, %ebx
je 0x20066b
cmpl $0x1, %ebx
je 0x20060d
testl %ebx, %ebx
jne 0x200689
testl %ebp, %ebp
je 0x2005c4
movslq %ebp, %rcx
movl -0x4(%rax,%rcx,4), %edx
testb $0xf, %dl
je 0x200749
cmpl %r10d, %ebp
jne 0x2005fc
addl %r10d, %r10d
testl %ebp, %ebp
movl $0x4, %ecx
cmovel %ecx, %r10d
leal (,%r10,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r10, %r14
callq 0x1fbaa1
movq (%rsp), %r11
movq 0x8(%rsp), %r8
movq %r14, %r10
movslq %ebp, %rcx
incl %ebp
movl $0x10, (%rax,%rcx,4)
jmp 0x200750
testl %ebp, %ebp
je 0x200622
movslq %ebp, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
je 0x200680
cmpl %r10d, %ebp
jne 0x20065a
addl %r10d, %r10d
testl %ebp, %ebp
movl $0x4, %ecx
cmovel %ecx, %r10d
leal (,%r10,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r10, %r14
callq 0x1fbaa1
movq (%rsp), %r11
movq 0x8(%rsp), %r8
movq %r14, %r10
movslq %ebp, %rcx
incl %ebp
movl $0x12, (%rax,%rcx,4)
jmp 0x2006f0
testl %ebp, %ebp
je 0x2006ac
movslq %ebp, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x3, %esi
jne 0x2006ac
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x2006f0
movslq %ebp, %r14
testl %ebp, %ebp
je 0x2006f5
movl -0x4(%rax,%r14,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x1, %edx
jne 0x2006f5
addl $0x10, %ecx
movl %ecx, -0x4(%rax,%r14,4)
jmp 0x200753
cmpl %r10d, %ebp
jne 0x2006e4
addl %r10d, %r10d
testl %ebp, %ebp
movl $0x4, %ecx
cmovel %ecx, %r10d
leal (,%r10,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r10, %r14
callq 0x1fbaa1
movq (%rsp), %r11
movq 0x8(%rsp), %r8
movq %r14, %r10
movslq %ebp, %rcx
incl %ebp
movl $0x13, (%rax,%rcx,4)
decl %r12d
jmp 0x200756
cmpl %r10d, %ebp
jne 0x20073d
addl %r10d, %r10d
testl %ebp, %ebp
movl $0x4, %ecx
cmovel %ecx, %r10d
leal (,%r10,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r13, 0x18(%rsp)
movq %r15, %r13
movq %r10, %r15
callq 0x1fbaa1
movq (%rsp), %r11
movq 0x8(%rsp), %r8
movq %r15, %r10
movq %r13, %r15
movq 0x18(%rsp), %r13
incl %ebp
movl $0x11, (%rax,%r14,4)
jmp 0x200753
addl $0x10, %edx
movl %edx, -0x4(%rax,%rcx,4)
decl %r12d
decl %r13d
testl %r13d, %r13d
setns %r14b
testl %r12d, %r12d
js 0x20076b
testl %r13d, %r13d
jns 0x20050a
testl %r12d, %r12d
js 0x2007b7
incl %r12d
testl %ebp, %ebp
movq 0x80(%rsp), %rbx
je 0x2007b1
movslq %ebp, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x2, %esi
jne 0x2007c5
shll $0x4, %r12d
addl %r12d, %edx
movl %edx, -0x4(%rax,%rcx,4)
testl %r13d, %r13d
js 0x20088a
incl %r13d
jmp 0x200823
testl %r12d, %r12d
js 0x2007c1
incl %r12d
xorl %ebp, %ebp
movb $0x1, %cl
jmp 0x2007c7
movq 0x80(%rsp), %rbx
jmp 0x200817
xorl %ebp, %ebp
jmp 0x200817
xorl %ecx, %ecx
cmpl %r10d, %ebp
jne 0x200806
leal (,%rbp,2), %edx
testb %cl, %cl
movq %r15, %rbx
movl $0x4, %r15d
cmovel %edx, %r15d
leal (,%r15,4), %ecx
movslq %ecx, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1fbaa1
movq %r15, %r10
movq %rbx, %r15
movq 0x80(%rsp), %rbx
shll $0x4, %r12d
orl $0x2, %r12d
movslq %ebp, %rcx
incl %ebp
movl %r12d, (%rax,%rcx,4)
testb %r14b, %r14b
je 0x20088a
incl %r13d
testl %ebp, %ebp
je 0x200845
movslq %ebp, %rcx
movl -0x4(%rax,%rcx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x200841
shll $0x4, %r13d
addl %r13d, %edx
movl %edx, -0x4(%rax,%rcx,4)
jmp 0x20088a
xorl %ecx, %ecx
jmp 0x200849
xorl %ebp, %ebp
movb $0x1, %cl
cmpl %r10d, %ebp
jne 0x20087a
leal (,%rbp,2), %edx
testb %cl, %cl
movl $0x4, %r14d
cmovel %edx, %r14d
leal (,%r14,4), %ecx
movslq %ecx, %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1fbaa1
movq %r14, %r10
shll $0x4, %r13d
incl %r13d
movslq %ebp, %rcx
incl %ebp
movl %r13d, (%rax,%rcx,4)
movq 0x78(%rsp), %rcx
cmpl $0x0, 0x10(%rsp)
jne 0x2008c2
movl %ebp, %edx
sarl %edx
testl %edx, %edx
jle 0x2008c2
movl %edx, %edx
leal -0x1(%rbp), %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 0x2008a5
movq 0x70(%rsp), %rdx
movl %r10d, (%rdx)
movl %ebp, (%rcx)
movq %rax, (%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop