#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 */

/* Variables and functions */
int /*<<< orphan*/ fc8300_read(void *, unsigned short, int, unsigned char *);
int /*<<< orphan*/ fc8300_tuner_set_pll(void *, unsigned short, unsigned int,
                                        int /*<<< orphan*/);
int /*<<< orphan*/ fc8300_write(void *, unsigned short, int, int);
int /*<<< orphan*/ msWait(int);
unsigned char pd_cal(void *handle, unsigned short devid, unsigned int freq,
                     unsigned char *pd_cal_table) {
  if (devid == 0x5f04)
    fc8300_tuner_set_pll(handle, 0x5800, freq, 0);
  else
    fc8300_tuner_set_pll(handle, devid, freq, 0);
  fc8300_write(handle, devid, 0x78, 0xf1);
  fc8300_write(handle, devid, 0x04, 0x12);
  msWait(3);
  fc8300_write(handle, devid, 0x78, 0x31);
  msWait(2);
  if (devid == 0x5f04)
    fc8300_read(handle, 0x5800, 0x98, &pd_cal_table[0]);
  else
    fc8300_read(handle, devid, 0x98, &pd_cal_table[devid & 0x0f]);
  fc8300_write(handle, devid, 0x04, 0x10);
  return 0;
}


x86 assembly (clang, -O0):
<pd_cal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movzwl -0xa(%rbp),%eax
cmp    $0x5f04,%eax
jne    1181 <pd_cal+0x41>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    $0x5800,%esi
xor    %ecx,%ecx
call   1030 <fc8300_tuner_set_pll@plt>
jmp    1196 <pd_cal+0x56>
mov    -0x8(%rbp),%rdi
mov    -0xa(%rbp),%ax
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
movzwl %ax,%esi
call   1030 <fc8300_tuner_set_pll@plt>
mov    -0x8(%rbp),%rdi
mov    $0x78,%edx
mov    $0xf1,%ecx
movzwl -0xa(%rbp),%esi
call   1060 <fc8300_write@plt>
mov    -0x8(%rbp),%rdi
mov    $0x4,%edx
mov    $0x12,%ecx
movzwl -0xa(%rbp),%esi
call   1060 <fc8300_write@plt>
mov    $0x3,%edi
call   1050 <msWait@plt>
mov    -0x8(%rbp),%rdi
mov    $0x78,%edx
mov    $0x31,%ecx
movzwl -0xa(%rbp),%esi
call   1060 <fc8300_write@plt>
mov    $0x2,%edi
call   1050 <msWait@plt>
movzwl -0xa(%rbp),%eax
cmp    $0x5f04,%eax
jne    121a <pd_cal+0xda>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x5800,%esi
mov    $0x98,%edx
call   1040 <fc8300_read@plt>
jmp    1240 <pd_cal+0x100>
mov    -0x8(%rbp),%rdi
mov    -0xa(%rbp),%ax
mov    -0x18(%rbp),%rcx
movzwl -0xa(%rbp),%edx
and    $0xf,%edx
movslq %edx,%rdx
add    %rdx,%rcx
mov    $0x98,%edx
movzwl %ax,%esi
call   1040 <fc8300_read@plt>
mov    -0x8(%rbp),%rdi
mov    $0x4,%edx
mov    $0x10,%ecx
movzwl -0xa(%rbp),%esi
call   1060 <fc8300_write@plt>
xor    %eax,%eax
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pd_cal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
cmp    $0x5f04,%esi
mov    $0x5800,%esi
cmovne %ebp,%esi
xor    %ecx,%ecx
call   1030 <fc8300_tuner_set_pll@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x78,%edx
mov    $0xf1,%ecx
call   1060 <fc8300_write@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x4,%edx
mov    $0x12,%ecx
call   1060 <fc8300_write@plt>
mov    $0x3,%edi
call   1050 <msWait@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x78,%edx
mov    $0x31,%ecx
call   1060 <fc8300_write@plt>
mov    $0x2,%edi
call   1050 <msWait@plt>
movzwl %bp,%r15d
cmp    $0x5f04,%ebp
jne    11ca <pd_cal+0x8a>
mov    %rbx,%rdi
mov    $0x5800,%esi
jmp    11d9 <pd_cal+0x99>
mov    %r15d,%eax
and    $0xf,%eax
add    %rax,%r14
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x98,%edx
mov    %r14,%rcx
call   1040 <fc8300_read@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x4,%edx
mov    $0x10,%ecx
call   1060 <fc8300_write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @pd_cal(i8* %0, i16 zeroext %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%9 = load i16, i16* %6
%10 = zext i16 %9 to i32
%11 = icmp eq i32 %10, 24324
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %7
%15 = call i32 @fc8300_tuner_set_pll(i8* %13, i16 zeroext 22528, i32 %14, i32 0)
br label %21
16: ; preds = %4
%17 = load i8*, i8** %5
%18 = load i16, i16* %6
%19 = load i32, i32* %7
%20 = call i32 @fc8300_tuner_set_pll(i8* %17, i16 zeroext %18, i32 %19, i32 0)
br label %21
21: ; preds = %16, %12
%22 = load i8*, i8** %5
%23 = load i16, i16* %6
%24 = call i32 @fc8300_write(i8* %22, i16 zeroext %23, i32 120, i32 241)
%25 = load i8*, i8** %5
%26 = load i16, i16* %6
%27 = call i32 @fc8300_write(i8* %25, i16 zeroext %26, i32 4, i32 18)
%28 = call i32 @msWait(i32 3)
%29 = load i8*, i8** %5
%30 = load i16, i16* %6
%31 = call i32 @fc8300_write(i8* %29, i16 zeroext %30, i32 120, i32 49)
%32 = call i32 @msWait(i32 2)
%33 = load i16, i16* %6
%34 = zext i16 %33 to i32
%35 = icmp eq i32 %34, 24324
br i1 %35, label %36, label %41
36: ; preds = %21
%37 = load i8*, i8** %5
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = call i32 @fc8300_read(i8* %37, i16 zeroext 22528, i32 152, i8* %39)
br label %51
41: ; preds = %21
%42 = load i8*, i8** %5
%43 = load i16, i16* %6
%44 = load i8*, i8** %8
%45 = load i16, i16* %6
%46 = zext i16 %45 to i32
%47 = and i32 %46, 15
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = call i32 @fc8300_read(i8* %42, i16 zeroext %43, i32 152, i8* %49)
br label %51
51: ; preds = %41, %36
%52 = load i8*, i8** %5
%53 = load i16, i16* %6
%54 = call i32 @fc8300_write(i8* %52, i16 zeroext %53, i32 4, i32 16)
ret i8 0
}

