#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 */
struct rlimit {
  unsigned long long rlim_cur;
  unsigned long long rlim_max;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_CORE;
unsigned long long const RLIM_INFINITY;
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int system(char *);
int set_corefile(const unsigned long long size) {
  struct rlimit lmt;
  lmt.rlim_cur = lmt.rlim_max = (size == (~0UL) ? RLIM_INFINITY : size);
  if (setrlimit(RLIMIT_CORE, &lmt) != 0)
    return -1;
  return system("echo \"core.%e.%p\" > /proc/sys/kernel/core_pattern");
}


x86 assembly (clang, -O0):
<set_corefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    114a <set_corefile+0x2a>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1152 <set_corefile+0x32>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    %rax,-0x20(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
je     1186 <set_corefile+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <set_corefile+0x75>
lea    0xe7b(%rip),%rdi
call   1030 <system@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_corefile>:
sub    $0x18,%rsp
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rdi
cmovne %rdi,%rax
mov    %rax,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <setrlimit@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     115d <set_corefile+0x3d>
add    $0x18,%rsp
ret
lea    0xea4(%rip),%rdi
call   1030 <system@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = constant i64 0
@RLIMIT_CORE = global i32 0
@.str = [50 x i8] c"echo \22core.%e.%p\22 > /proc/sys/kernel/core_pattern\00"
define i32 @set_corefile(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.rlimit
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = icmp eq i64 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i64, i64* @RLIM_INFINITY
br label %11
9: ; preds = %1
%10 = load i64, i64* %3
br label %11
11: ; preds = %9, %7
%12 = phi i64 [ %8, %7 ], [ %10, %9 ]
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 1
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i64 %12, i64* %14
%15 = load i32, i32* @RLIMIT_CORE
%16 = call i64 @setrlimit(i32 %15, %struct.rlimit* %4)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
store i32 -1, i32* %2
br label %21
19: ; preds = %11
%20 = call i32 @system(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %18
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = constant i64 0
@RLIMIT_CORE = global i32 0
@.str = [50 x i8] c"echo \22core.%e.%p\22 > /proc/sys/kernel/core_pattern\00"
define i32 @set_corefile(i64 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = icmp eq i64 %0, -1
%5 = select i1 %4, i64 0, i64 %0
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i64 %5, i64* %6
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i64 %5, i64* %7
%8 = load i32, i32* @RLIMIT_CORE
%9 = call i64 @setrlimit(i32 %8, %struct.rlimit* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @system(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %1, %11
%14 = phi i32 [ %12, %11 ], [ -1, %1 ]
ret i32 %14
}