#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*/ putch(char);
int /*<<< orphan*/ putstr(char *);
int /*<<< orphan*/ spi_flash_erase(unsigned int);
int /*<<< orphan*/ spi_flash_write_page(void *, unsigned int);
void spi_flash_program(void *from, unsigned int addr, unsigned int len) {
  unsigned int i, j;
  j = addr + len;
  putstr("Erasing: ");
  for (i = addr; (i < j); i += (64 * 1024)) {
    spi_flash_erase(i);
    putch('.');
  }
  putch('\n');
  putstr("Programming: ");
  for (i = addr; (i < j); i += 256) {
    spi_flash_write_page(from, i);
    from += 256;
    putch('.');
  }
  putch('\n');
}


x86 assembly (clang, -O0):
<spi_flash_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xe9e(%rip),%rdi
call   1040 <putstr@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    119b <spi_flash_program+0x5b>
mov    -0x14(%rbp),%edi
call   1050 <spi_flash_erase@plt>
mov    $0x2e,%edi
call   1060 <putch@plt>
mov    -0x14(%rbp),%eax
add    $0x10000,%eax
mov    %eax,-0x14(%rbp)
jmp    116d <spi_flash_program+0x2d>
mov    $0xa,%edi
call   1060 <putch@plt>
lea    0xe5e(%rip),%rdi
call   1040 <putstr@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    11f7 <spi_flash_program+0xb7>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <spi_flash_write_page@plt>
mov    -0x8(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x8(%rbp)
mov    $0x2e,%edi
call   1060 <putch@plt>
mov    -0x14(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x14(%rbp)
jmp    11b7 <spi_flash_program+0x77>
mov    $0xa,%edi
call   1060 <putch@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_flash_program>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
add    %esi,%r14d
lea    0xea7(%rip),%rdi
call   1040 <putstr@plt>
cmp    %ebx,%r14d
jbe    11d8 <spi_flash_program+0x98>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1050 <spi_flash_erase@plt>
mov    $0x2e,%edi
call   1060 <putch@plt>
add    $0x10000,%ebp
cmp    %r14d,%ebp
jb     1170 <spi_flash_program+0x30>
mov    $0xa,%edi
call   1060 <putch@plt>
lea    0xe6d(%rip),%rdi
call   1040 <putstr@plt>
cmp    %ebx,%r14d
jbe    11ee <spi_flash_program+0xae>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <spi_flash_write_page@plt>
add    $0x100,%r15
mov    $0x2e,%edi
call   1060 <putch@plt>
add    $0x100,%ebx
cmp    %r14d,%ebx
jb     11b0 <spi_flash_program+0x70>
jmp    11ee <spi_flash_program+0xae>
mov    $0xa,%edi
call   1060 <putch@plt>
lea    0xe21(%rip),%rdi
call   1040 <putstr@plt>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <putch@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"Erasing: \00"
@.str.1 = [14 x i8] c"Programming: \00"
define void @spi_flash_program(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = add i32 %9, %10
store i32 %11, i32* %8
%12 = call i32 @putstr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %5
store i32 %13, i32* %7
br label %14
14: ; preds = %22, %3
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32, i32* %7
%20 = call i32 @spi_flash_erase(i32 %19)
%21 = call i32 @putch(i8 signext 46)
br label %22
22: ; preds = %18
%23 = load i32, i32* %7
%24 = add i32 %23, 65536
store i32 %24, i32* %7
br label %14
25: ; preds = %14
%26 = call i32 @putch(i8 signext 10)
%27 = call i32 @putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %5
store i32 %28, i32* %7
br label %29
29: ; preds = %40, %25
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = icmp ult i32 %30, %31
br i1 %32, label %33, label %43
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = load i32, i32* %7
%36 = call i32 @spi_flash_write_page(i8* %34, i32 %35)
%37 = load i8*, i8** %4
%38 = getelementptr i8, i8* %37, i64 256
store i8* %38, i8** %4
%39 = call i32 @putch(i8 signext 46)
br label %40
40: ; preds = %33
%41 = load i32, i32* %7
%42 = add i32 %41, 256
store i32 %42, i32* %7
br label %29
43: ; preds = %29
%44 = call i32 @putch(i8 signext 10)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"Erasing: \00"
@.str.1 = [14 x i8] c"Programming: \00"
define void @spi_flash_program(i8* %0, i32 %1, i32 %2) {
%4 = add i32 %2, %1
%5 = tail call i32 @putstr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = icmp ugt i32 %4, %1
br i1 %6, label %7, label %24
7: ; preds = %3, %7
%8 = phi i32 [ %11, %7 ], [ %1, %3 ]
%9 = tail call i32 @spi_flash_erase(i32 %8)
%10 = tail call i32 @putch(i8 signext 46)
%11 = add i32 %8, 65536
%12 = icmp ult i32 %11, %4
br i1 %12, label %7, label %13
13: ; preds = %7
%14 = tail call i32 @putch(i8 signext 10)
%15 = tail call i32 @putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br i1 %6, label %16, label %27
16: ; preds = %13, %16
%17 = phi i8* [ %20, %16 ], [ %0, %13 ]
%18 = phi i32 [ %22, %16 ], [ %1, %13 ]
%19 = tail call i32 @spi_flash_write_page(i8* %17, i32 %18)
%20 = getelementptr i8, i8* %17, i64 256
%21 = tail call i32 @putch(i8 signext 46)
%22 = add i32 %18, 256
%23 = icmp ult i32 %22, %4
br i1 %23, label %16, label %27
24: ; preds = %3
%25 = tail call i32 @putch(i8 signext 10)
%26 = tail call i32 @putstr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %16, %24, %13
%28 = tail call i32 @putch(i8 signext 10)
ret void
}