#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*/ abort();
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int spawn(char *program, char **arg_list) {
  pid_t child_pid;
  child_pid = fork();
  if (child_pid != 0) {
    return child_pid;
  } else {
    printf("child pid = %d\n", getpid());
    execvp(program, arg_list);
    fprintf(stderr, "an error occurred in execvp\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1190 <spawn+0x30>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<spawn>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     117e <spawn+0x1e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe74(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"child pid = %d\0A\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"an error occurred in execvp\0A\00"
define i32 @spawn(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
store i8* %0, i8** %3
store i8** %1, i8*** %4
%6 = call i64 (...) @fork()
store i64 %6, i64* %5
%7 = load i64, i64* %5
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i64, i64* %5
%11 = trunc i64 %10 to i32
ret i32 %11
12: ; preds = %2
%13 = call i32 (...) @getpid()
%14 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %3
%16 = load i8**, i8*** %4
%17 = call i32 @execvp(i8* %15, i8** %16)
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
@.str = [16 x i8] c"child pid = %d\0A\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"an error occurred in execvp\0A\00"
define i32 @spawn(i8* %0, i8** %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = trunc i64 %3 to i32
ret i32 %6
7: ; preds = %2
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @execvp(i8* %0, i8** %1)
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 (...) @abort()
unreachable
}