#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_dev;
  int /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ perror(char *);
int get_dev_from_fd(int fd, dev_t *dev, int verbose) {
  struct stat st;
  if (0 != fstat(fd, &st)) {
    int err = errno;
    if (verbose)
      perror(" fstat() failed");
    return err;
  }
  if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
    *dev = st.st_rdev;
  else
    *dev = st.st_dev;
  return 0;
}


x86 assembly (clang, -O0):
<get_dev_from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1199 <get_dev_from_fd+0x59>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     118e <get_dev_from_fd+0x4e>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11db <get_dev_from_fd+0x9b>
mov    -0x18(%rbp),%edi
call   1040 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    11bd <get_dev_from_fd+0x7d>
mov    -0x18(%rbp),%edi
call   1030 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11cb <get_dev_from_fd+0x8b>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11d4 <get_dev_from_fd+0x94>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dev_from_fd>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <fstat@plt>
test   %rax,%rax
je     1175 <get_dev_from_fd+0x35>
mov    0x2e7f(%rip),%rax
mov    (%rax),%ebx
test   %ebp,%ebp
je     1199 <get_dev_from_fd+0x59>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
jmp    1199 <get_dev_from_fd+0x59>
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
call   1040 <S_ISBLK@plt>
test   %rax,%rax
jne    1191 <get_dev_from_fd+0x51>
mov    %ebp,%edi
call   1030 <S_ISCHR@plt>
test   %rax,%rax
je     11a2 <get_dev_from_fd+0x62>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rsp),%eax
jmp    1195 <get_dev_from_fd+0x55>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@errno = global i32 0
@.str = [16 x i8] c" fstat() failed\00"
define i32 @get_dev_from_fd(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i64 @fstat(i32 %10, %struct.stat* %8)
%12 = icmp ne i64 0, %11
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = load i32, i32* @errno
store i32 %14, i32* %9
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = load i32, i32* %9
store i32 %20, i32* %4
br label %40
21: ; preds = %3
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%23 = load i32, i32* %22
%24 = call i64 @S_ISBLK(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i64 @S_ISCHR(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26, %21
%32 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i32*, i32** %6
store i32 %33, i32* %34
br label %39
35: ; preds = %26
%36 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32*, i32** %6
store i32 %37, i32* %38
br label %39
39: ; preds = %35, %31
store i32 0, i32* %4
br label %40
40: ; preds = %39, %19
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@errno = global i32 0
@.str = [16 x i8] c" fstat() failed\00"
define i32 @get_dev_from_fd(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @fstat(i32 %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = icmp eq i32 %2, 0
br i1 %10, label %29, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %29
13: ; preds = %3
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISBLK(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i64 @S_ISCHR(i32 %15)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18, %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%23 = load i32, i32* %22
br label %27
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%26 = load i32, i32* %25
br label %27
27: ; preds = %24, %21
%28 = phi i32 [ %26, %24 ], [ %23, %21 ]
store i32 %28, i32* %1
br label %29
29: ; preds = %8, %11, %27
%30 = phi i32 [ 0, %27 ], [ %9, %11 ], [ %9, %8 ]
ret i32 %30
}