#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 getpid();
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 PID, numero de fils: %d, %d \n",
                               getpid(), nb_fork + 1);
      if (fprintf_return < 0) {
        perror("Error fprintf");
        return fprintf_return;
      }
      exit(0);
    }
    fprintf_return = fprintf(stdout, "Pere PID, nb de fils crees: %d, %d \n",
                             getpid(), 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);
  if (fprintf_return < 0) {
    perror("Error fprintf");
    return fprintf_return;
  }
  return nb_fork;
}


x86 assembly (clang, -O0):
<nfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    12b2 <nfork+0x152>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e9 <nfork+0x89>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11de <nfork+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
cmpl   $0x0,-0xc(%rbp)
jne    124a <nfork+0xea>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1243 <nfork+0xe3>
lea    0xdfb(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    124f <nfork+0xef>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129f <nfork+0x13f>
lea    0xd9f(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
jmp    12a4 <nfork+0x144>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119c <nfork+0x3c>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1311 <nfork+0x1b1>
lea    -0x18(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12fe <nfork+0x19e>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xd73(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
jmp    1303 <nfork+0x1a3>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12b9 <nfork+0x159>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xd48(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    134f <nfork+0x1ef>
lea    0xcef(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1355 <nfork+0x1f5>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    0x2e4f(%rip),%r12
lea    0xeac(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %ebp,%r13d
je     11e1 <nfork+0x81>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1260 <nfork+0x100>
cmp    $0xffffffff,%eax
je     1249 <nfork+0xe9>
mov    (%r12),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
add    $0x1,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
jns    11a0 <nfork+0x40>
jmp    1236 <nfork+0xd6>
test   %r14d,%r14d
jle    1208 <nfork+0xa8>
lea    0x4(%rsp),%rbx
mov    %r14d,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
je     1297 <nfork+0x137>
add    $0xffffffff,%ebp
jne    11f0 <nfork+0x90>
jmp    120b <nfork+0xab>
xor    %r14d,%r14d
mov    (%r12),%edi
lea    0xe56(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
js     1236 <nfork+0xd6>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0xdef(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%r14d
jmp    1224 <nfork+0xc4>
lea    0xdb0(%rip),%rdi
call   1050 <perror@plt>
cmp    $0x1,%ebp
sbb    %r14d,%r14d
or     %ebp,%r14d
jmp    1224 <nfork+0xc4>
mov    (%r12),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
add    $0x1,%ebp
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
jns    12b2 <nfork+0x152>
mov    %eax,%r14d
lea    0xd9e(%rip),%rdi
call   1050 <perror@plt>
jmp    1224 <nfork+0xc4>
mov    0x2d42(%rip),%rax
mov    (%rax),%r14d
lea    0xdb9(%rip),%rdi
call   1050 <perror@plt>
jmp    1224 <nfork+0xc4>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Error fork\00"
@stdout = global i32 0
@.str.1 = [35 x i8] c"Fils PID, numero de fils: %d, %d \0A\00"
@.str.2 = [14 x i8] c"Error fprintf\00"
@.str.3 = [37 x i8] c"Pere PID, nb de fils crees: %d, %d \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 = %54, %1
%11 = load i32, i32* %9
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %57
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 %85
23: ; preds = %18
%24 = load i32, i32* %9
store i32 %24, i32* %2
br label %85
25: ; preds = %14
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i32, i32* @stdout
%30 = call i32 (...) @getpid()
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
%33 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %8
store i32 %38, i32* %2
br label %85
39: ; preds = %28
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %25
br label %42
42: ; preds = %41
%43 = load i32, i32* @stdout
%44 = call i32 (...) @getpid()
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
%47 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* %8
store i32 %52, i32* %2
br label %85
53: ; preds = %42
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
store i32 %56, i32* %9
br label %10
57: ; preds = %10
store i32 0, i32* %9
br label %58
58: ; preds = %71, %57
%59 = load i32, i32* %9
%60 = load i32, i32* %3
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %74
62: ; preds = %58
%63 = call i32 @wait(i32* %7)
store i32 %63, i32* %5
%64 = load i32, i32* %5
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i32, i32* @errno
store i32 %67, i32* %6
%68 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* %6
store i32 %69, i32* %2
br label %85
70: ; preds = %62
br label %71
71: ; preds = %70
%72 = load i32, i32* %9
%73 = add nsw i32 %72, 1
store i32 %73, i32* %9
br label %58
74: ; preds = %58
%75 = load i32, i32* @stdout
%76 = load i32, i32* %9
%77 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %76)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%82 = load i32, i32* %8
store i32 %82, i32* %2
br label %85
83: ; preds = %74
%84 = load i32, i32* %9
store i32 %84, i32* %2
br label %85
85: ; preds = %83, %80, %66, %50, %36, %23, %22
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error fork\00"
@stdout = global i32 0
@.str.1 = [35 x i8] c"Fils PID, numero de fils: %d, %d \0A\00"
@.str.2 = [14 x i8] c"Error fprintf\00"
@.str.3 = [37 x i8] c"Pere PID, nb de fils crees: %d, %d \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 = %26, %1
%6 = phi i32 [ 0, %1 ], [ %29, %26 ]
%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 %34, label %44
10: ; preds = %5
%11 = tail call i32 (...) @fork()
switch i32 %11, label %26 [
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 %51
16: ; preds = %10
%17 = load i32, i32* @stdout
%18 = tail call i32 (...) @getpid()
%19 = add nuw nsw i32 %6, 1
%20 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %51
24: ; preds = %16
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %10
%27 = load i32, i32* @stdout
%28 = tail call i32 (...) @getpid()
%29 = add nuw i32 %6, 1
%30 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %5
32: ; preds = %26
%33 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %51
34: ; preds = %8, %41
%35 = phi i32 [ %42, %41 ], [ 0, %8 ]
%36 = call i32 @wait(i32* %2)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %51
41: ; preds = %34
%42 = add nuw nsw i32 %35, 1
%43 = icmp eq i32 %42, %0
br i1 %43, label %44, label %34
44: ; preds = %41, %8
%45 = phi i32 [ 0, %8 ], [ %0, %41 ]
%46 = load i32, i32* @stdout
%47 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %45)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %44, %12, %49, %38, %32, %22
%52 = phi i32 [ %20, %22 ], [ %30, %32 ], [ %39, %38 ], [ %47, %49 ], [ %15, %12 ], [ %45, %44 ]
ret i32 %52
}