#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_rdev;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ dev_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_BLK;
scalar_t__ DT_UNKNOWN;
int ENOENT;
scalar_t__ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char const *);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int find_dev_in_directory(dev_t dev, const char *dir, char *path, int verbose) {
  DIR *dp;
  struct dirent *entry;
  unsigned int maj = major(dev), min = minor(dev);
  *path = '\0';
  if (!(dp = opendir(dir))) {
    int err = errno;
    if (verbose)
      perror(dir);
    return err;
  }
  while ((entry = readdir(dp)) != NULL) {
    if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_BLK) {
      struct stat st;
      sprintf(path, "%s/%s", dir, entry->d_name);
      if (stat(path, &st)) {
        if (verbose)
          perror(path);
      } else if (S_ISBLK(st.st_mode)) {
        if (major(st.st_rdev) == maj && minor(st.st_rdev) == min) {
          closedir(dp);
          return 0;
        }
      }
    }
  }
  closedir(dp);
  *path = '\0';
  if (verbose)
    fprintf(stderr, "%d,%d: device not found in %s\n", major(dev), minor(dev),
            dir);
  return ENOENT;
}


x86 assembly (clang, -O0):
<find_dev_in_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <minor@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1214 <find_dev_in_directory+0x74>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1209 <find_dev_in_directory+0x69>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1363 <find_dev_in_directory+0x1c3>
jmp    1219 <find_dev_in_directory+0x79>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1306 <find_dev_in_directory+0x166>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
je     125e <find_dev_in_directory+0xbe>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1301 <find_dev_in_directory+0x161>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd8b(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12a9 <find_dev_in_directory+0x109>
cmpl   $0x0,-0x1c(%rbp)
je     12a4 <find_dev_in_directory+0x104>
mov    -0x18(%rbp),%rdi
call   10b0 <perror@plt>
jmp    12fc <find_dev_in_directory+0x15c>
mov    -0x44(%rbp),%edi
call   10a0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     12f7 <find_dev_in_directory+0x157>
mov    -0x48(%rbp),%edi
call   1050 <major@plt>
cmp    -0x34(%rbp),%eax
jne    12f2 <find_dev_in_directory+0x152>
mov    -0x48(%rbp),%edi
call   1040 <minor@plt>
cmp    -0x38(%rbp),%eax
jne    12f2 <find_dev_in_directory+0x152>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1363 <find_dev_in_directory+0x1c3>
jmp    12f7 <find_dev_in_directory+0x157>
jmp    12fc <find_dev_in_directory+0x15c>
jmp    1301 <find_dev_in_directory+0x161>
jmp    1219 <find_dev_in_directory+0x79>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x0,-0x1c(%rbp)
je     1357 <find_dev_in_directory+0x1b7>
mov    0x2cb1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <minor@plt>
mov    -0x50(%rbp),%edi
mov    -0x4c(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
lea    0xcb4(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev_in_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %edi,%ebx
call   1050 <major@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <minor@plt>
mov    %eax,0x10(%rsp)
movb   $0x0,0x0(%rbp)
mov    %r15,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e2 <find_dev_in_directory+0x142>
mov    %rax,%r13
mov    %r14d,0x4(%rsp)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1294 <find_dev_in_directory+0xf4>
mov    0x2de6(%rip),%r12
mov    0x2dbf(%rip),%r14
jmp    124c <find_dev_in_directory+0xac>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rax),%rcx
mov    %rbp,%rdi
lea    0xde2(%rip),%rsi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     125c <find_dev_in_directory+0xbc>
cmpl   $0x0,0x4(%rsp)
jne    128a <find_dev_in_directory+0xea>
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1294 <find_dev_in_directory+0xf4>
mov    (%rax),%rcx
cmp    (%r12),%rcx
je     1210 <find_dev_in_directory+0x70>
cmp    (%r14),%rcx
jne    123f <find_dev_in_directory+0x9f>
jmp    1210 <find_dev_in_directory+0x70>
mov    0xc(%rsp),%edi
call   10a0 <S_ISBLK@plt>
test   %rax,%rax
je     123f <find_dev_in_directory+0x9f>
mov    0x8(%rsp),%edi
call   1050 <major@plt>
cmp    0x14(%rsp),%eax
jne    123f <find_dev_in_directory+0x9f>
mov    0x8(%rsp),%edi
call   1040 <minor@plt>
cmp    0x10(%rsp),%eax
jne    123f <find_dev_in_directory+0x9f>
jmp    1301 <find_dev_in_directory+0x161>
mov    %rbp,%rdi
call   10b0 <perror@plt>
jmp    123f <find_dev_in_directory+0x9f>
mov    %r13,%rdi
call   1060 <closedir@plt>
movb   $0x0,0x0(%rbp)
cmpl   $0x0,0x4(%rsp)
je     12d7 <find_dev_in_directory+0x137>
mov    0x2d2a(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <major@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <minor@plt>
lea    0xd3e(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %eax,%ecx
mov    %r15,%r8
call   1070 <fprintf@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%ebp
jmp    12f0 <find_dev_in_directory+0x150>
mov    0x2ce7(%rip),%rax
mov    (%rax),%ebp
test   %r14d,%r14d
jne    130d <find_dev_in_directory+0x16d>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1060 <closedir@plt>
xor    %ebp,%ebp
jmp    12f0 <find_dev_in_directory+0x150>
mov    %r15,%rdi
call   10b0 <perror@plt>
jmp    12f0 <find_dev_in_directory+0x150>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
%struct.stat = type { i32, i32 }
@errno = global i32 0
@DT_UNKNOWN = global i64 0
@DT_BLK = global i64 0
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"%d,%d: device not found in %s\0A\00"
@ENOENT = global i32 0
define i32 @find_dev_in_directory(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca %struct.dirent*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.stat
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%16 = load i32, i32* %6
%17 = call i32 @major(i32 %16)
store i32 %17, i32* %12
%18 = load i32, i32* %6
%19 = call i32 @minor(i32 %18)
store i32 %19, i32* %13
%20 = load i8*, i8** %8
store i8 0, i8* %20
%21 = load i8*, i8** %7
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %33, label %24
24: ; preds = %4
%25 = load i32, i32* @errno
store i32 %25, i32* %14
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = call i32 @perror(i8* %29)
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %14
store i32 %32, i32* %5
br label %107
33: ; preds = %4
br label %34
34: ; preds = %90, %33
%35 = load i32*, i32** %10
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %11
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %91
38: ; preds = %34
%39 = load %struct.dirent*, %struct.dirent** %11
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @DT_UNKNOWN
%43 = icmp eq i64 %41, %42
br i1 %43, label %50, label %44
44: ; preds = %38
%45 = load %struct.dirent*, %struct.dirent** %11
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @DT_BLK
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %90
50: ; preds = %44, %38
%51 = load i8*, i8** %8
%52 = load i8*, i8** %7
%53 = load %struct.dirent*, %struct.dirent** %11
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 1
%55 = load i8*, i8** %54
%56 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %52, i8* %55)
%57 = load i8*, i8** %8
%58 = call i64 @stat(i8* %57, %struct.stat* %15)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %50
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i8*, i8** %8
%65 = call i32 @perror(i8* %64)
br label %66
66: ; preds = %63, %60
br label %89
67: ; preds = %50
%68 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 1
%69 = load i32, i32* %68
%70 = call i64 @S_ISBLK(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %88
72: ; preds = %67
%73 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 @major(i32 %74)
%76 = load i32, i32* %12
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %87
78: ; preds = %72
%79 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%80 = load i32, i32* %79
%81 = call i32 @minor(i32 %80)
%82 = load i32, i32* %13
%83 = icmp eq i32 %81, %82
br i1 %83, label %84, label %87
84: ; preds = %78
%85 = load i32*, i32** %10
%86 = call i32 @closedir(i32* %85)
store i32 0, i32* %5
br label %107
87: ; preds = %78, %72
br label %88
88: ; preds = %87, %67
br label %89
89: ; preds = %88, %66
br label %90
90: ; preds = %89, %44
br label %34
91: ; preds = %34
%92 = load i32*, i32** %10
%93 = call i32 @closedir(i32* %92)
%94 = load i8*, i8** %8
store i8 0, i8* %94
%95 = load i32, i32* %9
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %105
97: ; preds = %91
%98 = load i32, i32* @stderr
%99 = load i32, i32* %6
%100 = call i32 @major(i32 %99)
%101 = load i32, i32* %6
%102 = call i32 @minor(i32 %101)
%103 = load i8*, i8** %7
%104 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %100, i32 %102, i8* %103)
br label %105
105: ; preds = %97, %91
%106 = load i32, i32* @ENOENT
store i32 %106, i32* %5
br label %107
107: ; preds = %105, %84, %31
%108 = load i32, i32* %5
ret i32 %108
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i64, i8* }
@errno = global i32 0
@DT_UNKNOWN = global i64 0
@DT_BLK = global i64 0
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"%d,%d: device not found in %s\0A\00"
@ENOENT = global i32 0
define i32 @find_dev_in_directory(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = tail call i32 @major(i32 %0)
%7 = tail call i32 @minor(i32 %0)
store i8 0, i8* %2
%8 = tail call i32* @opendir(i8* %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %18, label %10
10: ; preds = %4
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %59, label %13
13: ; preds = %10
%14 = bitcast %struct.stat* %5 to i8*
%15 = icmp eq i32 %3, 0
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %23
18: ; preds = %4
%19 = load i32, i32* @errno
%20 = icmp eq i32 %3, 0
br i1 %20, label %69, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* %1)
br label %69
23: ; preds = %13, %56
%24 = phi %struct.dirent* [ %11, %13 ], [ %57, %56 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_UNKNOWN
%28 = icmp eq i64 %26, %27
%29 = load i64, i64* @DT_BLK
%30 = icmp eq i64 %26, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %56
32: ; preds = %23
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 1
%34 = load i8*, i8** %33
%35 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %34)
%36 = call i64 @stat(i8* %2, %struct.stat* %5)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %32
br i1 %15, label %55, label %39
39: ; preds = %38
%40 = call i32 @perror(i8* %2)
br label %55
41: ; preds = %32
%42 = load i32, i32* %16
%43 = call i64 @S_ISBLK(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %55, label %45
45: ; preds = %41
%46 = load i32, i32* %17
%47 = call i32 @major(i32 %46)
%48 = icmp eq i32 %47, %6
br i1 %48, label %49, label %55
49: ; preds = %45
%50 = load i32, i32* %17
%51 = call i32 @minor(i32 %50)
%52 = icmp eq i32 %51, %7
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @closedir(i32* %8)
br label %69
55: ; preds = %39, %38, %45, %49, %41
br label %56
56: ; preds = %55, %23
%57 = call %struct.dirent* @readdir(i32* %8)
%58 = icmp eq %struct.dirent* %57, null
br i1 %58, label %59, label %23
59: ; preds = %56, %10
%60 = call i32 @closedir(i32* %8)
store i8 0, i8* %2
%61 = icmp eq i32 %3, 0
br i1 %61, label %67, label %62
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 @major(i32 %0)
%65 = call i32 @minor(i32 %0)
%66 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %64, i32 %65, i8* %1)
br label %67
67: ; preds = %62, %59
%68 = load i32, i32* @ENOENT
br label %69
69: ; preds = %53, %18, %21, %67
%70 = phi i32 [ %68, %67 ], [ %19, %21 ], [ %19, %18 ], [ 0, %53 ]
ret i32 %70
}