#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*/ execlp(char const *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void test_fork_exec(const char *process) {
  pid_t pid = fork();
  int status;
  switch (pid) {
  case -1: {
    perror("fork");
    break;
  }
  case 0: {
    execlp(process, (char *)0, NULL);
    perror("exec");
    break;
  }
  default: {
    printf("Child id: %i\n", pid);
    fflush(NULL);
    break;
  }
  }
}


x86 assembly (clang, -O0):
<test_fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     118a <test_fork_exec+0x3a>
jmp    117a <test_fork_exec+0x2a>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     119b <test_fork_exec+0x4b>
jmp    11bc <test_fork_exec+0x6c>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <test_fork_exec+0x84>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <execlp@plt>
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <test_fork_exec+0x84>
mov    -0xc(%rbp),%esi
lea    0xe44(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <fflush@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fork_exec>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     117a <test_fork_exec+0x2a>
cmp    $0xffffffff,%eax
je     1193 <test_fork_exec+0x43>
lea    0xe9f(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %edi,%edi
pop    %rbx
jmp    1040 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <execlp@plt>
lea    0xe78(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>
lea    0xe66(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"exec\00"
@.str.2 = [14 x i8] c"Child id: %i\0A\00"
define void @test_fork_exec(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
switch i32 %6, label %13 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @execlp(i8* %10, i8* null, i32* null)
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 @fflush(i32* null)
br label %17
17: ; preds = %13, %9, %7
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"exec\00"
@.str.2 = [14 x i8] c"Child id: %i\0A\00"
define void @test_fork_exec(i8* %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %8 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %1
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
5: ; preds = %1
%6 = tail call i32 @execlp(i8* %0, i8* null, i32* null)
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %11
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%10 = tail call i32 @fflush(i32* null)
br label %11
11: ; preds = %8, %5, %3
ret void
}