#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_write(void *, unsigned short, int, unsigned char);
unsigned char tf_cal(void *handle, unsigned short devid) {
  unsigned char rfadc[3];
  unsigned int rfadc_avg = 0;
  unsigned char max_cb_code[1] = {0};
  unsigned char max_rfadc[1] = {0};
  unsigned char cal_temp;
  unsigned int x, i, j;
  for (x = 0; x < 1; x++) {
    for (i = 3; i < 12; i++) {
      cal_temp = (16 + i);
      fc8300_write(handle, devid, 0x1c, cal_temp);
      rfadc_avg = 0;
      for (j = 0; j < 3; j++) {
        fc8300_read(handle, devid, 0xf5, &rfadc[j]);
        rfadc_avg = rfadc_avg + rfadc[j];
      }
      rfadc_avg = rfadc_avg / 3;
      if (max_rfadc[x] < rfadc_avg) {
        max_rfadc[x] = (unsigned char)rfadc_avg;
        max_cb_code[x] = (16 + i);
      }
    }
  }
  return max_cb_code[0];
}


x86 assembly (clang, -O0):
<tf_cal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x15(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <memset@plt>
lea    -0x16(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <memset@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jae    1266 <tf_cal+0x136>
movl   $0x3,-0x20(%rbp)
cmpl   $0xc,-0x20(%rbp)
jae    1253 <tf_cal+0x123>
mov    -0x20(%rbp),%eax
add    $0x10,%eax
mov    %al,-0x17(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xa(%rbp),%ax
mov    $0x1c,%edx
movzwl %ax,%esi
movzbl -0x17(%rbp),%ecx
call   1050 <fc8300_write@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jae    1205 <tf_cal+0xd5>
mov    -0x8(%rbp),%rdi
mov    -0xa(%rbp),%ax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
lea    -0xd(%rbp),%rcx
add    %rdx,%rcx
mov    $0xf5,%edx
movzwl %ax,%esi
call   1040 <fc8300_read@plt>
mov    -0x14(%rbp),%eax
mov    -0x24(%rbp),%ecx
movzbl -0xd(%rbp,%rcx,1),%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bc <tf_cal+0x8c>
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
movzbl -0x16(%rbp,%rax,1),%eax
cmp    -0x14(%rbp),%eax
jae    1240 <tf_cal+0x110>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x1c(%rbp),%eax
mov    %cl,-0x16(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x10,%eax
mov    %al,%cl
mov    -0x1c(%rbp),%eax
mov    %cl,-0x15(%rbp,%rax,1)
jmp    1245 <tf_cal+0x115>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1182 <tf_cal+0x52>
jmp    1258 <tf_cal+0x128>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <tf_cal+0x41>
movzbl -0x15(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tf_cal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x10(%rsp)
mov    $0xfffffff7,%r14d
xor    %r15d,%r15d
movzwl %si,%eax
mov    %eax,0x4(%rsp)
xor    %ebx,%ebx
mov    0x10(%rsp),%rbp
mov    0x4(%rsp),%r13d
nop
mov    %r15d,0x8(%rsp)
lea    0x1c(%r14),%eax
mov    %eax,0xc(%rsp)
movzbl %al,%ecx
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0x1c,%edx
call   1040 <fc8300_write@plt>
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x1(%rsp),%rcx
call   1030 <fc8300_read@plt>
movzbl 0x1(%rsp),%r15d
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x2(%rsp),%rcx
call   1030 <fc8300_read@plt>
movzbl 0x2(%rsp),%r12d
add    %r15d,%r12d
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x3(%rsp),%rcx
call   1030 <fc8300_read@plt>
movzbl 0x3(%rsp),%eax
add    %r12d,%eax
movzbl 0x8(%rsp),%ecx
imul   $0xaaab,%eax,%r15d
shr    $0x11,%r15d
cmp    %cx,%r15w
movzbl %bl,%ebx
cmova  0xc(%rsp),%ebx
cmovbe %ecx,%r15d
inc    %r14d
jne    1150 <tf_cal+0x30>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @tf_cal(i8* %0, i16 zeroext %1) {
%3 = alloca i8*
%4 = alloca i16
%5 = alloca [3 x i8]
%6 = alloca i32
%7 = alloca [1 x i8]
%8 = alloca [1 x i8]
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i16 %1, i16* %4
store i32 0, i32* %6
%13 = bitcast [1 x i8]* %7 to i8*
%14 = bitcast [1 x i8]* %8 to i8*
store i32 0, i32* %10
br label %15
15: ; preds = %77, %2
%16 = load i32, i32* %10
%17 = icmp ult i32 %16, 1
br i1 %17, label %18, label %80
18: ; preds = %15
store i32 3, i32* %11
br label %19
19: ; preds = %73, %18
%20 = load i32, i32* %11
%21 = icmp ult i32 %20, 12
br i1 %21, label %22, label %76
22: ; preds = %19
%23 = load i32, i32* %11
%24 = add i32 16, %23
%25 = trunc i32 %24 to i8
store i8 %25, i8* %9
%26 = load i8*, i8** %3
%27 = load i16, i16* %4
%28 = load i8, i8* %9
%29 = call i32 @fc8300_write(i8* %26, i16 zeroext %27, i32 28, i8 zeroext %28)
store i32 0, i32* %6
store i32 0, i32* %12
br label %30
30: ; preds = %47, %22
%31 = load i32, i32* %12
%32 = icmp ult i32 %31, 3
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = load i8*, i8** %3
%35 = load i16, i16* %4
%36 = load i32, i32* %12
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %37
%39 = call i32 @fc8300_read(i8* %34, i16 zeroext %35, i32 245, i8* %38)
%40 = load i32, i32* %6
%41 = load i32, i32* %12
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = add i32 %40, %45
store i32 %46, i32* %6
br label %47
47: ; preds = %33
%48 = load i32, i32* %12
%49 = add i32 %48, 1
store i32 %49, i32* %12
br label %30
50: ; preds = %30
%51 = load i32, i32* %6
%52 = udiv i32 %51, 3
store i32 %52, i32* %6
%53 = load i32, i32* %10
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = load i32, i32* %6
%59 = icmp ult i32 %57, %58
br i1 %59, label %60, label %72
60: ; preds = %50
%61 = load i32, i32* %6
%62 = trunc i32 %61 to i8
%63 = load i32, i32* %10
%64 = zext i32 %63 to i64
%65 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 %64
store i8 %62, i8* %65
%66 = load i32, i32* %11
%67 = add i32 16, %66
%68 = trunc i32 %67 to i8
%69 = load i32, i32* %10
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 %70
store i8 %68, i8* %71
br label %72
72: ; preds = %60, %50
br label %73
73: ; preds = %72
%74 = load i32, i32* %11
%75 = add i32 %74, 1
store i32 %75, i32* %11
br label %19
76: ; preds = %19
br label %77
77: ; preds = %76
%78 = load i32, i32* %10
%79 = add i32 %78, 1
store i32 %79, i32* %10
br label %15
80: ; preds = %15
%81 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%82 = load i8, i8* %81
ret i8 %82
}

LLVM IR (-O2):
define zeroext i8 @tf_cal(i8* %0, i16 zeroext %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
br label %7
7: ; preds = %2, %7
%8 = phi i32 [ 3, %2 ], [ %31, %7 ]
%9 = phi i8 [ 0, %2 ], [ %30, %7 ]
%10 = phi i8 [ 0, %2 ], [ %29, %7 ]
%11 = trunc i32 %8 to i8
%12 = add nuw nsw i8 %11, 16
%13 = call i32 @fc8300_write(i8* %0, i16 zeroext %1, i32 28, i8 zeroext %12)
%14 = call i32 @fc8300_read(i8* %0, i16 zeroext %1, i32 245, i8* %4)
%15 = load i8, i8* %4
%16 = zext i8 %15 to i16
%17 = call i32 @fc8300_read(i8* %0, i16 zeroext %1, i32 245, i8* %5)
%18 = load i8, i8* %5
%19 = zext i8 %18 to i16
%20 = add nuw nsw i16 %16, %19
%21 = call i32 @fc8300_read(i8* %0, i16 zeroext %1, i32 245, i8* %6)
%22 = load i8, i8* %6
%23 = zext i8 %22 to i16
%24 = add nuw nsw i16 %20, %23
%25 = udiv i16 %24, 3
%26 = zext i8 %9 to i16
%27 = icmp ugt i16 %25, %26
%28 = trunc i16 %25 to i8
%29 = select i1 %27, i8 %12, i8 %10
%30 = select i1 %27, i8 %28, i8 %9
%31 = add nuw nsw i32 %8, 1
%32 = icmp eq i32 %31, 12
br i1 %32, label %33, label %7
33: ; preds = %7
ret i8 %29
}