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

/* Variables and functions */
scalar_t__ PATH_MAX;
int S_IFBLK;
int S_IFDIR;
int S_IFMT;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ die(char *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int find_dev_recursive(char *dirnamebuf, int number) {
  DIR *dp;
  struct dirent *dir;
  struct stat s;
  int dirnamelen = 0;
  if ((dp = opendir(dirnamebuf)) == NULL)
    die("opendir");
  dirnamelen = strlen(dirnamebuf);
  while ((dir = readdir(dp)) != NULL) {
    if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
      continue;
    if (dirnamelen + 1 + strlen(dir->d_name) > PATH_MAX)
      continue;
    dirnamebuf[dirnamelen] = '/';
    strcpy(dirnamebuf + dirnamelen + 1, dir->d_name);
    if (lstat(dirnamebuf, &s) < 0)
      continue;
    if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number)
      return 1;
    if ((s.st_mode & S_IFMT) == S_IFDIR &&
        find_dev_recursive(dirnamebuf, number))
      return 1;
  }
  dirnamebuf[dirnamelen] = 0;
  closedir(dp);
  return 0;
}


x86 assembly (clang, -O0):
<find_dev_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c9 <find_dev_recursive+0x39>
lea    0xe3c(%rip),%rdi
call   10b0 <die@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1317 <find_dev_recursive+0x187>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0f(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     1224 <find_dev_recursive+0x94>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf2(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1229 <find_dev_recursive+0x99>
jmp    11d5 <find_dev_recursive+0x45>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jle    1260 <find_dev_recursive+0xd0>
jmp    11d5 <find_dev_recursive+0x45>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
jge    12a3 <find_dev_recursive+0x113>
jmp    11d5 <find_dev_recursive+0x45>
mov    -0x30(%rbp),%eax
mov    0x2d2b(%rip),%rcx
and    (%rcx),%eax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%eax
jne    12d6 <find_dev_recursive+0x146>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    12d6 <find_dev_recursive+0x146>
movl   $0x1,-0x4(%rbp)
jmp    1333 <find_dev_recursive+0x1a3>
mov    -0x30(%rbp),%eax
mov    0x2cf8(%rip),%rcx
and    (%rcx),%eax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%eax
jne    1312 <find_dev_recursive+0x182>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <find_dev_recursive@plt>
cmp    $0x0,%eax
je     1312 <find_dev_recursive+0x182>
movl   $0x1,-0x4(%rbp)
jmp    1333 <find_dev_recursive+0x1a3>
jmp    11d5 <find_dev_recursive+0x45>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,0x4(%rsp)
mov    %rdi,%r14
call   1050 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11be <find_dev_recursive+0x2e>
lea    0xe47(%rip),%rdi
call   10b0 <die@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   10a0 <readdir@plt>
shl    $0x20,%rbp
test   %rax,%rax
je     12ec <find_dev_recursive+0x15c>
mov    %rax,%r12
movabs $0x100000000,%r13
add    %rbp,%r13
sar    $0x20,%r13
sar    $0x20,%rbp
lea    (%r14,%rbp,1),%rax
mov    %rax,0x20(%rsp)
mov    %rbp,0x10(%rsp)
lea    (%r14,%rbp,1),%rax
add    $0x1,%rax
mov    %rax,0x18(%rsp)
lea    0xdf1(%rip),%r15
jmp    1234 <find_dev_recursive+0xa4>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     12e5 <find_dev_recursive+0x155>
mov    (%r12),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1220 <find_dev_recursive+0x90>
mov    %rbp,%rdi
lea    0xdb7(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1220 <find_dev_recursive+0x90>
mov    %rbp,%rdi
call   1060 <strlen@plt>
add    %r13,%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jg     1220 <find_dev_recursive+0x90>
mov    0x20(%rsp),%rax
movb   $0x2f,(%rax)
mov    (%r12),%rsi
mov    0x18(%rsp),%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
lea    0x8(%rsp),%rsi
call   1080 <lstat@plt>
test   %rax,%rax
js     1220 <find_dev_recursive+0x90>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    $0x1,%ebp
mov    0x2d1e(%rip),%rcx
cmp    (%rcx),%eax
jne    12c0 <find_dev_recursive+0x130>
mov    0x4(%rsp),%ecx
cmp    %ecx,0xc(%rsp)
je     12ff <find_dev_recursive+0x16f>
mov    0x2cf9(%rip),%rcx
cmp    (%rcx),%eax
jne    1220 <find_dev_recursive+0x90>
mov    %r14,%rdi
mov    0x4(%rsp),%esi
call   1040 <find_dev_recursive@plt>
test   %eax,%eax
je     1220 <find_dev_recursive+0x90>
jmp    12ff <find_dev_recursive+0x16f>
mov    0x10(%rsp),%rbp
jmp    12f0 <find_dev_recursive+0x160>
sar    $0x20,%rbp
movb   $0x0,(%r14,%rbp,1)
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@PATH_MAX = global i64 0
@S_IFMT = global i32 0
@S_IFBLK = global i32 0
@S_IFDIR = global i32 0
define i32 @find_dev_recursive(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = tail call %struct.dirent* @readdir(i32* %5)
%12 = icmp eq %struct.dirent* %11, null
%13 = shl i64 %10, 32
br i1 %12, label %14, label %16
14: ; preds = %9
%15 = ashr exact i64 %13, 32
br label %61
16: ; preds = %9
%17 = add i64 %13, 4294967296
%18 = ashr exact i64 %17, 32
%19 = ashr exact i64 %13, 32
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
br label %24
24: ; preds = %16, %33
%25 = phi %struct.dirent* [ %11, %16 ], [ %34, %33 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = tail call i32 @strcmp(i8* dereferenceable(1) %27, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = tail call i32 @strcmp(i8* dereferenceable(1) %27, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %55, %58, %24, %30, %36, %41
%34 = tail call %struct.dirent* @readdir(i32* %5)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %61, label %24
36: ; preds = %30
%37 = tail call i64 @strlen(i8* dereferenceable(1) %27)
%38 = add nsw i64 %37, %18
%39 = load i64, i64* @PATH_MAX
%40 = icmp sgt i64 %38, %39
br i1 %40, label %33, label %41
41: ; preds = %36
store i8 47, i8* %20
%42 = load i8*, i8** %26
%43 = tail call i32 @strcpy(i8* %21, i8* %42)
%44 = call i64 @lstat(i8* %0, %struct.stat* %3)
%45 = icmp slt i64 %44, 0
br i1 %45, label %33, label %46
46: ; preds = %41
%47 = load i32, i32* %22
%48 = load i32, i32* @S_IFMT
%49 = and i32 %48, %47
%50 = load i32, i32* @S_IFBLK
%51 = icmp eq i32 %49, %50
%52 = load i32, i32* %23
%53 = icmp eq i32 %52, %1
%54 = select i1 %51, i1 %53, i1 false
br i1 %54, label %65, label %55
55: ; preds = %46
%56 = load i32, i32* @S_IFDIR
%57 = icmp eq i32 %49, %56
br i1 %57, label %58, label %33
58: ; preds = %55
%59 = tail call i32 @find_dev_recursive(i8* %0, i32 %1)
%60 = icmp eq i32 %59, 0
br i1 %60, label %33, label %65
61: ; preds = %33, %14
%62 = phi i64 [ %15, %14 ], [ %19, %33 ]
%63 = getelementptr inbounds i8, i8* %0, i64 %62
store i8 0, i8* %63
%64 = tail call i32 @closedir(i32* %5)
br label %65
65: ; preds = %58, %46, %61
%66 = phi i32 [ 0, %61 ], [ 1, %46 ], [ 1, %58 ]
ret i32 %66
}