#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 */
int AES_BLOCK_SIZE;
int /*<<< orphan*/ AES_ctr128_encrypt(unsigned char const *, unsigned char *,
                                      unsigned long const, int /*<<< orphan*/ *,
                                      unsigned char *, unsigned char *,
                                      unsigned int *);
int /*<<< orphan*/ AES_set_encrypt_key(unsigned char const *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int aes_128_ctr(const unsigned char key[16], unsigned char iv[AES_BLOCK_SIZE],
                const unsigned char *in, const unsigned long length,
                unsigned char *out, int enc) {
  AES_KEY akey;
  unsigned int num = 0;
  unsigned char tmp[AES_BLOCK_SIZE];
  if (!enc) {
    fprintf(stderr, "error: aes_128-ctr decrypt not implemented\n");
    return 1;
  }
  AES_set_encrypt_key(&key[0], 128, &akey);
  AES_ctr128_encrypt(in, out, length, &akey, iv, tmp, &num);
  return 0;
}


x86 assembly (clang, -O0):
<aes_128_ctr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    0x2e91(%rip),%rax
movl   $0x0,-0x3c(%rbp)
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11b5 <aes_128_ctr+0x85>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1202 <aes_128_ctr+0xd2>
mov    -0x10(%rbp),%rdi
mov    $0x80,%esi
lea    -0x38(%rbp),%rdx
call   1040 <AES_set_encrypt_key@plt>
mov    -0x60(%rbp),%r9
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%r8
lea    -0x38(%rbp),%rcx
lea    -0x3c(%rbp),%rax
sub    $0x10,%rsp
mov    %rax,(%rsp)
call   1030 <AES_ctr128_encrypt@plt>
add    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_128_ctr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r9d,%r9d
je     11ae <aes_128_ctr+0x7e>
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r12
lea    -0x30(%rbp),%rdx
mov    $0x80,%esi
call   1040 <AES_set_encrypt_key@plt>
sub    $0x8,%rsp
lea    -0x2c(%rbp),%rax
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
lea    -0x30(%rbp),%rcx
mov    %r12,%r8
mov    %rbx,%r9
push   %rax
call   1030 <AES_ctr128_encrypt@plt>
add    $0x10,%rsp
xor    %eax,%eax
jmp    11c8 <aes_128_ctr+0x98>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AES_BLOCK_SIZE = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"error: aes_128-ctr decrypt not implemented\0A\00"
define i32 @aes_128_ctr(i8* %0, i8* %1, i8* %2, i64 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%19 = load i32, i32* @AES_BLOCK_SIZE
%20 = zext i32 %19 to i64
store i32 0, i32* %15
%21 = load i32, i32* @AES_BLOCK_SIZE
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %16
%24 = alloca i8, i64 %22
store i64 %22, i64* %17
%25 = load i32, i32* %13
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %6
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
store i32 1, i32* %18
br label %39
30: ; preds = %6
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = call i32 @AES_set_encrypt_key(i8* %32, i32 128, i32* %14)
%34 = load i8*, i8** %10
%35 = load i8*, i8** %12
%36 = load i64, i64* %11
%37 = load i8*, i8** %9
%38 = call i32 @AES_ctr128_encrypt(i8* %34, i8* %35, i64 %36, i32* %14, i8* %37, i8* %24, i32* %15)
store i32 0, i32* %7
store i32 1, i32* %18
br label %39
39: ; preds = %30, %27
%40 = load i8*, i8** %16
%41 = load i32, i32* %7
ret i32 %41
}

LLVM IR (-O2):
@AES_BLOCK_SIZE = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"error: aes_128-ctr decrypt not implemented\0A\00"
define i32 @aes_128_ctr(i8* %0, i8* %1, i8* %2, i64 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32* %7 to i8*
%10 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%11 = load i32, i32* @AES_BLOCK_SIZE
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = icmp eq i32 %5, 0
br i1 %15, label %16, label %19
16: ; preds = %6
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %22
19: ; preds = %6
%20 = call i32 @AES_set_encrypt_key(i8* %0, i32 128, i32* %7)
%21 = call i32 @AES_ctr128_encrypt(i8* %2, i8* %4, i64 %3, i32* %7, i8* %1, i8* %14, i32* %8)
br label %22
22: ; preds = %19, %16
%23 = phi i32 [ 0, %19 ], [ 1, %16 ]
ret i32 %23
}