#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char rbuf = 0;
  int fd = open("/etc/passwd", O_RDONLY);
  int fd_null = open("/dev/null", O_RDWR);
  if (fd == -1) {
    perror("open");
    goto err;
  }
  while (read(fd, &rbuf, 1) != 0) {
    write(fd_null, &rbuf, 1);
  }
  close(fd);
  close(fd_null);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ae <main+0x5e>
lea    0xe72(%rip),%rdi
call   1070 <perror@plt>
jmp    1200 <main+0xb0>
jmp    11b3 <main+0x63>
mov    -0xc(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     11e4 <main+0x94>
mov    -0x10(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11b3 <main+0x63>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1207 <main+0xb7>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%ebx
je     11ee <main+0x9e>
mov    %eax,%r14d
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11d4 <main+0x84>
lea    0xf(%rsp),%rbp
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e5 <main+0x95>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i8 0, i8* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
13: ; preds = %0
br label %14
14: ; preds = %18, %13
%15 = load i32, i32* %3
%16 = call i64 @read(i32 %15, i8* %2, i32 1)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %4
%20 = call i32 @write(i32 %19, i8* %2, i32 1)
br label %14
21: ; preds = %14
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i32 0, i32* %1
br label %27
26: ; preds = %11
store i32 -1, i32* %1
br label %27
27: ; preds = %26, %21
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
define i32 @main() {
%1 = alloca i8
store i8 0, i8* %1
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %3, -1
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i64 @read(i32 %3, i8* %1, i32 1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %16, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %19
12: ; preds = %7, %12
%13 = call i32 @write(i32 %5, i8* %1, i32 1)
%14 = call i64 @read(i32 %3, i8* %1, i32 1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %12
16: ; preds = %12, %7
%17 = tail call i32 @close(i32 %3)
%18 = tail call i32 @close(i32 %5)
br label %19
19: ; preds = %10, %16
%20 = phi i32 [ -1, %10 ], [ 0, %16 ]
ret i32 %20
}