// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ma_ug_hybrid_gen(usg_t*)
ma_ug_t *ma_ug_hybrid_gen(usg_t *g)
{
    int32_t *mark; uint32_t i, v, n_vtx = g->n<<1;
    uint32_t w, x, l, start, end, len; ma_utg_t *p;
    kdq_t(uint64_t) *q; ///is a queue
    ma_ug_t *ug;

    ug = (ma_ug_t*)calloc(1, sizeof(ma_ug_t));
    ug->g = asg_init();
    ///each node has two directions
    mark = (int32_t*)calloc(n_vtx, 4);

    q = kdq_init(uint64_t);
    for (v = 0; v < n_vtx; ++v) {
        // fprintf(stderr, "+[M::%s::] v::%u, n_vtx::%u\n", __func__, v, n_vtx);
        if (g->a[v>>1].del || mark[v]) continue;
        if (usg_arc_n(g, v) == 0 && usg_arc_n(g, (v^1)) != 0) continue;
        // fprintf(stderr, "-[M::%s::] v::%u, n_vtx::%u\n", __func__, v, n_vtx);
        mark[v] = 1; q->count = 0, start = v, end = v^1, len = 0;
        // forward
        w = v;
        while (1) {
            /**
             * w----->x
             * w<-----x
             * that means the only suffix of w is x, and the only prefix of x is w
             **/
            if (usg_arc_n(g, w) != 1) break;
            x = usg_arc_a(g, w)[0].v; // w->x
            if (usg_arc_n(g, x^1) != 1) break;

            /**
             * another direction of w would be marked as used (since w has been used)
            **/
            mark[x] = mark[w^1] = 1;
            ///l is the edge length, instead of overlap length
            ///note: edge length is different with overlap length
            l = asg_arc_len(usg_arc_a(g, w)[0]);
            kdq_push(uint64_t, q, (uint64_t)w<<32 | l);
            end = x^1, len += l;
            w = x;
            if (x == v) break;
        }
        if (start != (end^1) || kdq_size(q) == 0) { // linear unitig
            ///length of seq, instead of edge
            l = g->a[end>>1].len;
            kdq_push(uint64_t, q, (uint64_t)(end^1)<<32 | l);
            len += l;
        } else { // circular unitig
            start = end = UINT32_MAX;
            goto add_usg_unitig; // then it is not necessary to do the backward
        }

        // backward
        x = v;
        while (1) { // similar to forward but not the same
            if (usg_arc_n(g, x^1) != 1) break;
            w = usg_arc_a(g, x^1)[0].v ^ 1; // w->x
            if (usg_arc_n(g, w) != 1) break;
            mark[x] = mark[w^1] = 1;
            l = asg_arc_len(usg_arc_a(g, w)[0]);
            ///w is the seq id + direction, l is the length of edge
            ///push element to the front of a queue
            kdq_unshift(uint64_t, q, (uint64_t)w<<32 | l);

            // fprintf(stderr, "uId: %u, >%.*s (%u)\n", 
            // ug->u.n, (int)Get_NAME_LENGTH((R_INF), w>>1), Get_NAME((R_INF), w>>1), w>>1);

            start = w, len += l;
            x = w;
        }

        add_usg_unitig:
        if (start != UINT32_MAX) mark[start] = mark[end] = 1;
        kv_pushp(ma_utg_t, ug->u, &p);
        p->s = 0, p->start = start, p->end = end, p->len = len, p->n = kdq_size(q), p->circ = (start == UINT32_MAX);
        p->m = p->n;
        kv_roundup32(p->m);
        p->a = (uint64_t*)malloc(8 * p->m);
        //all elements are saved here
        for (i = 0; i < kdq_size(q); ++i)
            p->a[i] = kdq_at(q, i);
        // fprintf(stderr, "*[M::%s::] v::%u, n_vtx::%u\n", __func__, v, n_vtx);
    }
    kdq_destroy(uint64_t, q);
    // fprintf(stderr, "-1-[M::%s::] **************\n", __func__);
    // add arcs between unitigs; reusing mark for a different purpose
    //ug saves all unitigs
    for (v = 0; v < n_vtx; ++v) mark[v] = -1;
    // fprintf(stderr, "-2-[M::%s::] **************\n", __func__);

    //mark all start nodes and end nodes of all unitigs
    for (i = 0; i < ug->u.n; ++i) {
        if (ug->u.a[i].circ) continue;
        mark[ug->u.a[i].start] = i<<1 | 0;
        mark[ug->u.a[i].end] = i<<1 | 1;
    }
    // fprintf(stderr, "-3-[M::%s::] **************\n", __func__);
    //scan all edges
    usg_arc_t *av; uint32_t nv;
    for (v = 0; v < n_vtx; v++) {
        av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (i = 0; i < nv; i++) {
            if(av[i].del) continue;
            if (mark[av[i].ul>>32^1] >= 0 && mark[av[i].v] >= 0) {
                uint32_t u = mark[(av[i].ul>>32)^1]^1;
                int l = ug->u.a[u>>1].len - av[i].ol;
                if (l < 0) l = 1;
                asg_arc_t *q = asg_arc_pushp(ug->g);
                q->ol = av[i].ol, q->del = 0;
                q->ul = (uint64_t)u<<32 | l;
                q->v = mark[av[i].v]; q->ou = 0;
            }
        }
    }
    // fprintf(stderr, "-4-[M::%s::] **************\n", __func__);
    for (i = 0; i < ug->u.n; ++i)
        asg_seq_set(ug->g, i, ug->u.a[i].len, 0);
    // fprintf(stderr, "-5-[M::%s::] **************\n", __func__);
    asg_cleanup(ug->g);
    free(mark);
    return ug;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movabsq $0x3ffffffffffffff, %r15 # imm = 0x3FFFFFFFFFFFFFF
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x38(%rsp)
leal (%rax,%rax), %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %rbx
callq 0x9fccc
movq %rbx, (%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x18(%rbx)
movl $0x4, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %rbx
movq (%rax), %rax
andq %r15, %rax
btsq $0x3b, %rax
movq %rax, (%rbx)
movq $0x3, 0x10(%rbx)
movl $0x20, %edi
callq 0x2280
movq %rbx, %r9
movq %rax, 0x18(%rbx)
movq %r14, 0x28(%rsp)
testl %r14d, %r14d
je 0x20f726
movl 0x28(%rsp), %r10d
xorl %r13d, %r13d
movq %r9, 0x8(%rsp)
movq %r10, 0x40(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
jne 0x20f71a
movq 0x10(%rsp), %rdx
cmpl $0x0, (%rdx,%r13,4)
jne 0x20f71a
addq %rcx, %rax
movl %r13d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpq $0x0, 0x10(%rax,%rcx,8)
je 0x20f5bf
movq 0x10(%rsp), %r11
movl $0x1, (%r11,%r13,4)
movq $0x0, 0x8(%r9)
movl %r13d, %eax
xorl $0x1, %eax
movl %eax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shrl %ecx
movl %r13d, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rsi
addq %rax, %rsi
leaq (%rdx,%rdx,2), %rdi
xorl %r12d, %r12d
cmpq $0x1, 0x10(%rsi,%rdi,8)
jne 0x20f3d6
leaq (%rsi,%rdi,8), %rsi
addq $0x10, %rsi
movq 0x10(%rsi), %rsi
movl 0x8(%rsi), %edi
movq %rdi, %rsi
xorq $0x1, %rsi
movl %edi, %r8d
shrl %r8d
movl %esi, %r9d
andl $0x1, %r9d
imulq $0x78, %r8, %r8
addq %rax, %r8
leaq (%r9,%r9,2), %r9
cmpq $0x1, 0x10(%r8,%r9,8)
movq 0x8(%rsp), %r9
movq (%rsp), %r10
jne 0x20f3da
xorl %r12d, %r12d
movl %r13d, %r14d
movl %edi, %ebx
movl %r14d, %edi
movl $0x1, %r8d
xorl %r8d, %edi
movl %r8d, (%r11,%rdi,4)
movl %ebx, %r15d
movl %r8d, (%r11,%r15,4)
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rax
movq 0x20(%rcx,%rax,8), %rax
movq (%r9), %rcx
shrq $0x3a, %rcx
movl $0x1, %edx
shlq %cl, %rdx
movl %esi, 0x18(%rsp)
movq (%rax), %rbp
cmpq %rdx, 0x8(%r9)
jne 0x20f332
incl %ecx
movq %r9, %rdi
movl %ecx, %esi
callq 0x221163
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r11
movq (%rsp), %r10
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movq (%r9), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rax, %rdx
movl %ebp, %eax
shlq $0x20, %r14
andq 0x10(%r9), %rdx
orq %rax, %r14
movq %r14, (%rcx,%rdx,8)
addl %ebp, %r12d
cmpq %r15, %r13
je 0x20f3da
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %ebx, %ecx
shrl %ecx
movl %ebx, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rsi
addq %rax, %rsi
leaq (%rdx,%rdx,2), %rdi
cmpq $0x1, 0x10(%rsi,%rdi,8)
jne 0x20f3da
leaq (%rsi,%rdi,8), %rsi
addq $0x10, %rsi
movq 0x10(%rsi), %rsi
movl 0x8(%rsi), %edi
movq %rdi, %rsi
xorq $0x1, %rsi
movl %edi, %r8d
shrl %r8d
movl %esi, %r9d
andl $0x1, %r9d
imulq $0x78, %r8, %r8
addq %rax, %r8
leaq (%r9,%r9,2), %r9
cmpq $0x1, 0x10(%r8,%r9,8)
movq 0x8(%rsp), %r9
movl %ebx, %r14d
je 0x20f2d3
jmp 0x20f3da
movq (%rsp), %r10
movl 0x18(%rsp), %eax
movl %eax, %ebx
xorl $0x1, %ebx
cmpq %rbx, %r13
jne 0x20f3fe
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq $0x0, 0x8(%r9)
je 0x20f3fe
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x20f5e9
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl 0x18(%rsp), %ecx
movl %ecx, %edx
shrl %edx
movq (%r9), %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
imulq $0x78, %rdx, %rdx
movl 0x8(%rax,%rdx), %r14d
cmpq %rsi, 0x8(%r9)
jne 0x20f446
incl %ecx
movq %r9, %rdi
movl %ecx, %esi
callq 0x221163
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r11
movq (%rsp), %r10
shlq $0x20, %rbx
orq %r14, %rbx
movq 0x8(%r9), %rax
movq 0x18(%r9), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r9)
movq (%r9), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rax, %rdx
andq 0x10(%r9), %rdx
movq %rbx, (%rcx,%rdx,8)
addl %r14d, %r12d
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shrl %ecx
movl %r13d, %edx
notl %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
cmpq $0x1, 0x10(%rcx,%rdx,8)
jne 0x20f5dc
leaq (%rcx,%rdx,8), %rcx
addq $0x10, %rcx
movl %r13d, %ebp
movq 0x10(%rcx), %rcx
movl 0x8(%rcx), %r15d
movq %r15, %rbx
xorq $0x1, %rbx
movl %r15d, %ecx
shrl %ecx
movl %ebx, %esi
andl $0x1, %esi
imulq $0x78, %rcx, %rdx
addq %rax, %rdx
leaq (%rsi,%rsi,2), %rax
cmpq $0x1, 0x10(%rdx,%rax,8)
jne 0x20f5e5
movl $0x1, %esi
movl %esi, (%r11,%r15,4)
movl %ebp, %ecx
movl %esi, (%r11,%rcx,4)
movq (%r9), %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
movq 0x20(%rdx,%rax,8), %rax
movq (%rax), %r14
cmpq %rsi, 0x8(%r9)
jne 0x20f528
incl %ecx
movq %r9, %rdi
movl %ecx, %esi
callq 0x221163
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r11
movq (%rsp), %r10
movl %r14d, %eax
movq %rbx, %rdx
shlq $0x20, %rdx
orq %rax, %rdx
incq 0x8(%r9)
movq (%r9), %rax
movq %rax, %rcx
shrq $0x3a, %rcx
movq $-0x1, %rsi
shlq %cl, %rsi
movq 0x18(%r9), %rcx
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
leaq (%rax,%r8), %rdi
testq %r8, %rax
notq %rsi
cmovneq %rdi, %rsi
andq %r8, %rsi
movabsq $-0x400000000000000, %rdi # imm = 0xFC00000000000000
andq %rdi, %rax
orq %rsi, %rax
movq %rax, (%r9)
movq %rdx, (%rcx,%rsi,8)
addl %r14d, %r12d
movq 0x20(%rsp), %rax
movq (%rax), %rax
shrl %r15d
movl %ebx, %ecx
notl %ecx
andl $0x1, %ecx
imulq $0x78, %r15, %rdx
addq %rax, %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0x10, %rcx
cmpq $0x1, (%rcx)
movl %ebx, %ebp
je 0x20f4b2
movl 0x18(%rsp), %edx
movl %ebx, %ebp
jmp 0x20f5e9
movl %r13d, %ecx
notl %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpq $0x0, 0x10(%rax,%rcx,8)
jne 0x20f71a
jmp 0x20f23e
movl 0x18(%rsp), %edx
movl %r13d, %ebp
jmp 0x20f5e9
movl 0x18(%rsp), %edx
cmpl $-0x1, %ebp
je 0x20f5ff
movl %edx, %eax
movl $0x1, %ecx
movl %ecx, (%r11,%rax,4)
movl %ebp, %eax
movl %ecx, (%r11,%rax,4)
movq (%r10), %rbx
movq 0x8(%r10), %rax
cmpq %rax, %rbx
jne 0x20f643
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r10)
movq 0x10(%r10), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
movl %edx, %r14d
callq 0x2370
movl %r14d, %edx
movq 0x8(%rsp), %r9
movq (%rsp), %r10
movq %rax, 0x10(%r10)
xorl %eax, %eax
cmpl $-0x1, %ebp
sete %al
movq 0x10(%r10), %r15
leaq 0x1(%rbx), %rcx
movq %rcx, (%r10)
leaq (%rbx,%rbx,4), %r14
movq $0x0, 0x20(%r15,%r14,8)
movl %ebp, 0x4(%r15,%r14,8)
movl %edx, 0x8(%r15,%r14,8)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl (%r15,%r14,8), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %r12d, %ecx
movl %ecx, (%r15,%r14,8)
movq 0x8(%r9), %rbx
movl %ebx, 0x10(%r15,%r14,8)
shll $0x1f, %eax
orl %r12d, %eax
movl %eax, (%r15,%r14,8)
leal -0x1(%rbx), %eax
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
leal 0x1(%rcx), %eax
movl %eax, 0xc(%r15,%r14,8)
leal 0x8(,%rcx,8), %edi
callq 0x2280
movq 0x8(%rsp), %r9
movq %rax, 0x18(%r15,%r14,8)
testq %rbx, %rbx
movq 0x40(%rsp), %r10
je 0x20f71a
movq 0x10(%r9), %rcx
movq 0x18(%r9), %rdx
movq (%r9), %rsi
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
andq %rdi, %rsi
xorl %edi, %edi
leaq (%rsi,%rdi), %r8
andq %rcx, %r8
movq (%rdx,%r8,8), %r8
movq %r8, (%rax,%rdi,8)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rbx
ja 0x20f700
incq %r13
cmpq %r10, %r13
jne 0x20f1f9
movq 0x18(%r9), %rdi
movq %r9, %rbx
callq 0x2220
movq %rbx, %rdi
callq 0x2220
cmpl $0x0, 0x28(%rsp)
je 0x20f75f
movq 0x38(%rsp), %rdx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shlq $0x3, %rdx
movq 0x10(%rsp), %rdi
movl $0xff, %esi
callq 0x20e0
movq (%rsp), %r8
movq (%r8), %r11
testq %r11, %r11
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %r10
je 0x20f7ae
movq 0x10(%r8), %rax
addq $0x8, %rax
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x0, -0x8(%rax)
js 0x20f79e
leal -0x1(%rcx), %esi
movl -0x4(%rax), %r9d
movl %esi, (%r10,%r9,4)
movl (%rax), %esi
movl %ecx, (%r10,%rsi,4)
movl %edx, %esi
addq $0x28, %rax
incl %edx
addl $0x2, %ecx
cmpq %rsi, %r11
ja 0x20f787
movq %r11, 0x18(%rsp)
cmpl $0x0, 0x28(%rsp)
je 0x20f8ff
xorl %ecx, %ecx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %ecx, %eax
shrl %eax
movq %rcx, 0x8(%rsp)
imulq $0x78, %rax, %rax
movq 0x20(%rsp), %rdx
addq (%rdx), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x10(%rax,%rcx,8), %ebx
testq %rbx, %rbx
je 0x20f8ee
movq 0x20(%rax,%rcx,8), %r13
shlq $0x5, %rbx
xorl %r15d, %r15d
movl 0xc(%r13,%r15), %eax
testl %eax, %eax
js 0x20f8e1
movl 0x4(%r13,%r15), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%r10,%rcx), %r14d
testl %r14d, %r14d
js 0x20f8e1
movl 0x8(%r13,%r15), %ecx
cmpl $0x0, (%r10,%rcx,4)
js 0x20f8e1
movq 0x10(%r8), %rcx
movl %r14d, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl %ebp, %ecx
subl %eax, %ecx
movl $0x1, %r12d
js 0x20f850
movl %ecx, %r12d
movl 0x4(%rdi), %ecx
andl %ebp, %ecx
movl (%rdi), %eax
cmpl %eax, %ecx
jne 0x20f88d
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%rdi)
movq 0x8(%rdi), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x30(%rsp), %rdi
movq (%rsp), %r8
movq %rax, 0x8(%rdi)
xorl $0x1, %r14d
movq 0x8(%rdi), %rax
movl 0x4(%rdi), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl %ebp, %esi
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%rdi)
leaq (%rdx,%rdx,2), %rcx
movl 0xc(%r13,%r15), %edx
andl %ebp, %edx
movl %edx, 0xc(%rax,%rcx,8)
shlq $0x20, %r14
movslq %r12d, %rdx
orq %r14, %rdx
movq %rdx, (%rax,%rcx,8)
movl 0x8(%r13,%r15), %edx
movl (%r10,%rdx,4), %edx
movl %edx, 0x8(%rax,%rcx,8)
andw $0xc000, 0x10(%rax,%rcx,8) # imm = 0xC000
addq $0x20, %r15
cmpq %r15, %rbx
jne 0x20f7fa
movq 0x8(%rsp), %rcx
incl %ecx
cmpl 0x28(%rsp), %ecx
jne 0x20f7c5
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0x20f939
movq (%rsp), %rax
movq 0x10(%rax), %rbx
xorl %r12d, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r12,%r12,4), %rax
movl (%rbx,%rax,8), %edx
andl %ebp, %edx
movl %r12d, %esi
xorl %ecx, %ecx
callq 0xa0115
movq 0x30(%rsp), %rdi
incl %r12d
cmpq %r12, %r14
ja 0x20f919
callq 0xa02fa
movq 0x10(%rsp), %rdi
callq 0x2220
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq