// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/./ksw2.h
// Function: ksw_backtrack
static inline void ksw_backtrack(void *km, int is_rot, int is_rev, int min_intron_len, 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[(size_t)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[(size_t)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, KSW_CIGAR_MATCH, 1), --i, --j;
		else if (state == 1 || (state == 3 && min_intron_len <= 0)) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, KSW_CIGAR_DEL, 1), --i;
		else if (state == 3 && min_intron_len > 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, KSW_CIGAR_N_SKIP, 1), --i;
		else cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, KSW_CIGAR_INS, 1), --j;
	}
	if (i >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, min_intron_len > 0 && i >= min_intron_len? KSW_CIGAR_N_SKIP : KSW_CIGAR_DEL, i + 1); // first deletion
	if (j >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, KSW_CIGAR_INS, 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 $0x18, %rsp
movq %r9, %r13
movq %r8, %rbx
movq %rcx, 0x10(%rsp)
movl %edx, 0x8(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, %r14
movq 0x78(%rsp), %rcx
movq 0x68(%rsp), %rax
movl 0x60(%rsp), %r15d
movl 0x58(%rsp), %r12d
movq %rsp, %rdx
andl $0x0, (%rdx)
movl (%rax), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
movq (%rcx), %rax
xorl %ebp, %ebp
testl %r12d, %r12d
js 0x20f37
testl %r15d, %r15d
js 0x20f37
leal (%r15,%r12), %ecx
movslq (%rbx,%rcx,4), %rdx
testq %r13, %r13
je 0x20e42
cmpl %edx, %r12d
setl %r8b
xorl %esi, %esi
cmpl (%r13,%rcx,4), %r12d
setg %dil
orb %dil, %r8b
je 0x20e50
movb %dil, %sil
pushq $0x2
popq %rdi
subl %esi, %edi
xorl %edx, %edx
movb $0x1, %sil
jmp 0x20e6f
cmpl %edx, %r12d
jge 0x20e50
xorl %edx, %edx
movb $0x1, %sil
pushq $0x2
jmp 0x20e6e
movl 0x50(%rsp), %esi
imulq %rsi, %rcx
movl %r12d, %esi
addq %rcx, %rsi
subq %rdx, %rsi
movq 0x10(%rsp), %rcx
movzbl (%rcx,%rsi), %edx
xorl %esi, %esi
pushq $-0x1
popq %rdi
testl %ebp, %ebp
je 0x20e90
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 0x20e95
andl $0x7, %edx
movl %edx, %ebp
testl %edx, %edx
cmovnel %edx, %ebp
testb %sil, %sil
cmovnel %edi, %ebp
cmpl $0x1, %ebp
je 0x20eb5
testl %ebp, %ebp
je 0x20ef6
cmpl $0x0, 0x8(%rsp)
jg 0x20ec7
cmpl $0x3, %ebp
jne 0x20ec7
movq %r14, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x2
jmp 0x20ee3
cmpl $0x0, 0x8(%rsp)
jle 0x20f15
cmpl $0x3, %ebp
jne 0x20f15
movq %r14, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x3
popq %r8
pushq $0x1
popq %r9
callq 0x20ffa
decl %r12d
jmp 0x20dfd
movq %r14, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x20ffa
decl %r12d
jmp 0x20f2f
movq %r14, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rax, %rcx
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0x20ffa
decl %r15d
jmp 0x20dfd
testl %r12d, %r12d
js 0x20f6e
movl 0x8(%rsp), %edx
testl %edx, %edx
setg %cl
cmpl %edx, %r12d
setge %dl
andb %cl, %dl
movzbl %dl, %r8d
orl $0x2, %r8d
incl %r12d
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
movq %rax, %rcx
movl %r12d, %r9d
callq 0x20ffa
movq 0x78(%rsp), %rbx
movl 0xc(%rsp), %ebp
testl %r15d, %r15d
js 0x20f99
incl %r15d
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
pushq $0x1
popq %r8
movq %r14, %rdi
movq %rax, %rcx
movl %r15d, %r9d
callq 0x20ffa
movq 0x70(%rsp), %r8
movq 0x68(%rsp), %r9
testl %ebp, %ebp
je 0x20fc6
movl (%rsp), %esi
movl 0x4(%rsp), %ecx
movl %ecx, (%r9)
movl %esi, (%r8)
movq %rax, (%rbx)
addq $0x18, %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 0x20faa
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 0x20fcb