#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 latency_entry {
  int dummy;
};
struct dirent {
  int /*<<< orphan*/ *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int atoi(int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct latency_entry *read_thread_stats(struct latency_entry *, int, int, int,
                                        int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
struct latency_entry *read_process_stats(struct latency_entry *list, int erase,
                                         int pid) {
  char dirname[64];
  DIR *dir;
  struct dirent *ent;
  struct latency_entry *e;
  int tid;
  sprintf(dirname, "/proc/%d/task", pid);
  dir = opendir(dirname);
  if (!dir) {
    fprintf(stderr, "Could not open task dir for process %d.\n", pid);
    fprintf(stderr, "Perhaps the process has terminated?\n");
    exit(EXIT_FAILURE);
  }
  e = list;
  while ((ent = readdir(dir))) {
    if (!isdigit(ent->d_name[0]))
      continue;
    tid = atoi(ent->d_name);
    e = read_thread_stats(e, erase, pid, tid, 0);
  }
  closedir(dir);
  return e;
}


x86 assembly (clang, -O0):
<read_process_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe50(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    120c <read_process_stats+0x7c>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x60(%rbp)
cmp    $0x0,%rax
je     1274 <read_process_stats+0xe4>
mov    -0x60(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1247 <read_process_stats+0xb7>
jmp    1214 <read_process_stats+0x84>
mov    -0x60(%rbp),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x6c(%rbp),%ecx
xor    %r8d,%r8d
call   1050 <read_thread_stats@plt>
mov    %rax,-0x68(%rbp)
jmp    1214 <read_process_stats+0x84>
mov    -0x58(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x68(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_process_stats>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe66(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     120d <read_process_stats+0x8d>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11f7 <read_process_stats+0x77>
mov    (%rax),%rdi
mov    (%rdi),%eax
add    $0xffffffd0,%eax
cmp    $0x9,%eax
ja     11c0 <read_process_stats+0x40>
call   1080 <atoi@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %r8d,%r8d
call   1040 <read_thread_stats@plt>
mov    %rax,%rbp
jmp    11c0 <read_process_stats+0x40>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %rbp,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcc(%rip),%rbx
mov    (%rbx),%edi
lea    0xdf1(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.latency_entry = type { i32 }
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/%d/task\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Could not open task dir for process %d.\0A\00"
@.str.2 = [37 x i8] c"Perhaps the process has terminated?\0A\00"
@EXIT_FAILURE = global i32 0
define %struct.latency_entry* @read_process_stats(%struct.latency_entry* %0, i32 %1, i32 %2) {
%4 = alloca %struct.latency_entry*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca %struct.latency_entry*
%11 = alloca i32
store %struct.latency_entry* %0, %struct.latency_entry** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %6
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %27, label %19
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i32, i32* %6
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %3
%28 = load %struct.latency_entry*, %struct.latency_entry** %4
store %struct.latency_entry* %28, %struct.latency_entry** %10
br label %29
29: ; preds = %42, %41, %27
%30 = load i32*, i32** %8
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %9
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %9
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i32*, i32** %35
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = call i32 @isdigit(i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %33
br label %29
42: ; preds = %33
%43 = load %struct.dirent*, %struct.dirent** %9
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32*, i32** %44
%46 = call i32 @atoi(i32* %45)
store i32 %46, i32* %11
%47 = load %struct.latency_entry*, %struct.latency_entry** %10
%48 = load i32, i32* %5
%49 = load i32, i32* %6
%50 = load i32, i32* %11
%51 = call %struct.latency_entry* @read_thread_stats(%struct.latency_entry* %47, i32 %48, i32 %49, i32 %50, i32 0)
store %struct.latency_entry* %51, %struct.latency_entry** %10
br label %29
52: ; preds = %29
%53 = load i32*, i32** %8
%54 = call i32 @closedir(i32* %53)
%55 = load %struct.latency_entry*, %struct.latency_entry** %10
ret %struct.latency_entry* %55
}

LLVM IR (-O2):
%struct.latency_entry = type { i32 }
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/%d/task\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Could not open task dir for process %d.\0A\00"
@.str.2 = [37 x i8] c"Perhaps the process has terminated?\0A\00"
@EXIT_FAILURE = global i32 0
define %struct.latency_entry* @read_process_stats(%struct.latency_entry* %0, i32 %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = call i32* @opendir(i8* %5)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %28
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %28, %19
%17 = tail call %struct.dirent* @readdir(i32* %7)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %30, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%21 = load i32*, i32** %20
%22 = load i32, i32* %21
%23 = add i32 %22, -48
%24 = icmp ult i32 %23, 10
br i1 %24, label %25, label %16
25: ; preds = %19
%26 = tail call i32 @atoi(i32* %21)
%27 = tail call %struct.latency_entry* @read_thread_stats(%struct.latency_entry* %29, i32 %1, i32 %2, i32 %26, i32 0)
br label %28
28: ; preds = %3, %25
%29 = phi %struct.latency_entry* [ %27, %25 ], [ %0, %3 ]
br label %16
30: ; preds = %16
%31 = tail call i32 @closedir(i32* %7)
ret %struct.latency_entry* %29
}