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

/* Variables and functions */
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int EVP_EncryptFinal_ex(int /*<<< orphan*/ *, void *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, void const *, void const *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, void *, int *, void const *,
                      size_t);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int crypto_enc(const void *in, size_t in_len, void *out, size_t *out_len,
               const void *iv, const void *key) {
  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  int ret_len;
  if (!ctx) {
    fprintf(stderr, "crypto_enc"
                    ": couldn't initialize EVP context\n");
    return 1;
  }
  if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) {
    fprintf(stderr, "crypto_enc"
                    ": EVP_EncryptInit_ex failed\n");
    return 1;
  }
  if (EVP_EncryptUpdate(ctx, out, &ret_len, in, in_len) != 1) {
    fprintf(stderr, "crypto_enc"
                    ": EVP_EncryptUpdate failed\n");
    return 1;
  }
  *out_len = ret_len;
  if (EVP_EncryptFinal_ex(ctx, out + *out_len, &ret_len) != 1) {
    fprintf(stderr, "crypto_enc"
                    ": EVP_EncryptFinal_ex failed\n");
    return 1;
  }
  *out_len += ret_len;
  EVP_CIPHER_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<crypto_enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    $0x0,%al
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c7 <crypto_enc+0x57>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c5 <crypto_enc+0x155>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <EVP_aes_256_cbc@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     1217 <crypto_enc+0xa7>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c5 <crypto_enc+0x155>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x44(%rbp),%rdx
call   1060 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     125a <crypto_enc+0xea>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c5 <crypto_enc+0x155>
movslq -0x44(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
add    (%rax),%rsi
lea    -0x44(%rbp),%rdx
call   1070 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     12a7 <crypto_enc+0x137>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c5 <crypto_enc+0x155>
movslq -0x44(%rbp),%rcx
mov    -0x28(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_enc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8,%rbp
mov    %rcx,0x10(%rsp)
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
xor    %eax,%eax
call   1090 <EVP_CIPHER_CTX_new@plt>
test   %rax,%rax
je     1215 <crypto_enc+0xa5>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <EVP_aes_256_cbc@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
jne    1227 <crypto_enc+0xb7>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rcx
mov    %r12,%r8
call   1060 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
jne    1239 <crypto_enc+0xc9>
movslq 0xc(%rsp),%rax
mov    0x10(%rsp),%rbp
mov    %rax,0x0(%rbp)
add    %rax,%r15
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
jne    124b <crypto_enc+0xdb>
movslq 0xc(%rsp),%rax
add    %rax,0x0(%rbp)
mov    %rbx,%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
xor    %eax,%eax
jmp    1265 <crypto_enc+0xf5>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
jmp    125b <crypto_enc+0xeb>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
jmp    125b <crypto_enc+0xeb>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
jmp    125b <crypto_enc+0xeb>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"crypto_enc: couldn't initialize EVP context\0A\00"
@.str.1 = [39 x i8] c"crypto_enc: EVP_EncryptInit_ex failed\0A\00"
@.str.2 = [38 x i8] c"crypto_enc: EVP_EncryptUpdate failed\0A\00"
@.str.3 = [40 x i8] c"crypto_enc: EVP_EncryptFinal_ex failed\0A\00"
define i32 @crypto_enc(i8* %0, i64 %1, i8* %2, i64* %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i32
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i64* %3, i64** %11
store i8* %4, i8** %12
store i8* %5, i8** %13
%16 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %16, i32** %14
%17 = load i32*, i32** %14
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %6
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %64
22: ; preds = %6
%23 = load i32*, i32** %14
%24 = call i32 (...) @EVP_aes_256_cbc()
%25 = load i8*, i8** %13
%26 = load i8*, i8** %12
%27 = call i32 @EVP_EncryptInit_ex(i32* %23, i32 %24, i32* null, i8* %25, i8* %26)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %64
32: ; preds = %22
%33 = load i32*, i32** %14
%34 = load i8*, i8** %10
%35 = load i8*, i8** %8
%36 = load i64, i64* %9
%37 = call i32 @EVP_EncryptUpdate(i32* %33, i8* %34, i32* %15, i8* %35, i64 %36)
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %7
br label %64
42: ; preds = %32
%43 = load i32, i32* %15
%44 = sext i32 %43 to i64
%45 = load i64*, i64** %11
store i64 %44, i64* %45
%46 = load i32*, i32** %14
%47 = load i8*, i8** %10
%48 = load i64*, i64** %11
%49 = load i64, i64* %48
%50 = getelementptr i8, i8* %47, i64 %49
%51 = call i32 @EVP_EncryptFinal_ex(i32* %46, i8* %50, i32* %15)
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %56
53: ; preds = %42
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %7
br label %64
56: ; preds = %42
%57 = load i32, i32* %15
%58 = sext i32 %57 to i64
%59 = load i64*, i64** %11
%60 = load i64, i64* %59
%61 = add i64 %60, %58
store i64 %61, i64* %59
%62 = load i32*, i32** %14
%63 = call i32 @EVP_CIPHER_CTX_free(i32* %62)
store i32 0, i32* %7
br label %64
64: ; preds = %56, %53, %39, %29, %19
%65 = load i32, i32* %7
ret i32 %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"crypto_enc: couldn't initialize EVP context\0A\00"
@.str.1 = [39 x i8] c"crypto_enc: EVP_EncryptInit_ex failed\0A\00"
@.str.2 = [38 x i8] c"crypto_enc: EVP_EncryptUpdate failed\0A\00"
@.str.3 = [40 x i8] c"crypto_enc: EVP_EncryptFinal_ex failed\0A\00"
define i32 @crypto_enc(i8* %0, i64 %1, i8* %2, i64* nocapture %3, i8* %4, i8* %5) {
%7 = alloca i32
%8 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%9 = bitcast i32* %7 to i8*
%10 = icmp eq i32* %8, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %42
14: ; preds = %6
%15 = tail call i32 (...) @EVP_aes_256_cbc()
%16 = tail call i32 @EVP_EncryptInit_ex(i32* %8, i32 %15, i32* null, i8* %5, i8* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %42
21: ; preds = %14
%22 = call i32 @EVP_EncryptUpdate(i32* %8, i8* %2, i32* %7, i8* %0, i64 %1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %42
27: ; preds = %21
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
store i64 %29, i64* %3
%30 = getelementptr i8, i8* %2, i64 %29
%31 = call i32 @EVP_EncryptFinal_ex(i32* %8, i8* %30, i32* %7)
%32 = icmp eq i32 %31, 1
br i1 %32, label %36, label %33
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %42
36: ; preds = %27
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = load i64, i64* %3
%40 = add i64 %39, %38
store i64 %40, i64* %3
%41 = call i32 @EVP_CIPHER_CTX_free(i32* %8)
br label %42
42: ; preds = %36, %33, %24, %18, %11
%43 = phi i32 [ 1, %18 ], [ 1, %24 ], [ 1, %33 ], [ 0, %36 ], [ 1, %11 ]
ret i32 %43
}