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


x86 assembly (clang, -O0):
<mbox_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe81(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     119e <mbox_open+0x2e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <mbox_open+0xf5>
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%edx
lea    0xe50(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e09(%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   1050 <mknod@plt>
cmp    $0x0,%rax
jge    121d <mbox_open+0xad>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <mbox_open+0xf5>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1256 <mbox_open+0xe6>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
lea    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <mbox_open+0xf5>
lea    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
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
lea    0xe83(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11f0 <mbox_open+0x80>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe72(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e2c(%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
js     11f9 <mbox_open+0x89>
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <open@plt>
test   %eax,%eax
js     120c <mbox_open+0x9c>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f0 <mbox_open+0x80>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebx
jmp    11f0 <mbox_open+0x80>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/vcio\00"
@.str.1 = [16 x i8] c"/tmp/mailbox-%d\00"
@S_IFCHR = global i32 0
@.str.2 = [33 x i8] c"Failed to create mailbox device\0A\00"
@.str.3 = [24 x i8] c"Can't open device file\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 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %24, label %5
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = call i32 @unlink(i8* %2)
%9 = load i32, i32* @S_IFCHR
%10 = or i32 %9, 384
%11 = tail call i32 @makedev(i32 100, i32 0)
%12 = call i64 @mknod(i8* %2, i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %5
%15 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %24
16: ; preds = %5
%17 = call i32 @open(i8* %2, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @unlink(i8* %2)
br label %24
22: ; preds = %16
%23 = call i32 @unlink(i8* %2)
br label %24
24: ; preds = %0, %22, %19, %14
%25 = phi i32 [ -1, %14 ], [ -1, %19 ], [ %17, %22 ], [ %3, %0 ]
ret i32 %25
}