#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 int pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int errno;
int execl(char *, char *, char *, int /*<<< orphan*/ *);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid;
  int j = 0;
  int i = 0;
  while (i < 2) {
    i++;
    if ((pid = fork()) == -1) {
      perror("fork");
      return errno;
    } else if (pid == 0)
      j = i;
  }
  if (j == 2) {
    j = execl("/bin/sleep", "sleep", "2", NULL);
    if (j == -1)
      perror("execl");
    printf("%d retour sleep %d\n", getpid(), errno);
  } else {
    printf("%d fils\n", (i - j));
    while (j < i) {
      j++;
      wait(NULL);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jge    11da <main+0x7a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11c0 <main+0x60>
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <main+0x128>
cmpl   $0x0,-0x8(%rbp)
jne    11d0 <main+0x70>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11d5 <main+0x75>
jmp    117d <main+0x1d>
cmpl   $0x2,-0xc(%rbp)
jne    1240 <main+0xe0>
lea    0xe1a(%rip),%rdi
lea    0xe18(%rip),%rsi
lea    0xe17(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <execl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    121b <main+0xbb>
lea    0xdfc(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127c <main+0x11c>
mov    -0x10(%rbp),%esi
sub    -0xc(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1277 <main+0x117>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
jmp    1254 <main+0xf4>
jmp    127c <main+0x11c>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     117b <main+0x1b>
cmp    $0xffffffff,%eax
jne    1180 <main+0x20>
jmp    1220 <main+0xc0>
mov    $0x1,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11cd <main+0x6d>
cmp    $0xffffffff,%eax
je     1220 <main+0xc0>
mov    $0x2,%esi
sub    %ebx,%esi
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
or     $0xfffffffe,%ebx
mov    0x2e25(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
call   1070 <wait@plt>
inc    %ebx
jne    11c0 <main+0x60>
jmp    1215 <main+0xb5>
lea    0xe31(%rip),%rdi
lea    0xe2f(%rip),%rsi
lea    0xe2e(%rip),%rdx
xor    %ecx,%ecx
call   1060 <execl@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xd5>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2de4(%rip),%rcx
mov    (%rcx),%edx
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc3(%rip),%r14
mov    (%r14),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd9(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dad(%rip),%r14
jmp    1215 <main+0xb5>
lea    0xdd6(%rip),%rdi
call   1050 <perror@plt>
jmp    11ee <main+0x8e>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@.str.1 = [11 x i8] c"/bin/sleep\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [2 x i8] c"2\00"
@.str.4 = [6 x i8] c"execl\00"
@.str.5 = [20 x i8] c"%d retour sleep %d\0A\00"
@.str.6 = [9 x i8] c"%d fils\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %22, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %23
8: ; preds = %5
%9 = load i32, i32* %4
%10 = add nsw i32 %9, 1
store i32 %10, i32* %4
%11 = call i32 (...) @fork()
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @errno
store i32 %15, i32* %1
br label %52
16: ; preds = %8
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32, i32* %4
store i32 %20, i32* %3
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21
br label %5
23: ; preds = %5
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* null)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = call i32 (...) @getpid()
%34 = load i32, i32* @errno
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %33, i32 %34)
br label %50
36: ; preds = %23
%37 = load i32, i32* %4
%38 = load i32, i32* %3
%39 = sub nsw i32 %37, %38
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %45, %36
%42 = load i32, i32* %3
%43 = load i32, i32* %4
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
store i32 %47, i32* %3
%48 = call i32 @wait(i32* null)
br label %41
49: ; preds = %41
br label %50
50: ; preds = %49, %32
%51 = load i32, i32* @EXIT_SUCCESS
store i32 %51, i32* %1
br label %52
52: ; preds = %50, %13
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@errno = global i32 0
@.str.1 = [11 x i8] c"/bin/sleep\00"
@.str.2 = [6 x i8] c"sleep\00"
@.str.3 = [2 x i8] c"2\00"
@.str.4 = [6 x i8] c"execl\00"
@.str.5 = [20 x i8] c"%d retour sleep %d\0A\00"
@.str.6 = [9 x i8] c"%d fils\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %5, %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %25
4: ; preds = %0
br label %5
5: ; preds = %0, %4
%6 = phi i32 [ 1, %4 ], [ 0, %0 ]
%7 = tail call i32 (...) @fork()
switch i32 %7, label %17 [
i32 -1, label %2
i32 0, label %8
]
8: ; preds = %5
%9 = tail call i32 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* null)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i32 (...) @getpid()
%15 = load i32, i32* @errno
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %14, i32 %15)
br label %25
17: ; preds = %5
%18 = sub nuw nsw i32 2, %6
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %20
%21 = phi i32 [ %22, %20 ], [ %6, %17 ]
%22 = add nuw nsw i32 %21, 1
%23 = tail call i32 @wait(i32* null)
%24 = icmp eq i32 %22, 2
br i1 %24, label %25, label %20
25: ; preds = %20, %13, %2
%26 = phi i32* [ @errno, %2 ], [ @EXIT_SUCCESS, %13 ], [ @EXIT_SUCCESS, %20 ]
%27 = load i32, i32* %26
ret i32 %27
}