#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 */
scalar_t__ WCOREDUMP(int);
scalar_t__ WEXITSTATUS(int);
scalar_t__ WTERMSIG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, ...);
int fork();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
void cli_spawn_proc(void (*cb)(void *), void *arg) {
  pid_t pid;
  int status;
  pid = fork();
  switch (pid) {
  case -1:
    fatal("cli_compile_cfile: fork() %s", strerror(errno));
  case 0:
    cb(arg);
    fatal("cli_spawn_proc: %s", strerror(errno));
  default:
    break;
  }
  if (waitpid(pid, &status, 0) == -1)
    fatal("couldn't wait for child %d", pid);
  if (WEXITSTATUS(status) || WTERMSIG(status) || WCOREDUMP(status))
    fatal("subprocess trouble, check output");
}


x86 assembly (clang, -O0):
<cli_spawn_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11ae <cli_spawn_proc+0x3e>
jmp    119e <cli_spawn_proc+0x2e>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11cc <cli_spawn_proc+0x5c>
jmp    11f4 <cli_spawn_proc+0x84>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rdi
call   *%rax
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
jmp    11f9 <cli_spawn_proc+0x89>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1221 <cli_spawn_proc+0xb1>
mov    -0x14(%rbp),%esi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
mov    -0x18(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    1257 <cli_spawn_proc+0xe7>
mov    -0x18(%rbp),%edi
call   1040 <WTERMSIG@plt>
cmp    $0x0,%rax
jne    1257 <cli_spawn_proc+0xe7>
mov    -0x18(%rbp),%edi
call   1030 <WCOREDUMP@plt>
cmp    $0x0,%rax
je     1265 <cli_spawn_proc+0xf5>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1080 <fatal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_spawn_proc>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11af <cli_spawn_proc+0x3f>
cmp    $0xffffffff,%ebx
jne    11d3 <cli_spawn_proc+0x63>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    %r15,%rdi
call   *%r14
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <fatal@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11f6 <cli_spawn_proc+0x86>
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <fatal@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
test   %rax,%rax
jne    1220 <cli_spawn_proc+0xb0>
mov    0xc(%rsp),%edi
call   1040 <WTERMSIG@plt>
test   %rax,%rax
jne    1220 <cli_spawn_proc+0xb0>
mov    0xc(%rsp),%edi
call   1030 <WCOREDUMP@plt>
test   %rax,%rax
je     122e <cli_spawn_proc+0xbe>
lea    0xe24(%rip),%rdi
xor    %eax,%eax
call   1080 <fatal@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [29 x i8] c"cli_compile_cfile: fork() %s\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"cli_spawn_proc: %s\00"
@.str.2 = [27 x i8] c"couldn't wait for child %d\00"
@.str.3 = [33 x i8] c"subprocess trouble, check output\00"
define void @cli_spawn_proc(void (i8*)* %0, i8* %1) {
%3 = alloca void (i8*)*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store void (i8*)* %0, void (i8*)** %3
store i8* %1, i8** %4
%7 = call i32 (...) @fork()
store i32 %7, i32* %5
%8 = load i32, i32* %5
switch i32 %8, label %19 [
i32 -1, label %9
i32 0, label %13
]
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %2, %9
%14 = load void (i8*)*, void (i8*)** %3
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %2, %13
br label %20
20: ; preds = %19
%21 = load i32, i32* %5
%22 = call i32 @waitpid(i32 %21, i32* %6, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* %6
%29 = call i64 @WEXITSTATUS(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %27
%32 = load i32, i32* %6
%33 = call i64 @WTERMSIG(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* %6
%37 = call i64 @WCOREDUMP(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35, %31, %27
%40 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"cli_compile_cfile: fork() %s\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"cli_spawn_proc: %s\00"
@.str.2 = [27 x i8] c"couldn't wait for child %d\00"
@.str.3 = [33 x i8] c"subprocess trouble, check output\00"
define void @cli_spawn_proc(void (i8*)* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %14 [
i32 -1, label %6
i32 0, label %10
]
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %2, %6
tail call void %0(i8* %1)
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %2
%15 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %5)
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* %3
%21 = call i64 @WEXITSTATUS(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = load i32, i32* %3
%25 = call i64 @WTERMSIG(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i64 @WCOREDUMP(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27, %23, %19
%32 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
ret void
}