// File: /workspace/github_c_makefile_star2_O3/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 0x2388e
movq 0x38(%rbx), %rcx
leaq 0x73fbe(%rip), %rsi     # 0x9782c
leaq 0x73fc2(%rip), %rdx     # 0x97837
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
callq 0x4050
movl $0x4b, (%rax)
jmp 0x238e3
movq %rdi, %r14
movl 0x14(%rbx), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x238a4
addl $0x30, %edx
movl %edx, (%rsp)
movl %edx, %r15d
movq (%rbx), %rdi
cmpq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
jg 0x238c9
movq 0x20(%rbx), %rdx
cmpq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
jg 0x238c9
movq 0x28(%rbx), %rsi
movslq %esi, %r8
cmpq %rsi, %r8
je 0x238f7
leaq 0x73f5c(%rip), %rsi     # 0x9782c
leaq 0x73f89(%rip), %rdx     # 0x97860
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
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
movl $0x2, %edi
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 0xcfdb
testb $0x8, 0x2(%r14)
jne 0x23980
testl %eax, %eax
js 0x239eb
movq %rsp, %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x23c4c
xorl %ecx, %ecx
testq %rax, %rax
setns %cl
jmp 0x23a18
xorl %ecx, %ecx
pxor %xmm0, %xmm0
movdqa 0x10(%rsp,%rcx,4), %xmm1
movdqa %xmm1, %xmm2
punpckhbw %xmm0, %xmm2    # xmm2 = xmm2[8],xmm0[8],xmm2[9],xmm0[9],xmm2[10],xmm0[10],xmm2[11],xmm0[11],xmm2[12],xmm0[12],xmm2[13],xmm0[13],xmm2[14],xmm0[14],xmm2[15],xmm0[15]
pshuflw $0x1b, %xmm2, %xmm2     # xmm2 = xmm2[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm2, %xmm2     # xmm2 = xmm2[0,1,2,3,7,6,5,4]
punpcklbw %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3],xmm1[4],xmm0[4],xmm1[5],xmm0[5],xmm1[6],xmm0[6],xmm1[7],xmm0[7]
pshuflw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[0,1,2,3,7,6,5,4]
packuswb %xmm2, %xmm1
movdqa %xmm1, 0x10(%rsp,%rcx,4)
addq $0x4, %rcx
cmpq $0x8, %rcx
jne 0x23986
movl %r15d, 0xc(%rsp)
testl %eax, %eax
js 0x239f3
bswapl %r15d
leaq 0xc(%rsp), %rsi
movl %r15d, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x23c4c
xorl %ecx, %ecx
testq %rax, %rax
setns %cl
jmp 0x239f5
movb $0x1, %r15b
jmp 0x23abe
xorl %ecx, %ecx
cmpl $0x0, 0x14(%rbx)
je 0x23a18
movzwl 0x12(%rbx), %eax
addq 0x38(%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
bswapl %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
movl 0x14(%rbx), %esi
cmpq %rsi, %rdx
jb 0x23a05
movb $0x1, %r15b
testl %ecx, %ecx
je 0x23abe
testb $0x40, 0x3(%r14)
je 0x23a5b
movslq 0x10(%r14), %rax
leal -0xff01(%rax), %ecx
cmpl $-0x22, %ecx
ja 0x23a5b
movq 0x28(%r14), %rcx
movdqa 0x10(%rsp), %xmm0
movdqa 0x20(%rsp), %xmm1
movdqu %xmm1, 0x10(%rcx,%rax)
movdqu %xmm0, (%rcx,%rax)
addl $0x20, 0x10(%r14)
jmp 0x23a72
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %r14, %rdi
callq 0xd005
testq %rax, %rax
js 0x23abe
movq 0x38(%rbx), %rsi
movzwl 0x12(%rbx), %r15d
movzbl 0xf(%rbx), %eax
subq %rax, %r15
testb $0x40, 0x3(%r14)
je 0x23aac
movslq 0x10(%r14), %rdi
movl $0xff00, %eax           # imm = 0xFF00
subq %rdi, %rax
cmpq %rax, %r15
jae 0x23aac
addq 0x28(%r14), %rdi
movq %r15, %rdx
callq 0x43c0
addl %r15d, 0x10(%r14)
jmp 0x23aba
movq %r14, %rdi
movq %r15, %rdx
callq 0xd005
movq %rax, %r15
shrq $0x3f, %r15
movl 0x14(%rbx), %r13d
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x23afe
xorl %ebp, %ebp
testb %r15b, %r15b
jne 0x23c0e
movzwl 0x12(%rbx), %eax
movq 0x38(%rbx), %rsi
addq %rax, %rsi
movslq 0x40(%rbx), %rdx
subq %rax, %rdx
movq %r14, %rdi
callq 0x23c4c
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x23c0e
movzwl 0x12(%rbx), %r12d
xorl %ebp, %ebp
testl %r13d, %r13d
jle 0x23b6d
movq 0x38(%rbx), %rax
leaq (%rax,%r12), %rdx
xorl %esi, %esi
xorl %r9d, %r9d
movl (%rdx,%rsi,4), %edi
leal (%rdi,%rdi), %ecx
movl $0x3c1a7, %r8d          # imm = 0x3C1A7
shrl %cl, %r8d
shll $0x1e, %r8d
sarl $0x1f, %r8d
shrl $0x4, %edi
andl %r8d, %edi
addq %rdi, %r9
incq %rsi
cmpq %rsi, %r13
jne 0x23b17
cmpq $0xfffffff, %r9         # imm = 0xFFFFFFF
jbe 0x23b70
leaq 0x73cdd(%rip), %rsi     # 0x9782c
leaq 0x73d36(%rip), %rdx     # 0x9788c
movl $0x1, %edi
movq %rax, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x13de5
jmp 0x238e3
xorl %r9d, %r9d
movl 0x18(%rbx), %eax
shll $0x4, %eax
orl $0x4, %eax
shll $0x4, %r9d
orl $0x3, %r9d
movl %eax, 0x4(%rsp)
movl %r9d, 0x8(%rsp)
testb %r15b, %r15b
jne 0x23c0e
leaq 0x4(%rsp), %rsi
movl $0x8, %edx
movq %r14, %rdi
callq 0x23c4c
testq %rax, %rax
js 0x23c0e
leal (%r12,%r13,4), %eax
movl %eax, %esi
addq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
subl %eax, %edx
movq %r14, %rdi
callq 0x23c4c
testq %rax, %rax
js 0x23c0e
leaq 0x73d2f(%rip), %rsi     # 0x978f8
movl $0x4, %edx
movq %r14, %rdi
callq 0x23c4c
movl 0x14(%rbx), %ecx
movl %ecx, 0x4(%rsp)
testq %rax, %rax
js 0x23c0e
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x23c4c
testq %rax, %rax
js 0x23c0e
addq 0x38(%rbx), %r12
movl 0x14(%rbx), %edx
shll $0x2, %edx
movq %r14, %rdi
movq %r12, %rsi
jmp 0x23aeb
testb $0x8, 0x2(%r14)
je 0x23c38
cmpl $0x0, 0x14(%rbx)
je 0x23c38
movzwl 0x12(%rbx), %eax
addq 0x38(%rbx), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
bswapl %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
movl 0x14(%rbx), %edx
cmpq %rdx, %rcx
jb 0x23c25
movl (%rsp), %ecx
addl $0x4, %ecx
xorl %eax, %eax
cmpl $0x1, %ebp
sbbl %eax, %eax
orl %ecx, %eax
jmp 0x238e8