// File: /workspace/github_cpp_makefile_star2_O2/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 0x7ba3(%rip), %rdi      # 0xd158
callq 0x3e98
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x5df6
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x5df6
leaq 0x80(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x5e28
leaq 0x68(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl %ebp, %edx
callq 0x5070
leaq 0x80(%rsp), %rdi
callq 0x77d6
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
pushq $0x10
popq %rsi
callq 0x5d22
pushq $0x10
popq %r13
leaq 0x8(%rsp), %r14
leaq 0xb0(%rsp), %r15
leaq 0x50(%rsp), %r12
subl $0x1, %r13d
jb 0x567c
movq %r14, %rdi
movq %r15, %rsi
callq 0x4ef2
movq %r12, %rdi
movq %r14, %rsi
callq 0x6ace
movq %r14, %rdi
callq 0x4360
jmp 0x5656
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rdx
leaq 0x50(%rsp), %rcx
movl %edx, %esi
callq 0x58a4
movslq %ebp, %rax
imulq $0x28, %rax, %rsi
addq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x5c70
leaq 0x8(%rsp), %rdi
callq 0x4d59
leaq 0x8(%rsp), %rdi
callq 0x4c33
movl %ebp, %eax
incl %ebp
imulq $0x28, %rax, %r15
addq $-0x28, %r15
leaq 0x8(%rsp), %r14
movq 0x68(%rsp), %rsi
decl %ebp
cmpl $0x1, %ebp
jle 0x5700
addq %r15, %rsi
movq %r14, %rdi
callq 0x5c70
movq %r14, %rdi
callq 0x4e8c
movq %r14, %rdi
callq 0x4d59
addq $-0x28, %r15
movq %r14, %rdi
callq 0x4c33
jmp 0x56cb
leaq 0x8(%rsp), %rdi
callq 0x5c70
leaq 0x30(%rsp), %r14
andq $0x0, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
xorl %r12d, %r12d
leaq 0x7(%rsp), %r15
cmpq $0x4, %r12
je 0x5763
imulq $0x18, %r12, %r13
xorl %ebp, %ebp
cmpq $0x80, %rbp
je 0x575e
movq 0x10(%rsp), %rax
movq (%rax,%r13), %rdi
addq %rbp, %rdi
callq 0x3e92
movb %al, 0x7(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7882
addq $0x20, %rbp
jmp 0x572f
incq %r12
jmp 0x5723
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x7996
leaq 0x30(%rsp), %rdi
callq 0x77d6
leaq 0x8(%rsp), %rdi
callq 0x59da
leaq 0x50(%rsp), %rdi
callq 0x59b8
leaq 0x68(%rsp), %rdi
callq 0x5dd4
leaq 0x98(%rsp), %rdi
callq 0x77d6
leaq 0xb0(%rsp), %rdi
callq 0x77d6
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5810
jmp 0x57f7
jmp 0x57f7
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x77d6
jmp 0x583b
movq %rax, %rbx
jmp 0x583b
movq %rax, %rbx
jmp 0x5848
jmp 0x580b
movq %rax, %rbx
jmp 0x5827
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4360
jmp 0x5827
movq %rax, %rbx
jmp 0x581d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x77d6
leaq 0x8(%rsp), %rdi
callq 0x59da
leaq 0x50(%rsp), %rdi
callq 0x59b8
leaq 0x68(%rsp), %rdi
callq 0x5dd4
leaq 0x98(%rsp), %rdi
callq 0x77d6
leaq 0xb0(%rsp), %rdi
callq 0x77d6
movq %rbx, %rdi
callq 0x21c0
nop