#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int main() {
  char rbuf = 0;
  FILE *pf = fopen("/etc/passwd", "r");
  FILE *pfnull = fopen("/dev/null", "rw");
  if (pf == NULL) {
    perror("fopen");
    goto err;
  }
  while (fread(&rbuf, 1, 1, pf) != 0) {
    fwrite(&rbuf, 1, 1, pfnull);
  }
  fclose(pf);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
lea    0xe96(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ad <main+0x5d>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
jmp    11fe <main+0xae>
jmp    11b2 <main+0x62>
mov    -0x10(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x0,%rax
je     11e9 <main+0x99>
mov    -0x18(%rbp),%rcx
lea    -0x5(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
jmp    11b2 <main+0x62>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1205 <main+0xb5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
lea    0xe9b(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0xe93(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
test   %r14,%r14
je     11eb <main+0x9b>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0xf(%rsp),%r15
movsbl 0xf(%rsp),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11e1 <main+0x91>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [3 x i8] c"rw\00"
@.str.4 = [6 x i8] c"fopen\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 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %3
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %22
11: ; preds = %0
br label %12
12: ; preds = %16, %11
%13 = load i32*, i32** %3
%14 = call i64 @fread(i8* %2, i32 1, i32 1, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32*, i32** %4
%18 = call i32 @fwrite(i8* %2, i32 1, i32 1, i32* %17)
br label %12
19: ; preds = %12
%20 = load i32*, i32** %3
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %1
br label %23
22: ; preds = %9
store i32 -1, i32* %1
br label %23
23: ; preds = %22, %19
%24 = load i32, i32* %1
ret i32 %24
}

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