#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*/ fc8300b_read(void *, unsigned short, int, unsigned char *);
int /*<<< orphan*/ fc8300b_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];
  unsigned char max_rfadc[1];
  unsigned char cal_temp = 0;
  unsigned int x, i, j;
  for (x = 0; x < 1; x++) {
    max_cb_code[x] = 0;
    max_rfadc[x] = 0;
    for (i = 3; i < 12; i++) {
      rfadc[x] = 0;
      rfadc_avg = 0;
      cal_temp = (0x10 + i);
      fc8300b_write(handle, devid, 0x1c, cal_temp);
      for (j = 0; j < 3; j++) {
        fc8300b_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] = (0x10 + 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)
movb   $0x0,-0x17(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jae    1252 <tf_cal+0x132>
mov    -0x1c(%rbp),%eax
movb   $0x0,-0x15(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
movb   $0x0,-0x16(%rbp,%rax,1)
movl   $0x3,-0x20(%rbp)
cmpl   $0xc,-0x20(%rbp)
jae    123f <tf_cal+0x11f>
mov    -0x1c(%rbp),%eax
movb   $0x0,-0xd(%rbp,%rax,1)
movl   $0x0,-0x14(%rbp)
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   1030 <fc8300b_write@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jae    11f1 <tf_cal+0xd1>
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 <fc8300b_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    11a8 <tf_cal+0x88>
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    122c <tf_cal+0x10c>
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    1231 <tf_cal+0x111>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1166 <tf_cal+0x46>
jmp    1244 <tf_cal+0x124>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1145 <tf_cal+0x25>
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)
movb   $0x0,0x1(%rsp)
lea    0x1c(%r14),%eax
mov    %eax,0xc(%rsp)
movzbl %al,%ecx
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0x1c,%edx
call   1030 <fc8300b_write@plt>
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x1(%rsp),%rcx
call   1040 <fc8300b_read@plt>
movzbl 0x1(%rsp),%r15d
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x2(%rsp),%rcx
call   1040 <fc8300b_read@plt>
movzbl 0x2(%rsp),%r12d
add    %r15d,%r12d
mov    %rbp,%rdi
mov    %r13d,%esi
mov    $0xf5,%edx
lea    0x3(%rsp),%rcx
call   1040 <fc8300b_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
store i8 0, i8* %9
store i32 0, i32* %10
br label %13
13: ; preds = %84, %2
%14 = load i32, i32* %10
%15 = icmp ult i32 %14, 1
br i1 %15, label %16, label %87
16: ; preds = %13
%17 = load i32, i32* %10
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* %10
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 %21
store i8 0, i8* %22
store i32 3, i32* %11
br label %23
23: ; preds = %80, %16
%24 = load i32, i32* %11
%25 = icmp ult i32 %24, 12
br i1 %25, label %26, label %83
26: ; preds = %23
%27 = load i32, i32* %10
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %28
store i8 0, i8* %29
store i32 0, i32* %6
%30 = load i32, i32* %11
%31 = add i32 16, %30
%32 = trunc i32 %31 to i8
store i8 %32, i8* %9
%33 = load i8*, i8** %3
%34 = load i16, i16* %4
%35 = load i8, i8* %9
%36 = call i32 @fc8300b_write(i8* %33, i16 zeroext %34, i32 28, i8 zeroext %35)
store i32 0, i32* %12
br label %37
37: ; preds = %54, %26
%38 = load i32, i32* %12
%39 = icmp ult i32 %38, 3
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = load i8*, i8** %3
%42 = load i16, i16* %4
%43 = load i32, i32* %12
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %44
%46 = call i32 @fc8300b_read(i8* %41, i16 zeroext %42, i32 245, i8* %45)
%47 = load i32, i32* %6
%48 = load i32, i32* %12
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = add i32 %47, %52
store i32 %53, i32* %6
br label %54
54: ; preds = %40
%55 = load i32, i32* %12
%56 = add i32 %55, 1
store i32 %56, i32* %12
br label %37
57: ; preds = %37
%58 = load i32, i32* %6
%59 = udiv i32 %58, 3
store i32 %59, i32* %6
%60 = load i32, i32* %10
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = load i32, i32* %6
%66 = icmp ult i32 %64, %65
br i1 %66, label %67, label %79
67: ; preds = %57
%68 = load i32, i32* %6
%69 = trunc i32 %68 to i8
%70 = load i32, i32* %10
%71 = zext i32 %70 to i64
%72 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 %71
store i8 %69, i8* %72
%73 = load i32, i32* %11
%74 = add i32 16, %73
%75 = trunc i32 %74 to i8
%76 = load i32, i32* %10
%77 = zext i32 %76 to i64
%78 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 %77
store i8 %75, i8* %78
br label %79
79: ; preds = %67, %57
br label %80
80: ; preds = %79
%81 = load i32, i32* %11
%82 = add i32 %81, 1
store i32 %82, i32* %11
br label %23
83: ; preds = %23
br label %84
84: ; preds = %83
%85 = load i32, i32* %10
%86 = add i32 %85, 1
store i32 %86, i32* %10
br label %13
87: ; preds = %13
%88 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%89 = load i8, i8* %88
ret i8 %89
}

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 ]
store i8 0, i8* %4
%11 = trunc i32 %8 to i8
%12 = add nuw nsw i8 %11, 16
%13 = call i32 @fc8300b_write(i8* %0, i16 zeroext %1, i32 28, i8 zeroext %12)
%14 = call i32 @fc8300b_read(i8* %0, i16 zeroext %1, i32 245, i8* %4)
%15 = load i8, i8* %4
%16 = zext i8 %15 to i16
%17 = call i32 @fc8300b_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 @fc8300b_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
}