#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 uint64_t;

/* Variables and functions */
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execve(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void main() {
  if (fork())
    _exit(0);
  close(0);
  close(1);
  close(2);
  open("/dev/tty1", 0);
  open("/dev/tty1", 0);
  open("/dev/tty1", 0);
  char buf[100];
  while (1) {
    write(0, "Shell # ", 8);
    read(0, buf, 100);
    if (*buf == '/') {
      if (!fork()) {
        uint64_t i;
        for (i = 0; i < 100; ++i)
          if (buf[i] == '\n') {
            buf[i] = '\0';
            break;
          }
        execve(buf, 0, 0);
        for (;;)
          ;
      }
      uint64_t stat;
      wait(&stat);
    } else
      write(0, buf, 100);
  }
  for (;;)
    ;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     11a3 <main+0x23>
xor    %edi,%edi
call   1030 <_exit@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xe3b(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
lea    0xe2d(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
lea    0xe1f(%rip),%rdi
xor    %esi,%esi
call   1080 <open@plt>
xor    %edi,%edi
lea    0xe19(%rip),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1060 <read@plt>
movsbl -0x70(%rbp),%eax
cmp    $0x2f,%eax
jne    128d <main+0x10d>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    127f <main+0xff>
movl   $0x0,-0x74(%rbp)
cmpl   $0x64,-0x74(%rbp)
jge    126d <main+0xed>
movslq -0x74(%rbp),%rax
movsbl -0x70(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    125a <main+0xda>
movslq -0x74(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
jmp    126d <main+0xed>
jmp    125f <main+0xdf>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1230 <main+0xb0>
lea    -0x70(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <execve@plt>
jmp    127a <main+0xfa>
lea    -0x78(%rbp),%rdi
call   1090 <wait@plt>
jmp    129d <main+0x11d>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1040 <write@plt>
jmp    11e8 <main+0x68>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
xor    %edi,%edi
test   %rax,%rax
jne    12a2 <main+0x122>
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xe42(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <open@plt>
lea    0xe27(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
jmp    11ff <main+0x7f>
nop
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
cmpb   $0x2f,0x10(%rsp)
jne    11f0 <main+0x70>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     123a <main+0xba>
mov    %r15,%rdi
call   1090 <wait@plt>
jmp    11ff <main+0x7f>
xor    %eax,%eax
nopl   0x0(%rax)
cmpb   $0xa,0x11(%rsp,%rax,1)
je     1272 <main+0xf2>
cmpb   $0xa,0x12(%rsp,%rax,1)
je     1278 <main+0xf8>
cmpb   $0xa,0x13(%rsp,%rax,1)
je     127e <main+0xfe>
cmpb   $0xa,0x14(%rsp,%rax,1)
je     1284 <main+0x104>
cmp    $0x5f,%rax
je     1292 <main+0x112>
lea    0x5(%rax),%rcx
cmpb   $0xa,0x15(%rsp,%rax,1)
mov    %rcx,%rax
jne    1240 <main+0xc0>
jmp    128b <main+0x10b>
add    $0x1,%rax
jmp    1288 <main+0x108>
add    $0x2,%rax
jmp    1288 <main+0x108>
add    $0x3,%rax
jmp    1288 <main+0x108>
add    $0x4,%rax
mov    %rax,%rcx
mov    %ecx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <execve@plt>
jmp    12a0 <main+0x120>
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/tty1\00"
@.str.1 = [9 x i8] c"Shell # \00"
define void @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = call i64 (...) @fork()
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @_exit(i32 0)
unreachable
8: ; preds = %0
%9 = call i32 @close(i32 0)
%10 = call i32 @close(i32 1)
%11 = call i32 @close(i32 2)
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
br label %15
15: ; preds = %8, %54
%16 = call i32 @write(i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%18 = call i32 @read(i32 0, i8* %17, i32 100)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br i1 %22, label %23, label %51
23: ; preds = %15
%24 = call i64 (...) @fork()
%25 = icmp ne i64 %24, 0
br i1 %25, label %49, label %26
26: ; preds = %23
store i32 0, i32* %2
br label %27
27: ; preds = %42, %26
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 100
br i1 %29, label %30, label %45
30: ; preds = %27
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %39
store i8 0, i8* %40
br label %45
41: ; preds = %30
br label %42
42: ; preds = %41
%43 = load i32, i32* %2
%44 = add nsw i32 %43, 1
store i32 %44, i32* %2
br label %27
45: ; preds = %37, %27
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%47 = call i32 @execve(i8* %46, i32 0, i32 0)
br label %48
48: ; preds = %48, %45
br label %48
49: ; preds = %23
%50 = call i32 @wait(i32* %3)
br label %54
51: ; preds = %15
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%53 = call i32 @write(i32 0, i8* %52, i32 100)
br label %54
54: ; preds = %51, %49
br label %15
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/tty1\00"
@.str.1 = [9 x i8] c"Shell # \00"
define void @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @_exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @close(i32 2)
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%12 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%15 = bitcast i32* %2 to i8*
br label %16
16: ; preds = %63, %7
%17 = call i32 @write(i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%18 = call i32 @read(i32 0, i8* %14, i32 100)
%19 = load i8, i8* %14
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %61
21: ; preds = %16
%22 = call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %59
24: ; preds = %52, %44, %39, %34, %28
%25 = phi i64 [ %30, %28 ], [ %35, %34 ], [ %40, %39 ], [ %45, %44 ], [ %50, %52 ]
%26 = and i64 %25, 4294967295
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %26
store i8 0, i8* %27
br label %56
28: ; preds = %21, %52
%29 = phi i64 [ %50, %52 ], [ 0, %21 ]
%30 = add nuw nsw i64 %29, 1
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
br i1 %33, label %24, label %34
34: ; preds = %28
%35 = add nuw nsw i64 %29, 2
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 10
br i1 %38, label %24, label %39
39: ; preds = %34
%40 = add nuw nsw i64 %29, 3
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 10
br i1 %43, label %24, label %44
44: ; preds = %39
%45 = add nuw nsw i64 %29, 4
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 10
br i1 %48, label %24, label %49
49: ; preds = %44
%50 = add nuw nsw i64 %29, 5
%51 = icmp eq i64 %50, 100
br i1 %51, label %56, label %52
52: ; preds = %49
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %50
%54 = load i8, i8* %53
%55 = icmp eq i8 %54, 10
br i1 %55, label %24, label %28
56: ; preds = %49, %24
%57 = call i32 @execve(i8* %14, i32 0, i32 0)
br label %58
58: ; preds = %58, %56
br label %58
59: ; preds = %21
%60 = call i32 @wait(i32* %2)
br label %63
61: ; preds = %16
%62 = call i32 @write(i32 0, i8* %14, i32 100)
br label %63
63: ; preds = %61, %59
br label %16
}