#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*/ execlp(char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  char buffer[256] = {0};
  int len = 0;
  int pid = 0;
  int status = 0;
  printf("%% ");
  while (fgets(buffer, 256, stdin) != NULL) {
    if ((len = strlen(buffer)) == 0)
      continue;
    buffer[len - 1] = '\0';
    if ((pid = fork()) == 0) {
      execlp(buffer, buffer, (char *)0);
      exit(127);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      fputs("wait pid error", stderr);
    }
    printf("%% ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movl   $0x0,-0x114(%rbp)
movl   $0x0,-0x118(%rbp)
movl   $0x0,-0x11c(%rbp)
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12b6 <main+0x126>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jne    1223 <main+0x93>
jmp    11df <main+0x4f>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x118(%rbp)
cmp    $0x0,%eax
jne    126d <main+0xdd>
lea    -0x110(%rbp),%rdi
lea    -0x110(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execlp@plt>
mov    $0x7f,%edi
call   1090 <exit@plt>
mov    -0x118(%rbp),%edi
lea    -0x11c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,-0x118(%rbp)
cmp    $0x0,%eax
jge    12a5 <main+0x115>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
lea    0xd64(%rip),%rdi
call   1050 <fputs@plt>
lea    0xd54(%rip),%rdi
call   1040 <printf@plt>
jmp    11df <main+0x4f>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0xdf2(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dce(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12b9 <main+0x139>
lea    0xc(%rsp),%r14
mov    0x2d93(%rip),%rbp
lea    0xdb8(%rip),%r15
lea    0xdad(%rip),%r12
jmp    127e <main+0xfe>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1040 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     12b9 <main+0x139>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1268 <main+0xe8>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     12cd <main+0x14d>
mov    %eax,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %eax,%eax
jns    1260 <main+0xe0>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1050 <fputs@plt>
jmp    1260 <main+0xe0>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.1 = [15 x i8] c"wait pid error\00"
@stderr = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [256 x i8]* %2 to i8*
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %37, %17, %0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %9, i32 256, i32 %10)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %39
13: ; preds = %8
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %8
18: ; preds = %13
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 (...) @fork()
store i32 %23, i32* %4
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%28 = call i32 @execlp(i8* %26, i8* %27, i8* null)
%29 = call i32 @exit(i32 127)
unreachable
30: ; preds = %18
%31 = load i32, i32* %4
%32 = call i32 @waitpid(i32 %31, i32* %5, i32 0)
store i32 %32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %30
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %8
39: ; preds = %8
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.1 = [15 x i8] c"wait pid error\00"
@stderr = global i32 0
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%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 256, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %33, label %9
9: ; preds = %0, %29
%10 = call i32 @strlen(i8* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %9
%13 = add nsw i32 %10, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 (...) @fork()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @execlp(i8* %3, i8* %3, i8* null)
%20 = call i32 @exit(i32 127)
unreachable
21: ; preds = %12
%22 = call i32 @waitpid(i32 %16, i32* %2, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %21
%28 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %9
%30 = load i32, i32* @stdin
%31 = call i32* @fgets(i8* %3, i32 256, i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %9
33: ; preds = %29, %0
ret i32 0
}