#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*/ disable_infen();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ spi_transfer(int *, int);
int /*<<< orphan*/ stderr;
void cmd_read_flash(const char *filename) {
  FILE *f;
  uint8_t buf[18432 + 3] = {0};
  if (check_rdismb()) {
    fprintf(stderr, "flash memory is protected\n");
    exit(EXIT_FAILURE);
  }
  disable_infen();
  printf("reading flash...\n");
  buf[0] = 0x03;
  buf[1] = 0x00;
  buf[2] = 0x00;
  if (!spi_transfer(buf, sizeof(buf))) {
    fprintf(stderr, "something wrong reading flash\n");
    exit(EXIT_FAILURE);
  }
  f = fopen(filename, "w");
  if (!f) {
    fprintf(stderr, "can't open %s to write\n", filename);
    exit(EXIT_FAILURE);
  }
  fwrite(buf + 3, sizeof(buf) - 3, 1, f);
  fclose(f);
}


x86 assembly (clang, -O0):
<cmd_read_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x12020,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x12020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1200c,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   10c0 <check_rdismb@plt>
cmp    $0x0,%rax
je     11f8 <cmd_read_flash+0x58>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <disable_infen@plt>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
movl   $0x3,-0x12020(%rbp)
movl   $0x0,-0x1201c(%rbp)
movl   $0x0,-0x12018(%rbp)
lea    -0x12020(%rbp),%rdi
mov    $0x1200c,%esi
call   1080 <spi_transfer@plt>
cmp    $0x0,%eax
jne    1268 <cmd_read_flash+0xc8>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b0 <cmd_read_flash+0x110>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x12020(%rbp),%rdi
add    $0xc,%rdi
mov    -0x10(%rbp),%rcx
mov    $0x12009,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x12020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_read_flash>:
push   %r14
push   %rbx
sub    $0x12018,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1200c,%edx
xor    %esi,%esi
call   1050 <memset@plt>
xor    %eax,%eax
call   10c0 <check_rdismb@plt>
test   %rax,%rax
jne    123d <cmd_read_flash+0x9d>
xor    %eax,%eax
call   1060 <disable_infen@plt>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
movq   $0x3,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1200c,%esi
call   1080 <spi_transfer@plt>
test   %eax,%eax
je     124f <cmd_read_flash+0xaf>
lea    0xe37(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1268 <cmd_read_flash+0xc8>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x12009,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x12018,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
jmp    125f <cmd_read_flash+0xbf>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1282 <cmd_read_flash+0xe2>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10a0 <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 = [18 x i8] c"reading flash...\0A\00"
@.str.2 = [31 x i8] c"something wrong reading flash\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [24 x i8] c"can't open %s to write\0A\00"
define void @cmd_read_flash(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [18435 x i32]
store i8* %0, i8** %2
%5 = bitcast [18435 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 = call i32 (...) @disable_infen()
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds [18435 x i32], [18435 x i32]* %4, i64 0, i64 0
store i32 3, i32* %16
%17 = getelementptr inbounds [18435 x i32], [18435 x i32]* %4, i64 0, i64 1
store i32 0, i32* %17
%18 = getelementptr inbounds [18435 x i32], [18435 x i32]* %4, i64 0, i64 2
store i32 0, i32* %18
%19 = getelementptr inbounds [18435 x i32], [18435 x i32]* %4, i64 0, i64 0
%20 = call i32 @spi_transfer(i32* %19, i32 73740)
%21 = icmp ne i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
%28 = load i8*, i8** %2
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %3
%30 = load i32*, i32** %3
%31 = icmp ne i32* %30, null
br i1 %31, label %38, label %32
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %2
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %27
%39 = getelementptr inbounds [18435 x i32], [18435 x i32]* %4, i64 0, i64 0
%40 = getelementptr inbounds i32, i32* %39, i64 3
%41 = load i32*, i32** %3
%42 = call i32 @fwrite(i32* %40, i32 73737, i32 1, i32* %41)
%43 = load i32*, i32** %3
%44 = call i32 @fclose(i32* %43)
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.2 = [31 x i8] c"something wrong reading flash\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [24 x i8] c"can't open %s to write\0A\00"
@str = [17 x i8] c"reading flash...\00"
define void @cmd_read_flash(i8* %0) {
%2 = alloca [18435 x i32]
%3 = bitcast [18435 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 = tail call i32 (...) @disable_infen()
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%14 = getelementptr inbounds [18435 x i32], [18435 x i32]* %2, i64 0, i64 0
store i32 3, i32* %14
%15 = getelementptr inbounds [18435 x i32], [18435 x i32]* %2, i64 0, i64 1
store i32 0, i32* %15
%16 = getelementptr inbounds [18435 x i32], [18435 x i32]* %2, i64 0, i64 2
store i32 0, i32* %16
%17 = call i32 @spi_transfer(i32* %14, i32 73740)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %11
%25 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds [18435 x i32], [18435 x i32]* %2, i64 0, i64 3
%34 = call i32 @fwrite(i32* %33, i32 73737, i32 1, i32* %25)
%35 = call i32 @fclose(i32* %25)
ret void
}