#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 int /*<<< orphan*/ cs;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
scalar_t__ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ sched_getaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int GetThreadCount() {
  cpu_set_t cs;
  int i;
  int count = 0;
  CPU_ZERO(&cs);
  sched_getaffinity(0, sizeof(cs), &cs);
  for (i = 0; i < 16; i++)
    if (CPU_ISSET(i, &cs))
      count++;
  return count;
}


x86 assembly (clang, -O0):
<GetThreadCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
lea    -0x4(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x4(%rbp),%rdx
call   1050 <sched_getaffinity@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x10,-0x8(%rbp)
jge    119b <GetThreadCount+0x6b>
mov    -0x8(%rbp),%edi
lea    -0x4(%rbp),%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1188 <GetThreadCount+0x58>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <GetThreadCount+0x5d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115f <GetThreadCount+0x2f>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetThreadCount>:
push   %rbp
push   %rbx
push   %rax
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <sched_getaffinity@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    $0x1,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x2,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x3,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x4,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x5,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x6,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x7,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x8,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0x9,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xa,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xb,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xc,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xd,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xe,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    $0xf,%edi
mov    %rbx,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @GetThreadCount() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %3
%4 = call i32 @CPU_ZERO(i32* %1)
%5 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
store i32 0, i32* %2
br label %6
6: ; preds = %17, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 16
br i1 %8, label %9, label %20
9: ; preds = %6
%10 = load i32, i32* %2
%11 = call i64 @CPU_ISSET(i32 %10, i32* %1)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
store i32 %15, i32* %3
br label %16
16: ; preds = %13, %9
br label %17
17: ; preds = %16
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
br label %6
20: ; preds = %6
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
define i32 @GetThreadCount() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @CPU_ZERO(i32* %1)
%4 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
%5 = call i64 @CPU_ISSET(i32 0, i32* %1)
%6 = icmp ne i64 %5, 0
%7 = zext i1 %6 to i32
%8 = call i64 @CPU_ISSET(i32 1, i32* %1)
%9 = icmp ne i64 %8, 0
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %7, %10
%12 = call i64 @CPU_ISSET(i32 2, i32* %1)
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %11, %14
%16 = call i64 @CPU_ISSET(i32 3, i32* %1)
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
%19 = add nuw nsw i32 %15, %18
%20 = call i64 @CPU_ISSET(i32 4, i32* %1)
%21 = icmp ne i64 %20, 0
%22 = zext i1 %21 to i32
%23 = add nuw nsw i32 %19, %22
%24 = call i64 @CPU_ISSET(i32 5, i32* %1)
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i32
%27 = add nuw nsw i32 %23, %26
%28 = call i64 @CPU_ISSET(i32 6, i32* %1)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
%31 = add nuw nsw i32 %27, %30
%32 = call i64 @CPU_ISSET(i32 7, i32* %1)
%33 = icmp ne i64 %32, 0
%34 = zext i1 %33 to i32
%35 = add nuw nsw i32 %31, %34
%36 = call i64 @CPU_ISSET(i32 8, i32* %1)
%37 = icmp ne i64 %36, 0
%38 = zext i1 %37 to i32
%39 = add nuw nsw i32 %35, %38
%40 = call i64 @CPU_ISSET(i32 9, i32* %1)
%41 = icmp ne i64 %40, 0
%42 = zext i1 %41 to i32
%43 = add nuw nsw i32 %39, %42
%44 = call i64 @CPU_ISSET(i32 10, i32* %1)
%45 = icmp ne i64 %44, 0
%46 = zext i1 %45 to i32
%47 = add nuw nsw i32 %43, %46
%48 = call i64 @CPU_ISSET(i32 11, i32* %1)
%49 = icmp ne i64 %48, 0
%50 = zext i1 %49 to i32
%51 = add nuw nsw i32 %47, %50
%52 = call i64 @CPU_ISSET(i32 12, i32* %1)
%53 = icmp ne i64 %52, 0
%54 = zext i1 %53 to i32
%55 = add nuw nsw i32 %51, %54
%56 = call i64 @CPU_ISSET(i32 13, i32* %1)
%57 = icmp ne i64 %56, 0
%58 = zext i1 %57 to i32
%59 = add nuw nsw i32 %55, %58
%60 = call i64 @CPU_ISSET(i32 14, i32* %1)
%61 = icmp ne i64 %60, 0
%62 = zext i1 %61 to i32
%63 = add nuw nsw i32 %59, %62
%64 = call i64 @CPU_ISSET(i32 15, i32* %1)
%65 = icmp ne i64 %64, 0
%66 = zext i1 %65 to i32
%67 = add nuw nsw i32 %63, %66
ret i32 %67
}