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

/* Variables and functions */
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ EX_UNAVAILABLE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ _Exit(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int close(int);
int dup2(int, int);
int errno;
int execve(char *, char **, char **);
int fork();
int open(char *, int /*<<< orphan*/);
scalar_t__ strsignal(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, scalar_t__);
int waitpid(int, int *, int /*<<< orphan*/);
int run_hook(char const **argv) {
  assert(argv != NULL);
  pid_t child = fork();
  if (child == -1)
    return errno;
  else if (child == 0) {
    extern char **environ;
    int devnull = open("/dev/null", O_RDWR);
    if (devnull == -1)
      _Exit(EX_UNAVAILABLE);
    if (dup2(devnull, STDIN_FILENO) == -1)
      _Exit(EX_OSERR);
    if (dup2(devnull, STDOUT_FILENO) == -1)
      _Exit(EX_OSERR);
    if (dup2(devnull, STDERR_FILENO) == -1)
      _Exit(EX_OSERR);
    for (int fh = STDERR_FILENO + 1; fh <= devnull; fh++)
      if (close(fh) == -1)
        _Exit(EX_OSERR);
    if (execve((char *)argv[0], (char **)argv, environ) == -1)
      _Exit(EX_UNAVAILABLE);
  } else {
    int status;
    if (waitpid(child, &status, 0) != child)
      return errno;
    if (WIFEXITED(status)) {
      if (WEXITSTATUS(status) != 0)
        syslog(LOG_ERR, "posthook='%s' rc=%d", argv[0], WEXITSTATUS(status));
    } else
      syslog(LOG_ERR, "posthook='%s' terminated='%s'", argv[0],
             strsignal(WTERMSIG(status)));
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_hook>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1213 <run_hook+0x43>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    141b <run_hook+0x24b>
cmpl   $0x0,-0x14(%rbp)
jne    1348 <run_hook+0x178>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    124d <run_hook+0x7d>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1275 <run_hook+0xa5>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    129d <run_hook+0xcd>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12c5 <run_hook+0xf5>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1312 <run_hook+0x142>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12ff <run_hook+0x12f>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
jmp    1304 <run_hook+0x134>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12d4 <run_hook+0x104>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2cb4(%rip),%rax
mov    (%rax),%rdx
call   1050 <execve@plt>
cmp    $0xffffffff,%eax
jne    1343 <run_hook+0x173>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
jmp    140f <run_hook+0x23f>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    -0x14(%rbp),%eax
je     1370 <run_hook+0x1a0>
mov    0x2c62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    141b <run_hook+0x24b>
mov    -0x20(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13ce <run_hook+0x1fe>
mov    -0x20(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     13c9 <run_hook+0x1f9>
mov    0x2bfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xc46(%rip),%rsi
call   1080 <syslog@plt>
jmp    140a <run_hook+0x23a>
mov    0x2bc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%edi
call   1090 <strsignal@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xc19(%rip),%rsi
call   1080 <syslog@plt>
jmp    140f <run_hook+0x23f>
jmp    1414 <run_hook+0x244>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_hook>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1261 <run_hook+0x91>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1253 <run_hook+0x83>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    %ebx,%eax
jne    1253 <run_hook+0x83>
mov    0xc(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     1332 <run_hook+0x162>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
test   %rax,%rax
je     1362 <run_hook+0x192>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xdbc(%rip),%rsi
jmp    1355 <run_hook+0x185>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
jmp    1364 <run_hook+0x194>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0xd8f(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1324 <run_hook+0x154>
mov    %eax,%r15d
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     12f8 <run_hook+0x128>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     12f8 <run_hook+0x128>
mov    0x2d0b(%rip),%rbx
mov    (%rbx),%esi
mov    %r15d,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     12f8 <run_hook+0x128>
mov    (%rbx),%ebx
cmp    %ebx,%r15d
cmovle %ebx,%r15d
neg    %r15d
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%r15,%rbx,1),%eax
cmp    $0x1,%eax
je     1306 <run_hook+0x136>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    12e0 <run_hook+0x110>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    (%r14),%rdi
mov    0x2cc8(%rip),%rax
mov    (%rax),%rdx
mov    %r14,%rsi
call   1050 <execve@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1364 <run_hook+0x194>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   10e0 <_Exit@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0xc(%rsp),%edi
call   1070 <WTERMSIG@plt>
mov    %eax,%edi
call   1090 <strsignal@plt>
lea    0xcc9(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <syslog@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@EX_UNAVAILABLE = global i32 0
@STDIN_FILENO = global i32 0
@EX_OSERR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@environ = external global i8**
@LOG_ERR = global i32 0
@.str.1 = [20 x i8] c"posthook='%s' rc=%d\00"
@.str.2 = [30 x i8] c"posthook='%s' terminated='%s'\00"
define i32 @run_hook(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8** %0, i8*** %3
%8 = load i8**, i8*** %3
%9 = icmp ne i8** %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* @errno
store i32 %16, i32* %2
br label %118
17: ; preds = %1
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %82
20: ; preds = %17
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* @EX_UNAVAILABLE
%27 = call i32 @_Exit(i32 %26)
unreachable
28: ; preds = %20
%29 = load i32, i32* %5
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @EX_OSERR
%35 = call i32 @_Exit(i32 %34)
unreachable
36: ; preds = %28
%37 = load i32, i32* %5
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @EX_OSERR
%43 = call i32 @_Exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32, i32* %5
%46 = load i32, i32* @STDERR_FILENO
%47 = call i32 @dup2(i32 %45, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* @EX_OSERR
%51 = call i32 @_Exit(i32 %50)
unreachable
52: ; preds = %44
%53 = load i32, i32* @STDERR_FILENO
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %55
55: ; preds = %67, %52
%56 = load i32, i32* %6
%57 = load i32, i32* %5
%58 = icmp sle i32 %56, %57
br i1 %58, label %59, label %70
59: ; preds = %55
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = load i32, i32* @EX_OSERR
%65 = call i32 @_Exit(i32 %64)
unreachable
66: ; preds = %59
br label %67
67: ; preds = %66
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
br label %55
70: ; preds = %55
%71 = load i8**, i8*** %3
%72 = getelementptr inbounds i8*, i8** %71, i64 0
%73 = load i8*, i8** %72
%74 = load i8**, i8*** %3
%75 = load i8**, i8*** @environ
%76 = call i32 @execve(i8* %73, i8** %74, i8** %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %81
78: ; preds = %70
%79 = load i32, i32* @EX_UNAVAILABLE
%80 = call i32 @_Exit(i32 %79)
unreachable
81: ; preds = %70
br label %116
82: ; preds = %17
%83 = load i32, i32* %4
%84 = call i32 @waitpid(i32 %83, i32* %7, i32 0)
%85 = load i32, i32* %4
%86 = icmp ne i32 %84, %85
br i1 %86, label %87, label %89
87: ; preds = %82
%88 = load i32, i32* @errno
store i32 %88, i32* %2
br label %118
89: ; preds = %82
%90 = load i32, i32* %7
%91 = call i64 @WIFEXITED(i32 %90)
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %106
93: ; preds = %89
%94 = load i32, i32* %7
%95 = call i64 @WEXITSTATUS(i32 %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %105
97: ; preds = %93
%98 = load i32, i32* @LOG_ERR
%99 = load i8**, i8*** %3
%100 = getelementptr inbounds i8*, i8** %99, i64 0
%101 = load i8*, i8** %100
%102 = load i32, i32* %7
%103 = call i64 @WEXITSTATUS(i32 %102)
%104 = call i32 @syslog(i32 %98, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %101, i64 %103)
br label %105
105: ; preds = %97, %93
br label %115
106: ; preds = %89
%107 = load i32, i32* @LOG_ERR
%108 = load i8**, i8*** %3
%109 = getelementptr inbounds i8*, i8** %108, i64 0
%110 = load i8*, i8** %109
%111 = load i32, i32* %7
%112 = call i32 @WTERMSIG(i32 %111)
%113 = call i64 @strsignal(i32 %112)
%114 = call i32 @syslog(i32 %107, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %110, i64 %113)
br label %115
115: ; preds = %106, %105
br label %116
116: ; preds = %115, %81
br label %117
117: ; preds = %116
store i32 0, i32* %2
br label %118
118: ; preds = %117, %87, %15
%119 = load i32, i32* %2
ret i32 %119
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@EX_UNAVAILABLE = global i32 0
@STDIN_FILENO = global i32 0
@EX_OSERR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@environ = external global i8**
@LOG_ERR = global i32 0
@.str.1 = [20 x i8] c"posthook='%s' rc=%d\00"
@.str.2 = [30 x i8] c"posthook='%s' terminated='%s'\00"
define i32 @run_hook(i8** %0) {
%2 = alloca i32
%3 = icmp ne i8** %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %58 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %1
%8 = load i32, i32* @errno
br label %86
9: ; preds = %1
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @EX_UNAVAILABLE
%15 = tail call i32 @_Exit(i32 %14)
unreachable
16: ; preds = %9
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i32 @dup2(i32 %11, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @EX_OSERR
%22 = tail call i32 @_Exit(i32 %21)
unreachable
23: ; preds = %16
%24 = load i32, i32* @STDOUT_FILENO
%25 = tail call i32 @dup2(i32 %11, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @EX_OSERR
%29 = tail call i32 @_Exit(i32 %28)
unreachable
30: ; preds = %23
%31 = load i32, i32* @STDERR_FILENO
%32 = tail call i32 @dup2(i32 %11, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @EX_OSERR
%36 = tail call i32 @_Exit(i32 %35)
unreachable
37: ; preds = %30
%38 = load i32, i32* @STDERR_FILENO
%39 = call i32 @llvm.smax.i32(i32 %11, i32 %38)
br label %40
40: ; preds = %49, %37
%41 = phi i32 [ %38, %37 ], [ %42, %49 ]
%42 = add i32 %41, 1
%43 = icmp eq i32 %41, %39
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i8*, i8** %0
%46 = load i8**, i8*** @environ
%47 = tail call i32 @execve(i8* %45, i8** %0, i8** %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %55, label %86
49: ; preds = %40
%50 = tail call i32 @close(i32 %42)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %40
52: ; preds = %49
%53 = load i32, i32* @EX_OSERR
%54 = tail call i32 @_Exit(i32 %53)
unreachable
55: ; preds = %44
%56 = load i32, i32* @EX_UNAVAILABLE
%57 = tail call i32 @_Exit(i32 %56)
unreachable
58: ; preds = %1
%59 = bitcast i32* %2 to i8*
%60 = call i32 @waitpid(i32 %6, i32* %2, i32 0)
%61 = icmp eq i32 %60, %6
br i1 %61, label %62, label %84
62: ; preds = %58
%63 = load i32, i32* %2
%64 = call i64 @WIFEXITED(i32 %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %76, label %66
66: ; preds = %62
%67 = load i32, i32* %2
%68 = call i64 @WEXITSTATUS(i32 %67)
%69 = icmp eq i64 %68, 0
br i1 %69, label %83, label %70
70: ; preds = %66
%71 = load i32, i32* @LOG_ERR
%72 = load i8*, i8** %0
%73 = load i32, i32* %2
%74 = call i64 @WEXITSTATUS(i32 %73)
%75 = call i32 @syslog(i32 %71, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %72, i64 %74)
br label %83
76: ; preds = %62
%77 = load i32, i32* @LOG_ERR
%78 = load i8*, i8** %0
%79 = load i32, i32* %2
%80 = call i32 @WTERMSIG(i32 %79)
%81 = call i64 @strsignal(i32 %80)
%82 = call i32 @syslog(i32 %77, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %78, i64 %81)
br label %83
83: ; preds = %76, %70, %66
br label %86
84: ; preds = %58
%85 = load i32, i32* @errno
br label %86
86: ; preds = %44, %83, %84, %7
%87 = phi i32 [ %8, %7 ], [ %85, %84 ], [ 0, %83 ], [ 0, %44 ]
ret i32 %87
}