#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int i, j, n;
  int *vet;
  int retValue;
  pid_t pid;
  if (argc != 2) {
    fprintf(stderr, "Errore numero argomenti.\n");
    return -1;
  }
  n = atoi(argv[1]);
  vet = (int *)malloc(n * sizeof(int));
  if (vet == NULL) {
    fprintf(stderr, "Errore allocazione vet.\n");
    return -2;
  }
  fprintf(stdout, "Numeri binari: \n");
  for (i = 0; i <= n; i++) {
    if (i == n) {
      for (j = 0; j < n; j++) {
        fprintf(stdout, "%d ", vet[j]);
      }
      fprintf(stdout, "\n");
      exit(0);
    } else {
      vet[i] = 0;
      if (fork()) {
        pid = wait(&retValue);
        vet[i] = 1;
        if (fork()) {
          pid = wait(&retValue);
          exit(0);
        }
      }
    }
  }
  free(vet);
  return 0;
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11b3 <main+0x43>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <main+0x1b7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1204 <main+0x94>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1327 <main+0x1b7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1317 <main+0x1a7>
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    129b <main+0x12b>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    127d <main+0x10d>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1241 <main+0xd1>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     12ff <main+0x18f>
lea    -0x2c(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     12fa <main+0x18a>
lea    -0x2c(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x30(%rbp)
xor    %edi,%edi
call   1070 <exit@plt>
jmp    12ff <main+0x18f>
jmp    1304 <main+0x194>
jmp    1309 <main+0x199>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1222 <main+0xb2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11cb <main+0x5b>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11e9 <main+0x79>
mov    %rax,%rbx
mov    0x2e30(%rip),%r12
mov    (%r12),%edi
lea    0xe80(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r14d,%r14d
jns    1216 <main+0xa6>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1205 <main+0x95>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1205 <main+0x95>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%r13d
jne    125c <main+0xec>
test   %r14d,%r14d
jle    1243 <main+0xd3>
lea    0xe1d(%rip),%r14
xor    %ebp,%ebp
mov    (%r12),%edi
mov    (%rbx,%rbp,4),%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1229 <main+0xb9>
mov    (%r12),%edi
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0x4(%rsp),%r15
xor    %ebp,%ebp
jmp    126e <main+0xfe>
add    $0x1,%rbp
cmp    %rbp,%r13
je     121b <main+0xab>
movl   $0x0,(%rbx,%rbp,4)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1265 <main+0xf5>
mov    %r15,%rdi
call   1080 <wait@plt>
movl   $0x1,(%rbx,%rbp,4)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1265 <main+0xf5>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Errore numero argomenti.\0A\00"
@.str.1 = [25 x i8] c"Errore allocazione vet.\0A\00"
@stdout = global i32 0
@.str.2 = [17 x i8] c"Numeri binari: \0A\00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\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 i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 4
%25 = trunc i64 %24 to i32
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i32*
store i32* %27, i32** %9
%28 = load i32*, i32** %9
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %91
33: ; preds = %17
%34 = load i32, i32* @stdout
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %36
36: ; preds = %85, %33
%37 = load i32, i32* %6
%38 = load i32, i32* %8
%39 = icmp sle i32 %37, %38
br i1 %39, label %40, label %88
40: ; preds = %36
%41 = load i32, i32* %6
%42 = load i32, i32* %8
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %64
44: ; preds = %40
store i32 0, i32* %7
br label %45
45: ; preds = %57, %44
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %60
49: ; preds = %45
%50 = load i32, i32* @stdout
%51 = load i32*, i32** %9
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %49
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %45
60: ; preds = %45
%61 = load i32, i32* @stdout
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %40
%65 = load i32*, i32** %9
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %65, i64 %67
store i32 0, i32* %68
%69 = call i64 (...) @fork()
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %83
71: ; preds = %64
%72 = call i32 @wait(i32* %10)
store i32 %72, i32* %11
%73 = load i32*, i32** %9
%74 = load i32, i32* %6
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %73, i64 %75
store i32 1, i32* %76
%77 = call i64 (...) @fork()
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %71
%80 = call i32 @wait(i32* %10)
store i32 %80, i32* %11
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %71
br label %83
83: ; preds = %82, %64
br label %84
84: ; preds = %83
br label %85
85: ; preds = %84
%86 = load i32, i32* %6
%87 = add nsw i32 %86, 1
store i32 %87, i32* %6
br label %36
88: ; preds = %36
%89 = load i32*, i32** %9
%90 = call i32 @free(i32* %89)
store i32 0, i32* %3
br label %91
91: ; preds = %88, %30, %14
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Errore numero argomenti.\0A\00"
@.str.1 = [25 x i8] c"Errore allocazione vet.\0A\00"
@stdout = global i32 0
@.str.2 = [17 x i8] c"Numeri binari: \0A\00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %58
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = shl i32 %12, 2
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %58
20: ; preds = %9
%21 = load i32, i32* @stdout
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp slt i32 %12, 0
br i1 %23, label %56, label %24
24: ; preds = %20
%25 = zext i32 %12 to i64
%26 = icmp eq i32 %12, 0
br i1 %26, label %27, label %41
27: ; preds = %53, %24
%28 = icmp sgt i32 %12, 0
br i1 %28, label %29, label %37
29: ; preds = %27, %29
%30 = phi i64 [ %35, %29 ], [ 0, %27 ]
%31 = load i32, i32* @stdout
%32 = getelementptr inbounds i32, i32* %15, i64 %30
%33 = load i32, i32* %32
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = add nuw nsw i64 %30, 1
%36 = icmp eq i64 %35, %25
br i1 %36, label %37, label %29
37: ; preds = %29, %27
%38 = load i32, i32* @stdout
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %24, %53
%42 = phi i64 [ %54, %53 ], [ 0, %24 ]
%43 = getelementptr inbounds i32, i32* %15, i64 %42
store i32 0, i32* %43
%44 = call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %53, label %46
46: ; preds = %41
%47 = call i32 @wait(i32* %3)
store i32 1, i32* %43
%48 = call i64 (...) @fork()
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = call i32 @wait(i32* %3)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %46, %41
%54 = add nuw nsw i64 %42, 1
%55 = icmp eq i64 %54, %25
br i1 %55, label %27, label %41
56: ; preds = %20
%57 = call i32 @free(i32* %15)
br label %58
58: ; preds = %56, %17, %6
%59 = phi i32 [ -1, %6 ], [ -2, %17 ], [ 0, %56 ]
ret i32 %59
}