#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 EX_OSERR;
int EX_SOFTWARE;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int wait_for_process(int);
int spawn_and_wait(char **argv) {
  int rv = EX_SOFTWARE;
  pid_t pid = fork();
  switch (pid) {
  case -1:
    perror("fork");
    rv = EX_OSERR;
    break;
  case 0:
    execvp(argv[0], argv);
    perror("exec");
    rv = EX_SOFTWARE;
    break;
  default:
    rv = wait_for_process(pid);
  }
  return rv;
}


x86 assembly (clang, -O0):
<spawn_and_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     1186 <spawn_and_wait+0x46>
jmp    1176 <spawn_and_wait+0x36>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11a3 <spawn_and_wait+0x63>
jmp    11d0 <spawn_and_wait+0x90>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11db <spawn_and_wait+0x9b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <execvp@plt>
lea    0xe4b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11db <spawn_and_wait+0x9b>
mov    -0x10(%rbp),%edi
call   1030 <wait_for_process@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_and_wait>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     115c <spawn_and_wait+0x1c>
cmp    $0xffffffff,%eax
je     117c <spawn_and_wait+0x3c>
mov    %eax,%edi
pop    %rbx
jmp    1030 <wait_for_process@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
lea    0xe97(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5e(%rip),%rax
jmp    118f <spawn_and_wait+0x4f>
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@EX_SOFTWARE = global i32 0
@.str = [5 x i8] c"fork\00"
@EX_OSERR = global i32 0
@.str.1 = [5 x i8] c"exec\00"
define i32 @spawn_and_wait(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
store i8** %0, i8*** %2
%5 = load i32, i32* @EX_SOFTWARE
store i32 %5, i32* %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = load i32, i32* %4
switch i32 %7, label %19 [
i32 -1, label %8
i32 0, label %11
]
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EX_OSERR
store i32 %10, i32* %3
br label %22
11: ; preds = %1
%12 = load i8**, i8*** %2
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %2
%16 = call i32 @execvp(i8* %14, i8** %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EX_SOFTWARE
store i32 %18, i32* %3
br label %22
19: ; preds = %1
%20 = load i32, i32* %4
%21 = call i32 @wait_for_process(i32 %20)
store i32 %21, i32* %3
br label %22
22: ; preds = %19, %11, %8
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@EX_SOFTWARE = global i32 0
@.str = [5 x i8] c"fork\00"
@EX_OSERR = global i32 0
@.str.1 = [5 x i8] c"exec\00"
define i32 @spawn_and_wait(i8** %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %11 [
i32 -1, label %3
i32 0, label %6
]
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* @EX_OSERR
br label %13
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EX_SOFTWARE
br label %13
11: ; preds = %1
%12 = tail call i32 @wait_for_process(i32 %2)
br label %13
13: ; preds = %11, %6, %3
%14 = phi i32 [ %12, %11 ], [ %10, %6 ], [ %5, %3 ]
ret i32 %14
}