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

/* Variables and functions */
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                       int *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *, unsigned char *,
                                      unsigned char *);
int /*<<< orphan*/ EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
int valid_code_do_crypt(char *outfile) {
  unsigned char outbuf[1024];
  int outlen, tmplen;
  unsigned char key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  unsigned char iv[] = {1, 2, 3, 4, 5, 6, 7, 8};
  char intext[] = "Some Crypto Text";
  EVP_CIPHER_CTX *ctx;
  FILE *out;
  ctx = EVP_CIPHER_CTX_new();
  EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
  if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
    EVP_CIPHER_CTX_free(ctx);
    return 0;
  }
  if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
    EVP_CIPHER_CTX_free(ctx);
    return 0;
  }
  outlen += tmplen;
  EVP_CIPHER_CTX_free(ctx);
  out = fopen(outfile, "wb");
  if (out == NULL) {
    return 0;
  }
  fwrite(outbuf, 1, outlen, out);
  fclose(out);
  return 1;
}


x86 assembly (clang, -O0):
<valid_code_do_crypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe4a(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    0xe44(%rip),%rax
mov    %rax,-0x428(%rbp)
mov    0xe3e(%rip),%rax
mov    %rax,-0x438(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x450(%rbp)
mov    0xe3a(%rip),%rax
mov    %rax,-0x448(%rbp)
mov    0xe35(%rip),%al
mov    %al,-0x440(%rbp)
mov    $0x0,%al
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x458(%rbp)
mov    -0x458(%rbp),%rax
mov    %rax,-0x480(%rbp)
mov    $0x0,%al
call   1030 <EVP_aes_256_cbc@plt>
mov    -0x480(%rbp),%rdi
mov    %eax,%esi
lea    -0x430(%rbp),%rcx
lea    -0x438(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
call   1090 <EVP_EncryptInit_ex@plt>
mov    -0x458(%rbp),%rax
mov    %rax,-0x478(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x470(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x468(%rbp)
lea    -0x450(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x478(%rbp),%rdi
mov    -0x470(%rbp),%rsi
mov    -0x468(%rbp),%rcx
mov    %eax,%r8d
lea    -0x414(%rbp),%rdx
call   1070 <EVP_EncryptUpdate@plt>
cmp    $0x0,%eax
jne    12bf <valid_code_do_crypt+0x11f>
mov    -0x458(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1384 <valid_code_do_crypt+0x1e4>
mov    -0x458(%rbp),%rdi
lea    -0x410(%rbp),%rsi
movslq -0x414(%rbp),%rax
add    %rax,%rsi
lea    -0x418(%rbp),%rdx
call   1080 <EVP_EncryptFinal_ex@plt>
cmp    $0x0,%eax
jne    1304 <valid_code_do_crypt+0x164>
mov    -0x458(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1384 <valid_code_do_crypt+0x1e4>
mov    -0x418(%rbp),%eax
add    -0x414(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    -0x458(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
mov    -0x10(%rbp),%rdi
lea    0xd04(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x460(%rbp)
cmpq   $0x0,-0x460(%rbp)
jne    1353 <valid_code_do_crypt+0x1b3>
movl   $0x0,-0x4(%rbp)
jmp    1384 <valid_code_do_crypt+0x1e4>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%edx
mov    -0x460(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x460(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_code_do_crypt>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rdi,%r14
movaps 0xe48(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0x807060504030201,%rax
06 07 08
mov    %rax,0x8(%rsp)
movaps 0xe3d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x20(%rsp)
xor    %r15d,%r15d
xor    %eax,%eax
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <EVP_aes_256_cbc@plt>
lea    0x30(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1090 <EVP_EncryptInit_ex@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    0x40(%rsp),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
mov    %eax,%r8d
call   1070 <EVP_EncryptUpdate@plt>
test   %eax,%eax
je     1296 <valid_code_do_crypt+0xf6>
movslq (%rsp),%rax
lea    (%rsp,%rax,1),%rsi
add    $0x40,%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <EVP_EncryptFinal_ex@plt>
test   %eax,%eax
je     12a0 <valid_code_do_crypt+0x100>
mov    0x4(%rsp),%eax
add    %eax,(%rsp)
mov    %rbx,%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
lea    0xdc0(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a8 <valid_code_do_crypt+0x108>
mov    %rax,%rbx
mov    (%rsp),%edx
lea    0x40(%rsp),%rdi
mov    $0x1,%r15d
mov    $0x1,%esi
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12ab <valid_code_do_crypt+0x10b>
mov    %rbx,%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
jmp    12ab <valid_code_do_crypt+0x10b>
mov    %rbx,%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.valid_code_do_crypt.key = [16 x i8] c"\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F"
@__const.valid_code_do_crypt.iv = [8 x i8] c"\01\02\03\04\05\06\07\08"
@__const.valid_code_do_crypt.intext = [17 x i8] c"Some Crypto Text\00"
@.str = [3 x i8] c"wb\00"
define i32 @valid_code_do_crypt(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca [8 x i8]
%9 = alloca [17 x i8]
%10 = alloca i32*
%11 = alloca i32*
store i8* %0, i8** %3
%12 = bitcast [16 x i8]* %7 to i8*
%13 = bitcast [8 x i8]* %8 to i8*
%14 = bitcast [17 x i8]* %9 to i8*
%15 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = call i32 (...) @EVP_aes_256_cbc()
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%20 = call i32 @EVP_EncryptInit_ex(i32* %16, i32 %17, i32* null, i8* %18, i8* %19)
%21 = load i32*, i32** %10
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%24 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @EVP_EncryptUpdate(i32* %21, i8* %22, i32* %5, i8* %23, i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %1
%29 = load i32*, i32** %10
%30 = call i32 @EVP_CIPHER_CTX_free(i32* %29)
store i32 0, i32* %2
br label %60
31: ; preds = %1
%32 = load i32*, i32** %10
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = call i32 @EVP_EncryptFinal_ex(i32* %32, i8* %36, i32* %6)
%38 = icmp ne i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = load i32*, i32** %10
%41 = call i32 @EVP_CIPHER_CTX_free(i32* %40)
store i32 0, i32* %2
br label %60
42: ; preds = %31
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = add nsw i32 %44, %43
store i32 %45, i32* %5
%46 = load i32*, i32** %10
%47 = call i32 @EVP_CIPHER_CTX_free(i32* %46)
%48 = load i8*, i8** %3
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %49, i32** %11
%50 = load i32*, i32** %11
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %53
52: ; preds = %42
store i32 0, i32* %2
br label %60
53: ; preds = %42
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = load i32, i32* %5
%56 = load i32*, i32** %11
%57 = call i32 @fwrite(i8* %54, i32 1, i32 %55, i32* %56)
%58 = load i32*, i32** %11
%59 = call i32 @fclose(i32* %58)
store i32 1, i32* %2
br label %60
60: ; preds = %53, %52, %39, %28
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@__const.valid_code_do_crypt.key = [16 x i8] c"\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F"
@__const.valid_code_do_crypt.intext = [17 x i8] c"Some Crypto Text\00"
@.str = [3 x i8] c"wb\00"
define i32 @valid_code_do_crypt(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = alloca i64
%7 = alloca [17 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%12 = bitcast i64* %6 to i8*
store i64 578437695752307201, i64* %6
%13 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%14 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%15 = tail call i32 (...) @EVP_aes_256_cbc()
%16 = call i32 @EVP_EncryptInit_ex(i32* %14, i32 %15, i32* null, i8* %11, i8* %12)
%17 = call i32 @strlen(i8* %13)
%18 = call i32 @EVP_EncryptUpdate(i32* %14, i8* %8, i32* %3, i8* %13, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @EVP_CIPHER_CTX_free(i32* %14)
br label %41
22: ; preds = %1
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %24
%26 = call i32 @EVP_EncryptFinal_ex(i32* %14, i8* %25, i32* %4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @EVP_CIPHER_CTX_free(i32* %14)
br label %41
30: ; preds = %22
%31 = load i32, i32* %4
%32 = load i32, i32* %3
%33 = add nsw i32 %32, %31
store i32 %33, i32* %3
%34 = call i32 @EVP_CIPHER_CTX_free(i32* %14)
%35 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %30
%38 = load i32, i32* %3
%39 = call i32 @fwrite(i8* %8, i32 1, i32 %38, i32* %35)
%40 = call i32 @fclose(i32* %35)
br label %41
41: ; preds = %30, %37, %28, %20
%42 = phi i32 [ 1, %37 ], [ 0, %28 ], [ 0, %20 ], [ 0, %30 ]
ret i32 %42
}