#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 int pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int wait(int *);
int main() {
  pid_t pid;
  int option, stat;
  while (1) {
    printf("\nEnter 1 to exec 'ls' program in child process and 0 to exit\n");
    scanf("%d", &option);
    if (!option)
      exit(0);
    printf("\n");
    if (fork() == 0)
      execl("/bin/ls", "ls", 0);
    pid = wait(&stat);
    printf("\nChild with pid %d is terminated with exit status %d\n", pid,
           WEXITSTATUS(stat));
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeaa(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11ae <main+0x3e>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11e2 <main+0x72>
lea    0xe6d(%rip),%rdi
lea    0xe6b(%rip),%rsi
xor    %edx,%edx
call   1070 <execl@plt>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    117f <main+0xf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe52(%rip),%rdi
mov    %rsp,%rsi
call   1060 <scanf@plt>
cmpl   $0x0,(%rsp)
jne    11c3 <main+0x33>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0x4(%rsp),%r12
lea    0xe3c(%rip),%r13
lea    0xe6b(%rip),%rbx
lea    0xe23(%rip),%rbp
mov    %rsp,%r14
jmp    122c <main+0x9c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   10a0 <wait@plt>
mov    %eax,%r15d
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %r13,%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <scanf@plt>
cmpl   $0x0,(%rsp)
je     11bc <main+0x2c>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    11f0 <main+0x60>
lea    0xdba(%rip),%rdi
lea    0xdb8(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
jmp    11f0 <main+0x60>

LLVM IR (-O0):
@.str = [62 x i8] c"\0AEnter 1 to exec 'ls' program in child process and 0 to exit\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [54 x i8] c"\0AChild with pid %d is terminated with exit status %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %0, %18
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 0)
br label %18
18: ; preds = %16, %12
%19 = call i32 @wait(i32* %4)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = load i32, i32* %4
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %20, i32 %22)
br label %5
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [54 x i8] c"\0AChild with pid %d is terminated with exit status %d\0A\00"
@str = [61 x i8] c"\0AEnter 1 to exec 'ls' program in child process and 0 to exit\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %17, %0
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0, %17
%12 = call i32 @putchar(i32 10)
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 0)
br label %17
17: ; preds = %15, %11
%18 = call i32 @wait(i32* %2)
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %18, i32 %20)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%24 = load i32, i32* %1
%25 = icmp eq i32 %24, 0
br i1 %25, label %9, label %11
}