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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
unsigned long fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ symDecrypt(char *, char const *, char *, scalar_t__);
int /*<<< orphan*/ symEncrypt(char *, char const *, char *, scalar_t__);
bool partialEncryptFile(const char *keyiv, const char *filename,
                        int64_t number_of_bytes, bool decrypt) {
  FILE *hostage = fopen(filename, "rb");
  if (hostage == NULL) {
    fprintf(stderr,
            "Error: partialEncryptFile: cannot open file for reading\n");
    return 0;
  }
  fseek(hostage, 0, SEEK_END);
  int64_t file_size = ftell(hostage);
  fseek(hostage, 0, SEEK_SET);
  number_of_bytes = file_size > number_of_bytes ? number_of_bytes : file_size;
  char plaintext[16 * 1024];
  memset(plaintext, '\0', 16 * 1024);
  fread(plaintext, 1, number_of_bytes, hostage);
  char ciphertext[16 * 1024];
  memset(ciphertext, '\0', 16 * 1024);
  if (decrypt) {
    symDecrypt(ciphertext, keyiv, plaintext, number_of_bytes);
  } else {
    symEncrypt(ciphertext, keyiv, plaintext, number_of_bytes);
  }
  fclose(hostage);
  if ((hostage = fopen(filename, "r+b")) == NULL) {
    fprintf(stderr,
            "Error: partialEncryptFile: failed to open file for writing(r+b)");
    return 0;
  }
  if (fwrite(ciphertext, 1, number_of_bytes, hostage) !=
      (unsigned long)number_of_bytes) {
    fprintf(stderr, "Error: partialEncryptFile: writing failed\n");
    return 0;
  }
  fclose(hostage);
  return 1;
}


