#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 */

/* Variables and functions */
int spi_rx();
int spi_sendpack(int, unsigned long);
int spi_tx(int);
int write_block(char *data, unsigned long address) {
  int ret;
  while (spi_rx() != 0xff)
    ;
  ret = spi_sendpack(0x40 + 24, address);
  while (ret != 0x00)
    ret = spi_rx();
  spi_tx(0xff);
  spi_tx(0xfe);
  int i;
  for (i = 0; i < 512; ++i) {
    spi_tx(data[i]);
  }
  spi_tx(0x00);
  return spi_tx(0x00);
}


x86 assembly (clang, -O0):
<write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <spi_rx@plt>
cmp    $0xff,%eax
je     1157 <write_block+0x27>
jmp    1140 <write_block+0x10>
mov    -0x10(%rbp),%rsi
mov    $0x58,%edi
call   1050 <spi_sendpack@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1181 <write_block+0x51>
mov    $0x0,%al
call   1040 <spi_rx@plt>
mov    %eax,-0x14(%rbp)
jmp    1168 <write_block+0x38>
mov    $0xff,%edi
call   1030 <spi_tx@plt>
mov    $0xfe,%edi
call   1030 <spi_tx@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x200,-0x18(%rbp)
jge    11c8 <write_block+0x98>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <spi_tx@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119c <write_block+0x6c>
xor    %edi,%edi
call   1030 <spi_tx@plt>
xor    %edi,%edi
call   1030 <spi_tx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <spi_rx@plt>
cmp    $0xff,%eax
jne    1140 <write_block+0x10>
mov    $0x58,%edi
mov    %r14,%rsi
call   1050 <spi_sendpack@plt>
test   %eax,%eax
je     116b <write_block+0x3b>
nop
xor    %eax,%eax
call   1040 <spi_rx@plt>
test   %eax,%eax
jne    1160 <write_block+0x30>
mov    $0xff,%edi
call   1030 <spi_tx@plt>
mov    $0xfe,%edi
call   1030 <spi_tx@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <spi_tx@plt>
add    $0x1,%rbx
cmp    $0x200,%rbx
jne    1190 <write_block+0x60>
xor    %edi,%edi
call   1030 <spi_tx@plt>
xor    %edi,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <spi_tx@plt>

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

LLVM IR (-O2):
define i32 @write_block(i8* nocapture %0, i64 %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32 (...) @spi_rx()
%5 = icmp eq i32 %4, 255
br i1 %5, label %6, label %3
6: ; preds = %3
%7 = tail call i32 @spi_sendpack(i32 88, i64 %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6, %9
%10 = tail call i32 (...) @spi_rx()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %9
12: ; preds = %9, %6
%13 = tail call i32 @spi_tx(i32 255)
%14 = tail call i32 @spi_tx(i32 254)
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %21, %15 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = tail call i32 @spi_tx(i32 %19)
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, 512
br i1 %22, label %23, label %15
23: ; preds = %15
%24 = tail call i32 @spi_tx(i32 0)
%25 = tail call i32 @spi_tx(i32 0)
ret i32 %25
}