#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 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 result;
  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);
  result = dup(fd_ii);
  result = 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,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     1226 <inst_start_shell+0x86>
mov    -0x5c(%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    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x58(%rbp)
lea    -0x20(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    0xdae(%rip),%rdi
call   1080 <execve@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
lea    0xd9a(%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
%7 = alloca i32
store i8* %0, i8** %2
%8 = bitcast [2 x i8*]* %3 to i8*
%9 = bitcast [6 x i8*]* %4 to i8*
%10 = bitcast i8* %9 to [6 x i8*]*
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i32 0, i32 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i32 0, i32 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i32 0, i32 2
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i32 0, i32 3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i32 0, i32 4
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i32 0, i32 0), i8** %15
%16 = call i32 (...) @fork()
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = load i32, i32* %7
ret i32 %20
21: ; preds = %1
%22 = load i32, i32* @stdin
%23 = call i32 @fclose(i32 %22)
%24 = load i32, i32* @stdout
%25 = call i32 @fclose(i32 %24)
%26 = load i32, i32* @stderr
%27 = call i32 @fclose(i32 %26)
%28 = call i32 (...) @setsid()
%29 = load i8*, i8** %2
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = load i32, i32* @TIOCSCTTY
%34 = call i32 @ioctl(i32 %32, i32 %33, i8* inttoptr (i64 1 to i8*))
%35 = load i32, i32* %5
%36 = call i32 @dup(i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %5
%38 = call i32 @dup(i32 %37)
store i32 %38, i32* %6
%39 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%40 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
%41 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %39, i8** %40)
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %43)
%45 = 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
}