#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;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int atoi(char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int sched_getcpu();
int /*<<< orphan*/ sched_setaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  long i = 0, j;
  int sum = 0;
  int core = -1;
  cpu_set_t my_set;
  CPU_ZERO(&my_set);
  if (argc == 1) {
    for (j = 0; j < 8; j++) {
      CPU_SET(j, &my_set);
    }
  } else if (argc == 2) {
    core = atoi(argv[1]);
    if (core >= 8) {
      printf("wrong argument.\n");
      return -1;
    }
    CPU_SET(core, &my_set);
  }
  sched_setaffinity(0, sizeof(cpu_set_t), &my_set);
  printf("Setting pid %d to cpu %d\n", getpid(), sched_getcpu());
  while (1) {
    for (i = 0; i < 2000000000; i += 1) {
      sum += 2;
    }
    printf("Process pid %d is running on cpu %d.    res=%d\n", getpid(),
           sched_getcpu(), sum);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
cmpl   $0x1,-0x8(%rbp)
jne    11e7 <main+0x77>
movq   $0x0,-0x20(%rbp)
cmpq   $0x8,-0x20(%rbp)
jge    11e2 <main+0x72>
mov    -0x20(%rbp),%rax
mov    %eax,%edi
lea    -0x2c(%rbp),%rsi
call   1090 <CPU_SET@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b7 <main+0x47>
jmp    1235 <main+0xc5>
cmpl   $0x2,-0x8(%rbp)
jne    1230 <main+0xc0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x8,-0x28(%rbp)
jl     1224 <main+0xb4>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbp
ret
mov    -0x28(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1090 <CPU_SET@plt>
jmp    1235 <main+0xc5>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x2c(%rbp),%rdx
call   1050 <sched_setaffinity@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1070 <sched_getcpu@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x77359400,-0x18(%rbp)
77
jge    1299 <main+0x129>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1271 <main+0x101>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1070 <sched_getcpu@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
mov    -0x24(%rbp),%ecx
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1269 <main+0xf9>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xc(%rsp),%rdi
call   1070 <CPU_ZERO@plt>
cmp    $0x2,%ebp
je     1209 <main+0x89>
cmp    $0x1,%ebp
jne    123d <main+0xbd>
lea    0xc(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x4,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x5,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x6,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    $0x7,%edi
jmp    1233 <main+0xb3>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
cmp    $0x8,%eax
jl     1233 <main+0xb3>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xc(%rsp),%rsi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1060 <sched_setaffinity@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <sched_getcpu@plt>
lea    0xd9b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xee6b2800,%ebx
lea    0xd9e(%rip),%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <sched_getcpu@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xee6b2800,%ebx
jmp    1280 <main+0x100>

LLVM IR (-O0):
@.str = [17 x i8] c"wrong argument.\0A\00"
@.str.1 = [26 x i8] c"Setting pid %d to cpu %d\0A\00"
@.str.2 = [48 x i8] c"Process pid %d is running on cpu %d. res=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
store i32 0, i32* %8
store i32 -1, i32* %9
%11 = call i32 @CPU_ZERO(i32* %10)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %26
14: ; preds = %2
store i64 0, i64* %7
br label %15
15: ; preds = %22, %14
%16 = load i64, i64* %7
%17 = icmp slt i64 %16, 8
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i32
%21 = call i32 @CPU_SET(i32 %20, i32* %10)
br label %22
22: ; preds = %18
%23 = load i64, i64* %7
%24 = add nsw i64 %23, 1
store i64 %24, i64* %7
br label %15
25: ; preds = %15
br label %42
26: ; preds = %2
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %41
29: ; preds = %26
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sge i32 %34, 8
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
ret i32 -1
38: ; preds = %29
%39 = load i32, i32* %9
%40 = call i32 @CPU_SET(i32 %39, i32* %10)
br label %41
41: ; preds = %38, %26
br label %42
42: ; preds = %41, %25
%43 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %10)
%44 = call i32 (...) @getpid()
%45 = call i32 (...) @sched_getcpu()
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %44, i32 %45)
br label %47
47: ; preds = %42, %57
store i64 0, i64* %6
br label %48
48: ; preds = %54, %47
%49 = load i64, i64* %6
%50 = icmp slt i64 %49, 2000000000
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 2
store i32 %53, i32* %8
br label %54
54: ; preds = %51
%55 = load i64, i64* %6
%56 = add nsw i64 %55, 1
store i64 %56, i64* %6
br label %48
57: ; preds = %48
%58 = call i32 (...) @getpid()
%59 = call i32 (...) @sched_getcpu()
%60 = load i32, i32* %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %58, i32 %59, i32 %60)
br label %47
}

LLVM IR (-O2):
@.str.1 = [26 x i8] c"Setting pid %d to cpu %d\0A\00"
@.str.2 = [48 x i8] c"Process pid %d is running on cpu %d. res=%d\0A\00"
@str = [16 x i8] c"wrong argument.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @CPU_ZERO(i32* %3)
switch i32 %0, label %24 [
i32 1, label %6
i32 2, label %14
]
6: ; preds = %2
%7 = call i32 @CPU_SET(i32 0, i32* %3)
%8 = call i32 @CPU_SET(i32 1, i32* %3)
%9 = call i32 @CPU_SET(i32 2, i32* %3)
%10 = call i32 @CPU_SET(i32 3, i32* %3)
%11 = call i32 @CPU_SET(i32 4, i32* %3)
%12 = call i32 @CPU_SET(i32 5, i32* %3)
%13 = call i32 @CPU_SET(i32 6, i32* %3)
br label %21
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
%18 = icmp sgt i32 %17, 7
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 -1
21: ; preds = %14, %6
%22 = phi i32 [ 7, %6 ], [ %17, %14 ]
%23 = call i32 @CPU_SET(i32 %22, i32* %3)
br label %24
24: ; preds = %21, %2
%25 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @sched_getcpu()
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %29, %24
%30 = phi i32 [ 0, %24 ], [ %31, %29 ]
%31 = add i32 %30, -294967296
%32 = call i32 (...) @getpid()
%33 = call i32 (...) @sched_getcpu()
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33, i32 %31)
br label %29
}