#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 */
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  scalar_t__ *__bits;
};
typedef TYPE_1__ cpu_set_t;
typedef scalar_t__ __cpu_mask;

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ SYS_sched_getaffinity;
TYPE_1__ *cpuset_alloc(int, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ cpuset_free(TYPE_1__ *);
scalar_t__ errno;
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, size_t, TYPE_1__ *);
int get_max_number_of_cpus(void) {
  int n, cpus = 2048;
  size_t setsize;
  cpu_set_t *set = cpuset_alloc(cpus, &setsize, NULL);
  if (!set)
    return -1;
  for (;;) {
    do {
      size_t __i;
      size_t __imax = (setsize) / sizeof(__cpu_mask);
      __cpu_mask *__bits = (set)->__bits;
      for (__i = 0; __i < __imax; ++__i)
        __bits[__i] = 0;
    } while (0);
    n = syscall(SYS_sched_getaffinity, 0, setsize, set);
    if (n < 0 && errno == EINVAL && cpus < 1024 * 1024) {
      cpuset_free(set);
      cpus *= 2;
      set = cpuset_alloc(cpus, &setsize, NULL);
      if (!set)
        return -1;
      continue;
    }
    cpuset_free(set);
    return n * 8;
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_max_number_of_cpus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x800,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <cpuset_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116a <get_max_number_of_cpus+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <get_max_number_of_cpus+0x137>
jmp    116f <get_max_number_of_cpus+0x3f>
jmp    1174 <get_max_number_of_cpus+0x44>
mov    -0x18(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11c2 <get_max_number_of_cpus+0x92>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1193 <get_max_number_of_cpus+0x63>
jmp    11c7 <get_max_number_of_cpus+0x97>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %esi,%esi
call   1030 <syscall@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1255 <get_max_number_of_cpus+0x125>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <get_max_number_of_cpus+0x125>
cmpl   $0x100000,-0xc(%rbp)
jge    1255 <get_max_number_of_cpus+0x125>
mov    -0x20(%rbp),%rdi
call   1050 <cpuset_free@plt>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <cpuset_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1250 <get_max_number_of_cpus+0x120>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <get_max_number_of_cpus+0x137>
jmp    116f <get_max_number_of_cpus+0x3f>
mov    -0x20(%rbp),%rdi
call   1050 <cpuset_free@plt>
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_max_number_of_cpus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x800,%r12d
mov    %rsp,%rsi
mov    $0x800,%edi
xor    %edx,%edx
call   1050 <cpuset_alloc@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     120c <get_max_number_of_cpus+0xcc>
mov    0x2e58(%rip),%r13
mov    0x2e59(%rip),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%rdx
cmp    $0x8,%rdx
jb     11ac <get_max_number_of_cpus+0x6c>
mov    (%rbx),%rdi
and    $0xfffffffffffffff8,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    (%rsp),%rdx
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   1040 <syscall@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ff <get_max_number_of_cpus+0xbf>
cmp    $0xfffff,%r12d
ja     11ff <get_max_number_of_cpus+0xbf>
mov    (%r14),%rax
mov    0x2e0d(%rip),%rcx
cmp    %rax,(%rcx)
jne    11ff <get_max_number_of_cpus+0xbf>
mov    %rbx,%rdi
call   1060 <cpuset_free@plt>
add    %r12d,%r12d
mov    %r12d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1050 <cpuset_alloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <get_max_number_of_cpus+0x50>
mov    $0xffffffff,%eax
jmp    120c <get_max_number_of_cpus+0xcc>
mov    %rbx,%rdi
call   1060 <cpuset_free@plt>
shl    $0x3,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64* }
@SYS_sched_getaffinity = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
define i32 @get_max_number_of_cpus() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.TYPE_5__*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64*
store i32 2048, i32* %3
%9 = load i32, i32* %3
%10 = call %struct.TYPE_5__* @cpuset_alloc(i32 %9, i64* %4, i32* null)
store %struct.TYPE_5__* %10, %struct.TYPE_5__** %5
%11 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%12 = icmp ne %struct.TYPE_5__* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i32 -1, i32* %1
br label %64
14: ; preds = %0
br label %15
15: ; preds = %58, %14
br label %16
16: ; preds = %15
%17 = load i64, i64* %4
%18 = udiv i64 %17, 8
store i64 %18, i64* %7
%19 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%20 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %19, i32 0, i32 0
%21 = load i64*, i64** %20
store i64* %21, i64** %8
store i64 0, i64* %6
br label %22
22: ; preds = %30, %16
%23 = load i64, i64* %6
%24 = load i64, i64* %7
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i64*, i64** %8
%28 = load i64, i64* %6
%29 = getelementptr inbounds i64, i64* %27, i64 %28
store i64 0, i64* %29
br label %30
30: ; preds = %26
%31 = load i64, i64* %6
%32 = add i64 %31, 1
store i64 %32, i64* %6
br label %22
33: ; preds = %22
br label %34
34: ; preds = %33
%35 = load i32, i32* @SYS_sched_getaffinity
%36 = load i64, i64* %4
%37 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%38 = call i32 @syscall(i32 %35, i32 0, i64 %36, %struct.TYPE_5__* %37)
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %59
41: ; preds = %34
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINVAL
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %59
45: ; preds = %41
%46 = load i32, i32* %3
%47 = icmp slt i32 %46, 1048576
br i1 %47, label %48, label %59
48: ; preds = %45
%49 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%50 = call i32 @cpuset_free(%struct.TYPE_5__* %49)
%51 = load i32, i32* %3
%52 = mul nsw i32 %51, 2
store i32 %52, i32* %3
%53 = load i32, i32* %3
%54 = call %struct.TYPE_5__* @cpuset_alloc(i32 %53, i64* %4, i32* null)
store %struct.TYPE_5__* %54, %struct.TYPE_5__** %5
%55 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%56 = icmp ne %struct.TYPE_5__* %55, null
br i1 %56, label %58, label %57
57: ; preds = %48
store i32 -1, i32* %1
br label %64
58: ; preds = %48
br label %15
59: ; preds = %45, %41, %34
%60 = load %struct.TYPE_5__*, %struct.TYPE_5__** %5
%61 = call i32 @cpuset_free(%struct.TYPE_5__* %60)
%62 = load i32, i32* %2
%63 = mul nsw i32 %62, 8
store i32 %63, i32* %1
br label %64
64: ; preds = %59, %57, %13
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64* }
@SYS_sched_getaffinity = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
define i32 @get_max_number_of_cpus() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = call %struct.TYPE_5__* @cpuset_alloc(i32 2048, i64* %1, i32* null)
%4 = icmp eq %struct.TYPE_5__* %3, null
br i1 %4, label %34, label %5
5: ; preds = %0, %26
%6 = phi %struct.TYPE_5__* [ %29, %26 ], [ %3, %0 ]
%7 = phi i32 [ %28, %26 ], [ 2048, %0 ]
%8 = load i64, i64* %1
%9 = icmp ult i64 %8, 8
br i1 %9, label %15, label %10
10: ; preds = %5
%11 = bitcast %struct.TYPE_5__* %6 to i8**
%12 = load i8*, i8** %11
%13 = and i64 %8, -8
%14 = load i64, i64* %1
br label %15
15: ; preds = %10, %5
%16 = phi i64 [ %14, %10 ], [ %8, %5 ]
%17 = load i32, i32* @SYS_sched_getaffinity
%18 = call i32 @syscall(i32 %17, i32 0, i64 %16, %struct.TYPE_5__* %6)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINVAL
%23 = icmp eq i64 %21, %22
%24 = icmp ult i32 %7, 1048576
%25 = and i1 %24, %23
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = call i32 @cpuset_free(%struct.TYPE_5__* %6)
%28 = shl nuw nsw i32 %7, 1
%29 = call %struct.TYPE_5__* @cpuset_alloc(i32 %28, i64* %1, i32* null)
%30 = icmp eq %struct.TYPE_5__* %29, null
br i1 %30, label %34, label %5
31: ; preds = %20, %15
%32 = call i32 @cpuset_free(%struct.TYPE_5__* %6)
%33 = shl nsw i32 %18, 3
br label %34
34: ; preds = %26, %0, %31
%35 = phi i32 [ %33, %31 ], [ -1, %0 ], [ -1, %26 ]
ret i32 %35
}