// File: 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:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, 0x30(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
movq %rdx, 0x1b0(%rsp)
movl %ecx, 0x1ac(%rsp)
leaq 0x126b4(%rip), %rdi     # 0x1e1d8
callq 0x6700
movq 0x48(%rsp), %rdi
callq 0x2170
movq 0x48(%rsp), %rdi
movq %rax, 0x188(%rsp)
callq 0x20d0
movq %rax, 0x180(%rsp)
leaq 0x17f(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0xce50
movq 0x50(%rsp), %rcx
movq 0x188(%rsp), %rsi
movq 0x180(%rsp), %rdx
leaq 0x190(%rsp), %rdi
callq 0xce60
jmp 0xbb83
leaq 0x17f(%rsp), %rdi
callq 0xcef0
movq 0x30(%rsp), %rdi
callq 0x2170
movq 0x30(%rsp), %rdi
movq %rax, 0x148(%rsp)
callq 0x20d0
movq %rax, 0x140(%rsp)
leaq 0x13f(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xce50
movq 0x28(%rsp), %rcx
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
leaq 0x150(%rsp), %rdi
callq 0xce60
jmp 0xbbea
leaq 0x13f(%rsp), %rdi
callq 0xcef0
leaq 0x108(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0xcf00
jmp 0xbc0e
movl 0x1ac(%rsp), %edx
leaq 0x120(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xb010
jmp 0xbc2c
leaq 0x108(%rsp), %rdi
callq 0xd000
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xc420
movq 0x20(%rsp), %rdi
movl $0x10, %esi
callq 0xcac0
jmp 0xbc5c
movl $0x0, 0xec(%rsp)
cmpl $0x10, 0xec(%rsp)
jge 0xbd8d
leaq 0xc8(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xaca0
jmp 0xbc8c
leaq 0xf0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xcc00
jmp 0xbca3
leaq 0xc8(%rsp), %rdi
callq 0x6f00
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0xbc67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x17f(%rsp), %rdi
callq 0xcef0
jmp 0xc0a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x13f(%rsp), %rdi
callq 0xcef0
jmp 0xc098
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xc08b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0xd000
jmp 0xc08b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xc071
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6f00
jmp 0xc071
leaq 0xa0(%rsp), %rdi
movl $0x4, %edx
leaq 0xf0(%rsp), %rcx
movl %edx, %esi
callq 0xc160
jmp 0xbdab
movslq 0x1ac(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xbdd2
leaq 0xa0(%rsp), %rdi
callq 0xa9e0
jmp 0xbde1
leaq 0xa0(%rsp), %rdi
callq 0xa790
jmp 0xbdf0
movl 0x1ac(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, 0x9c(%rsp)
jle 0xbe91
movslq 0x9c(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xbe36
leaq 0xa0(%rsp), %rdi
callq 0xabf0
jmp 0xbe45
leaq 0xa0(%rsp), %rdi
callq 0xa9e0
jmp 0xbe54
leaq 0xa0(%rsp), %rdi
callq 0xa790
jmp 0xbe63
jmp 0xbe65
movl 0x9c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0xbe01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xc064
xorl %eax, %eax
movl %eax, %esi
leaq 0x120(%rsp), %rdi
callq 0xd060
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xac60
jmp 0xbeb4
leaq 0x80(%rsp), %rdi
callq 0xd080
movl $0x0, 0x7c(%rsp)
cmpl $0x4, 0x7c(%rsp)
jge 0xbf67
movl $0x0, 0x78(%rsp)
cmpl $0x4, 0x78(%rsp)
jge 0xbf55
leaq 0xa0(%rsp), %rdi
leaq 0x7c(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0xc3e0
movq %rax, 0x18(%rsp)
jmp 0xbf01
movq 0x18(%rsp), %rdi
callq 0xac90
movb %al, 0x17(%rsp)
jmp 0xbf11
movb 0x17(%rsp), %al
movb %al, 0x77(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x77(%rsp), %rsi
callq 0xd090
jmp 0xbf2d
jmp 0xbf2f
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xbedc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0xc057
jmp 0xbf57
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xbec9
movb $0x0, 0x76(%rsp)
leaq 0x80(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xca20
movq (%rsp), %rdi
movq %rax, 0x68(%rsp)
callq 0xd0c0
movq %rax, 0x60(%rsp)
leaq 0x5f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x22c0
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0xd0f0
jmp 0xbfba
leaq 0x5f(%rsp), %rdi
callq 0x21f0
movb $0x1, 0x76(%rsp)
testb $0x1, 0x76(%rsp)
jne 0xbffc
jmp 0xbff2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x5f(%rsp), %rdi
callq 0x21f0
jmp 0xc057
movq 0x38(%rsp), %rdi
callq 0x20e0
leaq 0x80(%rsp), %rdi
callq 0xd000
leaq 0xa0(%rsp), %rdi
callq 0xc360
leaq 0xf0(%rsp), %rdi
callq 0xc2f0
leaq 0x120(%rsp), %rdi
callq 0xcdf0
leaq 0x150(%rsp), %rdi
callq 0xd000
leaq 0x190(%rsp), %rdi
callq 0xd000
movq 0x40(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
leaq 0x80(%rsp), %rdi
callq 0xd000
leaq 0xa0(%rsp), %rdi
callq 0xc360
leaq 0xf0(%rsp), %rdi
callq 0xc2f0
leaq 0x120(%rsp), %rdi
callq 0xcdf0
leaq 0x150(%rsp), %rdi
callq 0xd000
leaq 0x190(%rsp), %rdi
callq 0xd000
movq 0x170(%rsp), %rdi
callq 0x22b0
nopw %cs:(%rax,%rax)
nopl (%rax)