#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_deassert_cs();
unsigned char sd_rcv_byte();
int /*<<< orphan*/ sd_send_byte(int);
int sd_send_command(unsigned char cmd, unsigned char response_type,
                    unsigned char *response, unsigned char *argument) {
  int i;
  char response_length;
  unsigned char tmp;
  sd_assert_cs();
  sd_send_byte((cmd & 0x3F) | 0x40);
  for (i = 3; i >= 0; i--)
    sd_send_byte(argument[i]);
  sd_send_byte(0x95);
  response_length = 0;
  switch (response_type) {
  case 1:
  case 2:
    response_length = 1;
    break;
  case 3:
    response_length = 2;
    break;
  case 4:
    response_length = 5;
    break;
  default:
    break;
  }
  i = 0;
  do {
    tmp = sd_rcv_byte();
    i++;
  } while (((tmp & 0x80) != 0) && i < 100);
  if (i >= 100) {
    sd_deassert_cs();
    return 0;
  }
  for (i = response_length - 1; i >= 0; i--) {
    response[i] = tmp;
    tmp = sd_rcv_byte();
  }
  i = 0;
  if (response_type == 2) {
    do {
      i++;
      tmp = sd_rcv_byte();
    } while (tmp != 0xFF);
    sd_send_byte(0xFF);
  }
  sd_deassert_cs();
  return 1;
}


