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

/* Variables and functions */
int AES_BLOCK_SIZE;
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *, unsigned char *);
int /*<<< orphan*/ EVP_aes_256_ctr();
scalar_t__ RAND_bytes(unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
EVP_CIPHER_CTX *aes_ctx_create() {
  EVP_CIPHER_CTX *aes_ctx = NULL;
  unsigned char aes_key[32];
  if (RAND_bytes(aes_key, sizeof(aes_key)) <= 0) {
    return NULL;
  }
  unsigned char aes_iv[AES_BLOCK_SIZE];
  memset(aes_iv, 0, AES_BLOCK_SIZE);
  aes_ctx = EVP_CIPHER_CTX_new();
  if (aes_ctx == NULL) {
    return NULL;
  }
  if (1 !=
      EVP_EncryptInit_ex(aes_ctx, EVP_aes_256_ctr(), NULL, aes_key, aes_iv)) {
    return NULL;
  }
  return aes_ctx;
}


x86 assembly (clang, -O0):
<aes_ctx_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movq   $0x0,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x20,%esi
call   1030 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     1185 <aes_ctx_create+0x35>
movq   $0x0,-0x8(%rbp)
jmp    124a <aes_ctx_create+0xfa>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x0,%al
call   1070 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11eb <aes_ctx_create+0x9b>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1243 <aes_ctx_create+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1050 <EVP_aes_256_ctr@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%r8
mov    %eax,%esi
lea    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1060 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1234 <aes_ctx_create+0xe4>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1243 <aes_ctx_create+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_ctx_create>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    -0x40(%rbp),%rdi
mov    $0x20,%esi
call   1030 <RAND_bytes@plt>
test   %rax,%rax
jle    11d6 <aes_ctx_create+0x86>
mov    %rsp,%r12
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
xor    %eax,%eax
call   1070 <EVP_CIPHER_CTX_new@plt>
test   %rax,%rax
je     11d1 <aes_ctx_create+0x81>
mov    %rax,%r15
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <EVP_aes_256_ctr@plt>
lea    -0x40(%rbp),%rcx
mov    %r15,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbx,%r8
call   1060 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
cmove  %r15,%r14
mov    %r12,%rsp
jmp    11d9 <aes_ctx_create+0x89>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AES_BLOCK_SIZE = global i32 0
define i32* @aes_ctx_create() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32* null, i32** %2
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = call i64 @RAND_bytes(i8* %7, i32 32)
%9 = icmp sle i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32* null, i32** %1
br label %33
11: ; preds = %0
%12 = load i32, i32* @AES_BLOCK_SIZE
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8, i64 %13
store i64 %13, i64* %5
%16 = load i32, i32* @AES_BLOCK_SIZE
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
%18 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %18, i32** %2
%19 = load i32*, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %11
store i32* null, i32** %1
store i32 1, i32* %6
br label %31
22: ; preds = %11
%23 = load i32*, i32** %2
%24 = call i32 (...) @EVP_aes_256_ctr()
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%26 = call i32 @EVP_EncryptInit_ex(i32* %23, i32 %24, i32* null, i8* %25, i8* %15)
%27 = icmp ne i32 1, %26
br i1 %27, label %28, label %29
28: ; preds = %22
store i32* null, i32** %1
store i32 1, i32* %6
br label %31
29: ; preds = %22
%30 = load i32*, i32** %2
store i32* %30, i32** %1
store i32 1, i32* %6
br label %31
31: ; preds = %29, %28, %21
%32 = load i8*, i8** %4
br label %33
33: ; preds = %31, %10
%34 = load i32*, i32** %1
ret i32* %34
}

LLVM IR (-O2):
@AES_BLOCK_SIZE = global i32 0
define i32* @aes_ctx_create() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = call i64 @RAND_bytes(i8* %2, i32 32)
%4 = icmp slt i64 %3, 1
br i1 %4, label %21, label %5
5: ; preds = %0
%6 = load i32, i32* @AES_BLOCK_SIZE
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i32, i32* @AES_BLOCK_SIZE
%11 = call i32 @memset(i8* %9, i32 0, i32 %10)
%12 = call i32* (...) @EVP_CIPHER_CTX_new()
%13 = icmp eq i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %5
%15 = call i32 (...) @EVP_aes_256_ctr()
%16 = call i32 @EVP_EncryptInit_ex(i32* %12, i32 %15, i32* null, i8* %2, i8* %9)
%17 = icmp eq i32 %16, 1
%18 = select i1 %17, i32* %12, i32* null
br label %19
19: ; preds = %14, %5
%20 = phi i32* [ null, %5 ], [ %18, %14 ]
br label %21
21: ; preds = %0, %19
%22 = phi i32* [ %20, %19 ], [ null, %0 ]
ret i32* %22
}