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

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


x86 assembly (clang, -O0):
<flash_write>:
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    $0x6,%esi
call   1030 <bbspi_transfer@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
xor    %edi,%edi
mov    $0x2,%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>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11bb <flash_write+0xab>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118f <flash_write+0x7f>
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_write>:
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    $0x6,%esi
call   1030 <bbspi_transfer@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <bbspi_transfer@plt>
xor    %edi,%edi
mov    $0x2,%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>
test   %r14d,%r14d
jle    1194 <flash_write+0x84>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%esi
xor    %edi,%edi
call   1030 <bbspi_transfer@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <flash_write+0x70>
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_write(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 6)
%10 = call i32 @bbspi_transfer(i32 1, i32 0)
%11 = call i32 @bbspi_transfer(i32 0, i32 2)
%12 = load i32, i32* %4
%13 = and i32 %12, 16711680
%14 = ashr i32 %13, 16
%15 = call i32 @bbspi_transfer(i32 0, i32 %14)
%16 = load i32, i32* %4
%17 = and i32 %16, 65280
%18 = ashr i32 %17, 8
%19 = call i32 @bbspi_transfer(i32 0, i32 %18)
%20 = load i32, i32* %4
%21 = and i32 %20, 255
%22 = call i32 @bbspi_transfer(i32 0, i32 %21)
store i32 0, i32* %7
br label %23
23: ; preds = %34, %3
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i32*, i32** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @bbspi_transfer(i32 0, i32 %32)
br label %34
34: ; preds = %27
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %23
37: ; preds = %23
%38 = call i32 @bbspi_transfer(i32 1, i32 0)
%39 = load i32, i32* %6
ret i32 %39
}

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