#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_public_encrypt(int, unsigned char *, unsigned char *,
                       int /*<<< orphan*/ *, int);
int RSA_size(int /*<<< orphan*/ *);
scalar_t__ clock();
int /*<<< orphan*/ *createRSA(char const *, int);
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 encrypt(const char *keyFile, const char *inFileName,
             const char *outFileName) {
  RSA *rsa = createRSA(keyFile, 1);
  clock_t begin = clock();
  FILE *inFile;
  FILE *outFile;
  unsigned char *inBuffer = NULL;
  unsigned char *outBuffer = NULL;
  int padding = RSA_PKCS1_PADDING;
  int writeSize = RSA_size(rsa);
  int readSize = writeSize - 11;
  int readBytes = 0;
  int encSize = 0;
  inBuffer = malloc(readSize * sizeof(char));
  outBuffer = malloc(writeSize * 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, readSize);
    if ((readBytes = fread(inBuffer, 1, readSize, inFile)) == -1) {
      printf("Read error occurred\n");
      break;
    }
    memset(outBuffer, 0, writeSize);
    encSize = RSA_public_encrypt(readBytes, inBuffer, outBuffer, rsa, padding);
    if (encSize < 0) {
      printf("Error while encrypting data block\n");
      break;
    }
    if (fwrite(outBuffer, 1, encSize, outFile) == -1) {
      printf("Error while writing encrypted data block\n");
      break;
    }
    if (readBytes < readSize)
      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):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   10b0 <createRSA@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <clock@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <RSA_size@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
