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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char *, void *);
int /*<<< orphan*/ getpid();
char *ltoa(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ write(int, char *, int);
void setcg(char *cg) {
  int cgl = strlen("/sys/fs/cgroup") + strlen(cg) + 15;
  char buf[cgl];
  int fd;
  char *pid;
  char *cgp;
  if (strchr(cg, '/')) {
    cgp = cg;
  } else {
    strcpy(buf, "/sys/fs/cgroup");
    strcat(buf, "/");
    strcat(buf, cg);
    strcat(buf, "/tasks");
    cgp = buf;
  }
  if ((fd = open(cgp, O_WRONLY)) < 0)
    die("Can't open ", cgp, ((void *)-1));
  pid = ltoa(getpid());
  if (write(fd, pid, strlen(pid)) <= 0)
    die("Can't add process to ", cgp, ((void *)-1));
  close(fd);
}


x86 assembly (clang, -O0):
<setcg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4d(%rip),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x44(%rbp),%ecx
mov    %eax,%edx
mov    %ecx,%eax
mov    %edx,%ecx
lea    0xf(%rax,%rcx,1),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
cmp    $0x0,%rax
je     121f <setcg+0x7f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1264 <setcg+0xc4>
mov    -0x40(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x40(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1299 <setcg+0xf9>
mov    -0x38(%rbp),%rsi
lea    0xd8b(%rip),%rdi
mov    $0xffffffffffffffff,%rdx
call   10b0 <die@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%edi
call   1030 <ltoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jg     12f1 <setcg+0x151>
mov    -0x38(%rbp),%rsi
lea    0xd3f(%rip),%rdi
mov    $0xffffffffffffffff,%rdx
call   10b0 <die@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setcg>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe4c(%rip),%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1070 <strlen@plt>
add    %ebx,%eax
add    $0xf,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %r14,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     11f0 <setcg+0x50>
mov    %r14,%r15
jmp    1228 <setcg+0x88>
lea    0xe09(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
lea    0xe09(%rip),%rsi
mov    %r15,%rdi
call   10c0 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
lea    0xdf1(%rip),%rsi
mov    %r15,%rdi
call   10c0 <strcat@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1256 <setcg+0xb6>
lea    0xdd1(%rip),%rdi
mov    %r15,%rsi
mov    $0xffffffffffffffff,%rdx
call   10b0 <die@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%edi
call   1030 <ltoa@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jg     1297 <setcg+0xf7>
lea    0xd9c(%rip),%rdi
mov    %r15,%rsi
mov    $0xffffffffffffffff,%rdx
call   10b0 <die@plt>
mov    %r14d,%edi
call   1090 <close@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/sys/fs/cgroup\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"/tasks\00"
@O_WRONLY = global i32 0
@.str.3 = [12 x i8] c"Can't open \00"
@.str.4 = [22 x i8] c"Can't add process to \00"
define void @setcg(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %2
%9 = call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 15
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %4
%17 = alloca i8, i64 %15
store i64 %15, i64* %5
%18 = load i8*, i8** %2
%19 = call i64 @strchr(i8* %18, i8 signext 47)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = load i8*, i8** %2
store i8* %22, i8** %8
br label %29
23: ; preds = %1
%24 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8*, i8** %2
%27 = call i32 @strcat(i8* %17, i8* %26)
%28 = call i32 @strcat(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %17, i8** %8
br label %29
29: ; preds = %23, %21
%30 = load i8*, i8** %8
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %8
%36 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* inttoptr (i64 -1 to i8*))
br label %37
37: ; preds = %34, %29
%38 = call i32 (...) @getpid()
%39 = call i8* @ltoa(i32 %38)
store i8* %39, i8** %7
%40 = load i32, i32* %6
%41 = load i8*, i8** %7
%42 = load i8*, i8** %7
%43 = call i32 @strlen(i8* %42)
%44 = call i64 @write(i32 %40, i8* %41, i32 %43)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %37
%47 = load i8*, i8** %8
%48 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %47, i8* inttoptr (i64 -1 to i8*))
br label %49
49: ; preds = %46, %37
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/sys/fs/cgroup\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"/tasks\00"
@O_WRONLY = global i32 0
@.str.3 = [12 x i8] c"Can't open \00"
@.str.4 = [22 x i8] c"Can't add process to \00"
define void @setcg(i8* %0) {
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* %0)
%4 = add i32 %2, 15
%5 = add i32 %4, %3
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i64 @strchr(i8* %0, i8 signext 47)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @strcat(i8* %7, i8* %0)
%14 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %1, %10
%16 = phi i8* [ %7, %10 ], [ %0, %1 ]
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %16, i8* inttoptr (i64 -1 to i8*))
br label %22
22: ; preds = %20, %15
%23 = call i32 (...) @getpid()
%24 = call i8* @ltoa(i32 %23)
%25 = call i32 @strlen(i8* %24)
%26 = call i64 @write(i32 %18, i8* %24, i32 %25)
%27 = icmp slt i64 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %16, i8* inttoptr (i64 -1 to i8*))
br label %30
30: ; preds = %28, %22
%31 = call i32 @close(i32 %18)
ret void
}