// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
xorl %r12d, %r12d
cmpl $0x2, %edx
jb 0xbe866
movl %edx, %ebp
movq %rsi, %rbx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rdi), %esi
movq %rdi, 0x18(%rsp)
callq 0x693c2
testq %rax, %rax
je 0xbe866
decl %ebp
movl %ebp, 0xc(%rax)
movl %ebp, 0xc(%rsp)
movl %ebp, 0x10(%rax)
movq 0x18(%rax), %rdi
movq %rax, 0x10(%rsp)
callq 0x2220
movq 0x10(%rsp), %rax
movl 0x10(%rax), %r14d
leaq (,%r14,8), %rdi
callq 0x2280
movq 0x10(%rsp), %r10
movq %rax, 0x18(%r10)
leaq 0x8(%rbx), %rcx
xorl %edx, %edx
movq 0x18(%rsp), %rdi
cmpq %rdx, %r14
je 0xbe720
movl (%rcx), %esi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x18, %rcx
jmp 0xbe708
movq %rbx, 0x20(%rsp)
xorl %ebp, %ebp
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
xorl %r12d, %r12d
leal -0x1(%r14), %ecx
cmpq %rcx, %rbp
jae 0xbe7e6
movq (%rax,%rbp,8), %r13
leaq 0x1(%rbp), %r15
movl 0xc(%rax,%rbp,8), %ecx
movq 0x8(%rdi), %r9
movq 0x28(%rdi), %rdx
movq %r13, %rsi
shrq $0x20, %rsi
movq (%rdx,%rsi,8), %rdx
movq %rdx, %r8
shrq $0x20, %r8
movl %edx, %esi
imulq $0x18, %r8, %r8
addq %r8, %r9
addq $0xc, %r9
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0xbe7ae
cmpl $0x0, (%r9)
js 0xbe788
cmpl %ecx, -0x4(%r9)
je 0xbe791
incq %r8
addq $0x18, %r9
jmp 0xbe777
movl -0xc(%r9), %ebx
cmpl %r8d, %edx
je 0xbe7b0
andq %r11, %r13
movl %ebx, %ecx
orq %r13, %rcx
movq %rcx, (%rax,%rbp,8)
addl %ebx, %r12d
movq %r15, %rbp
jmp 0xbe734
xorl %ebx, %ebx
movq 0x194829(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x12c5bf(%rip), %rdi    # 0x1ead80
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
movq %r11, %r14
callq 0x2410
movq %r14, %r11
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
movq 0x18(%r10), %rax
movl 0x10(%r10), %r14d
jmp 0xbe79a
cmpl %ebp, %r14d
jbe 0xbe80f
movl %ebp, %ecx
movq (%rax,%rcx,8), %rdx
andq %rdx, %r11
movq 0x20(%rdi), %rsi
shrq $0x21, %rdx
movl (%rsi,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orq %rdx, %r11
movq %r11, (%rax,%rcx,8)
addl %edx, %r12d
movl 0xc(%rsp), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r12d
movl %r12d, (%r10)
movq 0x20(%rsp), %r8
movl 0xc(%r8), %eax
andl %esi, %eax
movl %eax, 0x4(%r10)
movl %ecx, %eax
imulq $0x18, %rax, %rax
movl 0xc(%r8,%rax), %ecx
andl %esi, %ecx
movl %r12d, %edx
subl %ecx, %edx
movl %edx, 0x8(%r10)
movl 0xc(%r8), %ecx
andl %esi, %ecx
movl 0xc(%r8,%rax), %eax
andl %esi, %eax
addl %ecx, %eax
subl %eax, %r12d
andl 0x14(%rdi), %esi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x69316
pushq $0x1
popq %r12
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq