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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int *realloc(int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int strtoul(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int get_thr_list(pid_t pid, pid_t **thr_listp, int *thr_cntp) {
  char dirname[PATH_MAX];
  DIR *dir;
  struct dirent *ent;
  pid_t *thr_list = NULL;
  int i = 0, size = 0, r = -1;
  snprintf(dirname, sizeof(dirname), "/proc/%d/task", pid);
  if ((dir = opendir(dirname)) == NULL) {
    perror("get_thr_list: opendir");
    goto ret;
  }
  while ((ent = readdir(dir))) {
    if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) {
      if (i >= size) {
        size += 64;
        if ((thr_list = realloc(thr_list, size * sizeof(pid))) == NULL) {
          perror("get_thr_list: realloc");
          free(thr_list);
          goto close;
        }
      }
      thr_list[i] = strtoul(ent->d_name, NULL, 10);
      i += 1;
    }
  }
  *thr_listp = thr_list;
  *thr_cntp = i;
  r = 0;
close:
  closedir(dir);
ret:
  return r;
}


x86 assembly (clang, -O0):
<get_thr_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe08(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x58(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1225 <get_thr_list+0x95>
lea    0xdf3(%rip),%rdi
call   10a0 <perror@plt>
jmp    1321 <get_thr_list+0x191>
jmp    122a <get_thr_list+0x9a>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12fd <get_thr_list+0x16d>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12f8 <get_thr_list+0x168>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12f8 <get_thr_list+0x168>
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jl     12ce <get_thr_list+0x13e>
mov    -0x48(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x48(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12c9 <get_thr_list+0x139>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    1318 <get_thr_list+0x188>
jmp    12ce <get_thr_list+0x13e>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10b0 <strtoul@plt>
mov    %eax,%edx
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    122a <get_thr_list+0x9a>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4c(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x4c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_thr_list>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ecx
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe33(%rip),%rdx
mov    %rbx,%rdi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12b9 <get_thr_list+0x129>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
mov    %r14,-0x38(%rbp)
mov    %r15,-0x30(%rbp)
je     1288 <get_thr_list+0xf8>
mov    %rax,%r12
xor    %r14d,%r14d
xor    %r13d,%r13d
xor    %r15d,%r15d
jmp    123b <get_thr_list+0xab>
mov    (%r12),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   10b0 <strtoul@plt>
movslq %r13d,%rcx
mov    %eax,(%r15,%rcx,4)
add    $0x1,%r13d
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     128e <get_thr_list+0xfe>
mov    (%r12),%edi
lea    0xddf(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122b <get_thr_list+0x9b>
mov    (%r12),%edi
lea    0xdc9(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122b <get_thr_list+0x9b>
cmp    %r14d,%r13d
jl     1210 <get_thr_list+0x80>
lea    0x100(,%r14,4),%esi
mov    %r15,%rdi
call   1090 <realloc@plt>
test   %rax,%rax
je     12cd <get_thr_list+0x13d>
mov    %rax,%r15
add    $0x40,%r14d
jmp    1210 <get_thr_list+0x80>
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    -0x30(%rbp),%rax
mov    %r15,(%rax)
mov    -0x38(%rbp),%rax
mov    %r13d,(%rax)
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12a7 <get_thr_list+0x117>
lea    0xd53(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   1030 <free@plt>
mov    $0xffffffff,%r14d
jmp    129f <get_thr_list+0x10f>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/task\00"
@.str.1 = [22 x i8] c"get_thr_list: opendir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [22 x i8] c"get_thr_list: realloc\00"
define i32 @get_thr_list(i32 %0, i32** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = call i32* @opendir(i8* %6)
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %52, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %58
15: ; preds = %10, %46
%16 = phi %struct.dirent* [ %50, %46 ], [ %11, %10 ]
%17 = phi i32 [ %49, %46 ], [ 0, %10 ]
%18 = phi i32 [ %48, %46 ], [ 0, %10 ]
%19 = phi i32* [ %47, %46 ], [ null, %10 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @strcmp(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %46, label %24
24: ; preds = %15
%25 = load i32, i32* %20
%26 = call i64 @strcmp(i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %46, label %28
28: ; preds = %24
%29 = icmp slt i32 %18, %17
br i1 %29, label %38, label %30
30: ; preds = %28
%31 = add nsw i32 %17, 64
%32 = shl i32 %31, 2
%33 = call i32* @realloc(i32* %19, i32 %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @free(i32* null)
br label %55
38: ; preds = %30, %28
%39 = phi i32* [ %33, %30 ], [ %19, %28 ]
%40 = phi i32 [ %31, %30 ], [ %17, %28 ]
%41 = load i32, i32* %20
%42 = call i32 @strtoul(i32 %41, i32* null, i32 10)
%43 = sext i32 %18 to i64
%44 = getelementptr inbounds i32, i32* %39, i64 %43
store i32 %42, i32* %44
%45 = add nsw i32 %18, 1
br label %46
46: ; preds = %38, %24, %15
%47 = phi i32* [ %39, %38 ], [ %19, %24 ], [ %19, %15 ]
%48 = phi i32 [ %45, %38 ], [ %18, %24 ], [ %18, %15 ]
%49 = phi i32 [ %40, %38 ], [ %17, %24 ], [ %17, %15 ]
%50 = call %struct.dirent* @readdir(i32* %8)
%51 = icmp eq %struct.dirent* %50, null
br i1 %51, label %52, label %15
52: ; preds = %46, %10
%53 = phi i32* [ null, %10 ], [ %47, %46 ]
%54 = phi i32 [ 0, %10 ], [ %48, %46 ]
store i32* %53, i32** %1
store i32 %54, i32* %2
br label %55
55: ; preds = %52, %35
%56 = phi i32 [ -1, %35 ], [ 0, %52 ]
%57 = call i32 @closedir(i32* %8)
br label %58
58: ; preds = %55, %13
%59 = phi i32 [ -1, %13 ], [ %56, %55 ]
ret i32 %59
}