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

/* Variables and functions */
int /*<<< orphan*/ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_CONF;
int /*<<< orphan*/ sched_getaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int sched_getcpu();
int /*<<< orphan*/ sched_setaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int sysconf(int /*<<< orphan*/);
int idlestat_wake_all(void) {
  int rcpu, i, ret;
  cpu_set_t cpumask;
  cpu_set_t original_cpumask;
  ret = sysconf(_SC_NPROCESSORS_CONF);
  if (ret < 0)
    return -1;
  rcpu = sched_getcpu();
  if (rcpu < 0)
    return -1;
  sched_getaffinity(0, sizeof(original_cpumask), &original_cpumask);
  for (i = 0; i < ret; i++) {
    if (i == rcpu)
      continue;
    if (!CPU_ISSET(i, &original_cpumask))
      continue;
    CPU_ZERO(&cpumask);
    CPU_SET(i, &cpumask);
    sched_setaffinity(0, sizeof(cpumask), &cpumask);
  }
  sched_setaffinity(0, sizeof(original_cpumask), &original_cpumask);
  return 0;
}


x86 assembly (clang, -O0):
<idlestat_wake_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    119f <idlestat_wake_all+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <idlestat_wake_all+0xe7>
mov    $0x0,%al
call   1070 <sched_getcpu@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bf <idlestat_wake_all+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <idlestat_wake_all+0xe7>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1080 <sched_getaffinity@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1240 <idlestat_wake_all+0xd0>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11f3 <idlestat_wake_all+0x83>
jmp    1232 <idlestat_wake_all+0xc2>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <CPU_ISSET@plt>
cmp    $0x0,%eax
jne    120d <idlestat_wake_all+0x9d>
jmp    1232 <idlestat_wake_all+0xc2>
lea    -0x14(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d6 <idlestat_wake_all+0x66>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idlestat_wake_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1220 <idlestat_wake_all+0xb0>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <sched_getcpu@plt>
test   %eax,%eax
js     1220 <idlestat_wake_all+0xb0>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1080 <sched_getaffinity@plt>
test   %r14d,%r14d
je     120d <idlestat_wake_all+0x9d>
lea    0x8(%rsp),%r15
lea    0xc(%rsp),%r12
jmp    11d8 <idlestat_wake_all+0x68>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
cmp    %ebx,%r14d
je     120d <idlestat_wake_all+0x9d>
cmp    %ebx,%ebp
je     11d0 <idlestat_wake_all+0x60>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <CPU_ISSET@plt>
test   %eax,%eax
je     11d0 <idlestat_wake_all+0x60>
mov    %r12,%rdi
call   1050 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r12,%rdx
call   1030 <sched_setaffinity@plt>
jmp    11d0 <idlestat_wake_all+0x60>
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1030 <sched_setaffinity@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
define i32 @idlestat_wake_all() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @_SC_NPROCESSORS_CONF
%8 = call i32 @sysconf(i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %43
12: ; preds = %0
%13 = call i32 (...) @sched_getcpu()
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %1
br label %43
17: ; preds = %12
%18 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %6)
store i32 0, i32* %3
br label %19
19: ; preds = %38, %17
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %41
23: ; preds = %19
%24 = load i32, i32* %3
%25 = load i32, i32* %2
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %38
28: ; preds = %23
%29 = load i32, i32* %3
%30 = call i32 @CPU_ISSET(i32 %29, i32* %6)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %28
br label %38
33: ; preds = %28
%34 = call i32 @CPU_ZERO(i32* %5)
%35 = load i32, i32* %3
%36 = call i32 @CPU_SET(i32 %35, i32* %5)
%37 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %5)
br label %38
38: ; preds = %33, %32, %27
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %19
41: ; preds = %19
%42 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %6)
store i32 0, i32* %1
br label %43
43: ; preds = %41, %16, %11
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
define i32 @idlestat_wake_all() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @_SC_NPROCESSORS_CONF
%6 = tail call i32 @sysconf(i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %0
%9 = tail call i32 (...) @sched_getcpu()
%10 = icmp slt i32 %9, 0
br i1 %10, label %29, label %11
11: ; preds = %8
%12 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
%13 = icmp eq i32 %6, 0
br i1 %13, label %27, label %14
14: ; preds = %11, %24
%15 = phi i32 [ %25, %24 ], [ 0, %11 ]
%16 = icmp eq i32 %15, %9
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = call i32 @CPU_ISSET(i32 %15, i32* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = call i32 @CPU_ZERO(i32* %1)
%22 = call i32 @CPU_SET(i32 %15, i32* %1)
%23 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %1)
br label %24
24: ; preds = %17, %14, %20
%25 = add nuw nsw i32 %15, 1
%26 = icmp eq i32 %25, %6
br i1 %26, label %27, label %14
27: ; preds = %24, %11
%28 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %2)
br label %29
29: ; preds = %8, %0, %27
%30 = phi i32 [ 0, %27 ], [ -1, %0 ], [ -1, %8 ]
ret i32 %30
}