#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 /*<<< orphan*/ assert(int);
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), 87);
    }
    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;
    assert(num_processes == (list_index - 1));
    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    0xe4c(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12bf <active_processes+0x12f>
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1209 <active_processes+0x79>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe09(%rip),%rdi
mov    $0x57,%edx
mov    $0x0,%al
call   1090 <err_exit@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1290 <active_processes+0x100>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     128b <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     1286 <active_processes+0xf6>
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <resize_list@plt>
mov    %rax,-0x18(%rbp)
jmp    128b <active_processes+0xfb>
jmp    1219 <active_processes+0x89>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <closedir@plt>
jmp    12dd <active_processes+0x14d>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1090 <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    0xe55(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     125b <active_processes+0xcb>
mov    %rax,%r14
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f1 <active_processes+0x61>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe23(%rip),%rdi
mov    %eax,%esi
mov    $0x57,%edx
xor    %eax,%eax
call   1090 <err_exit@plt>
movl   $0x0,(%r12)
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     127b <active_processes+0xeb>
xor    %ebx,%ebx
mov    $0x1,%ebp
lea    0xc(%rsp),%r15
jmp    122d <active_processes+0x9d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1282 <active_processes+0xf2>
mov    (%rax),%edi
call   10a0 <atoi@plt>
test   %eax,%eax
je     1220 <active_processes+0x90>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %eax,(%r12,%rcx,4)
add    $0x1,%ebx
cmp    0xc(%rsp),%ebp
jl     1220 <active_processes+0x90>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <resize_list@plt>
mov    %rax,%r12
jmp    1220 <active_processes+0x90>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <err_exit@plt>
jmp    129e <active_processes+0x10e>
mov    $0x1,%ebp
xor    %ebx,%ebx
mov    %ebx,(%r12)
add    $0xffffffff,%ebp
xor    %edi,%edi
cmp    %ebp,%ebx
sete   %dil
call   1060 <assert@plt>
mov    %r14,%rdi
call   1040 <closedir@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 %67
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 87)
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* %4
%60 = load i32, i32* %5
%61 = sub nsw i32 %60, 1
%62 = icmp eq i32 %59, %61
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = load i32*, i32** %1
%66 = call i32 @closedir(i32* %65)
br label %71
67: ; preds = %0
%68 = load i32, i32* @errno
%69 = call i32 @strerror(i32 %68)
%70 = call i32 (i8*, i32, ...) @err_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %67, %55
%72 = load i32*, i32** %3
ret i32* %72
}

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 %48, 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 87)
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 [ 1, %12 ], [ %35, %33 ]
%42 = phi i32* [ %6, %12 ], [ %36, %33 ]
store i32 %40, i32* %42
%43 = add nsw i32 %41, -1
%44 = icmp eq i32 %40, %43
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = call i32 @closedir(i32* %3)
br label %52
48: ; preds = %0
%49 = load i32, i32* @errno
%50 = tail call i32 @strerror(i32 %49)
%51 = tail call i32 (i8*, i32, ...) @err_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %48, %39
%53 = phi i32* [ %42, %39 ], [ undef, %48 ]
ret i32* %53
}