#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 */
scalar_t__ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ sched_getaffinity(int /*<<< orphan*/, int,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
void display_process_affinity() {
  cpu_set_t set;
  int cpu_set_size = sizeof(cpu_set_t);
  int number_of_cpus = -1;
  int i = 0;
  unsigned long long int affinity_mask;
  number_of_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  if (number_of_cpus > 64) {
    fprintf(stderr, "display_process_affinity: More than 64 cores\n");
  }
  sched_getaffinity(0, cpu_set_size, &set);
  affinity_mask = 0;
  for (i = number_of_cpus - 1; i >= 0; i--) {
    affinity_mask = affinity_mask << 1;
    if (CPU_ISSET(i, &set))
      affinity_mask |= 1;
  }
  fprintf(stderr, "Affinity mask: %16llx\n", affinity_mask);
}


x86 assembly (clang, -O0):
<display_process_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x4,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x40,-0xc(%rbp)
jle    118f <display_process_affinity+0x4f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%esi
xor    %edi,%edi
lea    -0x4(%rbp),%rdx
call   1060 <sched_getaffinity@plt>
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11f9 <display_process_affinity+0xb9>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x4(%rbp),%rsi
call   1030 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     11e6 <display_process_affinity+0xa6>
mov    -0x18(%rbp),%rax
or     $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11eb <display_process_affinity+0xab>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11ae <display_process_affinity+0x6e>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_process_affinity>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,%ebx
cmp    $0x41,%eax
jl     1188 <display_process_affinity+0x48>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xc(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1060 <sched_getaffinity@plt>
jmp    11a0 <display_process_affinity+0x60>
xor    %r14d,%r14d
lea    0xc(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1060 <sched_getaffinity@plt>
test   %ebx,%ebx
jle    11cf <display_process_affinity+0x8f>
add    $0x1,%ebx
xor    %r14d,%r14d
lea    0xc(%rsp),%r15
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%edi
mov    %r15,%rsi
call   1030 <CPU_ISSET@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%r14,2),%r14
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     11b0 <display_process_affinity+0x70>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"display_process_affinity: More than 64 cores\0A\00"
@.str.1 = [23 x i8] c"Affinity mask: %16llx\0A\00"
define void @display_process_affinity() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 4, i32* %2
store i32 -1, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* @_SC_NPROCESSORS_ONLN
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 64
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %0
%14 = load i32, i32* %2
%15 = call i32 @sched_getaffinity(i32 0, i32 %14, i32* %1)
store i64 0, i64* %5
%16 = load i32, i32* %3
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %4
br label %18
18: ; preds = %31, %13
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i64, i64* %5
%23 = shl i64 %22, 1
store i64 %23, i64* %5
%24 = load i32, i32* %4
%25 = call i64 @CPU_ISSET(i32 %24, i32* %1)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i64, i64* %5
%29 = or i64 %28, 1
store i64 %29, i64* %5
br label %30
30: ; preds = %27, %21
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
br label %18
34: ; preds = %18
%35 = load i32, i32* @stderr
%36 = load i64, i64* %5
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %36)
ret void
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"display_process_affinity: More than 64 cores\0A\00"
@.str.1 = [23 x i8] c"Affinity mask: %16llx\0A\00"
define void @display_process_affinity() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @_SC_NPROCESSORS_ONLN
%4 = tail call i32 @sysconf(i32 %3)
%5 = icmp sgt i32 %4, 64
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
br label %13
10: ; preds = %0
%11 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
%12 = icmp sgt i32 %4, 0
br i1 %12, label %13, label %24
13: ; preds = %6, %10
br label %14
14: ; preds = %13, %14
%15 = phi i32 [ %17, %14 ], [ %4, %13 ]
%16 = phi i64 [ %22, %14 ], [ 0, %13 ]
%17 = add nsw i32 %15, -1
%18 = shl i64 %16, 1
%19 = call i64 @CPU_ISSET(i32 %17, i32* %1)
%20 = icmp ne i64 %19, 0
%21 = zext i1 %20 to i64
%22 = or i64 %18, %21
%23 = icmp ugt i32 %15, 1
br i1 %23, label %14, label %24
24: ; preds = %14, %10
%25 = phi i64 [ 0, %10 ], [ %22, %14 ]
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %25)
ret void
}