// File: /workspace/github_cpp_makefile_star2_O1/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 0x2d43c
xorl %r14d, %r14d
jmp 0x2d609
movl %esi, %r14d
movq %rdi, %r15
movl $0x5844493d, 0x8(%rsp)  # imm = 0x5844493D
testl %edx, %edx
jne 0x2d458
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 %r14d, %r14d
js 0x2d59c
movl %r14d, %edx
incl %r14d
movq %r14, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x7(%rsp), %r13
xorl %r14d, %r14d
movq %r15, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
cmpq %rdx, %r12
je 0x2d4c3
movzbl (%r15,%r12), %eax
movb 0x6(%rsp), %cl
cmpb %cl, 0x8(%rsp,%rax)
sete %al
testb %cl, %cl
sete %cl
orb %al, %cl
jne 0x2d579
xorl %r15d, %r15d
testl %r14d, %r14d
je 0x2d50b
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 0x24ea2
incl %r15d
addl $0x9, %ebp
cmpl $0x12, %ebp
ja 0x2d4cb
cmpl $0x2, %r15d
jb 0x2d53d
movq 0x8(%rbx), %rax
leaq -0x1(%rax), %rcx
movl %r15d, %edx
subq %rdx, %rax
incq %rax
movb -0x1(%rax), %dl
movb (%rcx), %sil
movb %sil, -0x1(%rax)
movb %dl, (%rcx)
decq %rcx
leaq 0x1(%rax), %rdx
cmpq %rcx, %rax
movq %rdx, %rax
jb 0x2d522
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x2d553
movb 0x6(%rsp), %al
movb %al, (%rsi)
incq 0x8(%rbx)
jmp 0x2d560
movq %rbx, %rdi
leaq 0x6(%rsp), %rdx
callq 0x2d948
movq 0x10(%rsp), %r15
xorl %r14d, %r14d
movq 0x18(%rsp), %rdx
cmpq %rdx, %r12
jae 0x2d579
cmpb $0x4, (%r15,%r12)
jae 0x2d5e5
cmpq %rdx, %r12
jae 0x2d58e
movzbl (%r15,%r12), %eax
movb 0x8(%rsp,%rax), %al
movb %al, 0x6(%rsp)
incl %r14d
incq %r12
cmpq 0x20(%rsp), %r12
jne 0x2d4a1
leaq 0xf(%rsp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
callq 0x24ea2
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 0x2d5fc
movq 0x10(%rbx), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x63c0
jmp 0x2d5fc
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2d5f9
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