#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_CREAT;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ rio_read(int, char *, int);
int rio_write(int /*<<< orphan*/, char *, int);
int strlen(char *);
int main(void) {
  int fd;
  fd = open("example.c", O_RDONLY | O_CREAT, 0644);
  if (fd == -1)
    perror("open error");
  char buf[10];
  int n;
  while (1) {
    bzero(buf, 10);
    if (rio_read(fd, buf, 10) == 0)
      break;
    n = strlen(buf);
    if (rio_write(STDOUT_FILENO, buf, n) != n) {
      perror("write failed");
      exit(2);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11bb <main+0x4b>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
jmp    11c0 <main+0x50>
lea    -0x12(%rbp),%rdi
mov    $0xa,%esi
call   1080 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0xa,%edx
call   1040 <rio_read@plt>
cmp    $0x0,%rax
jne    11ee <main+0x7e>
jmp    1233 <main+0xc3>
lea    -0x12(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    -0x12(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <rio_write@plt>
cmp    -0x18(%rbp),%eax
je     122e <main+0xbe>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
jmp    11c0 <main+0x50>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e57(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1220 <main+0xb0>
lea    0xe(%rsp),%rbx
mov    0x2e37(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1080 <bzero@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <rio_read@plt>
test   %rax,%rax
je     1213 <main+0xa3>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <rio_write@plt>
cmp    %ebp,%eax
je     11c0 <main+0x50>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
jmp    11a5 <main+0x35>

LLVM IR (-O0):
@.str = [10 x i8] c"example.c\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [13 x i8] c"write failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 420)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
br label %14
14: ; preds = %13, %34
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%16 = call i32 @bzero(i8* %15, i32 10)
%17 = load i32, i32* %2
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%19 = call i64 @rio_read(i32 %17, i8* %18, i32 10)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
br label %35
22: ; preds = %14
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %4
%25 = load i32, i32* @STDOUT_FILENO
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%27 = load i32, i32* %4
%28 = call i32 @rio_write(i32 %25, i8* %26, i32 %27)
%29 = load i32, i32* %4
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %22
br label %14
35: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"example.c\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [13 x i8] c"write failed\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4, i32 420)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
br label %11
11: ; preds = %15, %9
%12 = call i32 @bzero(i8* %10, i32 10)
%13 = call i64 @rio_read(i32 %5, i8* %10, i32 10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = call i32 @strlen(i8* %10)
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @rio_write(i32 %17, i8* %10, i32 %16)
%19 = icmp eq i32 %18, %16
br i1 %19, label %11, label %20
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %11
ret i32 0
}