#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;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork1();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free_history();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ parsecmd(char *);
char *readline();
int /*<<< orphan*/ runcmd(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ wait();
int main(void) {
  char *buf;
  int fd;
  while ((fd = open("console", O_RDWR)) >= 0) {
    if (fd >= 3) {
      close(fd);
      break;
    }
  }
  while ((buf = readline())) {
    if (strncmp(buf, "cd ", 3) == 0) {
      buf += 3;
      while (*buf == ' ')
        ++buf;
      if (chdir(buf) < 0)
        fprintf(stderr, "cannot cd %s\n", buf);
      continue;
    }
    if (fork1() == 0)
      runcmd(parsecmd(buf));
    wait();
  }
  free_history();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     120c <main+0x4c>
cmpl   $0x3,-0x14(%rbp)
jl     1207 <main+0x47>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
jmp    120c <main+0x4c>
jmp    11cf <main+0xf>
jmp    1211 <main+0x51>
mov    $0x0,%al
call   1070 <readline@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12d0 <main+0x110>
mov    -0x10(%rbp),%rdi
lea    0xdd7(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12a3 <main+0xe3>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1272 <main+0xb2>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1251 <main+0x91>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    129e <main+0xde>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd73(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1211 <main+0x51>
mov    $0x0,%al
call   10a0 <fork1@plt>
cmp    $0x0,%rax
jne    12c4 <main+0x104>
mov    -0x10(%rbp),%rdi
call   10b0 <parsecmd@plt>
mov    %eax,%edi
call   1040 <runcmd@plt>
mov    $0x0,%al
call   10e0 <wait@plt>
jmp    1211 <main+0x51>
mov    $0x0,%al
call   1060 <free_history@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e19(%rip),%r14
lea    0xe2a(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     11fb <main+0x3b>
cmp    $0x3,%eax
jb     11e0 <main+0x20>
mov    %eax,%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1070 <readline@plt>
test   %rax,%rax
jne    1215 <main+0x55>
xor    %eax,%eax
call   1060 <free_history@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    %rax,%rbx
lea    0xde9(%rip),%r15
mov    0x2db2(%rip),%r12
lea    0xddf(%rip),%r14
jmp    1246 <main+0x86>
nop
xor    %eax,%eax
call   10e0 <wait@plt>
xor    %eax,%eax
call   1070 <readline@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1207 <main+0x47>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1280 <main+0xc0>
xor    %eax,%eax
call   10a0 <fork1@plt>
test   %rax,%rax
jne    1230 <main+0x70>
mov    %rbx,%rdi
call   10b0 <parsecmd@plt>
mov    %eax,%edi
call   1040 <runcmd@plt>
jmp    1230 <main+0x70>
nopl   0x0(%rax,%rax,1)
add    $0x2,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x20,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1290 <main+0xd0>
mov    %rbx,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jns    1237 <main+0x77>
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
jmp    1237 <main+0x77>

LLVM IR (-O0):
@.str = [8 x i8] c"console\00"
@O_RDWR = global i32 0
@.str.1 = [4 x i8] c"cd \00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"cannot cd %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
br label %4
4: ; preds = %14, %0
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = load i32, i32* %3
%10 = icmp sge i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* %3
%13 = call i32 @close(i32 %12)
br label %15
14: ; preds = %8
br label %4
15: ; preds = %11, %4
br label %16
16: ; preds = %50, %42, %15
%17 = call i8* (...) @readline()
store i8* %17, i8** %2
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %52
19: ; preds = %16
%20 = load i8*, i8** %2
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %19
%24 = load i8*, i8** %2
%25 = getelementptr inbounds i8, i8* %24, i64 3
store i8* %25, i8** %2
br label %26
26: ; preds = %31, %23
%27 = load i8*, i8** %2
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 32
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i8*, i8** %2
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %2
br label %26
34: ; preds = %26
%35 = load i8*, i8** %2
%36 = call i64 @chdir(i8* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %2
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %34
br label %16
43: ; preds = %19
%44 = call i64 (...) @fork1()
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i8*, i8** %2
%48 = call i32 @parsecmd(i8* %47)
%49 = call i32 @runcmd(i32 %48)
br label %50
50: ; preds = %46, %43
%51 = call i32 (...) @wait()
br label %16
52: ; preds = %16
%53 = call i32 (...) @free_history()
%54 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"console\00"
@O_RDWR = global i32 0
@.str.1 = [4 x i8] c"cd \00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"cannot cd %s\0A\00"
define i32 @main() {
br label %1
1: ; preds = %5, %0
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = icmp ugt i32 %3, 2
br i1 %6, label %7, label %1
7: ; preds = %5
%8 = tail call i32 @close(i32 %3)
br label %9
9: ; preds = %1, %7
%10 = tail call i8* (...) @readline()
%11 = icmp eq i8* %10, null
br i1 %11, label %40, label %12
12: ; preds = %9, %29
%13 = phi i8* [ %30, %29 ], [ %10, %9 ]
%14 = tail call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %13, i64 3
br label %18
18: ; preds = %18, %16
%19 = phi i8* [ %17, %16 ], [ %22, %18 ]
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 32
%22 = getelementptr inbounds i8, i8* %19, i64 1
br i1 %21, label %18, label %23
23: ; preds = %18
%24 = tail call i64 @chdir(i8* %19)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %29
29: ; preds = %23, %26, %38
%30 = tail call i8* (...) @readline()
%31 = icmp eq i8* %30, null
br i1 %31, label %40, label %12
32: ; preds = %12
%33 = tail call i64 (...) @fork1()
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = tail call i32 @parsecmd(i8* %13)
%37 = tail call i32 @runcmd(i32 %36)
br label %38
38: ; preds = %35, %32
%39 = tail call i32 (...) @wait()
br label %29
40: ; preds = %29, %9
%41 = tail call i32 (...) @free_history()
%42 = tail call i32 @exit(i32 0)
unreachable
}