int main() {
  unsigned char *vmem = (unsigned char *)0x91000000;
  volatile unsigned int *volatile iesr =
      (unsigned int *)(0x80000000U + (4 * 2));
  printf("Startup. Waiting for first vsync.\n");
  waitVsync();
  printf("Filling the screen red and waiting for vsync.\n");
  fillScreen(vmem, 0xff, 0, 0);
  waitVsync();
  printf("Filling the screen green and waiting for vsync.\n");
  fillScreen(vmem, 0, 0xff, 0);
  waitVsync();
  printf("Test done, looping.\n");
  unsigned int n = 0;
  while (1) {
    unsigned int reg = *iesr;
    if (!(reg & (1 << 0))) {
      printf("vsync pulse inactive after %d tries (0x%08x)\n", n, reg);
      n = 0;
      while (!(*iesr & (1 << 0)))
        ;
    } else
      n++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x91000000,%eax
mov    %rax,-0x10(%rbp)
mov    $0x80000008,%eax
mov    %rax,-0x18(%rbp)
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <waitVsync@plt>
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xff,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   1030 <fillScreen@plt>
mov    $0x0,%al
call   1050 <waitVsync@plt>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %ecx,%ecx
mov    $0xff,%edx
mov    %ecx,%esi
mov    $0x0,%al
call   1030 <fillScreen@plt>
mov    $0x0,%al
call   1050 <waitVsync@plt>
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1228 <main+0xf8>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121e <main+0xee>
jmp    1223 <main+0xf3>
jmp    1200 <main+0xd0>
jmp    1231 <main+0x101>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cd <main+0x9d>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    $0x91000000,%eax
add    $0xffffffffef000008,%rax
mov    %rax,0x8(%rsp)
lea    0xed2(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <waitVsync@plt>
lea    0xee1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x91000000,%edi
mov    $0xff,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1030 <fillScreen@plt>
xor    %eax,%eax
call   1060 <waitVsync@plt>
lea    0xee7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x91000000,%edi
xor    %esi,%esi
mov    $0xff,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1030 <fillScreen@plt>
xor    %eax,%eax
call   1060 <waitVsync@plt>
lea    0xeef(%rip),%rdi
call   1040 <puts@plt>
lea    0xe35(%rip),%rbx
nopl   0x0(%rax,%rax,1)
xor    %esi,%esi
mov    0x8(%rsp),%rax
mov    (%rax),%edx
test   $0x1,%dl
je     11f0 <main+0xb0>
add    $0x1,%esi
jmp    11d2 <main+0x92>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
testl  $0x1,(%rax)
je     1200 <main+0xc0>
jmp    11d0 <main+0x90>

LLVM IR (-O0):
@.str = [35 x i8] c"Startup. Waiting for first vsync.\0A\00"
@.str.1 = [47 x i8] c"Filling the screen red and waiting for vsync.\0A\00"
@.str.2 = [49 x i8] c"Filling the screen green and waiting for vsync.\0A\00"
@.str.3 = [21 x i8] c"Test done, looping.\0A\00"
@.str.4 = [46 x i8] c"vsync pulse inactive after %d tries (0x%08x)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i8* inttoptr (i64 2432696320 to i8*), i8** %2
store volatile i32* inttoptr (i64 2147483656 to i32*), i32** %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @waitVsync()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%9 = load i8*, i8** %2
%10 = call i32 (i8*, i32, i32, i32, ...) bitcast (i32 (...)* @fillScreen to i32 (i8*, i32, i32, i32, ...)*)(i8* %9, i32 255, i32 0, i32 0)
%11 = call i32 (...) @waitVsync()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = call i32 (i8*, i32, i32, i32, ...) bitcast (i32 (...)* @fillScreen to i32 (i8*, i32, i32, i32, ...)*)(i8* %13, i32 0, i32 255, i32 0)
%15 = call i32 (...) @waitVsync()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %17
17: ; preds = %0, %38
%18 = load volatile i32*, i32** %3
%19 = load volatile i32, i32* %18
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = and i32 %20, 1
%22 = icmp ne i32 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %25)
store i32 0, i32* %4
br label %27
27: ; preds = %33, %23
%28 = load volatile i32*, i32** %3
%29 = load volatile i32, i32* %28
%30 = and i32 %29, 1
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
br i1 %32, label %33, label %34
33: ; preds = %27
br label %27
34: ; preds = %27
br label %38
35: ; preds = %17
%36 = load i32, i32* %4
%37 = add i32 %36, 1
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %34
br label %17
}

LLVM IR (-O2):
@.str.4 = [46 x i8] c"vsync pulse inactive after %d tries (0x%08x)\0A\00"
@str = [34 x i8] c"Startup. Waiting for first vsync.\00"
@str.5 = [46 x i8] c"Filling the screen red and waiting for vsync.\00"
@str.6 = [48 x i8] c"Filling the screen green and waiting for vsync.\00"
@str.7 = [20 x i8] c"Test done, looping.\00"
define i32 @main() {
%1 = alloca i32*
%2 = bitcast i32** %1 to i8*
store volatile i32* inttoptr (i64 2147483656 to i32*), i32** %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @waitVsync()
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.5, i64 0, i64 0))
%6 = tail call i32 (i8*, i32, i32, i32, ...) bitcast (i32 (...)* @fillScreen to i32 (i8*, i32, i32, i32, ...)*)(i8* inttoptr (i64 2432696320 to i8*), i32 255, i32 0, i32 0)
%7 = tail call i32 (...) @waitVsync()
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
%9 = tail call i32 (i8*, i32, i32, i32, ...) bitcast (i32 (...)* @fillScreen to i32 (i8*, i32, i32, i32, ...)*)(i8* inttoptr (i64 2432696320 to i8*), i32 0, i32 255, i32 0)
%10 = tail call i32 (...) @waitVsync()
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %12
12: ; preds = %27, %0
%13 = phi i32 [ 0, %0 ], [ %28, %27 ]
%14 = load volatile i32*, i32** %1
%15 = load volatile i32, i32* %14
%16 = and i32 %15, 1
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %13, i32 %15)
br label %20
20: ; preds = %20, %18
%21 = load volatile i32*, i32** %1
%22 = load volatile i32, i32* %21
%23 = and i32 %22, 1
%24 = icmp eq i32 %23, 0
br i1 %24, label %20, label %27
25: ; preds = %12
%26 = add i32 %13, 1
br label %27
27: ; preds = %20, %25
%28 = phi i32 [ %26, %25 ], [ 0, %20 ]
br label %12
}