#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*/ err_exit(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ errno;
int *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int *resize_list(int *, int *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int *active_processes(void) {
  DIR *dirp;
  struct dirent *direntp;
  int *proc_list;
  int num_processes = 0;
  int list_index = 0;
  int list_size = 25;
  int pid;
  dirp = opendir("/proc");
  if (dirp != NULL) {
    proc_list = malloc(sizeof(int) * list_size);
    if (proc_list == NULL) {
      err_exit("Error on malloc: %s, line no %d\n", strerror(errno), 129);
    }
    proc_list[0] = num_processes;
    list_index = 1;
    while ((direntp = readdir(dirp)) != NULL) {
      pid = atoi(direntp->d_name);
      if (pid != 0) {
        proc_list[list_index++] = pid;
        num_processes++;
        if (list_index >= list_size) {
          proc_list = resize_list(proc_list, &list_size);
        }
      }
    }
    proc_list[0] = num_processes;
    closedir(dirp);
  } else {
    err_exit("Error on opendir, %s\n", strerror(errno));
  }
  return proc_list;
}


x86 assembly (clang, -O0):
<active_processes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x19,-0x24(%rbp)
lea    0xe5c(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1297 <active_processes+0x117>
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f9 <active_processes+0x79>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xe19(%rip),%rdi
mov    $0x81,%edx
mov    $0x0,%al
call   1080 <err_exit@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1280 <active_processes+0x100>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     127b <active_processes+0xfb>
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     1276 <active_processes+0xf6>
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <resize_list@plt>
mov    %rax,-0x18(%rbp)
jmp    127b <active_processes+0xfb>
jmp    1209 <active_processes+0x89>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <closedir@plt>
jmp    12b5 <active_processes+0x135>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1080 <err_exit@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<active_processes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x19,0xc(%rsp)
lea    0xe65(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1259 <active_processes+0xd9>
mov    %rax,%r14
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11e1 <active_processes+0x61>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe33(%rip),%rdi
mov    %eax,%esi
mov    $0x81,%edx
xor    %eax,%eax
call   1080 <err_exit@plt>
movl   $0x0,(%r12)
mov    %r14,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     124b <active_processes+0xcb>
mov    $0x1,%ebx
lea    0xc(%rsp),%r15
jmp    121d <active_processes+0x9d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     124b <active_processes+0xcb>
mov    (%rax),%edi
call   1090 <atoi@plt>
test   %eax,%eax
je     1210 <active_processes+0x90>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %eax,(%r12,%rcx,4)
add    $0x1,%ebp
cmp    0xc(%rsp),%ebx
jl     1210 <active_processes+0x90>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <resize_list@plt>
mov    %rax,%r12
jmp    1210 <active_processes+0x90>
mov    %ebp,(%r12)
mov    %r14,%rdi
call   1040 <closedir@plt>
jmp    1277 <active_processes+0xf7>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <err_exit@plt>
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [33 x i8] c"Error on malloc: %s, line no %d\0A\00"
@errno = global i32 0
@.str.2 = [22 x i8] c"Error on opendir, %s\0A\00"
define i32* @active_processes() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 25, i32* %1
%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 %43, label %5
5: ; preds = %0
%6 = tail call i32* @malloc(i32 100)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 (i8*, i32, ...) @err_exit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 129)
br label %12
12: ; preds = %8, %5
store i32 0, i32* %6
%13 = call %struct.dirent* @readdir(i32* %3)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %39, label %15
15: ; preds = %12, %33
%16 = phi %struct.dirent* [ %37, %33 ], [ %13, %12 ]
%17 = phi i32* [ %36, %33 ], [ %6, %12 ]
%18 = phi i32 [ %35, %33 ], [ 1, %12 ]
%19 = phi i32 [ %34, %33 ], [ 0, %12 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @atoi(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %15
%25 = add nsw i32 %18, 1
%26 = sext i32 %18 to i64
%27 = getelementptr inbounds i32, i32* %17, i64 %26
store i32 %22, i32* %27
%28 = add nsw i32 %19, 1
%29 = load i32, i32* %1
%30 = icmp slt i32 %25, %29
br i1 %30, label %33, label %31
31: ; preds = %24
%32 = call i32* @resize_list(i32* %17, i32* %1)
br label %33
33: ; preds = %24, %31, %15
%34 = phi i32 [ %28, %31 ], [ %28, %24 ], [ %19, %15 ]
%35 = phi i32 [ %25, %31 ], [ %25, %24 ], [ %18, %15 ]
%36 = phi i32* [ %32, %31 ], [ %17, %24 ], [ %17, %15 ]
%37 = call %struct.dirent* @readdir(i32* %3)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %15
39: ; preds = %33, %12
%40 = phi i32 [ 0, %12 ], [ %34, %33 ]
%41 = phi i32* [ %6, %12 ], [ %36, %33 ]
store i32 %40, i32* %41
%42 = call i32 @closedir(i32* %3)
br label %47
43: ; preds = %0
%44 = load i32, i32* @errno
%45 = tail call i32 @strerror(i32 %44)
%46 = tail call i32 (i8*, i32, ...) @err_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %39
%48 = phi i32* [ %41, %39 ], [ undef, %43 ]
ret i32* %48
}