// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/sam_header.c
// Function: sam_header_write
char *sam_header_write(const void *_header)
{
	const HeaderDict *header = (const HeaderDict*)_header;
    char *out = NULL;
    int len=0, nout=0;
    const list_t *hlines;

    // Calculate the length of the string to allocate
    hlines = header;
    while (hlines)
    {
        len += 4;   // @XY and \n

        HeaderLine *hline = hlines->data;
        list_t *tags = hline->tags;
        while (tags)
        {
            HeaderTag *tag = tags->data;
            len += strlen(tag->value) + 1;                  // \t
            if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
                len += strlen(tag->value) + 3;              // XY:
            tags = tags->next;
        }
        hlines = hlines->next;
    }

    nout = 0;
    out  = malloc(len+1);
    hlines = header;
    while (hlines)
    {
        HeaderLine *hline = hlines->data;

        nout += sprintf(out+nout,"@%c%c",hline->type[0],hline->type[1]);

        list_t *tags = hline->tags;
        while (tags)
        {
            HeaderTag *tag = tags->data;
            nout += sprintf(out+nout,"\t");
            if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
                nout += sprintf(out+nout,"%c%c:", tag->key[0],tag->key[1]);
            nout += sprintf(out+nout,"%s", tag->value);
            tags = tags->next;
        }
        hlines = hlines->next;
        nout += sprintf(out+nout,"\n");
    }
    out[len] = 0;
    return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x1f782
xorl %ebp, %ebp
movq %rdi, %r12
movq %rdi, %r14
addl $0x4, %ebp
movq 0x8(%r14), %rax
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x1f6b0
movq 0x8(%r15), %rbx
movq 0x8(%rbx), %rdi
callq 0x32d0
addl %eax, %ebp
incl %ebp
cmpb $0x20, (%rbx)
jne 0x1f6a6
cmpb $0x20, 0x1(%rbx)
je 0x1f6ab
addl %eax, %ebp
addl $0x3, %ebp
movq (%r15), %r15
jmp 0x1f685
movq (%r14), %r14
testq %r14, %r14
jne 0x1f67a
leal 0x1(%rbp), %eax
movslq %eax, %rdi
callq 0x31d0
movq %rax, %r14
testq %r12, %r12
je 0x1f791
movq %r12, %rcx
xorl %eax, %eax
movq %rcx, (%rsp)
movq 0x8(%rcx), %rbx
movslq %eax, %r12
leaq (%r14,%r12), %rdi
movsbl (%rbx), %edx
movsbl 0x1(%rbx), %ecx
leaq 0x7667(%rip), %rsi      # 0x26d58
xorl %eax, %eax
callq 0x3350
addl %eax, %r12d
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x1f764
movq 0x8(%r15), %rbx
movslq %r12d, %r12
movw $0x9, (%r14,%r12)
incl %r12d
movsbl (%rbx), %edx
movl %edx, %ecx
xorb $0x20, %cl
movb 0x1(%rbx), %al
movl %eax, %esi
xorb $0x20, %sil
orb %cl, %sil
je 0x1f746
movsbl %al, %ecx
movslq %r12d, %r12
leaq (%r14,%r12), %rdi
leaq 0x5dc8(%rip), %rsi      # 0x25504
xorl %eax, %eax
callq 0x3350
addl %eax, %r12d
movslq %r12d, %r12
leaq (%r14,%r12), %r13
movq 0x8(%rbx), %rsi
movq %r13, %rdi
callq 0x30c0
subl %r13d, %eax
addl %eax, %r12d
movq (%r15), %r15
jmp 0x1f6ff
movq (%rsp), %rcx
movq (%rcx), %rcx
movslq %r12d, %rax
movw $0xa, (%r14,%rax)
incl %eax
testq %rcx, %rcx
jne 0x1f6d4
jmp 0x1f791
movl $0x1, %edi
callq 0x31d0
movq %rax, %r14
xorl %ebp, %ebp
movslq %ebp, %rax
movb $0x0, (%r14,%rax)
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq