// File: /workspace/github_cpp_makefile_star2_O1/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:
movq 0x48(%rdi), %r10
cmpq $0x1, %r10
jne 0x98463
movq 0x58(%rdi), %rax
cmpw $0x7fff, 0x14(%rax)     # imm = 0x7FFF
jne 0x9842b
cmpl $0x0, 0x1c(%rax)
jne 0x9842b
cmpw $0x0, 0x12(%rax)
js 0x98463
movslq (%rax), %rcx
movl 0x4(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98463
movslq 0x4(%rax), %rcx
movl 0x8(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98463
movslq 0x8(%rax), %rcx
movl 0x14(%rdi), %edx
cmpq %rdx, %rcx
jne 0x98463
movslq 0xc(%rax), %rcx
movl 0x18(%rdi), %edx
movl $0x1, %eax
cmpq %rdx, %rcx
je 0x9858a
pushq %r15
pushq %r14
pushq %rbx
movl 0x4(%rdi), %r9d
movl 0x14(%rdi), %ecx
testq %r10, %r10
jle 0x984e7
movq 0x58(%rdi), %r11
xorl %edx, %edx
xorl %r8d, %r8d
movq %rcx, %rax
movq %r9, %rbx
movswq 0x14(%r11), %r14
cmpq $0x7fff, %r14           # imm = 0x7FFF
jne 0x984a0
cmpl $0x0, 0x1c(%r11)
jne 0x984a0
cmpw $0x0, 0x12(%r11)
js 0x984dc
movslq (%r11), %r15
subq %r15, %rbx
je 0x984b5
movq %rbx, %r15
negq %r15
cmovsq %rbx, %r15
addq %r15, %rdx
movslq 0x8(%r11), %rbx
subq %rbx, %rax
je 0x984cb
movq %rax, %rbx
negq %rbx
cmovsq %rax, %rbx
addq %rbx, %rdx
movslq 0x4(%r11), %rbx
incq %rbx
movslq 0xc(%r11), %rax
incq %rax
addq %r14, %r8
addq $0x20, %r11
decq %r10
jne 0x98483
jmp 0x984f2
xorl %r8d, %r8d
movq %r9, %rbx
movq %rcx, %rax
xorl %edx, %edx
movl 0x8(%rdi), %r10d
movl 0x18(%rdi), %edi
incl %r10d
incl %edi
subq %r10, %rbx
je 0x98510
movq %rbx, %r11
negq %r11
cmovsq %rbx, %r11
addq %r11, %rdx
subq %rdi, %rax
popq %rbx
popq %r14
popq %r15
je 0x98527
movq %rax, %r11
negq %r11
cmovsq %rax, %r11
addq %r11, %rdx
testq %rdx, %rdx
je 0x9858b
xorl %eax, %eax
cmpq %rsi, %rdx
jg 0x9858a
subl %r9d, %r10d
cvtsi2sd %rdx, %xmm4
cvtsi2sd %r10, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm4
ja 0x9858a
subl %ecx, %edi
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm4
ja 0x9858a
addq %r8, %rdx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm3
cmpnltpd %xmm1, %xmm3
cmpnltpd %xmm1, %xmm2
andpd %xmm3, %xmm2
movd %xmm2, %eax
andl $0x1, %eax
retq
movl $0x1, %eax
retq