// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %r15
movq 0x18(%rdi), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x635bf
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 0x635b3
testb $0x1, %bl
jne 0x6359b
movq 0x18(%rax), %rbx
movl %esi, %r14d
cmpl $0x4844, (%rbx,%r14,4)  # imm = 0x4844
je 0x635b3
addl %r10d, %esi
andl %edi, %esi
incl %r10d
cmpl %r8d, %esi
jne 0x6356c
xorl %r12d, %r12d
xorl %r9d, %r9d
xorl %r8d, %r8d
jmp 0x635d2
shrl %cl, %r11d
testb $0x3, %r11b
cmovnel %edx, %esi
jmp 0x635c1
xorl %esi, %esi
xorl %r8d, %r8d
cmpl %edx, %esi
jne 0x63897
xorl %r12d, %r12d
xorl %r9d, %r9d
testl %edx, %edx
je 0x639cb
xorl %r14d, %r14d
movq 0x10(%rax), %rcx
movl %r14d, %esi
shrl $0x4, %esi
movl (%rcx,%rsi,4), %esi
leal (%r14,%r14), %ecx
shrl %cl, %esi
testb $0x3, %sil
jne 0x63606
movq 0x18(%rax), %rcx
movl %r14d, %ebp
cmpl $0x4844, (%rcx,%rbp,4)  # imm = 0x4844
jne 0x63613
incl %r14d
cmpl %edx, %r14d
jne 0x635dd
jmp 0x639cb
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %r15, (%rsp)
movq %r14, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
leaq 0x1(%r9), %r13
cmpq %r12, %r13
jbe 0x6368d
movq %r9, %rax
shrq %rax
orq %r9, %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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r8, %rdi
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbx, %r9
movq %rax, %r8
testq %rax, %rax
je 0x639c4
movb $0x40, (%r8,%r9)
movq 0x18(%r15), %rax
movq 0x18(%rax), %rax
movl (%rax,%rbp,4), %r14d
leaq 0x3(%r9), %rbp
cmpq %r12, %rbp
jbe 0x636f6
addq $0x2, %r9
movq %r9, %rax
shrq %rax
orq %r9, %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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r8, %rdi
movq %r12, %rsi
callq 0x6c80
movq %rax, %r8
testq %rax, %rax
je 0x639c4
movl %r14d, %eax
shrl $0x8, %eax
movb %al, (%r8,%r13)
movq %r8, %rbx
movb %r14b, 0x1(%r8,%r13)
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %r14
movq (%r14), %r14
leaq 0x1(%rbp), %r9
testq %r14, %r14
je 0x63806
cmpq %r12, %r9
movq %rbx, %rdi
jbe 0x63777
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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbx, %r9
movq %rax, %rdi
testq %rax, %rax
je 0x639c4
movb $0x9, (%rdi,%rbp)
movq 0x8(%r14), %rsi
movslq 0x10(%r14), %r13
leaq (%r9,%r13), %r15
cmpq %r12, %r15
jbe 0x637e3
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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %rsi, %rbp
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbp, %rsi
movq %rbx, %r9
movq %rax, %rdi
testq %rax, %rax
je 0x639c4
movq %rdi, %rbx
addq %rdi, %r9
movq %r9, %rdi
movq %r13, %rdx
callq 0x6570
movq %r15, %rbp
cmpl $-0x1, %r13d
jne 0x63711
jmp 0x639c4
cmpq %r12, %r9
movq %rbx, %r8
jb 0x6385f
movq %r9, %rax
shrq %rax
orq %r9, %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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r8, %rdi
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbx, %r9
movq %rax, %r8
testq %rax, %rax
je 0x639c4
movw $0xa, (%r8,%rbp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq 0x10(%rsp), %rax
movq (%rsp), %r15
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rbp
jne 0x63633
movq 0x18(%r15), %rax
movl (%rax), %edx
jmp 0x63606
movq %r15, (%rsp)
movq 0x20(%rax), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rbp
movl $0x8, %r12d
movl $0x8, %edi
callq 0x69d0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x639f2
movq %rax, %rbx
movl $0x444840, (%rax)       # imm = 0x444840
addq $0x10, %rbp
movl $0x3, %r14d
movq (%rbp), %rbp
leaq 0x1(%r14), %r9
testq %rbp, %rbp
je 0x63a03
cmpq %r12, %r9
movq %rbx, %rdi
jbe 0x6393d
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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbx, %r9
movq %rax, %rdi
testq %rax, %rax
je 0x639c4
movb $0x9, (%rdi,%r14)
movq 0x8(%rbp), %rsi
movslq 0x10(%rbp), %r13
leaq (%r9,%r13), %r15
cmpq %r12, %r15
jbe 0x639a6
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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %rsi, %r14
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %r14, %rsi
movq %rbx, %r9
movq %rax, %rdi
testq %rax, %rax
je 0x639c4
movq %rdi, %rbx
addq %rdi, %r9
movq %r9, %rdi
movq %r13, %rdx
callq 0x6570
movq %r15, %r14
cmpl $-0x1, %r13d
jne 0x638d6
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x639f2
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x639e5
movq %r8, %rbx
movq %r9, %r14
callq 0x68a0
movq %r14, %r9
movq %rbx, %r8
movq %r9, (%r15)
movq %r12, 0x8(%r15)
movq %r8, 0x10(%r15)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r12, %r9
movq %rbx, %r8
jae 0x63a11
movq (%rsp), %r15
jmp 0x63a67
movq %r9, %rax
shrq %rax
orq %r9, %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, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r8, %rdi
movq %r12, %rsi
movq %r9, %rbx
callq 0x6c80
movq %rbx, %r9
movq %rax, %r8
testq %rax, %rax
movq (%rsp), %r15
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
je 0x639f2
movw $0xa, (%r8,%r14)
movq 0x18(%r15), %rax
movl (%rax), %edx
jmp 0x635d2