#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*/ puts(char *);
int /*<<< orphan*/ read(int, char *, int);
int main() {
  char buff[1024];
  int fd;
  if (0 < (fd = open("./test.txt", O_RDONLY))) {
    read(fd, buff, 1024);
    puts(buff);
    close(fd);
  } else {
    printf("file open failed.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    11bc <main+0x6c>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
lea    -0x410(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x414(%rbp),%edi
call   1050 <close@plt>
jmp    11c8 <main+0x78>
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x420,%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   1060 <open@plt>
test   %eax,%eax
jle    1188 <main+0x48>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1194 <main+0x54>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

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

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