#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ *realloc(scalar_t__ *, int);
int /*<<< orphan*/ stderr;
long strtol(char *, int /*<<< orphan*/ *, int);
pid_t *get_tasks(void) {
  int count = 0;
  int size = 3;
  pid_t *buffer = malloc(sizeof(pid_t) * size);
  DIR *dp = NULL;
  struct dirent *ep = NULL;
  buffer[0] = (pid_t)-1;
  dp = opendir("/proc/self/task");
  if (dp == NULL) {
    fprintf(stderr, "Failed to fetch tasks?\n");
    return buffer;
  }
  while ((ep = readdir(dp)) != NULL) {
    if (ep->d_name[0] == '.' || ep->d_name[0] == '\0') {
      continue;
    }
    long task_id = strtol(ep->d_name, NULL, 10);
    if (count + 1 >= size) {
      size = size * 2;
      buffer = realloc(buffer, sizeof(long) * size);
    }
    buffer[count] = (pid_t)task_id;
    count += 1;
    buffer[count] = (pid_t)-1;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<get_tasks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0x3,-0x10(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
lea    0xe55(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e1 <get_tasks+0x81>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <get_tasks+0x146>
jmp    11e6 <get_tasks+0x86>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     129e <get_tasks+0x13e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1223 <get_tasks+0xc3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1228 <get_tasks+0xc8>
jmp    11e6 <get_tasks+0x86>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cmp    -0x10(%rbp),%eax
jl     1270 <get_tasks+0x110>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movq   $0xffffffffffffffff,(%rax,%rcx,8)
ff
jmp    11e6 <get_tasks+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tasks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x18,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
movq   $0xffffffffffffffff,(%rax)
lea    0xe7a(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1232 <get_tasks+0xd2>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1247 <get_tasks+0xe7>
mov    $0x3,%r15d
xor    %ebp,%ebp
mov    %rbp,%r13
jmp    11cd <get_tasks+0x6d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1247 <get_tasks+0xe7>
mov    (%rax),%rdi
movzbl (%rdi),%eax
cmp    $0x2e,%al
je     11c0 <get_tasks+0x60>
test   %al,%al
je     11c0 <get_tasks+0x60>
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%r12
lea    0x1(%r13),%rbp
cmp    %ebp,%r15d
jg     1214 <get_tasks+0xb4>
lea    (%r15,%r15,1),%eax
mov    %eax,0x4(%rsp)
shl    $0x4,%r15d
mov    %r14,%rdi
mov    %r15d,%esi
call   1080 <realloc@plt>
mov    %rax,%r14
mov    0x4(%rsp),%eax
mov    %eax,%r15d
mov    %r12,(%r14,%r13,8)
movq   $0xffffffffffffffff,(%r14,%rbp,8)
ff
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1247 <get_tasks+0xe7>
mov    %rbp,%r13
jmp    11cd <get_tasks+0x6d>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [16 x i8] c"/proc/self/task\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to fetch tasks?\0A\00"
define i64* @get_tasks() {
%1 = tail call i64* @malloc(i32 24)
store i64 -1, i64* %1
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %39, label %34
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %39
10: ; preds = %34, %15
%11 = phi %struct.dirent* [ %36, %34 ], [ %16, %15 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
switch i8 %14, label %18 [
i8 46, label %15
i8 0, label %15
]
15: ; preds = %10, %10
%16 = tail call %struct.dirent* @readdir(i32* %2)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %39, label %10
18: ; preds = %10
%19 = tail call i64 @strtol(i8* nocapture %13, i32* null, i32 10)
%20 = add nuw i64 %35, 1
%21 = trunc i64 %20 to i32
%22 = icmp sgt i32 %38, %21
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = shl nsw i32 %38, 1
%25 = shl i32 %38, 4
%26 = tail call i64* @realloc(i64* %37, i32 %25)
br label %27
27: ; preds = %23, %18
%28 = phi i32 [ %24, %23 ], [ %38, %18 ]
%29 = phi i64* [ %26, %23 ], [ %37, %18 ]
%30 = getelementptr inbounds i64, i64* %29, i64 %35
store i64 %19, i64* %30
%31 = getelementptr inbounds i64, i64* %29, i64 %20
store i64 -1, i64* %31
%32 = tail call %struct.dirent* @readdir(i32* %2)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %39, label %34
34: ; preds = %4, %27
%35 = phi i64 [ %20, %27 ], [ 0, %4 ]
%36 = phi %struct.dirent* [ %32, %27 ], [ %5, %4 ]
%37 = phi i64* [ %29, %27 ], [ %1, %4 ]
%38 = phi i32 [ %28, %27 ], [ 3, %4 ]
br label %10
39: ; preds = %27, %15, %4, %7
%40 = phi i64* [ %1, %7 ], [ %1, %4 ], [ %37, %15 ], [ %29, %27 ]
ret i64* %40
}