#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 */
struct timeval {
  double tv_sec;
  double tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ _IONBF;
int adc_open();
double adc_read_voltage(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, double, double, double);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  setvbuf(stdout, NULL, _IONBF, 0);
  if (adc_open() != 1) {
    exit(1);
  }
  struct timeval t1, t2;
  double elapsedTime;
  double samplearray[100000];
  gettimeofday(&t1, NULL);
  int x;
  for (x = 0; x < 100000; x++) {
    samplearray[x] = adc_read_voltage(1, 0);
  }
  gettimeofday(&t2, NULL);
  double average = 0;
  double sum = 0;
  for (x = 0; x < 100000; x++) {
    sum += samplearray[x];
  }
  average = sum / 100000;
  elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
  elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  double samplerate = (100000 / elapsedTime) * 1000;
  printf("%d samples in %G ms.\nThe sample rate was %G samples per second\nThe "
         "average voltage was %Gv",
         100000, elapsedTime, samplerate, average);
  (void)argc;
  (void)argv;
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc3560,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1060 <setvbuf@plt>
mov    $0x0,%al
call   1050 <adc_open@plt>
cmp    $0x1,%eax
je     11b0 <main+0x50>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0xc3544(%rbp)
cmpl   $0x186a0,-0xc3544(%rbp)
jge    1207 <main+0xa7>
mov    $0x1,%edi
xor    %esi,%esi
call   1080 <adc_read_voltage@plt>
movslq -0xc3544(%rbp),%rax
movsd  %xmm0,-0xc3540(%rbp,%rax,8)
f3 ff
mov    -0xc3544(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc3544(%rbp)
jmp    11c7 <main+0x67>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0xc3550(%rbp)
ff
xorps  %xmm0,%xmm0
movsd  %xmm0,-0xc3558(%rbp)
ff
movl   $0x0,-0xc3544(%rbp)
cmpl   $0x186a0,-0xc3544(%rbp)
jge    1278 <main+0x118>
movslq -0xc3544(%rbp),%rax
movsd  -0xc3540(%rbp,%rax,8),%xmm0
f3 ff
addsd  -0xc3558(%rbp),%xmm0
ff
movsd  %xmm0,-0xc3558(%rbp)
ff
mov    -0xc3544(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc3544(%rbp)
jmp    1234 <main+0xd4>
movsd  -0xc3558(%rbp),%xmm0
ff
movsd  0xd80(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0xc3550(%rbp)
ff
movsd  -0x30(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xd5a(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  0xd3f(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xd31(%rip),%xmm0
divsd  -0x38(%rbp),%xmm0
movsd  0xd1c(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0xc3560(%rbp)
ff
movsd  -0x38(%rbp),%xmm0
movsd  -0xc3560(%rbp),%xmm1
ff
movsd  -0xc3550(%rbp),%xmm2
ff
lea    0xd04(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0xc3560,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0xc3520,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <setvbuf@plt>
xor    %eax,%eax
call   1050 <adc_open@plt>
cmp    $0x1,%eax
jne    1286 <main+0x126>
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
xor    %esi,%esi
call   1080 <adc_read_voltage@plt>
movsd  %xmm0,0x20(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x186a0,%rbx
jne    11b0 <main+0x50>
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
xorpd  %xmm2,%xmm2
mov    $0x7,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
addsd  -0x18(%rsp,%rax,8),%xmm2
addsd  -0x10(%rsp,%rax,8),%xmm2
addsd  -0x8(%rsp,%rax,8),%xmm2
addsd  (%rsp,%rax,8),%xmm2
addsd  0x8(%rsp,%rax,8),%xmm2
addsd  0x10(%rsp,%rax,8),%xmm2
addsd  0x18(%rsp,%rax,8),%xmm2
addsd  0x20(%rsp,%rax,8),%xmm2
add    $0x8,%rax
cmp    $0x186a7,%rax
jne    11f0 <main+0x90>
movsd  0xdcd(%rip),%xmm1
divsd  %xmm1,%xmm2
movsd  (%rsp),%xmm3
movsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm3
movsd  0xdb8(%rip),%xmm4
mulsd  %xmm4,%xmm3
subsd  0x18(%rsp),%xmm0
divsd  %xmm4,%xmm0
addsd  %xmm3,%xmm0
divsd  %xmm0,%xmm1
mulsd  %xmm4,%xmm1
lea    0xd9f(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0xc3520,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { double, double }
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [91 x i8] c"%d samples in %G ms.\0AThe sample rate was %G samples per second\0AThe average voltage was %Gv\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca double
%9 = alloca [100000 x double]
%10 = alloca i32
%11 = alloca double
%12 = alloca double
%13 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @stdout
%15 = load i32, i32* @_IONBF
%16 = call i32 @setvbuf(i32 %14, i32* null, i32 %15, i32 0)
%17 = call i32 (...) @adc_open()
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
store i32 0, i32* %10
br label %23
23: ; preds = %31, %21
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 100000
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = call double @adc_read_voltage(i32 1, i32 0)
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [100000 x double], [100000 x double]* %9, i64 0, i64 %29
store double %27, double* %30
br label %31
31: ; preds = %26
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %23
34: ; preds = %23
%35 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
store double 0.000000e+00, double* %11
store double 0.000000e+00, double* %12
store i32 0, i32* %10
br label %36
36: ; preds = %46, %34
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 100000
br i1 %38, label %39, label %49
39: ; preds = %36
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [100000 x double], [100000 x double]* %9, i64 0, i64 %41
%43 = load double, double* %42
%44 = load double, double* %12
%45 = fadd double %44, %43
store double %45, double* %12
br label %46
46: ; preds = %39
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %36
49: ; preds = %36
%50 = load double, double* %12
%51 = fdiv double %50, 1.000000e+05
store double %51, double* %11
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%53 = load double, double* %52
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%55 = load double, double* %54
%56 = fsub double %53, %55
%57 = fmul double %56, 1.000000e+03
store double %57, double* %8
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%59 = load double, double* %58
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%61 = load double, double* %60
%62 = fsub double %59, %61
%63 = fdiv double %62, 1.000000e+03
%64 = load double, double* %8
%65 = fadd double %64, %63
store double %65, double* %8
%66 = load double, double* %8
%67 = fdiv double 1.000000e+05, %66
%68 = fmul double %67, 1.000000e+03
store double %68, double* %13
%69 = load double, double* %8
%70 = load double, double* %13
%71 = load double, double* %11
%72 = call i32 @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i32 100000, double %69, double %70, double %71)
%73 = load i32, i32* %4
%74 = load i8**, i8*** %5
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [91 x i8] c"%d samples in %G ms.\0AThe sample rate was %G samples per second\0AThe average voltage was %Gv\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca [100000 x double]
%6 = load i32, i32* @stdout
%7 = load i32, i32* @_IONBF
%8 = tail call i32 @setvbuf(i32 %6, i32* null, i32 %7, i32 0)
%9 = tail call i32 (...) @adc_open()
%10 = icmp eq i32 %9, 1
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = bitcast %struct.timeval* %3 to i8*
%15 = bitcast %struct.timeval* %4 to i8*
%16 = bitcast [100000 x double]* %5 to i8*
%17 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
br label %18
18: ; preds = %13, %18
%19 = phi i64 [ 0, %13 ], [ %22, %18 ]
%20 = tail call double @adc_read_voltage(i32 1, i32 0)
%21 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %19
store double %20, double* %21
%22 = add nuw nsw i64 %19, 1
%23 = icmp eq i64 %22, 100000
br i1 %23, label %24, label %18
24: ; preds = %18
%25 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %26
26: ; preds = %26, %24
%27 = phi i64 [ 0, %24 ], [ %60, %26 ]
%28 = phi double [ 0.000000e+00, %24 ], [ %59, %26 ]
%29 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %27
%30 = load double, double* %29
%31 = fadd double %28, %30
%32 = or i64 %27, 1
%33 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %32
%34 = load double, double* %33
%35 = fadd double %31, %34
%36 = or i64 %27, 2
%37 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %36
%38 = load double, double* %37
%39 = fadd double %35, %38
%40 = or i64 %27, 3
%41 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %40
%42 = load double, double* %41
%43 = fadd double %39, %42
%44 = or i64 %27, 4
%45 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %44
%46 = load double, double* %45
%47 = fadd double %43, %46
%48 = or i64 %27, 5
%49 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %48
%50 = load double, double* %49
%51 = fadd double %47, %50
%52 = or i64 %27, 6
%53 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %52
%54 = load double, double* %53
%55 = fadd double %51, %54
%56 = or i64 %27, 7
%57 = getelementptr inbounds [100000 x double], [100000 x double]* %5, i64 0, i64 %56
%58 = load double, double* %57
%59 = fadd double %55, %58
%60 = add nuw nsw i64 %27, 8
%61 = icmp eq i64 %60, 100000
br i1 %61, label %62, label %26
62: ; preds = %26
%63 = fdiv double %59, 1.000000e+05
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%65 = load double, double* %64
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%67 = load double, double* %66
%68 = fsub double %65, %67
%69 = fmul double %68, 1.000000e+03
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%71 = load double, double* %70
%72 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%73 = load double, double* %72
%74 = fsub double %71, %73
%75 = fdiv double %74, 1.000000e+03
%76 = fadd double %69, %75
%77 = fdiv double 1.000000e+05, %76
%78 = fmul double %77, 1.000000e+03
%79 = tail call i32 @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i32 100000, double %76, double %78, double %63)
ret i32 0
}