// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/sam.c
// Function: sam_hdr_write
int sam_hdr_write(htsFile *fp, const sam_hdr_t *h)
{
    if (!fp || !h) {
        errno = EINVAL;
        return -1;
    }

    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;
        if (cram_set_header2(fd, h) < 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: {
        if (!h->hrecs && !h->text)
            return 0;
        char *text;
        kstring_t hdr_ks = { 0, 0, NULL };
        size_t l_text;
        ssize_t bytes;
        int r = 0, no_sq = 0;

        if (h->hrecs) {
            if (sam_hrecs_rebuild_text(h->hrecs, &hdr_ks) != 0)
                return -1;
            text = hdr_ks.s;
            l_text = hdr_ks.l;
        } else {
            const char *p = NULL;
            do {
                const char *q = p == NULL ? h->text : p + 4;
                p = strstr(q, "@SQ\t");
            } while (!(p == NULL || p == h->text || *(p - 1) == '\n'));
            no_sq = p == NULL;
            text = h->text;
            l_text = h->l_text;
        }

        if (fp->is_bgzf) {
            bytes = bgzf_write(fp->fp.bgzf, text, l_text);
        } else {
            bytes = hwrite(fp->fp.hfile, text, l_text);
        }
        free(hdr_ks.s);
        if (bytes != l_text)
            return -1;

        if (no_sq) {
            int i;
            for (i = 0; i < h->n_targets; ++i) {
                fp->line.l = 0;
                r |= kputsn("@SQ\tSN:", 7, &fp->line) < 0;
                r |= kputs(h->target_name[i], &fp->line) < 0;
                r |= kputsn("\tLN:", 4, &fp->line) < 0;
                r |= kputw(h->target_len[i], &fp->line) < 0;
                r |= kputc('\n', &fp->line) < 0;
                if (r != 0)
                    return -1;

                if (fp->is_bgzf) {
                    bytes = bgzf_write(fp->fp.bgzf, fp->line.s, fp->line.l);
                } else {
                    bytes = hwrite(fp->fp.hfile, fp->line.s, fp->line.l);
                }
                if (bytes != fp->line.l)
                    return -1;
            }
        }
        if (fp->is_bgzf) {
            if (bgzf_flush(fp->fp.bgzf) != 0) return -1;
        } else {
            if (hflush(fp->fp.hfile) != 0) return -1;
        }
        }
        break;

    case fastq_format:
    case fasta_format:
        // Nothing to output; FASTQ has no file headers.
        break;

    default:
        errno = EBADF;
        return -1;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x38901
cmpq $0x0, -0x18(%rbp)
jne 0x38918
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x78(%rbp)
subl $0x10, %eax
ja 0x38d7d
movq -0x78(%rbp), %rax
leaq 0xb8ae5(%rip), %rcx     # 0xf1420
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movl $0x1, 0x48(%rax)
movq -0x10(%rbp), %rax
movl $0x4, 0x4c(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x32860
cmpl $0x0, %eax
jge 0x3897c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
jmp 0x38d91
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x71c60
cmpl $0x0, %eax
jge 0x389ab
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
movq -0x10(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x389c7
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x73a80
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x77ca0
cmpl $0x0, %eax
jge 0x389e9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
jmp 0x38d91
movq -0x10(%rbp), %rax
movl $0x1, 0x48(%rax)
movq -0x10(%rbp), %rax
movl $0x3, 0x4c(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x38a26
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x38a26
movl $0x0, -0x4(%rbp)
jmp 0x38d98
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x38a86
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0xd8c10
cmpl $0x0, %eax
je 0x38a71
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x38b3c
movq $0x0, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x38aa3
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x38aaf
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
leaq 0xb74de(%rip), %rsi     # 0xeffa0
callq 0x40c0
movq %rax, -0x60(%rbp)
movb $0x1, %al
cmpq $0x0, -0x60(%rbp)
movb %al, -0x81(%rbp)
je 0x38b04
movq -0x60(%rbp), %rcx
movq -0x18(%rbp), %rdx
movb $0x1, %al
cmpq 0x28(%rdx), %rcx
movb %al, -0x81(%rbp)
je 0x38b04
movq -0x60(%rbp), %rax
movsbl -0x1(%rax), %eax
cmpl $0xa, %eax
sete %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x38a8e
cmpq $0x0, -0x60(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x38b68
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0xffb0
movq %rax, -0x50(%rbp)
jmp 0x38b81
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x38db0
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x38ba0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
cmpl $0x0, -0x58(%rbp)
je 0x38d2e
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x38d2c
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
leaq 0xb92a4(%rip), %rdi     # 0xf1e7f
movl $0x7, %esi
callq 0x38ef0
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
callq 0x38fa0
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
leaq 0xb9254(%rip), %rdi     # 0xf1e87
movl $0x4, %esi
callq 0x38ef0
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
callq 0x39000
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rsi
addq $0x10, %rsi
movl $0xa, %edi
callq 0x39080
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x38cb0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x38ce4
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 0xffb0
movq %rax, -0x50(%rbp)
jmp 0x38d05
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 0x38db0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
je 0x38d1c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
jmp 0x38d1e
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x38bb1
jmp 0x38d2e
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x38d5c
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0xf820
cmpl $0x0, %eax
je 0x38d5a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
jmp 0x38d79
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x143b0
cmpl $0x0, %eax
je 0x38d77
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
jmp 0x38d79
jmp 0x38d91
jmp 0x38d91
callq 0x4050
movl $0x9, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38d98
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)