#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*/ err(int, char *, ...);
int /*<<< orphan*/ execv(char const *, char **);
int fork();
pid_t spawnv(const char *prog, char **argv) {
  pid_t pid = fork();
  switch (pid) {
  case -1:
    err(1, "fork");
  case 0:
    execv(prog, argv);
    err(1, "%s: execv", prog);
  default:
    break;
  }
  return pid;
}


x86 assembly (clang, -O0):
<spawnv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     116e <spawnv+0x3e>
jmp    115e <spawnv+0x2e>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1181 <spawnv+0x51>
jmp    11a5 <spawnv+0x75>
mov    $0x1,%edi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1030 <err@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <execv@plt>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1030 <err@plt>
jmp    11aa <spawnv+0x7a>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawnv>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     115f <spawnv+0x2f>
cmp    $0xffffffff,%ebp
jne    1180 <spawnv+0x50>
lea    0xead(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <err@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <execv@plt>
lea    0xe94(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <err@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"%s: execv\00"
define i32 @spawnv(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
%7 = load i32, i32* %5
switch i32 %7, label %16 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %2
%9 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %2, %8
%11 = load i8*, i8** %3
%12 = load i8**, i8*** %4
%13 = call i32 @execv(i8* %11, i8** %12)
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %2, %10
br label %17
17: ; preds = %16
%18 = load i32, i32* %5
ret i32 %18
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [10 x i8] c"%s: execv\00"
define i32 @spawnv(i8* %0, i8** %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %9 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %2, %4
%7 = tail call i32 @execv(i8* %0, i8** %1)
%8 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %6, %2
ret i32 %3
}