// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_io.c
// Function: cram_write_SAM_hdr
int cram_write_SAM_hdr(cram_fd *fd, sam_hdr_t *hdr) {
    size_t header_len;
    int blank_block = (CRAM_MAJOR_VERS(fd->version) >= 3);

    /* Write CRAM MAGIC if not yet written. */
    if (fd->file_def->major_version == 0) {
        fd->file_def->major_version = CRAM_MAJOR_VERS(fd->version);
        fd->file_def->minor_version = CRAM_MINOR_VERS(fd->version);
        if (0 != cram_write_file_def(fd, fd->file_def))
            return -1;
    }

    /* 1.0 requires an UNKNOWN read-group */
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
        if (!sam_hrecs_find_rg(hdr->hrecs, "UNKNOWN"))
            if (sam_hdr_add_line(hdr, "RG",
                            "ID", "UNKNOWN", "SM", "UNKNOWN", NULL))
                return -1;
    }

    if (-1 == refs_from_header(fd))
        return -1;
    if (-1 == refs2id(fd->refs, fd->header))
        return -1;

    /* Fix M5 strings */
    if (fd->refs && !fd->no_ref && fd->embed_ref <= 1) {
        int i;
        for (i = 0; i < hdr->hrecs->nref; i++) {
            sam_hrec_type_t *ty;
            char *ref;

            if (!(ty = sam_hrecs_find_type_id(hdr->hrecs, "SQ", "SN", hdr->hrecs->ref[i].name)))
                return -1;

            if (!sam_hrecs_find_key(ty, "M5", NULL)) {
                char unsigned buf[16];
                char buf2[33];
                hts_pos_t rlen;
                hts_md5_context *md5;

                if (!fd->refs ||
                    !fd->refs->ref_id ||
                    !fd->refs->ref_id[i]) {
                    return -1;
                }
                rlen = fd->refs->ref_id[i]->length;
                ref = cram_get_ref(fd, i, 1, rlen);
                if (NULL == ref) {
                    if (fd->embed_ref == -1) {
                        // auto embed-ref
                        hts_log_warning("No M5 tags present and could not "
                                        "find reference");
                        hts_log_warning("Enabling embed_ref=2 option");
                        hts_log_warning("NOTE: the CRAM file will be bigger "
                                        "than using an external reference");
                        pthread_mutex_lock(&fd->ref_lock);
                        // Best guess.  It may be unmapped data with broken
                        // headers, in which case this will get ignored.
                        fd->embed_ref = 2;
                        pthread_mutex_unlock(&fd->ref_lock);
                        break;
                    }
                    return -1;
                }
                rlen = fd->refs->ref_id[i]->length; /* In case it just loaded */
                if (!(md5 = hts_md5_init()))
                    return -1;
                if (HTS_POS_MAX <= ULONG_MAX) {
                    // Platforms with 64-bit unsigned long update in one go
                    hts_md5_update(md5, ref, rlen);
                } else {
                    // Those with 32-bit ulong (Windows) may have to loop
                    // over epic references
                    hts_pos_t pos = 0;
                    while (rlen - pos > ULONG_MAX) {
                        hts_md5_update(md5, ref + pos, ULONG_MAX);
                        pos += ULONG_MAX;
                    }
                    hts_md5_update(md5, ref + pos, (unsigned long)(rlen - pos));
                }
                hts_md5_final(buf, md5);
                hts_md5_destroy(md5);
                cram_ref_decr(fd->refs, i);

                hts_md5_hex(buf2, buf);
                fd->refs->ref_id[i]->validated_md5 = 1;
                if (sam_hdr_update_line(hdr, "SQ", "SN", hdr->hrecs->ref[i].name, "M5", buf2, NULL))
                    return -1;
            }

            if (fd->ref_fn) {
                char ref_fn[PATH_MAX];
                full_path(ref_fn, fd->ref_fn);
                if (sam_hdr_update_line(hdr, "SQ", "SN", hdr->hrecs->ref[i].name, "UR", ref_fn, NULL))
                    return -1;
            }
        }
    }

    /* Length */
    header_len = sam_hdr_length(hdr);
    if (header_len > INT32_MAX) {
        hts_log_error("Header is too long for CRAM format");
        return -1;
    }
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
        if (-1 == int32_encode(fd, header_len))
            return -1;

        /* Text data */
        if (header_len != hwrite(fd->fp, sam_hdr_str(hdr), header_len))
            return -1;
    } else {
        /* Create block(s) inside a container */
        cram_block *b = cram_new_block(FILE_HEADER, 0);
        cram_container *c = cram_new_container(0, 0);
        int padded_length;
        char *pads;
        int is_cram_3 = (CRAM_MAJOR_VERS(fd->version) >= 3);

        if (!b || !c) {
            if (b) cram_free_block(b);
            if (c) cram_free_container(c);
            return -1;
        }

        if (int32_put_blk(b, header_len) < 0)
            return -1;
        if (header_len)
            BLOCK_APPEND(b, sam_hdr_str(hdr), header_len);
        BLOCK_UPLEN(b);

        // Compress header block if V3.0 and above
        if (CRAM_MAJOR_VERS(fd->version) >= 3)
            if (cram_compress_block(fd, b, NULL, -1, -1) < 0)
                return -1;

        if (blank_block) {
            c->length = b->comp_size + 2 + 4*is_cram_3 +
                fd->vv.varint_size(b->content_id)   +
                fd->vv.varint_size(b->uncomp_size)  +
                fd->vv.varint_size(b->comp_size);

            c->num_blocks = 2;
            c->num_landmarks = 2;
            if (!(c->landmark = malloc(2*sizeof(*c->landmark)))) {
                cram_free_block(b);
                cram_free_container(c);
                return -1;
            }
            c->landmark[0] = 0;
            c->landmark[1] = c->length;

            // Plus extra storage for uncompressed secondary blank block
            padded_length = MIN(c->length*.5, 10000);
            c->length += padded_length + 2 + 4*is_cram_3 +
                fd->vv.varint_size(b->content_id) +
                fd->vv.varint_size(padded_length)*2;
        } else {
            // Pad the block instead.
            c->num_blocks = 1;
            c->num_landmarks = 1;
            if (!(c->landmark = malloc(sizeof(*c->landmark))))
                return -1;
            c->landmark[0] = 0;

            padded_length = MAX(c->length*1.5, 10000) - c->length;

            c->length = b->comp_size + padded_length +
                2 + 4*is_cram_3 +
                fd->vv.varint_size(b->content_id)   +
                fd->vv.varint_size(b->uncomp_size)  +
                fd->vv.varint_size(b->comp_size);

            if (NULL == (pads = calloc(1, padded_length))) {
                cram_free_block(b);
                cram_free_container(c);
                return -1;
            }
            BLOCK_APPEND(b, pads, padded_length);
            BLOCK_UPLEN(b);
            free(pads);
        }

        if (-1 == cram_write_container(fd, c)) {
            cram_free_block(b);
            cram_free_container(c);
            return -1;
        }

        if (-1 == cram_write_block(fd, b)) {
            cram_free_block(b);
            cram_free_container(c);
            return -1;
        }

        if (blank_block) {
            BLOCK_RESIZE(b, padded_length);
            memset(BLOCK_DATA(b), 0, padded_length);
            BLOCK_SIZE(b) = padded_length;
            BLOCK_UPLEN(b);
            b->method = RAW;
            if (-1 == cram_write_block(fd, b)) {
                cram_free_block(b);
                cram_free_container(c);
                return -1;
            }
        }

        cram_free_block(b);
        cram_free_container(c);
    }

    if (0 != hflush(fd->fp))
        return -1;

    RP("=== Finishing saving header ===\n");

    return 0;

 block_err:
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rsi, %r15
movq %rdi, %rbx
movl 0xc(%rdi), %eax
movl %eax, %r14d
sarl $0x8, %r14d
movq 0x10(%rdi), %rsi
cmpb $0x0, 0x4(%rsi)
pushq $-0x1
popq %r12
je 0x4410f
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0x100, %eax            # imm = 0x100
jne 0x43de8
movq 0x38(%r15), %rdi
leaq 0x37440(%rip), %rsi     # 0x7b21a
callq 0x6d6fc
testq %rax, %rax
je 0x4412e
movq %rbx, %rdi
callq 0x40af8
cmpl $-0x1, %eax
je 0x4420b
movq 0x18(%rbx), %rsi
movq 0x50(%rbx), %rdi
callq 0x40917
cmpl $-0x1, %eax
je 0x4420b
cmpq $0x0, 0x50(%rbx)
je 0x441e3
cmpl $0x0, 0x224(%rbx)
jne 0x441e3
cmpl $0x1, 0x220(%rbx)
jg 0x441e3
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, 0x10(%rsp)
movq 0x38(%r15), %rdi
movslq 0x28(%rdi), %rax
cmpq %rax, %r13
jge 0x441e3
movq 0x30(%rdi), %rax
movq (%rax,%rbp), %rcx
leaq 0x37514(%rip), %rsi     # 0x7b372
leaq 0x37510(%rip), %rdx     # 0x7b375
callq 0x6a8fd
testq %rax, %rax
je 0x4420b
movq %rax, %rdi
leaq 0x374fb(%rip), %rsi     # 0x7b378
xorl %edx, %edx
callq 0x6b93e
testq %rax, %rax
movq %rbp, 0x8(%rsp)
jne 0x43f9c
movq 0x50(%rbx), %rax
testq %rax, %rax
je 0x4457e
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x4457e
movq (%rax,%r13,8), %rax
testq %rax, %rax
je 0x4457e
movq 0x10(%rax), %rcx
movq %rbx, %rdi
movl %r13d, %esi
pushq $0x1
popq %rdx
callq 0x41216
testq %rax, %rax
je 0x44169
movq %rax, %rbp
movq 0x50(%rbx), %rax
movq 0x10(%rax), %rax
movq (%rax,%r13,8), %rax
movq 0x10(%rax), %r12
callq 0x6ecda
testq %rax, %rax
je 0x4457e
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x6e2bd
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6ebc0
movq %r15, %rdi
callq 0x6ed04
movq 0x50(%rbx), %rdi
movl %r13d, %esi
callq 0x40d68
movq %r13, %r12
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6ed09
movq 0x50(%rbx), %rax
movq 0x10(%rax), %rax
movq (%rax,%r12,8), %rax
movl $0x1, 0x4c(%rax)
movq 0x10(%rsp), %r15
movq 0x38(%r15), %rax
movq 0x30(%rax), %rax
movq 0x8(%rsp), %rbp
movq (%rax,%rbp), %rcx
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x373fd(%rip), %rsi     # 0x7b372
leaq 0x373f9(%rip), %rdx     # 0x7b375
leaq 0x373f5(%rip), %r8      # 0x7b378
movq %r13, %r9
movq %r12, %r13
xorl %eax, %eax
callq 0x6b200
testl %eax, %eax
pushq $-0x1
popq %r12
jne 0x4420b
movq 0x80(%rbx), %rbp
testq %rbp, %rbp
je 0x440db
movq %r13, 0x18(%rsp)
movq %rbp, %rdi
callq 0x41d0
movq %rax, %r15
movq %rbp, %rdi
callq 0xe4f2
testl %eax, %eax
je 0x44001
cmpq $0x1001, %r15           # imm = 0x1001
jb 0x44074
pushq $0x1
popq %rdi
leaq 0x3964a(%rip), %rsi     # 0x7d629
leaq 0x3964d(%rip), %rdx     # 0x7d633
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x110c6
movq 0x10(%rsp), %r15
leaq 0x30(%rsp), %r13
jmp 0x44096
movb (%rbp), %al
cmpb $0x2f, %al
leaq 0x30(%rsp), %r13
je 0x4406d
cmpq $0x4, %r15
jb 0x4403a
movsbl %al, %edi
callq 0x466d1
leaq 0x30(%rsp), %r13
addb $-0x41, %al
cmpb $0x19, %al
ja 0x4403a
cmpb $0x3a, 0x1(%rbp)
jne 0x4403a
movzbl 0x2(%rbp), %eax
cmpl $0x5c, %eax
je 0x4406d
cmpl $0x2f, %eax
je 0x4406d
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdi
callq 0x4240
testq %rax, %rax
je 0x4406d
movq %r13, %rdi
callq 0x41d0
movq %rax, %r15
movq %rbp, %rdi
callq 0x41d0
addq %r15, %rax
incq %rax
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x440ec
movl $0xfff, %edx            # imm = 0xFFF
jmp 0x4407e
movl $0xfff, %edx            # imm = 0xFFF
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4250
movb $0x0, 0x102f(%rsp)
movq 0x10(%rsp), %r15
movq 0x38(%r15), %rax
movq 0x30(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx), %rcx
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x372bc(%rip), %rsi     # 0x7b372
leaq 0x372b8(%rip), %rdx     # 0x7b375
leaq 0x39008(%rip), %r8      # 0x7d0cc
movq %r13, %r9
xorl %eax, %eax
callq 0x6b200
testl %eax, %eax
movq 0x18(%rsp), %r13
jne 0x4420b
incq %r13
movq 0x8(%rsp), %rbp
addq $0x18, %rbp
jmp 0x43e3e
leaq (%rsp,%r15), %rdi
addq $0x30, %rdi
movl $0x1000, %esi           # imm = 0x1000
subq %r15, %rsi
leaq 0x3eab2(%rip), %rdx     # 0x82bb5
movq %rbp, %rcx
xorl %eax, %eax
callq 0x4800
jmp 0x44091
movb %r14b, 0x4(%rsi)
movb %al, 0x5(%rsi)
movq %rbx, %rdi
callq 0x43a22
testl %eax, %eax
jne 0x4420b
movl 0xc(%rbx), %eax
jmp 0x43dc3
andq $0x0, (%rsp)
leaq 0x35f19(%rip), %rsi     # 0x7a053
leaq 0x35f15(%rip), %rdx     # 0x7a056
leaq 0x36fe8(%rip), %r8      # 0x7b130
leaq 0x370cb(%rip), %rcx     # 0x7b21a
movq %r15, %rdi
movq %rcx, %r9
xorl %eax, %eax
callq 0x6a1fe
testl %eax, %eax
jne 0x4420b
jmp 0x43de8
cmpl $-0x1, 0x220(%rbx)
jne 0x4457e
leaq 0x38ef0(%rip), %r15     # 0x7d06d
leaq 0x38efc(%rip), %rdx     # 0x7d080
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x38f16(%rip), %rdx     # 0x7d0b0
movl %ebp, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x378ee(%rip), %rdx     # 0x7ba9b
movl %ebp, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x88f0(%rbx), %r15
movq %r15, %rdi
callq 0x4750
movl $0x2, 0x220(%rbx)
movq %r15, %rdi
callq 0x4340
pushq $-0x1
popq %r12
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x69cc1
testq $-0x80000000, %rax      # imm = 0x80000000
je 0x44222
leaq 0x38e73(%rip), %rsi     # 0x7d06d
leaq 0x38ece(%rip), %rdx     # 0x7d0cf
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movl %r12d, %ebp
movl %ebp, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x44275
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3f8d6
cmpl $-0x1, %eax
je 0x4420b
movq %r15, %rdi
movq (%rbx), %r15
callq 0x69dee
movq %r15, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x3e79d
movl %r12d, %ebp
cmpq %rax, %r13
jne 0x4420e
movq (%rbx), %rdi
callq 0xd64f
xorl %ebp, %ebp
negl %eax
sbbl %ebp, %ebp
jmp 0x4420e
movq %r15, %rbp
xorl %edi, %edi
xorl %esi, %esi
callq 0x3f426
movq %rax, %r12
xorl %edi, %edi
xorl %esi, %esi
callq 0x42222
movq %rax, %r15
testq %r12, %r12
setne %al
testq %r15, %r15
setne %cl
testb %cl, %al
je 0x443e9
movl 0xc(%rbx), %eax
movl %eax, 0x8(%rsp)
movq %r12, %rdi
movl %r13d, %esi
callq 0x3f225
testl %eax, %eax
js 0x4457e
testq %r13, %r13
je 0x442e1
movq %rbp, %rdi
callq 0x69dee
movq %r12, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x3efc2
testl %eax, %eax
js 0x4457e
movl 0x30(%r12), %eax
movl %eax, 0x14(%r12)
movl %eax, 0x10(%r12)
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
jl 0x44316
pushq $-0x1
popq %rbp
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl %ebp, %ecx
movl %ebp, %r8d
callq 0x4078d
testl %eax, %eax
js 0x4420e
cmpl $0x3, %r14d
jl 0x44404
xorl %eax, %eax
cmpl $0x300, 0x8(%rsp)       # imm = 0x300
setge %al
movslq 0xc(%r12), %rdi
leal 0x2(,%rax,4), %eax
movl 0x10(%r12), %r13d
movl %eax, 0x18(%rsp)
addl %eax, %r13d
callq *0x8a08(%rbx)
movl %eax, 0x8(%rsp)
movslq 0x14(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, %ebp
movslq 0x10(%r12), %rdi
callq *0x8a08(%rbx)
addl 0x8(%rsp), %ebp
addl %eax, %ebp
addl %r13d, %ebp
movl %ebp, (%r15)
movabsq $0x200000002, %rax      # imm = 0x200000002
movq %rax, 0x2c(%r15)
pushq $0x8
popq %rdi
callq 0x4680
movq %rax, 0x38(%r15)
testq %rax, %rax
je 0x4456e
andl $0x0, (%rax)
cvtsi2sd %ebp, %xmm0
mulsd 0x30c7b(%rip), %xmm0    # 0x75020
minsd 0x32503(%rip), %xmm0    # 0x768b0
movl %ebp, 0x4(%rax)
cvttsd2si %xmm0, %r13d
movslq 0xc(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, %ebp
movl %r13d, 0x8(%rsp)
movslq %r13d, %r13
movq %r13, %rdi
callq *0x8a08(%rbx)
movl 0x18(%rsp), %ecx
addl %ebp, %ecx
addl %ecx, %r13d
leal (%r13,%rax,2), %eax
addl %eax, (%r15)
jmp 0x444ec
testq %r12, %r12
je 0x443f6
movq %r12, %rdi
callq 0x3f8fc
testq %r15, %r15
jne 0x44576
jmp 0x4457e
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x2c(%r15)
pushq $0x4
popq %rdi
callq 0x4680
movq %rax, 0x38(%r15)
testq %rax, %rax
je 0x4457e
xorl %ebp, %ebp
cmpl $0x300, 0x8(%rsp)       # imm = 0x300
setge %bpl
andl $0x0, (%rax)
cvtsi2sdl (%r15), %xmm0
movsd 0x3740b(%rip), %xmm1    # 0x7b850
mulsd %xmm0, %xmm1
maxsd 0x3245f(%rip), %xmm1    # 0x768b0
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, 0x8(%rsp)
movslq 0xc(%r12), %rdi
shll $0x2, %ebp
addl 0x10(%r12), %ebp
callq *0x8a08(%rbx)
movl %eax, %r13d
movslq 0x14(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, 0x18(%rsp)
movslq 0x10(%r12), %rdi
callq *0x8a08(%rbx)
addl %ebp, %r13d
addl 0x18(%rsp), %r13d
movl 0x8(%rsp), %ecx
addl %ecx, %r13d
addl %r13d, %eax
addl $0x2, %eax
movl %eax, (%r15)
movslq %ecx, %r13
pushq $0x1
popq %rdi
movq %r13, %rsi
callq 0x4300
testq %rax, %rax
je 0x4456e
movq %rax, %rbp
movq %r12, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x3efc2
testl %eax, %eax
js 0x4457e
movl 0x30(%r12), %eax
movl %eax, 0x14(%r12)
movl %eax, 0x10(%r12)
movq %rbp, %rdi
callq 0x45c0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x42e04
cmpl $-0x1, %eax
je 0x4456e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3f6bc
cmpl $-0x1, %eax
je 0x4456e
cmpl $0x3, %r14d
jl 0x44559
movslq 0x8(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x44586
testl %eax, %eax
js 0x4457e
movq 0x20(%r12), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4260
movq %r14, 0x30(%r12)
movl %r14d, 0x14(%r12)
movl %r14d, 0x10(%r12)
andl $0x0, (%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3f6bc
cmpl $-0x1, %eax
je 0x4456e
movq %r12, %rdi
callq 0x3f8fc
movq %r15, %rdi
callq 0x4237d
jmp 0x44265
movq %r12, %rdi
callq 0x3f8fc
movq %r15, %rdi
callq 0x4237d
pushq $-0x1
popq %rbp
jmp 0x4420e