#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 {
  void *rlim_max;
  void *rlim_cur;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_CORE;
void *RLIM_INFINITY;
int /*<<< orphan*/ SIGSEGV;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ do_sleep(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ raise(int /*<<< orphan*/);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ sigdelset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
void coredump(void) {
  static int dumped = 0;
  struct rlimit rlim;
  sigset_t mask;
  if (dumped)
    return;
  dumped = 1;
  if (fork() != 0)
    return;
  sigfillset(&mask);
  sigprocmask(SIG_SETMASK, &mask, NULL);
  rlim.rlim_cur = RLIM_INFINITY;
  rlim.rlim_max = RLIM_INFINITY;
  setrlimit(RLIMIT_CORE, &rlim);
  chdir("/");
  signal(SIGSEGV, SIG_DFL);
  raise(SIGSEGV);
  sigdelset(&mask, SIGSEGV);
  sigprocmask(SIG_SETMASK, &mask, NULL);
  do_sleep(5);
  exit(0);
}


x86 assembly (clang, -O0):
<coredump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpl   $0x0,0x2ec9(%rip)
je     11ba <coredump+0x1a>
jmp    1291 <coredump+0xf1>
movl   $0x1,0x2eb4(%rip)
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     11da <coredump+0x3a>
jmp    1291 <coredump+0xf1>
lea    -0x14(%rbp),%rdi
call   1070 <sigfillset@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1080 <setrlimit@plt>
lea    0xdd2(%rip),%rdi
call   1050 <chdir@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1040 <raise@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10a0 <sigdelset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x5,%edi
call   1090 <do_sleep@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coredump>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
cmpb   $0x0,0x2ec8(%rip)
jne    11c5 <coredump+0x25>
movb   $0x1,0x2ebf(%rip)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     11cf <coredump+0x2f>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1070 <sigfillset@plt>
mov    0x2df5(%rip),%r14
mov    (%r14),%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    %rax,0x10(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1080 <setrlimit@plt>
lea    0xde2(%rip),%rdi
call   1050 <chdir@plt>
mov    0x2dbe(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    (%rbx),%edi
call   1040 <raise@plt>
mov    (%rbx),%esi
mov    %r15,%rdi
call   10a0 <sigdelset@plt>
mov    (%r14),%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    $0x5,%edi
call   1090 <do_sleep@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i8*, i8* }
@coredump.dumped = internal global i32 0
@SIG_SETMASK = global i32 0
@RLIM_INFINITY = global i8* null
@RLIMIT_CORE = global i32 0
@.str = [2 x i8] c"/\00"
@SIGSEGV = global i32 0
@SIG_DFL = global i32 0
define void @coredump() {
%1 = alloca %struct.rlimit
%2 = alloca i32
%3 = load i32, i32* @coredump.dumped
%4 = icmp ne i32 %3, 0
br i1 %4, label %5, label %6
5: ; preds = %0
br label %32
6: ; preds = %0
store i32 1, i32* @coredump.dumped
%7 = call i64 (...) @fork()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %6
br label %32
10: ; preds = %6
%11 = call i32 @sigfillset(i32* %2)
%12 = load i32, i32* @SIG_SETMASK
%13 = call i32 @sigprocmask(i32 %12, i32* %2, i32* null)
%14 = load i8*, i8** @RLIM_INFINITY
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
store i8* %14, i8** %15
%16 = load i8*, i8** @RLIM_INFINITY
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
store i8* %16, i8** %17
%18 = load i32, i32* @RLIMIT_CORE
%19 = call i32 @setrlimit(i32 %18, %struct.rlimit* %1)
%20 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @SIGSEGV
%22 = load i32, i32* @SIG_DFL
%23 = call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGSEGV
%25 = call i32 @raise(i32 %24)
%26 = load i32, i32* @SIGSEGV
%27 = call i32 @sigdelset(i32* %2, i32 %26)
%28 = load i32, i32* @SIG_SETMASK
%29 = call i32 @sigprocmask(i32 %28, i32* %2, i32* null)
%30 = call i32 @do_sleep(i32 5)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %9, %5
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i8*, i8* }
@coredump.dumped = internal unnamed_addr global i1 false
@SIG_SETMASK = global i32 0
@RLIM_INFINITY = global i8* null
@RLIMIT_CORE = global i32 0
@.str = [2 x i8] c"/\00"
@SIGSEGV = global i32 0
@SIG_DFL = global i32 0
define void @coredump() {
%1 = alloca %struct.rlimit
%2 = alloca i32
%3 = bitcast %struct.rlimit* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i1, i1* @coredump.dumped
br i1 %5, label %30, label %6
6: ; preds = %0
store i1 true, i1* @coredump.dumped
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = call i32 @sigfillset(i32* %2)
%11 = load i32, i32* @SIG_SETMASK
%12 = call i32 @sigprocmask(i32 %11, i32* %2, i32* null)
%13 = load i8*, i8** @RLIM_INFINITY
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i8* %13, i8** %15
%16 = load i32, i32* @RLIMIT_CORE
%17 = call i32 @setrlimit(i32 %16, %struct.rlimit* %1)
%18 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @SIGSEGV
%20 = load i32, i32* @SIG_DFL
%21 = call i32 @signal(i32 %19, i32 %20)
%22 = load i32, i32* @SIGSEGV
%23 = call i32 @raise(i32 %22)
%24 = load i32, i32* @SIGSEGV
%25 = call i32 @sigdelset(i32* %2, i32 %24)
%26 = load i32, i32* @SIG_SETMASK
%27 = call i32 @sigprocmask(i32 %26, i32* %2, i32* null)
%28 = call i32 @do_sleep(i32 5)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %6, %0
ret void
}