#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ bbspi_transfer(int, int);
int flash_read(uint32_t addr, uint8_t *buf, int count) {
  int i;
  bbspi_transfer(1, 0);
  bbspi_transfer(0, 0x0b);
  bbspi_transfer(0, (addr & 0xFF0000) >> 16);
  bbspi_transfer(0, (addr & 0xFF00) >> 8);
  bbspi_transfer(0, (addr & 0xFF));
  bbspi_transfer(0, 0);
  for (i = 0; i < count; i++) {
    buf[i] = bbspi_transfer(0, 0);
  }
  bbspi_transfer(1, 0);
  return count;
}


x86 assembly (clang, -O0):
<flash_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
xor    %edi,%edi
mov    $0xb,%esi
call   1030 <bbspi_transfer@plt>
mov    -0x4(%rbp),%esi
and    $0xff0000,%esi
sar    $0x10,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
mov    -0x4(%rbp),%esi
and    $0xff00,%esi
sar    $0x8,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
mov    -0x4(%rbp),%esi
and    $0xff,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1030 <bbspi_transfer@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b0 <flash_read+0xa0>
xor    %esi,%esi
mov    %esi,%edi
call   1030 <bbspi_transfer@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <flash_read+0x70>
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
xor    %edi,%edi
mov    $0xb,%esi
call   1030 <bbspi_transfer@plt>
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
movzbl %bh,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
movzbl %bl,%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
test   %r14d,%r14d
jle    1186 <flash_read+0x76>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   (%rax)
xor    %edi,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <flash_read+0x60>
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @flash_read(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%8 = call i32 @bbspi_transfer(i32 1, i32 0)
%9 = call i32 @bbspi_transfer(i32 0, i32 11)
%10 = load i32, i32* %4
%11 = and i32 %10, 16711680
%12 = ashr i32 %11, 16
%13 = call i32 @bbspi_transfer(i32 0, i32 %12)
%14 = load i32, i32* %4
%15 = and i32 %14, 65280
%16 = ashr i32 %15, 8
%17 = call i32 @bbspi_transfer(i32 0, i32 %16)
%18 = load i32, i32* %4
%19 = and i32 %18, 255
%20 = call i32 @bbspi_transfer(i32 0, i32 %19)
%21 = call i32 @bbspi_transfer(i32 0, i32 0)
store i32 0, i32* %7
br label %22
22: ; preds = %32, %3
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = call i32 @bbspi_transfer(i32 0, i32 0)
%28 = load i32*, i32** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
store i32 %27, i32* %31
br label %32
32: ; preds = %26
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %22
35: ; preds = %22
%36 = call i32 @bbspi_transfer(i32 1, i32 0)
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
define i32 @flash_read(i32 %0, i32* nocapture writeonly %1, i32 returned %2) {
%4 = tail call i32 @bbspi_transfer(i32 1, i32 0)
%5 = tail call i32 @bbspi_transfer(i32 0, i32 11)
%6 = lshr i32 %0, 16
%7 = and i32 %6, 255
%8 = tail call i32 @bbspi_transfer(i32 0, i32 %7)
%9 = lshr i32 %0, 8
%10 = and i32 %9, 255
%11 = tail call i32 @bbspi_transfer(i32 0, i32 %10)
%12 = and i32 %0, 255
%13 = tail call i32 @bbspi_transfer(i32 0, i32 %12)
%14 = tail call i32 @bbspi_transfer(i32 0, i32 0)
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %24
16: ; preds = %3
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %22, %18 ]
%20 = tail call i32 @bbspi_transfer(i32 0, i32 0)
%21 = getelementptr inbounds i32, i32* %1, i64 %19
store i32 %20, i32* %21
%22 = add nuw nsw i64 %19, 1
%23 = icmp eq i64 %22, %17
br i1 %23, label %24, label %18
24: ; preds = %18, %3
%25 = tail call i32 @bbspi_transfer(i32 1, i32 0)
ret i32 %2
}