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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int errno;
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int setaffinity(int thread_id, int *errno_var) {
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(thread_id, &mask);
  if (0 != sched_setaffinity(0, sizeof(mask), &mask)) {
    *errno_var = errno;
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<setaffinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1050 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     118d <setaffinity+0x5d>
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <setaffinity+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setaffinity>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <CPU_SET@plt>
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <sched_setaffinity@plt>
test   %rax,%rax
je     117b <setaffinity+0x4b>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @setaffinity(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%7 = call i32 @CPU_ZERO(i32* %6)
%8 = load i32, i32* %4
%9 = call i32 @CPU_SET(i32 %8, i32* %6)
%10 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %6)
%11 = icmp ne i64 0, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = load i32*, i32** %5
store i32 %13, i32* %14
store i32 -1, i32* %3
br label %16
15: ; preds = %2
store i32 0, i32* %3
br label %16
16: ; preds = %15, %12
%17 = load i32, i32* %3
ret i32 %17
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @setaffinity(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @CPU_ZERO(i32* %3)
%6 = call i32 @CPU_SET(i32 %0, i32* %3)
%7 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = load i32, i32* @errno
store i32 %10, i32* %1
br label %11
11: ; preds = %2, %9
%12 = phi i32 [ -1, %9 ], [ 0, %2 ]
ret i32 %12
}