#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_adj", O_WRONLY);
  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)
mov    -0x24(%rbp),%edi
lea    0xe2b(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
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    1234 <main+0x94>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xddb(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    (%rsi),%rdi
call   1060 <basename@plt>
mov    %rax,%r15
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xe42(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xa(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
jne    1203 <main+0x63>
lea    0xe16(%rip),%rdi
jmp    122a <main+0x8a>
mov    %rax,%rbx
lea    0xe11(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
lea    0xe00(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/oom_adj\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"-18\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [12 x i8] c"/.no_oom/%s\00"
@.str.4 = [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 ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%17 = load i32, i32* %8
%18 = call i32 @close(i32 %17)
%19 = load i8*, i8** %7
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 10
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i8*, i8** %6
%30 = load i8*, i8** %7
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %6
%33 = load i8**, i8*** %5
%34 = call i32 @execvp(i8* %32, i8** %33)
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/oom_adj\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"-18\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [12 x i8] c"/.no_oom/%s\00"
@.str.4 = [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 ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%8 = tail call i32 @close(i32 %6)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%10 = add nsw i64 %9, 10
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = tail call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%18 = tail call i32 @execvp(i8* %11, i8** %1)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
}