#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_DFL;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ fork();
int /*<<< orphan*/ log_error(char *);
int /*<<< orphan*/ log_fail(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int rpctest_try_catch_crash(int *termsig, int *exit_code) {
  pid_t pid;
  int status;
  signal(SIGCHLD, SIG_DFL);
  pid = fork();
  if (pid < 0) {
    log_fail("unable to fork: %m");
    return -1;
  }
  if (pid == 0) {
    int fd = open("/dev/null", O_RDWR);
    if (fd >= 0) {
      dup2(fd, 1);
      dup2(fd, 2);
    } else {
      log_error("/dev/null: %m");
    }
    return 0;
  }
  if (waitpid(pid, &status, 0) < 0) {
    log_fail("waitpid failed: %m");
    return -1;
  }
  *exit_code = *termsig = 0;
  if (WIFEXITED(status)) {
    *exit_code = WEXITSTATUS(status);
    return 1;
  }
  if (WIFSIGNALED(status)) {
    *termsig = WTERMSIG(status);
    return 2;
  }
  log_fail("unexpected wait status (neither exited nor crashed)");
  return 0;
}


x86 assembly (clang, -O0):
<rpctest_try_catch_crash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1205 <rpctest_try_catch_crash+0x55>
lea    0xe0c(%rip),%rdi
call   1070 <log_fail@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rpctest_try_catch_crash+0x16d>
cmpq   $0x0,-0x20(%rbp)
jne    1269 <rpctest_try_catch_crash+0xb9>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1251 <rpctest_try_catch_crash+0xa1>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x28(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
jmp    125d <rpctest_try_catch_crash+0xad>
lea    0xdc5(%rip),%rdi
call   1030 <log_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    131d <rpctest_try_catch_crash+0x16d>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0x0,%rax
jge    129a <rpctest_try_catch_crash+0xea>
lea    0xda2(%rip),%rdi
call   1070 <log_fail@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131d <rpctest_try_catch_crash+0x16d>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12dc <rpctest_try_catch_crash+0x12c>
mov    -0x24(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    131d <rpctest_try_catch_crash+0x16d>
mov    -0x24(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     130a <rpctest_try_catch_crash+0x15a>
mov    -0x24(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    131d <rpctest_try_catch_crash+0x16d>
lea    0xd2d(%rip),%rdi
call   1070 <log_fail@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpctest_try_catch_crash>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1223 <rpctest_try_catch_crash+0x73>
je     122c <rpctest_try_catch_crash+0x7c>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   10a0 <waitpid@plt>
test   %rax,%rax
js     1261 <rpctest_try_catch_crash+0xb1>
movl   $0x0,(%r14)
movl   $0x0,(%rbx)
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1274 <rpctest_try_catch_crash+0xc4>
call   1090 <WEXITSTATUS@plt>
mov    %eax,(%rbx)
mov    $0x1,%eax
jmp    12ad <rpctest_try_catch_crash+0xfd>
lea    0xdd6(%rip),%rdi
jmp    1268 <rpctest_try_catch_crash+0xb8>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdd7(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1291 <rpctest_try_catch_crash+0xe1>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
jmp    12ab <rpctest_try_catch_crash+0xfb>
lea    0xdc3(%rip),%rdi
call   1070 <log_fail@plt>
mov    $0xffffffff,%eax
jmp    12ad <rpctest_try_catch_crash+0xfd>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     129f <rpctest_try_catch_crash+0xef>
mov    0x4(%rsp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,(%r14)
mov    $0x2,%eax
jmp    12ad <rpctest_try_catch_crash+0xfd>
lea    0xd85(%rip),%rdi
call   1030 <log_error@plt>
jmp    12ab <rpctest_try_catch_crash+0xfb>
lea    0xd98(%rip),%rdi
call   1070 <log_fail@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@.str = [19 x i8] c"unable to fork: %m\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [14 x i8] c"/dev/null: %m\00"
@.str.3 = [19 x i8] c"waitpid failed: %m\00"
@.str.4 = [52 x i8] c"unexpected wait status (neither exited nor crashed)\00"
define i32 @rpctest_try_catch_crash(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%9 = load i32, i32* @SIGCHLD
%10 = load i32, i32* @SIG_DFL
%11 = call i32 @signal(i32 %9, i32 %10)
%12 = call i64 (...) @fork()
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @log_fail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
17: ; preds = %2
%18 = load i64, i64* %6
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* %8
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = load i32, i32* %8
%29 = call i32 @dup2(i32 %28, i32 2)
br label %32
30: ; preds = %20
%31 = call i32 @log_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
store i32 0, i32* %3
br label %59
33: ; preds = %17
%34 = load i64, i64* %6
%35 = call i64 @waitpid(i64 %34, i32* %7, i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @log_fail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
39: ; preds = %33
%40 = load i32*, i32** %4
store i32 0, i32* %40
%41 = load i32*, i32** %5
store i32 0, i32* %41
%42 = load i32, i32* %7
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i32, i32* %7
%47 = call i32 @WEXITSTATUS(i32 %46)
%48 = load i32*, i32** %5
store i32 %47, i32* %48
store i32 1, i32* %3
br label %59
49: ; preds = %39
%50 = load i32, i32* %7
%51 = call i64 @WIFSIGNALED(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* %7
%55 = call i32 @WTERMSIG(i32 %54)
%56 = load i32*, i32** %4
store i32 %55, i32* %56
store i32 2, i32* %3
br label %59
57: ; preds = %49
%58 = call i32 @log_fail(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %59
59: ; preds = %57, %53, %45, %37, %32, %15
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_DFL = global i32 0
@.str = [19 x i8] c"unable to fork: %m\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [14 x i8] c"/dev/null: %m\00"
@.str.3 = [19 x i8] c"waitpid failed: %m\00"
@.str.4 = [52 x i8] c"unexpected wait status (neither exited nor crashed)\00"
define i32 @rpctest_try_catch_crash(i32* nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @SIGCHLD
%6 = load i32, i32* @SIG_DFL
%7 = tail call i32 @signal(i32 %5, i32 %6)
%8 = tail call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @log_fail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %43
12: ; preds = %2
%13 = icmp eq i64 %8, 0
br i1 %13, label %14, label %23
14: ; preds = %12
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @dup2(i32 %16, i32 1)
%20 = tail call i32 @dup2(i32 %16, i32 2)
br label %43
21: ; preds = %14
%22 = tail call i32 @log_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %43
23: ; preds = %12
%24 = call i64 @waitpid(i64 %8, i32* %3, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @log_fail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %43
28: ; preds = %23
store i32 0, i32* %0
store i32 0, i32* %1
%29 = load i32, i32* %3
%30 = call i64 @WIFEXITED(i32 %29)
%31 = icmp eq i64 %30, 0
%32 = load i32, i32* %3
br i1 %31, label %35, label %33
33: ; preds = %28
%34 = call i32 @WEXITSTATUS(i32 %32)
store i32 %34, i32* %1
br label %43
35: ; preds = %28
%36 = call i64 @WIFSIGNALED(i32 %32)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* %3
%40 = call i32 @WTERMSIG(i32 %39)
store i32 %40, i32* %0
br label %43
41: ; preds = %35
%42 = call i32 @log_fail(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %18, %21, %41, %38, %33, %26, %10
%44 = phi i32 [ -1, %10 ], [ -1, %26 ], [ 1, %33 ], [ 2, %38 ], [ 0, %41 ], [ 0, %21 ], [ 0, %18 ]
ret i32 %44
}