#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 */

/* Variables and functions */
double CalculateTimerRate(int, char);
scalar_t__ I2CWrapperReadByte(int, int);
int /*<<< orphan*/ I2CWrapperWriteByte(int, int, unsigned char);
int /*<<< orphan*/ I2CWrapperWriteWord(int, int, int);
scalar_t__ fabs(double);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void AdjustOscillator(int handle) {
  signed char Best;
  double BestDelta;
  double CurrentDelta;
  signed char osc;
  double Target = 5000.0;
  printf("\n--------------- Adjust Oscillator\n");
  Best = (char)I2CWrapperReadByte(handle, 8);
  osc = Best;
  I2CWrapperWriteByte(handle, 8, (unsigned char)Best);
  BestDelta = CalculateTimerRate(handle, osc) - Target;
  if (BestDelta > 0.0) {
    do {
      osc--;
      if (osc < (-32))
        break;
      CurrentDelta = CalculateTimerRate(handle, osc) - Target;
      if (fabs(BestDelta) < fabs(CurrentDelta))
        break;
      BestDelta = CurrentDelta;
      Best = osc;
    } while (1);
  } else {
    do {
      osc++;
      if (osc > 31)
        break;
      CurrentDelta = CalculateTimerRate(handle, osc) - Target;
      if (fabs(BestDelta) < fabs(CurrentDelta))
        break;
      BestDelta = CurrentDelta;
      Best = osc;
    } while (1);
  }
  printf("Best  osctune = %d    frequency %.0f %+.3f%%\n", Best, Target,
         BestDelta * 100.0 / 5000.0);
  fflush(stdout);
  I2CWrapperWriteByte(handle, 8, (unsigned char)Best);
  I2CWrapperWriteWord(handle, 9, 0xAA55);
}


