#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*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int nvrm_open_file(const char *fname) {
  int res = open(fname, O_RDWR);
  if (res < 0)
    return res;
  if (fcntl(res, F_SETFD, FD_CLOEXEC) < 0) {
    close(res);
    return -1;
  }
  return res;
}


x86 assembly (clang, -O0):
<nvrm_open_file>:
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    1166 <nvrm_open_file+0x36>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <nvrm_open_file+0x74>
mov    -0x14(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    119e <nvrm_open_file+0x6e>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <nvrm_open_file+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvrm_open_file>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     116f <nvrm_open_file+0x3f>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    116f <nvrm_open_file+0x3f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @nvrm_open_file(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_RDWR
%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 %12
10: ; preds = %1
%11 = load i32, i32* %4
store i32 %11, i32* %2
br label %23
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @F_SETFD
%15 = load i32, i32* @FD_CLOEXEC
%16 = call i64 @fcntl(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %2
br label %23
21: ; preds = %12
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %23
23: ; preds = %21, %18, %10
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @nvrm_open_file(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = load i32, i32* @F_SETFD
%7 = load i32, i32* @FD_CLOEXEC
%8 = tail call i64 @fcntl(i32 %3, i32 %6, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @close(i32 %3)
br label %12
12: ; preds = %5, %1, %10
%13 = phi i32 [ -1, %10 ], [ %3, %1 ], [ %3, %5 ]
ret i32 %13
}