#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 getpid();
int /*<<< orphan*/ makedev(int, int /*<<< orphan*/);
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ unlink(char *);
int mbox_open(void) {
  int file_desc;
  char filename[64];
  sprintf(filename, "/dev/vcio");
  file_desc = open(filename, 0);
  if (file_desc < 0) {
    printf("Failed to open %s, trying old method.\n", filename);
    sprintf(filename, "/dev/mailbox-%d", getpid());
    unlink(filename);
    if (mknod(filename, S_IFCHR | 0600, makedev(100, 0)) < 0) {
      printf("Failed to create mailbox device %s: %m\n", filename);
      return -1;
    }
    file_desc = open(filename, 0);
  }
  if (file_desc < 0) {
    printf("Can't open device file %s: %m\n", filename);
    return -1;
  }
  return file_desc;
}


x86 assembly (clang, -O0):
<mbox_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    -0x50(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1245 <mbox_open+0xd5>
lea    -0x50(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1050 <printf@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
or     $0x180,%eax
mov    %eax,-0x54(%rbp)
mov    $0x64,%edi
xor    %esi,%esi
call   1080 <makedev@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    %eax,%edx
call   1060 <mknod@plt>
cmp    $0x0,%rax
jge    1237 <mbox_open+0xc7>
lea    -0x50(%rbp),%rsi
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <mbox_open+0x101>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    126b <mbox_open+0xfb>
lea    -0x50(%rbp),%rsi
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <mbox_open+0x101>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbox_open>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
movabs $0x6963762f7665642f,%rax
76 63 69
mov    %rax,(%rsp)
movw   $0x6f,0x8(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
jns    1227 <mbox_open+0xb7>
lea    0xe66(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e08(%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   1060 <mknod@plt>
test   %rax,%rax
js     1213 <mbox_open+0xa3>
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
jns    1227 <mbox_open+0xb7>
lea    0xe58(%rip),%rdi
jmp    121a <mbox_open+0xaa>
lea    0xe27(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/vcio\00"
@.str.1 = [39 x i8] c"Failed to open %s, trying old method.\0A\00"
@.str.2 = [16 x i8] c"/dev/mailbox-%d\00"
@S_IFCHR = global i32 0
@.str.3 = [40 x i8] c"Failed to create mailbox device %s: %m\0A\00"
@.str.4 = [31 x i8] c"Can't open device file %s: %m\0A\00"
define i32 @mbox_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = call i32 @open(i8* %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %0
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%12 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%17 = call i32 @unlink(i8* %16)
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%19 = load i32, i32* @S_IFCHR
%20 = or i32 %19, 384
%21 = call i32 @makedev(i32 100, i32 0)
%22 = call i64 @mknod(i8* %18, i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %10
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%26 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %25)
store i32 -1, i32* %1
br label %38
27: ; preds = %10
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%29 = call i32 @open(i8* %28, i32 0)
store i32 %29, i32* %2
br label %30
30: ; preds = %27, %0
%31 = load i32, i32* %2
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %34)
store i32 -1, i32* %1
br label %38
36: ; preds = %30
%37 = load i32, i32* %2
store i32 %37, i32* %1
br label %38
38: ; preds = %36, %33, %24
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/vcio\00"
@.str.1 = [39 x i8] c"Failed to open %s, trying old method.\0A\00"
@.str.2 = [16 x i8] c"/dev/mailbox-%d\00"
@S_IFCHR = global i32 0
@.str.3 = [40 x i8] c"Failed to create mailbox device %s: %m\0A\00"
@.str.4 = [31 x i8] c"Can't open device file %s: %m\0A\00"
define i32 @mbox_open() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = call i32 @open(i8* %2, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = call i32 @unlink(i8* %2)
%10 = load i32, i32* @S_IFCHR
%11 = or i32 %10, 384
%12 = call i32 @makedev(i32 100, i32 0)
%13 = call i64 @mknod(i8* %2, i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %5
%16 = call i32 @open(i8* %2, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15, %5
%19 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), %5 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), %15 ]
%20 = call i32 @printf(i8* %19, i8* %2)
br label %21
21: ; preds = %18, %0, %15
%22 = phi i32 [ %16, %15 ], [ %3, %0 ], [ -1, %18 ]
ret i32 %22
}