#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*/ cpu_set_t;

/* Variables and functions */
scalar_t__ CPU_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ info(char *, int);
int sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int set_cpu_affinity(cpu_set_t *cpu_set) {
  int status, i;
  cpu_set_t current_mask, new_mask;
  status = sched_getaffinity(0, sizeof(cpu_set_t), &current_mask);
  if (status) {
    error("set_cpu_affinity: getting CPU affinity mask: 0x%x\n", status);
    return 1;
  }
  for (i = 0; i < sizeof(cpu_set_t) * 8; i++) {
    if (CPU_ISSET(i, &current_mask))
      break;
  }
  if (i >= sizeof(cpu_set_t) * 8) {
    error("set_cpu_affinity: No schedulable CPU found!\n");
    return 1;
  }
  CPU_ZERO(&new_mask);
  CPU_SET(i, &new_mask);
  status = sched_setaffinity(0, sizeof(cpu_set_t), &new_mask);
  if (status) {
    error("set_cpu_affinity: setting CPU affinity mask: 0x%x\n", status);
    return 1;
  }
  info("set_cpu_affinity: using processr %d\n", i);
  *cpu_set = new_mask;
  return 0;
}


x86 assembly (clang, -O0):
<set_cpu_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x1c(%rbp),%rdx
call   1070 <sched_getaffinity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b6 <set_cpu_affinity+0x46>
mov    -0x14(%rbp),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <set_cpu_affinity+0x11f>
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x20,%rax
jae    11f9 <set_cpu_affinity+0x89>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <CPU_ISSET@plt>
cmp    $0x0,%rax
je     11e6 <set_cpu_affinity+0x76>
jmp    11f9 <set_cpu_affinity+0x89>
jmp    11eb <set_cpu_affinity+0x7b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bd <set_cpu_affinity+0x4d>
movslq -0x18(%rbp),%rax
cmp    $0x20,%rax
jb     1221 <set_cpu_affinity+0xb1>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <set_cpu_affinity+0x11f>
lea    -0x20(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x20(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1270 <set_cpu_affinity+0x100>
mov    -0x14(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <set_cpu_affinity+0x11f>
mov    -0x18(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1090 <info@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cpu_affinity>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebp,%ebp
lea    0x4(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1070 <sched_getaffinity@plt>
test   %eax,%eax
je     11b3 <set_cpu_affinity+0x43>
lea    0xe68(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <error@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x4(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <CPU_ISSET@plt>
test   %rax,%rax
jne    11e7 <set_cpu_affinity+0x77>
add    $0x1,%ebp
cmp    $0x20,%ebp
jne    11c0 <set_cpu_affinity+0x50>
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1060 <error@plt>
jmp    11a1 <set_cpu_affinity+0x31>
mov    %rsp,%r15
mov    %r15,%rdi
call   1050 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1080 <CPU_SET@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x4,%esi
mov    %r15,%rdx
call   1030 <sched_setaffinity@plt>
test   %eax,%eax
je     121d <set_cpu_affinity+0xad>
lea    0xe48(%rip),%rdi
jmp    1198 <set_cpu_affinity+0x28>
lea    0xe6f(%rip),%rdi
mov    %ebp,%esi
call   1090 <info@plt>
mov    (%rsp),%eax
mov    %eax,(%r14)
jmp    11a6 <set_cpu_affinity+0x36>

LLVM IR (-O0):
@.str = [51 x i8] c"set_cpu_affinity: getting CPU affinity mask: 0x%x\0A\00"
@.str.1 = [45 x i8] c"set_cpu_affinity: No schedulable CPU found!\0A\00"
@.str.2 = [51 x i8] c"set_cpu_affinity: setting CPU affinity mask: 0x%x\0A\00"
@.str.3 = [37 x i8] c"set_cpu_affinity: using processr %d\0A\00"
define i32 @set_cpu_affinity(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
%8 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %6)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %2
br label %49
14: ; preds = %1
store i32 0, i32* %5
br label %15
15: ; preds = %25, %14
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = icmp ult i64 %17, 32
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i32, i32* %5
%21 = call i64 @CPU_ISSET(i32 %20, i32* %6)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
br label %28
24: ; preds = %19
br label %25
25: ; preds = %24
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %15
28: ; preds = %23, %15
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = icmp uge i64 %30, 32
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %49
34: ; preds = %28
%35 = call i32 @CPU_ZERO(i32* %7)
%36 = load i32, i32* %5
%37 = call i32 @CPU_SET(i32 %36, i32* %7)
%38 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %7)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = load i32, i32* %4
%43 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %42)
store i32 1, i32* %2
br label %49
44: ; preds = %34
%45 = load i32, i32* %5
%46 = call i32 @info(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %7
%48 = load i32*, i32** %3
store i32 %47, i32* %48
store i32 0, i32* %2
br label %49
49: ; preds = %44, %41, %32, %11
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [51 x i8] c"set_cpu_affinity: getting CPU affinity mask: 0x%x\0A\00"
@.str.1 = [45 x i8] c"set_cpu_affinity: No schedulable CPU found!\0A\00"
@.str.2 = [51 x i8] c"set_cpu_affinity: setting CPU affinity mask: 0x%x\0A\00"
@.str.3 = [37 x i8] c"set_cpu_affinity: using processr %d\0A\00"
define i32 @set_cpu_affinity(i32* nocapture writeonly %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %29
10: ; preds = %1, %14
%11 = phi i32 [ %15, %14 ], [ 0, %1 ]
%12 = call i64 @CPU_ISSET(i32 %11, i32* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = add nuw nsw i32 %11, 1
%16 = icmp eq i32 %15, 32
br i1 %16, label %17, label %10
17: ; preds = %14
%18 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %29
19: ; preds = %10
%20 = call i32 @CPU_ZERO(i32* %3)
%21 = call i32 @CPU_SET(i32 %11, i32* %3)
%22 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %29
26: ; preds = %19
%27 = call i32 @info(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%28 = load i32, i32* %3
store i32 %28, i32* %0
br label %29
29: ; preds = %26, %24, %17, %8
%30 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 1, %24 ], [ 0, %26 ]
ret i32 %30
}