#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 scalar_t__ uint8_t;
typedef int uint16_t;

/* Variables and functions */
void *SPI_Read(int);
int /*<<< orphan*/ SPI_Write(int, int);
int /*<<< orphan*/ delay_us(int);
uint16_t recv(uint8_t sock, uint8_t *buf, uint16_t buflen) {
  uint16_t ptr, offaddr, realaddr;
  if (buflen <= 0 || sock != 0)
    return 1;
  if (buflen > 512)
    buflen = 512 - 2;
  ptr = SPI_Read(0x0428);
  offaddr = (((ptr & 0x00FF) << 8) + SPI_Read(0x0428 + 1));
  while (buflen) {
    buflen--;
    realaddr = 0x6000 + (offaddr & 0x07FF);
    *buf = SPI_Read(realaddr);
    offaddr++;
    buf++;
  }
  *buf = '\0';
  SPI_Write(0x0428, (offaddr & 0xFF00) >> 8);
  SPI_Write(0x0428 + 1, (offaddr & 0x00FF));
  SPI_Write(0x0401, 0x40);
  delay_us(5);
  return 1;
}


x86 assembly (clang, -O0):
<recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1158 <recv+0x28>
cmpq   $0x0,-0x10(%rbp)
je     1164 <recv+0x34>
movl   $0x1,-0x4(%rbp)
jmp    124c <recv+0x11c>
cmpl   $0x200,-0x1c(%rbp)
jle    1178 <recv+0x48>
movl   $0x1fe,-0x1c(%rbp)
mov    $0x428,%edi
call   1030 <SPI_Read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x429,%edi
call   1030 <SPI_Read@plt>
mov    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11f8 <recv+0xc8>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
and    $0x7ff,%eax
add    $0x6000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <SPI_Read@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    11a9 <recv+0x79>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x24(%rbp),%esi
and    $0xff00,%esi
sar    $0x8,%esi
mov    $0x428,%edi
call   1050 <SPI_Write@plt>
mov    -0x24(%rbp),%esi
and    $0xff,%esi
mov    $0x429,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x40,%esi
call   1050 <SPI_Write@plt>
mov    $0x5,%edi
call   1040 <delay_us@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv>:
test   %rdi,%rdi
jne    11f0 <recv+0xc0>
test   %edx,%edx
jle    11f0 <recv+0xc0>
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x201,%edx
mov    $0x1fe,%r15d
cmovb  %edx,%r15d
mov    $0x428,%edi
call   1030 <SPI_Read@plt>
shl    $0x8,%eax
movzwl %ax,%ebx
mov    $0x429,%edi
call   1030 <SPI_Read@plt>
add    %ebx,%eax
mov    %rax,%r12
xor    %ebx,%ebx
nopl   (%rax)
lea    (%r12,%rbx,1),%edi
and    $0x7ff,%edi
or     $0x6000,%edi
call   1030 <SPI_Read@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1180 <recv+0x50>
movq   $0x0,(%r14,%rbx,8)
mov    %r12,%rax
add    %ebx,%eax
movzbl %ah,%esi
mov    %rax,%rbx
mov    $0x428,%edi
call   1050 <SPI_Write@plt>
movzbl %bl,%esi
mov    $0x429,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x40,%esi
call   1050 <SPI_Write@plt>
mov    $0x5,%edi
call   1040 <delay_us@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
mov    $0x1,%eax
ret

LLVM IR (-O0):
define i32 @recv(i64 %0, i64* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %5
store i64* %1, i64** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp sle i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i64, i64* %5
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13, %3
store i32 1, i32* %4
br label %59
17: ; preds = %13
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 512
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 510, i32* %7
br label %21
21: ; preds = %20, %17
%22 = call i8* @SPI_Read(i32 1064)
%23 = ptrtoint i8* %22 to i32
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = and i32 %24, 255
%26 = shl i32 %25, 8
%27 = call i8* @SPI_Read(i32 1065)
%28 = sext i32 %26 to i64
%29 = getelementptr i8, i8* %27, i64 %28
%30 = ptrtoint i8* %29 to i32
store i32 %30, i32* %9
br label %31
31: ; preds = %34, %21
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %31
%35 = load i32, i32* %7
%36 = add nsw i32 %35, -1
store i32 %36, i32* %7
%37 = load i32, i32* %9
%38 = and i32 %37, 2047
%39 = add nsw i32 24576, %38
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = call i8* @SPI_Read(i32 %40)
%42 = ptrtoint i8* %41 to i64
%43 = load i64*, i64** %6
store i64 %42, i64* %43
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
%46 = load i64*, i64** %6
%47 = getelementptr inbounds i64, i64* %46, i32 1
store i64* %47, i64** %6
br label %31
48: ; preds = %31
%49 = load i64*, i64** %6
store i64 0, i64* %49
%50 = load i32, i32* %9
%51 = and i32 %50, 65280
%52 = ashr i32 %51, 8
%53 = call i32 @SPI_Write(i32 1064, i32 %52)
%54 = load i32, i32* %9
%55 = and i32 %54, 255
%56 = call i32 @SPI_Write(i32 1065, i32 %55)
%57 = call i32 @SPI_Write(i32 1025, i32 64)
%58 = call i32 @delay_us(i32 5)
store i32 1, i32* %4
br label %59
59: ; preds = %48, %16
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
define i32 @recv(i64 %0, i64* nocapture writeonly %1, i32 %2) {
%4 = icmp slt i32 %2, 1
%5 = icmp ne i64 %0, 0
%6 = or i1 %5, %4
br i1 %6, label %38, label %7
7: ; preds = %3
%8 = icmp ugt i32 %2, 512
%9 = select i1 %8, i32 510, i32 %2
%10 = tail call i8* @SPI_Read(i32 1064)
%11 = ptrtoint i8* %10 to i64
%12 = shl i64 %11, 8
%13 = and i64 %12, 65280
%14 = tail call i8* @SPI_Read(i32 1065)
%15 = getelementptr i8, i8* %14, i64 %13
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %7, %18
%19 = phi i32 [ %27, %18 ], [ %17, %7 ]
%20 = phi i64* [ %28, %18 ], [ %1, %7 ]
%21 = phi i32 [ %22, %18 ], [ %9, %7 ]
%22 = add nsw i32 %21, -1
%23 = and i32 %19, 2047
%24 = or i32 %23, 24576
%25 = tail call i8* @SPI_Read(i32 %24)
%26 = ptrtoint i8* %25 to i64
store i64 %26, i64* %20
%27 = add nsw i32 %19, 1
%28 = getelementptr inbounds i64, i64* %20, i64 1
%29 = icmp eq i32 %22, 0
br i1 %29, label %30, label %18
30: ; preds = %18
store i64 0, i64* %28
%31 = lshr i32 %27, 8
%32 = and i32 %31, 255
%33 = tail call i32 @SPI_Write(i32 1064, i32 %32)
%34 = and i32 %27, 255
%35 = tail call i32 @SPI_Write(i32 1065, i32 %34)
%36 = tail call i32 @SPI_Write(i32 1025, i32 64)
%37 = tail call i32 @delay_us(i32 5)
br label %38
38: ; preds = %3, %30
ret i32 1
}