#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 */
double calculateGp(int, int *, int *);
int /*<<< orphan*/ flush();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
char toupper(char);
double askForScores(int courses_count) {
  int units[courses_count];
  int grades[courses_count];
  int i;
  for (i = 0; i < courses_count; i++) {
    int k = i + 1;
    int unit;
    printf("\n\nUnit for course %d: ", k);
    scanf("%d", &unit);
    flush();
    units[i] = unit;
    int valid_grade = 0;
    char grade;
    while (valid_grade == 0) {
      valid_grade = 1;
      printf("\nGrade for course %d: ", k);
      scanf("%c", &grade);
      flush();
      grade = toupper(grade);
      int score;
      switch (grade) {
      case 'A':
        score = 5;
        break;
      case 'B':
        score = 4;
        break;
      case 'C':
        score = 3;
        break;
      case 'D':
        score = 2;
        break;
      case 'E':
        score = 1;
        break;
      case 'F':
        score = 0;
        break;
      default:
        valid_grade = 0;
        printf("\nINVALID: The grade must be from A-F. Please try again\n");
      }
      grades[i] = score;
    }
  }
  return calculateGp(courses_count, grades, units);
}


x86 assembly (clang, -O0):
<askForScores>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12f6 <askForScores+0x1a6>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe59(%rip),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    $0x0,%al
call   1030 <flush@plt>
mov    -0x48(%rbp),%rax
mov    -0x2c(%rbp),%edx
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    12e3 <askForScores+0x193>
movl   $0x1,-0x30(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x51(%rbp)
call   1050 <printf@plt>
mov    -0x51(%rbp),%al
lea    0xe1d(%rip),%rdi
lea    -0x31(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x51(%rbp),%al
call   1030 <flush@plt>
movsbl -0x31(%rbp),%edi
call   1040 <toupper@plt>
mov    %al,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
add    $0xffffffbf,%eax
mov    %eax,%ecx
mov    %rcx,-0x50(%rbp)
sub    $0x5,%eax
ja     12bb <askForScores+0x16b>
mov    -0x50(%rbp),%rax
lea    0xd96(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x5,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x4,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x3,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x2,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x1,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x0,-0x38(%rbp)
jmp    12d0 <askForScores+0x180>
movl   $0x0,-0x30(%rbp)
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%edx
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11fd <askForScores+0xad>
jmp    12e8 <askForScores+0x198>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <askForScores+0x59>
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rsi
mov    -0x4(%rbp),%edi
call   1060 <calculateGp@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<askForScores>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%eax
mov    %rsp,%rcx
mov    %rax,-0x48(%rbp)
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %edi,-0x30(%rbp)
test   %edi,%edi
jle    1266 <askForScores+0x106>
lea    -0x29(%rbp),%r14
xor    %r15d,%r15d
jmp    11ca <askForScores+0x6a>
nopl   (%rax)
mov    $0x46,%r13d
sub    %eax,%r13d
mov    %r13d,(%rbx,%r15,4)
mov    %r12,%r15
cmp    -0x48(%rbp),%r12
je     1266 <askForScores+0x106>
lea    0x1(%r15),%r12
lea    0xe2b(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe31(%rip),%rdi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
xor    %eax,%eax
call   1030 <flush@plt>
mov    -0x34(%rbp),%eax
mov    -0x40(%rbp),%rcx
mov    %eax,(%rcx,%r15,4)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe03(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe09(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
xor    %eax,%eax
call   1030 <flush@plt>
movsbl -0x29(%rbp),%edi
call   1040 <toupper@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
lea    -0x41(%rax),%ecx
cmp    $0x6,%ecx
jb     11b0 <askForScores+0x50>
lea    0xdd9(%rip),%rdi
call   1050 <puts@plt>
mov    %r13d,(%rbx,%r15,4)
jmp    1210 <askForScores+0xb0>
mov    -0x30(%rbp),%edi
mov    %rbx,%rsi
mov    -0x40(%rbp),%rdx
call   1070 <calculateGp@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"\0A\0AUnit for course %d: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [23 x i8] c"\0AGrade for course %d: \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [56 x i8] c"\0AINVALID: The grade must be from A-F. Please try again\0A\00"
define double @askForScores(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
store i32 %0, i32* %2
%12 = load i32, i32* %2
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %3
%15 = alloca i32, i64 %13
store i64 %13, i64* %4
%16 = load i32, i32* %2
%17 = zext i32 %16 to i64
%18 = alloca i32, i64 %17
store i64 %17, i64* %5
store i32 0, i32* %6
br label %19
19: ; preds = %60, %1
%20 = load i32, i32* %6
%21 = load i32, i32* %2
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %63
23: ; preds = %19
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%29 = call i32 (...) @flush()
%30 = load i32, i32* %8
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %15, i64 %32
store i32 %30, i32* %33
store i32 0, i32* %9
br label %34
34: ; preds = %54, %23
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %59
37: ; preds = %34
store i32 1, i32* %9
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%41 = call i32 (...) @flush()
%42 = load i8, i8* %10
%43 = call signext i8 @toupper(i8 signext %42)
store i8 %43, i8* %10
%44 = load i8, i8* %10
%45 = sext i8 %44 to i32
switch i32 %45, label %52 [
i32 65, label %46
i32 66, label %47
i32 67, label %48
i32 68, label %49
i32 69, label %50
i32 70, label %51
]
46: ; preds = %37
store i32 5, i32* %11
br label %54
47: ; preds = %37
store i32 4, i32* %11
br label %54
48: ; preds = %37
store i32 3, i32* %11
br label %54
49: ; preds = %37
store i32 2, i32* %11
br label %54
50: ; preds = %37
store i32 1, i32* %11
br label %54
51: ; preds = %37
store i32 0, i32* %11
br label %54
52: ; preds = %37
store i32 0, i32* %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %51, %50, %49, %48, %47, %46
%55 = load i32, i32* %11
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %18, i64 %57
store i32 %55, i32* %58
br label %34
59: ; preds = %34
br label %60
60: ; preds = %59
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %19
63: ; preds = %19
%64 = load i32, i32* %2
%65 = call double @calculateGp(i32 %64, i32* %18, i32* %15)
%66 = load i8*, i8** %3
ret double %65
}

LLVM IR (-O2):
@.str = [23 x i8] c"\0A\0AUnit for course %d: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [23 x i8] c"\0AGrade for course %d: \00"
@.str.3 = [3 x i8] c"%c\00"
@str = [55 x i8] c"\0AINVALID: The grade must be from A-F. Please try again\00"
define double @askForScores(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = zext i32 %0 to i64
%5 = alloca i32, i64 %4
%6 = alloca i32, i64 %4
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %35
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
br label %10
10: ; preds = %8, %32
%11 = phi i64 [ 0, %8 ], [ %13, %32 ]
%12 = phi i32 [ undef, %8 ], [ %33, %32 ]
%13 = add nuw nsw i64 %11, 1
%14 = trunc i64 %13 to i32
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%17 = call i32 (...) @flush()
%18 = load i32, i32* %2
%19 = getelementptr inbounds i32, i32* %5, i64 %11
store i32 %18, i32* %19
%20 = getelementptr inbounds i32, i32* %6, i64 %11
br label %21
21: ; preds = %30, %10
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%23 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%24 = call i32 (...) @flush()
%25 = load i8, i8* %3
%26 = call signext i8 @toupper(i8 signext %25)
store i8 %26, i8* %3
%27 = sext i8 %26 to i32
%28 = add nsw i32 %27, -65
%29 = icmp ult i32 %28, 6
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
store i32 %12, i32* %20
br label %21
32: ; preds = %21
%33 = sub nsw i32 70, %27
store i32 %33, i32* %20
%34 = icmp eq i64 %13, %4
br i1 %34, label %35, label %10
35: ; preds = %32, %1
%36 = call double @calculateGp(i32 %0, i32* %6, i32* %5)
ret double %36
}