#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*/ EXIT_FAILURE;
int WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *const);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int spawn_and_wait(char *const argv[]) {
  pid_t pid = fork();
  if (pid == 0) {
    execvp(argv[0], argv);
    fprintf(stderr, "can't execute '%s'\n", argv[0]);
    fflush(stderr);
    _exit(EXIT_FAILURE);
  }
  int status;
  waitpid(pid, &status, 0);
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<spawn_and_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <spawn_and_wait+0x6a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe47(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_and_wait>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11a3 <spawn_and_wait+0x33>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    0x2e2b(%rip),%r14
mov    (%r14),%edi
mov    (%rbx),%rdx
lea    0xe3e(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%r14),%edi
call   1050 <fflush@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"can't execute '%s'\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @spawn_and_wait(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
%4 = alloca i32
store i8** %0, i8*** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %1
%9 = load i8**, i8*** %2
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = load i8**, i8*** %2
%13 = call i32 @execvp(i8* %11, i8** %12)
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %2
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @stderr
%20 = call i32 @fflush(i32 %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @_exit(i32 %21)
unreachable
23: ; preds = %1
%24 = load i64, i64* %3
%25 = call i32 @waitpid(i64 %24, i32* %4, i32 0)
%26 = load i32, i32* %4
%27 = call i32 @WEXITSTATUS(i32 %26)
ret i32 %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"can't execute '%s'\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @spawn_and_wait(i8** %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = load i8*, i8** %0
%7 = tail call i32 @execvp(i8* %6, i8** %0)
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %0
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @stderr
%12 = tail call i32 @fflush(i32 %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @_exit(i32 %13)
unreachable
15: ; preds = %1
%16 = bitcast i32* %2 to i8*
%17 = call i32 @waitpid(i64 %3, i32* %2, i32 0)
%18 = load i32, i32* %2
%19 = call i32 @WEXITSTATUS(i32 %18)
ret i32 %19
}