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

/* Variables and functions */
int /*<<< orphan*/ addToCGroup(char const *, char *, scalar_t__, long, int);
int /*<<< orphan*/ asprintf(char **, char *, long);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getBeer(long);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, long);
scalar_t__ strstr(char *, char const *);
int /*<<< orphan*/ strtok(char *, char *);
long strtol(char *, int /*<<< orphan*/, int);
void cgroupProcessAndChildrenRecursively(const char *typeSignifier, long pid,
                                         long parentPid) {
  char fileName[128];
  char cgroup[256];
  FILE *cgroupCheck;
  char locationSignifier[64];
  sprintf(locationSignifier, "beerfridge_%d", parentPid);
  if (parentPid != pid && getBeer(pid)) {
    return;
  }
  sprintf(fileName, "/proc/%d/cgroup", pid);
  cgroupCheck = fopen(fileName, "r");
  if (cgroupCheck) {
    while (fgets(cgroup, 100, cgroupCheck)) {
      if (strstr(cgroup, typeSignifier)) {
        if (strstr(cgroup, locationSignifier)) {
          return;
        } else {
          break;
        }
      }
    }
    strtok(cgroup, "\n");
    addToCGroup(typeSignifier, locationSignifier, strstr(cgroup, "/"), pid,
                parentPid == pid);
    char *command;
    asprintf(&command, "ps --ppid %d -o pid=", pid);
    FILE *children = popen(command, "r");
    free(command);
    if (children) {
      char child[64];
      while (fgets(child, 63, children)) {
        cgroupProcessAndChildrenRecursively(typeSignifier, strtol(child, 0, 10),
                                            parentPid);
      }
    }
    pclose(children);
  }
}


