#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*/ send_ctrl_cmd(int);
int /*<<< orphan*/ send_data_cmd(unsigned int);
void lcm_setbacklight(unsigned int level) {
  unsigned int default_level = 52;
  unsigned int mapped_level = 0;
  if (level > 255)
    mapped_level = 255;
  if (level >= 102)
    mapped_level =
        default_level + (level - 102) * (255 - default_level) / (255 - 102);
  else
    mapped_level = default_level - (102 - level) * default_level / 102;
  send_ctrl_cmd(0x51);
  send_data_cmd(mapped_level);
}


x86 assembly (clang, -O0):
<lcm_setbacklight>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x34,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xff,-0x4(%rbp)
jbe    114d <lcm_setbacklight+0x2d>
movl   $0xff,-0xc(%rbp)
cmpl   $0x66,-0x4(%rbp)
jb     1186 <lcm_setbacklight+0x66>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x66,%eax
mov    $0xff,%ecx
sub    -0x8(%rbp),%ecx
imul   %ecx,%eax
mov    $0x99,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11ab <lcm_setbacklight+0x8b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x66,%eax
sub    -0x4(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    $0x66,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    $0x51,%edi
call   1040 <send_ctrl_cmd@plt>
mov    -0xc(%rbp),%edi
call   1030 <send_data_cmd@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lcm_setbacklight>:
push   %rbx
cmp    $0x66,%edi
jb     1143 <lcm_setbacklight+0x23>
imul   $0xcb,%edi,%eax
add    $0xffffaf1e,%eax
mov    $0xd62b80d7,%ebx
imul   %rax,%rbx
shr    $0x27,%rbx
add    $0x34,%ebx
jmp    1160 <lcm_setbacklight+0x40>
mov    $0x66,%eax
sub    %edi,%eax
imul   $0x34,%eax,%eax
movzwl %ax,%eax
imul   $0xa0a1,%eax,%eax
shr    $0x16,%eax
mov    $0x34,%ebx
sub    %eax,%ebx
mov    $0x51,%edi
call   1040 <send_ctrl_cmd@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1030 <send_data_cmd@plt>

LLVM IR (-O0):
define void @lcm_setbacklight(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 52, i32* %3
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = icmp ugt i32 %5, 255
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 255, i32* %4
br label %8
8: ; preds = %7, %1
%9 = load i32, i32* %2
%10 = icmp uge i32 %9, 102
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i32, i32* %3
%13 = load i32, i32* %2
%14 = sub i32 %13, 102
%15 = load i32, i32* %3
%16 = sub i32 255, %15
%17 = mul i32 %14, %16
%18 = udiv i32 %17, 153
%19 = add i32 %12, %18
store i32 %19, i32* %4
br label %28
20: ; preds = %8
%21 = load i32, i32* %3
%22 = load i32, i32* %2
%23 = sub i32 102, %22
%24 = load i32, i32* %3
%25 = mul i32 %23, %24
%26 = udiv i32 %25, 102
%27 = sub i32 %21, %26
store i32 %27, i32* %4
br label %28
28: ; preds = %20, %11
%29 = call i32 @send_ctrl_cmd(i32 81)
%30 = load i32, i32* %4
%31 = call i32 @send_data_cmd(i32 %30)
ret void
}

LLVM IR (-O2):
define void @lcm_setbacklight(i32 %0) {
%2 = icmp ugt i32 %0, 101
br i1 %2, label %3, label %8
3: ; preds = %1
%4 = mul i32 %0, 203
%5 = add i32 %4, -20706
%6 = udiv i32 %5, 153
%7 = add nuw nsw i32 %6, 52
br label %15
8: ; preds = %1
%9 = trunc i32 %0 to i16
%10 = sub nuw nsw i16 102, %9
%11 = mul nuw nsw i16 %10, 52
%12 = udiv i16 %11, 102
%13 = zext i16 %12 to i32
%14 = sub nuw nsw i32 52, %13
br label %15
15: ; preds = %8, %3
%16 = phi i32 [ %7, %3 ], [ %14, %8 ]
%17 = tail call i32 @send_ctrl_cmd(i32 81)
%18 = tail call i32 @send_data_cmd(i32 %16)
ret void
}