#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 cpus;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ SYS_sched_getaffinity;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(unsigned long *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, int, unsigned long **);
int max_number_of_cpus(void) {
  int n;
  int cpus = 2048;
  for (;;) {
    unsigned long buffer[(((cpus) + (((8 * sizeof(unsigned long))) - 1)) /
                          ((8 * sizeof(unsigned long))))];
    memset(buffer, 0, sizeof(buffer));
    n = syscall(SYS_sched_getaffinity, 0, ((cpus + 7) / 8), &buffer);
    if (n < 0 && errno == EINVAL && cpus < 1024 * 1024) {
      cpus *= 2;
      continue;
    }
    return n * 8;
  }
  fprintf(stderr, "cannot determine NR_CPUS; aborting");
  exit(1);
  return 0;
}


x86 assembly (clang, -O0):
<max_number_of_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x800,-0xc(%rbp)
movslq -0xc(%rbp),%rax
add    $0x3f,%rax
shr    $0x6,%rax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,8),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x30(%rbp),%rcx
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%esi
cltd
idiv   %esi
mov    %eax,%edx
xor    %esi,%esi
call   1040 <syscall@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d9 <max_number_of_cpus+0xb9>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <max_number_of_cpus+0xb9>
cmpl   $0x100000,-0xc(%rbp)
jge    11d9 <max_number_of_cpus+0xb9>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x3,-0x24(%rbp)
jmp    11e9 <max_number_of_cpus+0xc9>
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     1206 <max_number_of_cpus+0xe6>
jmp    1201 <max_number_of_cpus+0xe1>
jmp    112f <max_number_of_cpus+0xf>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_number_of_cpus>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x800,%r14d
mov    0x2e95(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rsp,%r12
movslq %r14d,%r13
mov    %rsp,%rbx
mov    %r13,%rax
shr    $0x3,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r13d,%edx
sar    $0x3,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    (%r15),%edi
or     $0x7,%r13d
lea    0x7(%r13),%edx
test   %r13d,%r13d
cmovns %r13d,%edx
sar    $0x3,%edx
xor    %esi,%esi
mov    %rbx,%rcx
call   1040 <syscall@plt>
test   %eax,%eax
jns    11b8 <max_number_of_cpus+0x98>
mov    0x2e48(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e36(%rip),%rdx
cmp    (%rdx),%rcx
jne    11b8 <max_number_of_cpus+0x98>
cmp    $0xfffff,%r14d
jg     11b8 <max_number_of_cpus+0x98>
mov    %r12,%rsp
add    %r14d,%r14d
jmp    1140 <max_number_of_cpus+0x20>
shl    $0x3,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SYS_sched_getaffinity = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
define i32 @max_number_of_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 2048, i32* %3
br label %7
7: ; preds = %38, %0
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = add i64 %9, 63
%11 = udiv i64 %10, 64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i64, i64 %11
store i64 %11, i64* %5
%14 = mul nuw i64 8, %11
%15 = trunc i64 %14 to i32
%16 = call i32 @memset(i64* %13, i32 0, i32 %15)
%17 = load i32, i32* @SYS_sched_getaffinity
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 7
%20 = sdiv i32 %19, 8
%21 = bitcast i64* %13 to i64**
%22 = call i32 @syscall(i32 %17, i32 0, i32 %20, i64** %21)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %7
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINVAL
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 1048576
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %3
%34 = mul nsw i32 %33, 2
store i32 %34, i32* %3
store i32 3, i32* %6
br label %38
35: ; preds = %29, %25, %7
%36 = load i32, i32* %2
%37 = mul nsw i32 %36, 8
store i32 %37, i32* %1
store i32 1, i32* %6
br label %38
38: ; preds = %35, %32
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
switch i32 %40, label %43 [
i32 3, label %7
i32 1, label %41
]
41: ; preds = %38
%42 = load i32, i32* %1
ret i32 %42
43: ; preds = %38
unreachable
}

LLVM IR (-O2):
@SYS_sched_getaffinity = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
define i32 @max_number_of_cpus() {
br label %1
1: ; preds = %21, %0
%2 = phi i32 [ 2048, %0 ], [ %22, %21 ]
%3 = sext i32 %2 to i64
%4 = lshr exact i64 %3, 6
%5 = call i8* @llvm.stacksave()
%6 = alloca i64, i64 %4
%7 = ashr exact i32 %2, 3
%8 = call i32 @memset(i64* %6, i32 0, i32 %7)
%9 = load i32, i32* @SYS_sched_getaffinity
%10 = or i32 %2, 7
%11 = sdiv i32 %10, 8
%12 = bitcast i64* %6 to i64**
%13 = call i32 @syscall(i32 %9, i32 0, i32 %11, i64** %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINVAL
%18 = icmp eq i64 %16, %17
%19 = icmp slt i32 %2, 1048576
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = shl nsw i32 %2, 1
br label %1
23: ; preds = %1, %15
%24 = shl nsw i32 %13, 3
ret i32 %24
}