// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, %rbx
movq (%rcx), %rax
movq %rax, 0x18(%rsp)
testq %rdx, %rdx
je 0x20341d
movq %rdx, %r10
movq %rsi, %r11
movq %rdi, %r12
movq 0x230(%rdi), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq %rdx, 0x8(%rsp)
movq %rsi, (%rsp)
movq (%r11,%r13,8), %r14
movq %r14, %rdx
shrq %rdx
leaq (%rdx,%rdx,2), %rbp
shlq $0x3, %rbp
addq 0x260(%r12), %rbp
testq %r13, %r13
je 0x202f35
movq -0x8(%r11,%r13,8), %rsi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq %rsi, %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
testb $0x1, %sil
jne 0x202fd8
leaq (%rax,%rcx,8), %rsi
movl 0x10(%rsi), %esi
decl %esi
jmp 0x202fda
movq (%rbp), %r15
movq (%rbx), %rax
addq %r15, %rax
cmpq %rax, 0x8(%rbx)
jae 0x202f9f
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 (%rsp), %r11
movq 0x8(%rsp), %r10
movq %rax, 0x10(%rbx)
testb $0x1, %r14b
jne 0x2031a8
testq %r15, %r15
jle 0x203411
movq 0x10(%rbp), %rax
movq 0x10(%rbx), %rcx
movl (%rax), %edx
movq (%rbx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rbx)
movq %rdx, (%rcx,%rsi,8)
addq $0x10, %rax
decq %r15
jne 0x202fba
jmp 0x203411
xorl %esi, %esi
leaq (%rdx,%rdx,4), %rdx
testb $0x1, %r14b
jne 0x202fe8
xorl %edi, %edi
jmp 0x202ff1
leaq (%rax,%rdx,8), %rdi
movl 0x10(%rdi), %edi
decl %edi
movq 0x18(%rax,%rcx,8), %rcx
movq (%rcx,%rsi,8), %rcx
shrq $0x21, %rcx
movq 0x18(%rax,%rdx,8), %rax
movq (%rax,%rdi,8), %rsi
movq 0x208(%r12), %rdx
movq %rsi, %rax
shrq $0x21, %rax
movq %rax, %rdi
subq %rdx, %rdi
jae 0x203175
movq 0x220(%r12), %rdx
movl (%rdx,%rcx,4), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rdx
je 0x203060
movq 0x1f0(%r12), %rsi
shlq $0x5, %rdx
movl 0x1c(%rsi,%rdx), %ecx
testq %rcx, %rcx
je 0x203060
addq %rdx, %rsi
movq (%rsi), %r15
cmpl (%r15), %eax
je 0x203063
addq $0x28, %r15
decq %rcx
jne 0x203052
xorl %r15d, %r15d
testq %r15, %r15
je 0x203472
testb $0x40, 0x24(%r15)
jne 0x203472
movq (%rbp), %rax
addq (%rbx), %rax
movl 0x20(%r15), %ecx
subl 0x1c(%r15), %ecx
subq %rcx, %rax
cmpq %rax, 0x8(%rbx)
jae 0x2030e9
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 (%rsp), %r11
movq 0x8(%rsp), %r10
movq %rax, 0x10(%rbx)
movl 0x1c(%r15), %esi
movl 0x20(%r15), %edi
movl %edi, %eax
subl %esi, %eax
movq (%rbp), %rcx
testb $0x1, %r14b
jne 0x20324f
cmpl %esi, %edi
je 0x203142
movq (%rbx), %rdx
subq %rax, %rdx
shlq $0x3, %rdx
addq 0x10(%rbx), %rdx
movq 0x10(%rbp), %rsi
cmpq $0x1, %rax
movq %rax, %rdi
adcq $0x0, %rdi
shlq $0x3, %rdi
xorl %r8d, %r8d
movl (%rsi,%r8,2), %r9d
cmpq %r9, (%rdx,%r8)
jne 0x203453
addq $0x8, %r8
cmpq %r8, %rdi
jne 0x20312b
subq %rax, %rcx
jle 0x203411
movq 0x10(%rbx), %rdx
shlq $0x4, %rax
addq 0x10(%rbp), %rax
movl (%rax), %esi
movq (%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rbx)
movq %rsi, (%rdx,%rdi,8)
addq $0x10, %rax
decq %rcx
jne 0x203157
jmp 0x203411
shrq $0x20, %rsi
xorl %r14d, %esi
andl $0x1, %esi
leaq (%rsi,%rdi,2), %rax
cmpq %rdx, %rcx
jae 0x2031ee
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
movq -0x8(%rdx,%rcx,8), %rdx
cmpq %rax, %rdx
jne 0x2032e9
decq %rcx
movq %rcx, (%rbx)
jmp 0x20333c
testq %r15, %r15
jle 0x203411
movq 0x10(%rbp), %rdx
movq 0x10(%rbx), %rax
leaq 0x1(%r15), %rcx
shlq $0x4, %r15
addq %r15, %rdx
addq $-0x10, %rdx
movl (%rdx), %esi
xorq $0x1, %rsi
movq (%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rbx)
movq %rsi, (%rax,%rdi,8)
decq %rcx
addq $-0x10, %rdx
cmpq $0x1, %rcx
jg 0x2031c8
jmp 0x203411
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
movl -0x8(%rdx,%rcx,8), %ecx
movq 0x28(%rsi), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rcx
je 0x203491
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%rsi), %rdx
cmpl $0x0, 0xc(%rdx)
js 0x203236
cmpl %eax, 0x8(%rdx)
je 0x203241
addq $0x18, %rdx
decq %rcx
jne 0x20322b
xorl %edx, %edx
testq %rdx, %rdx
jne 0x20333c
jmp 0x203491
movq %rcx, %rdx
subq %rax, %rdx
cmpl %esi, %edi
je 0x20329c
movq (%rbx), %rsi
subq %rax, %rsi
shlq $0x3, %rsi
addq 0x10(%rbx), %rsi
movq 0x10(%rbp), %rdi
movq %rcx, %r8
shlq $0x4, %r8
addq %r8, %rdi
addq $-0x10, %rdi
movq %rcx, %r8
movl (%rdi), %r9d
xorq $0x1, %r9
cmpq %r9, (%rsi)
jne 0x203434
decq %r8
addq $-0x10, %rdi
addq $0x8, %rsi
cmpq %rdx, %r8
jg 0x20327c
leaq -0x1(%rcx), %rsi
cmpq %rax, %rsi
js 0x203411
movq 0x10(%rbp), %rdi
movq 0x10(%rbx), %rsi
shlq $0x4, %rcx
shlq $0x4, %rax
subq %rax, %rcx
leaq (%rdi,%rcx), %rax
addq $-0x10, %rax
movl (%rax), %ecx
xorq $0x1, %rcx
movq (%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rbx)
movq %rcx, (%rsi,%rdi,8)
decq %rdx
addq $-0x10, %rax
testq %rdx, %rdx
jg 0x2032c4
jmp 0x203411
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq 0x28(%rsi), %rcx
movl %edx, %edx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rcx
je 0x2034b0
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%rsi), %rdx
cmpl $0x0, 0xc(%rdx)
js 0x203328
cmpl %eax, 0x8(%rdx)
je 0x203333
addq $0x18, %rdx
decq %rcx
jne 0x20331d
xorl %edx, %edx
testq %rdx, %rdx
je 0x2034b0
movq (%rbp), %r15
movq (%rbx), %rax
addq %r15, %rax
cmpq %rax, 0x8(%rbx)
jae 0x2033a6
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 (%rsp), %r11
movq 0x8(%rsp), %r10
movq %rax, 0x10(%rbx)
testb $0x1, %r14b
jne 0x2033d4
testq %r15, %r15
jle 0x203411
movq 0x10(%rbp), %rax
movq 0x10(%rbx), %rcx
movl (%rax), %edx
movq (%rbx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rbx)
movq %rdx, (%rcx,%rsi,8)
addq $0x10, %rax
decq %r15
jne 0x2033b9
jmp 0x203411
testq %r15, %r15
jle 0x203411
movq 0x10(%rbp), %rdx
movq 0x10(%rbx), %rax
leaq 0x1(%r15), %rcx
shlq $0x4, %r15
addq %r15, %rdx
addq $-0x10, %rdx
movl (%rdx), %esi
xorq $0x1, %rsi
movq (%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rbx)
movq %rsi, (%rax,%rdi,8)
decq %rcx
addq $-0x10, %rdx
cmpq $0x1, %rcx
jg 0x2033f0
incq %r13
cmpq %r10, %r13
jne 0x202ee6
movq (%rbx), %rax
subq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x388d4(%rip), %rdi     # 0x23bd0f
leaq 0x35ee3(%rip), %rsi     # 0x239325
leaq 0x3884f(%rip), %rcx     # 0x23bc98
movl $0x2420, %edx           # imm = 0x2420
callq 0x2110
leaq 0x38892(%rip), %rdi     # 0x23bcec
leaq 0x35ec4(%rip), %rsi     # 0x239325
leaq 0x38830(%rip), %rcx     # 0x23bc98
movl $0x2415, %edx           # imm = 0x2415
callq 0x2110
leaq 0x3880d(%rip), %rdi     # 0x23bc86
leaq 0x35ea5(%rip), %rsi     # 0x239325
leaq 0x38811(%rip), %rcx     # 0x23bc98
movl $0x2407, %edx           # imm = 0x2407
callq 0x2110
leaq 0x388a5(%rip), %rdi     # 0x23bd3d
leaq 0x35e86(%rip), %rsi     # 0x239325
leaq 0x387f2(%rip), %rcx     # 0x23bc98
movl $0x242a, %edx           # imm = 0x242A
callq 0x2110
leaq 0x38886(%rip), %rdi     # 0x23bd3d
leaq 0x35e67(%rip), %rsi     # 0x239325
leaq 0x387d3(%rip), %rcx     # 0x23bc98
movl $0x2430, %edx           # imm = 0x2430
callq 0x2110