#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*/ execvp(char *, char **);
int /*<<< orphan*/ fatal(char *);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
void forkexec(char *argv[]) {
  pid_t p;
  p = fork();
  if (p < 0)
    fatal("fork1");
  if (p > 0)
    waitpid(p, NULL, 0);
  else {
    setsid();
    execvp(argv[0], argv);
    fatal("exec");
  }
}


x86 assembly (clang, -O0):
<forkexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    117e <forkexec+0x2e>
lea    0xe87(%rip),%rdi
call   1060 <fatal@plt>
cmpq   $0x0,-0x10(%rbp)
jle    119d <forkexec+0x4d>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    11c0 <forkexec+0x70>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <execvp@plt>
lea    0xe4b(%rip),%rdi
call   1060 <fatal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkexec>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     116f <forkexec+0x1f>
je     117b <forkexec+0x2b>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
pop    %rbx
jmp    1040 <waitpid@plt>
lea    0xe8a(%rip),%rdi
call   1060 <fatal@plt>
xor    %eax,%eax
call   1030 <setsid@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
lea    0xe72(%rip),%rdi
pop    %rbx
jmp    1060 <fatal@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"fork1\00"
@.str.1 = [5 x i8] c"exec\00"
define void @forkexec(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
store i8** %0, i8*** %2
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i64, i64* %3
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i64, i64* %3
%14 = call i32 @waitpid(i64 %13, i32* null, i32 0)
br label %23
15: ; preds = %9
%16 = call i32 (...) @setsid()
%17 = load i8**, i8*** %2
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = load i8**, i8*** %2
%21 = call i32 @execvp(i8* %19, i8** %20)
%22 = call i32 @fatal(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %15, %12
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"fork1\00"
@.str.1 = [5 x i8] c"exec\00"
define void @forkexec(i8** %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %10
6: ; preds = %1
%7 = icmp eq i64 %2, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @waitpid(i64 %2, i32* null, i32 0)
br label %15
10: ; preds = %4, %6
%11 = tail call i32 (...) @setsid()
%12 = load i8*, i8** %0
%13 = tail call i32 @execvp(i8* %12, i8** %0)
%14 = tail call i32 @fatal(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %10, %8
ret void
}