#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*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ scanf(char *, int *);
void fig03_08() {
  unsigned int counter;
  int grade;
  int total;
  float average;
  total = 0;
  counter = 0;
  printf("%s", "Enter grade,-1 to end: ");
  scanf("%d", &grade);
  while (grade != -1) {
    total = total + grade;
    counter = counter + 1;
    printf("%s", "Enter grade,-1 to end: ");
    scanf("%d", &grade);
  }
  if (counter != 0) {
    average = (float)total / counter;
    printf("Class average is %.2f\n", average);
  } else {
    puts("No grades were entered");
  }
}


x86 assembly (clang, -O0):
<fig03_08>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
lea    0xeb3(%rip),%rdi
lea    0xeaf(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb9(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     11b1 <fig03_08+0x81>
mov    -0xc(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
lea    0xe72(%rip),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe78(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
jmp    116b <fig03_08+0x3b>
cmpl   $0x0,-0x4(%rbp)
je     11ed <fig03_08+0xbd>
cvtsi2ssl -0xc(%rbp),%xmm0
mov    -0x4(%rbp),%eax
cvtsi2ss %rax,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe3d(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
jmp    11f9 <fig03_08+0xc9>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fig03_08>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebe(%rip),%rdi
lea    0xeba(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec4(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x4(%rsp),%eax
cmp    $0xffffffff,%eax
je     11df <fig03_08+0xaf>
lea    0xe8f(%rip),%r14
lea    0xe8b(%rip),%r15
lea    0xe9c(%rip),%r12
lea    0x4(%rsp),%r13
xor    %ebp,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
add    %eax,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <scanf@plt>
mov    0x4(%rsp),%eax
add    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
jne    1190 <fig03_08+0x60>
test   %ebp,%ebp
je     11df <fig03_08+0xaf>
neg    %ebp
cvtsi2ss %ebx,%xmm0
mov    %ebp,%eax
cvtsi2ss %rax,%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe48(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
jmp    11eb <fig03_08+0xbb>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [24 x i8] c"Enter grade,-1 to end: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [23 x i8] c"Class average is %.2f\0A\00"
@.str.4 = [23 x i8] c"No grades were entered\00"
define void @fig03_08() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca float
store i32 0, i32* %3
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
br label %7
7: ; preds = %10, %0
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i32, i32* %3
%12 = load i32, i32* %2
%13 = add nsw i32 %11, %12
store i32 %13, i32* %3
%14 = load i32, i32* %1
%15 = add i32 %14, 1
store i32 %15, i32* %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
br label %7
18: ; preds = %7
%19 = load i32, i32* %1
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = load i32, i32* %3
%23 = sitofp i32 %22 to float
%24 = load i32, i32* %1
%25 = uitofp i32 %24 to float
%26 = fdiv float %23, %25
store float %26, float* %4
%27 = load float, float* %4
%28 = fpext float %27 to double
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %28)
br label %32
30: ; preds = %18
%31 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [24 x i8] c"Enter grade,-1 to end: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [23 x i8] c"Class average is %.2f\0A\00"
@.str.4 = [23 x i8] c"No grades were entered\00"
define void @fig03_08() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %25, label %7
7: ; preds = %0, %7
%8 = phi i32 [ %15, %7 ], [ %5, %0 ]
%9 = phi i32 [ %12, %7 ], [ 0, %0 ]
%10 = phi i32 [ %11, %7 ], [ 0, %0 ]
%11 = add nsw i32 %8, %10
%12 = add i32 %9, 1
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%15 = load i32, i32* %1
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %7
17: ; preds = %7
%18 = icmp eq i32 %12, 0
br i1 %18, label %25, label %19
19: ; preds = %17
%20 = sitofp i32 %11 to float
%21 = uitofp i32 %12 to float
%22 = fdiv float %20, %21
%23 = fpext float %22 to double
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %23)
br label %27
25: ; preds = %0, %17
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
ret void
}