#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*/ O_RDONLY;
int PATH_MAX;
scalar_t__ append_buf(char const *, int, int, char *, size_t *);
scalar_t__ close(int);
scalar_t__ copy_buf(int, int, char *, size_t *);
int open(char *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int read_proc(DIR *proc, const char *uptime, size_t uptimelen, int outfd,
              char *outbuf, size_t *outlen) {
  struct dirent *ent;
  rewinddir(proc);
  if (append_buf(uptime, uptimelen, outfd, outbuf, outlen) < 0)
    return -1;
  while ((ent = readdir(proc)) != NULL) {
    char filename[PATH_MAX];
    int fd;
    if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
      continue;
    sprintf(filename, "/proc/%s/stat", ent->d_name);
    fd = open(filename, O_RDONLY);
    if (fd < 0)
      continue;
    if (copy_buf(fd, outfd, outbuf, outlen) < 0)
      ;
    if (close(fd) < 0)
      continue;
  }
  if (append_buf("\n", 1, outfd, outbuf, outlen) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<read_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewinddir@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
call   1090 <append_buf@plt>
cmp    $0x0,%rax
jge    11c8 <read_proc+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <read_proc+0x1b5>
jmp    11cd <read_proc+0x5d>
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12ec <read_proc+0x17c>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     1236 <read_proc+0xc6>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1242 <read_proc+0xd2>
movl   $0x2,-0x58(%rbp)
jmp    12cb <read_proc+0x15b>
mov    -0x60(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rdx
lea    0xdac(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x60(%rbp),%rdi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1284 <read_proc+0x114>
movl   $0x2,-0x58(%rbp)
jmp    12cb <read_proc+0x15b>
mov    -0x54(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1060 <copy_buf@plt>
cmp    $0x0,%rax
jge    12a6 <read_proc+0x136>
jmp    12a6 <read_proc+0x136>
mov    -0x54(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    12c4 <read_proc+0x154>
movl   $0x2,-0x58(%rbp)
jmp    12cb <read_proc+0x15b>
movl   $0x0,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x58(%rbp),%eax
test   %eax,%eax
je     12e7 <read_proc+0x177>
jmp    12e2 <read_proc+0x172>
jmp    11cd <read_proc+0x5d>
jmp    11cd <read_proc+0x5d>
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
lea    0xd10(%rip),%rdi
mov    $0x1,%esi
call   1090 <append_buf@plt>
cmp    $0x0,%rax
jge    131e <read_proc+0x1ae>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <read_proc+0x1b5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r12
mov    %r8,%rbx
mov    %ecx,%r13d
mov    %rdx,-0x38(%rbp)
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <rewinddir@plt>
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
mov    %r13d,-0x2c(%rbp)
mov    %r13d,%edx
mov    %rbx,-0x40(%rbp)
mov    %rbx,%rcx
mov    %r12,-0x38(%rbp)
mov    %r12,%r8
call   1090 <append_buf@plt>
test   %rax,%rax
js     1276 <read_proc+0x106>
mov    %r14,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1254 <read_proc+0xe4>
mov    0x2e15(%rip),%r15
jmp    11f0 <read_proc+0x80>
nopl   (%rax)
mov    %r12,%rsp
mov    %r14,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1254 <read_proc+0xe4>
mov    %rsp,%r12
mov    (%r15),%ecx
mov    %rsp,%r13
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
mov    (%rax),%rdx
movzbl (%rdx),%eax
add    $0xc6,%al
cmp    $0xf6,%al
jb     11e0 <read_proc+0x70>
mov    %r13,%rdi
lea    0xde3(%rip),%rsi
call   1080 <sprintf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e0 <read_proc+0x70>
mov    %eax,%ebx
mov    %eax,%edi
mov    -0x2c(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1060 <copy_buf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11e0 <read_proc+0x70>
lea    0xdb3(%rip),%rdi
mov    $0x1,%esi
mov    -0x2c(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%r8
call   1090 <append_buf@plt>
sar    $0x3f,%rax
jmp    127b <read_proc+0x10b>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%s/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @read_proc(i32* %0, i8* %1, i64 %2, i32 %3, i8* %4, i64* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64*
%14 = alloca %struct.dirent*
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i32* %0, i32** %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i64* %5, i64** %13
%19 = load i32*, i32** %8
%20 = call i32 @rewinddir(i32* %19)
%21 = load i8*, i8** %9
%22 = load i64, i64* %10
%23 = trunc i64 %22 to i32
%24 = load i32, i32* %11
%25 = load i8*, i8** %12
%26 = load i64*, i64** %13
%27 = call i64 @append_buf(i8* %21, i32 %23, i32 %24, i8* %25, i64* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %6
store i32 -1, i32* %7
br label %92
30: ; preds = %6
br label %31
31: ; preds = %83, %80, %30
%32 = load i32*, i32** %8
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %14
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %35, label %84
35: ; preds = %31
%36 = load i32, i32* @PATH_MAX
%37 = zext i32 %36 to i64
%38 = call i8* @llvm.stacksave()
store i8* %38, i8** %15
%39 = alloca i8, i64 %37
store i64 %37, i64* %16
%40 = load %struct.dirent*, %struct.dirent** %14
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp slt i32 %45, 48
br i1 %46, label %55, label %47
47: ; preds = %35
%48 = load %struct.dirent*, %struct.dirent** %14
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp sgt i32 %53, 57
br i1 %54, label %55, label %56
55: ; preds = %47, %35
store i32 2, i32* %18
br label %80
56: ; preds = %47
%57 = load %struct.dirent*, %struct.dirent** %14
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @O_RDONLY
%62 = call i32 @open(i8* %39, i32 %61)
store i32 %62, i32* %17
%63 = load i32, i32* %17
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %56
store i32 2, i32* %18
br label %80
66: ; preds = %56
%67 = load i32, i32* %17
%68 = load i32, i32* %11
%69 = load i8*, i8** %12
%70 = load i64*, i64** %13
%71 = call i64 @copy_buf(i32 %67, i32 %68, i8* %69, i64* %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %66
br label %74
74: ; preds = %73, %66
%75 = load i32, i32* %17
%76 = call i64 @close(i32 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %74
store i32 2, i32* %18
br label %80
79: ; preds = %74
store i32 0, i32* %18
br label %80
80: ; preds = %79, %78, %65, %55
%81 = load i8*, i8** %15
%82 = load i32, i32* %18
switch i32 %82, label %94 [
i32 0, label %83
i32 2, label %31
]
83: ; preds = %80
br label %31
84: ; preds = %31
%85 = load i32, i32* %11
%86 = load i8*, i8** %12
%87 = load i64*, i64** %13
%88 = call i64 @append_buf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %85, i8* %86, i64* %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %84
store i32 -1, i32* %7
br label %92
91: ; preds = %84
store i32 0, i32* %7
br label %92
92: ; preds = %91, %90, %29
%93 = load i32, i32* %7
ret i32 %93
94: ; preds = %80
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%s/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @read_proc(i32* %0, i8* %1, i64 %2, i32 %3, i8* %4, i64* %5) {
%7 = tail call i32 @rewinddir(i32* %0)
%8 = trunc i64 %2 to i32
%9 = tail call i64 @append_buf(i8* %1, i32 %8, i32 %3, i8* %4, i64* %5)
%10 = icmp slt i64 %9, 0
br i1 %10, label %40, label %11
11: ; preds = %6
%12 = tail call %struct.dirent* @readdir(i32* %0)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %36, label %14
14: ; preds = %11, %33
%15 = phi %struct.dirent* [ %34, %33 ], [ %12, %11 ]
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = tail call i8* @llvm.stacksave()
%19 = alloca i8, i64 %17
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
%23 = add i8 %22, -58
%24 = icmp ult i8 %23, -10
br i1 %24, label %33, label %25
25: ; preds = %14
%26 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %21)
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %19, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = tail call i64 @copy_buf(i32 %28, i32 %3, i8* %4, i64* %5)
%32 = tail call i64 @close(i32 %28)
br label %33
33: ; preds = %30, %25, %14
tail call void @llvm.stackrestore(i8* %18)
%34 = tail call %struct.dirent* @readdir(i32* %0)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %14
36: ; preds = %33, %11
%37 = tail call i64 @append_buf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %3, i8* %4, i64* %5)
%38 = ashr i64 %37, 63
%39 = trunc i64 %38 to i32
br label %40
40: ; preds = %36, %6
%41 = phi i32 [ -1, %6 ], [ %39, %36 ]
ret i32 %41
}