void spi_read(long offset, long len, long adr) {
  unsigned char *p = (unsigned char *)adr;
  int cansend, i;
  volatile unsigned long *fifo = (volatile unsigned long *)0xd4035010;
  volatile unsigned long *stat = (volatile unsigned long *)0xd4035008;
  unsigned long regval;
  *(volatile unsigned long *)0xd401901c = 0x00004000;
  *(volatile unsigned long *)0xd4019058 = 0x00004000;
  *(volatile unsigned long *)0xd401e10c = 0xc0;
  ;
  *(volatile unsigned long *)0xd4019028 = 0x00004000;
  *fifo = (unsigned long)0x03;
  *fifo = (offset >> 16) & 0xff;
  *fifo = (offset >> 8) & 0xff;
  *fifo = offset & 0xff;
  *fifo = 0;
  *fifo = 0;
  for (i = 0; i < 4; i++) {
    while ((*stat & 8) == 0)
      ;
    regval = *fifo;
  }
  while (len > 0) {
    *fifo = 0;
    while ((*stat & 8) == 0)
      ;
    regval = *fifo;
    *p++ = (unsigned char)regval;
    len--;
  }
  while ((*stat & 8) == 0)
    ;
  regval = *fifo;
  while ((*stat & 8) == 0)
    ;
  regval = *fifo;
  *(volatile unsigned long *)0xd401901c = 0x00004000;
}


x86 assembly (clang, -O0):
<spi_read>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0xd4035010,%eax
mov    %rax,-0x30(%rbp)
mov    $0xd4035008,%eax
mov    %rax,-0x38(%rbp)
mov    $0xd401901c,%eax
movq   $0x4000,(%rax)
mov    $0xd4019058,%eax
movq   $0x4000,(%rax)
mov    $0xd401e10c,%eax
movq   $0xc0,(%rax)
mov    $0xd4019028,%eax
movq   $0x4000,(%rax)
mov    -0x30(%rbp),%rax
movq   $0x3,(%rax)
mov    -0x8(%rbp),%rcx
sar    $0x10,%rcx
and    $0xff,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
sar    $0x8,%rcx
and    $0xff,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
and    $0xff,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    1202 <spi_read+0x102>
jmp    11cf <spi_read+0xcf>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
and    $0x8,%rax
cmp    $0x0,%rax
jne    11e9 <spi_read+0xe9>
jmp    11cf <spi_read+0xcf>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c0 <spi_read+0xc0>
jmp    1207 <spi_read+0x107>
cmpq   $0x0,-0x10(%rbp)
jle    126a <spi_read+0x16a>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
and    $0x8,%rax
cmp    $0x0,%rax
jne    1237 <spi_read+0x137>
jmp    121d <spi_read+0x11d>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1207 <spi_read+0x107>
jmp    126f <spi_read+0x16f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
and    $0x8,%rax
cmp    $0x0,%rax
jne    1289 <spi_read+0x189>
jmp    126f <spi_read+0x16f>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
and    $0x8,%rax
cmp    $0x0,%rax
jne    12ae <spi_read+0x1ae>
jmp    1294 <spi_read+0x194>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0xd401901c,%eax
movq   $0x4000,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_read>:
push   %rbx
mov    %rdi,%rcx
mov    $0xd4035010,%eax
movq   $0x4000,-0x1bff4(%rax)
movq   $0x4000,-0x1bfb8(%rax)
movq   $0xc0,-0x16f04(%rax)
movq   $0x4000,-0x1bfe8(%rax)
movq   $0x3,(%rax)
movzbl %ch,%ebx
movzbl %cl,%edi
shr    $0x10,%ecx
movzbl %cl,%ecx
mov    %rcx,(%rax)
mov    %rbx,(%rax)
mov    %rdi,(%rax)
movq   $0x0,(%rax)
movq   $0x0,(%rax)
nop
testq  $0x8,-0x8(%rax)
je     1160 <spi_read+0x60>
mov    (%rax),%rcx
nopl   (%rax)
testq  $0x8,-0x8(%rax)
je     1170 <spi_read+0x70>
mov    (%rax),%rcx
nopl   (%rax)
testq  $0x8,-0x8(%rax)
je     1180 <spi_read+0x80>
mov    (%rax),%rcx
nopl   (%rax)
testq  $0x8,-0x8(%rax)
je     1190 <spi_read+0x90>
mov    (%rax),%rcx
test   %rsi,%rsi
jle    1240 <spi_read+0x140>
mov    %rsi,%rcx
test   $0x1,%sil
je     11d7 <spi_read+0xd7>
movq   $0x0,(%rax)
cs nopw 0x0(%rax,%rax,1)
testq  $0x8,-0x8(%rax)
je     11c0 <spi_read+0xc0>
mov    (%rax),%rcx
mov    %cl,(%rdx)
add    $0x1,%rdx
lea    -0x1(%rsi),%rcx
cmp    $0x1,%rsi
je     1240 <spi_read+0x140>
nopl   (%rax)
movq   $0x0,(%rax)
nopw   0x0(%rax,%rax,1)
testq  $0x8,-0x8(%rax)
je     11f0 <spi_read+0xf0>
mov    (%rax),%rsi
mov    %sil,(%rdx)
movq   $0x0,(%rax)
nopw   0x0(%rax,%rax,1)
testq  $0x8,-0x8(%rax)
je     1210 <spi_read+0x110>
mov    (%rax),%rsi
mov    %sil,0x1(%rdx)
add    $0x2,%rdx
lea    -0x2(%rcx),%rsi
cmp    $0x2,%rcx
mov    %rsi,%rcx
jg     11e0 <spi_read+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
testq  $0x8,-0x8(%rax)
je     1240 <spi_read+0x140>
mov    (%rax),%rcx
nopl   (%rax)
testq  $0x8,-0x8(%rax)
je     1250 <spi_read+0x150>
mov    (%rax),%rcx
movq   $0x4000,-0x1bff4(%rax)
pop    %rbx
ret

