#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*/ errno;
scalar_t__ execlp(char *, char *, int /*<<< orphan*/ *);
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;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int waitpid(int, 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)) {
      fprintf(stderr, "fork error.\n");
      exit(1);
    } else if (pid == 0) {
      if (execlp(buf, buf, NULL) < 0) {
        fprintf(stderr, "Couldn't execute: %s.\nexeclp faild: %s", buf,
                strerror(errno));
        exit(1);
      }
      exit(127);
    } else {
      if ((pid = waitpid(pid, &status, 0)) < 0) {
        fprintf(stderr, "waitpid error.\n");
        exit(1);
      }
    }
    printf("%% ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1341 <main+0x1b1>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    120d <main+0x7d>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1014(%rbp)
test   $0x1,%al
jne    1236 <main+0xa6>
jmp    1257 <main+0xc7>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1014(%rbp)
jne    12e2 <main+0x152>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
cmp    $0x0,%rax
jge    12d8 <main+0x148>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1024(%rbp),%edi
mov    -0x1020(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
mov    -0x1014(%rbp),%edi
lea    -0x1018(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jge    1326 <main+0x196>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    132b <main+0x19b>
jmp    1330 <main+0x1a0>
lea    0xcc9(%rip),%rdi
call   1040 <printf@plt>
jmp    11ae <main+0x1e>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1000,%rsp
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11bd <main+0x3d>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11dd <main+0x5d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    120a <main+0x8a>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %rsp,%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
test   %rax,%rax
jns    1253 <main+0xd3>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebx
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd7(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"fork error.\0A\00"
@.str.2 = [39 x i8] c"Couldn't execute: %s.\0Aexeclp faild: %s\00"
@errno = global i32 0
@.str.3 = [16 x i8] c"waitpid error.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i32
%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 = %61, %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 %63
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()
%28 = icmp slt i64 %27, 0
%29 = zext i1 %28 to i32
store i32 %29, i32* %3
br i1 %28, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %51
37: ; preds = %34
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%40 = call i64 @execlp(i8* %38, i8* %39, i32* null)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %37
%50 = call i32 @exit(i32 127)
unreachable
51: ; preds = %34
%52 = load i32, i32* %3
%53 = call i32 @waitpid(i32 %52, i32* %4, i32 0)
store i32 %53, i32* %3
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %51
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
%62 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %6
63: ; preds = %6
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"fork error.\0A\00"
@.str.2 = [39 x i8] c"Couldn't execute: %s.\0Aexeclp faild: %s\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = call i32* @fgets(i8* %2, i32 4096, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %37, label %7
7: ; preds = %0
%8 = call i32 @strlen(i8* %2)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %19
14: ; preds = %7
%15 = call i32 @strlen(i8* %2)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %14, %7
%20 = call i64 (...) @fork()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = call i64 @execlp(i8* %2, i8* %2, i32* null)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = call i32 @exit(i32 127)
unreachable
37: ; preds = %0
ret i32 0
}