// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam.c
// Function: bam_tag2cigar
static int bam_tag2cigar(bam1_t *b, int recal_bin, int give_warning) // return 0 if CIGAR is untouched; 1 if CIGAR is updated with CG
{
    bam1_core_t *c = &b->core;

    // Bail out as fast as possible for the easy case
    uint32_t test_CG = BAM_CSOFT_CLIP | (c->l_qseq << BAM_CIGAR_SHIFT);
    if (c->n_cigar == 0 || test_CG != *bam_get_cigar(b))
        return 0;

    // The above isn't fool proof - we may have old CIGAR tags that aren't used,
    // but this is much less likely so do as a secondary check.
    if (c->tid < 0 || c->pos < 0)
        return 0;

    // Do we have a CG tag?
    uint8_t *CG = bam_aux_get(b, "CG");
    int saved_errno = errno;
    if (!CG) {
        if (errno != ENOENT) return -1;  // Bad aux data
        errno = saved_errno; // restore errno on expected no-CG-tag case
        return 0;
    }

    // Now we start with the serious work migrating CG to CIGAR
    uint32_t cigar_st, n_cigar4, CG_st, CG_en, ori_len = b->l_data,
        *cigar0, CG_len, fake_bytes;
    cigar0 = bam_get_cigar(b);
    fake_bytes = c->n_cigar * 4;
    if (CG[0] != 'B' || !(CG[1] == 'I' || CG[1] == 'i'))
        return 0; // not of type B,I
    CG_len = le_to_u32(CG + 2);
    // don't move if the real CIGAR length is shorter than the fake cigar length
    if (CG_len < c->n_cigar || CG_len >= 1U<<29) return 0;

    // move from the CG tag to the right position
    cigar_st = (uint8_t*)cigar0 - b->data;
    c->n_cigar = CG_len;
    n_cigar4 = c->n_cigar * 4;
    CG_st = CG - b->data - 2;
    CG_en = CG_st + 8 + n_cigar4;
    if (possibly_expand_bam_data(b, n_cigar4 - fake_bytes) < 0) return -1;
    // we need c->n_cigar-fake_bytes bytes to swap CIGAR to the right place
    b->l_data = b->l_data - fake_bytes + n_cigar4;
    // insert c->n_cigar-fake_bytes empty space to make room
    memmove(b->data + cigar_st + n_cigar4, b->data + cigar_st + fake_bytes, ori_len - (cigar_st + fake_bytes));
    // copy the real CIGAR to the right place; -fake_bytes for the fake CIGAR
    memcpy(b->data + cigar_st, b->data + (n_cigar4 - fake_bytes) + CG_st + 8, n_cigar4);
    if (ori_len > CG_en) // move data after the CG tag
        memmove(b->data + CG_st + n_cigar4 - fake_bytes, b->data + CG_en + n_cigar4 - fake_bytes, ori_len - CG_en);
    b->l_data -= n_cigar4 + 8; // 8: CGBI (4 bytes) and CGBI length (4)
    if (recal_bin)
        b->core.bin = hts_reg2bin(b->core.pos, bam_endpos(b), 14, 5);
    if (give_warning)
        hts_log_warning("%s encodes a CIGAR with %d operators at the CG tag", bam_get_qname(b), c->n_cigar);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %r12d, %r12d
cmpl $0x0, 0x14(%rdi)
je 0x241ba
movl %edx, %ebp
movq %rdi, %rbx
movl 0x18(%rdi), %eax
shll $0x4, %eax
orl $0x4, %eax
movq 0x38(%rdi), %rcx
movzwl 0x12(%rdi), %edx
cmpl (%rcx,%rdx), %eax
jne 0x241ba
cmpl $0x0, 0x8(%rbx)
js 0x241ba
cmpq $0x0, (%rbx)
js 0x241ba
movl %esi, %r14d
leaq 0x76f89(%rip), %rsi     # 0x9af8f
movq %rbx, %rdi
callq 0x2c960
testq %rax, %rax
je 0x241a8
movq %rax, %r15
cmpb $0x42, (%rax)
jne 0x241ba
movzbl 0x1(%r15), %eax
orl $0x20, %eax
cmpl $0x69, %eax
jne 0x241ba
movl 0x14(%rbx), %r13d
movl 0x2(%r15), %eax
cmpl %r13d, %eax
setb %cl
cmpl $0x20000000, %eax       # imm = 0x20000000
setae %dl
orb %cl, %dl
jne 0x241ba
movl 0x40(%rbx), %ecx
movq %rcx, 0x30(%rsp)
movq 0x38(%rbx), %rcx
movq %rcx, 0x8(%rsp)
movzwl 0x12(%rbx), %ecx
movq %rcx, 0x28(%rsp)
shll $0x2, %r13d
movl %eax, 0x14(%rbx)
leaq (,%rax,4), %r12
movq %r12, 0x10(%rsp)
subl %r13d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x28ac4
testl %eax, %eax
js 0x241cc
subq 0x8(%rsp), %r15
movq 0x10(%rsp), %rcx
leal (%r15,%rcx), %eax
addl $0x6, %eax
movl %eax, 0x8(%rsp)
addl $-0x2, %r15d
addl %r12d, 0x40(%rbx)
movq 0x38(%rbx), %rsi
movq %r12, 0x20(%rsp)
movq 0x28(%rsp), %r12
addq %r12, %rsi
leaq (%rsi,%rcx), %rdi
movl %r13d, %eax
movq %rax, 0x18(%rsp)
addq %rax, %rsi
leal (%r12,%r13), %eax
movq 0x30(%rsp), %r13
movl %r13d, %edx
subl %eax, %edx
callq 0x4830
movq 0x38(%rbx), %rax
addq %rax, %r12
movq 0x20(%rsp), %rcx
addq %rcx, %rax
leaq 0x8(%r15,%rax), %rsi
movq %r12, %rdi
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x43c0
movl 0x8(%rsp), %eax
subl %eax, %r13d
jbe 0x2413b
movq 0x38(%rbx), %rsi
addq %rsi, %r15
addq %r12, %r15
movq 0x18(%rsp), %rcx
subq %rcx, %r15
movl %eax, %eax
addq %r12, %rsi
addq %rax, %rsi
subq %rcx, %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x4830
movl 0x40(%rbx), %eax
subl %r12d, %eax
addl $-0x8, %eax
movl %eax, 0x40(%rbx)
testl %r14d, %r14d
je 0x241db
movq (%rbx), %r14
movq %rbx, %rdi
callq 0x23b0a
decq %rax
movl $0x1249, %edi           # imm = 0x1249
movl $0xe, %edx
movl $0x6, %r8d
movl $0xc, %esi
movq %r14, %r9
movl %edx, %ecx
sarq %cl, %r9
movq %rax, %r10
sarq %cl, %r10
cmpq %r10, %r9
je 0x241d4
addq $0x3, %rdx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %r9d
addl %r9d, %edi
decl %r8d
addl $-0x3, %esi
cmpl $0x1, %r8d
ja 0x24173
xorl %edi, %edi
jmp 0x241d7
callq 0x4050
xorl %r12d, %r12d
cmpl $0x2, (%rax)
setne %r12b
negl %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x241ba
addl %r9d, %edi
movw %di, 0xc(%rbx)
movl $0x1, %r12d
testl %ebp, %ebp
je 0x241ba
movq 0x38(%rbx), %rcx
movl 0x14(%rbx), %r8d
leaq 0x76d9e(%rip), %rsi     # 0x9af92
leaq 0x76da5(%rip), %rdx     # 0x9afa0
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x241ba