#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 */
int sched_getaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int bind_context(cpu_set_t *new_context, cpu_set_t *old_context) {
  int err = 0;
  int ret = 0;
  if (old_context != NULL) {
    err = sched_getaffinity(0, sizeof(cpu_set_t), old_context);
    if (0 != err)
      ret = -1;
  }
  err += sched_setaffinity(0, sizeof(cpu_set_t), new_context);
  if (0 != err)
    ret = -1;
  return ret;
}


x86 assembly (clang, -O0):
<bind_context>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1173 <bind_context+0x53>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1040 <sched_getaffinity@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     116e <bind_context+0x4e>
movl   $0xffffffff,-0x18(%rbp)
jmp    1173 <bind_context+0x53>
mov    -0x8(%rbp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1030 <sched_setaffinity@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     11a2 <bind_context+0x82>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_context>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1143 <bind_context+0x23>
mov    %rsi,%rdx
xor    %edi,%edi
mov    $0x4,%esi
call   1040 <sched_getaffinity@plt>
mov    %eax,%ebx
neg    %eax
sbb    %ebp,%ebp
jmp    1147 <bind_context+0x27>
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <sched_setaffinity@plt>
add    %eax,%ebx
mov    $0xffffffff,%eax
cmove  %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bind_context(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32*, i32** %4
%11 = call i32 @sched_getaffinity(i32 0, i32 4, i32* %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 0, %12
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %6
br label %15
15: ; preds = %14, %9
br label %16
16: ; preds = %15, %2
%17 = load i32*, i32** %3
%18 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %17)
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = add nsw i64 %20, %18
%22 = trunc i64 %21 to i32
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp ne i32 0, %23
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 -1, i32* %6
br label %26
26: ; preds = %25, %16
%27 = load i32, i32* %6
ret i32 %27
}

LLVM IR (-O2):
define i32 @bind_context(i32* %0, i32* %1) {
%3 = icmp eq i32* %1, null
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = tail call i32 @sched_getaffinity(i32 0, i32 4, i32* %1)
%6 = icmp ne i32 %5, 0
%7 = zext i32 %5 to i64
%8 = sext i1 %6 to i32
br label %9
9: ; preds = %4, %2
%10 = phi i64 [ 0, %2 ], [ %7, %4 ]
%11 = phi i32 [ 0, %2 ], [ %8, %4 ]
%12 = tail call i64 @sched_setaffinity(i32 0, i32 4, i32* %0)
%13 = add i64 %12, %10
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i32 %11, i32 -1
ret i32 %16
}