// File: /workspace/github_c_makefile_star2_O1/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 0x6e954
movq %rdi, %rbx
leaq 0x2bd66(%rip), %rcx     # 0x9a254
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rdi
callq 0x6d141
jmp 0x6e5f4
movq 0x38(%rbx), %r15
movq %rsi, %rdi
callq 0x86cce
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x6e5f6
movq %r15, %rdi
movq %rax, %rsi
callq 0x577a9
testl %eax, %eax
js 0x6e5f6
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x6e551
movq %r15, %rdi
callq 0x58a8b
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x5b889
testl %eax, %eax
jns 0x6e5f4
jmp 0x6e5f6
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 0x6e5c9
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x6b2c4
movq (%rsp), %r14
movq 0x28(%r14), %rdi
leaq 0x2be76(%rip), %rsi     # 0x9a44e
callq 0x6180
testq %rax, %rax
je 0x6e607
movq 0x38(%rbx), %rdi
callq 0x6b143
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x6e5f6
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 0x6e5e2
xorl %r13d, %r13d
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
movq $0x0, 0x10(%rbx)
cmpq $0x8, 0x18(%rbx)
ja 0x6e643
movq $0x10, 0x18(%rbx)
movq 0x20(%rbx), %rdi
movl $0x10, %esi
callq 0x6c80
testq %rax, %rax
je 0x6e66f
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movl $0x3a4e5309, 0x3(%rcx,%rax) # imm = 0x3A4E5309
movl $0x9515340, (%rcx,%rax) # 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), %rcx
movslq %eax, %r12
leaq (%rcx,%r12), %rax
incq %rax
cmpq 0x18(%rbx), %rax
jb 0x6e6dd
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 0x6e701
movq %rax, 0x20(%rbx)
movq 0x20(%rbx), %rdi
addq 0x10(%rbx), %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), %rax
addq $0x5, %rax
cmpq 0x18(%rbx), %rax
jb 0x6e759
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 0x6e77d
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movl $0x3a4e4c09, (%rcx,%rax) # 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 0x6e78f
testl %eax, %eax
jns 0x6e7c5
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 0x6e822
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 0x6e85d
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 0x6e830
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%rbx), %rax
incq %rax
cmpq 0x18(%rbx), %rax
jb 0x6e8b4
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 0x6e8d4
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rbx)
movb $0xa, (%rcx,%rax)
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 0x6e922
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x6b222
movq %rax, %r15
movq (%rsp), %r14
movq 0x8(%rsp), %r13
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
cmpq 0x10(%rbx), %r15
jne 0x6e94a
incq %r13
movslq (%r14), %rax
cmpq %rax, %r13
jl 0x6e615
jmp 0x6e5e2
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6e5f6
callq 0x63c0