#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_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ _IO(char, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int ioctl(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int nvme_get_nsid(int fd) {
  static struct stat nvme_stat;
  int err = fstat(fd, &nvme_stat);
  if (err < 0)
    return -errno;
  if (!S_ISBLK(nvme_stat.st_mode)) {
    fprintf(stderr, "Error: requesting namespace-id from non-block device\n");
    errno = ENOTBLK;
    return -errno;
  }
  return ioctl(fd, _IO('N', 0x40));
}


x86 assembly (clang, -O0):
<nvme_get_nsid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2ee7(%rip),%rsi
call   1070 <fstat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    118a <nvme_get_nsid+0x3a>
xor    %eax,%eax
mov    0x2e58(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <nvme_get_nsid+0xaa>
mov    0x2ebc(%rip),%edi
call   1060 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    11d8 <nvme_get_nsid+0x88>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    0x2e15(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
mov    0x2e0a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <nvme_get_nsid+0xaa>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x4e,%edi
mov    $0x40,%esi
call   1030 <_IO@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvme_get_nsid>:
push   %rbx
mov    %edi,%ebx
lea    0x2ef2(%rip),%rsi
call   1070 <fstat@plt>
test   %eax,%eax
js     118b <nvme_get_nsid+0x3b>
mov    0x2ee3(%rip),%edi
call   1060 <S_ISBLK@plt>
test   %eax,%eax
je     1198 <nvme_get_nsid+0x48>
mov    $0x4e,%edi
mov    $0x40,%esi
call   1030 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
pop    %rbx
jmp    1040 <ioctl@plt>
mov    0x2e46(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
mov    %eax,(%rcx)
neg    %eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@nvme_get_nsid.nvme_stat = internal global %struct.stat zeroinitializer
@errno = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"Error: requesting namespace-id from non-block device\0A\00"
@ENOTBLK = global i32 0
define i32 @nvme_get_nsid(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_get_nsid.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 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %26
12: ; preds = %1
%13 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @nvme_get_nsid.nvme_stat, i32 0, i32 0)
%14 = call i32 @S_ISBLK(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @ENOTBLK
store i32 %19, i32* @errno
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
br label %26
22: ; preds = %12
%23 = load i32, i32* %3
%24 = call i32 @_IO(i8 signext 78, i32 64)
%25 = call i32 @ioctl(i32 %23, i32 %24)
store i32 %25, i32* %2
br label %26
26: ; preds = %22, %16, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@nvme_get_nsid.nvme_stat = internal global %struct.stat zeroinitializer
@errno = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"Error: requesting namespace-id from non-block device\0A\00"
@ENOTBLK = global i32 0
define i32 @nvme_get_nsid(i32 %0) {
%2 = tail call i32 @fstat(i32 %0, %struct.stat* @nvme_get_nsid.nvme_stat)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = sub nsw i32 0, %5
br label %19
7: ; preds = %1
%8 = load i32, i32* getelementptr inbounds (%struct.stat, %struct.stat* @nvme_get_nsid.nvme_stat, i64 0, i32 0)
%9 = tail call i32 @S_ISBLK(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @ENOTBLK
store i32 %14, i32* @errno
%15 = sub nsw i32 0, %14
br label %19
16: ; preds = %7
%17 = tail call i32 @_IO(i8 signext 78, i32 64)
%18 = tail call i32 @ioctl(i32 %0, i32 %17)
br label %19
19: ; preds = %16, %11, %4
%20 = phi i32 [ %6, %4 ], [ %18, %16 ], [ %15, %11 ]
ret i32 %20
}