// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: create_fake_read(asg_t*, asg_arc_t*, unsigned int)
uint32_t create_fake_read(asg_t *r_g, asg_arc_t* list, uint32_t list_n)
{
    if(list_n < 2) return 0;
    ma_utg_t* u = NULL;
    uint32_t v, w, l, i, k, nv, totalLen = 0;
    asg_arc_t *av = NULL;
    u = asg_F_seq_set(r_g, r_g->n_seq);
    if(u == NULL) return 0;
    u->n = u->m = list_n - 1;
    if(u->a) free(u->a);
    u->a = (uint64_t*)malloc(sizeof(uint64_t)*u->n);

    /*****************for debug**********************/
    // fprintf(stderr, "list_n: %u\n", list_n);
    // for (i = 0; i < list_n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u, w: %u, w&1: %u, len: %u, ol: %u\n", 
    //             i, (uint32_t)(list[i].ul>>33), (uint32_t)((list[i].ul>>32)&1), 
    //             list[i].v>>1, list[i].v&1, (uint32_t)list[i].ul, list[i].ol);
    // }
    /*****************for debug**********************/


    for (i = 0; i < u->n; i++)
    {
        u->a[i] = list[i].v;
        u->a[i] = u->a[i] << 32;
    }

    /*****************for debug**********************/
    // fprintf(stderr, "u->n: %u\n", u->n);
    // for (i = 0; i < u->n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u\n", 
    //                 i, (uint32_t)(u->a[i]>>33), (uint32_t)((u->a[i]>>32)&1));
    // }
    /*****************for debug**********************/
    
    
    for (i = 0; i < u->n - 1; i++)
    {
        v = (uint64_t)(u->a[i])>>32;
        w = (uint64_t)(u->a[i + 1])>>32; 
        av = asg_arc_a(r_g, v);
        nv = asg_arc_n(r_g, v);
        l = 0;
        
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(av[k].v == w) 
            {
                l = asg_arc_len(av[k]);
                break;
            }
        }

        if(k == nv) fprintf(stderr, "####ERROR\n");
        u->a[i] = v; u->a[i] = u->a[i]<<32; u->a[i] = u->a[i] | (uint64_t)(l);
        totalLen += l;
    }

    if(i < u->n)
    {
        v = (uint64_t)(u->a[i])>>32;
        l = r_g->seq[v>>1].len;
        u->a[i] = v;
        u->a[i] = u->a[i]<<32;
        u->a[i] = u->a[i] | (uint64_t)(l);
        totalLen += l;
    }

    /*****************for debug**********************/
    // fprintf(stderr, "u->n: %u\n", u->n);
    // for (i = 0; i < u->n; i++)
    // {
    //     fprintf(stderr, "(%u) v: %u, dir: %u, len: %u\n", 
    //                 i, (uint32_t)(u->a[i]>>33), (uint32_t)((u->a[i]>>32)&1), (uint32_t)(u->a[i]));
    // }
    /*****************for debug**********************/

    

    u->len = totalLen;
    u->circ = 0;
    u->start = list[0].ol;
    u->end = u->len - list[list_n - 1].ol;
    totalLen = u->len - list[0].ol - list[list_n - 1].ol;
    ///u->len = totalLen;

    /*****************for debug**********************/
    // fprintf(stderr, "u->len: %u, u->start: %u, u->end: %u\n", u->len, u->start, u->end);
    // fprintf(stderr, "totalLen: %u, list[0].ol: %u, list[list_n - 1].ol: %u\n", 
    // totalLen, list[0].ol, list[list_n - 1].ol);
    /*****************for debug**********************/

    /*****************for debug**********************/
    /**
    (*coverage_cut) = (ma_sub_t*)realloc((*coverage_cut), (r_g->n_seq+1)*sizeof(ma_sub_t));
    (*coverage_cut)[r_g->n_seq].del = 0;
    (*coverage_cut)[r_g->n_seq].c = PRIMARY_LABLE;
    (*coverage_cut)[r_g->n_seq].s = 0;
    (*coverage_cut)[r_g->n_seq].e = totalLen;
    **/
    asg_seq_set(r_g, r_g->n_seq, totalLen, 0);
    /*****************for debug**********************/
    return 1;     
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %r12d, %r12d
cmpl $0x2, %edx
jb 0x1012c0
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %rbx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rdi), %esi
callq 0xa0933
testq %rax, %rax
je 0x1012c0
decl %ebp
movl %ebp, 0xc(%rax)
movl %ebp, 0x10(%rax)
movq 0x18(%rax), %rdi
movl %ebp, %r14d
testq %rdi, %rdi
movq %rax, %r12
je 0x10110e
callq 0x2220
movl 0x10(%r12), %r14d
movl %r14d, %r15d
leaq (,%r15,8), %rdi
callq 0x2280
movq %rax, 0x18(%r12)
testl %r14d, %r14d
movq %r12, %r9
movl %ebp, 0xc(%rsp)
je 0x101165
leaq 0x8(%r13), %rcx
xorl %edx, %edx
movl (%rcx), %esi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %r15
jne 0x101135
cmpl $0x1, %r14d
jne 0x101165
xorl %r12d, %r12d
xorl %ebp, %ebp
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
jmp 0x101247
movq %r13, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq (%rax,%rbp,8), %r15
movq 0x28(%rbx), %rcx
movq %r15, %rdx
shrq $0x20, %rdx
movq (%rcx,%rdx,8), %rcx
movl $0x0, %edx
movl $0x0, %r13d
testl %ecx, %ecx
je 0x1011dd
movl 0xc(%rax,%rbp,8), %esi
movq 0x8(%rbx), %rdx
movq %rcx, %r8
shrq $0x20, %r8
movl %ecx, %edi
leaq (%r8,%r8,2), %r8
leaq (%rdx,%r8,8), %r8
addq $0xc, %r8
xorl %edx, %edx
cmpl $0x0, (%r8)
js 0x1011c6
cmpl %esi, -0x4(%r8)
je 0x1011d9
incq %rdx
addq $0x18, %r8
cmpq %rdx, %rdi
jne 0x1011ba
movl %ecx, %edx
xorl %r13d, %r13d
jmp 0x1011dd
movl -0xc(%r8), %r13d
cmpl %ecx, %edx
je 0x101203
andq %r10, %r15
movl %r13d, %ecx
orq %r15, %rcx
movq %rcx, (%rax,%rbp,8)
incq %rbp
addl %r13d, %r12d
leal -0x1(%r14), %ecx
cmpq %rcx, %rbp
jb 0x101179
jmp 0x10123d
movq 0x193dd6(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0xa, %esi
movl $0x1, %edx
leaq 0x137c62(%rip), %rdi    # 0x238e80
movq %r9, %r14
callq 0x2410
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq %r14, %r9
movq 0x18(%r14), %rax
movl 0x10(%r14), %r14d
jmp 0x1011e1
cmpl %ebp, %r14d
movq 0x10(%rsp), %r13
jbe 0x10126b
movl %ebp, %ecx
movq (%rax,%rcx,8), %rdx
andq %rdx, %r10
movq 0x20(%rbx), %rsi
shrq $0x21, %rdx
movl (%rsi,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orq %rdx, %r10
movq %r10, (%rax,%rcx,8)
addl %edx, %r12d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r12d
movl %r12d, (%r9)
movl 0xc(%r13), %eax
andl %esi, %eax
movl %eax, 0x4(%r9)
movl 0xc(%rsp), %eax
leaq (%rax,%rax,2), %rax
movl 0xc(%r13,%rax,8), %ecx
andl %esi, %ecx
movl %r12d, %edx
subl %ecx, %edx
movl %edx, 0x8(%r9)
movl 0xc(%r13), %ecx
andl %esi, %ecx
movl 0xc(%r13,%rax,8), %eax
andl %esi, %eax
addl %ecx, %eax
subl %eax, %r12d
andl 0x14(%rbx), %esi
movq %rbx, %rdi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0xa0887
movl $0x1, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq