#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(long);
void delaygame(long usec) {
  static long extratime[3] = {0, 0, 0};
  struct timeval start, end, diff;
  long extradelay, delay;
  int i;
  extradelay = (extratime[0] + extratime[1] + extratime[2]) / 3;
  if (usec > extradelay) {
    gettimeofday(&start, NULL);
    usleep(usec - extradelay);
    gettimeofday(&end, NULL);
    diff.tv_sec = end.tv_sec - start.tv_sec;
    diff.tv_usec = end.tv_usec - start.tv_usec;
    if (diff.tv_usec < 0) {
      diff.tv_sec--;
      diff.tv_usec += 1000000;
    }
    delay = diff.tv_sec * 1000000 + diff.tv_usec;
    extratime[0] = extratime[1];
    extratime[1] = extratime[2];
    extratime[2] = delay - (usec - extradelay);
  } else
    for (i = 0; i < 3; i++)
      extratime[i] = (extratime[i] * 9) / 10;
}


x86 assembly (clang, -O0):
<delaygame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2f0d(%rip),%rax
add    0x2f0e(%rip),%rax
add    0x2f0f(%rip),%rax
mov    $0x3,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x28(%rbp),%rax
jle    11fb <delaygame+0xdb>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rdi
sub    -0x28(%rbp),%rdi
call   1040 <usleep@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b4 <delaygame+0x94>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x1c(%rbp)
imul   $0xf4240,-0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
cltq
mov    %rax,-0x30(%rbp)
mov    0x2e7d(%rip),%rax
mov    %rax,0x2e6e(%rip)
mov    0x2e77(%rip),%rax
mov    %rax,0x2e68(%rip)
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,0x2e5a(%rip)
jmp    124b <delaygame+0x12b>
movl   $0x0,-0x34(%rbp)
cmpl   $0x3,-0x34(%rbp)
jge    1246 <delaygame+0x126>
movslq -0x34(%rbp),%rcx
lea    0x2e29(%rip),%rax
imul   $0x9,(%rax,%rcx,8),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,%rdx
movslq -0x34(%rbp),%rcx
lea    0x2e0c(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1202 <delaygame+0xe2>
jmp    124b <delaygame+0x12b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delaygame>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2f12(%rip),%rsi
mov    0x2f1b(%rip),%rcx
lea    (%rcx,%rsi,1),%rax
mov    0x2f20(%rip),%r8
add    %r8,%rax
movabs $0x5555555555555556,%rdx
55 55 55
imul   %rdx
mov    %rdx,%rbx
shr    $0x3f,%rbx
add    %rdx,%rbx
cmp    %rdi,%rbx
jge    11d3 <delaygame+0xb3>
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
sub    %rbx,%r14
mov    %r14,%rdi
call   1040 <usleep@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
sub    0x10(%rsp),%eax
sub    0x14(%rsp),%ecx
lea    0xf4240(%rcx),%edx
mov    %ecx,%esi
sar    $0x1f,%esi
add    %eax,%esi
test   %ecx,%ecx
cmovns %ecx,%edx
imul   $0xf4240,%esi,%eax
add    %edx,%eax
movslq %eax,%rdx
mov    0x2e97(%rip),%rax
mov    %rax,0x2e80(%rip)
mov    0x2e99(%rip),%rax
mov    %rax,0x2e82(%rip)
sub    %r14,%rdx
jmp    122d <delaygame+0x10d>
lea    (%rsi,%rsi,8),%rax
movabs $0x6666666666666667,%rsi
66 66 66
imul   %rsi
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
mov    %rdx,0x2e47(%rip)
lea    (%rcx,%rcx,8),%rax
imul   %rsi
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rcx
add    %rax,%rcx
lea    (%r8,%r8,8),%rax
imul   %rsi
mov    %rcx,0x2e31(%rip)
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
mov    %rdx,0x2e2c(%rip)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@delaygame.extratime = internal global [3 x i64] zeroinitializer
define void @delaygame(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i64 %0, i64* %2
%9 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 0)
%10 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 1)
%11 = add nsw i64 %9, %10
%12 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 2)
%13 = add nsw i64 %11, %12
%14 = sdiv i64 %13, 3
store i64 %14, i64* %6
%15 = load i64, i64* %2
%16 = load i64, i64* %6
%17 = icmp sgt i64 %15, %16
br i1 %17, label %18, label %62
18: ; preds = %1
%19 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%20 = load i64, i64* %2
%21 = load i64, i64* %6
%22 = sub nsw i64 %20, %21
%23 = call i32 @usleep(i64 %22)
%24 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sub nsw i32 %26, %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%34 = load i32, i32* %33
%35 = sub nsw i32 %32, %34
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%38 = load i32, i32* %37
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %18
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%42 = load i32, i32* %41
%43 = add nsw i32 %42, -1
store i32 %43, i32* %41
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%45 = load i32, i32* %44
%46 = add nsw i32 %45, 1000000
store i32 %46, i32* %44
br label %47
47: ; preds = %40, %18
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%49 = load i32, i32* %48
%50 = mul nsw i32 %49, 1000000
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%52 = load i32, i32* %51
%53 = add nsw i32 %50, %52
%54 = sext i32 %53 to i64
store i64 %54, i64* %7
%55 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 1)
store i64 %55, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 0)
%56 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 2)
store i64 %56, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 1)
%57 = load i64, i64* %7
%58 = load i64, i64* %2
%59 = load i64, i64* %6
%60 = sub nsw i64 %58, %59
%61 = sub nsw i64 %57, %60
store i64 %61, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 2)
br label %80
62: ; preds = %1
store i32 0, i32* %8
br label %63
63: ; preds = %76, %62
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 3
br i1 %65, label %66, label %79
66: ; preds = %63
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 %68
%70 = load i64, i64* %69
%71 = mul nsw i64 %70, 9
%72 = sdiv i64 %71, 10
%73 = load i32, i32* %8
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [3 x i64], [3 x i64]* @delaygame.extratime, i64 0, i64 %74
store i64 %72, i64* %75
br label %76
76: ; preds = %66
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
store i32 %78, i32* %8
br label %63
79: ; preds = %63
br label %80
80: ; preds = %79, %47
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@delaygame.extratime.0 = internal unnamed_addr global i64 0
@delaygame.extratime.1 = internal unnamed_addr global i64 0
@delaygame.extratime.2 = internal unnamed_addr global i64 0
define void @delaygame(i64 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = load i64, i64* @delaygame.extratime.0
%7 = load i64, i64* @delaygame.extratime.1
%8 = add nsw i64 %7, %6
%9 = load i64, i64* @delaygame.extratime.2
%10 = add nsw i64 %8, %9
%11 = sdiv i64 %10, 3
%12 = icmp slt i64 %11, %0
br i1 %12, label %20, label %13
13: ; preds = %1
%14 = mul nsw i64 %6, 9
%15 = sdiv i64 %14, 10
store i64 %15, i64* @delaygame.extratime.0
%16 = mul nsw i64 %7, 9
%17 = sdiv i64 %16, 10
store i64 %17, i64* @delaygame.extratime.1
%18 = mul nsw i64 %9, 9
%19 = sdiv i64 %18, 10
br label %46
20: ; preds = %1
%21 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%22 = sub nsw i64 %0, %11
%23 = tail call i32 @usleep(i64 %22)
%24 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%28 = load i32, i32* %27
%29 = sub i32 %26, %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%33 = load i32, i32* %32
%34 = sub nsw i32 %31, %33
%35 = icmp slt i32 %34, 0
%36 = add nsw i32 %34, 1000000
%37 = ashr i32 %34, 31
%38 = add nsw i32 %29, %37
%39 = select i1 %35, i32 %36, i32 %34
%40 = mul nsw i32 %38, 1000000
%41 = add nsw i32 %40, %39
%42 = sext i32 %41 to i64
%43 = load i64, i64* @delaygame.extratime.1
store i64 %43, i64* @delaygame.extratime.0
%44 = load i64, i64* @delaygame.extratime.2
store i64 %44, i64* @delaygame.extratime.1
%45 = sub nsw i64 %42, %22
br label %46
46: ; preds = %13, %20
%47 = phi i64 [ %19, %13 ], [ %45, %20 ]
store i64 %47, i64* @delaygame.extratime.2
ret void
}