// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: bam_plp_insertion_mod
int bam_plp_insertion_mod(const bam_pileup1_t *p,
                          hts_base_mod_state *m,
                          kstring_t *ins, int *del_len) {
    int j, k, indel, nb = 0;
    uint32_t *cigar;

    if (p->indel <= 0) {
        if (ks_resize(ins, 1) < 0)
            return -1;
        ins->l = 0;
        ins->s[0] = '\0';
        return 0;
    }

    if (del_len)
        *del_len = 0;

    // Measure indel length including pads
    indel = 0;
    k = p->cigar_ind+1;
    cigar = bam_get_cigar(p->b);
    while (k < p->b->core.n_cigar) {
        switch (cigar[k] & BAM_CIGAR_MASK) {
        case BAM_CPAD:
        case BAM_CINS:
            indel += (cigar[k] >> BAM_CIGAR_SHIFT);
            break;
        default:
            k = p->b->core.n_cigar;
            break;
        }
        k++;
    }
    nb = ins->l = indel;

    // Produce sequence
    if (ks_resize(ins, indel+1) < 0)
        return -1;
    indel = 0;
    k = p->cigar_ind+1;
    j = 1;
    while (k < p->b->core.n_cigar) {
        int l, c;
        switch (cigar[k] & BAM_CIGAR_MASK) {
        case BAM_CPAD:
            for (l = 0; l < (cigar[k]>>BAM_CIGAR_SHIFT); l++)
                ins->s[indel++] = '*';
            break;
        case BAM_CINS:
            for (l = 0; l < (cigar[k]>>BAM_CIGAR_SHIFT); l++, j++) {
                c = p->qpos + j - p->is_del < p->b->core.l_qseq
                    ? seq_nt16_str[bam_seqi(bam_get_seq(p->b),
                                            p->qpos + j - p->is_del)]
                    : 'N';
                ins->s[indel++] = c;
                int nm;
                hts_base_mod mod[256];
                if (m && (nm = bam_mods_at_qpos(p->b, p->qpos + j - p->is_del,
                                                m, mod, 256)) > 0) {
                    int o_indel = indel;
                    if (ks_resize(ins, ins->l + nm*16+3) < 0)
                        return -1;
                    ins->s[indel++] = '[';
                    int j;
                    for (j = 0; j < nm; j++) {
                        char qual[20];
                        if (mod[j].qual >= 0)
                            snprintf(qual, sizeof(qual), "%d", mod[j].qual);
                        else
                            *qual=0;
                        if (mod[j].modified_base < 0)
                            // ChEBI
                            indel += snprintf(&ins->s[indel], ins->m - indel,
                                              "%c(%d)%s",
                                              "+-"[mod[j].strand],
                                              -mod[j].modified_base,
                                              qual);
                        else
                            indel += snprintf(&ins->s[indel], ins->m - indel,
                                              "%c%c%s",
                                              "+-"[mod[j].strand],
                                              mod[j].modified_base,
                                              qual);
                    }
                    ins->s[indel++] = ']';
                    ins->l += indel - o_indel; // grow by amount we used
                }
            }
            break;
        case BAM_CDEL:
            // eg cigar 1M2I1D gives mpileup output in T+2AA-1C style
            if (del_len)
                *del_len = cigar[k]>>BAM_CIGAR_SHIFT;
            // fall through
        default:
            k = p->b->core.n_cigar;
            break;
        }
        k++;
    }
    ins->s[indel] = '\0';
    ins->l = indel; // string length

    return nb;      // base length
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1068, %rsp           # imm = 0x1068
movq %rdx, %rbx
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0xc(%rdi)
jle 0x2fa45
movq %rcx, %r15
movq %rsi, %r14
testq %rcx, %rcx
je 0x2f6f0
movl $0x0, (%r15)
movq 0x8(%rsp), %rax
movl 0x20(%rax), %r8d
movq (%rax), %rax
movzwl 0x12(%rax), %ecx
addq 0x38(%rax), %rcx
movq %rcx, 0x10(%rsp)
movl 0x14(%rax), %ecx
incl %r8d
xorl %ebp, %ebp
cmpl %ecx, %r8d
jae 0x2f744
xorl %ebp, %ebp
movl %r8d, %edx
movslq %edx, %rsi
movq 0x10(%rsp), %rdi
movl (%rdi,%rsi,4), %esi
movl %esi, %edi
andl $0xf, %edi
cmpl $0x6, %edi
je 0x2f735
cmpl $0x1, %edi
jne 0x2f73c
shrl $0x4, %esi
addl %esi, %ebp
jmp 0x2f73e
movl %ecx, %edx
incl %edx
cmpl %ecx, %edx
jb 0x2f71b
movslq %ebp, %rdx
movq %rdx, (%rbx)
incl %edx
movslq %edx, %rdx
cmpq %rdx, 0x8(%rbx)
movq %r14, 0x20(%rsp)
jae 0x2f7a1
movq %rdx, %rax
shrq %rax
xorl %r14d, %r14d
cmpl $-0x1, %ebp
cmovgeq %rax, %r14
addq %rdx, %r14
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x4840
testq %rax, %rax
je 0x2fa7c
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq 0x8(%rsp), %rax
movl 0x20(%rax), %r8d
movq (%rax), %rax
movl 0x14(%rax), %ecx
incl %r8d
movq 0x20(%rsp), %r14
xorl %edx, %edx
cmpl %ecx, %r8d
jae 0x2fa38
movl %ebp, 0x18(%rsp)
addq $0x14, %rax
movl $0x1, %r9d
leaq 0x40(%rsp), %r13
xorl %r12d, %r12d
movq 0x10(%rsp), %rdi
movq %r15, 0x30(%rsp)
movl %r8d, 0x4(%rsp)
movslq %r8d, %r8
movl (%rdi,%r8,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x6, %esi
je 0x2f9eb
cmpl $0x2, %esi
je 0x2f9d6
cmpl $0x1, %esi
jne 0x2f9e6
cmpl $0x10, %edx
jb 0x2fa11
xorl %r15d, %r15d
movq %r8, 0x38(%rsp)
movl %r12d, %esi
movq 0x8(%rsp), %rdx
movl 0x8(%rdx), %ecx
addl %r9d, %ecx
movl 0x14(%rdx), %eax
andl $0x1, %eax
subl %eax, %ecx
movq (%rdx), %rdx
movb $0x4e, %al
cmpl 0x18(%rdx), %ecx
jge 0x2f85b
movl 0x14(%rdx), %eax
shll $0x2, %eax
addq 0x38(%rdx), %rax
movzwl 0x12(%rdx), %edx
addq %rax, %rdx
movl %ecx, %eax
sarl %eax
cltq
movzbl (%rax,%rdx), %eax
shll $0x2, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %eax
andl $0xf, %eax
leaq 0x66208(%rip), %rcx     # 0x95a60
movb (%rcx,%rax), %al
movslq %esi, %rcx
leaq 0x1(%rcx), %r12
movq 0x10(%rbx), %rdx
movb %al, (%rdx,%rcx)
testq %r14, %r14
movl %r9d, 0x1c(%rsp)
je 0x2f9aa
movq %rsi, 0x28(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
addl %r9d, %esi
movl 0x14(%rax), %eax
andl $0x1, %eax
subl %eax, %esi
movq %r14, %rdx
leaq 0x60(%rsp), %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x3493a
testl %eax, %eax
jle 0x2f9aa
movl %eax, %ebp
movl %eax, %ecx
shll $0x4, %ecx
orl $0x3, %ecx
addq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq %rcx, 0x8(%rbx)
jae 0x2f8fd
movq %rcx, %rdx
shrq $0x3e, %rdx
movq %rcx, %rsi
shrq %rsi
testq %rdx, %rdx
movl $0x0, %r14d
cmoveq %rsi, %r14
addq %rcx, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x4840
testq %rax, %rax
je 0x2fa7c
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movb $0x5b, (%rax,%r12)
movq 0x28(%rsp), %rax
leal 0x2(%rax), %r12d
movl %ebp, %r14d
shlq $0x4, %r14
xorl %ebp, %ebp
movl 0x6c(%rsp,%rbp), %ecx
testl %ecx, %ecx
js 0x2f934
movl $0x14, %esi
movq %r13, %rdi
leaq 0x65dc4(%rip), %rdx     # 0x956ef
xorl %eax, %eax
callq 0x4800
jmp 0x2f939
movb $0x0, 0x40(%rsp)
movl 0x60(%rsp,%rbp), %r8d
movslq %r12d, %rax
movq 0x10(%rbx), %rdi
addq %rax, %rdi
movq 0x8(%rbx), %rsi
subq %rax, %rsi
movslq 0x68(%rsp,%rbp), %rax
leaq 0x6857b(%rip), %rcx     # 0x97ed6
movsbl (%rax,%rcx), %ecx
testl %r8d, %r8d
js 0x2f96d
leaq 0x6856e(%rip), %rdx     # 0x97ed9
jmp 0x2f977
negl %r8d
leaq 0x68556(%rip), %rdx     # 0x97ecd
movq %r13, %r9
xorl %eax, %eax
callq 0x4800
addl %eax, %r12d
addq $0x10, %rbp
cmpq %rbp, %r14
jne 0x2f914
movq 0x10(%rbx), %rax
movslq %r12d, %rcx
incl %r12d
movb $0x5d, (%rax,%rcx)
subl 0x28(%rsp), %ecx
movslq %ecx, %rax
addq %rax, (%rbx)
movq 0x20(%rsp), %r14
incl %r15d
movl 0x1c(%rsp), %r9d
incl %r9d
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %r8
movl (%rdi,%r8,4), %eax
shrl $0x4, %eax
cmpl %eax, %r15d
jb 0x2f809
movq 0x30(%rsp), %r15
jmp 0x2fa11
testq %r15, %r15
je 0x2f9e6
shrl $0x4, %edx
movl %edx, (%r15)
movl (%rax), %r8d
jmp 0x2fa16
movl %ecx, %r8d
jmp 0x2fa16
cmpl $0x10, %edx
jb 0x2fa11
movslq %r12d, %rcx
xorl %eax, %eax
movq 0x10(%rbx), %rdx
addq %rcx, %rdx
movb $0x2a, (%rax,%rdx)
movl (%rdi,%r8,4), %edx
shrl $0x4, %edx
incq %rax
cmpl %edx, %eax
jb 0x2f9f5
addl %eax, %r12d
movl 0x4(%rsp), %r8d
incl %r8d
movq 0x8(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rax), %ecx
addq $0x14, %rax
cmpl %ecx, %r8d
jb 0x2f7cc
movslq %r12d, %rdx
movl 0x18(%rsp), %ebp
movq 0x10(%rbx), %rax
movb $0x0, (%rax,%rdx)
movq %rdx, (%rbx)
jmp 0x2fa81
cmpq $0x0, 0x8(%rbx)
movq 0x10(%rbx), %rax
jne 0x2fa6e
movl $0x1, %esi
movq %rax, %rdi
callq 0x4840
testq %rax, %rax
je 0x2fa7c
movq %rax, 0x10(%rbx)
movq $0x1, 0x8(%rbx)
movq $0x0, (%rbx)
movb $0x0, (%rax)
xorl %ebp, %ebp
jmp 0x2fa81
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x1068, %rsp           # imm = 0x1068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq