#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 */
typedef int /*<<< orphan*/ uintmax_t;
typedef int /*<<< orphan*/ mode_t;
typedef scalar_t__ dev_t;
typedef int /*<<< orphan*/ dev;

/* Variables and functions */
scalar_t__ NODEV;
scalar_t__ S_ISBLK(scalar_t__);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(char *, char *, int);
int sysctlbyname(char *, char *, size_t *, scalar_t__ *, int);
char *devname_r(dev_t dev, mode_t type, char *buf, int len) {
  int i;
  size_t j;
  if (dev == NODEV || !(S_ISCHR(type) || S_ISBLK(dev))) {
    strlcpy(buf, "#NODEV", len);
    return (buf);
  }
  if (S_ISCHR(type)) {
    j = len;
    i = sysctlbyname("kern.devname", buf, &j, &dev, sizeof(dev));
    if (i == 0)
      return (buf);
  }
  snprintf(buf, len, "#%c:%#jx", S_ISCHR(type) ? 'C' : 'B', (uintmax_t)dev);
  return (buf);
}


x86 assembly (clang, -O0):
<devname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
je     119f <devname_r+0x4f>
mov    -0x14(%rbp),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    11bf <devname_r+0x6f>
mov    -0x10(%rbp),%rdi
call   1060 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    11bf <devname_r+0x6f>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe53(%rip),%rsi
call   1050 <strlcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1265 <devname_r+0x115>
mov    -0x14(%rbp),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1216 <devname_r+0xc6>
movslq -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe23(%rip),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1070 <sysctlbyname@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1211 <devname_r+0xc1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1265 <devname_r+0x115>
jmp    1216 <devname_r+0xc6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <S_ISCHR@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %rax,%rdx
mov    $0x42,%eax
mov    $0x43,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    -0x10(%rbp),%rcx
mov    %ecx,%r8d
lea    0xdbf(%rip),%rdx
movsbl %al,%ecx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devname_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rdi,0x8(%rsp)
mov    0x2e84(%rip),%rax
cmp    %rdi,(%rax)
jne    1188 <devname_r+0x38>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1050 <strlcpy@plt>
jmp    120a <devname_r+0xba>
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %esi,%edi
call   1040 <S_ISCHR@plt>
test   %rax,%rax
jne    11a7 <devname_r+0x57>
mov    %rbp,%rdi
call   1060 <S_ISBLK@plt>
test   %rax,%rax
je     1171 <devname_r+0x21>
mov    %r15d,%edi
call   1040 <S_ISCHR@plt>
test   %rax,%rax
je     11df <devname_r+0x8f>
movslq %r14d,%rax
mov    %rax,0x10(%rsp)
lea    0xe44(%rip),%rdi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rsi
mov    $0x8,%r8d
call   1070 <sysctlbyname@plt>
test   %eax,%eax
je     120a <devname_r+0xba>
mov    %r15d,%edi
call   1040 <S_ISCHR@plt>
mov    0x8(%rsp),%r8d
cmp    $0x1,%rax
mov    $0x43,%ecx
sbb    $0x0,%ecx
lea    0xe15(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NODEV = global i64 0
@.str = [7 x i8] c"#NODEV\00"
@.str.1 = [13 x i8] c"kern.devname\00"
@.str.2 = [9 x i8] c"#%c:%#jx\00"
define i8* @devname_r(i64 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i64 %0, i64* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i64, i64* %6
%13 = load i64, i64* @NODEV
%14 = icmp eq i64 %12, %13
br i1 %14, label %23, label %15
15: ; preds = %4
%16 = load i32, i32* %7
%17 = call i64 @S_ISCHR(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %15
%20 = load i64, i64* %6
%21 = call i64 @S_ISBLK(i64 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %19, %4
%24 = load i8*, i8** %8
%25 = load i32, i32* %9
%26 = call i32 @strlcpy(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %8
store i8* %27, i8** %5
br label %55
28: ; preds = %19, %15
%29 = load i32, i32* %7
%30 = call i64 @S_ISCHR(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
store i64 %34, i64* %11
%35 = load i8*, i8** %8
%36 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %35, i64* %11, i64* %6, i32 8)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = load i8*, i8** %8
store i8* %40, i8** %5
br label %55
41: ; preds = %32
br label %42
42: ; preds = %41, %28
%43 = load i8*, i8** %8
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = call i64 @S_ISCHR(i32 %45)
%47 = icmp ne i64 %46, 0
%48 = zext i1 %47 to i64
%49 = select i1 %47, i32 67, i32 66
%50 = trunc i32 %49 to i8
%51 = load i64, i64* %6
%52 = trunc i64 %51 to i32
%53 = call i32 @snprintf(i8* %43, i32 %44, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext %50, i32 %52)
%54 = load i8*, i8** %8
store i8* %54, i8** %5
br label %55
55: ; preds = %42, %39, %23
%56 = load i8*, i8** %5
ret i8* %56
}

LLVM IR (-O2):
@NODEV = global i64 0
@.str = [7 x i8] c"#NODEV\00"
@.str.1 = [13 x i8] c"kern.devname\00"
@.str.2 = [9 x i8] c"#%c:%#jx\00"
define i8* @devname_r(i64 %0, i32 %1, i8* returned %2, i32 %3) {
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %5
%7 = bitcast i64* %6 to i8*
%8 = load i64, i64* @NODEV
%9 = icmp eq i64 %8, %0
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = tail call i64 @S_ISCHR(i32 %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i64 @S_ISBLK(i64 %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13, %4
%17 = tail call i32 @strlcpy(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %32
18: ; preds = %13, %10
%19 = tail call i64 @S_ISCHR(i32 %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = sext i32 %3 to i64
store i64 %22, i64* %6
%23 = call i32 @sysctlbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %2, i64* %6, i64* %5, i32 8)
%24 = icmp eq i32 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21, %18
%26 = call i64 @S_ISCHR(i32 %1)
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i8 66, i8 67
%29 = load i64, i64* %5
%30 = trunc i64 %29 to i32
%31 = call i32 @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8 signext %28, i32 %30)
br label %32
32: ; preds = %21, %25, %16
ret i8* %2
}