x86 assembly (clang, -O0):
<partialEncryptFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fa <partialEncryptFile+0x5a>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1383 <partialEncryptFile+0x1e3>
mov    -0x30(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jle    124d <partialEncryptFile+0xad>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8048(%rbp)
jmp    1258 <partialEncryptFile+0xb8>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8048(%rbp)
mov    -0x8048(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x4040(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1060 <memset@plt>
lea    -0x4040(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
lea    -0x8040(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1060 <memset@plt>
cmpl   $0x0,-0x24(%rbp)
je     12cc <partialEncryptFile+0x12c>
lea    -0x8040(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x4040(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <symDecrypt@plt>
jmp    12e7 <partialEncryptFile+0x147>
lea    -0x8040(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x4040(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   10c0 <symEncrypt@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xd41(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    132f <partialEncryptFile+0x18f>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1383 <partialEncryptFile+0x1e3>
lea    -0x8040(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     1373 <partialEncryptFile+0x1d3>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
lea    0xd1e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1383 <partialEncryptFile+0x1e3>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<partialEncryptFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8008,%rsp
mov    %ecx,%r13d
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe3c(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1266 <partialEncryptFile+0xc6>
mov    %rax,%rbx
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    %rbp,%r15
cmovg  %rbp,%r15
lea    0x4000(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1060 <memset@plt>
mov    %rsp,%rdi
lea    0x4000(%rsp),%rdx
mov    %r12,%rsi
mov    %r15,%rcx
test   %r13d,%r13d
je     1278 <partialEncryptFile+0xd8>
call   1050 <symDecrypt@plt>
jmp    127d <partialEncryptFile+0xdd>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
jmp    12e5 <partialEncryptFile+0x145>
call   10c0 <symEncrypt@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdb0(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c3 <partialEncryptFile+0x123>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
cmp    %r15,%rax
jne    12d5 <partialEncryptFile+0x135>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    12ec <partialEncryptFile+0x14c>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
jmp    12e5 <partialEncryptFile+0x145>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"Error: partialEncryptFile: cannot open file for reading\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"r+b\00"
@.str.3 = [64 x i8] c"Error: partialEncryptFile: failed to open file for writing(r+b)\00"
@.str.4 = [43 x i8] c"Error: partialEncryptFile: writing failed\0A\00"
define i32 @partialEncryptFile(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i64
%12 = alloca [16384 x i8]
%13 = alloca [16384 x i8]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%14 = load i8*, i8** %7
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %83
21: ; preds = %4
%22 = load i32*, i32** %10
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %10
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %11
%27 = load i32*, i32** %10
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i64, i64* %11
%31 = load i64, i64* %8
%32 = icmp sgt i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = load i64, i64* %8
br label %37
35: ; preds = %21
%36 = load i64, i64* %11
br label %37
37: ; preds = %35, %33
%38 = phi i64 [ %34, %33 ], [ %36, %35 ]
store i64 %38, i64* %8
%39 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i8 signext 0, i32 16384)
%41 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%42 = load i64, i64* %8
%43 = load i32*, i32** %10
%44 = call i32 @fread(i8* %41, i32 1, i64 %42, i32* %43)
%45 = getelementptr inbounds [16384 x i8], [16384 x i8]* %13, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i8 signext 0, i32 16384)
%47 = load i32, i32* %9
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %37
%50 = getelementptr inbounds [16384 x i8], [16384 x i8]* %13, i64 0, i64 0
%51 = load i8*, i8** %6
%52 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%53 = load i64, i64* %8
%54 = call i32 @symDecrypt(i8* %50, i8* %51, i8* %52, i64 %53)
br label %61
55: ; preds = %37
%56 = getelementptr inbounds [16384 x i8], [16384 x i8]* %13, i64 0, i64 0
%57 = load i8*, i8** %6
%58 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%59 = load i64, i64* %8
%60 = call i32 @symEncrypt(i8* %56, i8* %57, i8* %58, i64 %59)
br label %61
61: ; preds = %55, %49
%62 = load i32*, i32** %10
%63 = call i32 @fclose(i32* %62)
%64 = load i8*, i8** %7
%65 = call i32* @fopen(i8* %64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %65, i32** %10
%66 = icmp eq i32* %65, null
br i1 %66, label %67, label %70
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %83
70: ; preds = %61
%71 = getelementptr inbounds [16384 x i8], [16384 x i8]* %13, i64 0, i64 0
%72 = load i64, i64* %8
%73 = load i32*, i32** %10
%74 = call i64 @fwrite(i8* %71, i32 1, i64 %72, i32* %73)
%75 = load i64, i64* %8
%76 = icmp ne i64 %74, %75
br i1 %76, label %77, label %80
77: ; preds = %70
%78 = load i32, i32* @stderr
%79 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %83
80: ; preds = %70
%81 = load i32*, i32** %10
%82 = call i32 @fclose(i32* %81)
store i32 1, i32* %5
br label %83
83: ; preds = %80, %77, %67, %18
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"Error: partialEncryptFile: cannot open file for reading\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"r+b\00"
@.str.3 = [64 x i8] c"Error: partialEncryptFile: failed to open file for writing(r+b)\00"
@.str.4 = [43 x i8] c"Error: partialEncryptFile: writing failed\0A\00"
define i32 @partialEncryptFile(i8* %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = alloca [16384 x i8]
%6 = alloca [16384 x i8]
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0))
br label %47
12: ; preds = %4
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %7, i32 0, i32 %13)
%15 = tail call i64 @ftell(i32* %7)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %7, i32 0, i32 %16)
%18 = icmp sgt i64 %15, %2
%19 = select i1 %18, i64 %2, i64 %15
%20 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i8 signext 0, i32 16384)
%22 = call i32 @fread(i8* %20, i32 1, i64 %19, i32* %7)
%23 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i8 signext 0, i32 16384)
%25 = icmp eq i32 %3, 0
br i1 %25, label %28, label %26
26: ; preds = %12
%27 = call i32 @symDecrypt(i8* %23, i8* %0, i8* %20, i64 %19)
br label %30
28: ; preds = %12
%29 = call i32 @symEncrypt(i8* %23, i8* %0, i8* %20, i64 %19)
br label %30
30: ; preds = %28, %26
%31 = call i32 @fclose(i32* %7)
%32 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0))
br label %45
37: ; preds = %30
%38 = call i64 @fwrite(i8* %23, i32 1, i64 %19, i32* %32)
%39 = icmp eq i64 %38, %19
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %45
43: ; preds = %37
%44 = call i32 @fclose(i32* %32)
br label %45
45: ; preds = %43, %40, %34
%46 = phi i32 [ 0, %34 ], [ 0, %40 ], [ 1, %43 ]
br label %47
47: ; preds = %45, %9
%48 = phi i32 [ 0, %9 ], [ %46, %45 ]
ret i32 %48
}