#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 /*<<< orphan*/ close(int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ read(int, char *, int);
int OpenReadFile(const char *filepath, int flag) {
  int fd;
  char buf[10];
  if (0 > (fd = open(filepath, flag))) {
    perror("open");
    printf("file: %s\n", filepath);
    return (-1);
  }
  read(fd, buf, 10);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<OpenReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    11a2 <OpenReadFile+0x52>
lea    0xe7f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <OpenReadFile+0x72>
mov    -0x18(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenReadFile>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1060 <open@plt>
test   %eax,%eax
js     1185 <OpenReadFile+0x35>
mov    %eax,%ebp
lea    0xe(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
jmp    117e <OpenReadFile+0x2e>

LLVM IR (-O0):
@.str = [5 x i8] c"open\00"
@.str.1 = [10 x i8] c"file: %s\0A\00"
define i32 @OpenReadFile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp sgt i32 0, %10
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %22
16: ; preds = %2
%17 = load i32, i32* %6
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 10)
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 0, i32* %3
br label %22
22: ; preds = %16, %12
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@.str = [5 x i8] c"open\00"
@.str.1 = [10 x i8] c"file: %s\0A\00"
define i32 @OpenReadFile(i8* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @open(i8* %0, i32 %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %13
10: ; preds = %2
%11 = call i32 @read(i32 %5, i8* %4, i32 10)
%12 = tail call i32 @close(i32 %5)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ -1, %7 ], [ 0, %10 ]
ret i32 %14
}