#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;
scalar_t__ 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*/ *);
int /*<<< orphan*/ warn(char *);
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);
  if (chdir("/") < 0)
    warn("chdir to / failed (#2)");
  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,0x2ec1(%rip)
je     11ca <coredump+0x1a>
jmp    12b7 <coredump+0x107>
movl   $0x1,0x2eac(%rip)
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
je     11ea <coredump+0x3a>
jmp    12b7 <coredump+0x107>
lea    -0x14(%rbp),%rdi
call   1080 <sigfillset@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1090 <setrlimit@plt>
lea    0xdc2(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    1259 <coredump+0xa9>
lea    0xdae(%rip),%rdi
call   1050 <warn@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1040 <raise@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   10b0 <sigdelset@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x5,%edi
call   10a0 <do_sleep@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coredump>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmpb   $0x0,0x2ec2(%rip)
jne    11d3 <coredump+0x23>
movb   $0x1,0x2eb9(%rip)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     11db <coredump+0x2b>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigfillset@plt>
mov    0x2de9(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1090 <setrlimit@plt>
lea    0xdd6(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
jns    1240 <coredump+0x90>
lea    0xdc7(%rip),%rdi
call   1050 <warn@plt>
mov    0x2da1(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    (%rbx),%edi
call   1040 <raise@plt>
mov    (%rbx),%esi
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <sigdelset@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    $0x5,%edi
call   10a0 <do_sleep@plt>
xor    %edi,%edi
call   10c0 <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"
@.str.1 = [23 x i8] c"chdir to / failed ()\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 %36
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 %36
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 i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @warn(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %10
%25 = load i32, i32* @SIGSEGV
%26 = load i32, i32* @SIG_DFL
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = load i32, i32* @SIGSEGV
%29 = call i32 @raise(i32 %28)
%30 = load i32, i32* @SIGSEGV
%31 = call i32 @sigdelset(i32* %2, i32 %30)
%32 = load i32, i32* @SIG_SETMASK
%33 = call i32 @sigprocmask(i32 %32, i32* %2, i32* null)
%34 = call i32 @do_sleep(i32 5)
%35 = call i32 @exit(i32 0)
unreachable
36: ; 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"
@.str.1 = [23 x i8] c"chdir to / failed ()\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 %34, 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 %34
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 i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %9
%21 = call i32 @warn(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %9
%23 = load i32, i32* @SIGSEGV
%24 = load i32, i32* @SIG_DFL
%25 = call i32 @signal(i32 %23, i32 %24)
%26 = load i32, i32* @SIGSEGV
%27 = call i32 @raise(i32 %26)
%28 = load i32, i32* @SIGSEGV
%29 = call i32 @sigdelset(i32* %2, i32 %28)
%30 = load i32, i32* @SIG_SETMASK
%31 = call i32 @sigprocmask(i32 %30, i32* %2, i32* null)
%32 = call i32 @do_sleep(i32 5)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %6, %0
ret void
}