#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 */

/* Variables and functions */
int abs(long);
scalar_t__ cg_create(char *);
int /*<<< orphan*/ cg_destroy(char *);
char *cg_name(char const *, char *);
char *cg_name_indexed(char *, char *, int);
long cg_read_key_long(char *, char *, char *);
long cg_read_long(char *, char *);
scalar_t__ cg_write(char *, char *, char *);
int /*<<< orphan*/ free(char *);
int get_nprocs();
int /*<<< orphan*/ printf(char *, long, long);
int test_percpu_basic(const char *root) {
  int ret = 1;
  char *parent, *child;
  long current, percpu;
  int i;
  parent = cg_name(root, "percpu_basic_test");
  if (!parent)
    goto cleanup;
  if (cg_create(parent))
    goto cleanup;
  if (cg_write(parent, "cgroup.subtree_control", "+memory"))
    goto cleanup;
  for (i = 0; i < 1000; i++) {
    child = cg_name_indexed(parent, "child", i);
    if (!child)
      return -1;
    if (cg_create(child))
      goto cleanup_children;
    free(child);
  }
  current = cg_read_long(parent, "memory.current");
  percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
  if (current > 0 && percpu > 0 &&
      abs(current - percpu) < (4096 * 32 * get_nprocs()))
    ret = 0;
  else
    printf("memory.current %ld\npercpu %ld\n", current, percpu);
cleanup_children:
  for (i = 0; i < 1000; i++) {
    child = cg_name_indexed(parent, "child", i);
    cg_destroy(child);
    free(child);
  }
cleanup:
  cg_destroy(parent);
  free(parent);
  return ret;
}


