// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl 0x44(%rdi), %eax
decl %eax
cmpl $0x5, %eax
ja 0x6e8ad
movq %rdi, %rbx
leaq 0x2c02b(%rip), %rcx     # 0x9a464
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rdi
callq 0x6d0e0
jmp 0x6e53f
movq 0x38(%rbx), %r15
movq %rsi, %rdi
callq 0x86873
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x6e541
movq %r15, %rdi
movq %rax, %rsi
callq 0x57a08
testl %eax, %eax
js 0x6e541
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x6e49c
movq %r15, %rdi
callq 0x58cb4
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x5ba60
testl %eax, %eax
jns 0x6e53f
jmp 0x6e541
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x40(%rbx)
movq %rsi, (%rsp)
movq 0x28(%rsi), %r12
movq 0x38(%rbx), %r15
movq %r12, %rdi
callq 0x62d0
movq %rax, %r13
movq 0x8(%r15), %rdi
movq 0x18(%r15), %r14
subq %rdi, %r14
cmpq %rax, %r14
movq %rax, %rbp
cmovbq %r14, %rbp
movq %r12, %rsi
movq %rbp, %rdx
callq 0x6570
addq %rbp, 0x8(%r15)
cmpq %r14, %r13
jbe 0x6e514
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x6b2d9
movq (%rsp), %r14
movq 0x28(%r14), %rdi
leaq 0x2c15b(%rip), %rsi     # 0x9a67e
callq 0x6180
testq %rax, %rax
je 0x6e552
movq 0x38(%rbx), %rdi
callq 0x6b16c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x6e541
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, (%r14)
jle 0x6e52d
xorl %r13d, %r13d
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
movq $0x0, 0x10(%rbx)
cmpq $0x9, 0x18(%rbx)
jb 0x6e577
movq 0x20(%rbx), %rax
xorl %ecx, %ecx
jmp 0x6e59a
movq $0x10, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl $0x10, %esi
callq 0x6c80
testq %rax, %rax
je 0x6e5be
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rcx
movl $0x3a4e5309, 0x3(%rax,%rcx) # imm = 0x3A4E5309
movl $0x9515340, (%rax,%rcx) # imm = 0x9515340
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x7(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0x0, 0x7(%rcx,%rax)
movq 0x20(%r14), %rax
movq (%rax,%r13,8), %r15
movq %r15, %rdi
callq 0x62d0
movq 0x10(%rbx), %rdi
movslq %eax, %r12
leaq (%rdi,%r12), %rax
incq %rax
cmpq 0x18(%rbx), %rax
jae 0x6e5e8
movq 0x20(%rbx), %rax
jmp 0x6e636
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
callq 0x6c80
testq %rax, %rax
je 0x6e655
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
addq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6570
addq 0x10(%rbx), %r12
movq %r12, 0x10(%rbx)
movq 0x20(%rbx), %rax
movb $0x0, (%rax,%r12)
movq 0x10(%rbx), %rcx
leaq 0x5(%rcx), %rax
cmpq 0x18(%rbx), %rax
jae 0x6e669
movq 0x20(%rbx), %rax
jmp 0x6e6b7
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
callq 0x6c80
testq %rax, %rax
je 0x6e6d3
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rcx
movl $0x3a4e4c09, (%rax,%rcx) # imm = 0x3A4E4C09
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x4(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0x0, 0x4(%rcx,%rax)
movq 0x10(%r14), %rax
movl (%rax,%r13,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorl %r15d, %r15d
movl %ecx, %edx
imulq %rbp, %rdx
shrq $0x23, %rdx
leal (%rdx,%rdx), %esi
leal (%rsi,%rsi,4), %esi
movl %ecx, %edi
subl %esi, %edi
orb $0x30, %dil
movb %dil, 0x10(%rsp,%r15)
incq %r15
cmpl $0x9, %ecx
movl %edx, %ecx
ja 0x6e6e5
testl %eax, %eax
jns 0x6e71b
movl %r15d, %eax
incl %r15d
movb $0x2d, 0x10(%rsp,%rax)
movq 0x10(%rbx), %rax
movslq %r15d, %rcx
addq %rcx, %rax
incq %rax
cmpq 0x18(%rbx), %rax
jb 0x6e778
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
callq 0x6c80
testq %rax, %rax
je 0x6e7b3
movq %rax, 0x20(%rbx)
movl %r15d, %eax
leaq 0xf(%rsp), %rcx
addq %rcx, %rax
incl %r15d
movb (%rax), %cl
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, 0x10(%rbx)
movb %cl, (%rsi,%rdx)
decl %r15d
decq %rax
cmpl $0x1, %r15d
jg 0x6e786
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%rbx), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x18(%rbx), %rcx
jae 0x6e7c7
movq 0x20(%rbx), %rax
jmp 0x6e819
movq %rcx, %rax
shrq %rax
orq %rcx, %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, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
callq 0x6c80
testq %rax, %rax
je 0x6e82d
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x10(%rbx)
movb $0xa, (%rax,%rdx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq %r13, 0x8(%rsp)
movq 0x38(%rbx), %r13
movq 0x10(%rbx), %r12
movq 0x20(%rbx), %rbp
movq 0x8(%r13), %rdi
movq 0x18(%r13), %r14
subq %rdi, %r14
cmpq %r12, %r14
movq %r12, %r15
cmovbq %r14, %r15
movq %rbp, %rsi
movq %r15, %rdx
callq 0x6570
addq %r15, 0x8(%r13)
cmpq %r12, %r14
jae 0x6e87b
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6b241
movq %rax, %r15
movq (%rsp), %r14
movq 0x8(%rsp), %r13
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
cmpq 0x10(%rbx), %r15
jne 0x6e8a3
incq %r13
movslq (%r14), %rax
cmpq %rax, %r13
jl 0x6e560
jmp 0x6e52d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6e541
callq 0x63c0