#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*/ *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(int argc, char **argv) {
  char buf[4096];
  pid_t pid;
  int status;
  printf("%% ");
  while (fgets(buf, 4096, stdin) != NULL) {
    buf[strlen(buf) - 1] = '\0';
    if ((pid = fork()) < 0) {
      fprintf(stderr, "fork_error\n");
      return 0;
    } else if (pid == 0) {
      execlp(buf, buf, (char *)0);
    }
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      fprintf(stderr, "waitpid error\n");
      return 0;
    }
    printf("%% ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     128d <main+0x11d>
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    120b <main+0x9b>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x124>
cmpq   $0x0,-0x1018(%rbp)
jne    1230 <main+0xc0>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
jmp    1235 <main+0xc5>
mov    -0x1018(%rbp),%rdi
lea    -0x101c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    127c <main+0x10c>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x124>
lea    0xd7d(%rip),%rdi
call   1040 <printf@plt>
jmp    1195 <main+0x25>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e54(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1247 <main+0xd7>
lea    0xe4b(%rip),%r14
lea    0xc(%rsp),%r15
lea    0xe3b(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r13,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1236 <main+0xc6>
mov    %rax,%rbx
jne    11fe <main+0x8e>
mov    %r13,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
js     122f <main+0xbf>
mov    %r12,%rdi
call   1040 <printf@plt>
mov    0x0(%rbp),%edx
mov    %r13,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
jmp    1247 <main+0xd7>
lea    0xdda(%rip),%r14
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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 = [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 [4096 x i8]
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %41, %2
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i32* @fgets(i8* %11, i32 4096, i32 %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %43
15: ; preds = %10
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %44
26: ; preds = %15
%27 = load i64, i64* %7
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%32 = call i32 @execlp(i8* %30, i8* %31, i8* null)
br label %33
33: ; preds = %29, %26
br label %34
34: ; preds = %33
%35 = load i64, i64* %7
%36 = call i64 @waitpid(i64 %35, i32* %8, i32 0)
store i64 %36, i64* %7
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %44
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %10
43: ; preds = %10
store i32 0, i32* %3
br label %44
44: ; preds = %43, %38, %23
%45 = load i32, i32* %3
ret i32 %45
}

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