#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 *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid = fork();
  if (pid > 0) {
    printf("Parent process\n");
    wait(NULL);
    printf("Close parent process\n");
    exit(0);
  } else if (pid == 0) {
    printf("Child process\n");
    execl("/bin/ls", "ls", "-l", (char *)0);
  } else {
    printf("ERROR: System call \"fork()\" failed\n\n");
    return 1;
  }
  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)
jle    119d <main+0x4d>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <main+0x87>
lea    0xe77(%rip),%rdi
call   1030 <printf@plt>
lea    0xe7a(%rip),%rdi
lea    0xe78(%rip),%rsi
lea    0xe74(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <execl@plt>
jmp    11ef <main+0x9f>
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fb <main+0xab>
jmp    11f4 <main+0xa4>
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
jg     11a0 <main+0x50>
je     1172 <main+0x22>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    119c <main+0x4c>
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7b(%rip),%rdi
lea    0xe79(%rip),%rsi
lea    0xe75(%rip),%rdx
xor    %ebx,%ebx
xor    %ecx,%ecx
call   1050 <execl@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Parent process\0A\00"
@.str.1 = [22 x i8] c"Close parent process\0A\00"
@.str.2 = [15 x i8] c"Child process\0A\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [37 x i8] c"ERROR: System call \22fork()\22 failed\0A\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 sgt i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @wait(i32* null)
%9 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %19
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %21
19: ; preds = %14
br label %20
20: ; preds = %19
store i32 0, i32* %1
br label %21
21: ; preds = %20, %17
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str.3 = [8 x i8] c"/bin/ls\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [3 x i8] c"-l\00"
@str = [36 x i8] c"ERROR: System call \22fork()\22 failed\0A\00"
@str.7 = [14 x i8] c"Child process\00"
@str.8 = [15 x i8] c"Parent process\00"
@str.9 = [21 x i8] c"Close parent process\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp sgt i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%5 = tail call i32 @wait(i32* null)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %1, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%12 = tail 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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %10, %13
%16 = phi i32 [ 0, %10 ], [ 1, %13 ]
ret i32 %16
}