#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 O_NONBLOCK;
int O_RDONLY;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int main() {
  int fd;
  fd = open("/Users/czz/c/fork.c", O_RDONLY | O_NONBLOCK);
  printf("%d\n", fd);
  if (fd < 0) {
    perror("open fail\n");
    return -1;
  }
  char buf[100];
  while (read(fd, buf, 99) > 0) {
    printf("%s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11a3 <main+0x63>
lea    0xe86(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <main+0xa1>
jmp    11a8 <main+0x68>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x63,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    11da <main+0x9a>
lea    -0x70(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a8 <main+0x68>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e83(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xe97(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
js     11d5 <main+0x85>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x63,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11cc <main+0x7c>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11b0 <main+0x60>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ce <main+0x7e>

LLVM IR (-O0):
@.str = [20 x i8] c"/Users/czz/c/fork.c\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [11 x i8] c"open fail\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %24
14: ; preds = %0
br label %15
15: ; preds = %20, %14
%16 = load i32, i32* %2
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = call i64 @read(i32 %16, i8* %17, i32 99)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %15
23: ; preds = %15
store i32 0, i32* %1
br label %24
24: ; preds = %23, %12
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [20 x i8] c"/Users/czz/c/fork.c\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [11 x i8] c"open fail\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %19
10: ; preds = %0
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%12 = call i64 @read(i32 %5, i8* %11, i32 99)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10, %14
%15 = call i32 @puts(i8* %11)
%16 = call i64 @read(i32 %5, i8* %11, i32 99)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %14, label %18
18: ; preds = %14, %10
br label %19
19: ; preds = %18, %8
%20 = phi i32 [ -1, %8 ], [ 0, %18 ]
ret i32 %20
}