#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 getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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);
  }
  printf("child pid = %d (my pid :getpid=%d)\n", pid, getpid());
  if (!pid) {
    printf("on entre dans execvp()...\n");
    execvp(prog_argv[0], prog_argv);
    printf("on sort de execvp()...\n");
  }
  if (pid) {
    printf("on attend...\n");
    waitpid(pid, NULL, 0);
    printf("c'est fini...\n");
  }
  printf("fin du prog...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe6b(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11dc <main+0x6c>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jne    122f <main+0xbf>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1263 <main+0xf3>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xe74(%rip),%rax
mov    %rax,(%rsp)
lea    0xe6c(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xec2(%rip),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1236 <main+0xb6>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe46(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     11fb <main+0x7b>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xe8a(%rip),%rdi
jmp    121d <main+0x9d>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf2(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execvp@plt>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdc9(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1090 <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
@.str.4 = [36 x i8] c"child pid = %d (my pid :getpid=%d)\0A\00"
@.str.5 = [27 x i8] c"on entre dans execvp()...\0A\00"
@.str.6 = [24 x i8] c"on sort de execvp()...\0A\00"
@.str.7 = [14 x i8] c"on attend...\0A\00"
@.str.8 = [15 x i8] c"c'est fini...\0A\00"
@.str.9 = [16 x i8] c"fin du prog...\0A\00"
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 = call i32 (...) @getpid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%27 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%30 = call i32 @execvp(i8* %28, i8** %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %32
32: ; preds = %25, %19
%33 = load i32, i32* %7
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%37 = load i32, i32* %7
%38 = call i32 @waitpid(i32 %37, i32* null, i32 0)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
br label %40
40: ; preds = %35, %32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%42 = load i32, i32* %3
ret i32 %42
}

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
@.str.4 = [36 x i8] c"child pid = %d (my pid :getpid=%d)\0A\00"
@str = [26 x i8] c"on entre dans execvp()...\00"
@str.10 = [23 x i8] c"on sort de execvp()...\00"
@str.11 = [15 x i8] c"fin du prog...\00"
@str.12 = [13 x i8] c"on attend...\00"
@str.13 = [14 x i8] c"c'est fini...\00"
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 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %9, i32 %16)
%18 = icmp eq i32 %9, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%21 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
br label %27
23: ; preds = %15
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0))
%25 = tail call i32 @waitpid(i32 %9, i32* null, i32 0)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
br label %27
27: ; preds = %19, %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
ret i32 0
}