#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 const resistance_from_adc(unsigned int, unsigned int const);
unsigned int find_maximum_adc(const double resistance_max,
                              const unsigned int reference_resistance,
                              const unsigned int adc_min) {
  unsigned int adc = 32767;
  while (adc > 0 &&
         (resistance_from_adc(adc, reference_resistance) > resistance_max)) {
    --adc;
  }
  if (!adc) {
    ++adc;
  }
  if (adc <= adc_min) {
    adc = adc_min + 1;
  }
  return adc;
}


x86 assembly (clang, -O0):
<find_maximum_adc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movl   $0x7fff,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jbe    114f <find_maximum_adc+0x3f>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <resistance_from_adc@plt>
ucomisd -0x8(%rbp),%xmm0
seta   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    115f <find_maximum_adc+0x4f>
jmp    116d <find_maximum_adc+0x5d>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    112a <find_maximum_adc+0x1a>
cmpl   $0x0,-0x14(%rbp)
jne    1180 <find_maximum_adc+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
ja     1195 <find_maximum_adc+0x85>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_maximum_adc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
movsd  %xmm0,(%rsp)
mov    $0x8000,%ebp
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%ebx
mov    %ebx,%edi
mov    %r15d,%esi
call   1030 <resistance_from_adc@plt>
ucomisd (%rsp),%xmm0
jbe    114b <find_maximum_adc+0x3b>
cmp    $0x2,%ebp
mov    %ebx,%ebp
jne    1130 <find_maximum_adc+0x20>
lea    0x1(%r14),%eax
cmp    %r14d,%ebx
cmova  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_maximum_adc(double %0, i32 %1, i32 %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 32767, i32* %7
br label %8
8: ; preds = %19, %3
%9 = load i32, i32* %7
%10 = icmp ugt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = call double @resistance_from_adc(i32 %12, i32 %13)
%15 = load double, double* %4
%16 = fcmp ogt double %14, %15
br label %17
17: ; preds = %11, %8
%18 = phi i1 [ false, %8 ], [ %16, %11 ]
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i32, i32* %7
%21 = add i32 %20, -1
store i32 %21, i32* %7
br label %8
22: ; preds = %17
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %7
%27 = add i32 %26, 1
store i32 %27, i32* %7
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = icmp ule i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %6
%34 = add i32 %33, 1
store i32 %34, i32* %7
br label %35
35: ; preds = %32, %28
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
define i32 @find_maximum_adc(double %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i32 [ 32767, %3 ], [ %8, %4 ]
%6 = tail call double @resistance_from_adc(i32 %5, i32 %1)
%7 = fcmp ule double %6, %0
%8 = add nsw i32 %5, -1
%9 = icmp eq i32 %8, 0
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %11, label %4
11: ; preds = %4
%12 = icmp ugt i32 %5, %2
%13 = add i32 %2, 1
%14 = select i1 %12, i32 %5, i32 %13
ret i32 %14
}