// File: /workspace/github_c_makefile_star2_O1/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 $0x1078, %rsp           # imm = 0x1078
movq %rdx, %rbx
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0xc(%rdi)
jle 0x30786
movq %rcx, %r15
movq %rsi, %r14
testq %rcx, %rcx
je 0x30428
movl $0x0, (%r15)
movq 0x8(%rsp), %rcx
movl 0x20(%rcx), %eax
movq (%rcx), %rcx
movzwl 0x12(%rcx), %r12d
addq 0x38(%rcx), %r12
movl 0x14(%rcx), %ecx
incl %eax
xorl %ebp, %ebp
cmpl %ecx, %eax
jae 0x3046e
xorl %ebp, %ebp
movslq %eax, %rdx
movl (%r12,%rdx,4), %edx
movl %edx, %esi
andl $0xf, %esi
cmpl $0x6, %esi
je 0x3045f
cmpl $0x1, %esi
jne 0x30466
shrl $0x4, %edx
addl %edx, %ebp
jmp 0x30468
movl %ecx, %eax
incl %eax
cmpl %ecx, %eax
jb 0x30449
movslq %ebp, %rax
movq %rax, (%rbx)
incl %eax
cltq
cmpq %rax, 0x8(%rbx)
movq %r14, 0x18(%rsp)
jae 0x304b8
movq %rax, %rcx
shrq %rcx
xorl %r14d, %r14d
cmpl $-0x1, %ebp
cmovgeq %rcx, %r14
addq %rax, %r14
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x4840
testq %rax, %rax
je 0x307be
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq 0x18(%rsp), %r14
movl %ebp, 0x24(%rsp)
movq 0x8(%rsp), %rax
movl 0x20(%rax), %esi
incl %esi
xorl %edi, %edi
movl $0x1, %ebp
movq %r15, 0x28(%rsp)
movq %r12, 0x48(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpl 0x14(%rax), %esi
jae 0x307c5
movslq %esi, %r8
movl (%r12,%r8,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x6, %edx
je 0x3074d
cmpl $0x2, %edx
je 0x30731
cmpl $0x1, %edx
jne 0x3073c
cmpl $0x10, (%r12,%r8,4)
jb 0x3073f
movq %r8, 0x40(%rsp)
movl %esi, 0x14(%rsp)
xorl %r13d, %r13d
movq %rdi, %rsi
movq 0x8(%rsp), %rdx
movl 0x8(%rdx), %ecx
addl %ebp, %ecx
movl 0x14(%rdx), %eax
andl $0x1, %eax
subl %eax, %ecx
movq (%rdx), %rdx
movb $0x4e, %al
cmpl 0x18(%rdx), %ecx
jge 0x30577
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 0x6843c(%rip), %rcx     # 0x989b0
movb (%rcx,%rax), %al
movslq %esi, %rcx
leaq 0x1(%rcx), %r12
movq 0x10(%rbx), %rdx
movb %al, (%rdx,%rcx)
movb $0x1, %cl
testq %r14, %r14
je 0x306fd
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
addl %ebp, %esi
movl 0x14(%rax), %eax
andl $0x1, %eax
subl %eax, %esi
movq %r14, %rdx
leaq 0x70(%rsp), %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x35d45
testl %eax, %eax
jle 0x30614
movl %eax, %r15d
shll $0x4, %eax
orl $0x3, %eax
addq (%rbx), %rax
movb $0x1, %cl
cmpq %rax, 0x8(%rbx)
jae 0x30625
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
testq %rcx, %rcx
movl $0x0, %r14d
cmoveq %rdx, %r14
addq %rax, %r14
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x4840
testq %rax, %rax
je 0x3061e
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq 0x18(%rsp), %r14
movb $0x1, %cl
jmp 0x30625
movl %r12d, %esi
movb $0x1, %cl
jmp 0x30700
xorl %ecx, %ecx
movq 0x18(%rsp), %r14
testb %cl, %cl
je 0x306fd
movq %rcx, 0x38(%rsp)
movq 0x10(%rbx), %rax
movb $0x5b, (%rax,%r12)
movq 0x30(%rsp), %rax
leal 0x2(%rax), %r14d
cmpl $0x2, %r15d
movl $0x1, %eax
cmovll %eax, %r15d
shlq $0x4, %r15
xorl %r12d, %r12d
movl 0x7c(%rsp,%r12), %ecx
testl %ecx, %ecx
js 0x3067b
movl $0x14, %esi
leaq 0x50(%rsp), %rdi
leaq 0x6803d(%rip), %rdx     # 0x986af
xorl %eax, %eax
callq 0x4800
jmp 0x30680
movb $0x0, 0x50(%rsp)
movl 0x70(%rsp,%r12), %r8d
movslq %r14d, %rax
movq 0x10(%rbx), %rdi
addq %rax, %rdi
movq 0x8(%rbx), %rsi
subq %rax, %rsi
movslq 0x78(%rsp,%r12), %rax
leaq 0x6a821(%rip), %rcx     # 0x9aec3
movsbl (%rax,%rcx), %ecx
testl %r8d, %r8d
js 0x306b4
leaq 0x6a814(%rip), %rdx     # 0x9aec6
jmp 0x306be
negl %r8d
leaq 0x6a7fc(%rip), %rdx     # 0x9aeba
leaq 0x50(%rsp), %r9
xorl %eax, %eax
callq 0x4800
addl %eax, %r14d
addq $0x10, %r12
cmpq %r12, %r15
jne 0x30658
movq 0x10(%rbx), %rax
movslq %r14d, %rcx
incl %r14d
movb $0x5d, (%rax,%rcx)
subl 0x30(%rsp), %ecx
movslq %ecx, %rax
addq %rax, (%rbx)
movl %r14d, %esi
movq 0x18(%rsp), %r14
movq 0x38(%rsp), %rcx
jmp 0x30700
movl %r12d, %esi
testb %cl, %cl
movq 0x48(%rsp), %r12
je 0x30776
incl %r13d
incl %ebp
movq 0x40(%rsp), %rax
movl (%r12,%rax,4), %eax
shrl $0x4, %eax
cmpl %eax, %r13d
jb 0x30529
movq %rsi, %rdi
movq 0x28(%rsp), %r15
movl 0x14(%rsp), %esi
jmp 0x3073f
testq %r15, %r15
je 0x3073c
shrl $0x4, %ecx
movl %ecx, (%r15)
movl 0x14(%rax), %esi
incl %esi
movb $0x1, %al
testb %al, %al
jne 0x304d7
jmp 0x307be
cmpl $0x10, (%r12,%r8,4)
jb 0x3073f
movslq %edi, %rcx
xorl %eax, %eax
movq 0x10(%rbx), %rdx
addq %rcx, %rdx
movb $0x2a, (%rax,%rdx)
movl (%r12,%r8,4), %edx
shrl $0x4, %edx
incq %rax
cmpl %edx, %eax
jb 0x30759
addl %eax, %edi
jmp 0x3073f
movq %rsi, %rdi
xorl %eax, %eax
movq 0x28(%rsp), %r15
movl 0x14(%rsp), %esi
jmp 0x30743
cmpq $0x0, 0x8(%rbx)
jne 0x307ac
movq 0x10(%rbx), %rdi
movl $0x1, %esi
callq 0x4840
testq %rax, %rax
je 0x307be
movq %rax, 0x10(%rbx)
movq $0x1, 0x8(%rbx)
movq $0x0, (%rbx)
movq 0x10(%rbx), %rax
movb $0x0, (%rax)
xorl %eax, %eax
jmp 0x307d7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x307d7
movq 0x10(%rbx), %rax
movslq %edi, %rcx
movb $0x0, (%rax,%rcx)
movq %rcx, (%rbx)
movl 0x24(%rsp), %eax
addq $0x1078, %rsp           # imm = 0x1078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq