// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: ff_lunalign(overlap_region*, double, double, long)
uint32_t inline ff_lunalign(overlap_region *z, double erate, double gap_rate, int64_t max_gap)
{
    if((z->w_list.n == 1) && (!(is_ualn_win(z->w_list.a[0]))) 
        && (z->w_list.a[0].x_start == ((int64_t)z->x_pos_s)) && (z->w_list.a[0].x_end == ((int64_t)z->x_pos_e)) 
            && (z->w_list.a[0].y_start == ((int64_t)z->y_pos_s)) && (z->w_list.a[0].y_end == ((int64_t)z->y_pos_e))) {
                return 1;
    }

    int64_t k, zwn = z->w_list.n, zq, zt, wq, wt, tot_e, tot_g, ql, tl; 

    // fprintf(stderr, "[M::%s]\n", __func__);
    
    zq = z->x_pos_s; zt = z->y_pos_s; tot_e = tot_g = 0; 
    for (k = 0; k < zwn; k++) {
        // fprintf(stderr, "[M::%s]\twk::%ld\tq::[%d, %d)\tt::[%d, %d)\terr::%d\n", __func__, 
        //     k, z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].y_start, z->w_list.a[k].y_end + 1, z->w_list.a[k].error);
        if(is_ualn_win(z->w_list.a[k])) continue;
        wq = z->w_list.a[k].x_start; 
        wt = z->w_list.a[k].y_start;

        if(wq != zq) tot_g += ((wq>=zq)?(wq-zq):(zq-wq));
        if(wt != zt) tot_g += ((wt>=zt)?(wt-zt):(zt-wt));

        zq = z->w_list.a[k].x_end + 1; 
        zt = z->w_list.a[k].y_end + 1;
        tot_e += z->w_list.a[k].error;

        // fprintf(stderr, "[M::%s]\twk::%ld\tq::[%d, %d)\tt::[%d, %d)\terr::%d\n", __func__, 
        //     k, z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].y_start, z->w_list.a[k].y_end + 1, z->w_list.a[k].error);
    }

    wq = z->x_pos_e + 1;
    wt = z->y_pos_e + 1;
    if(wq != zq) tot_g += ((wq>=zq)?(wq-zq):(zq-wq));
    if(wt != zt) tot_g += ((wt>=zt)?(wt-zt):(zt-wt));

    // fprintf(stderr, "[M::%s]\t%.*s(id::%u)\tq::[%u, %u)\t%.*s(id::%u)\tt::[%u, %u)\tre::%ld\trg::%ld\n", __func__, (int)Get_NAME_LENGTH(R_INF, z->x_id), Get_NAME(R_INF, z->x_id), z->x_id, z->x_pos_s, z->x_pos_e + 1, 
    // (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), z->y_id, z->y_pos_s, z->y_pos_e + 1, tot_e, tot_g);

    if(!tot_g) return 1;

    // fprintf(stderr, "-0-[M::%s]\n", __func__);

    if(tot_g > max_gap) return 0;

    // fprintf(stderr, "-1-[M::%s]\n", __func__);

    ql = z->x_pos_e + 1 - z->x_pos_s;
    tl = z->y_pos_e + 1 - z->y_pos_s;

    if((tot_g > (ql*gap_rate)) || (tot_g > (tl*gap_rate))) return 0;

    // fprintf(stderr, "-2-[M::%s]\n", __func__);

    tot_e += tot_g;

    if((tot_e > (ql*erate)) || (tot_e > (tl*erate))) return 0;

    // fprintf(stderr, "-3-[M::%s]\n", __func__);
    
    return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq 0x48(%rdi), %r10
cmpq $0x1, %r10
jne 0x98fea
movq 0x58(%rdi), %rax
cmpw $0x7fff, 0x14(%rax)     # imm = 0x7FFF
jne 0x98faa
cmpl $0x0, 0x1c(%rax)
jne 0x98faa
cmpw $0x0, 0x12(%rax)
js 0x98fe2
movslq (%rax), %rcx
movl 0x4(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98fe2
movslq 0x4(%rax), %rcx
movl 0x8(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98fe2
movslq 0x8(%rax), %rcx
movl 0x14(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98fe2
movslq 0xc(%rax), %rcx
movl 0x18(%rdi), %edx
movl $0x1, %eax
cmpq %rdx, %rcx
je 0x99100
movl 0x4(%rdi), %edx
movl 0x14(%rdi), %ecx
jmp 0x98ff6
movl 0x4(%rdi), %edx
movl 0x14(%rdi), %ecx
jl 0x99106
movq 0x58(%rdi), %rbx
xorl %r9d, %r9d
xorl %r8d, %r8d
movq %rcx, %rax
movq %rdx, %r11
movswq 0x14(%rbx), %r14
cmpq $0x7fff, %r14           # imm = 0x7FFF
jne 0x99021
cmpl $0x0, 0x1c(%rbx)
jne 0x99021
cmpw $0x0, 0x12(%rbx)
js 0x9905d
movslq (%rbx), %r15
subq %r15, %r11
je 0x99036
movq %r11, %r15
negq %r15
cmovsq %r11, %r15
addq %r15, %r9
movslq 0x8(%rbx), %r11
subq %r11, %rax
je 0x9904c
movq %rax, %r11
negq %r11
cmovsq %rax, %r11
addq %r11, %r9
movslq 0x4(%rbx), %r11
incq %r11
movslq 0xc(%rbx), %rax
incq %rax
addq %r14, %r8
addq $0x20, %rbx
decq %r10
jne 0x99006
movl 0x8(%rdi), %r10d
movl 0x18(%rdi), %edi
incl %r10d
incl %edi
subq %r10, %r11
je 0x99084
movq %r11, %rbx
negq %rbx
cmovsq %r11, %rbx
addq %rbx, %r9
subq %rdi, %rax
je 0x99096
movq %rax, %r11
negq %r11
cmovsq %rax, %r11
addq %r11, %r9
testq %r9, %r9
je 0x990fb
xorl %eax, %eax
cmpq %rsi, %r9
jg 0x99100
subl %edx, %r10d
cvtsi2sd %r9, %xmm4
cvtsi2sd %r10, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm4
ja 0x99100
subl %ecx, %edi
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm4
ja 0x99100
addq %r8, %r9
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm3
cmpnltpd %xmm1, %xmm3
cmpnltpd %xmm1, %xmm2
andpd %xmm3, %xmm2
movd %xmm2, %eax
andl $0x1, %eax
jmp 0x99100
movl $0x1, %eax
popq %rbx
popq %r14
popq %r15
retq
xorl %r8d, %r8d
movq %rdx, %r11
movq %rcx, %rax
xorl %r9d, %r9d
jmp 0x99066