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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int count_tasks(pid_t pid) {
  char path[100];
  int count;
  DIR *d;
  snprintf(path, sizeof(path), "/proc/%d/task/", (int)pid);
  d = opendir(path);
  if (d == NULL)
    return -1;
  for (count = 0; readdir(d) != NULL; count++)
    ;
  closedir(d);
  assert(count > 2);
  return count - 2;
}


x86 assembly (clang, -O0):
<count_tasks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x64,%esi
lea    0xe8b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x80(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jne    11a4 <count_tasks+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <count_tasks+0xb9>
movl   $0x0,-0x84(%rbp)
mov    -0x90(%rbp),%rdi
call   1070 <readdir@plt>
cmp    $0x0,%rax
je     11dd <count_tasks+0x8d>
jmp    11c9 <count_tasks+0x79>
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11ae <count_tasks+0x5e>
mov    -0x90(%rbp),%rdi
call   1050 <closedir@plt>
cmpl   $0x2,-0x84(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x84(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_tasks>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%rcx
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11bb <count_tasks+0x6b>
mov    %rax,%r14
mov    $0xfffffffd,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <readdir@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1190 <count_tasks+0x40>
mov    %r14,%rdi
call   1050 <closedir@plt>
lea    0x2(%rbx),%eax
xor    %edi,%edi
cmp    $0x3,%eax
setae  %dil
call   1060 <assert@plt>
jmp    11c0 <count_tasks+0x70>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%d/task/\00"
define i32 @count_tasks(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32*
store i64 %0, i64* %3
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = load i64, i64* %3
%9 = trunc i64 %8 to i32
%10 = call i32 @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %34
16: ; preds = %1
store i32 0, i32* %5
br label %17
17: ; preds = %22, %16
%18 = load i32*, i32** %6
%19 = call i32* @readdir(i32* %18)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %17
br label %22
22: ; preds = %21
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %17
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = call i32 @closedir(i32* %26)
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 2
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 2
store i32 %33, i32* %2
br label %34
34: ; preds = %25, %15
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/task/\00"
define i32 @count_tasks(i64 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = trunc i64 %0 to i32
%5 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32* @opendir(i8* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %19, label %8
8: ; preds = %1, %8
%9 = phi i32 [ %12, %8 ], [ 0, %1 ]
%10 = call i32* @readdir(i32* %6)
%11 = icmp eq i32* %10, null
%12 = add nuw nsw i32 %9, 1
br i1 %11, label %13, label %8
13: ; preds = %8
%14 = call i32 @closedir(i32* %6)
%15 = icmp ugt i32 %9, 2
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = add nsw i32 %9, -2
br label %19
19: ; preds = %1, %13
%20 = phi i32 [ %18, %13 ], [ -1, %1 ]
ret i32 %20
}