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

/* Variables and functions */
int /*<<< orphan*/ controller_in_clist(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int snprintf(char *, int, char *, ...);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
size_t strlen(char *);
char *get_cgroup(pid_t pid, const char *contrl) {
  char fn[256];
  FILE *f;
  char *line = NULL;
  size_t len = 0;
  size_t l = 0;
  int ret;
  char *cgrp = NULL;
  char *c1, *c2;
  if (pid) {
    ret = snprintf(fn, 256, "/proc/%d/cgroup", pid);
  } else {
    ret = snprintf(fn, 256, "/proc/self/cgroup");
  }
  if (ret < 0 || ret >= 256)
    return NULL;
  if (!(f = fopen(fn, "r")))
    return NULL;
  while (getline(&line, &len, f) != -1) {
    if (!line[0])
      continue;
    c1 = strchr(line, ':');
    if (!c1)
      goto out;
    c1++;
    c2 = strchr(c1, ':');
    if (!c2)
      goto out;
    *c2 = '\0';
    if (!controller_in_clist(c1, contrl))
      continue;
    c2++;
    l = strlen(c2);
    if (l && c2[l - 1] == '\n')
      c2[l - 1] = '\0';
    if (strcmp(c2, "/") == 0)
      goto out;
    cgrp = strdup(c2);
    break;
  }
out:
  fclose(f);
  free(line);
  return cgrp;
}


x86 assembly (clang, -O0):
<get_cgroup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x130(%rbp)
movq   $0x0,-0x138(%rbp)
movq   $0x0,-0x140(%rbp)
movq   $0x0,-0x150(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1210 <get_cgroup+0x70>
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x100,%esi
lea    0xe02(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    %eax,-0x144(%rbp)
jmp    1230 <get_cgroup+0x90>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
lea    0xded(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jl     124d <get_cgroup+0xad>
cmpl   $0x100,-0x144(%rbp)
jl     125a <get_cgroup+0xba>
movq   $0x0,-0x8(%rbp)
jmp    141d <get_cgroup+0x27d>
lea    -0x120(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    128b <get_cgroup+0xeb>
movq   $0x0,-0x8(%rbp)
jmp    141d <get_cgroup+0x27d>
jmp    1290 <get_cgroup+0xf0>
mov    -0x128(%rbp),%rdx
lea    -0x130(%rbp),%rdi
lea    -0x138(%rbp),%rsi
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     13f5 <get_cgroup+0x255>
mov    -0x130(%rbp),%rax
cmpb   $0x0,(%rax)
jne    12c8 <get_cgroup+0x128>
jmp    1290 <get_cgroup+0xf0>
mov    -0x130(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jne    12f3 <get_cgroup+0x153>
jmp    13fa <get_cgroup+0x25a>
mov    -0x158(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x160(%rbp)
cmpq   $0x0,-0x160(%rbp)
jne    1330 <get_cgroup+0x190>
jmp    13fa <get_cgroup+0x25a>
mov    -0x160(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x158(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <controller_in_clist@plt>
cmp    $0x0,%eax
jne    1358 <get_cgroup+0x1b8>
jmp    1290 <get_cgroup+0xf0>
mov    -0x160(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x160(%rbp)
mov    -0x160(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
je     13c0 <get_cgroup+0x220>
mov    -0x160(%rbp),%rax
mov    -0x140(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    13c0 <get_cgroup+0x220>
mov    -0x160(%rbp),%rax
mov    -0x140(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x160(%rbp),%rdi
lea    0xc56(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13e2 <get_cgroup+0x242>
jmp    13fa <get_cgroup+0x25a>
mov    -0x160(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x150(%rbp)
jmp    13fa <get_cgroup+0x25a>
mov    -0x128(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x130(%rbp),%rdi
call   1030 <free@plt>
mov    -0x150(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cgroup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
test   %edi,%edi
je     11ef <get_cgroup+0x4f>
mov    %edi,%ecx
lea    0xe2e(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
cmp    $0xff,%eax
jbe    1212 <get_cgroup+0x72>
jmp    12fe <get_cgroup+0x15e>
lea    0xe1a(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
cmp    $0xff,%eax
ja     12fe <get_cgroup+0x15e>
lea    0xe09(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12fe <get_cgroup+0x15e>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rax,%rdx
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     1303 <get_cgroup+0x163>
lea    0x8(%rsp),%r13
xor    %r15d,%r15d
jmp    1277 <get_cgroup+0xd7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rsp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     1306 <get_cgroup+0x166>
mov    (%rsp),%rdi
cmpb   $0x0,(%rdi)
je     1260 <get_cgroup+0xc0>
mov    $0x3a,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1306 <get_cgroup+0x166>
mov    %rax,%r12
add    $0x1,%r12
mov    %r12,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1306 <get_cgroup+0x166>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <controller_in_clist@plt>
test   %eax,%eax
je     1260 <get_cgroup+0xc0>
mov    %rbp,%r14
add    $0x1,%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
je     12dd <get_cgroup+0x13d>
cmpb   $0xa,0x0(%rbp,%rax,1)
jne    12dd <get_cgroup+0x13d>
movb   $0x0,0x0(%rbp,%rax,1)
lea    0xd40(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1303 <get_cgroup+0x163>
mov    %r14,%rdi
call   10c0 <strdup@plt>
mov    %rax,%r15
jmp    1306 <get_cgroup+0x166>
xor    %r15d,%r15d
jmp    1317 <get_cgroup+0x177>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/cgroup\00"
@.str.1 = [18 x i8] c"/proc/self/cgroup\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"/\00"
define i8* @get_cgroup(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* null, i8** %8
store i64 0, i64* %9
store i64 0, i64* %10
store i8* null, i8** %12
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %11
br label %24
21: ; preds = %2
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 256, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 %23, i32* %11
br label %24
24: ; preds = %21, %17
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %11
%29 = icmp sge i32 %28, 256
br i1 %29, label %30, label %31
30: ; preds = %27, %24
store i8* null, i8** %3
br label %103
31: ; preds = %27
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %7
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %31
store i8* null, i8** %3
br label %103
36: ; preds = %31
br label %37
37: ; preds = %67, %46, %36
%38 = load i32*, i32** %7
%39 = call i32 @getline(i8** %8, i64* %9, i32* %38)
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %96
41: ; preds = %37
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
%45 = icmp ne i8 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %41
br label %37
47: ; preds = %41
%48 = load i8*, i8** %8
%49 = call i8* @strchr(i8* %48, i8 signext 58)
store i8* %49, i8** %13
%50 = load i8*, i8** %13
%51 = icmp ne i8* %50, null
br i1 %51, label %53, label %52
52: ; preds = %47
br label %97
53: ; preds = %47
%54 = load i8*, i8** %13
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %13
%56 = load i8*, i8** %13
%57 = call i8* @strchr(i8* %56, i8 signext 58)
store i8* %57, i8** %14
%58 = load i8*, i8** %14
%59 = icmp ne i8* %58, null
br i1 %59, label %61, label %60
60: ; preds = %53
br label %97
61: ; preds = %53
%62 = load i8*, i8** %14
store i8 0, i8* %62
%63 = load i8*, i8** %13
%64 = load i8*, i8** %5
%65 = call i32 @controller_in_clist(i8* %63, i8* %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %61
br label %37
68: ; preds = %61
%69 = load i8*, i8** %14
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %14
%71 = load i8*, i8** %14
%72 = call i64 @strlen(i8* %71)
store i64 %72, i64* %10
%73 = load i64, i64* %10
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %88
75: ; preds = %68
%76 = load i8*, i8** %14
%77 = load i64, i64* %10
%78 = sub i64 %77, 1
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = icmp eq i32 %81, 10
br i1 %82, label %83, label %88
83: ; preds = %75
%84 = load i8*, i8** %14
%85 = load i64, i64* %10
%86 = sub i64 %85, 1
%87 = getelementptr inbounds i8, i8* %84, i64 %86
store i8 0, i8* %87
br label %88
88: ; preds = %83, %75, %68
%89 = load i8*, i8** %14
%90 = call i64 @strcmp(i8* %89, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%91 = icmp eq i64 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %88
br label %97
93: ; preds = %88
%94 = load i8*, i8** %14
%95 = call i8* @strdup(i8* %94)
store i8* %95, i8** %12
br label %96
96: ; preds = %93, %37
br label %97
97: ; preds = %96, %92, %60, %52
%98 = load i32*, i32** %7
%99 = call i32 @fclose(i32* %98)
%100 = load i8*, i8** %8
%101 = call i32 @free(i8* %100)
%102 = load i8*, i8** %12
store i8* %102, i8** %3
br label %103
103: ; preds = %97, %35, %30
%104 = load i8*, i8** %3
ret i8* %104
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/cgroup\00"
@.str.1 = [18 x i8] c"/proc/self/cgroup\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"/\00"
define noalias i8* @get_cgroup(i32 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%8 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%9 = icmp eq i32 %0, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %14
12: ; preds = %2
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
%16 = icmp ugt i32 %15, 255
br i1 %16, label %59, label %17
17: ; preds = %14
%18 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %59, label %20
20: ; preds = %17
%21 = call i32 @getline(i8** %4, i64* %5, i32* %18)
%22 = icmp eq i32 %21, -1
br i1 %22, label %54, label %23
23: ; preds = %20, %27
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %37
%28 = call i32 @getline(i8** %4, i64* %5, i32* %18)
%29 = icmp eq i32 %28, -1
br i1 %29, label %54, label %23
30: ; preds = %23
%31 = call i8* @strchr(i8* %24, i8 signext 58)
%32 = icmp eq i8* %31, null
br i1 %32, label %54, label %33
33: ; preds = %30
%34 = getelementptr inbounds i8, i8* %31, i64 1
%35 = call i8* @strchr(i8* %34, i8 signext 58)
%36 = icmp eq i8* %35, null
br i1 %36, label %54, label %37
37: ; preds = %33
store i8 0, i8* %35
%38 = call i32 @controller_in_clist(i8* %34, i8* %1)
%39 = icmp eq i32 %38, 0
br i1 %39, label %27, label %40
40: ; preds = %37
%41 = getelementptr inbounds i8, i8* %35, i64 1
%42 = call i64 @strlen(i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %40
%45 = getelementptr inbounds i8, i8* %35, i64 %42
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 10
br i1 %47, label %48, label %49
48: ; preds = %44
store i8 0, i8* %45
br label %49
49: ; preds = %48, %44, %40
%50 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %49
%53 = call i8* @strdup(i8* %41)
br label %54
54: ; preds = %30, %33, %27, %20, %52, %49
%55 = phi i8* [ null, %49 ], [ %53, %52 ], [ null, %20 ], [ null, %27 ], [ null, %33 ], [ null, %30 ]
%56 = call i32 @fclose(i32* %18)
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %17, %14, %54
%60 = phi i8* [ %55, %54 ], [ null, %14 ], [ null, %17 ]
ret i8* %60
}