#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*/ EXIT_FAILURE;
scalar_t__ atof(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fp;
  char *buffer = (char *)malloc(1024);
  int numlines = 0;
  float sum = 0.0f;
  if (argc != 2) {
    printf("Usage: %s filename.txt", argv[0]);
  } else {
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
      printf("Remember about .txt file extension.");
      exit(EXIT_FAILURE);
    }
    while (fgets(buffer, 1024, fp)) {
      sum += atof(buffer);
      numlines++;
    }
    fclose(fp);
    printf("Average of %d numbers is %.2f.", numlines, sum / numlines);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c7 <main+0x57>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1279 <main+0x109>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <main+0x96>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    120b <main+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     124d <main+0xdd>
mov    -0x20(%rbp),%rdi
call   1030 <atof@plt>
cvtsi2ss %rax,%xmm0
addss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    120b <main+0x9b>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%esi
movss  -0x28(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdcb(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x400,%edi
call   1070 <malloc@plt>
cmp    $0x2,%ebp
jne    120a <main+0x9a>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1263 <main+0xf3>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     121d <main+0xad>
xor    %ebp,%ebp
xorps  %xmm0,%xmm0
movss  %xmm0,0xc(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <atof@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  0xc(%rsp),%xmm1
addss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
jmp    1228 <main+0xb8>
mov    (%r14),%rsi
lea    0xdec(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1258 <main+0xe8>
xorps  %xmm0,%xmm0
movss  %xmm0,0xc(%rsp)
xor    %ebp,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
movss  0xc(%rsp),%xmm1
divss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
lea    0xdee(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdaf(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: %s filename.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [36 x i8] c"Remember about .txt file extension.\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"Average of %d numbers is %.2f.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @malloc(i32 1024)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
store i32 0, i32* %8
store float 0.000000e+00, float* %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %54
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
br label %31
31: ; preds = %36, %30
%32 = load i8*, i8** %7
%33 = load i32*, i32** %6
%34 = call i64 @fgets(i8* %32, i32 1024, i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = call i64 @atof(i8* %37)
%39 = sitofp i64 %38 to float
%40 = load float, float* %9
%41 = fadd float %40, %39
store float %41, float* %9
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %31
44: ; preds = %31
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %8
%48 = load float, float* %9
%49 = load i32, i32* %8
%50 = sitofp i32 %49 to float
%51 = fdiv float %48, %50
%52 = fpext float %51 to double
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %47, double %52)
br label %54
54: ; preds = %44, %14
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Usage: %s filename.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [36 x i8] c"Remember about .txt file extension.\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"Average of %d numbers is %.2f.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @malloc(i32 1024)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %38
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i64 @fgets(i8* %4, i32 1024, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %30, label %21
17: ; preds = %9
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %14, %21
%22 = phi float [ %26, %21 ], [ 0.000000e+00, %14 ]
%23 = phi i32 [ %27, %21 ], [ 0, %14 ]
%24 = tail call i64 @atof(i8* %4)
%25 = sitofp i64 %24 to float
%26 = fadd float %22, %25
%27 = add nuw nsw i32 %23, 1
%28 = tail call i64 @fgets(i8* %4, i32 1024, i32* %12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %21
30: ; preds = %21, %14
%31 = phi i32 [ 0, %14 ], [ %27, %21 ]
%32 = phi float [ 0.000000e+00, %14 ], [ %26, %21 ]
%33 = tail call i32 @fclose(i32* %12)
%34 = sitofp i32 %31 to float
%35 = fdiv float %32, %34
%36 = fpext float %35 to double
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %31, double %36)
br label %38
38: ; preds = %30, %6
ret i32 0
}