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


x86 assembly (clang, -O0):
<file_decrypt>:
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   1050 <pbkdf2@plt>
mov    $0x0,%al
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x2078(%rbp)
cmp    $0x0,%rax
jne    11f8 <file_decrypt+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
mov    -0x2078(%rbp),%rax
mov    %rax,-0x2080(%rbp)
mov    $0x0,%al
call   1090 <EVP_aes_256_gcm@plt>
mov    -0x2080(%rbp),%rdi
mov    %rax,%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%eax
jne    123c <file_decrypt+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
mov    -0x2078(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    $0x10,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%eax
jne    126f <file_decrypt+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
mov    -0x2078(%rbp),%rdi
lea    -0x60(%rbp),%rcx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%eax
jne    129f <file_decrypt+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
jmp    12a4 <file_decrypt+0x104>
lea    -0x1060(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x2064(%rbp)
mov    -0x2078(%rbp),%rdi
lea    -0x2060(%rbp),%rsi
lea    -0x1060(%rbp),%rcx
mov    -0x2064(%rbp),%r8d
lea    -0x2068(%rbp),%rdx
call   10b0 <EVP_DecryptUpdate@plt>
cmp    $0x0,%eax
jne    1301 <file_decrypt+0x161>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
lea    -0x2060(%rbp),%rdi
mov    -0x2068(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmpl   $0x1000,-0x2064(%rbp)
je     12a4 <file_decrypt+0x104>
mov    -0x2078(%rbp),%rdi
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rcx
mov    $0x10,%edx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
cmp    $0x0,%eax
jne    135f <file_decrypt+0x1bf>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
mov    -0x2078(%rbp),%rdi
lea    -0x2060(%rbp),%rsi
lea    -0x2068(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,-0x206c(%rbp)
mov    -0x2078(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
cmpl   $0x0,-0x206c(%rbp)
jg     13a4 <file_decrypt+0x204>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <file_decrypt+0x20b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2080,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2038,%rsp
mov    %r9,%r12
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdx,%rax
mov    %rdi,%r14
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1050 <pbkdf2@plt>
xor    %eax,%eax
call   10a0 <EVP_CIPHER_CTX_new@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1300 <file_decrypt+0x160>
mov    %rax,%r13
xor    %eax,%eax
call   1090 <EVP_aes_256_gcm@plt>
mov    %r13,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <EVP_DecryptInit_ex@plt>
test   %eax,%eax
je     1300 <file_decrypt+0x160>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
test   %eax,%eax
je     1300 <file_decrypt+0x160>
lea    0x10(%rsp),%rcx
mov    %r13,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %rbx,%r8
call   1070 <EVP_DecryptInit_ex@plt>
test   %eax,%eax
je     1300 <file_decrypt+0x160>
mov    %r15,0x8(%rsp)
lea    0x1030(%rsp),%rbp
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
mov    %r13,%rdi
mov    %rbx,%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rcx
mov    %eax,%r8d
call   10b0 <EVP_DecryptUpdate@plt>
test   %eax,%eax
je     12fb <file_decrypt+0x15b>
mov    0x4(%rsp),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   10c0 <fwrite@plt>
cmp    $0x1000,%r15d
je     1260 <file_decrypt+0xc0>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
mov    $0x10,%edx
mov    0x8(%rsp),%rcx
call   1080 <EVP_CIPHER_CTX_ctrl@plt>
test   %eax,%eax
mov    $0xffffffff,%ebp
je     1300 <file_decrypt+0x160>
lea    0x30(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r13,%rdi
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
xor    %ebp,%ebp
test   %ebx,%ebx
setle  %bpl
neg    %ebp
jmp    1300 <file_decrypt+0x160>
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_SET_TAG = global i32 0
define i32 @file_decrypt(i32* %0, i8* %1, i8* %2, i8* %3, i8* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca [32 x i8]
%15 = alloca [4096 x i8]
%16 = alloca [4096 x i8]
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32*
store i32* %0, i32** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i8* %4, i8** %12
store i32* %5, i32** %13
%21 = load i8*, i8** %10
%22 = load i8*, i8** %9
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%24 = call i32 @pbkdf2(i8* %21, i8* %22, i8* %23)
%25 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %25, i32** %20
%26 = icmp ne i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %6
store i32 -1, i32* %7
br label %84
28: ; preds = %6
%29 = load i32*, i32** %20
%30 = call i32* (...) @EVP_aes_256_gcm()
%31 = call i32 @EVP_DecryptInit_ex(i32* %29, i32* %30, i32* null, i8* null, i8* null)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %28
store i32 -1, i32* %7
br label %84
34: ; preds = %28
%35 = load i32*, i32** %20
%36 = load i32, i32* @EVP_CTRL_GCM_SET_IVLEN
%37 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %35, i32 %36, i32 16, i8* null)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %34
store i32 -1, i32* %7
br label %84
40: ; preds = %34
%41 = load i32*, i32** %20
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %14, i64 0, i64 0
%43 = load i8*, i8** %11
%44 = call i32 @EVP_DecryptInit_ex(i32* %41, i32* null, i32* null, i8* %42, i8* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %40
store i32 -1, i32* %7
br label %84
47: ; preds = %40
br label %48
48: ; preds = %64, %47
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %15, i64 0, i64 0
%50 = load i32*, i32** %8
%51 = call i32 @fread(i8* %49, i32 1, i32 4096, i32* %50)
store i32 %51, i32* %17
%52 = load i32*, i32** %20
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %15, i64 0, i64 0
%55 = load i32, i32* %17
%56 = call i32 @EVP_DecryptUpdate(i32* %52, i8* %53, i32* %18, i8* %54, i32 %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %48
store i32 -1, i32* %7
br label %84
59: ; preds = %48
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%61 = load i32, i32* %18
%62 = load i32*, i32** %13
%63 = call i32 @fwrite(i8* %60, i32 1, i32 %61, i32* %62)
br label %64
64: ; preds = %59
%65 = load i32, i32* %17
%66 = icmp eq i32 %65, 4096
br i1 %66, label %48, label %67
67: ; preds = %64
%68 = load i32*, i32** %20
%69 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%70 = load i8*, i8** %12
%71 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %68, i32 %69, i32 16, i8* %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %74, label %73
73: ; preds = %67
store i32 -1, i32* %7
br label %84
74: ; preds = %67
%75 = load i32*, i32** %20
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %16, i64 0, i64 0
%77 = call i32 @EVP_DecryptFinal_ex(i32* %75, i8* %76, i32* %18)
store i32 %77, i32* %19
%78 = load i32*, i32** %20
%79 = call i32 @EVP_CIPHER_CTX_free(i32* %78)
%80 = load i32, i32* %19
%81 = icmp sgt i32 %80, 0
br i1 %81, label %83, label %82
82: ; preds = %74
store i32 -1, i32* %7
br label %84
83: ; preds = %74
store i32 0, i32* %7
br label %84
84: ; preds = %83, %82, %73, %58, %46, %39, %33, %27
%85 = load i32, i32* %7
ret i32 %85
}

LLVM IR (-O2):
@EVP_CTRL_GCM_SET_IVLEN = global i32 0
@EVP_CTRL_GCM_SET_TAG = global i32 0
define i32 @file_decrypt(i32* nocapture %0, i8* %1, i8* %2, i8* %3, i8* %4, i32* nocapture %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_DecryptInit_ex(i32* %16, i32* %19, i32* null, i8* null, i8* null)
%21 = icmp eq i32 %20, 0
br i1 %21, label %46, label %22
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, 0
br i1 %25, label %46, label %26
26: ; preds = %22
%27 = call i32 @EVP_DecryptInit_ex(i32* %16, i32* null, i32* null, i8* %11, i8* %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %46, label %29
29: ; preds = %26, %33
%30 = call i32 @fread(i8* %12, i32 1, i32 4096, i32* %0)
%31 = call i32 @EVP_DecryptUpdate(i32* %16, i8* %13, i32* %10, i8* %12, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %46, label %33
33: ; preds = %29
%34 = load i32, i32* %10
%35 = call i32 @fwrite(i8* %13, i32 1, i32 %34, i32* %5)
%36 = icmp eq i32 %30, 4096
br i1 %36, label %29, label %37
37: ; preds = %33
%38 = load i32, i32* @EVP_CTRL_GCM_SET_TAG
%39 = call i32 @EVP_CIPHER_CTX_ctrl(i32* %16, i32 %38, i32 16, i8* %4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %37
%42 = call i32 @EVP_DecryptFinal_ex(i32* %16, i8* %13, i32* %10)
%43 = call i32 @EVP_CIPHER_CTX_free(i32* %16)
%44 = icmp slt i32 %42, 1
%45 = sext i1 %44 to i32
br label %46
46: ; preds = %29, %41, %37, %26, %22, %18, %6
%47 = phi i32 [ -1, %6 ], [ -1, %18 ], [ -1, %22 ], [ -1, %26 ], [ -1, %37 ], [ %45, %41 ], [ -1, %29 ]
ret i32 %47
}