#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*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *, unsigned char *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ EVP_aes_128_cbc();
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int encrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
            unsigned char *iv, unsigned char *plaintext) {
  EVP_CIPHER_CTX *ctx;
  int len;
  int plaintext_len;
  if (!(ctx = EVP_CIPHER_CTX_new())) {
    fprintf(stderr, "error1\n");
    abort();
  }
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) {
    fprintf(stderr, "error2\n");
    abort();
  }
  if (1 !=
      EVP_EncryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) {
    fprintf(stderr, "error3\n");
    abort();
  }
  plaintext_len = len;
  if (1 != EVP_EncryptFinal_ex(ctx, plaintext + len, &len)) {
    ERR_print_errors_fp(stderr);
    abort();
  }
  plaintext_len += len;
  EVP_CIPHER_CTX_free(ctx);
  return plaintext_len;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11dc <encrypt+0x4c>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1090 <EVP_aes_128_cbc@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1080 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     122d <encrypt+0x9d>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    -0x34(%rbp),%rdx
call   1060 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1271 <encrypt+0xe1>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
lea    -0x34(%rbp),%rdx
call   1070 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12b3 <encrypt+0x123>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <ERR_print_errors_fp@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%r12
xor    %eax,%eax
call   10a0 <EVP_CIPHER_CTX_new@plt>
test   %rax,%rax
je     122c <encrypt+0x9c>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <EVP_aes_128_cbc@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbp,%rcx
mov    %r13,%r8
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
jne    123e <encrypt+0xae>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rcx
mov    %r15d,%r8d
call   1060 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
jne    1250 <encrypt+0xc0>
movslq 0x4(%rsp),%rbp
add    %rbp,%r14
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
jne    126c <encrypt+0xdc>
add    0x4(%rsp),%ebp
mov    %rbx,%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
jmp    1260 <encrypt+0xd0>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
jmp    1260 <encrypt+0xd0>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <ERR_print_errors_fp@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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