#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 */
unsigned int spi_sendcmd(int, unsigned int, int, int);
void spi_flash_write_page(void *from, unsigned int addr) {
  unsigned int cmd, end, s, v;
  unsigned char *ap;
  ap = from;
  end = addr + 256;
  for (; (addr < end); addr += 4, ap += 4) {
    spi_sendcmd(0x6, 0, 1, 0);
    v = (ap[3] << 24) | (ap[2] << 16) | (ap[1] << 8) | ap[0];
    cmd = (addr << 8) | 0x2;
    spi_sendcmd(cmd, v, 8, 0);
    do {
      s = spi_sendcmd(0x5, 0, 1, 1);
    } while (s & 0x1);
  }
}


x86 assembly (clang, -O0):
<spi_flash_write_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11e4 <spi_flash_write_page+0xd4>
mov    $0x6,%edi
xor    %ecx,%ecx
mov    $0x1,%edx
mov    %ecx,%esi
call   1030 <spi_sendcmd@plt>
mov    -0x28(%rbp),%rax
movzbl 0x3(%rax),%eax
shl    $0x18,%eax
mov    -0x28(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x28(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x28(%rbp),%rcx
movzbl (%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x8,%eax
or     $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <spi_sendcmd@plt>
mov    $0x5,%edi
xor    %esi,%esi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <spi_sendcmd@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11a0 <spi_flash_write_page+0x90>
jmp    11ca <spi_flash_write_page+0xba>
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1132 <spi_flash_write_page+0x22>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_flash_write_page>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
sub    $0xffffff00,%ebp
jae    1185 <spi_flash_write_page+0x75>
mov    %esi,%ebx
mov    %rdi,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x6,%edi
xor    %esi,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <spi_sendcmd@plt>
mov    (%r14),%esi
mov    %ebx,%edi
shl    $0x8,%edi
or     $0x2,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <spi_sendcmd@plt>
nopw   0x0(%rax,%rax,1)
mov    $0x5,%edi
xor    %esi,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <spi_sendcmd@plt>
test   $0x1,%al
jne    1160 <spi_flash_write_page+0x50>
add    $0x4,%ebx
add    $0x4,%r14
cmp    %ebp,%ebx
jb     1130 <spi_flash_write_page+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @spi_flash_write_page(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
store i8* %10, i8** %9
%11 = load i32, i32* %4
%12 = add i32 %11, 256
store i32 %12, i32* %6
br label %13
13: ; preds = %54, %2
%14 = load i32, i32* %4
%15 = load i32, i32* %6
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %59
17: ; preds = %13
%18 = call i32 @spi_sendcmd(i32 6, i32 0, i32 1, i32 0)
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 3
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = shl i32 %22, 24
%24 = load i8*, i8** %9
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = shl i32 %27, 16
%29 = or i32 %23, %28
%30 = load i8*, i8** %9
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = shl i32 %33, 8
%35 = or i32 %29, %34
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = or i32 %35, %39
store i32 %40, i32* %8
%41 = load i32, i32* %4
%42 = shl i32 %41, 8
%43 = or i32 %42, 2
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = load i32, i32* %8
%46 = call i32 @spi_sendcmd(i32 %44, i32 %45, i32 8, i32 0)
br label %47
47: ; preds = %49, %17
%48 = call i32 @spi_sendcmd(i32 5, i32 0, i32 1, i32 1)
store i32 %48, i32* %7
br label %49
49: ; preds = %47
%50 = load i32, i32* %7
%51 = and i32 %50, 1
%52 = icmp ne i32 %51, 0
br i1 %52, label %47, label %53
53: ; preds = %49
br label %54
54: ; preds = %53
%55 = load i32, i32* %4
%56 = add i32 %55, 4
store i32 %56, i32* %4
%57 = load i8*, i8** %9
%58 = getelementptr inbounds i8, i8* %57, i64 4
store i8* %58, i8** %9
br label %13
59: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @spi_flash_write_page(i8* nocapture %0, i32 %1) {
%3 = add i32 %1, 256
%4 = icmp ult i32 %1, -256
br i1 %4, label %5, label %37
5: ; preds = %2, %33
%6 = phi i8* [ %35, %33 ], [ %0, %2 ]
%7 = phi i32 [ %34, %33 ], [ %1, %2 ]
%8 = tail call i32 @spi_sendcmd(i32 6, i32 0, i32 1, i32 0)
%9 = getelementptr inbounds i8, i8* %6, i64 3
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = shl nuw i32 %11, 24
%13 = getelementptr inbounds i8, i8* %6, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = shl nuw nsw i32 %15, 16
%17 = or i32 %16, %12
%18 = getelementptr inbounds i8, i8* %6, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = shl nuw nsw i32 %20, 8
%22 = or i32 %17, %21
%23 = load i8, i8* %6
%24 = zext i8 %23 to i32
%25 = or i32 %22, %24
%26 = shl i32 %7, 8
%27 = or i32 %26, 2
%28 = tail call i32 @spi_sendcmd(i32 %27, i32 %25, i32 8, i32 0)
br label %29
29: ; preds = %29, %5
%30 = tail call i32 @spi_sendcmd(i32 5, i32 0, i32 1, i32 1)
%31 = and i32 %30, 1
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %29
33: ; preds = %29
%34 = add i32 %7, 4
%35 = getelementptr inbounds i8, i8* %6, i64 4
%36 = icmp ult i32 %34, %3
br i1 %36, label %5, label %37
37: ; preds = %33, %2
ret void
}