// File: /workspace/github_c_makefile_star2_O1/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), %ecx
movl %ecx, (%rsp)
movzwl 0x12(%rsi), %ecx
subl %eax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
jl 0x24262
movq 0x38(%rbx), %rcx
leaq 0x765da(%rip), %rsi     # 0x9a81c
leaq 0x765de(%rip), %rdx     # 0x9a827
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
callq 0x4050
movl $0x4b, (%rax)
jmp 0x242b4
movq %rdi, %r14
movl 0x14(%rbx), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x24275
addl $0x30, %edx
movl %edx, (%rsp)
movq (%rbx), %rdi
cmpq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
jg 0x2429a
movq 0x20(%rbx), %rdx
cmpq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
jg 0x2429a
movq 0x28(%rbx), %rsi
movslq %esi, %r8
cmpq %rsi, %r8
je 0x242c8
leaq 0x7657b(%rip), %rsi     # 0x9a81c
leaq 0x765a8(%rip), %rdx     # 0x9a850
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
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)
movl 0x18(%rbx), %eax
movl %eax, 0x20(%rsp)
movl 0x1c(%rbx), %eax
movl %eax, 0x24(%rsp)
movl %edx, 0x28(%rsp)
movl %esi, 0x2c(%rsp)
movl (%rsp), %esi
addl $0x4, %esi
movq %r14, %rdi
callq 0xd091
movl %eax, %ecx
notl %ecx
shrl $0x1f, %ecx
testb $0x8, 0x2(%r14)
jne 0x24358
testl %eax, %eax
js 0x243b9
movq %rsp, %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x24620
xorl %ecx, %ecx
testq %rax, %rax
setns %cl
jmp 0x243b9
xorl %edx, %edx
movl 0x10(%rsp,%rdx,4), %esi
bswapl %esi
movl %esi, 0x10(%rsp,%rdx,4)
incq %rdx
cmpq $0x8, %rdx
jne 0x2435a
movl (%rsp), %edx
movl %edx, 0xc(%rsp)
testl %eax, %eax
js 0x24396
bswapl %edx
leaq 0xc(%rsp), %rsi
movl %edx, (%rsi)
movl $0x4, %edx
movq %r14, %rdi
callq 0x24620
xorl %ecx, %ecx
testq %rax, %rax
setns %cl
cmpl $0x0, 0x14(%rbx)
je 0x243b9
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 0x243a6
xorl %ebp, %ebp
testl %ecx, %ecx
je 0x2445f
testb $0x40, 0x3(%r14)
je 0x243f7
movslq 0x10(%r14), %rax
leal -0xff01(%rax), %ecx
cmpl $-0x22, %ecx
ja 0x243f7
movq 0x28(%r14), %rcx
movaps 0x10(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movups %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
addl $0x20, 0x10(%r14)
jmp 0x2440e
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %r14, %rdi
callq 0xd0bb
testq %rax, %rax
js 0x2445f
movq 0x38(%rbx), %rsi
movzwl 0x12(%rbx), %r15d
movzbl 0xf(%rbx), %eax
subq %rax, %r15
testb $0x40, 0x3(%r14)
je 0x24448
movslq 0x10(%r14), %rdi
movl $0xff00, %eax           # imm = 0xFF00
subq %rdi, %rax
cmpq %rax, %r15
jae 0x24448
addq 0x28(%r14), %rdi
movq %r15, %rdx
callq 0x43c0
addl %r15d, 0x10(%r14)
jmp 0x24456
movq %r14, %rdi
movq %r15, %rdx
callq 0xd0bb
movq %rax, %r15
xorl %ebp, %ebp
testq %r15, %r15
setns %bpl
movl 0x14(%rbx), %r13d
cmpq $0xffff, %r13           # imm = 0xFFFF
ja 0x24498
testl %ebp, %ebp
je 0x244d9
movzwl 0x12(%rbx), %eax
movq 0x38(%rbx), %rsi
addq %rax, %rsi
movslq 0x40(%rbx), %rdx
subq %rax, %rdx
movq %r14, %rdi
callq 0x24620
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x245e2
movq 0x38(%rbx), %rax
movzwl 0x12(%rbx), %r12d
testl %r13d, %r13d
jle 0x244e0
leaq (%rax,%r12), %rdx
xorl %esi, %esi
xorl %r15d, %r15d
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, %r15
incq %rsi
cmpq %rsi, %r13
jne 0x244af
jmp 0x244e3
xorl %ebp, %ebp
jmp 0x245e2
xorl %r15d, %r15d
cmpq $0x10000000, %r15       # imm = 0x10000000
jge 0x245a2
movl 0x18(%rbx), %eax
shll $0x4, %eax
orl $0x4, %eax
movl %r15d, %ecx
shll $0x4, %ecx
orl $0x3, %ecx
movl %eax, 0x4(%rsp)
movl %ecx, 0x8(%rsp)
testl %ebp, %ebp
je 0x245c7
leaq 0x4(%rsp), %rsi
movl $0x8, %edx
movq %r14, %rdi
callq 0x24620
testq %rax, %rax
js 0x245c7
leal (%r12,%r13,4), %eax
movl %eax, %esi
addq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
subl %eax, %edx
movq %r14, %rdi
callq 0x24620
testq %rax, %rax
js 0x245c7
leaq 0x76398(%rip), %rsi     # 0x9a8e8
movl $0x4, %edx
movq %r14, %rdi
callq 0x24620
xorl %ebp, %ebp
movl 0x14(%rbx), %ecx
movl %ecx, 0x4(%rsp)
testq %rax, %rax
js 0x245d0
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x24620
testq %rax, %rax
js 0x245d0
addq 0x38(%rbx), %r12
movl 0x14(%rbx), %edx
shll $0x2, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x24620
xorl %ebp, %ebp
testq %rax, %rax
setns %bpl
jmp 0x245d0
leaq 0x76273(%rip), %rsi     # 0x9a81c
leaq 0x762cc(%rip), %rdx     # 0x9a87c
movl $0x1, %edi
movq %rax, %rcx
movl %r13d, %r8d
movq %r15, %r9
xorl %eax, %eax
callq 0x14378
jmp 0x245d0
movl 0x14(%rbx), %eax
movl %eax, 0x4(%rsp)
xorl %ebp, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0xfffffff, %r15        # imm = 0xFFFFFFF
jg 0x242b9
testb $0x8, 0x2(%r14)
je 0x2460c
cmpl $0x0, 0x14(%rbx)
je 0x2460c
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 0x245f9
movl (%rsp), %ecx
addl $0x4, %ecx
xorl %eax, %eax
cmpl $0x1, %ebp
sbbl %eax, %eax
orl %ecx, %eax
jmp 0x242b9