// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x1, 0x8(%rax)
jge 0x543fb
movq $0x0, -0x8(%rbp)
jmp 0x54a74
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jge 0x54480
movq -0xc8(%rbp), %rdi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0xc8(%rbp), %rsi
movq -0x40(%rbp), %rax
subq $0x1, %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xa95f0
movq %rax, -0x90(%rbp)
cmpq $-0x80000000, -0x90(%rbp) # imm = 0x80000000
jne 0x54470
jmp 0x54480
jmp 0x54472
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x54420
movq -0x40(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jl 0x5449d
movq -0xd0(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x54a74
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x540b0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rax
addq $0x18, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x226f0
movq -0xd8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0xd8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
movq $-0x1, (%rax)
movq -0xc8(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0xb8(%rbp), %rax
movl %ecx, (%rax)
movq $-0x1, -0xb0(%rbp)
movq $-0x1, -0xa8(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rdi
movq -0xd0(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq $0x0, -0x80(%rbp)
movq $0x0, -0x40(%rbp)
movq $-0x1, -0x88(%rbp)
movq -0x40(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jge 0x549b0
movq -0xc8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $-0x1, -0x78(%rbp)
movq $-0x1, -0x70(%rbp)
movq -0x40(%rbp), %rax
subq -0x80(%rbp), %rax
cmpq -0x20(%rbp), %rax
jle 0x545f4
movq -0x40(%rbp), %rax
subq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0x5470c
movq -0xc8(%rbp), %rdi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0xc8(%rbp), %rsi
movq -0x48(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xa95f0
movq %rax, -0x90(%rbp)
cmpq $-0x80000000, -0x90(%rbp) # imm = 0x80000000
jne 0x54650
jmp 0x5470c
movq -0xb8(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x60(%rbp), %rax
jle 0x546a2
movq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x68(%rbp)
jle 0x546a0
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x546d2
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x40(%rbp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x546d0
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
cmpq -0x18(%rbp), %rax
jle 0x546ce
jmp 0x5470c
jmp 0x546d0
jmp 0x546d2
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x546f9
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x546fb
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x54600
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x88(%rbp)
jl 0x54758
movq -0xc8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0xc8(%rbp), %rcx
movq -0x88(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq -0x28(%rbp), %rax
jle 0x54823
movq $-0x80000000, -0x98(%rbp) # imm = 0x80000000
movq $-0x1, -0x88(%rbp)
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq -0x80(%rbp), %rcx
movb %al, -0xd9(%rbp)
jl 0x547c6
movq -0xc8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0xc8(%rbp), %rcx
movq -0x48(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq -0x28(%rbp), %rax
setle %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x547d2
jmp 0x54821
movq -0x98(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq -0x48(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jge 0x5480e
movq -0xb8(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x54810
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x5477a
jmp 0x54823
cmpq $0x0, -0x88(%rbp)
jl 0x548d4
movq -0x88(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x548d4
movq -0xc8(%rbp), %rdi
movq -0x40(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0xc8(%rbp), %rsi
movq -0x88(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xa95f0
movq %rax, -0xa0(%rbp)
cmpq $-0x80000000, -0xa0(%rbp) # imm = 0x80000000
je 0x548d2
movq -0x60(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq -0xb8(%rbp), %rdx
movq -0x88(%rbp), %rsi
movslq (%rdx,%rsi,4), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jge 0x548d2
movq -0xa0(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq -0x88(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x548d4
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0xb8(%rbp), %rax
movq -0x40(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x88(%rbp)
jl 0x54959
movq -0xc8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0xc8(%rbp), %rcx
movq -0x88(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq -0x28(%rbp), %rax
jg 0x54964
movq -0xb8(%rbp), %rax
movq -0x88(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xb8(%rbp), %rcx
movq -0x40(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x54964
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xb8(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
cmpq -0xb0(%rbp), %rax
jle 0x5499d
movq -0xb8(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x5499f
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x54598
movl $0x0, -0xbc(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0x549fe
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xbc(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x549c5
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0xbc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x54a69
movq -0xc8(%rbp), %rcx
movq -0x58(%rbp), %rax
movslq -0xbc(%rbp), %rdx
subq -0x40(%rbp), %rdx
subq $0x1, %rdx
movq (%rax,%rdx,8), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0xc8(%rbp), %rax
movq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x54a06
movslq -0xbc(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)