#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ W_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
int misc_check_writable_device(const char *devno, int blockdev, int chardev) {
  struct stat stats;
  if (stat(devno, &stats)) {
    error_reason(strerror(errno));
    return -1;
  }
  if (blockdev && chardev) {
    if (!(S_ISCHR(stats.st_mode) || S_ISBLK(stats.st_mode))) {
      error_reason("Not a device");
      return -1;
    }
  } else if (blockdev) {
    if (!S_ISBLK(stats.st_mode)) {
      error_reason("Not a block device");
      return -1;
    }
  } else if (chardev) {
    if (!S_ISCHR(stats.st_mode)) {
      error_reason("Not a character device");
      return -1;
    }
  }
  if (access(devno, W_OK)) {
    error_reason(strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<misc_check_writable_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     11ab <misc_check_writable_device+0x4b>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <misc_check_writable_device+0x161>
cmpl   $0x0,-0x14(%rbp)
je     1200 <misc_check_writable_device+0xa0>
cmpl   $0x0,-0x18(%rbp)
je     1200 <misc_check_writable_device+0xa0>
mov    -0x20(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    11fb <misc_check_writable_device+0x9b>
mov    -0x20(%rbp),%edi
call   1060 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    11fb <misc_check_writable_device+0x9b>
lea    0xe16(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <misc_check_writable_device+0x161>
jmp    127c <misc_check_writable_device+0x11c>
cmpl   $0x0,-0x14(%rbp)
je     1239 <misc_check_writable_device+0xd9>
mov    -0x20(%rbp),%edi
call   1060 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    1234 <misc_check_writable_device+0xd4>
lea    0xdea(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <misc_check_writable_device+0x161>
jmp    1277 <misc_check_writable_device+0x117>
cmpl   $0x0,-0x18(%rbp)
je     1272 <misc_check_writable_device+0x112>
mov    -0x20(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    126d <misc_check_writable_device+0x10d>
lea    0xdc4(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <misc_check_writable_device+0x161>
jmp    1272 <misc_check_writable_device+0x112>
jmp    1277 <misc_check_writable_device+0x117>
jmp    127c <misc_check_writable_device+0x11c>
mov    -0x10(%rbp),%rdi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     12ba <misc_check_writable_device+0x15a>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <misc_check_writable_device+0x161>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_check_writable_device>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
jne    11ee <misc_check_writable_device+0x8e>
test   %ebx,%ebx
je     11ab <misc_check_writable_device+0x4b>
test   %ebp,%ebp
je     11ab <misc_check_writable_device+0x4b>
mov    0x8(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
jne    11d8 <misc_check_writable_device+0x78>
mov    0x8(%rsp),%edi
call   1060 <S_ISBLK@plt>
test   %rax,%rax
jne    11d8 <misc_check_writable_device+0x78>
lea    0xe57(%rip),%rdi
jmp    11ff <misc_check_writable_device+0x9f>
test   %ebx,%ebx
je     11c6 <misc_check_writable_device+0x66>
mov    0x8(%rsp),%edi
call   1060 <S_ISBLK@plt>
test   %rax,%rax
jne    11d8 <misc_check_writable_device+0x78>
lea    0xe49(%rip),%rdi
jmp    11ff <misc_check_writable_device+0x9f>
test   %ebp,%ebp
je     11d8 <misc_check_writable_device+0x78>
mov    0x8(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
je     1216 <misc_check_writable_device+0xb6>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     1212 <misc_check_writable_device+0xb2>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1209 <misc_check_writable_device+0xa9>
lea    0xe03(%rip),%rdi
jmp    11ff <misc_check_writable_device+0x9f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [13 x i8] c"Not a device\00"
@.str.1 = [19 x i8] c"Not a block device\00"
@.str.2 = [23 x i8] c"Not a character device\00"
@W_OK = global i32 0
define i32 @misc_check_writable_device(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i64 @stat(i8* %9, %struct.stat* %8)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @error_reason(i8* %14)
store i32 -1, i32* %4
br label %69
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @S_ISCHR(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISBLK(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %27
%33 = call i32 @error_reason(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %69
34: ; preds = %27, %22
br label %59
35: ; preds = %19, %16
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @S_ISBLK(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = call i32 @error_reason(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %69
45: ; preds = %38
br label %58
46: ; preds = %35
%47 = load i32, i32* %7
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @S_ISCHR(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %49
%55 = call i32 @error_reason(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %69
56: ; preds = %49
br label %57
57: ; preds = %56, %46
br label %58
58: ; preds = %57, %45
br label %59
59: ; preds = %58, %34
%60 = load i8*, i8** %5
%61 = load i32, i32* @W_OK
%62 = call i64 @access(i8* %60, i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %59
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @error_reason(i8* %66)
store i32 -1, i32* %4
br label %69
68: ; preds = %59
store i32 0, i32* %4
br label %69
69: ; preds = %68, %64, %54, %43, %32, %12
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [13 x i8] c"Not a device\00"
@.str.1 = [19 x i8] c"Not a block device\00"
@.str.2 = [23 x i8] c"Not a character device\00"
@W_OK = global i32 0
define i32 @misc_check_writable_device(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %37
8: ; preds = %3
%9 = icmp ne i32 %1, 0
%10 = icmp ne i32 %2, 0
%11 = and i1 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISCHR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %12
%18 = load i32, i32* %13
%19 = call i64 @S_ISBLK(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %40, label %33
21: ; preds = %8
br i1 %9, label %22, label %27
22: ; preds = %21
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @S_ISBLK(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %40, label %33
27: ; preds = %21
br i1 %10, label %28, label %33
28: ; preds = %27
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISCHR(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %22, %28, %27, %12, %17
%34 = load i32, i32* @W_OK
%35 = call i64 @access(i8* %0, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %33, %3
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
br label %40
40: ; preds = %37, %28, %22, %17
%41 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %17 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %22 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %28 ], [ %39, %37 ]
%42 = call i32 @error_reason(i8* %41)
br label %43
43: ; preds = %40, %33
%44 = phi i32 [ 0, %33 ], [ -1, %40 ]
ret i32 %44
}