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

/* Variables and functions */
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ atoi(int /*<<< orphan*/);
int check_pid(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int crypt_get_debug_level();
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ numeric_name(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ report_proc(int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ stat(char *, struct stat *);
void debug_processes_using_device(const char *dm_name) {
  char short_dev_name[64], dev_name[1024];
  DIR *proc_dir;
  struct dirent *proc_dentry;
  struct stat st;
  pid_t pid;
  if (crypt_get_debug_level() != -1)
    return;
  snprintf(dev_name, sizeof(dev_name), "/dev/mapper/%s", dm_name);
  if (stat(dev_name, &st) || !S_ISBLK(st.st_mode))
    return;
  snprintf(short_dev_name, sizeof(short_dev_name), "/dev/dm-%u",
           minor(st.st_rdev));
  if (!(proc_dir = opendir("/proc")))
    return;
  while ((proc_dentry = readdir(proc_dir))) {
    if (!numeric_name(proc_dentry->d_name))
      continue;
    pid = atoi(proc_dentry->d_name);
    switch (check_pid(pid, dev_name, short_dev_name)) {
    case 1:
      report_proc(pid, dev_name);
      break;
    case 2:
      report_proc(pid, short_dev_name);
    default:
      break;
    }
  }
  closedir(proc_dir);
}


x86 assembly (clang, -O0):
<debug_processes_using_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10e0 <crypt_get_debug_level@plt>
cmp    $0xffffffff,%eax
je     11e4 <debug_processes_using_device+0x24>
jmp    135a <debug_processes_using_device+0x19a>
lea    -0x450(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe05(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x450(%rbp),%rdi
lea    -0x468(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1233 <debug_processes_using_device+0x73>
mov    -0x464(%rbp),%edi
call   10a0 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    1238 <debug_processes_using_device+0x78>
jmp    135a <debug_processes_using_device+0x19a>
lea    -0x50(%rbp),%rax
mov    %rax,-0x478(%rbp)
mov    -0x468(%rbp),%edi
call   1040 <minor@plt>
mov    -0x478(%rbp),%rdi
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xdac(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    0xda9(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x458(%rbp)
cmp    $0x0,%rax
jne    128c <debug_processes_using_device+0xcc>
jmp    135a <debug_processes_using_device+0x19a>
jmp    1291 <debug_processes_using_device+0xd1>
mov    -0x458(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x460(%rbp)
cmp    $0x0,%rax
je     134e <debug_processes_using_device+0x18e>
mov    -0x460(%rbp),%rax
mov    (%rax),%edi
call   1060 <numeric_name@plt>
cmp    $0x0,%eax
jne    12ca <debug_processes_using_device+0x10a>
jmp    1291 <debug_processes_using_device+0xd1>
mov    -0x460(%rbp),%rax
mov    (%rax),%edi
call   10c0 <atoi@plt>
mov    %eax,-0x46c(%rbp)
mov    -0x46c(%rbp),%edi
lea    -0x450(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   10b0 <check_pid@plt>
mov    %eax,%ecx
mov    %ecx,-0x47c(%rbp)
sub    $0x1,%eax
je     131e <debug_processes_using_device+0x15e>
jmp    130a <debug_processes_using_device+0x14a>
mov    -0x47c(%rbp),%eax
sub    $0x2,%eax
je     1335 <debug_processes_using_device+0x175>
jmp    1344 <debug_processes_using_device+0x184>
mov    -0x46c(%rbp),%edi
lea    -0x450(%rbp),%rsi
call   10d0 <report_proc@plt>
jmp    1349 <debug_processes_using_device+0x189>
mov    -0x46c(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   10d0 <report_proc@plt>
jmp    1349 <debug_processes_using_device+0x189>
jmp    1291 <debug_processes_using_device+0xd1>
mov    -0x458(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug_processes_using_device>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   10e0 <crypt_get_debug_level@plt>
cmp    $0xffffffff,%eax
jne    120d <debug_processes_using_device+0x4d>
lea    0xe1c(%rip),%rdx
lea    0x50(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     121c <debug_processes_using_device+0x5c>
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0xc(%rsp),%edi
call   10a0 <S_ISBLK@plt>
test   %eax,%eax
je     120d <debug_processes_using_device+0x4d>
mov    0x8(%rsp),%edi
call   1040 <minor@plt>
lea    0xdd6(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xdc4(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     120d <debug_processes_using_device+0x4d>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12ca <debug_processes_using_device+0x10a>
mov    %rax,%rbx
jmp    1297 <debug_processes_using_device+0xd7>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   10d0 <report_proc@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12ca <debug_processes_using_device+0x10a>
mov    (%rbx),%edi
call   1060 <numeric_name@plt>
test   %eax,%eax
je     1287 <debug_processes_using_device+0xc7>
mov    (%rbx),%edi
call   10c0 <atoi@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   10b0 <check_pid@plt>
mov    %r15,%rsi
cmp    $0x1,%eax
je     1280 <debug_processes_using_device+0xc0>
cmp    $0x2,%eax
jne    1287 <debug_processes_using_device+0xc7>
mov    %r14,%rsi
jmp    1280 <debug_processes_using_device+0xc0>
mov    %r12,%rdi
call   1070 <closedir@plt>
jmp    120d <debug_processes_using_device+0x4d>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32, i32 }
@.str = [15 x i8] c"/dev/mapper/%s\00"
@.str.1 = [11 x i8] c"/dev/dm-%u\00"
@.str.2 = [6 x i8] c"/proc\00"
define void @debug_processes_using_device(i8* %0) {
%2 = alloca i8*
%3 = alloca [64 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i32 (...) @crypt_get_debug_level()
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
br label %68
12: ; preds = %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = load i8*, i8** %2
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %13, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i64 @stat(i8* %16, %struct.stat* %7)
%18 = icmp ne i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @S_ISBLK(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %19, %12
br label %68
25: ; preds = %19
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%27 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @minor(i32 %28)
%30 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %26, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %5
%32 = icmp ne i32* %31, null
br i1 %32, label %34, label %33
33: ; preds = %25
br label %68
34: ; preds = %25
br label %35
35: ; preds = %64, %45, %34
%36 = load i32*, i32** %5
%37 = call %struct.dirent* @readdir(i32* %36)
store %struct.dirent* %37, %struct.dirent** %6
%38 = icmp ne %struct.dirent* %37, null
br i1 %38, label %39, label %65
39: ; preds = %35
%40 = load %struct.dirent*, %struct.dirent** %6
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @numeric_name(i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %39
br label %35
46: ; preds = %39
%47 = load %struct.dirent*, %struct.dirent** %6
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @atoi(i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%54 = call i32 @check_pid(i32 %51, i8* %52, i8* %53)
switch i32 %54, label %63 [
i32 1, label %55
i32 2, label %59
]
55: ; preds = %46
%56 = load i32, i32* %8
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%58 = call i32 @report_proc(i32 %56, i8* %57)
br label %64
59: ; preds = %46
%60 = load i32, i32* %8
%61 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%62 = call i32 @report_proc(i32 %60, i8* %61)
br label %63
63: ; preds = %46, %59
br label %64
64: ; preds = %63, %55
br label %35
65: ; preds = %35
%66 = load i32*, i32** %5
%67 = call i32 @closedir(i32* %66)
br label %68
68: ; preds = %65, %33, %24, %11
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i32 }
@.str = [15 x i8] c"/dev/mapper/%s\00"
@.str.1 = [11 x i8] c"/dev/dm-%u\00"
@.str.2 = [6 x i8] c"/proc\00"
define void @debug_processes_using_device(i8* %0) {
%2 = alloca [64 x i8]
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = tail call i32 (...) @crypt_get_debug_level()
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %48
10: ; preds = %1
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = call i64 @stat(i8* %6, %struct.stat* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %48
14: ; preds = %10
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @S_ISBLK(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %48, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @minor(i32 %21)
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %48, label %26
26: ; preds = %19
%27 = call %struct.dirent* @readdir(i32* %24)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %46, label %29
29: ; preds = %26, %43
%30 = phi %struct.dirent* [ %44, %43 ], [ %27, %26 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @numeric_name(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %29
%36 = load i32, i32* %31
%37 = call i32 @atoi(i32 %36)
%38 = call i32 @check_pid(i32 %37, i8* %6, i8* %5)
switch i32 %38, label %43 [
i32 1, label %40
i32 2, label %39
]
39: ; preds = %35
br label %40
40: ; preds = %35, %39
%41 = phi i8* [ %5, %39 ], [ %6, %35 ]
%42 = call i32 @report_proc(i32 %37, i8* %41)
br label %43
43: ; preds = %40, %35, %29
%44 = call %struct.dirent* @readdir(i32* %24)
%45 = icmp eq %struct.dirent* %44, null
br i1 %45, label %46, label %29
46: ; preds = %43, %26
%47 = call i32 @closedir(i32* %24)
br label %48
48: ; preds = %19, %10, %14, %1, %46
ret void
}