#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*/ sysctl_value;
struct stat {
  scalar_t__ st_rdev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fstat(int, struct stat *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, int);
long strtol(char *, int /*<<< orphan*/ *, int);
int sysctlbyname(char *, char *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
int devq_device_drm_get_drvname_from_fd(int fd, char *driver_name,
                                        size_t *driver_name_len) {
  int ret, i;
  struct stat st;
  char sysctl_name[32], sysctl_value[128];
  size_t sysctl_value_len, name_len;
  long dev;
  ret = fstat(fd, &st);
  if (ret != 0)
    return (-1);
  if (!S_ISCHR(st.st_mode)) {
    errno = EBADF;
    return (-1);
  }
  for (i = 0; i < 16; i++) {
    sprintf(sysctl_name, "hw.dri.%d.name", i);
    sysctl_value_len = sizeof(sysctl_value);
    ret = sysctlbyname(sysctl_name, sysctl_value, &sysctl_value_len, NULL, 0);
    if (ret != 0)
      continue;
    for (name_len = 0;
         name_len < sysctl_value_len && sysctl_value[name_len] != ' ';
         ++name_len)
      ;
    if (driver_name != NULL) {
      if (*driver_name_len < name_len) {
        *driver_name_len = name_len;
        errno = ENOMEM;
        return (-1);
      }
      memcpy(driver_name, sysctl_value, name_len);
    }
    if (driver_name_len)
      *driver_name_len = name_len;
    dev = strtol(sysctl_value + name_len, NULL, 16);
    if (dev == (long)st.st_rdev)
      return (i);
  }
  errno = ENOENT;
  return (-1);
}


x86 assembly (clang, -O0):
<devq_device_drm_get_drvname_from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     119b <devq_device_drm_get_drvname_from_fd+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <devq_device_drm_get_drvname_from_fd+0x220>
mov    -0x28(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    11ca <devq_device_drm_get_drvname_from_fd+0x6a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <devq_device_drm_get_drvname_from_fd+0x220>
movl   $0x0,-0x20(%rbp)
cmpl   $0x10,-0x20(%rbp)
jge    1367 <devq_device_drm_get_drvname_from_fd+0x207>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe17(%rip),%rsi
call   1060 <sprintf@plt>
movq   $0x80,-0xd8(%rbp)
lea    -0x50(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
lea    -0xd8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1070 <sysctlbyname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1229 <devq_device_drm_get_drvname_from_fd+0xc9>
jmp    1359 <devq_device_drm_get_drvname_from_fd+0x1f9>
movq   $0x0,-0xe0(%rbp)
mov    -0xe0(%rbp),%rcx
xor    %eax,%eax
cmp    -0xd8(%rbp),%rcx
mov    %al,-0xe9(%rbp)
jae    126b <devq_device_drm_get_drvname_from_fd+0x10b>
mov    -0xe0(%rbp),%rax
movsbl -0xd0(%rbp,%rax,1),%eax
ff
cmp    $0x20,%eax
setne  %al
mov    %al,-0xe9(%rbp)
mov    -0xe9(%rbp),%al
test   $0x1,%al
jne    127e <devq_device_drm_get_drvname_from_fd+0x11e>
jmp    129a <devq_device_drm_get_drvname_from_fd+0x13a>
jmp    1283 <devq_device_drm_get_drvname_from_fd+0x123>
mov    -0xe0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xe0(%rbp)
jmp    1234 <devq_device_drm_get_drvname_from_fd+0xd4>
cmpq   $0x0,-0x10(%rbp)
je     12fc <devq_device_drm_get_drvname_from_fd+0x19c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0xe0(%rbp),%rax
jae    12e5 <devq_device_drm_get_drvname_from_fd+0x185>
mov    -0xe0(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d12(%rip),%rax
mov    (%rax),%ecx
mov    0x2d01(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <devq_device_drm_get_drvname_from_fd+0x220>
mov    -0x10(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    -0xe0(%rbp),%rdx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     1315 <devq_device_drm_get_drvname_from_fd+0x1b5>
mov    -0xe0(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
lea    -0xd0(%rbp),%rdi
add    -0xe0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %rax,-0xe8(%rbp)
mov    -0xe8(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    1354 <devq_device_drm_get_drvname_from_fd+0x1f4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <devq_device_drm_get_drvname_from_fd+0x220>
jmp    1359 <devq_device_drm_get_drvname_from_fd+0x1f9>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11d1 <devq_device_drm_get_drvname_from_fd+0x71>
mov    0x2c52(%rip),%rax
mov    (%rax),%ecx
mov    0x2c61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devq_device_drm_get_drvname_from_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %rdx,%r13
mov    %rsi,%r15
lea    0x10(%rsp),%rsi
call   1080 <fstat@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    12ae <devq_device_drm_get_drvname_from_fd+0x14e>
mov    0x18(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %eax,%eax
je     1286 <devq_device_drm_get_drvname_from_fd+0x126>
xor    %ebp,%ebp
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0xe4e(%rip),%r12
lea    0xa0(%rsp),%r14
lea    0x20(%rsp),%rbx
jmp    11fd <devq_device_drm_get_drvname_from_fd+0x9d>
test   %r13,%r13
je     11ca <devq_device_drm_get_drvname_from_fd+0x6a>
mov    %r12,0x0(%r13)
lea    (%rsp,%r12,1),%rdi
add    $0x20,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
cmp    0x8(%rsp),%rax
lea    0xe16(%rip),%r12
lea    0x20(%rsp),%rbx
je     128f <devq_device_drm_get_drvname_from_fd+0x12f>
add    $0x1,%ebp
cmp    $0x10,%ebp
je     1278 <devq_device_drm_get_drvname_from_fd+0x118>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
movq   $0x80,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rsp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <sysctlbyname@plt>
test   %eax,%eax
jne    11f5 <devq_device_drm_get_drvname_from_fd+0x95>
mov    (%rsp),%rax
xor    %r12d,%r12d
test   %rax,%rax
je     1254 <devq_device_drm_get_drvname_from_fd+0xf4>
cs nopw 0x0(%rax,%rax,1)
nop
cmpb   $0x20,0x20(%rsp,%r12,1)
je     1254 <devq_device_drm_get_drvname_from_fd+0xf4>
add    $0x1,%r12
cmp    %r12,%rax
jne    1240 <devq_device_drm_get_drvname_from_fd+0xe0>
mov    %rax,%r12
test   %r15,%r15
je     11c1 <devq_device_drm_get_drvname_from_fd+0x61>
cmp    %r12,0x0(%r13)
jb     1293 <devq_device_drm_get_drvname_from_fd+0x133>
mov    %r15,%rdi
lea    0x20(%rsp),%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
jmp    11c6 <devq_device_drm_get_drvname_from_fd+0x66>
mov    $0xffffffff,%ebx
mov    0x2d3c(%rip),%rax
jmp    12a3 <devq_device_drm_get_drvname_from_fd+0x143>
mov    0x2d43(%rip),%rax
jmp    12a3 <devq_device_drm_get_drvname_from_fd+0x143>
mov    %ebp,%ebx
jmp    12ae <devq_device_drm_get_drvname_from_fd+0x14e>
mov    %r12,0x0(%r13)
mov    0x2d42(%rip),%rax
mov    $0xffffffff,%ebx
mov    (%rax),%eax
mov    0x2d2c(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@EBADF = global i32 0
@errno = global i32 0
@.str = [15 x i8] c"hw.dri.%d.name\00"
@ENOMEM = global i32 0
@ENOENT = global i32 0
define i32 @devq_device_drm_get_drvname_from_fd(i32 %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca [32 x i8]
%12 = alloca [128 x i8]
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64* %2, i64** %7
%16 = load i32, i32* %5
%17 = call i32 @fstat(i32 %16, %struct.stat* %10)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %99
21: ; preds = %3
%22 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @S_ISCHR(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = load i32, i32* @EBADF
store i32 %27, i32* @errno
store i32 -1, i32* %4
br label %99
28: ; preds = %21
store i32 0, i32* %9
br label %29
29: ; preds = %94, %28
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 16
br i1 %31, label %32, label %97
32: ; preds = %29
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%34 = load i32, i32* %9
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %34)
store i64 128, i64* %13
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%38 = call i32 @sysctlbyname(i8* %36, i8* %37, i64* %13, i32* null, i32 0)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %32
br label %94
42: ; preds = %32
store i64 0, i64* %14
br label %43
43: ; preds = %56, %42
%44 = load i64, i64* %14
%45 = load i64, i64* %13
%46 = icmp ult i64 %44, %45
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i64, i64* %14
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 32
br label %53
53: ; preds = %47, %43
%54 = phi i1 [ false, %43 ], [ %52, %47 ]
br i1 %54, label %55, label %59
55: ; preds = %53
br label %56
56: ; preds = %55
%57 = load i64, i64* %14
%58 = add i64 %57, 1
store i64 %58, i64* %14
br label %43
59: ; preds = %53
%60 = load i8*, i8** %6
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %76
62: ; preds = %59
%63 = load i64*, i64** %7
%64 = load i64, i64* %63
%65 = load i64, i64* %14
%66 = icmp ult i64 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i64, i64* %14
%69 = load i64*, i64** %7
store i64 %68, i64* %69
%70 = load i32, i32* @ENOMEM
store i32 %70, i32* @errno
store i32 -1, i32* %4
br label %99
71: ; preds = %62
%72 = load i8*, i8** %6
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%74 = load i64, i64* %14
%75 = call i32 @memcpy(i8* %72, i8* %73, i64 %74)
br label %76
76: ; preds = %71, %59
%77 = load i64*, i64** %7
%78 = icmp ne i64* %77, null
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i64, i64* %14
%81 = load i64*, i64** %7
store i64 %80, i64* %81
br label %82
82: ; preds = %79, %76
%83 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%84 = load i64, i64* %14
%85 = getelementptr inbounds i8, i8* %83, i64 %84
%86 = call i64 @strtol(i8* %85, i32* null, i32 16)
store i64 %86, i64* %15
%87 = load i64, i64* %15
%88 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%89 = load i64, i64* %88
%90 = icmp eq i64 %87, %89
br i1 %90, label %91, label %93
91: ; preds = %82
%92 = load i32, i32* %9
store i32 %92, i32* %4
br label %99
93: ; preds = %82
br label %94
94: ; preds = %93, %41
%95 = load i32, i32* %9
%96 = add nsw i32 %95, 1
store i32 %96, i32* %9
br label %29
97: ; preds = %29
%98 = load i32, i32* @ENOENT
store i32 %98, i32* @errno
store i32 -1, i32* %4
br label %99
99: ; preds = %97, %91, %67, %26, %20
%100 = load i32, i32* %4
ret i32 %100
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@EBADF = global i32 0
@errno = global i32 0
@.str = [15 x i8] c"hw.dri.%d.name\00"
@ENOMEM = global i32 0
@ENOENT = global i32 0
define i32 @devq_device_drm_get_drvname_from_fd(i32 %0, i8* %1, i64* %2) {
%4 = alloca %struct.stat
%5 = alloca [32 x i8]
%6 = alloca [128 x i8]
%7 = alloca i64
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = bitcast i64* %7 to i8*
%12 = call i32 @fstat(i32 %0, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %60
14: ; preds = %3
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%16 = load i32, i32* %15
%17 = tail call i32 @S_ISCHR(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %57, label %19
19: ; preds = %14
%20 = icmp eq i8* %1, null
%21 = icmp eq i64* %2, null
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%23 = load i64, i64* %22
br label %24
24: ; preds = %19, %54
%25 = phi i32 [ 0, %19 ], [ %55, %54 ]
%26 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %25)
store i64 128, i64* %7
%27 = call i32 @sysctlbyname(i8* %9, i8* %10, i64* %7, i32* null, i32 0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %54
29: ; preds = %24
%30 = load i64, i64* %7
%31 = icmp eq i64 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %29, %37
%33 = phi i64 [ %38, %37 ], [ 0, %29 ]
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 32
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = add nuw i64 %33, 1
%39 = icmp eq i64 %38, %30
br i1 %39, label %40, label %32
40: ; preds = %32, %37, %29
%41 = phi i64 [ 0, %29 ], [ %30, %37 ], [ %33, %32 ]
br i1 %20, label %48, label %42
42: ; preds = %40
%43 = load i64, i64* %2
%44 = icmp ult i64 %43, %41
br i1 %44, label %45, label %46
45: ; preds = %42
store i64 %41, i64* %2
br label %57
46: ; preds = %42
%47 = call i32 @memcpy(i8* %1, i8* %10, i64 %41)
br label %49
48: ; preds = %40
br i1 %21, label %50, label %49
49: ; preds = %46, %48
store i64 %41, i64* %2
br label %50
50: ; preds = %49, %48
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %41
%52 = call i64 @strtol(i8* nocapture %51, i32* null, i32 16)
%53 = icmp eq i64 %52, %23
br i1 %53, label %60, label %54
54: ; preds = %50, %24
%55 = add nuw nsw i32 %25, 1
%56 = icmp eq i32 %55, 16
br i1 %56, label %57, label %24
57: ; preds = %54, %14, %45
%58 = phi i32* [ @ENOMEM, %45 ], [ @EBADF, %14 ], [ @ENOENT, %54 ]
%59 = load i32, i32* %58
store i32 %59, i32* @errno
br label %60
60: ; preds = %50, %57, %3
%61 = phi i32 [ -1, %3 ], [ -1, %57 ], [ %25, %50 ]
ret i32 %61
}