#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_APPEND;
int O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int dprintf(int, char *, int);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int set_oom_adj_score(int pid, int score) {
  char path[PATH_MAX];
  int fd, len;
  sprintf(path, "/proc/%d/oom_score_adj", pid);
  fd = open(path, O_WRONLY | O_APPEND);
  if (fd < 0)
    return fd;
  len = dprintf(fd, "%d", score);
  if (len < 0) {
    close(fd);
    return len;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<set_oom_adj_score>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edx
lea    0xe7c(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c3 <set_oom_adj_score+0x83>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1212 <set_oom_adj_score+0xd2>
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%edx
lea    0xe47(%rip),%rsi
call   1040 <dprintf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11fc <set_oom_adj_score+0xbc>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1212 <set_oom_adj_score+0xd2>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_oom_adj_score>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%edx
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe93(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e54(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b9 <set_oom_adj_score+0x79>
lea    0xe7b(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
call   1040 <dprintf@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r14d,%ebx
sar    $0x1f,%ebx
and    %r14d,%ebx
mov    %ebx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/proc/%d/oom_score_adj\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @set_oom_adj_score(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* %4
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_APPEND
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i32, i32* %8
store i32 %24, i32* %3
store i32 1, i32* %10
br label %38
25: ; preds = %2
%26 = load i32, i32* %8
%27 = load i32, i32* %5
%28 = call i32 @dprintf(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %9
store i32 %34, i32* %3
store i32 1, i32* %10
br label %38
35: ; preds = %25
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
store i32 0, i32* %3
store i32 1, i32* %10
br label %38
38: ; preds = %35, %31, %23
%39 = load i8*, i8** %6
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [23 x i8] c"/proc/%d/oom_score_adj\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @set_oom_adj_score(i32 %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_APPEND
%9 = or i32 %8, %7
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = tail call i32 @dprintf(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%14 = icmp slt i32 %13, 0
%15 = tail call i32 @close(i32 %10)
%16 = select i1 %14, i32 %13, i32 0
br label %17
17: ; preds = %12, %2
%18 = phi i32 [ %10, %2 ], [ %16, %12 ]
ret i32 %18
}