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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ check_rdismb();
int /*<<< orphan*/ enable_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 /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ spi_transfer(int *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait_ready();
void cmd_write_ip(const char *filename) {
  FILE *f;
  uint8_t buf[512 + 3] = {0};
  if (check_rdismb()) {
    fprintf(stderr, "flash memory is protected\n");
    exit(EXIT_FAILURE);
  }
  buf[0] = 0x02;
  buf[1] = 0x00;
  buf[2] = 0x00;
  f = fopen(filename, "r");
  if (!f) {
    fprintf(stderr, "can't open %s to read\n", filename);
    exit(EXIT_FAILURE);
  }
  fread(buf + 3, sizeof(buf) - 3, 1, f);
  fclose(f);
  enable_infen();
  enable_wen();
  printf("writing InfoPage\n");
  if (!spi_transfer(buf, sizeof(buf))) {
    fprintf(stderr, "something wrong writing InfoPage\n");
    exit(EXIT_FAILURE);
  }
  wait_ready();
}


x86 assembly (clang, -O0):
<cmd_write_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x80c,%edx
call   1060 <memset@plt>
mov    $0x0,%al
call   10d0 <check_rdismb@plt>
cmp    $0x0,%rax
je     1218 <cmd_write_ip+0x58>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0x2,-0x820(%rbp)
movl   $0x0,-0x81c(%rbp)
movl   $0x0,-0x818(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    127e <cmd_write_ip+0xbe>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x820(%rbp),%rdi
add    $0xc,%rdi
mov    -0x10(%rbp),%rcx
mov    $0x809,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1080 <enable_infen@plt>
mov    $0x0,%al
call   10e0 <enable_wen@plt>
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
lea    -0x820(%rbp),%rdi
mov    $0x80c,%esi
call   10a0 <spi_transfer@plt>
cmp    $0x0,%eax
jne    12fe <cmd_write_ip+0x13e>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1070 <wait_ready@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_write_ip>:
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x80c,%edx
xor    %esi,%esi
call   1060 <memset@plt>
xor    %eax,%eax
call   10d0 <check_rdismb@plt>
test   %rax,%rax
jne    126f <cmd_write_ip+0xaf>
movq   $0x2,(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xe18(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1281 <cmd_write_ip+0xc1>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x809,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
call   1080 <enable_infen@plt>
xor    %eax,%eax
call   10e0 <enable_wen@plt>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x80c,%esi
call   10a0 <spi_transfer@plt>
test   %eax,%eax
je     129d <cmd_write_ip+0xdd>
xor    %eax,%eax
call   1070 <wait_ready@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
jmp    12ad <cmd_write_ip+0xed>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12b4 <cmd_write_ip+0xf4>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

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 = [18 x i8] c"writing InfoPage\0A\00"
@.str.4 = [34 x i8] c"something wrong writing InfoPage\0A\00"
define void @cmd_write_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [515 x i32]
store i8* %0, i8** %2
%5 = bitcast [515 x i32]* %4 to i8*
%6 = call i64 (...) @check_rdismb()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
store i32 2, i32* %14
%15 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 1
store i32 0, i32* %15
%16 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 2
store i32 0, i32* %16
%17 = load i8*, i8** %2
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %3
%19 = load i32*, i32** %3
%20 = icmp ne i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %2
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
%28 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
%29 = getelementptr inbounds i32, i32* %28, i64 3
%30 = load i32*, i32** %3
%31 = call i32 @fread(i32* %29, i32 2057, i32 1, i32* %30)
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
%34 = call i32 (...) @enable_infen()
%35 = call i32 (...) @enable_wen()
%36 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%37 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
%38 = call i32 @spi_transfer(i32* %37, i32 2060)
%39 = icmp ne i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %27
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %27
%46 = call i32 (...) @wait_ready()
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.4 = [34 x i8] c"something wrong writing InfoPage\0A\00"
@str = [17 x i8] c"writing InfoPage\00"
define void @cmd_write_ip(i8* %0) {
%2 = alloca [515 x i32]
%3 = bitcast [515 x i32]* %2 to i8*
%4 = tail call i64 (...) @check_rdismb()
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 0
store i32 2, i32* %12
%13 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 1
store i32 0, i32* %13
%14 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 2
store i32 0, i32* %14
%15 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %11
%23 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 3
%24 = call i32 @fread(i32* %23, i32 2057, i32 1, i32* %15)
%25 = tail call i32 @fclose(i32* %15)
%26 = tail call i32 (...) @enable_infen()
%27 = tail call i32 (...) @enable_wen()
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%29 = call i32 @spi_transfer(i32* %12, i32 2060)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %22
%37 = call i32 (...) @wait_ready()
ret void
}