// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x50(%rsp)
leal (%rax,%rax), %r14d
pushq $0x1
popq %r12
pushq $0x38
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, %rbx
callq 0x68f5d
movq %rbx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rax, 0x18(%rbx)
pushq $0x4
popq %rsi
movq %r14, 0x48(%rsp)
movq %r14, %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
pushq $0x20
popq %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2120
movq %rax, %rbx
movabsq $0x800000000000000, %rax # imm = 0x800000000000000
movq %rax, (%rbx)
movq $0x3, 0x10(%rbx)
movq %rbp, %rdi
callq 0x2280
movq %rax, 0x18(%rbx)
xorl %r14d, %r14d
movq $0x0, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r9d, %r9d
cmpq 0x48(%rsp), %r9
je 0x1cc03e
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r9d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
jne 0x1cc036
movq 0x8(%rsp), %rdx
cmpl $0x0, (%rdx,%r9,4)
jne 0x1cc036
addq %rcx, %rax
movl %r9d, %ecx
andl $0x1, %ecx
imulq $0x18, %rcx, %rcx
cmpq $0x0, 0x10(%rax,%rcx)
je 0x1cbef1
movq %r14, 0x28(%rsp)
movl $0x1, (%rdx,%r9,4)
andq $0x0, 0x8(%rbx)
movl %r9d, %eax
xorl $0x1, %eax
xorl %r14d, %r14d
movl %r9d, %esi
movq %r9, 0x10(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movl %esi, %edx
shrl %edx
movl %esi, %edi
andl $0x1, %edi
imulq $0x78, %rdx, %rdx
addq %rcx, %rdx
imulq $0x18, %rdi, %rdi
cmpq $0x1, 0x10(%rdi,%rdx)
jne 0x1cbd70
addq %rdi, %rdx
addq $0x10, %rdx
movq 0x10(%rdx), %rdx
movl 0x8(%rdx), %r13d
movq %r13, %r12
xorq $0x1, %r12
movl %r13d, %edi
shrl %edi
movl %r12d, %r8d
andl $0x1, %r8d
imulq $0x78, %rdi, %rdi
addq %rdi, %rcx
imulq $0x18, %r8, %rdi
cmpq $0x1, 0x10(%rdi,%rcx)
jne 0x1cbd70
movl %esi, %eax
pushq $0x1
popq %rcx
movq %rcx, %rdi
xorl %edi, %eax
movq 0x8(%rsp), %rcx
movl %edi, (%rcx,%rax,4)
movl %edi, (%rcx,%r13,4)
movq (%rdx), %rbp
movl %ebp, %eax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x1cc1e3
movq 0x10(%rsp), %r9
addl %ebp, %r14d
movl %r12d, %eax
movl %r13d, %esi
cmpq %r13, %r9
jne 0x1cbcd4
jmp 0x1cbd73
movl %eax, %r12d
movl %r12d, %esi
xorl $0x1, %esi
cmpq %rsi, %r9
jne 0x1cbd98
movl $0x80000000, %edi       # imm = 0x80000000
cmpq $0x0, 0x8(%rbx)
je 0x1cbd98
pushq $-0x1
popq %rax
movl %eax, %r13d
movl %eax, %r12d
jmp 0x1cbf17
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r12d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
movl 0x8(%rax,%rcx), %r13d
shlq $0x20, %rsi
orq %r13, %rsi
movq %rbx, %rdi
callq 0x1cc1e3
addl %r13d, %r14d
movq 0x10(%rsp), %rax
movl %eax, %r13d
movq 0x8(%rsp), %r8
pushq $0x1
popq %r9
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
notl %ecx
movl %r13d, %edx
shrl %edx
andl $0x1, %ecx
imulq $0x78, %rdx, %rdx
addq %rax, %rdx
imulq $0x18, %rcx, %rsi
cmpq $0x1, 0x10(%rsi,%rdx)
movl %r13d, %ecx
jne 0x1cbedc
addq %rsi, %rdx
addq $0x10, %rdx
movq 0x10(%rdx), %rdx
movl 0x8(%rdx), %edx
movq %rdx, %rbp
xorq $0x1, %rbp
movl %edx, %esi
shrl %esi
movl %ebp, %edi
andl $0x1, %edi
imulq $0x78, %rsi, %rsi
addq %rsi, %rax
imulq $0x18, %rdi, %rsi
cmpq $0x1, 0x10(%rsi,%rax)
jne 0x1cbedc
movl %r9d, (%r8,%rdx,4)
movl %r9d, (%r8,%rcx,4)
movq 0x20(%rsi,%rax), %rax
movq (%rax), %r13
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rax, %rcx
shrq $0x3a, %rcx
pushq $0x1
popq %rsi
shlq %cl, %rsi
cmpq %rsi, %rdx
jne 0x1cbe81
incl %ecx
movq %rbx, %rdi
movl %ecx, %esi
callq 0x1de4f8
pushq $0x1
popq %r9
movq 0x8(%rsp), %r8
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rax, %rcx
shrq $0x3a, %rcx
movl %r13d, %esi
movq %rbp, %rdi
shlq $0x20, %rdi
orq %rsi, %rdi
incq %rdx
movq %rdx, 0x8(%rbx)
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
leaq (%rax,%r10), %rdx
pushq $-0x1
popq %rsi
shlq %cl, %rsi
testq %r10, %rax
notq %rsi
cmovneq %rdx, %rsi
andq %r10, %rsi
movabsq $-0x400000000000000, %rcx # imm = 0xFC00000000000000
andq %rcx, %rax
orq %rsi, %rax
movq %rax, (%rbx)
movq 0x18(%rbx), %rax
movq %rdi, (%rax,%rsi,8)
addl %r13d, %r14d
movl %ebp, %r13d
jmp 0x1cbdd1
cmpl $-0x1, %r13d
je 0x1cbf0e
movl %r12d, %eax
movl %r9d, (%r8,%rax,4)
movl %r9d, (%r8,%rcx,4)
xorl %edi, %edi
jmp 0x1cbf17
movl %r9d, %ecx
notl %ecx
andl $0x1, %ecx
imulq $0x18, %rcx, %rcx
cmpq $0x0, 0x10(%rcx,%rax)
jne 0x1cc036
jmp 0x1cbcb1
movl $0x80000000, %edi       # imm = 0x80000000
pushq $-0x1
popq %r13
movq 0x40(%rsp), %rcx
cmpq %rcx, 0x18(%rsp)
movq 0x30(%rsp), %rax
jne 0x1cbf61
leaq (%rcx,%rcx), %rbp
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rbp
movq %rbp, 0x8(%rax)
imulq $0x28, %rbp, %rsi
movl %edi, %r15d
movq 0x28(%rsp), %rdi
callq 0x2370
movl %r15d, %edi
movq %rax, %r15
movq 0x30(%rsp), %rax
movq %r15, 0x10(%rax)
movq %rbp, 0x40(%rsp)
jmp 0x1cbf66
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq %rdx, (%rax)
imulq $0x28, %rcx, %rbp
andq $0x0, 0x20(%r15,%rbp)
movl %r13d, 0x4(%r15,%rbp)
movl %r12d, 0x8(%r15,%rbp)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl (%r15,%rbp), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r14d, %eax
movl %eax, (%r15,%rbp)
movq 0x8(%rbx), %r12
movl %r12d, 0x10(%r15,%rbp)
orl %edi, %r14d
movl %r14d, (%r15,%rbp)
leal -0x1(%r12), %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,%rbp)
leal 0x8(,%rcx,8), %edi
callq 0x2280
movq %rax, 0x18(%r15,%rbp)
xorl %ecx, %ecx
movq %r15, %r14
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
movq 0x10(%rsp), %r9
movl %ecx, %ecx
cmpq %rcx, %r12
jbe 0x1cc02c
movq (%rbx), %rdx
andq %rdi, %rdx
addq %rcx, %rdx
andq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
movq (%rsi,%rdx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incl %ecx
jmp 0x1cc008
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
incq %r9
jmp 0x1cbc61
movq 0x18(%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
pushq $0x1
popq %r8
movq 0x50(%rsp), %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shlq $0x3, %rcx
xorl %eax, %eax
movq 0x8(%rsp), %rdi
cmpq %rax, %rcx
je 0x1cc078
orl $-0x1, (%rdi,%rax)
addq $0x4, %rax
jmp 0x1cc069
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x18(%rsp)
jbe 0x1cc0ad
imulq $0x28, %rax, %rcx
cmpl $0x0, (%r14,%rcx)
js 0x1cc0a5
leaq (%r14,%rcx), %rdx
leal -0x1(%r8), %esi
movl 0x4(%rdx), %edx
movl %esi, (%rdi,%rdx,4)
movl 0x8(%r14,%rcx), %ecx
movl %r8d, (%rdi,%rcx,4)
incl %eax
addl $0x2, %r8d
jmp 0x1cc07a
xorl %ecx, %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl 0x48(%rsp), %ecx
je 0x1cc18e
movl %ecx, %eax
shrl %eax
movq %rcx, 0x10(%rsp)
andl $0x1, %ecx
imulq $0x78, %rax, %rax
movq 0x20(%rsp), %rdx
addq (%rdx), %rax
imulq $0x18, %rcx, %rcx
movq 0x20(%rcx,%rax), %r13
movl 0x10(%rcx,%rax), %ebp
shlq $0x5, %rbp
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x1cc182
movl 0xc(%r13,%r12), %eax
testl %eax, %eax
js 0x1cc179
movl 0x4(%r13,%r12), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%rdi,%rcx), %ebx
testl %ebx, %ebx
js 0x1cc179
movl 0x8(%r13,%r12), %ecx
cmpl $0x0, (%rdi,%rcx,4)
js 0x1cc179
movl %ebx, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movq %r14, %r15
movl (%r14,%rcx), %ecx
andl %esi, %ecx
subl %eax, %ecx
pushq $0x1
popq %r14
js 0x1cc137
movl %ecx, %r14d
xorl $0x1, %ebx
movq 0x38(%rsp), %rdi
callq 0x1ab555
movl 0xc(%r13,%r12), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
movl %ecx, 0xc(%rax)
shlq $0x20, %rbx
movslq %r14d, %rcx
orq %rbx, %rcx
movq %rcx, (%rax)
movl 0x8(%r13,%r12), %ecx
movq 0x8(%rsp), %rdi
movl (%rdi,%rcx,4), %ecx
movl %ecx, 0x8(%rax)
andw $0xc000, 0x10(%rax)     # imm = 0xC000
movq %r15, %r14
addq $0x20, %r12
jmp 0x1cc0ea
movq 0x10(%rsp), %rcx
incl %ecx
jmp 0x1cc0b4
xorl %eax, %eax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %eax, %ebx
cmpq %rbx, 0x18(%rsp)
jbe 0x1cc1bb
imulq $0x28, %rbx, %rax
movl (%r14,%rax), %edx
andl %ebp, %edx
movq 0x38(%rsp), %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x69316
leal 0x1(%rbx), %eax
jmp 0x1cc195
movq 0x38(%rsp), %rdi
callq 0x694eb
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq