// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_ref_pat_seq(asg_t*, ubuf_t*, unsigned long, double, unsigned int, unsigned int)
void gen_ref_pat_seq(asg_t *g, ubuf_t *b, uint64_t rul, double diff_rate, uint32_t v, uint32_t w/**, uint32_t is_debug**/)
{
    int64_t ref_n = b->dp.ref.n, k; uint64_t x, l; uint32_t p, arc_l;
    if(ref_n >= 2 && ((uint32_t)b->dp.ref.a[0]) == v && ((uint32_t)b->dp.ref.a[1]) == w) {
        // if(is_debug) fprintf(stderr, "+[M::%s::] ref_n::%ld, rul::%lu\n", __func__, ref_n, rul);
        if((ref_n) > 0 && ((b->dp.ref.a[ref_n-1]>>32) >= (rul*(1.0+diff_rate)))) {
            for (k = ref_n-1; k >= 0; k--) {
                if((b->dp.ref.a[k]>>32) < (rul*(1.0+diff_rate))) break;
                b->dp.g_flt.a[(uint32_t)b->dp.ref.a[k]] = 0;
            }
            ref_n = k + 1; b->dp.ref.n = ref_n;
        } else {
            p = (uint32_t)b->dp.ref.a[ref_n-1]; p ^= 1; p = b->a.a[p].p;
            for (l = b->dp.ref.a[ref_n-1]>>32; p != (uint32_t)-1; p = b->a.a[p].p) {
                x = p^1; arc_l = get_arch_len(g, ((uint32_t)b->dp.ref.a[b->dp.ref.n-1]), x); 
                assert(arc_l != (uint32_t)-1); l += arc_l; 
                if(l >= (rul*(1.0+diff_rate))) break;
                b->dp.g_flt.a[x] = 1; x += (l<<32); kv_push(uint64_t, b->dp.ref, x); 
            }
            ref_n = b->dp.ref.n;
        }
    } else {
        // if(is_debug) fprintf(stderr, "-[M::%s::] ref_n::%ld, rul::%lu\n", __func__, ref_n, rul);
        b->dp.ref.n = 0;
        for (k = 0; k < ref_n; k++) {
            b->dp.g_flt.a[(uint32_t)b->dp.ref.a[k]] = 0;
        }

        x = v; kv_push(uint64_t, b->dp.ref, x); b->dp.g_flt.a[x] = 1;
        for (p = w^1, l = 0; p != (uint32_t)-1; p = b->a.a[p].p) {
            x = p^1; arc_l = get_arch_len(g, ((uint32_t)b->dp.ref.a[b->dp.ref.n-1]), x); 
            assert(arc_l != (uint32_t)-1); l += arc_l; 
            if(l >= (rul*(1.0+diff_rate))) break;
            b->dp.g_flt.a[x] = 1; x += (l<<32); kv_push(uint64_t, b->dp.ref, x); 
        }
        ref_n = b->dp.ref.n;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x2, -0x30(%rbp)
jl 0x323e47
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq (%rax), %rax
cmpl -0x24(%rbp), %eax
jne 0x323e47
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq 0x8(%rax), %rax
cmpl -0x28(%rbp), %eax
jne 0x323e47
cmpq $0x0, -0x30(%rbp)
jle 0x323c12
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0x4(%rax,%rcx,8), %eax
cvtsi2sd %rax, %xmm0
movaps 0x5e4fd(%rip), %xmm2    # 0x382010
movq -0x18(%rbp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x5e4fc(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
movsd 0x5e4c5(%rip), %xmm2    # 0x382000
addsd -0x20(%rbp), %xmm2
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x323c12
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0x323bf2
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x4(%rax,%rcx,8), %eax
cvtsi2sd %rax, %xmm1
movaps 0x5e48c(%rip), %xmm2    # 0x382010
movq -0x18(%rbp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x5e48b(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
movsd 0x5e454(%rip), %xmm2    # 0x382000
addsd -0x20(%rbp), %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x323bbd
jmp 0x323bf2
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa0(%rcx), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x323b5a
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
jmp 0x323e42
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x4c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x48(%rbp)
cmpl $-0x1, -0x4c(%rbp)
je 0x323e33
movl -0x4c(%rbp), %eax
xorl $0x1, %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x90(%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %esi
movq -0x40(%rbp), %rax
movl %eax, %edx
callq 0x3239c0
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
je 0x323cbb
jmp 0x323cda
leaq 0x72e86(%rip), %rdi     # 0x396b48
leaq 0x724f0(%rip), %rsi     # 0x3961b9
movl $0xdb3, %edx            # imm = 0xDB3
leaq 0x72e89(%rip), %rcx     # 0x396b5e
callq 0x2130
movl -0x50(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movaps 0x5e31f(%rip), %xmm3    # 0x382010
movq -0x48(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x5e31e(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x18(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
movsd 0x5e2cf(%rip), %xmm2    # 0x382000
addsd -0x20(%rbp), %xmm2
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x323d45
jmp 0x323e33
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x40(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x48(%rbp), %rax
shlq $0x20, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x323de9
movq -0x10(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x323da2
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x323dad
movl $0x2, %eax
movq %rax, -0x58(%rbp)
jmp 0x323dad
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x323e17
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x4c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x323c6b
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x324149
movq -0x10(%rbp), %rax
movq $0x0, 0x90(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x323e9a
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa0(%rcx), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x323e5e
movl -0x24(%rbp), %eax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x323f22
movq -0x10(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x323edb
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0x323ee6
movl $0x2, %eax
movq %rax, -0x60(%rbp)
jmp 0x323ee6
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x40(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movl -0x28(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq $0x0, -0x48(%rbp)
cmpl $-0x1, -0x4c(%rbp)
je 0x32413a
movl -0x4c(%rbp), %eax
xorl $0x1, %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x90(%rcx), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %esi
movq -0x40(%rbp), %rax
movl %eax, %edx
callq 0x3239c0
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
je 0x323fc2
jmp 0x323fe1
leaq 0x72b7f(%rip), %rdi     # 0x396b48
leaq 0x721e9(%rip), %rsi     # 0x3961b9
movl $0xdc3, %edx            # imm = 0xDC3
leaq 0x72b82(%rip), %rcx     # 0x396b5e
callq 0x2130
movl -0x50(%rbp), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movaps 0x5e018(%rip), %xmm3    # 0x382010
movq -0x48(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x5e017(%rip), %xmm2    # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x18(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
movsd 0x5dfc8(%rip), %xmm2    # 0x382000
addsd -0x20(%rbp), %xmm2
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x32404c
jmp 0x32413a
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq -0x40(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x48(%rbp), %rax
shlq $0x20, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x3240f0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x3240a9
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x3240b4
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x3240b4
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x32411e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x4c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x323f72
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x30(%rbp)
addq $0x70, %rsp
popq %rbp
retq
nop