// File: /workspace/github_cpp_makefile_star2_O2/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 %rsi, %rbp
movq %rdi, %r12
testq %rdx, %rdx
movq %rdx, 0x10(%rsp)
je 0x7c701
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq 0x10(%rsp), %rdx
movq %rax, 0x30(%rsp)
jmp 0x7c70a
movq $0x0, 0x30(%rsp)
testq %rdx, %rdx
setne %al
testq %r15, %r15
setne %sil
cmpq $0x0, 0x40(%rsp)
setne %cl
cmpq $0x0, 0x48(%rsp)
setne %dl
andb %cl, %dl
andb %al, %sil
andb %dl, %sil
movb %sil, 0x38(%rsp)
xorl %r9d, %r9d
movq %r12, 0x20(%rsp)
movq %r15, 0x8(%rsp)
movl %r9d, %ecx
movq (%r12), %rax
cmpq %rcx, %rax
jbe 0x7ca0f
movq 0x10(%r12), %r13
imulq $0x28, %rcx, %rax
leal 0x1(%rcx), %r9d
cmpl $0x0, 0xc(%r13,%rax)
je 0x7c743
addq %rax, %r13
movl (%r13), %eax
shrl $0x1f, %eax
leaq 0x169f12(%rip), %rcx    # 0x1e668b
movsbl (%rax,%rcx), %r8d
leaq 0x50(%rsp), %rdi
leaq 0x16bb50(%rip), %rsi    # 0x1e82da
movq 0xb8(%rsp), %rdx
movl %r9d, 0x18(%rsp)
movl %r9d, %ecx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xb0(%rsp)
je 0x7c825
movq 0x20(%r13), %r14
testq %r14, %r14
leaq 0x16bb43(%rip), %rax    # 0x1e82fc
cmoveq %rax, %r14
movl (%r13), %r12d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r12d
xorl %r9d, %r9d
cmpb $0x0, 0x38(%rsp)
je 0x7c802
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x7c23f
popq %rcx
popq %rdx
movl %eax, %r9d
movq 0xc0(%rsp), %rdi
leaq 0x16bad2(%rip), %rsi    # 0x1e82e3
leaq 0x50(%rsp), %rdx
movq %r14, %rcx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22b0
jmp 0x7c888
movl (%r13), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
xorl %r8d, %r8d
cmpb $0x0, 0x38(%rsp)
je 0x7c86a
subq $0x10, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x7c23f
popq %rcx
popq %rdx
movl %eax, %r8d
movq 0xc0(%rsp), %rdi
leaq 0x16ba85(%rip), %rsi    # 0x1e82fe
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22b0
xorl %r12d, %r12d
xorl %r14d, %r14d
movl 0x10(%r13), %eax
cmpq %rax, %r12
jae 0x7ca00
movq 0x18(%r13), %rax
movq (%rax,%r12,8), %rax
cmpq $-0x1, %rax
je 0x7c8eb
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x167967(%rip), %rax    # 0x1e4223
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%rbp), %r10
jae 0x7c90e
testq %r15, %r15
je 0x7c931
imulq $0xc, %r10, %rax
movl (%r15,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r15,%rax), %eax
movq %rax, 0x28(%rsp)
jmp 0x7c941
movq 0xc0(%rsp), %rdi
leaq 0x16ba74(%rip), %rsi    # 0x1e836e
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0x7c9f0
testq %r15, %r15
je 0x7c9b5
imulq $0xc, %r10, %rax
movl (%r15,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r15,%rax), %ebx
jmp 0x7c9c0
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %eax
movq %rax, 0x28(%rsp)
xorl %r11d, %r11d
movq 0x28(%rbp), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0x7c954
xorl %eax, %eax
movq 0x40(%rbp), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq %rbp, %rbx
movq (%rcx,%r10,8), %rbp
subl %ebp, %r9d
addq 0x8(%rbx), %rbp
movzbl %al, %eax
leaq 0x16b9c8(%rip), %rcx    # 0x1e833d
movsbl (%rax,%rcx), %r15d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x16b989(%rip), %rsi    # 0x1e8316
leaq 0x58(%rsp), %rdx
movl %r14d, %ecx
xorl %eax, %eax
pushq %r15
pushq %r10
pushq 0x40(%rsp)
pushq %r11
pushq %rbp
movq %rbx, %rbp
callq 0x22b0
addq $0x30, %rsp
movq 0x8(%rsp), %r15
jmp 0x7c9f0
movq 0x18(%rbp), %rax
movl (%rax,%r10,8), %ebx
xorl %r11d, %r11d
movq 0xc0(%rsp), %rdi
leaq 0x16b975(%rip), %rsi    # 0x1e8344
leaq 0x50(%rsp), %rdx
movl %r14d, %ecx
leaq 0x16b98b(%rip), %r9     # 0x1e8369
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %rbx
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%r13), %rax
addl (%rax,%r12,8), %r14d
incq %r12
jmp 0x7c88e
movq 0x20(%rsp), %r12
movl 0x18(%rsp), %r9d
jmp 0x7c743
cmpq $0x0, 0x18(%r12)
je 0x7cd0a
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x7cd0a
movq 0x10(%r12), %rax
movq %rcx, 0x18(%rsp)
imulq $0x28, %rcx, %rbx
cmpl $0x0, 0xc(%rax,%rbx)
je 0x7ccfa
addq %rbx, %rax
movl (%rax), %r15d
testl %r15d, %r15d
js 0x7cc27
movq 0x18(%rsp), %rsi
leal (%rsi,%rsi), %edx
movq 0x18(%r12), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rax
movq %rdx, 0x38(%rsp)
movq (%rax,%rdx,8), %rax
movl %eax, %edx
shrq $0x20, %rax
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rsi, 0x8(%rsp)
incl %esi
movl %esi, 0x28(%rsp)
imulq $0x18, %rdx, %r14
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rbx
addq $0xc, %rbx
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x7cb43
movl (%rbx,%r15), %r10d
testl %r10d, %r10d
js 0x7cb3a
movl -0xc(%rbx,%r15), %r11d
movl -0x4(%rbx,%r15), %eax
movq 0x10(%r12), %rcx
imulq $0x28, 0x8(%rsp), %rdx
movl (%rcx,%rdx), %edx
shrl $0x1f, %edx
leaq 0x169bbb(%rip), %rsi    # 0x1e668b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r12d
shrl %r12d
imulq $0x28, %r12, %rdx
incl %r12d
movl (%rcx,%rdx), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %ebp
andl $0x1, %eax
leaq 0x16772d(%rip), %rcx    # 0x1e4223
movsbl (%rax,%rcx), %r13d
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x16b8d5(%rip), %rsi    # 0x1e83e3
movq 0xc0(%rsp), %rdx
movl 0x30(%rsp), %ecx
pushq $0x2b
popq %r9
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %r13
pushq %rbp
pushq %r12
movq 0x58(%rsp), %r12
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %r15
jmp 0x7ca98
movq 0x18(%r12), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rax
movl 0x38(%rsp), %edx
movq 0x8(%rax,%rdx,8), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rdx, %r14
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rbx
addq $0xc, %rbx
imulq $0x28, 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x7ccfa
movl (%rbx,%r15), %r10d
testl %r10d, %r10d
js 0x7cc1e
movl -0xc(%rbx,%r15), %r11d
movl -0x4(%rbx,%r15), %eax
movq 0x10(%r12), %rcx
movq 0x8(%rsp), %rdx
movl (%rcx,%rdx), %edx
shrl $0x1f, %edx
leaq 0x169ad7(%rip), %rsi    # 0x1e668b
movsbl (%rdx,%rsi), %r8d
movl %eax, %r12d
shrl %r12d
imulq $0x28, %r12, %rdx
incl %r12d
movl (%rcx,%rdx), %ecx
shrl $0x1f, %ecx
movsbl (%rcx,%rsi), %r13d
andl $0x1, %eax
leaq 0x167648(%rip), %rcx    # 0x1e4223
movsbl (%rax,%rcx), %ebp
subq $0x8, %rsp
movq 0xc8(%rsp), %rdi
leaq 0x16b7f1(%rip), %rsi    # 0x1e83e3
movq 0xc0(%rsp), %rdx
movl 0x30(%rsp), %ecx
pushq $0x2d
popq %r9
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
pushq %rbp
pushq %r13
pushq %r12
movq 0x58(%rsp), %r12
pushq %rdx
callq 0x22b0
addq $0x40, %rsp
addq $0x18, %r15
jmp 0x7cb7d
movq %r12, %rdi
movq 0x18(%rsp), %r12
movl %r12d, %esi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
callq 0x7c622
movl %eax, %r10d
leal 0x1(%r12), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r15d
subl %r10d, %r15d
movl $0x0, %eax
cmovbl %eax, %r15d
subq $0x8, %rsp
movq 0xc8(%rsp), %r13
movq %r13, %rdi
leaq 0x16b71d(%rip), %rsi    # 0x1e838d
movq 0xc0(%rsp), %rbp
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbp, %r8
movl %r14d, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r15
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq 0x20(%rsp), %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %r12
callq 0x7c622
movl %eax, %r10d
movq 0x10(%r12), %rax
movl (%rax,%rbx), %r11d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r11d
subl %r10d, %r11d
movl $0x0, %eax
cmovbl %eax, %r11d
subq $0x8, %rsp
movq %r13, %rdi
leaq 0x16b6db(%rip), %rsi    # 0x1e83b8
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbp, %r8
movl %r14d, %r9d
xorl %eax, %eax
pushq $0x0
pushq %r11
pushq %r10
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%rsp), %rcx
incl %ecx
movq (%r12), %rax
jmp 0x7ca1d
movq 0x30(%rsp), %rdi
callq 0x2220
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq