#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shared_events_add(char *);
int /*<<< orphan*/ shared_events_clear();
int /*<<< orphan*/ shared_events_close();
pid_t run_child(char **argv) {
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    shared_events_close();
    shared_events_clear();
    shared_events_add("exec");
    if (execv(argv[0], &argv[0]) == -1) {
      perror("exec");
    }
    shared_events_close();
    _exit(EXIT_FAILURE);
  }
  return pid;
}


x86 assembly (clang, -O0):
<run_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ba <run_child+0x3a>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1218 <run_child+0x98>
mov    $0x0,%al
call   1040 <shared_events_close@plt>
mov    $0x0,%al
call   1050 <shared_events_clear@plt>
lea    0xe2c(%rip),%rdi
call   1060 <shared_events_add@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <execv@plt>
cmp    $0xffffffff,%eax
jne    1203 <run_child+0x83>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1040 <shared_events_close@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    121d <run_child+0x9d>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_child>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1196 <run_child+0x16>
cmp    $0xffffffff,%eax
je     11e1 <run_child+0x61>
pop    %rbx
ret
xor    %eax,%eax
call   1040 <shared_events_close@plt>
xor    %eax,%eax
call   1050 <shared_events_clear@plt>
lea    0xe5a(%rip),%rdi
call   1060 <shared_events_add@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1090 <execv@plt>
cmp    $0xffffffff,%eax
jne    11cc <run_child+0x4c>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1040 <shared_events_close@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"exec\00"
define i32 @run_child(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
store i8** %0, i8*** %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = call i32 (...) @shared_events_close()
%16 = call i32 (...) @shared_events_clear()
%17 = call i32 @shared_events_add(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8**, i8*** %2
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %2
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = call i32 @execv(i8* %20, i8** %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %14
%28 = call i32 (...) @shared_events_close()
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @_exit(i32 %29)
unreachable
31: ; preds = %11
br label %32
32: ; preds = %31
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"exec\00"
define i32 @run_child(i8** %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %20 [
i32 -1, label %3
i32 0, label %7
]
3: ; preds = %1
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %1
%8 = tail call i32 (...) @shared_events_close()
%9 = tail call i32 (...) @shared_events_clear()
%10 = tail call i32 @shared_events_add(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** %0
%12 = tail call i32 @execv(i8* %11, i8** %0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %7
%17 = tail call i32 (...) @shared_events_close()
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @_exit(i32 %18)
unreachable
20: ; preds = %1
ret i32 %2
}