// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_ul_pat_seq(uc_block_t*, path_dp_t*, all_ul_t*, unsigned long, asg_t*, unsigned int, unsigned int, unsigned long*)
int64_t gen_ul_pat_seq(uc_block_t *a, path_dp_t *b, all_ul_t *ul, uint64_t idx, asg_t *g, uint32_t v, uint32_t is_backward, uint64_t *rl)
{
    uint32_t m = 0, pv, ai, pi; uint64_t *t, l = 0, tt, s, e; b->pat.n = 0; b->pat_cor.n = 0;
    if(is_backward) {
        for (ai = idx, l = 0; ai != (uint32_t)-1; ai = a[ai].pidx) {
            pv = (((uint32_t)(a[ai].hid))<<1)|((uint32_t)(a[ai].rev)); pv^=1;
            if((a[ai].pidx != (uint32_t)-1) && (!ulg_len_check(g, pv, &(a[ai])))) break;
            if((b->pat.n > 0) && (!ulg_len_check(g, pv^1, &(a[ai])))) break;
            if(pv == v) m++;
            kv_pushp(uint64_t, b->pat, &t); 
            update_l_coord(g, l, pv, &(a[ai]), &s, &e);
            kv_push(uint64_t, b->pat_cor, ((s<<32)|e));
            (*t) = pv; (*t) |= (l<<32);

            if(a[ai].pidx != (uint32_t)-1) l += a[ai].pdis;
            else l += g->seq[pv>>1].len;
        }
    } else {
        for (ai = idx, pi = 0; ai != (uint32_t)-1; ai = a[ai].aidx) {
            pv = (((uint32_t)(a[ai].hid))<<1)|((uint32_t)(a[ai].rev));
            if((a[ai].aidx != (uint32_t)-1) && (!ulg_len_check(g, pv, &(a[ai])))) break;
            if((pi > 0) && (!ulg_len_check(g, pv^1, &(a[ai])))) break;
            l = ai; pi++;
        }

        ai = l;
        kv_resize(uint64_t, b->pat, pi); b->pat.n = pi; 
        kv_resize(uint64_t, b->pat_cor, pi); b->pat_cor.n = pi; 
        for (l = 0; ai != (uint32_t)-1 && ai >= idx; ai = a[ai].pidx) {
            pv = (((uint32_t)(a[ai].hid))<<1)|((uint32_t)(a[ai].rev));
            pi--; if(pv == v) m++;
            b->pat.a[pi] = pv; b->pat.a[pi] |= (l<<32); 
            update_l_coord(g, l, pv, &(a[ai]), &s, &e);
            b->pat_cor.a[pi] = ((s<<32)|e);
            if(a[ai].pidx != (uint32_t)-1 && a[ai].pidx >= idx) l += a[ai].pdis;
            else l += g->seq[pv>>1].len;
        }
        assert(pi == 0);
        
        for (ai = 0; ai < b->pat.n; ai++) {
            tt = (b->pat.a[ai]>>32) + g->seq[((uint32_t)b->pat.a[ai])>>1].len;
            // if(l < tt) {
            //     fprintf(stderr, "\n[M::%s::] ai::%u, b->pat.n::%u, l::%lu, tt::%lu, beg::%lu\n", 
            //     __func__, ai, (uint32_t)b->pat.n, l, tt, (b->pat.a[ai]>>32));
            // }
            assert(l >= tt);
            tt = l - tt;
            b->pat.a[ai] = (uint32_t)b->pat.a[ai];
            b->pat.a[ai] += (tt<<32);
        }
    }
    (*rl) = l;

    assert(m > 0);
    return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, 0x1c(%rsp)
movq %r8, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x18(%rsi)
movq %rbp, 0x30(%rsi)
cmpl $0x0, 0x80(%rsp)
movq %rsi, 0x20(%rsp)
je 0x1f0b89
cmpl $-0x1, %ecx
je 0x1f0c0b
movq %rsi, %r15
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rbp, 0x10(%rsp)
movl %ecx, %eax
leaq (%rax,%rax,8), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movl (%r13), %eax
movl 0x20(%r13), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
movq %rax, 0x38(%rsp)
leal (%rbx,%rax,2), %r12d
movl %r12d, %ebp
xorl $0x1, %ebp
cmpl $-0x1, 0x14(%r13)
je 0x1f0a03
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x1f0877
testq %rax, %rax
je 0x1f0c13
movq 0x18(%r15), %r15
testq %r15, %r15
je 0x1f0a25
movq 0x8(%rsp), %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x1f0877
testq %rax, %rax
je 0x1f0c13
xorl %edi, %edi
cmpl 0x1c(%rsp), %ebp
sete %r8b
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
cmpq %rax, %r15
movq %rcx, %r15
jne 0x1f0a6d
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x20(%r15)
movq 0x28(%r15), %rdi
shlq $0x3, %rsi
movl %r8d, %r12d
callq 0x2370
movl %r12d, %r8d
xorl %edi, %edi
movq %rax, 0x28(%r15)
movq 0x18(%r15), %rsi
movq 0x28(%r15), %r9
leaq 0x1(%rsi), %rax
movq %rax, 0x18(%r15)
testb %bl, %bl
je 0x1f0a91
movl 0xc(%r13), %ebx
movl 0x10(%r13), %r12d
addq %r14, %rbx
addq %r14, %r12
jmp 0x1f0ac2
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x38(%rsp), %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rcx
movl (%rax,%rcx,8), %r12d
andq %rdx, %r12
addq %r14, %r12
movl 0xc(%r13), %eax
movl 0x10(%r13), %ecx
movq %r12, %rbx
subq %rcx, %rbx
subq %rax, %r12
movq 0x38(%r15), %rax
cmpq %rax, 0x30(%r15)
jne 0x1f0b15
movq %rsi, 0x40(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x38(%r15)
movq 0x40(%r15), %rdi
shlq $0x3, %rsi
movl %ebp, 0x34(%rsp)
movb %r8b, 0x7(%rsp)
movq %r9, %rbp
callq 0x2370
movq %rbp, %r9
movq 0x40(%rsp), %rsi
movb 0x7(%rsp), %r8b
movl 0x34(%rsp), %ebp
xorl %edi, %edi
movq %rax, 0x40(%r15)
shlq $0x20, %rbx
orq %r12, %rbx
movq 0x30(%r15), %rax
movq 0x40(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x30(%r15)
movq %rbx, (%rcx,%rax,8)
movl %ebp, %eax
movq %r14, %rcx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%r9,%rsi,8)
movl 0x14(%r13), %eax
movq %rax, %rcx
cmpl $-0x1, %eax
je 0x1f0b52
movl 0x18(%r13), %eax
jmp 0x1f0b6c
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %rsi
andl %edx, %esi
movl (%rax,%rsi,8), %eax
andl %edx, %eax
movq 0x10(%rsp), %rbp
movb %r8b, %dil
addl %edi, %ebp
movl %eax, %eax
addq %rax, %r14
cmpl $-0x1, %ecx
jne 0x1f09b3
jmp 0x1f0e6c
cmpl $-0x1, %ecx
movq %rcx, 0x10(%rsp)
je 0x1f0c1d
xorl %ebx, %ebx
xorl %r12d, %r12d
movl %ecx, %r15d
movl %r15d, %r14d
movl %r15d, %eax
leaq (%rax,%rax,8), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movl 0x20(%r13), %eax
shrl $0x5, %eax
andl $0x1, %eax
movl (%r13), %ecx
movl 0x1c(%r13), %r15d
leal (%rax,%rcx,2), %ebp
cmpl $-0x1, %r15d
je 0x1f0be1
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x1f0877
testq %rax, %rax
je 0x1f0c25
testl %r12d, %r12d
je 0x1f0bfd
xorl $0x1, %ebp
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x1f0877
testq %rax, %rax
je 0x1f0c25
incl %r12d
movl %r14d, %ebx
cmpl $-0x1, %r15d
jne 0x1f0b9f
jmp 0x1f0c28
xorl %r14d, %r14d
jmp 0x1f0e6c
movq 0x10(%rsp), %rbp
jmp 0x1f0e6c
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x1f0c28
movl %ebx, %r14d
movl %r12d, %ebx
movq 0x20(%rsp), %r15
cmpq %rbx, 0x20(%r15)
jae 0x1f0c88
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x20(%r15)
movq 0x28(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x28(%r15)
movq %rbx, 0x18(%r15)
cmpq %rbx, 0x38(%r15)
movq 0x10(%rsp), %r13
jae 0x1f0ce9
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x38(%r15)
movq 0x40(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x40(%r15)
movq %rbx, 0x30(%r15)
cmpl $-0x1, %r14d
sete %al
movl %r14d, %ebx
cmpq %r13, %rbx
setb %cl
orb %al, %cl
jne 0x1f0e02
movq 0x28(%r15), %rax
movq 0x40(%r15), %rcx
xorl %r14d, %r14d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %ebp, %ebp
leaq (%rbx,%rbx,8), %r8
movq 0x28(%rsp), %r9
leaq (%r9,%r8,4), %r10
movl (%r10), %r9d
movl 0x20(%r10), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
leal (%rbx,%r9,2), %r15d
decl %r12d
xorl %r8d, %r8d
cmpl 0x1c(%rsp), %r15d
sete %r11b
movq %r14, %r13
shlq $0x20, %r13
orq %r15, %r13
movq %r13, (%rax,%r12,8)
movq %rbp, %rdx
testl %ebx, %ebx
je 0x1f0d8a
movq 0x8(%rsp), %rbx
movq 0x20(%rbx), %rbx
movq %r9, %r15
andq %rdi, %r15
movl (%rbx,%r15,8), %ebx
andq %rdi, %rbx
addq %r14, %rbx
movl 0xc(%r10), %r15d
movl 0x10(%r10), %ebp
movq %rbx, %r13
subq %rbp, %r13
subq %r15, %rbx
jmp 0x1f0d98
movl 0xc(%r10), %r13d
movl 0x10(%r10), %ebx
addq %r14, %r13
addq %r14, %rbx
shlq $0x20, %r13
orq %rbx, %r13
movq %r13, (%rcx,%r12,8)
movl 0x14(%r10), %ebx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq %r15, %rbx
sete %bpl
movq 0x10(%rsp), %r13
cmpq %r13, %rbx
setb %r15b
orb %bpl, %r15b
je 0x1f0dda
movq 0x8(%rsp), %r10
movq 0x20(%r10), %r10
andl %esi, %r9d
movl (%r10,%r9,8), %r9d
andl %esi, %r9d
jmp 0x1f0dde
movl 0x18(%r10), %r9d
movq 0x20(%rsp), %r15
movq %rdx, %rbp
movb %r11b, %r8b
addl %r8d, %ebp
movl %r9d, %r8d
addq %r8, %r14
cmpl $-0x1, %ebx
je 0x1f0e07
cmpq %r13, %rbx
jae 0x1f0d1c
jmp 0x1f0e07
xorl %ebp, %ebp
xorl %r14d, %r14d
testl %r12d, %r12d
jne 0x1f0eca
cmpq $0x0, 0x18(%r15)
je 0x1f0e6c
movq 0x28(%r15), %rax
movq 0x8(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl $0x1, %edx
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movq %rdi, %r9
shrq $0x20, %r9
movl %edi, %r8d
shrl %r8d
movl (%rcx,%r8,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r9, %r8
cmpq %r8, %r14
jb 0x1f0e8c
movl %r14d, %r9d
subl %r8d, %r9d
movl %edi, %edi
shlq $0x20, %r9
orq %rdi, %r9
movq %r9, (%rax,%rsi,8)
movl %edx, %esi
incl %edx
cmpq %rsi, 0x18(%r15)
ja 0x1f0e2b
movq 0x88(%rsp), %rax
movq %r14, (%rax)
testl %ebp, %ebp
je 0x1f0eab
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48e19(%rip), %rdi     # 0x239cac
leaq 0x4848b(%rip), %rsi     # 0x239325
leaq 0x48d9a(%rip), %rcx     # 0x239c3b
movl $0xd8c, %edx            # imm = 0xD8C
callq 0x2110
leaq 0x4cdf7(%rip), %rdi     # 0x23dca9
leaq 0x4846c(%rip), %rsi     # 0x239325
leaq 0x48d7b(%rip), %rcx     # 0x239c3b
movl $0xd94, %edx            # imm = 0xD94
callq 0x2110
leaq 0x48d62(%rip), %rdi     # 0x239c33
leaq 0x4844d(%rip), %rsi     # 0x239325
leaq 0x48d5c(%rip), %rcx     # 0x239c3b
movl $0xd84, %edx            # imm = 0xD84
callq 0x2110