#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char **, int);
int write(int, char **, int);
main() {
  int fd, nread, nwrite;
  char buff[100];
  fd = open("abc", O_RDONLY);
  printf("open system call return value:%d\n", fd);
  if (fd < 0) {
    perror("Open FAILS\n");
    exit(0);
  }
  while (0 != read(fd, &buff, 7)) {
    nwrite = write(1, &buff, 7);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11c6 <main+0x56>
lea    0xe6c(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11cb <main+0x5b>
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x7,%edx
call   1060 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1205 <main+0x95>
lea    -0x80(%rbp),%rsi
mov    $0x1,%edi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
jmp    11cb <main+0x5b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe70(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
js     11f6 <main+0x86>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x7,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11e6 <main+0x76>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1060 <read@plt>
test   %rax,%rax
jne    11c0 <main+0x50>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"abc\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"open system call return value:%d\0A\00"
@.str.2 = [12 x i8] c"Open FAILS\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 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 %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %0
br label %16
16: ; preds = %21, %15
%17 = load i32, i32* %2
%18 = bitcast [100 x i8]* %5 to i8**
%19 = call i64 @read(i32 %17, i8** %18, i32 7)
%20 = icmp ne i64 0, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = bitcast [100 x i8]* %5 to i8**
%23 = call i32 @write(i32 1, i8** %22, i32 7)
store i32 %23, i32* %4
br label %16
24: ; preds = %16
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [4 x i8] c"abc\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"open system call return value:%d\0A\00"
@.str.2 = [12 x i8] c"Open FAILS\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %4, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = bitcast [100 x i8]* %1 to i8**
%9 = call i64 @read(i32 %4, i8** %8, i32 7)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %7, %14
%15 = call i32 @write(i32 1, i8** %8, i32 7)
%16 = call i64 @read(i32 %4, i8** %8, i32 7)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %14
18: ; preds = %14, %7
%19 = tail call i32 @close(i32 %4)
ret i32 0
}