void BLInternalFlashProgram(unsigned long ulDstAddr, unsigned char *pucSrcData,
                            unsigned long ulLength) {
  unsigned long ulLoop;
  for (ulLoop = 0; ulLoop < ulLength; ulLoop += 4) {
    (*((volatile unsigned long *)(0x400FD000))) = ulDstAddr + ulLoop;
    (*((volatile unsigned long *)(0x400FD004))) =
        *(unsigned long *)(pucSrcData + ulLoop);
    (*((volatile unsigned long *)(0x400FD008))) = 0xA4420000 | 0x00000001;
    while ((*((volatile unsigned long *)(0x400FD008))) & 0x00000001) {
    }
  }
}


x86 assembly (clang, -O0):
<BLInternalFlashProgram>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1188 <BLInternalFlashProgram+0x88>
mov    -0x8(%rbp),%rcx
add    -0x20(%rbp),%rcx
mov    $0x400fd000,%eax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%rcx
mov    $0x400fd004,%eax
mov    %rcx,(%rax)
mov    $0x400fd008,%eax
mov    $0xa4420001,%ecx
mov    %rcx,(%rax)
mov    $0x400fd008,%eax
mov    (%rax),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1172 <BLInternalFlashProgram+0x72>
jmp    1157 <BLInternalFlashProgram+0x57>
jmp    1177 <BLInternalFlashProgram+0x77>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    1118 <BLInternalFlashProgram+0x18>
pop    %rbp
ret

x86 assembly (clang, -O2):
<BLInternalFlashProgram>:
test   %rdx,%rdx
je     1146 <BLInternalFlashProgram+0x46>
xor    %eax,%eax
mov    $0xa4420001,%r8d
nopl   (%rax)
lea    (%rax,%rdi,1),%rcx
mov    %rcx,0x400fd000
40
mov    (%rsi,%rax,1),%rcx
mov    %rcx,0x400fd004
40
mov    %r8,0x400fd008
40
mov    0x400fd008,%rcx
40
test   $0x1,%cl
jne    1130 <BLInternalFlashProgram+0x30>
add    $0x4,%rax
cmp    %rdx,%rax
jb     1110 <BLInternalFlashProgram+0x10>
ret

LLVM IR (-O0):
define void @BLInternalFlashProgram(i64 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %27, %3
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = load i64, i64* %4
%14 = load i64, i64* %7
%15 = add i64 %13, %14
store volatile i64 %15, i64* inttoptr (i64 1074778112 to i64*)
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = bitcast i8* %18 to i64*
%20 = load i64, i64* %19
store volatile i64 %20, i64* inttoptr (i64 1074778116 to i64*)
store volatile i64 2755788801, i64* inttoptr (i64 1074778120 to i64*)
br label %21
21: ; preds = %25, %12
%22 = load volatile i64, i64* inttoptr (i64 1074778120 to i64*)
%23 = and i64 %22, 1
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %21
26: ; preds = %21
br label %27
27: ; preds = %26
%28 = load i64, i64* %7
%29 = add i64 %28, 4
store i64 %29, i64* %7
br label %8
30: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @BLInternalFlashProgram(i64 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3, %15
%6 = phi i64 [ %16, %15 ], [ 0, %3 ]
%7 = add i64 %6, %0
store volatile i64 %7, i64* inttoptr (i64 1074778112 to i64*)
%8 = getelementptr inbounds i8, i8* %1, i64 %6
%9 = bitcast i8* %8 to i64*
%10 = load i64, i64* %9
store volatile i64 %10, i64* inttoptr (i64 1074778116 to i64*)
store volatile i64 2755788801, i64* inttoptr (i64 1074778120 to i64*)
br label %11
11: ; preds = %11, %5
%12 = load volatile i64, i64* inttoptr (i64 1074778120 to i64*)
%13 = and i64 %12, 1
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %11
15: ; preds = %11
%16 = add i64 %6, 4
%17 = icmp ult i64 %16, %2
br i1 %17, label %5, label %18
18: ; preds = %15, %3
ret void
}