#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_S(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ __CPU_ZERO_S(int, int /*<<< orphan*/ *);
int sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int getaffinity(void) {
  cpu_set_t mask;
  int rc, i;
  __CPU_ZERO_S(sizeof(cpu_set_t), &mask);
  rc = sched_getaffinity(0, sizeof(mask), (cpu_set_t *)&mask);
  if (rc != 0) {
    return -2;
  }
  for (i = 0; i < 128; i++) {
    if (__CPU_ISSET_S(i, sizeof(cpu_set_t), &mask)) {
      return i;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<getaffinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x4,%edi
lea    -0x8(%rbp),%rsi
call   1040 <__CPU_ZERO_S@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1050 <sched_getaffinity@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     116f <getaffinity+0x3f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c3 <getaffinity+0x93>
movl   $0x0,-0x10(%rbp)
cmpl   $0x80,-0x10(%rbp)
jge    11bc <getaffinity+0x8c>
mov    -0x10(%rbp),%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1030 <__CPU_ISSET_S@plt>
cmp    $0x0,%rax
je     11a9 <getaffinity+0x79>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <getaffinity+0x93>
jmp    11ae <getaffinity+0x7e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <getaffinity+0x46>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaffinity>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%r14
mov    $0x4,%edi
mov    %r14,%rsi
call   1040 <__CPU_ZERO_S@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1050 <sched_getaffinity@plt>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
test   %ecx,%ecx
jne    1198 <getaffinity+0x68>
lea    0x4(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <__CPU_ISSET_S@plt>
test   %rax,%rax
jne    1196 <getaffinity+0x66>
add    $0x1,%ebx
cmp    $0x80,%ebx
jne    1170 <getaffinity+0x40>
mov    $0xffffffff,%eax
jmp    1198 <getaffinity+0x68>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @getaffinity() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @__CPU_ZERO_S(i32 4, i32* %2)
%6 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -2, i32* %1
br label %25
10: ; preds = %0
store i32 0, i32* %4
br label %11
11: ; preds = %21, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 128
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = load i32, i32* %4
%16 = call i64 @__CPU_ISSET_S(i32 %15, i32 4, i32* %2)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %4
store i32 %19, i32* %1
br label %25
20: ; preds = %14
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %11
24: ; preds = %11
store i32 -1, i32* %1
br label %25
25: ; preds = %24, %18, %9
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
define i32 @getaffinity() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @__CPU_ZERO_S(i32 4, i32* %1)
%4 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0, %10
%7 = phi i32 [ %11, %10 ], [ 0, %0 ]
%8 = call i64 @__CPU_ISSET_S(i32 %7, i32 4, i32* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = add nuw nsw i32 %7, 1
%12 = icmp eq i32 %11, 128
br i1 %12, label %13, label %6
13: ; preds = %10, %6, %0
%14 = phi i32 [ -2, %0 ], [ -1, %10 ], [ %7, %6 ]
ret i32 %14
}