#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*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ prctl(int, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  if (argc < 2) {
    fprintf(stderr, "Usage: subreaper <cmd> [args...]\n");
    return 2;
  }
  if (prctl(36, 1) < 0)
    perror("set_subreaper");
  signal(SIGCHLD, SIG_IGN);
  pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  } else if (pid) {
    waitpid(pid, 0, 0);
    exit(0);
  } else {
    execvp(argv[1], argv + 1);
    perror("execv");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c0 <main+0x40>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    $0x24,%edi
mov    $0x1,%esi
call   1050 <prctl@plt>
cmp    $0x0,%rax
jge    11e5 <main+0x65>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1228 <main+0xa8>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     1247 <main+0xc7>
mov    -0x18(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
lea    0xdd2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jg     11a2 <main+0x22>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%eax
pop    %rbx
ret
mov    %rsi,%rbx
mov    $0x24,%edi
mov    $0x1,%esi
call   1050 <prctl@plt>
test   %rax,%rax
jns    11c5 <main+0x45>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   1030 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11f1 <main+0x71>
lea    0xe41(%rip),%rdi
jmp    120a <main+0x8a>
jne    1219 <main+0x99>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1080 <execvp@plt>
lea    0xe2b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Usage: subreaper <cmd> [args...]\0A\00"
@.str.1 = [14 x i8] c"set_subreaper\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
ret i32 2
12: ; preds = %2
%13 = call i64 @prctl(i32 36, i32 1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @SIGCHLD
%19 = load i32, i32* @SIG_IGN
%20 = call i32 @signal(i32 %18, i32 %19)
%21 = call i64 (...) @fork()
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i64, i64* %6
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* %6
%32 = call i32 @waitpid(i64 %31, i32 0, i32 0)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %27
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = call i32 @execvp(i8* %37, i8** %39)
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Usage: subreaper <cmd> [args...]\0A\00"
@.str.1 = [14 x i8] c"set_subreaper\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
ret i32 2
7: ; preds = %2
%8 = tail call i64 @prctl(i32 36, i32 1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = load i32, i32* @SIGCHLD
%14 = load i32, i32* @SIG_IGN
%15 = tail call i32 @signal(i32 %13, i32 %14)
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = icmp eq i64 %16, 0
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = tail call i32 @waitpid(i64 %16, i32 0, i32 0)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %21
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @execvp(i8* %28, i8** %27)
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
}