#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*/ STDIN_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exec_hook(int, int, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int wait(int *);
int fork_exec_wait_hook(int hook, int argc, char **argv) {
  pid_t pid, reaped;
  int status;
  pid = fork();
  if (pid == -1)
    return -1;
  if (pid == 0) {
    close(STDIN_FILENO);
    exec_hook(hook, argc, argv);
    _exit(138);
  }
  for (;;) {
    reaped = wait(&status);
    if (reaped == -1) {
      perror("wait");
      return -1;
    }
    if (reaped == pid)
      return status == 138 ? -1 : status;
  }
}


x86 assembly (clang, -O0):
<fork_exec_wait_hook>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1192 <fork_exec_wait_hook+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <fork_exec_wait_hook+0xd2>
cmpl   $0x0,-0x1c(%rbp)
jne    11c3 <fork_exec_wait_hook+0x63>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <exec_hook@plt>
mov    $0x8a,%edi
call   1030 <_exit@plt>
jmp    11c8 <fork_exec_wait_hook+0x68>
lea    -0x24(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f6 <fork_exec_wait_hook+0x96>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <fork_exec_wait_hook+0xd2>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    122d <fork_exec_wait_hook+0xcd>
cmpl   $0x8a,-0x24(%rbp)
jne    121c <fork_exec_wait_hook+0xbc>
mov    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1222 <fork_exec_wait_hook+0xc2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <fork_exec_wait_hook+0xd2>
jmp    11c8 <fork_exec_wait_hook+0x68>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec_wait_hook>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11b2 <fork_exec_wait_hook+0x52>
test   %ebx,%ebx
je     11d2 <fork_exec_wait_hook+0x72>
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
je     11bf <fork_exec_wait_hook+0x5f>
cmp    %ebx,%eax
jne    1190 <fork_exec_wait_hook+0x30>
mov    0x4(%rsp),%eax
cmp    $0x8a,%eax
mov    $0xffffffff,%ebx
cmovne %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11b2 <fork_exec_wait_hook+0x52>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   1040 <exec_hook@plt>
mov    $0x8a,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [5 x i8] c"wait\00"
define i32 @fork_exec_wait_hook(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %46
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* @STDIN_FILENO
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = load i8**, i8*** %7
%24 = call i32 @exec_hook(i32 %21, i32 %22, i8** %23)
%25 = call i32 @_exit(i32 138)
unreachable
26: ; preds = %15
br label %27
27: ; preds = %45, %26
%28 = call i32 @wait(i32* %10)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %46
33: ; preds = %27
%34 = load i32, i32* %9
%35 = load i32, i32* %8
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %45
37: ; preds = %33
%38 = load i32, i32* %10
%39 = icmp eq i32 %38, 138
br i1 %39, label %40, label %41
40: ; preds = %37
br label %43
41: ; preds = %37
%42 = load i32, i32* %10
br label %43
43: ; preds = %41, %40
%44 = phi i32 [ -1, %40 ], [ %42, %41 ]
store i32 %44, i32* %4
br label %46
45: ; preds = %33
br label %27
46: ; preds = %43, %31, %14
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [5 x i8] c"wait\00"
define i32 @fork_exec_wait_hook(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @fork()
switch i32 %6, label %12 [
i32 -1, label %23
i32 0, label %7
]
7: ; preds = %3
%8 = load i32, i32* @STDIN_FILENO
%9 = tail call i32 @close(i32 %8)
%10 = tail call i32 @exec_hook(i32 %0, i32 %1, i8** %2)
%11 = tail call i32 @_exit(i32 138)
unreachable
12: ; preds = %3, %17
%13 = call i32 @wait(i32* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
17: ; preds = %12
%18 = icmp eq i32 %13, %6
br i1 %18, label %19, label %12
19: ; preds = %17
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 138
%22 = select i1 %21, i32 -1, i32 %20
br label %23
23: ; preds = %3, %19, %15
%24 = phi i32 [ -1, %15 ], [ %22, %19 ], [ %6, %3 ]
ret i32 %24
}