// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rcx, 0x30(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, 0x58(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
jle 0x5de30
movl $0x1, %ebp
cmpq $0x1, %rax
je 0x5de32
movq %rdi, %r13
movq (%rdi), %rcx
addq $0x1c, %rcx
movl $0x1, %edx
movl -0x4(%rcx), %edi
movl (%rcx), %esi
subl %esi, %edi
cmpl -0x14(%rcx), %edi
jb 0x5dae6
movl -0x8(%rcx), %edi
subl %esi, %edi
cmpl -0x18(%rcx), %edi
jb 0x5dae6
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %rax
jne 0x5dabc
movq %rax, %rbp
jmp 0x5de32
movq %r13, %rdi
movq %r8, %rsi
movq %r9, %rdx
callq 0x5d90c
movq (%r13), %rbx
movq 0x8(%r13), %r12
leaq 0x18(%r13), %rdi
xorl %ebp, %ebp
movq %r12, %rsi
xorl %edx, %edx
callq 0x3ecb0
movq 0x40(%r13), %rdi
movq 0x18(%r13), %r14
movq 0x20(%r13), %r15
movq $0x0, (%rdi)
movq $-0x1, (%r15)
movl 0xc(%rbx), %eax
movl %eax, (%r14)
leaq (,%r12,8), %rdx
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
callq 0x20e0
movq %r12, 0x48(%rsp)
testq %r12, %r12
jle 0x5de32
leaq -0x8(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq -0xc(%rbx), %rax
movq %rax, 0x38(%rsp)
movq $-0x1, %rsi
xorl %edi, %edi
movq $-0x1, %rax
movq $-0x1, %r9
xorl %r8d, %r8d
movq %rbx, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %rsi, 0x10(%rsp)
movq %r8, %rcx
movq %rdi, %rdx
shlq $0x4, %rdx
leaq (%rbx,%rdx), %r10
movl 0xc(%rbx,%rdx), %ebp
movq %rdi, %rdx
subq %r8, %rdx
movq %rdi, %r8
movq 0x50(%rsp), %rsi
subq %rsi, %r8
cmpq %rsi, %rdx
cmovleq %rcx, %r8
leaq -0x1(%rdi), %r12
movq %rbp, %r11
movq $-0x1, 0x8(%rsp)
movq %r12, %rdx
cmpq %r8, %rdi
jle 0x5dc6e
movl 0x8(%r10), %r13d
subl %ebp, %r13d
movslq %edi, %rcx
movq %rcx, 0x60(%rsp)
movq $-0x1, 0x8(%rsp)
movq 0x40(%rsp), %rsi
movq %r12, %rbx
xorl %r15d, %r15d
movq %rbp, %r11
cmpl (%rsi), %r13d
jb 0x5dc6b
movl 0x4(%r10), %edx
subl %ebp, %edx
cmpl -0x4(%rsi), %edx
jb 0x5dc6b
movslq (%r14,%rbx,4), %rdx
addq %rbp, %rdx
cmpq %r11, %rdx
jle 0x5dc28
xorl %r11d, %r11d
testq %r15, %r15
setg %r11b
subq %r11, %r15
movq %rdx, %r11
movq %rbx, 0x8(%rsp)
jmp 0x5dc42
movq 0x28(%rsp), %rcx
movq 0x60(%rsp), %rdx
cmpq %rdx, (%rcx,%rbx,8)
jne 0x5dc42
cmpq 0x58(%rsp), %r15
jge 0x5dc6b
incq %r15
movq 0x20(%rsp), %rcx
movq (%rcx,%rbx,8), %rdx
testq %rdx, %rdx
js 0x5dc59
movq 0x28(%rsp), %rcx
movq %rdi, (%rcx,%rdx,8)
leaq -0x1(%rbx), %rdx
addq $-0x10, %rsi
cmpq %r8, %rbx
movq %rdx, %rbx
jg 0x5dbf5
jmp 0x5dc6e
movq %rbx, %rdx
testq %r9, %r9
js 0x5dc91
movl 0x4(%r10), %ecx
movq %r9, %rsi
shlq $0x4, %rsi
movq 0x18(%rsp), %rbx
movl 0x4(%rbx,%rsi), %esi
subq %rsi, %rcx
cmpq 0x30(%rsp), %rcx
jle 0x5dce1
cmpq %r8, %rdi
jle 0x5dd28
movl 0x4(%r10), %ecx
movq $-0x1, %r9
movq $-0x80000000, %rsi      # imm = 0x80000000
movq 0x38(%rsp), %rbx
movl (%rbx), %r15d
movq %rcx, %r13
subq %r15, %r13
cmpq 0x30(%rsp), %r13
jg 0x5dce1
movslq (%r14,%r12,4), %r15
cmpq %r15, %rsi
cmovlq %r12, %r9
cmovleq %r15, %rsi
leaq -0x1(%r12), %r15
addq $-0x10, %rbx
cmpq %r8, %r12
movq %r15, %r12
jg 0x5dcb1
testq %r9, %r9
sets %cl
cmpq %rdx, %r9
setge %dl
orb %cl, %dl
jne 0x5dd49
movq %r9, %rcx
shlq $0x4, %rcx
movl 0x8(%r10), %edx
subl %ebp, %edx
movq 0x18(%rsp), %rbx
cmpl 0x8(%rbx,%rcx), %edx
movq 0x20(%rsp), %r15
jb 0x5dd1c
addq %rbx, %rcx
movl 0x4(%r10), %edx
subl %ebp, %edx
cmpl 0x4(%rcx), %edx
jae 0x5dd6f
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rcx
jmp 0x5dd8b
movl %r11d, (%r14,%rdi,4)
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %rcx
movq %rcx, (%r15,%rdi,8)
movq %rdi, %rcx
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rsi
jmp 0x5ddb7
movl %r11d, (%r14,%rdi,4)
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %rcx
movq %rcx, (%r15,%rdi,8)
movq %rdi, %rcx
testq %r9, %r9
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rsi
jns 0x5dd93
jmp 0x5ddb7
movslq (%r14,%r9,4), %rcx
addq %rbp, %rcx
cmpq %rcx, %r11
cmovleq %rcx, %r11
movq 0x8(%rsp), %rcx
cmovlq %r9, %rcx
movq 0x10(%rsp), %rsi
movl %r11d, (%r14,%rdi,4)
movq %rcx, (%r15,%rdi,8)
movl 0x4(%r10), %ecx
movq %r9, %rdx
shlq $0x4, %rdx
movl 0x4(%rbx,%rdx), %edx
subq %rdx, %rcx
cmpq 0x30(%rsp), %rcx
jg 0x5ddb4
cmpl %r11d, (%r14,%r9,4)
cmovlq %rdi, %r9
movq %r9, %rcx
movslq %r11d, %rdx
cmpq %rdx, %rsi
cmovlq %rdi, %rax
cmovleq %rdx, %rsi
incq %rdi
addq $0x10, 0x40(%rsp)
addq $0x10, 0x38(%rsp)
movq %rcx, %r9
cmpq 0x48(%rsp), %rdi
jne 0x5db84
testq %rax, %rax
js 0x5de30
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
incq %rcx
movq (%r15,%rax,8), %rax
testq %rax, %rax
jns 0x5ddee
movl %ecx, %ebp
movq %rbp, %rax
negq %rax
leaq (%rdx,%rbp,8), %rcx
addq $-0x8, %rcx
xorl %edx, %edx
movq %rbx, %rsi
movq (%rcx,%rdx,8), %rdi
shlq $0x4, %rdi
movups (%rbx,%rdi), %xmm0
movups %xmm0, (%rsi)
decq %rdx
addq $0x10, %rsi
cmpq %rdx, %rax
jne 0x5de13
jmp 0x5de32
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq