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

/* Variables and functions */
int /*<<< orphan*/ FILENAME_MAX;
scalar_t__ cgroupCreateInherit(char const *, char const *, char const *,
                               char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            char const *);
int /*<<< orphan*/ strcmp(char const *, char *);
int cgroupCreateType(const char *cgid, const char *prefix, const char *type) {
  char path[FILENAME_MAX + 1];
  snprintf(path, sizeof(path), "%s/%s/%s", prefix, type, cgid);
  if (mkdir(path, 0700))
    return -1;
  if (!strcmp(type, "cpuset")) {
    if (cgroupCreateInherit(cgid, prefix, type, "cpuset.mems") ||
        cgroupCreateInherit(cgid, prefix, type, "cpuset.cpus")) {
      rmdir(path);
      return -1;
    }
  }
  pid_t pid = getpid();
  snprintf(path, sizeof(path), "%s/%s/%s/tasks", prefix, type, cgid);
  FILE *fp = fopen(path, "we");
  if (fp == NULL || fprintf(fp, "%d", pid) <= 0) {
    if (fp != NULL)
      fclose(fp);
    snprintf(path, sizeof(path), "%s/%s/%s", prefix, type, cgid);
    rmdir(path);
    return -1;
  }
  fclose(fp);
  int rpid = 0;
  fp = fopen(path, "re");
  if (fp == NULL || fscanf(fp, "%d", &rpid) != 1 || rpid != pid) {
    if (fp != NULL)
      fclose(fp);
    snprintf(path, sizeof(path), "%s/%s/%s", prefix, type, cgid);
    rmdir(path);
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<cgroupCreateType>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xe04(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     122c <cgroupCreateType+0x8c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1427 <cgroupCreateType+0x287>
mov    -0x20(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    12aa <cgroupCreateType+0x10a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdb8(%rip),%rcx
call   1090 <cgroupCreateInherit@plt>
cmp    $0x0,%rax
jne    1289 <cgroupCreateType+0xe9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xda2(%rip),%rcx
call   1090 <cgroupCreateInherit@plt>
cmp    $0x0,%rax
je     12a5 <cgroupCreateType+0x105>
mov    -0x50(%rbp),%rdi
call   1060 <rmdir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1427 <cgroupCreateType+0x287>
jmp    12aa <cgroupCreateType+0x10a>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,-0x38(%rbp)
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xd55(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    0xd54(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1314 <cgroupCreateType+0x174>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd35(%rip),%rsi
call   10b0 <fprintf@plt>
cmp    $0x0,%rax
jg     1366 <cgroupCreateType+0x1c6>
cmpq   $0x0,-0x40(%rbp)
je     1328 <cgroupCreateType+0x188>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xcbb(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   1060 <rmdir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1427 <cgroupCreateType+0x287>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x50(%rbp),%rdi
movl   $0x0,-0x44(%rbp)
lea    0xcbc(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     13be <cgroupCreateType+0x21e>
mov    -0x40(%rbp),%rdi
lea    0xc9a(%rip),%rsi
lea    -0x44(%rbp),%rdx
call   1080 <fscanf@plt>
cmp    $0x1,%eax
jne    13be <cgroupCreateType+0x21e>
mov    -0x44(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     1410 <cgroupCreateType+0x270>
cmpq   $0x0,-0x40(%rbp)
je     13d2 <cgroupCreateType+0x232>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xc11(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   1060 <rmdir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1427 <cgroupCreateType+0x287>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgroupCreateType>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e27(%rip),%rax
mov    (%rax),%r13d
add    $0x1,%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe20(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r14,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1070 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jne    134c <cgroupCreateType+0x1ac>
mov    %r14,-0x30(%rbp)
lea    0xdef(%rip),%rsi
mov    %r15,%rdi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1305 <cgroupCreateType+0x165>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
lea    0xded(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    -0x30(%rbp),%rcx
mov    %r15,%r8
mov    %r12,-0x50(%rbp)
mov    %r12,%r9
call   1070 <snprintf@plt>
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12e6 <cgroupCreateType+0x146>
mov    %rax,%r12
lea    0xdc8(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   10b0 <fprintf@plt>
mov    %rax,-0x40(%rbp)
mov    %r12,%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x40(%rbp)
jle    12e6 <cgroupCreateType+0x146>
mov    %r14d,-0x40(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xd9b(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12e6 <cgroupCreateType+0x146>
mov    %rax,%r14
lea    0xd81(%rip),%rsi
lea    -0x34(%rbp),%rdx
mov    %rax,%rdi
call   1080 <fscanf@plt>
mov    %eax,%r12d
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    %r14,%rdi
call   1050 <fclose@plt>
cmp    $0x1,%r12d
jne    12e6 <cgroupCreateType+0x146>
xor    %ecx,%ecx
mov    -0x44(%rbp),%eax
cmp    -0x40(%rbp),%eax
je     134c <cgroupCreateType+0x1ac>
lea    0xd13(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    -0x30(%rbp),%rcx
mov    %r15,%r8
mov    -0x50(%rbp),%r9
call   1070 <snprintf@plt>
jmp    133f <cgroupCreateType+0x19f>
lea    0xd04(%rip),%rcx
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
mov    %r15,%rdx
call   1090 <cgroupCreateInherit@plt>
test   %rax,%rax
jne    133f <cgroupCreateType+0x19f>
lea    0xcf5(%rip),%rcx
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
mov    %r15,%rdx
call   1090 <cgroupCreateInherit@plt>
test   %rax,%rax
je     122a <cgroupCreateType+0x8a>
mov    %rbx,%rdi
call   1060 <rmdir@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [7 x i8] c"cpuset\00"
@.str.2 = [12 x i8] c"cpuset.mems\00"
@.str.3 = [12 x i8] c"cpuset.cpus\00"
@.str.4 = [15 x i8] c"%s/%s/%s/tasks\00"
@.str.5 = [3 x i8] c"we\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [3 x i8] c"re\00"
define i32 @cgroupCreateType(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i32, i32* @FILENAME_MAX
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
%19 = trunc i64 %16 to i32
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = load i8*, i8** %5
%23 = call i32 @snprintf(i8* %18, i32 %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %21, i8* %22)
%24 = call i64 @mkdir(i8* %18, i32 448)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %10
br label %104
27: ; preds = %3
%28 = load i8*, i8** %7
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %46, label %31
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i8*, i8** %6
%34 = load i8*, i8** %7
%35 = call i64 @cgroupCreateInherit(i8* %32, i8* %33, i8* %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %31
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = load i8*, i8** %7
%41 = call i64 @cgroupCreateInherit(i8* %38, i8* %39, i8* %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37, %31
%44 = call i32 @rmdir(i8* %18)
store i32 -1, i32* %4
store i32 1, i32* %10
br label %104
45: ; preds = %37
br label %46
46: ; preds = %45, %27
%47 = call i32 (...) @getpid()
store i32 %47, i32* %11
%48 = trunc i64 %16 to i32
%49 = load i8*, i8** %6
%50 = load i8*, i8** %7
%51 = load i8*, i8** %5
%52 = call i32 @snprintf(i8* %18, i32 %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %49, i8* %50, i8* %51)
%53 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %53, i32** %12
%54 = load i32*, i32** %12
%55 = icmp eq i32* %54, null
br i1 %55, label %61, label %56
56: ; preds = %46
%57 = load i32*, i32** %12
%58 = load i32, i32* %11
%59 = call i64 @fprintf(i32* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %58)
%60 = icmp sle i64 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %56, %46
%62 = load i32*, i32** %12
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = trunc i64 %16 to i32
%69 = load i8*, i8** %6
%70 = load i8*, i8** %7
%71 = load i8*, i8** %5
%72 = call i32 @snprintf(i8* %18, i32 %68, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %69, i8* %70, i8* %71)
%73 = call i32 @rmdir(i8* %18)
store i32 -1, i32* %4
store i32 1, i32* %10
br label %104
74: ; preds = %56
%75 = load i32*, i32** %12
%76 = call i32 @fclose(i32* %75)
store i32 0, i32* %13
%77 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %77, i32** %12
%78 = load i32*, i32** %12
%79 = icmp eq i32* %78, null
br i1 %79, label %88, label %80
80: ; preds = %74
%81 = load i32*, i32** %12
%82 = call i32 @fscanf(i32* %81, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %13)
%83 = icmp ne i32 %82, 1
br i1 %83, label %88, label %84
84: ; preds = %80
%85 = load i32, i32* %13
%86 = load i32, i32* %11
%87 = icmp ne i32 %85, %86
br i1 %87, label %88, label %101
88: ; preds = %84, %80, %74
%89 = load i32*, i32** %12
%90 = icmp ne i32* %89, null
br i1 %90, label %91, label %94
91: ; preds = %88
%92 = load i32*, i32** %12
%93 = call i32 @fclose(i32* %92)
br label %94
94: ; preds = %91, %88
%95 = trunc i64 %16 to i32
%96 = load i8*, i8** %6
%97 = load i8*, i8** %7
%98 = load i8*, i8** %5
%99 = call i32 @snprintf(i8* %18, i32 %95, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %96, i8* %97, i8* %98)
%100 = call i32 @rmdir(i8* %18)
store i32 -1, i32* %4
store i32 1, i32* %10
br label %104
101: ; preds = %84
%102 = load i32*, i32** %12
%103 = call i32 @fclose(i32* %102)
store i32 0, i32* %4
store i32 1, i32* %10
br label %104
104: ; preds = %101, %94, %67, %43, %26
%105 = load i8*, i8** %8
%106 = load i32, i32* %4
ret i32 %106
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [7 x i8] c"cpuset\00"
@.str.2 = [12 x i8] c"cpuset.mems\00"
@.str.3 = [12 x i8] c"cpuset.cpus\00"
@.str.4 = [15 x i8] c"%s/%s/%s/tasks\00"
@.str.5 = [3 x i8] c"we\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [3 x i8] c"re\00"
define i32 @cgroupCreateType(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = load i32, i32* @FILENAME_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i8* %0)
%10 = call i64 @mkdir(i8* %8, i32 448)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %51
12: ; preds = %3
%13 = call i32 @strcmp(i8* dereferenceable(1) %2, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = call i64 @cgroupCreateInherit(i8* %0, i8* %1, i8* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i64 @cgroupCreateInherit(i8* %0, i8* %1, i8* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18, %15
%22 = call i32 @rmdir(i8* %8)
br label %51
23: ; preds = %18, %12
%24 = call i32 (...) @getpid()
%25 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %2, i8* %0)
%26 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = call i64 @fprintf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%30 = icmp slt i64 %29, 1
%31 = call i32 @fclose(i32* %26)
br i1 %30, label %32, label %35
32: ; preds = %28, %23
%33 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i8* %0)
%34 = call i32 @rmdir(i8* %8)
br label %51
35: ; preds = %28
%36 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%37 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %46, label %39
39: ; preds = %35
%40 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %4)
%41 = icmp eq i32 %40, 1
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, %24
%44 = select i1 %41, i1 %43, i1 false
%45 = call i32 @fclose(i32* %37)
br i1 %44, label %49, label %46
46: ; preds = %39, %35
%47 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i8* %0)
%48 = call i32 @rmdir(i8* %8)
br label %49
49: ; preds = %39, %46
%50 = phi i32 [ -1, %46 ], [ 0, %39 ]
br label %51
51: ; preds = %32, %49, %3, %21
%52 = phi i32 [ -1, %21 ], [ -1, %3 ], [ -1, %32 ], [ %50, %49 ]
ret i32 %52
}