// File: /workspace/github_cpp_makefile_star2_O3/VanAndelInstitute[P]intent/src/edlib.cpp
// Function: edlibAlignmentToCigar
char* edlibAlignmentToCigar(const unsigned char* const alignment, const int alignmentLength,
                                       const EdlibCigarFormat cigarFormat) {
    if (cigarFormat != EDLIB_CIGAR_EXTENDED && cigarFormat != EDLIB_CIGAR_STANDARD) {
        return 0;
    }

    // Maps move code from alignment to char in cigar.
    //                        0    1    2    3
    char moveCodeToChar[] = {'=', 'I', 'D', 'X'};
    if (cigarFormat == EDLIB_CIGAR_STANDARD) {
        moveCodeToChar[0] = moveCodeToChar[3] = 'M';
    }

    vector<char>* cigar = new vector<char>();
    char lastMove = 0;  // Char of last move. 0 if there was no previous move.
    int numOfSameMoves = 0;
    for (int i = 0; i <= alignmentLength; i++) {
        // if new sequence of same moves started
        if (i == alignmentLength || (moveCodeToChar[alignment[i]] != lastMove && lastMove != 0)) {
            // Write number of moves to cigar string.
            int numDigits = 0;
            for (; numOfSameMoves; numOfSameMoves /= 10) {
                cigar->push_back('0' + numOfSameMoves % 10);
                numDigits++;
            }
            reverse(cigar->end() - numDigits, cigar->end());
            // Write code of move to cigar string.
            cigar->push_back(lastMove);
            // If not at the end, start new sequence of moves.
            if (i < alignmentLength) {
                // Check if alignment has valid values.
                if (alignment[i] > 3) {
                    delete cigar;
                    return 0;
                }
                numOfSameMoves = 0;
            }
        }
        if (i < alignmentLength) {
            lastMove = moveCodeToChar[alignment[i]];
            numOfSameMoves++;
        }
    }
    cigar->push_back(0);  // Null character termination.
    char* cigar_ = static_cast<char *>(malloc(cigar->size() * sizeof(char)));
    memcpy(cigar_, &(*cigar)[0], cigar->size() * sizeof(char));
    delete cigar;

    return cigar_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x1, %edx
jbe 0x2d21a
xorl %r14d, %r14d
jmp 0x2d3f7
movl %esi, %r15d
movq %rdi, %rbp
movl $0x5844493d, 0x8(%rsp)  # imm = 0x5844493D
testl %edx, %edx
jne 0x2d236
movb $0x4d, %al
movb %al, 0xb(%rsp)
movb %al, 0x8(%rsp)
movl $0x18, %edi
callq 0x63b0
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movb $0x0, 0x6(%rsp)
testl %r15d, %r15d
js 0x2d38a
movl %r15d, %edx
incl %r15d
xorl %r12d, %r12d
leaq 0x7(%rsp), %r13
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
cmpq %rdx, %r12
je 0x2d2a2
movzbl (%rbp,%r12), %eax
movb 0x6(%rsp), %cl
cmpb %cl, 0x8(%rsp,%rax)
sete %al
testb %cl, %cl
sete %cl
orb %al, %cl
jne 0x2d368
testl %r14d, %r14d
je 0x2d330
xorl %eax, %eax
movq %rax, %r15
movslq %r14d, %rbp
imulq $0x66666667, %rbp, %r14 # imm = 0x66666667
movq %r14, %rax
shrq $0x3f, %rax
sarq $0x22, %r14
addl %eax, %r14d
leal (%r14,%r14), %eax
leal (%rax,%rax,4), %eax
movl %ebp, %ecx
subl %eax, %ecx
addb $0x30, %cl
movb %cl, 0x7(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x24dce
addl $0x9, %ebp
leaq 0x1(%r15), %rax
cmpl $0x12, %ebp
ja 0x2d2ad
movq 0x8(%rbx), %rsi
cmpl $0x1, %eax
jne 0x2d306
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %rbp
jmp 0x2d334
leaq -0x1(%rsi), %rax
subq %r15, %rsi
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %rbp
movb -0x1(%rsi), %cl
movb (%rax), %dl
movb %dl, -0x1(%rsi)
movb %cl, (%rax)
decq %rax
leaq 0x1(%rsi), %rcx
cmpq %rax, %rsi
movq %rcx, %rsi
jb 0x2d317
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x2d346
movb 0x6(%rsp), %al
movb %al, (%rsi)
incq 0x8(%rbx)
jmp 0x2d353
movq %rbx, %rdi
leaq 0x6(%rsp), %rdx
callq 0x2dd2e
xorl %r14d, %r14d
movq 0x20(%rsp), %rdx
cmpq %rdx, %r12
jae 0x2d368
cmpb $0x4, (%rbp,%r12)
jae 0x2d3d3
cmpq %rdx, %r12
jae 0x2d37e
movzbl (%rbp,%r12), %eax
movb 0x8(%rsp,%rax), %al
movb %al, 0x6(%rsp)
incl %r14d
incq %r12
cmpq %r15, %r12
jne 0x2d27f
leaq 0xf(%rsp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
callq 0x24dce
movq (%rbx), %r15
movq 0x8(%rbx), %r12
subq %r15, %r12
movq %r12, %rdi
callq 0x6530
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6310
testq %r15, %r15
je 0x2d3ea
movq 0x10(%rbx), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x63c0
jmp 0x2d3ea
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2d3e7
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x63c0
xorl %r14d, %r14d
movl $0x18, %esi
movq %rbx, %rdi
callq 0x63c0
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq