// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, 0x10(%rsp)
cmpl $0x1, %edx
jbe 0x247f5
xorl %r12d, %r12d
jmp 0x2493f
movl %esi, %ebp
movl $0x5844493d, 0x8(%rsp)  # imm = 0x5844493D
testl %edx, %edx
jne 0x2480d
movb $0x4d, %al
movb %al, 0xb(%rsp)
movb %al, 0x8(%rsp)
pushq $0x18
popq %rdi
callq 0x63d0
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x6(%rsp), %rax
movb $0x0, (%rax)
movslq %ebp, %rdx
movl %edx, %eax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
pushq $0xa
popq %r15
leaq 0x7(%rsp), %r13
xorl %r12d, %r12d
movq %rdx, 0x18(%rsp)
cmpq %rdx, %r14
jg 0x248f9
cmpq 0x20(%rsp), %r14
je 0x24877
movq 0x10(%rsp), %rax
movzbl (%rax,%r14), %eax
movb 0x6(%rsp), %cl
cmpb %cl, 0x8(%rsp,%rax)
sete %al
testb %cl, %cl
sete %cl
orb %al, %cl
jne 0x248d7
xorl %ebp, %ebp
testl %r12d, %r12d
je 0x2489f
movl %r12d, %eax
cltd
idivl %r15d
addb $0x30, %dl
movb %dl, 0x7(%rsp)
movl %eax, %r12d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1d5d8
decq %rbp
jmp 0x24879
movq 0x8(%rbx), %rsi
addq %rsi, %rbp
movq %rbp, %rdi
callq 0x24eeb
movq %rbx, %rdi
leaq 0x6(%rsp), %rsi
callq 0x24bd8
xorl %r12d, %r12d
movq 0x18(%rsp), %rdx
cmpq %rdx, %r14
jge 0x248d7
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
cmpb $0x3, (%rax,%r14)
ja 0x2492c
cmpq %rdx, %r14
jge 0x248f1
movq 0x10(%rsp), %rax
movzbl (%rax,%r14), %eax
movb 0x8(%rsp,%rax), %al
movb %al, 0x6(%rsp)
incl %r12d
incq %r14
jmp 0x24849
leaq 0xf(%rsp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
callq 0x1d5d8
movq (%rbx), %r14
movq 0x8(%rbx), %r15
subq %r14, %r15
movq %r15, %rdi
callq 0x6550
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6350
movq %rbx, %rdi
callq 0x1a56a
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x63e0
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq