// File: /workspace/github_cpp_makefile_star2_O3/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, 0x2c(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %r12
movq %rsi, %r8
movq %rdi, 0x30(%rsp)
movq 0x88(%rsp), %rax
xorl %edx, %edx
movq %rdx, 0x18(%rsi)
movq %rdx, 0x30(%rsi)
cmpl $0x0, 0x80(%rsp)
movq %rsi, 0x10(%rsp)
je 0x1fc113
cmpl $-0x1, %r12d
je 0x1fc473
movq $0x0, 0x18(%rsp)
xorl %r14d, %r14d
movl %r12d, %eax
leaq (%rax,%rax,8), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movl (%r13), %eax
movl 0x20(%r13), %r15d
shrl $0x5, %r15d
andl $0x1, %r15d
movq %rax, 0x38(%rsp)
leal (%r15,%rax,2), %ebp
movl %ebp, %r12d
xorl $0x1, %r12d
cmpl $-0x1, 0x14(%r13)
je 0x1fbfb2
movq 0x20(%rsp), %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x1fbe0d
movq 0x10(%rsp), %r8
testq %rax, %rax
je 0x1fc1ab
movq 0x18(%r8), %rbx
testq %rbx, %rbx
je 0x1fbfd8
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x1fbe0d
movq 0x10(%rsp), %r8
testq %rax, %rax
je 0x1fc1ab
movq %r14, 0x40(%rsp)
xorl %r14d, %r14d
cmpl 0x2c(%rsp), %r12d
sete 0xf(%rsp)
movq 0x28(%r8), %rdi
cmpq 0x20(%r8), %rbx
jne 0x1fc021
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x20(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r8
movq %rax, %rdi
movq %rax, 0x28(%r8)
movq 0x18(%r8), %rbx
leaq 0x1(%rbx), %rax
movq %rax, 0x18(%r8)
testb %r15b, %r15b
je 0x1fc043
movl 0xc(%r13), %r15d
movl 0x10(%r13), %ebp
movq 0x18(%rsp), %rax
addq %rax, %r15
addq %rax, %rbp
jmp 0x1fc075
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x38(%rsp), %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rcx
movl (%rax,%rcx,8), %ebp
andq %rdx, %rbp
addq 0x18(%rsp), %rbp
movl 0xc(%r13), %eax
movl 0x10(%r13), %ecx
movq %rbp, %r15
subq %rcx, %r15
subq %rax, %rbp
movq 0x30(%r8), %rcx
movq 0x40(%r8), %rax
cmpq 0x38(%r8), %rcx
jne 0x1fc0b9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x38(%r8)
shlq $0x3, %rsi
movq %rdi, %r14
movq %rax, %rdi
callq 0x2370
movq %r14, %rdi
xorl %r14d, %r14d
movq 0x10(%rsp), %r8
movq %rax, 0x40(%r8)
movq 0x30(%r8), %rcx
movb 0xf(%rsp), %dl
movb %dl, %r14b
movq 0x40(%rsp), %rdx
addl %r14d, %edx
movq %rdx, %r14
shlq $0x20, %r15
orq %rbp, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, 0x30(%r8)
movq %r15, (%rax,%rcx,8)
movl %r12d, %eax
movq 0x18(%rsp), %rbp
movq %rbp, %rcx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, (%rdi,%rbx,8)
movl 0x14(%r13), %r12d
cmpl $-0x1, %r12d
je 0x1fc1b5
movl 0x18(%r13), %eax
addq %rax, %rbp
movq %rbp, 0x18(%rsp)
jmp 0x1fbf5d
cmpl $-0x1, %r12d
je 0x1fc1d9
xorl %eax, %eax
xorl %edx, %edx
movl %r12d, %r15d
movl %eax, 0x18(%rsp)
movl %r15d, %ebx
movl %r15d, %eax
leaq (%rax,%rax,8), %rax
movq 0x30(%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 0x1fc175
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rdx, %r14
movq %r13, %rdx
callq 0x1fbe0d
movq %r14, %rdx
movq 0x10(%rsp), %r8
testq %rax, %rax
je 0x1fc1de
testl %edx, %edx
je 0x1fc19b
xorl $0x1, %ebp
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rdx, %r14
movq %r13, %rdx
callq 0x1fbe0d
movq %r14, %rdx
movq 0x10(%rsp), %r8
testq %rax, %rax
je 0x1fc1de
incl %edx
movl %ebx, %eax
cmpl $-0x1, %r15d
jne 0x1fc124
jmp 0x1fc1e2
movq 0x18(%rsp), %rbp
jmp 0x1fc432
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x38(%rsp), %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %rbp
jmp 0x1fc432
xorl %r13d, %r13d
jmp 0x1fc1e5
movl 0x18(%rsp), %ebx
movl %ebx, %r13d
movl %edx, %ebx
cmpq %rbx, 0x20(%r8)
jae 0x1fc24a
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(%r8)
movq 0x28(%r8), %rdi
leaq 0x8(,%rcx,8), %rsi
movq %rdx, %r14
callq 0x2370
movq %r14, %rdx
movq 0x10(%rsp), %r8
movq %rax, 0x28(%r8)
movq %rbx, 0x18(%r8)
cmpq %rbx, 0x38(%r8)
jae 0x1fc2b1
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(%r8)
movq 0x40(%r8), %rdi
leaq 0x8(,%rcx,8), %rsi
movq %rdx, %r14
callq 0x2370
movq %r14, %rdx
movq 0x10(%rsp), %r8
movq %rax, 0x40(%r8)
movq %rbx, 0x30(%r8)
cmpq %r12, %r13
jae 0x1fc2c4
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x1fc3c9
movq 0x28(%r8), %rax
movq 0x40(%r8), %rcx
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %r14, %rsi
leaq (,%r13,8), %r8
addq %r13, %r8
movq 0x30(%rsp), %r9
leaq (%r9,%r8,4), %r10
movl (%r10), %r9d
movl 0x20(%r10), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
leal (%rbx,%r9,2), %r14d
decl %edx
xorl %r8d, %r8d
cmpl 0x2c(%rsp), %r14d
sete %r11b
movq %rbp, %r15
shlq $0x20, %r15
orq %r14, %r15
movq %rdx, %rdi
movq %r15, (%rax,%rdx,8)
testl %ebx, %ebx
je 0x1fc350
movq 0x20(%rsp), %rbx
movq 0x20(%rbx), %rbx
movq %r9, %r14
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %r14
movl (%rbx,%r14,8), %ebx
andq %rdx, %rbx
movq %rbp, %rdx
addq %rbp, %rbx
movl 0xc(%r10), %r15d
movl 0x10(%r10), %r13d
movq %rbx, %r14
subq %r13, %r14
subq %r15, %rbx
jmp 0x1fc361
movl 0xc(%r10), %r14d
movl 0x10(%r10), %ebx
addq %rbp, %r14
movq %rbp, %rdx
addq %rbp, %rbx
shlq $0x20, %r14
orq %rbx, %r14
movq %r14, (%rcx,%rdi,8)
movl 0x14(%r10), %r13d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rbx, %r13
sete %bl
cmpq %r12, %r13
setb %bpl
orb %bl, %bpl
je 0x1fc3a1
movq 0x20(%rsp), %r10
movq 0x20(%r10), %r10
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andl %ebx, %r9d
movl (%r10,%r9,8), %r9d
andl %ebx, %r9d
jmp 0x1fc3a5
movl 0x18(%r10), %r9d
movq %rdx, %rbp
movq %rsi, %r14
movb %r11b, %r8b
addl %r8d, %r14d
movl %r9d, %r8d
addq %r8, %rbp
cmpl $-0x1, %r13d
movq %rdi, %rdx
je 0x1fc3c9
cmpq %r12, %r13
jae 0x1fc2d1
testl %edx, %edx
jne 0x1fc499
movq 0x10(%rsp), %r10
cmpq $0x0, 0x18(%r10)
je 0x1fc432
movq 0x28(%r10), %rax
movq 0x20(%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, %rbp
jb 0x1fc454
movl %ebp, %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(%r10)
ja 0x1fc3f1
movq 0x88(%rsp), %rax
movq %rbp, (%rax)
testl %r14d, %r14d
je 0x1fc47a
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x49b35(%rip), %rdi     # 0x245f90
leaq 0x491a7(%rip), %rsi     # 0x245609
leaq 0x49ab6(%rip), %rcx     # 0x245f1f
movl $0xd8c, %edx            # imm = 0xD8C
callq 0x2110
movq $0x0, (%rax)
leaq 0x4db0c(%rip), %rdi     # 0x249f8d
leaq 0x49181(%rip), %rsi     # 0x245609
leaq 0x49a90(%rip), %rcx     # 0x245f1f
movl $0xd94, %edx            # imm = 0xD94
callq 0x2110
leaq 0x49a77(%rip), %rdi     # 0x245f17
leaq 0x49162(%rip), %rsi     # 0x245609
leaq 0x49a71(%rip), %rcx     # 0x245f1f
movl $0xd84, %edx            # imm = 0xD84
callq 0x2110