#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;
size_t strlen(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  char buf[80] = {0};
  printf("-> ");
  while (fgets(buf, 80, stdin) != NULL) {
    pid_t pid = fork();
    if (pid < 0) {
      printf("failed to spawn a new process\n");
      continue;
    } else if (pid == 0) {
      size_t len = strlen(buf);
      if (buf[len - 1] == '\n') {
        buf[len - 1] = '\0';
      }
      execlp(buf, buf, (char *)0);
      printf("failed to exec a given command\n");
      return 1;
    }
    int status = 0;
    if ((pid = waitpid(pid, &status, 0)) < 0) {
      printf("waitpid error!");
      return 1;
    }
    printf("-> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     129c <main+0x12c>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jge    11e6 <main+0x76>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
jmp    119e <main+0x2e>
cmpq   $0x0,-0x68(%rbp)
jne    124a <main+0xda>
lea    -0x60(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
sub    $0x1,%rax
movsbl -0x60(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    1221 <main+0xb1>
mov    -0x70(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <main+0x133>
jmp    124f <main+0xdf>
movl   $0x0,-0x74(%rbp)
mov    -0x68(%rbp),%rdi
lea    -0x74(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jge    128b <main+0x11b>
lea    0xdc9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <main+0x133>
lea    0xd6e(%rip),%rdi
call   1040 <printf@plt>
jmp    119e <main+0x2e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x60,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e3c(%rip),%r13
mov    0x0(%r13),%edx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1230 <main+0xc0>
lea    0xe67(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
lea    0xe24(%rip),%r12
jmp    11fe <main+0x8e>
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1230 <main+0xc0>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e0 <main+0x70>
je     1234 <main+0xc4>
movl   $0x0,0xc(%rsp)
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
js     1265 <main+0xf5>
mov    %r12,%rdi
call   1050 <printf@plt>
jmp    11e8 <main+0x78>
xor    %eax,%eax
jmp    1276 <main+0x106>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1248 <main+0xd8>
movb   $0x0,0xf(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
jmp    1271 <main+0x101>
lea    0xd98(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"-> \00"
@stdin = global i32 0
@.str.1 = [31 x i8] c"failed to spawn a new process\0A\00"
@.str.2 = [32 x i8] c"failed to exec a given command\0A\00"
@.str.3 = [15 x i8] c"waitpid error!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [80 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [80 x i8]* %2 to i8*
%7 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %47, %17, %0
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %9, i32 80, i32 %10)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %49
13: ; preds = %8
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %8
19: ; preds = %13
%20 = load i64, i64* %3
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %4
%25 = load i64, i64* %4
%26 = sub i64 %25, 1
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i64, i64* %4
%33 = sub i64 %32, 1
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %31, %22
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%38 = call i32 @execlp(i8* %36, i8* %37, i8* null)
%39 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %50
40: ; preds = %19
br label %41
41: ; preds = %40
store i32 0, i32* %5
%42 = load i64, i64* %3
%43 = call i64 @waitpid(i64 %42, i32* %5, i32 0)
store i64 %43, i64* %3
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %50
47: ; preds = %41
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %8
49: ; preds = %8
store i32 0, i32* %1
br label %50
50: ; preds = %49, %45, %35
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [4 x i8] c"-> \00"
@stdin = global i32 0
@.str.3 = [15 x i8] c"waitpid error!\00"
@str = [31 x i8] c"failed to exec a given command\00"
@str.4 = [30 x i8] c"failed to spawn a new process\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdin
%6 = call i32* @fgets(i8* %3, i32 80, i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %38, label %8
8: ; preds = %0
%9 = bitcast i32* %2 to i8*
br label %10
10: ; preds = %8, %34
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0))
br label %34
15: ; preds = %10
%16 = icmp eq i64 %11, 0
br i1 %16, label %17, label %27
17: ; preds = %15
%18 = call i64 @strlen(i8* %3)
%19 = add i64 %18, -1
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %17
store i8 0, i8* %20
br label %24
24: ; preds = %17, %23
%25 = call i32 @execlp(i8* %3, i8* %3, i8* null)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %38
27: ; preds = %15
store i32 0, i32* %2
%28 = call i64 @waitpid(i64 %11, i32* %2, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %38
32: ; preds = %27
%33 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %13
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %3, i32 80, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %10
38: ; preds = %34, %30, %0, %24
%39 = phi i32 [ 1, %24 ], [ 0, %0 ], [ 1, %30 ], [ 0, %34 ]
ret i32 %39
}