// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_affine_traceback(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, char*, char*, unsigned long, unsigned long*, unsigned long, Candidates_list*, unsigned int, unsigned int, unsigned int)
uint64_t gen_affine_traceback(overlap_region *o, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, char *tstr, uint64_t ql, 
uint64_t *qhp, uint64_t qhp_l, Candidates_list *ac, uint32_t hpc_max, uint32_t min_ach, uint32_t block)
{
    if(o->w_list.n <= 0) return 0;
    int64_t nw = o->w_list.n, snw, k, t, qi, ti, ci, p_qi, p_ti, cn, qs, qe, ts, te, tl; 
    window_list *z; uint8_t c; uint32_t cl, pcl, id = o->y_id, rev = o->y_pos_strand; k_mer_hit *p;
    uint64_t hm, aocc = 0, bocc = 0; qi = ti = 0; p_qi = p_ti = INT32_MIN; pcl = 0;
    clear_Candidates_list(ac); 
    if(o->w_list.n > (uint64_t)ac->size) {
        ac->size = o->w_list.n; REALLOC(ac->list, ac->size);
    }

    if(hpc_g) tl = hpc_len(*hpc_g, id);
    else if(uref) tl = uref->ug->u.a[id].len;
    else tl = Get_READ_LENGTH((*rref), id);
    for (k = 0; k < nw; k++) {
        z = &(o->w_list.a[k]); ci = 0; cn = z->clen;
        qi = z->x_start; ti = z->y_start;
        for (ci = 0; ci < cn; ci++) {
            get_cigar_cell(z, &(o->w_list), ci, &c, &cl);
            if (c == 0) { //match   
                if((p_qi == qi) && (p_ti == ti)) {
                    pcl += cl;
                } else {
                    ///push
                    if(pcl > 0) {
                        hm = 0; qs = qi - pcl; qe = qi; ts = ti - pcl; te = ti;
                        if(pcl > min_ach) hm = trim_hpc(uref, hpc_g, rref, qstr, tstr, hpc_max, ql, tl, id, rev, &qs, &qe, &ts, &te);
                        if(hm) aocc++;
                        if(hm || aocc == 0) {
                            cl_pushp(k_mer_hit, *ac, &p);
                            p->readID = p->strand = !!hm;
                            p->cnt = qe - qs; p->self_offset = qe; p->offset = te;
                            if(p->cnt > min_ach) bocc++;
                        }
                    }
                    ///push
                    pcl = 0;
                }
                qi += cl; ti += cl;
                p_qi = qi; p_ti = ti;
            } else {
                ///push
                if(pcl > 0) {
                    hm = 0; qs = qi - pcl; qe = qi; ts = ti - pcl; te = ti;
                    if(pcl > min_ach) hm = trim_hpc(uref, hpc_g, rref, qstr, tstr, hpc_max, ql, tl, id, rev, &qs, &qe, &ts, &te);
                    if(hm) aocc++;
                    if(hm || aocc == 0) {
                        cl_pushp(k_mer_hit, *ac, &p);
                        p->readID = p->strand = !!hm;
                        p->cnt = qe - qs; p->self_offset = qe; p->offset = te;
                        if(p->cnt > min_ach) bocc++;
                    }
                }
                pcl = 0;
                ///push
                if (c == 1) {
                    qi += cl; ti += cl; 
                } if (c == 2) {///t has more bases than p
                    ti += cl; 
                } if (c == 3) {///p has more bases than t
                    qi += cl; 
                }
            } 
        }
    }

    ///push
    if(pcl > 0) {
        hm = 0; qs = qi - pcl; qe = qi; ts = ti - pcl; te = ti;
        if(pcl > min_ach) hm = trim_hpc(uref, hpc_g, rref, qstr, tstr, hpc_max, ql, tl, id, rev, &qs, &qe, &ts, &te);
        if(hm) aocc++;
        if(hm || aocc == 0) {
            cl_pushp(k_mer_hit, *ac, &p);
            p->readID = p->strand = !!hm;
            p->cnt = qe - qs; p->self_offset = qe; p->offset = te;
            if(p->cnt > min_ach) bocc++;
        }
    }
    ///push

    nw = ac->length; snw = -1;
    if(aocc > 0) {
        if(aocc < (uint64_t)ac->length) {
            for (k = t = snw = 0; k < nw; k++) {
                if(ac->list[k].readID) {
                    ac->list[t] = ac->list[k]; 
                    if(ac->list[t].cnt <= block) snw++;
                    else snw += (ac->list[t].cnt/block) + (((ac->list[t].cnt%block) > 0)?1:0);
                    t++;
                }
            }
            ac->length = t;
        }
    } else if (bocc > 0) {
        if(bocc < (uint64_t)ac->length) {
            for (k = t = snw = 0; k < nw; k++) {
                if(ac->list[k].cnt > min_ach) {
                    ac->list[t] = ac->list[k]; 
                    if(ac->list[t].cnt <= block) snw++;
                    else snw += (ac->list[t].cnt/block) + (((ac->list[t].cnt%block) > 0)?1:0);
                    t++;
                }
            }
            ac->length = t;
        }
    }
    nw = ac->length;
    gen_affine_traceback_dp(ac, 25, 5000, 5000, block, snw);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x48(%rdi), %rax
movq %rax, 0x88(%rsp)
testq %rax, %rax
je 0x5defd
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rcx
movq 0xf8(%rsp), %rbx
movl 0x10(%rdi), %eax
movq %rax, 0x18(%rsp)
movl 0x1c(%rdi), %eax
movq %rax, 0x70(%rsp)
movq %rbx, %rdi
movq %rcx, 0x20(%rsp)
callq 0x3f1bd
movq 0x20(%rsp), %r9
movq %rbx, %rdi
movq 0x48(%r9), %rsi
cmpq 0x10(%rbx), %rsi
jbe 0x5dedc
movq %rsi, 0x10(%rdi)
movq (%rdi), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x20(%rsp), %r9
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
testq %r15, %r15
je 0x5df04
movq (%r15), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x5df36
xorl %eax, %eax
jmp 0x5e5e6
testq %r12, %r12
je 0x5df24
movq (%r12), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x5df36
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
movl 0x110(%rsp), %ebx
movq $-0x1, %r14
cmpq $0x0, 0x88(%rsp)
jle 0x5e5c7
movq %r12, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $0x0, 0x40(%rsp)
xorl %r8d, %r8d
xorl %ebp, %ebp
xorl %eax, %eax
movq $-0x80000000, 0x58(%rsp) # imm = 0x80000000
movq 0x58(%r9), %r10
movq %rax, 0x98(%rsp)
shlq $0x5, %rax
movl 0x1c(%r10,%rax), %r11d
movslq (%r10,%rax), %rbx
movslq 0x8(%r10,%rax), %r12
testq %r11, %r11
je 0x5e356
addq %rax, %r10
movl %ebp, %eax
xorl %r14d, %r14d
movq %r10, 0x50(%rsp)
movq %r11, 0x48(%rsp)
movq 0x70(%r9), %rcx
movl 0x18(%r10), %edx
addl %r14d, %edx
movzwl (%rcx,%rdx,2), %r13d
movl %r13d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl $0x3fff, %r13d          # imm = 0x3FFF
ja 0x5dffc
cmpq %rbx, 0x58(%rsp)
jne 0x5e0d2
cmpq %r12, %rsi
jne 0x5e0d2
addl %r15d, %eax
movl %eax, %ebp
jmp 0x5e33a
testl %eax, %eax
je 0x5e251
movl %eax, %ecx
movq %rbx, %rdx
subq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r12, %rdx
subq %rcx, %rdx
movq %rdx, 0x30(%rsp)
movq %r12, (%rsp)
cmpl 0x108(%rsp), %eax
movq %rsi, 0x90(%rsp)
jbe 0x5e19a
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %rbp
movq 0x80(%rsp), %r8
movl 0x100(%rsp), %r9d
movq %rsp, %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq 0x90(%rsp)
pushq 0x40(%rsp)
pushq 0x58(%rsp)
pushq 0x118(%rsp)
callq 0x5d640
movq 0xd0(%rsp), %rsi
movq 0x88(%rsp), %r11
movq 0x90(%rsp), %r10
movq 0x60(%rsp), %r9
movq %rbp, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
testq %rax, %rax
je 0x5e19a
incq %r8
movb $0x1, %bpl
jmp 0x5e1a8
xorl %ebp, %ebp
testl %eax, %eax
je 0x5e33a
movl %eax, %ecx
movq %rbx, %rdx
subq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r12, %rdx
subq %rcx, %rdx
movq %rdx, 0x30(%rsp)
movq %r12, (%rsp)
cmpl 0x108(%rsp), %eax
jbe 0x5e28e
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %r13
movq 0x80(%rsp), %r8
movl 0x100(%rsp), %r9d
movq %rsp, %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq 0x90(%rsp)
pushq 0x40(%rsp)
pushq 0x58(%rsp)
pushq 0x118(%rsp)
callq 0x5d640
movq 0x88(%rsp), %r11
movq 0x90(%rsp), %r10
movq 0x60(%rsp), %r9
movq %r13, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
testq %rax, %rax
je 0x5e28e
incq %r8
movb $0x1, %sil
jmp 0x5e29f
testq %r8, %r8
jne 0x5e251
xorl %r8d, %r8d
xorl %ebp, %ebp
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
cmpq 0x10(%rdi), %rcx
jne 0x5e209
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rdi)
shlq $0x4, %rsi
movq %rax, %rdi
movq %rbp, 0xa0(%rsp)
movq %r8, %rbp
callq 0x2370
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x20(%rsp), %r9
movq %rbp, %r8
movq 0xa0(%rsp), %rbp
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movzbl %bpl, %edx
movl %edx, %esi
shll $0x1f, %esi
orl %edx, %esi
movl %esi, (%rax,%rcx)
movl 0x8(%rsp), %edx
movl %edx, %esi
subl 0x10(%rsp), %esi
movl %esi, 0xc(%rax,%rcx)
movl %edx, 0x8(%rax,%rcx)
movl (%rsp), %edx
movl %edx, 0x4(%rax,%rcx)
cmpl %esi, 0x108(%rsp)
adcq $0x0, 0x40(%rsp)
movq 0x90(%rsp), %rsi
shrl $0xe, %r13d
xorl %ebp, %ebp
cmpw $0x1, %r13w
movzwl %r15w, %eax
movl $0x0, %ecx
cmoveq %rax, %rcx
addq %rcx, %r12
cmpw $0x2, %r13w
movl $0x0, %edx
cmoveq %rax, %rdx
addq %rdx, %r12
cmpw $0x3, %r13w
cmoveq %rax, %rcx
addq %rcx, %rbx
jmp 0x5e348
xorl %esi, %esi
testq %r8, %r8
je 0x5e29c
xorl %ebp, %ebp
jmp 0x5e33a
xorl %r8d, %r8d
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
cmpq 0x10(%rdi), %rcx
jne 0x5e2fa
movq %rsi, 0x58(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rdi)
shlq $0x4, %rsi
movq %rax, %rdi
movq %r8, %r13
callq 0x2370
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x20(%rsp), %r9
movq %r13, %r8
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movzbl %sil, %edx
movl %edx, %esi
shll $0x1f, %esi
orl %edx, %esi
movl %esi, (%rax,%rcx)
movl 0x8(%rsp), %edx
movl %edx, %esi
subl 0x10(%rsp), %esi
movl %esi, 0xc(%rax,%rcx)
movl %edx, 0x8(%rax,%rcx)
movl (%rsp), %edx
movl %edx, 0x4(%rax,%rcx)
cmpl %esi, 0x108(%rsp)
adcq $0x0, 0x40(%rsp)
addq %r15, %rbx
addq %r15, %r12
movq %rbx, 0x58(%rsp)
movq %r12, %rsi
incq %r14
movl %ebp, %eax
cmpq %r14, %r11
jne 0x5dfbb
movq 0x98(%rsp), %rax
incq %rax
cmpq 0x88(%rsp), %rax
jne 0x5df82
testl %ebp, %ebp
je 0x5e434
movl %ebp, %eax
movq %rbx, %rcx
subq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r12, %rcx
subq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq %r12, (%rsp)
cmpl 0x108(%rsp), %ebp
movq $-0x1, %r14
jbe 0x5e41b
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %rbx
movq 0x80(%rsp), %r8
movl 0x100(%rsp), %r9d
movq %rsp, %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq 0x90(%rsp)
pushq 0x40(%rsp)
pushq 0x58(%rsp)
pushq 0x118(%rsp)
callq 0x5d640
movq %rbx, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
testq %rax, %rax
je 0x5e41b
incq %r8
movb $0x1, %bl
jmp 0x5e44a
testq %r8, %r8
je 0x5e445
leaq 0x8(%rdi), %rsi
movq 0x8(%rdi), %r11
movl 0x110(%rsp), %ebx
jmp 0x5e4e2
movq $-0x1, %r14
movq 0x40(%rsp), %r15
jmp 0x5e4ce
xorl %r8d, %r8d
xorl %ebx, %ebx
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
cmpq 0x10(%rdi), %rcx
movq 0x40(%rsp), %r15
jne 0x5e491
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x10(%rdi)
shlq $0x4, %rsi
movq %rax, %rdi
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movzbl %bl, %edx
movl %edx, %esi
shll $0x1f, %esi
orl %edx, %esi
movl %esi, (%rax,%rcx)
movl 0x8(%rsp), %edx
movl %edx, %esi
subl 0x10(%rsp), %esi
movl %esi, 0xc(%rax,%rcx)
movl %edx, 0x8(%rax,%rcx)
movl (%rsp), %edx
movl %edx, 0x4(%rax,%rcx)
cmpl %esi, 0x108(%rsp)
adcq $0x0, %r15
leaq 0x8(%rdi), %rsi
movq 0x8(%rdi), %r11
testq %r8, %r8
movl 0x110(%rsp), %ebx
je 0x5e54b
cmpq %r11, %r8
jae 0x5e5c7
testq %r11, %r11
jle 0x5e5be
movq (%rdi), %r10
xorl %r9d, %r9d
xorl %r14d, %r14d
xorl %r8d, %r8d
testl $0x7fffffff, (%r10,%r9) # imm = 0x7FFFFFFF
je 0x5e540
leaq (%r10,%r9), %rax
movq %r8, %rcx
shlq $0x4, %rcx
movups (%rax), %xmm0
movups %xmm0, (%r10,%rcx)
movq (%rdi), %r10
movl 0xc(%r10,%rcx), %ecx
movl $0x1, %eax
cmpl %ebx, %ecx
jbe 0x5e53a
movl %ecx, %eax
xorl %edx, %edx
divl %ebx
cmpl $0x1, %edx
sbbl $-0x1, %eax
addq %rax, %r14
incq %r8
addq $0x10, %r9
decq %r11
jne 0x5e500
jmp 0x5e5c4
testq %r15, %r15
setne %al
cmpq %r11, %r15
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5e5c7
testq %r11, %r11
jle 0x5e5be
movq (%rdi), %r10
xorl %r9d, %r9d
xorl %r14d, %r14d
xorl %r8d, %r8d
movl 0x108(%rsp), %eax
cmpl %eax, 0xc(%r10,%r9)
jbe 0x5e5b3
leaq (%r10,%r9), %rax
movq %r8, %rcx
shlq $0x4, %rcx
movups (%rax), %xmm0
movups %xmm0, (%r10,%rcx)
movq (%rdi), %r10
movl 0xc(%r10,%rcx), %ecx
movl $0x1, %eax
cmpl %ebx, %ecx
jbe 0x5e5ad
movl %ecx, %eax
xorl %edx, %edx
divl %ebx
cmpl $0x1, %edx
sbbl $-0x1, %eax
addq %rax, %r14
incq %r8
addq $0x10, %r9
decq %r11
jne 0x5e56f
jmp 0x5e5c4
xorl %r8d, %r8d
xorl %r14d, %r14d
movq %r8, (%rsi)
movl %ebx, %r8d
movl $0x19, %esi
movl $0x1388, %edx           # imm = 0x1388
movl $0x1388, %ecx           # imm = 0x1388
movq %r14, %r9
callq 0x5da74
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq