// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_ug_integer_seq_on_fly(ul_resolve_t*, unsigned long*, unsigned long, asg64_v*)
uint64_t gen_ug_integer_seq_on_fly(ul_resolve_t *uidx, uint64_t *u_a, uint64_t u_n, asg64_v *res)
{
    ul2ul_idx_t *idx = &(uidx->uovl); ma_ug_t *raw = uidx->l1_ug;
    uint64_t k, rn = res->n, nn, u_rev, uv, uw, bv, bw, is_bv_ul, is_bw_ul, x; int64_t z, zn;
    ma_ug_t *iug = idx->i_ug; uinfo_srt_warp_t *seq; ul2ul_t *m;
    for (k = 0; k < u_n; k++) {
        seq = &(idx->cc.iug_a[u_a[k]>>1]); u_rev = u_a[k]&1;
        // if(u_n == 6 && u_a[0] == 37683 && u_a[5] == 45894) {
        //     fprintf(stderr, "\n[M::%s::] k::%lu, u_a[k]>>1::%lu, u_a[k]&1::%lu\n", __func__, k, u_a[k]>>1, u_a[k]&1);
        //     for (z = 0, zn = seq->n; z < zn; z++) {
        //         fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u\n", __func__, seq->a[z].v>>1, seq->a[z].v&1);
        //     }
        // }
        if(k > 0) {
            uv = u_a[k-1]; uw = u_a[k];
            bv = iug->u.a[uv>>1].a[((uv&1)?(0):(iug->u.a[uv>>1].n-1))]>>32; if(uv&1) bv ^= 1; //pre
            bw = iug->u.a[uw>>1].a[((uw&1)?(iug->u.a[uw>>1].n-1):(0))]>>32; if(uw&1) bw ^= 1; //current
            is_bv_ul = ulg_type((*idx), (bv>>1)); is_bw_ul = ulg_type((*idx), (bw>>1)); 
            if(is_bw_ul) {
                m = get_ul_spec_ovlp(idx, bv>>1, bw>>1);
                assert(m && (!m->is_del));
                nn = res->n + seq->n - (m->te_k - m->ts_k); 
                kv_resize(uint64_t, *res, nn); 
                nn = m->te_k - m->ts_k; ///skipped length
                zn = seq->n; 
                // fprintf(stderr, "[M::%s::] bv>>1::%lu, bv&1::%lu, bw>>1::%lu, bw&1::%lu, m->qs_k::%u, m->qe_k::%u, m->ts_k::%u, m->te_k::%u\n", __func__, 
                // bv>>1, bv&1, bw>>1, bw&1, m->qs_k, m->qe_k, m->ts_k, m->te_k);
                if(!u_rev) {
                    ///debug
                    for (z = 0; z < ((int64_t)nn); z++) {
                        // if(res->a[res->n-nn+z] != seq->a[z].v) {
                        //     fprintf(stderr, "****[M::%s::] k::%lu, z::%ld, nn::%lu, res->n::%u, res->v>>1::%lu, res->v&1::%lu, seq->a[z].v>>1::%u, seq->a[z].v&1::%u\n", 
                        //     __func__, k, z, nn, (uint32_t)res->n, res->a[res->n-nn+z]>>1, res->a[res->n-nn+z]&1, seq->a[z].v>>1, seq->a[z].v&1);
                        // }
                        assert(res->a[res->n-nn+z] == seq->a[z].v);
                    }

                    for (z = nn; z < zn; z++) {
                        res->a[res->n++] = seq->a[z].v;
                    }
                } else {
                    ///debug
                    for (z = zn - 1; z >= zn - ((int64_t)nn); z--) {
                        // fprintf(stderr, "[M::%s::z->%ld::idx->%ld] nn::%lu, zn::%ld, res->n::%ld, res>>1::%lu, res&1::%lu, seq>>1::%u, seq&1::%u\n", 
                        // __func__, z, (int64_t)(res->n-nn+(zn-1-z)), nn, zn, (int64_t)(res->n), res->a[res->n-nn+(zn-1-z)]>>1, res->a[res->n-nn+(zn-1-z)]&1, seq->a[z].v>>1, seq->a[z].v&1);
                        assert(res->a[res->n-nn+(zn-1-z)] == (seq->a[z].v^1));
                    }

                    for (z = zn - 1 - ((int64_t)nn); z >= 0; z--) {
                        res->a[res->n++] = seq->a[z].v^1;
                    }
                }
            } else {
                x = ulg_id(uidx->uovl, (bw>>1)); x <<= 1; x += (bw&1);
                if(!is_bv_ul) {///if previous ul is also a raw utg node
                    assert(get_specfic_edge(raw->g, res->a[res->n-1], x));
                } else {
                    // assert(((res->a[res->n-1].v) == x));
                    if(((res->a[res->n-1]) == x)) {
                        res->n--;
                    } else {
                        assert(get_specfic_edge(raw->g, res->a[res->n-1], x));
                    }
                }
                nn = res->n + seq->n; zn = seq->n;
                kv_resize(uint64_t, *res, nn);
                if(!u_rev) {
                    for (z = 0; z < zn; z++) {
                        res->a[res->n++] = seq->a[z].v;
                    }
                } else {
                    for (z = zn - 1; z >= 0; z--) {
                        res->a[res->n++] = seq->a[z].v^1;
                    }
                }
            }
        } else {
            nn = res->n + seq->n; zn = seq->n;
            kv_resize(uint64_t, *res, nn);
            if(!u_rev) {
                for (z = 0; z < zn; z++) {
                    // fprintf(stderr, ">+<[M::%s::res->n->%ld] a>>1::%u, a&1::%u\n", 
                    //     __func__, (int64_t)res->n, seq->a[z].v>>1, seq->a[z].v&1);
                    res->a[res->n++] = seq->a[z].v;
                }
            } else {
                for (z = zn - 1; z >= 0; z--) {
                    // fprintf(stderr, ">-<[M::%s::res->n->%ld] a>>1::%u, a&1::%u\n", 
                    //     __func__, (int64_t)res->n, seq->a[z].v>>1, (seq->a[z].v^1)&1);
                    res->a[res->n++] = seq->a[z].v^1;
                }
            }
        }
    }
    
    return res->n - rn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbx
movq %rdx, 0x30(%rsp)
movq %rdi, %r12
leaq 0x1f0(%rdi), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
movq (%rcx), %rax
movq %rax, 0x10(%rsp)
movq 0x230(%rdi), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq %rsi, 0x28(%rsp)
cmpq 0x30(%rsp), %r13
je 0x1c1be0
movq (%rsi,%r13,8), %r15
movq %r15, %rdx
shrq %rdx
imulq $0x18, %rdx, %r14
addq 0x260(%r12), %r14
testq %r13, %r13
je 0x1c17e0
movq -0x8(%rsi,%r13,8), %rsi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq %rsi, %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
testb $0x1, %sil
jne 0x1c1881
leaq (%rax,%rcx), %rsi
movl 0x10(%rsi), %esi
decl %esi
jmp 0x1c1883
movq (%r14), %rbp
movq (%rbx), %rax
addq %rbp, %rax
cmpq %rax, 0x8(%rbx)
jae 0x1c1840
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
testb $0x1, %r15b
jne 0x1c19d4
testq %rbp, %rbp
movl $0x0, %eax
cmovleq %rax, %rbp
xorl %eax, %eax
subq $0x1, %rbp
jb 0x1c1bd3
movq 0x10(%r14), %rcx
movl (%rcx,%rax), %ecx
movq (%rbx), %rdx
movq 0x10(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rbx)
movq %rcx, (%rsi,%rdx,8)
addq $0x10, %rax
jmp 0x1c1858
xorl %esi, %esi
imulq $0x28, %rdx, %rdx
testb $0x1, %r15b
jne 0x1c1891
xorl %edi, %edi
jmp 0x1c189a
leaq (%rax,%rdx), %rdi
movl 0x10(%rdi), %edi
decl %edi
movq 0x18(%rax,%rcx), %rcx
movq (%rcx,%rsi,8), %rsi
shrq $0x21, %rsi
movq 0x18(%rax,%rdx), %rax
movq (%rax,%rdi,8), %rcx
movq 0x208(%r12), %rax
movq %rcx, %rdx
shrq $0x21, %rdx
movq %rdx, %rdi
subq %rax, %rdi
jae 0x1c19a1
movq 0x18(%rsp), %rdi
callq 0x1c16f3
testq %rax, %rax
je 0x1c1c35
movq %rax, %rbp
testb $0x40, 0x24(%rax)
jne 0x1c1c35
movq (%r14), %rax
movq (%rbx), %rdx
movl 0x20(%rbp), %ecx
subl 0x1c(%rbp), %ecx
addq %rax, %rdx
subq %rcx, %rdx
cmpq %rdx, 0x8(%rbx)
jae 0x1c195d
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %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
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movl 0x20(%rbp), %ecx
movq (%r14), %rax
subl 0x1c(%rbp), %ecx
testb $0x1, %r15b
jne 0x1c1a31
movq %rcx, %rdx
negq %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x1c1a86
movq (%rbx), %rdi
shlq $0x3, %rdi
addq 0x10(%rbx), %rdi
movq 0x10(%r14), %r8
movl (%r8,%rsi), %r8d
leaq 0x1(%rdx), %r9
addq $0x10, %rsi
cmpq %r8, (%rdi,%rdx,8)
movq %r9, %rdx
je 0x1c196f
jmp 0x1c1c16
shrq $0x20, %rcx
xorl %r15d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rdi,2), %rdx
cmpq %rax, %rsi
jae 0x1c1a0a
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rsi
cmpq %rdx, %rsi
jne 0x1c1af1
decq %rax
movq %rax, (%rbx)
jmp 0x1c1b08
movq %rbp, %rax
shlq $0x4, %rax
addq $-0x10, %rax
testq %rbp, %rbp
jle 0x1c1bd3
decq %rbp
movq 0x10(%r14), %rcx
movl (%rcx,%rax), %ecx
xorq $0x1, %rcx
movq (%rbx), %rdx
movq 0x10(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rbx)
movq %rcx, (%rsi,%rdx,8)
jmp 0x1c19db
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movl -0x8(%rcx,%rax,8), %esi
callq 0x1de62d
testq %rax, %rax
jne 0x1c1b08
jmp 0x1c1c54
leaq -0x1(%rax), %rdx
movq %rax, %rsi
subq %rcx, %rsi
leaq (,%rcx,8), %rdi
negq %rdi
shlq $0x4, %rax
addq $-0x10, %rax
movq %rdx, %r8
cmpq %rsi, %r8
jl 0x1c1ab8
movq (%rbx), %r9
shlq $0x3, %r9
addq 0x10(%rbx), %r9
movq 0x10(%r14), %r10
movl (%r10,%rax), %r10d
xorq $0x1, %r10
decq %r8
leaq 0x8(%rdi), %r11
addq $-0x10, %rax
cmpq %r10, (%rdi,%r9)
movq %r11, %rdi
je 0x1c1a51
jmp 0x1c1bf7
movq %rcx, %rdx
shlq $0x4, %rdx
cmpq %rax, %rcx
jge 0x1c1bd3
movq 0x10(%r14), %rsi
movl (%rsi,%rdx), %esi
movq (%rbx), %rdi
movq 0x10(%rbx), %r8
leaq 0x1(%rdi), %r9
movq %r9, (%rbx)
movq %rsi, (%r8,%rdi,8)
incq %rcx
addq $0x10, %rdx
jmp 0x1c1a8d
subq %rcx, %rdx
movq %rdx, %rax
shlq $0x4, %rax
testq %rdx, %rdx
js 0x1c1bd3
movq 0x10(%r14), %rcx
movl (%rcx,%rax), %ecx
xorq $0x1, %rcx
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
leaq 0x1(%rsi), %r8
movq %r8, (%rbx)
movq %rcx, (%rdi,%rsi,8)
decq %rdx
addq $-0x10, %rax
jmp 0x1c1ac2
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x1de62d
testq %rax, %rax
je 0x1c1c73
movq (%r14), %rbp
movq (%rbx), %rax
addq %rbp, %rax
cmpq %rax, 0x8(%rbx)
jae 0x1c1b68
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
testb $0x1, %r15b
jne 0x1c1ba1
testq %rbp, %rbp
movl $0x0, %eax
cmovleq %rax, %rbp
xorl %eax, %eax
subq $0x1, %rbp
jb 0x1c1bd3
movq 0x10(%r14), %rcx
movl (%rcx,%rax), %ecx
movq (%rbx), %rdx
movq 0x10(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rbx)
movq %rcx, (%rsi,%rdx,8)
addq $0x10, %rax
jmp 0x1c1b7c
movq %rbp, %rax
shlq $0x4, %rax
addq $-0x10, %rax
testq %rbp, %rbp
jle 0x1c1bd3
decq %rbp
movq 0x10(%r14), %rcx
movl (%rcx,%rax), %ecx
xorq $0x1, %rcx
movq (%rbx), %rdx
movq 0x10(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%rbx)
movq %rcx, (%rsi,%rdx,8)
jmp 0x1c1ba8
incq %r13
movq 0x28(%rsp), %rsi
jmp 0x1c178a
movq (%rbx), %rax
subq 0x10(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x383b4(%rip), %rdi     # 0x1f9fb2
leaq 0x358d4(%rip), %rsi     # 0x1f74d9
leaq 0x3832f(%rip), %rcx     # 0x1f9f3b
movl $0x2420, %edx           # imm = 0x2420
callq 0x2110
leaq 0x38372(%rip), %rdi     # 0x1f9f8f
leaq 0x358b5(%rip), %rsi     # 0x1f74d9
leaq 0x38310(%rip), %rcx     # 0x1f9f3b
movl $0x2415, %edx           # imm = 0x2415
callq 0x2110
leaq 0x382ed(%rip), %rdi     # 0x1f9f29
leaq 0x35896(%rip), %rsi     # 0x1f74d9
leaq 0x382f1(%rip), %rcx     # 0x1f9f3b
movl $0x2407, %edx           # imm = 0x2407
callq 0x2110
leaq 0x38385(%rip), %rdi     # 0x1f9fe0
leaq 0x35877(%rip), %rsi     # 0x1f74d9
leaq 0x382d2(%rip), %rcx     # 0x1f9f3b
movl $0x242a, %edx           # imm = 0x242A
callq 0x2110
leaq 0x38366(%rip), %rdi     # 0x1f9fe0
leaq 0x35858(%rip), %rsi     # 0x1f74d9
leaq 0x382b3(%rip), %rcx     # 0x1f9f3b
movl $0x2430, %edx           # imm = 0x2430
callq 0x2110