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

/* Variables and functions */
scalar_t__ CPU_ISSET(unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int _cpu_count_s(size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sched_getaffinity(int /*<<< orphan*/, size_t, int /*<<< orphan*/ *);
unsigned long list_cpus_sched_affinity(cpu_set_t *set, size_t setsize) {
  unsigned long cpu_num;
  int i, count;
  cpu_set_t local_set;
  if (!set) {
    set = &local_set;
    setsize = sizeof(local_set);
    CPU_ZERO((set));
  }
  if (sched_getaffinity(0, setsize, set) == -1) {
    perror("sched_getaffinity");
    return 0;
  }
  count = _cpu_count_s((setsize), (set));
  for (i = cpu_num = 0; i < count && cpu_num <= 8 * setsize; cpu_num++) {
    if (CPU_ISSET((cpu_num), (set))) {
      i++;
    }
  }
  return cpu_num;
}


x86 assembly (clang, -O0):
<list_cpus_sched_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <list_cpus_sched_affinity+0x34>
lea    -0x2c(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x4,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1070 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
jne    11b5 <list_cpus_sched_affinity+0x65>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1248 <list_cpus_sched_affinity+0xf8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <_cpu_count_s@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x28(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jge    11fa <list_cpus_sched_affinity+0xaa>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x3,%rcx
cmp    %rcx,%rax
setbe  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    120a <list_cpus_sched_affinity+0xba>
jmp    1240 <list_cpus_sched_affinity+0xf0>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     122a <list_cpus_sched_affinity+0xda>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    122f <list_cpus_sched_affinity+0xdf>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d4 <list_cpus_sched_affinity+0x84>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_cpus_sched_affinity>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
test   %rdi,%rdi
jne    117a <list_cpus_sched_affinity+0x2a>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1050 <CPU_ZERO@plt>
mov    $0x4,%r15d
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <sched_getaffinity@plt>
cmp    $0xffffffff,%eax
je     11d5 <list_cpus_sched_affinity+0x85>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <_cpu_count_s@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    11e1 <list_cpus_sched_affinity+0x91>
mov    %eax,%r12d
shl    $0x3,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <CPU_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
lea    0x1(%rbx),%rax
cmp    %r12d,%ebp
jge    11e3 <list_cpus_sched_affinity+0x93>
cmp    %r15,%rbx
mov    %rax,%rbx
jb     11b0 <list_cpus_sched_affinity+0x60>
jmp    11e3 <list_cpus_sched_affinity+0x93>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"sched_getaffinity\00"
define i64 @list_cpus_sched_affinity(i32* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
store i32* %9, i32** %4
store i64 4, i64* %5
%13 = load i32*, i32** %4
%14 = call i32 @CPU_ZERO(i32* %13)
br label %15
15: ; preds = %12, %2
%16 = load i64, i64* %5
%17 = load i32*, i32** %4
%18 = call i32 @sched_getaffinity(i32 0, i64 %16, i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %51
22: ; preds = %15
%23 = load i64, i64* %5
%24 = load i32*, i32** %4
%25 = call i32 @_cpu_count_s(i64 %23, i32* %24)
store i32 %25, i32* %8
store i64 0, i64* %6
store i32 0, i32* %7
br label %26
26: ; preds = %46, %22
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i64, i64* %6
%32 = load i64, i64* %5
%33 = mul i64 8, %32
%34 = icmp ule i64 %31, %33
br label %35
35: ; preds = %30, %26
%36 = phi i1 [ false, %26 ], [ %34, %30 ]
br i1 %36, label %37, label %49
37: ; preds = %35
%38 = load i64, i64* %6
%39 = load i32*, i32** %4
%40 = call i64 @CPU_ISSET(i64 %38, i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %45
45: ; preds = %42, %37
br label %46
46: ; preds = %45
%47 = load i64, i64* %6
%48 = add i64 %47, 1
store i64 %48, i64* %6
br label %26
49: ; preds = %35
%50 = load i64, i64* %6
store i64 %50, i64* %3
br label %51
51: ; preds = %49, %20
%52 = load i64, i64* %3
ret i64 %52
}

LLVM IR (-O2):
@.str = [18 x i8] c"sched_getaffinity\00"
define i64 @list_cpus_sched_affinity(i32* %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32* %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = call i32 @CPU_ZERO(i32* %3)
br label %8
8: ; preds = %6, %2
%9 = phi i32* [ %0, %2 ], [ %3, %6 ]
%10 = phi i64 [ %1, %2 ], [ 4, %6 ]
%11 = call i32 @sched_getaffinity(i32 0, i64 %10, i32* %9)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %30
15: ; preds = %8
%16 = call i32 @_cpu_count_s(i64 %10, i32* %9)
%17 = shl i64 %10, 3
%18 = icmp sgt i32 %16, 0
br i1 %18, label %19, label %30
19: ; preds = %15, %19
%20 = phi i32 [ %25, %19 ], [ 0, %15 ]
%21 = phi i64 [ %26, %19 ], [ 0, %15 ]
%22 = call i64 @CPU_ISSET(i64 %21, i32* %9)
%23 = icmp ne i64 %22, 0
%24 = zext i1 %23 to i32
%25 = add nuw nsw i32 %20, %24
%26 = add nuw i64 %21, 1
%27 = icmp slt i32 %25, %16
%28 = icmp ult i64 %21, %17
%29 = and i1 %27, %28
br i1 %29, label %19, label %30
30: ; preds = %19, %15, %13
%31 = phi i64 [ 0, %13 ], [ 0, %15 ], [ %26, %19 ]
ret i64 %31
}