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

/* Variables and functions */
int /*<<< orphan*/ execlp(char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  char buf[4096];
  pid_t pid;
  int status;
  printf("%% ");
  while (fgets(buf, 4096, stdin) != NULL) {
    if (buf[strlen(buf) - 1] == '\n') {
      buf[strlen(buf) - 1] = '\0';
    }
    if ((pid = fork()) < 0) {
      fprintf(stderr, "创建子进程出错!\n");
    } else if (pid == 0) {
      execlp(buf, buf, (char *)0);
      fprintf(stderr, "不能执行%s命令!\n", buf);
      exit(127);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      fprintf(stderr, "waitepid error!\n");
    }
    printf("%% ");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12d1 <main+0x151>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11fd <main+0x7d>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    1231 <main+0xb1>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1283 <main+0x103>
cmpq   $0x0,-0x1018(%rbp)
jne    127e <main+0xfe>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
jmp    1283 <main+0x103>
mov    -0x1018(%rbp),%rdi
lea    -0x101c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    12c0 <main+0x140>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xd39(%rip),%rdi
call   1040 <printf@plt>
jmp    119e <main+0x1e>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e44(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c5 <main+0x45>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2e0c(%rip),%r12
lea    0xc(%rsp),%r15
lea    0xe28(%rip),%r13
jmp    11fd <main+0x7d>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11be <main+0x3e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    121d <main+0x9d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbp
test   %rax,%rax
js     1230 <main+0xb0>
jne    1242 <main+0xc2>
jmp    126b <main+0xeb>
mov    (%r12),%edi
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
jns    11e0 <main+0x60>
mov    (%r12),%edi
lea    0xdd4(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11e0 <main+0x60>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    (%r12),%edi
lea    0xd94(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"\E5\88\9B\E5\BB\BA\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\87\BA\E9\94\99!\0A\00"
@.str.2 = [23 x i8] c"\E4\B8\8D\E8\83\BD\E6\89\A7\E8\A1\8C%s\E5\91\BD\E4\BB\A4!\0A\00"
@.str.3 = [17 x i8] c"waitepid error!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %51, %0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %7, i32 4096, i32 %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %53
11: ; preds = %6
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %20, %11
%27 = call i64 (...) @fork()
store i64 %27, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %44
32: ; preds = %26
%33 = load i64, i64* %3
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%38 = call i32 @execlp(i8* %36, i8* %37, i8* null)
%39 = load i32, i32* @stderr
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 127)
unreachable
43: ; preds = %32
br label %44
44: ; preds = %43, %29
%45 = load i64, i64* %3
%46 = call i64 @waitpid(i64 %45, i32* %4, i32 0)
store i64 %46, i64* %3
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %48, %44
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
53: ; preds = %6
%54 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"\E5\88\9B\E5\BB\BA\E5\AD\90\E8\BF\9B\E7\A8\8B\E5\87\BA\E9\94\99!\0A\00"
@.str.2 = [23 x i8] c"\E4\B8\8D\E8\83\BD\E6\89\A7\E8\A1\8C%s\E5\91\BD\E4\BB\A4!\0A\00"
@.str.3 = [17 x i8] c"waitepid error!\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %3, i32 4096, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %45, label %9
9: ; preds = %0, %40
%10 = call i32 @strlen(i8* %3)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %21
16: ; preds = %9
%17 = call i32 @strlen(i8* %3)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %16, %9
%22 = call i64 (...) @fork()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %34
27: ; preds = %21
%28 = icmp eq i64 %22, 0
br i1 %28, label %29, label %34
29: ; preds = %27
%30 = call i32 @execlp(i8* %3, i8* %3, i8* null)
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%33 = call i32 @exit(i32 127)
unreachable
34: ; preds = %27, %24
%35 = call i64 @waitpid(i64 %22, i32* %2, i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %34
%41 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @stdin
%43 = call i32* @fgets(i8* %3, i32 4096, i32 %42)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %9
45: ; preds = %40, %0
%46 = call i32 @exit(i32 0)
unreachable
}