#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 */
int SPI_Read(int);
int /*<<< orphan*/ SPI_Write(int, int);
int /*<<< orphan*/ delay_ms(int);
int /*<<< orphan*/ disconnect(scalar_t__);
uint16_t send(uint8_t sock, uint8_t *buf, uint16_t buflen) {
  uint16_t ptr, offaddr, realaddr, txsize, timeout;
  if (buflen <= 0 || sock != 0)
    return 0;
  txsize = SPI_Read(0x0420);
  txsize = (((txsize & 0x00FF) << 8) + SPI_Read(0x0420 + 1));
  timeout = 0;
  while (txsize < buflen) {
    delay_ms(1);
    txsize = SPI_Read(0x0420);
    txsize = (((txsize & 0x00FF) << 8) + SPI_Read(0x0420 + 1));
    if (timeout++ > 1000) {
      disconnect(sock);
      return 0;
    }
  }
  ptr = SPI_Read(0x0424);
  offaddr = (((ptr & 0x00FF) << 8) + SPI_Read(0x0424 + 1));
  while (buflen) {
    buflen--;
    realaddr = 0x4000 + (offaddr & 0x07FF);
    SPI_Write(realaddr, *buf);
    offaddr++;
    buf++;
  }
  SPI_Write(0x0424, (offaddr & 0xFF00) >> 8);
  SPI_Write(0x0424 + 1, (offaddr & 0x00FF));
  SPI_Write(0x0401, 0x20);
  while (SPI_Read(0x0401))
    ;
  return 1;
}


