// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %r12
movq 0x18(%rdi), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x3f2de
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 0x3f2d2
testb $0x1, %bl
jne 0x3f2ba
movq 0x18(%rax), %rbx
movl %esi, %r14d
cmpl $0x4844, (%rbx,%r14,4)  # imm = 0x4844
je 0x3f2d2
addl %r10d, %esi
andl %edi, %esi
incl %r10d
cmpl %r8d, %esi
jne 0x3f28b
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r8d, %r8d
jmp 0x3f2f1
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %esi
jmp 0x3f2e0
xorl %esi, %esi
xorl %r8d, %r8d
cmpl %edx, %esi
jne 0x3f5da
xorl %r15d, %r15d
xorl %r13d, %r13d
testl %edx, %edx
je 0x3f711
xorl %ebx, %ebx
movq 0x10(%rax), %rcx
movl %ebx, %esi
shrl $0x4, %esi
movl (%rcx,%rsi,4), %esi
leal (%rbx,%rbx), %ecx
shrl %cl, %esi
testb $0x3, %sil
jne 0x3f321
movq 0x18(%rax), %rcx
movl %ebx, %ebp
cmpl $0x4844, (%rcx,%rbp,4)  # imm = 0x4844
jne 0x3f32c
incl %ebx
cmpl %edx, %ebx
jne 0x3f2fb
jmp 0x3f711
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rax
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq %rbx, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
leaq 0x1(%r13), %r14
cmpq %r14, %r15
jae 0x3f3aa
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
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
je 0x3f70a
movb $0x40, (%r8,%r13)
movq 0x18(%r12), %rax
movq 0x18(%rax), %rax
movl (%rax,%rbp,4), %r12d
leaq 0x3(%r13), %rbp
cmpq %rbp, %r15
jae 0x3f41e
addq $0x2, %r13
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
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
je 0x3f70a
movl %r12d, %eax
shrl $0x8, %eax
movb %al, (%r8,%r14)
movq %r8, %rbx
movb %r12b, 0x1(%r8,%r14)
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x3f536
leaq 0x1(%rbp), %r12
cmpq %r12, %r15
movq %rbx, %rdi
jae 0x3f4a3
movq %rbp, %rax
shrq %rax
orq %rbp, %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 %r15, %rsi
callq 0x48d0
movq %rax, %rdi
testq %rax, %rax
je 0x3f70a
movb $0x9, (%rdi,%rbp)
movq 0x8(%r14), %rsi
movslq 0x10(%r14), %r13
leaq (%r12,%r13), %rbx
cmpq %rbx, %r15
jae 0x3f513
addq %r13, %rbp
movq %rbp, %rax
shrq %rax
orq %rbp, %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 %rsi, %rbp
movq %r15, %rsi
callq 0x48d0
movq %rbp, %rsi
movq %rax, %rdi
testq %rax, %rax
je 0x3f70a
movq %rbx, %rbp
movq %rdi, %rbx
addq %rdi, %r12
movq %r12, %rdi
movq %r13, %rdx
callq 0x4420
cmpl $-0x1, %r13d
jne 0x3f439
jmp 0x3f70a
leaq 0x2(%rbp), %rax
leaq 0x1(%rbp), %r13
cmpq %rax, %r15
movq %rbx, %r8
jae 0x3f5a1
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
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
movq 0x10(%rsp), %rbx
jne 0x3f5a6
jmp 0x3f70a
movq 0x10(%rsp), %rbx
movw $0xa, (%r8,%rbp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq 0x18(%rsp), %rax
movq (%rsp), %r12
movq 0x20(%rsp), %rbp
jne 0x3f34c
movq 0x18(%r12), %rax
movl (%rax), %edx
jmp 0x3f321
movq %r12, (%rsp)
movq 0x20(%rax), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rbp
movl $0x8, %r15d
movl $0x8, %edi
callq 0x4700
testq %rax, %rax
je 0x3f70a
movq %rax, %rbx
movl $0x444840, (%rax)       # imm = 0x444840
addq $0x10, %rbp
movl $0x3, %r14d
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x3f745
leaq 0x1(%r14), %r12
cmpq %r12, %r15
movq %rbx, %rdi
jae 0x3f67f
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, %r15
shrq $0x20, %r15
orq %rax, %r15
incq %r15
movq %r15, %rsi
callq 0x48d0
movq %rax, %rdi
testq %rax, %rax
je 0x3f70a
movb $0x9, (%rdi,%r14)
movq 0x8(%rbp), %rsi
movslq 0x10(%rbp), %r13
leaq (%r12,%r13), %rbx
cmpq %rbx, %r15
jae 0x3f6ec
addq %r13, %r14
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, %r15
shrq $0x20, %r15
orq %rax, %r15
incq %r15
movq %rsi, %r14
movq %r15, %rsi
callq 0x48d0
movq %r14, %rsi
movq %rax, %rdi
testq %rax, %rax
je 0x3f70a
movq %rbx, %r14
movq %rdi, %rbx
addq %rdi, %r12
movq %r12, %rdi
movq %r13, %rdx
callq 0x4420
cmpl $-0x1, %r13d
jne 0x3f614
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3f736
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x3f726
movq %r8, %rbx
callq 0x4610
movq %rbx, %r8
movq %r13, (%r12)
movq %r15, 0x8(%r12)
movq %r8, 0x10(%r12)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2(%r14), %rax
leaq 0x1(%r14), %r13
cmpq %rax, %r15
movq %rbx, %r8
jae 0x3f7b1
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
callq 0x48d0
movq %rax, %r8
testq %rax, %rax
movq (%rsp), %r12
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jne 0x3f7b5
jmp 0x3f736
movq (%rsp), %r12
movw $0xa, (%r8,%r14)
movq 0x18(%r12), %rax
movl (%rax), %edx
jmp 0x3f2f1