#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 pid_t;
typedef int /*<<< orphan*/ mask;

/* Variables and functions */
int /*<<< orphan*/ __NR_gettid;
int /*<<< orphan*/ __NR_sched_setaffinity;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int syscall(int /*<<< orphan*/, ...);
void pinCPU(int cpuID) {
  int mask = 0x00000001;
  while (cpuID--) {
    mask = mask << 1;
  }
  pid_t myself = syscall(__NR_gettid);
  fprintf(stderr, "[TIMERANDROID] Set thread %d pin on %X\n", myself, mask);
  int ret = syscall(__NR_sched_setaffinity, myself, sizeof(mask), &mask);
  if (ret != 0) {
    perror("sched_setaffinity");
    return;
  }
}


x86 assembly (clang, -O0):
<pinCPU>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     1164 <pinCPU+0x34>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1142 <pinCPU+0x12>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    $0x0,%al
call   1040 <syscall@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe73(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    $0x4,%edx
lea    -0x8(%rbp),%rcx
mov    $0x0,%al
call   1040 <syscall@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11c7 <pinCPU+0x97>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pinCPU>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
je     114b <pinCPU+0x1b>
lea    -0x1(%rdi),%ecx
mov    %edi,%eax
and    $0x7,%eax
cmp    $0x7,%ecx
jae    1152 <pinCPU+0x22>
mov    $0x1,%ebx
jmp    1168 <pinCPU+0x38>
mov    $0x1,%ebx
jmp    1177 <pinCPU+0x47>
and    $0xfffffff8,%edi
mov    $0x1,%ebx
nopw   0x0(%rax,%rax,1)
shl    $0x8,%ebx
add    $0xfffffff8,%edi
jne    1160 <pinCPU+0x30>
test   %eax,%eax
je     1177 <pinCPU+0x47>
nopl   0x0(%rax)
add    %ebx,%ebx
add    $0xffffffff,%eax
jne    1170 <pinCPU+0x40>
mov    %ebx,0x4(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1040 <syscall@plt>
mov    %eax,%ebp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1030 <fprintf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rcx
mov    $0x4,%edx
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <syscall@plt>
test   %eax,%eax
jne    11cd <pinCPU+0x9d>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
jmp    11c6 <pinCPU+0x96>

LLVM IR (-O0):
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"[TIMERANDROID] Set thread %d pin on %X\0A\00"
@__NR_sched_setaffinity = global i32 0
@.str.1 = [18 x i8] c"sched_setaffinity\00"
define void @pinCPU(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
br label %6
6: ; preds = %10, %1
%7 = load i32, i32* %2
%8 = add nsw i32 %7, -1
store i32 %8, i32* %2
%9 = icmp ne i32 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* %3
%12 = shl i32 %11, 1
store i32 %12, i32* %3
br label %6
13: ; preds = %6
%14 = load i32, i32* @__NR_gettid
%15 = call i32 (i32, ...) @syscall(i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* @stderr
%17 = load i32, i32* %4
%18 = load i32, i32* %3
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32, i32* @__NR_sched_setaffinity
%21 = load i32, i32* %4
%22 = call i32 (i32, ...) @syscall(i32 %20, i32 %21, i64 4, i32* %3)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %13
ret void
}

LLVM IR (-O2):
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"[TIMERANDROID] Set thread %d pin on %X\0A\00"
@__NR_sched_setaffinity = global i32 0
@.str.1 = [18 x i8] c"sched_setaffinity\00"
define void @pinCPU(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp eq i32 %0, 0
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = add i32 %0, -1
%7 = and i32 %0, 7
%8 = icmp ult i32 %6, 7
br i1 %8, label %17, label %9
9: ; preds = %5
%10 = and i32 %0, -8
br label %11
11: ; preds = %11, %9
%12 = phi i32 [ 1, %9 ], [ %14, %11 ]
%13 = phi i32 [ 0, %9 ], [ %15, %11 ]
%14 = shl i32 %12, 8
%15 = add i32 %13, 8
%16 = icmp eq i32 %15, %10
br i1 %16, label %17, label %11
17: ; preds = %11, %5
%18 = phi i32 [ undef, %5 ], [ %14, %11 ]
%19 = phi i32 [ 1, %5 ], [ %14, %11 ]
%20 = icmp eq i32 %7, 0
br i1 %20, label %27, label %21
21: ; preds = %17, %21
%22 = phi i32 [ %24, %21 ], [ %19, %17 ]
%23 = phi i32 [ %25, %21 ], [ 0, %17 ]
%24 = shl i32 %22, 1
%25 = add i32 %23, 1
%26 = icmp eq i32 %25, %7
br i1 %26, label %27, label %21
27: ; preds = %17, %21, %1
%28 = phi i32 [ 1, %1 ], [ %18, %17 ], [ %24, %21 ]
store i32 %28, i32* %2
%29 = load i32, i32* @__NR_gettid
%30 = tail call i32 (i32, ...) @syscall(i32 %29)
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %28)
%33 = load i32, i32* @__NR_sched_setaffinity
%34 = call i32 (i32, ...) @syscall(i32 %33, i32 %30, i64 4, i32* %2)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %27, %36
ret void
}