// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_affine_traceback_dp(Candidates_list*, long, long, long, long, long)
int64_t gen_affine_traceback_dp(Candidates_list *ac, int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t block, int64_t block_n)
{
    if(ac->length < 1) return 0;
    int64_t i, j, *p, *t, max_f, n_skip, max_j, end_j, st, max_ii, sc, max, tmp, msc_i, msc; 
    int32_t *f, cL; k_mer_hit* a = ac->list; int64_t a_n = ac->length; Chain_Data* dp;

    for (i = 1; i < a_n; ++i) {
        sc = traceback_sc(&a[i], &a[i-1]);
        if(sc == INT32_MIN) break;
    }
    if(i >= a_n) return a_n;

    split_long_anchors(ac, block, block_n);
    a = ac->list; a_n = ac->length; dp = &(ac->chainDP);
    
    resize_Chain_Data(dp, a_n, NULL); t = dp->tmp; f = dp->score; p = dp->pre;
    t[0] = 0; p[0] = -1; f[0] = a[0].cnt;
    msc_i = msc = -1; i = 0;

    memset(t, 0, (a_n*sizeof((*t))));
    for (i = st = 0, max_ii = -1; i < a_n; ++i) {
        max_f = a[i].cnt; n_skip = 0; max_j = end_j = -1;
        if ((i-st) > max_iter) st = i-max_iter;

        for (j = i - 1; j >= st; --j) {
            sc = traceback_sc(&a[i], &a[j]);
            if(sc == INT32_MIN) break;
            sc += f[j];
            if (sc > max_f) {
				max_f = sc, max_j = j;
				if (n_skip > 0) --n_skip;
			} else if (t[j] == (int32_t)i) {
				if (++n_skip > max_skip)
					break;
			}
			if (p[j] >= 0) t[p[j]] = i;
        }
        end_j = j;

        if (max_ii < 0 || ((int64_t)a[i].offset) - ((int64_t)a[max_ii].offset) > max_dis) {
			max = INT32_MIN; max_ii = -1;
			for (j = i - 1; (j >= st) && ((((int64_t)a[i].offset)-((int64_t)a[j].offset))<=max_dis); --j) {
                if (max < f[j]) {
                    max = f[j], max_ii = j;
                }
            }
		}

        if (max_ii >= 0 && max_ii < end_j) {///just have a try with a[i]<->a[max_ii]
			tmp = traceback_sc(&a[i], &a[max_ii]);
			if (tmp != INT32_MIN && max_f < tmp + f[max_ii])
				max_f = tmp + f[max_ii], max_j = max_ii;
		}
        f[i] = max_f; p[i] = max_j;
        if ((max_ii < 0) || (((((int64_t)a[i].offset)-((int64_t)a[max_ii].offset))<=max_dis) && (f[max_ii]<f[i]))) {
            max_ii = i;
        }

        if(f[i] > msc) {
            msc = f[i]; msc_i = i;
        }
    }

    cL = 0; i = msc_i; 
    while (i >= 0) {
        t[cL++] = i; i = p[i];
    }
    for (i = 0; i < cL; i++) a[i] = a[t[cL-i-1]];
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x8(%rdi), %r13
testq %r13, %r13
jle 0x2e03b
movq %r9, %r15
movq %r8, %r12
movq %rdi, %rbp
movq (%rdi), %rsi
movq %r13, %rbx
decq %rbx
je 0x2e03e
leaq 0x10(%rsi), %r14
leaq 0x10(%rsi), %rdi
callq 0x580b0
movq %r14, %rsi
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x2dd2c
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2db99
movq (%rbp), %r15
movq 0x8(%rbp), %rbx
leaq 0x18(%rbp), %rdi
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1383e
movq 0x40(%rbp), %rdi
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %rax
andq $0x0, (%rdi)
movq %rax, 0x8(%rsp)
orq $-0x1, (%rax)
movl 0xc(%r15), %eax
movl %eax, (%r12)
leaq (,%rbx,8), %rdx
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
callq 0x20e0
movq %r12, %rdi
testq %rbx, %rbx
movl $0x0, %eax
movq %rax, 0x18(%rsp)
cmovleq %r14, %rbx
movq %rbx, 0x50(%rsp)
leaq -0x10(%r15), %r13
leaq -0xc(%r15), %rax
movq %rax, 0x30(%rsp)
pushq $-0x1
popq %rax
movq %rax, %rcx
movq %rax, %rdx
movq %rax, %rsi
xorl %r8d, %r8d
movq %r12, 0x28(%rsp)
movq %r15, 0x48(%rsp)
cmpq 0x50(%rsp), %r8
je 0x2e053
movq %rcx, 0x68(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movq %r8, %rax
shlq $0x4, %rax
leaq (%r15,%rax), %rbp
movl 0xc(%r15,%rax), %r9d
movq %r8, %rax
movq 0x18(%rsp), %rsi
subq %rsi, %rax
movq %r8, %rcx
movq 0x58(%rsp), %rdx
subq %rdx, %rcx
cmpq %rdx, %rax
cmovgq %rcx, %rsi
movq %rsi, 0x18(%rsp)
leaq -0x1(%r8), %r14
movq %r8, 0x20(%rsp)
movslq %r8d, %rax
movq %rax, 0x80(%rsp)
movq %r13, 0x70(%rsp)
xorl %r15d, %r15d
pushq $-0x1
popq %rax
movq %rax, (%rsp)
movq %r14, %rbx
cmpq 0x18(%rsp), %rbx
jl 0x2dee8
movq %rbp, %rdi
movq %r13, %rsi
movq %r9, %r12
callq 0x580b0
movq %r12, %r9
movq 0x28(%rsp), %rdi
cmpq $-0x80000000, %rax      # imm = 0x80000000
je 0x2dee8
movslq (%rdi,%rbx,4), %rcx
addq %rcx, %rax
cmpq %r9, %rax
jle 0x2dea3
xorl %ecx, %ecx
testq %r15, %r15
setg %cl
subq %rcx, %r15
movq %rax, %r9
movq %rbx, (%rsp)
jmp 0x2dec0
movq 0x10(%rsp), %rax
movq 0x80(%rsp), %rcx
cmpq %rcx, (%rax,%rbx,8)
jne 0x2dec0
cmpq 0x78(%rsp), %r15
jge 0x2dee8
incq %r15
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rax
testq %rax, %rax
js 0x2dedc
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
decq %rbx
addq $-0x10, %r13
jmp 0x2de5a
movq 0x68(%rsp), %r12
testq %r12, %r12
movq 0x48(%rsp), %r15
movq 0x60(%rsp), %r13
js 0x2df13
movl 0x4(%rbp), %eax
movq %r12, %rcx
shlq $0x4, %rcx
movl 0x4(%r15,%rcx), %ecx
subq %rcx, %rax
cmpq %r13, %rax
jle 0x2df4f
movq $-0x80000000, %rax      # imm = 0x80000000
movq 0x30(%rsp), %rcx
pushq $-0x1
popq %r12
cmpq 0x18(%rsp), %r14
jl 0x2df4f
movl 0x4(%rbp), %edx
movl (%rcx), %esi
subq %rsi, %rdx
cmpq %r13, %rdx
jg 0x2df4f
movslq (%rdi,%r14,4), %rdx
cmpq %rdx, %rax
cmovlq %r14, %r12
cmovleq %rdx, %rax
decq %r14
addq $-0x10, %rcx
jmp 0x2df23
testq %r12, %r12
sets %al
cmpq %rbx, %r12
setge %cl
orb %al, %cl
movq %r12, %r14
jne 0x2df9c
movq %r9, %rbx
movq %r14, %rsi
shlq $0x4, %rsi
addq %r15, %rsi
movq %rbp, %rdi
callq 0x580b0
cmpq $-0x80000000, %rax      # imm = 0x80000000
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %r8
jne 0x2dfc6
movq 0x28(%rsp), %rdi
movq %rbx, %r9
movq (%rsp), %rcx
jmp 0x2dfe4
movq 0x20(%rsp), %r8
movl %r9d, (%rdi,%r8,4)
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax,%r8,8)
movq %r8, %rax
testq %r14, %r14
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
jns 0x2dff1
jmp 0x2e013
movq 0x28(%rsp), %rdi
movslq (%rdi,%r14,4), %rcx
addq %rcx, %rax
movq %rbx, %r9
cmpq %rax, %rbx
cmovleq %rax, %r9
movq (%rsp), %rcx
cmovlq %r14, %rcx
movl %r9d, (%rdi,%r8,4)
movq 0x8(%rsp), %rax
movq %rcx, (%rax,%r8,8)
movl 0x4(%rbp), %eax
movq %r14, %rcx
shlq $0x4, %rcx
movl 0x4(%r15,%rcx), %ecx
subq %rcx, %rax
cmpq %r13, %rax
jg 0x2e010
cmpl %r9d, (%rdi,%r14,4)
cmovlq %r8, %r14
movq %r14, %rax
movslq %r9d, %rcx
cmpq %rcx, %rsi
cmovlq %r8, %rdx
cmovleq %rcx, %rsi
incq %r8
movq 0x70(%rsp), %r13
addq $0x10, %r13
addq $0x10, 0x30(%rsp)
movq %rax, %rcx
jmp 0x2dde8
xorl %r13d, %r13d
movq %r13, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testq %rdx, %rdx
js 0x2e071
movq 0x10(%rsp), %rcx
movq %rdx, (%rcx,%rax,8)
incq %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rdx,8), %rdx
jmp 0x2e055
movl %eax, %r13d
movq 0x10(%rsp), %rcx
leaq (%rcx,%r13,8), %rcx
addq $-0x8, %rcx
xorl %edx, %edx
movq %r15, %rsi
movq %rax, %rdi
addq %rdx, %rdi
je 0x2e03e
movq (%rcx,%rdx,8), %rdi
shlq $0x4, %rdi
movups (%r15,%rdi), %xmm0
movups %xmm0, (%rsi)
decq %rdx
addq $0x10, %rsi
jmp 0x2e086