#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*/ 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*/ _SC_NPROCESSORS_CONF;
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sysconf(int /*<<< orphan*/);
int main() {
  int i = 0;
  int pid;
  int no_processors;
  no_processors = sysconf(_SC_NPROCESSORS_CONF);
  printf("System has %d processors.\n", no_processors);
  while (i < no_processors - 1) {
    pid = fork();
    if (pid == 0) {
      printf("\n In child\n");
      break;
    } else
      i++;
  }
  cpu_set_t cset;
  CPU_ZERO(&cset);
  CPU_SET(i, &cset);
  if (sched_setaffinity(0, sizeof(cset), &cset) == -1)
    perror("CPU Affinity...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11ee <main+0x7e>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11e0 <main+0x70>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ee <main+0x7e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a8 <main+0x38>
lea    -0x14(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1080 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1228 <main+0xb8>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebp
lea    0xe66(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x2,%ebp
jl     11d2 <main+0x52>
add    $0xffffffff,%ebp
xor    %ebx,%ebx
nop
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11c6 <main+0x46>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11b0 <main+0x30>
mov    %ebp,%ebx
jmp    11d2 <main+0x52>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
je     1206 <main+0x86>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    11fd <main+0x7d>

LLVM IR (-O0):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [27 x i8] c"System has %d processors.\0A\00"
@.str.1 = [12 x i8] c"\0A In child\0A\00"
@.str.2 = [17 x i8] c"CPU Affinity...\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* %2
%6 = load i32, i32* @_SC_NPROCESSORS_CONF
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %24, %0
%11 = load i32, i32* %2
%12 = load i32, i32* %4
%13 = sub nsw i32 %12, 1
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = call i32 (...) @fork()
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %25
21: ; preds = %15
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %24
24: ; preds = %21
br label %10
25: ; preds = %19, %10
%26 = call i32 @CPU_ZERO(i32* %5)
%27 = load i32, i32* %2
%28 = call i32 @CPU_SET(i32 %27, i32* %5)
%29 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@_SC_NPROCESSORS_CONF = global i32 0
@.str = [27 x i8] c"System has %d processors.\0A\00"
@.str.2 = [17 x i8] c"CPU Affinity...\0A\00"
@str = [11 x i8] c"\0A In child\00"
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @_SC_NPROCESSORS_CONF
%3 = tail call i32 @sysconf(i32 %2)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = add i32 %3, -1
%6 = icmp sgt i32 %3, 1
br i1 %6, label %7, label %16
7: ; preds = %0, %13
%8 = phi i32 [ %14, %13 ], [ 0, %0 ]
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %16
13: ; preds = %7
%14 = add nuw nsw i32 %8, 1
%15 = icmp eq i32 %14, %5
br i1 %15, label %16, label %7
16: ; preds = %13, %0, %11
%17 = phi i32 [ %8, %11 ], [ 0, %0 ], [ %5, %13 ]
%18 = bitcast i32* %1 to i8*
%19 = call i32 @CPU_ZERO(i32* %1)
%20 = call i32 @CPU_SET(i32 %17, i32* %1)
%21 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
ret i32 0
}