// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/sam.c
// Function: bam_write1
int bam_write1(BGZF *fp, const bam1_t *b)
{
    const bam1_core_t *c = &b->core;
    uint32_t x[8], block_len = b->l_data - c->l_extranul + 32, y;
    int i, ok;
    if (c->l_qname - c->l_extranul > 255) {
        hts_log_error("QNAME \"%s\" is longer than 254 characters", bam_get_qname(b));
        errno = EOVERFLOW;
        return -1;
    }
    if (c->n_cigar > 0xffff) block_len += 16; // "16" for "CGBI", 4-byte tag length and 8-byte fake CIGAR
    if (c->pos > INT_MAX ||
        c->mpos > INT_MAX ||
        c->isize < INT_MIN || c->isize > INT_MAX) {
        hts_log_error("Positional data is too large for BAM format");
        return -1;
    }
    x[0] = c->tid;
    x[1] = c->pos;
    x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | (c->l_qname - c->l_extranul);
    if (c->n_cigar > 0xffff) x[3] = (uint32_t)c->flag << 16 | 2;
    else x[3] = (uint32_t)c->flag << 16 | (c->n_cigar & 0xffff);
    x[4] = c->l_qseq;
    x[5] = c->mtid;
    x[6] = c->mpos;
    x[7] = c->isize;
    ok = (bgzf_flush_try(fp, 4 + block_len) >= 0);
    if (fp->is_be) {
        for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
        y = block_len;
        if (ok) ok = (bgzf_write_small(fp, ed_swap_4p(&y), 4) >= 0);
        swap_data(c, b->l_data, b->data, 1);
    } else {
        if (ok) ok = (bgzf_write_small(fp, &block_len, 4) >= 0);
    }
    if (ok) ok = (bgzf_write_small(fp, x, 32) >= 0);
    if (ok) ok = (bgzf_write_small(fp, b->data, c->l_qname - c->l_extranul) >= 0);
    if (c->n_cigar <= 0xffff) { // no long CIGAR; write normally
        if (ok) ok = (bgzf_write_small(fp, b->data + c->l_qname, b->l_data - c->l_qname) >= 0);
    } else { // with long CIGAR, insert a fake CIGAR record and move the real CIGAR to the CG:B,I tag
        uint8_t buf[8];
        uint32_t cigar_st, cigar_en, cigar[2];
        hts_pos_t cigreflen = bam_cigar2rlen(c->n_cigar, bam_get_cigar(b));
        if (cigreflen >= (1<<28)) {
            // Length of reference covered is greater than the biggest
            // CIGAR operation currently allowed.
            hts_log_error("Record %s with %d CIGAR ops and ref length %"PRIhts_pos
                          " cannot be written in BAM.  Try writing SAM or CRAM instead.\n",
                          bam_get_qname(b), c->n_cigar, cigreflen);
            return -1;
        }
        cigar_st = (uint8_t*)bam_get_cigar(b) - b->data;
        cigar_en = cigar_st + c->n_cigar * 4;
        cigar[0] = (uint32_t)c->l_qseq << 4 | BAM_CSOFT_CLIP;
        cigar[1] = (uint32_t)cigreflen << 4 | BAM_CREF_SKIP;
        u32_to_le(cigar[0], buf);
        u32_to_le(cigar[1], buf + 4);
        if (ok) ok = (bgzf_write_small(fp, buf, 8) >= 0); // write cigar: <read_length>S<ref_length>N
        if (ok) ok = (bgzf_write_small(fp, &b->data[cigar_en], b->l_data - cigar_en) >= 0); // write data after CIGAR
        if (ok) ok = (bgzf_write_small(fp, "CGBI", 4) >= 0); // write CG:B,I
        u32_to_le(c->n_cigar, buf);
        if (ok) ok = (bgzf_write_small(fp, buf, 4) >= 0); // write the true CIGAR length
        if (ok) ok = (bgzf_write_small(fp, &b->data[cigar_st], c->n_cigar * 4) >= 0); // write the real CIGAR
    }
    if (fp->is_be) swap_data(c, b->l_data, b->data, 0);
    return ok? 4 + block_len : -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0xf(%rcx), %ecx
subl %ecx, %eax
addl $0x20, %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
movzwl 0x12(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0xf(%rcx), %ecx
subl %ecx, %eax
cmpl $0xff, %eax
jle 0x345f4
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movl $0x1, %edi
leaq 0xbd778(%rip), %rsi     # 0xf1d47
leaq 0xbd77c(%rip), %rdx     # 0xf1d52
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x34ad5
movq -0x20(%rbp), %rax
cmpl $0xffff, 0x14(%rax)     # imm = 0xFFFF
jbe 0x3460a
movl -0x44(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
jg 0x34641
movq -0x20(%rbp), %rax
cmpq $0x7fffffff, 0x20(%rax) # imm = 0x7FFFFFFF
jg 0x34641
movq -0x20(%rbp), %rax
cmpq $-0x80000000, 0x28(%rax) # imm = 0x80000000
jl 0x34641
movq -0x20(%rbp), %rax
cmpq $0x7fffffff, 0x28(%rax) # imm = 0x7FFFFFFF
jle 0x34667
movl $0x1, %edi
leaq 0xbd6fa(%rip), %rsi     # 0xf1d47
leaq 0xbd727(%rip), %rdx     # 0xf1d7b
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x34ad5
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movzwl 0xc(%rax), %eax
shll $0x10, %eax
movq -0x20(%rbp), %rcx
movzbl 0xe(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x20(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movq -0x20(%rbp), %rdx
movzbl 0xf(%rdx), %edx
subl %edx, %ecx
orl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpl $0xffff, 0x14(%rax)     # imm = 0xFFFF
jbe 0x346ca
movq -0x20(%rbp), %rax
movzwl 0x10(%rax), %eax
shll $0x10, %eax
orl $0x2, %eax
movl %eax, -0x34(%rbp)
jmp 0x346e7
movq -0x20(%rbp), %rax
movzwl 0x10(%rax), %eax
shll $0x10, %eax
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
callq 0xff00
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x347cf
movl $0x0, -0x4c(%rbp)
cmpl $0x8, -0x4c(%rbp)
jge 0x34773
leaq -0x40(%rbp), %rdi
movslq -0x4c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x32830
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x3474e
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x347b0
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x32830
movq -0x78(%rbp), %rdi
movq %rax, %rsi
movl $0x4, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdx
movl $0x1, %ecx
callq 0x341c0
jmp 0x347f8
cmpl $0x0, -0x50(%rbp)
je 0x347f6
movq -0x10(%rbp), %rdi
leaq -0x44(%rbp), %rsi
movl $0x4, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
jmp 0x347f8
cmpl $0x0, -0x50(%rbp)
je 0x3481f
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x20, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x3485a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x20(%rbp), %rax
movzwl 0x12(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0xf(%rcx), %ecx
subl %ecx, %eax
movslq %eax, %rdx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0xffff, 0x14(%rax)     # imm = 0xFFFF
ja 0x348b3
cmpl $0x0, -0x50(%rbp)
je 0x348ae
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x20(%rbp), %rax
movzwl 0x12(%rax), %eax
cltq
addq %rax, %rsi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x20(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
subl %ecx, %eax
movslq %eax, %rdx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
jmp 0x34a89
movq -0x20(%rbp), %rax
movl 0x14(%rax), %edi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x18(%rbp), %rax
movzwl 0x12(%rax), %eax
cltq
addq %rax, %rsi
callq 0x339b0
movq %rax, -0x70(%rbp)
cmpq $0x10000000, -0x70(%rbp) # imm = 0x10000000
jl 0x3491c
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movl 0x14(%rax), %r8d
movq -0x70(%rbp), %r9
movl $0x1, %edi
leaq 0xbd445(%rip), %rsi     # 0xf1d47
leaq 0xbd49e(%rip), %rdx     # 0xf1da7
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x34ad5
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
shll $0x4, %eax
orl $0x4, %eax
movl %eax, -0x68(%rbp)
movq -0x70(%rbp), %rax
shll $0x4, %eax
orl $0x3, %eax
movl %eax, -0x64(%rbp)
movl -0x68(%rbp), %edi
leaq -0x58(%rbp), %rsi
callq 0x34b90
movl -0x64(%rbp), %edi
leaq -0x58(%rbp), %rsi
addq $0x4, %rsi
callq 0x34b90
cmpl $0x0, -0x50(%rbp)
je 0x349b2
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x8, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x349ec
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x60(%rbp), %eax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
subl -0x60(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x34a16
movq -0x10(%rbp), %rdi
leaq 0xbd416(%rip), %rsi     # 0xf1e13
movl $0x4, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %edi
leaq -0x58(%rbp), %rsi
callq 0x34b90
cmpl $0x0, -0x50(%rbp)
je 0x34a4d
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x4, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x34a87
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x5c(%rbp), %eax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x34af0
cmpq $0x0, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
jmp 0x34a89
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x34ab4
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdx
xorl %ecx, %ecx
callq 0x341c0
cmpl $0x0, -0x50(%rbp)
je 0x34ac5
movl -0x44(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x7c(%rbp)
jmp 0x34acf
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x7c(%rbp)
jmp 0x34acf
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)