#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*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(void) {
  char *argv[] = {"ls", "-al"};
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork failed");
    exit(1);
  } else if (pid == 0) {
    execv("/bin/ls", argv);
  } else {
    wait(&status);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c97(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11a9 <main+0x59>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11c9 <main+0x79>
lea    -0x20(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1060 <execv@plt>
jmp    11d2 <main+0x82>
lea    -0x2c(%rbp),%rdi
call   1050 <wait@plt>
jmp    11d7 <main+0x87>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
movaps 0x2ca5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1192 <main+0x42>
je     117a <main+0x2a>
lea    0xc(%rsp),%rdi
call   1050 <wait@plt>
jmp    118b <main+0x3b>
lea    0xe8f(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <execv@plt>
xor    %eax,%eax
add    $0x28,%rsp
ret
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0)]
@stderr = global i32 0
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i8*]
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast [2 x i8*]* %2 to i8*
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %3
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%17 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %16)
br label %20
18: ; preds = %12
%19 = call i32 @wait(i32* %4)
br label %20
20: ; preds = %18, %15
br label %21
21: ; preds = %20
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [4 x i8] c"-al\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0)]
@stderr = global i32 0
@.str.2 = [12 x i8] c"fork failed\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
define i32 @main() {
%1 = alloca [2 x i8*]
%2 = alloca i32
%3 = bitcast [2 x i8*]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = icmp eq i64 %5, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = getelementptr inbounds [2 x i8*], [2 x i8*]* %1, i64 0, i64 0
%15 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %14)
br label %18
16: ; preds = %11
%17 = call i32 @wait(i32* %2)
br label %18
18: ; preds = %13, %16
ret i32 0
}