#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 /*<<< orphan*/ S_ISDEV(int);
int /*<<< orphan*/ S_ISFIFO(int);
scalar_t__ mknod(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, int *);
int do_mknod(char *path, char *modes, char *devs) {
  int mode;
  int dev;
  mode = -1;
  sscanf(modes, "%o", &mode);
  if (mode == -1) {
    printf("mknod: bad mode\n");
    return (-1);
  }
  if (!S_ISFIFO(mode) && !S_ISDEV(mode)) {
    printf("mknod: mode is not device/fifo\n");
    return (-1);
  }
  if (sscanf(devs, "%d", &dev) != 1) {
    printf("mknod: bad device\n");
    return (-1);
  }
  if (mknod(path, mode, dev) != 0) {
    perror("mknod");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<do_mknod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7a(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <sscanf@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b1 <do_mknod+0x51>
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <do_mknod+0xf8>
mov    -0x24(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    11eb <do_mknod+0x8b>
mov    -0x24(%rbp),%edi
call   1070 <S_ISDEV@plt>
cmp    $0x0,%eax
jne    11eb <do_mknod+0x8b>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <do_mknod+0xf8>
mov    -0x20(%rbp),%rdi
lea    0xe3e(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1220 <do_mknod+0xc0>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <do_mknod+0xf8>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <mknod@plt>
cmp    $0x0,%rax
je     1251 <do_mknod+0xf1>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <do_mknod+0xf8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mknod>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%rax
mov    %rdi,%r14
movl   $0xffffffff,(%rsp)
lea    0xe85(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1060 <sscanf@plt>
mov    (%rsp),%edi
cmp    $0xffffffff,%edi
je     11e3 <do_mknod+0x83>
call   1040 <S_ISFIFO@plt>
test   %eax,%eax
jne    11a3 <do_mknod+0x43>
mov    (%rsp),%edi
call   1070 <S_ISDEV@plt>
test   %eax,%eax
je     11f5 <do_mknod+0x95>
lea    0xe59(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11ec <do_mknod+0x8c>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    %r14,%rdi
call   1050 <mknod@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1206 <do_mknod+0xa6>
lea    0xe2a(%rip),%rdi
call   1080 <perror@plt>
jmp    1201 <do_mknod+0xa1>
lea    0xe53(%rip),%rdi
jmp    11fc <do_mknod+0x9c>
lea    0xe38(%rip),%rdi
jmp    11fc <do_mknod+0x9c>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%o\00"
@.str.1 = [17 x i8] c"mknod: bad mode\0A\00"
@.str.2 = [32 x i8] c"mknod: mode is not device/fifo\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [19 x i8] c"mknod: bad device\0A\00"
@.str.5 = [6 x i8] c"mknod\00"
define i32 @do_mknod(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 -1, i32* %8
%10 = load i8*, i8** %6
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8)
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
16: ; preds = %3
%17 = load i32, i32* %8
%18 = call i32 @S_ISFIFO(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = load i32, i32* %8
%22 = call i32 @S_ISDEV(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
26: ; preds = %20, %16
%27 = load i8*, i8** %7
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %9)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
32: ; preds = %26
%33 = load i8*, i8** %5
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = call i64 @mknod(i8* %33, i32 %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
40: ; preds = %32
store i32 0, i32* %4
br label %41
41: ; preds = %40, %38, %30, %24, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"%o\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.5 = [6 x i8] c"mknod\00"
@str = [31 x i8] c"mknod: mode is not device/fifo\00"
@str.6 = [18 x i8] c"mknod: bad device\00"
@str.7 = [16 x i8] c"mknod: bad mode\00"
define i32 @do_mknod(i8* %0, i8* nocapture %1, i8* nocapture %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 -1, i32* %4
%8 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
br label %34
13: ; preds = %3
%14 = call i32 @S_ISFIFO(i32 %9)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %4
%18 = call i32 @S_ISDEV(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %34
22: ; preds = %16, %13
%23 = call i32 @sscanf(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%24 = icmp eq i32 %23, 1
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.6, i64 0, i64 0))
br label %34
27: ; preds = %22
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = call i64 @mknod(i8* %0, i32 %28, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %27, %32, %25, %20, %11
%35 = phi i32 [ -1, %11 ], [ -1, %25 ], [ -1, %32 ], [ -1, %20 ], [ 0, %27 ]
ret i32 %35
}