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

/* Variables and functions */
int readReg(int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ writeReg(int, int);
int performSingleRefCalibration(uint8_t vhv_init_byte) {
  int iTimeout;
  writeReg(0x00, 0x01 | vhv_init_byte);
  iTimeout = 0;
  while ((readReg(0x13) & 0x07) == 0) {
    iTimeout++;
    usleep(5000);
    if (iTimeout > 100) {
      return 0;
    }
  }
  writeReg(0x0B, 0x01);
  writeReg(0x00, 0x00);
  return 1;
}


x86 assembly (clang, -O0):
<performSingleRefCalibration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
or     $0x1,%esi
xor    %edi,%edi
call   1030 <writeReg@plt>
movl   $0x0,-0xc(%rbp)
mov    $0x13,%edi
call   1040 <readReg@plt>
and    $0x7,%eax
cmp    $0x0,%eax
jne    1193 <performSingleRefCalibration+0x63>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1388,%edi
call   1050 <usleep@plt>
cmpl   $0x64,-0xc(%rbp)
jle    118e <performSingleRefCalibration+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <performSingleRefCalibration+0x82>
jmp    114f <performSingleRefCalibration+0x1f>
mov    $0xb,%edi
mov    $0x1,%esi
call   1030 <writeReg@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1030 <writeReg@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<performSingleRefCalibration>:
push   %rbx
mov    %edi,%esi
or     $0x1,%esi
xor    %edi,%edi
call   1030 <writeReg@plt>
mov    $0x65,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x13,%edi
call   1040 <readReg@plt>
test   $0x7,%al
jne    1171 <performSingleRefCalibration+0x41>
mov    $0x1388,%edi
call   1050 <usleep@plt>
add    $0xffffffff,%ebx
jne    1150 <performSingleRefCalibration+0x20>
xor    %ebx,%ebx
jmp    118e <performSingleRefCalibration+0x5e>
mov    $0x1,%ebx
mov    $0xb,%edi
mov    $0x1,%esi
call   1030 <writeReg@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <writeReg@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

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