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

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int set_affinity(int processor) {
  extern int sched_getaffinity();
  extern int sched_setaffinity();
  unsigned long new_mask;
  unsigned int len = sizeof(new_mask);
  unsigned long cur_mask;
  pid_t p = 0;
  int ret;
  new_mask = 1 << (processor);
  ret = sched_getaffinity(p, len, &cur_mask);
  if (ret != 0)
    abort();
  ret = sched_setaffinity(p, len, &new_mask);
  if (ret != 0)
    abort();
  ret = sched_getaffinity(p, len, &cur_mask);
  if (ret != 0)
    abort();
  if (cur_mask != new_mask) {
    printf("affinity did not get set! exiting\n");
    exit(-1);
  }
  fflush(stdout);
  return 0;
}


x86 assembly (clang, -O0):
<set_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x8,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1080 <sched_getaffinity@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11ae <set_affinity+0x4e>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1050 <sched_setaffinity@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11d3 <set_affinity+0x73>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1080 <sched_getaffinity@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11f8 <set_affinity+0x98>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     121c <set_affinity+0xbc>
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_affinity>:
sub    $0x18,%rsp
mov    %edi,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x8,%esi
xor    %eax,%eax
call   1080 <sched_getaffinity@plt>
test   %eax,%eax
jne    11da <set_affinity+0x7a>
lea    0x10(%rsp),%rdx
xor    %edi,%edi
mov    $0x8,%esi
xor    %eax,%eax
call   1050 <sched_setaffinity@plt>
test   %eax,%eax
jne    11da <set_affinity+0x7a>
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    $0x8,%esi
xor    %eax,%eax
call   1080 <sched_getaffinity@plt>
test   %eax,%eax
jne    11da <set_affinity+0x7a>
mov    0x8(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    11e1 <set_affinity+0x81>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"affinity did not get set! exiting\0A\00"
@stdout = global i32 0
define i32 @set_affinity(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 8, i32* %4
store i32 0, i32* %6
%8 = load i32, i32* %2
%9 = shl i32 1, %8
%10 = sext i32 %9 to i64
store i64 %10, i64* %3
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_getaffinity to i32 (i32, i32, i64*, ...)*)(i32 %11, i32 %12, i64* %5)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %1
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_setaffinity to i32 (i32, i32, i64*, ...)*)(i32 %19, i32 %20, i64* %3)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 (...) @abort()
unreachable
26: ; preds = %18
%27 = load i32, i32* %6
%28 = load i32, i32* %4
%29 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_getaffinity to i32 (i32, i32, i64*, ...)*)(i32 %27, i32 %28, i64* %5)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %26
%35 = load i64, i64* %5
%36 = load i64, i64* %3
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %34
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@str = [34 x i8] c"affinity did not get set! exiting\00"
define i32 @set_affinity(i32 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = shl nuw i32 1, %0
%7 = sext i32 %6 to i64
store i64 %7, i64* %2
%8 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_getaffinity to i32 (i32, i32, i64*, ...)*)(i32 0, i32 8, i64* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %1
%13 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_setaffinity to i32 (i32, i32, i64*, ...)*)(i32 0, i32 8, i64* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %12
%18 = call i32 (i32, i32, i64*, ...) bitcast (i32 (...)* @sched_getaffinity to i32 (i32, i32, i64*, ...)*)(i32 0, i32 8, i64* %3)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %17
%23 = load i64, i64* %3
%24 = load i64, i64* %2
%25 = icmp eq i64 %23, %24
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
ret i32 0
}