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

/* Variables and functions */
int /*<<< orphan*/ c2_addr_write(int);
int /*<<< orphan*/ check_status();
int /*<<< orphan*/ fpdat_read();
int /*<<< orphan*/ fpdat_write(size_t);
void flash_block_read(uint16_t addr, void *data, size_t size) {
  int i;
  if (!size)
    return;
  c2_addr_write(0xb4);
  fpdat_write(0x06);
  check_status();
  fpdat_write(addr >> 8);
  fpdat_write(addr);
  fpdat_write(size);
  check_status();
  for (i = 0; i != size; i++)
    ((uint8_t *)data)[i] = fpdat_read();
}


x86 assembly (clang, -O0):
<flash_block_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1163 <flash_block_read+0x23>
jmp    11dc <flash_block_read+0x9c>
mov    $0xb4,%edi
call   1040 <c2_addr_write@plt>
mov    $0x6,%edi
call   1060 <fpdat_write@plt>
mov    $0x0,%al
call   1030 <check_status@plt>
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
movslq %eax,%rdi
call   1060 <fpdat_write@plt>
movslq -0x4(%rbp),%rdi
call   1060 <fpdat_write@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fpdat_write@plt>
mov    $0x0,%al
call   1030 <check_status@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11dc <flash_block_read+0x9c>
mov    $0x0,%al
call   1050 <fpdat_read@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ac <flash_block_read+0x6c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_block_read>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     11b4 <flash_block_read+0x74>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0xb4,%edi
call   1040 <c2_addr_write@plt>
mov    $0x6,%edi
call   1060 <fpdat_write@plt>
xor    %eax,%eax
call   1030 <check_status@plt>
mov    %ebp,%eax
sar    $0x8,%eax
movslq %eax,%rdi
call   1060 <fpdat_write@plt>
movslq %ebp,%rdi
call   1060 <fpdat_write@plt>
mov    %rbx,%rdi
call   1060 <fpdat_write@plt>
xor    %eax,%eax
call   1030 <check_status@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <fpdat_read@plt>
mov    %eax,(%r14,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11a0 <flash_block_read+0x60>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @flash_block_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i64, i64* %6
%9 = icmp ne i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %3
br label %40
11: ; preds = %3
%12 = call i32 @c2_addr_write(i32 180)
%13 = call i32 @fpdat_write(i64 6)
%14 = call i32 (...) @check_status()
%15 = load i32, i32* %4
%16 = ashr i32 %15, 8
%17 = sext i32 %16 to i64
%18 = call i32 @fpdat_write(i64 %17)
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = call i32 @fpdat_write(i64 %20)
%22 = load i64, i64* %6
%23 = call i32 @fpdat_write(i64 %22)
%24 = call i32 (...) @check_status()
store i32 0, i32* %7
br label %25
25: ; preds = %37, %11
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = load i64, i64* %6
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %25
%31 = call i32 (...) @fpdat_read()
%32 = load i8*, i8** %5
%33 = bitcast i8* %32 to i32*
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
store i32 %31, i32* %36
br label %37
37: ; preds = %30
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %25
40: ; preds = %10, %25
ret void
}

LLVM IR (-O2):
define void @flash_block_read(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3
%6 = tail call i32 @c2_addr_write(i32 180)
%7 = tail call i32 @fpdat_write(i64 6)
%8 = tail call i32 (...) @check_status()
%9 = ashr i32 %0, 8
%10 = sext i32 %9 to i64
%11 = tail call i32 @fpdat_write(i64 %10)
%12 = sext i32 %0 to i64
%13 = tail call i32 @fpdat_write(i64 %12)
%14 = tail call i32 @fpdat_write(i64 %2)
%15 = tail call i32 (...) @check_status()
%16 = bitcast i8* %1 to i32*
br label %17
17: ; preds = %5, %17
%18 = phi i64 [ 0, %5 ], [ %21, %17 ]
%19 = tail call i32 (...) @fpdat_read()
%20 = getelementptr inbounds i32, i32* %16, i64 %18
store i32 %19, i32* %20
%21 = add nuw i64 %18, 1
%22 = icmp eq i64 %21, %2
br i1 %22, label %23, label %17
23: ; preds = %17, %3
ret void
}