// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movl 0x40(%rsi), %edx
movzbl 0xf(%rsi), %eax
subl %eax, %edx
leal 0x20(%rdx), %r15d
movl %r15d, (%rsp)
movzwl 0x12(%rsi), %ecx
subl %eax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
jl 0x1eb1e
movq 0x38(%rbx), %rcx
leaq 0x5ac7c(%rip), %rsi     # 0x7977c
leaq 0x5ac80(%rip), %rdx     # 0x79787
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
callq 0x4050
movl $0x4b, (%rax)
jmp 0x1eb71
movq %rdi, %r14
movl 0x14(%rbx), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x1eb34
addl $0x30, %edx
movl %edx, (%rsp)
movl %edx, %r15d
movq (%rbx), %rdi
cmpq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
jg 0x1eb59
movq 0x20(%rbx), %rdx
cmpq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
jg 0x1eb59
movq 0x28(%rbx), %rsi
movslq %esi, %r8
cmpq %rsi, %r8
je 0x1eb83
leaq 0x5ac1c(%rip), %rsi     # 0x7977c
leaq 0x5ac49(%rip), %rdx     # 0x797b0
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
pushq $-0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%rbx), %r8d
movl %r8d, 0x10(%rsp)
movl %edi, 0x14(%rsp)
movl 0xc(%rbx), %edi
shll $0x10, %edi
movzbl 0xe(%rbx), %r8d
shll $0x8, %r8d
orl %edi, %r8d
orl %ecx, %r8d
movl %r8d, 0x18(%rsp)
movl 0x10(%rbx), %ecx
shll $0x10, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
pushq $0x2
popq %rdi
cmovbl %eax, %edi
orl %ecx, %edi
movl %edi, 0x1c(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x20(%rsp)
movl %edx, 0x28(%rsp)
movl %esi, 0x2c(%rsp)
leal 0x4(%r15), %esi
movq %r14, %rdi
callq 0xba3a
testb $0x8, 0x2(%r14)
jne 0x1ec02
testl %eax, %eax
js 0x1ec43
movq %rsp, %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x1edd5
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x1ec56
xorl %ecx, %ecx
cmpq $0x8, %rcx
je 0x1ec19
movl 0x10(%rsp,%rcx,4), %edx
bswapl %edx
movl %edx, 0x10(%rsp,%rcx,4)
incq %rcx
jmp 0x1ec04
movl %r15d, 0xc(%rsp)
testl %eax, %eax
js 0x1ec48
bswapl %r15d
leaq 0xc(%rsp), %rsi
movl %r15d, (%rsi)
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x1edd5
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x1ec4a
movb $0x1, %r12b
jmp 0x1ec90
xorl %ebp, %ebp
movq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x1e89a
movb $0x1, %r12b
testl %ebp, %ebp
je 0x1ec90
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movq %r14, %rdi
callq 0x1edd5
testq %rax, %rax
js 0x1ec90
movq 0x38(%rbx), %rsi
movzwl 0x12(%rbx), %edx
movzbl 0xf(%rbx), %eax
subq %rax, %rdx
movq %r14, %rdi
callq 0x1edd5
movq %rax, %r12
shrq $0x3f, %r12
movl 0x14(%rbx), %r13d
cmpl $0xffff, %r13d          # imm = 0xFFFF
ja 0x1ecd0
xorl %ebp, %ebp
testb %r12b, %r12b
jne 0x1ed88
movzwl 0x12(%rbx), %eax
movq 0x38(%rbx), %rsi
addq %rax, %rsi
movslq 0x40(%rbx), %rdx
subq %rax, %rdx
movq %r14, %rdi
callq 0x1edd5
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x1ed88
movq 0x38(%rbx), %rbp
movzwl 0x12(%rbx), %r15d
leaq (%r15,%rbp), %rsi
movl %r13d, %edi
callq 0x1e4e1
cmpq $0xfffffff, %rax        # imm = 0xFFFFFFF
jg 0x1edaf
movl 0x18(%rbx), %ecx
shll $0x4, %ecx
orl $0x4, %ecx
shll $0x4, %eax
orl $0x3, %eax
movl %ecx, 0x4(%rsp)
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
testb %r12b, %r12b
jne 0x1ed88
leaq 0x4(%rsp), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x1edd5
testq %rax, %rax
js 0x1ed88
leal (%r15,%r13,4), %eax
movl %eax, %esi
addq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
subl %eax, %edx
movq %r14, %rdi
callq 0x1edd5
testq %rax, %rax
js 0x1ed88
leaq 0x5ab01(%rip), %rsi     # 0x79848
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x1edd5
movl 0x14(%rbx), %ecx
movl %ecx, 0x4(%rsp)
testq %rax, %rax
js 0x1ed88
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x1edd5
testq %rax, %rax
js 0x1ed88
addq 0x38(%rbx), %r15
movl 0x14(%rbx), %edx
shll $0x2, %edx
movq %r14, %rdi
movq %r15, %rsi
jmp 0x1ecbd
testb $0x8, 0x2(%r14)
je 0x1ed9b
movq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x1e89a
movl (%rsp), %ecx
addl $0x4, %ecx
xorl %eax, %eax
cmpl $0x1, %ebp
sbbl %eax, %eax
orl %ecx, %eax
jmp 0x1eb74
leaq 0x5a9c6(%rip), %rsi     # 0x7977c
leaq 0x5aa1f(%rip), %rdx     # 0x797dc
pushq $0x1
popq %rdi
movq %rbp, %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x1eb71