// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/htslib/sam.c
// Function: sam_hdr_write
int sam_hdr_write(htsFile *fp, const bam_hdr_t *h)
{
    switch (fp->format.format) {
    case binary_format:
        fp->format.category = sequence_data;
        fp->format.format = bam;
        /* fall-through */
    case bam:
        bam_hdr_write(fp->fp.bgzf, h);
        break;

    case cram: {
        cram_fd *fd = fp->fp.cram;
        SAM_hdr *hdr = bam_header_to_cram((bam_hdr_t *)h);
        if (! hdr) return -1;
        if (cram_set_header(fd, hdr) < 0) return -1;
        if (fp->fn_aux)
            cram_load_reference(fd, fp->fn_aux);
        if (cram_write_SAM_hdr(fd, fd->header) < 0) return -1;
        }
        break;

    case text_format:
        fp->format.category = sequence_data;
        fp->format.format = sam;
        /* fall-through */
    case sam: {
        char *p;
        hputs(h->text, fp->fp.hfile);
        p = strstr(h->text, "@SQ\t"); // FIXME: we need a loop to make sure "@SQ\t" does not match something unwanted!!!
        if (p == 0) {
            int i;
            for (i = 0; i < h->n_targets; ++i) {
                fp->line.l = 0;
                kputsn("@SQ\tSN:", 7, &fp->line); kputs(h->target_name[i], &fp->line);
                kputsn("\tLN:", 4, &fp->line); kputw(h->target_len[i], &fp->line); kputc('\n', &fp->line);
                if ( hwrite(fp->fp.hfile, fp->line.s, fp->line.l) != fp->line.l ) return -1;
            }
        }
        if ( hflush(fp->fp.hfile) != 0 ) return -1;
        }
        break;

    default:
        abort();
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x44(%rdi), %eax
decl %eax
cmpl $0x5, %eax
ja 0x62a69
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x268c6(%rip), %rcx     # 0x89184
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x61730
jmp 0x629cd
movq 0x38(%rbx), %r15
movq %r14, %rdi
callq 0x758a2
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x629cf
movq %r15, %rdi
movq %rax, %rsi
callq 0x4f0fb
testl %eax, %eax
js 0x629cf
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x62922
movq %r15, %rdi
callq 0x501e9
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x52a08
testl %eax, %eax
jns 0x629cd
jmp 0x629cf
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x40(%rbx)
movq 0x28(%r14), %r15
movq 0x38(%rbx), %r12
movq %r15, %rdi
callq 0x62d0
movq %rax, %r13
movq %r12, 0x10(%rsp)
movq 0x8(%r12), %rdi
movq 0x18(%r12), %r12
subq %rdi, %r12
cmpq %rax, %r12
movq %rax, %rbp
cmovbq %r12, %rbp
movq %r15, 0x8(%rsp)
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6570
movq 0x10(%rsp), %rcx
addq %rbp, 0x8(%rcx)
cmpq %r12, %r13
jbe 0x629a6
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x5f99f
movq 0x28(%r14), %rdi
leaq 0x2696d(%rip), %rsi     # 0x8931e
callq 0x6180
testq %rax, %rax
je 0x629e0
movq 0x38(%rbx), %rdi
callq 0x5f83e
testl %eax, %eax
je 0x629cd
pushq $-0x1
popq %rbp
jmp 0x629cf
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rbx), %r15
xorl %r13d, %r13d
pushq $0xa
popq %r12
pushq $-0x1
popq %rbp
movslq (%r14), %rax
cmpq %rax, %r13
jge 0x629bb
andq $0x0, 0x10(%rbx)
leaq 0x26962(%rip), %rdi     # 0x89364
pushq $0x7
popq %rsi
movq %r15, %rdx
callq 0x62774
movq 0x20(%r14), %rax
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x62a6e
leaq 0x26948(%rip), %rdi     # 0x8936c
pushq $0x4
popq %rsi
movq %r15, %rdx
callq 0x62774
movq 0x10(%r14), %rax
movl (%rax,%r13,4), %edi
movq %r15, %rsi
callq 0x62a91
movl %r12d, %edi
movq %r15, %rsi
callq 0x62808
movq 0x38(%rbx), %rdi
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
callq 0x62b5d
incq %r13
cmpq 0x10(%rbx), %rax
je 0x629ee
jmp 0x629cf
callq 0x63c0