#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*/ printf(char *, ...);
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) {
      printf("fork error");
    } else if (pid == 0) {
      execlp(buf, buf, (char *)0);
      printf("couldn't execute: %s\n", buf);
      return -1;
    }
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      printf("waitpid error");
    }
    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    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1281 <main+0x121>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    11ef <main+0x8f>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <main+0xda>
cmpq   $0x0,-0x1018(%rbp)
jne    1235 <main+0xd5>
lea    -0x1010(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execlp@plt>
lea    -0x1010(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <main+0x128>
jmp    123a <main+0xda>
mov    -0x1018(%rbp),%rdi
lea    -0x101c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jge    126e <main+0x10e>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1187 <main+0x27>
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    0xe88(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e60(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1264 <main+0x104>
lea    0xc(%rsp),%r15
lea    0xe73(%rip),%r12
lea    0xe47(%rip),%r13
jmp    11e5 <main+0x85>
nopl   0x0(%rax,%rax,1)
xor    %ebp,%ebp
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1264 <main+0x104>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbp
test   %rax,%rax
js     1210 <main+0xb0>
jne    121e <main+0xbe>
jmp    123c <main+0xdc>
nopw   0x0(%rax,%rax,1)
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    11c0 <main+0x60>
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c0 <main+0x60>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <execlp@plt>
lea    0xdba(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [22 x i8] c"couldn't execute: %s\0A\00"
@.str.3 = [14 x i8] c"waitpid error\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 (i8*, ...) @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 %25
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %35
25: ; preds = %15
%26 = load i64, i64* %7
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%31 = call i32 @execlp(i8* %29, i8* %30, i8* null)
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %3
br label %44
34: ; preds = %25
br label %35
35: ; preds = %34, %23
%36 = load i64, i64* %7
%37 = call i64 @waitpid(i64 %36, i32* %8, i32 0)
store i64 %37, i64* %7
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = call i32 (i8*, ...) @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, %28
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [4 x i8] c"%% \00"
@stdin = global i32 0
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [22 x i8] c"couldn't execute: %s\0A\00"
@.str.3 = [14 x i8] c"waitpid error\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 (i8*, ...) @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 %35, label %11
11: ; preds = %2, %30
%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 %18, label %20
18: ; preds = %11
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %25
20: ; preds = %11
%21 = icmp eq i64 %16, 0
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = call i32 @execlp(i8* %5, i8* %5, i8* null)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %35
25: ; preds = %20, %18
%26 = call i64 @waitpid(i64 %16, i32* %4, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @stdin
%33 = call i32* @fgets(i8* %5, i32 4096, i32 %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %11
35: ; preds = %30, %2, %22
%36 = phi i32 [ -1, %22 ], [ 0, %2 ], [ 0, %30 ]
ret i32 %36
}