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

/* Variables and functions */
scalar_t__ read_text(char *, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strtok(char *, char const *);
int cg_find_unified_root(char *root, size_t len) {
  char buf[10 * 4096];
  char *fs, *mount, *type;
  const char delim[] = "\n\t ";
  if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
    return -1;
  for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
    mount = strtok(NULL, delim);
    type = strtok(NULL, delim);
    strtok(NULL, delim);
    strtok(NULL, delim);
    strtok(NULL, delim);
    if (strcmp(fs, "cgroup") == 0 && strcmp(type, "cgroup2") == 0) {
      strncpy(root, mount, len);
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<cg_find_unified_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xea7(%rip),%eax
mov    %eax,-0xa03c(%rbp)
lea    -0xa020(%rbp),%rsi
lea    0xe97(%rip),%rdi
mov    $0xa000,%edx
call   1050 <read_text@plt>
cmp    $0x0,%rax
jg     118d <cg_find_unified_root+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <cg_find_unified_root+0x155>
lea    -0xa020(%rbp),%rdi
lea    -0xa03c(%rbp),%rsi
call   1060 <strtok@plt>
mov    %rax,-0xa028(%rbp)
cmpq   $0x0,-0xa028(%rbp)
je     128e <cg_find_unified_root+0x14e>
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
mov    %rax,-0xa030(%rbp)
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
mov    %rax,-0xa038(%rbp)
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
mov    -0xa028(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    126d <cg_find_unified_root+0x12d>
mov    -0xa038(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    126d <cg_find_unified_root+0x12d>
mov    -0x10(%rbp),%rdi
mov    -0xa030(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1295 <cg_find_unified_root+0x155>
jmp    1272 <cg_find_unified_root+0x132>
lea    -0xa03c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
mov    %rax,-0xa028(%rbp)
jmp    11a7 <cg_find_unified_root+0x67>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cg_find_unified_root>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa028,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
movl   $0x20090a,0xc(%rsp)
lea    0xe9a(%rip),%rdi
lea    0x20(%rsp),%rsi
mov    $0xa000,%edx
call   1050 <read_text@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jle    1245 <cg_find_unified_root+0x105>
lea    0x20(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     1245 <cg_find_unified_root+0x105>
mov    %rax,%r15
mov    %rbp,0x10(%rsp)
mov    %r14,0x18(%rsp)
lea    0xc(%rsp),%rbx
lea    0xe5e(%rip),%rbp
lea    0xe5e(%rip),%r13
jmp    11d2 <cg_find_unified_root+0x92>
nopl   (%rax)
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%r15
test   %rax,%rax
je     1240 <cg_find_unified_root+0x100>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%r14
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%r12
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11c0 <cg_find_unified_root+0x80>
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11c0 <cg_find_unified_root+0x80>
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    0x18(%rsp),%rdx
call   1030 <strncpy@plt>
xor    %ebx,%ebx
jmp    1245 <cg_find_unified_root+0x105>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xa028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.cg_find_unified_root.delim = [4 x i8] c"\0A\09 \00"
@.str = [18 x i8] c"/proc/self/mounts\00"
@.str.1 = [7 x i8] c"cgroup\00"
@.str.2 = [8 x i8] c"cgroup2\00"
define i32 @cg_find_unified_root(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [40960 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [4 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = bitcast [4 x i8]* %10 to i8*
%12 = getelementptr inbounds [40960 x i8], [40960 x i8]* %6, i64 0, i64 0
%13 = call i64 @read_text(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12, i32 40960)
%14 = icmp sle i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %51
16: ; preds = %2
%17 = getelementptr inbounds [40960 x i8], [40960 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%19 = call i8* @strtok(i8* %17, i8* %18)
store i8* %19, i8** %7
br label %20
20: ; preds = %47, %16
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %50
23: ; preds = %20
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%25 = call i8* @strtok(i8* null, i8* %24)
store i8* %25, i8** %8
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%27 = call i8* @strtok(i8* null, i8* %26)
store i8* %27, i8** %9
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%29 = call i8* @strtok(i8* null, i8* %28)
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%31 = call i8* @strtok(i8* null, i8* %30)
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%33 = call i8* @strtok(i8* null, i8* %32)
%34 = load i8*, i8** %7
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %23
%38 = load i8*, i8** %9
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i8*, i8** %4
%43 = load i8*, i8** %8
%44 = load i64, i64* %5
%45 = call i32 @strncpy(i8* %42, i8* %43, i64 %44)
store i32 0, i32* %3
br label %51
46: ; preds = %37, %23
br label %47
47: ; preds = %46
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%49 = call i8* @strtok(i8* null, i8* %48)
store i8* %49, i8** %7
br label %20
50: ; preds = %20
store i32 -1, i32* %3
br label %51
51: ; preds = %50, %41, %15
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/self/mounts\00"
@.str.1 = [7 x i8] c"cgroup\00"
@.str.2 = [8 x i8] c"cgroup2\00"
define i32 @cg_find_unified_root(i8* %0, i64 %1) {
%3 = alloca [40960 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [40960 x i8], [40960 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
store i32 2099466, i32* %4
%7 = call i64 @read_text(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %5, i32 40960)
%8 = icmp slt i64 %7, 1
br i1 %8, label %29, label %9
9: ; preds = %2
%10 = call i8* @strtok(i8* %5, i8* %6)
%11 = icmp eq i8* %10, null
br i1 %11, label %29, label %12
12: ; preds = %9, %26
%13 = phi i8* [ %27, %26 ], [ %10, %9 ]
%14 = call i8* @strtok(i8* null, i8* %6)
%15 = call i8* @strtok(i8* null, i8* %6)
%16 = call i8* @strtok(i8* null, i8* %6)
%17 = call i8* @strtok(i8* null, i8* %6)
%18 = call i8* @strtok(i8* null, i8* %6)
%19 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %12
%22 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @strncpy(i8* %0, i8* %14, i64 %1)
br label %29
26: ; preds = %12, %21
%27 = call i8* @strtok(i8* null, i8* %6)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %12
29: ; preds = %26, %9, %2, %24
%30 = phi i32 [ 0, %24 ], [ -1, %2 ], [ -1, %9 ], [ -1, %26 ]
ret i32 %30
}