#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[], char *env[]) {
  int i;
  int a = 1;
  int aux = 0;
  int estadoChild = 0;
  for (i = 0; i < 10; i++) {
    switch (aux = fork()) {
    case 0:
      a = 3;
      printf("A=%d\n", a);
      exit(a);
    case -1:
      printf("Error al crear el proceso hijo\n");
      exit(-1);
    default:
      wait(&estadoChild);
      if (WIFEXITED(estadoChild) == 0) {
        printf("El proceso hijo a terminado anormalmente\n");
        exit(-1);
      } else {
        a = WEXITSTATUS(estadoChild);
        printf("a=%d\n", a);
      }
    }
  }
  exit(a);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    126b <main+0x10b>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11ed <main+0x8d>
jmp    11bd <main+0x5d>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    1205 <main+0xa5>
jmp    11cd <main+0x6d>
movl   $0x3,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <exit@plt>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    -0x28(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x28(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1237 <main+0xd7>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1258 <main+0xf8>
jmp    125d <main+0xfd>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1196 <main+0x36>
mov    -0x20(%rbp),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    $0xa,%ebx
lea    0x4(%rsp),%r14
lea    0xe76(%rip),%r15
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11d8 <main+0x68>
test   %eax,%eax
je     11e1 <main+0x71>
mov    %r14,%rdi
call   1080 <wait@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     11fe <main+0x8e>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebx
jne    1190 <main+0x20>
mov    %ebp,%edi
call   1070 <exit@plt>
lea    0xe2d(%rip),%rdi
jmp    1205 <main+0x95>
lea    0xe18(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"A=%d\0A\00"
@.str.1 = [32 x i8] c"Error al crear el proceso hijo\0A\00"
@.str.2 = [42 x i8] c"El proceso hijo a terminado anormalmente\0A\00"
@.str.3 = [6 x i8] c"a=%d\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 1, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %8
br label %12
12: ; preds = %40, %3
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %43
15: ; preds = %12
%16 = call i32 (...) @fork()
store i32 %16, i32* %10
switch i32 %16, label %25 [
i32 0, label %17
i32 -1, label %22
]
17: ; preds = %15
store i32 3, i32* %9
%18 = load i32, i32* %9
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %9
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %15
%26 = call i32 @wait(i32* %11)
%27 = load i32, i32* %11
%28 = call i32 @WIFEXITED(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %25
%34 = load i32, i32* %11
%35 = call i32 @WEXITSTATUS(i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %33
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %12
43: ; preds = %12
%44 = load i32, i32* %9
%45 = call i32 @exit(i32 %44)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"A=%d\0A\00"
@.str.3 = [6 x i8] c"a=%d\0A\00"
@str = [31 x i8] c"Error al crear el proceso hijo\00"
@str.4 = [41 x i8] c"El proceso hijo a terminado anormalmente\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
br label %6
6: ; preds = %3, %23
%7 = phi i32 [ 0, %3 ], [ %27, %23 ]
%8 = call i32 (...) @fork()
switch i32 %8, label %15 [
i32 0, label %9
i32 -1, label %12
]
9: ; preds = %6
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 3)
%11 = call i32 @exit(i32 3)
unreachable
12: ; preds = %6
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %6
%16 = call i32 @wait(i32* %4)
%17 = load i32, i32* %4
%18 = call i32 @WIFEXITED(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.4, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %15
%24 = load i32, i32* %4
%25 = call i32 @WEXITSTATUS(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = add nuw nsw i32 %7, 1
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %6
29: ; preds = %23
%30 = call i32 @exit(i32 %25)
unreachable
}