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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ check_process(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_proc(char *);
int /*<<< orphan*/ print_usage(int, char **);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  DIR *dirp;
  struct dirent *directory_entry;
  char dirname[256];
  if (argc != 2) {
    print_usage(argc, argv);
    return 1;
  }
  printf("Processes with handles to \"%s\":\n", argv[1]);
  if ((dirp = opendir("/proc")) == NULL) {
    perror("Unabled to open /proc");
    return 1;
  }
  do {
    if ((directory_entry = readdir(dirp)) != NULL) {
      if (directory_entry->d_type == DT_DIR) {
        strcpy(dirname, "/proc");
        strcat(dirname, "/");
        strcat(dirname, directory_entry->d_name);
        if (!check_process(dirname, argv[1])) {
          print_proc(dirname);
        }
      }
    }
  } while (directory_entry != NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x3b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10b0 <print_usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x138>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe26(%rip),%rdi
call   1050 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1211 <main+0x81>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x138>
jmp    1216 <main+0x86>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12b1 <main+0x121>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <main+0x11c>
lea    -0x120(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
call   10a0 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1060 <check_process@plt>
cmp    $0x0,%eax
jne    12a7 <main+0x117>
lea    -0x120(%rbp),%rdi
call   1080 <print_proc@plt>
jmp    12ac <main+0x11c>
jmp    12b1 <main+0x121>
jmp    12b6 <main+0x126>
cmpq   $0x0,-0x20(%rbp)
jne    1216 <main+0x86>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r12
cmp    $0x2,%edi
jne    1259 <main+0xc9>
mov    0x8(%r12),%rsi
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
lea    0xe6c(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     127c <main+0xec>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <readdir@plt>
mov    %rax,%r14
xor    %eax,%eax
test   %r14,%r14
je     126a <main+0xda>
mov    0x2ded(%rip),%r15
mov    %rsp,%rbx
lea    0xe3a(%rip),%r13
jmp    1210 <main+0x80>
nop
mov    %rbp,%rdi
call   1070 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1268 <main+0xd8>
mov    (%r14),%rax
cmp    (%r15),%rax
jne    1200 <main+0x70>
mov    %rbx,%rdi
lea    0xe0f(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcat@plt>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    0x8(%r12),%rsi
mov    %rbx,%rdi
call   1060 <check_process@plt>
test   %eax,%eax
jne    1200 <main+0x70>
mov    %rbx,%rdi
call   1080 <print_proc@plt>
jmp    1200 <main+0x70>
mov    %r12,%rsi
call   10b0 <print_usage@plt>
mov    $0x1,%eax
jmp    126a <main+0xda>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    126a <main+0xda>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [33 x i8] c"Processes with handles to \22%s\22:\0A\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [22 x i8] c"Unabled to open /proc\00"
@DT_DIR = global i64 0
@.str.3 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @print_usage(i32 %12, i8** %13)
store i32 1, i32* %3
br label %61
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %61
24: ; preds = %15
br label %25
25: ; preds = %57, %24
%26 = load i32*, i32** %6
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %7
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %56
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %7
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @DT_DIR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %55
35: ; preds = %29
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%37 = call i32 @strcpy(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%41 = load %struct.dirent*, %struct.dirent** %7
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 1
%43 = load i8*, i8** %42
%44 = call i32 @strcat(i8* %40, i8* %43)
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @check_process(i8* %45, i8* %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %35
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%53 = call i32 @print_proc(i8* %52)
br label %54
54: ; preds = %51, %35
br label %55
55: ; preds = %54, %29
br label %56
56: ; preds = %55, %25
br label %57
57: ; preds = %56
%58 = load %struct.dirent*, %struct.dirent** %7
%59 = icmp ne %struct.dirent* %58, null
br i1 %59, label %25, label %60
60: ; preds = %57
store i32 0, i32* %3
br label %61
61: ; preds = %60, %22, %11
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [33 x i8] c"Processes with handles to \22%s\22:\0A\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [22 x i8] c"Unabled to open /proc\00"
@DT_DIR = global i64 0
@.str.3 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @print_usage(i32 %0, i8** %1)
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = call %struct.dirent* @readdir(i32* %12)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %39, label %19
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %39
19: ; preds = %14, %36
%20 = phi %struct.dirent* [ %37, %36 ], [ %15, %14 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* @DT_DIR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %36
25: ; preds = %19
%26 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 1
%29 = load i8*, i8** %28
%30 = call i32 @strcat(i8* %4, i8* %29)
%31 = load i8*, i8** %9
%32 = call i32 @check_process(i8* %4, i8* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @print_proc(i8* %4)
br label %36
36: ; preds = %25, %34, %19
%37 = call %struct.dirent* @readdir(i32* %12)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %19
39: ; preds = %36, %14, %17, %6
%40 = phi i32 [ 1, %6 ], [ 1, %17 ], [ 0, %14 ], [ 0, %36 ]
ret i32 %40
}