// File: karel-brinda[P]ococo/ext/htslib/sam.c
// Function: bam_hdr_write
int bam_hdr_write(BGZF *fp, const bam_hdr_t *h)
{
    char buf[4];
    int32_t i, name_len, x;
    // write "BAM1"
    strncpy(buf, "BAM\1", 4);
    if (bgzf_write(fp, buf, 4) < 0) return -1;
    // write plain text and the number of reference sequences
    if (fp->is_be) {
        x = ed_swap_4(h->l_text);
        if (bgzf_write(fp, &x, 4) < 0) return -1;
        if (h->l_text) {
            if (bgzf_write(fp, h->text, h->l_text) < 0) return -1;
        }
        x = ed_swap_4(h->n_targets);
        if (bgzf_write(fp, &x, 4) < 0) return -1;
    } else {
        if (bgzf_write(fp, &h->l_text, 4) < 0) return -1;
        if (h->l_text) {
            if (bgzf_write(fp, h->text, h->l_text) < 0) return -1;
        }
        if (bgzf_write(fp, &h->n_targets, 4) < 0) return -1;
    }
    // write sequence names and lengths
    for (i = 0; i != h->n_targets; ++i) {
        char *p = h->target_name[i];
        name_len = strlen(p) + 1;
        if (fp->is_be) {
            x = ed_swap_4(name_len);
            if (bgzf_write(fp, &x, 4) < 0) return -1;
        } else {
            if (bgzf_write(fp, &name_len, 4) < 0) return -1;
        }
        if (bgzf_write(fp, p, name_len) < 0) return -1;
        if (fp->is_be) {
            x = ed_swap_4(h->target_len[i]);
            if (bgzf_write(fp, &x, 4) < 0) return -1;
        } else {
            if (bgzf_write(fp, &h->target_len[i], 4) < 0) return -1;
        }
    }
    if (bgzf_flush(fp) < 0) return -1;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x1c(%rbp), %rdi
leaq 0x5fb21(%rip), %rsi     # 0x7ad0c
movl $0x4, %edx
callq 0x42c0
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b219
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1b2d0
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x1b4d0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b261
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x1b299
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b297
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b299
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x1b4d0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b2cb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b356
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b2f8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x1b330
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b32e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b330
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b354
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b356
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x1b4a8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4230
addq $0x1, %rax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1b3d2
movl -0x24(%rbp), %edi
callq 0x1b4d0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b3d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b3f8
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b3f6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b3f8
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x24(%rbp), %rdx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b41b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1b466
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b4d0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b464
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b498
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x20(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x65700
cmpq $0x0, %rax
jge 0x1b496
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
jmp 0x1b498
jmp 0x1b49a
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1b35d
movq -0x10(%rbp), %rdi
callq 0x65080
cmpl $0x0, %eax
jge 0x1b4bf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1b4c6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop