#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_ENC(unsigned char const *, unsigned char *,
                               unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ padding_AES(unsigned char const *, unsigned char *, int);
int /*<<< orphan*/ xor_128(unsigned char *, unsigned char const *,
                           unsigned char *);
int AES_CBC_ENC(const unsigned char *IV, const unsigned char *key,
                const unsigned char *input, int inputLength,
                unsigned char *output, int outputLength) {
  unsigned char X[16], Y[16], M_last[16];
  if (inputLength <= 0)
    return 0;
  int n = (inputLength + (16 - 1)) / 16;
  memcpy(X, IV, 16);
  padding_AES(&input[16 * (n - 1)], M_last, inputLength);
  for (int i = 0; (i < n) && outputLength > 0; i++) {
    unsigned const char *text = &input[16 * i];
    if (i == n - 1) {
      text = M_last;
    }
    int outLen = (16 < outputLength) ? 16 : outputLength;
    xor_128(X, text, Y);
    AES_128_ENC(key, Y, X);
    memcpy(output, X, outLen);
    outputLength -= outLen;
    output += outLen;
  }
  return n * 16;
}


x86 assembly (clang, -O0):
<AES_CBC_ENC>:
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     1178 <AES_CBC_ENC+0x38>
movl   $0x0,-0x4(%rbp)
jmp    12c2 <AES_CBC_ENC+0x182>
mov    -0x24(%rbp),%eax
add    $0xf,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x74(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x74(%rbp),%eax
sub    $0x1,%eax
shl    $0x4,%eax
cltq
add    %rax,%rdi
lea    -0x70(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <padding_AES@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%ecx
xor    %eax,%eax
cmp    -0x74(%rbp),%ecx
mov    %al,-0x85(%rbp)
jge    11e1 <AES_CBC_ENC+0xa1>
cmpl   $0x0,-0x34(%rbp)
setg   %al
mov    %al,-0x85(%rbp)
mov    -0x85(%rbp),%al
test   $0x1,%al
jne    11f4 <AES_CBC_ENC+0xb4>
jmp    12b9 <AES_CBC_ENC+0x179>
mov    -0x20(%rbp),%rax
mov    -0x78(%rbp),%ecx
shl    $0x4,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
mov    -0x78(%rbp),%eax
mov    -0x74(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1221 <AES_CBC_ENC+0xe1>
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    $0x10,%eax
cmp    -0x34(%rbp),%eax
jge    123f <AES_CBC_ENC+0xff>
mov    $0x10,%eax
mov    %eax,-0x8c(%rbp)
jmp    1248 <AES_CBC_ENC+0x108>
mov    -0x34(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x80(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1030 <xor_128@plt>
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1040 <AES_128_ENC@plt>
mov    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x84(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x84(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x84(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    11c0 <AES_CBC_ENC+0x80>
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_ENC>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,0x18(%rsp)
test   %ecx,%ecx
jle    124c <AES_CBC_ENC+0x10c>
mov    %r9d,%ebx
mov    %r8,%r15
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rdi,%rsi
lea    0xf(%r14),%eax
mov    %eax,0x4(%rsp)
mov    %eax,%r12d
shr    $0x4,%r12d
lea    0x20(%rsp),%rdi
mov    $0x10,%edx
call   1060 <memcpy@plt>
lea    -0x1(%r12),%r13d
mov    %r13d,%edi
shl    $0x4,%edi
add    %rbp,%rdi
lea    0x30(%rsp),%rsi
mov    %r14d,%edx
call   1050 <padding_AES@plt>
test   %ebx,%ebx
jle    1243 <AES_CBC_ENC+0x103>
mov    %r13d,%eax
mov    %r12d,%ecx
mov    %rcx,0x8(%rsp)
neg    %rax
mov    %rax,0x10(%rsp)
mov    $0x1,%r13d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rsp),%rax
add    %r13,%rax
cmp    $0x1,%rax
mov    %rbp,%rsi
lea    0x30(%rsp),%rax
cmove  %rax,%rsi
cmp    $0x10,%ebx
mov    $0x10,%r12d
cmovb  %ebx,%r12d
lea    0x20(%rsp),%rdi
lea    0x40(%rsp),%r14
mov    %r14,%rdx
call   1030 <xor_128@plt>
mov    0x18(%rsp),%rdi
mov    %r14,%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdx
call   1040 <AES_128_ENC@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
cmp    0x8(%rsp),%r13
jae    1243 <AES_CBC_ENC+0x103>
sub    %r12d,%ebx
add    %r12,%r15
add    $0x1,%r13
add    $0x10,%rbp
test   %ebx,%ebx
jg     11d0 <AES_CBC_ENC+0x90>
mov    0x4(%rsp),%eax
and    $0xfffffff0,%eax
jmp    124e <AES_CBC_ENC+0x10e>
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_ENC(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*
%20 = alloca i32
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
%21 = load i32, i32* %11
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %6
store i32 0, i32* %7
br label %94
24: ; preds = %6
%25 = load i32, i32* %11
%26 = add nsw i32 %25, 15
%27 = sdiv i32 %26, 16
store i32 %27, i32* %17
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%29 = load i8*, i8** %8
%30 = call i32 @memcpy(i8* %28, i8* %29, i32 16)
%31 = load i8*, i8** %10
%32 = load i32, i32* %17
%33 = sub nsw i32 %32, 1
%34 = mul nsw i32 16, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
%38 = load i32, i32* %11
%39 = call i32 @padding_AES(i8* %36, i8* %37, i32 %38)
store i32 0, i32* %18
br label %40
40: ; preds = %88, %24
%41 = load i32, i32* %18
%42 = load i32, i32* %17
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %13
%46 = icmp sgt i32 %45, 0
br label %47
47: ; preds = %44, %40
%48 = phi i1 [ false, %40 ], [ %46, %44 ]
br i1 %48, label %49, label %91
49: ; preds = %47
%50 = load i8*, i8** %10
%51 = load i32, i32* %18
%52 = mul nsw i32 16, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8* %54, i8** %19
%55 = load i32, i32* %18
%56 = load i32, i32* %17
%57 = sub nsw i32 %56, 1
%58 = icmp eq i32 %55, %57
br i1 %58, label %59, label %61
59: ; preds = %49
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %16, i64 0, i64 0
store i8* %60, i8** %19
br label %61
61: ; preds = %59, %49
%62 = load i32, i32* %13
%63 = icmp slt i32 16, %62
br i1 %63, label %64, label %65
64: ; preds = %61
br label %67
65: ; preds = %61
%66 = load i32, i32* %13
br label %67
67: ; preds = %65, %64
%68 = phi i32 [ 16, %64 ], [ %66, %65 ]
store i32 %68, i32* %20
%69 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%70 = load i8*, i8** %19
%71 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%72 = call i32 @xor_128(i8* %69, i8* %70, i8* %71)
%73 = load i8*, i8** %9
%74 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%75 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%76 = call i32 @AES_128_ENC(i8* %73, i8* %74, i8* %75)
%77 = load i8*, i8** %12
%78 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%79 = load i32, i32* %20
%80 = call i32 @memcpy(i8* %77, i8* %78, i32 %79)
%81 = load i32, i32* %20
%82 = load i32, i32* %13
%83 = sub nsw i32 %82, %81
store i32 %83, i32* %13
%84 = load i32, i32* %20
%85 = load i8*, i8** %12
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8* %87, i8** %12
br label %88
88: ; preds = %67
%89 = load i32, i32* %18
%90 = add nsw i32 %89, 1
store i32 %90, i32* %18
br label %40
91: ; preds = %47
%92 = load i32, i32* %17
%93 = mul nsw i32 %92, 16
store i32 %93, i32* %7
br label %94
94: ; preds = %91, %23
%95 = load i32, i32* %7
ret i32 %95
}

LLVM IR (-O2):
define i32 @AES_CBC_ENC(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 %49, label %14
14: ; preds = %6
%15 = add nuw nsw i32 %3, 15
%16 = lshr i32 %15, 4
%17 = call i32 @memcpy(i8* %10, i8* %0, i32 16)
%18 = add nsw i32 %16, -1
%19 = shl nuw nsw i32 %18, 4
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %2, i64 %20
%22 = call i32 @padding_AES(i8* %21, i8* %12, i32 %3)
%23 = icmp sgt i32 %5, 0
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = zext i32 %18 to i64
%26 = zext i32 %16 to i64
br label %29
27: ; preds = %29, %14
%28 = and i32 %15, -16
br label %49
29: ; preds = %24, %29
%30 = phi i64 [ 0, %24 ], [ %45, %29 ]
%31 = phi i8* [ %4, %24 ], [ %44, %29 ]
%32 = phi i32 [ %5, %24 ], [ %42, %29 ]
%33 = shl nsw i64 %30, 4
%34 = getelementptr inbounds i8, i8* %2, i64 %33
%35 = icmp eq i64 %30, %25
%36 = select i1 %35, i8* %12, i8* %34
%37 = icmp ult i32 %32, 16
%38 = select i1 %37, i32 %32, i32 16
%39 = call i32 @xor_128(i8* %10, i8* %36, i8* %11)
%40 = call i32 @AES_128_ENC(i8* %1, i8* %11, i8* %10)
%41 = call i32 @memcpy(i8* %31, i8* %10, i32 %38)
%42 = sub nsw i32 %32, %38
%43 = zext i32 %38 to i64
%44 = getelementptr inbounds i8, i8* %31, i64 %43
%45 = add nuw nsw i64 %30, 1
%46 = icmp ult i64 %45, %26
%47 = icmp sgt i32 %42, 0
%48 = select i1 %46, i1 %47, i1 false
br i1 %48, label %29, label %27
49: ; preds = %6, %27
%50 = phi i32 [ %28, %27 ], [ 0, %6 ]
ret i32 %50
}