// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_ul_seq(ul_resolve_t*, unsigned long, asgc8_v*)
void gen_ul_seq(ul_resolve_t *uidx, uint64_t iug_id, asgc8_v * res)
{
    ul2ul_idx_t *idx = &(uidx->uovl); ma_ug_t *raw = uidx->l1_ug; uint64_t k, m, s, e, ol, rev, Ns;
    uinfo_srt_warp_t *seq = &(idx->cc.iug_a[iug_id]); ma_utg_t *ru; asg_arc_t *z; 
    for (k = res->n = 0; k < seq->n; k++) {
        s = seq->a[k].s; e = seq->a[k].e; rev = seq->a[k].v&1; 
        ru = &(raw->u.a[seq->a[k].v>>1]); Ns = 0;
        if(k + 1 < seq->n) {
            z = get_specfic_edge(raw->g, seq->a[k].v, seq->a[k+1].v);
            if(z) {
                ol = z->ol;
                if(!rev) {
                    // assert(seq->a[k].e == ru->len);
                    e = (ru->len > ol)?(ru->len-ol):(0);
                } else {
                    // assert(seq->a[k].s == 0);
                    s = ol;
                }
            } else {
                Ns = 50;
            }
        }
        if(s < e) {
            kv_resize(char, (*res), res->n + e - s);
            retrieve_u_seq(NULL, res->a + res->n, ru, rev, (rev)?(ru->len-e):(s), e - s, NULL);
            res->n += e - s;
        }

        if(Ns) {
            kv_resize(char, (*res), res->n + Ns);
            for (m = 0; m < Ns; m++) res->a[res->n++] = 'N';
        }        
    }
    
    kv_push(char, *res, '\0');    
    // fprintf(stderr, "[M::%s::iug_id->%lu] # u->len::%u, # res->n::%u, # strlen(res->a)::%u\n", 
    // __func__, iug_id, idx->i_ug->u.a[iug_id].len, (uint32_t)res->n, (uint32_t)strlen(res->a));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
imulq $0x18, %rsi, %r14
addq 0x260(%rdi), %r14
movq 0x18(%rdi), %r12
andq $0x0, (%rdx)
movq $0x0, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq %r14, 0x30(%rsp)
movq (%r14), %rcx
movq 0x20(%rsp), %r13
cmpq %rcx, %r13
jae 0x1be713
movq 0x10(%r14), %rax
movq %r13, %rdx
shlq $0x4, %rdx
movl (%rax,%rdx), %esi
movl 0x4(%rax,%rdx), %r14d
movl 0x8(%rax,%rdx), %edi
movl %esi, %ebp
movl %esi, %edx
shrl %edx
imulq $0x28, %rdx, %r15
addq 0x10(%r12), %r15
andl $0x1, %ebp
incq %r13
movb $0x1, %dl
cmpq %rcx, %r13
movq %r13, 0x20(%rsp)
jae 0x1be5c5
movq %rdi, 0x10(%rsp)
movq 0x18(%r12), %rdi
movq %r13, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edx
callq 0x1de62d
testq %rax, %rax
je 0x1be5ca
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
testl %ebp, %ebp
jne 0x1be5d2
movl (%r15), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %rdi
subq %rax, %rdi
cmpl %eax, %ecx
movl $0x0, %eax
cmovbeq %rax, %rdi
xorl %r13d, %r13d
movb $0x1, %dl
jmp 0x1be5df
xorl %r13d, %r13d
jmp 0x1be5df
xorl %edx, %edx
pushq $0x32
popq %r13
jmp 0x1be5da
xorl %r13d, %r13d
movq %rax, %r14
movb $0x1, %dl
movq 0x10(%rsp), %rdi
movq %rdi, %r12
subq %r14, %r12
jbe 0x1be68e
movq (%rbx), %rax
leaq (%r12,%rax), %rcx
movq 0x10(%rbx), %rsi
cmpq %rcx, 0x8(%rbx)
movl %edx, 0x1c(%rsp)
jae 0x1be65a
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
incq %rax
movq %rax, 0x8(%rbx)
movq %rdi, 0x10(%rsp)
movq %rsi, %rdi
movq %rax, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
testl %ebp, %ebp
je 0x1be66b
movl (%r15), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
subq %rdi, %r14
addq %rax, %rsi
andq $0x0, (%rsp)
movzbl %bpl, %ecx
xorl %edi, %edi
movq %r15, %rdx
movq %r14, %r8
movq %r12, %r9
callq 0x9f29
addq %r12, (%rbx)
movl 0x1c(%rsp), %edx
testb %dl, %dl
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r12
jne 0x1be531
movq (%rbx), %rax
addq %r13, %rax
cmpq %rax, 0x8(%rbx)
jae 0x1be708
decq %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, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
jmp 0x1be708
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb $0x4e, (%rcx,%rax)
subq $0x1, %r13
jae 0x1be6f6
jmp 0x1be531
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1be741
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rsi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%rbx)
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movb $0x0, (%rax,%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq