#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(long, 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 == 2) {
    core = atoi(argv[1]);
    if (core >= 8) {
      printf("wrong argument.\n");
      return -1;
    }
    for (j = 0; j < 8; j++) {
      if (j != core) {
        CPU_SET(j, &my_set);
      }
    }
  } else {
    printf("improper usage");
    return -1;
  }
  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   $0x2,-0x8(%rbp)
jne    122f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x8,-0x28(%rbp)
jl     11e3 <main+0x73>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <main+0x169>
movq   $0x0,-0x20(%rbp)
cmpq   $0x8,-0x20(%rbp)
jge    122a <main+0xba>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
cmp    %rcx,%rax
je     1214 <main+0xa4>
mov    -0x20(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1090 <CPU_SET@plt>
jmp    1219 <main+0xa9>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11eb <main+0x7b>
jmp    1249 <main+0xd9>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <main+0x169>
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    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x77359400,-0x18(%rbp)
77
jge    12ad <main+0x13d>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1285 <main+0x115>
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    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127d <main+0x10d>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

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
jne    11b8 <main+0x38>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
cmp    $0x7,%eax
jle    11d4 <main+0x54>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
jmp    11c6 <main+0x46>
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebx
lea    0xc(%rsp),%rsi
test   %eax,%eax
je     126d <main+0xed>
xor    %edi,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x1,%ebx
je     1277 <main+0xf7>
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x2,%ebx
je     1286 <main+0x106>
lea    0xc(%rsp),%rsi
mov    $0x2,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x3,%ebx
je     1295 <main+0x115>
lea    0xc(%rsp),%rsi
mov    $0x3,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x4,%ebx
je     12a4 <main+0x124>
lea    0xc(%rsp),%rsi
mov    $0x4,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x5,%ebx
je     12b3 <main+0x133>
lea    0xc(%rsp),%rsi
mov    $0x5,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x6,%ebx
je     12c2 <main+0x142>
lea    0xc(%rsp),%rsi
mov    $0x6,%edi
call   10a0 <CPU_SET@plt>
cmp    $0x7,%ebx
jne    12c2 <main+0x142>
jmp    12d1 <main+0x151>
mov    $0x1,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x2,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x3,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x4,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x5,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x6,%edi
call   10a0 <CPU_SET@plt>
lea    0xc(%rsp),%rsi
mov    $0x7,%edi
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    0xd16(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xee6b2800,%ebx
lea    0xd19(%rip),%r14
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    1310 <main+0x190>

LLVM IR (-O0):
@.str = [17 x i8] c"wrong argument.\0A\00"
@.str.1 = [15 x i8] c"improper usage\00"
@.str.2 = [26 x i8] c"Setting pid %d to cpu %d\0A\00"
@.str.3 = [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, 2
br i1 %13, label %14, label %40
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp sge i32 %19, 8
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
23: ; preds = %14
store i64 0, i64* %7
br label %24
24: ; preds = %36, %23
%25 = load i64, i64* %7
%26 = icmp slt i64 %25, 8
br i1 %26, label %27, label %39
27: ; preds = %24
%28 = load i64, i64* %7
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %28, %30
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i64, i64* %7
%34 = call i32 @CPU_SET(i64 %33, i32* %10)
br label %35
35: ; preds = %32, %27
br label %36
36: ; preds = %35
%37 = load i64, i64* %7
%38 = add nsw i64 %37, 1
store i64 %38, i64* %7
br label %24
39: ; preds = %24
br label %42
40: ; preds = %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
42: ; preds = %39
%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.2, 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.3, i64 0, i64 0), i32 %58, i32 %59, i32 %60)
br label %47
62: ; preds = %40, %21
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"improper usage\00"
@.str.2 = [26 x i8] c"Setting pid %d to cpu %d\0A\00"
@.str.3 = [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)
%6 = icmp eq i32 %0, 2
br i1 %6, label %7, label %56
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @atoi(i8* %9)
%11 = icmp sgt i32 %10, 7
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = call i32 @CPU_SET(i64 1, i32* %3)
br label %24
16: ; preds = %7
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %64
18: ; preds = %12
%19 = call i32 @CPU_SET(i64 0, i32* %3)
%20 = icmp eq i32 %10, 1
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = call i32 @CPU_SET(i64 1, i32* %3)
%23 = icmp eq i32 %10, 2
br i1 %23, label %29, label %26
24: ; preds = %14, %18
%25 = call i32 @CPU_SET(i64 2, i32* %3)
br label %29
26: ; preds = %21
%27 = call i32 @CPU_SET(i64 2, i32* %3)
%28 = icmp eq i32 %10, 3
br i1 %28, label %34, label %31
29: ; preds = %24, %21
%30 = call i32 @CPU_SET(i64 3, i32* %3)
br label %34
31: ; preds = %26
%32 = call i32 @CPU_SET(i64 3, i32* %3)
%33 = icmp eq i32 %10, 4
br i1 %33, label %39, label %36
34: ; preds = %29, %26
%35 = call i32 @CPU_SET(i64 4, i32* %3)
br label %39
36: ; preds = %31
%37 = call i32 @CPU_SET(i64 4, i32* %3)
%38 = icmp eq i32 %10, 5
br i1 %38, label %44, label %41
39: ; preds = %34, %31
%40 = call i32 @CPU_SET(i64 5, i32* %3)
br label %44
41: ; preds = %36
%42 = call i32 @CPU_SET(i64 5, i32* %3)
%43 = icmp eq i32 %10, 6
br i1 %43, label %49, label %46
44: ; preds = %39, %36
%45 = call i32 @CPU_SET(i64 6, i32* %3)
br label %49
46: ; preds = %41
%47 = call i32 @CPU_SET(i64 6, i32* %3)
%48 = icmp eq i32 %10, 7
br i1 %48, label %51, label %49
49: ; preds = %41, %44, %46
%50 = call i32 @CPU_SET(i64 7, i32* %3)
br label %51
51: ; preds = %49, %46
%52 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%53 = call i32 (...) @getpid()
%54 = call i32 (...) @sched_getcpu()
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %53, i32 %54)
br label %58
56: ; preds = %2
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %64
58: ; preds = %58, %51
%59 = phi i32 [ 0, %51 ], [ %60, %58 ]
%60 = add i32 %59, -294967296
%61 = call i32 (...) @getpid()
%62 = call i32 (...) @sched_getcpu()
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %62, i32 %60)
br label %58
64: ; preds = %56, %16
ret i32 -1
}