#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sys_read(int, char *, int);
int main() {
  int fd = open("read.c", O_RDONLY);
  if (fd == -1) {
    printf("open file failed\n");
    return 0;
  }
  char buf[1024];
  sys_read(fd, buf, 1000);
  printf("%s\n", buf);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118e <main+0x4e>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bf <main+0x7f>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3e8,%edx
call   1060 <sys_read@plt>
lea    -0x410(%rbp),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
lea    0xea6(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1189 <main+0x49>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1060 <sys_read@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1195 <main+0x55>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"read.c\00"
@O_RDONLY = global i32 0
@str = [17 x i8] c"open file failed\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %12
7: ; preds = %0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%9 = call i32 @sys_read(i32 %3, i8* %8, i32 1000)
%10 = call i32 @puts(i8* %8)
%11 = call i32 @close(i32 %3)
br label %12
12: ; preds = %7, %5
ret i32 0
}