// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
movq 0x48(%rdi), %rax
movq %rax, 0x88(%rsp)
testq %rax, %rax
je 0x5e6e4
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rcx
movq 0xf8(%rsp), %rbx
movl 0x10(%rdi), %eax
movq %rax, 0x20(%rsp)
movl 0x1c(%rdi), %eax
movq %rax, 0x70(%rsp)
movq %rbx, %rdi
movq %rcx, 0x28(%rsp)
callq 0x3fd1c
movq 0x28(%rsp), %r9
movq %rbx, %rdi
movq 0x48(%r9), %rsi
cmpq 0x10(%rbx), %rsi
jbe 0x5e6c3
movq %rsi, 0x10(%rdi)
movq (%rdi), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
testq %r14, %r14
je 0x5e6eb
movq (%r14), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x5e71c
xorl %eax, %eax
jmp 0x5ed96
testq %r15, %r15
je 0x5e70a
movq (%r15), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x5e71c
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq %r15, 0x60(%rsp)
movq %r14, 0x68(%rsp)
cmpq $0x0, 0x88(%rsp)
jle 0x5eb2d
movq $-0x80000000, %rsi      # imm = 0x80000000
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
movq $-0x80000000, 0x98(%rsp) # imm = 0x80000000
movq 0x58(%r9), %r10
movq %rax, 0xa0(%rsp)
shlq $0x5, %rax
movl 0x1c(%r10,%rax), %r11d
movslq (%r10,%rax), %r12
movslq 0x8(%r10,%rax), %r15
testq %r11, %r11
je 0x5eb0e
addq %rax, %r10
xorl %r14d, %r14d
movq %r10, 0x50(%rsp)
movq %r11, 0x48(%rsp)
movq 0x70(%r9), %rax
movl 0x18(%r10), %edx
addl %r14d, %edx
movzwl (%rax,%rdx,2), %r13d
movl %r13d, %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
cmpl $0x3fff, %r13d          # imm = 0x3FFF
ja 0x5e7d6
cmpq %r12, 0x98(%rsp)
jne 0x5e89d
cmpq %r15, %rsi
jne 0x5e89d
addl %ebx, %ecx
movl %ecx, %ebp
jmp 0x5eaed
testl %ecx, %ecx
je 0x5eaae
movl %ecx, %eax
movq %r12, %rdx
subq %rax, %rdx
movq %rdx, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, %rdx
subq %rax, %rdx
movq %rdx, 0x38(%rsp)
movq %r15, (%rsp)
cmpl 0x108(%rsp), %ecx
movq %rsi, 0x90(%rsp)
jbe 0x5e9ff
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %rbp
movq 0x80(%rsp), %r8
movl 0x100(%rsp), %r9d
movq %rsp, %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq 0x90(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x118(%rsp)
callq 0x5de1e
movq 0xd0(%rsp), %rsi
movq 0x88(%rsp), %r11
movq 0x90(%rsp), %r10
movq 0x68(%rsp), %r9
movq %rbp, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
jmp 0x5ea01
testl %ecx, %ecx
je 0x5eaeb
movl %ecx, %eax
movq %r12, %rdx
subq %rax, %rdx
movq %rdx, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, %rdx
subq %rax, %rdx
movq %rdx, 0x38(%rsp)
movq %r15, (%rsp)
xorl %ebp, %ebp
movl $0x0, %eax
cmpl 0x108(%rsp), %ecx
jbe 0x5e952
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %r13
movq 0x80(%rsp), %r8
movl 0x100(%rsp), %r9d
movq %rsp, %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
pushq 0x90(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x118(%rsp)
callq 0x5de1e
movq 0x88(%rsp), %r11
movq 0x90(%rsp), %r10
movq 0x68(%rsp), %r9
movq %r13, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
movzbl %al, %r13d
addq %r13, %r8
testb %r13b, %r13b
jne 0x5e967
testq %r8, %r8
jne 0x5eaed
movq 0x10(%rdi), %rax
cmpq %rax, 0x8(%rdi)
jne 0x5e9b5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rdi)
movq (%rdi), %rdi
shlq $0x4, %rsi
movq %r8, 0x58(%rsp)
callq 0x2370
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x58(%rsp), %r8
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movl %r13d, %edx
shll $0x1f, %edx
orl %r13d, %edx
movl %edx, (%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, 0x18(%rsp)
jmp 0x5eaed
xorl %eax, %eax
movzbl %al, %ebp
addq %rbp, %r8
testb %bpl, %bpl
jne 0x5ea15
testq %r8, %r8
jne 0x5eaae
movq 0x10(%rdi), %rax
cmpq %rax, 0x8(%rdi)
jne 0x5ea63
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rdi)
movq (%rdi), %rdi
shlq $0x4, %rsi
movq %r8, 0x58(%rsp)
callq 0x2370
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x58(%rsp), %r8
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movl %ebp, %edx
shll $0x1f, %edx
orl %ebp, %edx
movl %edx, (%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, 0x18(%rsp)
movq 0x90(%rsp), %rsi
shrl $0xe, %r13d
xorl %ebp, %ebp
cmpw $0x1, %r13w
movzwl %bx, %eax
movl $0x0, %ecx
cmoveq %rax, %rcx
addq %rcx, %r12
addq %r15, %rcx
cmpw $0x2, %r13w
movl $0x0, %r15d
cmoveq %rax, %r15
addq %rcx, %r15
cmpw $0x3, %r13w
cmovneq %rbp, %rax
addq %rax, %r12
jmp 0x5eafe
xorl %ebp, %ebp
addq %rbx, %r12
addq %rbx, %r15
movq %r12, 0x98(%rsp)
movq %r15, %rsi
incq %r14
movl %ebp, %ecx
cmpq %r14, %r11
jne 0x5e794
jmp 0x5eb10
movl %ecx, %ebp
movq 0xa0(%rsp), %rax
incq %rax
movl %ebp, %ecx
cmpq 0x88(%rsp), %rax
jne 0x5e75d
jmp 0x5eb41
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r8d, %r8d
movq $0x0, 0x18(%rsp)
testl %ebp, %ebp
je 0x5ebef
movl %ebp, %eax
movq %r12, %rcx
subq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, %rcx
subq %rax, %rcx
movq %rcx, 0x38(%rsp)
movq %r15, (%rsp)
cmpl 0x108(%rsp), %ebp
jbe 0x5ebde
movl 0x100(%rsp), %r9d
movq %rsp, %rax
leaq 0x38(%rsp), %r10
leaq 0x8(%rsp), %r11
leaq 0x10(%rsp), %rbx
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %r8, %r14
movq 0x80(%rsp), %r8
pushq %rax
pushq %r10
pushq %r11
pushq %rbx
pushq 0x90(%rsp)
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x118(%rsp)
callq 0x5de1e
movq %r14, %r8
movq 0x138(%rsp), %rdi
addq $0x40, %rsp
jmp 0x5ebe0
xorl %eax, %eax
movzbl %al, %ebx
addq %rbx, %r8
testb %bl, %bl
jne 0x5ebf9
testq %r8, %r8
je 0x5ebf9
movq 0x18(%rsp), %rcx
jmp 0x5ec7a
movq 0x10(%rdi), %rax
cmpq %rax, 0x8(%rdi)
jne 0x5ec34
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%rdi)
movq (%rdi), %rdi
shlq $0x4, %rsi
movq %r8, %r14
callq 0x2370
movq %r14, %r8
movq 0xf8(%rsp), %rdi
movq %rax, (%rdi)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rcx
movl %ebx, %edx
shll $0x1f, %edx
orl %ebx, %edx
movl %edx, (%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)
movq 0x18(%rsp), %rcx
adcq $0x0, %rcx
movl 0x110(%rsp), %esi
movq 0x8(%rdi), %r11
testq %r8, %r8
je 0x5ecf8
movq $-0x1, %r9
cmpq %r11, %r8
jae 0x5ed7a
testq %r11, %r11
jle 0x5ed70
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r8d, %r8d
movq (%rdi), %rax
testl $0x7fffffff, (%rax,%r10) # imm = 0x7FFFFFFF
je 0x5eced
leaq (%rax,%r10), %rcx
movq %r8, %rdx
shlq $0x4, %rdx
movups (%rcx), %xmm0
movups %xmm0, (%rax,%rdx)
movq (%rdi), %rax
movl 0xc(%rax,%rdx), %ecx
movl $0x1, %eax
cmpl %esi, %ecx
jbe 0x5ece7
movl %ecx, %eax
xorl %edx, %edx
divl %esi
cmpl $0x1, %edx
sbbl $-0x1, %eax
addq %rax, %r9
incq %r8
addq $0x10, %r10
decq %r11
jne 0x5ecac
jmp 0x5ed76
testq %rcx, %rcx
setne %al
cmpq %r11, %rcx
setb %cl
andb %al, %cl
movq $-0x1, %r9
cmpb $0x1, %cl
jne 0x5ed7a
testq %r11, %r11
jle 0x5ed70
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r8d, %r8d
movq (%rdi), %rax
movl 0x108(%rsp), %ecx
cmpl %ecx, 0xc(%rax,%r10)
jbe 0x5ed65
leaq (%rax,%r10), %rcx
movq %r8, %rdx
shlq $0x4, %rdx
movups (%rcx), %xmm0
movups %xmm0, (%rax,%rdx)
movq (%rdi), %rax
movl 0xc(%rax,%rdx), %ecx
movl $0x1, %eax
cmpl %esi, %ecx
jbe 0x5ed5f
movl %ecx, %eax
xorl %edx, %edx
divl %esi
cmpl $0x1, %edx
sbbl $-0x1, %eax
addq %rax, %r9
incq %r8
addq $0x10, %r10
decq %r11
jne 0x5ed20
jmp 0x5ed76
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %r8, 0x8(%rdi)
movl %esi, %r8d
movl $0x19, %esi
movl $0x1388, %edx           # imm = 0x1388
movl $0x1388, %ecx           # imm = 0x1388
callq 0x5e23e
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq