#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 uint16_t;

/* Variables and functions */
int readReg(int);
int readReg16(int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ writeReg(int, int);
uint16_t readRangeContinuousMillimeters(void) {
  int iTimeout = 0;
  uint16_t range;
  while ((readReg(0x13) & 0x07) == 0) {
    iTimeout++;
    usleep(5000);
    if (iTimeout > 50) {
      return -1;
    }
  }
  range = readReg16(0x14 + 10);
  writeReg(0x0B, 0x01);
  return range;
}


x86 assembly (clang, -O0):
<readRangeContinuousMillimeters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x13,%edi
call   1050 <readReg@plt>
and    $0x7,%eax
cmp    $0x0,%eax
jne    1193 <readRangeContinuousMillimeters+0x53>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x1388,%edi
call   1060 <usleep@plt>
cmpl   $0x32,-0x8(%rbp)
jle    118e <readRangeContinuousMillimeters+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <readRangeContinuousMillimeters+0x75>
jmp    114f <readRangeContinuousMillimeters+0xf>
mov    $0x1e,%edi
call   1030 <readReg16@plt>
mov    %eax,-0xc(%rbp)
mov    $0xb,%edi
mov    $0x1,%esi
call   1040 <writeReg@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readRangeContinuousMillimeters>:
push   %rbx
mov    $0x33,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0x13,%edi
call   1050 <readReg@plt>
test   $0x7,%al
jne    1174 <readRangeContinuousMillimeters+0x34>
mov    $0x1388,%edi
call   1060 <usleep@plt>
add    $0xffffffff,%ebx
jne    1150 <readRangeContinuousMillimeters+0x10>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    $0x1e,%edi
call   1030 <readReg16@plt>
mov    %eax,%ebx
mov    $0xb,%edi
mov    $0x1,%esi
call   1040 <writeReg@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @readRangeContinuousMillimeters() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %2
br label %4
4: ; preds = %15, %0
%5 = call i32 @readReg(i32 19)
%6 = and i32 %5, 7
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %4
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
store i32 %10, i32* %2
%11 = call i32 @usleep(i32 5000)
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 50
br i1 %13, label %14, label %15
14: ; preds = %8
store i32 -1, i32* %1
br label %20
15: ; preds = %8
br label %4
16: ; preds = %4
%17 = call i32 @readReg16(i32 30)
store i32 %17, i32* %3
%18 = call i32 @writeReg(i32 11, i32 1)
%19 = load i32, i32* %3
store i32 %19, i32* %1
br label %20
20: ; preds = %16, %14
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
define i32 @readRangeContinuousMillimeters() {
br label %1
1: ; preds = %6, %0
%2 = phi i32 [ 0, %0 ], [ %7, %6 ]
%3 = tail call i32 @readReg(i32 19)
%4 = and i32 %3, 7
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = add nuw nsw i32 %2, 1
%8 = tail call i32 @usleep(i32 5000)
%9 = icmp eq i32 %7, 51
br i1 %9, label %13, label %1
10: ; preds = %1
%11 = tail call i32 @readReg16(i32 30)
%12 = tail call i32 @writeReg(i32 11, i32 1)
br label %13
13: ; preds = %6, %10
%14 = phi i32 [ %11, %10 ], [ -1, %6 ]
ret i32 %14
}