#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 *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
main() {
  pid_t k;
  char buf[100];
  int status;
  int len;
  while (1) {
    fprintf(stdout, "[%d]$ ", getpid());
    fgets(buf, 100, stdin);
    len = strlen(buf);
    if (len == 1)
      continue;
    buf[len - 1] = '\0';
    k = fork();
    if (k == 0) {
      if (execlp(buf, buf, NULL) == -1)
        exit(1);
    } else {
      waitpid(k, &status, 0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x8c(%rbp),%edi
mov    %eax,%edx
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x1,-0x88(%rbp)
jne    11f4 <main+0x74>
jmp    1192 <main+0x12>
mov    -0x88(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x80(%rbp,%rax,1)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1243 <main+0xc3>
lea    -0x80(%rbp),%rdi
lea    -0x80(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
cmp    $0xffffffff,%eax
jne    123e <main+0xbe>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1255 <main+0xd5>
mov    -0x10(%rbp),%rdi
lea    -0x84(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1192 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    0x2e43(%rip),%r12
lea    0xe64(%rip),%r14
mov    0x2e45(%rip),%r13
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
jmp    11bd <main+0x3d>
nop
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    (%r12),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
je     11bd <main+0x3d>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    11b0 <main+0x30>
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
cmp    $0xffffffff,%eax
jne    11bd <main+0x3d>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [7 x i8] c"[%d]$ \00"
@stdin = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %0, %17, %37
%7 = load i32, i32* @stdout
%8 = call i32 (...) @getpid()
%9 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %10, i32 100, i32 %11)
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %6
br label %6
18: ; preds = %6
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i64 (...) @fork()
store i64 %23, i64* %2
%24 = load i64, i64* %2
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %18
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%29 = call i32 @execlp(i8* %27, i8* %28, i32* null)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
br label %37
34: ; preds = %18
%35 = load i64, i64* %2
%36 = call i32 @waitpid(i64 %35, i32* %4, i32 0)
br label %37
37: ; preds = %34, %33
br label %6
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [7 x i8] c"[%d]$ \00"
@stdin = global i32 0
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %26, %0
%6 = load i32, i32* @stdout
%7 = call i32 (...) @getpid()
%8 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %3, i32 100, i32 %9)
%11 = call i32 @strlen(i8* %3)
%12 = icmp eq i32 %11, 1
br i1 %12, label %26, label %13
13: ; preds = %5
%14 = add nsw i32 %11, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = call i32 @execlp(i8* %3, i8* %3, i32* null)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = call i32 @waitpid(i64 %17, i32* %2, i32 0)
br label %26
26: ; preds = %24, %19, %5
br label %5
}