#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 {
  scalar_t__ rlim_max;
  scalar_t__ rlim_cur;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_CORE;
int WEXITSTATUS(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int wait(int *);
void onsigchld(int sig) {
  int exitstatus;
  pid_t pid;
  pid = wait(&exitstatus);
  if (pid == -1) {
    perror("wait");
    exit(2);
  }
  if (WIFSIGNALED(exitstatus)) {
    struct rlimit rlim;
    rlim.rlim_cur = 0;
    rlim.rlim_max = 0;
    if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
      perror("rlimit - continuing");
    }
    kill(getpid(), WTERMSIG(exitstatus));
  } else {
    exit(WEXITSTATUS(exitstatus));
  }
}


x86 assembly (clang, -O0):
<onsigchld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c7 <onsigchld+0x37>
lea    0xe48(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1231 <onsigchld+0xa1>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1070 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1210 <onsigchld+0x80>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
call   1050 <kill@plt>
jmp    1240 <onsigchld+0xb0>
mov    -0x8(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10a0 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onsigchld>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     1201 <onsigchld+0x71>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     1217 <onsigchld+0x87>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1070 <setrlimit@plt>
cmp    $0xffffffff,%eax
je     11f3 <onsigchld+0x63>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <kill@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
jmp    11d2 <onsigchld+0x42>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@.str = [5 x i8] c"wait\00"
@RLIMIT_CORE = global i32 0
@.str.1 = [20 x i8] c"rlimit - continuing\00"
define void @onsigchld(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rlimit
store i32 %0, i32* %2
%6 = call i32 @wait(i32* %3)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 2)
unreachable
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i64 @WIFSIGNALED(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
store i64 0, i64* %18
%19 = load i32, i32* @RLIMIT_CORE
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = call i32 (...) @getpid()
%26 = load i32, i32* %3
%27 = call i32 @WTERMSIG(i32 %26)
%28 = call i32 @kill(i32 %25, i32 %27)
br label %33
29: ; preds = %12
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@.str = [5 x i8] c"wait\00"
@RLIMIT_CORE = global i32 0
@.str.1 = [20 x i8] c"rlimit - continuing\00"
define void @onsigchld(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
%4 = bitcast i32* %2 to i8*
%5 = call i32 @wait(i32* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 2)
unreachable
10: ; preds = %1
%11 = load i32, i32* %2
%12 = call i64 @WIFSIGNALED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %27, label %14
14: ; preds = %10
%15 = bitcast %struct.rlimit* %3 to i8*
%16 = bitcast %struct.rlimit* %3 to i8*
%17 = load i32, i32* @RLIMIT_CORE
%18 = call i32 @setrlimit(i32 %17, %struct.rlimit* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = call i32 (...) @getpid()
%24 = load i32, i32* %2
%25 = call i32 @WTERMSIG(i32 %24)
%26 = call i32 @kill(i32 %23, i32 %25)
ret void
27: ; preds = %10
%28 = load i32, i32* %2
%29 = call i32 @WEXITSTATUS(i32 %28)
%30 = call i32 @exit(i32 %29)
unreachable
}