#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 int uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ cs_low();
int spi(int);
int sdmmc_cmd(uint8_t cmd_code, uint32_t arg) {
  int i;
  uint8_t result;
  cs_low();
  while (spi(0xff) != 0xff)
    ;
  spi(0x40 | cmd_code);
  spi((arg >> 24) & 0xff);
  spi((arg >> 16) & 0xff);
  spi((arg >> 8) & 0xff);
  spi(arg & 0xff);
  spi(cmd_code == 8 ? 0x87 : 0x95);
  if (cmd_code == 12) {
    spi(0xff);
  }
  for (i = 0; i < 256; i++) {
    result = spi(0xff);
    if ((result & 0x80) == 0) {
      return result;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<sdmmc_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <cs_low@plt>
mov    $0xff,%edi
call   1030 <spi@plt>
cmp    $0xff,%eax
je     114f <sdmmc_cmd+0x2f>
jmp    1135 <sdmmc_cmd+0x15>
mov    -0x8(%rbp),%edi
or     $0x40,%edi
call   1030 <spi@plt>
mov    -0xc(%rbp),%edi
sar    $0x18,%edi
and    $0xff,%edi
call   1030 <spi@plt>
mov    -0xc(%rbp),%edi
sar    $0x10,%edi
and    $0xff,%edi
call   1030 <spi@plt>
mov    -0xc(%rbp),%edi
sar    $0x8,%edi
and    $0xff,%edi
call   1030 <spi@plt>
mov    -0xc(%rbp),%edi
and    $0xff,%edi
call   1030 <spi@plt>
mov    -0x8(%rbp),%ecx
mov    $0x95,%edi
mov    $0x87,%eax
cmp    $0x8,%ecx
cmove  %eax,%edi
call   1030 <spi@plt>
cmpl   $0xc,-0x8(%rbp)
jne    11c7 <sdmmc_cmd+0xa7>
mov    $0xff,%edi
call   1030 <spi@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x100,-0x10(%rbp)
jge    1217 <sdmmc_cmd+0xf7>
mov    $0xff,%edi
call   1030 <spi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1204 <sdmmc_cmd+0xe4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <sdmmc_cmd+0xfe>
jmp    1209 <sdmmc_cmd+0xe9>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ce <sdmmc_cmd+0xae>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdmmc_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <cs_low@plt>
xchg   %ax,%ax
mov    $0xff,%edi
call   1030 <spi@plt>
cmp    $0xff,%eax
jne    1130 <sdmmc_cmd+0x10>
mov    %ebp,%edi
or     $0x40,%edi
call   1030 <spi@plt>
mov    %ebx,%eax
shr    $0x18,%eax
mov    %eax,%edi
call   1030 <spi@plt>
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%edi
call   1030 <spi@plt>
movzbl %bh,%edi
call   1030 <spi@plt>
movzbl %bl,%edi
call   1030 <spi@plt>
cmp    $0x8,%ebp
mov    $0x87,%eax
mov    $0x95,%edi
cmove  %eax,%edi
call   1030 <spi@plt>
cmp    $0xc,%ebp
jne    1198 <sdmmc_cmd+0x78>
mov    $0xff,%edi
call   1030 <spi@plt>
mov    $0x100,%ebx
nopl   (%rax)
mov    $0xff,%edi
call   1030 <spi@plt>
test   $0x80,%al
je     11b8 <sdmmc_cmd+0x98>
add    $0xffffffff,%ebx
jne    11a0 <sdmmc_cmd+0x80>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sdmmc_cmd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32 (...) @cs_low()
br label %9
9: ; preds = %12, %2
%10 = call i32 @spi(i32 255)
%11 = icmp ne i32 %10, 255
br i1 %11, label %12, label %13
12: ; preds = %9
br label %9
13: ; preds = %9
%14 = load i32, i32* %4
%15 = or i32 64, %14
%16 = call i32 @spi(i32 %15)
%17 = load i32, i32* %5
%18 = ashr i32 %17, 24
%19 = and i32 %18, 255
%20 = call i32 @spi(i32 %19)
%21 = load i32, i32* %5
%22 = ashr i32 %21, 16
%23 = and i32 %22, 255
%24 = call i32 @spi(i32 %23)
%25 = load i32, i32* %5
%26 = ashr i32 %25, 8
%27 = and i32 %26, 255
%28 = call i32 @spi(i32 %27)
%29 = load i32, i32* %5
%30 = and i32 %29, 255
%31 = call i32 @spi(i32 %30)
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 8
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 135, i32 149
%36 = call i32 @spi(i32 %35)
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 12
br i1 %38, label %39, label %41
39: ; preds = %13
%40 = call i32 @spi(i32 255)
br label %41
41: ; preds = %39, %13
store i32 0, i32* %6
br label %42
42: ; preds = %53, %41
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 256
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = call i32 @spi(i32 255)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = and i32 %47, 128
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = load i32, i32* %7
store i32 %51, i32* %3
br label %57
52: ; preds = %45
br label %53
53: ; preds = %52
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %42
56: ; preds = %42
store i32 -1, i32* %3
br label %57
57: ; preds = %56, %50
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
define i32 @sdmmc_cmd(i32 %0, i32 %1) {
%3 = tail call i32 (...) @cs_low()
br label %4
4: ; preds = %4, %2
%5 = tail call i32 @spi(i32 255)
%6 = icmp eq i32 %5, 255
br i1 %6, label %7, label %4
7: ; preds = %4
%8 = or i32 %0, 64
%9 = tail call i32 @spi(i32 %8)
%10 = lshr i32 %1, 24
%11 = tail call i32 @spi(i32 %10)
%12 = lshr i32 %1, 16
%13 = and i32 %12, 255
%14 = tail call i32 @spi(i32 %13)
%15 = lshr i32 %1, 8
%16 = and i32 %15, 255
%17 = tail call i32 @spi(i32 %16)
%18 = and i32 %1, 255
%19 = tail call i32 @spi(i32 %18)
%20 = icmp eq i32 %0, 8
%21 = select i1 %20, i32 135, i32 149
%22 = tail call i32 @spi(i32 %21)
%23 = icmp eq i32 %0, 12
br i1 %23, label %24, label %26
24: ; preds = %7
%25 = tail call i32 @spi(i32 255)
br label %26
26: ; preds = %24, %7
br label %30
27: ; preds = %30
%28 = add nuw nsw i32 %31, 1
%29 = icmp eq i32 %28, 256
br i1 %29, label %35, label %30
30: ; preds = %26, %27
%31 = phi i32 [ %28, %27 ], [ 0, %26 ]
%32 = tail call i32 @spi(i32 255)
%33 = and i32 %32, 128
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %27
35: ; preds = %27, %30
%36 = phi i32 [ %32, %30 ], [ -1, %27 ]
ret i32 %36
}