#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*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  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) {
      printf("fork error\n");
      exit(1);
    } else if (pid == 0) {
      execlp(buf, buf, (char *)0);
      printf("couldn't execute: %s", buf);
      exit(127);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      printf("waitpid error");
    }
    printf("%% ");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12af <main+0x13f>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11ef <main+0x7f>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    121f <main+0xaf>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x1018(%rbp)
jne    1263 <main+0xf3>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
lea    -0x1010(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
jmp    1268 <main+0xf8>
mov    -0x1018(%rbp),%rdi
lea    -0x101c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    129c <main+0x12c>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1190 <main+0x20>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1010,%rsp
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e43(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11c7 <main+0x47>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xc(%rsp),%r14
lea    0xe46(%rip),%r15
lea    0xe26(%rip),%r12
jmp    1200 <main+0x80>
nopl   0x0(%rax)
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11c0 <main+0x40>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1220 <main+0xa0>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     124c <main+0xcc>
je     1262 <main+0xe2>
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
jns    11e0 <main+0x60>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e0 <main+0x60>
lea    0xdd4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
lea    0xd89(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.1 = [12 x i8] c"fork error\0A\00"
@.str.2 = [21 x i8] c"couldn't execute: %s\00"
@.str.3 = [14 x i8] c"waitpid error\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 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %49, %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 %51
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 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = load i64, i64* %3
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %42
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 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 127)
unreachable
42: ; preds = %32
br label %43
43: ; preds = %42
%44 = load i64, i64* %3
%45 = call i64 @waitpid(i64 %44, i32* %4, i32 0)
store i64 %45, i64* %3
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
51: ; preds = %6
%52 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.2 = [21 x i8] c"couldn't execute: %s\00"
@.str.3 = [14 x i8] c"waitpid error\00"
@str = [11 x i8] c"fork error\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 (i8*, ...) @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 %43, label %9
9: ; preds = %0, %38
%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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = icmp eq i64 %22, 0
br i1 %28, label %29, label %33
29: ; preds = %27
%30 = call i32 @execlp(i8* %3, i8* %3, i8* null)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%32 = call i32 @exit(i32 127)
unreachable
33: ; preds = %27
%34 = call i64 @waitpid(i64 %22, i32* %2, i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @stdin
%41 = call i32* @fgets(i8* %3, i32 4096, i32 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %9
43: ; preds = %38, %0
%44 = call i32 @exit(i32 0)
unreachable
}