#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(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    1278 <cg_find_unified_root+0x138>
lea    -0xa020(%rbp),%rdi
lea    -0xa03c(%rbp),%rsi
call   1060 <strtok@plt>
mov    %rax,-0xa028(%rbp)
cmpq   $0x0,-0xa028(%rbp)
je     1271 <cg_find_unified_root+0x131>
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    -0xa038(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1250 <cg_find_unified_root+0x110>
mov    -0x10(%rbp),%rdi
mov    -0xa030(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <cg_find_unified_root+0x138>
jmp    1255 <cg_find_unified_root+0x115>
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    $0xa018,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0x20090a,0x4(%rsp)
lea    0xe9a(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa000,%edx
call   1050 <read_text@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
jle    121c <cg_find_unified_root+0xdc>
lea    0x10(%rsp),%rdi
lea    0x4(%rsp),%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     121c <cg_find_unified_root+0xdc>
mov    %rbx,0x8(%rsp)
lea    0x4(%rsp),%rbx
lea    0xe65(%rip),%r13
nopl   (%rax)
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbp
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%r14
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    %r14,%rdi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1209 <cg_find_unified_root+0xc9>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    11b0 <cg_find_unified_root+0x70>
jmp    121c <cg_find_unified_root+0xdc>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%rdx
call   1030 <strncpy@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0xa018,%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 = [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 %47
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 = %43, %16
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %46
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** %9
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %23
%38 = load i8*, i8** %4
%39 = load i8*, i8** %8
%40 = load i64, i64* %5
%41 = call i32 @strncpy(i8* %38, i8* %39, i64 %40)
store i32 0, i32* %3
br label %47
42: ; preds = %23
br label %43
43: ; preds = %42
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%45 = call i8* @strtok(i8* null, i8* %44)
store i8* %45, i8** %7
br label %20
46: ; preds = %20
store i32 -1, i32* %3
br label %47
47: ; preds = %46, %37, %15
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/self/mounts\00"
@.str.1 = [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 %25, label %9
9: ; preds = %2
%10 = call i8* @strtok(i8* %5, i8* %6)
%11 = icmp eq i8* %10, null
br i1 %11, label %25, label %12
12: ; preds = %9, %22
%13 = call i8* @strtok(i8* null, i8* %6)
%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 i64 @strcmp(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @strncpy(i8* %0, i8* %13, i64 %1)
br label %25
22: ; preds = %12
%23 = call i8* @strtok(i8* null, i8* %6)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %12
25: ; preds = %22, %9, %2, %20
%26 = phi i32 [ 0, %20 ], [ -1, %2 ], [ -1, %9 ], [ -1, %22 ]
ret i32 %26
}