#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
scalar_t__ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int /*<<< orphan*/);
int dmbSafeOpen(const char *path, int flags, mode_t mode) {
  register int fd;
  do {
    fd = open(path, flags, mode);
  } while (fd == -1 && errno == EINTR);
  if (fd != -1)
    fcntl(fd, F_SETFD, FD_CLOEXEC);
  return fd;
}


x86 assembly (clang, -O0):
<dmbSafeOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jne    116d <dmbSafeOpen+0x4d>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1132 <dmbSafeOpen+0x12>
cmpl   $0xffffffff,-0x14(%rbp)
je     119c <dmbSafeOpen+0x7c>
mov    -0x14(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dmbSafeOpen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e95(%rip),%r12
mov    0x2e86(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    116f <dmbSafeOpen+0x4f>
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
je     1150 <dmbSafeOpen+0x30>
cmp    $0xffffffff,%ebx
je     118d <dmbSafeOpen+0x6d>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @dmbSafeOpen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
br label %8
8: ; preds = %20, %3
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = call i32 @open(i8* %9, i32 %10, i32 %11)
store i32 %12, i32* %7
br label %13
13: ; preds = %8
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %8, label %22
22: ; preds = %20
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* %7
%27 = load i32, i32* @F_SETFD
%28 = load i32, i32* @FD_CLOEXEC
%29 = call i32 @fcntl(i32 %26, i32 %27, i32 %28)
br label %30
30: ; preds = %25, %22
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @dmbSafeOpen(i8* nocapture %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %4, %3
%5 = tail call i32 @open(i8* %0, i32 %1, i32 %2)
%6 = icmp eq i32 %5, -1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %4, label %11
11: ; preds = %4
br i1 %6, label %16, label %12
12: ; preds = %11
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = tail call i32 @fcntl(i32 %5, i32 %13, i32 %14)
br label %16
16: ; preds = %12, %11
ret i32 %5
}