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

/* Variables and functions */
int /*<<< orphan*/ _pqos_cap_get(struct pqos_cap const **,
                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ log_printf(int, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mkdir(char *, int);
int resctrl_alloc_get_grps_num(struct pqos_cap const *, unsigned int *);
scalar_t__ snprintf(char *, int, char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int os_alloc_prep(void) {
  unsigned i, num_grps = 0;
  const struct pqos_cap *cap;
  int ret;
  _pqos_cap_get(&cap, NULL);
  ret = resctrl_alloc_get_grps_num(cap, &num_grps);
  if (ret != 0)
    return ret;
  for (i = 1; i < num_grps; i++) {
    char buf[128];
    struct stat st;
    memset(buf, 0, sizeof(buf));
    if (snprintf(buf, sizeof(buf) - 1, "%s/COS%d", "/sys/fs/resctrl", (int)i) <
        0)
      return 1;
    if (stat(buf, &st) == 0) {
      log_printf((1 << 3),
                 "DEBUG: "
                 "resctrl group COS%d detected\n",
                 i);
      continue;
    }
    if (mkdir(buf, 0755) == -1) {
      log_printf((1 << 3),
                 "DEBUG: "
                 "Failed to create resctrl group %s!\n",
                 buf);
      return 7;
    }
    log_printf((1 << 3),
               "DEBUG: "
               "resctrl group COS%d created\n",
               i);
  }
  return 0;
}


x86 assembly (clang, -O0):
<os_alloc_prep>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <_pqos_cap_get@plt>
mov    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1080 <resctrl_alloc_get_grps_num@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11b4 <os_alloc_prep+0x44>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b6 <os_alloc_prep+0x146>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    12af <os_alloc_prep+0x13f>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x7f,%esi
lea    0xe0f(%rip),%rdx
lea    0xe11(%rip),%rcx
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    1213 <os_alloc_prep+0xa3>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <os_alloc_prep+0x146>
lea    -0xa0(%rbp),%rdi
lea    -0xa8(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    124b <os_alloc_prep+0xdb>
mov    -0x8(%rbp),%edx
mov    $0x8,%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1060 <log_printf@plt>
jmp    12a1 <os_alloc_prep+0x131>
lea    -0xa0(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    128b <os_alloc_prep+0x11b>
lea    -0xa0(%rbp),%rdx
mov    $0x8,%edi
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1060 <log_printf@plt>
movl   $0x7,-0x4(%rbp)
jmp    12b6 <os_alloc_prep+0x146>
mov    -0x8(%rbp),%edx
mov    $0x8,%edi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1060 <log_printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11bb <os_alloc_prep+0x4b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_alloc_prep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
movl   $0x0,0xc(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1090 <_pqos_cap_get@plt>
mov    0x18(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1080 <resctrl_alloc_get_grps_num@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1275 <os_alloc_prep+0x105>
cmpl   $0x2,0xc(%rsp)
mov    $0x0,%ebx
jb     1275 <os_alloc_prep+0x105>
lea    0x20(%rsp),%r13
lea    0xe3f(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xe43(%rip),%r14
mov    $0x1,%ebp
xor    %ebx,%ebx
jmp    11f7 <os_alloc_prep+0x87>
nop
mov    $0x8,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <log_printf@plt>
add    $0x1,%ebp
cmp    0xc(%rsp),%ebp
jae    1275 <os_alloc_prep+0x105>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %r13,%rdi
mov    $0x7f,%esi
lea    0xdeb(%rip),%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
call   1040 <snprintf@plt>
test   %rax,%rax
js     1270 <os_alloc_prep+0x100>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <stat@plt>
mov    %r14,%rsi
test   %rax,%rax
je     11e0 <os_alloc_prep+0x70>
mov    %r13,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
lea    0xe1d(%rip),%rsi
cmp    $0xffffffff,%eax
jne    11e0 <os_alloc_prep+0x70>
lea    0xde6(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    $0x8,%edi
xor    %eax,%eax
call   1060 <log_printf@plt>
mov    $0x7,%ebx
jmp    1275 <os_alloc_prep+0x105>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pqos_cap = type { i32 }
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s/COS%d\00"
@.str.1 = [16 x i8] c"/sys/fs/resctrl\00"
@.str.2 = [37 x i8] c"DEBUG: resctrl group COS%d detected\0A\00"
@.str.3 = [43 x i8] c"DEBUG: Failed to create resctrl group %s!\0A\00"
@.str.4 = [36 x i8] c"DEBUG: resctrl group COS%d created\0A\00"
define i32 @os_alloc_prep() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pqos_cap*
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca %struct.stat
store i32 0, i32* %3
%8 = call i32 @_pqos_cap_get(%struct.pqos_cap** %4, i32* null)
%9 = load %struct.pqos_cap*, %struct.pqos_cap** %4
%10 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %9, i32* %3)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = load i32, i32* %5
store i32 %14, i32* %1
br label %49
15: ; preds = %0
store i32 1, i32* %2
br label %16
16: ; preds = %45, %15
%17 = load i32, i32* %2
%18 = load i32, i32* %3
%19 = icmp ult i32 %17, %18
br i1 %19, label %20, label %48
20: ; preds = %16
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 128)
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %2
%25 = call i64 @snprintf(i8* %23, i32 127, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 1, i32* %1
br label %49
28: ; preds = %20
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%30 = call i64 @stat(i8* %29, %struct.stat* %7)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %2
%34 = call i32 (i32, i8*, ...) @log_printf(i32 8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %45
35: ; preds = %28
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 @mkdir(i8* %36, i32 493)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%41 = call i32 (i32, i8*, ...) @log_printf(i32 8, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %40)
store i32 7, i32* %1
br label %49
42: ; preds = %35
%43 = load i32, i32* %2
%44 = call i32 (i32, i8*, ...) @log_printf(i32 8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %32
%46 = load i32, i32* %2
%47 = add i32 %46, 1
store i32 %47, i32* %2
br label %16
48: ; preds = %16
store i32 0, i32* %1
br label %49
49: ; preds = %48, %39, %27, %13
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.pqos_cap = type { i32 }
%struct.stat = type { i32 }
@.str = [9 x i8] c"%s/COS%d\00"
@.str.1 = [16 x i8] c"/sys/fs/resctrl\00"
@.str.2 = [37 x i8] c"DEBUG: resctrl group COS%d detected\0A\00"
@.str.3 = [43 x i8] c"DEBUG: Failed to create resctrl group %s!\0A\00"
@.str.4 = [36 x i8] c"DEBUG: resctrl group COS%d created\0A\00"
define i32 @os_alloc_prep() {
%1 = alloca i32
%2 = alloca %struct.pqos_cap*
%3 = alloca [128 x i8]
%4 = alloca %struct.stat
%5 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%6 = bitcast %struct.pqos_cap** %2 to i8*
%7 = call i32 @_pqos_cap_get(%struct.pqos_cap** %2, i32* null)
%8 = load %struct.pqos_cap*, %struct.pqos_cap** %2
%9 = call i32 @resctrl_alloc_get_grps_num(%struct.pqos_cap* %8, i32* %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %0
%12 = load i32, i32* %1
%13 = icmp ugt i32 %12, 1
br i1 %13, label %14, label %38
14: ; preds = %11
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%16 = bitcast %struct.stat* %4 to i8*
br label %17
17: ; preds = %14, %32
%18 = phi i32 [ 1, %14 ], [ %35, %32 ]
%19 = call i32 @memset(i8* %15, i32 0, i32 128)
%20 = call i64 @snprintf(i8* %15, i32 127, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%21 = icmp slt i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %17
%23 = call i64 @stat(i8* %15, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %22
%26 = call i32 @mkdir(i8* %15, i32 493)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = call i32 (i32, i8*, ...) @log_printf(i32 8, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %30
30: ; preds = %17, %28
%31 = phi i32 [ 7, %28 ], [ 1, %17 ]
br label %38
32: ; preds = %25, %22
%33 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), %22 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), %25 ]
%34 = call i32 (i32, i8*, ...) @log_printf(i32 8, i8* %33, i32 %18)
%35 = add nuw i32 %18, 1
%36 = load i32, i32* %1
%37 = icmp ult i32 %35, %36
br i1 %37, label %17, label %38
38: ; preds = %32, %11, %30, %0
%39 = phi i32 [ %9, %0 ], [ %31, %30 ], [ 0, %11 ], [ 0, %32 ]
ret i32 %39
}