#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 */
int ENOTBLK;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int nvme_verify_chr(int fd) {
  static struct stat nvme_stat;
  int err = fstat(fd, &nvme_stat);
  if (err < 0) {
    perror("fstat");
    return errno;
  }
  if (!S_ISCHR(nvme_stat.st_mode)) {
    fprintf(stderr, "Error: requesting reset on non-controller handle\n");
    return ENOTBLK;
  }
  return 0;
}


x86 assembly (clang, -O0):
<nvme_verify_chr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2eef(%rip),%rsi
call   1060 <fstat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1184 <nvme_verify_chr+0x44>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <nvme_verify_chr+0x85>
mov    0x2eba(%rip),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    11be <nvme_verify_chr+0x7e>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <nvme_verify_chr+0x85>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvme_verify_chr>:
push   %rax
lea    0x2efc(%rip),%rsi
call   1060 <fstat@plt>
test   %eax,%eax
js     1186 <nvme_verify_chr+0x46>
mov    0x2eed(%rip),%edi
call   1040 <S_ISCHR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     1166 <nvme_verify_chr+0x26>
pop    %rcx
ret
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e3f(%rip),%rax
jmp    1182 <nvme_verify_chr+0x42>

LLVM IR (-O0):
%struct.stat = type { i32 }
@nvme_verify_chr.nvme_stat = internal global %struct.stat zeroinitializer
@.str = [6 x i8] c"fstat\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [50 x i8] c"Error: requesting reset on non-controller handle\0A\00"
@ENOTBLK = global i32 0
define i32 @nvme_verify_chr(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @fstat(i32 %5, %struct.stat* @nvme_verify_chr.nvme_stat)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
store i32 %11, i32* %2
br label %21
12: ; preds = %1
%13 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @nvme_verify_chr.nvme_stat, i32 0, i32 0)
%14 = call i32 @S_ISCHR(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @ENOTBLK
store i32 %19, i32* %2
br label %21
20: ; preds = %12
store i32 0, i32* %2
br label %21
21: ; preds = %20, %16, %9
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@nvme_verify_chr.nvme_stat = internal global %struct.stat zeroinitializer
@.str = [6 x i8] c"fstat\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [50 x i8] c"Error: requesting reset on non-controller handle\0A\00"
@ENOTBLK = global i32 0
define i32 @nvme_verify_chr(i32 %0) {
%2 = tail call i32 @fstat(i32 %0, %struct.stat* @nvme_verify_chr.nvme_stat)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
br label %15
7: ; preds = %1
%8 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @nvme_verify_chr.nvme_stat, i64 0, i32 0)
%9 = tail call i32 @S_ISCHR(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @ENOTBLK
br label %15
15: ; preds = %7, %11, %4
%16 = phi i32 [ %6, %4 ], [ %14, %11 ], [ 0, %7 ]
ret i32 %16
}