#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 scalar_t__ max_speed_hz;
typedef unsigned int clk;

/* Variables and functions */
unsigned long clk_get_apb_rate();
int ls1c_fls(unsigned int);
unsigned int ls1c_spi_get_div(unsigned int max_speed_hz) {
  unsigned long clk = 0;
  unsigned int div = 0;
  unsigned int div_tmp = 0;
  unsigned int bit = 0;
  clk = clk_get_apb_rate();
  div = (((clk) + (max_speed_hz)-1) / (max_speed_hz));
  if (div < 2)
    div = 2;
  if (div > 4096)
    div = 4096;
  bit = ls1c_fls(div) - 1;
  switch (1 << bit) {
  case 16:
    div_tmp = 2;
    if (div > (1 << bit)) {
      div_tmp++;
    }
    break;
  case 32:
    div_tmp = 3;
    if (div > (1 << bit)) {
      div_tmp += 2;
    }
    break;
  case 8:
    div_tmp = 4;
    if (div > (1 << bit)) {
      div_tmp -= 2;
    }
    break;
  default:
    div_tmp = bit - 1;
    if (div > (1 << bit)) {
      div_tmp++;
    }
    break;
  }
  return div_tmp;
}


x86 assembly (clang, -O0):
<ls1c_spi_get_div>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <clk_get_apb_rate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
add    %rcx,%rax
sub    $0x1,%rax
mov    -0x4(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jae    117d <ls1c_spi_get_div+0x5d>
movl   $0x2,-0x14(%rbp)
cmpl   $0x1000,-0x14(%rbp)
jbe    1191 <ls1c_spi_get_div+0x71>
movl   $0x1000,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <ls1c_fls@plt>
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%cl
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x20(%rbp)
sub    $0x8,%eax
je     1234 <ls1c_spi_get_div+0x114>
jmp    11ba <ls1c_spi_get_div+0x9a>
mov    -0x20(%rbp),%eax
sub    $0x10,%eax
je     11dc <ls1c_spi_get_div+0xbc>
jmp    11cb <ls1c_spi_get_div+0xab>
mov    -0x20(%rbp),%eax
sub    $0x20,%eax
je     1208 <ls1c_spi_get_div+0xe8>
jmp    1260 <ls1c_spi_get_div+0x140>
movl   $0x2,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jbe    1203 <ls1c_spi_get_div+0xe3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    128e <ls1c_spi_get_div+0x16e>
movl   $0x3,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jbe    122f <ls1c_spi_get_div+0x10f>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    128e <ls1c_spi_get_div+0x16e>
movl   $0x4,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jbe    125b <ls1c_spi_get_div+0x13b>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    128e <ls1c_spi_get_div+0x16e>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jbe    1289 <ls1c_spi_get_div+0x169>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    128e <ls1c_spi_get_div+0x16e>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls1c_spi_get_div>:
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <clk_get_apb_rate@plt>
mov    %ebx,%ecx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,%rdx
shr    $0x20,%rdx
je     1143 <ls1c_spi_get_div+0x23>
xor    %edx,%edx
div    %rcx
jmp    1147 <ls1c_spi_get_div+0x27>
xor    %edx,%edx
div    %ecx
cmp    $0x3,%eax
mov    $0x2,%ecx
cmovae %eax,%ecx
cmp    $0x1000,%ecx
mov    $0x1000,%ebx
cmovb  %ecx,%ebx
mov    %ebx,%edi
call   1030 <ls1c_fls@plt>
lea    -0x1(%rax),%ecx
mov    $0x1,%edx
shl    %cl,%edx
cmp    $0x8,%edx
je     118d <ls1c_spi_get_div+0x6d>
cmp    $0x20,%edx
je     119c <ls1c_spi_get_div+0x7c>
cmp    $0x10,%edx
jne    11ab <ls1c_spi_get_div+0x8b>
xor    %eax,%eax
cmp    $0x11,%ebx
setae  %al
or     $0x2,%eax
pop    %rbx
ret
xor    %eax,%eax
cmp    $0x9,%ebx
setb   %al
add    %eax,%eax
add    $0x2,%eax
pop    %rbx
ret
xor    %eax,%eax
cmp    $0x21,%ebx
setae  %al
add    %eax,%eax
add    $0x3,%eax
pop    %rbx
ret
add    $0xfffffffe,%eax
cmp    %edx,%ebx
cmova  %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @ls1c_spi_get_div(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = call i64 (...) @clk_get_apb_rate()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = load i32, i32* %2
%10 = zext i32 %9 to i64
%11 = add i64 %8, %10
%12 = sub i64 %11, 1
%13 = load i32, i32* %2
%14 = zext i32 %13 to i64
%15 = udiv i64 %12, %14
%16 = trunc i64 %15 to i32
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ult i32 %17, 2
br i1 %18, label %19, label %20
19: ; preds = %1
store i32 2, i32* %4
br label %20
20: ; preds = %19, %1
%21 = load i32, i32* %4
%22 = icmp ugt i32 %21, 4096
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 4096, i32* %4
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %4
%26 = call i32 @ls1c_fls(i32 %25)
%27 = sub nsw i32 %26, 1
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = shl i32 1, %28
switch i32 %29, label %57 [
i32 16, label %30
i32 32, label %39
i32 8, label %48
]
30: ; preds = %24
store i32 2, i32* %5
%31 = load i32, i32* %4
%32 = load i32, i32* %6
%33 = shl i32 1, %32
%34 = icmp ugt i32 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* %5
%37 = add i32 %36, 1
store i32 %37, i32* %5
br label %38
38: ; preds = %35, %30
br label %68
39: ; preds = %24
store i32 3, i32* %5
%40 = load i32, i32* %4
%41 = load i32, i32* %6
%42 = shl i32 1, %41
%43 = icmp ugt i32 %40, %42
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* %5
%46 = add i32 %45, 2
store i32 %46, i32* %5
br label %47
47: ; preds = %44, %39
br label %68
48: ; preds = %24
store i32 4, i32* %5
%49 = load i32, i32* %4
%50 = load i32, i32* %6
%51 = shl i32 1, %50
%52 = icmp ugt i32 %49, %51
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32, i32* %5
%55 = sub i32 %54, 2
store i32 %55, i32* %5
br label %56
56: ; preds = %53, %48
br label %68
57: ; preds = %24
%58 = load i32, i32* %6
%59 = sub i32 %58, 1
store i32 %59, i32* %5
%60 = load i32, i32* %4
%61 = load i32, i32* %6
%62 = shl i32 1, %61
%63 = icmp ugt i32 %60, %62
br i1 %63, label %64, label %67
64: ; preds = %57
%65 = load i32, i32* %5
%66 = add i32 %65, 1
store i32 %66, i32* %5
br label %67
67: ; preds = %64, %57
br label %68
68: ; preds = %67, %56, %47, %38
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
define i32 @ls1c_spi_get_div(i32 %0) {
%2 = tail call i64 (...) @clk_get_apb_rate()
%3 = zext i32 %0 to i64
%4 = add nsw i64 %3, -1
%5 = add i64 %4, %2
%6 = udiv i64 %5, %3
%7 = trunc i64 %6 to i32
%8 = icmp ugt i32 %7, 2
%9 = select i1 %8, i32 %7, i32 2
%10 = icmp ult i32 %9, 4096
%11 = select i1 %10, i32 %9, i32 4096
%12 = tail call i32 @ls1c_fls(i32 %11)
%13 = add nsw i32 %12, -1
%14 = shl nuw i32 1, %13
switch i32 %14, label %24 [
i32 16, label %15
i32 32, label %18
i32 8, label %21
]
15: ; preds = %1
%16 = icmp ugt i32 %11, 16
%17 = select i1 %16, i32 3, i32 2
br label %28
18: ; preds = %1
%19 = icmp ugt i32 %11, 32
%20 = select i1 %19, i32 5, i32 3
br label %28
21: ; preds = %1
%22 = icmp ugt i32 %11, 8
%23 = select i1 %22, i32 2, i32 4
br label %28
24: ; preds = %1
%25 = add i32 %12, -2
%26 = icmp ugt i32 %11, %14
%27 = select i1 %26, i32 %13, i32 %25
br label %28
28: ; preds = %24, %21, %18, %15
%29 = phi i32 [ %17, %15 ], [ %20, %18 ], [ %23, %21 ], [ %27, %24 ]
ret i32 %29
}