#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 MAXPATHLEN;
scalar_t__ UnprefixedCustomShell(char const *);
int asprintf(char **, char *, char const *);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ execlp(char const *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char const *, char *, int /*<<< orphan*/);
char *strrchr(char const *, char);
int launch_shell(const char *customShell) {
  const char *shell = UnprefixedCustomShell(customShell) ?: getenv("SHELL");
  if (!shell) {
    err(1, "SHELL environment variable not set");
  }
  if (customShell) {
    extern const char **environ;
    for (int i = 0; environ[i] != NULL; i++) {
      if (!strncmp(environ[i], "SHELL=", strlen("SHELL="))) {
        environ[i] = customShell;
        break;
      }
    }
  }
  char *slash = strrchr(shell, '/');
  char *argv0;
  int len;
  if (slash) {
    len = asprintf(&argv0, "-%s", slash + 1);
  } else {
    len = asprintf(&argv0, "-%s", shell);
  }
  if (!argv0) {
    err(1, "asprintf returned NULL (out of memory?)");
  }
  if (len <= 0) {
    err(1, "failed to format shell's argv[0]");
  }
  if (len >= MAXPATHLEN) {
    errx(1, "shell path is too long");
  }
  execlp(shell, argv0, (char *)0);
  err(1, "Failed to exec %s with arg %s", shell, argv0);
}


