#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 check_fd(int);
scalar_t__ errno;
int open_(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int check_open(const char *path, int flags) {
  int fd;
  errno = 0;
  printf("Check success of open('%s', %x)... ", path ?: "(null)", flags);
  fd = open_(path, flags, 0);
  return check_fd(fd);
}


x86 assembly (clang, -O0):
<check_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     116c <check_open+0x3c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    117c <check_open+0x4c>
lea    0xeb1(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    117c <check_open+0x4c>
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1050 <open_@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <check_fd@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_open>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2ea1(%rip),%rax
movq   $0x0,(%rax)
test   %rdi,%rdi
lea    0xed4(%rip),%rsi
cmovne %rdi,%rsi
lea    0xea5(%rip),%rdi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <open_@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <check_fd@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [36 x i8] c"Check success of open('%s', %x)... \00"
@.str.1 = [7 x i8] c"(null)\00"
define i32 @check_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i64 0, i64* @errno
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %2
br label %10
9: ; preds = %2
br label %10
10: ; preds = %9, %8
%11 = phi i8* [ %6, %8 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %9 ]
%12 = load i32, i32* %4
%13 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = call i32 @open_(i8* %14, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = call i32 @check_fd(i32 %17)
ret i32 %18
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [36 x i8] c"Check success of open('%s', %x)... \00"
@.str.1 = [7 x i8] c"(null)\00"
define i32 @check_open(i8* %0, i32 %1) {
store i64 0, i64* @errno
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %1)
%6 = tail call i32 @open_(i8* %0, i32 %1, i32 0)
%7 = tail call i32 @check_fd(i32 %6)
ret i32 %7
}