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

/* Variables and functions */
scalar_t__ CPU_ISSET(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_CONF;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, double);
int sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sched_setaffinity(int /*<<< orphan*/, int, unsigned long *);
int sysconf(int /*<<< orphan*/);
double waste_time(int);
int main(int argc, char **argv) {
  unsigned long mask = 1;
  cpu_set_t cs;
  int ret;
  int cpunum = sysconf(_SC_NPROCESSORS_CONF);
  printf("Total [%d] CPU cores\n", cpunum);
  ret = sched_setaffinity(0, sizeof(mask), &mask);
  if (ret < 0) {
    perror("sched_setaffinity");
  }
  CPU_ZERO(&cs);
  if (ret = sched_getaffinity(0, sizeof(cs), &cs)) {
    printf("Failed to get CPU cores info [%d]\n", ret);
    return 3;
  }
  char i = 0;
  for (i = 0; i < cpunum; i++) {
    if (CPU_ISSET(i, &cs))
      printf("Initially: I am running on CPU core[%d]\n", i);
  }
  printf("result: %f\n", waste_time(200000));
  mask = 2;
  ret = sched_setaffinity(0, sizeof(mask), &mask);
  if (ret < 0) {
    perror("sched_setaffinity");
  }
  CPU_ZERO(&cs);
  if (ret = sched_getaffinity(0, sizeof(cs), &cs)) {
    printf("Failed to get CPU cores info [%d]\n", ret);
    return 3;
  }
  for (i = 0; i < cpunum; i++) {
    if (CPU_ISSET(i, &cs))
      printf("Afterwards: I am running on CPU core[%d]\n", i);
  }
  printf("result: %f\n", waste_time(200000));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x24(%rbp)
cvtsi2sdl -0x24(%rbp),%xmm0
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
mov    $0x8,%esi
lea    -0x18(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e9 <main+0x69>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x1c(%rbp),%rdx
call   10a0 <sched_getaffinity@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     122b <main+0xab>
cvtsi2sdl -0x20(%rbp),%xmm0
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    136a <main+0x1ea>
movb   $0x0,-0x25(%rbp)
movb   $0x0,-0x25(%rbp)
movsbl -0x25(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    127d <main+0xfd>
lea    -0x1c(%rbp),%rsi
movsbl -0x25(%rbp),%edi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     126b <main+0xeb>
movsbl -0x25(%rbp),%eax
cvtsi2sd %eax,%xmm0
lea    0xde5(%rip),%rdi
call   1030 <printf@plt>
jmp    1270 <main+0xf0>
mov    -0x25(%rbp),%al
add    $0x1,%al
mov    %al,-0x25(%rbp)
jmp    1233 <main+0xb3>
mov    $0x30d40,%edi
call   1060 <waste_time@plt>
lea    0xde6(%rip),%rdi
call   1030 <printf@plt>
movq   $0x2,-0x18(%rbp)
xor    %edi,%edi
mov    $0x8,%esi
lea    -0x18(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12c4 <main+0x144>
lea    0xd57(%rip),%rdi
call   1080 <perror@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x1c(%rbp),%rdx
call   10a0 <sched_getaffinity@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1306 <main+0x186>
cvtsi2sdl -0x20(%rbp),%xmm0
lea    0xd33(%rip),%rdi
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    136a <main+0x1ea>
movb   $0x0,-0x25(%rbp)
movsbl -0x25(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1354 <main+0x1d4>
lea    -0x1c(%rbp),%rsi
movsbl -0x25(%rbp),%edi
call   1050 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     1342 <main+0x1c2>
movsbl -0x25(%rbp),%eax
cvtsi2sd %eax,%xmm0
lea    0xd43(%rip),%rdi
call   1030 <printf@plt>
jmp    1347 <main+0x1c7>
mov    -0x25(%rbp),%al
add    $0x1,%al
mov    %al,-0x25(%rbp)
jmp    130a <main+0x18a>
mov    $0x30d40,%edi
call   1060 <waste_time@plt>
lea    0xd0f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x1,0x8(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%r15d
cvtsi2sd %eax,%xmm0
lea    0xe4f(%rip),%rdi
call   1030 <printf@plt>
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x8,%esi
call   1040 <sched_setaffinity@plt>
test   %eax,%eax
js     1318 <main+0x198>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   10a0 <sched_getaffinity@plt>
test   %eax,%eax
jne    1296 <main+0x116>
test   %r15d,%r15d
jle    123e <main+0xbe>
xor    %ebx,%ebx
lea    0x4(%rsp),%r12
lea    0xe45(%rip),%r14
jmp    121b <main+0x9b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%bl
movsbl %bl,%eax
cmp    %eax,%r15d
jle    123e <main+0xbe>
movsbl %bl,%ebp
mov    %ebp,%edi
mov    %r12,%rsi
call   1050 <CPU_ISSET@plt>
test   %rax,%rax
je     1210 <main+0x90>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mov    %r14,%rdi
call   1030 <printf@plt>
jmp    1210 <main+0x90>
mov    $0x30d40,%edi
call   1060 <waste_time@plt>
lea    0xe25(%rip),%rdi
call   1030 <printf@plt>
movq   $0x2,0x8(%rsp)
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x8,%esi
call   1040 <sched_setaffinity@plt>
test   %eax,%eax
js     1329 <main+0x1a9>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   10a0 <sched_getaffinity@plt>
test   %eax,%eax
je     12bb <main+0x13b>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
lea    0xd84(%rip),%rdi
call   1030 <printf@plt>
mov    $0x3,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15d,%r15d
jle    12fe <main+0x17e>
xor    %ebx,%ebx
lea    0x4(%rsp),%r12
lea    0xdb2(%rip),%r14
jmp    12db <main+0x15b>
add    $0x1,%bl
movsbl %bl,%eax
cmp    %eax,%r15d
jle    12fe <main+0x17e>
movsbl %bl,%ebp
mov    %ebp,%edi
mov    %r12,%rsi
call   1050 <CPU_ISSET@plt>
test   %rax,%rax
je     12d0 <main+0x150>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mov    %r14,%rdi
call   1030 <printf@plt>
jmp    12d0 <main+0x150>
mov    $0x30d40,%edi
call   1060 <waste_time@plt>
lea    0xd65(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    12ae <main+0x12e>
lea    0xcf7(%rip),%rdi
call   1080 <perror@plt>
jmp    11cf <main+0x4f>
lea    0xce6(%rip),%rdi
call   1080 <perror@plt>
jmp    1276 <main+0xf6>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [22 x i8] c"Total [%d] CPU cores\0A\00"
@.str.1 = [18 x i8] c"sched_setaffinity\00"
@.str.2 = [35 x i8] c"Failed to get CPU cores info [%d]\0A\00"
@.str.3 = [41 x i8] c"Initially: I am running on CPU core[%d]\0A\00"
@.str.4 = [12 x i8] c"result: %f\0A\00"
@.str.5 = [42 x i8] c"Afterwards: I am running on CPU core[%d]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1, i64* %6
%11 = load i32, i32* @_SC_NPROCESSORS_CONF
%12 = call i32 @sysconf(i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = sitofp i32 %13 to double
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %14)
%16 = call i32 @sched_setaffinity(i32 0, i32 8, i64* %6)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 @CPU_ZERO(i32* %7)
%23 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %7)
store i32 %23, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %8
%27 = sitofp i32 %26 to double
%28 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), double %27)
store i32 3, i32* %3
br label %84
29: ; preds = %21
store i8 0, i8* %10
store i8 0, i8* %10
br label %30
30: ; preds = %44, %29
%31 = load i8, i8* %10
%32 = sext i8 %31 to i32
%33 = load i32, i32* %9
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = load i8, i8* %10
%37 = call i64 @CPU_ISSET(i8 signext %36, i32* %7)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i8, i8* %10
%41 = sitofp i8 %40 to double
%42 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), double %41)
br label %43
43: ; preds = %39, %35
br label %44
44: ; preds = %43
%45 = load i8, i8* %10
%46 = add i8 %45, 1
store i8 %46, i8* %10
br label %30
47: ; preds = %30
%48 = call double @waste_time(i32 200000)
%49 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), double %48)
store i64 2, i64* %6
%50 = call i32 @sched_setaffinity(i32 0, i32 8, i64* %6)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = call i32 @CPU_ZERO(i32* %7)
%57 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %7)
store i32 %57, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = load i32, i32* %8
%61 = sitofp i32 %60 to double
%62 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), double %61)
store i32 3, i32* %3
br label %84
63: ; preds = %55
store i8 0, i8* %10
br label %64
64: ; preds = %78, %63
%65 = load i8, i8* %10
%66 = sext i8 %65 to i32
%67 = load i32, i32* %9
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %81
69: ; preds = %64
%70 = load i8, i8* %10
%71 = call i64 @CPU_ISSET(i8 signext %70, i32* %7)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %69
%74 = load i8, i8* %10
%75 = sitofp i8 %74 to double
%76 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), double %75)
br label %77
77: ; preds = %73, %69
br label %78
78: ; preds = %77
%79 = load i8, i8* %10
%80 = add i8 %79, 1
store i8 %80, i8* %10
br label %64
81: ; preds = %64
%82 = call double @waste_time(i32 200000)
%83 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), double %82)
br label %84
84: ; preds = %81, %59, %25
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [22 x i8] c"Total [%d] CPU cores\0A\00"
@.str.1 = [18 x i8] c"sched_setaffinity\00"
@.str.2 = [35 x i8] c"Failed to get CPU cores info [%d]\0A\00"
@.str.3 = [41 x i8] c"Initially: I am running on CPU core[%d]\0A\00"
@.str.4 = [12 x i8] c"result: %f\0A\00"
@.str.5 = [42 x i8] c"Afterwards: I am running on CPU core[%d]\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = alloca i32
%5 = bitcast i64* %3 to i8*
store i64 1, i64* %3
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @_SC_NPROCESSORS_CONF
%8 = tail call i32 @sysconf(i32 %7)
%9 = sitofp i32 %8 to double
%10 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %9)
%11 = call i32 @sched_setaffinity(i32 0, i32 8, i64* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @CPU_ZERO(i32* %4)
%17 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = icmp sgt i32 %8, 0
br i1 %20, label %24, label %35
21: ; preds = %15
%22 = sitofp i32 %17 to double
%23 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), double %22)
br label %64
24: ; preds = %19, %31
%25 = phi i8 [ %32, %31 ], [ 0, %19 ]
%26 = call i64 @CPU_ISSET(i8 signext %25, i32* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = sitofp i8 %25 to double
%30 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), double %29)
br label %31
31: ; preds = %24, %28
%32 = add i8 %25, 1
%33 = sext i8 %32 to i32
%34 = icmp sgt i32 %8, %33
br i1 %34, label %24, label %35
35: ; preds = %31, %19
%36 = call double @waste_time(i32 200000)
%37 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), double %36)
store i64 2, i64* %3
%38 = call i32 @sched_setaffinity(i32 0, i32 8, i64* %3)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = call i32 @CPU_ZERO(i32* %4)
%44 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %4)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
br i1 %20, label %50, label %61
47: ; preds = %42
%48 = sitofp i32 %44 to double
%49 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), double %48)
br label %64
50: ; preds = %46, %57
%51 = phi i8 [ %58, %57 ], [ 0, %46 ]
%52 = call i64 @CPU_ISSET(i8 signext %51, i32* %4)
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %50
%55 = sitofp i8 %51 to double
%56 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), double %55)
br label %57
57: ; preds = %50, %54
%58 = add i8 %51, 1
%59 = sext i8 %58 to i32
%60 = icmp sgt i32 %8, %59
br i1 %60, label %50, label %61
61: ; preds = %57, %46
%62 = call double @waste_time(i32 200000)
%63 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), double %62)
br label %64
64: ; preds = %47, %61, %21
%65 = phi i32 [ 3, %21 ], [ 3, %47 ], [ 0, %61 ]
ret i32 %65
}