// File: /workspace/github_cpp_makefile_star2_O3/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
movq 0x18(%rdi), %r8
movq 0x260(%rdi), %rcx
leaq (%rsi,%rsi,2), %rdx
movq $0x0, (%rbx)
movq (%rcx,%rdx,8), %rax
testq %rax, %rax
je 0x20acb6
leaq (%rcx,%rdx,8), %r9
xorl %r10d, %r10d
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x10(%r9), %rcx
movq %r10, %rsi
shlq $0x4, %rsi
movl (%rcx,%rsi), %edx
movl 0x4(%rcx,%rsi), %r14d
movl 0x8(%rcx,%rsi), %r12d
movl %edx, %r13d
andl $0x1, %r13d
movl %edx, %esi
shrl %esi
leaq (%rsi,%rsi,4), %r15
shlq $0x3, %r15
addq 0x10(%r8), %r15
incq %r10
cmpq %rax, %r10
jae 0x20ab0a
movq 0x18(%r8), %rsi
movq 0x28(%rsi), %rax
movq (%rax,%rdx,8), %rax
movq %rax, %rdx
movl $0x32, %ebp
movb $0x1, %r11b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x20ab06
movq %r10, %rdi
shlq $0x4, %rdi
movl (%rcx,%rdi), %ecx
movq 0x8(%rsi), %rsi
shrq $0x20, %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
movslq (%rsi,%rdi), %rax
testq %rax, %rax
js 0x20aafd
cmpl %ecx, -0x4(%rsi,%rdi)
je 0x20ac80
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x20aaea
xorl %edx, %edx
jmp 0x20ab11
movb $0x1, %dl
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %r12, 0x30(%rsp)
subq %r14, %r12
jbe 0x20abdc
movq (%rbx), %rax
leaq (%r12,%rax), %rcx
movq 0x10(%rbx), %rsi
cmpq %rcx, 0x8(%rbx)
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
movl %edx, 0xc(%rsp)
jae 0x20ab8e
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 %rsi, %rdi
movq %rax, %rsi
callq 0x2370
movq %rax, %rsi
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
testl %r13d, %r13d
je 0x20aba2
movl (%r15), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
subq 0x30(%rsp), %r14
addq %rax, %rsi
movq $0x0, (%rsp)
movzbl %r13b, %ecx
xorl %edi, %edi
movq %r15, %rdx
movq %r14, %r8
movq %r12, %r9
callq 0x36049
addq %r12, (%rbx)
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %r9
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0xc(%rsp), %edx
testb %dl, %dl
jne 0x20ac72
movq (%rbx), %rax
addq %rbp, %rax
cmpq %rax, 0x8(%rbx)
jae 0x20ac4e
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
movq %r10, %r14
movq %r11, %r15
callq 0x2370
movq %r15, %r11
movq %r14, %r10
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq %rax, 0x10(%rbx)
testb %r11b, %r11b
je 0x20ac72
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb $0x4e, (%rcx,%rax)
decq %rbp
jne 0x20ac5b
movq (%r9), %rax
cmpq %rax, %r10
jb 0x20aa6a
jmp 0x20acb1
movb $0x1, %dl
testl %r13d, %r13d
jne 0x20aca4
movl (%r15), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %r12
subq %rax, %r12
xorl %r11d, %r11d
cmpl %eax, %ecx
cmovbeq %r11, %r12
jmp 0x20ab0f
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %rax, %r14
jmp 0x20ab11
movq (%rbx), %rcx
jmp 0x20acb8
xorl %ecx, %ecx
cmpq 0x8(%rbx), %rcx
jne 0x20ace4
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0x20ace8
movq 0x10(%rbx), %rax
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