#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_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int open(char const *, int, int);
int utils_device_open(const char *device, int mode) {
  int m, f, fd;
  m = mode | O_NONBLOCK;
  for (f = 0; f < 5; f++) {
    fd = open(device, m, 0600);
    if (fd >= 0)
      break;
  }
  if (fd < 0)
    return fd;
  if (m != mode)
    fcntl(fd, F_SETFL, mode);
  return fd;
}


x86 assembly (clang, -O0):
<utils_device_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2eaf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    1185 <utils_device_open+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1172 <utils_device_open+0x52>
jmp    1185 <utils_device_open+0x65>
jmp    1177 <utils_device_open+0x57>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1145 <utils_device_open+0x25>
cmpl   $0x0,-0x20(%rbp)
jge    119a <utils_device_open+0x7a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c0 <utils_device_open+0xa0>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11ba <utils_device_open+0x9a>
mov    -0x20(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_device_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2eb4(%rip),%rax
mov    (%rax),%ebp
or     %esi,%ebp
mov    %ebp,%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <utils_device_open+0x7e>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <utils_device_open+0x7e>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <utils_device_open+0x7e>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <utils_device_open+0x7e>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x180,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <utils_device_open+0x96>
cmp    %r15d,%ebp
je     11b6 <utils_device_open+0x96>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15d,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @utils_device_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %9, %10
store i32 %11, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %23, %2
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 5
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = call i32 @open(i8* %16, i32 %17, i32 384)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %26
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
br label %12
26: ; preds = %21, %12
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32, i32* %8
store i32 %30, i32* %3
br label %42
31: ; preds = %26
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* %8
%37 = load i32, i32* @F_SETFL
%38 = load i32, i32* %5
%39 = call i32 @fcntl(i32 %36, i32 %37, i32 %38)
br label %40
40: ; preds = %35, %31
%41 = load i32, i32* %8
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %29
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @utils_device_open(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %1
%5 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %2
%8 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %19, label %10
10: ; preds = %7
%11 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @open(i8* %0, i32 %4, i32 384)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %16, %13, %10, %7, %2
%20 = phi i32 [ %5, %2 ], [ %8, %7 ], [ %11, %10 ], [ %14, %13 ], [ %17, %16 ]
%21 = icmp eq i32 %4, %1
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @F_SETFL
%24 = tail call i32 @fcntl(i32 %20, i32 %23, i32 %1)
br label %25
25: ; preds = %16, %22, %19
%26 = phi i32 [ %20, %22 ], [ %20, %19 ], [ %17, %16 ]
ret i32 %26
}