#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*/ assert(int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int wait_for_process(int);
int spawn_and_wait(int argc, char **argv) {
  int rv = EX_SOFTWARE;
  pid_t pid = fork();
  assert(argc > 1);
  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    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x2,%eax
setge  %al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11aa <spawn_and_wait+0x5a>
jmp    119a <spawn_and_wait+0x4a>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11c7 <spawn_and_wait+0x77>
jmp    11f4 <spawn_and_wait+0xa4>
lea    0xe4f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11ff <spawn_and_wait+0xaf>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
lea    0xe27(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11ff <spawn_and_wait+0xaf>
mov    -0x18(%rbp),%edi
call   1030 <wait_for_process@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_and_wait>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0x2,%ebx
setge  %dil
call   1040 <assert@plt>
test   %ebp,%ebp
je     1184 <spawn_and_wait+0x34>
cmp    $0xffffffff,%ebp
je     11a4 <spawn_and_wait+0x54>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <wait_for_process@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e36(%rip),%rax
jmp    11b7 <spawn_and_wait+0x67>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* @EX_SOFTWARE
store i32 %7, i32* %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = load i32, i32* %3
%10 = icmp sgt i32 %9, 1
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %6
switch i32 %13, label %25 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EX_OSERR
store i32 %16, i32* %5
br label %28
17: ; preds = %2
%18 = load i8**, i8*** %4
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %4
%22 = call i32 @execvp(i8* %20, i8** %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EX_SOFTWARE
store i32 %24, i32* %5
br label %28
25: ; preds = %2
%26 = load i32, i32* %6
%27 = call i32 @wait_for_process(i32 %26)
store i32 %27, i32* %5
br label %28
28: ; preds = %25, %17, %14
%29 = load i32, i32* %5
ret i32 %29
}

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(i32 %0, i8** %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp sgt i32 %0, 1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
switch i32 %3, label %15 [
i32 -1, label %7
i32 0, label %10
]
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EX_OSERR
br label %17
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @execvp(i8* %11, i8** %1)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EX_SOFTWARE
br label %17
15: ; preds = %2
%16 = tail call i32 @wait_for_process(i32 %3)
br label %17
17: ; preds = %15, %10, %7
%18 = phi i32 [ %16, %15 ], [ %14, %10 ], [ %9, %7 ]
ret i32 %18
}