#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*/ cpus;
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 /*<<< orphan*/ sched_setaffinity(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sched_yield();
int strlen(char *);
int main(int argc, char *argv[]) {
  int i;
  int pid;
  int cpuid;
  cpuid = -1;
  if (argc >= 1) {
    for (i = 1; i < argc; i++) {
      if (strlen(argv[i]) > 3) {
        if (argv[i][1] == 'A') {
          cpuid = atoi(&argv[i][3]);
        }
      }
      if (strlen(argv[i]) >= 2) {
        if (argv[i][1] == '?' || argv[i][1] == 'h' || argv[i][1] == 'H') {
          printf("<usage>: busyloop -A=<cpuid>\n");
          printf("\t Default <cpuid> = -1 (no affinity)\n");
          printf("\t Test forever until you send kill -9 <pid> from a Separate "
                 "Shell\n");
          return 0;
        }
      }
    }
  }
  pid = getpid();
  printf("PID: %d\n", pid);
  if (cpuid >= 0) {
    printf("Affine CPU: %d\n", cpuid);
    cpu_set_t cpus;
    CPU_ZERO(&cpus);
    CPU_SET(cpuid, &cpus);
    sched_setaffinity(pid, sizeof(cpus), &cpus);
  }
  while (1) {
    sched_yield();
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     12be <main+0x13e>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12b9 <main+0x139>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
cmp    $0x3,%eax
jle    120a <main+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
cmp    $0x41,%eax
jne    1205 <main+0x85>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
add    $0x3,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    120a <main+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     12a6 <main+0x126>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
cmp    $0x3f,%eax
je     126f <main+0xef>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
cmp    $0x68,%eax
je     126f <main+0xef>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
cmp    $0x48,%eax
jne    12a1 <main+0x121>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    12a6 <main+0x126>
jmp    12ab <main+0x12b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <main+0x2e>
jmp    12be <main+0x13e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     131a <main+0x19a>
mov    -0x1c(%rbp),%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1080 <CPU_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <CPU_SET@plt>
mov    -0x18(%rbp),%edi
mov    $0x4,%esi
lea    -0x20(%rbp),%rdx
call   1060 <sched_setaffinity@plt>
jmp    131f <main+0x19f>
mov    $0x0,%al
call   1070 <sched_yield@plt>
jmp    131f <main+0x19f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
cmp    $0x2,%edi
jl     1256 <main+0xc6>
mov    %rsi,%r12
mov    %edi,%r15d
mov    $0xffffffff,%r14d
mov    $0x1,%ebx
movabs $0x20000000201,%r13
jmp    11d9 <main+0x49>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1256 <main+0xc6>
mov    (%r12,%rbx,8),%rdi
call   1050 <strlen@plt>
mov    (%r12,%rbx,8),%rbp
cmp    $0x4,%eax
jl     11fd <main+0x6d>
cmpb   $0x41,0x1(%rbp)
jne    11fd <main+0x6d>
lea    0x3(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jl     11d0 <main+0x40>
mov    (%r12,%rbx,8),%rax
movzbl 0x1(%rax),%eax
add    $0xc1,%al
cmp    $0x29,%al
ja     11d0 <main+0x40>
movzbl %al,%eax
bt     %rax,%r13
jae    11d0 <main+0x40>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
lea    0xd9a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r14d,%r14d
js     12b0 <main+0x120>
lea    0xd8e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <CPU_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10b0 <CPU_SET@plt>
mov    %ebx,%edi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1070 <sched_setaffinity@plt>
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <sched_yield@plt>
jmp    12b0 <main+0x120>

LLVM IR (-O0):
@.str = [30 x i8] c"<usage>: busyloop -A=<cpuid>\0A\00"
@.str.1 = [38 x i8] c"\09 Default <cpuid> = -1 (no affinity)\0A\00"
@.str.2 = [67 x i8] c"\09 Test forever until you send kill -9 <pid> from a Separate Shell\0A\00"
@.str.3 = [9 x i8] c"PID: %d\0A\00"
@.str.4 = [16 x i8] c"Affine CPU: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 1
br i1 %11, label %12, label %92
12: ; preds = %2
store i32 1, i32* %6
br label %13
13: ; preds = %88, %12
%14 = load i32, i32* %6
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %91
17: ; preds = %13
%18 = load i8**, i8*** %5
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i32 @strlen(i8* %22)
%24 = icmp sgt i32 %23, 3
br i1 %24, label %25, label %44
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 65
br i1 %34, label %35, label %43
35: ; preds = %25
%36 = load i8**, i8*** %5
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = getelementptr inbounds i8, i8* %40, i64 3
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %8
br label %43
43: ; preds = %35, %25
br label %44
44: ; preds = %43, %17
%45 = load i8**, i8*** %5
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
%49 = load i8*, i8** %48
%50 = call i32 @strlen(i8* %49)
%51 = icmp sge i32 %50, 2
br i1 %51, label %52, label %87
52: ; preds = %44
%53 = load i8**, i8*** %5
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 63
br i1 %61, label %82, label %62
62: ; preds = %52
%63 = load i8**, i8*** %5
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = getelementptr inbounds i8, i8* %67, i64 1
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 104
br i1 %71, label %82, label %72
72: ; preds = %62
%73 = load i8**, i8*** %5
%74 = load i32, i32* %6
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8*, i8** %73, i64 %75
%77 = load i8*, i8** %76
%78 = getelementptr inbounds i8, i8* %77, i64 1
%79 = load i8, i8* %78
%80 = sext i8 %79 to i32
%81 = icmp eq i32 %80, 72
br i1 %81, label %82, label %86
82: ; preds = %72, %62, %52
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
86: ; preds = %72
br label %87
87: ; preds = %86, %44
br label %88
88: ; preds = %87
%89 = load i32, i32* %6
%90 = add nsw i32 %89, 1
store i32 %90, i32* %6
br label %13
91: ; preds = %13
br label %92
92: ; preds = %91, %2
%93 = call i32 (...) @getpid()
store i32 %93, i32* %7
%94 = load i32, i32* %7
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %94)
%96 = load i32, i32* %8
%97 = icmp sge i32 %96, 0
br i1 %97, label %98, label %106
98: ; preds = %92
%99 = load i32, i32* %8
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %99)
%101 = call i32 @CPU_ZERO(i32* %9)
%102 = load i32, i32* %8
%103 = call i32 @CPU_SET(i32 %102, i32* %9)
%104 = load i32, i32* %7
%105 = call i32 @sched_setaffinity(i32 %104, i32 4, i32* %9)
br label %106
106: ; preds = %98, %92
br label %107
107: ; preds = %106, %107
%108 = call i32 (...) @sched_yield()
br label %107
}

LLVM IR (-O2):
@.str.3 = [9 x i8] c"PID: %d\0A\00"
@.str.4 = [16 x i8] c"Affine CPU: %d\0A\00"
@str = [29 x i8] c"<usage>: busyloop -A=<cpuid>\00"
@str.5 = [37 x i8] c"\09 Default <cpuid> = -1 (no affinity)\00"
@str.6 = [66 x i8] c"\09 Test forever until you send kill -9 <pid> from a Separate Shell\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %37
5: ; preds = %2
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %5, %34
%8 = phi i64 [ 1, %5 ], [ %35, %34 ]
%9 = phi i32 [ -1, %5 ], [ %23, %34 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %8
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 3
%14 = load i8*, i8** %10
br i1 %13, label %15, label %22
15: ; preds = %7
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 65
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %14, i64 3
%21 = tail call i32 @atoi(i8* %20)
br label %22
22: ; preds = %7, %15, %19
%23 = phi i32 [ %21, %19 ], [ %9, %15 ], [ %9, %7 ]
%24 = tail call i32 @strlen(i8* %14)
%25 = icmp sgt i32 %24, 1
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
switch i8 %29, label %34 [
i8 63, label %30
i8 104, label %30
i8 72, label %30
]
30: ; preds = %26, %26, %26
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str.6, i64 0, i64 0))
ret i32 0
34: ; preds = %26, %22
%35 = add nuw nsw i64 %8, 1
%36 = icmp eq i64 %35, %6
br i1 %36, label %37, label %7
37: ; preds = %34, %2
%38 = phi i32 [ -1, %2 ], [ %23, %34 ]
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = icmp sgt i32 %38, -1
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%44 = bitcast i32* %3 to i8*
%45 = call i32 @CPU_ZERO(i32* %3)
%46 = call i32 @CPU_SET(i32 %38, i32* %3)
%47 = call i32 @sched_setaffinity(i32 %39, i32 4, i32* %3)
br label %48
48: ; preds = %42, %37
br label %49
49: ; preds = %48, %49
%50 = call i32 (...) @sched_yield()
br label %49
}