#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*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    return 1;
  } else if (pid == 0) {
    printf("child process: excute ls\n");
    execlp("/bin/ls", "ls", NULL);
  } else {
    printf("parent process: wait for child process\n");
    wait(NULL);
    printf("Child Complete\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1196 <main+0x46>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <main+0xa6>
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <main+0x79>
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
lea    0xe72(%rip),%rdi
lea    0xe70(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execlp@plt>
jmp    11ea <main+0x9a>
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
jmp    11ef <main+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1182 <main+0x32>
je     119e <main+0x4e>
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
jmp    11c1 <main+0x71>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%ebx
jmp    11c1 <main+0x71>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5b(%rip),%rdi
lea    0xea8(%rip),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <execlp@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [26 x i8] c"child process: excute ls\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [40 x i8] c"parent process: wait for child process\0A\00"
@.str.5 = [16 x i8] c"Child Complete\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = load i64, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %21
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %19
15: ; preds = %9
%16 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @wait(i32* null)
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %15, %12
br label %20
20: ; preds = %19
store i32 0, i32* %1
br label %21
21: ; preds = %20, %6
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@str = [39 x i8] c"parent process: wait for child process\00"
@str.6 = [15 x i8] c"Child Complete\00"
@str.7 = [25 x i8] c"child process: excute ls\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%10 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %15
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @wait(i32* null)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
br label %15
15: ; preds = %11, %8, %3
%16 = phi i32 [ 1, %3 ], [ 0, %8 ], [ 0, %11 ]
ret i32 %16
}