#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_cipher(int const, char *, char const *, char *);
short aes_pkcs7padding(char const *, size_t const, char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
void aes_cbcenc(const int bits, const char *in, const size_t length,
                const char *key, const char *iv, char *out) {
  const char *vector = iv;
  char blk[16];
  for (size_t d = 0; d + 15 < length; d += 16) {
    memcpy(blk, in + d, 16);
    for (short i = 0; i < 16; i++)
      blk[i] ^= vector[i];
    aes_cipher(bits, blk, key, out + d);
    vector = out + d;
  }
  short quot = aes_pkcs7padding(in, length, blk);
  for (short i = 0; i < 16; i++)
    blk[i] ^= vector[i];
  aes_cipher(bits, blk, key, out + length - quot);
}


x86 assembly (clang, -O0):
<aes_cbcenc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0xf,%rax
cmp    -0x18(%rbp),%rax
jae    11fd <aes_cbcenc+0xcd>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x58(%rbp),%rsi
mov    $0x10,%edx
call   1030 <memcpy@plt>
movw   $0x0,-0x5a(%rbp)
movswl -0x5a(%rbp),%eax
cmp    $0x10,%eax
jge    11c8 <aes_cbcenc+0x98>
mov    -0x38(%rbp),%rax
movswq -0x5a(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
movswq -0x5a(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%ecx
xor    %edx,%ecx
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x5a(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x5a(%rbp)
jmp    118d <aes_cbcenc+0x5d>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
add    -0x58(%rbp),%rcx
call   1050 <aes_cipher@plt>
mov    -0x30(%rbp),%rax
add    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x58(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x58(%rbp)
jmp    115f <aes_cbcenc+0x2f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1040 <aes_pkcs7padding@plt>
mov    %ax,-0x5c(%rbp)
movw   $0x0,-0x5e(%rbp)
movswl -0x5e(%rbp),%eax
cmp    $0x10,%eax
jge    1253 <aes_cbcenc+0x123>
mov    -0x38(%rbp),%rax
movswq -0x5e(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
movswq -0x5e(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%ecx
xor    %edx,%ecx
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x5e(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x5e(%rbp)
jmp    1218 <aes_cbcenc+0xe8>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
add    -0x18(%rbp),%rcx
movswl -0x5c(%rbp),%eax
movslq %eax,%r8
xor    %eax,%eax
sub    %r8,%rax
add    %rax,%rcx
call   1050 <aes_cipher@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_cbcenc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%r15
mov    %r8,%rbp
mov    %rcx,%r14
mov    %edi,%ebx
cmp    $0x10,%rdx
jb     11f4 <aes_cbcenc+0xc4>
mov    %ebx,0xc(%rsp)
mov    $0xf,%r12d
xor    %r13d,%r13d
mov    %r15,%rbx
mov    %r14,0x28(%rsp)
mov    %r15,0x20(%rsp)
mov    %rsi,0x30(%rsp)
mov    %rsi,%r15
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    %rdx,%r14
mov    $0x10,%edx
call   1030 <memcpy@plt>
movups 0x0(%rbp),%xmm0
xorps  0x10(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x20(%rsp),%rax
lea    (%rax,%r13,1),%rbp
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    0x28(%rsp),%rdx
mov    %rbx,%rcx
call   1050 <aes_cipher@plt>
mov    %r14,%rdx
add    $0x10,%r13
add    $0x10,%r15
add    $0x10,%rbx
add    $0x10,%r12
cmp    %r14,%r12
jb     1180 <aes_cbcenc+0x50>
add    $0xfffffffffffffff0,%rbx
mov    %rbx,%rbp
mov    0x20(%rsp),%r15
mov    0x28(%rsp),%r14
mov    0xc(%rsp),%ebx
mov    0x30(%rsp),%rsi
lea    0x10(%rsp),%r12
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rdx,%r13
mov    %r12,%rdx
call   1040 <aes_pkcs7padding@plt>
movups 0x0(%rbp),%xmm0
xorps  0x10(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
add    %r13,%r15
movswq %ax,%rax
sub    %rax,%r15
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1050 <aes_cipher@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @aes_cbcenc(i32 %0, i8* %1, i64 %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca [16 x i8]
%15 = alloca i64
%16 = alloca i16
%17 = alloca i16
%18 = alloca i16
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i8* %5, i8** %12
%19 = load i8*, i8** %11
store i8* %19, i8** %13
store i64 0, i64* %15
br label %20
20: ; preds = %63, %6
%21 = load i64, i64* %15
%22 = add i64 %21, 15
%23 = load i64, i64* %9
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %66
25: ; preds = %20
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%27 = load i8*, i8** %8
%28 = load i64, i64* %15
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = call i32 @memcpy(i8* %26, i8* %29, i32 16)
store i16 0, i16* %16
br label %31
31: ; preds = %49, %25
%32 = load i16, i16* %16
%33 = sext i16 %32 to i32
%34 = icmp slt i32 %33, 16
br i1 %34, label %35, label %52
35: ; preds = %31
%36 = load i8*, i8** %13
%37 = load i16, i16* %16
%38 = sext i16 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = load i16, i16* %16
%43 = sext i16 %42 to i64
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = xor i32 %46, %41
%48 = trunc i32 %47 to i8
store i8 %48, i8* %44
br label %49
49: ; preds = %35
%50 = load i16, i16* %16
%51 = add i16 %50, 1
store i16 %51, i16* %16
br label %31
52: ; preds = %31
%53 = load i32, i32* %7
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%55 = load i8*, i8** %10
%56 = load i8*, i8** %12
%57 = load i64, i64* %15
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = call i32 @aes_cipher(i32 %53, i8* %54, i8* %55, i8* %58)
%60 = load i8*, i8** %12
%61 = load i64, i64* %15
%62 = getelementptr inbounds i8, i8* %60, i64 %61
store i8* %62, i8** %13
br label %63
63: ; preds = %52
%64 = load i64, i64* %15
%65 = add i64 %64, 16
store i64 %65, i64* %15
br label %20
66: ; preds = %20
%67 = load i8*, i8** %8
%68 = load i64, i64* %9
%69 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%70 = call signext i16 @aes_pkcs7padding(i8* %67, i64 %68, i8* %69)
store i16 %70, i16* %17
store i16 0, i16* %18
br label %71
71: ; preds = %89, %66
%72 = load i16, i16* %18
%73 = sext i16 %72 to i32
%74 = icmp slt i32 %73, 16
br i1 %74, label %75, label %92
75: ; preds = %71
%76 = load i8*, i8** %13
%77 = load i16, i16* %18
%78 = sext i16 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = load i16, i16* %18
%83 = sext i16 %82 to i64
%84 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = xor i32 %86, %81
%88 = trunc i32 %87 to i8
store i8 %88, i8* %84
br label %89
89: ; preds = %75
%90 = load i16, i16* %18
%91 = add i16 %90, 1
store i16 %91, i16* %18
br label %71
92: ; preds = %71
%93 = load i32, i32* %7
%94 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%95 = load i8*, i8** %10
%96 = load i8*, i8** %12
%97 = load i64, i64* %9
%98 = getelementptr inbounds i8, i8* %96, i64 %97
%99 = load i16, i16* %17
%100 = sext i16 %99 to i32
%101 = sext i32 %100 to i64
%102 = sub i64 0, %101
%103 = getelementptr inbounds i8, i8* %98, i64 %102
%104 = call i32 @aes_cipher(i32 %93, i8* %94, i8* %95, i8* %103)
ret void
}

LLVM IR (-O2):
define void @aes_cbcenc(i32 %0, i8* %1, i64 %2, i8* %3, i8* nocapture %4, i8* %5) {
%7 = alloca <16 x i8>
%8 = getelementptr inbounds <16 x i8>, <16 x i8>* %7, i64 0, i64 0
%9 = icmp ugt i64 %2, 15
br i1 %9, label %22, label %10
10: ; preds = %22, %6
%11 = phi i8* [ %4, %6 ], [ %31, %22 ]
%12 = call signext i16 @aes_pkcs7padding(i8* %1, i64 %2, i8* %8)
%13 = bitcast i8* %11 to <16 x i8>*
%14 = load <16 x i8>, <16 x i8>* %13
%15 = load <16 x i8>, <16 x i8>* %7
%16 = xor <16 x i8> %15, %14
store <16 x i8> %16, <16 x i8>* %7
%17 = getelementptr inbounds i8, i8* %5, i64 %2
%18 = sext i16 %12 to i64
%19 = sub nsw i64 0, %18
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @aes_cipher(i32 %0, i8* %8, i8* %3, i8* %20)
ret void
22: ; preds = %6, %22
%23 = phi i8* [ %31, %22 ], [ %4, %6 ]
%24 = phi i64 [ %33, %22 ], [ 0, %6 ]
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = call i32 @memcpy(i8* %8, i8* %25, i32 16)
%27 = bitcast i8* %23 to <16 x i8>*
%28 = load <16 x i8>, <16 x i8>* %27
%29 = load <16 x i8>, <16 x i8>* %7
%30 = xor <16 x i8> %29, %28
store <16 x i8> %30, <16 x i8>* %7
%31 = getelementptr inbounds i8, i8* %5, i64 %24
%32 = call i32 @aes_cipher(i32 %0, i8* %8, i8* %3, i8* %31)
%33 = add i64 %24, 16
%34 = or i64 %33, 15
%35 = icmp ult i64 %34, %2
br i1 %35, label %22, label %10
}