#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 atoi(int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ isdigit(int /*<<< orphan*/);
int *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int *realloc(int *, int);
int strlen(int /*<<< orphan*/ *);
int *jetspace_get_pids(int *size) {
  DIR *dir = opendir("/proc/");
  if (dir == NULL)
    return NULL;
  struct dirent *ent;
  int n = 0;
  int *tgt = malloc(sizeof(int));
  while ((ent = readdir(dir)) != NULL) {
    bool valid = true;
    for (int x = 0; x < strlen(ent->d_name); x++)
      if (!isdigit(ent->d_name[x]))
        valid = false;
    if (!valid)
      continue;
    n++;
    tgt = realloc(tgt, sizeof(*tgt) * n);
    tgt[n - 1] = atoi(ent->d_name);
  }
  closedir(dir);
  *size = n;
  return tgt;
}


x86 assembly (clang, -O0):
<jetspace_get_pids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <jetspace_get_pids+0x34>
movq   $0x0,-0x8(%rbp)
jmp    12af <jetspace_get_pids+0x12f>
movl   $0x0,-0x24(%rbp)
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1295 <jetspace_get_pids+0x115>
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jge    1243 <jetspace_get_pids+0xc3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1230 <jetspace_get_pids+0xb0>
movl   $0x0,-0x34(%rbp)
jmp    1235 <jetspace_get_pids+0xb5>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ee <jetspace_get_pids+0x6e>
cmpl   $0x0,-0x34(%rbp)
jne    1252 <jetspace_get_pids+0xd2>
jmp    11c9 <jetspace_get_pids+0x49>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11c9 <jetspace_get_pids+0x49>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jetspace_get_pids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe78(%rip),%rdi
call   1030 <opendir@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1279 <jetspace_get_pids+0x109>
mov    %rax,%r15
mov    %rbx,0x8(%rsp)
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1267 <jetspace_get_pids+0xf7>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %r13d,%r13d
jmp    1209 <jetspace_get_pids+0x99>
nopl   0x0(%rax)
lea    0x1(%r13),%r14d
lea    0x4(,%r13,4),%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
mov    %rax,%r12
mov    (%rbx),%rdi
call   1090 <atoi@plt>
movslq %r13d,%rcx
mov    %eax,(%r12,%rcx,4)
mov    %r14d,%r13d
mov    %r15,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1267 <jetspace_get_pids+0xf7>
mov    (%rbx),%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11d0 <jetspace_get_pids+0x60>
mov    %r13,0x10(%rsp)
mov    %r12,%r13
mov    %r15,%r12
mov    $0x1,%r14d
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    (%rbx),%rdi
mov    (%rdi,%r15,4),%eax
add    $0xffffffd0,%eax
cmp    $0xa,%eax
cmovae %ebp,%r14d
add    $0x1,%r15
call   1040 <strlen@plt>
cltq
cmp    %rax,%r15
jl     1230 <jetspace_get_pids+0xc0>
test   %r14d,%r14d
mov    %r12,%r15
mov    %r13,%r12
mov    0x10(%rsp),%r13
jne    11d0 <jetspace_get_pids+0x60>
jmp    11f9 <jetspace_get_pids+0x89>
mov    %r15,%rdi
call   1050 <closedir@plt>
mov    0x8(%rsp),%rax
mov    %r13d,(%rax)
jmp    127c <jetspace_get_pids+0x10c>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [7 x i8] c"/proc/\00"
define i32* @jetspace_get_pids(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
%10 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32* null, i32** %2
br label %71
14: ; preds = %1
store i32 0, i32* %6
%15 = call i32* @malloc(i32 4)
store i32* %15, i32** %7
br label %16
16: ; preds = %47, %46, %14
%17 = load i32*, i32** %4
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %5
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %65
20: ; preds = %16
store i32 1, i32* %8
store i32 0, i32* %9
br label %21
21: ; preds = %40, %20
%22 = load i32, i32* %9
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = call i32 @strlen(i32* %25)
%27 = icmp slt i32 %22, %26
br i1 %27, label %28, label %43
28: ; preds = %21
%29 = load %struct.dirent*, %struct.dirent** %5
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i32*, i32** %30
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = call i32 @isdigit(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %28
store i32 0, i32* %8
br label %39
39: ; preds = %38, %28
br label %40
40: ; preds = %39
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %21
43: ; preds = %21
%44 = load i32, i32* %8
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %43
br label %16
47: ; preds = %43
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
%50 = load i32*, i32** %7
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = mul i64 4, %52
%54 = trunc i64 %53 to i32
%55 = call i32* @realloc(i32* %50, i32 %54)
store i32* %55, i32** %7
%56 = load %struct.dirent*, %struct.dirent** %5
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i32*, i32** %57
%59 = call i32 @atoi(i32* %58)
%60 = load i32*, i32** %7
%61 = load i32, i32* %6
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %60, i64 %63
store i32 %59, i32* %64
br label %16
65: ; preds = %16
%66 = load i32*, i32** %4
%67 = call i32 @closedir(i32* %66)
%68 = load i32, i32* %6
%69 = load i32*, i32** %3
store i32 %68, i32* %69
%70 = load i32*, i32** %7
store i32* %70, i32** %2
br label %71
71: ; preds = %65, %13
%72 = load i32*, i32** %2
ret i32* %72
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [7 x i8] c"/proc/\00"
define i32* @jetspace_get_pids(i32* nocapture writeonly %0) {
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %48, label %4
4: ; preds = %1
%5 = tail call i32* @malloc(i32 4)
%6 = tail call %struct.dirent* @readdir(i32* %2)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %44, label %8
8: ; preds = %4, %39
%9 = phi %struct.dirent* [ %42, %39 ], [ %6, %4 ]
%10 = phi i32* [ %41, %39 ], [ %5, %4 ]
%11 = phi i32 [ %40, %39 ], [ 0, %4 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%13 = load i32*, i32** %12
%14 = tail call i32 @strlen(i32* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %18, label %31
16: ; preds = %18
%17 = icmp eq i32 %26, 0
br i1 %17, label %39, label %31
18: ; preds = %8, %18
%19 = phi i64 [ %27, %18 ], [ 0, %8 ]
%20 = phi i32 [ %26, %18 ], [ 1, %8 ]
%21 = load i32*, i32** %12
%22 = getelementptr inbounds i32, i32* %21, i64 %19
%23 = load i32, i32* %22
%24 = add i32 %23, -48
%25 = icmp ult i32 %24, 10
%26 = select i1 %25, i32 %20, i32 0
%27 = add nuw nsw i64 %19, 1
%28 = tail call i32 @strlen(i32* %21)
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %18, label %16
31: ; preds = %8, %16
%32 = add nsw i32 %11, 1
%33 = shl i32 %32, 2
%34 = tail call i32* @realloc(i32* %10, i32 %33)
%35 = load i32*, i32** %12
%36 = tail call i32 @atoi(i32* %35)
%37 = sext i32 %11 to i64
%38 = getelementptr inbounds i32, i32* %34, i64 %37
store i32 %36, i32* %38
br label %39
39: ; preds = %16, %31
%40 = phi i32 [ %32, %31 ], [ %11, %16 ]
%41 = phi i32* [ %34, %31 ], [ %10, %16 ]
%42 = tail call %struct.dirent* @readdir(i32* %2)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %8
44: ; preds = %39, %4
%45 = phi i32 [ 0, %4 ], [ %40, %39 ]
%46 = phi i32* [ %5, %4 ], [ %41, %39 ]
%47 = tail call i32 @closedir(i32* %2)
store i32 %45, i32* %0
br label %48
48: ; preds = %1, %44
%49 = phi i32* [ %46, %44 ], [ null, %1 ]
ret i32* %49
}