#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 O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ log_printf(int, char *, ...);
int open(char const *, int);
int safe_open(const char *pathname, int flags) {
  int fd;
  fd = open(pathname, flags);
  if (fd < 0) {
    log_printf(2, "While trying to open %s", pathname);
    if (flags & O_RDWR)
      log_printf(2, " for read/write access");
    else if (flags & O_RDONLY)
      log_printf(2, " for read access");
    else if (flags & O_WRONLY)
      log_printf(2, " for write access");
    log_printf(2, ": %m\n");
    exit(1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<safe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1218 <safe_open+0xe8>
mov    -0x8(%rbp),%rdx
mov    $0x2,%edi
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
mov    -0xc(%rbp),%eax
mov    0x2e6f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     119c <safe_open+0x6c>
mov    $0x2,%edi
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11fb <safe_open+0xcb>
mov    -0xc(%rbp),%eax
mov    0x2e3a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c9 <safe_open+0x99>
mov    $0x2,%edi
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11f6 <safe_open+0xc6>
mov    -0xc(%rbp),%eax
mov    0x2df5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f1 <safe_open+0xc1>
mov    $0x2,%edi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11f6 <safe_open+0xc6>
jmp    11fb <safe_open+0xcb>
mov    $0x2,%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_open>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <open@plt>
test   %eax,%eax
js     1148 <safe_open+0x18>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xeb1(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <log_printf@plt>
mov    0x2e83(%rip),%rax
test   %ebp,(%rax)
jne    1188 <safe_open+0x58>
mov    0x2e70(%rip),%rax
test   %ebp,(%rax)
jne    1191 <safe_open+0x61>
mov    0x2e4d(%rip),%rax
test   %ebp,(%rax)
je     11a4 <safe_open+0x74>
lea    0xeba(%rip),%rsi
jmp    1198 <safe_open+0x68>
lea    0xe89(%rip),%rsi
jmp    1198 <safe_open+0x68>
lea    0xe97(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <log_printf@plt>
lea    0xea7(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <log_printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"While trying to open %s\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c" for read/write access\00"
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c" for read access\00"
@O_WRONLY = global i32 0
@.str.3 = [18 x i8] c" for write access\00"
@.str.4 = [6 x i8] c": %m\0A\00"
define i32 @safe_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* %4
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %39
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* %4
%15 = load i32, i32* @O_RDWR
%16 = and i32 %14, %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %36
20: ; preds = %11
%21 = load i32, i32* %4
%22 = load i32, i32* @O_RDONLY
%23 = and i32 %21, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %35
27: ; preds = %20
%28 = load i32, i32* %4
%29 = load i32, i32* @O_WRONLY
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
br label %35
35: ; preds = %34, %25
br label %36
36: ; preds = %35, %18
%37 = call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %2
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@.str = [24 x i8] c"While trying to open %s\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c" for read/write access\00"
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c" for read access\00"
@O_WRONLY = global i32 0
@.str.3 = [18 x i8] c" for write access\00"
@.str.4 = [6 x i8] c": %m\0A\00"
define i32 @safe_open(i8* %0, i32 %1) {
%3 = tail call i32 @open(i8* %0, i32 %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %2
%6 = tail call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @O_RDWR
%8 = and i32 %7, %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %5
%11 = load i32, i32* @O_RDONLY
%12 = and i32 %11, %1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @O_WRONLY
%16 = and i32 %15, %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14, %10, %5
%19 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %10 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %14 ]
%20 = tail call i32 (i32, i8*, ...) @log_printf(i32 2, i8* %19)
br label %21
21: ; preds = %18, %14
%22 = tail call i32 (i32, i8*, ...) @log_printf(i32 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %2
ret i32 %3
}