// File: /workspace/github_c_makefile_star2_O0/skoren[P]bacValidation/bamcat.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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xad55
movl -0x1c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xad44
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3150
movq %rax, %rcx
addq $0x1, %rcx
movslq -0x1c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x1c(%rbp)
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0xad18
movq -0x40(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
je 0xad36
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3150
movq %rax, %rcx
addq $0x3, %rcx
movslq -0x1c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x1c(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xacce
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xaca2
movl $0x0, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x3520
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xaeac
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rax
movsbl (%rax), %edx
movq -0x48(%rbp), %rax
movsbl 0x1(%rax), %ecx
leaq 0x781e(%rip), %rsi      # 0x125cc
movb $0x0, %al
callq 0x30c0
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0xae7c
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
leaq 0x7d45(%rip), %rsi      # 0x12b35
movb $0x0, %al
callq 0x30c0
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0xae16
movq -0x58(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x20, %eax
je 0xae44
movq -0x18(%rbp), %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
movq -0x58(%rbp), %rax
movsbl (%rax), %edx
movq -0x58(%rbp), %rax
movsbl 0x1(%rax), %ecx
leaq 0x779b(%rip), %rsi      # 0x125d2
movb $0x0, %al
callq 0x30c0
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x7cb4(%rip), %rsi      # 0x12b12
movb $0x0, %al
callq 0x30c0
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0xadc7
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
leaq 0x73d3(%rip), %rsi      # 0x1226d
movb $0x0, %al
callq 0x30c0
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0xad76
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)