#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*/ cpuset;
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 get_num_ranks(void) {
  int i, num_ranks = 0;
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
  sched_getaffinity(0, sizeof(cpuset), &cpuset);
  for (i = 0; i < sizeof(cpuset) * 8; i++) {
    if (CPU_ISSET(i, &cpuset))
      ++num_ranks;
  }
  return num_ranks;
}


x86 assembly (clang, -O0):
<get_num_ranks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1050 <sched_getaffinity@plt>
movl   $0x0,-0x4(%rbp)
movslq -0x4(%rbp),%rax
cmp    $0x20,%rax
jae    119f <get_num_ranks+0x6f>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     118c <get_num_ranks+0x5c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1191 <get_num_ranks+0x61>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    115f <get_num_ranks+0x2f>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_ranks>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <CPU_ZERO@plt>
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1050 <sched_getaffinity@plt>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
add    $0x1,%ebx
cmp    $0x20,%ebx
jne    1160 <get_num_ranks+0x30>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @get_num_ranks() {
%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)
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %11, %5 ]
%7 = phi i32 [ 0, %0 ], [ %12, %5 ]
%8 = call i64 @CPU_ISSET(i32 %7, i32* %1)
%9 = icmp ne i64 %8, 0
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %6, %10
%12 = add nuw nsw i32 %7, 1
%13 = icmp eq i32 %12, 32
br i1 %13, label %14, label %5
14: ; preds = %5
ret i32 %11
}