#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 thread_info {
  int dummy;
};
struct dirent {
  int /*<<< orphan*/ *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ add_thread(int, int, struct thread_info *);
int atoi(int /*<<< orphan*/ *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void add_threads(int pid, struct thread_info *proc_info) {
  char path[1024];
  DIR *d;
  struct dirent *de;
  sprintf(path, "/proc/%d/task", pid);
  d = opendir(path);
  if (d == 0)
    return;
  while ((de = readdir(d)) != 0) {
    if (isdigit(de->d_name[0])) {
      int tid = atoi(de->d_name);
      add_thread(pid, tid, proc_info);
    }
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<add_threads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    11be <add_threads+0x4e>
jmp    1233 <add_threads+0xc3>
jmp    11c3 <add_threads+0x53>
mov    -0x418(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
je     1227 <add_threads+0xb7>
mov    -0x420(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1222 <add_threads+0xb2>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x424(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x424(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <add_thread@plt>
jmp    11c3 <add_threads+0x53>
mov    -0x418(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_threads>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe76(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     120d <add_threads+0x9d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11f8 <add_threads+0x88>
mov    %rax,%rbp
jmp    11d0 <add_threads+0x60>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11f8 <add_threads+0x88>
mov    0x0(%rbp),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11c0 <add_threads+0x50>
mov    0x0(%rbp),%rdi
call   1080 <atoi@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <add_thread@plt>
jmp    11c0 <add_threads+0x50>
mov    %rbx,%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.thread_info = type { i32 }
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/%d/task\00"
define void @add_threads(i32 %0, %struct.thread_info* %1) {
%3 = alloca i32
%4 = alloca %struct.thread_info*
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i32 %0, i32* %3
store %struct.thread_info* %1, %struct.thread_info** %4
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
br label %43
17: ; preds = %2
br label %18
18: ; preds = %39, %17
%19 = load i32*, i32** %6
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %7
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %7
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 0
%27 = load i32, i32* %26
%28 = call i64 @isdigit(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %22
%31 = load %struct.dirent*, %struct.dirent** %7
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i32*, i32** %32
%34 = call i32 @atoi(i32* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %3
%36 = load i32, i32* %8
%37 = load %struct.thread_info*, %struct.thread_info** %4
%38 = call i32 @add_thread(i32 %35, i32 %36, %struct.thread_info* %37)
br label %39
39: ; preds = %30, %22
br label %18
40: ; preds = %18
%41 = load i32*, i32** %6
%42 = call i32 @closedir(i32* %41)
br label %43
43: ; preds = %40, %16
ret void
}

LLVM IR (-O2):
%struct.thread_info = type { i32 }
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/%d/task\00"
define void @add_threads(i32 %0, %struct.thread_info* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %27, label %8
8: ; preds = %2
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8, %22
%12 = phi %struct.dirent* [ %23, %22 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i32*, i32** %13
%15 = load i32, i32* %14
%16 = tail call i64 @isdigit(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = load i32*, i32** %13
%20 = tail call i32 @atoi(i32* %19)
%21 = tail call i32 @add_thread(i32 %0, i32 %20, %struct.thread_info* %1)
br label %22
22: ; preds = %18, %11
%23 = tail call %struct.dirent* @readdir(i32* %6)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %11
25: ; preds = %22, %8
%26 = tail call i32 @closedir(i32* %6)
br label %27
27: ; preds = %2, %25
ret void
}