#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 */

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int status;
  int pid;
  char *prog_argv[4];
  prog_argv[0] = "ls";
  prog_argv[1] = "-l";
  prog_argv[2] = "/";
  prog_argv[3] = NULL;
  if ((pid = fork()) < 0) {
    perror("Fork failed");
    exit(errno);
  }
  if (!pid) {
    execvp(prog_argv[0], prog_argv);
  }
  if (pid) {
    waitpid(pid, NULL, 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe8b(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11bc <main+0x6c>
lea    0xe5f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11d3 <main+0x83>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <execvp@plt>
cmpl   $0x0,-0x18(%rbp)
je     11eb <main+0x9b>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
lea    0xea5(%rip),%rax
mov    %rax,(%rsp)
lea    0xe9d(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe94(%rip),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     11b0 <main+0x60>
je     119a <main+0x4a>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <waitpid@plt>
jmp    11a9 <main+0x59>
lea    0xe5f(%rip),%rdi
mov    %rsp,%rsi
call   1050 <execvp@plt>
xor    %eax,%eax
add    $0x28,%rsp
ret
lea    0xe51(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [12 x i8] c"Fork failed\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 3
store i8* null, i8** %12
%13 = call i32 (...) @fork()
store i32 %13, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%24 = load i8*, i8** %23
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%26 = call i32 @execvp(i8* %24, i8** %25)
br label %27
27: ; preds = %22, %19
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %7
%32 = call i32 @waitpid(i32 %31, i32* null, i32 0)
br label %33
33: ; preds = %30, %27
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [12 x i8] c"Fork failed\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i8*]
%4 = bitcast [4 x i8*]* %3 to i8*
%5 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 3
store i8* null, i8** %8
%9 = tail call i32 (...) @fork()
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = icmp eq i32 %9, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5)
br label %21
19: ; preds = %15
%20 = tail call i32 @waitpid(i32 %9, i32* null, i32 0)
br label %21
21: ; preds = %17, %19
ret i32 0
}