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

/* Variables and functions */
int /*<<< orphan*/ *CPU_ALLOC(long);
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
scalar_t__ CPU_ISSET(long, int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_CONF;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getpid();
scalar_t__ sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
long sysconf(int /*<<< orphan*/);
unsigned int get_current_cpu_count(void) {
  unsigned int curr_cpu_count = 0;
  long i;
  cpu_set_t *cur_mask;
  long num_configured_cpus = sysconf(_SC_NPROCESSORS_CONF);
  if (num_configured_cpus < 0) {
    fprintf(stderr, "get_current_cpu_count: Failed to get the number of "
                    "configured CPUs\n");
    return 0;
  }
  cur_mask = CPU_ALLOC(num_configured_cpus);
  if (cur_mask == NULL) {
    fprintf(stderr, "get_current_cpu_count: Failed to alloc cpu_set_t\n");
    return 0;
  }
  if (sched_getaffinity(getpid(), sizeof(cur_mask), cur_mask)) {
    fprintf(stderr, "get_current_cpu_count: Failed to get CPU affinity\n");
  } else {
    for (i = 0; i < num_configured_cpus; i++) {
      if (CPU_ISSET(i, cur_mask)) {
        curr_cpu_count++;
      }
    }
  }
  CPU_FREE(cur_mask);
  return curr_cpu_count;
}


x86 assembly (clang, -O0):
<get_current_cpu_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11bd <get_current_cpu_count+0x4d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1291 <get_current_cpu_count+0x121>
mov    -0x20(%rbp),%rdi
call   1030 <CPU_ALLOC@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f6 <get_current_cpu_count+0x86>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1291 <get_current_cpu_count+0x121>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rdx
mov    $0x8,%esi
call   1090 <sched_getaffinity@plt>
cmp    $0x0,%rax
je     1231 <get_current_cpu_count+0xc1>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1282 <get_current_cpu_count+0x112>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    127d <get_current_cpu_count+0x10d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1267 <get_current_cpu_count+0xf7>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    126c <get_current_cpu_count+0xfc>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1239 <get_current_cpu_count+0xc9>
jmp    1282 <get_current_cpu_count+0x112>
mov    -0x18(%rbp),%rdi
call   1070 <CPU_FREE@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_cpu_count>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
test   %rax,%rax
js     11d3 <get_current_cpu_count+0x63>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <CPU_ALLOC@plt>
test   %rax,%rax
je     11e5 <get_current_cpu_count+0x75>
mov    %rax,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
mov    $0x8,%esi
mov    %r14,%rdx
call   1090 <sched_getaffinity@plt>
test   %rax,%rax
je     11ff <get_current_cpu_count+0x8f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
call   1060 <fprintf@plt>
jmp    122c <get_current_cpu_count+0xbc>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
jmp    11f5 <get_current_cpu_count+0x85>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1060 <fprintf@plt>
xor    %r12d,%r12d
jmp    1234 <get_current_cpu_count+0xc4>
test   %r15,%r15
je     122c <get_current_cpu_count+0xbc>
xor    %r12d,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%r12d
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1210 <get_current_cpu_count+0xa0>
mov    %r14,%rdi
call   1070 <CPU_FREE@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@stderr = global i32 0
@.str = [68 x i8] c"get_current_cpu_count: Failed to get the number of configured CPUs\0A\00"
@.str.1 = [50 x i8] c"get_current_cpu_count: Failed to alloc cpu_set_t\0A\00"
@.str.2 = [51 x i8] c"get_current_cpu_count: Failed to get CPU affinity\0A\00"
define i32 @get_current_cpu_count() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
store i32 0, i32* %2
%6 = load i32, i32* @_SC_NPROCESSORS_CONF
%7 = call i64 @sysconf(i32 %6)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %51
13: ; preds = %0
%14 = load i64, i64* %5
%15 = call i32* @CPU_ALLOC(i64 %14)
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %51
21: ; preds = %13
%22 = call i32 (...) @getpid()
%23 = load i32*, i32** %4
%24 = call i64 @sched_getaffinity(i32 %22, i32 8, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %47
29: ; preds = %21
store i64 0, i64* %3
br label %30
30: ; preds = %43, %29
%31 = load i64, i64* %3
%32 = load i64, i64* %5
%33 = icmp slt i64 %31, %32
br i1 %33, label %34, label %46
34: ; preds = %30
%35 = load i64, i64* %3
%36 = load i32*, i32** %4
%37 = call i64 @CPU_ISSET(i64 %35, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32, i32* %2
%41 = add i32 %40, 1
store i32 %41, i32* %2
br label %42
42: ; preds = %39, %34
br label %43
43: ; preds = %42
%44 = load i64, i64* %3
%45 = add nsw i64 %44, 1
store i64 %45, i64* %3
br label %30
46: ; preds = %30
br label %47
47: ; preds = %46, %26
%48 = load i32*, i32** %4
%49 = call i32 @CPU_FREE(i32* %48)
%50 = load i32, i32* %2
store i32 %50, i32* %1
br label %51
51: ; preds = %47, %18, %10
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@stderr = global i32 0
@.str = [68 x i8] c"get_current_cpu_count: Failed to get the number of configured CPUs\0A\00"
@.str.1 = [50 x i8] c"get_current_cpu_count: Failed to alloc cpu_set_t\0A\00"
@.str.2 = [51 x i8] c"get_current_cpu_count: Failed to get CPU affinity\0A\00"
define i32 @get_current_cpu_count() {
%1 = load i32, i32* @_SC_NPROCESSORS_CONF
%2 = tail call i64 @sysconf(i32 %1)
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
br label %34
7: ; preds = %0
%8 = tail call i32* @CPU_ALLOC(i64 %2)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %34
13: ; preds = %7
%14 = tail call i32 (...) @getpid()
%15 = tail call i64 @sched_getaffinity(i32 %14, i32 8, i32* %8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = icmp eq i64 %2, 0
br i1 %18, label %31, label %22
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %31
22: ; preds = %17, %22
%23 = phi i32 [ %28, %22 ], [ 0, %17 ]
%24 = phi i64 [ %29, %22 ], [ 0, %17 ]
%25 = tail call i64 @CPU_ISSET(i64 %24, i32* %8)
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
%28 = add i32 %23, %27
%29 = add nuw nsw i64 %24, 1
%30 = icmp eq i64 %29, %2
br i1 %30, label %31, label %22
31: ; preds = %22, %17, %19
%32 = phi i32 [ 0, %19 ], [ 0, %17 ], [ %28, %22 ]
%33 = tail call i32 @CPU_FREE(i32* %8)
br label %34
34: ; preds = %31, %10, %4
%35 = phi i32 [ 0, %4 ], [ 0, %10 ], [ %32, %31 ]
ret i32 %35
}