#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 */
int CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int, int);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
void setAffinity(unsigned cpu_mask) {
  int cpu = 0;
  cpu_set_t cpu_set;
  CPU_ZERO(&cpu_set);
  for (cpu = 0; cpu < 32; ++cpu) {
    if ((1 << cpu) & cpu_mask) {
      CPU_SET(cpu, &cpu_set);
    }
  }
  printf("setAffinity: %d,%d,%d,%d\n", CPU_ISSET(0, &cpu_set),
         CPU_ISSET(1, &cpu_set), CPU_ISSET(2, &cpu_set),
         CPU_ISSET(3, &cpu_set));
  int rc = sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set);
  if (rc != 0) {
    perror("sched_set_affinity");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<setAffinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x20,-0x8(%rbp)
jge    11d1 <setAffinity+0x61>
mov    -0x8(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
and    -0x4(%rbp),%eax
cmp    $0x0,%eax
je     11be <setAffinity+0x4e>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <CPU_SET@plt>
jmp    11c3 <setAffinity+0x53>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1192 <setAffinity+0x22>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1,%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,-0x18(%rbp)
mov    $0x2,%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,-0x14(%rbp)
mov    $0x3,%edi
lea    -0xc(%rbp),%rsi
call   1050 <CPU_ISSET@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    %eax,%r8d
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     125a <setAffinity+0xea>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setAffinity>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1060 <CPU_ZERO@plt>
xor    %ebp,%ebp
jmp    1198 <setAffinity+0x28>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x20,%ebp
je     11a9 <setAffinity+0x39>
bt     %ebp,%ebx
jae    1190 <setAffinity+0x20>
mov    %ebp,%edi
mov    %r14,%rsi
call   1090 <CPU_SET@plt>
jmp    1190 <setAffinity+0x20>
lea    0x4(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,%r14d
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,%r15d
mov    $0x2,%edi
mov    %rbx,%rsi
call   1050 <CPU_ISSET@plt>
mov    %eax,%ebp
mov    $0x3,%edi
mov    %rbx,%rsi
call   1050 <CPU_ISSET@plt>
lea    0xe12(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1030 <printf@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1040 <sched_setaffinity@plt>
test   %eax,%eax
jne    121c <setAffinity+0xac>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"setAffinity: %d,%d,%d,%d\0A\00"
@.str.1 = [19 x i8] c"sched_set_affinity\00"
define void @setAffinity(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%6 = call i32 @CPU_ZERO(i32* %4)
store i32 0, i32* %3
br label %7
7: ; preds = %20, %1
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 32
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = load i32, i32* %3
%12 = shl i32 1, %11
%13 = load i32, i32* %2
%14 = and i32 %12, %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %3
%18 = call i32 @CPU_SET(i32 %17, i32* %4)
br label %19
19: ; preds = %16, %10
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %7
23: ; preds = %7
%24 = call i32 @CPU_ISSET(i32 0, i32* %4)
%25 = call i32 @CPU_ISSET(i32 1, i32* %4)
%26 = call i32 @CPU_ISSET(i32 2, i32* %4)
%27 = call i32 @CPU_ISSET(i32 3, i32* %4)
%28 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25, i32 %26, i32 %27)
%29 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %4)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"setAffinity: %d,%d,%d,%d\0A\00"
@.str.1 = [19 x i8] c"sched_set_affinity\00"
define void @setAffinity(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @CPU_ZERO(i32* %2)
br label %5
5: ; preds = %1, %12
%6 = phi i32 [ 0, %1 ], [ %13, %12 ]
%7 = shl nuw i32 1, %6
%8 = and i32 %7, %0
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = call i32 @CPU_SET(i32 %6, i32* %2)
br label %12
12: ; preds = %5, %10
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 32
br i1 %14, label %15, label %5
15: ; preds = %12
%16 = call i32 @CPU_ISSET(i32 0, i32* %2)
%17 = call i32 @CPU_ISSET(i32 1, i32* %2)
%18 = call i32 @CPU_ISSET(i32 2, i32* %2)
%19 = call i32 @CPU_ISSET(i32 3, i32* %2)
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i32 %18, i32 %19)
%21 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
ret void
}