#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 */
unsigned int readl(int);
int /*<<< orphan*/ writel(int, unsigned int);
unsigned int spi_sendcmd(unsigned int cmd, unsigned int data, int tx, int rx) {
  unsigned int s;
  do {
    s = readl(0x11300000);
  } while (s & 0x00010000);
  writel(0x11300008, data);
  writel(0x11300004, cmd);
  s = 0x03000100 | tx | (rx << 4);
  writel(0x11300000, s);
  do {
    s = readl(0x11300000);
  } while (s & 0x00010000);
  s = readl(0x11300008);
  switch (rx) {
  case 1:
    s &= 0xff;
    break;
  case 2:
    s &= 0xffff;
    break;
  case 3:
    s &= 0xffffff;
    break;
  case 4:
    s &= 0xffffffff;
    break;
  default:
    s = 0;
    break;
  }
  return s;
}


x86 assembly (clang, -O0):
<spi_sendcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    $0x11300000,%edi
call   1030 <readl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
jne    1134 <spi_sendcmd+0x14>
mov    -0x8(%rbp),%esi
mov    $0x11300008,%edi
call   1040 <writel@plt>
mov    -0x4(%rbp),%esi
mov    $0x11300004,%edi
call   1040 <writel@plt>
mov    -0xc(%rbp),%eax
or     $0x3000100,%eax
mov    -0x10(%rbp),%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    $0x11300000,%edi
call   1040 <writel@plt>
mov    $0x11300000,%edi
call   1030 <readl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
jne    118c <spi_sendcmd+0x6c>
mov    $0x11300008,%edi
call   1030 <readl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x3,%eax
ja     121e <spi_sendcmd+0xfe>
mov    -0x20(%rbp),%rax
lea    0xe29(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x14(%rbp)
jmp    1225 <spi_sendcmd+0x105>
mov    -0x14(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1225 <spi_sendcmd+0x105>
mov    -0x14(%rbp),%eax
and    $0xffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1225 <spi_sendcmd+0x105>
mov    -0x14(%rbp),%eax
and    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1225 <spi_sendcmd+0x105>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_sendcmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x11300000,%edi
call   1030 <readl@plt>
test   $0x10000,%eax
jne    1140 <spi_sendcmd+0x20>
mov    $0x11300008,%edi
mov    %ebp,%esi
call   1040 <writel@plt>
mov    $0x11300004,%edi
mov    %r15d,%esi
call   1040 <writel@plt>
mov    %ebx,%esi
shl    $0x4,%esi
or     %r14d,%esi
or     $0x3000100,%esi
mov    $0x11300000,%edi
call   1040 <writel@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x11300000,%edi
call   1030 <readl@plt>
test   $0x10000,%eax
jne    1190 <spi_sendcmd+0x70>
mov    $0x11300008,%edi
call   1030 <readl@plt>
add    $0xffffffff,%ebx
cmp    $0x3,%ebx
ja     11c8 <spi_sendcmd+0xa8>
lea    0xe46(%rip),%rcx
movslq (%rcx,%rbx,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
movzbl %al,%eax
jmp    11d6 <spi_sendcmd+0xb6>
xor    %eax,%eax
jmp    11d6 <spi_sendcmd+0xb6>
movzwl %ax,%eax
jmp    11d6 <spi_sendcmd+0xb6>
and    $0xffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @spi_sendcmd(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
br label %10
10: ; preds = %12, %4
%11 = call i32 @readl(i32 288358400)
store i32 %11, i32* %9
br label %12
12: ; preds = %10
%13 = load i32, i32* %9
%14 = and i32 %13, 65536
%15 = icmp ne i32 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %12
%17 = load i32, i32* %6
%18 = call i32 @writel(i32 288358408, i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @writel(i32 288358404, i32 %19)
%21 = load i32, i32* %7
%22 = or i32 50331904, %21
%23 = load i32, i32* %8
%24 = shl i32 %23, 4
%25 = or i32 %22, %24
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = call i32 @writel(i32 288358400, i32 %26)
br label %28
28: ; preds = %30, %16
%29 = call i32 @readl(i32 288358400)
store i32 %29, i32* %9
br label %30
30: ; preds = %28
%31 = load i32, i32* %9
%32 = and i32 %31, 65536
%33 = icmp ne i32 %32, 0
br i1 %33, label %28, label %34
34: ; preds = %30
%35 = call i32 @readl(i32 288358408)
store i32 %35, i32* %9
%36 = load i32, i32* %8
switch i32 %36, label %49 [
i32 1, label %37
i32 2, label %40
i32 3, label %43
i32 4, label %46
]
37: ; preds = %34
%38 = load i32, i32* %9
%39 = and i32 %38, 255
store i32 %39, i32* %9
br label %50
40: ; preds = %34
%41 = load i32, i32* %9
%42 = and i32 %41, 65535
store i32 %42, i32* %9
br label %50
43: ; preds = %34
%44 = load i32, i32* %9
%45 = and i32 %44, 16777215
store i32 %45, i32* %9
br label %50
46: ; preds = %34
%47 = load i32, i32* %9
%48 = and i32 %47, -1
store i32 %48, i32* %9
br label %50
49: ; preds = %34
store i32 0, i32* %9
br label %50
50: ; preds = %49, %46, %43, %40, %37
%51 = load i32, i32* %9
ret i32 %51
}

LLVM IR (-O2):
define i32 @spi_sendcmd(i32 %0, i32 %1, i32 %2, i32 %3) {
br label %5
5: ; preds = %5, %4
%6 = tail call i32 @readl(i32 288358400)
%7 = and i32 %6, 65536
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %5
9: ; preds = %5
%10 = tail call i32 @writel(i32 288358408, i32 %1)
%11 = tail call i32 @writel(i32 288358404, i32 %0)
%12 = shl i32 %3, 4
%13 = or i32 %12, %2
%14 = or i32 %13, 50331904
%15 = tail call i32 @writel(i32 288358400, i32 %14)
br label %16
16: ; preds = %16, %9
%17 = tail call i32 @readl(i32 288358400)
%18 = and i32 %17, 65536
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %16
20: ; preds = %16
%21 = tail call i32 @readl(i32 288358408)
switch i32 %3, label %28 [
i32 1, label %22
i32 2, label %24
i32 3, label %26
i32 4, label %29
]
22: ; preds = %20
%23 = and i32 %21, 255
br label %29
24: ; preds = %20
%25 = and i32 %21, 65535
br label %29
26: ; preds = %20
%27 = and i32 %21, 16777215
br label %29
28: ; preds = %20
br label %29
29: ; preds = %20, %28, %26, %24, %22
%30 = phi i32 [ 0, %28 ], [ %27, %26 ], [ %25, %24 ], [ %23, %22 ], [ %21, %20 ]
ret i32 %30
}