LLVM IR (-O0):
define void @spi_read(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64*
%11 = alloca i64*
%12 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%13 = load i64, i64* %6
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %7
store i64* inttoptr (i64 3556986896 to i64*), i64** %10
store i64* inttoptr (i64 3556986888 to i64*), i64** %11
store volatile i64 16384, i64* inttoptr (i64 3556872220 to i64*)
store volatile i64 16384, i64* inttoptr (i64 3556872280 to i64*)
store volatile i64 192, i64* inttoptr (i64 3556892940 to i64*)
store volatile i64 16384, i64* inttoptr (i64 3556872232 to i64*)
%15 = load i64*, i64** %10
store volatile i64 3, i64* %15
%16 = load i64, i64* %4
%17 = ashr i64 %16, 16
%18 = and i64 %17, 255
%19 = load i64*, i64** %10
store volatile i64 %18, i64* %19
%20 = load i64, i64* %4
%21 = ashr i64 %20, 8
%22 = and i64 %21, 255
%23 = load i64*, i64** %10
store volatile i64 %22, i64* %23
%24 = load i64, i64* %4
%25 = and i64 %24, 255
%26 = load i64*, i64** %10
store volatile i64 %25, i64* %26
%27 = load i64*, i64** %10
store volatile i64 0, i64* %27
%28 = load i64*, i64** %10
store volatile i64 0, i64* %28
store i32 0, i32* %9
br label %29
29: ; preds = %42, %3
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %45
32: ; preds = %29
br label %33
33: ; preds = %38, %32
%34 = load i64*, i64** %11
%35 = load volatile i64, i64* %34
%36 = and i64 %35, 8
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %33
39: ; preds = %33
%40 = load i64*, i64** %10
%41 = load volatile i64, i64* %40
store i64 %41, i64* %12
br label %42
42: ; preds = %39
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %29
45: ; preds = %29
br label %46
46: ; preds = %57, %45
%47 = load i64, i64* %5
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %46
%50 = load i64*, i64** %10
store volatile i64 0, i64* %50
br label %51
51: ; preds = %56, %49
%52 = load i64*, i64** %11
%53 = load volatile i64, i64* %52
%54 = and i64 %53, 8
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %51
br label %51
57: ; preds = %51
%58 = load i64*, i64** %10
%59 = load volatile i64, i64* %58
store i64 %59, i64* %12
%60 = load i64, i64* %12
%61 = trunc i64 %60 to i8
%62 = load i8*, i8** %7
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %7
store i8 %61, i8* %62
%64 = load i64, i64* %5
%65 = add nsw i64 %64, -1
store i64 %65, i64* %5
br label %46
66: ; preds = %46
br label %67
67: ; preds = %72, %66
%68 = load i64*, i64** %11
%69 = load volatile i64, i64* %68
%70 = and i64 %69, 8
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
br label %67
73: ; preds = %67
%74 = load i64*, i64** %10
%75 = load volatile i64, i64* %74
store i64 %75, i64* %12
br label %76
76: ; preds = %81, %73
%77 = load i64*, i64** %11
%78 = load volatile i64, i64* %77
%79 = and i64 %78, 8
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %76
br label %76
82: ; preds = %76
%83 = load i64*, i64** %10
%84 = load volatile i64, i64* %83
store i64 %84, i64* %12
store volatile i64 16384, i64* inttoptr (i64 3556872220 to i64*)
ret void
}