x86 assembly (clang, -O0):
<cgroupProcessAndChildrenRecursively>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1f0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe07(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1224 <cgroupProcessAndChildrenRecursively+0x54>
mov    -0x10(%rbp),%rdi
call   10e0 <getBeer@plt>
cmp    $0x0,%rax
je     1224 <cgroupProcessAndChildrenRecursively+0x54>
jmp    13f3 <cgroupProcessAndChildrenRecursively+0x223>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdd8(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x1a8(%rbp)
cmpq   $0x0,-0x1a8(%rbp)
je     13f3 <cgroupProcessAndChildrenRecursively+0x223>
jmp    1268 <cgroupProcessAndChildrenRecursively+0x98>
lea    -0x1a0(%rbp),%rdi
mov    -0x1a8(%rbp),%rdx
mov    $0x64,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12d0 <cgroupProcessAndChildrenRecursively+0x100>
lea    -0x1a0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     12cb <cgroupProcessAndChildrenRecursively+0xfb>
lea    -0x1a0(%rbp),%rdi
lea    -0x1f0(%rbp),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     12c6 <cgroupProcessAndChildrenRecursively+0xf6>
jmp    13f3 <cgroupProcessAndChildrenRecursively+0x223>
jmp    12d0 <cgroupProcessAndChildrenRecursively+0x100>
jmp    1268 <cgroupProcessAndChildrenRecursively+0x98>
lea    -0x1a0(%rbp),%rdi
lea    0xd42(%rip),%rsi
call   10c0 <strtok@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x1f0(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x1a0(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   10f0 <strstr@plt>
mov    -0x250(%rbp),%rdi
mov    -0x248(%rbp),%rsi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%r8d
call   1090 <addToCGroup@plt>
mov    -0x10(%rbp),%rdx
lea    -0x1f8(%rbp),%rdi
lea    0xcd8(%rip),%rsi
call   1080 <asprintf@plt>
mov    -0x1f8(%rbp),%rdi
lea    0xcbf(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x200(%rbp)
mov    -0x1f8(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x200(%rbp)
je     13e7 <cgroupProcessAndChildrenRecursively+0x217>
jmp    138a <cgroupProcessAndChildrenRecursively+0x1ba>
lea    -0x240(%rbp),%rdi
mov    -0x200(%rbp),%rdx
mov    $0x3f,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     13e2 <cgroupProcessAndChildrenRecursively+0x212>
mov    -0x8(%rbp),%rax
mov    %rax,-0x258(%rbp)
lea    -0x240(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    -0x258(%rbp),%rdi
mov    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <cgroupProcessAndChildrenRecursively@plt>
jmp    138a <cgroupProcessAndChildrenRecursively+0x1ba>
jmp    13e7 <cgroupProcessAndChildrenRecursively+0x217>
mov    -0x200(%rbp),%rdi
call   1050 <pclose@plt>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgroupProcessAndChildrenRecursively>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe10(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10d0 <sprintf@plt>
cmp    %r15,%r14
je     1210 <cgroupProcessAndChildrenRecursively+0x40>
mov    %r15,%rdi
call   10e0 <getBeer@plt>
test   %rax,%rax
jne    136a <cgroupProcessAndChildrenRecursively+0x19a>
lea    0xdf7(%rip),%rsi
lea    0x190(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
call   10d0 <sprintf@plt>
lea    0xded(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     136a <cgroupProcessAndChildrenRecursively+0x19a>
mov    %rax,%rbx
lea    0x90(%rsp),%r13
nopl   (%rax)
mov    %r13,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1290 <cgroupProcessAndChildrenRecursively+0xc0>
mov    %r13,%rdi
mov    %r12,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     1250 <cgroupProcessAndChildrenRecursively+0x80>
lea    0x90(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    136a <cgroupProcessAndChildrenRecursively+0x19a>
xor    %ebx,%ebx
cmp    %r15,%r14
sete   %bl
lea    0xd81(%rip),%rsi
lea    0x90(%rsp),%r13
mov    %r13,%rdi
call   10c0 <strtok@plt>
lea    0xd6c(%rip),%rsi
mov    %r13,%rdi
call   10f0 <strstr@plt>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1090 <addToCGroup@plt>
lea    0xd49(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r15,%rdx
call   1080 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xd2a(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %r15,%r15
je     1362 <cgroupProcessAndChildrenRecursively+0x192>
lea    0x50(%rsp),%rdi
mov    $0x3f,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1362 <cgroupProcessAndChildrenRecursively+0x192>
lea    0x50(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <cgroupProcessAndChildrenRecursively@plt>
mov    %rbx,%rdi
mov    $0x3f,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1330 <cgroupProcessAndChildrenRecursively+0x160>
mov    %r15,%rdi
call   1050 <pclose@plt>
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"beerfridge_%d\00"
@.str.1 = [16 x i8] c"/proc/%d/cgroup\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [21 x i8] c"ps --ppid %d -o pid=\00"
define void @cgroupProcessAndChildrenRecursively(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [128 x i8]
%8 = alloca [256 x i8]
%9 = alloca i32*
%10 = alloca [64 x i8]
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca [64 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%15 = load i64, i64* %6
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %15)
%17 = load i64, i64* %6
%18 = load i64, i64* %5
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i64, i64* %5
%22 = call i64 @getBeer(i64 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
br label %88
25: ; preds = %20, %3
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%27 = load i64, i64* %5
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %27)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %88
33: ; preds = %25
br label %34
34: ; preds = %51, %33
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%36 = load i32*, i32** %9
%37 = call i64 @fgets(i8* %35, i32 100, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %34
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%41 = load i8*, i8** %4
%42 = call i64 @strstr(i8* %40, i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%47 = call i64 @strstr(i8* %45, i8* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %88
50: ; preds = %44
br label %52
51: ; preds = %39
br label %34
52: ; preds = %50, %34
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%54 = call i32 @strtok(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%55 = load i8*, i8** %4
%56 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%58 = call i64 @strstr(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%59 = load i64, i64* %5
%60 = load i64, i64* %6
%61 = load i64, i64* %5
%62 = icmp eq i64 %60, %61
%63 = zext i1 %62 to i32
%64 = call i32 @addToCGroup(i8* %55, i8* %56, i64 %58, i64 %59, i32 %63)
%65 = load i64, i64* %5
%66 = call i32 @asprintf(i8** %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 %65)
%67 = load i8*, i8** %11
%68 = call i32* @popen(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %68, i32** %12
%69 = load i8*, i8** %11
%70 = call i32 @free(i8* %69)
%71 = load i32*, i32** %12
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %85
73: ; preds = %52
br label %74
74: ; preds = %79, %73
%75 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%76 = load i32*, i32** %12
%77 = call i64 @fgets(i8* %75, i32 63, i32* %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %84
79: ; preds = %74
%80 = load i8*, i8** %4
%81 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%82 = call i64 @strtol(i8* %81, i32 0, i32 10)
%83 = load i64, i64* %6
br label %74
84: ; preds = %74
br label %85
85: ; preds = %84, %52
%86 = load i32*, i32** %12
%87 = call i32 @pclose(i32* %86)
br label %88
88: ; preds = %24, %49, %85, %25
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"beerfridge_%d\00"
@.str.1 = [16 x i8] c"/proc/%d/cgroup\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [21 x i8] c"ps --ppid %d -o pid=\00"
define void @cgroupProcessAndChildrenRecursively(i8* %0, i64 %1, i64 %2) {
%4 = alloca [128 x i8]
%5 = alloca [256 x i8]
%6 = alloca [64 x i8]
%7 = alloca i8*
%8 = alloca [64 x i8]
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%12 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %2)
%13 = icmp eq i64 %2, %1
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = tail call i64 @getBeer(i64 %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %53
17: ; preds = %14, %3
%18 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%19 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %53, label %21
21: ; preds = %17, %24
%22 = call i64 @fgets(i8* %10, i32 100, i32* %19)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = call i64 @strstr(i8* %10, i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %24
%28 = call i64 @strstr(i8* %10, i8* %11)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %53
30: ; preds = %21, %27
%31 = call i32 @strtok(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = call i64 @strstr(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = zext i1 %13 to i32
%34 = call i32 @addToCGroup(i8* %0, i8* %11, i64 %32, i64 %1, i32 %33)
%35 = bitcast i8** %7 to i8*
%36 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 %1)
%37 = load i8*, i8** %7
%38 = call i32* @popen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
%41 = icmp eq i32* %38, null
br i1 %41, label %51, label %42
42: ; preds = %30
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%44 = call i64 @fgets(i8* %43, i32 63, i32* %38)
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %42, %46
%47 = call i64 @strtol(i8* %43, i32 0, i32 10)
%48 = call i64 @fgets(i8* %43, i32 63, i32* %38)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %46
50: ; preds = %46, %42
br label %51
51: ; preds = %50, %30
%52 = call i32 @pclose(i32* %38)
br label %53
53: ; preds = %17, %51, %27, %14
ret void
}