#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 *, int /*<<< orphan*/ *);
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) {
      execlp(buf, buf, NULL);
    } else {
      waitpid(k, &status, 0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x8c(%rbp),%edi
mov    %eax,%edx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e31(%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    11e4 <main+0x74>
jmp    1182 <main+0x12>
mov    -0x88(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x80(%rbp,%rax,1)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1220 <main+0xb0>
lea    -0x80(%rbp),%rdi
lea    -0x80(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
jmp    1232 <main+0xc2>
mov    -0x10(%rbp),%rdi
lea    -0x84(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1182 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    0x2e53(%rip),%r12
lea    0xe74(%rip),%r14
mov    0x2e55(%rip),%r13
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
jmp    11ad <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     11ad <main+0x3d>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11a0 <main+0x30>
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
jmp    11ad <main+0x3d>

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, %33
%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 %30
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)
br label %33
30: ; preds = %18
%31 = load i64, i64* %2
%32 = call i32 @waitpid(i64 %31, i32* %4, i32 0)
br label %33
33: ; preds = %30, %26
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 = %21, %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 %21, 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 %22
19: ; preds = %13
%20 = call i32 @execlp(i8* %3, i8* %3, i32* null)
br label %21
21: ; preds = %19, %22, %5
br label %5
22: ; preds = %13
%23 = call i32 @waitpid(i64 %17, i32* %2, i32 0)
br label %21
}