// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: append_rId_to_Unitig(asg_t*, ma_ug_t*, unsigned int, unsigned int, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int)
void append_rId_to_Unitig(asg_t *r_g, ma_ug_t* ug, uint32_t uId, uint32_t rId,
ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, R_to_U* ruIndex, int max_hang, int min_ovlp)
{
    asg_arc_t t;
    t.ul = (uint64_t)-1;
    ma_utg_t *nsu = &(ug->u.a[uId>>1]);
    uint32_t l, i, beg, end;

    if(nsu->m < (nsu->n+1))
    {
        nsu->m = nsu->n+1;
        nsu->a = (uint64_t*)realloc(nsu->a, nsu->m*sizeof(uint64_t));
    }

    if((uId&1) == 0)
    {
        beg = nsu->end^1;
        end = rId;
    }
    
    if((uId&1) == 1)
    {
        beg = rId^1;
        end = nsu->start;
    }

    ///t is the edge that from beg to end
    get_edge_from_source(sources, coverage_cut, ruIndex, max_hang, min_ovlp, beg, 
    end, &t);

    ///add rId to the end of nsu
    if((uId&1) == 0)
    {
        nsu->len -= (uint32_t)nsu->a[nsu->n-1];

        l = r_g->seq[rId>>1].len;
        nsu->a[nsu->n] = rId;
        nsu->a[nsu->n] <<=32;
        nsu->a[nsu->n] = nsu->a[nsu->n] | (uint64_t)(l);

        l = asg_arc_len(t);
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1]>>32;
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1]<<32;
        nsu->a[nsu->n-1] = nsu->a[nsu->n-1] | (uint64_t)(l);

        nsu->len = nsu->len + (uint32_t)nsu->a[nsu->n-1] + (uint32_t)nsu->a[nsu->n];
        nsu->end = rId^1;
    
        nsu->n++;
    }

    ///add rId to the start of nsu
    if((uId&1) == 1)
    {
        rId = rId^1;
        for(i = nsu->n; i >= 1; i--)
        {
            nsu->a[i] = nsu->a[i-1];
        }
        
        l = asg_arc_len(t);
        nsu->a[0] = rId;
        nsu->a[0] = nsu->a[0]<<32;
        nsu->a[0] = nsu->a[0] | (uint64_t)(l);
        nsu->len = nsu->len + (uint32_t)nsu->a[0];
        nsu->start = rId;

        nsu->n++;
    }


    set_R_to_U(ruIndex, rId>>1, uId>>1, 1, &(r_g->seq[rId>>1].c));




    /*************************just for debug**************************/
    uint32_t v, totalLen = 0;
    v = (uint64_t)(nsu->a[0])>>32;
    if(nsu->start != UINT32_MAX && nsu->start != v) fprintf(stderr, "hehe\n");

    v = (uint64_t)(nsu->a[nsu->n-1])>>32;
    if(nsu->end != UINT32_MAX && nsu->end != (v^1)) fprintf(stderr, "haha\n");

    for (i = 0; i < nsu->n; i++)
    {
        l = (uint32_t)(nsu->a[i]);
        totalLen = totalLen + l;
    }
    if(totalLen != nsu->len) fprintf(stderr, "xxxx\n");

    ////fprintf(stderr, "utg%.6dl, dir: %u\n", uId+1, uId&1);
    /*************************just for debug**************************/

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %rbp
movq %r8, %r12
movl %ecx, %ebx
movl %edx, %r13d
movq %rdi, 0x8(%rsp)
movq $-0x1, 0x10(%rsp)
movq 0x10(%rsi), %rax
movl %edx, %r14d
shrl %r14d
leaq (%r14,%r14,4), %rcx
leaq (%rax,%rcx,8), %r15
movl 0x10(%r15), %eax
incl %eax
cmpl %eax, 0xc(%r15)
jae 0xf4215
movl %eax, 0xc(%r15)
movq 0x18(%r15), %rdi
movl %eax, %esi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x18(%r15)
movl 0x70(%rsp), %r8d
movl 0x68(%rsp), %ecx
testb $0x1, %r13b
jne 0xf42c4
movl 0x8(%r15), %r9d
xorl $0x1, %r9d
leaq 0x10(%rsp), %r13
movq %r12, %rdi
movq %rbp, %rsi
pushq %r13
pushq %rbx
callq 0xb3df3
addq $0x10, %rsp
movq 0x18(%r15), %rcx
movl (%r15), %esi
movl 0x10(%r15), %eax
leal -0x1(%rax), %r8d
movl %esi, %r9d
subl (%rcx,%r8,8), %r9d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r9d
movl $0x80000000, %edi       # imm = 0x80000000
andl %edi, %esi
orl %r9d, %esi
movl %esi, (%r15)
movq 0x8(%rsp), %r12
movq 0x20(%r12), %r9
movl %ebx, %esi
shrl %esi
movl (%r9,%rsi,8), %r9d
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %r11
shlq $0x20, %r11
orq %r10, %r11
movq %r11, (%rcx,%rax,8)
movl (%r13), %r10d
movl %r10d, (%rcx,%r8,8)
movl (%r15), %ecx
addl %r10d, %r9d
addl %ecx, %r9d
andl %edx, %r9d
andl %edi, %ecx
orl %r9d, %ecx
movl %ecx, (%r15)
xorl $0x1, %ebx
movl %ebx, 0x8(%r15)
movq %rsi, %rbx
jmp 0xf4343
movl %ebx, %r13d
xorl $0x1, %r13d
movl 0x4(%r15), %eax
leaq 0x10(%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rsi
movl %r13d, %r9d
pushq %rdx
pushq %rax
callq 0xb3df3
addq $0x10, %rsp
movl 0x10(%r15), %eax
movq 0x18(%r15), %rcx
testq %rax, %rax
je 0xf430c
movq %rax, %rsi
movq %rax, %rdx
movq -0x8(%rcx,%rsi,8), %rdi
movq %rdi, (%rcx,%rsi,8)
decq %rdx
movq %rdx, %rsi
jne 0xf42fb
movq 0x10(%rsp), %rdx
movl %edx, %esi
movq %r13, %rdi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, (%rcx)
movl (%r15), %ecx
addl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%r15)
movl %r13d, 0x4(%r15)
shrl %ebx
movl %ebx, %esi
movq 0x8(%rsp), %r12
movq 0x60(%rsp), %rdi
incl %eax
movl %eax, 0x10(%r15)
movq 0x20(%r12), %rax
leaq (%rax,%rbx,8), %r8
addq $0x4, %r8
movl %r14d, %edx
movl $0x1, %ecx
callq 0xa27e9
movl 0x4(%r15), %eax
cmpl $-0x1, %eax
je 0xf437a
movq 0x18(%r15), %rcx
cmpl 0x4(%rcx), %eax
jne 0xf43d6
movl 0x8(%r15), %ecx
movl 0x10(%r15), %eax
cmpl $-0x1, %ecx
je 0xf4397
movq 0x18(%r15), %rdx
leal -0x1(%rax), %esi
xorl 0x4(%rdx,%rsi,8), %ecx
cmpl $0x1, %ecx
jne 0xf43dd
testl %eax, %eax
je 0xf43b2
movq 0x18(%r15), %rcx
movl %eax, %edx
xorl %esi, %esi
xorl %eax, %eax
addl (%rcx,%rsi,8), %eax
incq %rsi
cmpq %rsi, %rdx
jne 0xf43a5
jmp 0xf43b4
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%r15), %ecx
cmpl %ecx, %eax
jne 0xf43cf
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x119d0
jmp 0xf43c0
callq 0x119b4
jmp 0xf437a
movq 0x1a0bfc(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0x144270(%rip), %rdi    # 0x23865e
movl $0x5, %esi
movl $0x1, %edx
callq 0x2410
movl 0x10(%r15), %eax
jmp 0xf4397