#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_SUCCESS;
int /*<<< orphan*/ execl(char const *, char const *, char *, char const *,
                         char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
char *getenv(char *);
int /*<<< orphan*/ setsid();
pid_t spawn(const char *cmd) {
  pid_t pid;
  const char *shell;
  pid = fork();
  if (pid)
    return pid;
  shell = getenv("SHELL");
  if (!shell)
    shell = "/bin/sh";
  setsid();
  execl(shell, shell, "-c", cmd, (char *)NULL);
  exit(EXIT_SUCCESS);
  return pid;
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117c <spawn+0x2c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <spawn+0x52>
lea    0xe68(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <setsid@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe52(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <execl@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<spawn>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     116b <spawn+0x1b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe85(%rip),%r14
cmovne %rax,%r14
xor    %eax,%eax
call   1040 <setsid@plt>
lea    0xe7b(%rip),%rdx
mov    %r14,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1060 <execl@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"-c\00"
@EXIT_SUCCESS = global i32 0
define i64 @spawn(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i64, i64* %3
ret i64 %9
10: ; preds = %1
%11 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %15
15: ; preds = %14, %10
%16 = call i32 (...) @setsid()
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = load i8*, i8** %2
%20 = call i32 @execl(i8* %17, i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %19, i8* null)
%21 = load i32, i32* @EXIT_SUCCESS
%22 = call i32 @exit(i32 %21)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"-c\00"
@EXIT_SUCCESS = global i32 0
define i64 @spawn(i8* %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %5, label %4
4: ; preds = %1
ret i64 %2
5: ; preds = %1
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %6
%9 = tail call i32 (...) @setsid()
%10 = tail call i32 @execl(i8* %8, i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* null)
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
}