#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 /*<<< orphan*/);
int mfs_file_read(int, char *, int);
int /*<<< orphan*/ printf(char *, char *);
int mfs_cat(char *filename) {
  char buf2[513];
  int tmp;
  int fdr = mfs_file_open(filename, 0);
  if (fdr < 0) {
    printf("%s", ("Cannot open file\n"));
    return 0;
  }
  while ((tmp = mfs_file_read(fdr, buf2, 512)) == 512) {
    buf2[512] = '\0';
    printf("%s", (buf2));
  }
  if (tmp > 0) {
    buf2[tmp] = '\0';
    printf("%s", (buf2));
  }
  tmp = mfs_file_close(fdr);
  printf("%s", ("\n"));
  return 1;
}


x86 assembly (clang, -O0):
<mfs_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <mfs_file_open@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    118c <mfs_cat+0x4c>
lea    0xe8c(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    122f <mfs_cat+0xef>
jmp    1191 <mfs_cat+0x51>
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <mfs_file_read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x200,%eax
jne    11d5 <mfs_cat+0x95>
movb   $0x0,-0x20(%rbp)
lea    -0x220(%rbp),%rsi
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
jmp    1191 <mfs_cat+0x51>
cmpl   $0x0,-0x224(%rbp)
jle    1204 <mfs_cat+0xc4>
movslq -0x224(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
mov    -0x228(%rbp),%edi
call   1030 <mfs_file_close@plt>
mov    %eax,-0x224(%rbp)
lea    0xde4(%rip),%rdi
lea    0xdf0(%rip),%rsi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfs_cat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
xor    %ebp,%ebp
xor    %esi,%esi
call   1060 <mfs_file_open@plt>
test   %eax,%eax
js     11e3 <mfs_cat+0xa3>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1050 <mfs_file_read@plt>
cmp    $0x200,%eax
jne    11a9 <mfs_cat+0x69>
lea    0xe85(%rip),%r14
mov    %rsp,%rbp
xchg   %ax,%ax
movb   $0x0,0x200(%rsp)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1050 <mfs_file_read@plt>
cmp    $0x200,%eax
je     1180 <mfs_cat+0x40>
test   %eax,%eax
jle    11c2 <mfs_cat+0x82>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe46(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1030 <mfs_file_close@plt>
lea    0xe30(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    11f6 <mfs_cat+0xb6>
lea    0xe16(%rip),%rdi
lea    0xe12(%rip),%rsi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [18 x i8] c"Cannot open file\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @mfs_cat(i8* %0) {
%2 = alloca [513 x i8]
%3 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @mfs_file_open(i8* %0, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = call i32 @mfs_file_read(i32 %4, i8* %3, i32 512)
%8 = icmp eq i32 %7, 512
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 512
br 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 %27
13: ; preds = %9, %13
store i8 0, i8* %10
%14 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
%15 = call i32 @mfs_file_read(i32 %4, i8* %3, i32 512)
%16 = icmp eq i32 %15, 512
br i1 %16, label %13, label %17
17: ; preds = %13, %6
%18 = phi i32 [ %7, %6 ], [ %15, %13 ]
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %24
24: ; preds = %20, %17
%25 = call i32 @mfs_file_close(i32 %4)
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %24, %11
%28 = phi i32 [ 0, %11 ], [ 1, %24 ]
ret i32 %28
}