// File: SimpleButNotNaive[P]AES/decryptor.cpp
// Function: aes_decryptor::decryption(std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
string aes_decryptor::decryption(vector<vector<uint8>> cipher, string key,
                               string init_vector) {
    uint8 key_array[16];
    for (int i = 0; i < key.size(); i++) {
        key_array[i] = key[i];
    }
    generate_roundkey(key_array);
    reverse_round_key();

    uint8 pre_y[16];
    for(int i = 0;i < init_vector.size();i++){
        pre_y[i] = init_vector[i];
    }

    uint8 post_y[16];
    uint8 current_y[16];
    uint8* p_text_array = new uint8[16*cipher.size()];
    for(auto group: cipher){
        for(int i = 0;i < 16;i++){
            post_y[i] = group[i];
            current_y[i] = group[i];
        }
        decrypt_one_group(current_y);

        xor_group(current_y, pre_y);
        splice(p_text_array, current_y);

        for(int i = 0;i < 16;i++){
            pre_y[i] = post_y[i];
        }
    }

    int truncate_size = truncate(p_text_array, cipher.size() * 16);

    string ret(p_text_array, p_text_array + cipher.size() * 16 - truncate_size);

    delete p_text_array;

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %r8, -0x118(%rbp)
movq %rcx, -0x110(%rbp)
movq %rdx, -0x108(%rbp)
movq %rdi, -0x100(%rbp)
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x110(%rbp), %rdi
movslq -0x44(%rbp), %rax
movq %rax, -0x120(%rbp)
callq 0x20d0
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jae 0x2e72
movq -0x110(%rbp), %rdi
movslq -0x44(%rbp), %rsi
callq 0x2220
movb (%rax), %cl
movslq -0x44(%rbp), %rax
movb %cl, -0x40(%rbp,%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2e27
movq -0xf0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2a50
movq -0xf0(%rbp), %rdi
callq 0x31e0
movl $0x0, -0x64(%rbp)
movq -0x118(%rbp), %rdi
movslq -0x64(%rbp), %rax
movq %rax, -0x128(%rbp)
callq 0x20d0
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x2ee0
movq -0x118(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x2220
movb (%rax), %cl
movslq -0x64(%rbp), %rax
movb %cl, -0x60(%rbp,%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2e95
movq -0x108(%rbp), %rdi
callq 0x36b0
movq %rax, %rdi
shlq $0x4, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x108(%rbp), %rax
movq %rcx, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x36e0
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x3710
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3740
testb $0x1, %al
jne 0x2f52
jmp 0x3098
leaq -0xa8(%rbp), %rdi
callq 0x3780
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x37a0
movl $0x0, -0xcc(%rbp)
cmpl $0x10, -0xcc(%rbp)
jge 0x2fd4
movslq -0xcc(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x3890
movb (%rax), %cl
movslq -0xcc(%rbp), %rax
movb %cl, -0x80(%rbp,%rax)
movslq -0xcc(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x3890
movb (%rax), %cl
movslq -0xcc(%rbp), %rax
movb %cl, -0x90(%rbp,%rax)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x2f77
movq -0xf0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3280
jmp 0x2fe9
movq -0xf0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x2b20
jmp 0x3002
movq -0xf0(%rbp), %rdi
movq -0x98(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x33c0
jmp 0x301e
movl $0x0, -0xe0(%rbp)
cmpl $0x10, -0xe0(%rbp)
jge 0x307b
movslq -0xe0(%rbp), %rax
movb -0x80(%rbp,%rax), %cl
movslq -0xe0(%rbp), %rax
movb %cl, -0x60(%rbp,%rax)
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0x3028
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x38b0
jmp 0x31c8
leaq -0xc8(%rbp), %rdi
callq 0x38b0
leaq -0xa8(%rbp), %rdi
callq 0x3910
jmp 0x2f36
movq -0x108(%rbp), %rdi
movq -0x98(%rbp), %rax
movq %rax, -0x148(%rbp)
callq 0x36b0
movq -0xf0(%rbp), %rdi
movq -0x148(%rbp), %rsi
movl %eax, %edx
shll $0x4, %edx
callq 0x3420
movq -0x108(%rbp), %rdi
movl %eax, -0xe4(%rbp)
movb $0x0, -0xe5(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x140(%rbp)
callq 0x36b0
movq %rax, %rcx
movq -0x140(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0xe4(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x138(%rbp)
leaq -0xe6(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x21e0
movq -0x100(%rbp), %rdi
movq -0x140(%rbp), %rsi
movq -0x138(%rbp), %rdx
movq -0x130(%rbp), %rcx
callq 0x3930
jmp 0x3149
leaq -0xe6(%rbp), %rdi
callq 0x2160
movq -0x98(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, %rax
je 0x317a
movq -0x150(%rbp), %rdi
movl $0x1, %esi
callq 0x2140
movb $0x1, -0xe5(%rbp)
testb $0x1, -0xe5(%rbp)
jne 0x31b8
jmp 0x31ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0xe6(%rbp), %rdi
callq 0x2160
jmp 0x31c8
movq -0x100(%rbp), %rdi
callq 0x2238
movq -0xf8(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x21d0
nopw %cs:(%rax,%rax)