// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8e3b(%rip), %rdi      # 0xf148
callq 0x4c1e
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x7246
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rcx
callq 0x7246
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rax
movq %rax, %r15
subq %rsi, %r15
je 0x6379
js 0x6647
movq %r15, %rdi
callq 0x2100
movq %rax, %r14
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rax
movq %rax, %r12
subq %rsi, %r12
jmp 0x637f
xorl %r14d, %r14d
movq %r15, %r12
movq %r14, 0x80(%rsp)
addq %r14, %r15
movq %r15, 0x90(%rsp)
cmpq %rsi, %rax
je 0x63a2
movq %r14, %rdi
movq %r12, %rdx
callq 0x2170
addq %r12, %r14
leaq 0x80(%rsp), %rsi
movq %r14, 0x8(%rsi)
leaq 0x98(%rsp), %rdi
movl %ebp, %edx
callq 0x6042
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x63dd
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x10, %esi
callq 0x713c
movl $0x10, %r13d
leaq 0x8(%rsp), %r14
leaq 0xb0(%rsp), %r15
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rdx
cmpq %rdi, %rdx
je 0x6443
movzbl (%rdi), %r12d
leaq 0x1(%rdi), %rsi
cmpq %rdx, %rsi
je 0x6439
subq %rsi, %rdx
callq 0x2170
movq 0x70(%rsp), %rdx
decq %rdx
movq %rdx, 0x70(%rsp)
jmp 0x6446
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 0x647d
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
decl %r13d
jne 0x6410
leaq 0x8(%rsp), %rdi
leaq 0xb0(%rsp), %rcx
movl $0x4, %esi
movl $0x4, %edx
callq 0x6bd2
movq 0x98(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x70b4
cmpl $0x2, %ebp
jl 0x64fe
movl %ebp, %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r15
movl $0x28, %r12d
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x5a28
movq %r14, %rdi
callq 0x5b00
movq %r14, %rdi
callq 0x5d0c
movq 0x98(%rsp), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x70b4
addq $0x28, %r12
cmpq %r12, %r15
jne 0x64ca
leaq 0x8(%rsp), %rdi
callq 0x5a28
leaq 0x8(%rsp), %rdi
callq 0x5b00
movslq %ebp, %rax
leaq (%rax,%rax,4), %rsi
shlq $0x3, %rsi
addq 0x98(%rsp), %rsi
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 0x6583
movb %al, (%rsi)
incq 0x38(%rsp)
jmp 0x658e
movq %r15, %rdi
movq %r12, %rdx
callq 0x8ed6
addq $0x20, %r14
cmpq $0x80, %r14
jne 0x6559
incq %r13
cmpq $0x4, %r13
jne 0x654b
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 0x65d4
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x87ad(%rip), %rax      # 0xed88
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x746c
leaq 0xb0(%rsp), %rdi
callq 0x6d3c
leaq 0x98(%rsp), %rdi
callq 0x71fe
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x661b
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x6632
movq 0x78(%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 0x66ad
jmp 0x6682
jmp 0x6682
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x66f6
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x66f6
movq %rax, %rbx
jmp 0x670d
jmp 0x66a3
movq %rax, %rbx
jmp 0x66dc
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x66dc
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
jmp 0x66dc
movq %rax, %rbx
jmp 0x66c7
movq %rax, %rbx
jmp 0x66f6
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x66c7
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
leaq 0x86ba(%rip), %rax      # 0xed88
leaq 0x10(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x746c
leaq 0xb0(%rsp), %rdi
callq 0x6d3c
leaq 0x98(%rsp), %rdi
callq 0x71fe
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x670d
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x6724
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2110
movq %rbx, %rdi
callq 0x21b0