#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 */
int /*<<< orphan*/ uart_print_hex(unsigned int);
int /*<<< orphan*/ uart_printf(char *);
int /*<<< orphan*/ uart_puts(char *);
int /*<<< orphan*/ uart_send_int(unsigned int);
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;
    uart_printf("\ndest_addr = 0x");
    uart_print_hex(dest);
    uart_printf("  count = ");
    uart_send_int(count);
    uart_puts("");
    (*(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) & 7;
    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
sub    $0x20,%rsp
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    1193 <write_to_boot+0x53>
cmpl   $0x5,-0x10(%rbp)
jae    1193 <write_to_boot+0x53>
mov    -0x4(%rbp),%eax
sub    $0x800,%eax
mov    %eax,-0x4(%rbp)
lea    0xe66(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <uart_print_hex@plt>
lea    0xe62(%rip),%rdi
call   1030 <uart_printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <uart_send_int@plt>
lea    0xe58(%rip),%rdi
call   1060 <uart_puts@plt>
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    $0x7,%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    1249 <write_to_boot+0x109>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1259 <write_to_boot+0x119>
jmp    1297 <write_to_boot+0x157>
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    1284 <write_to_boot+0x144>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    1289 <write_to_boot+0x149>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    122b <write_to_boot+0xeb>
mov    $0x70200008,%eax
movl   $0x10,(%rax)
mov    $0x70200028,%eax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12ba <write_to_boot+0x17a>
jmp    12bf <write_to_boot+0x17f>
jmp    12a2 <write_to_boot+0x162>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jb     12d0 <write_to_boot+0x190>
jmp    12de <write_to_boot+0x19e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1174 <write_to_boot+0x34>
mov    $0x70200004,%eax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    $0x70200004,%eax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_boot>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r13d
andl   $0xfffffffd,0x70200004
fd
xor    %ebp,%ebp
lea    0xeaf(%rip),%r15
lea    0xeb2(%rip),%r12
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r13d,%eax
lea    -0x1(%r14),%ecx
lea    -0x800(%rax),%r13d
cmp    $0x4,%ecx
cmovae %eax,%r13d
lea    0xe74(%rip),%rdi
call   1030 <uart_printf@plt>
mov    %r13d,%edi
call   1050 <uart_print_hex@plt>
mov    %r15,%rdi
call   1030 <uart_printf@plt>
mov    %ebp,%edi
call   1040 <uart_send_int@plt>
mov    %r12,%rdi
call   1060 <uart_puts@plt>
movl   $0x80,0x70200008
movl   $0x0,0x7020000c
movl   $0x0,0x7020000c
mov    %r14,%rcx
movzbl %cl,%eax
mov    %eax,0x7020000c
movzbl %ch,%eax
mov    %eax,0x7020000c
mov    %ecx,%eax
shr    $0x10,%eax
and    $0x7,%eax
mov    %eax,0x7020000c
xor    %ecx,%ecx
cmp    %ebx,%ebp
jae    1240 <write_to_boot+0x100>
nopl   (%rax)
mov    %ebp,%eax
mov    %ecx,%edx
lea    (%rdx,%r13,1),%ecx
mov    (%rcx),%ecx
mov    %ecx,0x70200010
lea    0x4(%rax),%ebp
cmp    $0x800,%edx
cmovae %eax,%ebp
lea    0x4(%rdx),%ecx
cmp    %ebx,%ebp
setb   %al
cmp    $0xffb,%edx
ja     1231 <write_to_boot+0xf1>
cmp    %ebx,%ebp
jb     1200 <write_to_boot+0xc0>
add    %ecx,%r13d
jmp    1242 <write_to_boot+0x102>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
movl   $0x10,0x70200008
nopl   (%rax)
testl  $0x1,0x70200028
je     1250 <write_to_boot+0x110>
add    $0x1,%r14d
test   %al,%al
jne    1170 <write_to_boot+0x30>
orl    $0x2,0x70200004
02
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"\0Adest_addr = 0x\00"
@.str.1 = [11 x i8] c" count = \00"
@.str.2 = [1 x i8] zeroinitializer
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, %79
%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
%21 = call i32 @uart_printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = call i32 @uart_print_hex(i32 %22)
%24 = call i32 @uart_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %5
%26 = call i32 @uart_send_int(i32 %25)
%27 = call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store volatile i32 128, i32* inttoptr (i64 1881145352 to i32*)
%28 = load i32, i32* %7
%29 = and i32 %28, 255
store volatile i32 %29, i32* inttoptr (i64 1881145356 to i32*)
%30 = load i32, i32* %7
%31 = lshr i32 %30, 8
%32 = and i32 %31, 31
store volatile i32 %32, i32* inttoptr (i64 1881145356 to i32*)
%33 = load i32, i32* %6
%34 = and i32 %33, 255
store volatile i32 %34, i32* inttoptr (i64 1881145356 to i32*)
%35 = load i32, i32* %6
%36 = lshr i32 %35, 8
%37 = and i32 %36, 255
store volatile i32 %37, i32* inttoptr (i64 1881145356 to i32*)
%38 = load i32, i32* %6
%39 = lshr i32 %38, 16
%40 = and i32 %39, 7
store volatile i32 %40, i32* inttoptr (i64 1881145356 to i32*)
%41 = load i32, i32* %7
store i32 %41, i32* %8
br label %42
42: ; preds = %64, %20
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 4096
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* %5
%47 = load i32, i32* %4
%48 = icmp ult i32 %46, %47
br label %49
49: ; preds = %45, %42
%50 = phi i1 [ false, %42 ], [ %48, %45 ]
br i1 %50, label %51, label %67
51: ; preds = %49
%52 = load i32, i32* %3
%53 = zext i32 %52 to i64
%54 = inttoptr i64 %53 to i32*
%55 = load i32, i32* %54
store volatile i32 %55, i32* inttoptr (i64 1881145360 to i32*)
%56 = load i32, i32* %3
%57 = add i32 %56, 4
store i32 %57, i32* %3
%58 = load i32, i32* %8
%59 = icmp slt i32 %58, 2048
br i1 %59, label %60, label %63
60: ; preds = %51
%61 = load i32, i32* %5
%62 = add i32 %61, 4
store i32 %62, i32* %5
br label %63
63: ; preds = %60, %51
br label %64
64: ; preds = %63
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 4
store i32 %66, i32* %8
br label %42
67: ; preds = %49
store volatile i32 16, i32* inttoptr (i64 1881145352 to i32*)
br label %68
68: ; preds = %67, %73
%69 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%70 = and i32 %69, 1
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %68
br label %74
73: ; preds = %68
br label %68
74: ; preds = %72
%75 = load i32, i32* %5
%76 = load i32, i32* %4
%77 = icmp uge i32 %75, %76
br i1 %77, label %78, label %79
78: ; preds = %74
br label %82
79: ; preds = %74
%80 = load i32, i32* %6
%81 = add i32 %80, 1
store i32 %81, i32* %6
br label %11
82: ; preds = %78
%83 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%84 = or i32 %83, 2
store volatile i32 %84, i32* inttoptr (i64 1881145348 to i32*)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"\0Adest_addr = 0x\00"
@.str.1 = [11 x i8] c" count = \00"
@.str.2 = [1 x i8] zeroinitializer
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 = %47, %2
%6 = phi i32 [ 0, %2 ], [ %25, %47 ]
%7 = phi i32 [ 0, %2 ], [ %48, %47 ]
%8 = phi i32 [ %0, %2 ], [ %26, %47 ]
%9 = add i32 %7, -1
%10 = icmp ult i32 %9, 4
%11 = add i32 %8, -2048
%12 = select i1 %10, i32 %11, i32 %8
%13 = tail call i32 @uart_printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @uart_print_hex(i32 %12)
%15 = tail call i32 @uart_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @uart_send_int(i32 %6)
%17 = tail call i32 @uart_puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
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*)
%18 = and i32 %7, 255
store volatile i32 %18, i32* inttoptr (i64 1881145356 to i32*)
%19 = lshr i32 %7, 8
%20 = and i32 %19, 255
store volatile i32 %20, i32* inttoptr (i64 1881145356 to i32*)
%21 = lshr i32 %7, 16
%22 = and i32 %21, 7
store volatile i32 %22, i32* inttoptr (i64 1881145356 to i32*)
%23 = icmp ult i32 %6, %1
br i1 %23, label %28, label %24
24: ; preds = %28, %5
%25 = phi i32 [ %6, %5 ], [ %38, %28 ]
%26 = phi i32 [ %12, %5 ], [ %35, %28 ]
%27 = phi i1 [ false, %5 ], [ %41, %28 ]
store volatile i32 16, i32* inttoptr (i64 1881145352 to i32*)
br label %43
28: ; preds = %5, %28
%29 = phi i32 [ %39, %28 ], [ 0, %5 ]
%30 = phi i32 [ %35, %28 ], [ %12, %5 ]
%31 = phi i32 [ %38, %28 ], [ %6, %5 ]
%32 = zext i32 %30 to i64
%33 = inttoptr i64 %32 to i32*
%34 = load i32, i32* %33
store volatile i32 %34, i32* inttoptr (i64 1881145360 to i32*)
%35 = add i32 %30, 4
%36 = icmp ult i32 %29, 2048
%37 = add i32 %31, 4
%38 = select i1 %36, i32 %37, i32 %31
%39 = add nuw nsw i32 %29, 4
%40 = icmp ult i32 %29, 4092
%41 = icmp ult i32 %38, %1
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %28, label %24
43: ; preds = %43, %24
%44 = load volatile i32, i32* inttoptr (i64 1881145384 to i32*)
%45 = and i32 %44, 1
%46 = icmp eq i32 %45, 0
br i1 %46, label %43, label %47
47: ; preds = %43
%48 = add i32 %7, 1
br i1 %27, label %5, label %49
49: ; preds = %47
%50 = load volatile i32, i32* inttoptr (i64 1881145348 to i32*)
%51 = or i32 %50, 2
store volatile i32 %51, i32* inttoptr (i64 1881145348 to i32*)
ret void
}