#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 EVP_CIPHER_CTX_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                        unsigned char *);
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_CTRL_GCM_GET_TAG;
int /*<<< orphan*/ EVP_CTRL_GCM_SET_IVLEN;
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_256_gcm();
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pbkdf2(char *, unsigned char *, unsigned char *);
int file_encrypt(FILE *f, unsigned char *salt, char *pwd, unsigned char *iv,
                 FILE *fout, unsigned char *tag) {
  unsigned char key[32];
  unsigned char in_buff[4096];
  unsigned char out_buff[4096];
  int in_len, out_len;
  EVP_CIPHER_CTX *ctx;
  pbkdf2(pwd, salt, key);
  if (!(ctx = EVP_CIPHER_CTX_new()))
    return -1;
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    return -1;
  if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    return -1;
  if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
    return -1;
  do {
    in_len = fread(in_buff, 1, 4096, f);
    if (1 != EVP_EncryptUpdate(ctx, out_buff, &out_len, in_buff, in_len))
      return -1;
    fwrite(out_buff, 1, out_len, fout);
  } while (in_len == 4096);
  if (1 != EVP_EncryptFinal_ex(ctx, out_buff, &out_len))
    return -1;
  if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
    return -1;
  EVP_CIPHER_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<file_encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2080,%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    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1040 <pbkdf2@plt>
mov    $0x0,%al
call   10b0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x2070(%rbp)
cmp    $0x0,%rax
jne    11f8 <file_encrypt+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
mov    -0x2070(%rbp),%rax
mov    %rax,-0x2078(%rbp)
mov    $0x0,%al
call   10a0 <EVP_aes_256_gcm@plt>
mov    -0x2078(%rbp),%rdi
mov    %rax,%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1242 <file_encrypt+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
mov    -0x2070(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    $0x10,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <EVP_CIPHER_CTX_ctrl@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     127b <file_encrypt+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
mov    -0x2070(%rbp),%rdi
lea    -0x60(%rbp),%rcx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12b1 <file_encrypt+0x111>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
jmp    12b6 <file_encrypt+0x116>
lea    -0x1060(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x2064(%rbp)
mov    -0x2070(%rbp),%rdi
lea    -0x2060(%rbp),%rsi
lea    -0x1060(%rbp),%rcx
mov    -0x2064(%rbp),%r8d
lea    -0x2068(%rbp),%rdx
call   1060 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1319 <file_encrypt+0x179>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
lea    -0x2060(%rbp),%rdi
mov    -0x2068(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmpl   $0x1000,-0x2064(%rbp)
je     12b6 <file_encrypt+0x116>
mov    -0x2070(%rbp),%rdi
lea    -0x2060(%rbp),%rsi
lea    -0x2068(%rbp),%rdx
call   1070 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1379 <file_encrypt+0x1d9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
mov    -0x2070(%rbp),%rdi
mov    0x2c61(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rcx
mov    $0x10,%edx
call   1090 <EVP_CIPHER_CTX_ctrl@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     13b2 <file_encrypt+0x212>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c5 <file_encrypt+0x225>
mov    -0x2070(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2080,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2038,%rsp
mov    %r9,%r15
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%rax
mov    %rdi,%r14
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1040 <pbkdf2@plt>
xor    %eax,%eax
call   10b0 <EVP_CIPHER_CTX_new@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     12fd <file_encrypt+0x15d>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <EVP_aes_256_gcm@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
jne    12fd <file_encrypt+0x15d>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1090 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x1,%eax
jne    12fd <file_encrypt+0x15d>
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r13,%r8
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
jne    12fd <file_encrypt+0x15d>
mov    %r15,0x8(%rsp)
lea    0x1030(%rsp),%r13
lea    0x30(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %rbp,%rsi
lea    0x4(%rsp),%rdx
mov    %r13,%rcx
mov    %eax,%r8d
call   1060 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
jne    12f8 <file_encrypt+0x158>
mov    0x4(%rsp),%edx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   10c0 <fwrite@plt>
cmp    $0x1000,%r15d
je     1260 <file_encrypt+0xc0>
lea    0x30(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
mov    $0xffffffff,%ebp
jne    12fd <file_encrypt+0x15d>
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x10,%edx
mov    0x8(%rsp),%rcx
call   1090 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x1,%eax
jne    12fd <file_encrypt+0x15d>
mov    %rbx,%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
xor    %ebp,%ebp
jmp    12fd <file_encrypt+0x15d>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x2038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
@EVP_CTRL_GCM_GET_TAG = global i32 0
define i32 @file_encrypt(i32* %0, i8* %1, i8* %2, i8* %3, i32* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca [32 x i8]
%15 = alloca [4096 x i8]
%16 = alloca [4096 x i8]
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32*
store i32* %0, i32** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i32* %4, i32** %12
store i8* %5, i8** %13
%20 = load i8*, i8** %10
%21 = load i8*, i8** %9
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%23 = call i32 @pbkdf2(i8* %20, i8* %21, i8* %22)
%24 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %24, i32** %19
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %6
store i32 -1, i32* %7
br label %82
27: ; preds = %6
%28 = load i32*, i32** %19
%29 = call i32* (...) @EVP_aes_256_gcm()
%30 = call i32 @EVP_EncryptInit_ex(i32* %28, i32* %29, i32* null, i8* null, i8* null)
%31 = icmp ne i32 1, %30
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %7
br label %82
33: ; preds = %27
%34 = load i32*, i32** %19
%35 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%36 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %34, i32 %35, i32 16, i8* null)
%37 = icmp ne i32 1, %36
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %7
br label %82
39: ; preds = %33
%40 = load i32*, i32** %19
%41 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%42 = load i8*, i8** %11
%43 = call i32 @EVP_EncryptInit_ex(i32* %40, i32* null, i32* null, i8* %41, i8* %42)
%44 = icmp ne i32 1, %43
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 -1, i32* %7
br label %82
46: ; preds = %39
br label %47
47: ; preds = %63, %46
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %15, i64 0, i64 0
%49 = load i32*, i32** %8
%50 = call i32 @fread(i8* %48, i32 1, i32 4096, i32* %49)
store i32 %50, i32* %17
%51 = load i32*, i32** %19
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %15, i64 0, i64 0
%54 = load i32, i32* %17
%55 = call i32 @EVP_EncryptUpdate(i32* %51, i8* %52, i32* %18, i8* %53, i32 %54)
%56 = icmp ne i32 1, %55
br i1 %56, label %57, label %58
57: ; preds = %47
store i32 -1, i32* %7
br label %82
58: ; preds = %47
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%60 = load i32, i32* %18
%61 = load i32*, i32** %12
%62 = call i32 @fwrite(i8* %59, i32 1, i32 %60, i32* %61)
br label %63
63: ; preds = %58
%64 = load i32, i32* %17
%65 = icmp eq i32 %64, 4096
br i1 %65, label %47, label %66
66: ; preds = %63
%67 = load i32*, i32** %19
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%69 = call i32 @EVP_EncryptFinal_ex(i32* %67, i8* %68, i32* %18)
%70 = icmp ne i32 1, %69
br i1 %70, label %71, label %72
71: ; preds = %66
store i32 -1, i32* %7
br label %82
72: ; preds = %66
%73 = load i32*, i32** %19
%74 = load i32, i32* @EVP_CTRL_GCM_GET_TAG
%75 = load i8*, i8** %13
%76 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %73, i32 %74, i32 16, i8* %75)
%77 = icmp ne i32 1, %76
br i1 %77, label %78, label %79
78: ; preds = %72
store i32 -1, i32* %7
br label %82
79: ; preds = %72
%80 = load i32*, i32** %19
%81 = call i32 @EVP_CIPHER_CTX_free(i32* %80)
store i32 0, i32* %7
br label %82
82: ; preds = %79, %78, %71, %57, %45, %38, %32, %26
%83 = load i32, i32* %7
ret i32 %83
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
@EVP_CTRL_GCM_GET_TAG = global i32 0
define i32 @file_encrypt(i32* nocapture %0, i8* %1, i8* %2, i8* %3, i32* nocapture %4, i8* %5) {
%7 = alloca [32 x i8]
%8 = alloca [4096 x i8]
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%14 = bitcast i32* %10 to i8*
%15 = call i32 @pbkdf2(i8* %2, i8* %1, i8* %11)
%16 = call i32* (...) @EVP_CIPHER_CTX_new()
%17 = icmp eq i32* %16, null
br i1 %17, label %46, label %18
18: ; preds = %6
%19 = call i32* (...) @EVP_aes_256_gcm()
%20 = call i32 @EVP_EncryptInit_ex(i32* %16, i32* %19, i32* null, i8* null, i8* null)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%24 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %16, i32 %23, i32 16, i8* null)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %46
26: ; preds = %22
%27 = call i32 @EVP_EncryptInit_ex(i32* %16, i32* null, i32* null, i8* %11, i8* %3)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %46
29: ; preds = %26, %33
%30 = call i32 @fread(i8* %12, i32 1, i32 4096, i32* %0)
%31 = call i32 @EVP_EncryptUpdate(i32* %16, i8* %13, i32* %10, i8* %12, i32 %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load i32, i32* %10
%35 = call i32 @fwrite(i8* %13, i32 1, i32 %34, i32* %4)
%36 = icmp eq i32 %30, 4096
br i1 %36, label %29, label %37
37: ; preds = %33
%38 = call i32 @EVP_EncryptFinal_ex(i32* %16, i8* %13, i32* %10)
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = load i32, i32* @EVP_CTRL_GCM_GET_TAG
%42 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %16, i32 %41, i32 16, i8* %5)
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @EVP_CIPHER_CTX_free(i32* %16)
br label %46
46: ; preds = %29, %40, %37, %26, %22, %18, %6, %44
%47 = phi i32 [ 0, %44 ], [ -1, %6 ], [ -1, %18 ], [ -1, %22 ], [ -1, %26 ], [ -1, %37 ], [ -1, %40 ], [ -1, %29 ]
ret i32 %47
}