// File: /workspace/github_cpp_makefile_star2_O1/permutationlock[P]aes_math/lib/aes.cpp
// Function: encrypt(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 encrypt(string plaintext, string key, int rounds){
    // Set up Galois Field
    GaloisPolynomial::globalSetModulus(rijndael_Mod);
    
    // Convert strings to unsigned characters
    vector<unsigned char> vplaintext(plaintext.begin(), plaintext.end());
    vector<unsigned char> vkey(key.begin(), key.end());
    
    // Expand key
    vector<QSMatrix<GaloisPolynomial>> keyMatrices = expandKey(vkey, rounds);
    
    // Convert plaintext to matrix representation
    vector<GaloisPolynomial> sVec;
    sVec.reserve(16);
    for(int i=0; i<16; i++){
        sVec.push_back(extractPoly(vplaintext));
    }
    QSMatrix<GaloisPolynomial> state(4, 4, sVec);
    
    // Perform rounds on plaintext
    addRoundKey(state, keyMatrices[0]);
    for(int i=1; i<rounds; i++){
        subBytes(state);
        shiftRows(state);
        mixColumns(state);
        addRoundKey(state, keyMatrices[i]);
    }
    subBytes(state);
    shiftRows(state);
    addRoundKey(state, keyMatrices[rounds]);
    
    // Convert back to string
    vector<unsigned char> vciphertext;
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            vciphertext.push_back(polyToChar(state(i,j)));
        }
    }
    string ciphertext(vciphertext.begin(), vciphertext.end());
    
    return ciphertext;
}

// 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 0x8d8d(%rip), %rdi      # 0xf148
callq 0x4d8c
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0xa8(%rsp), %rdi
leaq 0x50(%rsp), %rcx
callq 0x7272
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
leaq 0x50(%rsp), %rcx
callq 0x7272
movq 0x80(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
subq 0x78(%rsp), %r14
je 0x6428
js 0x66be
movq %r14, %rdi
callq 0x2100
movq %rax, %r15
jmp 0x642b
xorl %r15d, %r15d
movq %r15, 0x30(%rsp)
movq %r15, 0x38(%rsp)
addq %r15, %r14
movq %r14, 0x40(%rsp)
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %r14
subq %rsi, %r14
je 0x645a
movq %r15, %rdi
movq %r14, %rdx
callq 0x2170
addq %r14, %r15
leaq 0x30(%rsp), %rsi
movq %r15, 0x8(%rsi)
leaq 0x90(%rsp), %rdi
movl %ebp, %edx
callq 0x60f1
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x648c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x10, %esi
callq 0x7174
movl $0x10, %r13d
leaq 0x50(%rsp), %r14
leaq 0xa8(%rsp), %r15
leaq 0xc0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x5eb6
movq %r12, %rdi
movq %r14, %rsi
callq 0x7f88
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x64f4
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %r13d
jne 0x64c7
leaq 0x50(%rsp), %rdi
leaq 0xc0(%rsp), %rcx
movl $0x4, %esi
movl $0x4, %edx
callq 0x6c1e
movq 0x90(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x70ec
cmpl $0x2, %ebp
jl 0x6575
movl %ebp, %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r15
movl $0x28, %r12d
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x5b02
movq %r14, %rdi
callq 0x5bd2
movq %r14, %rdi
callq 0x5dcc
movq 0x90(%rsp), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x70ec
addq $0x28, %r12
cmpq %r12, %r15
jne 0x6541
leaq 0x50(%rsp), %rdi
callq 0x5b02
leaq 0x50(%rsp), %rdi
callq 0x5bd2
movslq %ebp, %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0x90(%rsp), %rsi
leaq 0x50(%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 0x58(%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 0x65f2
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0x65fd
movq %r14, %rdi
movq %r15, %rdx
callq 0x8dc0
addq $0x20, %rbp
cmpq $0x80, %rbp
jne 0x65c8
incq %r12
cmpq $0x4, %r12
jne 0x65c2
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 0x6643
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x873e(%rip), %rax      # 0xed88
leaq 0x58(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x7498
leaq 0xc0(%rsp), %rdi
callq 0x6d8c
leaq 0x90(%rsp), %rdi
callq 0x7230
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x668c
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x66a9
movq 0xb8(%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 0x671e
jmp 0x66f3
jmp 0x66f3
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6767
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x6767
movq %rax, %rbx
jmp 0x6781
jmp 0x6714
movq %rax, %rbx
jmp 0x674d
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x674d
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x674d
movq %rax, %rbx
jmp 0x6738
movq %rax, %rbx
jmp 0x6767
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6738
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x8649(%rip), %rax      # 0xed88
leaq 0x58(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x7498
leaq 0xc0(%rsp), %rdi
callq 0x6d8c
leaq 0x90(%rsp), %rdi
callq 0x7230
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x6781
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x679e
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0