#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*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char buf[256];
  pid_t pid;
  int status;
  fprintf(stdout, "%% ");
  while (NULL != fgets(buf, 256, stdin)) {
    if ('\n' == buf[strlen(buf) - 1])
      buf[strlen(buf) - 1] = 0;
    if ((pid = fork()) < 0) {
      fprintf(stderr, "fork error\n");
      exit(1);
    } else if (0 == pid) {
      execlp(buf, buf, (char *)0);
      fprintf(stderr, "Couldn't execute: %s\n", buf);
      exit(127);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0)
      fprintf(stdout, "waitpid error\n");
    fprintf(stdout, "%% ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12ec <main+0x17c>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%ecx
ff
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1207 <main+0x97>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jge    1240 <main+0xd0>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
cmp    -0x118(%rbp),%rax
jne    128e <main+0x11e>
lea    -0x110(%rbp),%rdi
lea    -0x110(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
jmp    1293 <main+0x123>
mov    -0x118(%rbp),%rdi
lea    -0x11c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jge    12d0 <main+0x160>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11a0 <main+0x30>
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
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe6d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e47(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     1253 <main+0xe3>
lea    0xc(%rsp),%r14
lea    0xe5b(%rip),%r15
lea    0xe2e(%rip),%r12
jmp    1203 <main+0x93>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     1253 <main+0xe3>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1223 <main+0xb3>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1267 <main+0xf7>
je     1288 <main+0x118>
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    11e0 <main+0x70>
mov    0x0(%r13),%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11e0 <main+0x70>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd66(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"fork error\0A\00"
@.str.2 = [22 x i8] c"Couldn't execute: %s\0A\00"
@.str.3 = [15 x i8] c"waitpid error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @stdout
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %57, %2
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @stdin
%14 = call i32* @fgets(i8* %12, i32 256, i32 %13)
%15 = icmp ne i32* null, %14
br i1 %15, label %16, label %60
16: ; preds = %11
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 10, %23
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %25, %16
%32 = call i64 (...) @fork()
store i64 %32, i64* %7
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = load i64, i64* %7
%40 = icmp eq i64 0, %39
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%44 = call i32 @execlp(i8* %42, i8* %43, i8* null)
%45 = load i32, i32* @stderr
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 127)
unreachable
49: ; preds = %38
br label %50
50: ; preds = %49
%51 = load i64, i64* %7
%52 = call i64 @waitpid(i64 %51, i32* %8, i32 0)
store i64 %52, i64* %7
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* @stdout
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %54, %50
%58 = load i32, i32* @stdout
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %11
60: ; preds = %11
ret i32 0
}

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