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

/* Variables and functions */
int spi_xfer(uint8_t bitlen, const void *dout, void *din, uint32_t flags) {
  static unsigned int data1_reg_val;
  unsigned int len;
  int i;
  const uint16_t *txp = dout;
  uint16_t *rxp = din;
  len = bitlen / 16;
  *(volatile uint32_t *)(0x01E12000 + 0x40);
  for (i = 0; i < len; i++) {
    while (*(volatile uint32_t *)(0x01E12000 + 0x40) & 0x20000000)
      ;
    data1_reg_val &= ~0xFFFF;
    if (txp) {
      data1_reg_val |= *txp & 0xFFFF;
      txp++;
    }
    if ((i == (len - 1)) && (flags & 0x02)) {
      *(volatile uint32_t *)(0x01E12000 + 0x3c) = data1_reg_val & ~(1 << 28);
    } else {
      *(volatile uint32_t *)(0x01E12000 + 0x3c) = data1_reg_val;
    }
    if (rxp != NULL) {
      while (*(volatile uint32_t *)(0x01E12000 + 0x40) & (0x80000000))
        ;
      if (rxp) {
        *rxp = *(volatile uint32_t *)(0x01E12000 + 0x40) & 0xFFFF;
        rxp++;
      } else {
        *(volatile uint32_t *)(0x01E12000 + 0x40);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<spi_xfer>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    $0x1e12040,%eax
mov    (%rax),%eax
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    1265 <spi_xfer+0x165>
jmp    114f <spi_xfer+0x4f>
mov    $0x1e12040,%eax
mov    (%rax),%eax
and    $0x20000000,%eax
cmp    $0x0,%eax
je     1169 <spi_xfer+0x69>
jmp    114f <spi_xfer+0x4f>
mov    0x2eb5(%rip),%eax
and    $0xffff0000,%eax
mov    %eax,0x2eaa(%rip)
cmpq   $0x0,-0x30(%rbp)
je     11a8 <spi_xfer+0xa8>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
and    $0xffff,%eax
or     0x2e8e(%rip),%eax
mov    %eax,0x2e88(%rip)
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11e0 <spi_xfer+0xe0>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11e0 <spi_xfer+0xe0>
mov    0x2e56(%rip),%ecx
and    $0xefffffff,%ecx
mov    $0x1e1203c,%eax
mov    %ecx,(%rax)
jmp    11ed <spi_xfer+0xed>
mov    0x2e3e(%rip),%ecx
mov    $0x1e1203c,%eax
mov    %ecx,(%rax)
cmpq   $0x0,-0x38(%rbp)
je     1252 <spi_xfer+0x152>
jmp    11fd <spi_xfer+0xfd>
mov    $0x1e12040,%eax
mov    (%rax),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1217 <spi_xfer+0x117>
jmp    11fd <spi_xfer+0xfd>
cmpq   $0x0,-0x38(%rbp)
je     1246 <spi_xfer+0x146>
mov    $0x1e12040,%eax
mov    (%rax),%ecx
and    $0xffff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
jmp    124d <spi_xfer+0x14d>
mov    $0x1e12040,%eax
mov    (%rax),%eax
jmp    1252 <spi_xfer+0x152>
jmp    1257 <spi_xfer+0x157>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113e <spi_xfer+0x3e>
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_xfer>:
lea    0xf(%rdi),%r8d
test   %edi,%edi
cmovs  %r8d,%edi
mov    0x1e12040,%eax
cmp    $0x1f,%r8d
jae    111a <spi_xfer+0x1a>
xor    %eax,%eax
ret
sar    $0x4,%edi
lea    -0x1(%rdi),%r8d
cmp    $0x2,%edi
mov    $0x1,%r9d
cmovae %edi,%r9d
xor    %r10d,%r10d
jmp    1140 <spi_xfer+0x40>
xor    %edx,%edx
add    $0x1,%r10d
cmp    %r9d,%r10d
je     1117 <spi_xfer+0x17>
xchg   %ax,%ax
testl  $0x20000000,0x1e12040
jne    1140 <spi_xfer+0x40>
movzwl 0x2ed2(%rip),%eax
shl    $0x10,%eax
mov    %eax,0x2ec7(%rip)
test   %rsi,%rsi
je     1173 <spi_xfer+0x73>
movzwl (%rsi),%edi
or     %edi,%eax
mov    %eax,0x2eb7(%rip)
add    $0x4,%rsi
jmp    1175 <spi_xfer+0x75>
xor    %esi,%esi
mov    %eax,%edi
and    $0xefffffff,%edi
cmp    %r8d,%r10d
cmovne %eax,%edi
test   $0x2,%cl
cmove  %eax,%edi
mov    %edi,0x1e1203c
test   %rdx,%rdx
je     1133 <spi_xfer+0x33>
cs nopw 0x0(%rax,%rax,1)
nop
cmpl   $0x0,0x1e12040
js     11a0 <spi_xfer+0xa0>
mov    0x1e12040,%eax
movzwl %ax,%eax
mov    %eax,(%rdx)
add    $0x4,%rdx
jmp    1135 <spi_xfer+0x35>

LLVM IR (-O0):
@spi_xfer.data1_reg_val = internal global i32 0
define i32 @spi_xfer(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %6
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %11
%15 = load i8*, i8** %7
%16 = bitcast i8* %15 to i32*
store i32* %16, i32** %12
%17 = load i32, i32* %5
%18 = sdiv i32 %17, 16
store i32 %18, i32* %9
%19 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
store i32 0, i32* %10
br label %20
20: ; preds = %79, %4
%21 = load i32, i32* %10
%22 = load i32, i32* %9
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %82
24: ; preds = %20
br label %25
25: ; preds = %29, %24
%26 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%27 = and i32 %26, 536870912
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
br label %25
30: ; preds = %25
%31 = load i32, i32* @spi_xfer.data1_reg_val
%32 = and i32 %31, -65536
store i32 %32, i32* @spi_xfer.data1_reg_val
%33 = load i32*, i32** %11
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %43
35: ; preds = %30
%36 = load i32*, i32** %11
%37 = load i32, i32* %36
%38 = and i32 %37, 65535
%39 = load i32, i32* @spi_xfer.data1_reg_val
%40 = or i32 %39, %38
store i32 %40, i32* @spi_xfer.data1_reg_val
%41 = load i32*, i32** %11
%42 = getelementptr inbounds i32, i32* %41, i32 1
store i32* %42, i32** %11
br label %43
43: ; preds = %35, %30
%44 = load i32, i32* %10
%45 = load i32, i32* %9
%46 = sub i32 %45, 1
%47 = icmp eq i32 %44, %46
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* %8
%50 = and i32 %49, 2
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* @spi_xfer.data1_reg_val
%54 = and i32 %53, -268435457
store volatile i32 %54, i32* inttoptr (i64 31531068 to i32*)
br label %57
55: ; preds = %48, %43
%56 = load i32, i32* @spi_xfer.data1_reg_val
store volatile i32 %56, i32* inttoptr (i64 31531068 to i32*)
br label %57
57: ; preds = %55, %52
%58 = load i32*, i32** %12
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %78
60: ; preds = %57
br label %61
61: ; preds = %65, %60
%62 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%63 = and i32 %62, -2147483648
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61
br label %61
66: ; preds = %61
%67 = load i32*, i32** %12
%68 = icmp ne i32* %67, null
br i1 %68, label %69, label %75
69: ; preds = %66
%70 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%71 = and i32 %70, 65535
%72 = load i32*, i32** %12
store i32 %71, i32* %72
%73 = load i32*, i32** %12
%74 = getelementptr inbounds i32, i32* %73, i32 1
store i32* %74, i32** %12
br label %77
75: ; preds = %66
%76 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
br label %77
77: ; preds = %75, %69
br label %78
78: ; preds = %77, %57
br label %79
79: ; preds = %78
%80 = load i32, i32* %10
%81 = add nsw i32 %80, 1
store i32 %81, i32* %10
br label %20
82: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@spi_xfer.data1_reg_val = internal unnamed_addr global i32 0
define i32 @spi_xfer(i32 %0, i8* %1, i8* writeonly %2, i32 %3) {
%5 = sdiv i32 %0, 16
%6 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%7 = add i32 %0, 15
%8 = icmp ult i32 %7, 31
br i1 %8, label %52, label %9
9: ; preds = %4
%10 = bitcast i8* %2 to i32*
%11 = bitcast i8* %1 to i32*
%12 = add nsw i32 %5, -1
%13 = and i32 %3, 2
%14 = icmp eq i32 %13, 0
%15 = call i32 @llvm.umax.i32(i32 %5, i32 1)
br label %16
16: ; preds = %9, %48
%17 = phi i32* [ %10, %9 ], [ %49, %48 ]
%18 = phi i32* [ %11, %9 ], [ %35, %48 ]
%19 = phi i32 [ 0, %9 ], [ %50, %48 ]
br label %20
20: ; preds = %16, %20
%21 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%22 = and i32 %21, 536870912
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %20
24: ; preds = %20
%25 = load i32, i32* @spi_xfer.data1_reg_val
%26 = and i32 %25, -65536
store i32 %26, i32* @spi_xfer.data1_reg_val
%27 = icmp eq i32* %18, null
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* %18
%30 = and i32 %29, 65535
%31 = or i32 %30, %26
store i32 %31, i32* @spi_xfer.data1_reg_val
%32 = getelementptr inbounds i32, i32* %18, i64 1
br label %33
33: ; preds = %28, %24
%34 = phi i32 [ %31, %28 ], [ %26, %24 ]
%35 = phi i32* [ %32, %28 ], [ null, %24 ]
%36 = icmp ne i32 %19, %12
%37 = or i1 %14, %36
%38 = and i32 %34, -268435457
%39 = select i1 %37, i32 %34, i32 %38
store volatile i32 %39, i32* inttoptr (i64 31531068 to i32*)
%40 = icmp eq i32* %17, null
br i1 %40, label %48, label %41
41: ; preds = %33, %41
%42 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%43 = icmp sgt i32 %42, -1
br i1 %43, label %44, label %41
44: ; preds = %41
%45 = load volatile i32, i32* inttoptr (i64 31531072 to i32*)
%46 = and i32 %45, 65535
store i32 %46, i32* %17
%47 = getelementptr inbounds i32, i32* %17, i64 1
br label %48
48: ; preds = %33, %44
%49 = phi i32* [ %47, %44 ], [ null, %33 ]
%50 = add nuw i32 %19, 1
%51 = icmp eq i32 %50, %15
br i1 %51, label %52, label %16
52: ; preds = %48, %4
ret i32 0
}