void write_to_boot(unsigned int dest, unsigned int length) {
  unsigned int count = 0;
  unsigned int page = 0;
  unsigned int column = 0;
  (*(volatile unsigned int *)(0x70200004)) &= ~2;
  while (1) {
    if (page > 0 && page < 5)
      dest -= 2048;
    (*(volatile unsigned int *)(0x70200008)) = 0x80;
    (*(volatile unsigned int *)(0x7020000C)) = column & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (column >> 8) & 0x1F;
    (*(volatile unsigned int *)(0x7020000C)) = page & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (page >> 8) & 0xFF;
    (*(volatile unsigned int *)(0x7020000C)) = (page >> 16) & 0xFF;
    for (int i = column; i < 4096 && count < length; i += 4) {
      (*(volatile unsigned int *)(0x70200010)) = *(unsigned int *)dest;
      dest += 4;
      if (i < 2048)
        count += 4;
    }
    (*(volatile unsigned int *)(0x70200008)) = 0x10;
    while (1) {
      if ((*(volatile unsigned int *)(0x70200028)) & 1)
        break;
    }
    if (count >= length)
      break;
    ++page;
  }
  (*(volatile unsigned int *)(0x70200004)) |= 2;
  return;
}


x86 assembly (clang, -O0):
<write_to_boot>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x70200004,%eax
mov    (%rax),%ecx
and    $0xfffffffd,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
cmpl   $0x0,-0x10(%rbp)
jbe    114f <write_to_boot+0x4f>
cmpl   $0x5,-0x10(%rbp)
jae    114f <write_to_boot+0x4f>
mov    -0x4(%rbp),%eax
sub    $0x800,%eax
mov    %eax,-0x4(%rbp)
mov    $0x70200008,%eax
movl   $0x80,(%rax)
mov    -0x14(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
shr    $0x8,%ecx
and    $0x1f,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
shr    $0x8,%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
shr    $0x10,%ecx
and    $0xff,%ecx
mov    $0x7020000c,%eax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x1000,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jge    11d4 <write_to_boot+0xd4>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11e4 <write_to_boot+0xe4>
jmp    1222 <write_to_boot+0x122>
mov    -0x4(%rbp),%eax
mov    (%rax),%ecx
mov    $0x70200010,%eax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x800,-0x18(%rbp)
jge    120f <write_to_boot+0x10f>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    1214 <write_to_boot+0x114>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    11b6 <write_to_boot+0xb6>
mov    $0x70200008,%eax
movl   $0x10,(%rax)
mov    $0x70200028,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1245 <write_to_boot+0x145>
jmp    124a <write_to_boot+0x14a>
jmp    122d <write_to_boot+0x12d>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jb     125b <write_to_boot+0x15b>
jmp    1269 <write_to_boot+0x169>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1130 <write_to_boot+0x30>
mov    $0x70200004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_boot>:
push   %rbx
andl   $0xfffffffd,0x70200004
fd
xor    %ecx,%ecx
xor    %ebx,%ebx
nopl   (%rax)
mov    %edi,%eax
lea    -0x1(%rbx),%edx
lea    -0x800(%rax),%edi
cmp    $0x4,%edx
cmovae %eax,%edi
movl   $0x80,0x70200008
movl   $0x0,0x7020000c
movl   $0x0,0x7020000c
movzbl %bl,%eax
mov    %eax,0x7020000c
movzbl %bh,%eax
mov    %eax,0x7020000c
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x7020000c
xor    %eax,%eax
cmp    %esi,%ecx
jae    11b0 <write_to_boot+0xb0>
mov    %rbx,%r8
xchg   %ax,%ax
mov    %ecx,%edx
mov    %eax,%ebx
lea    (%rdi,%rbx,1),%eax
mov    (%rax),%eax
mov    %eax,0x70200010
lea    0x4(%rdx),%ecx
cmp    $0x800,%ebx
cmovae %edx,%ecx
lea    0x4(%rbx),%eax
cmp    %esi,%ecx
setb   %dl
cmp    $0xffb,%ebx
ja     11a0 <write_to_boot+0xa0>
cmp    %esi,%ecx
jb     1170 <write_to_boot+0x70>
add    %eax,%edi
mov    %r8,%rbx
jmp    11b2 <write_to_boot+0xb2>
nopw   0x0(%rax,%rax,1)
xor    %edx,%edx
movl   $0x10,0x70200008
nopl   (%rax)
testl  $0x1,0x70200028
je     11c0 <write_to_boot+0xc0>
add    $0x1,%ebx
test   %dl,%dl
jne    1110 <write_to_boot+0x10>
orl    $0x2,0x70200004
02
pop    %rbx
ret

LLVM IR (-O0):
define void @write_to_boot(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%10 = and i32 %9, -3
store volatile i32 %10, i32* inttoptr (i64 1881145348 to i32*)
br label %11
11: ; preds = %2, %72
%12 = load i32, i32* %6
%13 = icmp ugt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load i32, i32* %6
%16 = icmp ult i32 %15, 5
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %3
%19 = sub i32 %18, 2048
store i32 %19, i32* %3
br label %20
20: ; preds = %17, %14, %11
store volatile i32 128, i32* inttoptr (i64 1881145352 to i32*)
%21 = load i32, i32* %7
%22 = and i32 %21, 255
store volatile i32 %22, i32* inttoptr (i64 1881145356 to i32*)
%23 = load i32, i32* %7
%24 = lshr i32 %23, 8
%25 = and i32 %24, 31
store volatile i32 %25, i32* inttoptr (i64 1881145356 to i32*)
%26 = load i32, i32* %6
%27 = and i32 %26, 255
store volatile i32 %27, i32* inttoptr (i64 1881145356 to i32*)
%28 = load i32, i32* %6
%29 = lshr i32 %28, 8
%30 = and i32 %29, 255
store volatile i32 %30, i32* inttoptr (i64 1881145356 to i32*)
%31 = load i32, i32* %6
%32 = lshr i32 %31, 16
%33 = and i32 %32, 255
store volatile i32 %33, i32* inttoptr (i64 1881145356 to i32*)
%34 = load i32, i32* %7
store i32 %34, i32* %8
br label %35
35: ; preds = %57, %20
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 4096
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = icmp ult i32 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %44, label %60
44: ; preds = %42
%45 = load i32, i32* %3
%46 = zext i32 %45 to i64
%47 = inttoptr i64 %46 to i32*
%48 = load i32, i32* %47
store volatile i32 %48, i32* inttoptr (i64 1881145360 to i32*)
%49 = load i32, i32* %3
%50 = add i32 %49, 4
store i32 %50, i32* %3
%51 = load i32, i32* %8
%52 = icmp slt i32 %51, 2048
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i32, i32* %5
%55 = add i32 %54, 4
store i32 %55, i32* %5
br label %56
56: ; preds = %53, %44
br label %57
57: ; preds = %56
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 4
store i32 %59, i32* %8
br label %35
60: ; preds = %42
store volatile i32 16, i32* inttoptr (i64 1881145352 to i32*)
br label %61
61: ; preds = %60, %66
%62 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%63 = and i32 %62, 1
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61
br label %67
66: ; preds = %61
br label %61
67: ; preds = %65
%68 = load i32, i32* %5
%69 = load i32, i32* %4
%70 = icmp uge i32 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %67
br label %75
72: ; preds = %67
%73 = load i32, i32* %6
%74 = add i32 %73, 1
store i32 %74, i32* %6
br label %11
75: ; preds = %71
%76 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%77 = or i32 %76, 2
store volatile i32 %77, i32* inttoptr (i64 1881145348 to i32*)
ret void
}

LLVM IR (-O2):
define void @write_to_boot(i32 %0, i32 %1) {
%3 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%4 = and i32 %3, -3
store volatile i32 %4, i32* inttoptr (i64 1881145348 to i32*)
br label %5
5: ; preds = %42, %2
%6 = phi i32 [ 0, %2 ], [ %20, %42 ]
%7 = phi i32 [ 0, %2 ], [ %43, %42 ]
%8 = phi i32 [ %0, %2 ], [ %21, %42 ]
%9 = add i32 %7, -1
%10 = icmp ult i32 %9, 4
%11 = add i32 %8, -2048
%12 = select i1 %10, i32 %11, i32 %8
store volatile i32 128, i32* inttoptr (i64 1881145352 to i32*)
store volatile i32 0, i32* inttoptr (i64 1881145356 to i32*)
store volatile i32 0, i32* inttoptr (i64 1881145356 to i32*)
%13 = and i32 %7, 255
store volatile i32 %13, i32* inttoptr (i64 1881145356 to i32*)
%14 = lshr i32 %7, 8
%15 = and i32 %14, 255
store volatile i32 %15, i32* inttoptr (i64 1881145356 to i32*)
%16 = lshr i32 %7, 16
%17 = and i32 %16, 255
store volatile i32 %17, i32* inttoptr (i64 1881145356 to i32*)
%18 = icmp ult i32 %6, %1
br i1 %18, label %23, label %19
19: ; preds = %23, %5
%20 = phi i32 [ %6, %5 ], [ %33, %23 ]
%21 = phi i32 [ %12, %5 ], [ %30, %23 ]
%22 = phi i1 [ false, %5 ], [ %36, %23 ]
store volatile i32 16, i32* inttoptr (i64 1881145352 to i32*)
br label %38
23: ; preds = %5, %23
%24 = phi i32 [ %34, %23 ], [ 0, %5 ]
%25 = phi i32 [ %30, %23 ], [ %12, %5 ]
%26 = phi i32 [ %33, %23 ], [ %6, %5 ]
%27 = zext i32 %25 to i64
%28 = inttoptr i64 %27 to i32*
%29 = load i32, i32* %28
store volatile i32 %29, i32* inttoptr (i64 1881145360 to i32*)
%30 = add i32 %25, 4
%31 = icmp ult i32 %24, 2048
%32 = add i32 %26, 4
%33 = select i1 %31, i32 %32, i32 %26
%34 = add nuw nsw i32 %24, 4
%35 = icmp ult i32 %24, 4092
%36 = icmp ult i32 %33, %1
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %23, label %19
38: ; preds = %38, %19
%39 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%40 = and i32 %39, 1
%41 = icmp eq i32 %40, 0
br i1 %41, label %38, label %42
42: ; preds = %38
%43 = add i32 %7, 1
br i1 %22, label %5, label %44
44: ; preds = %42
%45 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%46 = or i32 %45, 2
store volatile i32 %46, i32* inttoptr (i64 1881145348 to i32*)
ret void
}