// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: cal_integer_most_dis(unsigned int, unsigned long*, unsigned long, double)
uint64_t cal_integer_most_dis(uint32_t qid, uint64_t *a, uint64_t a_n, double cluster_rate)
{
    if(a_n <= 0) return (uint64_t)-1;
    // fprintf(stderr, "\n[M::%s::] a_n::%lu\n", __func__, a_n);
    uint64_t k, l, i, m, r_an = a_n, max_m, max_i, cc, cd, nd; int64_t z;
    for (k = 1, l = m = max_m = 0, max_i = (uint64_t)-1; k <= a_n; k++) {
        a[k-1] <<= 1; a[k-1] >>= 1;
        // fprintf(stderr, "[k->%lu] d::%lu, rev::%lu\n", k-1, a[k-1]>>1, a[k-1]&1);
        if((k == a_n) || (((a[k]&((uint64_t)(0x7fffffffffffffff)))>>1) 
                                        != ((a[l]&((uint64_t)(0x7fffffffffffffff)))>>1))) {
            for (i = l; i < k; i++) {
                if(!(a[i]&1)) break;
            }
            a[m] = a[l]; a[m] >>= 1; a[m] <<= 1; 
            ///if all integer sequence are mapped reversely 
            if(i >= k) a[m] += 1;
            a[m] |= ((uint64_t)(k-l))<<32;
            if((k-l) > max_m) {
                max_m = k - l; max_i = m;
            } else if((k-l) == max_m && i < k) {///i < k means this distance is supported by forward sequences
                max_m = k - l; max_i = m;
            }
            l = k; m++;
        }
    }
    a_n = m; assert(a_n > 0);
    // fprintf(stderr, "[M::%s::] m::%lu\n", __func__, m);
    if(max_m > 0 && max_m > (r_an>>1)) {
        // fprintf(stderr, "[M::%s::] dis::%u\n", __func__, ((uint32_t)a[max_i])>>1);
        return ((uint32_t)a[max_i])>>1;
    }

    for (k = 0, max_m = 0, max_i = (uint64_t)-1; k < a_n; k++) {
        cd = (((uint32_t)a[k])>>1); z = k;
        // fprintf(stderr, "[mk->%lu] cd::%lu\n", k, cd);
        for (cc = cd, z--; z >= 0; z--) {
            nd = (((uint32_t)a[z])>>1); assert(nd < cd);
            if(((cd-nd) > (nd*cluster_rate)) && ((cd-nd) > 512)) break;
            cc += (a[z]>>32);
        }
        for (i = k+1; i < a_n; i++) {
            nd = (((uint32_t)a[i])>>1); 
            // if(!(nd > cd)) {
            //     fprintf(stderr, "[M::%s::] qid::%u, a_n::%lu, i::%lu, k::%lu, cd::%lu, nd::%lu\n", 
            //     __func__, qid, a_n, i, k, cd, nd);
            // }
            assert(nd > cd);
            if(((nd-cd) > (nd*cluster_rate)) && ((nd-cd) > 512)) break;
            cc += (a[i]>>32);
        }

        if(cc > max_m) {
            max_m = cc; max_i = k;
        } else if(cc == max_m && (!(a[k]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = k;
        }
    }

    k = max_i;
    cd = (((uint32_t)a[k])>>1); z = k;
    for (max_m = cd, max_i = k, z--; z >= 0; z--) {
        nd = (((uint32_t)a[z])>>1); assert(nd < cd);
        if(((cd-nd) > (nd*cluster_rate)) && ((cd-nd) > 512)) break;
        cc = (a[z]>>32);
        if(cc > max_m) {
            max_m = cc; max_i = z;
        } else if(cc == max_m && (!(a[z]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = z;
        }
    }
    for (i = k+1; i < a_n; i++) {
        nd = (((uint32_t)a[i])>>1); assert(nd > cd);
        if(((nd-cd) > (nd*cluster_rate)) && ((nd-cd) > 512)) break;
        cc = (a[i]>>32);
        if(cc > max_m) {
            max_m = cc; max_i = i;
        } else if(cc == max_m && (!(a[i]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = i;
        }
    }

    // fprintf(stderr, "[M::%s::] dis::%u\n", __func__, ((uint32_t)a[max_i])>>1);
    return ((uint32_t)a[max_i])>>1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0x1f9b66
leaq 0x1(%rdx), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
movabsq $0x7ffffffffffffffe, %r8 # imm = 0x7FFFFFFFFFFFFFFE
movl $0x1, %r9d
movq $-0x1, %rdi
xorl %r10d, %r10d
leaq 0x1(%r8), %r11
xorl %ebx, %ebx
xorl %eax, %eax
xorl %r14d, %r14d
andq %r11, -0x8(%rsi,%r9,8)
cmpq %rdx, %r9
je 0x1f9880
movq (%rsi,%r14,8), %r15
xorq (%rsi,%r9,8), %r15
testq %r8, %r15
je 0x1f98f8
movq %r9, %r15
subq %r14, %r15
setbe %bpl
jbe 0x1f98b6
testb $0x1, (%rsi,%r14,8)
je 0x1f98b6
leaq 0x1(%r14), %r13
movq %r13, %r12
addq %r10, %r13
cmpq $0x1, %r13
je 0x1f98af
leaq 0x1(%r12), %r13
testb $0x1, (%rsi,%r12,8)
jne 0x1f9897
cmpq %r9, %r12
setae %bpl
movq (%rsi,%r14,8), %r14
andq $-0x2, %r14
movzbl %bpl, %r12d
movq %r15, %r13
shlq $0x20, %r13
orq %r12, %r13
orq %r14, %r13
movq %r13, (%rsi,%rax,8)
cmpq %rbx, %r15
jbe 0x1f98dd
movq %rax, %rdi
jmp 0x1f98ef
jne 0x1f98ec
testb %bpl, %bpl
cmovneq %rbx, %r15
cmoveq %rax, %rdi
jmp 0x1f98ef
movq %rbx, %r15
incq %rax
movq %r9, %r14
movq %r15, %rbx
incq %r9
decq %r10
cmpq %rcx, %r9
jne 0x1f9869
testq %rax, %rax
je 0x1f9bf8
shrq %rdx
cmpq %rdx, %rbx
ja 0x1f9b5f
movq $-0x1, %rcx
movl $0x1, %edx
xorl %edi, %edi
movq 0x2b90e(%rip), %xmm1    # 0x225240
movapd 0x2b916(%rip), %xmm2    # 0x225250
xorl %ebx, %ebx
movq %rbx, %r9
movq (%rsi,%rbx,8), %r10
movl %r10d, %r11d
shrl %r11d
movq %r11, %r8
testq %rbx, %rbx
jle 0x1f99b5
movq %rdx, %rbx
movq %r11, %r8
movq -0x10(%rsi,%rbx,8), %r14
movl %r14d, %r15d
shrl %r15d
cmpl %r15d, %r11d
jbe 0x1f9b7c
movq %r11, %r12
subq %r15, %r12
movq %r12, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r12            # imm = 0x201
jb 0x1f99a5
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r15d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1f99b5
shrq $0x20, %r14
addq %r14, %r8
decq %rbx
cmpq $0x1, %rbx
jg 0x1f9957
leaq 0x1(%r9), %rbx
cmpq %rax, %rbx
jae 0x1f9a1a
movq %rbx, %r14
movq (%rsi,%r14,8), %r15
movl %r15d, %r12d
shrl %r12d
movq %r12, %r13
subq %r11, %r13
jbe 0x1f9b9b
movq %r13, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r13            # imm = 0x201
jb 0x1f9a0b
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r12d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1f9a1a
shrq $0x20, %r15
addq %r15, %r8
incq %r14
cmpq %rax, %r14
jb 0x1f99c1
cmpq %rdi, %r8
jbe 0x1f9a24
movq %r9, %rcx
jmp 0x1f9a37
jne 0x1f9a34
testb $0x1, %r10b
cmovneq %rdi, %r8
cmoveq %r9, %rcx
jmp 0x1f9a37
movq %rdi, %r8
incq %rdx
movq %r8, %rdi
cmpq %rax, %rbx
jne 0x1f993c
movl (%rsi,%rcx,8), %edx
shrl %edx
movq %rdx, %r8
movq %rcx, %rdi
testq %rcx, %rcx
jle 0x1f9ae3
leaq -0x1(%rcx), %r9
movq %rcx, %rdi
movq %rdx, %r8
movq (%rsi,%r9,8), %r10
movl %r10d, %r11d
shrl %r11d
cmpl %r11d, %edx
jbe 0x1f9bba
movq %rdx, %rbx
subq %r11, %rbx
movq %rbx, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %rbx            # imm = 0x201
jb 0x1f9ab1
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r11d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1f9ae3
movq %r10, %r11
shrq $0x20, %r11
cmpq %r8, %r11
jbe 0x1f9ac5
movq %r11, %r8
movq %r9, %rdi
jmp 0x1f9ad3
xorq %r8, %r11
andl $0x1, %r10d
orq %r11, %r10
cmoveq %r9, %rdi
leaq -0x1(%r9), %r10
incq %r9
cmpq $0x1, %r9
movq %r10, %r9
jg 0x1f9a64
incq %rcx
cmpq %rax, %rcx
jae 0x1f9b5f
movq (%rsi,%rcx,8), %r9
movl %r9d, %r10d
shrl %r10d
movq %r10, %r11
subq %rdx, %r11
jbe 0x1f9bd9
movq %r11, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r11            # imm = 0x201
jb 0x1f9b35
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r10d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1f9b5f
movq %r9, %r10
shrq $0x20, %r10
cmpq %r8, %r10
jbe 0x1f9b49
movq %r10, %r8
movq %rcx, %rdi
jmp 0x1f9b57
xorq %r8, %r10
andl $0x1, %r9d
orq %r10, %r9
cmoveq %rcx, %rdi
incq %rcx
cmpq %rcx, %rax
jne 0x1f9aeb
movl (%rsi,%rdi,8), %eax
shrl %eax
jmp 0x1f9b6d
movq $-0x1, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x413e7(%rip), %rdi     # 0x23af6a
leaq 0x3f79b(%rip), %rsi     # 0x239325
leaq 0x41393(%rip), %rcx     # 0x23af24
movl $0x1886, %edx           # imm = 0x1886
callq 0x2110
leaq 0x413d0(%rip), %rdi     # 0x23af72
leaq 0x3f77c(%rip), %rsi     # 0x239325
leaq 0x41374(%rip), %rcx     # 0x23af24
movl $0x1890, %edx           # imm = 0x1890
callq 0x2110
leaq 0x413a9(%rip), %rdi     # 0x23af6a
leaq 0x3f75d(%rip), %rsi     # 0x239325
leaq 0x41355(%rip), %rcx     # 0x23af24
movl $0x189f, %edx           # imm = 0x189F
callq 0x2110
leaq 0x41392(%rip), %rdi     # 0x23af72
leaq 0x3f73e(%rip), %rsi     # 0x239325
leaq 0x41336(%rip), %rcx     # 0x23af24
movl $0x18a9, %edx           # imm = 0x18A9
callq 0x2110
leaq 0x4131d(%rip), %rdi     # 0x23af1c
leaq 0x3f71f(%rip), %rsi     # 0x239325
leaq 0x41317(%rip), %rcx     # 0x23af24
movl $0x187b, %edx           # imm = 0x187B
callq 0x2110