#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 */
typedef size_t uint8_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
size_t addr2page(int);
scalar_t__ check_rdismb();
int /*<<< orphan*/ disable_infen();
int /*<<< orphan*/ enable_wen();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int hexfile_getline(int /*<<< orphan*/ *, int *, size_t *, int);
scalar_t__ memcmp(size_t *, size_t *, int);
int /*<<< orphan*/ memcpy(size_t *, size_t *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ spi_transfer(size_t *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait_ready();
void cmd_write_flash(const char *filename) {
  FILE *fd;
  uint8_t buffer[255 + 3] = {0};
  uint8_t orig[255 + 3] = {0};
  uint8_t comp[255 + 3] = {0};
  uint8_t page_erased[36] = {0};
  int count;
  uint16_t address;
  if (check_rdismb()) {
    fprintf(stderr, "flash memory is protected\n");
    exit(EXIT_FAILURE);
  }
  disable_infen();
  fd = fopen(filename, "r");
  if (!fd) {
    fprintf(stderr, "can't open %s to read\n", filename);
    exit(EXIT_FAILURE);
  }
  while ((count = hexfile_getline(fd, &address, buffer + 3,
                                  sizeof(buffer) - 3)) > 0) {
    uint8_t pg_start = addr2page(address);
    uint8_t pg_end = addr2page(address + count);
    while (pg_start <= pg_end) {
      if (!page_erased[pg_start]) {
        printf("erasing page %d\n", pg_start);
        buffer[0] = 0x52;
        buffer[1] = pg_start;
        enable_wen();
        if (!spi_transfer(buffer, 2)) {
          fprintf(stderr, "SPI error\n");
          exit(EXIT_FAILURE);
        }
        wait_ready();
        page_erased[pg_start] = 1;
      }
      ++pg_start;
    }
    enable_wen();
    memcpy(orig, buffer + 3, count);
    buffer[0] = 0x02;
    buffer[1] = address >> 8;
    buffer[2] = address & 0xff;
    printf("writing %i bytes at 0x%04hx...\n", count, address);
    if (!spi_transfer(buffer, count + 3)) {
      fprintf(stderr, "SPI error\n");
      fclose(fd);
      exit(EXIT_FAILURE);
    }
    wait_ready();
    comp[0] = 0x03;
    comp[1] = address >> 8;
    comp[2] = address & 0xff;
    if (!spi_transfer(comp, count + 3)) {
      fprintf(stderr, "SPI error\n");
      fclose(fd);
      exit(EXIT_FAILURE);
    }
    if (memcmp(orig, comp + 3, count)) {
      fprintf(stderr, "error checking memory\n");
      fclose(fd);
      exit(EXIT_FAILURE);
    }
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<cmd_write_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1980,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x810,%edx
call   1050 <memset@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x810,%edx
call   1050 <memset@plt>
lea    -0x1840(%rbp),%rdi
xor    %esi,%esi
mov    $0x810,%edx
call   1050 <memset@plt>
lea    -0x1960(%rbp),%rdi
xor    %esi,%esi
mov    $0x120,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   1100 <check_rdismb@plt>
cmp    $0x0,%rax
je     1281 <cmd_write_flash+0x91>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1060 <disable_infen@plt>
mov    -0x8(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12d0 <cmd_write_flash+0xe0>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    12d5 <cmd_write_flash+0xe5>
mov    -0x10(%rbp),%rdi
lea    -0x820(%rbp),%rdx
add    $0x18,%rdx
lea    -0x1968(%rbp),%rsi
mov    $0x80d,%ecx
call   10b0 <hexfile_getline@plt>
mov    %eax,-0x1964(%rbp)
cmp    $0x0,%eax
jle    1598 <cmd_write_flash+0x3a8>
mov    -0x1968(%rbp),%edi
call   10e0 <addr2page@plt>
mov    %rax,-0x1970(%rbp)
mov    -0x1968(%rbp),%edi
add    -0x1964(%rbp),%edi
call   10e0 <addr2page@plt>
mov    %rax,-0x1978(%rbp)
mov    -0x1970(%rbp),%rax
cmp    -0x1978(%rbp),%rax
ja     13ff <cmd_write_flash+0x20f>
mov    -0x1970(%rbp),%rax
cmpq   $0x0,-0x1960(%rbp,%rax,8)
jne    13e8 <cmd_write_flash+0x1f8>
mov    -0x1970(%rbp),%rax
mov    %eax,%esi
lea    0xccc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x52,-0x820(%rbp)
mov    -0x1970(%rbp),%rax
mov    %rax,-0x818(%rbp)
mov    $0x0,%al
call   1110 <enable_wen@plt>
lea    -0x820(%rbp),%rdi
mov    $0x2,%esi
call   10c0 <spi_transfer@plt>
cmp    $0x0,%eax
jne    13ce <cmd_write_flash+0x1de>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
lea    0xc8c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c11(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1080 <wait_ready@plt>
mov    -0x1970(%rbp),%rax
movq   $0x1,-0x1960(%rbp,%rax,8)
mov    -0x1970(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1970(%rbp)
jmp    132e <cmd_write_flash+0x13e>
mov    $0x0,%al
call   1110 <enable_wen@plt>
lea    -0x1030(%rbp),%rdi
lea    -0x820(%rbp),%rsi
add    $0x18,%rsi
mov    -0x1964(%rbp),%edx
call   10a0 <memcpy@plt>
movq   $0x2,-0x820(%rbp)
mov    -0x1968(%rbp),%eax
sar    $0x8,%eax
cltq
mov    %rax,-0x818(%rbp)
mov    -0x1968(%rbp),%eax
and    $0xff,%eax
cltq
mov    %rax,-0x810(%rbp)
mov    -0x1964(%rbp),%esi
mov    -0x1968(%rbp),%edx
lea    0xbe9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x820(%rbp),%rdi
mov    -0x1964(%rbp),%esi
add    $0x3,%esi
call   10c0 <spi_transfer@plt>
cmp    $0x0,%eax
jne    14ba <cmd_write_flash+0x2ca>
mov    0x2b4d(%rip),%rax
mov    (%rax),%edi
lea    0xba9(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1080 <wait_ready@plt>
movq   $0x3,-0x1840(%rbp)
mov    -0x1968(%rbp),%eax
sar    $0x8,%eax
cltq
mov    %rax,-0x1838(%rbp)
mov    -0x1968(%rbp),%eax
and    $0xff,%eax
cltq
mov    %rax,-0x1830(%rbp)
lea    -0x1840(%rbp),%rdi
mov    -0x1964(%rbp),%esi
add    $0x3,%esi
call   10c0 <spi_transfer@plt>
cmp    $0x0,%eax
jne    153e <cmd_write_flash+0x34e>
mov    0x2ac9(%rip),%rax
mov    (%rax),%edi
lea    0xb25(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2aa1(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0x1030(%rbp),%rdi
lea    -0x1840(%rbp),%rsi
add    $0x18,%rsi
mov    -0x1964(%rbp),%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     1593 <cmd_write_flash+0x3a3>
mov    0x2a74(%rip),%rax
mov    (%rax),%edi
lea    0xafb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2a4c(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    12d5 <cmd_write_flash+0xe5>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x1980,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_write_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1968,%rsp
mov    %rdi,%rbx
lea    0x130(%rsp),%rdi
mov    $0x810,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x1150(%rsp),%rdi
mov    $0x810,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x940(%rsp),%rdi
mov    $0x810,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x10(%rsp),%rdi
mov    $0x120,%edx
xor    %esi,%esi
call   1050 <memset@plt>
xor    %eax,%eax
call   1100 <check_rdismb@plt>
test   %rax,%rax
jne    14af <cmd_write_flash+0x2bf>
xor    %eax,%eax
call   1060 <disable_infen@plt>
lea    0xdac(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     14c1 <cmd_write_flash+0x2d1>
lea    0x148(%rsp),%r12
lea    0x1150(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %r12,%rdx
mov    $0x80d,%ecx
call   10b0 <hexfile_getline@plt>
test   %eax,%eax
jle    1439 <cmd_write_flash+0x249>
mov    %eax,%ebp
mov    %eax,%r14d
mov    0x4(%rsp),%edi
call   10e0 <addr2page@plt>
mov    %rax,%r13
mov    0x4(%rsp),%edi
add    %ebp,%edi
call   10e0 <addr2page@plt>
cmp    %rax,%r13
jbe    13c0 <cmd_write_flash+0x1d0>
xor    %eax,%eax
call   1110 <enable_wen@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   10a0 <memcpy@plt>
movq   $0x2,0x130(%rsp)
mov    0x4(%rsp),%edx
mov    %edx,%eax
sar    $0x8,%eax
cltq
mov    %rax,0x138(%rsp)
movzbl %dl,%eax
mov    %rax,0x140(%rsp)
lea    0xd26(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x3,%ebp
lea    0x130(%rsp),%rdi
mov    %ebp,%esi
call   10c0 <spi_transfer@plt>
test   %eax,%eax
je     146e <cmd_write_flash+0x27e>
xor    %eax,%eax
call   1080 <wait_ready@plt>
movq   $0x3,0x940(%rsp)
mov    0x4(%rsp),%eax
movzbl %al,%ecx
sar    $0x8,%eax
cltq
mov    %rax,0x948(%rsp)
mov    %rcx,0x950(%rsp)
lea    0x940(%rsp),%rdi
mov    %ebp,%esi
call   10c0 <spi_transfer@plt>
test   %eax,%eax
je     146e <cmd_write_flash+0x27e>
mov    %r15,%rdi
lea    0x958(%rsp),%rsi
mov    %r14d,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     12a0 <cmd_write_flash+0xb0>
jmp    1480 <cmd_write_flash+0x290>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbx
jmp    13dd <cmd_write_flash+0x1ed>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r13
cmp    %rbx,%r13
ja     12e4 <cmd_write_flash+0xf4>
cmpq   $0x0,0x10(%rsp,%r13,8)
jne    13d0 <cmd_write_flash+0x1e0>
lea    0xc48(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x52,0x130(%rsp)
mov    %r13,0x138(%rsp)
xor    %eax,%eax
call   1110 <enable_wen@plt>
lea    0x130(%rsp),%rdi
mov    $0x2,%esi
call   10c0 <spi_transfer@plt>
test   %eax,%eax
je     1455 <cmd_write_flash+0x265>
xor    %eax,%eax
call   1080 <wait_ready@plt>
movq   $0x1,0x10(%rsp,%r13,8)
jmp    13d0 <cmd_write_flash+0x1e0>
mov    0x8(%rsp),%rdi
call   1030 <fclose@plt>
add    $0x1968,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b84(%rip),%rax
mov    (%rax),%edi
lea    0xbe0(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    14a1 <cmd_write_flash+0x2b1>
mov    0x2b6b(%rip),%rax
mov    (%rax),%edi
lea    0xbc7(%rip),%rsi
jmp    1490 <cmd_write_flash+0x2a0>
mov    0x2b59(%rip),%rax
mov    (%rax),%edi
lea    0xbe0(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1030 <fclose@plt>
mov    0x2b30(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2b2a(%rip),%rax
mov    (%rax),%edi
lea    0xb41(%rip),%rsi
jmp    1465 <cmd_write_flash+0x275>
mov    0x2b18(%rip),%rax
mov    (%rax),%edi
lea    0xb4c(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    14a1 <cmd_write_flash+0x2b1>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"flash memory is protected\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"can't open %s to read\0A\00"
@.str.3 = [17 x i8] c"erasing page %d\0A\00"
@.str.4 = [11 x i8] c"SPI error\0A\00"
@.str.5 = [32 x i8] c"writing %i bytes at 0x%04hx...\0A\00"
@.str.6 = [23 x i8] c"error checking memory\0A\00"
define void @cmd_write_flash(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [258 x i64]
%5 = alloca [258 x i64]
%6 = alloca [258 x i64]
%7 = alloca [36 x i64]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %2
%12 = bitcast [258 x i64]* %4 to i8*
%13 = bitcast [258 x i64]* %5 to i8*
%14 = bitcast [258 x i64]* %6 to i8*
%15 = bitcast [36 x i64]* %7 to i8*
%16 = call i64 (...) @check_rdismb()
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = call i32 (...) @disable_infen()
%25 = load i8*, i8** %2
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %3
%27 = load i32*, i32** %3
%28 = icmp ne i32* %27, null
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %2
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %23
br label %36
36: ; preds = %149, %35
%37 = load i32*, i32** %3
%38 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
%39 = getelementptr inbounds i64, i64* %38, i64 3
%40 = call i32 @hexfile_getline(i32* %37, i32* %9, i64* %39, i32 2061)
store i32 %40, i32* %8
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %150
42: ; preds = %36
%43 = load i32, i32* %9
%44 = call i64 @addr2page(i32 %43)
store i64 %44, i64* %10
%45 = load i32, i32* %9
%46 = load i32, i32* %8
%47 = add nsw i32 %45, %46
%48 = call i64 @addr2page(i32 %47)
store i64 %48, i64* %11
br label %49
49: ; preds = %78, %42
%50 = load i64, i64* %10
%51 = load i64, i64* %11
%52 = icmp ule i64 %50, %51
br i1 %52, label %53, label %81
53: ; preds = %49
%54 = load i64, i64* %10
%55 = getelementptr inbounds [36 x i64], [36 x i64]* %7, i64 0, i64 %54
%56 = load i64, i64* %55
%57 = icmp ne i64 %56, 0
br i1 %57, label %78, label %58
58: ; preds = %53
%59 = load i64, i64* %10
%60 = trunc i64 %59 to i32
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
store i64 82, i64* %62
%63 = load i64, i64* %10
%64 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 1
store i64 %63, i64* %64
%65 = call i32 (...) @enable_wen()
%66 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
%67 = call i32 @spi_transfer(i64* %66, i32 2)
%68 = icmp ne i32 %67, 0
br i1 %68, label %74, label %69
69: ; preds = %58
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %58
%75 = call i32 (...) @wait_ready()
%76 = load i64, i64* %10
%77 = getelementptr inbounds [36 x i64], [36 x i64]* %7, i64 0, i64 %76
store i64 1, i64* %77
br label %78
78: ; preds = %74, %53
%79 = load i64, i64* %10
%80 = add i64 %79, 1
store i64 %80, i64* %10
br label %49
81: ; preds = %49
%82 = call i32 (...) @enable_wen()
%83 = getelementptr inbounds [258 x i64], [258 x i64]* %5, i64 0, i64 0
%84 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
%85 = getelementptr inbounds i64, i64* %84, i64 3
%86 = load i32, i32* %8
%87 = call i32 @memcpy(i64* %83, i64* %85, i32 %86)
%88 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
store i64 2, i64* %88
%89 = load i32, i32* %9
%90 = ashr i32 %89, 8
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 1
store i64 %91, i64* %92
%93 = load i32, i32* %9
%94 = and i32 %93, 255
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 2
store i64 %95, i64* %96
%97 = load i32, i32* %8
%98 = load i32, i32* %9
%99 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %97, i32 %98)
%100 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
%101 = load i32, i32* %8
%102 = add nsw i32 %101, 3
%103 = call i32 @spi_transfer(i64* %100, i32 %102)
%104 = icmp ne i32 %103, 0
br i1 %104, label %112, label %105
105: ; preds = %81
%106 = load i32, i32* @stderr
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%108 = load i32*, i32** %3
%109 = call i32 @fclose(i32* %108)
%110 = load i32, i32* @EXIT_FAILURE
%111 = call i32 @exit(i32 %110)
unreachable
112: ; preds = %81
%113 = call i32 (...) @wait_ready()
%114 = getelementptr inbounds [258 x i64], [258 x i64]* %6, i64 0, i64 0
store i64 3, i64* %114
%115 = load i32, i32* %9
%116 = ashr i32 %115, 8
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds [258 x i64], [258 x i64]* %6, i64 0, i64 1
store i64 %117, i64* %118
%119 = load i32, i32* %9
%120 = and i32 %119, 255
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds [258 x i64], [258 x i64]* %6, i64 0, i64 2
store i64 %121, i64* %122
%123 = getelementptr inbounds [258 x i64], [258 x i64]* %6, i64 0, i64 0
%124 = load i32, i32* %8
%125 = add nsw i32 %124, 3
%126 = call i32 @spi_transfer(i64* %123, i32 %125)
%127 = icmp ne i32 %126, 0
br i1 %127, label %135, label %128
128: ; preds = %112
%129 = load i32, i32* @stderr
%130 = call i32 (i32, i8*, ...) @fprintf(i32 %129, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%131 = load i32*, i32** %3
%132 = call i32 @fclose(i32* %131)
%133 = load i32, i32* @EXIT_FAILURE
%134 = call i32 @exit(i32 %133)
unreachable
135: ; preds = %112
%136 = getelementptr inbounds [258 x i64], [258 x i64]* %5, i64 0, i64 0
%137 = getelementptr inbounds [258 x i64], [258 x i64]* %6, i64 0, i64 0
%138 = getelementptr inbounds i64, i64* %137, i64 3
%139 = load i32, i32* %8
%140 = call i64 @memcmp(i64* %136, i64* %138, i32 %139)
%141 = icmp ne i64 %140, 0
br i1 %141, label %142, label %149
142: ; preds = %135
%143 = load i32, i32* @stderr
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %143, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%145 = load i32*, i32** %3
%146 = call i32 @fclose(i32* %145)
%147 = load i32, i32* @EXIT_FAILURE
%148 = call i32 @exit(i32 %147)
unreachable
149: ; preds = %135
br label %36
150: ; preds = %36
%151 = load i32*, i32** %3
%152 = call i32 @fclose(i32* %151)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"flash memory is protected\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"can't open %s to read\0A\00"
@.str.3 = [17 x i8] c"erasing page %d\0A\00"
@.str.4 = [11 x i8] c"SPI error\0A\00"
@.str.5 = [32 x i8] c"writing %i bytes at 0x%04hx...\0A\00"
@.str.6 = [23 x i8] c"error checking memory\0A\00"
define void @cmd_write_flash(i8* %0) {
%2 = alloca [258 x i64]
%3 = alloca [258 x i64]
%4 = alloca [258 x i64]
%5 = alloca [36 x i64]
%6 = alloca i32
%7 = bitcast [258 x i64]* %2 to i8*
%8 = bitcast [258 x i64]* %3 to i8*
%9 = bitcast [258 x i64]* %4 to i8*
%10 = bitcast [36 x i64]* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = tail call i64 (...) @check_rdismb()
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = tail call i32 (...) @disable_infen()
%21 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %33, label %23
23: ; preds = %19
%24 = getelementptr inbounds [258 x i64], [258 x i64]* %2, i64 0, i64 0
%25 = getelementptr inbounds [258 x i64], [258 x i64]* %2, i64 0, i64 3
%26 = getelementptr inbounds [258 x i64], [258 x i64]* %2, i64 0, i64 1
%27 = getelementptr inbounds [258 x i64], [258 x i64]* %3, i64 0, i64 0
%28 = getelementptr inbounds [258 x i64], [258 x i64]* %2, i64 0, i64 2
%29 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 0
%30 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 1
%31 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 2
%32 = getelementptr inbounds [258 x i64], [258 x i64]* %4, i64 0, i64 3
br label %38
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %23, %102
%39 = call i32 @hexfile_getline(i32* %21, i32* %6, i64* %25, i32 2061)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %111
41: ; preds = %38
%42 = load i32, i32* %6
%43 = call i64 @addr2page(i32 %42)
%44 = load i32, i32* %6
%45 = add nsw i32 %44, %39
%46 = call i64 @addr2page(i32 %45)
%47 = icmp ugt i64 %43, %46
br i1 %47, label %69, label %48
48: ; preds = %41, %66
%49 = phi i64 [ %67, %66 ], [ %43, %41 ]
%50 = getelementptr inbounds [36 x i64], [36 x i64]* %5, i64 0, i64 %49
%51 = load i64, i64* %50
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %66
53: ; preds = %48
%54 = trunc i64 %49 to i32
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %54)
store i64 82, i64* %24
store i64 %49, i64* %26
%56 = call i32 (...) @enable_wen()
%57 = call i32 @spi_transfer(i64* %24, i32 2)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %53
%65 = call i32 (...) @wait_ready()
store i64 1, i64* %50
br label %66
66: ; preds = %64, %48
%67 = add i64 %49, 1
%68 = icmp ugt i64 %67, %46
br i1 %68, label %69, label %48
69: ; preds = %66, %41
%70 = call i32 (...) @enable_wen()
%71 = call i32 @memcpy(i64* %27, i64* %25, i32 %39)
store i64 2, i64* %24
%72 = load i32, i32* %6
%73 = ashr i32 %72, 8
%74 = sext i32 %73 to i64
store i64 %74, i64* %26
%75 = and i32 %72, 255
%76 = zext i32 %75 to i64
store i64 %76, i64* %28
%77 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %72)
%78 = add nsw i32 %39, 3
%79 = call i32 @spi_transfer(i64* %24, i32 %78)
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %87
81: ; preds = %69
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%84 = call i32 @fclose(i32* %21)
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %69
%88 = call i32 (...) @wait_ready()
store i64 3, i64* %29
%89 = load i32, i32* %6
%90 = ashr i32 %89, 8
%91 = sext i32 %90 to i64
store i64 %91, i64* %30
%92 = and i32 %89, 255
%93 = zext i32 %92 to i64
store i64 %93, i64* %31
%94 = call i32 @spi_transfer(i64* %29, i32 %78)
%95 = icmp eq i32 %94, 0
br i1 %95, label %96, label %102
96: ; preds = %87
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%99 = call i32 @fclose(i32* %21)
%100 = load i32, i32* @EXIT_FAILURE
%101 = call i32 @exit(i32 %100)
unreachable
102: ; preds = %87
%103 = call i64 @memcmp(i64* %27, i64* %32, i32 %39)
%104 = icmp eq i64 %103, 0
br i1 %104, label %38, label %105
105: ; preds = %102
%106 = load i32, i32* @stderr
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%108 = call i32 @fclose(i32* %21)
%109 = load i32, i32* @EXIT_FAILURE
%110 = call i32 @exit(i32 %109)
unreachable
111: ; preds = %38
%112 = call i32 @fclose(i32* %21)
ret void
}