#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 scalar_t__ pid_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int *malloc(int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int n, temp_d, *v, i, statVal;
  pid_t pid;
  if (argc < 2) {
    fprintf(stdout, "Usage: %s <nElem>\n", argv[0]);
    exit(1);
  }
  n = atoi(argv[1]);
  v = malloc(sizeof(int) * n);
  fprintf(stdout, "Insert %d numbers:\n", n);
  for (i = 0; i < n; i++) {
    scanf("%d", &temp_d);
    v[i] = temp_d;
  }
  fprintf(stdout, "\nPrint those numbers in reverse order:\n");
  for (i = 0; i < n; i++) {
    pid = fork();
    if (pid) {
      waitpid(pid, &statVal, 0);
      fprintf(stdout, "%d\n", v[i]);
      break;
    }
  }
  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)
jge    11b8 <main+0x48>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1234 <main+0xc4>
lea    0xe18(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fc <main+0x8c>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12bd <main+0x14d>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12aa <main+0x13a>
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12bd <main+0x14d>
jmp    12af <main+0x13f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1252 <main+0xe2>
xor    %eax,%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    $0x1,%edi
jle    126e <main+0xfe>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    0x2e39(%rip),%rbx
mov    (%rbx),%edi
lea    0xe63(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %r14d,%r14d
jle    1226 <main+0xb6>
mov    %r14d,%ebp
xor    %r13d,%r13d
lea    0xe5b(%rip),%r12
mov    %rsp,%rbx
nop
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
mov    (%rsp),%eax
mov    %eax,(%r15,%r13,4)
add    $0x1,%r13
cmp    %r13,%rbp
jne    11d0 <main+0x60>
mov    0x2dee(%rip),%r12
mov    (%r12),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %r14d,%r14d
jle    125d <main+0xed>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1238 <main+0xc8>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1210 <main+0xa0>
jmp    125d <main+0xed>
mov    (%rbx),%edi
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    125d <main+0xed>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    (%r12),%edi
mov    (%r15,%rbx,4),%edx
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [19 x i8] c"Usage: %s <nElem>\0A\00"
@.str.1 = [20 x i8] c"Insert %d numbers:\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [40 x i8] c"\0APrint those numbers in reverse order:\0A\00"
@.str.4 = [4 x i8] c"%d\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 i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stdout
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = mul i64 4, %27
%29 = trunc i64 %28 to i32
%30 = call i32* @malloc(i32 %29)
store i32* %30, i32** %8
%31 = load i32, i32* @stdout
%32 = load i32, i32* %6
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 0, i32* %9
br label %34
34: ; preds = %45, %21
%35 = load i32, i32* %9
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%40 = load i32, i32* %7
%41 = load i32*, i32** %8
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32 %40, i32* %44
br label %45
45: ; preds = %38
%46 = load i32, i32* %9
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
br label %34
48: ; preds = %34
%49 = load i32, i32* @stdout
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %9
br label %51
51: ; preds = %70, %48
%52 = load i32, i32* %9
%53 = load i32, i32* %6
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %73
55: ; preds = %51
%56 = call i64 (...) @fork()
store i64 %56, i64* %11
%57 = load i64, i64* %11
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %55
%60 = load i64, i64* %11
%61 = call i32 @waitpid(i64 %60, i32* %10, i32 0)
%62 = load i32, i32* @stdout
%63 = load i32*, i32** %8
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %67)
br label %73
69: ; preds = %55
br label %70
70: ; preds = %69
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %51
73: ; preds = %59, %51
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [19 x i8] c"Usage: %s <nElem>\0A\00"
@.str.1 = [20 x i8] c"Insert %d numbers:\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [40 x i8] c"\0APrint those numbers in reverse order:\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = shl i32 %16, 2
%18 = tail call i32* @malloc(i32 %17)
%19 = load i32, i32* @stdout
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%21 = icmp sgt i32 %16, 0
br i1 %21, label %25, label %22
22: ; preds = %13
%23 = load i32, i32* @stdout
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %51
25: ; preds = %13
%26 = zext i32 %16 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ 0, %25 ], [ %32, %27 ]
%29 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%30 = load i32, i32* %3
%31 = getelementptr inbounds i32, i32* %18, i64 %28
store i32 %30, i32* %31
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %26
br i1 %33, label %34, label %27
34: ; preds = %27
%35 = load i32, i32* @stdout
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br i1 %21, label %37, label %51
37: ; preds = %34, %48
%38 = phi i32 [ %49, %48 ], [ 0, %34 ]
%39 = call i64 (...) @fork()
%40 = icmp eq i64 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %37
%42 = call i32 @waitpid(i64 %39, i32* %4, i32 0)
%43 = load i32, i32* @stdout
%44 = zext i32 %38 to i64
%45 = getelementptr inbounds i32, i32* %18, i64 %44
%46 = load i32, i32* %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %51
48: ; preds = %37
%49 = add nuw nsw i32 %38, 1
%50 = icmp eq i32 %49, %16
br i1 %50, label %51, label %37
51: ; preds = %48, %22, %34, %41
ret i32 0
}