#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*/ *opendir(char *);
int /*<<< orphan*/ print_proc(int);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(int /*<<< orphan*/, char *, int *, int *);
int strlen(int /*<<< orphan*/);
void print_procs(void) {
  DIR *dir = opendir("/proc");
  struct dirent *de;
  printf(" PID USER EXECUTABLE\n");
  while ((de = readdir(dir))) {
    int pid, end;
    int r = sscanf(de->d_name, "%d%n", &pid, &end);
    if (r == 1 && end == strlen(de->d_name)) {
      print_proc(pid);
    }
  }
}


x86 assembly (clang, -O0):
<print_procs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11ed <print_procs+0x8d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1070 <sscanf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    11e8 <print_procs+0x88>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jne    11e8 <print_procs+0x88>
mov    -0x14(%rbp),%edi
call   1080 <print_proc@plt>
jmp    1184 <print_procs+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_procs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ee <print_procs+0x8e>
mov    %rax,%rbx
lea    0xe69(%rip),%r14
lea    0x4(%rsp),%r15
mov    %rsp,%r12
jmp    11c0 <print_procs+0x60>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ee <print_procs+0x8e>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11b0 <print_procs+0x50>
mov    (%rsp),%ebp
mov    (%rbx),%edi
call   1050 <strlen@plt>
cmp    %eax,%ebp
jne    11b0 <print_procs+0x50>
mov    0x4(%rsp),%edi
call   1080 <print_proc@plt>
jmp    11b0 <print_procs+0x50>
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"
@.str.1 = [22 x i8] c" PID USER EXECUTABLE\0A\00"
@.str.2 = [5 x i8] c"%d%n\00"
define void @print_procs() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %1
%7 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %29, %0
%9 = load i32*, i32** %1
%10 = call %struct.dirent* @readdir(i32* %9)
store %struct.dirent* %10, %struct.dirent** %2
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = load %struct.dirent*, %struct.dirent** %2
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @sscanf(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = load i32, i32* %4
%21 = load %struct.dirent*, %struct.dirent** %2
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strlen(i32 %23)
%25 = icmp eq i32 %20, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* %3
%28 = call i32 @print_proc(i32 %27)
br label %29
29: ; preds = %26, %19, %12
br label %8
30: ; preds = %8
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.2 = [5 x i8] c"%d%n\00"
@str = [21 x i8] c" PID USER EXECUTABLE\00"
define void @print_procs() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%5 = call %struct.dirent* @readdir(i32* %3)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %27, label %7
7: ; preds = %0
%8 = bitcast i32* %1 to i8*
%9 = bitcast i32* %2 to i8*
br label %10
10: ; preds = %7, %24
%11 = phi %struct.dirent* [ %5, %7 ], [ %25, %24 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @sscanf(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %24
16: ; preds = %10
%17 = load i32, i32* %2
%18 = load i32, i32* %12
%19 = call i32 @strlen(i32 %18)
%20 = icmp eq i32 %17, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %1
%23 = call i32 @print_proc(i32 %22)
br label %24
24: ; preds = %21, %16, %10
%25 = call %struct.dirent* @readdir(i32* %3)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %10
27: ; preds = %24, %0
ret void
}