// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x5, %eax
ja 0xa7f91
movq -0x40(%rbp), %rax
leaq 0x3c4b5(%rip), %rcx     # 0xe4238
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movl $0x1, 0x40(%rax)
movq -0x10(%rbp), %rax
movl $0x4, 0x44(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0xa5b30
jmp 0xa7f96
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0xcb440
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa7de4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa7f9d
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x810c0
cmpl $0x0, %eax
jge 0xa7e02
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa7f9d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0xa7e1e
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x82cb0
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x88b70
cmpl $0x0, %eax
jge 0xa7e40
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa7f9d
jmp 0xa7f96
movq -0x10(%rbp), %rax
movl $0x1, 0x40(%rax)
movq -0x10(%rbp), %rax
movl $0x3, 0x44(%rax)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0xa7fb0
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x3a422(%rip), %rsi     # 0xe22a1
callq 0x6170
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xa7f74
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xa7f72
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
leaq 0x3c56a(%rip), %rdi     # 0xe442e
movl $0x7, %esi
callq 0xa7a90
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
callq 0xa8050
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
leaq 0x3c53c(%rip), %rdi     # 0xe4436
movl $0x4, %esi
callq 0xa7a90
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %edi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
callq 0xa8090
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
movl $0xa, %edi
callq 0xa7c00
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0xa8280
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
je 0xa7f62
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa7f9d
jmp 0xa7f64
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xa7e9a
jmp 0xa7f74
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0xa1f60
cmpl $0x0, %eax
je 0xa7f8f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa7f9d
jmp 0xa7f96
callq 0x63b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)