#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 timespec {
  long tv_nsec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int get_mcp3008_channels(int *, int, struct timespec *);
int /*<<< orphan*/ printf(char *, long, ...);
int /*<<< orphan*/ spiClose();
scalar_t__ spiOpen(char *);
int main(int argc, char **argv) {
  struct timespec tp, start, nanotime;
  int nsamples = 100000;
  int v[8];
  int i;
  long usecs = 0;
  long nsecs = 0;
  int secs = 0;
  int badxfers = 0;
  if (spiOpen("/dev/spidev0.0") < 0) {
    return -1;
  }
  clock_gettime(CLOCK_REALTIME, &tp);
  start.tv_nsec = tp.tv_nsec;
  start.tv_sec = tp.tv_sec;
  for (i = 0; i < nsamples; i++) {
    badxfers = get_mcp3008_channels(v, 8, &nanotime);
    if (badxfers)
      printf("There were %d bad SPI transfer requests before getting data..\n",
             badxfers);
    secs = (nanotime.tv_sec - start.tv_sec);
    nsecs = (nanotime.tv_nsec - start.tv_nsec);
    usecs = nsecs / 1000 + 1000000 * secs;
    printf("%ld %d %d %d %d %d %d %d %d\n", usecs, v[0], v[1], v[2], v[3], v[4],
           v[5], v[6], v[7]);
  }
  double frequency = ((double)nsamples * 1000000) / ((double)usecs);
  printf("DeltaT  %ld usecs at frequency %f Hz\n", usecs / nsamples, frequency);
  spiClose();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0xb8,%rsp
movl   $0x0,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x186a0,-0x4c(%rbp)
movq   $0x0,-0x80(%rbp)
movq   $0x0,-0x88(%rbp)
movl   $0x0,-0x8c(%rbp)
movl   $0x0,-0x90(%rbp)
lea    0xe69(%rip),%rdi
call   1050 <spiOpen@plt>
cmp    $0x0,%rax
jge    11ba <main+0x6a>
movl   $0xffffffff,-0xc(%rbp)
jmp    130b <main+0x1bb>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    12b4 <main+0x164>
lea    -0x70(%rbp),%rdi
mov    $0x8,%esi
lea    -0x48(%rbp),%rdx
call   1070 <get_mcp3008_channels@plt>
mov    %eax,-0x90(%rbp)
cmpl   $0x0,-0x90(%rbp)
je     1227 <main+0xd7>
movslq -0x90(%rbp),%rsi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x48(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0xf4240,-0x8c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rsi
mov    -0x70(%rbp),%edx
mov    -0x6c(%rbp),%ecx
mov    -0x68(%rbp),%r8d
mov    -0x64(%rbp),%r9d
mov    -0x60(%rbp),%ebx
mov    -0x5c(%rbp),%r11d
mov    -0x58(%rbp),%r10d
mov    -0x54(%rbp),%eax
lea    0xdc8(%rip),%rdi
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11e1 <main+0x91>
cvtsi2sdl -0x4c(%rbp),%xmm0
movsd  0xd3f(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdq -0x80(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
mov    -0x80(%rbp),%rax
movslq -0x4c(%rbp),%rcx
cqto
idiv   %rcx
mov    %rax,%rsi
movsd  -0x98(%rbp),%xmm0
ff
lea    0xd7d(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <spiClose@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xb8,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
lea    0xea3(%rip),%rdi
call   1050 <spiOpen@plt>
test   %rax,%rax
js     1293 <main+0x143>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0x38(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x38(%rsp),%r15
movslq 0x40(%rsp),%r12
mov    $0x186a0,%r14d
jmp    121b <main+0xcb>
nopl   0x0(%rax,%rax,1)
movslq 0x30(%rsp),%rcx
sub    %r12,%rcx
mov    0x28(%rsp),%rax
sub    %r15,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
imul   $0xf4240,%rcx,%rbx
add    %rdx,%rbx
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
mov    0xc(%rsp),%r9d
mov    0x10(%rsp),%r10d
mov    0x14(%rsp),%r11d
mov    0x18(%rsp),%ebp
mov    0x1c(%rsp),%r13d
lea    0xe56(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
push   %r13
push   %rbp
push   %r11
push   %r10
call   1040 <printf@plt>
add    $0x20,%rsp
add    $0xffffffff,%r14d
je     124b <main+0xfb>
mov    %rsp,%rdi
mov    $0x8,%esi
lea    0x28(%rsp),%rdx
call   1070 <get_mcp3008_channels@plt>
test   %eax,%eax
je     11a0 <main+0x50>
movslq %eax,%rsi
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a0 <main+0x50>
cvtsi2sd %rbx,%xmm1
movsd  0xda8(%rip),%xmm0
movabs $0x29f16b11c6d1e109,%rcx
6b f1 29
mov    %rbx,%rax
imul   %rcx
divsd  %xmm1,%xmm0
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0xe,%rdx
add    %rdx,%rsi
lea    0xdf2(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <spiClose@plt>
jmp    1298 <main+0x148>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@.str = [15 x i8] c"/dev/spidev0.0\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [63 x i8] c"There were %d bad SPI transfer requests before getting data..\0A\00"
@.str.2 = [29 x i8] c"%ld %d %d %d %d %d %d %d %d\0A\00"
@.str.3 = [38 x i8] c"DeltaT %ld usecs at frequency %f Hz\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca [8 x i32]
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
%16 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 100000, i32* %9
store i64 0, i64* %12
store i64 0, i64* %13
store i32 0, i32* %14
store i32 0, i32* %15
%17 = call i64 @spiOpen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %94
20: ; preds = %2
%21 = load i32, i32* @CLOCK_REALTIME
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %6)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
store i64 %24, i64* %25
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
store i32 %27, i32* %28
store i32 0, i32* %11
br label %29
29: ; preds = %77, %20
%30 = load i32, i32* %11
%31 = load i32, i32* %9
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %80
33: ; preds = %29
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 0
%35 = call i32 @get_mcp3008_channels(i32* %34, i32 8, %struct.timespec* %8)
store i32 %35, i32* %15
%36 = load i32, i32* %15
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* %15
%40 = sext i32 %39 to i64
%41 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i64 %40)
br label %42
42: ; preds = %38, %33
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%46 = load i32, i32* %45
%47 = sub nsw i32 %44, %46
store i32 %47, i32* %14
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%51 = load i64, i64* %50
%52 = sub nsw i64 %49, %51
store i64 %52, i64* %13
%53 = load i64, i64* %13
%54 = sdiv i64 %53, 1000
%55 = load i32, i32* %14
%56 = mul nsw i32 1000000, %55
%57 = sext i32 %56 to i64
%58 = add nsw i64 %54, %57
store i64 %58, i64* %12
%59 = load i64, i64* %12
%60 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 0
%61 = load i32, i32* %60
%62 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 1
%63 = load i32, i32* %62
%64 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 2
%65 = load i32, i32* %64
%66 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 3
%67 = load i32, i32* %66
%68 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 4
%69 = load i32, i32* %68
%70 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 5
%71 = load i32, i32* %70
%72 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 6
%73 = load i32, i32* %72
%74 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 7
%75 = load i32, i32* %74
%76 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %59, i32 %61, i32 %63, i32 %65, i32 %67, i32 %69, i32 %71, i32 %73, i32 %75)
br label %77
77: ; preds = %42
%78 = load i32, i32* %11
%79 = add nsw i32 %78, 1
store i32 %79, i32* %11
br label %29
80: ; preds = %29
%81 = load i32, i32* %9
%82 = sitofp i32 %81 to double
%83 = fmul double %82, 1.000000e+06
%84 = load i64, i64* %12
%85 = sitofp i64 %84 to double
%86 = fdiv double %83, %85
store double %86, double* %16
%87 = load i64, i64* %12
%88 = load i32, i32* %9
%89 = sext i32 %88 to i64
%90 = sdiv i64 %87, %89
%91 = load double, double* %16
%92 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %90, double %91)
%93 = call i32 (...) @spiClose()
store i32 0, i32* %3
br label %94
94: ; preds = %80, %19
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@.str = [15 x i8] c"/dev/spidev0.0\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [63 x i8] c"There were %d bad SPI transfer requests before getting data..\0A\00"
@.str.2 = [29 x i8] c"%ld %d %d %d %d %d %d %d %d\0A\00"
@.str.3 = [38 x i8] c"DeltaT %ld usecs at frequency %f Hz\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca [8 x i32]
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = bitcast [8 x i32]* %5 to i8*
%9 = tail call i64 @spiOpen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = icmp slt i64 %9, 0
br i1 %10, label %61, label %11
11: ; preds = %2
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %3)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%21 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 1
%22 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 2
%23 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 3
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 4
%25 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 5
%26 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
%27 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 7
br label %28
28: ; preds = %11, %35
%29 = phi i32 [ 0, %11 ], [ %53, %35 ]
%30 = call i32 @get_mcp3008_channels(i32* %18, i32 8, %struct.timespec* %4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = sext i32 %30 to i64
%34 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i64 %33)
br label %35
35: ; preds = %32, %28
%36 = load i32, i32* %19
%37 = sub nsw i32 %36, %17
%38 = load i64, i64* %20
%39 = sub nsw i64 %38, %15
%40 = sdiv i64 %39, 1000
%41 = mul nsw i32 %37, 1000000
%42 = sext i32 %41 to i64
%43 = add nsw i64 %40, %42
%44 = load i32, i32* %18
%45 = load i32, i32* %21
%46 = load i32, i32* %22
%47 = load i32, i32* %23
%48 = load i32, i32* %24
%49 = load i32, i32* %25
%50 = load i32, i32* %26
%51 = load i32, i32* %27
%52 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %43, i32 %44, i32 %45, i32 %46, i32 %47, i32 %48, i32 %49, i32 %50, i32 %51)
%53 = add nuw nsw i32 %29, 1
%54 = icmp eq i32 %53, 100000
br i1 %54, label %55, label %28
55: ; preds = %35
%56 = sitofp i64 %43 to double
%57 = fdiv double 1.000000e+11, %56
%58 = sdiv i64 %43, 100000
%59 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %58, double %57)
%60 = call i32 (...) @spiClose()
br label %61
61: ; preds = %2, %55
%62 = phi i32 [ 0, %55 ], [ -1, %2 ]
ret i32 %62
}