#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 pqos_cpuinfo {
  int dummy;
};
struct pqos_cap {
  int dummy;
};
typedef int /*<<< orphan*/ mon_group;

/* Variables and functions */
int /*<<< orphan*/ _pqos_cap_get(struct pqos_cap const **,
                                 struct pqos_cpuinfo const **);
int /*<<< orphan*/ log_printf(int, char *, unsigned int const);
int pqos_cpu_check_core(struct pqos_cpuinfo const *, unsigned int const);
int resctrl_alloc_assoc_set(unsigned int const, unsigned int const);
int resctrl_alloc_get_grps_num(struct pqos_cap const *, unsigned int *);
int resctrl_lock_exclusive();
int /*<<< orphan*/ resctrl_lock_release();
int resctrl_mon_assoc_get(unsigned int const, char *, int);
int resctrl_mon_assoc_set(unsigned int const, char *);
int os_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
  int ret;
  unsigned grps;
  int ret_mon;
  char mon_group[256];
  const struct pqos_cap *cap;
  const struct pqos_cpuinfo *cpu;
  _pqos_cap_get(&cap, &cpu);
  ret = pqos_cpu_check_core(cpu, lcore);
  if (ret != 0)
    return 2;
  ret = resctrl_alloc_get_grps_num(cap, &grps);
  if (ret != 0)
    return ret;
  if (class_id >= grps)
    return 2;
  ret = resctrl_lock_exclusive();
  if (ret != 0)
    return ret;
  ret_mon = resctrl_mon_assoc_get(lcore, mon_group, sizeof(mon_group));
  if (ret_mon != 0 && ret_mon != 3)
    log_printf((1 << 1),
               "WARN: "
               "Failed to obtain monitoring group assignment for "
               "core %u\n",
               lcore);
  ret = resctrl_alloc_assoc_set(lcore, class_id);
  if (ret != 0)
    goto os_alloc_assoc_set_exit;
  if (ret_mon == 0) {
    ret_mon = resctrl_mon_assoc_set(lcore, mon_group);
    if (ret_mon != 0)
      log_printf((1 << 1),
                 "WARN: "
                 "Could not assign core %d back to monitoring "
                 "group\n",
                 lcore);
  }
os_alloc_assoc_set_exit:
  resctrl_lock_release();
  return ret;
}


