#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 read_block(char *data, unsigned long address) {
  int ret;
  while (spi_rx() != 0xff)
    ;
  ret = spi_sendpack(0x40 + 17, address);
  while (ret != 0x00)
    ret = spi_rx();
  while (spi_rx() != 0xfe)
    ;
  int i;
  for (i = 0; i < 512; ++i) {
    data[i] = spi_rx();
  }
  int checksum = spi_rx() << 8;
  checksum |= spi_rx();
  return 0;
}


x86 assembly (clang, -O0):
<read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <spi_rx@plt>
cmp    $0xff,%eax
je     1147 <read_block+0x27>
jmp    1130 <read_block+0x10>
mov    -0x10(%rbp),%rsi
mov    $0x51,%edi
call   1040 <spi_sendpack@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1171 <read_block+0x51>
mov    $0x0,%al
call   1030 <spi_rx@plt>
mov    %eax,-0x14(%rbp)
jmp    1158 <read_block+0x38>
jmp    1176 <read_block+0x56>
mov    $0x0,%al
call   1030 <spi_rx@plt>
cmp    $0xfe,%eax
je     118d <read_block+0x6d>
jmp    1176 <read_block+0x56>
movl   $0x0,-0x18(%rbp)
cmpl   $0x200,-0x18(%rbp)
jge    11c3 <read_block+0xa3>
mov    $0x0,%al
call   1030 <spi_rx@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1194 <read_block+0x74>
mov    $0x0,%al
call   1030 <spi_rx@plt>
shl    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <spi_rx@plt>
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <spi_rx@plt>
cmp    $0xff,%eax
jne    1130 <read_block+0x10>
mov    $0x51,%edi
mov    %r14,%rsi
call   1040 <spi_sendpack@plt>
test   %eax,%eax
je     1160 <read_block+0x40>
nop
xor    %eax,%eax
call   1030 <spi_rx@plt>
test   %eax,%eax
jne    1150 <read_block+0x30>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <spi_rx@plt>
cmp    $0xfe,%eax
jne    1160 <read_block+0x40>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <spi_rx@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x200,%rbx
jne    1170 <read_block+0x50>
xor    %eax,%eax
call   1030 <spi_rx@plt>
xor    %eax,%eax
call   1030 <spi_rx@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @read_block(i8* nocapture writeonly %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 81, 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
br label %13
13: ; preds = %12, %13
%14 = tail call i32 (...) @spi_rx()
%15 = icmp eq i32 %14, 254
br i1 %15, label %16, label %13
16: ; preds = %13, %16
%17 = phi i64 [ %21, %16 ], [ 0, %13 ]
%18 = tail call i32 (...) @spi_rx()
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 %19, i8* %20
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, 512
br i1 %22, label %23, label %16
23: ; preds = %16
%24 = tail call i32 (...) @spi_rx()
%25 = tail call i32 (...) @spi_rx()
ret i32 0
}