#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ _pqos_cap_get(struct pqos_cap const **,
                                 int /*<<< orphan*/ *);
int resctrl_alloc_get_grps_num(struct pqos_cap const *, unsigned int *);
int resctrl_alloc_get_unused_group(unsigned int, unsigned int *);
int resctrl_alloc_task_write(unsigned int, int /*<<< orphan*/ const);
int resctrl_lock_exclusive();
int /*<<< orphan*/ resctrl_lock_release();
int os_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
                        const unsigned task_num, unsigned *class_id) {
  unsigned i, num_rctl_grps = 0;
  int ret;
  const struct pqos_cap *cap;
  ;
  ;
  ;
  (void)(technology);
  _pqos_cap_get(&cap, NULL);
  ret = resctrl_alloc_get_grps_num(cap, &num_rctl_grps);
  if (ret != 0)
    return ret;
  if (num_rctl_grps == 0)
    return 1;
  ret = resctrl_lock_exclusive();
  if (ret != 0)
    return ret;
  ret = resctrl_alloc_get_unused_group(num_rctl_grps, class_id);
  if (ret != 0)
    goto os_alloc_assign_pid_unlock;
  for (i = 0; i < task_num; i++) {
    ret = resctrl_alloc_task_write(*class_id, task_array[i]);
    if (ret != 0)
      goto os_alloc_assign_pid_unlock;
  }
os_alloc_assign_pid_unlock:
  resctrl_lock_release();
  return ret;
}


x86 assembly (clang, -O0):
<os_alloc_assign_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <_pqos_cap_get@plt>
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <resctrl_alloc_get_grps_num@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11af <os_alloc_assign_pid+0x4f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <os_alloc_assign_pid+0x101>
cmpl   $0x0,-0x28(%rbp)
jne    11c5 <os_alloc_assign_pid+0x65>
movl   $0x1,-0x4(%rbp)
jmp    1261 <os_alloc_assign_pid+0x101>
mov    $0x0,%al
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11e4 <os_alloc_assign_pid+0x84>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <os_alloc_assign_pid+0x101>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <resctrl_alloc_get_unused_group@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1202 <os_alloc_assign_pid+0xa2>
jmp    1254 <os_alloc_assign_pid+0xf4>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    124f <os_alloc_assign_pid+0xef>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,4),%esi
call   1070 <resctrl_alloc_task_write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     123c <os_alloc_assign_pid+0xdc>
jmp    1254 <os_alloc_assign_pid+0xf4>
jmp    1241 <os_alloc_assign_pid+0xe1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1209 <os_alloc_assign_pid+0xa9>
jmp    1254 <os_alloc_assign_pid+0xf4>
mov    $0x0,%al
call   1050 <resctrl_lock_release@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_alloc_assign_pid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r12
mov    %edx,%r15d
mov    %rsi,%r14
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1080 <_pqos_cap_get@plt>
mov    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
call   1060 <resctrl_alloc_get_grps_num@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11ad <os_alloc_assign_pid+0x4d>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmpl   $0x0,0x4(%rsp)
je     11fe <os_alloc_assign_pid+0x9e>
xor    %eax,%eax
call   1040 <resctrl_lock_exclusive@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    119e <os_alloc_assign_pid+0x3e>
mov    0x4(%rsp),%edi
mov    %r12,%rsi
call   1030 <resctrl_alloc_get_unused_group@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1207 <os_alloc_assign_pid+0xa7>
test   %r15d,%r15d
je     11fa <os_alloc_assign_pid+0x9a>
mov    %r15d,%r15d
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r12),%edi
mov    (%r14,%rbx,4),%esi
call   1070 <resctrl_alloc_task_write@plt>
test   %eax,%eax
jne    1205 <os_alloc_assign_pid+0xa5>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11e0 <os_alloc_assign_pid+0x80>
xor    %ebp,%ebp
jmp    1207 <os_alloc_assign_pid+0xa7>
mov    $0x1,%ebp
jmp    119e <os_alloc_assign_pid+0x3e>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <resctrl_lock_release@plt>
jmp    119e <os_alloc_assign_pid+0x3e>

LLVM IR (-O0):
%struct.pqos_cap = type { i32 }
define i32 @os_alloc_assign_pid(i32 %0, i32* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.pqos_cap*
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 0, i32* %11
%14 = load i32, i32* %6
%15 = call i32 @_pqos_cap_get(%struct.pqos_cap** %13, i32* null)
%16 = load %struct.pqos_cap*, %struct.pqos_cap** %13
%17 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %16, i32* %11)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* %12
store i32 %21, i32* %5
br label %64
22: ; preds = %4
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 1, i32* %5
br label %64
26: ; preds = %22
%27 = call i32 (...) @resctrl_lock_exclusive()
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* %12
store i32 %31, i32* %5
br label %64
32: ; preds = %26
%33 = load i32, i32* %11
%34 = load i32*, i32** %9
%35 = call i32 @resctrl_alloc_get_unused_group(i32 %33, i32* %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %61
39: ; preds = %32
store i32 0, i32* %10
br label %40
40: ; preds = %57, %39
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = icmp ult i32 %41, %42
br i1 %43, label %44, label %60
44: ; preds = %40
%45 = load i32*, i32** %9
%46 = load i32, i32* %45
%47 = load i32*, i32** %7
%48 = load i32, i32* %10
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = call i32 @resctrl_alloc_task_write(i32 %46, i32 %51)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %44
br label %61
56: ; preds = %44
br label %57
57: ; preds = %56
%58 = load i32, i32* %10
%59 = add i32 %58, 1
store i32 %59, i32* %10
br label %40
60: ; preds = %40
br label %61
61: ; preds = %60, %55, %38
%62 = call i32 (...) @resctrl_lock_release()
%63 = load i32, i32* %12
store i32 %63, i32* %5
br label %64
64: ; preds = %61, %30, %25, %20
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
%struct.pqos_cap = type { i32 }
define i32 @os_alloc_assign_pid(i32 %0, i32* nocapture %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.pqos_cap*
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = bitcast %struct.pqos_cap** %6 to i8*
%9 = call i32 @_pqos_cap_get(%struct.pqos_cap** %6, i32* null)
%10 = load %struct.pqos_cap*, %struct.pqos_cap** %6
%11 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %10, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %4
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %40, label %16
16: ; preds = %13
%17 = call i32 (...) @resctrl_lock_exclusive()
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %40
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i32 @resctrl_alloc_get_unused_group(i32 %20, i32* %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = icmp eq i32 %2, 0
br i1 %24, label %37, label %25
25: ; preds = %23
%26 = zext i32 %2 to i64
br label %29
27: ; preds = %29
%28 = icmp eq i64 %36, %26
br i1 %28, label %37, label %29
29: ; preds = %25, %27
%30 = phi i64 [ 0, %25 ], [ %36, %27 ]
%31 = load i32, i32* %3
%32 = getelementptr inbounds i32, i32* %1, i64 %30
%33 = load i32, i32* %32
%34 = call i32 @resctrl_alloc_task_write(i32 %31, i32 %33)
%35 = icmp eq i32 %34, 0
%36 = add nuw nsw i64 %30, 1
br i1 %35, label %27, label %37
37: ; preds = %29, %27, %23, %19
%38 = phi i32 [ %21, %19 ], [ 0, %23 ], [ %34, %29 ], [ 0, %27 ]
%39 = call i32 (...) @resctrl_lock_release()
br label %40
40: ; preds = %16, %13, %4, %37
%41 = phi i32 [ %38, %37 ], [ %11, %4 ], [ 1, %13 ], [ %17, %16 ]
ret i32 %41
}