#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
scalar_t__ errno;
int /*<<< orphan*/ makechardev();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ say(int /*<<< orphan*/, char *, char *, int);
int strerror(scalar_t__);
int mbox_open() {
  int file_desc;
  file_desc = open("/dev/gpu_mb", 0);
  if (file_desc < 0) {
    if (errno == ENOENT)
      makechardev();
    file_desc = open("/dev/gpu_mb", 0);
    if (file_desc < 0) {
      say(LOG_ERR, "Can't open device file %s - '%s'\n", "/dev/gpu_mb",
          strerror(errno));
      say(LOG_INFO, "Try creating a device file with: sudo mknod %s c %d 0\n",
          "/dev/gpu_mb", 100);
      return -1;
    }
  }
  return file_desc;
}


x86 assembly (clang, -O0):
<mbox_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
xor    %esi,%esi
call   1030 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1204 <mbox_open+0xc4>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
jne    1184 <mbox_open+0x44>
mov    $0x0,%al
call   1040 <makechardev@plt>
lea    0xe75(%rip),%rdi
xor    %esi,%esi
call   1030 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ff <mbox_open+0xbf>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%ecx
lea    0xe46(%rip),%rsi
lea    0xe33(%rip),%rdx
call   1060 <say@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
lea    0xe17(%rip),%rdx
mov    $0x64,%ecx
call   1060 <say@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <mbox_open+0xca>
jmp    1204 <mbox_open+0xc4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbox_open>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
xor    %esi,%esi
call   1030 <open@plt>
test   %eax,%eax
jns    11d2 <mbox_open+0x92>
mov    0x2e83(%rip),%r14
mov    (%r14),%rax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%rax
jne    1173 <mbox_open+0x33>
xor    %eax,%eax
call   1040 <makechardev@plt>
lea    0xe86(%rip),%rdi
xor    %esi,%esi
call   1030 <open@plt>
test   %eax,%eax
jns    11d2 <mbox_open+0x92>
mov    0x2e34(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rdi
call   1050 <strerror@plt>
lea    0xe6f(%rip),%rsi
lea    0xe5c(%rip),%r14
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1060 <say@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    %r14,%rdx
mov    $0x64,%ecx
call   1060 <say@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/gpu_mb\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [34 x i8] c"Can't open device file %s - '%s'\0A\00"
@LOG_INFO = global i32 0
@.str.2 = [55 x i8] c"Try creating a device file with: sudo mknod %s c %d 0\0A\00"
define i32 @mbox_open() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %24
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 (...) @makechardev()
br label %12
12: ; preds = %10, %6
%13 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* @LOG_ERR
%18 = load i64, i64* @errno
%19 = call i32 @strerror(i64 %18)
%20 = call i32 @say(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @LOG_INFO
%22 = call i32 @say(i32 %21, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 100)
store i32 -1, i32* %1
br label %26
23: ; preds = %12
br label %24
24: ; preds = %23, %0
%25 = load i32, i32* %2
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %16
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/gpu_mb\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [34 x i8] c"Can't open device file %s - '%s'\0A\00"
@LOG_INFO = global i32 0
@.str.2 = [55 x i8] c"Try creating a device file with: sudo mknod %s c %d 0\0A\00"
define i32 @mbox_open() {
%1 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %19
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENOENT
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (...) @makechardev()
br label %9
9: ; preds = %7, %3
%10 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32, i32* @LOG_ERR
%14 = load i64, i64* @errno
%15 = tail call i32 @strerror(i64 %14)
%16 = tail call i32 @say(i32 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @LOG_INFO
%18 = tail call i32 @say(i32 %17, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 100)
br label %19
19: ; preds = %0, %9, %12
%20 = phi i32 [ -1, %12 ], [ %10, %9 ], [ %1, %0 ]
ret i32 %20
}