#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*/ fscanf(int /*<<< orphan*/ *, char *, ...);
double nextSample(FILE *file, int sampleSize, int numChan) {
  unsigned char b;
  short w;
  long l;
  int i;
  double res = 0;
  for (i = 0; i < numChan; i++) {
    switch (sampleSize) {
    case 1:
      fscanf(file, "%1c", &b);
      res += ((int)b) - 0x80;
      break;
    case 2:
      fscanf(file, "%2c", &w);
      res += w >> 8;
      break;
    case 3:
      fscanf(file, "%3c", &l);
      res += w >> 16;
      break;
    case 4:
      fscanf(file, "%4c", &l);
      res += w >> 24;
      break;
    }
  }
  return res / numChan;
}


x86 assembly (clang, -O0):
<nextSample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1236 <nextSample+0x126>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     1223 <nextSample+0x113>
mov    -0x38(%rbp),%rax
lea    0xea3(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rdi
lea    0xe9f(%rip),%rsi
lea    -0x11(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
movzbl -0x11(%rbp),%eax
sub    $0x80,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1223 <nextSample+0x113>
mov    -0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
movswl -0x14(%rbp),%eax
sar    $0x8,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1223 <nextSample+0x113>
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
movswl -0x14(%rbp),%eax
sar    $0x10,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1223 <nextSample+0x113>
mov    -0x8(%rbp),%rdi
lea    0xe19(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
movswl -0x14(%rbp),%eax
sar    $0x18,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1228 <nextSample+0x118>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <nextSample+0x21>
movsd  -0x30(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextSample>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
test   %edx,%edx
jle    120f <nextSample+0xff>
mov    %esi,%ebx
mov    %rdi,%rbp
xorpd  %xmm0,%xmm0
add    $0xffffffff,%ebx
lea    0xec4(%rip),%r12
lea    0x10(%rsp),%r13
mov    %r15d,%r14d
jmp    1187 <nextSample+0x77>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xec2(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1030 <fscanf@plt>
movsd  0x8(%rsp),%xmm0
movswl 0x4(%rsp),%eax
sar    $0xf,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm0
add    $0xffffffff,%r14d
je     1213 <nextSample+0x103>
cmp    $0x3,%ebx
ja     117d <nextSample+0x6d>
movslq (%r12,%rbx,4),%rax
add    %r12,%rax
movsd  %xmm0,0x8(%rsp)
jmp    *%rax
mov    %rbp,%rdi
lea    0xe6b(%rip),%rsi
lea    0x7(%rsp),%rdx
xor    %eax,%eax
call   1030 <fscanf@plt>
movsd  0x8(%rsp),%xmm0
movzbl 0x7(%rsp),%eax
add    $0xffffff80,%eax
jmp    1172 <nextSample+0x62>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xe3a(%rip),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1030 <fscanf@plt>
movsd  0x8(%rsp),%xmm0
movsbl 0x5(%rsp),%eax
jmp    1172 <nextSample+0x62>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xe0e(%rip),%rsi
jmp    115a <nextSample+0x4a>
xorpd  %xmm0,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %r15d,%xmm1
divsd  %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%1c\00"
@.str.1 = [4 x i8] c"%2c\00"
@.str.2 = [4 x i8] c"%3c\00"
@.str.3 = [4 x i8] c"%4c\00"
define double @nextSample(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i16
%9 = alloca i64
%10 = alloca i32
%11 = alloca double
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %11
store i32 0, i32* %10
br label %12
12: ; preds = %55, %3
%13 = load i32, i32* %10
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %58
16: ; preds = %12
%17 = load i32, i32* %5
switch i32 %17, label %54 [
i32 1, label %18
i32 2, label %27
i32 3, label %36
i32 4, label %45
]
18: ; preds = %16
%19 = load i32*, i32** %4
%20 = call i32 (i32*, i8*, ...) @fscanf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %7)
%21 = load i8, i8* %7
%22 = zext i8 %21 to i32
%23 = sub nsw i32 %22, 128
%24 = sitofp i32 %23 to double
%25 = load double, double* %11
%26 = fadd double %25, %24
store double %26, double* %11
br label %54
27: ; preds = %16
%28 = load i32*, i32** %4
%29 = call i32 (i32*, i8*, ...) @fscanf(i32* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %8)
%30 = load i16, i16* %8
%31 = sext i16 %30 to i32
%32 = ashr i32 %31, 8
%33 = sitofp i32 %32 to double
%34 = load double, double* %11
%35 = fadd double %34, %33
store double %35, double* %11
br label %54
36: ; preds = %16
%37 = load i32*, i32** %4
%38 = call i32 (i32*, i8*, ...) @fscanf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %9)
%39 = load i16, i16* %8
%40 = sext i16 %39 to i32
%41 = ashr i32 %40, 16
%42 = sitofp i32 %41 to double
%43 = load double, double* %11
%44 = fadd double %43, %42
store double %44, double* %11
br label %54
45: ; preds = %16
%46 = load i32*, i32** %4
%47 = call i32 (i32*, i8*, ...) @fscanf(i32* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %9)
%48 = load i16, i16* %8
%49 = sext i16 %48 to i32
%50 = ashr i32 %49, 24
%51 = sitofp i32 %50 to double
%52 = load double, double* %11
%53 = fadd double %52, %51
store double %53, double* %11
br label %54
54: ; preds = %16, %45, %36, %27, %18
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %12
58: ; preds = %12
%59 = load double, double* %11
%60 = load i32, i32* %6
%61 = sitofp i32 %60 to double
%62 = fdiv double %59, %61
ret double %62
}

LLVM IR (-O2):
@.str = [4 x i8] c"%1c\00"
@.str.1 = [4 x i8] c"%2c\00"
@.str.2 = [4 x i8] c"%3c\00"
@.str.3 = [4 x i8] c"%4c\00"
define double @nextSample(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca i8
%5 = alloca i16
%6 = alloca i64
%7 = bitcast i16* %5 to i8*
%8 = bitcast i64* %6 to i8*
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %42
10: ; preds = %3, %38
%11 = phi double [ %39, %38 ], [ 0.000000e+00, %3 ]
%12 = phi i32 [ %40, %38 ], [ 0, %3 ]
switch i32 %1, label %38 [
i32 1, label %13
i32 2, label %20
i32 3, label %26
i32 4, label %32
]
13: ; preds = %10
%14 = call i32 (i32*, i8*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %4)
%15 = load i8, i8* %4
%16 = zext i8 %15 to i32
%17 = add nsw i32 %16, -128
%18 = sitofp i32 %17 to double
%19 = fadd double %11, %18
br label %38
20: ; preds = %10
%21 = call i32 (i32*, i8*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %5)
%22 = load i16, i16* %5
%23 = ashr i16 %22, 8
%24 = sitofp i16 %23 to double
%25 = fadd double %11, %24
br label %38
26: ; preds = %10
%27 = call i32 (i32*, i8*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %6)
%28 = load i16, i16* %5
%29 = ashr i16 %28, 15
%30 = sitofp i16 %29 to double
%31 = fadd double %11, %30
br label %38
32: ; preds = %10
%33 = call i32 (i32*, i8*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %6)
%34 = load i16, i16* %5
%35 = ashr i16 %34, 15
%36 = sitofp i16 %35 to double
%37 = fadd double %11, %36
br label %38
38: ; preds = %13, %20, %26, %32, %10
%39 = phi double [ %11, %10 ], [ %37, %32 ], [ %31, %26 ], [ %25, %20 ], [ %19, %13 ]
%40 = add nuw nsw i32 %12, 1
%41 = icmp eq i32 %40, %2
br i1 %41, label %42, label %10
42: ; preds = %38, %3
%43 = phi double [ 0.000000e+00, %3 ], [ %39, %38 ]
%44 = sitofp i32 %2 to double
%45 = fdiv double %43, %44
ret double %45
}