#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NOCTTY;
int O_NONBLOCK;
int O_WRONLY;
int fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int);
int open_nb(char *buf) {
  int fd, n;
  if ((fd = open(buf, O_WRONLY | O_NONBLOCK | O_NOCTTY)) < 0)
    return -1;
  n = fcntl(fd, F_GETFL);
  n &= ~(O_NONBLOCK);
  fcntl(fd, F_SETFL, n);
  return fd;
}


x86 assembly (clang, -O0):
<open_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1168 <open_nb+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <open_nb+0x8c>
mov    -0x14(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_nb>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    0x2eb6(%rip),%r14
mov    (%r14),%esi
or     (%rax),%esi
mov    0x2e9a(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     1178 <open_nb+0x58>
mov    %eax,%ebx
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edx
not    %edx
and    %eax,%edx
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
jmp    117d <open_nb+0x5d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define i32 @open_nb(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %28
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @F_GETFL
%18 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* @O_NONBLOCK
%20 = xor i32 %19, -1
%21 = load i32, i32* %5
%22 = and i32 %21, %20
store i32 %22, i32* %5
%23 = load i32, i32* %4
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* %5
%26 = call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %15, %14
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define i32 @open_nb(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %2
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %10)
%12 = load i32, i32* @O_NONBLOCK
%13 = xor i32 %12, -1
%14 = and i32 %11, %13
%15 = load i32, i32* @F_SETFL
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %15, i32 %14)
br label %17
17: ; preds = %1, %9
%18 = phi i32 [ %7, %9 ], [ -1, %1 ]
ret i32 %18
}