// File: /workspace/github_cpp_makefile_star2_O1/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, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
jle 0x5e5cb
cmpq $0x1, %rax
setne %cl
je 0x5e5cd
movq %rdi, %rbp
movq (%rdi), %rdx
movl 0x18(%rdx), %esi
movl 0x1c(%rdx), %edi
subl %edi, %esi
cmpl 0x8(%rdx), %esi
jb 0x5e2c3
addq $0x2c, %rdx
movl $0x2, %esi
movl -0x18(%rdx), %r10d
subl %edi, %r10d
cmpl -0x28(%rdx), %r10d
jb 0x5e2c3
cmpq %rsi, %rax
je 0x5e5cd
setg %cl
movl -0x4(%rdx), %r10d
movl (%rdx), %edi
subl %edi, %r10d
leaq 0x10(%rdx), %r11
incq %rsi
cmpl -0x14(%rdx), %r10d
movq %r11, %rdx
jae 0x5e291
testb $0x1, %cl
je 0x5e5cd
movq %rbp, %rdi
movq %r8, %rsi
movq %r9, %rdx
callq 0x5e0e4
movq (%rbp), %rbx
movq 0x8(%rbp), %r12
leaq 0x18(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3f828
movq 0x40(%rbp), %rdi
movq 0x18(%rbp), %r14
movq 0x20(%rbp), %r15
movq $0x0, (%rdi)
movq $-0x1, (%r15)
movl 0xc(%rbx), %eax
movl %eax, (%r14)
leaq (,%r12,8), %rdx
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
callq 0x20e0
movq %r12, 0x40(%rsp)
testq %r12, %r12
jle 0x5e5dc
leaq -0x8(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq -0xc(%rbx), %rax
movq %rax, 0x30(%rsp)
movq $-0x1, %rsi
xorl %edi, %edi
movq $-0x1, %rax
movq $-0x1, %r9
xorl %r10d, %r10d
movq %rbx, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movq %rsi, 0x58(%rsp)
movq %r10, %rcx
movq %rdi, %rdx
shlq $0x4, %rdx
leaq (%rbx,%rdx), %rsi
movq %rsi, 0x8(%rsp)
movl 0xc(%rbx,%rdx), %r13d
movq %rdi, %rdx
subq %r10, %rdx
movq %rdi, %r10
movq 0x48(%rsp), %rsi
subq %rsi, %r10
cmpq %rsi, %rdx
cmovleq %rcx, %r10
leaq -0x1(%rdi), %r11
movq %r13, %r8
movq $-0x1, 0x10(%rsp)
movq %r11, %r15
cmpq %r10, %rdi
jle 0x5e46c
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %edx
subl %r13d, %edx
movslq %edi, %rsi
movq $-0x1, 0x10(%rsp)
movq 0x38(%rsp), %rbp
movq %r11, %rbx
xorl %r12d, %r12d
movq %r13, %r8
movq $-0x80000000, %r15      # imm = 0x80000000
cmpl (%rbp), %edx
jb 0x5e3fd
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %ecx
subl %r13d, %ecx
cmpl -0x4(%rbp), %ecx
jb 0x5e3fd
movq %r13, %r15
cmpq $-0x80000000, %r15      # imm = 0x80000000
je 0x5e469
movslq (%r14,%rbx,4), %rcx
addq %rcx, %r15
cmpq %r8, %r15
jle 0x5e427
xorl %ecx, %ecx
testq %r12, %r12
setg %cl
subq %rcx, %r12
movq %r15, %r8
movq %rbx, 0x10(%rsp)
jmp 0x5e43c
movq 0x20(%rsp), %rcx
cmpq %rsi, (%rcx,%rbx,8)
jne 0x5e43c
cmpq 0x60(%rsp), %r12
jge 0x5e469
incq %r12
movq 0x28(%rsp), %rcx
movq (%rcx,%rbx,8), %r15
testq %r15, %r15
js 0x5e453
movq 0x20(%rsp), %rcx
movq %rdi, (%rcx,%r15,8)
leaq -0x1(%rbx), %r15
addq $-0x10, %rbp
cmpq %r10, %rbx
movq %r15, %rbx
jg 0x5e3de
jmp 0x5e46c
movq %rbx, %r15
testq %r9, %r9
movq 0x50(%rsp), %rbp
js 0x5e496
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %ecx
movq %r9, %rdx
shlq $0x4, %rdx
movq 0x18(%rsp), %rsi
movl 0x4(%rsi,%rdx), %edx
subq %rdx, %rcx
cmpq %rbp, %rcx
jle 0x5e4e2
movq $-0x1, %r9
cmpq %r10, %rdi
jle 0x5e4e2
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %edx
movq $-0x80000000, %rsi      # imm = 0x80000000
movq 0x30(%rsp), %rbx
movl (%rbx), %ecx
movq %rdx, %r12
subq %rcx, %r12
cmpq %rbp, %r12
jg 0x5e4e2
movslq (%r14,%r11,4), %rcx
cmpq %rcx, %rsi
cmovlq %r11, %r9
cmovleq %rcx, %rsi
leaq -0x1(%r11), %rcx
addq $-0x10, %rbx
cmpq %r10, %r11
movq %rcx, %r11
jg 0x5e4b6
testq %r9, %r9
sets %cl
cmpq %r15, %r9
setge %dl
orb %cl, %dl
jne 0x5e55c
movq %r9, %rdx
shlq $0x4, %rdx
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %ecx
subl %r13d, %ecx
movq $-0x80000000, %rsi      # imm = 0x80000000
movq 0x18(%rsp), %rbx
cmpl 0x8(%rbx,%rdx), %ecx
movq 0x28(%rsp), %r15
jb 0x5e53a
addq %rbx, %rdx
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %ecx
subl %r13d, %ecx
cmpl 0x4(%rdx), %ecx
movq $-0x80000000, %rcx      # imm = 0x80000000
cmovbq %rcx, %r13
movq %r13, %rsi
cmpq $-0x80000000, %rsi      # imm = 0x80000000
je 0x5e566
movslq (%r14,%r9,4), %rcx
addq %rcx, %rsi
cmpq %rsi, %r8
cmovleq %rsi, %r8
movq 0x10(%rsp), %rcx
cmovlq %r9, %rcx
jmp 0x5e56b
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x10(%rsp), %rcx
movl %r8d, (%r14,%rdi,4)
movq %rcx, (%r15,%rdi,8)
testq %r9, %r9
movq 0x58(%rsp), %rsi
js 0x5e59e
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %ecx
movq %r9, %rdx
shlq $0x4, %rdx
movl 0x4(%rbx,%rdx), %edx
subq %rdx, %rcx
cmpq %rbp, %rcx
jg 0x5e5a1
cmpl %r8d, (%r14,%r9,4)
jge 0x5e5a1
movq %rdi, %r9
movslq %r8d, %rcx
cmpq %rcx, %rsi
cmovlq %rdi, %rax
cmovleq %rcx, %rsi
incq %rdi
addq $0x10, 0x38(%rsp)
addq $0x10, 0x30(%rsp)
cmpq 0x40(%rsp), %rdi
jne 0x5e368
jmp 0x5e5e3
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, %rax
testq %rax, %rax
movq 0x20(%rsp), %rdi
movl $0x0, %ecx
js 0x5e604
xorl %ecx, %ecx
movq %rax, (%rdi,%rcx,8)
incq %rcx
movq (%r15,%rax,8), %rax
testq %rax, %rax
jns 0x5e5f4
movl %ecx, %eax
testl %ecx, %ecx
je 0x5e5cd
movq %rbx, %rcx
movq %rax, %rdx
movq -0x8(%rdi,%rdx,8), %rsi
shlq $0x4, %rsi
movups (%rbx,%rsi), %xmm0
movups %xmm0, (%rcx)
addq $0x10, %rcx
decq %rdx
jne 0x5e610
jmp 0x5e5cd