#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 */
int EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ cg_write(char const *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int cg_run(const char *cgroup, int (*fn)(const char *cgroup, void *arg),
           void *arg) {
  int pid, retcode;
  pid = fork();
  if (pid < 0) {
    return pid;
  } else if (pid == 0) {
    char buf[64];
    snprintf(buf, sizeof(buf), "%d", getpid());
    if (cg_write(cgroup, "cgroup.procs", buf))
      exit(EXIT_FAILURE);
    exit(fn(cgroup, arg));
  } else {
    waitpid(pid, &retcode, 0);
    if (WIFEXITED(retcode))
      return WEXITSTATUS(retcode);
    else
      return -1;
  }
}


x86 assembly (clang, -O0):
<cg_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b6 <cg_run+0x36>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125e <cg_run+0xde>
cmpl   $0x0,-0x24(%rbp)
jne    1227 <cg_run+0xa7>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe1f(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    0xe0e(%rip),%rsi
call   1050 <cg_write@plt>
cmp    $0x0,%rax
je     1212 <cg_run+0x92>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   *%rax
mov    %eax,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x28(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1257 <cg_run+0xd7>
mov    -0x28(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    125e <cg_run+0xde>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cg_run>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     11c9 <cg_run+0x49>
je     11d5 <cg_run+0x55>
mov    %rsp,%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    (%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11c4 <cg_run+0x44>
mov    (%rsp),%edi
call   1060 <WEXITSTATUS@plt>
jmp    11c9 <cg_run+0x49>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe1d(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
lea    0xe07(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1050 <cg_write@plt>
test   %rax,%rax
jne    121c <cg_run+0x9c>
mov    %rbx,%rdi
mov    %r14,%rsi
call   *%r15
mov    %eax,%edi
call   1090 <exit@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [13 x i8] c"cgroup.procs\00"
@EXIT_FAILURE = global i32 0
define i32 @cg_run(i8* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x i8]
store i8* %0, i8** %5
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %6
store i8* %2, i8** %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %8
store i32 %15, i32* %4
br label %46
16: ; preds = %3
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%21 = call i32 (...) @getpid()
%22 = call i32 @snprintf(i8* %20, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%25 = call i64 @cg_write(i8* %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%32 = load i8*, i8** %5
%33 = load i8*, i8** %7
%34 = call i32 %31(i8* %32, i8* %33)
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %16
%37 = load i32, i32* %8
%38 = call i32 @waitpid(i32 %37, i32* %9, i32 0)
%39 = load i32, i32* %9
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i32, i32* %9
%44 = call i32 @WEXITSTATUS(i32 %43)
store i32 %44, i32* %4
br label %46
45: ; preds = %36
store i32 -1, i32* %4
br label %46
46: ; preds = %45, %42, %14
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [13 x i8] c"cgroup.procs\00"
@EXIT_FAILURE = global i32 0
define i32 @cg_run(i8* %0, i32 (i8*, i8*)* nocapture %1, i8* %2) {
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (...) @fork()
%8 = icmp slt i32 %7, 0
br i1 %8, label %31, label %9
9: ; preds = %3
%10 = icmp eq i32 %7, 0
br i1 %10, label %11, label %23
11: ; preds = %9
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%13 = tail call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %12, i32 64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i64 @cg_write(i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = call i32 %1(i8* %0, i8* %2)
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %9
%24 = call i32 @waitpid(i32 %7, i32* %4, i32 0)
%25 = load i32, i32* %4
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* %4
%30 = call i32 @WEXITSTATUS(i32 %29)
br label %31
31: ; preds = %23, %3, %28
%32 = phi i32 [ %30, %28 ], [ %7, %3 ], [ -1, %23 ]
ret i32 %32
}