#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 fc8150_bb_read(void *, int, unsigned char *);
int fc8150_read(void *, int, unsigned char *);
int fc8150_get_rssi(void *hDevice, int *rssi) {
  int res = 0;
  unsigned char LNA, RFVGA, CSF, PREAMP_PGA, CRNTMODE1, CRNTMODE0 = 0x00;
  int K = -105;
  int PGA = 0;
  int A2 = 5;
  int A3 = 2;
  res = fc8150_read(hDevice, 0xA3, &LNA);
  res = fc8150_read(hDevice, 0xA4, &RFVGA);
  res = fc8150_read(hDevice, 0xA8, &CSF);
  res = fc8150_bb_read(hDevice, 0x106E, &PREAMP_PGA);
  if (res != 0)
    return res;
  if (127 < PREAMP_PGA)
    PGA = -1 * ((256 - PREAMP_PGA) + 1);
  else if (PREAMP_PGA <= 127)
    PGA = PREAMP_PGA;
  CRNTMODE1 = (LNA >> 6) & 0x01;
  CRNTMODE0 = (LNA >> 5) & 0x01;
  *rssi = (LNA & 0x07) * 6 + (RFVGA & 0x1F) +
          ((CSF & 0x03) + ((CSF & 0x70) >> 4)) * 6 - PGA / 4 + A2 * CRNTMODE1 +
          A3 * CRNTMODE0 + K;
  return 0;
}


