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

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ slice(char **, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
int vfork();
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  char command[2048];
  pid_t forkStatus;
  char *args[2048];
  int tem_e;
  while (1) {
    tem_e = 0;
    printf("> ");
    scanf(" %[^\n]", command);
    if (strcmp(command, "q") == 0) {
      break;
    } else {
      char *end = strrchr(command, '&');
      if ('&' == command[strlen(command) - 1]) {
        tem_e = 1;
        *end = '\0';
      }
      forkStatus = vfork();
      if (forkStatus == 0) {
        slice(args, command);
        execvp(command, args);
      } else if (forkStatus != -1) {
        if (!tem_e) {
          wait(NULL);
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4830,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x4824(%rbp)
lea    0xe4d(%rip),%rdi
call   1050 <printf@plt>
lea    -0x810(%rbp),%rsi
lea    0xe3d(%rip),%rdi
call   1080 <scanf@plt>
lea    -0x810(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11ed <main+0x5d>
jmp    12b9 <main+0x129>
lea    -0x810(%rbp),%rdi
mov    $0x26,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x4830(%rbp)
lea    -0x810(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x810(%rbp,%rax,1),%ecx
ff
mov    $0x26,%eax
cmp    %ecx,%eax
jne    123f <main+0xaf>
movl   $0x1,-0x4824(%rbp)
mov    -0x4830(%rbp),%rax
movb   $0x0,(%rax)
call   1090 <vfork@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0x0,-0x814(%rbp)
jne    1282 <main+0xf2>
lea    -0x4820(%rbp),%rdi
lea    -0x810(%rbp),%rsi
call   1030 <slice@plt>
lea    -0x810(%rbp),%rdi
lea    -0x4820(%rbp),%rsi
call   10a0 <execvp@plt>
jmp    12af <main+0x11f>
cmpl   $0xffffffff,-0x814(%rbp)
je     12aa <main+0x11a>
cmpl   $0x0,-0x4824(%rbp)
jne    12a5 <main+0x115>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    12aa <main+0x11a>
jmp    12af <main+0x11f>
jmp    12b4 <main+0x124>
jmp    11a2 <main+0x12>
xor    %eax,%eax
add    $0x4830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4818,%rsp
lea    0xe58(%rip),%rdi
call   1050 <printf@plt>
lea    0xe4f(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <scanf@plt>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1291 <main+0x101>
lea    0xe20(%rip),%rbp
lea    0xe1c(%rip),%r13
lea    0xe1c(%rip),%r12
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1247 <main+0xb7>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%r14
lea    0x810(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <slice@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rbp
call   10a0 <execvp@plt>
mov    %rbp,%rdi
call   1050 <printf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1291 <main+0x101>
mov    %rbx,%rdi
mov    $0x26,%esi
call   1060 <strrchr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movzbl -0x1(%rax,%rbx,1),%r15d
cmp    $0x26,%r15b
jne    1271 <main+0xe1>
movb   $0x0,(%r14)
call   1090 <vfork@plt>
test   %eax,%eax
je     1200 <main+0x70>
cmp    $0xffffffff,%eax
je     1224 <main+0x94>
cmp    $0x26,%r15b
je     1224 <main+0x94>
mov    0x8(%rsp),%rdi
call   10b0 <wait@plt>
jmp    1224 <main+0x94>
xor    %eax,%eax
add    $0x4818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"> \00"
@.str.1 = [7 x i8] c" %[^\0A]\00"
@.str.2 = [2 x i8] c"q\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2048 x i8]
%3 = alloca i32
%4 = alloca [2048 x i8*]
%5 = alloca i32
%6 = alloca i8*
store i32 0, i32* %1
br label %7
7: ; preds = %0, %50
store i32 0, i32* %5
%8 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%10 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %7
br label %51
15: ; preds = %7
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%17 = call i8* @strrchr(i8* %16, i8 signext 38)
store i8* %17, i8** %6
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 38, %24
br i1 %25, label %26, label %28
26: ; preds = %15
store i32 1, i32* %5
%27 = load i8*, i8** %6
store i8 0, i8* %27
br label %28
28: ; preds = %26, %15
%29 = call i32 @vfork()
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = getelementptr inbounds [2048 x i8*], [2048 x i8*]* %4, i64 0, i64 0
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%35 = call i32 @slice(i8** %33, i8* %34)
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds [2048 x i8*], [2048 x i8*]* %4, i64 0, i64 0
%38 = call i32 @execvp(i8* %36, i8** %37)
br label %49
39: ; preds = %28
%40 = load i32, i32* %3
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @wait(i32* null)
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47, %39
br label %49
49: ; preds = %48, %32
br label %50
50: ; preds = %49
br label %7
51: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"> \00"
@.str.1 = [7 x i8] c" %[^\0A]\00"
@.str.2 = [2 x i8] c"q\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = alloca [2048 x i8*]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%4 = bitcast [2048 x i8*]* %2 to i8*
%5 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%7 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %36, label %9
9: ; preds = %0
%10 = getelementptr inbounds [2048 x i8*], [2048 x i8*]* %2, i64 0, i64 0
br label %11
11: ; preds = %9, %31
%12 = call i8* @strrchr(i8* %3, i8 signext 38)
%13 = call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 38
br i1 %18, label %19, label %20
19: ; preds = %11
store i8 0, i8* %12
br label %20
20: ; preds = %19, %11
%21 = call i32 @vfork()
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @slice(i8** %10, i8* %3)
%25 = call i32 @execvp(i8* %3, i8** %10)
br label %31
26: ; preds = %20
%27 = icmp eq i32 %21, -1
%28 = select i1 %27, i1 true, i1 %18
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @wait(i32* null)
br label %31
31: ; preds = %26, %29, %23
%32 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%34 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %11
36: ; preds = %31, %0
ret i32 0
}