x86 assembly (clang, -O0):
<launch_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <UnprefixedCustomShell@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     11c0 <launch_shell+0x30>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11d0 <launch_shell+0x40>
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f6 <launch_shell+0x66>
mov    $0x1,%edi
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
cmpq   $0x0,-0x10(%rbp)
je     1291 <launch_shell+0x101>
movl   $0x0,-0x1c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     128c <launch_shell+0xfc>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
lea    0xdeb(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xdd9(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    1279 <launch_shell+0xe9>
mov    -0x10(%rbp),%rdx
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    128c <launch_shell+0xfc>
jmp    127e <launch_shell+0xee>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1208 <launch_shell+0x78>
jmp    1291 <launch_shell+0x101>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12ce <launch_shell+0x13e>
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
lea    -0x30(%rbp),%rdi
lea    0xd6f(%rip),%rsi
call   1090 <asprintf@plt>
mov    %eax,-0x34(%rbp)
jmp    12e5 <launch_shell+0x155>
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xd53(%rip),%rsi
call   1090 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1303 <launch_shell+0x173>
mov    $0x1,%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
cmpl   $0x0,-0x34(%rbp)
jg     1320 <launch_shell+0x190>
mov    $0x1,%edi
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x34(%rbp),%eax
mov    0x2cbe(%rip),%rcx
cmp    (%rcx),%eax
jl     1343 <launch_shell+0x1b3>
mov    $0x1,%edi
lea    0xd3f(%rip),%rsi
call   1060 <errx@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execlp@plt>
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%edi
lea    0xd2c(%rip),%rsi
mov    $0x0,%al
call   1080 <err@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch_shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   10a0 <UnprefixedCustomShell@plt>
test   %rax,%rax
jne    11b4 <launch_shell+0x24>
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
je     11c3 <launch_shell+0x33>
test   %r14,%r14
jne    11db <launch_shell+0x4b>
jmp    1237 <launch_shell+0xa7>
lea    0xe3c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
test   %r14,%r14
je     1237 <launch_shell+0xa7>
mov    0x2dfe(%rip),%r12
mov    (%r12),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
je     1237 <launch_shell+0xa7>
xor    %ebp,%ebp
lea    0xe32(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     122d <launch_shell+0x9d>
mov    (%r12),%rax
mov    0x8(%rax,%rbp,8),%rbx
add    $0x1,%rbp
test   %rbx,%rbx
jne    1200 <launch_shell+0x70>
jmp    1237 <launch_shell+0xa7>
mov    %ebp,%eax
mov    (%r12),%rcx
mov    %r14,(%rcx,%rax,8)
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     125c <launch_shell+0xcc>
add    $0x1,%rax
lea    0xddc(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
jmp    1269 <launch_shell+0xd9>
lea    0xdcd(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
call   1090 <asprintf@plt>
mov    %eax,%ebp
cmpq   $0x0,(%rsp)
je     12e1 <launch_shell+0x151>
test   %ebp,%ebp
jg     128e <launch_shell+0xfe>
lea    0xdda(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
mov    0x2d53(%rip),%rax
cmp    (%rax),%ebp
jl     12aa <launch_shell+0x11a>
lea    0xddd(%rip),%rsi
mov    $0x1,%edi
call   1060 <errx@plt>
mov    (%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   10b0 <execlp@plt>
mov    (%rsp),%rcx
lea    0xdd1(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <err@plt>
test   %ebp,%ebp
jg     128e <launch_shell+0xfe>
jmp    127b <launch_shell+0xeb>

LLVM IR (-O0):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [35 x i8] c"SHELL environment variable not set\00"
@environ = external global i8**
@.str.2 = [7 x i8] c"SHELL=\00"
@.str.3 = [4 x i8] c"-%s\00"
@.str.4 = [40 x i8] c"asprintf returned NULL (out of memory?)\00"
@.str.5 = [33 x i8] c"failed to format shell's argv[0]\00"
@MAXPATHLEN = global i32 0
@.str.6 = [23 x i8] c"shell path is too long\00"
@.str.7 = [30 x i8] c"Failed to exec %s with arg %s\00"
define i32 @launch_shell(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i64 @UnprefixedCustomShell(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = inttoptr i64 %10 to i8*
br label %16
14: ; preds = %1
%15 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
%17 = phi i8* [ %13, %12 ], [ %15, %14 ]
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %53
25: ; preds = %22
store i32 0, i32* %5
br label %26
26: ; preds = %49, %25
%27 = load i8**, i8*** @environ
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %52
33: ; preds = %26
%34 = load i8**, i8*** @environ
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @strncmp(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %33
%43 = load i8*, i8** %3
%44 = load i8**, i8*** @environ
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
store i8* %43, i8** %47
br label %52
48: ; preds = %33
br label %49
49: ; preds = %48
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
br label %26
52: ; preds = %42, %26
br label %53
53: ; preds = %52, %22
%54 = load i8*, i8** %4
%55 = call i8* @strrchr(i8* %54, i8 signext 47)
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 %61, i32* %8
br label %65
62: ; preds = %53
%63 = load i8*, i8** %4
%64 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %63)
store i32 %64, i32* %8
br label %65
65: ; preds = %62, %58
%66 = load i8*, i8** %7
%67 = icmp ne i8* %66, null
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %65
%71 = load i32, i32* %8
%72 = icmp sle i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %70
%74 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %75
75: ; preds = %73, %70
%76 = load i32, i32* %8
%77 = load i32, i32* @MAXPATHLEN
%78 = icmp sge i32 %76, %77
br i1 %78, label %79, label %81
79: ; preds = %75
%80 = call i32 @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %81
81: ; preds = %79, %75
%82 = load i8*, i8** %4
%83 = load i8*, i8** %7
%84 = call i32 @execlp(i8* %82, i8* %83, i8* null)
%85 = load i8*, i8** %4
%86 = load i8*, i8** %7
%87 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %85, i8* %86)
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [35 x i8] c"SHELL environment variable not set\00"
@environ = external global i8**
@.str.2 = [7 x i8] c"SHELL=\00"
@.str.3 = [4 x i8] c"-%s\00"
@.str.4 = [40 x i8] c"asprintf returned NULL (out of memory?)\00"
@.str.5 = [33 x i8] c"failed to format shell's argv[0]\00"
@MAXPATHLEN = global i32 0
@.str.6 = [23 x i8] c"shell path is too long\00"
@.str.7 = [30 x i8] c"Failed to exec %s with arg %s\00"
define i32 @launch_shell(i8* %0) {
%2 = alloca i8*
%3 = tail call i64 @UnprefixedCustomShell(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = inttoptr i64 %3 to i8*
br label %9
7: ; preds = %1
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %5
%10 = phi i8* [ %6, %5 ], [ %8, %7 ]
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = icmp eq i8* %0, null
br i1 %15, label %36, label %16
16: ; preds = %14
%17 = load i8**, i8*** @environ
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %36, label %20
20: ; preds = %16, %30
%21 = phi i64 [ %31, %30 ], [ 0, %16 ]
%22 = phi i8* [ %34, %30 ], [ %18, %16 ]
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = and i64 %21, 4294967295
%28 = load i8**, i8*** @environ
%29 = getelementptr inbounds i8*, i8** %28, i64 %27
store i8* %0, i8** %29
br label %36
30: ; preds = %20
%31 = add nuw i64 %21, 1
%32 = load i8**, i8*** @environ
%33 = getelementptr inbounds i8*, i8** %32, i64 %31
%34 = load i8*, i8** %33
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %20
36: ; preds = %30, %16, %26, %14
%37 = tail call i8* @strrchr(i8* dereferenceable(1) %10, i8 signext 47)
%38 = bitcast i8** %2 to i8*
%39 = icmp eq i8* %37, null
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = getelementptr inbounds i8, i8* %37, i64 1
%42 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %45
43: ; preds = %36
%44 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %45
45: ; preds = %43, %40
%46 = phi i32 [ %42, %40 ], [ %44, %43 ]
%47 = load i8*, i8** %2
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = icmp slt i32 %46, 1
br i1 %52, label %53, label %55
53: ; preds = %51
%54 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %51
%56 = load i32, i32* @MAXPATHLEN
%57 = icmp slt i32 %46, %56
br i1 %57, label %60, label %58
58: ; preds = %55
%59 = call i32 @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load i8*, i8** %2
%62 = call i32 @execlp(i8* %10, i8* %61, i8* null)
%63 = load i8*, i8** %2
%64 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %10, i8* %63)
ret i32 undef
}