#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ AES_128_DEC(unsigned char const *, unsigned char const *,
                               unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ xor_128(unsigned char *, unsigned char *, unsigned char *);
int AES_CBC_DEC(const unsigned char *IV, const unsigned char *key,
                const unsigned char *input, int inputLength,
                unsigned char *output, int outputLength) {
  unsigned char X[16], text[16], Z[16];
  if (inputLength <= 0)
    return 0;
  inputLength = (inputLength / 16) * 16;
  int n = (inputLength + (16 - 1)) / 16;
  memcpy(Z, IV, 16);
  for (int i = 0; (i < n) && outputLength > 0; i++) {
    unsigned const char *cipher = &input[16 * i];
    AES_128_DEC(key, cipher, X);
    xor_128(Z, X, text);
    memcpy(Z, cipher, 16);
    memcpy(output, text, 16);
    outputLength -= 16;
    output += 16;
  }
  return n * 16;
}


x86 assembly (clang, -O0):
<AES_CBC_DEC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1168 <AES_CBC_DEC+0x38>
movl   $0x0,-0x4(%rbp)
jmp    125d <AES_CBC_DEC+0x12d>
mov    -0x24(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
shl    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0xf,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x74(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%ecx
xor    %eax,%eax
cmp    -0x74(%rbp),%ecx
mov    %al,-0x81(%rbp)
jge    11c4 <AES_CBC_DEC+0x94>
cmpl   $0x0,-0x34(%rbp)
setg   %al
mov    %al,-0x81(%rbp)
mov    -0x81(%rbp),%al
test   $0x1,%al
jne    11d7 <AES_CBC_DEC+0xa7>
jmp    1254 <AES_CBC_DEC+0x124>
mov    -0x20(%rbp),%rax
mov    -0x78(%rbp),%ecx
shl    $0x4,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x80(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1050 <AES_128_DEC@plt>
lea    -0x70(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1030 <xor_128@plt>
lea    -0x70(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    -0x34(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x30(%rbp)
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    11a3 <AES_CBC_DEC+0x73>
mov    -0x74(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AES_CBC_DEC>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,0x18(%rsp)
test   %ecx,%ecx
jle    1226 <AES_CBC_DEC+0xf6>
mov    %r9d,%r15d
mov    %r8,%rbx
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rdi,%rsi
or     $0xf,%r14d
lea    0xf(%r14),%eax
test   %r14d,%r14d
cmovns %r14d,%eax
sar    $0x4,%eax
mov    %eax,0xc(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x10,%edx
call   1040 <memcpy@plt>
cmp    $0x10,%r14d
jl     121d <AES_CBC_DEC+0xed>
test   %r15d,%r15d
jle    121d <AES_CBC_DEC+0xed>
movslq 0xc(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    $0x1,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rsp),%rdi
mov    %rbp,%rsi
lea    0x40(%rsp),%r14
mov    %r14,%rdx
call   1050 <AES_128_DEC@plt>
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    %r14,%rsi
lea    0x30(%rsp),%r14
mov    %r14,%rdx
call   1030 <xor_128@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
cmp    0x10(%rsp),%r13
jge    121d <AES_CBC_DEC+0xed>
lea    -0x10(%r15),%eax
add    $0x10,%rbx
add    $0x10,%rbp
add    $0x1,%r13
cmp    $0x10,%r15d
mov    %eax,%r15d
ja     11b0 <AES_CBC_DEC+0x80>
mov    0xc(%rsp),%eax
shl    $0x4,%eax
jmp    1228 <AES_CBC_DEC+0xf8>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @AES_CBC_DEC(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca [16 x i8]
%15 = alloca [16 x i8]
%16 = alloca [16 x i8]
%17 = alloca i32
%18 = alloca i32
%19 = alloca i8*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%20 = load i32, i32* %11
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %6
store i32 0, i32* %7
br label %72
23: ; preds = %6
%24 = load i32, i32* %11
%25 = sdiv i32 %24, 16
%26 = mul nsw i32 %25, 16
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = add nsw i32 %27, 15
%29 = sdiv i32 %28, 16
store i32 %29, i32* %17
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%31 = load i8*, i8** %8
%32 = call i32 @memcpy(i8* %30, i8* %31, i32 16)
store i32 0, i32* %18
br label %33
33: ; preds = %66, %23
%34 = load i32, i32* %18
%35 = load i32, i32* %17
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* %13
%39 = icmp sgt i32 %38, 0
br label %40
40: ; preds = %37, %33
%41 = phi i1 [ false, %33 ], [ %39, %37 ]
br i1 %41, label %42, label %69
42: ; preds = %40
%43 = load i8*, i8** %10
%44 = load i32, i32* %18
%45 = mul nsw i32 16, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8* %47, i8** %19
%48 = load i8*, i8** %9
%49 = load i8*, i8** %19
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%51 = call i32 @AES_128_DEC(i8* %48, i8* %49, i8* %50)
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%55 = call i32 @xor_128(i8* %52, i8* %53, i8* %54)
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%57 = load i8*, i8** %19
%58 = call i32 @memcpy(i8* %56, i8* %57, i32 16)
%59 = load i8*, i8** %12
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%61 = call i32 @memcpy(i8* %59, i8* %60, i32 16)
%62 = load i32, i32* %13
%63 = sub nsw i32 %62, 16
store i32 %63, i32* %13
%64 = load i8*, i8** %12
%65 = getelementptr inbounds i8, i8* %64, i64 16
store i8* %65, i8** %12
br label %66
66: ; preds = %42
%67 = load i32, i32* %18
%68 = add nsw i32 %67, 1
store i32 %68, i32* %18
br label %33
69: ; preds = %40
%70 = load i32, i32* %17
%71 = mul nsw i32 %70, 16
store i32 %71, i32* %7
br label %72
72: ; preds = %69, %22
%73 = load i32, i32* %7
ret i32 %73
}

LLVM IR (-O2):
define i32 @AES_CBC_DEC(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca [16 x i8]
%8 = alloca [16 x i8]
%9 = alloca [16 x i8]
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%13 = icmp slt i32 %3, 1
br i1 %13, label %41, label %14
14: ; preds = %6
%15 = or i32 %3, 15
%16 = sdiv i32 %15, 16
%17 = call i32 @memcpy(i8* %12, i8* %0, i32 16)
%18 = icmp sgt i32 %15, 15
%19 = icmp sgt i32 %5, 0
%20 = and i1 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = sext i32 %16 to i64
br label %25
23: ; preds = %25, %14
%24 = shl nsw i32 %16, 4
br label %41
25: ; preds = %21, %25
%26 = phi i64 [ 0, %21 ], [ %37, %25 ]
%27 = phi i32 [ %5, %21 ], [ %35, %25 ]
%28 = phi i8* [ %4, %21 ], [ %36, %25 ]
%29 = shl nsw i64 %26, 4
%30 = getelementptr inbounds i8, i8* %2, i64 %29
%31 = call i32 @AES_128_DEC(i8* %1, i8* %30, i8* %10)
%32 = call i32 @xor_128(i8* %12, i8* %10, i8* %11)
%33 = call i32 @memcpy(i8* %12, i8* %30, i32 16)
%34 = call i32 @memcpy(i8* %28, i8* %11, i32 16)
%35 = add nsw i32 %27, -16
%36 = getelementptr inbounds i8, i8* %28, i64 16
%37 = add nuw nsw i64 %26, 1
%38 = icmp slt i64 %37, %22
%39 = icmp ugt i32 %27, 16
%40 = select i1 %38, i1 %39, i1 false
br i1 %40, label %25, label %23
41: ; preds = %6, %23
%42 = phi i32 [ %24, %23 ], [ 0, %6 ]
ret i32 %42
}