#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 */
struct bitmask {
  int dummy;
};
typedef int /*<<< orphan*/ cpus;
typedef int /*<<< orphan*/ cpu_set_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ EFAULT;
int /*<<< orphan*/ EINVAL;
size_t GetNumCPUs();
int /*<<< orphan*/ Gettid();
int /*<<< orphan*/ assert(struct bitmask *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
struct bitmask *numa_bitmask_alloc(size_t);
int /*<<< orphan*/ numa_bitmask_free(struct bitmask *);
int /*<<< orphan*/ numa_bitmask_setbit(struct bitmask *, int);
scalar_t__ numa_max_node();
int /*<<< orphan*/ numa_set_membind(struct bitmask *);
int /*<<< orphan*/ perror(char *);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int mtcp_core_affinitize(int cpu) {
  cpu_set_t cpus;
  struct bitmask *bmask;
  FILE *fp;
  char sysfname[1024];
  int phy_id;
  size_t n;
  int ret;
  int unused;
  n = GetNumCPUs();
  if (cpu < 0 || cpu >= (int)n) {
    errno = -EINVAL;
    return -1;
  }
  CPU_ZERO(&cpus);
  CPU_SET((unsigned)cpu, &cpus);
  ret = sched_setaffinity(Gettid(), sizeof(cpus), &cpus);
  if (numa_max_node() == 0)
    return ret;
  bmask = numa_bitmask_alloc(n);
  assert(bmask);
  snprintf(sysfname, 1024 - 1,
           "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  fp = fopen(sysfname, "r");
  if (!fp) {
    perror(sysfname);
    errno = EFAULT;
    return -1;
  }
  unused = fscanf(fp, "%d", &phy_id);
  numa_bitmask_setbit(bmask, phy_id);
  numa_set_membind(bmask);
  numa_bitmask_free(bmask);
  fclose(fp);
  (void)unused;
  return ret;
}


x86 assembly (clang, -O0):
<mtcp_core_affinitize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <GetNumCPUs@plt>
mov    %rax,-0x430(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1238 <mtcp_core_affinitize+0x38>
mov    -0x8(%rbp),%eax
mov    -0x430(%rbp),%rcx
cmp    %ecx,%eax
jl     1258 <mtcp_core_affinitize+0x58>
xor    %ecx,%ecx
mov    0x2d8f(%rip),%rax
sub    (%rax),%ecx
mov    0x2d8e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <mtcp_core_affinitize+0x179>
lea    -0xc(%rbp),%rdi
call   10c0 <CPU_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1100 <CPU_SET@plt>
mov    $0x0,%al
call   10b0 <Gettid@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1090 <sched_setaffinity@plt>
mov    %eax,-0x434(%rbp)
mov    $0x0,%al
call   1120 <numa_max_node@plt>
cmp    $0x0,%rax
jne    12a9 <mtcp_core_affinitize+0xa9>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1379 <mtcp_core_affinitize+0x179>
mov    -0x430(%rbp),%rdi
call   1110 <numa_bitmask_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <assert@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3ff,%esi
lea    0xd28(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xd50(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1329 <mtcp_core_affinitize+0x129>
lea    -0x420(%rbp),%rdi
call   10e0 <perror@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%ecx
mov    0x2cbd(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <mtcp_core_affinitize+0x179>
mov    -0x20(%rbp),%rdi
lea    0xd09(%rip),%rsi
lea    -0x424(%rbp),%rdx
call   1080 <fscanf@plt>
mov    %eax,-0x438(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x424(%rbp),%esi
call   1030 <numa_bitmask_setbit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <numa_set_membind@plt>
mov    -0x18(%rbp),%rdi
call   10f0 <numa_bitmask_free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtcp_core_affinitize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <GetNumCPUs@plt>
test   %ebp,%ebp
js     12eb <mtcp_core_affinitize+0xeb>
mov    %rax,%r15
cmp    %ebp,%r15d
jle    12eb <mtcp_core_affinitize+0xeb>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   10c0 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1100 <CPU_SET@plt>
xor    %eax,%eax
call   10b0 <Gettid@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1090 <sched_setaffinity@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1120 <numa_max_node@plt>
test   %rax,%rax
je     1303 <mtcp_core_affinitize+0x103>
mov    %r15,%rdi
call   1110 <numa_bitmask_alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <assert@plt>
lea    0xd7a(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x3ff,%esi
mov    %ebp,%ecx
call   1070 <snprintf@plt>
lea    0xd9a(%rip),%rsi
mov    %r15,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1314 <mtcp_core_affinitize+0x114>
mov    %rax,%rbp
lea    0xd85(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1080 <fscanf@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1030 <numa_bitmask_setbit@plt>
mov    %rbx,%rdi
call   1040 <numa_set_membind@plt>
mov    %rbx,%rdi
call   10f0 <numa_bitmask_free@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    1303 <mtcp_core_affinitize+0x103>
mov    0x2cde(%rip),%rax
sub    (%rax),%ebx
mov    0x2cdd(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdi
call   10e0 <perror@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    0x2caa(%rip),%rcx
mov    %eax,(%rcx)
jmp    12fd <mtcp_core_affinitize+0xfd>

LLVM IR (-O0):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [59 x i8] c"/sys/devices/system/cpu/cpu%d/topology/physical_package_id\00"
@.str.1 = [2 x i8] c"r\00"
@EFAULT = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @mtcp_core_affinitize(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.bitmask*
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
%12 = call i64 (...) @GetNumCPUs()
store i64 %12, i64* %9
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = load i32, i32* %3
%17 = load i64, i64* %9
%18 = trunc i64 %17 to i32
%19 = icmp sge i32 %16, %18
br i1 %19, label %20, label %23
20: ; preds = %15, %1
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
store i32 %22, i32* @errno
store i32 -1, i32* %2
br label %63
23: ; preds = %15
%24 = call i32 @CPU_ZERO(i32* %4)
%25 = load i32, i32* %3
%26 = call i32 @CPU_SET(i32 %25, i32* %4)
%27 = call i32 (...) @Gettid()
%28 = call i32 @sched_setaffinity(i32 %27, i32 4, i32* %4)
store i32 %28, i32* %10
%29 = call i64 (...) @numa_max_node()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %10
store i32 %32, i32* %2
br label %63
33: ; preds = %23
%34 = load i64, i64* %9
%35 = call %struct.bitmask* @numa_bitmask_alloc(i64 %34)
store %struct.bitmask* %35, %struct.bitmask** %5
%36 = load %struct.bitmask*, %struct.bitmask** %5
%37 = call i32 @assert(%struct.bitmask* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%39 = load i32, i32* %3
%40 = call i32 @snprintf(i8* %38, i32 1023, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %42, i32** %6
%43 = load i32*, i32** %6
%44 = icmp ne i32* %43, null
br i1 %44, label %49, label %45
45: ; preds = %33
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%47 = call i32 @perror(i8* %46)
%48 = load i32, i32* @EFAULT
store i32 %48, i32* @errno
store i32 -1, i32* %2
br label %63
49: ; preds = %33
%50 = load i32*, i32** %6
%51 = call i32 @fscanf(i32* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
store i32 %51, i32* %11
%52 = load %struct.bitmask*, %struct.bitmask** %5
%53 = load i32, i32* %8
%54 = call i32 @numa_bitmask_setbit(%struct.bitmask* %52, i32 %53)
%55 = load %struct.bitmask*, %struct.bitmask** %5
%56 = call i32 @numa_set_membind(%struct.bitmask* %55)
%57 = load %struct.bitmask*, %struct.bitmask** %5
%58 = call i32 @numa_bitmask_free(%struct.bitmask* %57)
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
%61 = load i32, i32* %11
%62 = load i32, i32* %10
store i32 %62, i32* %2
br label %63
63: ; preds = %49, %45, %31, %20
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [59 x i8] c"/sys/devices/system/cpu/cpu%d/topology/physical_package_id\00"
@.str.1 = [2 x i8] c"r\00"
@EFAULT = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @mtcp_core_affinitize(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = tail call i64 (...) @GetNumCPUs()
%9 = icmp sgt i32 %0, -1
%10 = trunc i64 %8 to i32
%11 = icmp sgt i32 %10, %0
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* @errno
br label %39
16: ; preds = %1
%17 = call i32 @CPU_ZERO(i32* %2)
%18 = call i32 @CPU_SET(i32 %0, i32* %2)
%19 = call i32 (...) @Gettid()
%20 = call i32 @sched_setaffinity(i32 %19, i32 4, i32* %2)
%21 = call i64 (...) @numa_max_node()
%22 = icmp eq i64 %21, 0
br i1 %22, label %39, label %23
23: ; preds = %16
%24 = call %struct.bitmask* @numa_bitmask_alloc(i64 %8)
%25 = call i32 @assert(%struct.bitmask* %24)
%26 = call i32 @snprintf(i8* %6, i32 1023, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %0)
%27 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* %6)
%31 = load i32, i32* @EFAULT
store i32 %31, i32* @errno
br label %39
32: ; preds = %23
%33 = call i32 @fscanf(i32* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%34 = load i32, i32* %4
%35 = call i32 @numa_bitmask_setbit(%struct.bitmask* %24, i32 %34)
%36 = call i32 @numa_set_membind(%struct.bitmask* %24)
%37 = call i32 @numa_bitmask_free(%struct.bitmask* %24)
%38 = call i32 @fclose(i32* %27)
br label %39
39: ; preds = %16, %32, %29, %13
%40 = phi i32 [ -1, %13 ], [ %20, %32 ], [ -1, %29 ], [ %20, %16 ]
ret i32 %40
}