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

/* Variables and functions */
int /*<<< orphan*/ CPU_OR(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int set_affinities(pid_t pid, long cpuids) {
  int i = 0;
  cpu_set_t set;
  CPU_ZERO(&set);
  const int bit_size = sizeof(cpuids) * 8;
  for (i = 0; i < bit_size; ++i) {
    if ((1l << i) & cpuids) {
      cpu_set_t tmp;
      CPU_ZERO(&tmp);
      CPU_SET(i, &tmp);
      CPU_OR(&set, &tmp, &set);
    }
  }
  return sched_setaffinity(pid, sizeof(set), &set);
}


x86 assembly (clang, -O0):
<set_affinities>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
movl   $0x40,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x40,-0x14(%rbp)
jge    11ca <set_affinities+0x8a>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
and    -0x10(%rbp),%rax
cmp    $0x0,%rax
je     11b7 <set_affinities+0x77>
lea    -0x20(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <CPU_SET@plt>
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    %rdx,%rdi
call   1040 <CPU_OR@plt>
jmp    11bc <set_affinities+0x7c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116d <set_affinities+0x2d>
mov    -0x4(%rbp),%edi
mov    $0x4,%esi
lea    -0x18(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_affinities>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r12
mov    %edi,%r14d
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1050 <CPU_ZERO@plt>
xor    %ebp,%ebp
lea    0xc(%rsp),%rbx
jmp    117a <set_affinities+0x3a>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    $0x40,%rbp
je     11a2 <set_affinities+0x62>
bt     %rbp,%r12
jae    1170 <set_affinities+0x30>
mov    %rbx,%rdi
call   1050 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <CPU_SET@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <CPU_OR@plt>
jmp    1170 <set_affinities+0x30>
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
mov    $0x4,%esi
call   1030 <sched_setaffinity@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @set_affinities(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
store i32 0, i32* %5
%9 = call i32 @CPU_ZERO(i32* %6)
store i32 64, i32* %7
store i32 0, i32* %5
br label %10
10: ; preds = %26, %2
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 64
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i32, i32* %5
%15 = zext i32 %14 to i64
%16 = shl i64 1, %15
%17 = load i64, i64* %4
%18 = and i64 %16, %17
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = call i32 @CPU_ZERO(i32* %8)
%22 = load i32, i32* %5
%23 = call i32 @CPU_SET(i32 %22, i32* %8)
%24 = call i32 @CPU_OR(i32* %6, i32* %8, i32* %6)
br label %25
25: ; preds = %20, %13
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %10
29: ; preds = %10
%30 = load i32, i32* %3
%31 = call i32 @sched_setaffinity(i32 %30, i32 4, i32* %6)
ret i32 %31
}

LLVM IR (-O2):
define i32 @set_affinities(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = call i32 @CPU_ZERO(i32* %3)
%7 = bitcast i32* %4 to i8*
br label %8
8: ; preds = %2, %18
%9 = phi i64 [ 0, %2 ], [ %19, %18 ]
%10 = shl nuw i64 1, %9
%11 = and i64 %10, %1
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = call i32 @CPU_ZERO(i32* %4)
%15 = trunc i64 %9 to i32
%16 = call i32 @CPU_SET(i32 %15, i32* %4)
%17 = call i32 @CPU_OR(i32* %3, i32* %4, i32* %3)
br label %18
18: ; preds = %8, %13
%19 = add nuw nsw i64 %9, 1
%20 = icmp eq i64 %19, 64
br i1 %20, label %21, label %8
21: ; preds = %18
%22 = call i32 @sched_setaffinity(i32 %0, i32 4, i32* %3)
ret i32 %22
}