#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 /*<<< orphan*/ CPU_SET(long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int errno;
scalar_t__ sched_setaffinity(long, int, int /*<<< orphan*/ *);
int set_cpu_affinity(long pid, long *cpu_affinity, int seq_len) {
  cpu_set_t cpu_set;
  int i;
  CPU_ZERO(&cpu_set);
  for (i = 0; i < seq_len; i++) {
    CPU_SET(cpu_affinity[i], &cpu_set);
  }
  if (sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_set)) {
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_cpu_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1182 <set_cpu_affinity+0x52>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <CPU_SET@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1153 <set_cpu_affinity+0x23>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
lea    -0x20(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     11af <set_cpu_affinity+0x7f>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <set_cpu_affinity+0x86>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cpu_affinity>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
call   1040 <CPU_ZERO@plt>
test   %ebp,%ebp
jle    1175 <set_cpu_affinity+0x45>
mov    %ebp,%ebp
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rdi
mov    %r15,%rsi
call   1050 <CPU_SET@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <set_cpu_affinity+0x30>
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
mov    $0x4,%esi
call   1030 <sched_setaffinity@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     119a <set_cpu_affinity+0x6a>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
define i32 @set_cpu_affinity(i64 %0, i64* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @CPU_ZERO(i32* %4)
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %17
8: ; preds = %3
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %15, %10 ]
%12 = getelementptr inbounds i64, i64* %1, i64 %11
%13 = load i64, i64* %12
%14 = call i32 @CPU_SET(i64 %13, i32* %4)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %9
br i1 %16, label %17, label %10
17: ; preds = %10, %3
%18 = call i64 @sched_setaffinity(i64 %0, i32 4, i32* %4)
%19 = icmp eq i64 %18, 0
%20 = load i32, i32* @errno
%21 = select i1 %19, i32 0, i32 %20
ret i32 %21
}