#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int copy_data(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_file(const char *filename) {
  int fd = open(filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr,
            "Error: "
            "failed to open input file %s (%s)\n",
            filename, strerror(errno));
    return -1;
  }
  int rc = copy_data(fd, STDOUT_FILENO);
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bf <read_file+0x6f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe52(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <read_file+0x91>
mov    -0x14(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1050 <copy_data@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1188 <read_file+0x38>
mov    %eax,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <copy_data@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    11b8 <read_file+0x68>
mov    0x2e41(%rip),%rax
mov    (%rax),%ebp
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5a(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"Error: failed to open input file %s (%s)\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @read_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
store i32 -1, i32* %2
br label %24
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @copy_data(i32 %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %5
store i32 %23, i32* %2
br label %24
24: ; preds = %17, %11
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"Error: failed to open input file %s (%s)\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @read_file(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %8)
br label %14
10: ; preds = %1
%11 = load i32, i32* @STDOUT_FILENO
%12 = tail call i32 @copy_data(i32 %3, i32 %11)
%13 = tail call i32 @close(i32 %3)
br label %14
14: ; preds = %10, %5
%15 = phi i32 [ -1, %5 ], [ %12, %10 ]
ret i32 %15
}