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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ child(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  FILE *myFile;
  int n, i, j;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "Errore numero argomenti.\n");
    return -1;
  }
  if ((myFile = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Errore apertura file fopen.\n");
    return -2;
  }
  fscanf(myFile, "%d", &n);
  fclose(myFile);
  if ((fd = open(argv[1], O_RDWR)) == -1) {
    fprintf(stderr, "Errore apertura file open.\n");
    return -3;
  }
  for (i = 1; i < n; i++) {
    for (j = 1; j < n - i; j++) {
      if (fork() > 0) {
        wait((int *)0);
      } else {
        child(fd, j - 1);
        exit(0);
      }
    }
  }
  close(fd);
  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     11e1 <main+0x41>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <main+0x178>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1224 <main+0x84>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1318 <main+0x178>
mov    -0x18(%rbp),%rdi
lea    0xe0a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1284 <main+0xe4>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1318 <main+0x178>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1309 <main+0x169>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    12f6 <main+0x156>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
jle    12ce <main+0x12e>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    12e3 <main+0x143>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
sub    $0x1,%esi
call   1030 <child@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12e8 <main+0x148>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    129e <main+0xfe>
jmp    12fb <main+0x15b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    128b <main+0xeb>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    126d <main+0xcd>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe5b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1295 <main+0xf5>
mov    %rax,%rbp
lea    0xe62(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x8(%rbx),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b1 <main+0x111>
mov    %eax,%r14d
mov    0xc(%rsp),%eax
cmp    $0x2,%eax
jl     1289 <main+0xe9>
mov    $0x1,%ebp
jmp    1227 <main+0x87>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    %eax,%ebp
jge    1289 <main+0xe9>
mov    %eax,%ecx
sub    %ebp,%ecx
cmp    $0x2,%ecx
jl     1220 <main+0x80>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jle    12d4 <main+0x134>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    0xc(%rsp),%eax
mov    %eax,%ecx
sub    %ebp,%ecx
lea    0x1(%rbx),%edx
add    $0x2,%ebx
cmp    %ecx,%ebx
mov    %edx,%ebx
jl     1240 <main+0xa0>
jmp    1220 <main+0x80>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12cb <main+0x12b>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12cb <main+0x12b>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    12cb <main+0x12b>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xfffffffd,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14d,%edi
mov    %ebx,%esi
call   1030 <child@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Errore numero argomenti.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"Errore apertura file fopen.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@O_RDWR = global i32 0
@.str.4 = [28 x i8] c"Errore apertura file open.\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %3
br label %73
25: ; preds = %16
%26 = load i32*, i32** %6
%27 = call i32 @fscanf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %7)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %10
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -3, i32* %3
br label %73
39: ; preds = %25
store i32 1, i32* %8
br label %40
40: ; preds = %67, %39
%41 = load i32, i32* %8
%42 = load i32, i32* %7
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %70
44: ; preds = %40
store i32 1, i32* %9
br label %45
45: ; preds = %63, %44
%46 = load i32, i32* %9
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = sub nsw i32 %47, %48
%50 = icmp slt i32 %46, %49
br i1 %50, label %51, label %66
51: ; preds = %45
%52 = call i64 (...) @fork()
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @wait(i32* null)
br label %62
56: ; preds = %51
%57 = load i32, i32* %10
%58 = load i32, i32* %9
%59 = sub nsw i32 %58, 1
%60 = call i32 @child(i32 %57, i32 %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %54
br label %63
63: ; preds = %62
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %45
66: ; preds = %45
br label %67
67: ; preds = %66
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %40
70: ; preds = %40
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %70, %36, %22, %13
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Errore numero argomenti.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"Errore apertura file fopen.\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@O_RDWR = global i32 0
@.str.4 = [28 x i8] c"Errore apertura file open.\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 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %55
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %55
17: ; preds = %9
%18 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%19 = call i32 @fclose(i32* %12)
%20 = load i8*, i8** %10
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = load i32, i32* %3
%26 = icmp sgt i32 %25, 1
br i1 %26, label %30, label %53
27: ; preds = %17
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %55
30: ; preds = %24, %49
%31 = phi i32 [ %50, %49 ], [ %25, %24 ]
%32 = phi i32 [ %51, %49 ], [ 1, %24 ]
%33 = sub nsw i32 %31, %32
%34 = icmp sgt i32 %33, 1
br i1 %34, label %35, label %49
35: ; preds = %30, %39
%36 = phi i32 [ %41, %39 ], [ 1, %30 ]
%37 = call i64 (...) @fork()
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = call i32 @wait(i32* null)
%41 = add nuw nsw i32 %36, 1
%42 = load i32, i32* %3
%43 = sub nsw i32 %42, %32
%44 = icmp slt i32 %41, %43
br i1 %44, label %35, label %49
45: ; preds = %35
%46 = add nsw i32 %36, -1
%47 = call i32 @child(i32 %22, i32 %46)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %39, %30
%50 = phi i32 [ %31, %30 ], [ %42, %39 ]
%51 = add nuw nsw i32 %32, 1
%52 = icmp slt i32 %51, %50
br i1 %52, label %30, label %53
53: ; preds = %49, %24
%54 = call i32 @close(i32 %22)
br label %55
55: ; preds = %53, %27, %14, %6
%56 = phi i32 [ -1, %6 ], [ -2, %14 ], [ -3, %27 ], [ 0, %53 ]
ret i32 %56
}