// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_print2(ma_ug_t const*, All_reads*, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, int, char const*, _IO_FILE*)
void ma_ug_print2(const ma_ug_t *ug, All_reads *RNF, asg_t* read_g, const ma_sub_t *coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, int print_seq, const char* prefix, FILE *fp)
{
    uint8_t* primary_flag = read_g?(uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t)):NULL;
	uint32_t i, j, l, pc = read_g && coverage_cut && sources && ruIndex?1:0, co;
	char name[32];
	for (i = 0; i < ug->u.n; ++i) { // the Segment lines in GFA
		ma_utg_t *p = &ug->u.a[i];
        if(p->m == 0) continue;
        sprintf(name, "%s%.6d%c", prefix, i + 1, "lc"[p->circ]);
		if (print_seq) fprintf(fp, "S\t%s\t%s\tLN:i:%d\trd:i:%u\n", name, p->s? p->s : "*", p->len, 
        pc?get_ug_coverage(p, read_g, coverage_cut, sources, ruIndex, primary_flag, NULL, NULL):0);
		else fprintf(fp, "S\t%s\t*\tLN:i:%d\trd:i:%u\n", name, p->len, 
        pc?get_ug_coverage(p, read_g, coverage_cut, sources, ruIndex, primary_flag, NULL, NULL):0);
        
		for (j = l = 0; j < p->n; j++) {
            if(p->a[j] != (uint64_t)-1)
            {
                uint32_t x = p->a[j]>>33;
                if(x<RNF->total_reads)
                {

                    fprintf(fp, "A\t%s\t%d\t%c\t%.*s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
                    (int)Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x), 
                    coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, 
                    "apmaaa"[((RNF->trio_flag[x]!=FATHER && RNF->trio_flag[x]!=MOTHER)?AMBIGU:RNF->trio_flag[x])]);
                }
                else
                {
                    fprintf(fp, "A\t%s\t%d\t%c\t%s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
                        "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                }
            }
            else
            {
                fprintf(fp, "A\t%s\t%d\t*\t*\t*\t*\tid:i:*\tHG:A:*\n", name, l);
            }
            l += (uint32_t)p->a[j];
        }
	}
	// for (i = 0; i < ug->g->n_arc; ++i) { // the Link lines in GFA
	// 	uint32_t u = ug->g->arc[i].ul>>32, v = ug->g->arc[i].v;
	// 	fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\n", 
    //     prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
	// 	prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], ug->g->arc[i].ol, asg_arc_len(ug->g->arc[i]));
	// }
    if(ug->g)
    {
        asg_arc_t* au = NULL;
        uint32_t nu, u, v;
        for (i = 0; i < ug->u.n; ++i) {
            if(ug->u.a[i].m == 0) continue;
            if(ug->u.a[i].circ) {
                co = cal_circle_ov(ug, i, 0, read_g);
                fprintf(fp, "L\t%s%.6dc\t+\t%s%.6dc\t+\t%dM\tL1:i:%d\tL2:i:%u\n", 
                prefix, i+1, prefix, i+1, co, ((ug->u.a[i].len>=co)?(ug->u.a[i].len-co):0), 0);
                co = cal_circle_ov(ug, i, 1, read_g);
                fprintf(fp, "L\t%s%.6dc\t-\t%s%.6dc\t-\t%dM\tL1:i:%d\tL2:i:%u\n", 
                prefix, i+1, prefix, i+1, co, ((ug->u.a[i].len>=co)?(ug->u.a[i].len-co):0), 0);
            } else {
                u = i<<1;
                au = asg_arc_a(ug->g, u);
                nu = asg_arc_n(ug->g, u);
                for (j = 0; j < nu; j++)
                {
                    if(au[j].del) continue;
                    v = au[j].v;
                    fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\tL2:i:%u\n", 
                    prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
                    prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], au[j].ol, asg_arc_len(au[j]), 0/**au[j].ou**/);
                }


                u = (i<<1) + 1;
                au = asg_arc_a(ug->g, u);
                nu = asg_arc_n(ug->g, u);
                for (j = 0; j < nu; j++)
                {
                    if(au[j].del) continue;
                    v = au[j].v;
                    fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\tL2:i:%u\n", 
                    prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
                    prefix,	(v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], au[j].ol, asg_arc_len(au[j]), 0/**au[j].ou**/);
                }
            } 
        }
    }
    free(primary_flag);    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r12
