#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 errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
int wait(int *);
int nfork(int n) {
  pid_t fork_return = NULL;
  pid_t wait_return = NULL;
  int error = 0;
  int wait_status = 0;
  int fprintf_return = 0;
  int nb_fork = 0;
  for (nb_fork = 0; nb_fork < n; nb_fork++) {
    fork_return = fork();
    if (fork_return == -1) {
      perror("Error fork");
      if (nb_fork == 0)
        return -1;
      return nb_fork;
    } else if (fork_return == 0) {
      fprintf_return = fprintf(stdout, "Fils: %d \n", nb_fork + 1);
      if (fprintf_return < 0) {
        perror("Error fprintf");
        return fprintf_return;
      }
      exit(0);
    }
    fprintf_return = fprintf(stdout, "J'ai cree: %d fils \n", nb_fork + 1);
    if (fprintf_return < 0) {
      perror("Error fprintf");
      return fprintf_return;
    }
  }
  for (nb_fork = 0; nb_fork < n; nb_fork++) {
    wait_return = wait(&wait_status);
    if (wait_return == -1) {
      error = errno;
      perror("Error wait");
      return error;
    }
  }
  fprintf_return =
      fprintf(stdout, "J'ai cree un total de: %d fils \n", nb_fork);
  return nb_fork;
}


