#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int sum = 0, num = 0, input = 1;
  FILE *in = fopen("grades.txt", "rt");
  while (!feof(in)) {
    fscanf(in, "%d", &input);
    if (input > 0) {
      sum += input;
      num++;
    }
  }
  printf("The average of the numbers in 'grades.txt' is: %d\n", sum / num);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
lea    0xe95(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1199 <main+0x59>
jmp    11ce <main+0x8e>
mov    -0x18(%rbp),%rdi
lea    0xe6a(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <fscanf@plt>
cmpl   $0x0,-0x10(%rbp)
jle    11c9 <main+0x89>
mov    -0x10(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117b <main+0x3b>
mov    -0x8(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,%esi
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x1,0x4(%rsp)
lea    0xea6(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %r13d,%r13d
lea    0xe93(%rip),%r14
lea    0x4(%rsp),%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fscanf@plt>
mov    0x4(%rsp),%eax
xor    %ecx,%ecx
test   %eax,%eax
setg   %cl
cmovle %r13d,%eax
add    %ecx,%ebx
add    %eax,%ebp
mov    %r12,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1190 <main+0x50>
mov    %ebp,%eax
xor    %edx,%edx
div    %ebx
lea    0xe47(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%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 = [3 x i8] c"rt\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [51 x i8] c"The average of the numbers in 'grades.txt' is: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 1, i32* %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
br label %7
7: ; preds = %23, %0
%8 = load i32*, i32** %5
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = load i32*, i32** %5
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* %4
%19 = load i32, i32* %2
%20 = add nsw i32 %19, %18
store i32 %20, i32* %2
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %23
23: ; preds = %17, %12
br label %7
24: ; preds = %7
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = sdiv i32 %25, %26
%28 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"grades.txt\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [51 x i8] c"The average of the numbers in 'grades.txt' is: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @feof(i32* %3)
%5 = icmp eq i32 %4, 0
br label %6
6: ; preds = %0, %6
%7 = phi i32 [ %15, %6 ], [ 0, %0 ]
%8 = phi i32 [ %13, %6 ], [ 0, %0 ]
%9 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
%12 = zext i1 %11 to i32
%13 = add nuw nsw i32 %8, %12
%14 = select i1 %11, i32 %10, i32 0
%15 = add nuw nsw i32 %14, %7
%16 = call i32 @feof(i32* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %6, label %18
18: ; preds = %6
%19 = udiv i32 %15, %13
%20 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %19)
ret i32 0
}