#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 */

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ die(char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ err(char const *, int /*<<< orphan*/, char const *);
int /*<<< orphan*/ errno;
int fork();
int /*<<< orphan*/ wait(int *);
bool spawn(void (*run)(void *), void *args, const char *msg, const char *id) {
  int status;
  switch (fork()) {
  case -1:
    die("failed to fork", errno, NULL);
  case 0:
    run(args);
  }
  wait(&status);
  return WEXITSTATUS(status) == 0 || err(msg, 0, id);
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1192 <spawn+0x42>
jmp    1182 <spawn+0x32>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11ab <spawn+0x5b>
jmp    11b5 <spawn+0x65>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <die@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
lea    -0x24(%rbp),%rdi
call   1060 <wait@plt>
mov    -0x24(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     11f1 <spawn+0xa1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <err@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     118b <spawn+0x3b>
cmp    $0xffffffff,%eax
jne    1190 <spawn+0x40>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
xor    %edx,%edx
call   1050 <die@plt>
mov    %r12,%rdi
call   *%rbx
lea    0x4(%rsp),%rdi
call   1060 <wait@plt>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %rax,%rax
je     11c2 <spawn+0x72>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <err@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    11c7 <spawn+0x77>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"failed to fork\00"
@errno = global i32 0
define i32 @spawn(void (i8*)* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca void (i8*)*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store void (i8*)* %0, void (i8*)** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%10 = call i32 (...) @fork()
switch i32 %10, label %17 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %4
%12 = load i32, i32* @errno
%13 = call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12, i32* null)
br label %14
14: ; preds = %4, %11
%15 = load void (i8*)*, void (i8*)** %5
%16 = load i8*, i8** %6
br label %17
17: ; preds = %14, %4
%18 = call i32 @wait(i32* %9)
%19 = load i32, i32* %9
%20 = call i64 @WEXITSTATUS(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = call i64 @err(i8* %23, i32 0, i8* %24)
%26 = icmp ne i64 %25, 0
br label %27
27: ; preds = %22, %17
%28 = phi i1 [ true, %17 ], [ %26, %22 ]
%29 = zext i1 %28 to i32
ret i32 %29
}

LLVM IR (-O2):
@.str = [15 x i8] c"failed to fork\00"
@errno = global i32 0
define i32 @spawn(void (i8*)* nocapture %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @fork()
switch i32 %7, label %12 [
i32 -1, label %8
i32 0, label %11
]
8: ; preds = %4
%9 = load i32, i32* @errno
%10 = tail call i32 @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9, i32* null)
br label %11
11: ; preds = %4, %8
tail call void %0(i8* %1)
br label %12
12: ; preds = %11, %4
%13 = call i32 @wait(i32* %5)
%14 = load i32, i32* %5
%15 = call i64 @WEXITSTATUS(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = call i64 @err(i8* %2, i32 0, i8* %3)
%19 = icmp ne i64 %18, 0
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %17, %12
%22 = phi i32 [ 1, %12 ], [ %20, %17 ]
ret i32 %22
}