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

/* Variables and functions */
int mfs_file_close(int);
int mfs_file_open(char *, int);
int mfs_file_read(int, char *, int);
int /*<<< orphan*/ mfs_file_write(int, char *, int);
int /*<<< orphan*/ print(char *);
int mfs_file_copy(char *from_file, char *to_file) {
  char buf2[512];
  int tmp;
  int fdr = mfs_file_open(from_file, 0);
  int fdw = mfs_file_open(to_file, 3);
  if (fdr < 0 || fdw < 0) {
    print("Cannot open file to read/write\n");
    mfs_file_close(fdr);
    mfs_file_close(fdw);
    return 0;
  }
  while ((tmp = mfs_file_read(fdr, buf2, 512)) == 512) {
    mfs_file_write(fdw, buf2, 512);
  }
  if (tmp > 0) {
    mfs_file_write(fdw, buf2, tmp);
  }
  tmp = mfs_file_close(fdr);
  tmp = mfs_file_close(fdw);
  return 1;
}


x86 assembly (clang, -O0):
<mfs_file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1070 <mfs_file_open@plt>
mov    %eax,-0x228(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3,%esi
call   1070 <mfs_file_open@plt>
mov    %eax,-0x22c(%rbp)
cmpl   $0x0,-0x228(%rbp)
jl     11a2 <mfs_file_copy+0x52>
cmpl   $0x0,-0x22c(%rbp)
jge    11d0 <mfs_file_copy+0x80>
lea    0xe57(%rip),%rdi
call   1040 <print@plt>
mov    -0x228(%rbp),%edi
call   1030 <mfs_file_close@plt>
mov    -0x22c(%rbp),%edi
call   1030 <mfs_file_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1267 <mfs_file_copy+0x117>
jmp    11d5 <mfs_file_copy+0x85>
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <mfs_file_read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x200,%eax
jne    1219 <mfs_file_copy+0xc9>
mov    -0x22c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <mfs_file_write@plt>
jmp    11d5 <mfs_file_copy+0x85>
cmpl   $0x0,-0x224(%rbp)
jle    123e <mfs_file_copy+0xee>
mov    -0x22c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1050 <mfs_file_write@plt>
mov    -0x228(%rbp),%edi
call   1030 <mfs_file_close@plt>
mov    %eax,-0x224(%rbp)
mov    -0x22c(%rbp),%edi
call   1030 <mfs_file_close@plt>
mov    %eax,-0x224(%rbp)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfs_file_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbp
xor    %r14d,%r14d
xor    %esi,%esi
call   1070 <mfs_file_open@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    $0x3,%esi
call   1070 <mfs_file_open@plt>
mov    %eax,%r15d
test   %ebx,%ebx
js     11ee <mfs_file_copy+0x9e>
test   %r15d,%r15d
js     11ee <mfs_file_copy+0x9e>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1060 <mfs_file_read@plt>
cmp    $0x200,%eax
jne    11c6 <mfs_file_copy+0x76>
mov    %rsp,%rbp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1050 <mfs_file_write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1060 <mfs_file_read@plt>
cmp    $0x200,%eax
je     11a0 <mfs_file_copy+0x50>
test   %eax,%eax
jle    11d7 <mfs_file_copy+0x87>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %eax,%edx
call   1050 <mfs_file_write@plt>
mov    %ebx,%edi
call   1030 <mfs_file_close@plt>
mov    %r15d,%edi
call   1030 <mfs_file_close@plt>
mov    $0x1,%r14d
jmp    1209 <mfs_file_copy+0xb9>
lea    0xe0b(%rip),%rdi
call   1040 <print@plt>
mov    %ebx,%edi
call   1030 <mfs_file_close@plt>
mov    %r15d,%edi
call   1030 <mfs_file_close@plt>
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Cannot open file to read/write\0A\00"
define i32 @mfs_file_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32 @mfs_file_open(i8* %10, i32 0)
store i32 %11, i32* %8
%12 = load i8*, i8** %5
%13 = call i32 @mfs_file_open(i8* %12, i32 3)
store i32 %13, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16, %2
%20 = call i32 @print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %8
%22 = call i32 @mfs_file_close(i32 %21)
%23 = load i32, i32* %9
%24 = call i32 @mfs_file_close(i32 %23)
store i32 0, i32* %3
br label %48
25: ; preds = %16
br label %26
26: ; preds = %31, %25
%27 = load i32, i32* %8
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%29 = call i32 @mfs_file_read(i32 %27, i8* %28, i32 512)
store i32 %29, i32* %7
%30 = icmp eq i32 %29, 512
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* %9
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%34 = call i32 @mfs_file_write(i32 %32, i8* %33, i32 512)
br label %26
35: ; preds = %26
%36 = load i32, i32* %7
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i32, i32* %9
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%41 = load i32, i32* %7
%42 = call i32 @mfs_file_write(i32 %39, i8* %40, i32 %41)
br label %43
43: ; preds = %38, %35
%44 = load i32, i32* %8
%45 = call i32 @mfs_file_close(i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %9
%47 = call i32 @mfs_file_close(i32 %46)
store i32 %47, i32* %7
store i32 1, i32* %3
br label %48
48: ; preds = %43, %19
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [32 x i8] c"Cannot open file to read/write\0A\00"
define i32 @mfs_file_copy(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @mfs_file_open(i8* %0, i32 0)
%6 = tail call i32 @mfs_file_open(i8* %1, i32 3)
%7 = icmp slt i32 %5, 0
%8 = icmp slt i32 %6, 0
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = call i32 @mfs_file_read(i32 %5, i8* %4, i32 512)
%12 = icmp eq i32 %11, 512
br i1 %12, label %17, label %21
13: ; preds = %2
%14 = tail call i32 @print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @mfs_file_close(i32 %5)
%16 = tail call i32 @mfs_file_close(i32 %6)
br label %29
17: ; preds = %10, %17
%18 = call i32 @mfs_file_write(i32 %6, i8* %4, i32 512)
%19 = call i32 @mfs_file_read(i32 %5, i8* %4, i32 512)
%20 = icmp eq i32 %19, 512
br i1 %20, label %17, label %21
21: ; preds = %17, %10
%22 = phi i32 [ %11, %10 ], [ %19, %17 ]
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @mfs_file_write(i32 %6, i8* %4, i32 %22)
br label %26
26: ; preds = %24, %21
%27 = call i32 @mfs_file_close(i32 %5)
%28 = call i32 @mfs_file_close(i32 %6)
br label %29
29: ; preds = %26, %13
%30 = phi i32 [ 0, %13 ], [ 1, %26 ]
ret i32 %30
}