#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 /*<<< orphan*/ sigset_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_SETMASK;
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
pid_t spawn(char **command) {
  pid_t pid;
  pid_t parent_pid = getpid();
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  } else if (pid) {
    return pid;
  } else {
    sigset_t mask;
    sigemptyset(&mask);
    sigprocmask(SIG_SETMASK, &mask, NULL);
    if (execvp(command[0], command)) {
      perror("exec");
      kill(parent_pid, SIGTERM);
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c0 <spawn+0x40>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
je     11d5 <spawn+0x55>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1246 <spawn+0xc6>
lea    -0x1c(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
cmp    $0x0,%rax
je     1235 <spawn+0xb5>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    123a <spawn+0xba>
jmp    123f <spawn+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11bc <spawn+0x3c>
cmp    $0xffffffff,%ebp
jne    11ec <spawn+0x6c>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1050 <sigemptyset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
test   %rax,%rax
jne    11f9 <spawn+0x79>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1060 <kill@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@SIG_SETMASK = global i32 0
@.str.1 = [5 x i8] c"exec\00"
@SIGTERM = global i32 0
define i32 @spawn(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
%7 = call i32 (...) @getpid()
store i32 %7, i32* %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32, i32* %4
store i32 %18, i32* %2
br label %38
19: ; preds = %14
%20 = call i32 @sigemptyset(i32* %6)
%21 = load i32, i32* @SIG_SETMASK
%22 = call i32 @sigprocmask(i32 %21, i32* %6, i32* null)
%23 = load i8**, i8*** %3
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = load i8**, i8*** %3
%27 = call i64 @execvp(i8* %25, i8** %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %5
%32 = load i32, i32* @SIGTERM
%33 = call i32 @kill(i32 %31, i32 %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %19
br label %36
36: ; preds = %35
br label %37
37: ; preds = %36
store i32 0, i32* %2
br label %38
38: ; preds = %37, %17
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@SIG_SETMASK = global i32 0
@.str.1 = [5 x i8] c"exec\00"
@SIGTERM = global i32 0
define i32 @spawn(i8** %0) {
%2 = alloca i32
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @fork()
switch i32 %4, label %22 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
%10 = call i32 @sigemptyset(i32* %2)
%11 = load i32, i32* @SIG_SETMASK
%12 = call i32 @sigprocmask(i32 %11, i32* %2, i32* null)
%13 = load i8*, i8** %0
%14 = call i64 @execvp(i8* %13, i8** %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @SIGTERM
%19 = call i32 @kill(i32 %3, i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %8
br label %22
22: ; preds = %1, %21
ret i32 %4
}