#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 {
  char *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(char *, char *);
long strtol(char *, char **, int);
int lsof_main(int argc, char *argv[]) {
  long int pid = 0;
  char *endptr;
  if (argc == 2) {
    pid = strtol(argv[1], &endptr, 10);
  }
  print_header();
  if (pid) {
    lsof_dumpinfo(pid);
  } else {
    DIR *dir = opendir("/proc");
    if (dir == NULL) {
      fprintf(stderr, "Couldn't open /proc\n");
      return -1;
    }
    struct dirent *de;
    while ((de = readdir(dir))) {
      if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
        continue;
      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)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11bb <lsof_main+0x3b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <print_header@plt>
cmpq   $0x0,-0x18(%rbp)
je     11db <lsof_main+0x5b>
mov    -0x18(%rbp),%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    12b5 <lsof_main+0x135>
lea    0xe1e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1217 <lsof_main+0x97>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <lsof_main+0x13c>
jmp    121c <lsof_main+0x9c>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12ac <lsof_main+0x12c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xddb(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     126b <lsof_main+0xeb>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdbe(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1270 <lsof_main+0xf0>
jmp    121c <lsof_main+0x9c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     129e <lsof_main+0x11e>
jmp    121c <lsof_main+0x9c>
mov    -0x18(%rbp),%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    121c <lsof_main+0x9c>
mov    -0x28(%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11c3 <lsof_main+0x43>
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <print_header@plt>
test   %r14,%r14
je     11ca <lsof_main+0x4a>
mov    %r14,%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    1288 <lsof_main+0x108>
xor    %eax,%eax
call   10a0 <print_header@plt>
lea    0xe2f(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     126e <lsof_main+0xee>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1262 <lsof_main+0xe2>
lea    0xe26(%rip),%r14
lea    0xe1e(%rip),%r15
lea    0x8(%rsp),%r12
jmp    121d <lsof_main+0x9d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1262 <lsof_main+0xe2>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1210 <lsof_main+0x90>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1210 <lsof_main+0x90>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1210 <lsof_main+0x90>
mov    %rax,%rdi
call   1050 <lsof_dumpinfo@plt>
jmp    1210 <lsof_main+0x90>
mov    %r13,%rdi
call   1040 <closedir@plt>
xor    %ebx,%ebx
jmp    1288 <lsof_main+0x108>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.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 i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @strtol(i8* %15, i8** %7, i32 10)
store i64 %16, i64* %6
br label %17
17: ; preds = %12, %2
%18 = call i32 (...) @print_header()
%19 = load i64, i64* %6
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i64, i64* %6
%23 = call i32 @lsof_dumpinfo(i64 %22)
br label %65
24: ; preds = %17
%25 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
31: ; preds = %24
br label %32
32: ; preds = %59, %58, %48, %31
%33 = load i32*, i32** %8
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %9
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %62
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %9
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %9
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @strcmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42, %36
br label %32
49: ; preds = %42
%50 = load %struct.dirent*, %struct.dirent** %9
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call i64 @strtol(i8* %52, i8** %7, i32 10)
store i64 %53, i64* %6
%54 = load i8*, i8** %7
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %49
br label %32
59: ; preds = %49
%60 = load i64, i64* %6
%61 = call i32 @lsof_dumpinfo(i64 %60)
br label %32
62: ; preds = %32
%63 = load i32*, i32** %8
%64 = call i32 @closedir(i32* %63)
br label %65
65: ; preds = %62, %21
store i32 0, i32* %3
br label %66
66: ; preds = %65, %28
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.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 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (...) @print_header()
br label %16
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i64 @strtol(i8* %10, i8** %3, i32 10)
%12 = tail call i32 (...) @print_header()
%13 = icmp eq i64 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @lsof_dumpinfo(i64 %11)
br label %46
16: ; preds = %6, %8
%17 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = tail call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %44, label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %46
25: ; preds = %19, %34
%26 = phi %struct.dirent* [ %35, %34 ], [ %20, %19 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = tail call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = tail call i32 @strcmp(i8* dereferenceable(1) %28, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25, %31, %42, %37
%35 = tail call %struct.dirent* @readdir(i32* %17)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %44, label %25
37: ; preds = %31
%38 = call i64 @strtol(i8* %28, i8** %3, i32 10)
%39 = load i8*, i8** %3
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 0
br i1 %41, label %42, label %34
42: ; preds = %37
%43 = tail call i32 @lsof_dumpinfo(i64 %38)
br label %34
44: ; preds = %34, %19
%45 = tail call i32 @closedir(i32* %17)
br label %46
46: ; preds = %14, %44, %22
%47 = phi i32 [ -1, %22 ], [ 0, %44 ], [ 0, %14 ]
ret i32 %47
}