// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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:
        if (bam_hdr_write(fp->fp.bgzf, h) < 0) return -1;
        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 0x122ff
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x32008(%rip), %rcx     # 0x44154
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 0x10fe8
testl %eax, %eax
jns 0x12263
jmp 0x1225e
movq 0x38(%rbx), %r15
movq %r14, %rdi
callq 0x2eca5
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x12265
movq %r15, %rdi
movq %rax, %rsi
callq 0x29038
testl %eax, %eax
js 0x12265
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x121b8
movq %r15, %rdi
callq 0x2a280
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x2ca85
testl %eax, %eax
jns 0x12263
jmp 0x12265
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x40(%rbx)
movq 0x28(%r14), %r15
movq 0x38(%rbx), %r12
movq %r15, %rdi
callq 0x4210
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 0x4420
movq 0x10(%rsp), %rcx
addq %rbp, 0x8(%rcx)
cmpq %r12, %r13
jbe 0x1223c
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3a2ef
movq 0x28(%r14), %rdi
leaq 0x320a7(%rip), %rsi     # 0x442ee
callq 0x4110
testq %rax, %rax
je 0x12276
movq 0x38(%rbx), %rdi
callq 0x3a18e
testl %eax, %eax
je 0x12263
pushq $-0x1
popq %rbp
jmp 0x12265
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 0x12251
andq $0x0, 0x10(%rbx)
leaq 0x3209c(%rip), %rdi     # 0x44334
pushq $0x7
popq %rsi
movq %r15, %rdx
callq 0x12090
movq 0x20(%r14), %rax
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x12304
leaq 0x32082(%rip), %rdi     # 0x4433c
pushq $0x4
popq %rsi
movq %r15, %rdx
callq 0x12090
movq 0x10(%r14), %rax
movl (%rax,%r13,4), %edi
movq %r15, %rsi
callq 0x12327
movl %r12d, %edi
movq %r15, %rsi
callq 0x120dc
movq 0x38(%rbx), %rdi
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
callq 0x123b1
incq %r13
cmpq 0x10(%rbx), %rax
je 0x12284
jmp 0x12265
callq 0x42f0