#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_S(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ __CPU_ZERO_S(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setaffinity_np(int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int setaffinity_pthreads(int num) {
  cpu_set_t mask;
  if (num > 128 || num < 0) {
    return -1;
  }
  __CPU_ZERO_S(sizeof(cpu_set_t), &mask);
  __CPU_SET_S(num, sizeof(cpu_set_t), &mask);
  if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
    fprintf(stderr, "Error: pthread_setaffinity_np in thread %i\n", num);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setaffinity_pthreads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x80,-0x8(%rbp)
jg     1172 <setaffinity_pthreads+0x22>
cmpl   $0x0,-0x8(%rbp)
jge    117e <setaffinity_pthreads+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <setaffinity_pthreads+0x99>
mov    $0x4,%edi
lea    -0xc(%rbp),%rsi
call   1050 <__CPU_ZERO_S@plt>
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1070 <__CPU_SET_S@plt>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
jge    11e2 <setaffinity_pthreads+0x92>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe2f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <setaffinity_pthreads+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setaffinity_pthreads>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebp
cmp    $0x80,%edi
ja     11c1 <setaffinity_pthreads+0x71>
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    $0x4,%edi
mov    %r14,%rsi
call   1050 <__CPU_ZERO_S@plt>
mov    %ebx,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1070 <__CPU_SET_S@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <pthread_self@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <pthread_setaffinity_np@plt>
test   %rax,%rax
jns    11c1 <setaffinity_pthreads+0x71>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"Error: pthread_setaffinity_np in thread %i\0A\00"
define i32 @setaffinity_pthreads(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sgt i32 %5, 128
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %7, %1
store i32 -1, i32* %2
br label %23
11: ; preds = %7
%12 = call i32 @__CPU_ZERO_S(i32 4, i32* %4)
%13 = load i32, i32* %3
%14 = call i32 @__CPU_SET_S(i32 %13, i32 4, i32* %4)
%15 = call i32 (...) @pthread_self()
%16 = call i64 @pthread_setaffinity_np(i32 %15, i32 4, i32* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = load i32, i32* %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 -1, i32* %2
br label %23
22: ; preds = %11
store i32 0, i32* %2
br label %23
23: ; preds = %22, %18, %10
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"Error: pthread_setaffinity_np in thread %i\0A\00"
define i32 @setaffinity_pthreads(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp ugt i32 %0, 128
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = call i32 @__CPU_ZERO_S(i32 4, i32* %2)
%7 = call i32 @__CPU_SET_S(i32 %0, i32 4, i32* %2)
%8 = call i32 (...) @pthread_self()
%9 = call i64 @pthread_setaffinity_np(i32 %8, i32 4, i32* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %14
14: ; preds = %5, %1, %11
%15 = phi i32 [ -1, %11 ], [ -1, %1 ], [ 0, %5 ]
ret i32 %15
}