#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*/ SYS_gettid;
int getpid();
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
void *mem1(void *tr) {
  int size = 0;
  int chunk_size = 512;
  void *p = NULL;
  char cmd[128];
  sprintf(cmd, "echo %ld >> /sys/fs/cgroup/memory/zz/tasks ",
          syscall(SYS_gettid));
  system(cmd);
  while (1) {
    if ((p = malloc(chunk_size)) == NULL) {
      printf("out of memory!!\n");
      break;
    }
    memset(p, 1, chunk_size);
    size += chunk_size;
    printf("[%d] - memory is allocated [%8d] bytes \n", getpid(), size);
    sleep(1);
  }
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<mem1>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x200,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%edx
lea    0xe1f(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1040 <system@plt>
mov    -0x18(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    121b <mem1+0x8b>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125e <mem1+0xce>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11f2 <mem1+0x62>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem1>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1080 <syscall@plt>
lea    0xe41(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
mov    $0x200,%ebx
mov    $0x200,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1236 <mem1+0x96>
lea    0xe3d(%rip),%r14
nop
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x200,%edi
call   10a0 <malloc@plt>
add    $0x200,%ebx
test   %rax,%rax
jne    11f0 <mem1+0x50>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
add    $0x88,%rsp
pop    %rbx
pop    %r14
jmp    1090 <pthread_exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"echo %ld >> /sys/fs/cgroup/memory/zz/tasks \00"
@SYS_gettid = global i32 0
@.str.1 = [17 x i8] c"out of memory!!\0A\00"
@.str.2 = [41 x i8] c"[%d] - memory is allocated [%8d] bytes \0A\00"
define i8* @mem1(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [128 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 512, i32* %5
store i8* null, i8** %6
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* @SYS_gettid
%10 = call i32 @syscall(i32 %9)
%11 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%13 = call i32 @system(i8* %12)
br label %14
14: ; preds = %1, %20
%15 = load i32, i32* %5
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %31
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = load i32, i32* %5
%23 = call i32 @memset(i8* %21, i32 1, i32 %22)
%24 = load i32, i32* %5
%25 = load i32, i32* %4
%26 = add nsw i32 %25, %24
store i32 %26, i32* %4
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28)
%30 = call i32 @sleep(i32 1)
br label %14
31: ; preds = %18
%32 = call i32 @pthread_exit(i32* null)
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@.str = [44 x i8] c"echo %ld >> /sys/fs/cgroup/memory/zz/tasks \00"
@SYS_gettid = global i32 0
@.str.2 = [41 x i8] c"[%d] - memory is allocated [%8d] bytes \0A\00"
@str = [16 x i8] c"out of memory!!\00"
define noalias i8* @mem1(i8* nocapture readnone %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SYS_gettid
%5 = tail call i32 @syscall(i32 %4)
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 @system(i8* %3)
%8 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %13, %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @pthread_exit(i32* null)
ret i8* undef
13: ; preds = %1, %13
%14 = phi i8* [ %21, %13 ], [ %8, %1 ]
%15 = phi i32 [ %17, %13 ], [ 0, %1 ]
%16 = tail call i32 @memset(i8* %14, i32 1, i32 512)
%17 = add nuw nsw i32 %15, 512
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %17)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%22 = icmp eq i8* %21, null
br i1 %22, label %10, label %13
}