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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_attr_setaffinity_np(int /*<<< orphan*/ *, int,
                                       int /*<<< orphan*/ *);
scalar_t__ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          void *(*)(void *), void *);
void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu) {
  pthread_t tid;
  cpu_set_t cpuset;
  pthread_attr_t attr;
  CPU_ZERO(&cpuset);
  CPU_SET(cpu, &cpuset);
  pthread_attr_init(&attr);
  if (pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset)) {
    perror("pthread_attr_setaffinity_np");
    exit(1);
  }
  if (pthread_create(&tid, &attr, fn, arg)) {
    perror("pthread_create");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<start_thread_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <CPU_SET@plt>
lea    -0x24(%rbp),%rdi
call   1040 <pthread_attr_init@plt>
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
lea    -0x20(%rbp),%rdx
call   1030 <pthread_attr_setaffinity_np@plt>
cmp    $0x0,%rax
je     11d5 <start_thread_on+0x65>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <pthread_create@plt>
cmp    $0x0,%rax
je     120a <start_thread_on+0x9a>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_thread_on>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1060 <CPU_ZERO@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <CPU_SET@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <pthread_attr_init@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r12,%rdx
call   1030 <pthread_attr_setaffinity_np@plt>
test   %rax,%rax
jne    11e4 <start_thread_on+0x74>
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <pthread_create@plt>
test   %rax,%rax
jne    11ed <start_thread_on+0x7d>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe15(%rip),%rdi
jmp    11f4 <start_thread_on+0x84>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"pthread_attr_setaffinity_np\00"
@.str.1 = [15 x i8] c"pthread_create\00"
define void @start_thread_on(i8* (i8*)* %0, i8* %1, i64 %2) {
%4 = alloca i8* (i8*)*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* (i8*)* %0, i8* (i8*)** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = call i32 @CPU_ZERO(i32* %8)
%11 = load i64, i64* %6
%12 = call i32 @CPU_SET(i64 %11, i32* %8)
%13 = call i32 @pthread_attr_init(i32* %9)
%14 = call i64 @pthread_attr_setaffinity_np(i32* %9, i32 4, i32* %8)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = load i8* (i8*)*, i8* (i8*)** %4
%21 = load i8*, i8** %5
%22 = call i64 @pthread_create(i32* %7, i32* %9, i8* (i8*)* %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"pthread_attr_setaffinity_np\00"
@.str.1 = [15 x i8] c"pthread_create\00"
define void @start_thread_on(i8* (i8*)* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = call i32 @CPU_ZERO(i32* %5)
%11 = call i32 @CPU_SET(i64 %2, i32* %5)
%12 = call i32 @pthread_attr_init(i32* %6)
%13 = call i64 @pthread_attr_setaffinity_np(i32* %6, i32 4, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = call i64 @pthread_create(i32* %4, i32* %6, i8* (i8*)* %0, i8* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %18
ret void
}