#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 */
typedef int /*<<< orphan*/ pathname;
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
scalar_t__ EEXIST;
int ENOENT;
int S_IFCHR;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int chrdev_get_major();
scalar_t__ errno;
int /*<<< orphan*/ makedev(int, unsigned int);
int mknod(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int chrdev_create(unsigned minor) {
  int majno;
  int x;
  int ret = 0;
  dev_t dev;
  char pathname[64];
  snprintf(pathname, sizeof(pathname), "/dev/mhvtl%u", minor);
  majno = chrdev_get_major();
  if (majno == -ENOENT) {
    ;
    ret = -1;
    goto err;
  }
  dev = makedev(majno, minor);
  ;
  ;
  x = mknod(pathname, S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, dev);
  if (x < 0) {
    if (errno == EEXIST)
      return 0;

    ;
    ret = -1;
  }
err:
  return ret;
}


x86 assembly (clang, -O0):
<chrdev_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe9b(%rip),%rdx
call   1030 <snprintf@plt>
mov    $0x0,%al
call   1050 <chrdev_get_major@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e20(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1196 <chrdev_create+0x56>
movl   $0xffffffff,-0x14(%rbp)
jmp    121c <chrdev_create+0xdc>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1060 <makedev@plt>
mov    %eax,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    -0x18(%rbp),%edx
call   1040 <mknod@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1217 <chrdev_create+0xd7>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <chrdev_create+0xd0>
movl   $0x0,-0x4(%rbp)
jmp    1222 <chrdev_create+0xe2>
movl   $0xffffffff,-0x14(%rbp)
jmp    121c <chrdev_create+0xdc>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrdev_create>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
lea    0xeb1(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1030 <snprintf@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <chrdev_get_major@plt>
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ecx
add    %eax,%ecx
jne    117b <chrdev_create+0x3b>
mov    $0xffffffff,%ebp
jmp    11dc <chrdev_create+0x9c>
mov    %eax,%edi
mov    %ebx,%esi
call   1060 <makedev@plt>
mov    0x2e3d(%rip),%rcx
mov    0x2e1e(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e43(%rip),%rcx
or     (%rcx),%esi
mov    0x2e12(%rip),%rcx
or     (%rcx),%esi
mov    0x2e39(%rip),%rcx
or     (%rcx),%esi
mov    %rsp,%rdi
mov    %eax,%edx
call   1040 <mknod@plt>
test   %eax,%eax
jns    11dc <chrdev_create+0x9c>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rcx
mov    0x2e00(%rip),%rdx
xor    %eax,%eax
mov    $0xffffffff,%ebp
cmp    (%rdx),%rcx
je     11de <chrdev_create+0x9e>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/mhvtl%u\00"
@ENOENT = global i32 0
@S_IFCHR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @chrdev_create(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %3
store i32 0, i32* %6
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @chrdev_get_major()
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* @ENOENT
%15 = sub nsw i32 0, %14
%16 = icmp eq i32 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %6
br label %43
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* %3
%21 = call i32 @makedev(i32 %19, i32 %20)
store i32 %21, i32* %7
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* @S_IFCHR
%24 = load i32, i32* @S_IRUSR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IWGRP
%31 = or i32 %29, %30
%32 = load i32, i32* %7
%33 = call i32 @mknod(i8* %22, i32 %31, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %18
%37 = load i64, i64* @errno
%38 = load i64, i64* @EEXIST
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %2
br label %45
41: ; preds = %36
store i32 -1, i32* %6
br label %42
42: ; preds = %41, %18
br label %43
43: ; preds = %42, %17
%44 = load i32, i32* %6
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %40
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/mhvtl%u\00"
@ENOENT = global i32 0
@S_IFCHR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @chrdev_create(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 (...) @chrdev_get_major()
%6 = load i32, i32* @ENOENT
%7 = sub nsw i32 0, %6
%8 = icmp eq i32 %5, %7
br i1 %8, label %26, label %9
9: ; preds = %1
%10 = call i32 @makedev(i32 %5, i32 %0)
%11 = load i32, i32* @S_IFCHR
%12 = load i32, i32* @S_IRUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWGRP
%19 = or i32 %17, %18
%20 = call i32 @mknod(i8* %3, i32 %19, i32 %10)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %9
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %28, label %26
26: ; preds = %22, %1, %9
%27 = phi i32 [ 0, %9 ], [ -1, %1 ], [ -1, %22 ]
br label %28
28: ; preds = %22, %26
%29 = phi i32 [ %27, %26 ], [ 0, %22 ]
ret i32 %29
}