// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movl 0x44(%rdi), %eax
decl %eax
cmpl $0x5, %eax
ja 0x15dd1
movq %rdi, %rbx
leaq 0x3b861(%rip), %rcx     # 0x51174
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rdi
callq 0x14510
testl %eax, %eax
jns 0x15a26
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x15a28
movq 0x38(%rbx), %r15
movq %rsi, %rdi
callq 0x3990e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x15a28
movq %r15, %rdi
movq %rax, %rsi
callq 0x3310c
testl %eax, %eax
js 0x15a28
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x15983
movq %r15, %rdi
callq 0x345b1
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x3735d
testl %eax, %eax
jns 0x15a26
jmp 0x15a28
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x40(%rbx)
movq %rsi, (%rsp)
movq 0x28(%rsi), %r12
movq 0x38(%rbx), %r15
movq %r12, %rdi
callq 0x4230
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 0x4420
addq %rbp, 0x8(%r15)
cmpq %r14, %r13
jbe 0x159fb
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x473a1
movq (%rsp), %r14
movq 0x28(%r14), %rdi
leaq 0x3b984(%rip), %rsi     # 0x5138e
callq 0x4120
testq %rax, %rax
je 0x15a39
movq 0x38(%rbx), %rdi
callq 0x47234
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x15a28
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 0x15a14
xorl %ebp, %ebp
movq $0x0, 0x10(%rbx)
movq 0x20(%rbx), %rax
cmpq $0x8, 0x18(%rbx)
ja 0x15a78
movl $0x10, %esi
movq %rax, %rdi
callq 0x48d0
testq %rax, %rax
je 0x15a9e
movq %rax, 0x20(%rbx)
movq $0x10, 0x18(%rbx)
movq 0x10(%rbx), %rcx
jmp 0x15a7a
xorl %ecx, %ecx
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,%rbp,8), %r15
movq %r15, %rdi
callq 0x4230
movq 0x10(%rbx), %rdi
movslq %eax, %r12
leaq (%r12,%rdi), %rax
addq $0x2, %rax
cmpq %rax, 0x18(%rbx)
jae 0x15b26
addq %r12, %rdi
incq %rdi
movq %rdi, %rax
shrq %rax
orq %rdi, %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, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r13
shrq $0x20, %r13
orq %rax, %r13
incq %r13
movq 0x20(%rbx), %rdi
movq %r13, %rsi
callq 0x48d0
testq %rax, %rax
je 0x15b49
movq %rax, 0x20(%rbx)
movq %r13, 0x18(%rbx)
movq 0x10(%rbx), %rdi
jmp 0x15b2a
movq 0x20(%rbx), %rax
addq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4420
addq 0x10(%rbx), %r12
movq %r12, 0x10(%rbx)
movq 0x20(%rbx), %rax
movb $0x0, (%rax,%r12)
movq 0x10(%rbx), %rcx
leaq 0x6(%rcx), %rax
cmpq %rax, 0x18(%rbx)
jae 0x15bb8
addq $0x5, %rcx
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, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r15
shrq $0x20, %r15
orq %rax, %r15
incq %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x48d0
testq %rax, %rax
je 0x15bd8
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rcx
jmp 0x15bbc
movq 0x20(%rbx), %rax
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,%rbp,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorl %r12d, %r12d
movl $0xcccccccd, %r8d       # imm = 0xCCCCCCCD
movl %ecx, %edx
imulq %r8, %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,%r12)
incq %r12
cmpl $0x9, %ecx
movl %edx, %ecx
ja 0x15bef
testl %eax, %eax
jns 0x15c25
movl %r12d, %eax
incl %r12d
movb $0x2d, 0x10(%rsp,%rax)
movslq %r12d, %rcx
movq 0x10(%rbx), %rax
leaq (%rax,%rcx), %rdx
addq $0x2, %rdx
cmpq %rdx, 0x18(%rbx)
jae 0x15c97
addq %rcx, %rax
incq %rax
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %r15
shrq $0x20, %r15
orq %rcx, %r15
incq %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x48d0
testq %rax, %rax
je 0x15cd2
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movl %r12d, %eax
leaq 0xf(%rsp), %rcx
addq %rcx, %rax
incl %r12d
movb (%rax), %cl
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, 0x10(%rbx)
movb %cl, (%rsi,%rdx)
decl %r12d
decq %rax
cmpl $0x1, %r12d
jg 0x15ca5
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%rbx), %rcx
leaq 0x2(%rcx), %rax
cmpq %rax, 0x18(%rbx)
jae 0x15d40
incq %rcx
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, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %r15
shrq $0x20, %r15
orq %rax, %r15
incq %r15
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x48d0
testq %rax, %rax
je 0x15d5c
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rcx
jmp 0x15d44
movq 0x20(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbx)
movb $0xa, (%rax,%rcx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq %rbp, 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 0x4420
addq %r15, 0x8(%r13)
cmpq %r12, %r14
jae 0x15daa
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x47309
movq %rax, %r15
movq (%rsp), %r14
movq 0x8(%rsp), %rbp
cmpq 0x10(%rbx), %r15
jne 0x1593b
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x15a41
jmp 0x15a14
callq 0x42e0