// File: karel-brinda[P]ococo/ext/htslib/cram/sam_header.c
// Function: sam_hdr_vadd
int sam_hdr_vadd(SAM_hdr *sh, const char *type, va_list ap, ...) {
    va_list args;
    SAM_hdr_type *h_type;
    SAM_hdr_tag *h_tag, *last;
    int new;
    khint32_t type_i = (type[0]<<8) | type[1], k;

    if (EOF == kputc_('@', &sh->text))
	return -1;
    if (EOF == kputsn(type, 2, &sh->text))
	return -1;

    if (!(h_type = pool_alloc(sh->type_pool)))
	return -1;
    if (-1 == (k = kh_put(sam_hdr, sh->h, type_i, &new)))
	return -1;

    // Form the ring, either with self or other lines of this type
    if (!new) {
	SAM_hdr_type *t = kh_val(sh->h, k), *p;
	p = t->prev;
	    
	assert(p->next == t);
	p->next = h_type;
	h_type->prev = p;

	t->prev = h_type;
	h_type->next = t;
	h_type->order = p->order + 1;
    } else {
	kh_val(sh->h, k) = h_type;
	h_type->prev = h_type->next = h_type;
	h_type->order = 0;
    }

    last = NULL;

    // Any ... varargs
    va_start(args, ap);
    for (;;) {
	char *k, *v;
	int idx;
	
	if (!(k = (char *)va_arg(args, char *)))
	    break;
	v = va_arg(args, char *);

	if (EOF == kputc_('\t', &sh->text))
	    return -1;

	if (!(h_tag = pool_alloc(sh->tag_pool)))
	    return -1;
	idx = ks_len(&sh->text);
	
	if (EOF == kputs(k, &sh->text))
	    return -1;
	if (EOF == kputc_(':', &sh->text))
	    return -1;
	if (EOF == kputs(v, &sh->text))
	    return -1;

	h_tag->len = ks_len(&sh->text) - idx;
	h_tag->str = string_ndup(sh->str_pool,
				 ks_str(&sh->text) + idx,
				 h_tag->len);
	h_tag->next = NULL;
	if (!h_tag->str)
	    return -1;

	if (last)
	    last->next = h_tag;
	else
	    h_type->tag = h_tag;
	
	last = h_tag;
    }
    va_end(args);

    // Plus the specified va_list params
    for (;;) {
	char *k, *v;
	int idx;
	
	if (!(k = (char *)va_arg(ap, char *)))
	    break;
	v = va_arg(ap, char *);

	if (EOF == kputc_('\t', &sh->text))
	    return -1;

	if (!(h_tag = pool_alloc(sh->tag_pool)))
	    return -1;
	idx = ks_len(&sh->text);
	
	if (EOF == kputs(k, &sh->text))
	    return -1;
	if (EOF == kputc_(':', &sh->text))
	    return -1;
	if (EOF == kputs(v, &sh->text))
	    return -1;

	h_tag->len = ks_len(&sh->text) - idx;
	h_tag->str = string_ndup(sh->str_pool,
				 ks_str(&sh->text) + idx,
				 h_tag->len);
	h_tag->next = NULL;
	if (!h_tag->str)
	    return -1;

	if (last)
	    last->next = h_tag;
	else
	    h_type->tag = h_tag;
	
	last = h_tag;
    }
    va_end(ap);

    if (EOF == kputc('\n', &sh->text))
	return -1;

    int itype = (type[0]<<8) | type[1];
    if (-1 == sam_hdr_update_hashes(sh, itype, h_type))
	return -1;

    return h_type->order;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
testb %al, %al
je 0x5c977
movaps %xmm0, -0x130(%rbp)
movaps %xmm1, -0x120(%rbp)
movaps %xmm2, -0x110(%rbp)
movaps %xmm3, -0x100(%rbp)
movaps %xmm4, -0xf0(%rbp)
movaps %xmm5, -0xe0(%rbp)
movaps %xmm6, -0xd0(%rbp)
movaps %xmm7, -0xc0(%rbp)
movq %r9, -0x138(%rbp)
movq %r8, -0x140(%rbp)
movq %rcx, -0x148(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rsi
movl $0x40, %edi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5c9d4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x2, %esi
callq 0x5b950
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5c9fd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x75ef0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x5ca20
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x60(%rbp), %esi
leaq -0x5c(%rbp), %rdx
callq 0x5ba90
movl %eax, %ecx
movl %ecx, -0x64(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5ca4e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
cmpl $0x0, -0x5c(%rbp)
jne 0x5caea
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rax
cmpq -0x70(%rbp), %rax
jne 0x5ca8a
jmp 0x5caa9
leaq 0x21049(%rip), %rdi     # 0x7dada
leaq 0x2104f(%rip), %rsi     # 0x7dae7
movl $0x1ca, %edx            # imm = 0x1CA
leaq 0x210ae(%rip), %rcx     # 0x7db52
callq 0x4360
movq -0x48(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x78(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x5cb1f
movq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x64(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x18(%rax)
movq $0x0, -0x58(%rbp)
leaq -0x40(%rbp), %rax
leaq -0x160(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x18, (%rax)
leaq -0x40(%rbp), %rax
movq %rax, -0x170(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x164(%rbp)
cmpl $0x28, %eax
ja 0x5cb86
movq -0x170(%rbp), %rcx
movl -0x164(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x178(%rbp)
jmp 0x5cba3
movq -0x170(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
jne 0x5cbbc
jmp 0x5cd9b
leaq -0x40(%rbp), %rax
movq %rax, -0x188(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x17c(%rbp)
cmpl $0x28, %eax
ja 0x5cbf7
movq -0x188(%rbp), %rcx
movl -0x17c(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x190(%rbp)
jmp 0x5cc14
movq -0x188(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rsi
movl $0x9, %edi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5cc4a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x75ef0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x5cc6d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rdi
callq 0x5b930
movl %eax, -0x8c(%rbp)
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5d0e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5cca0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rsi
movl $0x3a, %edi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5ccc5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5d0e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5ccec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rdi
callq 0x5b930
movslq -0x8c(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x198(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5b9e0
movq -0x198(%rbp), %rdi
movq %rax, %rsi
movslq -0x8c(%rbp), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
movslq 0x10(%rax), %rdx
callq 0x601b0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5cd6e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
cmpq $0x0, -0x58(%rbp)
je 0x5cd82
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
jmp 0x5cd8e
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x5cb4b
leaq -0x40(%rbp), %rax
movq -0x20(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movl (%rax), %eax
movl %eax, -0x19c(%rbp)
cmpl $0x28, %eax
ja 0x5cdd9
movq -0x1a8(%rbp), %rcx
movl -0x19c(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x1b0(%rbp)
jmp 0x5cdf6
movq -0x1a8(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x1b0(%rbp)
movq -0x1b0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
jne 0x5ce12
jmp 0x5cff3
movq -0x20(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movl (%rax), %eax
movl %eax, -0x1b4(%rbp)
cmpl $0x28, %eax
ja 0x5ce4c
movq -0x1c0(%rbp), %rcx
movl -0x1b4(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x1c8(%rbp)
jmp 0x5ce69
movq -0x1c0(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rsi
movl $0x9, %edi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5ce9f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x75ef0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x5cec2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rdi
callq 0x5b930
movl %eax, -0xa4(%rbp)
movq -0x98(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5d0e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5cef8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rsi
movl $0x3a, %edi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5cf1d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0xa0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5d0e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5cf44
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x10(%rbp), %rdi
callq 0x5b930
movslq -0xa4(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x1d0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5b9e0
movq -0x1d0(%rbp), %rdi
movq %rax, %rsi
movslq -0xa4(%rbp), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
movslq 0x10(%rax), %rdx
callq 0x601b0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5cfc6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
cmpq $0x0, -0x58(%rbp)
je 0x5cfda
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
jmp 0x5cfe6
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x5cd9f
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rsi
movl $0xa, %edi
callq 0x5d120
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5d019
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x5bde0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5d05a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5d064
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq