// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %r8, %rbp
movl %ecx, %ebx
movl %edx, %r12d
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 0xf1e2a
movl %eax, 0xc(%r15)
movq 0x18(%r15), %rdi
movl %eax, %esi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x18(%r15)
testb $0x1, %r12b
jne 0xf1e38
movl 0x8(%r15), %r9d
xorl $0x1, %r9d
movl 0x70(%rsp), %r8d
movl 0x68(%rsp), %ecx
testb $0x1, %r12b
jne 0xf1edc
leaq 0x10(%rsp), %r12
movq %rbp, %rdi
movq %r13, %rsi
pushq %r12
pushq %rbx
callq 0xb2ced
addq $0x10, %rsp
movq 0x18(%r15), %rcx
movl (%r15), %edi
movl 0x10(%r15), %eax
leal -0x1(%rax), %r8d
movl %edi, %r9d
subl (%rcx,%r8,8), %r9d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r9d
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %edi
orl %r9d, %edi
movl %edi, (%r15)
movq 0x8(%rsp), %r11
movq 0x20(%r11), %rdi
movl %ebx, %r9d
shrl %r9d
movl (%rdi,%r9,8), %edi
movl %edi, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq %rbx, %r10
shlq $0x20, %r10
orq %r9, %r10
movq %r10, (%rcx,%rax,8)
movl (%r12), %r9d
movl %r9d, (%rcx,%r8,8)
movl (%r15), %ecx
addl %r9d, %edi
addl %ecx, %edi
andl %edx, %edi
andl %esi, %ecx
orl %edi, %ecx
movl %ecx, (%r15)
movl %ebx, %ecx
xorl $0x1, %ecx
movl %ecx, 0x8(%r15)
jmp 0xf1f57
xorl $0x1, %ebx
movl 0x4(%r15), %eax
leaq 0x10(%rsp), %rdx
movq %rbp, %rdi
movq %r13, %rsi
movl %ebx, %r9d
pushq %rdx
pushq %rax
callq 0xb2ced
addq $0x10, %rsp
movl 0x10(%r15), %eax
testq %rax, %rax
je 0xf1f20
movq 0x18(%r15), %rcx
movq %rax, %rsi
movq %rax, %rdx
movq -0x8(%rcx,%rsi,8), %rdi
movq %rdi, (%rcx,%rsi,8)
decq %rdx
movq %rdx, %rsi
jne 0xf1f0f
movq 0x10(%rsp), %rcx
movl %ecx, %edx
movq 0x18(%r15), %rsi
movq %rbx, %rdi
shlq $0x20, %rdi
orq %rdx, %rdi
movq %rdi, (%rsi)
movl (%r15), %edx
addl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %ecx, %edx
movl %edx, (%r15)
movl %ebx, 0x4(%r15)
movq 0x8(%rsp), %r11
movq 0x60(%rsp), %rdi
incl %eax
movl %eax, 0x10(%r15)
shrl %ebx
movq 0x20(%r11), %rax
leaq (%rax,%rbx,8), %r8
addq $0x4, %r8
movl %ebx, %esi
movl %r14d, %edx
movl $0x1, %ecx
callq 0xa1cd4
movl 0x4(%r15), %eax
cmpl $-0x1, %eax
je 0xf1f91
movq 0x18(%r15), %rcx
cmpl 0x4(%rcx), %eax
jne 0xf1fef
movl 0x8(%r15), %eax
cmpl $-0x1, %eax
je 0xf1fad
movq 0x18(%r15), %rcx
movl 0x10(%r15), %edx
decl %edx
xorl 0x4(%rcx,%rdx,8), %eax
cmpl $0x1, %eax
jne 0xf1ff6
movl 0x10(%r15), %eax
testq %rax, %rax
je 0xf1fcb
movq 0x18(%r15), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
addl (%rdx,%rsi,8), %ecx
incq %rsi
cmpq %rsi, %rax
jne 0xf1fbe
jmp 0xf1fcd
xorl %ecx, %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%r15), %eax
cmpl %eax, %ecx
jne 0xf1fe8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11d8e
jmp 0xf1fd9
callq 0x11d56
jmp 0xf1f91
callq 0x11d72
jmp 0xf1fad