#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 /*<<< orphan*/ OSD_Puts(char *);
int SPI_PUMP();
int cmd_write(int, unsigned long);
int sd_read_sector(unsigned long lba, unsigned char *buf) {
  int result = 0;
  int i;
  int r;
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  {
    while ((*(volatile unsigned int *)(0xFFFFFFD0 + 0x0) & (1 << 15)))
      ;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x0) = (1 | (1 << 8));
  };
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  r = cmd_write(0xff0051, lba);
  if (r != 0) {
    OSD_Puts("Read failed\n");
    return (result);
  }
  i = 1500000;
  while (--i) {
    int v;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
    v = (*(volatile unsigned int *)(0xFFFFFFD0 + 0x04) & 255);
    if (v == 0xfe) {
      int j;
      for (j = 0; j < 128; ++j) {
        int t, v;
        t = SPI_PUMP();
        *(int *)buf = t;
        buf += 4;
      }
      i = 1;
      result = 1;
    }
  }
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  {
    while ((*(volatile unsigned int *)(0xFFFFFFD0 + 0x0) & (1 << 15)))
      ;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x0) = (0);
  };
  return (result);
}


x86 assembly (clang, -O0):
<sd_read_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd0,%eax
mov    (%rax),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     116c <sd_read_sector+0x3c>
jmp    1152 <sd_read_sector+0x22>
mov    $0xffffffd0,%eax
movl   $0x101,(%rax)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    -0x10(%rbp),%rsi
mov    $0xff0051,%edi
call   1050 <cmd_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11b4 <sd_read_sector+0x84>
lea    0xe5c(%rip),%rdi
call   1040 <OSD_Puts@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1289 <sd_read_sector+0x159>
movl   $0x16e360,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1248 <sd_read_sector+0x118>
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
mov    (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0xfe,-0x28(%rbp)
jne    1243 <sd_read_sector+0x113>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x80,-0x2c(%rbp)
jge    1235 <sd_read_sector+0x105>
mov    $0x0,%al
call   1030 <SPI_PUMP@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11fb <sd_read_sector+0xcb>
movl   $0x1,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
jmp    11bb <sd_read_sector+0x8b>
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd0,%eax
mov    (%rax),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1278 <sd_read_sector+0x148>
jmp    125e <sd_read_sector+0x12e>
mov    $0xffffffd0,%eax
movl   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_read_sector>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rsi
mov    $0xffffffd4,%r15d
movl   $0xff,(%r15)
nopl   0x0(%rax,%rax,1)
testl  $0x8000,-0x4(%r15)
jne    1150 <sd_read_sector+0x20>
movl   $0x101,-0x4(%r15)
movl   $0xff,(%r15)
mov    $0xff0051,%edi
call   1050 <cmd_write@plt>
test   %eax,%eax
je     118a <sd_read_sector+0x5a>
lea    0xe82(%rip),%rdi
call   1040 <OSD_Puts@plt>
xor    %eax,%eax
jmp    1222 <sd_read_sector+0xf2>
mov    $0xffe91ca4,%eax
nop
movl   $0xff,(%r15)
mov    (%r15),%ecx
cmp    $0xfe,%cl
je     11d5 <sd_read_sector+0xa5>
movl   $0xff,(%r15)
mov    (%r15),%ecx
cmp    $0xfe,%cl
je     11d5 <sd_read_sector+0xa5>
movl   $0xff,(%r15)
mov    (%r15),%ecx
cmp    $0xfe,%cl
je     11d5 <sd_read_sector+0xa5>
test   %eax,%eax
je     11fe <sd_read_sector+0xce>
movl   $0xff,(%r15)
mov    (%r15),%ecx
cmp    $0xfe,%cl
je     11d5 <sd_read_sector+0xa5>
add    $0x4,%eax
jmp    1190 <sd_read_sector+0x60>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <SPI_PUMP@plt>
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    $0x80,%ebx
jne    11e0 <sd_read_sector+0xb0>
mov    $0x1,%eax
jmp    1200 <sd_read_sector+0xd0>
xor    %eax,%eax
movl   $0xff,(%r15)
movl   $0xff,(%r15)
xchg   %ax,%ax
testl  $0x8000,-0x4(%r15)
jne    1210 <sd_read_sector+0xe0>
movl   $0x0,-0x4(%r15)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Read failed\0A\00"
define i32 @sd_read_sector(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %13
13: ; preds = %17, %2
%14 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%15 = and i32 %14, 32768
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %13
18: ; preds = %13
store volatile i32 257, i32* inttoptr (i64 4294967248 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%19 = load i64, i64* %4
%20 = call i32 @cmd_write(i32 16711761, i64 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @OSD_Puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %60
26: ; preds = %18
store i32 1500000, i32* %7
br label %27
27: ; preds = %51, %26
%28 = load i32, i32* %7
%29 = add nsw i32 %28, -1
store i32 %29, i32* %7
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %52
31: ; preds = %27
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%32 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%33 = and i32 %32, 255
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 254
br i1 %35, label %36, label %51
36: ; preds = %31
store i32 0, i32* %10
br label %37
37: ; preds = %47, %36
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 128
br i1 %39, label %40, label %50
40: ; preds = %37
%41 = call i32 (...) @SPI_PUMP()
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = load i8*, i8** %5
%44 = bitcast i8* %43 to i32*
store i32 %42, i32* %44
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i64 4
store i8* %46, i8** %5
br label %47
47: ; preds = %40
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
br label %37
50: ; preds = %37
store i32 1, i32* %7
store i32 1, i32* %6
br label %51
51: ; preds = %50, %31
br label %27
52: ; preds = %27
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %53
53: ; preds = %57, %52
%54 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%55 = and i32 %54, 32768
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %53
br label %53
58: ; preds = %53
store volatile i32 0, i32* inttoptr (i64 4294967248 to i32*)
%59 = load i32, i32* %6
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %23
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [13 x i8] c"Read failed\0A\00"
define i32 @sd_read_sector(i64 %0, i8* nocapture writeonly %1) {
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %3
3: ; preds = %3, %2
%4 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%5 = and i32 %4, 32768
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %3
7: ; preds = %3
store volatile i32 257, i32* inttoptr (i64 4294967248 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%8 = tail call i32 @cmd_write(i32 16711761, i64 %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @OSD_Puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %49
12: ; preds = %7, %40
%13 = phi i32 [ %41, %40 ], [ 1499999, %7 ]
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%14 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%15 = and i32 %14, 255
%16 = icmp eq i32 %15, 254
br i1 %16, label %17, label %26
17: ; preds = %36, %30, %26, %12
br label %18
18: ; preds = %17, %18
%19 = phi i8* [ %23, %18 ], [ %1, %17 ]
%20 = phi i32 [ %24, %18 ], [ 0, %17 ]
%21 = tail call i32 (...) @SPI_PUMP()
%22 = bitcast i8* %19 to i32*
store i32 %21, i32* %22
%23 = getelementptr inbounds i8, i8* %19, i64 4
%24 = add nuw nsw i32 %20, 1
%25 = icmp eq i32 %24, 128
br i1 %25, label %42, label %18
26: ; preds = %12
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%27 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%28 = and i32 %27, 255
%29 = icmp eq i32 %28, 254
br i1 %29, label %17, label %30
30: ; preds = %26
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%31 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%32 = and i32 %31, 255
%33 = icmp eq i32 %32, 254
br i1 %33, label %17, label %34
34: ; preds = %30
%35 = icmp eq i32 %13, 3
br i1 %35, label %42, label %36
36: ; preds = %34
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%37 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%38 = and i32 %37, 255
%39 = icmp eq i32 %38, 254
br i1 %39, label %17, label %40
40: ; preds = %36
%41 = add nsw i32 %13, -4
br label %12
42: ; preds = %34, %18
%43 = phi i32 [ 1, %18 ], [ 0, %34 ]
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %44
44: ; preds = %44, %42
%45 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%46 = and i32 %45, 32768
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %44
48: ; preds = %44
store volatile i32 0, i32* inttoptr (i64 4294967248 to i32*)
br label %49
49: ; preds = %48, %10
%50 = phi i32 [ 0, %10 ], [ %43, %48 ]
ret i32 %50
}