#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int S_IXUSR;
int WEXITSTATUS(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int atoi(char *const);
int /*<<< orphan*/ close(int);
int fill_mem(int);
int fork();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int strlen(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *const argv[]) {
  if (argc != 2) {
    printf("Usage: ./fillmem mb_count\n");
    return -1;
  }
  int mb_count = atoi(argv[1]);
  int pid = fork();
  if (pid == 0) {
    return fill_mem(mb_count);
  } else {
    int status;
    wait(&status);
    int fd = open("/tmp/fillmem_output.txt", O_WRONLY | O_CREAT | O_TRUNC,
                  S_IRUSR | S_IWUSR | S_IXUSR);
    if (fd < 0) {
      return -1;
    }
    if (WIFSIGNALED(status)) {
      char buf[200];
      sprintf(buf,
              "OOM Killer stopped the program with signal %d, exit code %d\n",
              WTERMSIG(status), WEXITSTATUS(status));
      write(fd, buf, strlen(buf) + 1);
    } else {
      write(fd, "Memory filling was successful\n", 31);
    }
    close(fd);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120b <main+0x3b>
lea    0xe06(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <main+0x186>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    123f <main+0x6f>
mov    -0x14(%rbp),%edi
call   1080 <fill_mem@plt>
mov    %eax,-0x4(%rbp)
jmp    1356 <main+0x186>
lea    -0x1c(%rbp),%rdi
call   10e0 <wait@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
mov    0x2d5d(%rip),%rax
or     (%rax),%edx
mov    0x2d5c(%rip),%rax
or     (%rax),%edx
lea    0xd96(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a3 <main+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <main+0x186>
mov    -0x1c(%rbp),%edi
call   1070 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1333 <main+0x163>
lea    -0xf0(%rbp),%rax
mov    %rax,-0x108(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,-0x100(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    -0x108(%rbp),%rdi
mov    -0x100(%rbp),%edx
mov    %eax,%ecx
lea    0xd44(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0xfc(%rbp)
lea    -0xf0(%rbp),%rax
mov    %rax,-0xf8(%rbp)
lea    -0xf0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xfc(%rbp),%edi
mov    -0xf8(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1030 <write@plt>
jmp    1347 <main+0x177>
mov    -0x20(%rbp),%edi
lea    0xd33(%rip),%rsi
mov    $0x1f,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xe0,%rsp
cmp    $0x2,%edi
jne    129c <main+0xcc>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     12af <main+0xdf>
lea    0xc(%rsp),%rdi
call   10e0 <wait@plt>
mov    0x2da1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2d96(%rip),%rax
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9c(%rip),%rax
or     (%rax),%edx
lea    0xdbb(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     12c1 <main+0xf1>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     12c8 <main+0xf8>
mov    0xc(%rsp),%edi
call   1090 <WTERMSIG@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xd9f(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
call   10d0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %r14,%rsi
jmp    12d6 <main+0x106>
lea    0xdd1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    12e4 <main+0x114>
mov    %ebx,%edi
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <fill_mem@plt>
mov    $0xffffffff,%eax
jmp    12e4 <main+0x114>
lea    0xd86(%rip),%rsi
mov    %ebx,%edi
mov    $0x1f,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: ./fillmem mb_count\0A\00"
@.str.1 = [24 x i8] c"/tmp/fillmem_output.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.2 = [61 x i8] c"OOM Killer stopped the program with signal %d, exit code %d\0A\00"
@.str.3 = [31 x i8] c"Memory filling was successful\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = call i32 (...) @fork()
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* %6
%25 = call i32 @fill_mem(i32 %24)
store i32 %25, i32* %3
br label %65
26: ; preds = %15
%27 = call i32 @wait(i32* %8)
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IXUSR
%37 = or i32 %35, %36
%38 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %26
store i32 -1, i32* %3
br label %65
42: ; preds = %26
%43 = load i32, i32* %8
%44 = call i64 @WIFSIGNALED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = getelementptr inbounds [200 x i8], [200 x i8]* %10, i64 0, i64 0
%48 = load i32, i32* %8
%49 = call i32 @WTERMSIG(i32 %48)
%50 = load i32, i32* %8
%51 = call i32 @WEXITSTATUS(i32 %50)
%52 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %51)
%53 = load i32, i32* %9
%54 = getelementptr inbounds [200 x i8], [200 x i8]* %10, i64 0, i64 0
%55 = getelementptr inbounds [200 x i8], [200 x i8]* %10, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = add nsw i32 %56, 1
%58 = call i32 @write(i32 %53, i8* %54, i32 %57)
br label %62
59: ; preds = %42
%60 = load i32, i32* %9
%61 = call i32 @write(i32 %60, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 31)
br label %62
62: ; preds = %59, %46
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
store i32 0, i32* %3
br label %65
65: ; preds = %62, %41, %23, %13
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str.1 = [24 x i8] c"/tmp/fillmem_output.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str.2 = [61 x i8] c"OOM Killer stopped the program with signal %d, exit code %d\0A\00"
@.str.3 = [31 x i8] c"Memory filling was successful\0A\00"
@str = [26 x i8] c"Usage: ./fillmem mb_count\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [200 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %51
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @fill_mem(i32 %11)
br label %51
16: ; preds = %8
%17 = bitcast i32* %3 to i8*
%18 = call i32 @wait(i32* %3)
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IXUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %49, label %31
31: ; preds = %16
%32 = load i32, i32* %3
%33 = call i64 @WIFSIGNALED(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %45, label %35
35: ; preds = %31
%36 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%37 = load i32, i32* %3
%38 = call i32 @WTERMSIG(i32 %37)
%39 = load i32, i32* %3
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %40)
%42 = call i32 @strlen(i8* %36)
%43 = add nsw i32 %42, 1
%44 = call i32 @write(i32 %29, i8* %36, i32 %43)
br label %47
45: ; preds = %31
%46 = call i32 @write(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 31)
br label %47
47: ; preds = %45, %35
%48 = call i32 @close(i32 %29)
br label %49
49: ; preds = %16, %47
%50 = phi i32 [ 0, %47 ], [ -1, %16 ]
br label %51
51: ; preds = %14, %49, %6
%52 = phi i32 [ -1, %6 ], [ %15, %14 ], [ %50, %49 ]
ret i32 %52
}