#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 */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp = fopen("grades.txt", "r");
  int num = 0, sum = 0, above = 0, below = 0, count = 0;
  if (fp == NULL) {
    printf("ERROR: Could not open file.");
    return -1;
  }
  while ((fscanf(fp, "%d", &num)) != EOF) {
    if (num > 50)
      above++;
    if (num < 50)
      below++;
    sum += num;
    count++;
  }
  printf("Total number of grades: %d\n", count);
  printf("Grades above 50: %d\n", above);
  printf("Grades below 50: %d\n", below);
  printf("Average grade (mean): %.2f%%\n", (float)sum / count);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <main+0x6e>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <main+0x137>
jmp    11b3 <main+0x73>
mov    -0x10(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <main+0xd4>
cmpl   $0x32,-0x14(%rbp)
jle    11ea <main+0xaa>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x32,-0x14(%rbp)
jge    11fd <main+0xbd>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b3 <main+0x73>
mov    -0x24(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cvtsi2ssl -0x18(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe12(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@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
lea    0xeae(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
movl   $0x0,(%rsp)
test   %rax,%rax
je     125e <main+0x11e>
mov    %rax,%r15
lea    0xeb1(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    0x2e5e(%rip),%rcx
xor    %r14d,%r14d
xorps  %xmm0,%xmm0
mov    $0x0,%ebp
mov    $0x0,%ebx
mov    $0x0,%r12d
cmp    (%rcx),%rax
je     11f6 <main+0xb6>
xor    %r12d,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %r13d,%r13d
nop
mov    (%rsp),%eax
mov    %rcx,%r14
xor    %ecx,%ecx
cmp    $0x33,%eax
setge  %cl
add    %ecx,%ebp
xor    %ecx,%ecx
cmp    $0x32,%eax
setl   %cl
add    %ecx,%ebx
add    %eax,%r13d
add    $0x1,%r12d
mov    %r15,%rdi
lea    0xe4e(%rip),%rsi
mov    %rsp,%rdx
call   1050 <fscanf@plt>
mov    %r14,%rcx
cmp    (%r14),%rax
jne    11b0 <main+0x70>
xorps  %xmm0,%xmm0
cvtsi2ss %r13d,%xmm0
xor    %r14d,%r14d
movss  %xmm0,0x4(%rsp)
lea    0xe29(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe34(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe39(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %r12d,%xmm0
movss  0x4(%rsp),%xmm1
divss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
lea    0xe25(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1272 <main+0x132>
lea    0xda8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"grades.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"ERROR: Could not open file.\00"
@.str.3 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.4 = [28 x i8] c"Total number of grades: %d\0A\00"
@.str.5 = [21 x i8] c"Grades above 50: %d\0A\00"
@.str.6 = [21 x i8] c"Grades below 50: %d\0A\00"
@.str.7 = [30 x i8] c"Average grade (mean): %.2f%%\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %53
13: ; preds = %0
br label %14
14: ; preds = %31, %13
%15 = load i32*, i32** %2
%16 = call i64 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%17 = load i64, i64* @EOF
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %37
19: ; preds = %14
%20 = load i32, i32* %3
%21 = icmp sgt i32 %20, 50
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %25
25: ; preds = %22, %19
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, 50
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %3
%33 = load i32, i32* %4
%34 = add nsw i32 %33, %32
store i32 %34, i32* %4
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %14
37: ; preds = %14
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %5
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %4
%45 = sitofp i32 %44 to float
%46 = load i32, i32* %7
%47 = sitofp i32 %46 to float
%48 = fdiv float %45, %47
%49 = fpext float %48 to double
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), double %49)
%51 = load i32*, i32** %2
%52 = call i32 @fclose(i32* %51)
store i32 0, i32* %1
br label %53
53: ; preds = %37, %11
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [11 x i8] c"grades.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"ERROR: Could not open file.\00"
@.str.3 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.4 = [28 x i8] c"Total number of grades: %d\0A\00"
@.str.5 = [21 x i8] c"Grades above 50: %d\0A\00"
@.str.6 = [21 x i8] c"Grades below 50: %d\0A\00"
@.str.7 = [30 x i8] c"Average grade (mean): %.2f%%\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = icmp eq i32* %2, null
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%7 = load i64, i64* @EOF
%8 = icmp eq i64 %6, %7
br i1 %8, label %30, label %11
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %43
11: ; preds = %5, %11
%12 = phi i32 [ %24, %11 ], [ 0, %5 ]
%13 = phi i32 [ %22, %11 ], [ 0, %5 ]
%14 = phi i32 [ %19, %11 ], [ 0, %5 ]
%15 = phi i32 [ %23, %11 ], [ 0, %5 ]
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 50
%18 = zext i1 %17 to i32
%19 = add nuw nsw i32 %14, %18
%20 = icmp slt i32 %16, 50
%21 = zext i1 %20 to i32
%22 = add nuw nsw i32 %13, %21
%23 = add nsw i32 %16, %15
%24 = add nuw nsw i32 %12, 1
%25 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%26 = load i64, i64* @EOF
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %11
28: ; preds = %11
%29 = sitofp i32 %23 to float
br label %30
30: ; preds = %28, %5
%31 = phi float [ 0.000000e+00, %5 ], [ %29, %28 ]
%32 = phi i32 [ 0, %5 ], [ %19, %28 ]
%33 = phi i32 [ 0, %5 ], [ %22, %28 ]
%34 = phi i32 [ 0, %5 ], [ %24, %28 ]
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%38 = sitofp i32 %34 to float
%39 = fdiv float %31, %38
%40 = fpext float %39 to double
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), double %40)
%42 = call i32 @fclose(i32* %2)
br label %43
43: ; preds = %30, %9
%44 = phi i32 [ -1, %9 ], [ 0, %30 ]
ret i32 %44
}