#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 */
typedef int /*<<< orphan*/ AES_KEY;

/* Variables and functions */
unsigned long AES_BLOCK_SIZE;
int /*<<< orphan*/ AES_decrypt(unsigned char const *, unsigned char *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ AES_encrypt(unsigned char const *, unsigned char *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ AES_set_decrypt_key(unsigned char const *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ AES_set_encrypt_key(unsigned char const *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long);
int /*<<< orphan*/ stderr;
int aes_128_ecb(const unsigned char key[16], const unsigned char *in,
                const unsigned long length, unsigned char *out, int enc) {
  AES_KEY akey;
  unsigned long len = length;
  if (enc) {
    AES_set_encrypt_key(&key[0], 128, &akey);
    while (len >= AES_BLOCK_SIZE) {
      AES_encrypt(in, out, &akey);
      len -= AES_BLOCK_SIZE;
      out += AES_BLOCK_SIZE;
      in += AES_BLOCK_SIZE;
    }
  } else {
    AES_set_decrypt_key(&key[0], 128, &akey);
    while (len >= AES_BLOCK_SIZE) {
      AES_decrypt(in, out, &akey);
      len -= AES_BLOCK_SIZE;
      out += AES_BLOCK_SIZE;
      in += AES_BLOCK_SIZE;
    }
  }
  if (len != 0)
    fprintf(stderr, "error: len=%ld, should be 0\n", len);
  return 0;
}


x86 assembly (clang, -O0):
<aes_128_ecb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f8 <aes_128_ecb+0xa8>
mov    -0x8(%rbp),%rdi
mov    $0x80,%esi
lea    -0x28(%rbp),%rdx
call   1030 <AES_set_encrypt_key@plt>
mov    -0x30(%rbp),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jb     11f3 <aes_128_ecb+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <AES_encrypt@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1190 <aes_128_ecb+0x40>
jmp    1272 <aes_128_ecb+0x122>
mov    -0x8(%rbp),%rdi
mov    $0x80,%esi
lea    -0x28(%rbp),%rdx
call   1050 <AES_set_decrypt_key@plt>
mov    -0x30(%rbp),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jb     126d <aes_128_ecb+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <AES_decrypt@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    120a <aes_128_ecb+0xba>
jmp    1272 <aes_128_ecb+0x122>
cmpq   $0x0,-0x30(%rbp)
je     1296 <aes_128_ecb+0x146>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd6f(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_128_ecb>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0xc(%rsp),%rdx
mov    $0x80,%esi
test   %r8d,%r8d
je     11b2 <aes_128_ecb+0x62>
call   1030 <AES_set_encrypt_key@plt>
mov    0x2e67(%rip),%r13
cmp    %r14,0x0(%r13)
ja     11f0 <aes_128_ecb+0xa0>
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <AES_encrypt@plt>
mov    0x0(%r13),%rax
sub    %rax,%r14
add    %rax,%r15
add    %rax,%rbx
cmp    %rax,%r14
jae    1190 <aes_128_ecb+0x40>
jmp    11f0 <aes_128_ecb+0xa0>
call   1050 <AES_set_decrypt_key@plt>
mov    0x2e2a(%rip),%r13
cmp    %r14,0x0(%r13)
ja     11f0 <aes_128_ecb+0xa0>
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <AES_decrypt@plt>
mov    0x0(%r13),%rax
sub    %rax,%r14
add    %rax,%r15
add    %rax,%rbx
cmp    %rax,%r14
jae    11d0 <aes_128_ecb+0x80>
test   %r14,%r14
je     120d <aes_128_ecb+0xbd>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@AES_BLOCK_SIZE = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"error: len=%ld, should be 0\0A\00"
define i32 @aes_128_ecb(i8* %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = load i64, i64* %8
store i64 %13, i64* %12
%14 = load i32, i32* %10
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %5
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = call i32 @AES_set_encrypt_key(i8* %18, i32 128, i32* %11)
br label %20
20: ; preds = %24, %16
%21 = load i64, i64* %12
%22 = load i64, i64* @AES_BLOCK_SIZE
%23 = icmp uge i64 %21, %22
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i8*, i8** %9
%27 = call i32 @AES_encrypt(i8* %25, i8* %26, i32* %11)
%28 = load i64, i64* @AES_BLOCK_SIZE
%29 = load i64, i64* %12
%30 = sub i64 %29, %28
store i64 %30, i64* %12
%31 = load i64, i64* @AES_BLOCK_SIZE
%32 = load i8*, i8** %9
%33 = getelementptr inbounds i8, i8* %32, i64 %31
store i8* %33, i8** %9
%34 = load i64, i64* @AES_BLOCK_SIZE
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 %34
store i8* %36, i8** %7
br label %20
37: ; preds = %20
br label %60
38: ; preds = %5
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = call i32 @AES_set_decrypt_key(i8* %40, i32 128, i32* %11)
br label %42
42: ; preds = %46, %38
%43 = load i64, i64* %12
%44 = load i64, i64* @AES_BLOCK_SIZE
%45 = icmp uge i64 %43, %44
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = load i8*, i8** %7
%48 = load i8*, i8** %9
%49 = call i32 @AES_decrypt(i8* %47, i8* %48, i32* %11)
%50 = load i64, i64* @AES_BLOCK_SIZE
%51 = load i64, i64* %12
%52 = sub i64 %51, %50
store i64 %52, i64* %12
%53 = load i64, i64* @AES_BLOCK_SIZE
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i64 %53
store i8* %55, i8** %9
%56 = load i64, i64* @AES_BLOCK_SIZE
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i64 %56
store i8* %58, i8** %7
br label %42
59: ; preds = %42
br label %60
60: ; preds = %59, %37
%61 = load i64, i64* %12
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = load i64, i64* %12
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %65)
br label %67
67: ; preds = %63, %60
ret i32 0
}

LLVM IR (-O2):
@AES_BLOCK_SIZE = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"error: len=%ld, should be 0\0A\00"
define i32 @aes_128_ecb(i8* %0, i8* %1, i64 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = icmp eq i32 %4, 0
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = call i32 @AES_set_encrypt_key(i8* %0, i32 128, i32* %6)
%11 = load i64, i64* @AES_BLOCK_SIZE
%12 = icmp ugt i64 %11, %2
br i1 %12, label %37, label %13
13: ; preds = %9, %13
%14 = phi i64 [ %19, %13 ], [ %2, %9 ]
%15 = phi i8* [ %21, %13 ], [ %1, %9 ]
%16 = phi i8* [ %20, %13 ], [ %3, %9 ]
%17 = call i32 @AES_encrypt(i8* %15, i8* %16, i32* %6)
%18 = load i64, i64* @AES_BLOCK_SIZE
%19 = sub i64 %14, %18
%20 = getelementptr inbounds i8, i8* %16, i64 %18
%21 = getelementptr inbounds i8, i8* %15, i64 %18
%22 = icmp ult i64 %19, %18
br i1 %22, label %37, label %13
23: ; preds = %5
%24 = call i32 @AES_set_decrypt_key(i8* %0, i32 128, i32* %6)
%25 = load i64, i64* @AES_BLOCK_SIZE
%26 = icmp ugt i64 %25, %2
br i1 %26, label %37, label %27
27: ; preds = %23, %27
%28 = phi i64 [ %33, %27 ], [ %2, %23 ]
%29 = phi i8* [ %35, %27 ], [ %1, %23 ]
%30 = phi i8* [ %34, %27 ], [ %3, %23 ]
%31 = call i32 @AES_decrypt(i8* %29, i8* %30, i32* %6)
%32 = load i64, i64* @AES_BLOCK_SIZE
%33 = sub i64 %28, %32
%34 = getelementptr inbounds i8, i8* %30, i64 %32
%35 = getelementptr inbounds i8, i8* %29, i64 %32
%36 = icmp ult i64 %33, %32
br i1 %36, label %37, label %27
37: ; preds = %13, %27, %9, %23
%38 = phi i64 [ %2, %23 ], [ %2, %9 ], [ %33, %27 ], [ %19, %13 ]
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %38)
br label %43
43: ; preds = %40, %37
ret i32 0
}