// File: /workspace/github_cpp_makefile_star2_O3/permutationlock[P]aes_math/lib/aes.cpp
// Function: decrypt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
string decrypt(string ciphertext, string key, int rounds){
    // Set up Galois Field
    GaloisPolynomial::globalSetModulus(rijndael_Mod);
    
    // Convert strings to unsigned characters
    vector<unsigned char> vciphertext(ciphertext.begin(), ciphertext.end());
    vector<unsigned char> vkey(key.begin(), key.end());
    
    // Expand key
    vector<QSMatrix<GaloisPolynomial>> keyMatrices = expandKey(vkey, rounds);
    
    // Convert ciphertext to matrix representation
    vector<GaloisPolynomial> sVec;
    sVec.reserve(16);
    for(int i=0; i<16; i++){
        sVec.push_back(extractPoly(vciphertext));
    }
    QSMatrix<GaloisPolynomial> state(4, 4, sVec);
    
    // Perform rounds on ciphertext
    addRoundKey(state, keyMatrices[rounds]);
    shiftRows_inverse(state);
    subBytes_inverse(state);
    for(int i=rounds-1; i>0; i--){
        addRoundKey(state, keyMatrices[i]);
        mixColumns_inverse(state);
        shiftRows_inverse(state);
        subBytes_inverse(state);
    }
    addRoundKey(state, keyMatrices[0]);
    
    // Convert back to string
    vector<unsigned char> vplaintext;
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            vplaintext.push_back(polyToChar(state(i,j)));
        }
    }
    string plaintext(vplaintext.begin(), vplaintext.end());
    
    return plaintext;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x89f9(%rip), %rdi      # 0xf148
callq 0x4c1e
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x7246
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x7246
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rax
movq %rax, %r15
subq %rsi, %r15
je 0x67bb
js 0x6a77
movq %r15, %rdi
callq 0x2100
movq %rax, %r14
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rax
movq %rax, %r12
subq %rsi, %r12
jmp 0x67c1
xorl %r14d, %r14d
movq %r15, %r12
movq %r14, 0x78(%rsp)
addq %r14, %r15
movq %r15, 0x88(%rsp)
cmpq %rsi, %rax
je 0x67e1
movq %r14, %rdi
movq %r12, %rdx
callq 0x2170
addq %r12, %r14
leaq 0x78(%rsp), %rsi
movq %r14, 0x8(%rsi)
leaq 0xb0(%rsp), %rdi
movl %ebp, %edx
callq 0x6042
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x6816
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x10, %esi
callq 0x713c
movl $0x10, %r13d
leaq 0x8(%rsp), %r14
leaq 0x90(%rsp), %r15
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
cmpq %rdi, %rdx
je 0x687c
movzbl (%rdi), %r12d
leaq 0x1(%rdi), %rsi
cmpq %rdx, %rsi
je 0x6872
subq %rsi, %rdx
callq 0x2170
movq 0x68(%rsp), %rdx
decq %rdx
movq %rdx, 0x68(%rsp)
jmp 0x687f
xorl %r12d, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl $0x2, %edx
movl $0x8, %ecx
callq 0x441a
movq %r15, %rdi
movq %r14, %rsi
callq 0x7ffc
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x68b6
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %r13d
jne 0x6849
leaq 0x8(%rsp), %rdi
leaq 0x90(%rsp), %rcx
movl $0x4, %esi
movl $0x4, %edx
callq 0x6bd2
movslq %ebp, %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0xb0(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x70b4
leaq 0x8(%rsp), %rdi
callq 0x5bf9
leaq 0x8(%rsp), %rdi
callq 0x5a94
movl %ebp, %eax
incl %ebp
leaq (%rax,%rax,4), %rax
leaq -0x28(,%rax,8), %r15
leaq 0x8(%rsp), %r14
movq 0xb0(%rsp), %rsi
decl %ebp
cmpl $0x1, %ebp
jle 0x6955
addq %r15, %rsi
movq %r14, %rdi
callq 0x70b4
movq %r14, %rdi
callq 0x5d76
movq %r14, %rdi
callq 0x5bf9
addq $-0x28, %r15
movq %r14, %rdi
callq 0x5a94
jmp 0x691d
leaq 0x8(%rsp), %rdi
callq 0x70b4
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
xorl %r13d, %r13d
leaq 0x7(%rsp), %r12
leaq (,%r13,2), %rbp
addq %r13, %rbp
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
addq %r14, %rdi
callq 0x4c18
movb %al, 0x7(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x69b3
movb %al, (%rsi)
incq 0x38(%rsp)
jmp 0x69be
movq %r15, %rdi
movq %r12, %rdx
callq 0x8ed6
addq $0x20, %r14
cmpq $0x80, %r14
jne 0x6989
incq %r13
cmpq $0x4, %r13
jne 0x697b
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x8fc8
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6a04
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x837d(%rip), %rax      # 0xed88
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x746c
leaq 0x90(%rsp), %rdi
callq 0x6d3c
leaq 0xb0(%rsp), %rdi
callq 0x71fe
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x6a4b
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x6a62
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2050
jmp 0x6ada
jmp 0x6aaf
jmp 0x6aaf
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x6b23
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6b23
movq %rax, %rbx
jmp 0x6b3a
jmp 0x6ad0
movq %rax, %rbx
jmp 0x6b09
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x6b09
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6b09
movq %rax, %rbx
jmp 0x6af4
movq %rax, %rbx
jmp 0x6b23
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6af4
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x828d(%rip), %rax      # 0xed88
leaq 0x10(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x746c
leaq 0x90(%rsp), %rdi
callq 0x6d3c
leaq 0xb0(%rsp), %rdi
callq 0x71fe
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x6b3a
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x6b51
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0
nop