#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_create(char *, int);
int pfs_open(char *, char);
int pfs_write(int, void *, int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ pread(int, void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int ifdes, fdes;
  int err_value;
  char *input_fname = "tocopy.txt";
  char *buf;
  int cache_hit;
  ssize_t nread;
  initialize(argc, argv);
  ifdes = open(input_fname, O_RDONLY);
  buf = (char *)malloc(5 * 1024);
  nread = pread(ifdes, (void *)buf, 4 * 1024, 0);
  err_value = pfs_create("pfs_file1", 3);
  if (err_value < 0) {
    printf("Unable to create a file\n");
    exit(0);
  }
  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, 0, &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    0xe23(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <initialize@plt>
mov    -0x28(%rbp),%rdi
mov    0x2df0(%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,%edx
xor    %ecx,%ecx
call   10c0 <pread@plt>
mov    %eax,-0x38(%rbp)
lea    0xdde(%rip),%rdi
mov    $0x3,%esi
call   10d0 <pfs_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1259 <main+0x99>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xdab(%rip),%rdi
mov    $0x77,%esi
call   1050 <pfs_open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128c <main+0xcc>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r8
call   1040 <pfs_write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd92(%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    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe13(%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
xor    %ecx,%ecx
call   10d0 <pread@plt>
lea    0xdf0(%rip),%rdi
mov    $0x3,%esi
call   10e0 <pfs_create@plt>
test   %eax,%eax
js     1288 <main+0xb8>
lea    0xddb(%rip),%rdi
mov    $0x77,%esi
call   1060 <pfs_open@plt>
test   %eax,%eax
js     1291 <main+0xc1>
mov    %eax,%ebp
lea    0xc(%rsp),%r8
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1050 <pfs_write@plt>
lea    0xdb8(%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    0xdb5(%rip),%rdi
jmp    1298 <main+0xc8>
lea    0xd99(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10f0 <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 = [25 x i8] c"Unable to create a file\0A\00"
@.str.3 = [20 x i8] c"Error opening file\0A\00"
@.str.4 = [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 0)
store i32 %23, i32* %12
%24 = call i32 @pfs_create(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 3)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %2
%31 = call i32 @pfs_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %30
%38 = load i32, i32* %7
%39 = load i8*, i8** %10
%40 = call i32 @pfs_write(i32 %38, i8* %39, i32 4096, i32 0, i32* %11)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %7
%44 = call i32 @pfs_close(i32 %43)
%45 = load i8*, i8** %10
%46 = call i32 @free(i8* %45)
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
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.4 = [28 x i8] c"Wrote %d bytes to the file\0A\00"
@str = [19 x i8] c"Error opening file\00"
@str.5 = [24 x i8] c"Unable to create a 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 0)
%11 = tail call i32 @pfs_create(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 3)
%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 ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = tail call i32 @pfs_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 119)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %16
%23 = call i32 @pfs_write(i32 %17, i8* %9, i32 4096, i32 0, i32* %3)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = call i32 @pfs_close(i32 %17)
%26 = call i32 @free(i8* %9)
%27 = call i32 @close(i32 %7)
ret i32 0
}