#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*/ check(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(int /*<<< orphan*/, char *, int *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void do_procfs(void) {
  DIR *procdir;
  struct dirent *entry;
  int foundany, pid;
  procdir = opendir("/proc");
  if (!procdir)
    fatal("opendir /proc: %s", strerror(errno));
  foundany = 0;
  while ((entry = readdir(procdir)) != NULL) {
    if (sscanf(entry->d_name, "%d", &pid) != 1)
      continue;
    foundany++;
    check(pid);
  }
  closedir(procdir);
  if (!foundany)
    fatal("nothing in /proc - not mounted?");
}


x86 assembly (clang, -O0):
<do_procfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b1 <do_procfs+0x41>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1209 <do_procfs+0x99>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11f3 <do_procfs+0x83>
jmp    11b8 <do_procfs+0x48>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <check@plt>
jmp    11b8 <do_procfs+0x48>
mov    -0x8(%rbp),%rdi
call   1040 <closedir@plt>
cmpl   $0x0,-0x14(%rbp)
jne    122a <do_procfs+0xba>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_procfs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe82(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a9 <do_procfs+0x39>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <fatal@plt>
xor    %ebp,%ebp
lea    0xe66(%rip),%r14
lea    0x4(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11ed <do_procfs+0x7d>
mov    (%rax),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11c0 <do_procfs+0x50>
add    $0x1,%ebp
mov    0x4(%rsp),%edi
call   1070 <check@plt>
jmp    11c0 <do_procfs+0x50>
mov    %rbx,%rdi
call   1040 <closedir@plt>
test   %ebp,%ebp
jne    1207 <do_procfs+0x97>
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
call   1090 <fatal@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [18 x i8] c"opendir /proc: %s\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [32 x i8] c"nothing in /proc - not mounted?\00"
define void @do_procfs() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i32
%5 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = icmp ne i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %0
store i32 0, i32* %3
br label %13
13: ; preds = %24, %23, %12
%14 = load i32*, i32** %1
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %2
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %2
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @sscanf(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %17
br label %13
24: ; preds = %17
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
%27 = load i32, i32* %4
%28 = call i32 @check(i32 %27)
br label %13
29: ; preds = %13
%30 = load i32*, i32** %1
%31 = call i32 @closedir(i32* %30)
%32 = load i32, i32* %3
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [18 x i8] c"opendir /proc: %s\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [32 x i8] c"nothing in /proc - not mounted?\00"
define void @do_procfs() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %0
br label %10
10: ; preds = %9, %20
%11 = phi i32 [ %21, %20 ], [ 0, %9 ]
br label %12
12: ; preds = %10, %15
%13 = call %struct.dirent* @readdir(i32* %3)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @sscanf(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %12
20: ; preds = %15
%21 = add nuw nsw i32 %11, 1
%22 = load i32, i32* %1
%23 = call i32 @check(i32 %22)
br label %10
24: ; preds = %12
%25 = call i32 @closedir(i32* %3)
%26 = icmp eq i32 %11, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
ret void
}