// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
shll $0x4, %eax
orl $0x4, %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x3427f
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq -0x10(%rbp), %rdx
movzwl 0x12(%rdx), %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx), %eax
je 0x3428b
movl $0x0, -0x4(%rbp)
jmp 0x34567
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jl 0x3429f
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jge 0x342ab
movl $0x0, -0x4(%rbp)
jmp 0x34567
movq -0x10(%rbp), %rdi
leaq 0xbe20a(%rip), %rsi     # 0xf24c0
callq 0x3e760
movq %rax, -0x30(%rbp)
callq 0x4050
movl (%rax), %eax
movl %eax, -0x34(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x34302
callq 0x4050
cmpl $0x2, (%rax)
je 0x342e6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x34567
movl -0x34(%rbp), %eax
movl %eax, -0x5c(%rbp)
callq 0x4050
movl -0x5c(%rbp), %ecx
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x34567
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
shll $0x2, %eax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x42, %eax
jne 0x34359
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x49, %eax
je 0x34365
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x69, %eax
je 0x34365
movl $0x0, -0x4(%rbp)
jmp 0x34567
movq -0x30(%rbp), %rdi
addq $0x2, %rdi
callq 0x32820
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jb 0x3438a
cmpl $0x20000000, -0x54(%rbp) # imm = 0x20000000
jb 0x34396
movl $0x0, -0x4(%rbp)
jmp 0x34567
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x38(%rbp)
movl -0x54(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
shll $0x2, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq %rcx, %rax
subq $0x2, %rax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x8, %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3a350
cmpl $0x0, %eax
jge 0x34405
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x34567
movq -0x10(%rbp), %rax
movl 0x40(%rax), %ecx
subl -0x58(%rbp), %ecx
addl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x38(%rbp), %eax
addq %rax, %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x38(%rbp), %eax
addq %rax, %rsi
movl -0x58(%rbp), %eax
addq %rax, %rsi
movl -0x48(%rbp), %eax
movl -0x38(%rbp), %ecx
addl -0x58(%rbp), %ecx
subl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x4810
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x38(%rbp), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x3c(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, %eax
addq %rax, %rsi
movl -0x40(%rbp), %eax
addq %rax, %rsi
addq $0x8, %rsi
movl -0x3c(%rbp), %eax
movl %eax, %edx
callq 0x43b0
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jbe 0x344e3
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movl -0x40(%rbp), %eax
addq %rax, %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
movl -0x58(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x44(%rbp), %eax
addq %rax, %rsi
movl -0x3c(%rbp), %eax
addq %rax, %rsi
movl -0x58(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x4810
movl -0x3c(%rbp), %edx
addl $0x8, %edx
movq -0x10(%rbp), %rax
movl 0x40(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x40(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x34530
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x33a20
movq -0x68(%rbp), %rdi
movq %rax, %rsi
movl $0xe, %edx
movl $0x5, %ecx
callq 0x33880
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
cmpl $0x0, -0x18(%rbp)
je 0x34560
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movl 0x14(%rax), %r8d
movl $0x3, %edi
leaq 0xbdf71(%rip), %rsi     # 0xf24c3
leaq 0xbdf78(%rip), %rdx     # 0xf24d1
movb $0x0, %al
callq 0x1b030
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq