#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lsof_dumpinfo(long);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ print_header();
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
long strtol(int /*<<< orphan*/, char **, int);
int lsof_main(int argc, char *argv[]) {
  DIR *dir = opendir("/proc");
  if (dir == NULL) {
    fprintf(stderr, "Couldn't open /proc\n");
    return -1;
  }
  print_header();
  struct dirent *de;
  while ((de = readdir(dir))) {
    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
      continue;
    char *endptr;
    long int pid = strtol(de->d_name, &endptr, 10);
    if (*endptr != '\0')
      continue;
    lsof_dumpinfo(pid);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<lsof_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cb <lsof_main+0x4b>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <lsof_main+0xef>
mov    $0x0,%al
call   10a0 <print_header@plt>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     125f <lsof_main+0xdf>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     121f <lsof_main+0x9f>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1224 <lsof_main+0xa4>
jmp    11d2 <lsof_main+0x52>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1251 <lsof_main+0xd1>
jmp    11d2 <lsof_main+0x52>
mov    -0x30(%rbp),%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    11d2 <lsof_main+0x52>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsof_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     122b <lsof_main+0xab>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   10a0 <print_header@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1221 <lsof_main+0xa1>
mov    %rax,%rbp
lea    0xe58(%rip),%r13
lea    0xe50(%rip),%r15
mov    %rsp,%r12
jmp    11e0 <lsof_main+0x60>
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1221 <lsof_main+0xa1>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11d0 <lsof_main+0x50>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11d0 <lsof_main+0x50>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    11d0 <lsof_main+0x50>
mov    %rax,%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    11d0 <lsof_main+0x50>
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    1246 <lsof_main+0xc6>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Couldn't open /proc\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i32 @lsof_main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %39
9: ; preds = %2
%10 = tail call i32 (...) @print_header()
%11 = call %struct.dirent* @readdir(i32* %4)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %37, label %13
13: ; preds = %9
%14 = bitcast i8** %3 to i8*
br label %15
15: ; preds = %13, %25
%16 = phi %struct.dirent* [ %11, %13 ], [ %26, %25 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strcmp(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %15
%22 = load i32, i32* %17
%23 = call i32 @strcmp(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15, %21, %36
%26 = call %struct.dirent* @readdir(i32* %4)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %37, label %15
28: ; preds = %21
%29 = load i32, i32* %17
%30 = call i64 @strtol(i32 %29, i8** %3, i32 10)
%31 = load i8*, i8** %3
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @lsof_dumpinfo(i64 %30)
br label %36
36: ; preds = %28, %34
br label %25
37: ; preds = %25, %9
%38 = call i32 @closedir(i32* %4)
br label %39
39: ; preds = %37, %6
%40 = phi i32 [ -1, %6 ], [ 0, %37 ]
ret i32 %40
}