#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;

/* Variables and functions */
int /*<<< orphan*/ floor(double);
int read_byte_data(int, int);
int /*<<< orphan*/ write_byte_data(int, int, int);
void set_pwm_freq(double freq, uint8_t address) {
  double scaleval = 25000000.0;
  scaleval /= 4096.0;
  scaleval /= freq;
  scaleval -= 1.0;
  uint8_t prescale = (uint8_t)floor(scaleval + 0.5);
  uint8_t oldmode = read_byte_data(address, 0x00);
  uint8_t newmode = (oldmode & 0x7F) | 0x10;
  write_byte_data(address, 0x00, newmode);
  write_byte_data(address, 0xFE, prescale);
  write_byte_data(address, 0x00, oldmode);
  write_byte_data(address, 0x00, oldmode | 0x80);
}


x86 assembly (clang, -O0):
<set_pwm_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movsd  0xed0(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xeb6(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe85(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe6c(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
call   1050 <floor@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <read_byte_data@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0x7f,%eax
or     $0x10,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1040 <write_byte_data@plt>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    $0xfe,%esi
call   1040 <write_byte_data@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%edx
xor    %esi,%esi
call   1040 <write_byte_data@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%edx
or     $0x80,%edx
xor    %esi,%esi
call   1040 <write_byte_data@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pwm_freq>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
movsd  0xec2(%rip),%xmm1
divsd  %xmm0,%xmm1
addsd  0xebe(%rip),%xmm1
addsd  0xebe(%rip),%xmm1
movapd %xmm1,%xmm0
call   1050 <floor@plt>
mov    %eax,%r14d
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <read_byte_data@plt>
mov    %eax,%ebp
mov    %eax,%edx
and    $0x6f,%edx
or     $0x10,%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <write_byte_data@plt>
mov    %ebx,%edi
mov    $0xfe,%esi
mov    %r14d,%edx
call   1040 <write_byte_data@plt>
mov    %ebx,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <write_byte_data@plt>
or     $0x80,%ebp
mov    %ebx,%edi
xor    %esi,%esi
mov    %ebp,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <write_byte_data@plt>

LLVM IR (-O0):
define void @set_pwm_freq(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store double %0, double* %3
store i32 %1, i32* %4
store double 2.500000e+07, double* %5
%9 = load double, double* %5
%10 = fdiv double %9, 4.096000e+03
store double %10, double* %5
%11 = load double, double* %3
%12 = load double, double* %5
%13 = fdiv double %12, %11
store double %13, double* %5
%14 = load double, double* %5
%15 = fsub double %14, 1.000000e+00
store double %15, double* %5
%16 = load double, double* %5
%17 = fadd double %16, 5.000000e-01
%18 = call i32 @floor(double %17)
store i32 %18, i32* %6
%19 = load i32, i32* %4
%20 = call i32 @read_byte_data(i32 %19, i32 0)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = and i32 %21, 127
%23 = or i32 %22, 16
store i32 %23, i32* %8
%24 = load i32, i32* %4
%25 = load i32, i32* %8
%26 = call i32 @write_byte_data(i32 %24, i32 0, i32 %25)
%27 = load i32, i32* %4
%28 = load i32, i32* %6
%29 = call i32 @write_byte_data(i32 %27, i32 254, i32 %28)
%30 = load i32, i32* %4
%31 = load i32, i32* %7
%32 = call i32 @write_byte_data(i32 %30, i32 0, i32 %31)
%33 = load i32, i32* %4
%34 = load i32, i32* %7
%35 = or i32 %34, 128
%36 = call i32 @write_byte_data(i32 %33, i32 0, i32 %35)
ret void
}

LLVM IR (-O2):
define void @set_pwm_freq(double %0, i32 %1) {
%3 = fdiv double 0x40B7D78400000000, %0
%4 = fadd double %3, -1.000000e+00
%5 = fadd double %4, 5.000000e-01
%6 = tail call i32 @floor(double %5)
%7 = tail call i32 @read_byte_data(i32 %1, i32 0)
%8 = and i32 %7, 111
%9 = or i32 %8, 16
%10 = tail call i32 @write_byte_data(i32 %1, i32 0, i32 %9)
%11 = tail call i32 @write_byte_data(i32 %1, i32 254, i32 %6)
%12 = tail call i32 @write_byte_data(i32 %1, i32 0, i32 %7)
%13 = or i32 %7, 128
%14 = tail call i32 @write_byte_data(i32 %1, i32 0, i32 %13)
ret void
}