#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*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ initialize(int, char **);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pfs_close(int);
int pfs_open(char *, char);
int pfs_write(int, void *, int, int, int *);
int /*<<< orphan*/ pread(int, void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int ifdes, fdes;
  int err_value;
  char *input_fname = "tocopy.txt";
  char *buf;
  ssize_t nread;
  int cache_hit;
  initialize(argc, argv);
  ifdes = open(input_fname, O_RDONLY);
  buf = (char *)malloc(5 * 1024);
  nread = pread(ifdes, (void *)buf, 4 * 1024, 4 * 1024);
  fdes = pfs_open("pfs_file1", 'w');
  if (fdes < 0) {
    printf("Error opening file\n");
    exit(0);
  }
  err_value = pfs_write(fdes, (void *)buf, 4 * 1024, 4 * 1024, &cache_hit);
  printf("Wrote %d bytes to the file\n", err_value);
  pfs_close(fdes);
  free(buf);
  close(ifdes);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe33(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <initialize@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1400,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x1000,%ecx
mov    %ecx,%edx
call   10c0 <pread@plt>
mov    %eax,-0x34(%rbp)
lea    0xdee(%rip),%rdi
mov    $0x77,%esi
call   1050 <pfs_open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1249 <main+0x99>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x1000,%ecx
lea    -0x38(%rbp),%r8
mov    %ecx,%edx
call   1040 <pfs_write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   10a0 <pfs_close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
call   1090 <initialize@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%r14d
mov    $0x1400,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
mov    $0x1000,%ecx
call   10d0 <pread@plt>
lea    0xdfd(%rip),%rdi
mov    $0x77,%esi
call   1060 <pfs_open@plt>
test   %eax,%eax
js     1269 <main+0xa9>
mov    %eax,%ebp
lea    0xc(%rsp),%r8
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
mov    $0x1000,%ecx
call   1050 <pfs_write@plt>
lea    0xdd7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   10b0 <pfs_close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"tocopy.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"pfs_file1\00"
@.str.2 = [20 x i8] c"Error opening file\0A\00"
@.str.3 = [28 x i8] c"Wrote %d bytes to the file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %9
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @initialize(i32 %13, i8** %14)
%16 = load i8*, i8** %9
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = call i64 @malloc(i32 5120)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %10
%21 = load i32, i32* %6
%22 = load i8*, i8** %10
%23 = call i32 @pread(i32 %21, i8* %22, i32 4096, i32 4096)
store i32 %23, i32* %11
%24 = call i32 @pfs_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %2
%31 = load i32, i32* %7
%32 = load i8*, i8** %10
%33 = call i32 @pfs_write(i32 %31, i8* %32, i32 4096, i32 4096, i32* %12)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %7
%37 = call i32 @pfs_close(i32 %36)
%38 = load i8*, i8** %10
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"tocopy.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"pfs_file1\00"
@.str.3 = [28 x i8] c"Wrote %d bytes to the file\0A\00"
@str = [19 x i8] c"Error opening file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @initialize(i32 %0, i8** %1)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i64 @malloc(i32 5120)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @pread(i32 %7, i8* %9, i32 4096, i32 4096)
%11 = tail call i32 @pfs_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = call i32 @pfs_write(i32 %11, i8* %9, i32 4096, i32 4096, i32* %3)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = call i32 @pfs_close(i32 %11)
%20 = call i32 @free(i8* %9)
%21 = call i32 @close(i32 %7)
ret i32 0
}