#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 ssize_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ format(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ getpid();
int readlink(int /*<<< orphan*/, char *, int);
void install(const char *arg0, int enable_coredump) {
  char path[PATH_MAX];
  ssize_t n;
  FILE *fp;
  n = readlink(format("/proc"
                      "/%d/exe",
                      getpid()),
               path, sizeof(path) - 1);
  if (n == -1)
    fatal("failed to determine program's path");
  path[n] = '\0';
  fp = fopen("/proc"
             "/sys/kernel/core_pattern",
             "w");
  if (fp == NULL)
    fatal("could not open core_pattern for installation");
  fprintf(fp, "|%s %i %%i %%p %%s %%u %%g\n", path, enable_coredump);
  fclose(fp);
  fp = fopen("/proc"
             "/sys/kernel/core_pipe_limit",
             "w");
  if (fp == NULL)
    fatal("could not open core_pipe_limit for installation");
  fprintf(fp, "%d\n", 3);
  fclose(fp);
}


x86 assembly (clang, -O0):
<install>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
call   1070 <format@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f2 <install+0x82>
lea    0xe20(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x40(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xe2b(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122c <install+0xbc>
lea    0xe29(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe44(%rip),%rdi
lea    0xdf2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127c <install+0x10c>
lea    0xe43(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x30(%rbp),%rdi
lea    0xe63(%rip),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    0x2e6c(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
mov    %rbx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
call   1070 <format@plt>
lea    -0x1(%rbx),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1030 <readlink@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11cf <install+0x5f>
lea    0xe43(%rip),%rdi
call   1090 <fatal@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
lea    0xe52(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11fe <install+0x8e>
lea    0xe57(%rip),%rdi
call   1090 <fatal@plt>
lea    0xe78(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe75(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1244 <install+0xd4>
lea    0xe7b(%rip),%rdi
call   1090 <fatal@plt>
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [35 x i8] c"failed to determine program's path\00"
@.str.2 = [30 x i8] c"/proc/sys/kernel/core_pattern\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [45 x i8] c"could not open core_pattern for installation\00"
@.str.5 = [28 x i8] c"|%s %i %%i %%p %%s %%u %%g\0A\00"
@.str.6 = [33 x i8] c"/proc/sys/kernel/core_pipe_limit\00"
@.str.7 = [48 x i8] c"could not open core_pipe_limit for installation\00"
@.str.8 = [4 x i8] c"%d\0A\00"
define void @install(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = call i32 (...) @getpid()
%14 = call i32 @format(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = sub i64 %10, 1
%16 = trunc i64 %15 to i32
%17 = call i32 @readlink(i32 %14, i8* %12, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %12, i64 %24
store i8 0, i8* %25
%26 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %22
%32 = load i32*, i32** %8
%33 = load i32, i32* %4
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %12, i32 %33)
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
%37 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %37, i32** %8
%38 = load i32*, i32** %8
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @fatal(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %31
%43 = load i32*, i32** %8
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%45 = load i32*, i32** %8
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [35 x i8] c"failed to determine program's path\00"
@.str.2 = [30 x i8] c"/proc/sys/kernel/core_pattern\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [45 x i8] c"could not open core_pattern for installation\00"
@.str.5 = [28 x i8] c"|%s %i %%i %%p %%s %%u %%g\0A\00"
@.str.6 = [33 x i8] c"/proc/sys/kernel/core_pipe_limit\00"
@.str.7 = [48 x i8] c"could not open core_pipe_limit for installation\00"
@.str.8 = [4 x i8] c"%d\0A\00"
define void @install(i8* nocapture readnone %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @format(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = add i32 %3, -1
%9 = call i32 @readlink(i32 %7, i8* %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds i8, i8* %5, i64 %14
store i8 0, i8* %15
%16 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %5, i32 %1)
%22 = call i32 @fclose(i32* %16)
%23 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @fatal(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%29 = call i32 @fclose(i32* %23)
ret void
}