#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 */
unsigned long Counter();
int /*<<< orphan*/ sleep_milliseconds(unsigned long);
void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) {
  unsigned long actual_counter;
  while (1) {
    actual_counter = Counter();
    if (actual_counter >= *counter_var && actual_counter < *counter_var + delay)
      sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
    else
      break;
  }
  *counter_var = actual_counter;
}


x86 assembly (clang, -O0):
<WaitUntilDelayReached>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <Counter@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%rax
jb     1181 <WaitUntilDelayReached+0x61>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rcx
add    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    1181 <WaitUntilDelayReached+0x61>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    -0x10(%rbp),%rdi
sub    -0x18(%rbp),%rdi
shr    $0x1,%rdi
call   1040 <sleep_milliseconds@plt>
jmp    1186 <WaitUntilDelayReached+0x66>
jmp    118b <WaitUntilDelayReached+0x6b>
jmp    1130 <WaitUntilDelayReached+0x10>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitUntilDelayReached>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <Counter@plt>
mov    (%rbx),%rdi
cmp    %rdi,%rax
jb     1151 <WaitUntilDelayReached+0x31>
add    %r14,%rdi
sub    %rax,%rdi
jbe    1151 <WaitUntilDelayReached+0x31>
shr    %rdi
call   1040 <sleep_milliseconds@plt>
jmp    1130 <WaitUntilDelayReached+0x10>
mov    %rax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @WaitUntilDelayReached(i64* %0, i64 %1) {
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
store i64* %0, i64** %3
store i64 %1, i64* %4
br label %6
6: ; preds = %2, %29
%7 = call i64 (...) @Counter()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = load i64*, i64** %3
%10 = load i64, i64* %9
%11 = icmp uge i64 %8, %10
br i1 %11, label %12, label %28
12: ; preds = %6
%13 = load i64, i64* %5
%14 = load i64*, i64** %3
%15 = load i64, i64* %14
%16 = load i64, i64* %4
%17 = add i64 %15, %16
%18 = icmp ult i64 %13, %17
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i64*, i64** %3
%21 = load i64, i64* %20
%22 = load i64, i64* %4
%23 = add i64 %21, %22
%24 = load i64, i64* %5
%25 = sub i64 %23, %24
%26 = udiv i64 %25, 2
%27 = call i32 @sleep_milliseconds(i64 %26)
br label %29
28: ; preds = %12, %6
br label %30
29: ; preds = %19
br label %6
30: ; preds = %28
%31 = load i64, i64* %5
%32 = load i64*, i64** %3
store i64 %31, i64* %32
ret void
}

LLVM IR (-O2):
define void @WaitUntilDelayReached(i64* nocapture %0, i64 %1) {
%3 = tail call i64 (...) @Counter()
%4 = load i64, i64* %0
%5 = icmp ult i64 %3, %4
br i1 %5, label %18, label %6
6: ; preds = %2, %11
%7 = phi i64 [ %16, %11 ], [ %4, %2 ]
%8 = phi i64 [ %15, %11 ], [ %3, %2 ]
%9 = add i64 %7, %1
%10 = icmp ugt i64 %9, %8
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = sub i64 %9, %8
%13 = lshr i64 %12, 1
%14 = tail call i32 @sleep_milliseconds(i64 %13)
%15 = tail call i64 (...) @Counter()
%16 = load i64, i64* %0
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %6
18: ; preds = %6, %11, %2
%19 = phi i64 [ %3, %2 ], [ %15, %11 ], [ %8, %6 ]
store i64 %19, i64* %0
ret void
}