#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 */
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ init_cgroups_controller(char *, char *);
char *strsep(char **, char *);
int init_cgroups(void) {
  FILE *f = fopen("/proc/self/cgroup", "r");
  if (!f)
    return -1;
  ssize_t size;
  size_t bufsize = 0;
  char *str = NULL;
  while ((size = getline(&str, &bufsize, f)) > 0) {
    char *run = str;
    char *id = strsep(&run, ":");
    char *controller = strsep(&run, ":");
    char *path = strsep(&run, "\n");
    if (!id || !controller || !path) {
      errno = EINVAL;
      break;
    }
    if (init_cgroups_controller(controller, path) < 0)
      break;
  }
  int err = errno;
  free(str);
  if (fclose(f) < 0 && !err)
    err = errno;
  return err ? -1 : 0;
}


x86 assembly (clang, -O0):
<init_cgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xe91(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <init_cgroups+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <init_cgroups+0x151>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1260 <init_cgroups+0x100>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1228 <init_cgroups+0xc8>
cmpq   $0x0,-0x40(%rbp)
je     1228 <init_cgroups+0xc8>
cmpq   $0x0,-0x48(%rbp)
jne    123f <init_cgroups+0xdf>
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
mov    0x2da8(%rip),%rax
mov    %ecx,(%rax)
jmp    1260 <init_cgroups+0x100>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <init_cgroups_controller@plt>
cmp    $0x0,%rax
jge    125b <init_cgroups+0xfb>
jmp    1260 <init_cgroups+0x100>
jmp    11a6 <init_cgroups+0x46>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
jge    129e <init_cgroups+0x13e>
cmpl   $0x0,-0x4c(%rbp)
jne    129e <init_cgroups+0x13e>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_cgroups>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1235 <init_cgroups+0xd5>
mov    %rax,%r15
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
test   %eax,%eax
jle    124e <init_cgroups+0xee>
lea    0xe57(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe4d(%rip),%r12
nopl   0x0(%rax)
mov    (%rsp),%rax
mov    %rax,0x10(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strsep@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strsep@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strsep@plt>
test   %rbp,%rbp
je     123c <init_cgroups+0xdc>
test   %r13,%r13
je     123c <init_cgroups+0xdc>
test   %rax,%rax
je     123c <init_cgroups+0xdc>
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <init_cgroups_controller@plt>
test   %rax,%rax
js     124e <init_cgroups+0xee>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r15,%rdx
call   1080 <getline@plt>
test   %eax,%eax
jg     11d0 <init_cgroups+0x70>
jmp    124e <init_cgroups+0xee>
mov    $0xffffffff,%eax
jmp    127c <init_cgroups+0x11c>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    0x2d94(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2d8b(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %ebx,%ecx
test   %ebx,%ebx
jne    1272 <init_cgroups+0x112>
mov    0x0(%rbp),%ecx
test   %rax,%rax
cmovns %ebx,%ecx
neg    %ecx
sbb    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/proc/self/cgroup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"\0A\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @init_cgroups() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %0
store i32 -1, i32* %1
br label %59
15: ; preds = %0
store i64 0, i64* %4
store i8* null, i8** %5
br label %16
16: ; preds = %41, %15
%17 = load i32*, i32** %2
%18 = call i32 @getline(i8** %5, i64* %4, i32* %17)
store i32 %18, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %42
20: ; preds = %16
%21 = load i8*, i8** %5
store i8* %21, i8** %6
%22 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %7
%23 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %23, i8** %8
%24 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %24, i8** %9
%25 = load i8*, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %30, %27, %20
%34 = load i32, i32* @EINVAL
store i32 %34, i32* @errno
br label %42
35: ; preds = %30
%36 = load i8*, i8** %8
%37 = load i8*, i8** %9
%38 = call i64 @init_cgroups_controller(i8* %36, i8* %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
br label %42
41: ; preds = %35
br label %16
42: ; preds = %40, %33, %16
%43 = load i32, i32* @errno
store i32 %43, i32* %10
%44 = load i8*, i8** %5
%45 = call i32 @free(i8* %44)
%46 = load i32*, i32** %2
%47 = call i64 @fclose(i32* %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* %10
%51 = icmp ne i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %49
%53 = load i32, i32* @errno
store i32 %53, i32* %10
br label %54
54: ; preds = %52, %49, %42
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 0
%57 = zext i1 %56 to i64
%58 = select i1 %56, i32 -1, i32 0
store i32 %58, i32* %1
br label %59
59: ; preds = %54, %14
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/self/cgroup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"\0A\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @init_cgroups() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %44, label %6
6: ; preds = %0
%7 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%8 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%9 = call i32 @getline(i8** %2, i64* %1, i32* %4)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %32
11: ; preds = %6
%12 = bitcast i8** %3 to i8*
br label %13
13: ; preds = %11, %29
%14 = load i8*, i8** %2
store i8* %14, i8** %3
%15 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp ne i8* %15, null
%19 = icmp ne i8* %16, null
%20 = select i1 %18, i1 %19, i1 false
%21 = icmp ne i8* %17, null
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
br label %28
25: ; preds = %13
%26 = call i64 @init_cgroups_controller(i8* %16, i8* %17)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25, %23
br label %32
29: ; preds = %25
%30 = call i32 @getline(i8** %2, i64* %1, i32* %4)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %13, label %32
32: ; preds = %29, %6, %28
%33 = load i32, i32* @errno
%34 = load i8*, i8** %2
%35 = call i32 @free(i8* %34)
%36 = call i64 @fclose(i32* %4)
%37 = icmp sgt i64 %36, -1
%38 = icmp ne i32 %33, 0
%39 = select i1 %37, i1 true, i1 %38
%40 = load i32, i32* @errno
%41 = select i1 %39, i32 %33, i32 %40
%42 = icmp ne i32 %41, 0
%43 = sext i1 %42 to i32
br label %44
44: ; preds = %0, %32
%45 = phi i32 [ %43, %32 ], [ -1, %0 ]
ret i32 %45
}