#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 EOF;
int inbyte();
int /*<<< orphan*/ mfs_file_close(int);
int mfs_file_open(char *, int);
int /*<<< orphan*/ mfs_file_write(int, char *, int);
int /*<<< orphan*/ print(char *);
int mfs_copy_stdin_to_file(char *filename) {
  char buf2[512];
  int offset = 0;
  int c;
  int fdw = mfs_file_open(filename, 3);
  if (fdw < 0) {
    print("Cannot open file\n");
    return 0;
  }
  while ((c = inbyte()) != EOF) {
    buf2[offset++] = c;
    if (offset == 512) {
      mfs_file_write(fdw, buf2, 512);
      offset = 0;
    }
  }
  if (offset != 512) {
    mfs_file_write(fdw, buf2, offset);
  }
  mfs_file_close(fdw);
  return 1;
}


x86 assembly (clang, -O0):
<mfs_copy_stdin_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x214(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3,%esi
call   1070 <mfs_file_open@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jge    11a2 <mfs_copy_stdin_to_file+0x52>
lea    0xe6f(%rip),%rdi
call   1040 <print@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <mfs_copy_stdin_to_file+0x105>
jmp    11a7 <mfs_copy_stdin_to_file+0x57>
mov    $0x0,%al
call   1060 <inbyte@plt>
mov    %eax,-0x218(%rbp)
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%eax
je     121b <mfs_copy_stdin_to_file+0xcb>
mov    -0x218(%rbp),%eax
mov    %al,%cl
mov    -0x214(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x214(%rbp)
cltq
mov    %cl,-0x210(%rbp,%rax,1)
cmpl   $0x200,-0x214(%rbp)
jne    1216 <mfs_copy_stdin_to_file+0xc6>
mov    -0x21c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <mfs_file_write@plt>
movl   $0x0,-0x214(%rbp)
jmp    11a7 <mfs_copy_stdin_to_file+0x57>
cmpl   $0x200,-0x214(%rbp)
je     1243 <mfs_copy_stdin_to_file+0xf3>
mov    -0x21c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
call   1050 <mfs_file_write@plt>
mov    -0x21c(%rbp),%edi
call   1030 <mfs_file_close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfs_copy_stdin_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    $0x3,%esi
call   1070 <mfs_file_open@plt>
test   %eax,%eax
js     11c0 <mfs_copy_stdin_to_file+0x70>
mov    %eax,%r15d
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <inbyte@plt>
mov    0x2e6a(%rip),%rbx
cmp    (%rbx),%eax
je     11d8 <mfs_copy_stdin_to_file+0x88>
mov    %rsp,%r14
xor    %ebp,%ebp
jmp    119b <mfs_copy_stdin_to_file+0x4b>
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <inbyte@plt>
cmp    (%rbx),%eax
je     11d0 <mfs_copy_stdin_to_file+0x80>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%rsp,%rcx,1)
cmp    $0x200,%ebp
jne    1190 <mfs_copy_stdin_to_file+0x40>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1050 <mfs_file_write@plt>
xor    %ebp,%ebp
jmp    1190 <mfs_copy_stdin_to_file+0x40>
lea    0xe39(%rip),%rdi
call   1040 <print@plt>
xor    %eax,%eax
jmp    11f2 <mfs_copy_stdin_to_file+0xa2>
cmp    $0x200,%ebp
je     11e5 <mfs_copy_stdin_to_file+0x95>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1050 <mfs_file_write@plt>
mov    %r15d,%edi
call   1030 <mfs_file_close@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [18 x i8] c"Cannot open file\0A\00"
@EOF = global i32 0
define i32 @mfs_copy_stdin_to_file(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @mfs_file_open(i8* %0, i32 3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = call i32 (...) @inbyte()
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %29, label %12
10: ; preds = %1
%11 = tail call i32 @print(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %34
12: ; preds = %6, %22
%13 = phi i32 [ %24, %22 ], [ %7, %6 ]
%14 = phi i32 [ %23, %22 ], [ 0, %6 ]
%15 = trunc i32 %13 to i8
%16 = add nsw i32 %14, 1
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %17
store i8 %15, i8* %18
%19 = icmp eq i32 %16, 512
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @mfs_file_write(i32 %4, i8* %3, i32 512)
br label %22
22: ; preds = %20, %12
%23 = phi i32 [ 0, %20 ], [ %16, %12 ]
%24 = call i32 (...) @inbyte()
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %12
27: ; preds = %22
%28 = icmp eq i32 %23, 512
br i1 %28, label %32, label %29
29: ; preds = %6, %27
%30 = phi i32 [ %23, %27 ], [ 0, %6 ]
%31 = call i32 @mfs_file_write(i32 %4, i8* %3, i32 %30)
br label %32
32: ; preds = %29, %27
%33 = call i32 @mfs_file_close(i32 %4)
br label %34
34: ; preds = %32, %10
%35 = phi i32 [ 0, %10 ], [ 1, %32 ]
ret i32 %35
}