#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_cap {
  int dummy;
};
typedef int pid_t;
typedef int /*<<< orphan*/ mon_group;

/* Variables and functions */
int /*<<< orphan*/ _pqos_cap_get(struct pqos_cap const **,
                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ log_printf(int, char *, int const);
int resctrl_alloc_assoc_set_pid(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_pid(int const, char *, int);
int resctrl_mon_assoc_set_pid(int const, char *);
int os_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) {
  int ret;
  unsigned max_cos = 0;
  int ret_mon;
  char mon_group[256];
  const struct pqos_cap *cap;
  _pqos_cap_get(&cap, NULL);
  ret = resctrl_alloc_get_grps_num(cap, &max_cos);
  if (ret != 0)
    return ret;
  if (class_id >= max_cos) {
    log_printf((1 << 2),
               "ERROR: "
               "COS out of bounds for task %d\n",
               (int)task);
    return 2;
  }
  ret = resctrl_lock_exclusive();
  if (ret != 0)
    return ret;
  ret_mon = resctrl_mon_assoc_get_pid(task, mon_group, sizeof(mon_group));
  if (ret_mon != 0 && ret_mon != 3)
    log_printf((1 << 1),
               "WARN: "
               "Failed to obtain monitoring group assignment for "
               "task %d\n",
               task);
  ret = resctrl_alloc_assoc_set_pid(task, class_id);
  if (ret != 0)
    goto os_alloc_assoc_set_pid_exit;
  if (ret_mon == 0) {
    ret_mon = resctrl_mon_assoc_set_pid(task, mon_group);
    if (ret_mon != 0)
      log_printf((1 << 1),
                 "WARN: "
                 "Could not assign task %d back to monitoring "
                 "group\n",
                 task);
  }
os_alloc_assoc_set_pid_exit:
  resctrl_lock_release();
  return ret;
}


x86 assembly (clang, -O0):
<os_alloc_assoc_set_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x128(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <_pqos_cap_get@plt>
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     11d0 <os_alloc_assoc_set_pid+0x50>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c8 <os_alloc_assoc_set_pid+0x148>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     11fc <os_alloc_assoc_set_pid+0x7c>
mov    -0x8(%rbp),%edx
mov    $0x4,%edi
lea    0xe15(%rip),%rsi
call   1050 <log_printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12c8 <os_alloc_assoc_set_pid+0x148>
mov    $0x0,%al
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     121b <os_alloc_assoc_set_pid+0x9b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c8 <os_alloc_assoc_set_pid+0x148>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   10a0 <resctrl_mon_assoc_get_pid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     125a <os_alloc_assoc_set_pid+0xda>
cmpl   $0x3,-0x18(%rbp)
je     125a <os_alloc_assoc_set_pid+0xda>
mov    -0x8(%rbp),%edx
mov    $0x2,%edi
lea    0xdd1(%rip),%rsi
call   1050 <log_printf@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <resctrl_alloc_assoc_set_pid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1277 <os_alloc_assoc_set_pid+0xf7>
jmp    12bb <os_alloc_assoc_set_pid+0x13b>
cmpl   $0x0,-0x18(%rbp)
jne    12b6 <os_alloc_assoc_set_pid+0x136>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   1030 <resctrl_mon_assoc_set_pid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12b1 <os_alloc_assoc_set_pid+0x131>
mov    -0x8(%rbp),%edx
mov    $0x2,%edi
lea    0xdba(%rip),%rsi
call   1050 <log_printf@plt>
jmp    12b6 <os_alloc_assoc_set_pid+0x136>
jmp    12bb <os_alloc_assoc_set_pid+0x13b>
mov    $0x0,%al
call   1060 <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_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1090 <_pqos_cap_get@plt>
mov    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
call   1080 <resctrl_alloc_get_grps_num@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1265 <os_alloc_assoc_set_pid+0xe5>
cmp    %ebp,0x4(%rsp)
jbe    124c <os_alloc_assoc_set_pid+0xcc>
xor    %eax,%eax
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1265 <os_alloc_assoc_set_pid+0xe5>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x100,%edx
call   10a0 <resctrl_mon_assoc_get_pid@plt>
mov    %eax,%r15d
test   %eax,%eax
je     120d <os_alloc_assoc_set_pid+0x8d>
cmp    $0x3,%r15d
je     120d <os_alloc_assoc_set_pid+0x8d>
lea    0xe26(%rip),%rsi
mov    $0x2,%edi
mov    %r14d,%edx
call   1050 <log_printf@plt>
mov    %r14d,%edi
mov    %ebp,%esi
call   1070 <resctrl_alloc_assoc_set_pid@plt>
mov    %eax,%ebx
or     %eax,%r15d
jne    1243 <os_alloc_assoc_set_pid+0xc3>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
call   1030 <resctrl_mon_assoc_set_pid@plt>
test   %eax,%eax
je     1243 <os_alloc_assoc_set_pid+0xc3>
lea    0xe30(%rip),%rsi
mov    $0x2,%edi
mov    %r14d,%edx
call   1050 <log_printf@plt>
xor    %eax,%eax
call   1060 <resctrl_lock_release@plt>
jmp    1265 <os_alloc_assoc_set_pid+0xe5>
lea    0xdad(%rip),%rsi
mov    $0x4,%edi
mov    %r14d,%edx
call   1050 <log_printf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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