x86 assembly (clang, -O0):
<fc8150_get_rssi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x22(%rbp)
movl   $0xffffff97,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x5,-0x30(%rbp)
movl   $0x2,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xa3,%esi
lea    -0x1d(%rbp),%rdx
call   1030 <fc8150_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xa4,%esi
lea    -0x1e(%rbp),%rdx
call   1030 <fc8150_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xa8,%esi
lea    -0x1f(%rbp),%rdx
call   1030 <fc8150_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x106e,%esi
lea    -0x20(%rbp),%rdx
call   1040 <fc8150_bb_read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11c0 <fc8150_get_rssi+0xa0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <fc8150_get_rssi+0x160>
movzbl -0x20(%rbp),%ecx
mov    $0x7f,%eax
cmp    %ecx,%eax
jge    11ed <fc8150_get_rssi+0xcd>
movzbl -0x20(%rbp),%ecx
mov    $0x100,%eax
sub    %ecx,%eax
add    $0x1,%eax
imul   $0xffffffff,%eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    1206 <fc8150_get_rssi+0xe6>
movzbl -0x20(%rbp),%eax
cmp    $0x7f,%eax
jg     1201 <fc8150_get_rssi+0xe1>
movzbl -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1206 <fc8150_get_rssi+0xe6>
movzbl -0x1d(%rbp),%eax
sar    $0x6,%eax
and    $0x1,%eax
mov    %al,-0x21(%rbp)
movzbl -0x1d(%rbp),%eax
sar    $0x5,%eax
and    $0x1,%eax
mov    %al,-0x22(%rbp)
movzbl -0x1d(%rbp),%eax
and    $0x7,%eax
imul   $0x6,%eax,%ecx
movzbl -0x1e(%rbp),%eax
and    $0x1f,%eax
add    %eax,%ecx
movzbl -0x1f(%rbp),%eax
and    $0x3,%eax
movzbl -0x1f(%rbp),%edx
and    $0x70,%edx
sar    $0x4,%edx
add    %edx,%eax
imul   $0x6,%eax,%eax
add    %eax,%ecx
mov    -0x2c(%rbp),%eax
mov    $0x4,%esi
cltd
idiv   %esi
sub    %eax,%ecx
mov    -0x30(%rbp),%eax
movzbl -0x21(%rbp),%edx
imul   %edx,%eax
add    %eax,%ecx
mov    -0x34(%rbp),%eax
movzbl -0x22(%rbp),%edx
imul   %edx,%eax
add    %eax,%ecx
add    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fc8150_get_rssi>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x7(%rsp),%rdx
mov    $0xa3,%esi
call   1030 <fc8150_read@plt>
lea    0x6(%rsp),%rdx
mov    %rbx,%rdi
mov    $0xa4,%esi
call   1030 <fc8150_read@plt>
lea    0x5(%rsp),%rdx
mov    %rbx,%rdi
mov    $0xa8,%esi
call   1030 <fc8150_read@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x106e,%esi
call   1040 <fc8150_bb_read@plt>
test   %eax,%eax
jne    11ef <fc8150_get_rssi+0xcf>
movzbl 0x4(%rsp),%r8d
lea    -0x101(%r8),%esi
movzbl 0x7(%rsp),%ecx
mov    %ecx,%r9d
shr    $0x6,%r9d
and    $0x1,%r9d
mov    %ecx,%ebx
and    $0x7,%ebx
movzbl 0x6(%rsp),%r10d
and    $0x1f,%r10d
movzbl 0x5(%rsp),%edx
mov    %edx,%edi
and    $0x3,%edi
shr    $0x4,%edx
and    $0x7,%edx
add    %edi,%edx
test   %r8b,%r8b
lea    (%rbx,%rbx,2),%edi
lea    (%r10,%rdi,2),%edi
lea    (%rdx,%rdx,2),%ebx
cmovns %r8d,%esi
lea    0x3(%rsi),%edx
test   %si,%si
cmovns %esi,%edx
movswl %dx,%edx
sar    $0x2,%edx
neg    %edx
movswl %dx,%edx
lea    (%r9,%r9,4),%esi
shr    $0x4,%ecx
and    $0x2,%ecx
add    %edi,%ecx
add    %esi,%ecx
add    %edx,%ecx
lea    (%rcx,%rbx,2),%ecx
add    $0xffffff97,%ecx
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @fc8150_get_rssi(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %6
store i8 0, i8* %12
store i32 -105, i32* %13
store i32 0, i32* %14
store i32 5, i32* %15
store i32 2, i32* %16
%17 = load i8*, i8** %4
%18 = call i32 @fc8150_read(i8* %17, i32 163, i8* %7)
store i32 %18, i32* %6
%19 = load i8*, i8** %4
%20 = call i32 @fc8150_read(i8* %19, i32 164, i8* %8)
store i32 %20, i32* %6
%21 = load i8*, i8** %4
%22 = call i32 @fc8150_read(i8* %21, i32 168, i8* %9)
store i32 %22, i32* %6
%23 = load i8*, i8** %4
%24 = call i32 @fc8150_bb_read(i8* %23, i32 4206, i8* %10)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %92
29: ; preds = %2
%30 = load i8, i8* %10
%31 = zext i8 %30 to i32
%32 = icmp slt i32 127, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i8, i8* %10
%35 = zext i8 %34 to i32
%36 = sub nsw i32 256, %35
%37 = add nsw i32 %36, 1
%38 = mul nsw i32 -1, %37
store i32 %38, i32* %14
br label %47
39: ; preds = %29
%40 = load i8, i8* %10
%41 = zext i8 %40 to i32
%42 = icmp sle i32 %41, 127
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8, i8* %10
%45 = zext i8 %44 to i32
store i32 %45, i32* %14
br label %46
46: ; preds = %43, %39
br label %47
47: ; preds = %46, %33
%48 = load i8, i8* %7
%49 = zext i8 %48 to i32
%50 = ashr i32 %49, 6
%51 = and i32 %50, 1
%52 = trunc i32 %51 to i8
store i8 %52, i8* %11
%53 = load i8, i8* %7
%54 = zext i8 %53 to i32
%55 = ashr i32 %54, 5
%56 = and i32 %55, 1
%57 = trunc i32 %56 to i8
store i8 %57, i8* %12
%58 = load i8, i8* %7
%59 = zext i8 %58 to i32
%60 = and i32 %59, 7
%61 = mul nsw i32 %60, 6
%62 = load i8, i8* %8
%63 = zext i8 %62 to i32
%64 = and i32 %63, 31
%65 = add nsw i32 %61, %64
%66 = load i8, i8* %9
%67 = zext i8 %66 to i32
%68 = and i32 %67, 3
%69 = load i8, i8* %9
%70 = zext i8 %69 to i32
%71 = and i32 %70, 112
%72 = ashr i32 %71, 4
%73 = add nsw i32 %68, %72
%74 = mul nsw i32 %73, 6
%75 = add nsw i32 %65, %74
%76 = load i32, i32* %14
%77 = sdiv i32 %76, 4
%78 = sub nsw i32 %75, %77
%79 = load i32, i32* %15
%80 = load i8, i8* %11
%81 = zext i8 %80 to i32
%82 = mul nsw i32 %79, %81
%83 = add nsw i32 %78, %82
%84 = load i32, i32* %16
%85 = load i8, i8* %12
%86 = zext i8 %85 to i32
%87 = mul nsw i32 %84, %86
%88 = add nsw i32 %83, %87
%89 = load i32, i32* %13
%90 = add nsw i32 %88, %89
%91 = load i32*, i32** %5
store i32 %90, i32* %91
store i32 0, i32* %3
br label %92
92: ; preds = %47, %27
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
define i32 @fc8150_get_rssi(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = call i32 @fc8150_read(i8* %0, i32 163, i8* %3)
%8 = call i32 @fc8150_read(i8* %0, i32 164, i8* %4)
%9 = call i32 @fc8150_read(i8* %0, i32 168, i8* %5)
%10 = call i32 @fc8150_bb_read(i8* %0, i32 4206, i8* %6)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %47
12: ; preds = %2
%13 = load i8, i8* %6
%14 = zext i8 %13 to i32
%15 = icmp slt i8 %13, 0
%16 = add nuw nsw i32 %14, 65279
%17 = select i1 %15, i32 %16, i32 %14
%18 = load i8, i8* %3
%19 = lshr i8 %18, 6
%20 = and i8 %19, 1
%21 = and i8 %18, 7
%22 = mul nuw nsw i8 %21, 6
%23 = load i8, i8* %4
%24 = and i8 %23, 31
%25 = add nuw nsw i8 %22, %24
%26 = zext i8 %25 to i32
%27 = load i8, i8* %5
%28 = zext i8 %27 to i32
%29 = and i32 %28, 3
%30 = lshr i32 %28, 4
%31 = and i32 %30, 7
%32 = add nuw nsw i32 %31, %29
%33 = mul nuw nsw i32 %32, 6
%34 = trunc i32 %17 to i16
%35 = sdiv i16 %34, -4
%36 = sext i16 %35 to i32
%37 = mul nuw nsw i8 %20, 5
%38 = zext i8 %37 to i32
%39 = lshr i8 %18, 4
%40 = and i8 %39, 2
%41 = zext i8 %40 to i32
%42 = add nuw nsw i32 %41, -105
%43 = add nuw nsw i32 %42, %26
%44 = add nuw nsw i32 %43, %38
%45 = add nsw i32 %44, %36
%46 = add nsw i32 %45, %33
store i32 %46, i32* %1
br label %47
47: ; preds = %2, %12
ret i32 %10
}