// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/sdict.c
// Function: write_segs_to_agp
void write_segs_to_agp(sd_seg_t *segs, uint32_t n, sd_aseq_t *aseq, sdict_t *sd, char *name, FILE *fo)
{
    uint64_t len;
    sd_seg_t seg;
    uint32_t i, j, t;
    len = 0;
    t = 0;
    if (!name) name = aseq? aseq->name : 0;
    if (!name) {
        fprintf(stderr, "[E::%s] sequence name required\n", __func__);
        exit(EXIT_FAILURE);
    } 
    if (aseq) {
        // with gap information
        for (i = 0, j = 0; i < n; ++i) {
            seg = segs[i];
            while (j < aseq->gn && aseq->gs[j] <= i) {
                // print gap
                write_agp_gap(fo, name, len + 1, len + aseq->gsize[j], ++t, aseq->gcomp[j], aseq->gsize[j], 
                        aseq->gtype[j], aseq->glink[j], aseq->gevid[j]);
                len += aseq->gsize[j];
                ++j;
            }
            write_agp_seq(fo, name, seg.b + 1, seg.b + seg.y, ++t, seg.t, sd->s[seg.c >> 1].name, 
                    seg.x + 1, seg.x + seg.y, seg.r);
            len += seg.y;
        }
        while (j < aseq->gn) {
            // print trailing gap
            write_agp_gap(fo, name, len + 1, len + aseq->gsize[j], ++t, aseq->gcomp[j], aseq->gsize[j], 
                    aseq->gtype[j], aseq->glink[j], aseq->gevid[j]);
            len += aseq->gsize[j];
            ++j;
        }
        if (len != aseq->len + aseq->gap) {
            fprintf(stderr, "[E::%s] sequence length conflict: %lu %lu\n", __func__, len, aseq->len + aseq->gap);
            //exit(EXIT_FAILURE);
        }
    } else {
        // no gap information provided
        // using default settings
        for (i = 0; i < n; ++i) {
            seg = segs[i];
            write_agp_seq(fo, name, len + 1, len + seg.y, ++t, seg.t, sd->s[seg.c >> 1].name, seg.x + 1, seg.x + seg.y, seg.r);
            /***
            fprintf(fo, "%s\t%lu\t%lu\t%d\t%s\t%s\t%u\t%u\t%s\n", 
                    name, len + 1, len + seg.y, ++t,
                    agp_component_type_val(DEFAULT_AGP_SEQ_COMPONENT_TYPE),
                    sd->s[seg.c >> 1].name, seg.x + 1, seg.x + seg.y,
                    seg.c & 1? agp_orientation_val(AGP_OT_MINUS) : agp_orientation_val(AGP_OT_PLUS));
            **/
            len += seg.y;
            if (i != n - 1) {
                write_agp_gap(fo, name, len + 1, len + DEFAULT_AGP_GAP_SIZE, ++t, DEFAULT_AGP_GAP_COMPONENT_TYPE, 
                        DEFAULT_AGP_GAP_SIZE, DEFAULT_AGP_GAP_TYPE, AGP_LG_YES, DEFAULT_AGP_LINKAGE_EVIDENCE);
                /***
                fprintf(fo, "%s\t%lu\t%lu\t%d\t%s\t%d\t%s\t%s\t%s\n", 
                        name, len + 1, len + DEFAULT_AGP_GAP_SIZE, ++t,
                        agp_component_type_val(DEFAULT_AGP_GAP_COMPONENT_TYPE), 
                        DEFAULT_AGP_GAP_SIZE,
                        agp_gap_type_val(DEFAULT_AGP_GAP_TYPE),
                        agp_linkage_val(AGP_LG_YES),
                        agp_linkage_evidence_val(DEFAULT_AGP_LINKAGE_EVIDENCE));
                **/
                len += DEFAULT_AGP_GAP_SIZE;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x18(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, %r13
movq %rdi, 0x40(%rsp)
movq %r8, 0x10(%rsp)
testq %r8, %r8
je 0x6565
testq %r13, %r13
jne 0x6580
leal -0x1(%rsi), %eax
movl %esi, %ecx
imulq $0x30, %rcx, %rcx
movq %rcx, 0x8(%rsp)
imulq $0x30, %rax, %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r8d, %r8d
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rbx
cmpq %r13, 0x8(%rsp)
je 0x67ba
movq 0x40(%rsp), %rdx
movl 0x18(%rdx,%r13), %ecx
movl 0x1c(%rdx,%r13), %eax
movl 0x20(%rdx,%r13), %esi
movl 0x24(%rdx,%r13), %r9d
movl 0x28(%rdx,%r13), %r10d
leaq 0x1(%r15), %rdx
addq %rsi, %r15
leal 0x1(%r8), %ebp
movq 0x48(%rsp), %rdi
movq 0x8(%rdi), %rdi
movq %rdi, 0x30(%rsp)
shrl %ecx
imulq $0x18, %rcx, %r11
leal (%rsi,%rax), %r14d
incl %eax
movq %r12, %rdi
movq %r8, 0x28(%rsp)
movq %rbx, %rsi
movq %r15, %rcx
movl %ebp, %r8d
pushq %r10
pushq %r14
pushq %rax
movq 0x48(%rsp), %rax
pushq (%rax,%r11)
callq 0x6acf
addq $0x20, %rsp
cmpq %r13, 0x20(%rsp)
je 0x6559
leaq 0x1(%r15), %rdx
movslq 0x6d24(%rip), %rax      # 0xd230
movq %rax, %rcx
addq %r15, %rcx
movq 0x28(%rsp), %r14
addl $0x2, %r14d
movl 0x6d02(%rip), %r9d      # 0xd224
movl 0x6cff(%rip), %r10d     # 0xd228
movl 0x6cfc(%rip), %r11d     # 0xd22c
movq %r12, %rdi
movq %rbx, %rsi
movl %r14d, %r8d
pushq %r11
pushq $0xb88c316              # imm = 0xB88C316
pushq %r10
pushq %rax
callq 0x6820
addq $0x20, %rsp
movslq 0x6cdd(%rip), %rax      # 0xd230
addq %rax, %r15
movl %r14d, %ebp
addq $0x30, %r13
movl %ebp, %r8d
jmp 0x6484
testq %r13, %r13
je 0x67f9
movq (%r13), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x67f9
movl %esi, %eax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
movq %r13, 0x28(%rsp)
movl %edi, %edi
cmpq 0x50(%rsp), %r12
je 0x6729
imulq $0x30, %r12, %rax
movq 0x40(%rsp), %rcx
movq 0x10(%rcx,%rax), %rdx
movq %rdx, 0x68(%rsp)
movl 0x18(%rcx,%rax), %edx
movq %rdx, 0x58(%rsp)
movl 0x1c(%rcx,%rax), %edx
movq %rdx, 0x70(%rsp)
movl 0x20(%rcx,%rax), %edx
movq %rdx, 0x20(%rsp)
movl 0x24(%rcx,%rax), %edx
movl %edx, 0x3c(%rsp)
movl 0x28(%rcx,%rax), %eax
movq %rax, 0x60(%rsp)
leaq (,%rdi,4), %r14
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
leaq (%rdi,%rbx), %rax
movl 0x20(%r13), %ecx
cmpq %rcx, %rax
jae 0x66b1
movq 0x28(%r13), %rax
addq %r14, %rax
movl (%rax,%rbx,4), %eax
cmpq %rax, %r12
jb 0x66b1
leaq 0x1(%rbp), %rdx
movq 0x30(%r13), %rax
addq %r14, %rax
movl (%rax,%rbx,4), %eax
movq %rax, %rcx
addq %rbp, %rcx
movq 0x30(%rsp), %rsi
leal (%rsi,%rbx), %r8d
movq 0x38(%r13), %rsi
addq %r14, %rsi
movl (%rsi,%rbx,4), %r9d
movq 0x40(%r13), %rsi
addq %r14, %rsi
movl (%rsi,%rbx,4), %r10d
movq 0x48(%r13), %rsi
addq %r14, %rsi
movl (%rsi,%rbx,4), %r11d
movq 0x50(%r13), %rsi
addq %r14, %rsi
movl (%rsi,%rbx,4), %r15d
movq %rbp, %r13
movq %r12, %rbp
movq %rdi, %r12
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
pushq %r15
pushq %r11
pushq %r10
pushq %rax
callq 0x6820
movq %r12, %rdi
movq %rbp, %r12
movq %r13, %rbp
movq 0x48(%rsp), %r13
addq $0x20, %rsp
movq 0x30(%r13), %rax
addq %r14, %rax
movl (%rax,%rbx,4), %eax
addq %rax, %rbp
incq %rbx
jmp 0x65fe
addl %ebx, %edi
movq 0x68(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movq 0x20(%rsp), %r15
addq %r15, %rcx
movq 0x8(%rsp), %r8
addl %ebx, %r8d
incl %r8d
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x58(%rsp), %rsi
shrl %esi
imulq $0x18, %rsi, %r10
movq 0x70(%rsp), %r14
leal (%r15,%r14), %r11d
incl %r14d
movq %rdi, %rbx
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r8, 0x8(%rsp)
movl 0x3c(%rsp), %r9d
pushq 0x60(%rsp)
pushq %r11
pushq %r14
pushq (%rax,%r10)
callq 0x6acf
movq %rbx, %rdi
addq $0x20, %rsp
addq %r15, %rbp
incq %r12
jmp 0x659c
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
movl 0x20(%r13), %eax
cmpq %rax, %rdi
jae 0x67ad
leaq 0x1(%rbp), %rdx
movq 0x30(%r13), %rax
movq 0x38(%r13), %rsi
movl (%rax,%rdi,4), %eax
movq %rax, %rcx
addq %rbp, %rcx
movl (%rsi,%rdi,4), %r9d
movq 0x40(%r13), %rsi
movl (%rsi,%rdi,4), %r10d
movq 0x48(%r13), %rsi
movl (%rsi,%rdi,4), %r11d
movq 0x50(%r13), %rsi
movl (%rsi,%rdi,4), %ebx
movq %rdi, %r12
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r14
movl %r14d, %r8d
pushq %rbx
pushq %r11
pushq %r10
pushq %rax
callq 0x6820
movq %r12, %rdi
addq $0x20, %rsp
movq 0x30(%r13), %rax
movl (%rax,%r12,4), %eax
addq %rax, %rbp
incq %rdi
incl %r14d
movq %r14, 0x8(%rsp)
jmp 0x6735
movq 0x10(%r13), %r8
addq 0x8(%r13), %r8
cmpq %r8, %rbp
jne 0x67c9
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6820(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x2fc0(%rip), %rsi      # 0x979a
leaq 0x2fa7(%rip), %rdx      # 0x9788
movq %rbp, %rcx
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2200
movq 0x67f0(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x2f5e(%rip), %rsi      # 0x9768
leaq 0x2f77(%rip), %rdx      # 0x9788
xorl %eax, %eax
callq 0x2200
pushq $0x1
popq %rdi
callq 0x2360