x86 assembly (clang, -O0):
<os_alloc_assoc_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x128(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   10a0 <_pqos_cap_get@plt>
mov    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <pqos_cpu_check_core@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11dc <os_alloc_assoc_set+0x4c>
movl   $0x2,-0x4(%rbp)
jmp    12e8 <os_alloc_assoc_set+0x158>
mov    -0x128(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1080 <resctrl_alloc_get_grps_num@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1204 <os_alloc_assoc_set+0x74>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e8 <os_alloc_assoc_set+0x158>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     121c <os_alloc_assoc_set+0x8c>
movl   $0x2,-0x4(%rbp)
jmp    12e8 <os_alloc_assoc_set+0x158>
mov    $0x0,%al
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     123b <os_alloc_assoc_set+0xab>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e8 <os_alloc_assoc_set+0x158>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <resctrl_mon_assoc_get@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     127a <os_alloc_assoc_set+0xea>
cmpl   $0x3,-0x18(%rbp)
je     127a <os_alloc_assoc_set+0xea>
mov    -0x8(%rbp),%edx
mov    $0x2,%edi
lea    0xd8b(%rip),%rsi
call   1060 <log_printf@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10b0 <resctrl_alloc_assoc_set@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1297 <os_alloc_assoc_set+0x107>
jmp    12db <os_alloc_assoc_set+0x14b>
cmpl   $0x0,-0x18(%rbp)
jne    12d6 <os_alloc_assoc_set+0x146>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   1090 <resctrl_mon_assoc_set@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12d1 <os_alloc_assoc_set+0x141>
mov    -0x8(%rbp),%edx
mov    $0x2,%edi
lea    0xd74(%rip),%rsi
call   1060 <log_printf@plt>
jmp    12d6 <os_alloc_assoc_set+0x146>
jmp    12db <os_alloc_assoc_set+0x14b>
mov    $0x0,%al
call   1070 <resctrl_lock_release@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_alloc_assoc_set>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0x18(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <_pqos_cap_get@plt>
mov    0x10(%rsp),%rdi
mov    %ebx,%esi
call   1030 <pqos_cpu_check_core@plt>
mov    $0x2,%ebp
test   %eax,%eax
jne    1268 <os_alloc_assoc_set+0xd8>
mov    0x18(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1080 <resctrl_alloc_get_grps_num@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1268 <os_alloc_assoc_set+0xd8>
mov    $0x2,%ebp
cmp    %r14d,0xc(%rsp)
jbe    1268 <os_alloc_assoc_set+0xd8>
xor    %eax,%eax
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1268 <os_alloc_assoc_set+0xd8>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x100,%edx
call   1050 <resctrl_mon_assoc_get@plt>
mov    %eax,%r15d
test   %eax,%eax
je     122d <os_alloc_assoc_set+0x9d>
cmp    $0x3,%r15d
je     122d <os_alloc_assoc_set+0x9d>
lea    0xddf(%rip),%rsi
mov    $0x2,%edi
mov    %ebx,%edx
call   1060 <log_printf@plt>
mov    %ebx,%edi
mov    %r14d,%esi
call   10b0 <resctrl_alloc_assoc_set@plt>
mov    %eax,%ebp
or     %eax,%r15d
jne    1261 <os_alloc_assoc_set+0xd1>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
call   1090 <resctrl_mon_assoc_set@plt>
test   %eax,%eax
je     1261 <os_alloc_assoc_set+0xd1>
lea    0xdeb(%rip),%rsi
mov    $0x2,%edi
mov    %ebx,%edx
call   1060 <log_printf@plt>
xor    %eax,%eax
call   1070 <resctrl_lock_release@plt>
mov    %ebp,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pqos_cap = type { i32 }
%struct.pqos_cpuinfo = type { i32 }
@.str = [64 x i8] c"WARN: Failed to obtain monitoring group assignment for core %u\0A\00"
@.str.1 = [57 x i8] c"WARN: Could not assign core %d back to monitoring group\0A\00"
define i32 @os_alloc_assoc_set(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca %struct.pqos_cap*
%11 = alloca %struct.pqos_cpuinfo*
store i32 %0, i32* %4
store i32 %1, i32* %5
%12 = call i32 @_pqos_cap_get(%struct.pqos_cap** %10, %struct.pqos_cpuinfo** %11)
%13 = load %struct.pqos_cpuinfo*, %struct.pqos_cpuinfo** %11
%14 = load i32, i32* %4
%15 = call i32 @pqos_cpu_check_core(%struct.pqos_cpuinfo* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 2, i32* %3
br label %73
19: ; preds = %2
%20 = load %struct.pqos_cap*, %struct.pqos_cap** %10
%21 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %20, i32* %7)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %73
26: ; preds = %19
%27 = load i32, i32* %5
%28 = load i32, i32* %7
%29 = icmp uge i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 2, i32* %3
br label %73
31: ; preds = %26
%32 = call i32 (...) @resctrl_lock_exclusive()
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %73
37: ; preds = %31
%38 = load i32, i32* %4
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%40 = call i32 @resctrl_mon_assoc_get(i32 %38, i8* %39, i32 256)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %37
%44 = load i32, i32* %8
%45 = icmp ne i32 %44, 3
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %4
%48 = call i32 @log_printf(i32 2, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %46, %43, %37
%50 = load i32, i32* %4
%51 = load i32, i32* %5
%52 = call i32 @resctrl_alloc_assoc_set(i32 %50, i32 %51)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %49
br label %70
56: ; preds = %49
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %56
%60 = load i32, i32* %4
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%62 = call i32 @resctrl_mon_assoc_set(i32 %60, i8* %61)
store i32 %62, i32* %8
%63 = load i32, i32* %8
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i32, i32* %4
%67 = call i32 @log_printf(i32 2, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %65, %59
br label %69
69: ; preds = %68, %56
br label %70
70: ; preds = %69, %55
%71 = call i32 (...) @resctrl_lock_release()
%72 = load i32, i32* %6
store i32 %72, i32* %3
br label %73
73: ; preds = %70, %35, %30, %24, %18
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.pqos_cap = type { i32 }
%struct.pqos_cpuinfo = type { i32 }
@.str = [64 x i8] c"WARN: Failed to obtain monitoring group assignment for core %u\0A\00"
@.str.1 = [57 x i8] c"WARN: Could not assign core %d back to monitoring group\0A\00"
define i32 @os_alloc_assoc_set(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.pqos_cap*
%6 = alloca %struct.pqos_cpuinfo*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.pqos_cap** %5 to i8*
%10 = bitcast %struct.pqos_cpuinfo** %6 to i8*
%11 = call i32 @_pqos_cap_get(%struct.pqos_cap** %5, %struct.pqos_cpuinfo** %6)
%12 = load %struct.pqos_cpuinfo*, %struct.pqos_cpuinfo** %6
%13 = call i32 @pqos_cpu_check_core(%struct.pqos_cpuinfo* %12, i32 %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %2
%16 = load %struct.pqos_cap*, %struct.pqos_cap** %5
%17 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %16, i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %41
19: ; preds = %15
%20 = load i32, i32* %3
%21 = icmp ugt i32 %20, %1
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = call i32 (...) @resctrl_lock_exclusive()
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %41
25: ; preds = %22
%26 = call i32 @resctrl_mon_assoc_get(i32 %0, i8* %8, i32 256)
switch i32 %26, label %27 [
i32 3, label %29
i32 0, label %29
]
27: ; preds = %25
%28 = call i32 @log_printf(i32 2, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %29
29: ; preds = %25, %25, %27
%30 = call i32 @resctrl_alloc_assoc_set(i32 %0, i32 %1)
%31 = icmp eq i32 %30, 0
%32 = icmp eq i32 %26, 0
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = call i32 @resctrl_mon_assoc_set(i32 %0, i8* %8)
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @log_printf(i32 2, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %39
39: ; preds = %37, %34, %29
%40 = call i32 (...) @resctrl_lock_release()
br label %41
41: ; preds = %22, %19, %15, %2, %39
%42 = phi i32 [ %30, %39 ], [ 2, %2 ], [ %17, %15 ], [ 2, %19 ], [ %23, %22 ]
ret i32 %42
}