x86 assembly (clang, -O0):
<nfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1280 <nfork+0x130>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d9 <nfork+0x89>
lea    0xe4d(%rip),%rdi
call   1040 <perror@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11ce <nfork+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <nfork+0x1b0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <nfork+0x1b0>
cmpl   $0x0,-0xc(%rbp)
jne    1229 <nfork+0xd9>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
lea    0xe12(%rip),%rsi
call   1030 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1222 <nfork+0xd2>
lea    0xe04(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <nfork+0x1b0>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    122e <nfork+0xde>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
lea    0xde0(%rip),%rsi
call   1030 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126d <nfork+0x11d>
lea    0xdb9(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <nfork+0x1b0>
jmp    1272 <nfork+0x122>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118c <nfork+0x3c>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12df <nfork+0x18f>
lea    -0x18(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12cc <nfork+0x17c>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xd7d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <nfork+0x1b0>
jmp    12d1 <nfork+0x181>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1287 <nfork+0x137>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xd52(%rip),%rsi
call   1030 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nfork>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
test   %edi,%edi
mov    $0x0,%r13d
cmovg  %edi,%r13d
movl   $0x0,0x4(%rsp)
mov    0x2e5f(%rip),%r12
lea    0xea4(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %ebp,%r13d
je     11d1 <nfork+0x81>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     121c <nfork+0xcc>
cmp    $0xffffffff,%eax
je     1246 <nfork+0xf6>
mov    (%r12),%edi
add    $0x1,%ebp
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <fprintf@plt>
test   %eax,%eax
jns    1190 <nfork+0x40>
mov    %eax,%ebx
lea    0xe4f(%rip),%rdi
call   1040 <perror@plt>
mov    %ebx,%r14d
jmp    120a <nfork+0xba>
test   %r14d,%r14d
jle    11f4 <nfork+0xa4>
lea    0x4(%rsp),%rbx
mov    %r14d,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
je     125d <nfork+0x10d>
add    $0xffffffff,%ebp
jne    11e0 <nfork+0x90>
jmp    11f7 <nfork+0xa7>
xor    %r14d,%r14d
mov    (%r12),%edi
lea    0xe42(%rip),%rsi
mov    %r14d,%edx
call   1030 <fprintf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edi
add    $0x1,%ebp
lea    0xde1(%rip),%rsi
mov    %ebp,%edx
call   1030 <fprintf@plt>
test   %eax,%eax
jns    1275 <nfork+0x125>
mov    %eax,%r14d
lea    0xdd7(%rip),%rdi
call   1040 <perror@plt>
jmp    120a <nfork+0xba>
lea    0xdb3(%rip),%rdi
call   1040 <perror@plt>
cmp    $0x1,%ebp
sbb    %r14d,%r14d
or     %ebp,%r14d
jmp    120a <nfork+0xba>
mov    0x2d7c(%rip),%rax
mov    (%rax),%r14d
lea    0xdcb(%rip),%rdi
call   1040 <perror@plt>
jmp    120a <nfork+0xba>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Error fork\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"Fils: %d \0A\00"
@.str.2 = [14 x i8] c"Error fprintf\00"
@.str.3 = [21 x i8] c"J'ai cree: %d fils \0A\00"
@errno = global i32 0
@.str.4 = [11 x i8] c"Error wait\00"
@.str.5 = [33 x i8] c"J'ai cree un total de: %d fils \0A\00"
define i32 @nfork(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %9
br label %10
10: ; preds = %52, %1
%11 = load i32, i32* %9
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %55
14: ; preds = %10
%15 = call i32 (...) @fork()
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %77
23: ; preds = %18
%24 = load i32, i32* %9
store i32 %24, i32* %2
br label %77
25: ; preds = %14
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i32, i32* @stdout
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %8
store i32 %37, i32* %2
br label %77
38: ; preds = %28
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %25
br label %41
41: ; preds = %40
%42 = load i32, i32* @stdout
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %8
store i32 %50, i32* %2
br label %77
51: ; preds = %41
br label %52
52: ; preds = %51
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
br label %10
55: ; preds = %10
store i32 0, i32* %9
br label %56
56: ; preds = %69, %55
%57 = load i32, i32* %9
%58 = load i32, i32* %3
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %72
60: ; preds = %56
%61 = call i32 @wait(i32* %7)
store i32 %61, i32* %5
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = load i32, i32* @errno
store i32 %65, i32* %6
%66 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%67 = load i32, i32* %6
store i32 %67, i32* %2
br label %77
68: ; preds = %60
br label %69
69: ; preds = %68
%70 = load i32, i32* %9
%71 = add nsw i32 %70, 1
store i32 %71, i32* %9
br label %56
72: ; preds = %56
%73 = load i32, i32* @stdout
%74 = load i32, i32* %9
%75 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %74)
store i32 %75, i32* %8
%76 = load i32, i32* %9
store i32 %76, i32* %2
br label %77
77: ; preds = %72, %64, %48, %35, %23, %22
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error fork\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"Fils: %d \0A\00"
@.str.2 = [14 x i8] c"Error fprintf\00"
@.str.3 = [21 x i8] c"J'ai cree: %d fils \0A\00"
@errno = global i32 0
@.str.4 = [11 x i8] c"Error wait\00"
@.str.5 = [33 x i8] c"J'ai cree un total de: %d fils \0A\00"
define i32 @nfork(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = call i32 @llvm.smax.i32(i32 %0, i32 0)
br label %5
5: ; preds = %25, %1
%6 = phi i32 [ 0, %1 ], [ %27, %25 ]
%7 = icmp eq i32 %6, %4
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = icmp sgt i32 %0, 0
br i1 %9, label %32, label %42
10: ; preds = %5
%11 = tail call i32 (...) @fork()
switch i32 %11, label %25 [
i32 -1, label %12
i32 0, label %16
]
12: ; preds = %10
%13 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32 %6, 0
%15 = select i1 %14, i32 -1, i32 %6
br label %46
16: ; preds = %10
%17 = load i32, i32* @stdout
%18 = add nuw nsw i32 %6, 1
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %46
23: ; preds = %16
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %10
%26 = load i32, i32* @stdout
%27 = add nuw i32 %6, 1
%28 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %5
30: ; preds = %25
%31 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %46
32: ; preds = %8, %39
%33 = phi i32 [ %40, %39 ], [ 0, %8 ]
%34 = call i32 @wait(i32* %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %46
39: ; preds = %32
%40 = add nuw nsw i32 %33, 1
%41 = icmp eq i32 %40, %0
br i1 %41, label %42, label %32
42: ; preds = %39, %8
%43 = phi i32 [ 0, %8 ], [ %0, %39 ]
%44 = load i32, i32* @stdout
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %43)
br label %46
46: ; preds = %12, %42, %36, %30, %21
%47 = phi i32 [ %19, %21 ], [ %28, %30 ], [ %37, %36 ], [ %43, %42 ], [ %15, %12 ]
ret i32 %47
}