#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*/ sd_assert_cs();
int /*<<< orphan*/ sd_packarg(unsigned char *, unsigned int);
unsigned char sd_rcv_byte();
int /*<<< orphan*/ sd_send_byte(int);
scalar_t__ sd_send_command(int, int, unsigned char *, unsigned char *);
int sd_read_block(unsigned int blockaddr, unsigned char *buf) {
  unsigned char response[5];
  unsigned char argument[4];
  unsigned int i = 0;
  unsigned char tmp;
  blockaddr <<= 9;
  sd_packarg(argument, blockaddr);
  if (sd_send_command(17, 1, response, argument) == 0)
    return 0;
  if (response[0] != 0)
    return 0;
  sd_assert_cs();
  i = 0;
  do {
    tmp = sd_rcv_byte();
    i++;
  } while ((tmp == 0xFF) && (i < 1000));
  if ((i >= 1000) || ((tmp & 0xE0) == 0)) {
    sd_send_byte(0xFF);
    return 0;
  }
  for (i = 0; i < 512; i++)
    buf[i] = sd_rcv_byte();
  return 1;
}


x86 assembly (clang, -O0):
<sd_read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x9,%eax
mov    %eax,-0x8(%rbp)
lea    -0x19(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1070 <sd_packarg@plt>
lea    -0x15(%rbp),%rdx
lea    -0x19(%rbp),%rcx
mov    $0x11,%edi
mov    $0x1,%esi
call   1040 <sd_send_command@plt>
cmp    $0x0,%rax
jne    11a8 <sd_read_block+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1280 <sd_read_block+0x130>
movzbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     11c1 <sd_read_block+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1280 <sd_read_block+0x130>
mov    $0x0,%al
call   1050 <sd_assert_cs@plt>
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <sd_rcv_byte@plt>
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movzbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0xff,%ecx
mov    %al,-0x22(%rbp)
jne    1204 <sd_read_block+0xb4>
cmpl   $0x3e8,-0x20(%rbp)
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11cf <sd_read_block+0x7f>
cmpl   $0x3e8,-0x20(%rbp)
jae    122e <sd_read_block+0xde>
movzbl -0x21(%rbp),%eax
and    $0xe0,%eax
cmp    $0x0,%eax
jne    1244 <sd_read_block+0xf4>
mov    $0xff,%edi
call   1030 <sd_send_byte@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <sd_read_block+0x130>
movl   $0x0,-0x20(%rbp)
cmpl   $0x200,-0x20(%rbp)
jae    1279 <sd_read_block+0x129>
mov    $0x0,%al
call   1060 <sd_rcv_byte@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    124b <sd_read_block+0xfb>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_read_block>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%esi
shl    $0x9,%esi
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1070 <sd_packarg@plt>
lea    0x7(%rsp),%rdx
mov    $0x11,%edi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <sd_send_command@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     120a <sd_read_block+0xba>
cmpb   $0x0,0x7(%rsp)
jne    120a <sd_read_block+0xba>
xor    %eax,%eax
call   1050 <sd_assert_cs@plt>
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <sd_rcv_byte@plt>
cmp    $0xff,%al
jne    11c6 <sd_read_block+0x76>
cmp    $0x3e7,%ebp
jb     11b0 <sd_read_block+0x60>
cmp    $0x3e6,%ebp
ja     11fe <sd_read_block+0xae>
cmp    $0x20,%al
jb     11fe <sd_read_block+0xae>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <sd_rcv_byte@plt>
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0x200,%rbp
jne    11e0 <sd_read_block+0x90>
mov    $0x1,%eax
jmp    120a <sd_read_block+0xba>
mov    $0xff,%edi
call   1030 <sd_send_byte@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sd_read_block(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [5 x i8]
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %8
%10 = load i32, i32* %4
%11 = shl i32 %10, 9
store i32 %11, i32* %4
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %4
%14 = call i32 @sd_packarg(i8* %12, i32 %13)
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%17 = call i64 @sd_send_command(i32 17, i32 1, i8* %15, i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 0, i32* %3
br label %65
20: ; preds = %2
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 0, i32* %3
br label %65
26: ; preds = %20
%27 = call i32 (...) @sd_assert_cs()
store i32 0, i32* %8
br label %28
28: ; preds = %39, %26
%29 = call zeroext i8 (...) @sd_rcv_byte()
store i8 %29, i8* %9
%30 = load i32, i32* %8
%31 = add i32 %30, 1
store i32 %31, i32* %8
br label %32
32: ; preds = %28
%33 = load i8, i8* %9
%34 = zext i8 %33 to i32
%35 = icmp eq i32 %34, 255
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %8
%38 = icmp ult i32 %37, 1000
br label %39
39: ; preds = %36, %32
%40 = phi i1 [ false, %32 ], [ %38, %36 ]
br i1 %40, label %28, label %41
41: ; preds = %39
%42 = load i32, i32* %8
%43 = icmp uge i32 %42, 1000
br i1 %43, label %49, label %44
44: ; preds = %41
%45 = load i8, i8* %9
%46 = zext i8 %45 to i32
%47 = and i32 %46, 224
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44, %41
%50 = call i32 @sd_send_byte(i32 255)
store i32 0, i32* %3
br label %65
51: ; preds = %44
store i32 0, i32* %8
br label %52
52: ; preds = %61, %51
%53 = load i32, i32* %8
%54 = icmp ult i32 %53, 512
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = call zeroext i8 (...) @sd_rcv_byte()
%57 = load i8*, i8** %5
%58 = load i32, i32* %8
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 %56, i8* %60
br label %61
61: ; preds = %55
%62 = load i32, i32* %8
%63 = add i32 %62, 1
store i32 %63, i32* %8
br label %52
64: ; preds = %52
store i32 1, i32* %3
br label %65
65: ; preds = %64, %49, %25, %19
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
define i32 @sd_read_block(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca [5 x i8]
%4 = alloca [4 x i8]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%7 = shl i32 %0, 9
%8 = call i32 @sd_packarg(i8* %6, i32 %7)
%9 = call i64 @sd_send_command(i32 17, i32 1, i8* %5, i8* %6)
%10 = icmp ne i64 %9, 0
%11 = load i8, i8* %5
%12 = icmp eq i8 %11, 0
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %35
14: ; preds = %2
%15 = call i32 (...) @sd_assert_cs()
br label %16
16: ; preds = %16, %14
%17 = phi i32 [ 0, %14 ], [ %19, %16 ]
%18 = call zeroext i8 (...) @sd_rcv_byte()
%19 = add nuw nsw i32 %17, 1
%20 = icmp eq i8 %18, -1
%21 = icmp ult i32 %17, 999
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %16, label %23
23: ; preds = %16
%24 = icmp ugt i32 %17, 998
%25 = icmp ult i8 %18, 32
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @sd_send_byte(i32 255)
br label %35
29: ; preds = %23, %29
%30 = phi i64 [ %33, %29 ], [ 0, %23 ]
%31 = call zeroext i8 (...) @sd_rcv_byte()
%32 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 %31, i8* %32
%33 = add nuw nsw i64 %30, 1
%34 = icmp eq i64 %33, 512
br i1 %34, label %35, label %29
35: ; preds = %29, %2, %27
%36 = phi i32 [ 0, %27 ], [ 0, %2 ], [ 1, %29 ]
ret i32 %36
}