#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 dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ major_minor_match(int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strdup(int /*<<< orphan*/);
char *find_dev_name(int major, int minor) {
  int saved_errno;
  DIR *enum_dir;
  struct dirent *enum_ent;
  int fd;
  char *dev_name = NULL;
  if ((enum_dir = opendir("/dev/cxl")) == NULL)
    return NULL;
  fd = dirfd(enum_dir);
  saved_errno = errno;
  errno = 0;
  do {
    if (!(enum_ent = readdir(enum_dir))) {
      if (errno == 0)
        errno = saved_errno;
      goto err_exit;
    }
  } while (!major_minor_match(fd, enum_ent->d_name, major, minor));
  if ((dev_name = strdup(enum_ent->d_name)) == NULL)
    goto err_exit;
  closedir(enum_dir);
  return dev_name;
err_exit:
  closedir(enum_dir);
  return NULL;
}


x86 assembly (clang, -O0):
<find_dev_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe83(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119d <find_dev_name+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1268 <find_dev_name+0x108>
mov    -0x20(%rbp),%rdi
call   1050 <dirfd@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e24(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11fa <find_dev_name+0x9a>
mov    0x2e00(%rip),%rax
cmpl   $0x0,(%rax)
jne    11f5 <find_dev_name+0x95>
mov    -0x14(%rbp),%ecx
mov    0x2ded(%rip),%rax
mov    %ecx,(%rax)
jmp    1257 <find_dev_name+0xf7>
jmp    11ff <find_dev_name+0x9f>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1070 <major_minor_match@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c2 <find_dev_name+0x62>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1080 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1241 <find_dev_name+0xe1>
jmp    1257 <find_dev_name+0xf7>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1268 <find_dev_name+0x108>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r12d
lea    0xe88(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11e4 <find_dev_name+0x84>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <dirfd@plt>
mov    %eax,%ebp
mov    0x2e4a(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x0(%r13)
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11e8 <find_dev_name+0x88>
mov    %rax,%rbx
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12d,%edx
mov    %r15d,%ecx
call   1070 <major_minor_match@plt>
test   %eax,%eax
je     11b0 <find_dev_name+0x50>
mov    (%rbx),%edi
call   1080 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f9 <find_dev_name+0x99>
jmp    11f7 <find_dev_name+0x97>
xor    %ebx,%ebx
jmp    1201 <find_dev_name+0xa1>
cmpl   $0x0,0x0(%r13)
jne    11f7 <find_dev_name+0x97>
mov    0x4(%rsp),%eax
mov    %eax,0x0(%r13)
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <closedir@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"/dev/cxl\00"
@errno = global i32 0
define i8* @find_dev_name(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %10
%11 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %53
14: ; preds = %2
%15 = load i32*, i32** %7
%16 = call i32 @dirfd(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* @errno
store i32 %17, i32* %6
store i32 0, i32* @errno
br label %18
18: ; preds = %29, %14
%19 = load i32*, i32** %7
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %8
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* %6
store i32 %26, i32* @errno
br label %27
27: ; preds = %25, %22
br label %50
28: ; preds = %18
br label %29
29: ; preds = %28
%30 = load i32, i32* %9
%31 = load %struct.dirent*, %struct.dirent** %8
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* %4
%35 = load i32, i32* %5
%36 = call i32 @major_minor_match(i32 %30, i32 %33, i32 %34, i32 %35)
%37 = icmp ne i32 %36, 0
%38 = xor i1 %37, true
br i1 %38, label %18, label %39
39: ; preds = %29
%40 = load %struct.dirent*, %struct.dirent** %8
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @strdup(i32 %42)
store i8* %43, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %39
br label %50
46: ; preds = %39
%47 = load i32*, i32** %7
%48 = call i32 @closedir(i32* %47)
%49 = load i8*, i8** %10
store i8* %49, i8** %3
br label %53
50: ; preds = %45, %27
%51 = load i32*, i32** %7
%52 = call i32 @closedir(i32* %51)
store i8* null, i8** %3
br label %53
53: ; preds = %50, %46, %13
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"/dev/cxl\00"
@errno = global i32 0
define i8* @find_dev_name(i32 %0, i32 %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %29, label %5
5: ; preds = %2
%6 = tail call i32 @dirfd(i32* %3)
%7 = load i32, i32* @errno
store i32 0, i32* @errno
br label %8
8: ; preds = %15, %5
%9 = tail call %struct.dirent* @readdir(i32* %3)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %11
store i32 %7, i32* @errno
br label %25
15: ; preds = %8
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @major_minor_match(i32 %6, i32 %17, i32 %0, i32 %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %8, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i8* @strdup(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20, %11, %14
br label %26
26: ; preds = %20, %25
%27 = phi i8* [ null, %25 ], [ %23, %20 ]
%28 = tail call i32 @closedir(i32* %3)
br label %29
29: ; preds = %26, %2
%30 = phi i8* [ null, %2 ], [ %27, %26 ]
ret i8* %30
}