#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int Nums[100];
  int NumNums, Median;
  int Load_Mem(char *, int *);
  if (argc != 2) {
    printf("usage: ./P1-2 valuefile\n");
    exit(1);
  }
  NumNums = Load_Mem(*++argv, Nums);
  if (NumNums != 100) {
    printf("valuefiles must contain 100 entries\n");
    exit(1);
  }
  int i, j, temp;
  for (i = 0; i < NumNums; i++) {
    for (j = i + 1; j < NumNums; j++) {
      if (Nums[i] > Nums[j]) {
        temp = Nums[j];
        Nums[j] = Nums[i];
        Nums[i] = temp;
      }
    }
  }
  if (NumNums % 2 == 0) {
    Median = (Nums[NumNums / 2] + Nums[NumNums / 2 - 1]) / 2;
  } else {
    Median = (Nums[NumNums / 2]);
  }
  printf("The median of the array is %d\n", Median);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     116b <main+0x3b>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
lea    -0x1a0(%rbp),%rsi
call   1040 <Load_Mem@plt>
mov    %eax,-0x1a4(%rbp)
cmpl   $0x64,-0x1a4(%rbp)
je     11b5 <main+0x85>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0x1ac(%rbp)
mov    -0x1ac(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jge    128a <main+0x15a>
mov    -0x1ac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b0(%rbp)
mov    -0x1b0(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jge    1271 <main+0x141>
movslq -0x1ac(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
movslq -0x1b0(%rbp),%rcx
cmp    -0x1a0(%rbp,%rcx,4),%eax
jle    1258 <main+0x128>
movslq -0x1b0(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
mov    %eax,-0x1b4(%rbp)
movslq -0x1ac(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%ecx
movslq -0x1b0(%rbp),%rax
mov    %ecx,-0x1a0(%rbp,%rax,4)
mov    -0x1b4(%rbp),%ecx
movslq -0x1ac(%rbp),%rax
mov    %ecx,-0x1a0(%rbp,%rax,4)
jmp    125d <main+0x12d>
mov    -0x1b0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b0(%rbp)
jmp    11e0 <main+0xb0>
jmp    1276 <main+0x146>
mov    -0x1ac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1ac(%rbp)
jmp    11bf <main+0x8f>
mov    -0x1a4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12f4 <main+0x1c4>
mov    -0x1a4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    -0x1a0(%rbp,%rax,4),%eax
mov    %eax,-0x1b8(%rbp)
mov    -0x1a4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1b8(%rbp),%eax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    -0x1a0(%rbp,%rcx,4),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1a8(%rbp)
jmp    1311 <main+0x1e1>
mov    -0x1a4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    -0x1a0(%rbp,%rax,4),%eax
mov    %eax,-0x1a8(%rbp)
mov    -0x1a8(%rbp),%esi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
sub    $0x198,%rsp
cmp    $0x2,%edi
jne    11ab <main+0x6b>
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
call   1050 <Load_Mem@plt>
cmp    $0x64,%eax
jne    11b4 <main+0x74>
mov    $0x1,%eax
xor    %r8d,%r8d
jmp    117a <main+0x3a>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rax
cmp    $0x64,%r8
je     11ca <main+0x8a>
mov    %r8,%rdx
add    $0x1,%r8
cmp    $0x62,%rdx
ja     1170 <main+0x30>
mov    (%rsp,%rdx,4),%ecx
mov    %rax,%rdi
jmp    119a <main+0x5a>
nop
add    $0x1,%rdi
cmp    $0x64,%rdi
je     1170 <main+0x30>
mov    (%rsp,%rdi,4),%esi
cmp    %esi,%ecx
jle    1190 <main+0x50>
mov    %ecx,(%rsp,%rdi,4)
mov    %esi,(%rsp,%rdx,4)
mov    %esi,%ecx
jmp    1190 <main+0x50>
lea    0xe91(%rip),%rdi
jmp    11bb <main+0x7b>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0xc4(%rsp),%eax
add    0xc8(%rsp),%eax
mov    %eax,%esi
shr    $0x1f,%esi
add    %eax,%esi
sar    %esi
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"usage: ./P1-2 valuefile\0A\00"
@.str.1 = [37 x i8] c"valuefiles must contain 100 entries\0A\00"
@.str.2 = [31 x i8] c"The median of the array is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x 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 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i32 1
store i8** %19, i8*** %5
%20 = load i8*, i8** %19
%21 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 0
%22 = call i32 @Load_Mem(i8* %20, i32* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 100
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
store i32 0, i32* %9
br label %29
29: ; preds = %71, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %74
33: ; preds = %29
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %10
br label %36
36: ; preds = %67, %33
%37 = load i32, i32* %10
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %70
40: ; preds = %36
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = icmp sgt i32 %44, %48
br i1 %49, label %50, label %66
50: ; preds = %40
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %52
%54 = load i32, i32* %53
store i32 %54, i32* %11
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %56
%58 = load i32, i32* %57
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %60
store i32 %58, i32* %61
%62 = load i32, i32* %11
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %64
store i32 %62, i32* %65
br label %66
66: ; preds = %50, %40
br label %67
67: ; preds = %66
%68 = load i32, i32* %10
%69 = add nsw i32 %68, 1
store i32 %69, i32* %10
br label %36
70: ; preds = %36
br label %71
71: ; preds = %70
%72 = load i32, i32* %9
%73 = add nsw i32 %72, 1
store i32 %73, i32* %9
br label %29
74: ; preds = %29
%75 = load i32, i32* %7
%76 = srem i32 %75, 2
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %92
78: ; preds = %74
%79 = load i32, i32* %7
%80 = sdiv i32 %79, 2
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %81
%83 = load i32, i32* %82
%84 = load i32, i32* %7
%85 = sdiv i32 %84, 2
%86 = sub nsw i32 %85, 1
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %87
%89 = load i32, i32* %88
%90 = add nsw i32 %83, %89
%91 = sdiv i32 %90, 2
store i32 %91, i32* %8
br label %98
92: ; preds = %74
%93 = load i32, i32* %7
%94 = sdiv i32 %93, 2
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [100 x i32], [100 x i32]* %6, i64 0, i64 %95
%97 = load i32, i32* %96
store i32 %97, i32* %8
br label %98
98: ; preds = %92, %78
%99 = load i32, i32* %8
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %99)
%101 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [31 x i8] c"The median of the array is %d\0A\00"
@str = [36 x i8] c"valuefiles must contain 100 entries\00"
@str.3 = [24 x i8] c"usage: ./P1-2 valuefile\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i32]
%4 = bitcast [100 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.3, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%13 = call i32 @Load_Mem(i8* %11, i32* %12)
%14 = icmp eq i32 %13, 100
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %36, %21
%19 = add nuw nsw i64 %23, 1
%20 = icmp eq i64 %24, 100
br i1 %20, label %40, label %21
21: ; preds = %9, %18
%22 = phi i64 [ %24, %18 ], [ 0, %9 ]
%23 = phi i64 [ %19, %18 ], [ 1, %9 ]
%24 = add nuw nsw i64 %22, 1
%25 = icmp ult i64 %22, 99
br i1 %25, label %26, label %18
26: ; preds = %21
%27 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %22
%28 = load i32, i32* %27
br label %29
29: ; preds = %26, %36
%30 = phi i32 [ %28, %26 ], [ %37, %36 ]
%31 = phi i64 [ %23, %26 ], [ %38, %36 ]
%32 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = icmp sgt i32 %30, %33
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 %30, i32* %32
store i32 %33, i32* %27
br label %36
36: ; preds = %29, %35
%37 = phi i32 [ %30, %29 ], [ %33, %35 ]
%38 = add nuw nsw i64 %31, 1
%39 = icmp eq i64 %38, 100
br i1 %39, label %18, label %29
40: ; preds = %18
%41 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 50
%42 = load i32, i32* %41
%43 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 49
%44 = load i32, i32* %43
%45 = add nsw i32 %44, %42
%46 = sdiv i32 %45, 2
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 0)
unreachable
}