#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 S_IFCHR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ makedev(int, int /*<<< orphan*/);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ unlink(char *);
int mbox_open() {
  int file_desc;
  file_desc = open("/dev/vcio", 0);
  if (file_desc < 0) {
    unlink("/dev/pi-blaster-mbox");
    if (mknod("/dev/pi-blaster-mbox", S_IFCHR | 0600, makedev(100, 0)) < 0)
      fatal("Failed to create mailbox device\n");
    file_desc = open("/dev/pi-blaster-mbox", 0);
    if (file_desc < 0) {
      printf("Can't open device file: %s\n", "/dev/pi-blaster-mbox");
      perror(NULL);
      exit(-1);
    }
  }
  return file_desc;
}


x86 assembly (clang, -O0):
<mbox_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1239 <mbox_open+0xb9>
lea    0xe60(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
or     $0x180,%eax
mov    %eax,-0x8(%rbp)
mov    $0x64,%edi
xor    %esi,%esi
call   1080 <makedev@plt>
mov    -0x8(%rbp),%esi
mov    %eax,%edx
lea    0xe32(%rip),%rdi
call   1050 <mknod@plt>
cmp    $0x0,%rax
jge    11f3 <mbox_open+0x73>
lea    0xe31(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe10(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1234 <mbox_open+0xb4>
lea    0xe2b(%rip),%rdi
lea    0xdee(%rip),%rsi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    1239 <mbox_open+0xb9>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbox_open>:
push   %rbp
push   %rbx
push   %rax
lea    0xe76(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
jns    11ed <mbox_open+0x6d>
lea    0xe6e(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e35(%rip),%rax
mov    $0x180,%ebp
or     (%rax),%ebp
mov    $0x64,%edi
xor    %esi,%esi
call   1080 <makedev@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <mknod@plt>
test   %rax,%rax
jns    11db <mbox_open+0x5b>
lea    0xe49(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe28(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     11f4 <mbox_open+0x74>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe45(%rip),%rdi
lea    0xe08(%rip),%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/vcio\00"
@.str.1 = [21 x i8] c"/dev/pi-blaster-mbox\00"
@S_IFCHR = global i32 0
@.str.2 = [33 x i8] c"Failed to create mailbox device\0A\00"
@.str.3 = [28 x i8] c"Can't open device file: %s\0A\00"
define i32 @mbox_open() {
%1 = alloca i32
%2 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %23
5: ; preds = %0
%6 = call i32 @unlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @S_IFCHR
%8 = or i32 %7, 384
%9 = call i32 @makedev(i32 100, i32 0)
%10 = call i64 @mknod(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = call i32 @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %5
%15 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @perror(i32* null)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %14
br label %23
23: ; preds = %22, %0
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/vcio\00"
@.str.1 = [21 x i8] c"/dev/pi-blaster-mbox\00"
@S_IFCHR = global i32 0
@.str.2 = [33 x i8] c"Failed to create mailbox device\0A\00"
@.str.3 = [28 x i8] c"Can't open device file: %s\0A\00"
define i32 @mbox_open() {
%1 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 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 = tail call i32 @unlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @S_IFCHR
%6 = or i32 %5, 384
%7 = tail call i32 @makedev(i32 100, i32 0)
%8 = tail call i64 @mknod(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @perror(i32* null)
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %12, %0
%20 = phi i32 [ %13, %12 ], [ %1, %0 ]
ret i32 %20
}