#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*/ F_SETFD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ error(char *, char *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int open_read_aux(char *file) {
  int fd = open(file, O_RDONLY);
  if (fd < 0) {
    error("esh: cannot open \"%s\" for reading.", file);
    return -1;
  }
  fcntl(fd, F_SETFD, 1);
  return fd;
}


x86 assembly (clang, -O0):
<open_read_aux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1177 <open_read_aux+0x47>
mov    -0x10(%rbp),%rsi
lea    0xe9a(%rip),%rdi
call   1040 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <open_read_aux+0x63>
mov    -0x14(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_read_aux>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1162 <open_read_aux+0x32>
mov    %eax,%ebx
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <fcntl@plt>
jmp    1176 <open_read_aux+0x46>
lea    0xe97(%rip),%rdi
mov    %r14,%rsi
call   1040 <error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [35 x i8] c"esh: cannot open \22%s\22 for reading.\00"
@F_SETFD = global i32 0
define i32 @open_read_aux(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
br label %18
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load i32, i32* @F_SETFD
%16 = call i32 @fcntl(i32 %14, i32 %15, i32 1)
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %18
18: ; preds = %13, %10
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [35 x i8] c"esh: cannot open \22%s\22 for reading.\00"
@F_SETFD = global i32 0
define i32 @open_read_aux(i8* %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 %7
5: ; preds = %1
%6 = tail call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
7: ; preds = %1
%8 = load i32, i32* @F_SETFD
%9 = tail call i32 @fcntl(i32 %3, i32 %8, i32 1)
br label %10
10: ; preds = %7, %5
%11 = phi i32 [ -1, %5 ], [ %3, %7 ]
ret i32 %11
}