#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*/ delay_us(int);
int spi_setup_bus(uint32_t freq, uint8_t polarity, uint8_t phrase) {
  uint32_t scalar;
  volatile uint32_t data1_reg = 0;
  *(volatile uint32_t *)(0x01E12000 + 0x0) = 0;
  delay_us(1000);
  *(volatile uint32_t *)(0x01E12000 + 0x0) = 1;
  *(volatile uint32_t *)(0x01E12000 + 0x4) = 3;
  *(volatile uint32_t *)(0x01E12000 + 0x14) =
      (1 << 0) | (1 << 9) | (1 << 10) | (1 << 11);
  scalar = (((150000000) / freq) - 1) & 0xFF;
  *(volatile uint32_t *)(0x01E12000 + 0x50) =
      16 | (scalar << 8) | ((phrase & 0x1) << 16) | ((polarity & 0x1) << 17) |
      (0 << 20);
  data1_reg = (1 << 28) | (0 << 16);
  *(volatile uint32_t *)(0x01E12000 + 0x3c) = data1_reg;
  *(volatile uint32_t *)(0x01E12000 + 0x4c) = 1;
  *(volatile uint32_t *)(0x01E12000 + 0x8) = 0;
  *(volatile uint32_t *)(0x01E12000 + 0xc) = 0;
  *(volatile uint32_t *)(0x01E12000 + 0x4) |= (1 << 24);
  return 0;
}


x86 assembly (clang, -O0):
<spi_setup_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x1e12000,%eax
movl   $0x0,(%rax)
mov    $0x3e8,%edi
call   1030 <delay_us@plt>
mov    $0x1e12000,%eax
movl   $0x1,(%rax)
mov    $0x1e12004,%eax
movl   $0x3,(%rax)
mov    $0x1e12014,%eax
movl   $0xe01,(%rax)
mov    $0x8f0d180,%eax
cltd
idivl  -0x4(%rbp)
sub    $0x1,%eax
and    $0xff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
shl    $0x8,%ecx
or     $0x10,%ecx
mov    -0xc(%rbp),%eax
and    $0x1,%eax
shl    $0x10,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%eax
and    $0x1,%eax
shl    $0x11,%eax
or     %eax,%ecx
or     $0x0,%ecx
mov    $0x1e12050,%eax
mov    %ecx,(%rax)
movl   $0x10000000,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    $0x1e1203c,%eax
mov    %ecx,(%rax)
mov    $0x1e1204c,%eax
movl   $0x1,(%rax)
mov    $0x1e12008,%eax
movl   $0x0,(%rax)
mov    $0x1e1200c,%eax
movl   $0x0,(%rax)
mov    $0x1e12004,%eax
mov    (%rax),%ecx
or     $0x1000000,%ecx
mov    $0x1e12004,%eax
mov    %ecx,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_setup_bus>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
movl   $0x0,0xc(%rsp)
movl   $0x0,0x1e12000
mov    $0x3e8,%edi
call   1030 <delay_us@plt>
movl   $0x1,0x1e12000
movl   $0x3,0x1e12004
movl   $0xe01,0x1e12014
mov    $0x8f0d180,%eax
xor    %edx,%edx
idiv   %r14d
shl    $0x8,%eax
add    $0xff00,%eax
movzwl %ax,%eax
and    $0x1,%ebp
shl    $0x10,%ebp
and    $0x1,%ebx
shl    $0x11,%ebx
or     %ebp,%ebx
add    %ebx,%eax
add    $0x10,%eax
mov    %eax,0x1e12050
movl   $0x10000000,0xc(%rsp)
10
mov    0xc(%rsp),%eax
mov    %eax,0x1e1203c
movl   $0x1,0x1e1204c
movl   $0x0,0x1e12008
movl   $0x0,0x1e1200c
orl    $0x1000000,0x1e12004
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @spi_setup_bus(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store volatile i32 0, i32* %8
store volatile i32 0, i32* inttoptr (i64 31531008 to i32*)
%9 = call i32 @delay_us(i32 1000)
store volatile i32 1, i32* inttoptr (i64 31531008 to i32*)
store volatile i32 3, i32* inttoptr (i64 31531012 to i32*)
store volatile i32 3585, i32* inttoptr (i64 31531028 to i32*)
%10 = load i32, i32* %4
%11 = sdiv i32 150000000, %10
%12 = sub nsw i32 %11, 1
%13 = and i32 %12, 255
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = shl i32 %14, 8
%16 = or i32 16, %15
%17 = load i32, i32* %6
%18 = and i32 %17, 1
%19 = shl i32 %18, 16
%20 = or i32 %16, %19
%21 = load i32, i32* %5
%22 = and i32 %21, 1
%23 = shl i32 %22, 17
%24 = or i32 %20, %23
%25 = or i32 %24, 0
store volatile i32 %25, i32* inttoptr (i64 31531088 to i32*)
store volatile i32 268435456, i32* %8
%26 = load volatile i32, i32* %8
store volatile i32 %26, i32* inttoptr (i64 31531068 to i32*)
store volatile i32 1, i32* inttoptr (i64 31531084 to i32*)
store volatile i32 0, i32* inttoptr (i64 31531016 to i32*)
store volatile i32 0, i32* inttoptr (i64 31531020 to i32*)
%27 = load volatile i32, i32* inttoptr (i64 31531012 to i32*)
%28 = or i32 %27, 16777216
store volatile i32 %28, i32* inttoptr (i64 31531012 to i32*)
ret i32 0
}

LLVM IR (-O2):
define i32 @spi_setup_bus(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store volatile i32 0, i32* %4
store volatile i32 0, i32* inttoptr (i64 31531008 to i32*)
%6 = tail call i32 @delay_us(i32 1000)
store volatile i32 1, i32* inttoptr (i64 31531008 to i32*)
store volatile i32 3, i32* inttoptr (i64 31531012 to i32*)
store volatile i32 3585, i32* inttoptr (i64 31531028 to i32*)
%7 = sdiv i32 150000000, %0
%8 = shl i32 %7, 8
%9 = add i32 %8, 65280
%10 = and i32 %9, 65280
%11 = shl i32 %2, 16
%12 = and i32 %11, 65536
%13 = shl i32 %1, 17
%14 = and i32 %13, 131072
%15 = or i32 %14, %12
%16 = or i32 %15, %10
%17 = or i32 %16, 16
store volatile i32 %17, i32* inttoptr (i64 31531088 to i32*)
store volatile i32 268435456, i32* %4
%18 = load volatile i32, i32* %4
store volatile i32 %18, i32* inttoptr (i64 31531068 to i32*)
store volatile i32 1, i32* inttoptr (i64 31531084 to i32*)
store volatile i32 0, i32* inttoptr (i64 31531016 to i32*)
store volatile i32 0, i32* inttoptr (i64 31531020 to i32*)
%19 = load volatile i32, i32* inttoptr (i64 31531012 to i32*)
%20 = or i32 %19, 16777216
store volatile i32 %20, i32* inttoptr (i64 31531012 to i32*)
ret i32 0
}