#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*/ cpuset;
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 fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) {
  int pid;
  cpu_set_t cpuset;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  }
  if (pid) {
    return;
  }
  CPU_ZERO(&cpuset);
  CPU_SET(cpu, &cpuset);
  if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
    perror("sched_setaffinity");
    exit(1);
  }
  fn(arg);
  exit(0);
}


x86 assembly (clang, -O0):
<start_process_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119e <start_process_on+0x3e>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11ae <start_process_on+0x4e>
add    $0x20,%rsp
pop    %rbp
ret
lea    -0x20(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x20(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     11f4 <start_process_on+0x94>
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<start_process_on>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     118d <start_process_on+0x2d>
cmp    $0xffffffff,%eax
je     11c6 <start_process_on+0x66>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <CPU_ZERO@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r12,%rdx
call   1030 <sched_setaffinity@plt>
test   %rax,%rax
jne    11cf <start_process_on+0x6f>
mov    %r14,%rdi
call   *%r15
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe33(%rip),%rdi
jmp    11d6 <start_process_on+0x76>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

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