// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r12
movq 0x18(%rdi), %rax
movl (%rax), %edx
testl %edx, %edx
movq %rdi, 0x8(%rsp)
je 0x401c0
leal -0x1(%rdx), %edi
movl %edi, %r8d
andl $0x4844, %r8d           # imm = 0x4844
movq 0x10(%rax), %r9
movl $0x1, %r10d
movl %r8d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r9,%rcx,4), %r11d
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %r11d, %ebx
shrl %cl, %ebx
testb $0x2, %bl
jne 0x401b4
testb $0x1, %bl
jne 0x401a3
movq 0x18(%rax), %rbx
movl %esi, %r14d
cmpl $0x4844, (%rbx,%r14,4)  # imm = 0x4844
movq 0x8(%rsp), %r12
je 0x401b4
addl %r10d, %esi
andl %edi, %esi
incl %r10d
cmpl %r8d, %esi
jne 0x4016f
movl %edx, %esi
jmp 0x401c2
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %esi
jmp 0x401c2
xorl %esi, %esi
xorl %r8d, %r8d
movl $0x0, %ebp
movl $0x0, %r13d
cmpl %edx, %esi
jne 0x405bd
movq 0x18(%r12), %rax
cmpl $0x0, (%rax)
je 0x40593
xorl %esi, %esi
movq 0x10(%rax), %rcx
movl %esi, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
leal (%rsi,%rsi), %ecx
shrl %cl, %edx
movl $0x7, %ecx
testb $0x3, %dl
movq %rsi, 0x30(%rsp)
jne 0x40572
movq 0x18(%rax), %rdx
movl %esi, %esi
cmpl $0x4844, (%rdx,%rsi,4)  # imm = 0x4844
je 0x40572
movq 0x20(%rax), %rax
movq (%rax,%rsi,8), %rax
movq %rax, 0x38(%rsp)
movq %rax, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
leaq 0x1(%r13), %rbx
cmpq %rbx, %rbp
jae 0x4029d
movq %r13, %rax
shrq %rax
orq %r13, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r15
shrq $0x20, %r15
orq %rax, %r15
incq %r15
movq %r8, %rdi
movq %r15, %rsi
movq %r8, %r14
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
jne 0x402a0
movb $0x1, %cl
movq %r13, %rbx
movq %r14, %r8
jmp 0x402aa
movq %rbp, %r15
movb $0x40, (%r8,%r13)
xorl %ecx, %ecx
movq %r15, %rbp
movq 0x28(%rsp), %rdx
testb %cl, %cl
je 0x402bd
xorl %eax, %eax
movq %rbx, %r13
jmp 0x40555
movq 0x18(%r12), %rax
movq 0x18(%rax), %rax
movq %rbp, %r15
movl (%rax,%rdx,4), %ebp
leaq 0x2(%rbx), %r13
cmpq %r13, %r15
jae 0x40336
movq %r15, 0x10(%rsp)
leaq 0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %r15
shrq $0x20, %r15
orq %rcx, %r15
incq %r15
movq %r8, %rdi
movq %r15, %rsi
movq %r8, %r14
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
je 0x4049c
movl %ebp, %ecx
shrl $0x8, %ecx
movb %cl, (%r8,%rbx)
movb %bpl, 0x1(%r8,%rbx)
xorl %ecx, %ecx
movq %r15, %rbp
testb %cl, %cl
je 0x40354
xorl %eax, %eax
jmp 0x40555
movq 0x18(%rsp), %rax
leaq 0x10(%rax), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x404ae
leaq 0x1(%r13), %r15
cmpq %r15, %rbp
jae 0x403d0
movq %r13, %rax
shrq %rax
orq %r13, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r12
shrq $0x20, %r12
orq %rax, %r12
incq %r12
movq %r8, %rdi
movq %r12, %rsi
movq %r8, %rbx
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
jne 0x403d3
movb $0x1, %cl
movq %r13, %r15
movq %rbx, %r8
jmp 0x403dd
movq %rbp, %r12
movb $0x9, (%r8,%r13)
xorl %ecx, %ecx
movq %r12, %rbp
testb %cl, %cl
jne 0x4051b
movq 0x8(%r14), %r13
movslq 0x10(%r14), %r12
leaq (%r15,%r12), %rax
cmpq %rax, %rbp
movq %rax, 0x40(%rsp)
jae 0x4046a
movq %rbp, 0x10(%rsp)
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rbp
shrq $0x20, %rbp
orq %rcx, %rbp
incq %rbp
movq %r8, %rdi
movq %rbp, %rsi
movq %r8, 0x20(%rsp)
callq 0x48d0
movq %rax, %rbx
testq %rax, %rax
jne 0x4046d
movb $0x1, %al
movq %r15, %r13
movq 0x10(%rsp), %rbp
movq 0x20(%rsp), %r8
jmp 0x4048d
movq %r8, %rbx
addq %rbx, %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4420
cmpl $-0x1, %r12d
sete %al
movq %rbx, %r8
movq 0x40(%rsp), %r13
testb %al, %al
je 0x4035d
xorl %eax, %eax
jmp 0x40520
movb $0x1, %cl
movq %rbx, %r13
movq 0x10(%rsp), %rbp
movq %r14, %r8
jmp 0x40349
leaq 0x2(%r13), %rax
cmpq %rax, %rbp
jae 0x40527
leaq 0x1(%r13), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %r15
shrq $0x20, %r15
orq %rcx, %r15
incq %r15
movq %r8, %rdi
movq %r15, %rsi
movq %r8, %rbx
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
movq 0x8(%rsp), %r12
jne 0x4052f
movb $0x1, %cl
movq %rbx, %r8
jmp 0x4053e
xorl %eax, %eax
movq %r15, %r13
movq 0x8(%rsp), %r12
jmp 0x40555
movq %rbp, %r15
movq 0x8(%rsp), %r12
movw $0xa, (%r8,%r13)
incq %r13
xorl %ecx, %ecx
movq %r15, %rbp
testb %cl, %cl
jne 0x4034d
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %al
testb %al, %al
je 0x4056d
movq 0x18(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x40236
xorl %ecx, %ecx
jmp 0x40572
movl $0x1, %ecx
cmpl $0x7, %ecx
je 0x4057f
testl %ecx, %ecx
jne 0x40737
movq 0x30(%rsp), %rsi
incl %esi
movq 0x18(%r12), %rax
cmpl (%rax), %esi
jne 0x401e8
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x405a8
movq %r8, %rbx
callq 0x4610
movq %rbx, %r8
movq %r13, (%r12)
movq %rbp, 0x8(%r12)
movq %r8, 0x10(%r12)
xorl %eax, %eax
jmp 0x4073c
movq 0x20(%rax), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %r13
movl $0x8, %ebp
movl $0x8, %edi
callq 0x4700
testq %rax, %rax
je 0x405ec
movl $0x444840, (%rax)       # imm = 0x444840
movl $0x3, %r14d
movq %rax, %rbx
jmp 0x405f3
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r14d, %r14d
testq %rax, %rax
je 0x40737
addq $0x10, %r13
movq (%r13), %r13
testq %r13, %r13
je 0x4074b
leaq 0x1(%r14), %r15
cmpq %r15, %rbp
jae 0x4066e
movq %r14, %rax
shrq %rax
orq %r14, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r12
shrq $0x20, %r12
orq %rax, %r12
incq %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x48d0
testq %rax, %rax
jne 0x40674
movb $0x1, %cl
movq %r14, %r15
movq %rbx, %rdi
jmp 0x40681
movq %rbx, %rax
movq %rbp, %r12
movb $0x9, (%rax,%r14)
xorl %ecx, %ecx
movq %rax, %rdi
movq %r12, %rbp
testb %cl, %cl
jne 0x40737
movq 0x8(%r13), %rsi
movslq 0x10(%r13), %r12
leaq (%r15,%r12), %rax
cmpq %rax, %rbp
jae 0x4070f
movq %rbp, 0x10(%rsp)
movq %rax, %r14
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rbp
shrq $0x20, %rbp
orq %rcx, %rbp
incq %rbp
movq %rsi, %rbx
movq %rbp, %rsi
movq %rdi, 0x20(%rsp)
callq 0x48d0
movq %rbx, %rsi
movq %rax, %rbx
testq %rax, %rax
jne 0x40715
movb $0x1, %al
movq %r15, %r14
movq 0x10(%rsp), %rbp
movq 0x20(%rsp), %rbx
jmp 0x4072a
movq %rax, %r14
movq %rdi, %rbx
addq %rbx, %r15
movq %r15, %rdi
movq %r12, %rdx
callq 0x4420
cmpl $-0x1, %r12d
sete %al
movq 0x8(%rsp), %r12
testb %al, %al
je 0x40600
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r13
leaq 0x2(%r14), %rax
cmpq %rax, %rbp
jae 0x407b3
leaq 0x1(%r13), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %r15
shrq $0x20, %r15
orq %rcx, %r15
incq %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
jne 0x407b9
xorl %ecx, %ecx
movq %rbx, %r8
jmp 0x407c8
movq %rbx, %r8
movq %rbp, %r15
movw $0xa, (%r8,%r13)
incq %r13
movb $0x1, %cl
movq %r15, %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %cl, %cl
jne 0x401d8
jmp 0x4073c