// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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 0x162c3
xorl %ebp, %ebp
movq %rdi, %r12
movq %rdi, %r14
addl $0x4, %ebp
movq 0x10(%r14), %rax
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x161ee
movq 0x10(%r15), %rbx
movq 0x8(%rbx), %rdi
callq 0x31e0
addl %eax, %ebp
incl %ebp
cmpb $0x20, (%rbx)
jne 0x161e3
cmpb $0x20, 0x1(%rbx)
je 0x161e8
addl %eax, %ebp
addl $0x3, %ebp
movq 0x8(%r15), %r15
jmp 0x161c2
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x161b7
leal 0x1(%rbp), %eax
movslq %eax, %rdi
callq 0x33e0
movq %rax, %r14
testq %r12, %r12
je 0x162d2
movq %r12, %rcx
xorl %eax, %eax
movq %rcx, (%rsp)
movq 0x10(%rcx), %rbx
movslq %eax, %r12
leaq (%r14,%r12), %rdi
movsbl (%rbx), %edx
movsbl 0x1(%rbx), %ecx
leaq 0x6cb8(%rip), %rsi      # 0x1cee8
xorl %eax, %eax
callq 0x3510
addl %eax, %r12d
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x162a4
movq 0x10(%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 0x16285
movsbl %al, %ecx
movslq %r12d, %r12
leaq (%r14,%r12), %rdi
leaq 0x6c73(%rip), %rsi      # 0x1ceee
xorl %eax, %eax
callq 0x3510
addl %eax, %r12d
movslq %r12d, %r12
leaq (%r14,%r12), %r13
movq 0x8(%rbx), %rsi
movq %r13, %rdi
callq 0x31d0
subl %r13d, %eax
addl %eax, %r12d
movq 0x8(%r15), %r15
jmp 0x1623e
movq (%rsp), %rcx
movq 0x8(%rcx), %rcx
movslq %r12d, %rax
movw $0xa, (%r14,%rax)
incl %eax
testq %rcx, %rcx
jne 0x16213
jmp 0x162d2
movl $0x1, %edi
callq 0x33e0
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