#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 */
int /*<<< orphan*/ EXIT_FAILURE;
char *basename(char const *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ execl(char const *, char *, int /*<<< orphan*/ *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
void exec_shell(void) {
  const char *shell = getenv("SHELL"), *shell_basename;
  char *arg0;
  if (!shell)
    shell = "/bin/sh";
  shell_basename = basename(shell);
  arg0 = malloc(strlen(shell_basename) + 2);
  if (!arg0)
    err(EXIT_FAILURE, "failed to allocate memory");
  arg0[0] = '-';
  strcpy(arg0 + 1, shell_basename);
  execl(shell, arg0, NULL);
  err(EXIT_FAILURE, "failed to execute %s", shell);
}


x86 assembly (clang, -O0):
<exec_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119e <exec_shell+0x2e>
lea    0xe6c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e6 <exec_shell+0x76>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    -0x18(%rbp),%rax
movb   $0x2d,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_shell>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe7b(%rip),%r15
cmovne %rax,%r15
mov    %r15,%rdi
call   1060 <basename@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ca <exec_shell+0x5a>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1070 <err@plt>
movb   $0x2d,(%rbx)
mov    %rbx,%rdi
add    $0x1,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <execl@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <err@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"failed to allocate memory\00"
@.str.3 = [21 x i8] c"failed to execute %s\00"
define void @exec_shell() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %8
8: ; preds = %7, %0
%9 = load i8*, i8** %1
%10 = call i8* @basename(i8* %9)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %12, 2
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %8
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 (i32, i8*, ...) @err(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %8
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 45, i8* %22
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = load i8*, i8** %2
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %1
%28 = load i8*, i8** %3
%29 = call i32 @execl(i8* %27, i8* %28, i32* null)
%30 = load i32, i32* @EXIT_FAILURE
%31 = load i8*, i8** %1
%32 = call i32 (i32, i8*, ...) @err(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %31)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"failed to allocate memory\00"
@.str.3 = [21 x i8] c"failed to execute %s\00"
define void @exec_shell() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %1
%4 = tail call i8* @basename(i8* %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%6 = add nsw i64 %5, 2
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 (i32, i8*, ...) @err(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %9, %0
store i8 45, i8* %7
%13 = getelementptr inbounds i8, i8* %7, i64 1
%14 = tail call i32 @strcpy(i8* %13, i8* %4)
%15 = tail call i32 @execl(i8* %3, i8* %7, i32* null)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 (i32, i8*, ...) @err(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %3)
ret void
}