#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 */

/* Variables and functions */
int /*<<< orphan*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTERM;
int errno;
int execvp(char const *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int getppid();
int parent_main(int);
int prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Usage: safeexec COMMAND [COMMAND_ARG...]\n");
    return 1;
  }
  {
    const char *command_path = argv[1];
    char **command_args = &argv[1];
    int ret;
    ret = fork();
    if (ret == -1) {
      {
        fprintf(stderr,
                "[error:%d] "
                "fork() failed"
                ": pid=%d, ppid=%d, error=%s(%d)\n",
                38, getpid(), getppid(), strerror(errno), errno);
        exit(1);
      };
    }
    if (ret > 0) {
      if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
        {
          fprintf(stderr,
                  "[error:%d] "
                  "parent prctl() failed"
                  ": pid=%d, ppid=%d, error=%s(%d)\n",
                  42, getpid(), getppid(), strerror(errno), errno);
          exit(1);
        };
      }
      return parent_main(ret);
    } else {
      if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) {
        {
          fprintf(stderr,
                  "[error:%d] "
                  "child prctl() failed"
                  ": pid=%d, ppid=%d, error=%s(%d)\n",
                  46, getpid(), getppid(), strerror(errno), errno);
          exit(1);
        };
      }
      if (execvp(command_path, command_args) == -1) {
        {
          fprintf(stderr,
                  "[error:%d] "
                  "execvp() faied"
                  ": pid=%d, ppid=%d, error=%s(%d)\n",
                  47, getpid(), getppid(), strerror(errno), errno);
          exit(1);
        };
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d3 <main+0x43>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140b <main+0x27b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1263 <main+0xd3>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x28(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    %rax,%r9
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
lea    0xde0(%rip),%rsi
mov    $0x26,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jle    1301 <main+0x171>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x161>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    %rax,%r9
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
lea    0xd8b(%rip),%rsi
mov    $0x2a,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
call   1060 <parent_main@plt>
mov    %eax,-0x4(%rbp)
jmp    140b <main+0x27b>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
cmp    $0xffffffff,%eax
jne    1385 <main+0x1f5>
mov    0x2ca8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x40(%rbp)
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%ecx
mov    -0x40(%rbp),%r8d
mov    %rax,%r9
mov    0x2c65(%rip),%rax
mov    (%rax),%eax
lea    0xd38(%rip),%rsi
mov    $0x2e,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    13ff <main+0x26f>
mov    0x2c2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x50(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%ecx
mov    -0x4c(%rbp),%r8d
mov    %rax,%r9
mov    0x2beb(%rip),%rax
mov    (%rax),%eax
lea    0xcfe(%rip),%rsi
mov    $0x2f,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1404 <main+0x274>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11ba <main+0x2a>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    1237 <main+0xa7>
mov    %rsi,%rbx
mov    0x8(%rsi),%r14
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xb2>
mov    %eax,%ebp
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
test   %ebp,%ebp
jle    1204 <main+0x74>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xf8>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <parent_main@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
cmp    $0xffffffff,%eax
je     12ce <main+0x13e>
add    $0x8,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1311 <main+0x181>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d87(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2d62(%rip),%rbp
mov    0x0(%rbp),%edi
call   10a0 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xdaf(%rip),%rsi
mov    %r14d,%edi
mov    $0x26,%edx
jmp    1352 <main+0x1c2>
mov    0x2d41(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2d1c(%rip),%rbp
mov    0x0(%rbp),%edi
call   10a0 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xda2(%rip),%rsi
mov    %r14d,%edi
mov    $0x2a,%edx
jmp    1352 <main+0x1c2>
mov    0x2cfb(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2cd6(%rip),%rbp
mov    0x0(%rbp),%edi
call   10a0 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xd9d(%rip),%rsi
mov    %r14d,%edi
mov    $0x2e,%edx
jmp    1352 <main+0x1c2>
mov    0x2cb8(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
mov    0x2c93(%rip),%rbp
mov    0x0(%rbp),%edi
call   10a0 <strerror@plt>
mov    0x0(%rbp),%ecx
mov    %ecx,(%rsp)
lea    0xd9a(%rip),%rsi
mov    %r14d,%edi
mov    $0x2f,%edx
mov    %r15d,%ecx
mov    %ebx,%r8d
mov    %rax,%r9
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"Usage: safeexec COMMAND [COMMAND_ARG...]\0A\00"
@.str.1 = [57 x i8] c"[error:%d] fork() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@errno = global i32 0
@PR_SET_PDEATHSIG = global i32 0
@SIGTERM = global i32 0
@.str.2 = [65 x i8] c"[error:%d] parent prctl() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@SIGKILL = global i32 0
@.str.3 = [64 x i8] c"[error:%d] child prctl() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@.str.4 = [58 x i8] c"[error:%d] execvp() faied: pid=%d, ppid=%d, error=%s(%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
store i8** %19, i8*** %7
%20 = call i32 (...) @fork()
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %32
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i32 (...) @getpid()
%26 = call i32 (...) @getppid()
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = load i32, i32* @errno
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 38, i32 %25, i32 %26, i8* %28, i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %14
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %52
35: ; preds = %32
%36 = load i32, i32* @PR_SET_PDEATHSIG
%37 = load i32, i32* @SIGTERM
%38 = call i32 @prctl(i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %49
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (...) @getpid()
%43 = call i32 (...) @getppid()
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = load i32, i32* @errno
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 42, i32 %42, i32 %43, i8* %45, i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %35
%50 = load i32, i32* %8
%51 = call i32 @parent_main(i32 %50)
store i32 %51, i32* %3
br label %82
52: ; preds = %32
%53 = load i32, i32* @PR_SET_PDEATHSIG
%54 = load i32, i32* @SIGKILL
%55 = call i32 @prctl(i32 %53, i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %66
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = call i32 (...) @getpid()
%60 = call i32 (...) @getppid()
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = load i32, i32* @errno
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32 46, i32 %59, i32 %60, i8* %62, i32 %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %52
%67 = load i8*, i8** %6
%68 = load i8**, i8*** %7
%69 = call i32 @execvp(i8* %67, i8** %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %80
71: ; preds = %66
%72 = load i32, i32* @stderr
%73 = call i32 (...) @getpid()
%74 = call i32 (...) @getppid()
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = load i32, i32* @errno
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 47, i32 %73, i32 %74, i8* %76, i32 %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %66
br label %81
81: ; preds = %80
store i32 0, i32* %3
br label %82
82: ; preds = %81, %49, %11
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"Usage: safeexec COMMAND [COMMAND_ARG...]\0A\00"
@.str.1 = [57 x i8] c"[error:%d] fork() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@errno = global i32 0
@PR_SET_PDEATHSIG = global i32 0
@SIGTERM = global i32 0
@.str.2 = [65 x i8] c"[error:%d] parent prctl() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@SIGKILL = global i32 0
@.str.3 = [64 x i8] c"[error:%d] child prctl() failed: pid=%d, ppid=%d, error=%s(%d)\0A\00"
@.str.4 = [58 x i8] c"[error:%d] execvp() faied: pid=%d, ppid=%d, error=%s(%d)\0A\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 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %64
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = load i32, i32* @errno
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 38, i32 %14, i32 %15, i8* %17, i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %7
%22 = icmp sgt i32 %10, 0
%23 = load i32, i32* @PR_SET_PDEATHSIG
br i1 %22, label %24, label %39
24: ; preds = %21
%25 = load i32, i32* @SIGTERM
%26 = tail call i32 @prctl(i32 %23, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = tail call i32 (...) @getpid()
%31 = tail call i32 (...) @getppid()
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = load i32, i32* @errno
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 42, i32 %30, i32 %31, i8* %33, i32 %34)
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %24
%38 = tail call i32 @parent_main(i32 %10)
br label %64
39: ; preds = %21
%40 = load i32, i32* @SIGKILL
%41 = tail call i32 @prctl(i32 %23, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %52
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = tail call i32 (...) @getpid()
%46 = tail call i32 (...) @getppid()
%47 = load i32, i32* @errno
%48 = tail call i8* @strerror(i32 %47)
%49 = load i32, i32* @errno
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0), i32 46, i32 %45, i32 %46, i8* %48, i32 %49)
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %39
%53 = tail call i32 @execvp(i8* %9, i8** %8)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = tail call i32 (...) @getpid()
%58 = tail call i32 (...) @getppid()
%59 = load i32, i32* @errno
%60 = tail call i8* @strerror(i32 %59)
%61 = load i32, i32* @errno
%62 = tail call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32 47, i32 %57, i32 %58, i8* %60, i32 %61)
%63 = tail call i32 @exit(i32 1)
unreachable
64: ; preds = %37, %52, %4
%65 = phi i32 [ 1, %4 ], [ 0, %52 ], [ %38, %37 ]
ret i32 %65
}