x86 assembly (clang, -O0):
<send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1168 <send+0x28>
cmpq   $0x0,-0x10(%rbp)
je     1174 <send+0x34>
movl   $0x0,-0x4(%rbp)
jmp    12f3 <send+0x1b3>
mov    $0x420,%edi
call   1030 <SPI_Read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    $0x421,%edi
call   1030 <SPI_Read@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121f <send+0xdf>
mov    $0x1,%edi
call   1040 <delay_ms@plt>
mov    $0x420,%edi
call   1030 <SPI_Read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
mov    %eax,-0x38(%rbp)
mov    $0x421,%edi
call   1030 <SPI_Read@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x30(%rbp)
cmp    $0x3e8,%eax
jle    121a <send+0xda>
mov    -0x10(%rbp),%rdi
call   1060 <disconnect@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f3 <send+0x1b3>
jmp    11aa <send+0x6a>
mov    $0x424,%edi
call   1030 <SPI_Read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
mov    %eax,-0x3c(%rbp)
mov    $0x425,%edi
call   1030 <SPI_Read@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     129c <send+0x15c>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
and    $0x7ff,%eax
add    $0x4000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%esi
call   1050 <SPI_Write@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    124e <send+0x10e>
mov    -0x24(%rbp),%esi
and    $0xff00,%esi
sar    $0x8,%esi
mov    $0x424,%edi
call   1050 <SPI_Write@plt>
mov    -0x24(%rbp),%esi
and    $0xff,%esi
mov    $0x425,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x20,%esi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
call   1030 <SPI_Read@plt>
cmp    $0x0,%eax
je     12ec <send+0x1ac>
jmp    12d4 <send+0x194>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
jne    1273 <send+0x133>
mov    %edx,%r12d
test   %edx,%edx
jle    1273 <send+0x133>
mov    %rsi,%r14
mov    $0x420,%edi
call   1030 <SPI_Read@plt>
shl    $0x8,%eax
movzwl %ax,%ebx
mov    $0x421,%edi
call   1030 <SPI_Read@plt>
add    %ebx,%eax
mov    $0x3ea,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r12d,%eax
jge    11ce <send+0x8e>
mov    $0x1,%edi
call   1040 <delay_ms@plt>
mov    $0x420,%edi
call   1030 <SPI_Read@plt>
shl    $0x8,%eax
movzwl %ax,%ebp
mov    $0x421,%edi
call   1030 <SPI_Read@plt>
add    %ebp,%eax
add    $0xffffffff,%ebx
jne    1190 <send+0x50>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <disconnect@plt>
jmp    1273 <send+0x133>
mov    $0x424,%edi
call   1030 <SPI_Read@plt>
shl    $0x8,%eax
movzwl %ax,%ebx
mov    $0x425,%edi
call   1030 <SPI_Read@plt>
add    %ebx,%eax
test   %r12d,%r12d
je     1226 <send+0xe6>
mov    %eax,%r13d
mov    %eax,%r15d
mov    %r12d,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%edi
and    $0x7ff,%edi
or     $0x4000,%edi
mov    (%r14,%rbp,8),%esi
call   1050 <SPI_Write@plt>
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1200 <send+0xc0>
mov    %r13d,%eax
add    %ebp,%eax
movzbl %ah,%esi
mov    $0x424,%edi
mov    %eax,%ebx
call   1050 <SPI_Write@plt>
movzbl %bl,%esi
mov    $0x425,%edi
call   1050 <SPI_Write@plt>
mov    $0x401,%edi
mov    $0x20,%esi
call   1050 <SPI_Write@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x401,%edi
call   1030 <SPI_Read@plt>
test   %eax,%eax
jne    1260 <send+0x120>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send(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
%11 = alloca i32
%12 = alloca i32
store i64 %0, i64* %5
store i64* %1, i64** %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i64, i64* %5
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %3
store i32 0, i32* %4
br label %84
19: ; preds = %15
%20 = call i32 @SPI_Read(i32 1056)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = and i32 %21, 255
%23 = shl i32 %22, 8
%24 = call i32 @SPI_Read(i32 1057)
%25 = add nsw i32 %23, %24
store i32 %25, i32* %11
store i32 0, i32* %12
br label %26
26: ; preds = %44, %19
%27 = load i32, i32* %11
%28 = load i32, i32* %7
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = call i32 @delay_ms(i32 1)
%32 = call i32 @SPI_Read(i32 1056)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = and i32 %33, 255
%35 = shl i32 %34, 8
%36 = call i32 @SPI_Read(i32 1057)
%37 = add nsw i32 %35, %36
store i32 %37, i32* %11
%38 = load i32, i32* %12
%39 = add nsw i32 %38, 1
store i32 %39, i32* %12
%40 = icmp sgt i32 %38, 1000
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i64, i64* %5
%43 = call i32 @disconnect(i64 %42)
store i32 0, i32* %4
br label %84
44: ; preds = %30
br label %26
45: ; preds = %26
%46 = call i32 @SPI_Read(i32 1060)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = and i32 %47, 255
%49 = shl i32 %48, 8
%50 = call i32 @SPI_Read(i32 1061)
%51 = add nsw i32 %49, %50
store i32 %51, i32* %9
br label %52
52: ; preds = %55, %45
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %70
55: ; preds = %52
%56 = load i32, i32* %7
%57 = add nsw i32 %56, -1
store i32 %57, i32* %7
%58 = load i32, i32* %9
%59 = and i32 %58, 2047
%60 = add nsw i32 16384, %59
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = load i64*, i64** %6
%63 = load i64, i64* %62
%64 = trunc i64 %63 to i32
%65 = call i32 @SPI_Write(i32 %61, i32 %64)
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1
store i32 %67, i32* %9
%68 = load i64*, i64** %6
%69 = getelementptr inbounds i64, i64* %68, i32 1
store i64* %69, i64** %6
br label %52
70: ; preds = %52
%71 = load i32, i32* %9
%72 = and i32 %71, 65280
%73 = ashr i32 %72, 8
%74 = call i32 @SPI_Write(i32 1060, i32 %73)
%75 = load i32, i32* %9
%76 = and i32 %75, 255
%77 = call i32 @SPI_Write(i32 1061, i32 %76)
%78 = call i32 @SPI_Write(i32 1025, i32 32)
br label %79
79: ; preds = %82, %70
%80 = call i32 @SPI_Read(i32 1025)
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %79
br label %79
83: ; preds = %79
store i32 1, i32* %4
br label %84
84: ; preds = %83, %41, %18
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
define i32 @send(i64 %0, i64* nocapture %1, i32 %2) {
%4 = icmp slt i32 %2, 1
%5 = icmp ne i64 %0, 0
%6 = or i1 %5, %4
br i1 %6, label %59, label %7
7: ; preds = %3
%8 = tail call i32 @SPI_Read(i32 1056)
%9 = shl i32 %8, 8
%10 = and i32 %9, 65280
%11 = tail call i32 @SPI_Read(i32 1057)
%12 = add nsw i32 %10, %11
br label %13
13: ; preds = %17, %7
%14 = phi i32 [ %12, %7 ], [ %23, %17 ]
%15 = phi i32 [ 0, %7 ], [ %24, %17 ]
%16 = icmp slt i32 %14, %2
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = tail call i32 @delay_ms(i32 1)
%19 = tail call i32 @SPI_Read(i32 1056)
%20 = shl i32 %19, 8
%21 = and i32 %20, 65280
%22 = tail call i32 @SPI_Read(i32 1057)
%23 = add nsw i32 %21, %22
%24 = add nuw nsw i32 %15, 1
%25 = icmp eq i32 %24, 1002
br i1 %25, label %26, label %13
26: ; preds = %17
%27 = tail call i32 @disconnect(i64 0)
br label %59
28: ; preds = %13
%29 = tail call i32 @SPI_Read(i32 1060)
%30 = shl i32 %29, 8
%31 = and i32 %30, 65280
%32 = tail call i32 @SPI_Read(i32 1061)
%33 = add nsw i32 %31, %32
%34 = icmp eq i32 %2, 0
br i1 %34, label %48, label %35
35: ; preds = %28, %35
%36 = phi i32 [ %45, %35 ], [ %33, %28 ]
%37 = phi i64* [ %46, %35 ], [ %1, %28 ]
%38 = phi i32 [ %39, %35 ], [ %2, %28 ]
%39 = add nsw i32 %38, -1
%40 = and i32 %36, 2047
%41 = or i32 %40, 16384
%42 = load i64, i64* %37
%43 = trunc i64 %42 to i32
%44 = tail call i32 @SPI_Write(i32 %41, i32 %43)
%45 = add nsw i32 %36, 1
%46 = getelementptr inbounds i64, i64* %37, i64 1
%47 = icmp eq i32 %39, 0
br i1 %47, label %48, label %35
48: ; preds = %35, %28
%49 = phi i32 [ %33, %28 ], [ %45, %35 ]
%50 = lshr i32 %49, 8
%51 = and i32 %50, 255
%52 = tail call i32 @SPI_Write(i32 1060, i32 %51)
%53 = and i32 %49, 255
%54 = tail call i32 @SPI_Write(i32 1061, i32 %53)
%55 = tail call i32 @SPI_Write(i32 1025, i32 32)
br label %56
56: ; preds = %56, %48
%57 = tail call i32 @SPI_Read(i32 1025)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %56
59: ; preds = %56, %3, %26
%60 = phi i32 [ 0, %26 ], [ 0, %3 ], [ 1, %56 ]
ret i32 %60
}