// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x897f(%rip), %rdi      # 0xf148
callq 0x4d8c
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rcx
callq 0x7272
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
leaq 0x48(%rsp), %rcx
callq 0x7272
movq 0x78(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
subq 0x70(%rsp), %r14
je 0x6833
js 0x6abd
movq %r14, %rdi
callq 0x2100
movq %rax, %r15
jmp 0x6836
xorl %r15d, %r15d
movq %r15, 0x30(%rsp)
movq %r15, 0x38(%rsp)
addq %r15, %r14
movq %r14, 0x40(%rsp)
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %r14
subq %rsi, %r14
je 0x6862
movq %r15, %rdi
movq %r14, %rdx
callq 0x2170
addq %r14, %r15
leaq 0x30(%rsp), %rsi
movq %r15, 0x8(%rsi)
leaq 0xc0(%rsp), %rdi
movl %ebp, %edx
callq 0x60f1
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6894
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x10, %esi
callq 0x7174
movl $0x10, %r13d
leaq 0x48(%rsp), %r14
leaq 0x88(%rsp), %r15
leaq 0xa0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x5eb6
movq %r12, %rdi
movq %r14, %rsi
callq 0x7f88
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x68fc
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %r13d
jne 0x68cf
leaq 0x48(%rsp), %rdi
leaq 0xa0(%rsp), %rcx
movl $0x4, %esi
movl $0x4, %edx
callq 0x6c1e
movslq %ebp, %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0xc0(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x70ec
leaq 0x48(%rsp), %rdi
callq 0x5cc4
leaq 0x48(%rsp), %rdi
callq 0x5b6a
movl %ebp, %eax
leaq (%rax,%rax,4), %rax
leaq -0x28(,%rax,8), %r15
incl %ebp
leaq 0x48(%rsp), %r14
movq 0xc0(%rsp), %rsi
decl %ebp
cmpl $0x1, %ebp
jle 0x699b
addq %r15, %rsi
movq %r14, %rdi
callq 0x70ec
movq %r14, %rdi
callq 0x5e36
movq %r14, %rdi
callq 0x5cc4
addq $-0x28, %r15
movq %r14, %rdi
callq 0x5b6a
jmp 0x6963
leaq 0x48(%rsp), %rdi
callq 0x70ec
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
xorl %r12d, %r12d
leaq 0xf(%rsp), %r15
leaq (%r12,%r12,2), %r13
xorl %ebp, %ebp
movq 0x50(%rsp), %rax
movq (%rax,%r13,8), %rdi
addq %rbp, %rdi
callq 0x4d86
movb %al, 0xf(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x69f1
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0x69fc
movq %r14, %rdi
movq %r15, %rdx
callq 0x8dc0
addq $0x20, %rbp
cmpq $0x80, %rbp
jne 0x69c7
incq %r12
cmpq $0x4, %r12
jne 0x69c1
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x8eb2
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6a42
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x833f(%rip), %rax      # 0xed88
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x7498
leaq 0xa0(%rsp), %rdi
callq 0x6d8c
leaq 0xc0(%rsp), %rdi
callq 0x7230
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x6a8b
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x6aa8
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2050
jmp 0x6b1d
jmp 0x6af2
jmp 0x6af2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6b66
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6b66
movq %rax, %rbx
jmp 0x6b80
jmp 0x6b13
movq %rax, %rbx
jmp 0x6b4c
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x6b4c
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6b4c
movq %rax, %rbx
jmp 0x6b37
movq %rax, %rbx
jmp 0x6b66
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6b37
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x824a(%rip), %rax      # 0xed88
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x7498
leaq 0xa0(%rsp), %rdi
callq 0x6d8c
leaq 0xc0(%rsp), %rdi
callq 0x7230
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x6b80
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x6b9d
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0
nop