#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;
char *basename(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
char *malloc(scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  char *realprog;
  char *bn = basename(argv[0]);
  int fd;
  fd = open("/proc/self/oom_score_adj", O_WRONLY);
  if (fd >= 0) {
    write(fd, "-1000", 5);
    close(fd);
  } else {
    fd = open("/proc/self/oom_adj", O_WRONLY);
    if (fd >= 0) {
      write(fd, "-18", 3);
      close(fd);
    }
  }
  realprog = malloc(strlen(bn) + 10);
  if (!realprog) {
    perror("malloc");
    exit(1);
  }
  sprintf(realprog, "/.no_oom/%s", bn);
  execvp(realprog, argv);
  perror("execvp");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <basename@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1209 <main+0x69>
mov    -0x24(%rbp),%edi
lea    0xe27(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    124c <main+0xac>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1247 <main+0xa7>
mov    -0x24(%rbp),%edi
lea    0xdfd(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    124c <main+0xac>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0xa,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1286 <main+0xe6>
lea    0xdbf(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xda8(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    (%rsi),%rdi
call   1060 <basename@plt>
mov    %rax,%r15
mov    0x2e1c(%rip),%rbx
mov    (%rbx),%esi
lea    0xe3b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e0 <main+0x40>
mov    %eax,%ebp
lea    0xe42(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
jmp    1202 <main+0x62>
mov    (%rbx),%esi
lea    0xe36(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     120e <main+0x6e>
mov    %eax,%ebp
lea    0xe37(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xa(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
jne    122d <main+0x8d>
lea    0xe0b(%rip),%rdi
jmp    1254 <main+0xb4>
mov    %rax,%rbp
lea    0xe06(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"/proc/self/oom_score_adj\00"
@O_WRONLY = global i32 0
@.str.1 = [6 x i8] c"-1000\00"
@.str.2 = [19 x i8] c"/proc/self/oom_adj\00"
@.str.3 = [4 x i8] c"-18\00"
@.str.4 = [7 x i8] c"malloc\00"
@.str.5 = [12 x i8] c"/.no_oom/%s\00"
@.str.6 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = call i8* @basename(i8* %11)
store i8* %12, i8** %7
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* %8
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%20 = load i32, i32* %8
%21 = call i32 @close(i32 %20)
br label %33
22: ; preds = %2
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* %8
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %27, %22
br label %33
33: ; preds = %32, %17
%34 = load i8*, i8** %7
%35 = call i64 @strlen(i8* %34)
%36 = add nsw i64 %35, 10
%37 = call i8* @malloc(i64 %36)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %43, label %40
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = load i8*, i8** %6
%45 = load i8*, i8** %7
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %6
%48 = load i8**, i8*** %5
%49 = call i32 @execvp(i8* %47, i8** %48)
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [25 x i8] c"/proc/self/oom_score_adj\00"
@O_WRONLY = global i32 0
@.str.1 = [6 x i8] c"-1000\00"
@.str.2 = [19 x i8] c"/proc/self/oom_adj\00"
@.str.3 = [4 x i8] c"-18\00"
@.str.4 = [7 x i8] c"malloc\00"
@.str.5 = [12 x i8] c"/.no_oom/%s\00"
@.str.6 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i8*, i8** %1
%4 = tail call i8* @basename(i8* %3)
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %16
10: ; preds = %2
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = tail call i32 @write(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
br label %16
16: ; preds = %8, %14
%17 = phi i32 [ %12, %14 ], [ %6, %8 ]
%18 = tail call i32 @close(i32 %17)
br label %19
19: ; preds = %16, %10
%20 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%21 = add nsw i64 %20, 10
%22 = tail call i8* @malloc(i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = tail call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%29 = tail call i32 @execvp(i8* %22, i8** %1)
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
}