#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 uint64_t;
typedef int pid_t;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ error(int, int, char *, int);
int maybe_mkdir(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ write64(char *, char *, int);
void limit_memory(pid_t pid, uint64_t memlimit) {
  char dir[PATH_MAX];
  int r;
  snprintf(dir, sizeof dir, "/sys/fs/cgroup/memory/%d", pid);
  r = maybe_mkdir(dir, 0700);
  if (r < 0)
    error(1, -r, "can't setup memory cgroup for %d", pid);
  write64(dir, "memory.limit_in_bytes", memlimit);
  write64(dir, "memory.memsw.limit_in_bytes", 0);
  write64(dir, "cgroup.procs", pid);
}


x86 assembly (clang, -O0):
<limit_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe7a(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <maybe_mkdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bf <limit_memory+0x7f>
xor    %esi,%esi
sub    -0x1c(%rbp),%esi
mov    -0x4(%rbp),%ecx
mov    $0x1,%edi
lea    0xe5f(%rip),%rdx
call   1050 <error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1060 <write64@plt>
mov    -0x28(%rbp),%rdi
lea    0xe73(%rip),%rsi
xor    %edx,%edx
call   1060 <write64@plt>
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe7a(%rip),%rsi
call   1060 <write64@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<limit_memory>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x1c0,%esi
call   1040 <maybe_mkdir@plt>
test   %eax,%eax
jns    11a8 <limit_memory+0x68>
neg    %eax
lea    0xe80(%rip),%rdx
mov    $0x1,%edi
mov    %eax,%esi
mov    %r14d,%ecx
call   1050 <error@plt>
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1060 <write64@plt>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <write64@plt>
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <write64@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [25 x i8] c"/sys/fs/cgroup/memory/%d\00"
@.str.1 = [33 x i8] c"can't setup memory cgroup for %d\00"
@.str.2 = [22 x i8] c"memory.limit_in_bytes\00"
@.str.3 = [28 x i8] c"memory.memsw.limit_in_bytes\00"
@.str.4 = [13 x i8] c"cgroup.procs\00"
define void @limit_memory(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = trunc i64 %9 to i32
%13 = load i32, i32* %3
%14 = call i32 @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @maybe_mkdir(i8* %11, i32 448)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i32, i32* %7
%20 = sub nsw i32 0, %19
%21 = load i32, i32* %3
%22 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %18, %2
%24 = load i32, i32* %4
%25 = call i32 @write64(i8* %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = call i32 @write64(i8* %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 0)
%27 = load i32, i32* %3
%28 = call i32 @write64(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [25 x i8] c"/sys/fs/cgroup/memory/%d\00"
@.str.1 = [33 x i8] c"can't setup memory cgroup for %d\00"
@.str.2 = [22 x i8] c"memory.limit_in_bytes\00"
@.str.3 = [28 x i8] c"memory.memsw.limit_in_bytes\00"
@.str.4 = [13 x i8] c"cgroup.procs\00"
define void @limit_memory(i32 %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @maybe_mkdir(i8* %5, i32 448)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = sub nsw i32 0, %7
%11 = call i32 @error(i32 1, i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
12: ; preds = %9, %2
%13 = call i32 @write64(i8* %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%14 = call i32 @write64(i8* %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 0)
%15 = call i32 @write64(i8* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %0)
ret void
}