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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int EXIT_SUCCESS;
int /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int getpid();
int sched_getcpu();
int /*<<< orphan*/ sched_setaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int main(void) {
  int cpu;
  cpu_set_t cpu_set;
  int i;
  for (;;) {
    for (cpu = 0; cpu < sysconf(_SC_NPROCESSORS_ONLN); cpu++) {
      CPU_ZERO(&cpu_set);
      CPU_SET(cpu, &cpu_set);
      sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
      fprintf(stderr, "[%d] CPU -> %d\n", getpid(), sched_getcpu());
      for (i = 0; i < 500000000; i++)
        ;
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jge    1237 <main+0xc7>
lea    -0xc(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <sched_getcpu@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
lea    0xe08(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x1dcd6500,-0x10(%rbp)
jge    1224 <main+0xb4>
jmp    1216 <main+0xa6>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1204 <main+0x94>
jmp    1229 <main+0xb9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1186 <main+0x16>
jmp    117f <main+0xf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rbx
lea    0x4(%rsp),%r15
mov    0x2e4a(%rip),%r13
lea    0xe6b(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
jle    11a0 <main+0x30>
xor    %ebp,%ebp
nopl   (%rax)
mov    %r15,%rdi
call   1060 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r15,%rdx
call   1040 <sched_setaffinity@plt>
mov    %r12,%rbx
mov    0x0(%r13),%r12d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <sched_getcpu@plt>
mov    %r12d,%edi
mov    %rbx,%r12
mov    0x2df2(%rip),%rbx
mov    %r12,%rsi
mov    %r14d,%edx
mov    %eax,%ecx
call   1050 <fprintf@plt>
add    $0x1,%ebp
mov    (%rbx),%edi
call   1070 <sysconf@plt>
cmp    %eax,%ebp
jl     11b0 <main+0x40>
jmp    11a0 <main+0x30>

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%d] CPU -> %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %31, %0
store i32 0, i32* %2
br label %6
6: ; preds = %28, %5
%7 = load i32, i32* %2
%8 = load i32, i32* @_SC_NPROCESSORS_ONLN
%9 = call i32 @sysconf(i32 %8)
%10 = icmp slt i32 %7, %9
br i1 %10, label %11, label %31
11: ; preds = %6
%12 = call i32 @CPU_ZERO(i32* %3)
%13 = load i32, i32* %2
%14 = call i32 @CPU_SET(i32 %13, i32* %3)
%15 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%16 = load i32, i32* @stderr
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @sched_getcpu()
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
store i32 0, i32* %4
br label %20
20: ; preds = %24, %11
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 500000000
br i1 %22, label %23, label %27
23: ; preds = %20
br label %24
24: ; preds = %23
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %20
27: ; preds = %20
br label %28
28: ; preds = %27
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
br label %6
31: ; preds = %6
br label %5
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%d] CPU -> %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %7, %0
%4 = load i32, i32* @_SC_NPROCESSORS_ONLN
%5 = call i32 @sysconf(i32 %4)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %8, %3
br label %3
8: ; preds = %3, %8
%9 = phi i32 [ %17, %8 ], [ 0, %3 ]
%10 = call i32 @CPU_ZERO(i32* %1)
%11 = call i32 @CPU_SET(i32 %9, i32* %1)
%12 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %1)
%13 = load i32, i32* @stderr
%14 = call i32 (...) @getpid()
%15 = call i32 (...) @sched_getcpu()
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = add nuw nsw i32 %9, 1
%18 = load i32, i32* @_SC_NPROCESSORS_ONLN
%19 = call i32 @sysconf(i32 %18)
%20 = icmp slt i32 %17, %19
br i1 %20, label %8, label %7
}