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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISFIFO(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int mkdirat(int, char const *, int /*<<< orphan*/);
int mkfifoat(int, char const *, int /*<<< orphan*/);
int openat(int, char const *, int, int /*<<< orphan*/);
int mknodat(int fd, const char *path, mode_t mode, dev_t dev) {
  (void)(dev);
  if (S_ISDIR(mode)) {
    return mkdirat(fd, path, mode);
  } else if (S_ISFIFO(mode)) {
    return mkfifoat(fd, path, mode);
  } else if (S_ISREG(mode)) {
    return openat(fd, path, O_WRONLY | O_EXCL | O_CREAT, mode);
  } else {
    errno = EINVAL;
    return -1;
  }
}


x86 assembly (clang, -O0):
<mknodat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     119e <mknodat+0x3e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <mkdirat@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <mknodat+0xc4>
mov    -0x14(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%rax
je     11c7 <mknodat+0x67>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <mkfifoat@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <mknodat+0xc4>
mov    -0x14(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%rax
je     120b <mknodat+0xab>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    -0x14(%rbp),%ecx
call   1050 <openat@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <mknodat+0xc4>
mov    0x2db6(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mknodat>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     1187 <mknodat+0x27>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <mkdirat@plt>
mov    %ebx,%edi
call   1040 <S_ISFIFO@plt>
test   %rax,%rax
je     11a3 <mknodat+0x43>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mkfifoat@plt>
mov    %ebx,%edi
call   1080 <S_ISREG@plt>
test   %rax,%rax
je     11da <mknodat+0x7a>
mov    0x2e02(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%ecx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <openat@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    0x2de6(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @mknodat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
%11 = load i32, i32* %8
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = call i32 @mkdirat(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %5
br label %44
19: ; preds = %4
%20 = load i32, i32* %8
%21 = call i64 @S_ISFIFO(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = call i32 @mkfifoat(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %5
br label %44
28: ; preds = %19
%29 = load i32, i32* %8
%30 = call i64 @S_ISREG(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_EXCL
%37 = or i32 %35, %36
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = load i32, i32* %8
%41 = call i32 @openat(i32 %33, i8* %34, i32 %39, i32 %40)
store i32 %41, i32* %5
br label %44
42: ; preds = %28
%43 = load i32, i32* @EINVAL
store i32 %43, i32* @errno
store i32 -1, i32* %5
br label %44
44: ; preds = %42, %32, %23, %14
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @mknodat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i64 @S_ISDIR(i32 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @mkdirat(i32 %0, i8* %1, i32 %2)
br label %26
9: ; preds = %4
%10 = tail call i64 @S_ISFIFO(i32 %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @mkfifoat(i32 %0, i8* %1, i32 %2)
br label %26
14: ; preds = %9
%15 = tail call i64 @S_ISREG(i32 %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_EXCL
%20 = or i32 %19, %18
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = tail call i32 @openat(i32 %0, i8* %1, i32 %22, i32 %2)
br label %26
24: ; preds = %14
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
br label %26
26: ; preds = %24, %17, %12, %7
%27 = phi i32 [ %8, %7 ], [ %13, %12 ], [ %23, %17 ], [ -1, %24 ]
ret i32 %27
}