// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_scg_print(kvect_sec_t const*, All_reads*, asg_t*, ma_sub_t const*, ma_hit_t_alloc*, R_to_U*, int, char const*, _IO_FILE*)
void ma_scg_print(const kvect_sec_t *sc, 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;
	uint64_t i, j, l, pc = read_g && coverage_cut && sources && ruIndex?1:0, tl, m, x; sec_t *scp;
	char name[32]; uint64_t Rb, Cb, tRb, tCb, Cov; ma_utg_t *z = NULL; uint8_t c;
	for (i = 0; i < sc->n; ++i) { // the Segment lines in GFA
        scp = &(sc->a[i]);
        ///debug
        // prt_scaf_stats(scp, sc->ctg, prefix, i);

        for (j = tl = tRb = tCb = 0; j < scp->n; j++) {
            z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); tl += z->len; tl += (scp->a[j]>>32);
            if(pc) {
                get_ug_coverage(z, read_g, coverage_cut, sources, ruIndex, primary_flag, &Rb, &Cb); tRb += Rb; tCb += Cb;
            }
        }
        sprintf(name, "%s%.6lu%c", prefix, i + 1, "lc"[scp->is_c]); Cov = ((tRb==0)?(0):(tCb/tRb));

        if (!print_seq) {
            fprintf(fp, "S\t%s\t*\tLN:i:%lu\trd:i:%lu\n", name, tl, Cov);
        } else {
            fprintf(fp, "S\t%s\t", name);
            for (j = 0; j < scp->n; j++) {
                z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); 
                if(!(((uint32_t)scp->a[j])&1)) {
                    fprintf(fp, "%s", z->s);
                } else {
                    for(l = 0; l < z->len; l++) {
                        c = (uint8_t)(z->s[z->len-1-l]);
                        fprintf(fp, "%c", ((c>=128)?'N':comp_tab[c]));
                    }
                }
                for(l = 0; l < (scp->a[j]>>32); l++) fprintf(fp, "N");
            }
            fprintf(fp, "\tLN:i:%lu\trd:i:%lu\n", tl, Cov);
        }


        for (j = tl = 0; j < scp->n; j++) {
            z = &(sc->ctg->u.a[((uint32_t)scp->a[j])>>1]); l = tl;
            if(!(((uint32_t)scp->a[j])&1)) {
                for (m = 0; m < z->n; m++) {
                    x = z->a[m]>>33;
                    if(x<RNF->total_reads) {
                        fprintf(fp, "A\t%s\t%lu\t%c\t%.*s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[(z->a[m]>>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%lu\t%c\t%s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[(z->a[m]>>32)&1],
                            "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                    }
                    l += (uint32_t)(z->a[m]);
                }
            } else {
                for (m = 0; m < z->n; m++) {
                    x = z->a[z->n-m-1]>>33;
                    if(x<RNF->total_reads) {
                        fprintf(fp, "A\t%s\t%lu\t%c\t%.*s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[((z->a[z->n-m-1]>>32)&1)^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%lu\t%c\t%s\t%d\t%d\tid:i:%lu\tHG:A:%c\n", name, l, "+-"[((z->a[z->n-m-1]>>32)&1)^1],
                            "FAKE", coverage_cut?coverage_cut[x].s:0, coverage_cut?coverage_cut[x].e:(int)Get_READ_LENGTH((*RNF), x), x, '*');
                    }
                    l += (uint32_t)(z->a[z->n-m-1]);
                }
            }
            tl = l;

            if((scp->a[j]>>32) > 0) fprintf(fp, "A\t%s\t%lu\t+\tNs\t0\t%lu\tid:i:-1\tHG:A:a\n", name, l, (scp->a[j]>>32));

            tl += (scp->a[j]>>32);
        }
	}
    free(primary_flag);    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r13
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
testq %rdx, %rdx
movq %rdx, 0x40(%rsp)
je 0x7cef7
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rdx
jmp 0x7cf00
movq $0x0, 0x28(%rsp)
testq %rdx, %rdx
setne %al
testq %r13, %r13
setne %sil
cmpq $0x0, 0x48(%rsp)
setne %cl
cmpq $0x0, 0x50(%rsp)
setne %dl
andb %cl, %dl
andb %al, %sil
andb %dl, %sil
movb %sil, 0x7(%rsp)
pushq $0x4e
popq %rbp
xorl %eax, %eax
movq %r13, 0x58(%rsp)
movq %r14, 0x10(%rsp)
cmpq 0x8(%r14), %rax
jae 0x7d4e5
movq %rax, 0x20(%rsp)
shlq $0x5, %rax
addq (%r14), %rax
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x38(%rsp), %r15
cmpq 0x8(%r15), %r12
jae 0x7d010
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rax), %rdi
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %r15
movl %r15d, %eax
shrl %eax
imulq $0x28, %rax, %rax
movl (%rdi,%rax), %ebp
cmpb $0x0, 0x7(%rsp)
je 0x7cff2
addq %rax, %rdi
movq %rbx, 0x8(%rsp)
movq %r14, %rbx
movq %rcx, %r14
movq 0x40(%rsp), %rsi
movq %r13, %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x28(%rsp), %r9
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0x7c23f
movq %r14, %rcx
movq %rbx, %r14
movq 0x18(%rsp), %rbx
addq $0x10, %rsp
addq 0x90(%rsp), %r14
addq 0x88(%rsp), %rbx
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
shrq $0x20, %r15
addq %rcx, %r15
addq %rbp, %r15
incq %r12
movq %r15, %rcx
pushq $0x4e
popq %rbp
jmp 0x7cf60
movq %rcx, 0x8(%rsp)
movq 0x20(%rsp), %r9
incq %r9
movq 0x10(%r15), %rax
leaq 0x169663(%rip), %rcx    # 0x1e668b
movsbl (%rax,%rcx), %r8d
leaq 0x60(%rsp), %rdi
leaq 0x16b3d9(%rip), %rsi    # 0x1e8412
movq 0xd8(%rsp), %rdx
movq %r9, 0x20(%rsp)
movq %r9, %rcx
xorl %eax, %eax
callq 0x2050
testq %r14, %r14
je 0x7d05f
movq %rbx, %rax
xorl %edx, %edx
divq %r14
jmp 0x7d061
xorl %eax, %eax
movq 0x10(%rsp), %r14
cmpl $0x0, 0xd0(%rsp)
je 0x7d167
movq %rax, 0x30(%rsp)
movq 0xe0(%rsp), %rdi
leaq 0x16b421(%rip), %rsi    # 0x1e84a9
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
xorl %ebx, %ebx
cmpq 0x8(%r15), %rbx
jae 0x7d145
movq 0x18(%r14), %rax
movq 0x18(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrl %edx
imulq $0x28, %rdx, %r14
addq 0x10(%rax), %r14
testb $0x1, %cl
jne 0x7d101
movq 0x20(%r14), %rdi
movq 0xe0(%rsp), %rsi
callq 0x21d0
xorl %r14d, %r14d
movq 0x38(%rsp), %r15
movq 0x18(%r15), %rax
movl 0x4(%rax,%rbx,8), %eax
cmpq %rax, %r14
jae 0x7d0f7
movl %ebp, %edi
movq 0xe0(%rsp), %rsi
callq 0x2270
incq %r14
jmp 0x7d0d6
incq %rbx
movq 0x10(%rsp), %r14
jmp 0x7d096
pushq $-0x1
popq %r15
xorl %r12d, %r12d
movl (%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jae 0x7d0ce
addq 0x20(%r14), %rax
movsbq (%r15,%rax), %rax
movl %ebp, %edi
testq %rax, %rax
js 0x7d130
leaq 0x16abd4(%rip), %rcx    # 0x1e7d00
movsbl (%rax,%rcx), %edi
movq 0xe0(%rsp), %rsi
callq 0x2270
incq %r12
decq %r15
jmp 0x7d108
movq 0xe0(%rsp), %rdi
leaq 0x16b341(%rip), %rsi    # 0x1e8495
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
jmp 0x7d18a
movq 0xe0(%rsp), %rdi
leaq 0x16b319(%rip), %rsi    # 0x1e848f
leaq 0x60(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x22b0
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x18(%rsp), %rsi
cmpq 0x8(%r15), %r12
jae 0x7d4db
movq 0x18(%r14), %rax
movq 0x18(%r15), %rcx
movq %r12, 0x30(%rsp)
movq (%rcx,%r12,8), %rcx
movl %ecx, %edx
shrl %edx
imulq $0x28, %rdx, %r12
addq 0x10(%rax), %r12
testb $0x1, %cl
jne 0x7d30f
xorl %r14d, %r14d
movl 0x10(%r12), %eax
cmpq %rax, %r14
jae 0x7d481
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x167032(%rip), %rax    # 0x1e4223
adcq $0x0, %rax
movsbl (%rax), %r8d
cmpq 0x50(%rsi), %r10
jae 0x7d21c
testq %r13, %r13
je 0x7d240
imulq $0xc, %r10, %rax
movl (%r13,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r13,%rax), %ebp
jmp 0x7d24b
testq %r13, %r13
je 0x7d2ba
imulq $0xc, %r10, %rax
movl (%r13,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r13,%rax), %r15d
jmp 0x7d2c5
movq 0x18(%rsi), %rax
movl (%rax,%r10,8), %ebp
xorl %r11d, %r11d
movq 0x28(%rsi), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0x7d25e
xorl %eax, %eax
movq 0x40(%rsi), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r15
subl %r15d, %r9d
addq 0x8(%rsi), %r15
movzbl %al, %eax
leaq 0x16b0c1(%rip), %rcx    # 0x1e833d
movsbl (%rax,%rcx), %r13d
subq $0x8, %rsp
movq 0xe8(%rsp), %rdi
leaq 0x16b21b(%rip), %rsi    # 0x1e84af
leaq 0x68(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
pushq %r13
pushq %r10
pushq %rbp
pushq %r11
pushq %r15
callq 0x22b0
addq $0x30, %rsp
movq 0x58(%rsp), %r13
pushq $0x4e
popq %rbp
jmp 0x7d2f6
movq 0x18(%rsi), %rax
movl (%rax,%r10,8), %r15d
xorl %r11d, %r11d
movq 0xe0(%rsp), %rdi
leaq 0x16b204(%rip), %rsi    # 0x1e84d8
leaq 0x60(%rsp), %rdx
movq %rbx, %rcx
leaq 0x16b086(%rip), %r9     # 0x1e8369
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r15
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movq 0x18(%r12), %rax
movl (%rax,%r14,8), %eax
addq %rax, %rbx
incq %r14
movq 0x18(%rsp), %rsi
jmp 0x7d1c7
movl 0x10(%r12), %eax
pushq $-0x8
popq %rbp
xorl %r14d, %r14d
movl %eax, %eax
cmpq %rax, %r14
jae 0x7d47e
shlq $0x3, %rax
addq 0x18(%r12), %rax
movq (%rbp,%rax), %rax
movq %rax, %r10
shrq $0x21, %r10
btq $0x20, %rax
leaq 0x166edd(%rip), %rax    # 0x1e4223
sbbq $-0x1, %rax
movsbl (%rax), %r8d
cmpq 0x50(%rsi), %r10
jae 0x7d376
testq %r13, %r13
je 0x7d39a
imulq $0xc, %r10, %rax
movl (%r13,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r13,%rax), %eax
movq %rax, 0x8(%rsp)
jmp 0x7d3aa
testq %r13, %r13
je 0x7d419
imulq $0xc, %r10, %rax
movl (%r13,%rax), %r11d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r11d
movl 0x4(%r13,%rax), %r15d
jmp 0x7d424
movq 0x18(%rsi), %rax
movl (%rax,%r10,8), %eax
movq %rax, 0x8(%rsp)
xorl %r11d, %r11d
movq 0x28(%rsi), %rax
movzbl (%rax,%r10), %eax
leal -0x1(%rax), %ecx
cmpb $0x2, %cl
jb 0x7d3bd
xorl %eax, %eax
movq 0x40(%rsi), %rcx
movl 0x8(%rcx,%r10,8), %r9d
movq (%rcx,%r10,8), %r13
subl %r13d, %r9d
addq 0x8(%rsi), %r13
movzbl %al, %eax
leaq 0x16af62(%rip), %rcx    # 0x1e833d
movsbl (%rax,%rcx), %r15d
subq $0x8, %rsp
movq 0xe8(%rsp), %rdi
leaq 0x16b0bc(%rip), %rsi    # 0x1e84af
leaq 0x68(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
pushq %r15
pushq %r10
pushq 0x20(%rsp)
pushq %r11
pushq %r13
callq 0x22b0
addq $0x30, %rsp
movq 0x58(%rsp), %r13
jmp 0x7d455
movq 0x18(%rsi), %rax
movl (%rax,%r10,8), %r15d
xorl %r11d, %r11d
movq 0xe0(%rsp), %rdi
leaq 0x16b0a5(%rip), %rsi    # 0x1e84d8
leaq 0x60(%rsp), %rdx
movq %rbx, %rcx
leaq 0x16af27(%rip), %r9     # 0x1e8369
xorl %eax, %eax
pushq $0x2a
pushq %r10
pushq %r15
pushq %r11
callq 0x22b0
addq $0x20, %rsp
movl 0x10(%r12), %eax
movq %rax, %rcx
shlq $0x3, %rcx
addq 0x18(%r12), %rcx
movl (%rbp,%rcx), %ecx
addq %rcx, %rbx
incq %r14
addq $-0x8, %rbp
movq 0x18(%rsp), %rsi
jmp 0x7d31a
pushq $0x4e
popq %rbp
movq 0x38(%rsp), %r15
movq 0x18(%r15), %rax
movq 0x30(%rsp), %r12
movq (%rax,%r12,8), %rax
movq %rax, %r8
shrq $0x20, %r8
movq 0x10(%rsp), %r14
je 0x7d4cc
movq 0xe0(%rsp), %rdi
leaq 0x16b04f(%rip), %rsi    # 0x1e84ff
leaq 0x60(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22b0
movq 0x18(%rsp), %rsi
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rax
shrq $0x20, %rax
addq %rax, %rbx
incq %r12
jmp 0x7d194
movq 0x20(%rsp), %rax
jmp 0x7cf3b
movq 0x28(%rsp), %rdi
callq 0x2220
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq