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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ sched_getaffinity(int, int, int /*<<< orphan*/ *);
scalar_t__ sched_setaffinity(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
int main() {
  cpu_set_t cset;
  int i = 0;
  int numOfCPUs = sysconf(_SC_NPROCESSORS_ONLN);
  pid_t ret = fork();
  if (ret < 0) {
    perror("Error forking a child: ");
    return -1;
  } else if (ret == 0) {
    prctl(PR_SET_PDEATHSIG, SIGHUP);
    while (1) {
      printf("Child: %d\n", i);
      sleep(1);
      i++;
    }
  } else {
    while (1) {
      i = i % numOfCPUs;
      CPU_ZERO(&cset);
      CPU_SET(i, &cset);
      printf("Trying to set CPU affinity for (%d) to (%d)\n", ret, i);
      if (sched_setaffinity(ret, sizeof(cset), &cset) < 0) {
        perror("Error setting CPU affinity for child");
      } else {
        printf("CPU affinity for (%d) set to (%d)\n", ret, i);
      }
      sleep(1);
      CPU_ZERO(&cset);
      printf("Trying to read CPU affinity for (%d)\n", ret);
      if (sched_getaffinity(ret, sizeof(cset), &cset) < 0) {
        perror("Error getting CPU affinity for child");
      } else {
        printf("CPU affinity for (%d) is (%d)\n", ret, i);
      }
      sleep(1);
      i++;
    }
    wait(&i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f2 <main+0x52>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbp
ret
cmpl   $0x0,-0x14(%rbp)
jne    123f <main+0x9f>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
mov    -0xc(%rbp),%esi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    %eax,-0xc(%rbp)
jmp    1213 <main+0x73>
jmp    1244 <main+0xa4>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10a0 <CPU_SET@plt>
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
cmp    $0x0,%rax
jge    12a3 <main+0x103>
lea    0xdb7(%rip),%rdi
call   1070 <perror@plt>
jmp    12b7 <main+0x117>
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    -0x8(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x14(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1090 <sched_getaffinity@plt>
cmp    $0x0,%rax
jge    1307 <main+0x167>
lea    0xdc1(%rip),%rdi
call   1070 <perror@plt>
jmp    131b <main+0x17b>
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1244 <main+0xa4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     12fb <main+0x15b>
je     12ba <main+0x11a>
mov    %eax,%ebp
lea    0x4(%rsp),%rbx
lea    0xe44(%rip),%r15
lea    0xeb2(%rip),%r13
xor    %r12d,%r12d
jmp    1211 <main+0x71>
nopl   0x0(%rax,%rax,1)
lea    0xeec(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0x1,%r12d
mov    %r12d,%eax
cltd
idiv   %r14d
mov    %edx,%r12d
mov    %rbx,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10a0 <CPU_SET@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1040 <sched_setaffinity@plt>
test   %rax,%rax
jns    1260 <main+0xc0>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
jmp    1273 <main+0xd3>
nop
lea    0xe0e(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbx,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1090 <sched_getaffinity@plt>
test   %rax,%rax
jns    11f0 <main+0x50>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    1203 <main+0x63>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
lea    0xd40(%rip),%rbx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0x1,%ebp
jmp    12e0 <main+0x140>
lea    0xcfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [24 x i8] c"Error forking a child: \00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.1 = [11 x i8] c"Child: %d\0A\00"
@.str.2 = [45 x i8] c"Trying to set CPU affinity for (%d) to (%d)\0A\00"
@.str.3 = [37 x i8] c"Error setting CPU affinity for child\00"
@.str.4 = [35 x i8] c"CPU affinity for (%d) set to (%d)\0A\00"
@.str.5 = [38 x i8] c"Trying to read CPU affinity for (%d)\0A\00"
@.str.6 = [37 x i8] c"Error getting CPU affinity for child\00"
@.str.7 = [31 x i8] c"CPU affinity for (%d) is (%d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%6 = load i32, i32* @_SC_NPROCESSORS_ONLN
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
ret i32 -1
13: ; preds = %0
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = load i32, i32* @PR_SET_PDEATHSIG
%18 = load i32, i32* @SIGHUP
%19 = call i32 @prctl(i32 %17, i32 %18)
br label %20
20: ; preds = %16, %20
%21 = load i32, i32* %3
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @sleep(i32 1)
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %20
26: ; preds = %13
br label %27
27: ; preds = %26, %60
%28 = load i32, i32* %3
%29 = load i32, i32* %4
%30 = srem i32 %28, %29
store i32 %30, i32* %3
%31 = call i32 @CPU_ZERO(i32* %2)
%32 = load i32, i32* %3
%33 = call i32 @CPU_SET(i32 %32, i32* %2)
%34 = load i32, i32* %5
%35 = load i32, i32* %3
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %35)
%37 = load i32, i32* %5
%38 = call i64 @sched_setaffinity(i32 %37, i32 4, i32* %2)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %46
42: ; preds = %27
%43 = load i32, i32* %5
%44 = load i32, i32* %3
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %43, i32 %44)
br label %46
46: ; preds = %42, %40
%47 = call i32 @sleep(i32 1)
%48 = call i32 @CPU_ZERO(i32* %2)
%49 = load i32, i32* %5
%50 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %5
%52 = call i64 @sched_getaffinity(i32 %51, i32 4, i32* %2)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %60
56: ; preds = %46
%57 = load i32, i32* %5
%58 = load i32, i32* %3
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %57, i32 %58)
br label %60
60: ; preds = %56, %54
%61 = call i32 @sleep(i32 1)
%62 = load i32, i32* %3
%63 = add nsw i32 %62, 1
store i32 %63, i32* %3
br label %27
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@.str = [24 x i8] c"Error forking a child: \00"
@PR_SET_PDEATHSIG = global i32 0
@SIGHUP = global i32 0
@.str.1 = [11 x i8] c"Child: %d\0A\00"
@.str.2 = [45 x i8] c"Trying to set CPU affinity for (%d) to (%d)\0A\00"
@.str.3 = [37 x i8] c"Error setting CPU affinity for child\00"
@.str.4 = [35 x i8] c"CPU affinity for (%d) set to (%d)\0A\00"
@.str.5 = [38 x i8] c"Trying to read CPU affinity for (%d)\0A\00"
@.str.6 = [37 x i8] c"Error getting CPU affinity for child\00"
@.str.7 = [31 x i8] c"CPU affinity for (%d) is (%d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @_SC_NPROCESSORS_ONLN
%4 = tail call i32 @sysconf(i32 %3)
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
ret i32 -1
9: ; preds = %0
%10 = icmp eq i32 %5, 0
br i1 %10, label %11, label %20
11: ; preds = %9
%12 = load i32, i32* @PR_SET_PDEATHSIG
%13 = load i32, i32* @SIGHUP
%14 = tail call i32 @prctl(i32 %12, i32 %13)
br label %15
15: ; preds = %15, %11
%16 = phi i32 [ 0, %11 ], [ %19, %15 ]
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = tail call i32 @sleep(i32 1)
%19 = add nuw nsw i32 %16, 1
br label %15
20: ; preds = %9, %42
%21 = phi i32 [ %44, %42 ], [ 0, %9 ]
%22 = srem i32 %21, %4
%23 = call i32 @CPU_ZERO(i32* %1)
%24 = call i32 @CPU_SET(i32 %22, i32* %1)
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %22)
%26 = call i64 @sched_setaffinity(i32 %5, i32 4, i32* %1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %20
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %5, i32 %22)
br label %32
32: ; preds = %30, %28
%33 = call i32 @sleep(i32 1)
%34 = call i32 @CPU_ZERO(i32* %1)
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %5)
%36 = call i64 @sched_getaffinity(i32 %5, i32 4, i32* %1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %42
40: ; preds = %32
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %5, i32 %22)
br label %42
42: ; preds = %40, %38
%43 = call i32 @sleep(i32 1)
%44 = add nsw i32 %22, 1
br label %20
}