#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*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ getchar();
int getpid();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t k;
  int status;
  while (1) {
    fprintf(stdout, "[%d]$ ", getpid());
    getchar();
    k = fork();
    if (k == 0) {
      fprintf(stdout, "I'm the child: %d\n", getpid());
      execl("/bin/ls", "/bin/ls", NULL);
    } else {
      fprintf(stdout, "I'm the parent: %d\n", getpid());
      waitpid(k, &status, 0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x8c>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execl@plt>
jmp    121f <main+0xbf>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
jmp    116f <main+0xf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6e(%rip),%r12
lea    0xe87(%rip),%rbx
lea    0xe9a(%rip),%r15
lea    0x4(%rsp),%r13
jmp    11ab <main+0x4b>
nopw   0x0(%rax,%rax,1)
lea    0xe8b(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    (%r12),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbp
mov    (%r12),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r14d,%edi
test   %rbp,%rbp
jne    1190 <main+0x30>
lea    0xe1a(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <execl@plt>
jmp    11ab <main+0x4b>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [7 x i8] c"[%d]$ \00"
@.str.1 = [19 x i8] c"I'm the child: %d\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [20 x i8] c"I'm the parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
br label %4
4: ; preds = %0, %23
%5 = load i32, i32* @stdout
%6 = call i32 (...) @getpid()
%7 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 (...) @getchar()
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %4
%13 = load i32, i32* @stdout
%14 = call i32 (...) @getpid()
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %23
17: ; preds = %4
%18 = load i32, i32* @stdout
%19 = call i32 (...) @getpid()
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i64, i64* %2
%22 = call i32 @waitpid(i64 %21, i32* %3, i32 0)
br label %23
23: ; preds = %17, %12
br label %4
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [7 x i8] c"[%d]$ \00"
@.str.1 = [19 x i8] c"I'm the child: %d\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [20 x i8] c"I'm the parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %18, %0
%4 = load i32, i32* @stdout
%5 = call i32 (...) @getpid()
%6 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (...) @getchar()
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
%10 = load i32, i32* @stdout
%11 = call i32 (...) @getpid()
br i1 %9, label %12, label %15
12: ; preds = %3
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%14 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %18
15: ; preds = %3
%16 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%17 = call i32 @waitpid(i64 %8, i32* %1, i32 0)
br label %18
18: ; preds = %15, %12
br label %3
}