#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 os_elapsed();
int /*<<< orphan*/ ws_poll(int /*<<< orphan*/ *, long);
void display_delay(int t, int slowdown) {
  static unsigned long delay_check = 1000;
  static unsigned long sim_time = 0;
  unsigned long elapsed;
  long delay;
  sim_time += t;
  if (sim_time < delay_check)
    return;
  elapsed = os_elapsed();
  if (elapsed == ~0L) {
    slowdown = 0;
    elapsed = sim_time;
  }
  if (slowdown)
    delay = sim_time - elapsed;
  else
    delay = 0;
  if (elapsed > 100000 || delay > 100000) {
    if (delay_check > 1) {
      delay_check -= delay_check >> 3;
    }
  } else if ((elapsed < 50000) || (slowdown && (delay < 50000))) {
    int gain = delay_check >> 3;
    if (gain == 0)
      gain = 1;
    delay_check += gain;
  }
  if (delay < 0)
    delay = 0;
  ws_poll(NULL, delay);
  sim_time = 0;
}


x86 assembly (clang, -O0):
<display_delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq -0x4(%rbp),%rax
add    0x2f07(%rip),%rax
mov    %rax,0x2f00(%rip)
mov    0x2ef9(%rip),%rax
cmp    0x2ee2(%rip),%rax
jae    1159 <display_delay+0x39>
jmp    1279 <display_delay+0x159>
mov    $0x0,%al
call   1030 <os_elapsed@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1181 <display_delay+0x61>
movl   $0x0,-0x8(%rbp)
mov    0x2ec3(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     119f <display_delay+0x7f>
mov    0x2eae(%rip),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11a7 <display_delay+0x87>
movq   $0x0,-0x18(%rbp)
cmpq   $0x186a0,-0x10(%rbp)
ja     11c3 <display_delay+0xa3>
cmpq   $0x186a0,-0x18(%rbp)
jle    11f2 <display_delay+0xd2>
cmpq   $0x1,0x2e65(%rip)
01
jbe    11ed <display_delay+0xcd>
mov    0x2e58(%rip),%rcx
shr    $0x3,%rcx
mov    0x2e4d(%rip),%rax
sub    %rcx,%rax
mov    %rax,0x2e43(%rip)
jmp    124e <display_delay+0x12e>
cmpq   $0xc350,-0x10(%rbp)
jb     1218 <display_delay+0xf8>
cmpl   $0x0,-0x8(%rbp)
je     1249 <display_delay+0x129>
cmpq   $0xc350,-0x18(%rbp)
jge    1249 <display_delay+0x129>
mov    0x2e11(%rip),%rax
shr    $0x3,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1237 <display_delay+0x117>
movl   $0x1,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
add    0x2dee(%rip),%rax
mov    %rax,0x2de7(%rip)
jmp    124e <display_delay+0x12e>
cmpq   $0x0,-0x18(%rbp)
jge    1261 <display_delay+0x141>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <ws_poll@plt>
movq   $0x0,0x2dc7(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_delay>:
push   %rbp
push   %rbx
push   %rax
movslq %edi,%rax
add    0x2f13(%rip),%rax
mov    %rax,0x2f0c(%rip)
cmp    0x2ef5(%rip),%rax
jb     11f5 <display_delay+0xd5>
mov    %esi,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <os_elapsed@plt>
cmp    $0xffffffffffffffff,%rax
setne  %dl
mov    0x2ee6(%rip),%rsi
cmove  %rsi,%rax
test   %ebp,%ebp
setne  %cl
and    %dl,%cl
sub    %rax,%rsi
test   %cl,%cl
cmovne %rsi,%rbx
cmp    $0x186a0,%rax
ja     11b9 <display_delay+0x99>
cmp    $0x186a1,%rbx
jge    11b9 <display_delay+0x99>
cmp    $0xc350,%rax
jb     1198 <display_delay+0x78>
cmp    $0xc350,%rbx
setl   %al
and    %al,%cl
cmp    $0x1,%cl
jne    11d7 <display_delay+0xb7>
mov    0x2e91(%rip),%rcx
mov    %rcx,%rax
shr    $0x3,%rax
test   %eax,%eax
movslq %eax,%rdx
mov    $0x1,%eax
cmovne %rdx,%rax
add    %rcx,%rax
jmp    11d0 <display_delay+0xb0>
mov    0x2e70(%rip),%rax
cmp    $0x2,%rax
jb     11d7 <display_delay+0xb7>
mov    %rax,%rcx
shr    $0x3,%rcx
sub    %rcx,%rax
mov    %rax,0x2e59(%rip)
xor    %eax,%eax
test   %rbx,%rbx
cmovs  %rax,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <ws_poll@plt>
movq   $0x0,0x2e4b(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@display_delay.delay_check = internal global i64 1000
@display_delay.sim_time = internal global i64 0
define void @display_delay(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = load i64, i64* @display_delay.sim_time
%11 = add i64 %10, %9
store i64 %11, i64* @display_delay.sim_time
%12 = load i64, i64* @display_delay.sim_time
%13 = load i64, i64* @display_delay.delay_check
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %2
br label %74
16: ; preds = %2
%17 = call i64 (...) @os_elapsed()
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
store i32 0, i32* %4
%21 = load i64, i64* @display_delay.sim_time
store i64 %21, i64* %5
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @display_delay.sim_time
%27 = load i64, i64* %5
%28 = sub i64 %26, %27
store i64 %28, i64* %6
br label %30
29: ; preds = %22
store i64 0, i64* %6
br label %30
30: ; preds = %29, %25
%31 = load i64, i64* %5
%32 = icmp ugt i64 %31, 100000
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i64, i64* %6
%35 = icmp sgt i64 %34, 100000
br i1 %35, label %36, label %45
36: ; preds = %33, %30
%37 = load i64, i64* @display_delay.delay_check
%38 = icmp ugt i64 %37, 1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i64, i64* @display_delay.delay_check
%41 = lshr i64 %40, 3
%42 = load i64, i64* @display_delay.delay_check
%43 = sub i64 %42, %41
store i64 %43, i64* @display_delay.delay_check
br label %44
44: ; preds = %39, %36
br label %67
45: ; preds = %33
%46 = load i64, i64* %5
%47 = icmp ult i64 %46, 50000
br i1 %47, label %54, label %48
48: ; preds = %45
%49 = load i32, i32* %4
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %66
51: ; preds = %48
%52 = load i64, i64* %6
%53 = icmp slt i64 %52, 50000
br i1 %53, label %54, label %66
54: ; preds = %51, %45
%55 = load i64, i64* @display_delay.delay_check
%56 = lshr i64 %55, 3
%57 = trunc i64 %56 to i32
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %54
store i32 1, i32* %7
br label %61
61: ; preds = %60, %54
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = load i64, i64* @display_delay.delay_check
%65 = add i64 %64, %63
store i64 %65, i64* @display_delay.delay_check
br label %66
66: ; preds = %61, %51, %48
br label %67
67: ; preds = %66, %44
%68 = load i64, i64* %6
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %67
store i64 0, i64* %6
br label %71
71: ; preds = %70, %67
%72 = load i64, i64* %6
%73 = call i32 @ws_poll(i32* null, i64 %72)
store i64 0, i64* @display_delay.sim_time
br label %74
74: ; preds = %71, %15
ret void
}

LLVM IR (-O2):
@display_delay.delay_check = internal unnamed_addr global i64 1000
@display_delay.sim_time = internal unnamed_addr global i64 0
define void @display_delay(i32 %0, i32 %1) {
%3 = sext i32 %0 to i64
%4 = load i64, i64* @display_delay.sim_time
%5 = add i64 %4, %3
store i64 %5, i64* @display_delay.sim_time
%6 = load i64, i64* @display_delay.delay_check
%7 = icmp ult i64 %5, %6
br i1 %7, label %47, label %8
8: ; preds = %2
%9 = tail call i64 (...) @os_elapsed()
%10 = icmp eq i64 %9, -1
%11 = load i64, i64* @display_delay.sim_time
%12 = select i1 %10, i64 %11, i64 %9
%13 = icmp ne i32 %1, 0
%14 = xor i1 %10, true
%15 = and i1 %13, %14
%16 = sub i64 %11, %12
%17 = select i1 %15, i64 %16, i64 0
%18 = icmp ugt i64 %12, 100000
%19 = icmp sgt i64 %17, 100000
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %21, label %27
21: ; preds = %8
%22 = load i64, i64* @display_delay.delay_check
%23 = icmp ugt i64 %22, 1
br i1 %23, label %24, label %43
24: ; preds = %21
%25 = lshr i64 %22, 3
%26 = sub i64 %22, %25
br label %41
27: ; preds = %8
%28 = icmp ult i64 %12, 50000
br i1 %28, label %32, label %29
29: ; preds = %27
%30 = icmp slt i64 %17, 50000
%31 = select i1 %15, i1 %30, i1 false
br i1 %31, label %32, label %43
32: ; preds = %29, %27
%33 = load i64, i64* @display_delay.delay_check
%34 = lshr i64 %33, 3
%35 = trunc i64 %34 to i32
%36 = icmp eq i32 %35, 0
%37 = shl i64 %34, 32
%38 = ashr exact i64 %37, 32
%39 = select i1 %36, i64 1, i64 %38
%40 = add i64 %39, %33
br label %41
41: ; preds = %24, %32
%42 = phi i64 [ %40, %32 ], [ %26, %24 ]
store i64 %42, i64* @display_delay.delay_check
br label %43
43: ; preds = %41, %29, %21
%44 = icmp sgt i64 %17, 0
%45 = select i1 %44, i64 %17, i64 0
%46 = tail call i32 @ws_poll(i32* null, i64 %45)
store i64 0, i64* @display_delay.sim_time
br label %47
47: ; preds = %2, %43
ret void
}