x86 assembly (clang, -O0):
<AdjustOscillator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movsd  0xe7d(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
call   1050 <I2CWrapperReadByte@plt>
mov    %al,-0x5(%rbp)
mov    -0x5(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
movzbl -0x5(%rbp),%edx
call   1090 <I2CWrapperWriteByte@plt>
mov    -0x4(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1080 <CalculateTimerRate@plt>
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    126b <AdjustOscillator+0xfb>
jmp    11ea <AdjustOscillator+0x7a>
mov    -0x19(%rbp),%al
add    $0xff,%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0xffffffe0,%eax
jge    1204 <AdjustOscillator+0x94>
jmp    1266 <AdjustOscillator+0xf6>
mov    -0x4(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1080 <CalculateTimerRate@plt>
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <fabs@plt>
mov    %rax,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1060 <fabs@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jge    1247 <AdjustOscillator+0xd7>
jmp    1266 <AdjustOscillator+0xf6>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x5(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    11ea <AdjustOscillator+0x7a>
jmp    1266 <AdjustOscillator+0xf6>
jmp    12f1 <AdjustOscillator+0x181>
jmp    1270 <AdjustOscillator+0x100>
mov    -0x19(%rbp),%al
add    $0x1,%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x1f,%eax
jle    128a <AdjustOscillator+0x11a>
jmp    12ec <AdjustOscillator+0x17c>
mov    -0x4(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1080 <CalculateTimerRate@plt>
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <fabs@plt>
mov    %rax,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1060 <fabs@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jge    12cd <AdjustOscillator+0x15d>
jmp    12ec <AdjustOscillator+0x17c>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x5(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    1270 <AdjustOscillator+0x100>
jmp    12ec <AdjustOscillator+0x17c>
jmp    12f1 <AdjustOscillator+0x181>
movsbl -0x5(%rbp),%esi
movsd  -0x28(%rbp),%xmm0
movsd  0xd06(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
movsd  0xcf1(%rip),%xmm2
divsd  %xmm2,%xmm1
lea    0xd1a(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
movzbl -0x5(%rbp),%edx
call   1090 <I2CWrapperWriteByte@plt>
mov    -0x4(%rbp),%edi
mov    $0x9,%esi
mov    $0xaa55,%edx
call   1030 <I2CWrapperWriteWord@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AdjustOscillator>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    $0x8,%esi
call   1060 <I2CWrapperReadByte@plt>
mov    %rax,%rbx
movzbl %bl,%r14d
mov    %r15d,%edi
mov    $0x8,%esi
mov    %r14d,%edx
call   10a0 <I2CWrapperWriteByte@plt>
movsbl %r14b,%esi
mov    %r15d,%edi
call   1090 <CalculateTimerRate@plt>
addsd  0xe2f(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1240 <AdjustOscillator+0xc0>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ebp
movsd  %xmm0,0x10(%rsp)
lea    -0x1(%rbp),%ebx
cmp    $0xe0,%bl
jl     128d <AdjustOscillator+0x10d>
movsbl %bl,%esi
mov    %r15d,%edi
call   1090 <CalculateTimerRate@plt>
addsd  0xdf9(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
call   1070 <fabs@plt>
mov    %rax,%r14
movsd  0x8(%rsp),%xmm0
call   1070 <fabs@plt>
movsd  0x8(%rsp),%xmm0
cmp    %rax,%r14
jge    11e0 <AdjustOscillator+0x60>
jmp    128d <AdjustOscillator+0x10d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%ebp
movsd  %xmm0,0x10(%rsp)
lea    0x1(%rbp),%ebx
cmp    $0x1f,%bl
jg     128d <AdjustOscillator+0x10d>
movsbl %bl,%esi
mov    %r15d,%edi
call   1090 <CalculateTimerRate@plt>
addsd  0xd9d(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
call   1070 <fabs@plt>
mov    %rax,%r14
movsd  0x8(%rsp),%xmm0
call   1070 <fabs@plt>
movsd  0x8(%rsp),%xmm0
cmp    %rax,%r14
jge    1240 <AdjustOscillator+0xc0>
movsbl %bpl,%ebx
movsd  0x10(%rsp),%xmm1
mulsd  0xd69(%rip),%xmm1
movsd  0xd69(%rip),%xmm0
divsd  %xmm0,%xmm1
lea    0xd66(%rip),%rdi
mov    %ebx,%esi
mov    $0x2,%al
call   1050 <printf@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movzbl %bl,%edx
mov    %r15d,%edi
mov    $0x8,%esi
call   10a0 <I2CWrapperWriteByte@plt>
mov    %r15d,%edi
mov    $0x9,%esi
mov    $0xaa55,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <I2CWrapperWriteWord@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"\0A--------------- Adjust Oscillator\0A\00"
@.str.1 = [46 x i8] c"Best osctune = %d frequency %.0f %+.3f%%\0A\00"
@stdout = global i32 0
define void @AdjustOscillator(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca double
%5 = alloca double
%6 = alloca i8
%7 = alloca double
store i32 %0, i32* %2
store double 5.000000e+03, double* %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = call i64 @I2CWrapperReadByte(i32 %9, i32 8)
%11 = trunc i64 %10 to i8
store i8 %11, i8* %3
%12 = load i8, i8* %3
store i8 %12, i8* %6
%13 = load i32, i32* %2
%14 = load i8, i8* %3
%15 = call i32 @I2CWrapperWriteByte(i32 %13, i32 8, i8 zeroext %14)
%16 = load i32, i32* %2
%17 = load i8, i8* %6
%18 = call double @CalculateTimerRate(i32 %16, i8 signext %17)
%19 = load double, double* %7
%20 = fsub double %18, %19
store double %20, double* %4
%21 = load double, double* %4
%22 = fcmp ogt double %21, 0.000000e+00
br i1 %22, label %23, label %48
23: ; preds = %1
br label %24
24: ; preds = %46, %23
%25 = load i8, i8* %6
%26 = add i8 %25, -1
store i8 %26, i8* %6
%27 = load i8, i8* %6
%28 = sext i8 %27 to i32
%29 = icmp slt i32 %28, -32
br i1 %29, label %30, label %31
30: ; preds = %24
br label %47
31: ; preds = %24
%32 = load i32, i32* %2
%33 = load i8, i8* %6
%34 = call double @CalculateTimerRate(i32 %32, i8 signext %33)
%35 = load double, double* %7
%36 = fsub double %34, %35
store double %36, double* %5
%37 = load double, double* %4
%38 = call i64 @fabs(double %37)
%39 = load double, double* %5
%40 = call i64 @fabs(double %39)
%41 = icmp slt i64 %38, %40
br i1 %41, label %42, label %43
42: ; preds = %31
br label %47
43: ; preds = %31
%44 = load double, double* %5
store double %44, double* %4
%45 = load i8, i8* %6
store i8 %45, i8* %3
br label %46
46: ; preds = %43
br i1 true, label %24, label %47
47: ; preds = %46, %42, %30
br label %73
48: ; preds = %1
br label %49
49: ; preds = %71, %48
%50 = load i8, i8* %6
%51 = add i8 %50, 1
store i8 %51, i8* %6
%52 = load i8, i8* %6
%53 = sext i8 %52 to i32
%54 = icmp sgt i32 %53, 31
br i1 %54, label %55, label %56
55: ; preds = %49
br label %72
56: ; preds = %49
%57 = load i32, i32* %2
%58 = load i8, i8* %6
%59 = call double @CalculateTimerRate(i32 %57, i8 signext %58)
%60 = load double, double* %7
%61 = fsub double %59, %60
store double %61, double* %5
%62 = load double, double* %4
%63 = call i64 @fabs(double %62)
%64 = load double, double* %5
%65 = call i64 @fabs(double %64)
%66 = icmp slt i64 %63, %65
br i1 %66, label %67, label %68
67: ; preds = %56
br label %72
68: ; preds = %56
%69 = load double, double* %5
store double %69, double* %4
%70 = load i8, i8* %6
store i8 %70, i8* %3
br label %71
71: ; preds = %68
br i1 true, label %49, label %72
72: ; preds = %71, %67, %55
br label %73
73: ; preds = %72, %47
%74 = load i8, i8* %3
%75 = sext i8 %74 to i32
%76 = load double, double* %7
%77 = load double, double* %4
%78 = fmul double %77, 1.000000e+02
%79 = fdiv double %78, 5.000000e+03
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %75, double %76, double %79)
%81 = load i32, i32* @stdout
%82 = call i32 @fflush(i32 %81)
%83 = load i32, i32* %2
%84 = load i8, i8* %3
%85 = call i32 @I2CWrapperWriteByte(i32 %83, i32 8, i8 zeroext %84)
%86 = load i32, i32* %2
%87 = call i32 @I2CWrapperWriteWord(i32 %86, i32 9, i32 43605)
ret void
}

LLVM IR (-O2):
@.str.1 = [46 x i8] c"Best osctune = %d frequency %.0f %+.3f%%\0A\00"
@stdout = global i32 0
@str = [35 x i8] c"\0A--------------- Adjust Oscillator\00"
define void @AdjustOscillator(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 @I2CWrapperReadByte(i32 %0, i32 8)
%4 = trunc i64 %3 to i8
%5 = tail call i32 @I2CWrapperWriteByte(i32 %0, i32 8, i8 zeroext %4)
%6 = tail call double @CalculateTimerRate(i32 %0, i8 signext %4)
%7 = fadd double %6, -5.000000e+03
%8 = fcmp ogt double %7, 0.000000e+00
br i1 %8, label %9, label %20
9: ; preds = %1, %14
%10 = phi double [ %16, %14 ], [ %7, %1 ]
%11 = phi i8 [ %12, %14 ], [ %4, %1 ]
%12 = add i8 %11, -1
%13 = icmp slt i8 %12, -32
br i1 %13, label %31, label %14
14: ; preds = %9
%15 = tail call double @CalculateTimerRate(i32 %0, i8 signext %12)
%16 = fadd double %15, -5.000000e+03
%17 = tail call i64 @fabs(double %10)
%18 = tail call i64 @fabs(double %16)
%19 = icmp slt i64 %17, %18
br i1 %19, label %31, label %9
20: ; preds = %1, %25
%21 = phi double [ %27, %25 ], [ %7, %1 ]
%22 = phi i8 [ %23, %25 ], [ %4, %1 ]
%23 = add i8 %22, 1
%24 = icmp sgt i8 %23, 31
br i1 %24, label %31, label %25
25: ; preds = %20
%26 = tail call double @CalculateTimerRate(i32 %0, i8 signext %23)
%27 = fadd double %26, -5.000000e+03
%28 = tail call i64 @fabs(double %21)
%29 = tail call i64 @fabs(double %27)
%30 = icmp slt i64 %28, %29
br i1 %30, label %31, label %20
31: ; preds = %20, %25, %9, %14
%32 = phi double [ %10, %14 ], [ %10, %9 ], [ %21, %25 ], [ %21, %20 ]
%33 = phi i8 [ %11, %14 ], [ %11, %9 ], [ %22, %25 ], [ %22, %20 ]
%34 = sext i8 %33 to i32
%35 = fmul double %32, 1.000000e+02
%36 = fdiv double %35, 5.000000e+03
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %34, double 5.000000e+03, double %36)
%38 = load i32, i32* @stdout
%39 = tail call i32 @fflush(i32 %38)
%40 = tail call i32 @I2CWrapperWriteByte(i32 %0, i32 8, i8 zeroext %33)
%41 = tail call i32 @I2CWrapperWriteWord(i32 %0, i32 9, i32 43605)
ret void
}