#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*/ O_RDWR;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ dup(int);
int errno;
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, void *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int inst_start_shell(char *tty_tv) {
  char *args_apci[] = {"bash", 0};
  char *env_pci[] = {"TERM=linux",
                     "PS1=`pwd -P` # ",
                     "HOME=/",
                     "HISTFILE=",
                     "PATH=/lbin:/bin:/sbin:/usr/bin:/usr/sbin",
                     0};
  int fd_ii;
  int sh_pid = fork();
  if (sh_pid != 0)
    return sh_pid;
  fclose(stdin);
  fclose(stdout);
  fclose(stderr);
  setsid();
  fd_ii = open(tty_tv, O_RDWR);
  ioctl(fd_ii, TIOCSCTTY, (void *)1);
  (void)dup(fd_ii);
  (void)dup(fd_ii);
  execve("/bin/bash", args_apci, env_pci);
  fprintf(stderr, "Couldn't start shell (errno = %d)\n", errno);
  exit(-1);
  return 0;
}


x86 assembly (clang, -O0):
<inst_start_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2c2d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c2a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
lea    0xe27(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe27(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe2c(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe28(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe27(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     1226 <inst_start_shell+0x86>
mov    -0x58(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1060 <ioctl@plt>
mov    -0x54(%rbp),%edi
call   1040 <dup@plt>
mov    -0x54(%rbp),%edi
call   1040 <dup@plt>
lea    -0x20(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    0xdb4(%rip),%rdi
call   1080 <execve@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
lea    0xda0(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<inst_start_shell>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
movaps 0x2c3f(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe4b(%rip),%rax
mov    %rax,(%rsp)
lea    0xe4b(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe4f(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe4a(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe48(%rip),%rax
mov    %rax,0x20(%rsp)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11fc <inst_start_shell+0x6c>
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    0x2db1(%rip),%r14
mov    (%r14),%edi
call   1030 <fclose@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    %ebx,%edi
call   1040 <dup@plt>
mov    %ebx,%edi
call   1040 <dup@plt>
lea    0xdea(%rip),%rdi
lea    0x30(%rsp),%rsi
mov    %rsp,%rdx
call   1070 <execve@plt>
mov    (%r14),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    0xdd4(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"bash\00"
@__const.inst_start_shell.args_apci = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [11 x i8] c"TERM=linux\00"
@.str.2 = [16 x i8] c"PS1=`pwd -P` # \00"
@.str.3 = [7 x i8] c"HOME=/\00"
@.str.4 = [10 x i8] c"HISTFILE=\00"
@.str.5 = [41 x i8] c"PATH=/lbin:/bin:/sbin:/usr/bin:/usr/sbin\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
@.str.6 = [10 x i8] c"/bin/bash\00"
@.str.7 = [35 x i8] c"Couldn't start shell (errno = %d)\0A\00"
@errno = global i32 0
define i32 @inst_start_shell(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i8*]
%4 = alloca [6 x i8*]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast [2 x i8*]* %3 to i8*
%8 = bitcast [6 x i8*]* %4 to i8*
%9 = bitcast i8* %8 to [6 x i8*]*
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i32 0, i32 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i32 0, i32 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i32 0, i32 2
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i32 0, i32 3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i32 0, i32 4
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i32 0, i32 0), i8** %14
%15 = call i32 (...) @fork()
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = load i32, i32* %6
ret i32 %19
20: ; preds = %1
%21 = load i32, i32* @stdin
%22 = call i32 @fclose(i32 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fclose(i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 @fclose(i32 %25)
%27 = call i32 (...) @setsid()
%28 = load i8*, i8** %2
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = load i32, i32* @TIOCSCTTY
%33 = call i32 @ioctl(i32 %31, i32 %32, i8* inttoptr (i64 1 to i8*))
%34 = load i32, i32* %5
%35 = call i32 @dup(i32 %34)
%36 = load i32, i32* %5
%37 = call i32 @dup(i32 %36)
%38 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%39 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
%40 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %38, i8** %39)
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"bash\00"
@__const.inst_start_shell.args_apci = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [11 x i8] c"TERM=linux\00"
@.str.2 = [16 x i8] c"PS1=`pwd -P` # \00"
@.str.3 = [7 x i8] c"HOME=/\00"
@.str.4 = [10 x i8] c"HISTFILE=\00"
@.str.5 = [41 x i8] c"PATH=/lbin:/bin:/sbin:/usr/bin:/usr/sbin\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
@.str.6 = [10 x i8] c"/bin/bash\00"
@.str.7 = [35 x i8] c"Couldn't start shell (errno = %d)\0A\00"
@errno = global i32 0
define i32 @inst_start_shell(i8* nocapture %0) {
%2 = alloca [2 x i8*]
%3 = alloca [6 x i8*]
%4 = bitcast [2 x i8*]* %2 to i8*
%5 = bitcast [6 x i8*]* %3 to i8*
%6 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 4
%7 = bitcast i8** %6 to i8*
%8 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i8** %12
%13 = tail call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %1
ret i32 %13
16: ; preds = %1
%17 = load i32, i32* @stdin
%18 = tail call i32 @fclose(i32 %17)
%19 = load i32, i32* @stdout
%20 = tail call i32 @fclose(i32 %19)
%21 = load i32, i32* @stderr
%22 = tail call i32 @fclose(i32 %21)
%23 = tail call i32 (...) @setsid()
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* %0, i32 %24)
%26 = load i32, i32* @TIOCSCTTY
%27 = tail call i32 @ioctl(i32 %25, i32 %26, i8* inttoptr (i64 1 to i8*))
%28 = tail call i32 @dup(i32 %25)
%29 = tail call i32 @dup(i32 %25)
%30 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%31 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %30, i8** %8)
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %33)
%35 = call i32 @exit(i32 -1)
unreachable
}