#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 {
    for (j = 1; j < argc; j++) {
      core = atoi(argv[j]);
      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    1259 <main+0xe9>
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jge    1254 <main+0xe4>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x8,-0x28(%rbp)
jl     1237 <main+0xc7>
lea    0xddb(%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>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ef <main+0x7f>
jmp    1259 <main+0xe9>
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    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x77359400,-0x18(%rbp)
77
jge    12bd <main+0x14d>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1295 <main+0x125>
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    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128d <main+0x11d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x4(%rsp),%rdi
call   1070 <CPU_ZERO@plt>
cmp    $0x1,%ebp
jne    1207 <main+0x87>
lea    0x4(%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
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
jmp    1241 <main+0xc1>
jle    1241 <main+0xc1>
movslq %ebp,%rbp
mov    $0x1,%ebx
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rdi
call   1090 <atoi@plt>
cmp    $0x8,%eax
jge    12a8 <main+0x128>
mov    %eax,%edi
mov    %r14,%rsi
call   10a0 <CPU_SET@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1220 <main+0xa0>
lea    0x4(%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    0xd97(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xee6b2800,%ebx
lea    0xd9a(%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    1280 <main+0x100>
lea    0xd9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 %49
26: ; preds = %2
store i64 1, i64* %7
br label %27
27: ; preds = %45, %26
%28 = load i64, i64* %7
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = icmp slt i64 %28, %30
br i1 %31, label %32, label %48
32: ; preds = %27
%33 = load i8**, i8*** %5
%34 = load i64, i64* %7
%35 = getelementptr inbounds i8*, i8** %33, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp sge i32 %38, 8
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
ret i32 -1
42: ; preds = %32
%43 = load i32, i32* %9
%44 = call i32 @CPU_SET(i32 %43, i32* %10)
br label %45
45: ; preds = %42
%46 = load i64, i64* %7
%47 = add nsw i64 %46, 1
store i64 %47, i64* %7
br label %27
48: ; preds = %27
br label %49
49: ; preds = %48, %25
%50 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %10)
%51 = call i32 (...) @getpid()
%52 = call i32 (...) @sched_getcpu()
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %51, i32 %52)
br label %54
54: ; preds = %49, %64
store i64 0, i64* %6
br label %55
55: ; preds = %61, %54
%56 = load i64, i64* %6
%57 = icmp slt i64 %56, 2000000000
br i1 %57, label %58, label %64
58: ; preds = %55
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 2
store i32 %60, i32* %8
br label %61
61: ; preds = %58
%62 = load i64, i64* %6
%63 = add nsw i64 %62, 1
store i64 %63, i64* %6
br label %55
64: ; preds = %55
%65 = call i32 (...) @getpid()
%66 = call i32 (...) @sched_getcpu()
%67 = load i32, i32* %8
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %65, i32 %66, i32 %67)
br label %54
}

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)
%6 = icmp eq i32 %0, 1
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = call i32 @CPU_SET(i32 0, i32* %3)
%9 = call i32 @CPU_SET(i32 1, i32* %3)
%10 = call i32 @CPU_SET(i32 2, i32* %3)
%11 = call i32 @CPU_SET(i32 3, i32* %3)
%12 = call i32 @CPU_SET(i32 4, i32* %3)
%13 = call i32 @CPU_SET(i32 5, i32* %3)
%14 = call i32 @CPU_SET(i32 6, i32* %3)
%15 = call i32 @CPU_SET(i32 7, i32* %3)
br label %31
16: ; preds = %2
%17 = sext i32 %0 to i64
%18 = icmp sgt i32 %0, 1
br i1 %18, label %19, label %31
19: ; preds = %16, %27
%20 = phi i64 [ %29, %27 ], [ 1, %16 ]
%21 = getelementptr inbounds i8*, i8** %1, i64 %20
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = icmp sgt i32 %23, 7
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 -1
27: ; preds = %19
%28 = call i32 @CPU_SET(i32 %23, i32* %3)
%29 = add nuw nsw i64 %20, 1
%30 = icmp eq i64 %29, %17
br i1 %30, label %31, label %19
31: ; preds = %27, %7, %16
%32 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%33 = call i32 (...) @getpid()
%34 = call i32 (...) @sched_getcpu()
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %34)
br label %36
36: ; preds = %36, %31
%37 = phi i32 [ 0, %31 ], [ %38, %36 ]
%38 = add i32 %37, -294967296
%39 = call i32 (...) @getpid()
%40 = call i32 (...) @sched_getcpu()
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40, i32 %38)
br label %36
}