#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 fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ mfs_file_close(int);
int mfs_file_open(char *, int);
int /*<<< orphan*/ mfs_file_write(int, char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
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) {
    printf("%s", ("Cannot open file\n"));
    return 0;
  }
  while ((c = fgetc(stdin)) != 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    11a9 <mfs_copy_stdin_to_file+0x59>
lea    0xe6f(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1263 <mfs_copy_stdin_to_file+0x113>
jmp    11ae <mfs_copy_stdin_to_file+0x5e>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %eax,-0x218(%rbp)
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
je     1229 <mfs_copy_stdin_to_file+0xd9>
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    1224 <mfs_copy_stdin_to_file+0xd4>
mov    -0x21c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1060 <mfs_file_write@plt>
movl   $0x0,-0x214(%rbp)
jmp    11ae <mfs_copy_stdin_to_file+0x5e>
cmpl   $0x200,-0x214(%rbp)
je     1251 <mfs_copy_stdin_to_file+0x101>
mov    -0x21c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
call   1060 <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   %r12
push   %rbx
sub    $0x200,%rsp
mov    $0x3,%esi
call   1070 <mfs_file_open@plt>
test   %eax,%eax
js     11d2 <mfs_copy_stdin_to_file+0x82>
mov    %eax,%r15d
mov    0x2e71(%rip),%r12
mov    (%r12),%edi
call   1050 <fgetc@plt>
mov    0x2e59(%rip),%rbx
xor    %ebp,%ebp
cmp    (%rbx),%eax
je     11f1 <mfs_copy_stdin_to_file+0xa1>
xor    %ebp,%ebp
mov    %rsp,%r14
jmp    11ad <mfs_copy_stdin_to_file+0x5d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
call   1050 <fgetc@plt>
cmp    (%rbx),%eax
je     11e9 <mfs_copy_stdin_to_file+0x99>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,(%rsp,%rcx,1)
cmp    $0x200,%ebp
jne    11a0 <mfs_copy_stdin_to_file+0x50>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1060 <mfs_file_write@plt>
xor    %ebp,%ebp
jmp    11a0 <mfs_copy_stdin_to_file+0x50>
lea    0xe27(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    120b <mfs_copy_stdin_to_file+0xbb>
cmp    $0x200,%ebp
je     11fe <mfs_copy_stdin_to_file+0xae>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1060 <mfs_file_write@plt>
mov    %r15d,%edi
call   1030 <mfs_file_close@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [18 x i8] c"Cannot open file\0A\00"
@stdin = global i32 0
@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 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %45
14: ; preds = %1
br label %15
15: ; preds = %33, %14
%16 = load i32, i32* @stdin
%17 = call i32 @fgetc(i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %15
%21 = load i32, i32* %6
%22 = trunc i32 %21 to i8
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %25
store i8 %22, i8* %26
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 512
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = load i32, i32* %7
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = call i32 @mfs_file_write(i32 %30, i8* %31, i32 512)
store i32 0, i32* %5
br label %33
33: ; preds = %29, %20
br label %15
34: ; preds = %15
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 512
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i32, i32* %7
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%40 = load i32, i32* %5
%41 = call i32 @mfs_file_write(i32 %38, i8* %39, i32 %40)
br label %42
42: ; preds = %37, %34
%43 = load i32, i32* %7
%44 = call i32 @mfs_file_close(i32 %43)
store i32 1, i32* %2
br label %45
45: ; preds = %42, %12
%46 = load i32, i32* %2
ret i32 %46
}

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