sub    $0xb,%eax
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
movslq -0x54(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
movslq -0x50(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd9f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    128c <encrypt+0xcc>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xd85(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12c2 <encrypt+0x102>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    12c7 <encrypt+0x107>
mov    -0x40(%rbp),%rdi
mov    -0x54(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,-0x58(%rbp)
cmp    $0xffffffff,%eax
jne    1309 <encrypt+0x149>
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1396 <encrypt+0x1d6>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x58(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x4c(%rbp),%r8d
call   1040 <RSA_public_encrypt@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    134f <encrypt+0x18f>
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1396 <encrypt+0x1d6>
mov    -0x48(%rbp),%rdi
mov    -0x5c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    1380 <encrypt+0x1c0>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1396 <encrypt+0x1d6>
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1391 <encrypt+0x1d1>
jmp    1396 <encrypt+0x1d6>
jmp    12c7 <encrypt+0x107>
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1070 <clock@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
sub    -0x28(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c13(%rip),%rax
divsd  (%rax),%xmm0
lea    0xca6(%rip),%rdi
mov    $0x1,%al
call   1080 <printf@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x10(%rsp)
mov    %rsi,%r13
mov    $0x1,%esi
call   10c0 <createRSA@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <clock@plt>
mov    %rax,%r12
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,0x20(%rsp)
mov    %rbx,%rdi
call   1070 <RSA_size@plt>
mov    %eax,%ebp
lea    -0xb(%rbp),%r14d
mov    %r14d,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
mov    %ebp,%edi
call   10b0 <malloc@plt>
mov    %rax,%r15
lea    0xdc2(%rip),%rsi
mov    %r13,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1361 <encrypt+0x191>
mov    %rax,%r13
mov    %r12,0x8(%rsp)
lea    0xda4(%rip),%rsi
mov    0x10(%rsp),%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1361 <encrypt+0x191>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   10a0 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r13,%rcx
call   1060 <fread@plt>
cmp    $0xffffffff,%eax
je     12ef <encrypt+0x11f>
mov    %eax,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    0x18(%rsp),%rdx
call   10a0 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    0x20(%rsp),%rcx
mov    0x4(%rsp),%r8d
call   1040 <RSA_public_encrypt@plt>
test   %eax,%eax
js     12f8 <encrypt+0x128>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   10f0 <fwrite@plt>
cmp    $0xffffffff,%eax
je     1301 <encrypt+0x131>
cmp    %r14d,%ebp
jge    1280 <encrypt+0xb0>
jmp    130d <encrypt+0x13d>
lea    0xd73(%rip),%rdi
jmp    1308 <encrypt+0x138>
lea    0xd48(%rip),%rdi
jmp    1308 <encrypt+0x138>
lea    0xd16(%rip),%rdi
call   1050 <puts@plt>
lea    0xd05(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1080 <clock@plt>
sub    0x8(%rsp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2ca7(%rip),%rax
divsd  (%rax),%xmm0
lea    0xcb8(%rip),%rdi
mov    $0x1,%al
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <printf@plt>
lea    0xd15(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10e0 <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 encrypting data block\0A\00"
@.str.5 = [42 x i8] c"Error while writing encrypted 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 @encrypt(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 i32
%18 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%19 = load i8*, i8** %4
%20 = call i32* @createRSA(i8* %19, i32 1)
store i32* %20, i32** %7
%21 = call i64 (...) @clock()
store i64 %21, i64* %8
store i8* null, i8** %11
store i8* null, i8** %12
%22 = load i32, i32* @RSA_PKCS1_PADDING
store i32 %22, i32* %13
%23 = load i32*, i32** %7
%24 = call i32 @RSA_size(i32* %23)
store i32 %24, i32* %14
%25 = load i32, i32* %14
%26 = sub nsw i32 %25, 11
store i32 %26, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
%27 = load i32, i32* %15
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %11
%32 = load i32, i32* %14
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %12
%37 = load i8*, i8** %5
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %38, i32** %9
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %3
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %3
%44 = load i8*, i8** %6
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %45, i32** %10
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %43
br label %51
51: ; preds = %50, %89
%52 = load i8*, i8** %11
%53 = load i32, i32* %15
%54 = call i32 @memset(i8* %52, i32 0, i32 %53)
%55 = load i8*, i8** %11
%56 = load i32, i32* %15
%57 = load i32*, i32** %9
%58 = call i32 @fread(i8* %55, i32 1, i32 %56, i32* %57)
store i32 %58, i32* %16
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %51
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %90
62: ; preds = %51
%63 = load i8*, i8** %12
%64 = load i32, i32* %14
%65 = call i32 @memset(i8* %63, i32 0, i32 %64)
%66 = load i32, i32* %16
%67 = load i8*, i8** %11
%68 = load i8*, i8** %12
%69 = load i32*, i32** %7
%70 = load i32, i32* %13
%71 = call i32 @RSA_public_encrypt(i32 %66, i8* %67, i8* %68, i32* %69, i32 %70)
store i32 %71, i32* %17
%72 = load i32, i32* %17
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %62
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %90
76: ; preds = %62
%77 = load i8*, i8** %12
%78 = load i32, i32* %17
%79 = load i32*, i32** %10
%80 = call i32 @fwrite(i8* %77, i32 1, i32 %78, i32* %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %84
82: ; preds = %76
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %90
84: ; preds = %76
%85 = load i32, i32* %16
%86 = load i32, i32* %15
%87 = icmp slt i32 %85, %86
br i1 %87, label %88, label %89
88: ; preds = %84
br label %90
89: ; preds = %84
br label %51
90: ; preds = %88, %82, %74, %60
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%92 = load i8*, i8** %11
%93 = call i32 @free(i8* %92)
%94 = load i8*, i8** %12
%95 = call i32 @free(i8* %94)
%96 = call i64 (...) @clock()
store i64 %96, i64* %18
%97 = load i64, i64* %18
%98 = load i64, i64* %8
%99 = sub nsw i64 %97, %98
%100 = sitofp i64 %99 to double
%101 = load double, double* @CLOCKS_PER_SEC
%102 = fdiv double %100, %101
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), double %102)
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 encrypted data block\00"
@str.9 = [34 x i8] c"Error while encrypting 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 @encrypt(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = tail call i32* @createRSA(i8* %0, i32 1)
%5 = tail call i64 (...) @clock()
%6 = load i32, i32* @RSA_PKCS1_PADDING
%7 = tail call i32 @RSA_size(i32* %4)
%8 = add nsw i32 %7, -11
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i8* @malloc(i32 %7)
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16, %33
%23 = tail call i32 @memset(i8* %9, i32 0, i32 %8)
%24 = tail call i32 @fread(i8* %9, i32 1, i32 %8, i32* %11)
%25 = icmp eq i32 %24, -1
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = tail call i32 @memset(i8* %10, i32 0, i32 %7)
%28 = tail call i32 @RSA_public_encrypt(i32 %24, i8* %9, i8* %10, i32* %4, i32 %6)
%29 = icmp slt i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = tail call i32 @fwrite(i8* %10, i32 1, i32 %28, i32* %17)
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = icmp slt i32 %24, %8
br i1 %34, label %38, label %22
35: ; preds = %30, %26, %22
%36 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), %22 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @str.9, i64 0, i64 0), %26 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0), %30 ]
%37 = tail call i32 @puts(i8* dereferenceable(1) %36)
br label %38
38: ; preds = %33, %35
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @free(i8* %9)
%41 = tail call i32 @free(i8* %10)
%42 = tail call i64 (...) @clock()
%43 = sub nsw i64 %42, %5
%44 = sitofp i64 %43 to double
%45 = load double, double* @CLOCKS_PER_SEC
%46 = fdiv double %44, %45
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), double %46)
ret void
}