#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 */
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
char *getenv(char *);
int /*<<< orphan*/ setsid();
char *strrchr(char *, char);
void fork_exec(char *cmd) {
  char *envshell, *envshellname;
  pid_t pid = fork();
  switch (pid) {
  case 0:
    setsid();
    envshell = getenv("SHELL");
    if (envshell == NULL) {
      envshell = "/bin/sh";
    }
    envshellname = strrchr(envshell, '/');
    if (envshellname == NULL) {
      envshellname = envshell;
    } else {
      envshellname++;
    }
    execlp(envshell, envshellname, "-c", cmd, NULL);
    err("exec failed, cleaning up child");
    exit(1);
    break;
  case -1:
    err("can't fork");
    break;
  }
}


x86 assembly (clang, -O0):
<fork_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1240 <fork_exec+0xd0>
jmp    119a <fork_exec+0x2a>
mov    -0x20(%rbp),%eax
test   %eax,%eax
jne    124c <fork_exec+0xdc>
jmp    11aa <fork_exec+0x3a>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <fork_exec+0x67>
lea    0xe33(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <fork_exec+0x91>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    120d <fork_exec+0x9d>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xdee(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execlp@plt>
lea    0xde0(%rip),%rdi
call   1060 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xde9(%rip),%rdi
call   1060 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_exec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     118f <fork_exec+0x1f>
test   %eax,%eax
je     11a2 <fork_exec+0x32>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe9a(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <err@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe50(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe47(%rip),%r14
cmovne %rax,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r14,%rsi
lea    0xe2c(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <execlp@plt>
lea    0xe1a(%rip),%rdi
call   1060 <err@plt>
mov    $0x1,%edi
call   1070 <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"
@.str.3 = [31 x i8] c"exec failed, cleaning up child\00"
@.str.4 = [11 x i8] c"can't fork\00"
define void @fork_exec(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
%7 = load i32, i32* %5
switch i32 %7, label %33 [
i32 0, label %8
i32 -1, label %31
]
8: ; preds = %1
%9 = call i32 (...) @setsid()
%10 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %8
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %14
14: ; preds = %13, %8
%15 = load i8*, i8** %3
%16 = call i8* @strrchr(i8* %15, i8 signext 47)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i8*, i8** %3
store i8* %20, i8** %4
br label %24
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
br label %24
24: ; preds = %21, %19
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = load i8*, i8** %2
%28 = call i32 @execlp(i8* %25, i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32* null)
%29 = call i32 @err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %1
%32 = call i32 @err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %1, %31
ret void
}

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"
@.str.3 = [31 x i8] c"exec failed, cleaning up child\00"
@.str.4 = [11 x i8] c"can't fork\00"
define void @fork_exec(i8* %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %17 [
i32 0, label %3
i32 -1, label %15
]
3: ; preds = %1
%4 = tail call i32 (...) @setsid()
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %5
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 47)
%9 = icmp eq i8* %8, null
%10 = getelementptr inbounds i8, i8* %8, i64 1
%11 = select i1 %9, i8* %7, i8* %10
%12 = tail call i32 @execlp(i8* %7, i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%13 = tail call i32 @err(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = tail call i32 @err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %17
17: ; preds = %1, %15
ret void
}