#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_TRUNC;
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 mkdir(char const *, int /*<<< orphan*/);
int mkfifo(char const *, int /*<<< orphan*/);
int open(char const *, int, int /*<<< orphan*/);
int mknod(const char *path, mode_t mode, dev_t dev) {
  (void)(dev);
  if (S_ISDIR(mode)) {
    return mkdir(path, mode);
  } else if (S_ISFIFO(mode)) {
    return mkfifo(path, mode);
  } else if (S_ISREG(mode)) {
    return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
  } else {
    errno = EINVAL;
    return -1;
  }
}


x86 assembly (clang, -O0):
<mknod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1198 <mknod+0x38>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    1218 <mknod+0xb8>
mov    -0x14(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%rax
je     11be <mknod+0x5e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <mkfifo@plt>
mov    %eax,-0x4(%rbp)
jmp    1218 <mknod+0xb8>
mov    -0x14(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     11ff <mknod+0x9f>
mov    -0x10(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    -0x14(%rbp),%edx
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    1218 <mknod+0xb8>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc1(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mknod>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     1186 <mknod+0x26>
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <mkdir@plt>
mov    %ebx,%edi
call   1040 <S_ISFIFO@plt>
test   %rax,%rax
je     11a3 <mknod+0x43>
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <mkfifo@plt>
mov    %ebx,%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11db <mknod+0x7b>
mov    0x2e02(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <open@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    0x2de5(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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