#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*/ pid_t;
typedef int /*<<< orphan*/ DIR;

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


x86 assembly (clang, -O0):
<do_procinit>:
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_procinit+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     120c <do_procinit+0x9c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11f3 <do_procinit+0x83>
jmp    11b8 <do_procinit+0x48>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%edi
call   1070 <check@plt>
jmp    11b8 <do_procinit+0x48>
mov    -0x8(%rbp),%rdi
call   1040 <closedir@plt>
cmpl   $0x0,-0x14(%rbp)
jne    122d <do_procinit+0xbd>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_procinit>:
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_procinit+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
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11ec <do_procinit+0x7c>
mov    (%rax),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11c0 <do_procinit+0x50>
add    $0x1,%ebp
mov    (%rsp),%edi
call   1070 <check@plt>
jmp    11c0 <do_procinit+0x50>
mov    %rbx,%rdi
call   1040 <closedir@plt>
test   %ebp,%ebp
jne    1206 <do_procinit+0x96>
lea    0xe1d(%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 = [4 x i8] c"%ld\00"
@.str.3 = [32 x i8] c"nothing in /proc - not mounted?\00"
define void @do_procinit() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i64
%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 %30
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 ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %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 i64, i64* %4
%28 = trunc i64 %27 to i32
%29 = call i32 @check(i32 %28)
br label %13
30: ; preds = %13
%31 = load i32*, i32** %1
%32 = call i32 @closedir(i32* %31)
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
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 = [4 x i8] c"%ld\00"
@.str.3 = [32 x i8] c"nothing in /proc - not mounted?\00"
define void @do_procinit() {
%1 = alloca i64
%2 = bitcast i64* %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 %25, 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 ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %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 i64, i64* %1
%23 = trunc i64 %22 to i32
%24 = call i32 @check(i32 %23)
br label %10
25: ; preds = %12
%26 = call i32 @closedir(i32* %3)
%27 = icmp eq i32 %11, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret void
}