#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 */
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int main() {
  int fd;
  ssize_t len;
  char buff[1024];
  fd = open("/etc/hosts", O_RDONLY);
  if (fd == -1) {
    printf("open error\n");
    return 1;
  }
  while ((len = read(fd, buff, 1024))) {
    buff[len] = '\0';
    printf("%s", buff);
  }
  close(fd);
  fd = open("/opt/notexsit", O_RDWR, 0666);
  if (fd < 0)
    printf("open error %d", fd);
  else {
    printf("open return %d", fd);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1190 <main+0x50>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1244 <main+0x104>
jmp    1195 <main+0x55>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11dd <main+0x9d>
mov    -0x10(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1195 <main+0x55>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1224 <main+0xe4>
mov    -0x8(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123d <main+0xfd>
mov    -0x8(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1209 <main+0xb9>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11c5 <main+0x75>
lea    0xe73(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
xor    %ebx,%ebx
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     121c <main+0xcc>
lea    0xe33(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    122e <main+0xde>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    122e <main+0xde>
lea    0xdf9(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/etc/hosts\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open error\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [14 x i8] c"/opt/notexsit\00"
@O_RDWR = global i32 0
@.str.4 = [14 x i8] c"open error %d\00"
@.str.5 = [15 x i8] c"open return %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %38
11: ; preds = %0
br label %12
12: ; preds = %17, %11
%13 = load i32, i32* %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 1024)
store i64 %15, i64* %3
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i64, i64* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %18
store i8 0, i8* %19
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %12
22: ; preds = %12
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @O_RDWR
%26 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 438)
store i32 %26, i32* %2
%27 = load i32, i32* %2
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %30)
br label %37
32: ; preds = %22
%33 = load i32, i32* %2
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %32, %29
store i32 0, i32* %1
br label %38
38: ; preds = %37, %9
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [11 x i8] c"/etc/hosts\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [14 x i8] c"/opt/notexsit\00"
@O_RDWR = global i32 0
@.str.4 = [14 x i8] c"open error %d\00"
@.str.5 = [15 x i8] c"open return %d\00"
@str = [11 x i8] c"open error\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 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = call i64 @read(i32 %4, i8* %2, i32 1024)
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %27
11: ; preds = %6, %11
%12 = phi i64 [ %15, %11 ], [ %7, %6 ]
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%15 = call i64 @read(i32 %4, i8* %2, i32 1024)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %11
17: ; preds = %11, %6
%18 = call i32 @close(i32 %4)
%19 = load i32, i32* @O_RDWR
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 438)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %27
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%26 = call i32 @close(i32 %20)
br label %27
27: ; preds = %22, %24, %9
%28 = phi i32 [ 1, %9 ], [ 0, %24 ], [ 0, %22 ]
ret i32 %28
}