#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 double uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ atof(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
double cal_average(const char *filename) {
  uint32_t data_count = 0;
  double sum = 0.0;
  char buffer[16];
  FILE *fp = fopen(filename, "r");
  if (!fp)
    exit(1);
  while (fgets(buffer, 16, fp)) {
    ++data_count;
    sum += atof(buffer);
  }
  fclose(fp);
  return sum / data_count;
}


x86 assembly (clang, -O0):
<cal_average>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1195 <cal_average+0x45>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    119a <cal_average+0x4a>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x10,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11e5 <cal_average+0x95>
movsd  0xe42(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <atof@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    119a <cal_average+0x4a>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movsd  -0x18(%rbp),%xmm0
divsd  -0x10(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cal_average>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1219 <cal_average+0xc9>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e9 <cal_average+0x99>
xorpd  %xmm1,%xmm1
lea    0x10(%rsp),%rbx
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
nopl   0x0(%rax,%rax,1)
addsd  0xe58(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <atof@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
movsd  0x8(%rsp),%xmm1
test   %rax,%rax
jne    11a0 <cal_average+0x50>
jmp    11f6 <cal_average+0xa6>
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
call   1040 <fclose@plt>
movsd  (%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define double @cal_average(i8* %0) {
%2 = alloca i8*
%3 = alloca double
%4 = alloca double
%5 = alloca [16 x i8]
%6 = alloca i32*
store i8* %0, i8** %2
store double 0.000000e+00, double* %3
store double 0.000000e+00, double* %4
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
br label %14
14: ; preds = %19, %13
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%16 = load i32*, i32** %6
%17 = call i64 @fgets(i8* %15, i32 16, i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load double, double* %3
%21 = fadd double %20, 1.000000e+00
store double %21, double* %3
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%23 = call i64 @atof(i8* %22)
%24 = sitofp i64 %23 to double
%25 = load double, double* %4
%26 = fadd double %25, %24
store double %26, double* %4
br label %14
27: ; preds = %14
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load double, double* %4
%31 = load double, double* %3
%32 = fdiv double %30, %31
ret double %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define double @cal_average(i8* nocapture %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 16, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %20, label %11
9: ; preds = %1
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %6, %11
%12 = phi double [ %14, %11 ], [ 0.000000e+00, %6 ]
%13 = phi double [ %17, %11 ], [ 0.000000e+00, %6 ]
%14 = fadd double %12, 1.000000e+00
%15 = call i64 @atof(i8* %3)
%16 = sitofp i64 %15 to double
%17 = fadd double %13, %16
%18 = call i64 @fgets(i8* %3, i32 16, i32* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %11
20: ; preds = %11, %6
%21 = phi double [ 0.000000e+00, %6 ], [ %17, %11 ]
%22 = phi double [ 0.000000e+00, %6 ], [ %14, %11 ]
%23 = call i32 @fclose(i32* %4)
%24 = fdiv double %21, %22
ret double %24
}