LLVM IR (-O2):
define void @spi_read(i64 %0, i64 %1, i64 %2) {
store volatile i64 16384, i64* inttoptr (i64 3556872220 to i64*)
store volatile i64 16384, i64* inttoptr (i64 3556872280 to i64*)
store volatile i64 192, i64* inttoptr (i64 3556892940 to i64*)
store volatile i64 16384, i64* inttoptr (i64 3556872232 to i64*)
store volatile i64 3, i64* inttoptr (i64 3556986896 to i64*)
%4 = lshr i64 %0, 16
%5 = and i64 %4, 255
store volatile i64 %5, i64* inttoptr (i64 3556986896 to i64*)
%6 = lshr i64 %0, 8
%7 = and i64 %6, 255
store volatile i64 %7, i64* inttoptr (i64 3556986896 to i64*)
%8 = and i64 %0, 255
store volatile i64 %8, i64* inttoptr (i64 3556986896 to i64*)
store volatile i64 0, i64* inttoptr (i64 3556986896 to i64*)
store volatile i64 0, i64* inttoptr (i64 3556986896 to i64*)
br label %27
9: ; preds = %49
%10 = inttoptr i64 %2 to i8*
%11 = and i64 %1, 1
%12 = icmp eq i64 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %9
store volatile i64 0, i64* inttoptr (i64 3556986896 to i64*)
br label %14
14: ; preds = %14, %13
%15 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%16 = and i64 %15, 8
%17 = icmp eq i64 %16, 0
br i1 %17, label %14, label %18
18: ; preds = %14
%19 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %20, i8* %10
%22 = add nsw i64 %1, -1
br label %23
23: ; preds = %18, %9
%24 = phi i64 [ %1, %9 ], [ %22, %18 ]
%25 = phi i8* [ %10, %9 ], [ %21, %18 ]
%26 = icmp eq i64 %1, 1
br i1 %26, label %73, label %52
27: ; preds = %3, %27
%28 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%29 = and i64 %28, 8
%30 = icmp eq i64 %29, 0
br i1 %30, label %27, label %31
31: ; preds = %27
%32 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
br label %33
33: ; preds = %33, %31
%34 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%35 = and i64 %34, 8
%36 = icmp eq i64 %35, 0
br i1 %36, label %33, label %37
37: ; preds = %33
%38 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
br label %39
39: ; preds = %39, %37
%40 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%41 = and i64 %40, 8
%42 = icmp eq i64 %41, 0
br i1 %42, label %39, label %43
43: ; preds = %39
%44 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
br label %45
45: ; preds = %45, %43
%46 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%47 = and i64 %46, 8
%48 = icmp eq i64 %47, 0
br i1 %48, label %45, label %49
49: ; preds = %45
%50 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
%51 = icmp sgt i64 %1, 0
br i1 %51, label %9, label %73
52: ; preds = %23, %67
%53 = phi i64 [ %71, %67 ], [ %24, %23 ]
%54 = phi i8* [ %70, %67 ], [ %25, %23 ]
store volatile i64 0, i64* inttoptr (i64 3556986896 to i64*)
br label %55
55: ; preds = %55, %52
%56 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%57 = and i64 %56, 8
%58 = icmp eq i64 %57, 0
br i1 %58, label %55, label %59
59: ; preds = %55
%60 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
%61 = trunc i64 %60 to i8
%62 = getelementptr inbounds i8, i8* %54, i64 1
store i8 %61, i8* %54
store volatile i64 0, i64* inttoptr (i64 3556986896 to i64*)
br label %63
63: ; preds = %63, %59
%64 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%65 = and i64 %64, 8
%66 = icmp eq i64 %65, 0
br i1 %66, label %63, label %67
67: ; preds = %63
%68 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
%69 = trunc i64 %68 to i8
%70 = getelementptr inbounds i8, i8* %54, i64 2
store i8 %69, i8* %62
%71 = add nsw i64 %53, -2
%72 = icmp sgt i64 %53, 2
br i1 %72, label %52, label %73
73: ; preds = %23, %67, %49
br label %74
74: ; preds = %73, %74
%75 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%76 = and i64 %75, 8
%77 = icmp eq i64 %76, 0
br i1 %77, label %74, label %78
78: ; preds = %74
%79 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
br label %80
80: ; preds = %80, %78
%81 = load volatile i64, i64* inttoptr (i64 3556986888 to i64*)
%82 = and i64 %81, 8
%83 = icmp eq i64 %82, 0
br i1 %83, label %80, label %84
84: ; preds = %80
%85 = load volatile i64, i64* inttoptr (i64 3556986896 to i64*)
store volatile i64 16384, i64* inttoptr (i64 3556872220 to i64*)
ret void
}