#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*/ buf;

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork1();
scalar_t__ getcmd(char *, int);
int open(char *, int);
int /*<<< orphan*/ parsecmd(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ runcmd(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ wait();
int main(void) {
  static char buf[100];
  int fd;
  while ((fd = open("console", 0x002)) >= 0) {
    if (fd >= 3) {
      close(fd);
      break;
    }
  }
  chdir("/doc/");
  while (getcmd(buf, sizeof(buf)) >= 0) {
    if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
      buf[strlen(buf) - 1] = 0;
      if (chdir(buf + 3) < 0)
        printf("cannot cd %s\n", buf + 3);
      continue;
    }
    if (fork1() == 0)
      runcmd(parsecmd(buf));
    wait();
  }
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x2,%esi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     11f8 <main+0x48>
cmpl   $0x3,-0x8(%rbp)
jl     11f3 <main+0x43>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    11f8 <main+0x48>
jmp    11bf <main+0xf>
lea    0xe09(%rip),%rdi
call   1060 <chdir@plt>
lea    0x2e85(%rip),%rdi
mov    $0x64,%esi
call   1030 <getcmd@plt>
cmp    $0x0,%rax
jl     12d2 <main+0x122>
movsbl 0x2e6a(%rip),%eax
cmp    $0x63,%eax
jne    12a2 <main+0xf2>
movsbl 0x2e5b(%rip),%eax
cmp    $0x64,%eax
jne    12a2 <main+0xf2>
movsbl 0x2e4c(%rip),%eax
cmp    $0x20,%eax
jne    12a2 <main+0xf2>
lea    0x2e3a(%rip),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2e28(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e1d(%rip),%rdi
add    $0x3,%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    129d <main+0xed>
lea    0xd81(%rip),%rdi
lea    0x2dfc(%rip),%rsi
add    $0x3,%rsi
call   1070 <printf@plt>
jmp    1204 <main+0x54>
mov    $0x0,%al
call   1090 <fork1@plt>
cmp    $0x0,%rax
jne    12c6 <main+0x116>
lea    0x2dd6(%rip),%rdi
call   10a0 <parsecmd@plt>
mov    %eax,%edi
call   1050 <runcmd@plt>
mov    $0x0,%al
call   10d0 <wait@plt>
jmp    1204 <main+0x54>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe44(%rip),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x2,%esi
call   10b0 <open@plt>
test   %eax,%eax
js     11dd <main+0x2d>
cmp    $0x3,%eax
jb     11c0 <main+0x10>
mov    %eax,%edi
call   1080 <close@plt>
lea    0xe24(%rip),%rdi
call   1060 <chdir@plt>
lea    0x2ea0(%rip),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1030 <getcmd@plt>
test   %rax,%rax
js     129a <main+0xea>
lea    0x2e86(%rip),%r14
lea    0xdfa(%rip),%r15
jmp    1245 <main+0x95>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork1@plt>
test   %rax,%rax
je     1289 <main+0xd9>
xor    %eax,%eax
call   10d0 <wait@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1030 <getcmd@plt>
test   %rax,%rax
js     129a <main+0xea>
cmpb   $0x63,0x2e44(%rip)
jne    1220 <main+0x70>
cmpb   $0x64,0x2e3c(%rip)
jne    1220 <main+0x70>
cmpb   $0x20,0x2e34(%rip)
jne    1220 <main+0x70>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
jns    1233 <main+0x83>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <printf@plt>
jmp    1233 <main+0x83>
mov    %rbx,%rdi
call   10a0 <parsecmd@plt>
mov    %eax,%edi
call   1050 <runcmd@plt>
jmp    122c <main+0x7c>
mov    $0x1,%edi
call   10c0 <exit@plt>

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

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