// File: karel-brinda[P]ococo/ext/htslib/cram/sam_header.c
// Function: sam_hdr_rebuild
int sam_hdr_rebuild(SAM_hdr *hdr) {
    /* Order: HD then others */
    kstring_t ks = KS_INITIALIZER;
    khint_t k;


    k = kh_get(sam_hdr, hdr->h, K("HD"));
    if (k != kh_end(hdr->h)) {
	SAM_hdr_type *ty = kh_val(hdr->h, k);
	SAM_hdr_tag *tag;
	if (EOF == kputs("@HD", &ks))
	    return -1;
	for (tag = ty->tag; tag; tag = tag->next) {
	    if (EOF == kputc_('\t', &ks))
		return -1;
	    if (EOF == kputsn_(tag->str, tag->len, &ks))
		return -1;
	}
	if (EOF == kputc('\n', &ks))
	    return -1;
    }

    for (k = kh_begin(hdr->h); k != kh_end(hdr->h); k++) {
	SAM_hdr_type *t1, *t2;

	if (!kh_exist(hdr->h, k))
	    continue;

	if (kh_key(hdr->h, k) == K("HD"))
	    continue;

	t1 = t2 = kh_val(hdr->h, k);
	do {
	    SAM_hdr_tag *tag;
	    char c[2];

	    if (EOF == kputc_('@', &ks))
		return -1;
	    c[0] = kh_key(hdr->h, k)>>8;
	    c[1] = kh_key(hdr->h, k)&0xff;
	    if (EOF == kputsn_(c, 2, &ks))
		return -1;
	    for (tag = t1->tag; tag; tag=tag->next) {
		if (EOF == kputc_('\t', &ks))
		    return -1;
		if (EOF == kputsn_(tag->str, tag->len, &ks))
		    return -1;
	    }
	    if (EOF == kputc('\n', &ks))
		return -1;
	    t1 = t1->next;
	} while (t1 != t2);
    }

    if (ks_str(&hdr->text))
	KS_FREE(&hdr->text);

    hdr->text = ks;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x42d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movsbl 0x1ff08(%rip), %esi     # 0x7dba3
shll $0x8, %esi
movsbl 0x1feff(%rip), %eax     # 0x7dba4
orl %eax, %esi
callq 0x5d690
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
je 0x5dd9d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
leaq 0x1fec2(%rip), %rdi     # 0x7dba2
leaq -0x28(%rbp), %rsi
callq 0x5d0e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5dd00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x5dd76
movl $0x9, %edi
leaq -0x28(%rbp), %rsi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5dd38
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x10(%rax), %esi
leaq -0x28(%rbp), %rdx
callq 0x5dfd0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5dd67
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
jmp 0x5dd69
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x5dd0c
movl $0xa, %edi
leaq -0x28(%rbp), %rsi
callq 0x5d120
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5dd9b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
jmp 0x5dd9d
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
je 0x5df6f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x2c(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x5dde5
jmp 0x5df61
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movsbl 0x1fda5(%rip), %ecx     # 0x7dba3
shll $0x8, %ecx
movsbl 0x1fd9c(%rip), %edx     # 0x7dba4
orl %edx, %ecx
cmpl %ecx, %eax
jne 0x5de13
jmp 0x5df61
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movl $0x40, %edi
leaq -0x28(%rbp), %rsi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5de53
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x8, %eax
movb %al, -0x5a(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
movb %al, -0x59(%rbp)
leaq -0x5a(%rbp), %rdi
movl $0x2, %esi
leaq -0x28(%rbp), %rdx
callq 0x5dfd0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5deae
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x5df24
movl $0x9, %edi
leaq -0x28(%rbp), %rsi
callq 0x5d070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5dee6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x58(%rbp), %rax
movl 0x10(%rax), %esi
leaq -0x28(%rbp), %rdx
callq 0x5dfd0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5df15
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
jmp 0x5df17
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x5deba
movl $0xa, %edi
leaq -0x28(%rbp), %rsi
callq 0x5d120
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x5df46
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5dfbe
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x5de2e
jmp 0x5df61
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x5dda4
movq -0x10(%rbp), %rdi
callq 0x5b9e0
cmpq $0x0, %rax
je 0x5df9c
jmp 0x5df80
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x5df98
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x46a0
jmp 0x5df9a
jmp 0x5df9c
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)