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

/* Variables and functions */
double CLOCKS_PER_SEC;
int RSA_PKCS1_PADDING;
int RSA_private_decrypt(int, unsigned char *, unsigned char *,
                        int /*<<< orphan*/ *, int);
int RSA_size(int /*<<< orphan*/ *);
scalar_t__ clock();
int /*<<< orphan*/ *createRSA(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void decrypt(const char *keyFile, const char *inFileName,
             const char *outFileName) {
  RSA *rsa = createRSA(keyFile, 0);
  clock_t begin = clock();
  FILE *inFile;
  FILE *outFile;
  unsigned char *inBuffer = NULL;
  unsigned char *outBuffer = NULL;
  int padding = RSA_PKCS1_PADDING;
  int blockSize = RSA_size(rsa);
  int readBytes = 0;
  int decSize = 0;
  inBuffer = malloc(blockSize * sizeof(char));
  outBuffer = malloc(blockSize * sizeof(char));
  if ((inFile = fopen(inFileName, "r")) == NULL) {
    printf("Open file error occurred\n");
    exit(1);
  }
  if ((outFile = fopen(outFileName, "w")) == NULL) {
    printf("Open file error occurred\n");
    exit(1);
  }
  while (1) {
    memset(inBuffer, 0, blockSize);
    if ((readBytes = fread(inBuffer, 1, blockSize, inFile)) == -1) {
      printf("Read error occurred\n");
      break;
    }
    if (!readBytes)
      break;
    memset(outBuffer, 0, blockSize);
    decSize = RSA_private_decrypt(blockSize, inBuffer, outBuffer, rsa, padding);
    if (decSize < 0) {
      printf("Error while decrypting data block\n");
      break;
    }
    if (fwrite(outBuffer, 1, decSize, outFile) == -1) {
      printf("Error while writing decrypted data block\n");
      break;
    }
  }
  printf("DONE\n");
  free(inBuffer);
  free(outBuffer);
  clock_t end = clock();
  printf("Elapsed: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
}


x86 assembly (clang, -O0):
<decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   10a0 <createRSA@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <RSA_size@plt>
mov    %eax,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movslq -0x50(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
movslq -0x50(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1280 <decrypt+0xc0>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12b6 <decrypt+0xf6>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12bb <decrypt+0xfb>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    12fd <decrypt+0x13d>
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1388 <decrypt+0x1c8>
cmpl   $0x0,-0x54(%rbp)
jne    130c <decrypt+0x14c>
jmp    1388 <decrypt+0x1c8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x50(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x4c(%rbp),%r8d
call   10e0 <RSA_private_decrypt@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    1352 <decrypt+0x192>
lea    0xced(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1388 <decrypt+0x1c8>
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    1383 <decrypt+0x1c3>
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1388 <decrypt+0x1c8>
jmp    12bb <decrypt+0xfb>
lea    0xcf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c21(%rip),%rax
divsd  (%rax),%xmm0
lea    0xcb4(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %rsi,%r14
xor    %esi,%esi
call   10b0 <createRSA@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1070 <clock@plt>
mov    %rax,0x10(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r12,%rdi
call   1060 <RSA_size@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
lea    0xdd4(%rip),%rsi
mov    %r14,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1335 <decrypt+0x165>
mov    %rax,%r14
lea    0xdbb(%rip),%rsi
mov    %r13,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1335 <decrypt+0x165>
mov    %rax,%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1090 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0xffffffff,%eax
je     12cc <decrypt+0xfc>
test   %eax,%eax
je     12e1 <decrypt+0x111>
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1090 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    0xc(%rsp),%r8d
call   10f0 <RSA_private_decrypt@plt>
test   %eax,%eax
js     12d5 <decrypt+0x105>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r13,%rcx
call   10e0 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    1260 <decrypt+0x90>
lea    0xd54(%rip),%rdi
jmp    12dc <decrypt+0x10c>
lea    0xd96(%rip),%rdi
jmp    12dc <decrypt+0x10c>
lea    0xd6b(%rip),%rdi
call   1040 <puts@plt>
lea    0xd31(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1070 <clock@plt>
sub    0x10(%rsp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2cd3(%rip),%rax
divsd  (%rax),%xmm0
lea    0xce4(%rip),%rdi
mov    $0x1,%al
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <printf@plt>
lea    0xd41(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@RSA_PKCS1_PADDING = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Open file error occurred\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"Read error occurred\0A\00"
@.str.4 = [35 x i8] c"Error while decrypting data block\0A\00"
@.str.5 = [42 x i8] c"Error while writing decrypted data block\0A\00"
@.str.6 = [6 x i8] c"DONE\0A\00"
@.str.7 = [21 x i8] c"Elapsed: %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define void @decrypt(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%18 = load i8*, i8** %4
%19 = call i32* @createRSA(i8* %18, i32 0)
store i32* %19, i32** %7
%20 = call i64 (...) @clock()
store i64 %20, i64* %8
store i8* null, i8** %11
store i8* null, i8** %12
%21 = load i32, i32* @RSA_PKCS1_PADDING
store i32 %21, i32* %13
%22 = load i32*, i32** %7
%23 = call i32 @RSA_size(i32* %22)
store i32 %23, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
%24 = load i32, i32* %14
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %11
%29 = load i32, i32* %14
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 1
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %12
%34 = load i8*, i8** %5
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %35, i32** %9
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %3
%41 = load i8*, i8** %6
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %10
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
br label %48
48: ; preds = %47, %85
%49 = load i8*, i8** %11
%50 = load i32, i32* %14
%51 = call i32 @memset(i8* %49, i32 0, i32 %50)
%52 = load i8*, i8** %11
%53 = load i32, i32* %14
%54 = load i32*, i32** %9
%55 = call i32 @fread(i8* %52, i32 1, i32 %53, i32* %54)
store i32 %55, i32* %15
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %48
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %86
59: ; preds = %48
%60 = load i32, i32* %15
%61 = icmp ne i32 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %59
br label %86
63: ; preds = %59
%64 = load i8*, i8** %12
%65 = load i32, i32* %14
%66 = call i32 @memset(i8* %64, i32 0, i32 %65)
%67 = load i32, i32* %14
%68 = load i8*, i8** %11
%69 = load i8*, i8** %12
%70 = load i32*, i32** %7
%71 = load i32, i32* %13
%72 = call i32 @RSA_private_decrypt(i32 %67, i8* %68, i8* %69, i32* %70, i32 %71)
store i32 %72, i32* %16
%73 = load i32, i32* %16
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %63
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %86
77: ; preds = %63
%78 = load i8*, i8** %12
%79 = load i32, i32* %16
%80 = load i32*, i32** %10
%81 = call i32 @fwrite(i8* %78, i32 1, i32 %79, i32* %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %77
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %86
85: ; preds = %77
br label %48
86: ; preds = %83, %75, %62, %57
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%88 = load i8*, i8** %11
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %12
%91 = call i32 @free(i8* %90)
%92 = call i64 (...) @clock()
store i64 %92, i64* %17
%93 = load i64, i64* %17
%94 = load i64, i64* %8
%95 = sub nsw i64 %93, %94
%96 = sitofp i64 %95 to double
%97 = load double, double* @CLOCKS_PER_SEC
%98 = fdiv double %96, %97
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), double %98)
ret void
}

LLVM IR (-O2):
@RSA_PKCS1_PADDING = global i32 0
@.str = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.7 = [21 x i8] c"Elapsed: %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@str = [5 x i8] c"DONE\00"
@str.8 = [41 x i8] c"Error while writing decrypted data block\00"
@str.9 = [34 x i8] c"Error while decrypting data block\00"
@str.10 = [20 x i8] c"Read error occurred\00"
@str.12 = [25 x i8] c"Open file error occurred\00"
define void @decrypt(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = tail call i32* @createRSA(i8* %0, i32 0)
%5 = tail call i64 (...) @clock()
%6 = load i32, i32* @RSA_PKCS1_PADDING
%7 = tail call i32 @RSA_size(i32* %4)
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i8* @malloc(i32 %7)
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15, %28
%22 = tail call i32 @memset(i8* %8, i32 0, i32 %7)
%23 = tail call i32 @fread(i8* %8, i32 1, i32 %7, i32* %10)
switch i32 %23, label %24 [
i32 -1, label %31
i32 0, label %34
]
24: ; preds = %21
%25 = tail call i32 @memset(i8* %9, i32 0, i32 %7)
%26 = tail call i32 @RSA_private_decrypt(i32 %7, i8* %8, i8* %9, i32* %4, i32 %6)
%27 = icmp slt i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = tail call i32 @fwrite(i8* %9, i32 1, i32 %26, i32* %16)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %21
31: ; preds = %28, %24, %21
%32 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), %21 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @str.9, i64 0, i64 0), %24 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0), %28 ]
%33 = tail call i32 @puts(i8* dereferenceable(1) %32)
br label %34
34: ; preds = %21, %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @free(i8* %8)
%37 = tail call i32 @free(i8* %9)
%38 = tail call i64 (...) @clock()
%39 = sub nsw i64 %38, %5
%40 = sitofp i64 %39 to double
%41 = load double, double* @CLOCKS_PER_SEC
%42 = fdiv double %40, %41
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), double %42)
ret void
}