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

/* Variables and functions */
int /*<<< orphan*/ NODEV;
int S_IFCHR;
int S_IFMT;
int /*<<< orphan*/ major(int /*<<< orphan*/);
int /*<<< orphan*/ minor(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int sysctlbyname(char *, char *, size_t *, int /*<<< orphan*/ *, int);
char *devname_r(dev_t dev, mode_t type, char *buf, int len) {
  int i;
  size_t j;
  char *r;
  if ((type & S_IFMT) == S_IFCHR) {
    j = len;
    i = sysctlbyname("kern.devname", buf, &j, &dev, sizeof(dev));
    if (i == 0)
      return (buf);
  }
  if (dev == NODEV)
    r = "#NODEV";
  else
    r = "#%c:%d:0x%x";
  snprintf(buf, len, r, (type & S_IFMT) == S_IFCHR ? 'C' : 'B', major(dev),
           minor(dev));
  return (buf);
}


x86 assembly (clang, -O0):
<devname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e79(%rip),%rcx
and    (%rcx),%eax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
jne    11b5 <devname_r+0x75>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe7d(%rip),%rdi
lea    -0x28(%rbp),%rdx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <sysctlbyname@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11b0 <devname_r+0x70>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1257 <devname_r+0x117>
jmp    11b5 <devname_r+0x75>
mov    -0xc(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11d7 <devname_r+0x97>
lea    0xe3f(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11e2 <devname_r+0xa2>
lea    0xe36(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%edx
mov    0x2dd6(%rip),%rax
and    (%rax),%edx
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    $0x42,%eax
mov    $0x43,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %al,-0x31(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <minor@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%r8d
mov    %eax,%r9d
mov    -0x31(%rbp),%al
movsbl %al,%ecx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devname_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %edi,0x4(%rsp)
mov    0x2e7b(%rip),%r12
mov    (%r12),%eax
and    %esi,%eax
mov    0x2e66(%rip),%rbx
mov    (%rbx),%ecx
cmp    %ecx,%eax
jne    11a9 <devname_r+0x69>
mov    %esi,%ebp
movslq %r14d,%rax
mov    %rax,0x8(%rsp)
lea    0xe7f(%rip),%rdi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %r15,%rsi
mov    $0x4,%r8d
call   1060 <sysctlbyname@plt>
test   %eax,%eax
je     11f5 <devname_r+0xb5>
mov    0x4(%rsp),%edi
mov    (%rbx),%ecx
and    (%r12),%ebp
mov    %ebp,%eax
mov    0x2e40(%rip),%rdx
cmp    (%rdx),%edi
lea    0xe54(%rip),%rdx
lea    0xe54(%rip),%rbp
cmove  %rdx,%rbp
xor    %ebx,%ebx
cmp    %ecx,%eax
sete   %bl
call   1050 <major@plt>
mov    %eax,%r12d
mov    0x4(%rsp),%edi
call   1030 <minor@plt>
or     $0x42,%ebx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %r12d,%r8d
mov    %eax,%r9d
call   1040 <snprintf@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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