testq %rdx, %rdx
je 0xb68d1
movl 0x14(%rbx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x28(%rsp)
jmp 0xb68da
movq $0x0, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
testq %rbx, %rbx
setne %sil
testq %r15, %r15
setne %al
cmpq $0x0, 0x40(%rsp)
setne %cl
cmpq $0x0, 0x48(%rsp)
setne %dl
cmpq $0x0, (%r12)
movq %r12, 0x10(%rsp)
je 0xb6c0c
andb %dl, %cl
andb %al, %sil
andb %cl, %sil
movb %sil, 0x38(%rsp)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq 0x10(%r12), %rcx
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xb6bee
leaq (%rcx,%rax,8), %r13
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ecx
movl (%r13), %eax
shrl $0x1f, %eax
leaq 0x171cd2(%rip), %rdx    # 0x22862b
movsbl (%rax,%rdx), %r8d
leaq 0x50(%rsp), %rdi
leaq 0x173880(%rip), %rsi    # 0x22a1ea
movq 0xb8(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xb0(%rsp)
je 0xb69ff
movq 0x20(%r13), %r12
testq %r12, %r12
leaq 0x17387b(%rip), %rax    # 0x22a20c
cmoveq %rax, %r12
movl (%r13), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
xorl %r9d, %r9d
cmpb $0x0, 0x38(%rsp)
je 0xb69dc
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x38(%rsp), %r9
callq 0xb62e5
addq $0x10, %rsp
movl %eax, %r9d
movq 0xc0(%rsp), %rdi
leaq 0x173808(%rip), %rsi    # 0x22a1f3
leaq 0x50(%rsp), %rdx
movq %r12, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x22b0
jmp 0xb6a64
movl (%r13), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
xorl %r8d, %r8d
cmpb $0x0, 0x38(%rsp)
je 0xb6a46
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x38(%rsp), %r9
callq 0xb62e5
addq $0x10, %rsp
movl %eax, %r8d
movq 0xc0(%rsp), %rdi
leaq 0x1737b9(%rip), %rsi    # 0x22a20e
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22b0
cmpl $0x0, 0x10(%r13)
je 0xb6bee
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x18(%r13), %rax
movq (%rax,%r14,8), %rax
cmpq $-0x1, %rax
je 0xb6abb
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x16f72d(%rip), %rax    # 0x2261c3
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%rbp), %r10
jae 0xb6ade
testq %r15, %r15
je 0xb6af5
leaq (%r10,%r10,2), %rax
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
jmp 0xb6af8
movq 0xc0(%rsp), %rdi
leaq 0x1737b4(%rip), %rsi    # 0x22a27e
leaq 0x50(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0xb6bd6
leaq (%r10,%r10,2), %rax
testq %r15, %r15
je 0xb6b08
movl (%r15,%rax,4), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
jmp 0xb6b0b
xorl %r11d, %r11d
testq %r15, %r15
je 0xb6b1e
leaq (%r10,%r10,2), %rax
movl 0x4(%r15,%rax,4), %eax
jmp 0xb6b26
xorl %r11d, %r11d
testq %r15, %r15
je 0xb6b9e
movl 0x4(%r15,%rax,4), %ebx
jmp 0xb6ba6
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %eax
movq %rax, 0x18(%rsp)
movq 0x28(%rbp), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0xb6b3e
xorl %eax, %eax
movq 0x40(%rbp), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r15
subl %r15d, %r9d
addq 0x8(%rbp), %r15
movzbl %al, %eax
movq %rbp, %rbx
leaq 0x1736ee(%rip), %rcx    # 0x22a24d
movsbl (%rax,%rcx), %ebp
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x1736b0(%rip), %rsi    # 0x22a226
leaq 0x58(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
pushq %rbp
movq %rbx, %rbp
pushq %r10
pushq 0x30(%rsp)
pushq %r11
pushq %r15
callq 0x22b0
addq $0x30, %rsp
movq 0x20(%rsp), %r15
jmp 0xb6bd6
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %ebx
movq 0xc0(%rsp), %rdi
leaq 0x17369f(%rip), %rsi    # 0x22a254
leaq 0x50(%rsp), %rdx
movl %r12d, %ecx
leaq 0x1736b5(%rip), %r9     # 0x22a279
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %rbx
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%r13), %rax
addl (%rax,%r14,8), %r12d
incq %r14
movl 0x10(%r13), %eax
cmpq %rax, %r14
jb 0xb6a75
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, %rax
movq 0x10(%rsp), %r12
movq %rcx, 0x8(%rsp)
cmpq %rcx, (%r12)
ja 0xb692b
cmpq $0x0, 0x18(%r12)
je 0xb6f54
cmpq $0x0, (%r12)
je 0xb6f54
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movq 0x10(%r12), %rcx
leaq (%rax,%rax,4), %rbx
cmpl $0x0, 0xc(%rcx,%rbx,8)
je 0xb6f3b
leaq (%rcx,%rbx,8), %rax
movl (%rax), %r14d
testl %r14d, %r14d
js 0xb6e61
movq 0x8(%rsp), %rax
leal (%rax,%rax), %edx
movq 0x18(%r12), %rcx
movq 0x28(%rcx), %rax
movq %rdx, 0x38(%rsp)
movq (%rax,%rdx,8), %rax
testl %eax, %eax
je 0xb6d5c
movq 0x8(%rcx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movq 0x8(%rsp), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rsi, 0x18(%rsp)
incl %esi
movl %esi, 0x20(%rsp)
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rbp
addq $0xc, %rbp
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
xorl %r13d, %r13d
movl (%rbp,%r13), %r10d
testl %r10d, %r10d
js 0xb6d4f
movl -0xc(%rbp,%r13), %r11d
movl -0x4(%rbp,%r13), %eax
movq 0x10(%r12), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %edx
shrl $0x1f, %edx
leaq 0x17194a(%rip), %rsi    # 0x22862b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r14d
shrl %r14d
leaq (%r14,%r14,4), %rdx
incl %r14d
movl (%rcx,%rdx,8), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %r12d
andl $0x1, %eax
leaq 0x16f4bb(%rip), %rcx    # 0x2261c3
movsbl (%rax,%rcx), %r15d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x1735d3(%rip), %rsi    # 0x22a2f3
movq 0xc0(%rsp), %rdx
movl 0x28(%rsp), %ecx
movl $0x2b, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %r15
pushq %r12
movq 0x40(%rsp), %r12
pushq %r14
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %r13
cmpq %r13, %rbx
jne 0xb6cae
movq 0x18(%r12), %rcx
movq 0x28(%rcx), %rax
movl 0x38(%rsp), %edx
movq 0x8(%rax,%rdx,8), %rax
testl %eax, %eax
je 0xb6f3b
movq 0x8(%rcx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movq 0x8(%rsp), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leal 0x1(%rsi), %edi
movl %edi, 0x18(%rsp)
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r15
addq $0xc, %r15
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
leaq (%rsi,%rsi,4), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movl (%r15,%r13), %r10d
testl %r10d, %r10d
js 0xb6e4f
movl -0xc(%r15,%r13), %r11d
movl -0x4(%r15,%r13), %eax
movq 0x10(%r12), %rcx
movq 0x20(%rsp), %rdx
movl (%rcx,%rdx,8), %edx
shrl $0x1f, %edx
leaq 0x171848(%rip), %rsi    # 0x22862b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r14d
shrl %r14d
leaq (%r14,%r14,4), %rdx
incl %r14d
movl (%rcx,%rdx,8), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %r12d
andl $0x1, %eax
leaq 0x16f3b9(%rip), %rcx    # 0x2261c3
movsbl (%rax,%rcx), %ebp
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x1734d2(%rip), %rsi    # 0x22a2f3
movq 0xc0(%rsp), %rdx
movl 0x20(%rsp), %ecx
movl $0x2d, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %rbp
pushq %r12
movq 0x40(%rsp), %r12
pushq %r14
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %r13
cmpq %r13, %rbx
jne 0xb6db5
jmp 0xb6f3b
movq %r12, %rdi
movq 0x8(%rsp), %r15
movl %r15d, %esi
xorl %edx, %edx
movq 0x30(%rsp), %rcx
callq 0xb67cd
movl %eax, %r10d
leal 0x1(%r15), %r12d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
subl %r10d, %r14d
movl $0x0, %eax
cmovbl %eax, %r14d
xorl %r11d, %r11d
subq $0x8, %rsp
movq 0xc8(%rsp), %r13
movq %r13, %rdi
leaq 0x1733f1(%rip), %rsi    # 0x22a29d
movq 0xc0(%rsp), %rbp
movq %rbp, %rdx
movl %r12d, %ecx
movq %rbp, %r8
movl %r12d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r14
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq 0x10(%rsp), %rdi
movl %r15d, %esi
movl $0x1, %edx
movq 0x30(%rsp), %rcx
callq 0xb67cd
movl %eax, %r10d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%rbx,8), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
subl %r10d, %r11d
movl $0x0, %ebx
cmovbl %ebx, %r11d
subq $0x8, %rsp
movq %r13, %rdi
leaq 0x1733ae(%rip), %rsi    # 0x22a2c8
movq %rbp, %rdx
movl %r12d, %ecx
movq %rbp, %r8
movl %r12d, %r9d
movq 0x18(%rsp), %r12
xorl %eax, %eax
pushq %rbx
pushq %r11
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq 0x8(%rsp), %rdx
incl %edx
movq %rdx, %rax
movq %rdx, 0x8(%rsp)
cmpq %rdx, (%r12)
ja 0xb6c2e
movq 0x28(%rsp), %rdi
callq 0x2220
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq