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

/* Variables and functions */
scalar_t__ CPU_ISSET(long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int errno;
scalar_t__ sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
unsigned int getaffinity(unsigned long int NumCPUS,
                         unsigned long int *maskArray[], int *errno_var) {
  cpu_set_t mask;
  CPU_ZERO(&mask);
  *errno_var = 0;
  if (0 == sched_getaffinity(0, sizeof(mask), &mask)) {
    long li;
    for (li = 0; li < NumCPUS; li++) {
      unsigned long int valueMask = 0;
      if (CPU_ISSET(li, &mask)) {
        valueMask = 1;
      }
      *(unsigned long int *)maskArray = valueMask;
      maskArray++;
    }
    return 0;
  } else {
    *errno_var = errno;
    return 1;
  }
}


x86 assembly (clang, -O0):
<getaffinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x24(%rbp),%rdx
call   1050 <sched_getaffinity@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11e6 <getaffinity+0xb6>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11da <getaffinity+0xaa>
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     11b2 <getaffinity+0x82>
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    117d <getaffinity+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    11fc <getaffinity+0xcc>
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaffinity>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <CPU_ZERO@plt>
movl   $0x0,(%r12)
xor    %r15d,%r15d
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <sched_getaffinity@plt>
test   %rax,%rax
je     1187 <getaffinity+0x57>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
mov    $0x1,%r15d
jmp    11c0 <getaffinity+0x90>
test   %r13,%r13
je     11c0 <getaffinity+0x90>
xor    %r15d,%r15d
lea    0xc(%rsp),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <CPU_ISSET@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    %rcx,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11a0 <getaffinity+0x70>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @getaffinity(i64 %0, i64** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i64 %0, i64* %5
store i64** %1, i64*** %6
store i32* %2, i32** %7
%11 = call i32 @CPU_ZERO(i32* %8)
%12 = load i32*, i32** %7
store i32 0, i32* %12
%13 = call i64 @sched_getaffinity(i32 0, i32 4, i32* %8)
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %35
15: ; preds = %3
store i64 0, i64* %9
br label %16
16: ; preds = %31, %15
%17 = load i64, i64* %9
%18 = load i64, i64* %5
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
store i64 0, i64* %10
%21 = load i64, i64* %9
%22 = call i64 @CPU_ISSET(i64 %21, i32* %8)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i64 1, i64* %10
br label %25
25: ; preds = %24, %20
%26 = load i64, i64* %10
%27 = load i64**, i64*** %6
%28 = bitcast i64** %27 to i64*
store i64 %26, i64* %28
%29 = load i64**, i64*** %6
%30 = getelementptr inbounds i64*, i64** %29, i32 1
store i64** %30, i64*** %6
br label %31
31: ; preds = %25
%32 = load i64, i64* %9
%33 = add nsw i64 %32, 1
store i64 %33, i64* %9
br label %16
34: ; preds = %16
store i32 0, i32* %4
br label %38
35: ; preds = %3
%36 = load i32, i32* @errno
%37 = load i32*, i32** %7
store i32 %36, i32* %37
store i32 1, i32* %4
br label %38
38: ; preds = %35, %34
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @getaffinity(i64 %0, i64** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @CPU_ZERO(i32* %4)
store i32 0, i32* %2
%7 = call i64 @sched_getaffinity(i32 0, i32 4, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = icmp eq i64 %0, 0
br i1 %10, label %23, label %11
11: ; preds = %9, %11
%12 = phi i64 [ %19, %11 ], [ 0, %9 ]
%13 = phi i64** [ %18, %11 ], [ %1, %9 ]
%14 = call i64 @CPU_ISSET(i64 %12, i32* %4)
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i64
%17 = bitcast i64** %13 to i64*
store i64 %16, i64* %17
%18 = getelementptr inbounds i64*, i64** %13, i64 1
%19 = add nuw i64 %12, 1
%20 = icmp eq i64 %19, %0
br i1 %20, label %23, label %11
21: ; preds = %3
%22 = load i32, i32* @errno
store i32 %22, i32* %2
br label %23
23: ; preds = %11, %9, %21
%24 = phi i32 [ 1, %21 ], [ 0, %9 ], [ 0, %11 ]
ret i32 %24
}