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

/* Variables and functions */
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
scalar_t__ gettid();
int read_mapping(int, char *, int /*<<< orphan*/ **, size_t *);
int sched_setaffinity(scalar_t__, size_t, int /*<<< orphan*/ *);
int be_migrate_thread_to_domain(pid_t tid, int domain) {
  int ret;
  cpu_set_t *cpu_set;
  size_t sz;
  ret = read_mapping(domain, "domains", &cpu_set, &sz);
  if (ret != 0)
    return ret;
  if (tid == 0)
    tid = gettid();
  ret = sched_setaffinity(tid, sz, cpu_set);
  CPU_FREE(cpu_set);
  return ret;
}


x86 assembly (clang, -O0):
<be_migrate_thread_to_domain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xea7(%rip),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1060 <read_mapping@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     117e <be_migrate_thread_to_domain+0x3e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <be_migrate_thread_to_domain+0x77>
cmpq   $0x0,-0x10(%rbp)
jne    1194 <be_migrate_thread_to_domain+0x54>
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <CPU_FREE@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<be_migrate_thread_to_domain>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%eax
mov    %rdi,%rbx
lea    0xeae(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %eax,%edi
call   1060 <read_mapping@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1196 <be_migrate_thread_to_domain+0x56>
test   %rbx,%rbx
jne    1178 <be_migrate_thread_to_domain+0x38>
xor    %eax,%eax
call   1030 <gettid@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <sched_setaffinity@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1050 <CPU_FREE@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"domains\00"
define i32 @be_migrate_thread_to_domain(i64 %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = bitcast i32** %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = call i32 @read_mapping(i32 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32** %3, i64* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = icmp eq i64 %0, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i64 (...) @gettid()
br label %13
13: ; preds = %11, %9
%14 = phi i64 [ %12, %11 ], [ %0, %9 ]
%15 = load i64, i64* %4
%16 = load i32*, i32** %3
%17 = call i32 @sched_setaffinity(i64 %14, i64 %15, i32* %16)
%18 = load i32*, i32** %3
%19 = call i32 @CPU_FREE(i32* %18)
br label %20
20: ; preds = %2, %13
%21 = phi i32 [ %17, %13 ], [ %7, %2 ]
ret i32 %21
}