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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, long long);
long long rdtsc();
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int main(int argc, char *argv[]) {
  int cpu = 0;
  int loop = 99999;
  cpu_set_t cpumask;
  CPU_ZERO(&cpumask);
  if (argc == 2)
    sscanf(argv[1], "%d", &cpu);
  CPU_SET(cpu, &cpumask);
  if (sched_setaffinity(0, sizeof(cpumask), &cpumask) < 0) {
    perror("sched_setaffinity:");
  }
  while (loop--)
    ;
  long long tsc = rdtsc();
  printf("cpu%d:%llu\n", cpu, tsc);
  return 0;
}


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)
movl   $0x0,-0x14(%rbp)
movl   $0x1869f,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11bf <main+0x4f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x1c(%rbp),%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0x0,%rax
jge    11f1 <main+0x81>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
jmp    11f6 <main+0x86>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
je     120f <main+0x9f>
jmp    11f6 <main+0x86>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdee(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,(%rsp)
lea    0x4(%rsp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
cmp    $0x2,%ebp
jne    11a6 <main+0x36>
mov    0x8(%rbx),%rdi
lea    0xe65(%rip),%rsi
mov    %rsp,%rdx
call   1060 <sscanf@plt>
mov    (%rsp),%edi
lea    0x4(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <sched_setaffinity@plt>
test   %rax,%rax
js     11e9 <main+0x79>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    (%rsp),%esi
lea    0xe3e(%rip),%rdi
mov    %rax,%rdx
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
jmp    11c7 <main+0x57>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [19 x i8] c"sched_setaffinity:\00"
@.str.2 = [12 x i8] c"cpu%d:%llu\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 i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 99999, i32* %7
%10 = call i32 @CPU_ZERO(i32* %8)
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %18
18: ; preds = %13, %2
%19 = load i32, i32* %6
%20 = call i32 @CPU_SET(i32 %19, i32* %8)
%21 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
br label %26
26: ; preds = %30, %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, -1
store i32 %28, i32* %7
%29 = icmp ne i32 %27, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %26
31: ; preds = %26
%32 = call i64 (...) @rdtsc()
store i64 %32, i64* %9
%33 = load i32, i32* %6
%34 = load i64, i64* %9
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %33, i64 %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [19 x i8] c"sched_setaffinity:\00"
@.str.2 = [12 x i8] c"cpu%d:%llu\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
%7 = call i32 @CPU_ZERO(i32* %4)
%8 = icmp eq i32 %0, 2
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
br label %14
14: ; preds = %9, %2
%15 = phi i32 [ %13, %9 ], [ 0, %2 ]
%16 = call i32 @CPU_SET(i32 %15, i32* %4)
%17 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = call i64 (...) @rdtsc()
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23, i64 %22)
ret i32 0
}