// File: /workspace/github_cpp_makefile_star2_O2/lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::CBC_decrypt()
void Cipher::CBC_decrypt(){   
    Sequence tag = CBC_MAC(*inputBlock, false, true);             //Generate MAC Tag from Ciphertext
    Sequence readTag = inputBlock->getSequenceVector().at(0);     //Get the MAC Tag in the Ciphertext
    if(authenticateSequences(&readTag, &tag)){                    //Compare them for authentication purposes
        Sequence prevSq(16), cipherText(16); 
        int size = inputBlock->getSequenceVector().size();
        int i = -1;
        for(Sequence sq: inputBlock->getSequenceVector()){         
            i++;
            if(i <= 1){
                prevSq.updateSequence(sq);  //Skip decryption of first and last Block of ciphertext
                continue;                   //These blocks are the IV and MAC Tag respectively
            }
            cipherText.updateSequence(sq);  //Keep track of ciphertext, we'll need it next round   
            decrypt(&sq, aesKeyExp);
            sq = sq ^ prevSq;               
            prevSq.updateSequence(cipherText);  //Update previous sequence, to ciphertext values
        }
        try{
            removePadding(&inputBlock->getSequenceVector().at(size-1));     //Last block will have padding
        }catch(const char* str){
            throw str;
        }

        //Blocks have been authenticated, decrypted, and padding removed. Ready to write plaintext
        writePlainText();
        /* Sequence cleanup as it can leave traces in memory */
        prevSq.~Sequence();
        cipherText.~Sequence();
    }else
        throw "Ciphertext has been modified, it won't be decrypted!\n";     //Authenticate-then-decrypt
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rsi
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x597a
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4d22
leaq 0x78(%rsp), %r14
movq %rax, (%r14)
movl %edx, 0x8(%r14)
leaq 0x38(%rsp), %rdi
callq 0x316c
movq 0x10(%rbx), %rdi
callq 0x318e
movq %rax, %rdi
xorl %esi, %esi
callq 0x58c0
movups (%rax), %xmm0
leaq 0x50(%rsp), %rsi
movaps %xmm0, (%rsi)
movq %r14, %rdx
callq 0x4ebe
testb %al, %al
je 0x53dd
leaq 0x68(%rsp), %r14
pushq $0x10
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x2ea0
leaq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x2ea0
movq 0x10(%rbx), %rdi
callq 0x318e
movq (%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rbx), %rdi
callq 0x318e
movq (%rax), %rbp
movq 0x8(%rax), %r13
pushq $-0x1
popq %r15
movq %rsp, %r12
cmpq %r13, %rbp
je 0x538b
movups (%rbp), %xmm0
movaps %xmm0, (%rsp)
movq (%rsp), %rsi
movl 0x8(%rsp), %edx
testl %r15d, %r15d
jle 0x537a
leaq 0x28(%rsp), %rdi
callq 0x2f3c
movq (%rbx), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4bf8
movq %r12, %rdi
movq %r14, %rsi
callq 0x2fe8
movq (%rax), %rcx
movq %rcx, (%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
movq %r14, %rdi
callq 0x2f3c
addq $0x10, %rbp
incl %r15d
jmp 0x5326
movq 0x10(%rbx), %rdi
callq 0x318e
movq 0x20(%rsp), %rcx
subq 0x18(%rsp), %rcx
shlq $0x1c, %rcx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
addq %rcx, %rsi
sarq $0x20, %rsi
movq %rax, %rdi
callq 0x58c0
movq %rax, %rsi
callq 0x4f0c
movq %rbx, %rdi
callq 0x4f8a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2100
leaq 0x23c7(%rip), %rcx      # 0x77b3
movq %rcx, (%rax)
movq 0x5bd2(%rip), %rsi      # 0xafc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x23a0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x316c
jmp 0x5446
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x5446
movq %rbx, %rdi
callq 0x20a0
movq %rax, %rbx
pushq $0x8
popq %rdi
callq 0x2100
movq %rbx, (%rax)
movq 0x5b94(%rip), %rsi      # 0xafc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x23a0
movq %rax, %rbx
callq 0x2370
movq %rbx, %rdi
callq 0x23d0