#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 /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(int, char *, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void set_oom_adj(char *pid) {
  int fd;
  char filename[128];
  if (snprintf(filename, sizeof(filename), "/proc/%s/oom_adj", pid) < 0)
    die(20, "generating filename string: '%s'\n", filename);
  fd = open(filename, O_WRONLY);
  if (fd < 0)
    die(21, "opening file '%s'\n", filename);
  if (write(fd, "-17", strlen("-17")) < 0)
    die(22, "writting oom adjust value to file: '%s'\n", filename);
  close(fd);
}


x86 assembly (clang, -O0):
<set_oom_adj>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x80,%esi
lea    0xe7a(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0x0,%rax
jge    11ad <set_oom_adj+0x4d>
lea    -0x90(%rbp),%rdx
mov    $0x14,%edi
lea    0xe69(%rip),%rsi
call   1080 <die@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e7 <set_oom_adj+0x87>
lea    -0x90(%rbp),%rdx
mov    $0x15,%edi
lea    0xe51(%rip),%rsi
call   1080 <die@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x94(%rbp)
lea    0xe4f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x94(%rbp),%edi
mov    %eax,%edx
lea    0xe3b(%rip),%rsi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1232 <set_oom_adj+0xd2>
lea    -0x90(%rbp),%rdx
mov    $0x16,%edi
lea    0xe1d(%rip),%rsi
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_oom_adj>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rcx
lea    0xe8c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
test   %rax,%rax
jns    119a <set_oom_adj+0x3a>
lea    0xe84(%rip),%rsi
mov    %rsp,%rdx
mov    $0x14,%edi
call   1080 <die@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c5 <set_oom_adj+0x65>
lea    0xe7b(%rip),%rsi
mov    %rsp,%rdx
mov    $0x15,%edi
call   1080 <die@plt>
lea    0xe7a(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11f9 <set_oom_adj+0x99>
lea    0xe5e(%rip),%rsi
mov    %rsp,%rdx
mov    $0x16,%edi
call   1080 <die@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%s/oom_adj\00"
@.str.1 = [34 x i8] c"generating filename string: '%s'\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [19 x i8] c"opening file '%s'\0A\00"
@.str.3 = [4 x i8] c"-17\00"
@.str.4 = [41 x i8] c"writting oom adjust value to file: '%s'\0A\00"
define void @set_oom_adj(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [128 x i8]
store i8* %0, i8** %2
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = load i8*, i8** %2
%7 = call i64 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%11 = call i32 @die(i32 20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = call i32 @die(i32 21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %12
%22 = load i32, i32* %3
%23 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%24 = call i64 @write(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%28 = call i32 @die(i32 22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %21
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%s/oom_adj\00"
@.str.1 = [34 x i8] c"generating filename string: '%s'\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [19 x i8] c"opening file '%s'\0A\00"
@.str.3 = [4 x i8] c"-17\00"
@.str.4 = [41 x i8] c"writting oom adjust value to file: '%s'\0A\00"
define void @set_oom_adj(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i64 @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @die(i32 20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @die(i32 21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %14
14: ; preds = %12, %8
%15 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%16 = call i64 @write(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @die(i32 22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %20
20: ; preds = %18, %14
%21 = call i32 @close(i32 %10)
ret void
}