#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 execlp(char *, char *, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  char buf[6];
  pid_t p;
  int status;
  while (fgets(buf, 6, stdin) != NULL) {
    if (buf[strlen(buf) - 1] == '\n')
      buf[strlen(buf) - 1] = '\0';
    if ((p = fork()) < 0)
      printf("fork error!\n");
    else if (p == 0) {
      int ret;
      ret = execlp(buf, buf, (char *)0);
      if (ret < 0)
        perror("execlp error:");
      printf("shouldn't execute here!\n");
    } else {
    }
    if (waitpid(p, &status, 0) < 0)
      perror("waitpid failed:");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xa(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     126f <main+0xff>
lea    -0xa(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0xa(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    11cf <main+0x5f>
lea    -0xa(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0xa(%rbp,%rax,1)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11f5 <main+0x85>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
jmp    1245 <main+0xd5>
cmpq   $0x0,-0x18(%rbp)
jne    123b <main+0xcb>
lea    -0xa(%rbp),%rdi
lea    -0xa(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    122a <main+0xba>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
jmp    1240 <main+0xd0>
jmp    1240 <main+0xd0>
jmp    1245 <main+0xd5>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    126a <main+0xfa>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
jmp    117f <main+0xf>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e63(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x12(%rsp),%r13
mov    %r13,%rdi
mov    $0x6,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     125b <main+0xeb>
lea    0xe8c(%rip),%r12
lea    0xc(%rsp),%r14
lea    0xe68(%rip),%r15
jmp    11ec <main+0x7c>
nopl   0x0(%rax,%rax,1)
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
js     124a <main+0xda>
mov    0x0(%rbp),%edx
mov    %r13,%rdi
mov    $0x6,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     125b <main+0xeb>
mov    %r13,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r13,1)
jne    120e <main+0x9e>
mov    %r13,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r13,1)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
mov    %r12,%rdi
test   %rax,%rax
js     11c0 <main+0x50>
jne    11c5 <main+0x55>
mov    %r13,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    %r15,%rdi
test   %eax,%eax
jns    11c0 <main+0x50>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
mov    %r15,%rdi
jmp    11c0 <main+0x50>
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
jmp    11d7 <main+0x67>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [13 x i8] c"fork error!\0A\00"
@.str.1 = [14 x i8] c"execlp error:\00"
@.str.2 = [25 x i8] c"shouldn't execute here!\0A\00"
@.str.3 = [16 x i8] c"waitpid failed:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [6 x i8]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %52, %0
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %7, i32 6, i32 %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %53
11: ; preds = %6
%12 = getelementptr inbounds [6 x i8], [6 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 [6 x i8], [6 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 [6 x i8], [6 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 [6 x i8], [6 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %20, %11
%27 = call i64 (...) @fork()
store i64 %27, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %46
31: ; preds = %26
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %31
%35 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%36 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%37 = call i32 @execlp(i8* %35, i8* %36, i8* null)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %45
44: ; preds = %31
br label %45
45: ; preds = %44, %42
br label %46
46: ; preds = %45, %29
%47 = load i64, i64* %3
%48 = call i64 @waitpid(i64 %47, i32* %4, i32 0)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
br label %6
53: ; preds = %6
ret i32 0
}

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