x86 assembly (clang, -O0):
<test_percpu_basic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1060 <cg_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <test_percpu_basic+0x37>
jmp    1377 <test_percpu_basic+0x1c7>
mov    -0x20(%rbp),%rdi
call   1070 <cg_create@plt>
cmp    $0x0,%rax
je     11ff <test_percpu_basic+0x4f>
jmp    1377 <test_percpu_basic+0x1c7>
mov    -0x20(%rbp),%rdi
lea    0xe08(%rip),%rsi
lea    0xe18(%rip),%rdx
call   10b0 <cg_write@plt>
cmp    $0x0,%rax
je     1225 <test_percpu_basic+0x75>
jmp    1377 <test_percpu_basic+0x1c7>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3e8,-0x3c(%rbp)
jge    1296 <test_percpu_basic+0xe6>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%edx
lea    0xdea(%rip),%rsi
call   10a0 <cg_name_indexed@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1267 <test_percpu_basic+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    138f <test_percpu_basic+0x1df>
mov    -0x28(%rbp),%rdi
call   1070 <cg_create@plt>
cmp    $0x0,%rax
je     127f <test_percpu_basic+0xcf>
jmp    1327 <test_percpu_basic+0x177>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    122c <test_percpu_basic+0x7c>
mov    -0x20(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   1090 <cg_read_long@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xd91(%rip),%rsi
lea    0xd96(%rip),%rdx
call   10c0 <cg_read_key_long@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    130e <test_percpu_basic+0x15e>
cmpq   $0x0,-0x38(%rbp)
jle    130e <test_percpu_basic+0x15e>
mov    -0x30(%rbp),%rdi
sub    -0x38(%rbp),%rdi
call   1050 <abs@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   10d0 <get_nprocs@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
shl    $0x11,%ecx
cmp    %ecx,%eax
jge    130e <test_percpu_basic+0x15e>
movl   $0x0,-0x14(%rbp)
jmp    1322 <test_percpu_basic+0x172>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xd3d(%rip),%rdi
call   1080 <printf@plt>
jmp    1327 <test_percpu_basic+0x177>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3e8,-0x3c(%rbp)
jge    1372 <test_percpu_basic+0x1c2>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%edx
lea    0xce8(%rip),%rsi
call   10a0 <cg_name_indexed@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <cg_destroy@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    132e <test_percpu_basic+0x17e>
jmp    1377 <test_percpu_basic+0x1c7>
mov    -0x20(%rbp),%rdi
call   1030 <cg_destroy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_percpu_basic>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe3e(%rip),%rsi
call   1060 <cg_name@plt>
mov    %rax,%r12
mov    $0x1,%r13d
test   %rax,%rax
je     12fb <test_percpu_basic+0x14b>
mov    %r12,%rdi
call   1070 <cg_create@plt>
test   %rax,%rax
jne    12fb <test_percpu_basic+0x14b>
lea    0xe21(%rip),%rsi
lea    0xe31(%rip),%rdx
mov    %r12,%rdi
call   10b0 <cg_write@plt>
test   %rax,%rax
jne    12fb <test_percpu_basic+0x14b>
xor    %ebx,%ebx
lea    0xe1f(%rip),%r14
mov    $0x1,%r13d
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   10a0 <cg_name_indexed@plt>
test   %rax,%rax
je     131d <test_percpu_basic+0x16d>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <cg_create@plt>
test   %rax,%rax
jne    12c4 <test_percpu_basic+0x114>
mov    %rbp,%rdi
call   1040 <free@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    1220 <test_percpu_basic+0x70>
lea    0xdd7(%rip),%rsi
mov    %r12,%rdi
call   1090 <cg_read_long@plt>
mov    %rax,%r14
lea    0xdd4(%rip),%rsi
lea    0xdd9(%rip),%rdx
mov    %r12,%rdi
call   10c0 <cg_read_key_long@plt>
mov    %rax,%r15
test   %r14,%r14
jle    12ac <test_percpu_basic+0xfc>
test   %r15,%r15
jle    12ac <test_percpu_basic+0xfc>
mov    %r14,%rdi
sub    %r15,%rdi
call   1050 <abs@plt>
mov    %eax,%ebx
xor    %r13d,%r13d
xor    %eax,%eax
call   10d0 <get_nprocs@plt>
shl    $0x11,%eax
cmp    %eax,%ebx
jl     12c4 <test_percpu_basic+0x114>
lea    0xda7(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <printf@plt>
mov    $0x1,%r13d
xor    %ebx,%ebx
lea    0xd64(%rip),%r14
nopl   (%rax)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   10a0 <cg_name_indexed@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <cg_destroy@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    12d0 <test_percpu_basic+0x120>
mov    %r12,%rdi
call   1030 <cg_destroy@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r13d
jmp    130b <test_percpu_basic+0x15b>

LLVM IR (-O0):
@.str = [18 x i8] c"percpu_basic_test\00"
@.str.1 = [23 x i8] c"cgroup.subtree_control\00"
@.str.2 = [8 x i8] c"+memory\00"
@.str.3 = [6 x i8] c"child\00"
@.str.4 = [15 x i8] c"memory.current\00"
@.str.5 = [12 x i8] c"memory.stat\00"
@.str.6 = [8 x i8] c"percpu \00"
@.str.7 = [31 x i8] c"memory.current %ld\0Apercpu %ld\0A\00"
define i32 @test_percpu_basic(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i32 1, i32* %4
%10 = load i8*, i8** %3
%11 = call i8* @cg_name(i8* %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
br label %87
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = call i64 @cg_create(i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %87
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = call i64 @cg_write(i8* %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
br label %87
25: ; preds = %20
store i32 0, i32* %9
br label %26
26: ; preds = %44, %25
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 1000
br i1 %28, label %29, label %47
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = load i32, i32* %9
%32 = call i8* @cg_name_indexed(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %36, label %35
35: ; preds = %29
store i32 -1, i32* %2
br label %93
36: ; preds = %29
%37 = load i8*, i8** %6
%38 = call i64 @cg_create(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %71
41: ; preds = %36
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
br label %44
44: ; preds = %41
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %26
47: ; preds = %26
%48 = load i8*, i8** %5
%49 = call i64 @cg_read_long(i8* %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i64 %49, i64* %7
%50 = load i8*, i8** %5
%51 = call i64 @cg_read_key_long(i8* %50, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i64 %51, i64* %8
%52 = load i64, i64* %7
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %66
54: ; preds = %47
%55 = load i64, i64* %8
%56 = icmp sgt i64 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = load i64, i64* %7
%59 = load i64, i64* %8
%60 = sub nsw i64 %58, %59
%61 = call i32 @abs(i64 %60)
%62 = call i32 (...) @get_nprocs()
%63 = mul nsw i32 131072, %62
%64 = icmp slt i32 %61, %63
br i1 %64, label %65, label %66
65: ; preds = %57
store i32 0, i32* %4
br label %70
66: ; preds = %57, %54, %47
%67 = load i64, i64* %7
%68 = load i64, i64* %8
%69 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 %67, i64 %68)
br label %70
70: ; preds = %66, %65
br label %71
71: ; preds = %70, %40
store i32 0, i32* %9
br label %72
72: ; preds = %83, %71
%73 = load i32, i32* %9
%74 = icmp slt i32 %73, 1000
br i1 %74, label %75, label %86
75: ; preds = %72
%76 = load i8*, i8** %5
%77 = load i32, i32* %9
%78 = call i8* @cg_name_indexed(i8* %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %77)
store i8* %78, i8** %6
%79 = load i8*, i8** %6
%80 = call i32 @cg_destroy(i8* %79)
%81 = load i8*, i8** %6
%82 = call i32 @free(i8* %81)
br label %83
83: ; preds = %75
%84 = load i32, i32* %9
%85 = add nsw i32 %84, 1
store i32 %85, i32* %9
br label %72
86: ; preds = %72
br label %87
87: ; preds = %86, %24, %19, %14
%88 = load i8*, i8** %5
%89 = call i32 @cg_destroy(i8* %88)
%90 = load i8*, i8** %5
%91 = call i32 @free(i8* %90)
%92 = load i32, i32* %4
store i32 %92, i32* %2
br label %93
93: ; preds = %87, %35
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
@.str = [18 x i8] c"percpu_basic_test\00"
@.str.1 = [23 x i8] c"cgroup.subtree_control\00"
@.str.2 = [8 x i8] c"+memory\00"
@.str.3 = [6 x i8] c"child\00"
@.str.4 = [15 x i8] c"memory.current\00"
@.str.5 = [12 x i8] c"memory.stat\00"
@.str.6 = [8 x i8] c"percpu \00"
@.str.7 = [31 x i8] c"memory.current %ld\0Apercpu %ld\0A\00"
define i32 @test_percpu_basic(i8* %0) {
%2 = tail call i8* @cg_name(i8* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %44, label %4
4: ; preds = %1
%5 = tail call i64 @cg_create(i8* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %44
7: ; preds = %4
%8 = tail call i64 @cg_write(i8* %2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %44
10: ; preds = %7, %17
%11 = phi i32 [ %19, %17 ], [ 0, %7 ]
%12 = tail call i8* @cg_name_indexed(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %48, label %14
14: ; preds = %10
%15 = tail call i64 @cg_create(i8* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %14
%18 = tail call i32 @free(i8* %12)
%19 = add nuw nsw i32 %11, 1
%20 = icmp eq i32 %19, 1000
br i1 %20, label %21, label %10
21: ; preds = %17
%22 = tail call i64 @cg_read_long(i8* %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i64 @cg_read_key_long(i8* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%24 = icmp sgt i64 %22, 0
%25 = icmp sgt i64 %23, 0
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = sub nsw i64 %22, %23
%29 = tail call i32 @abs(i64 %28)
%30 = tail call i32 (...) @get_nprocs()
%31 = shl nsw i32 %30, 17
%32 = icmp slt i32 %29, %31
br i1 %32, label %35, label %33
33: ; preds = %27, %21
%34 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 %22, i64 %23)
br label %35
35: ; preds = %14, %27, %33
%36 = phi i32 [ 1, %33 ], [ 0, %27 ], [ 1, %14 ]
br label %37
37: ; preds = %35, %37
%38 = phi i32 [ 0, %35 ], [ %42, %37 ]
%39 = tail call i8* @cg_name_indexed(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = tail call i32 @cg_destroy(i8* %39)
%41 = tail call i32 @free(i8* %39)
%42 = add nuw nsw i32 %38, 1
%43 = icmp eq i32 %42, 1000
br i1 %43, label %44, label %37
44: ; preds = %37, %7, %4, %1
%45 = phi i32 [ 1, %4 ], [ 1, %7 ], [ 1, %1 ], [ %36, %37 ]
%46 = tail call i32 @cg_destroy(i8* %2)
%47 = tail call i32 @free(i8* %2)
br label %48
48: ; preds = %10, %44
%49 = phi i32 [ %45, %44 ], [ -1, %10 ]
ret i32 %49
}