LLVM IR (-O2):
define zeroext i8 @pd_cal(i8* %0, i16 zeroext %1, i32 %2, i8* %3) {
%5 = icmp eq i16 %1, 24324
%6 = select i1 %5, i16 22528, i16 %1
%7 = tail call i32 @fc8300_tuner_set_pll(i8* %0, i16 zeroext %6, i32 %2, i32 0)
%8 = tail call i32 @fc8300_write(i8* %0, i16 zeroext %1, i32 120, i32 241)
%9 = tail call i32 @fc8300_write(i8* %0, i16 zeroext %1, i32 4, i32 18)
%10 = tail call i32 @msWait(i32 3)
%11 = tail call i32 @fc8300_write(i8* %0, i16 zeroext %1, i32 120, i32 49)
%12 = tail call i32 @msWait(i32 2)
br i1 %5, label %13, label %15
13: ; preds = %4
%14 = tail call i32 @fc8300_read(i8* %0, i16 zeroext 22528, i32 152, i8* %3)
br label %20
15: ; preds = %4
%16 = and i16 %1, 15
%17 = zext i16 %16 to i64
%18 = getelementptr inbounds i8, i8* %3, i64 %17
%19 = tail call i32 @fc8300_read(i8* %0, i16 zeroext %1, i32 152, i8* %18)
br label %20
20: ; preds = %15, %13
%21 = tail call i32 @fc8300_write(i8* %0, i16 zeroext %1, i32 4, i32 16)
ret i8 0
}