#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_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stop_and_wait();
char *strerror(int /*<<< orphan*/);
int open_read(char *filename) {
  int fd;
  do {
    fd = open(filename, O_RDONLY | 0);
    if (fd < 0) {
      fprintf(stderr, "Error opening input: %s\n", strerror(errno));
      stop_and_wait();
    }
  } while (fd < 0);
  return fd;
}


x86 assembly (clang, -O0):
<open_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a1 <open_read+0x61>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe6b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <stop_and_wait@plt>
jmp    11a6 <open_read+0x66>
cmpl   $0x0,-0xc(%rbp)
jl     114c <open_read+0xc>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e93(%rip),%r15
mov    (%r15),%esi
call   1050 <open@plt>
test   %eax,%eax
jns    11b0 <open_read+0x70>
mov    0x2e70(%rip),%r12
mov    0x2e61(%rip),%r13
lea    0xe8a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <stop_and_wait@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1180 <open_read+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Error opening input: %s\0A\00"
@errno = global i32 0
define i32 @open_read(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %1, %5
%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 ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 (...) @stop_and_wait()
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %5, label %14
14: ; preds = %5, %1
%15 = phi i32 [ %3, %1 ], [ %12, %5 ]
ret i32 %15
}