// File: /workspace/github_cpp_makefile_star2_O1/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 0x15e0a
movq %rdi, %rbx
leaq 0x3d80c(%rip), %rcx     # 0x53164
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x40(%rbx)
movq 0x38(%rbx), %rdi
callq 0x1451d
testl %eax, %eax
jns 0x15a6b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x15a6d
movq 0x38(%rbx), %r15
movq %rsi, %rdi
callq 0x3a59d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x15a6d
movq %r15, %rdi
movq %rax, %rsi
callq 0x33e2a
testl %eax, %eax
js 0x15a6d
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x159c8
movq %r15, %rdi
callq 0x35395
movq 0x18(%r15), %rsi
movq %r15, %rdi
callq 0x38193
testl %eax, %eax
jns 0x15a6b
jmp 0x15a6d
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 0x15a40
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x483f8
movq (%rsp), %r14
movq 0x28(%r14), %rdi
leaq 0x3d90f(%rip), %rsi     # 0x5335e
callq 0x4120
testq %rax, %rax
je 0x15a7e
movq 0x38(%rbx), %rdi
callq 0x48277
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x15a6d
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 0x15a59
xorl %ebp, %ebp
movq $0x0, 0x10(%rbx)
cmpq $0x8, 0x18(%rbx)
ja 0x15ab4
movq 0x20(%rbx), %rdi
movl $0x10, %esi
callq 0x48d0
testq %rax, %rax
je 0x15ae0
movq %rax, 0x20(%rbx)
movq $0x10, 0x18(%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,%rbp,8), %r12
movq %r12, %rdi
callq 0x4230
movslq %eax, %r15
movq 0x10(%rbx), %rax
leaq (%r15,%rax), %rcx
addq $0x2, %rcx
cmpq %rcx, 0x18(%rbx)
jae 0x15b62
addq %r15, %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, %r13
shrq $0x20, %r13
orq %rcx, %r13
incq %r13
movq 0x20(%rbx), %rdi
movq %r13, %rsi
callq 0x48d0
testq %rax, %rax
je 0x15b86
movq %rax, 0x20(%rbx)
movq %r13, 0x18(%rbx)
movq 0x20(%rbx), %rdi
addq 0x10(%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4420
addq 0x10(%rbx), %r15
movq %r15, 0x10(%rbx)
movq 0x20(%rbx), %rax
movb $0x0, (%rax,%r15)
movq 0x10(%rbx), %rax
leaq 0x6(%rax), %rcx
cmpq %rcx, 0x18(%rbx)
jae 0x15bef
addq $0x5, %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 0x15c13
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%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,%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 0x15c2a
testl %eax, %eax
jns 0x15c60
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 0x15cd2
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 0x15d0d
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 0x15ce0
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq 0x10(%rbx), %rax
leaq 0x2(%rax), %rcx
cmpq %rcx, 0x18(%rbx)
jae 0x15d75
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 0x15d95
movq %rax, 0x20(%rbx)
movq %r15, 0x18(%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 %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 0x15de3
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x48356
movq %rax, %r15
movq (%rsp), %r14
movq 0x8(%rsp), %rbp
cmpq 0x10(%rbx), %r15
jne 0x15980
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x15a86
jmp 0x15a59
callq 0x42e0