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

/* Variables and functions */
int /*<<< orphan*/ TRACE_DEBUG(char *, int, int, int);
int /*<<< orphan*/ assert(int);
uint16_t FindClockConfiguration(uint32_t frequency, uint32_t mck) {
  uint32_t divisors[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
  uint8_t divisor = 0;
  uint32_t prescaler;
  assert(frequency < mck);
  prescaler = (mck / divisors[divisor]) / frequency;
  while ((prescaler > 255) && (divisor < 11)) {
    divisor++;
    prescaler = (mck / divisors[divisor]) / frequency;
  }
  if (divisor < 11) {
    TRACE_DEBUG("Found divisor=%u and prescaler=%u for freq=%uHz\n\r",
                divisors[divisor], prescaler, frequency);
    return prescaler | (divisor << 8);
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<FindClockConfiguration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xeb7(%rip),%rsi
mov    $0x2c,%edx
call   1040 <memcpy@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0xc(%rbp),%eax
mov    -0x48(%rbp),%rcx
cltd
idivl  -0x40(%rbp,%rcx,4)
cltd
idivl  -0x8(%rbp)
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmpl   $0xff,-0x4c(%rbp)
mov    %al,-0x4d(%rbp)
jle    119e <FindClockConfiguration+0x6e>
cmpq   $0xb,-0x48(%rbp)
setb   %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
test   $0x1,%al
jne    11ae <FindClockConfiguration+0x7e>
jmp    11d2 <FindClockConfiguration+0xa2>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x48(%rbp),%rcx
cltd
idivl  -0x40(%rbp,%rcx,4)
cltd
idivl  -0x8(%rbp)
mov    %eax,-0x4c(%rbp)
jmp    1181 <FindClockConfiguration+0x51>
cmpq   $0xb,-0x48(%rbp)
jae    120e <FindClockConfiguration+0xde>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%esi
mov    -0x4c(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe3a(%rip),%rdi
call   1050 <TRACE_DEBUG@plt>
movslq -0x4c(%rbp),%rax
mov    -0x48(%rbp),%rcx
shl    $0x8,%rcx
or     %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1215 <FindClockConfiguration+0xe5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FindClockConfiguration>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
xor    %edi,%edi
cmp    %esi,%r14d
setl   %dil
call   1030 <assert@plt>
mov    %r15d,%eax
cltd
idiv   %r14d
cmp    $0x100,%eax
jl     118c <FindClockConfiguration+0x6c>
xor    %ebx,%ebx
lea    0xeae(%rip),%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rcx
mov    %r15d,%eax
cltd
idivl  0x4(%rsi,%rbx,4)
add    $0x1,%rbx
cltd
idiv   %r14d
mov    %eax,%ebp
cmp    $0x100,%eax
jl     1182 <FindClockConfiguration+0x62>
cmp    $0xa,%rcx
jb     1160 <FindClockConfiguration+0x40>
xor    %eax,%eax
cmp    $0x9,%rcx
jbe    1190 <FindClockConfiguration+0x70>
jmp    11b2 <FindClockConfiguration+0x92>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0xe69(%rip),%rax
mov    (%rax,%rbx,4),%esi
lea    0xe8b(%rip),%rdi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1040 <TRACE_DEBUG@plt>
shl    $0x8,%ebx
or     %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.FindClockConfiguration.divisors = [11 x i32] [i32 1, i32 2, i32 4, i32 8, i32 16, i32 32, i32 64, i32 128, i32 256, i32 512, i32 1024]
@.str = [50 x i8] c"Found divisor=%u and prescaler=%u for freq=%uHz\0A\0D\00"
define i32 @FindClockConfiguration(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [11 x i32]
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = bitcast [11 x i32]* %6 to i8*
store i64 0, i64* %7
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %5
%16 = load i64, i64* %7
%17 = getelementptr inbounds [11 x i32], [11 x i32]* %6, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = sdiv i32 %15, %18
%20 = load i32, i32* %4
%21 = sdiv i32 %19, %20
store i32 %21, i32* %8
br label %22
22: ; preds = %30, %2
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 255
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i64, i64* %7
%27 = icmp ult i64 %26, 11
br label %28
28: ; preds = %25, %22
%29 = phi i1 [ false, %22 ], [ %27, %25 ]
br i1 %29, label %30, label %40
30: ; preds = %28
%31 = load i64, i64* %7
%32 = add i64 %31, 1
store i64 %32, i64* %7
%33 = load i32, i32* %5
%34 = load i64, i64* %7
%35 = getelementptr inbounds [11 x i32], [11 x i32]* %6, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = sdiv i32 %33, %36
%38 = load i32, i32* %4
%39 = sdiv i32 %37, %38
store i32 %39, i32* %8
br label %22
40: ; preds = %28
%41 = load i64, i64* %7
%42 = icmp ult i64 %41, 11
br i1 %42, label %43, label %56
43: ; preds = %40
%44 = load i64, i64* %7
%45 = getelementptr inbounds [11 x i32], [11 x i32]* %6, i64 0, i64 %44
%46 = load i32, i32* %45
%47 = load i32, i32* %8
%48 = load i32, i32* %4
%49 = call i32 @TRACE_DEBUG(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %47, i32 %48)
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = load i64, i64* %7
%53 = shl i64 %52, 8
%54 = or i64 %51, %53
%55 = trunc i64 %54 to i32
store i32 %55, i32* %3
br label %57
56: ; preds = %40
store i32 0, i32* %3
br label %57
57: ; preds = %56, %43
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@__const.FindClockConfiguration.divisors = [11 x i32] [i32 1, i32 2, i32 4, i32 8, i32 16, i32 32, i32 64, i32 128, i32 256, i32 512, i32 1024]
@.str = [50 x i8] c"Found divisor=%u and prescaler=%u for freq=%uHz\0A\0D\00"
define i32 @FindClockConfiguration(i32 %0, i32 %1) {
%3 = icmp slt i32 %0, %1
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = sdiv i32 %1, %0
%7 = icmp sgt i32 %6, 255
br i1 %7, label %8, label %19
8: ; preds = %2, %8
%9 = phi i64 [ %10, %8 ], [ 0, %2 ]
%10 = add nuw nsw i64 %9, 1
%11 = getelementptr inbounds [11 x i32], [11 x i32]* @__const.FindClockConfiguration.divisors, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = sdiv i32 %1, %12
%14 = sdiv i32 %13, %0
%15 = icmp sgt i32 %14, 255
%16 = icmp ult i64 %9, 10
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %8, label %18
18: ; preds = %8
br i1 %16, label %19, label %28
19: ; preds = %2, %18
%20 = phi i32 [ %14, %18 ], [ %6, %2 ]
%21 = phi i64 [ %10, %18 ], [ 0, %2 ]
%22 = getelementptr inbounds [11 x i32], [11 x i32]* @__const.FindClockConfiguration.divisors, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = tail call i32 @TRACE_DEBUG(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %20, i32 %0)
%25 = trunc i64 %21 to i32
%26 = shl nuw nsw i32 %25, 8
%27 = or i32 %26, %20
br label %28
28: ; preds = %18, %19
%29 = phi i32 [ %27, %19 ], [ 0, %18 ]
ret i32 %29
}