// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdi, %rbx
movq 0x10(%rdi), %rsi
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x644a
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x580a
movq %rax, 0x28(%rsp)
movl %edx, 0x30(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x353c
movq 0x10(%rbx), %rdi
callq 0x3572
movq (%rax), %rcx
cmpq %rcx, 0x8(%rax)
je 0x5f81
movups (%rcx), %xmm0
leaq 0x60(%rsp), %rsi
movaps %xmm0, (%rsi)
leaq 0x28(%rsp), %rdx
callq 0x59bc
testb %al, %al
je 0x5f93
leaq 0x38(%rsp), %rdi
movl $0x10, %esi
callq 0x31dc
leaq 0x18(%rsp), %rdi
movl $0x10, %esi
callq 0x31dc
movq 0x10(%rbx), %rdi
callq 0x3572
movq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x10(%rbx), %rdi
callq 0x3572
movq (%rax), %rbp
movq 0x8(%rax), %r13
cmpq %r13, %rbp
je 0x5f25
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
leaq 0x38(%rsp), %r14
movq %rsp, %r12
movups (%rbp), %xmm0
movaps %xmm0, (%rsp)
movq (%rsp), %rsi
movl 0x8(%rsp), %edx
testl %r15d, %r15d
jle 0x5f11
leaq 0x18(%rsp), %rdi
callq 0x327e
movq (%rbx), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5602
movq %r12, %rdi
movq %r14, %rsi
callq 0x335d
movq (%rax), %rcx
movq %rcx, (%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %rsi
movl 0x20(%rsp), %edx
movq %r14, %rdi
callq 0x327e
addq $0x10, %rbp
incl %r15d
cmpq %r13, %rbp
jne 0x5ec2
movq 0x10(%rbx), %rdi
callq 0x3572
movq 0x10(%rsp), %rcx
shlq $0x1c, %rcx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
addq %rcx, %rsi
sarq $0x20, %rsi
movq (%rax), %rcx
movq 0x8(%rax), %rdx
subq %rcx, %rdx
sarq $0x4, %rdx
cmpq %rsi, %rdx
jbe 0x5fb8
shlq $0x4, %rsi
addq %rsi, %rcx
movq %rcx, %rsi
callq 0x5a2e
movq %rbx, %rdi
callq 0x5ab2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x186c(%rip), %rdi      # 0x77f4
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2330
movl $0x8, %edi
callq 0x2120
leaq 0x17ef(%rip), %rcx      # 0x7793
movq %rcx, (%rax)
movq 0x5012(%rip), %rsi      # 0xafc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x23d0
leaq 0x1835(%rip), %rdi      # 0x77f4
xorl %eax, %eax
callq 0x2330
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x353c
jmp 0x600e
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x600e
movq %rbx, %rdi
callq 0x20d0
movq %rax, %rbx
movl $0x8, %edi
callq 0x2120
movq %rbx, (%rax)
movq 0x4fc4(%rip), %rsi      # 0xafc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x23d0
movq %rax, %rbx
callq 0x23a0
movq %rbx, %rdi
callq 0x2400