x86 assembly (clang, -O0):
<sd_send_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %dil,%sil
mov    %sil,-0x5(%rbp)
mov    %al,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    $0x0,%al
call   1040 <sd_assert_cs@plt>
movzbl -0x5(%rbp),%edi
and    $0x3f,%edi
or     $0x40,%edi
call   1030 <sd_send_byte@plt>
movl   $0x3,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11a3 <sd_send_command+0x63>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <sd_send_byte@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    117a <sd_send_command+0x3a>
mov    $0x95,%edi
call   1030 <sd_send_byte@plt>
movb   $0x0,-0x1d(%rbp)
movzbl -0x6(%rbp),%eax
mov    %eax,-0x24(%rbp)
add    $0xffffffff,%eax
sub    $0x2,%eax
jb     11eb <sd_send_command+0xab>
jmp    11c9 <sd_send_command+0x89>
mov    -0x24(%rbp),%eax
sub    $0x3,%eax
je     11f4 <sd_send_command+0xb4>
jmp    11da <sd_send_command+0x9a>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
je     11fd <sd_send_command+0xbd>
jmp    1206 <sd_send_command+0xc6>
movb   $0x1,-0x1d(%rbp)
jmp    120b <sd_send_command+0xcb>
movb   $0x2,-0x1d(%rbp)
jmp    120b <sd_send_command+0xcb>
movb   $0x5,-0x1d(%rbp)
jmp    120b <sd_send_command+0xcb>
jmp    120b <sd_send_command+0xcb>
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <sd_rcv_byte@plt>
mov    %al,-0x1e(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movzbl -0x1e(%rbp),%ecx
and    $0x80,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
je     1247 <sd_send_command+0x107>
cmpl   $0x64,-0x1c(%rbp)
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1212 <sd_send_command+0xd2>
cmpl   $0x64,-0x1c(%rbp)
jl     126f <sd_send_command+0x12f>
mov    $0x0,%al
call   1060 <sd_deassert_cs@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fc <sd_send_command+0x1bc>
movsbl -0x1d(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     12a9 <sd_send_command+0x169>
mov    -0x1e(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    $0x0,%al
call   1050 <sd_rcv_byte@plt>
mov    %al,-0x1e(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1279 <sd_send_command+0x139>
movl   $0x0,-0x1c(%rbp)
movzbl -0x6(%rbp),%eax
cmp    $0x2,%eax
jne    12ee <sd_send_command+0x1ae>
jmp    12c2 <sd_send_command+0x182>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <sd_rcv_byte@plt>
mov    %al,-0x1e(%rbp)
movzbl -0x1e(%rbp),%eax
cmp    $0xff,%eax
jne    12c2 <sd_send_command+0x182>
mov    $0xff,%edi
call   1030 <sd_send_byte@plt>
mov    $0x0,%al
call   1060 <sd_deassert_cs@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_send_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%ebx
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <sd_assert_cs@plt>
and    $0x3f,%bl
or     $0x40,%bl
movzbl %bl,%edi
call   1030 <sd_send_byte@plt>
movzbl 0x3(%rbp),%edi
call   1030 <sd_send_byte@plt>
movzbl 0x2(%rbp),%edi
call   1030 <sd_send_byte@plt>
movzbl 0x1(%rbp),%edi
call   1030 <sd_send_byte@plt>
movzbl 0x0(%rbp),%edi
call   1030 <sd_send_byte@plt>
mov    $0x95,%edi
call   1030 <sd_send_byte@plt>
lea    -0x1(%r14),%eax
cmp    $0x2,%al
jb     11bc <sd_send_command+0x7c>
cmp    $0x3,%r14b
je     11c3 <sd_send_command+0x83>
mov    $0xffffffff,%ebx
cmp    $0x4,%r14b
jne    11cb <sd_send_command+0x8b>
mov    $0x4,%ebx
jmp    11c8 <sd_send_command+0x88>
mov    $0x1,%r12b
xor    %ebx,%ebx
jmp    11cb <sd_send_command+0x8b>
mov    $0x1,%ebx
mov    $0x1,%r12b
mov    $0xffffffff,%ebp
add    $0x1,%ebp
xor    %eax,%eax
call   1050 <sd_rcv_byte@plt>
test   %al,%al
jns    11e3 <sd_send_command+0xa3>
cmp    $0x63,%ebp
jb     11d0 <sd_send_command+0x90>
xor    %r13d,%r13d
cmp    $0x62,%ebp
ja     1235 <sd_send_command+0xf5>
test   %r12b,%r12b
je     1206 <sd_send_command+0xc6>
mov    %al,(%r15,%rbx,1)
xor    %eax,%eax
call   1050 <sd_rcv_byte@plt>
add    $0xffffffffffffffff,%rbx
lea    0x1(%rbx),%ecx
test   %ecx,%ecx
jg     11f0 <sd_send_command+0xb0>
mov    $0x1,%r13d
cmp    $0x2,%r14b
jne    1235 <sd_send_command+0xf5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <sd_rcv_byte@plt>
cmp    $0xff,%al
jne    1220 <sd_send_command+0xe0>
mov    $0xff,%edi
call   1030 <sd_send_byte@plt>
xor    %eax,%eax
call   1060 <sd_deassert_cs@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sd_send_command(i8 zeroext %0, i8 zeroext %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
store i8 %0, i8* %6
store i8 %1, i8* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = call i32 (...) @sd_assert_cs()
%14 = load i8, i8* %6
%15 = zext i8 %14 to i32
%16 = and i32 %15, 63
%17 = or i32 %16, 64
%18 = call i32 @sd_send_byte(i32 %17)
store i32 3, i32* %10
br label %19
19: ; preds = %30, %4
%20 = load i32, i32* %10
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = call i32 @sd_send_byte(i32 %28)
br label %30
30: ; preds = %22
%31 = load i32, i32* %10
%32 = add nsw i32 %31, -1
store i32 %32, i32* %10
br label %19
33: ; preds = %19
%34 = call i32 @sd_send_byte(i32 149)
store i8 0, i8* %11
%35 = load i8, i8* %7
%36 = zext i8 %35 to i32
switch i32 %36, label %40 [
i32 1, label %37
i32 2, label %37
i32 3, label %38
i32 4, label %39
]
37: ; preds = %33, %33
store i8 1, i8* %11
br label %41
38: ; preds = %33
store i8 2, i8* %11
br label %41
39: ; preds = %33
store i8 5, i8* %11
br label %41
40: ; preds = %33
br label %41
41: ; preds = %40, %39, %38, %37
store i32 0, i32* %10
br label %42
42: ; preds = %54, %41
%43 = call zeroext i8 (...) @sd_rcv_byte()
store i8 %43, i8* %12
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
br label %46
46: ; preds = %42
%47 = load i8, i8* %12
%48 = zext i8 %47 to i32
%49 = and i32 %48, 128
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = load i32, i32* %10
%53 = icmp slt i32 %52, 100
br label %54
54: ; preds = %51, %46
%55 = phi i1 [ false, %46 ], [ %53, %51 ]
br i1 %55, label %42, label %56
56: ; preds = %54
%57 = load i32, i32* %10
%58 = icmp sge i32 %57, 100
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 (...) @sd_deassert_cs()
store i32 0, i32* %5
br label %95
61: ; preds = %56
%62 = load i8, i8* %11
%63 = sext i8 %62 to i32
%64 = sub nsw i32 %63, 1
store i32 %64, i32* %10
br label %65
65: ; preds = %75, %61
%66 = load i32, i32* %10
%67 = icmp sge i32 %66, 0
br i1 %67, label %68, label %78
68: ; preds = %65
%69 = load i8, i8* %12
%70 = load i8*, i8** %8
%71 = load i32, i32* %10
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8 %69, i8* %73
%74 = call zeroext i8 (...) @sd_rcv_byte()
store i8 %74, i8* %12
br label %75
75: ; preds = %68
%76 = load i32, i32* %10
%77 = add nsw i32 %76, -1
store i32 %77, i32* %10
br label %65
78: ; preds = %65
store i32 0, i32* %10
%79 = load i8, i8* %7
%80 = zext i8 %79 to i32
%81 = icmp eq i32 %80, 2
br i1 %81, label %82, label %93
82: ; preds = %78
br label %83
83: ; preds = %87, %82
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 1
store i32 %85, i32* %10
%86 = call zeroext i8 (...) @sd_rcv_byte()
store i8 %86, i8* %12
br label %87
87: ; preds = %83
%88 = load i8, i8* %12
%89 = zext i8 %88 to i32
%90 = icmp ne i32 %89, 255
br i1 %90, label %83, label %91
91: ; preds = %87
%92 = call i32 @sd_send_byte(i32 255)
br label %93
93: ; preds = %91, %78
%94 = call i32 (...) @sd_deassert_cs()
store i32 1, i32* %5
br label %95
95: ; preds = %93, %59
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
define i32 @sd_send_command(i8 zeroext %0, i8 zeroext %1, i8* nocapture writeonly %2, i8* nocapture %3) {
%5 = tail call i32 (...) @sd_assert_cs()
%6 = and i8 %0, 63
%7 = or i8 %6, 64
%8 = zext i8 %7 to i32
%9 = tail call i32 @sd_send_byte(i32 %8)
%10 = getelementptr inbounds i8, i8* %3, i64 3
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = tail call i32 @sd_send_byte(i32 %12)
%14 = getelementptr inbounds i8, i8* %3, i64 2
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = tail call i32 @sd_send_byte(i32 %16)
%18 = getelementptr inbounds i8, i8* %3, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = tail call i32 @sd_send_byte(i32 %20)
%22 = load i8, i8* %3
%23 = zext i8 %22 to i32
%24 = tail call i32 @sd_send_byte(i32 %23)
%25 = tail call i32 @sd_send_byte(i32 149)
switch i8 %1, label %29 [
i8 1, label %26
i8 2, label %26
i8 3, label %27
i8 4, label %28
]
26: ; preds = %4, %4
br label %29
27: ; preds = %4
br label %29
28: ; preds = %4
br label %29
29: ; preds = %4, %28, %27, %26
%30 = phi i1 [ false, %4 ], [ true, %28 ], [ true, %27 ], [ true, %26 ]
%31 = phi i64 [ 4294967295, %4 ], [ 4, %28 ], [ 1, %27 ], [ 0, %26 ]
br label %32
32: ; preds = %32, %29
%33 = phi i32 [ 0, %29 ], [ %35, %32 ]
%34 = tail call zeroext i8 (...) @sd_rcv_byte()
%35 = add nuw nsw i32 %33, 1
%36 = icmp slt i8 %34, 0
%37 = icmp ult i32 %33, 99
%38 = select i1 %36, i1 %37, i1 false
br i1 %38, label %32, label %39
39: ; preds = %32
%40 = icmp ugt i32 %33, 98
br i1 %40, label %57, label %41
41: ; preds = %39
br i1 %30, label %42, label %50
42: ; preds = %41, %42
%43 = phi i64 [ %49, %42 ], [ %31, %41 ]
%44 = phi i8 [ %46, %42 ], [ %34, %41 ]
%45 = getelementptr inbounds i8, i8* %2, i64 %43
store i8 %44, i8* %45
%46 = tail call zeroext i8 (...) @sd_rcv_byte()
%47 = trunc i64 %43 to i32
%48 = icmp sgt i32 %47, 0
%49 = add nsw i64 %43, -1
br i1 %48, label %42, label %50
50: ; preds = %42, %41
%51 = icmp eq i8 %1, 2
br i1 %51, label %52, label %57
52: ; preds = %50, %52
%53 = tail call zeroext i8 (...) @sd_rcv_byte()
%54 = icmp eq i8 %53, -1
br i1 %54, label %55, label %52
55: ; preds = %52
%56 = tail call i32 @sd_send_byte(i32 255)
br label %57
57: ; preds = %50, %55, %39
%58 = phi i32 [ 0, %39 ], [ 1, %55 ], [ 1, %50 ]
%59 = tail call i32 (...) @sd_deassert_cs()
ret i32 %58
}