#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*/ 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");
  }
  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    11fe <safe_open+0xde>
mov    -0x8(%rbp),%rdx
mov    $0x2,%edi
lea    0xea8(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
mov    -0xc(%rbp),%eax
mov    0x2e7f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     118c <safe_open+0x6c>
mov    $0x2,%edi
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11eb <safe_open+0xcb>
mov    -0xc(%rbp),%eax
mov    0x2e4a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b9 <safe_open+0x99>
mov    $0x2,%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11e6 <safe_open+0xc6>
mov    -0xc(%rbp),%eax
mov    0x2e05(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11e1 <safe_open+0xc1>
mov    $0x2,%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
jmp    11e6 <safe_open+0xc6>
jmp    11eb <safe_open+0xcb>
mov    $0x2,%edi
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1030 <log_printf@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_open>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11a4 <safe_open+0x84>
lea    0xec4(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <log_printf@plt>
mov    0x2e96(%rip),%rax
test   %ebp,(%rax)
je     115f <safe_open+0x3f>
lea    0xebb(%rip),%rsi
jmp    1185 <safe_open+0x65>
mov    0x2e7a(%rip),%rax
test   %ebp,(%rax)
je     1173 <safe_open+0x53>
lea    0xebe(%rip),%rsi
jmp    1185 <safe_open+0x65>
mov    0x2e4e(%rip),%rax
test   %ebp,(%rax)
je     1191 <safe_open+0x71>
lea    0xebb(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <log_printf@plt>
lea    0xeba(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <log_printf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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 %38
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))
br label %38
38: ; preds = %36, %2
%39 = load i32, i32* %5
ret i32 %39
}

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 %23
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))
br label %23
23: ; preds = %21, %2
ret i32 %3
}