#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*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ *CPU_ALLOC(int);
size_t CPU_ALLOC_SIZE(int);
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
scalar_t__ CPU_ISSET_S(int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO_S(size_t, int /*<<< orphan*/ *);
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int sched_getaffinity(int /*<<< orphan*/, size_t, int /*<<< orphan*/ *);
int boundto(int *nelements_set, int *int_mask) {
  cpu_set_t *mask;
  size_t size;
  int i;
  int nrcpus = 1024;
  int knt = 0;
realloc:
  mask = CPU_ALLOC(nrcpus);
  size = CPU_ALLOC_SIZE(nrcpus);
  CPU_ZERO_S(size, mask);
  if (sched_getaffinity(0, size, mask) == -1) {
    CPU_FREE(mask);
    if (errno == EINVAL && nrcpus < (1024 << 8)) {
      nrcpus = nrcpus << 2;
      goto realloc;
    }
    perror("sched_getaffinity");
    return -1;
  }
  for (i = 0; i < nrcpus; i++) {
    if (CPU_ISSET_S(i, size, mask)) {
      int_mask[i] = 1;
      knt++;
    }
  }
  *nelements_set = knt;
  CPU_FREE(mask);
  return 0;
}


x86 assembly (clang, -O0):
<boundto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x400,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <CPU_ALLOC_SIZE@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <CPU_ZERO_S@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
call   1070 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
jne    1221 <boundto+0xb1>
mov    -0x20(%rbp),%rdi
call   1050 <CPU_FREE@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <boundto+0x99>
cmpl   $0x40000,-0x30(%rbp)
jge    1209 <boundto+0x99>
mov    -0x30(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x30(%rbp)
jmp    118e <boundto+0x1e>
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <boundto+0x122>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1279 <boundto+0x109>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <CPU_ISSET_S@plt>
cmp    $0x0,%rax
je     1266 <boundto+0xf6>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    126b <boundto+0xfb>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1228 <boundto+0xb8>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <CPU_FREE@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<boundto>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,(%rsp)
mov    $0x400,%ebp
mov    $0x400,%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,%r12
mov    $0x400,%edi
call   1040 <CPU_ALLOC_SIZE@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <CPU_ZERO_S@plt>
xor    %r13d,%r13d
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
jne    122f <boundto+0xbf>
mov    $0x400,%ebp
mov    0x2e0b(%rip),%r14
nopl   (%rax)
mov    %r12,%rdi
call   1050 <CPU_FREE@plt>
cmp    $0x3ffff,%ebp
jg     128c <boundto+0x11c>
mov    (%r14),%rax
mov    0x2df2(%rip),%rcx
cmp    %rax,(%rcx)
jne    128c <boundto+0x11c>
shl    $0x2,%ebp
mov    %ebp,%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1040 <CPU_ALLOC_SIZE@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <CPU_ZERO_S@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     11d0 <boundto+0x60>
test   %ebp,%ebp
jle    1269 <boundto+0xf9>
mov    %ebp,%r14d
xor    %ebp,%ebp
jmp    1249 <boundto+0xd9>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r14
je     126c <boundto+0xfc>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1090 <CPU_ISSET_S@plt>
test   %rax,%rax
je     1240 <boundto+0xd0>
movl   $0x1,(%r15,%rbp,4)
add    $0x1,%r13d
jmp    1240 <boundto+0xd0>
xor    %r13d,%r13d
mov    (%rsp),%rax
mov    %r13d,(%rax)
mov    %r12,%rdi
call   1050 <CPU_FREE@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    127d <boundto+0x10d>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@.str = [18 x i8] c"sched_getaffinity\00"
define i32 @boundto(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 1024, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %32, %2
%12 = load i32, i32* %9
%13 = call i32* @CPU_ALLOC(i32 %12)
store i32* %13, i32** %6
%14 = load i32, i32* %9
%15 = call i64 @CPU_ALLOC_SIZE(i32 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = load i32*, i32** %6
%18 = call i32 @CPU_ZERO_S(i64 %16, i32* %17)
%19 = load i64, i64* %7
%20 = load i32*, i32** %6
%21 = call i32 @sched_getaffinity(i32 0, i64 %19, i32* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %37
23: ; preds = %11
%24 = load i32*, i32** %6
%25 = call i32 @CPU_FREE(i32* %24)
%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 = %23
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 262144
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %9
%34 = shl i32 %33, 2
store i32 %34, i32* %9
br label %11
35: ; preds = %29, %23
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
37: ; preds = %11
store i32 0, i32* %8
br label %38
38: ; preds = %56, %37
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %59
42: ; preds = %38
%43 = load i32, i32* %8
%44 = load i64, i64* %7
%45 = load i32*, i32** %6
%46 = call i64 @CPU_ISSET_S(i32 %43, i64 %44, i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = load i32*, i32** %5
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store i32 1, i32* %52
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %55
55: ; preds = %48, %42
br label %56
56: ; preds = %55
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %38
59: ; preds = %38
%60 = load i32, i32* %10
%61 = load i32*, i32** %4
store i32 %60, i32* %61
%62 = load i32*, i32** %6
%63 = call i32 @CPU_FREE(i32* %62)
store i32 0, i32* %3
br label %64
64: ; preds = %59, %35
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@.str = [18 x i8] c"sched_getaffinity\00"
define i32 @boundto(i32* nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = tail call i32* @CPU_ALLOC(i32 1024)
%4 = tail call i64 @CPU_ALLOC_SIZE(i32 1024)
%5 = tail call i32 @CPU_ZERO_S(i64 %4, i32* %3)
%6 = tail call i32 @sched_getaffinity(i32 0, i64 %4, i32* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %15, label %10
8: ; preds = %24
%9 = icmp sgt i32 %25, 0
br i1 %9, label %10, label %46
10: ; preds = %2, %8
%11 = phi i64 [ %27, %8 ], [ %4, %2 ]
%12 = phi i32* [ %26, %8 ], [ %3, %2 ]
%13 = phi i32 [ %25, %8 ], [ 1024, %2 ]
%14 = zext i32 %13 to i64
br label %33
15: ; preds = %2, %24
%16 = phi i32* [ %26, %24 ], [ %3, %2 ]
%17 = phi i32 [ %25, %24 ], [ 1024, %2 ]
%18 = tail call i32 @CPU_FREE(i32* %16)
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp eq i64 %19, %20
%22 = icmp slt i32 %17, 262144
%23 = and i1 %22, %21
br i1 %23, label %24, label %31
24: ; preds = %15
%25 = shl i32 %17, 2
%26 = tail call i32* @CPU_ALLOC(i32 %25)
%27 = tail call i64 @CPU_ALLOC_SIZE(i32 %25)
%28 = tail call i32 @CPU_ZERO_S(i64 %27, i32* %26)
%29 = tail call i32 @sched_getaffinity(i32 0, i64 %27, i32* %26)
%30 = icmp eq i32 %29, -1
br i1 %30, label %15, label %8
31: ; preds = %15
%32 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %50
33: ; preds = %10, %42
%34 = phi i64 [ 0, %10 ], [ %44, %42 ]
%35 = phi i32 [ 0, %10 ], [ %43, %42 ]
%36 = trunc i64 %34 to i32
%37 = tail call i64 @CPU_ISSET_S(i32 %36, i64 %11, i32* %12)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %33
%40 = getelementptr inbounds i32, i32* %1, i64 %34
store i32 1, i32* %40
%41 = add nsw i32 %35, 1
br label %42
42: ; preds = %33, %39
%43 = phi i32 [ %41, %39 ], [ %35, %33 ]
%44 = add nuw nsw i64 %34, 1
%45 = icmp eq i64 %44, %14
br i1 %45, label %46, label %33
46: ; preds = %42, %8
%47 = phi i32* [ %26, %8 ], [ %12, %42 ]
%48 = phi i32 [ 0, %8 ], [ %43, %42 ]
store i32 %48, i32* %0
%49 = tail call i32 @CPU_FREE(i32* %47)
br label %50
50: ; preds = %46, %31
%51 = phi i32 [ -1, %31 ], [ 0, %46 ]
ret i32 %51
}