#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 /*<<< orphan*/ uid_t;
struct passwd {
  char *pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ cbreak();
int /*<<< orphan*/ changeDirectory(char *);
int /*<<< orphan*/ curs_set(int);
int /*<<< orphan*/ echo();
int /*<<< orphan*/ free(char *);
char getch();
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ move(int, scalar_t__);
int /*<<< orphan*/ nocbreak();
int /*<<< orphan*/ noecho();
char *realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ system(char *);
void insertShell(int scry, int scrx) {
  struct passwd *pw;
  uid_t uid;
  uid = geteuid();
  pw = getpwuid(uid);
  echo();
  curs_set(1);
  nocbreak();
  move(scry, strlen(pw->pw_name) + 2);
  char *shcomm = malloc(2);
  *shcomm = getch();
  char newchar;
  if (*shcomm != '\n') {
    *(shcomm + 1) = '\0';
    char *shlastchar = shcomm;
    while ((newchar = getch()) != '\n') {
      shcomm = realloc(shcomm, strlen(shcomm) + 2);
      *(++shlastchar) = newchar;
      *(shlastchar + 1) = '\0';
    }
    if (strncmp(shcomm, "cd ", 3) == 0) {
      changeDirectory(shcomm + 3);
    } else {
      system(shcomm);
    }
  }
  free(shcomm);
  noecho();
  curs_set(0);
  cbreak();
}


x86 assembly (clang, -O0):
<insertShell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <echo@plt>
mov    $0x1,%edi
call   1110 <curs_set@plt>
mov    $0x0,%al
call   10f0 <nocbreak@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rsi
add    $0x2,%rsi
call   1120 <move@plt>
mov    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10d0 <getch@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1336 <insertShell+0x136>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10d0 <getch@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     12f7 <insertShell+0xf7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
add    $0x2,%rsi
call   10c0 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x21(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,0x1(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    129a <insertShell+0x9a>
mov    -0x20(%rbp),%rdi
lea    0xcfe(%rip),%rsi
mov    $0x3,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1328 <insertShell+0x128>
mov    -0x20(%rbp),%rdi
add    $0x3,%rdi
call   10e0 <changeDirectory@plt>
jmp    1331 <insertShell+0x131>
mov    -0x20(%rbp),%rdi
call   1090 <system@plt>
jmp    1336 <insertShell+0x136>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1100 <noecho@plt>
xor    %edi,%edi
call   1110 <curs_set@plt>
mov    $0x0,%al
call   1060 <cbreak@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertShell>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <geteuid@plt>
mov    %eax,%edi
call   1070 <getpwuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <echo@plt>
mov    $0x1,%edi
call   1110 <curs_set@plt>
xor    %eax,%eax
call   10f0 <nocbreak@plt>
mov    (%rbx),%rdi
call   1080 <strlen@plt>
lea    0x2(%rax),%rsi
mov    %ebp,%edi
call   1120 <move@plt>
mov    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10d0 <getch@plt>
mov    %al,(%r14)
cmp    $0xa,%al
je     12db <insertShell+0xdb>
movb   $0x0,0x1(%r14)
xor    %eax,%eax
call   10d0 <getch@plt>
cmp    $0xa,%al
je     12af <insertShell+0xaf>
mov    %eax,%ebx
mov    %r14,%rbp
add    $0x2,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <strlen@plt>
lea    0x2(%rax),%rsi
mov    %r14,%rdi
call   10c0 <realloc@plt>
mov    %rax,%r14
mov    %bl,-0x1(%rbp)
movb   $0x0,0x0(%rbp)
xor    %eax,%eax
call   10d0 <getch@plt>
mov    %eax,%ebx
add    $0x1,%rbp
cmp    $0xa,%al
jne    1280 <insertShell+0x80>
lea    0xd4a(%rip),%rsi
mov    %r14,%rdi
mov    $0x3,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     12d2 <insertShell+0xd2>
mov    %r14,%rdi
call   1090 <system@plt>
jmp    12db <insertShell+0xdb>
lea    0x3(%r14),%rdi
call   10e0 <changeDirectory@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1100 <noecho@plt>
xor    %edi,%edi
call   1110 <curs_set@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <cbreak@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [4 x i8] c"cd \00"
define void @insertShell(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.passwd*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 (...) @geteuid()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call %struct.passwd* @getpwuid(i32 %11)
store %struct.passwd* %12, %struct.passwd** %5
%13 = call i32 (...) @echo()
%14 = call i32 @curs_set(i32 1)
%15 = call i32 (...) @nocbreak()
%16 = load i32, i32* %3
%17 = load %struct.passwd*, %struct.passwd** %5
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 2
%22 = call i32 @move(i32 %16, i64 %21)
%23 = call i8* @malloc(i32 2)
store i8* %23, i8** %7
%24 = call signext i8 (...) @getch()
%25 = load i8*, i8** %7
store i8 %24, i8* %25
%26 = load i8*, i8** %7
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 10
br i1 %29, label %30, label %61
30: ; preds = %2
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 0, i8* %32
%33 = load i8*, i8** %7
store i8* %33, i8** %9
br label %34
34: ; preds = %38, %30
%35 = call signext i8 (...) @getch()
store i8 %35, i8* %8
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 10
br i1 %37, label %38, label %49
38: ; preds = %34
%39 = load i8*, i8** %7
%40 = load i8*, i8** %7
%41 = call i64 @strlen(i8* %40)
%42 = add nsw i64 %41, 2
%43 = call i8* @realloc(i8* %39, i64 %42)
store i8* %43, i8** %7
%44 = load i8, i8* %8
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %9
store i8 %44, i8* %46
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i64 1
store i8 0, i8* %48
br label %34
49: ; preds = %34
%50 = load i8*, i8** %7
%51 = call i64 @strncmp(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i64 3
%56 = call i32 @changeDirectory(i8* %55)
br label %60
57: ; preds = %49
%58 = load i8*, i8** %7
%59 = call i32 @system(i8* %58)
br label %60
60: ; preds = %57, %53
br label %61
61: ; preds = %60, %2
%62 = load i8*, i8** %7
%63 = call i32 @free(i8* %62)
%64 = call i32 (...) @noecho()
%65 = call i32 @curs_set(i32 0)
%66 = call i32 (...) @cbreak()
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [4 x i8] c"cd \00"
define void @insertShell(i32 %0, i32 %1) {
%3 = tail call i32 (...) @geteuid()
%4 = tail call %struct.passwd* @getpwuid(i32 %3)
%5 = tail call i32 (...) @echo()
%6 = tail call i32 @curs_set(i32 1)
%7 = tail call i32 (...) @nocbreak()
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = add nsw i64 %10, 2
%12 = tail call i32 @move(i32 %0, i64 %11)
%13 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%14 = tail call signext i8 (...) @getch()
store i8 %14, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %40, label %16
16: ; preds = %2
%17 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %17
%18 = tail call signext i8 (...) @getch()
%19 = icmp eq i8 %18, 10
br i1 %19, label %31, label %20
20: ; preds = %16, %20
%21 = phi i8 [ %29, %20 ], [ %18, %16 ]
%22 = phi i8* [ %27, %20 ], [ %13, %16 ]
%23 = phi i8* [ %26, %20 ], [ %13, %16 ]
%24 = tail call i64 @strlen(i8* dereferenceable(1) %23)
%25 = add nsw i64 %24, 2
%26 = tail call i8* @realloc(i8* %23, i64 %25)
%27 = getelementptr inbounds i8, i8* %22, i64 1
store i8 %21, i8* %27
%28 = getelementptr inbounds i8, i8* %22, i64 2
store i8 0, i8* %28
%29 = tail call signext i8 (...) @getch()
%30 = icmp eq i8 %29, 10
br i1 %30, label %31, label %20
31: ; preds = %20, %16
%32 = phi i8* [ %13, %16 ], [ %26, %20 ]
%33 = tail call i64 @strncmp(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = getelementptr inbounds i8, i8* %32, i64 3
%37 = tail call i32 @changeDirectory(i8* %36)
br label %40
38: ; preds = %31
%39 = tail call i32 @system(i8* %32)
br label %40
40: ; preds = %35, %38, %2
%41 = phi i8* [ %13, %2 ], [ %32, %38 ], [ %32, %35 ]
%42 = tail call i32 @free(i8* %41)
%43 = tail call i32 (...) @noecho()
%44 = tail call i32 @curs_set(i32 0)
%45 = tail call i32 (...) @cbreak()
ret void
}