// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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 $0x38, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x18(%rdi), %r14
movq %r14, %rdi
movl $0x4844, %esi           # imm = 0x4844
callq 0x58fd8
pushq $-0x1
popq %rbp
cmpl (%r14), %eax
jne 0x5941d
xorl %r14d, %r14d
pushq $0x40
popq %rdi
leaq 0x10(%rsp), %r15
movq 0x18(%rbx), %rax
cmpl (%rax), %r14d
je 0x594a5
movq 0x10(%rax), %rcx
movl %r14d, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
leal (%r14,%r14), %ecx
shrl %cl, %edx
testb $0x3, %dl
jne 0x5934b
movq 0x18(%rax), %rcx
movl %r14d, %r13d
cmpl $0x4844, (%rcx,%r13,4)  # imm = 0x4844
jne 0x59350
incl %r14d
jmp 0x59315
movq 0x20(%rax), %rax
movq (%rax,%r13,8), %rax
movq %rax, 0x30(%rsp)
movq %rax, 0x8(%rsp)
movl %ebp, %r12d
movq %rbx, %rbp
movq %r15, %rsi
callq 0x58c6d
cmpl $-0x1, %eax
je 0x5947c
movq 0x18(%rbp), %rax
movq 0x18(%rax), %rax
movzwl (%rax,%r13,4), %eax
rolw $0x8, %ax
movw %ax, 0x6(%rsp)
leaq 0x6(%rsp), %rdi
pushq $0x2
popq %rsi
movq %r15, %rdx
callq 0x594c3
cmpl $-0x1, %eax
je 0x5947c
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x593e6
pushq $0x9
popq %rdi
movq %r15, %rsi
callq 0x58c6d
cmpl $-0x1, %eax
je 0x5947c
movq 0x8(%rbx), %rdi
movl 0x10(%rbx), %esi
movq %r15, %rdx
callq 0x594c3
cmpl $-0x1, %eax
jne 0x593b1
jmp 0x5947c
movq %r15, %rdi
callq 0x58d12
cmpl $-0x1, %eax
je 0x5947c
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq 0x30(%rsp), %rax
movq %rbp, %rbx
movl %r12d, %ebp
pushq $0x40
popq %rdi
jne 0x59362
jmp 0x5934b
movq 0x20(%r14), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %r12
leaq 0x2f4a6(%rip), %rdi     # 0x888d4
leaq 0x10(%rsp), %rsi
callq 0x58cef
cmpl $-0x1, %eax
je 0x5947f
addq $0x10, %r12
pushq $0x9
popq %r14
leaq 0x10(%rsp), %r15
movq (%r12), %r12
testq %r12, %r12
je 0x59490
movl %r14d, %edi
movq %r15, %rsi
callq 0x58c6d
cmpl $-0x1, %eax
je 0x5947f
movq 0x8(%r12), %rdi
movl 0x10(%r12), %esi
movq %r15, %rdx
callq 0x594c3
cmpl $-0x1, %eax
jne 0x5944a
jmp 0x5947f
movl %r12d, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdi
callq 0x58d12
cmpl $-0x1, %eax
jne 0x5930a
jmp 0x5947f
movq 0x10(%rbx), %rdi
callq 0x68a0
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rbx)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rbx)
xorl %ebp, %ebp
jmp 0x5947f