#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 */
typedef int uint8_t;
typedef int uint16_t;

/* Variables and functions */
uint8_t battery_level_in_percent(const uint16_t mvolts) {
  uint8_t battery_level;
  if (mvolts >= 3000) {
    battery_level = 100;
  } else if (mvolts > 2900) {
    battery_level = 100 - ((3000 - mvolts) * 58) / 100;
  } else if (mvolts > 2740) {
    battery_level = 42 - ((2900 - mvolts) * 24) / 160;
  } else if (mvolts > 2440) {
    battery_level = 18 - ((2740 - mvolts) * 12) / 300;
  } else if (mvolts > 2100) {
    battery_level = 6 - ((2440 - mvolts) * 6) / 340;
  } else {
    battery_level = 0;
  }
  return battery_level;
}


x86 assembly (clang, -O0):
<battery_level_in_percent>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
cmpl   $0xbb8,-0x4(%rbp)
jl     1120 <battery_level_in_percent+0x20>
movl   $0x64,-0x8(%rbp)
jmp    11ff <battery_level_in_percent+0xff>
cmpl   $0xb54,-0x4(%rbp)
jle    1151 <battery_level_in_percent+0x51>
mov    $0xbb8,%eax
sub    -0x4(%rbp),%eax
imul   $0x3a,%eax,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x64,%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    11fa <battery_level_in_percent+0xfa>
cmpl   $0xab4,-0x4(%rbp)
jle    1182 <battery_level_in_percent+0x82>
mov    $0xb54,%eax
sub    -0x4(%rbp),%eax
imul   $0x18,%eax,%eax
mov    $0xa0,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x2a,%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    11f5 <battery_level_in_percent+0xf5>
cmpl   $0x988,-0x4(%rbp)
jle    11b3 <battery_level_in_percent+0xb3>
mov    $0xab4,%eax
sub    -0x4(%rbp),%eax
imul   $0xc,%eax,%eax
mov    $0x12c,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x12,%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    11f0 <battery_level_in_percent+0xf0>
cmpl   $0x834,-0x4(%rbp)
jle    11e4 <battery_level_in_percent+0xe4>
mov    $0x988,%eax
sub    -0x4(%rbp),%eax
imul   $0x6,%eax,%eax
mov    $0x154,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x6,%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    11eb <battery_level_in_percent+0xeb>
movl   $0x0,-0x8(%rbp)
jmp    11f0 <battery_level_in_percent+0xf0>
jmp    11f5 <battery_level_in_percent+0xf5>
jmp    11fa <battery_level_in_percent+0xfa>
jmp    11ff <battery_level_in_percent+0xff>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<battery_level_in_percent>:
mov    $0x64,%eax
cmp    $0xbb7,%edi
jg     11b9 <battery_level_in_percent+0xb9>
cmp    $0xb55,%edi
jl     1139 <battery_level_in_percent+0x39>
mov    $0xbb8,%eax
sub    %edi,%eax
imul   $0x3a,%eax,%eax
movzwl %ax,%eax
shr    $0x2,%eax
imul   $0x147b,%eax,%ecx
shr    $0x11,%ecx
mov    $0x64,%eax
jmp    11b7 <battery_level_in_percent+0xb7>
cmp    $0xab5,%edi
jl     1161 <battery_level_in_percent+0x61>
mov    $0xb54,%eax
sub    %edi,%eax
shl    $0x3,%eax
lea    (%rax,%rax,2),%eax
movzwl %ax,%eax
imul   $0xcccd,%eax,%ecx
shr    $0x17,%ecx
mov    $0x2a,%eax
jmp    11b7 <battery_level_in_percent+0xb7>
cmp    $0x989,%edi
jl     1190 <battery_level_in_percent+0x90>
mov    $0xab4,%eax
sub    %edi,%eax
movzwl %ax,%ecx
imul   $0x47af,%ecx,%ecx
shr    $0x10,%ecx
sub    %ecx,%eax
movzwl %ax,%edx
shr    %edx
add    %ecx,%edx
shr    $0x4,%edx
mov    $0x12,%eax
sub    %edx,%eax
ret
xor    %eax,%eax
cmp    $0x835,%edi
jl     11b9 <battery_level_in_percent+0xb9>
mov    $0x988,%eax
sub    %edi,%eax
add    %eax,%eax
lea    (%rax,%rax,2),%eax
movzwl %ax,%eax
imul   $0xc0c1,%eax,%ecx
shr    $0x18,%ecx
mov    $0x6,%eax
sub    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @battery_level_in_percent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sge i32 %4, 3000
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 100, i32* %3
br label %48
7: ; preds = %1
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 2900
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i32, i32* %2
%12 = sub nsw i32 3000, %11
%13 = mul nsw i32 %12, 58
%14 = sdiv i32 %13, 100
%15 = sub nsw i32 100, %14
store i32 %15, i32* %3
br label %47
16: ; preds = %7
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 2740
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* %2
%21 = sub nsw i32 2900, %20
%22 = mul nsw i32 %21, 24
%23 = sdiv i32 %22, 160
%24 = sub nsw i32 42, %23
store i32 %24, i32* %3
br label %46
25: ; preds = %16
%26 = load i32, i32* %2
%27 = icmp sgt i32 %26, 2440
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* %2
%30 = sub nsw i32 2740, %29
%31 = mul nsw i32 %30, 12
%32 = sdiv i32 %31, 300
%33 = sub nsw i32 18, %32
store i32 %33, i32* %3
br label %45
34: ; preds = %25
%35 = load i32, i32* %2
%36 = icmp sgt i32 %35, 2100
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* %2
%39 = sub nsw i32 2440, %38
%40 = mul nsw i32 %39, 6
%41 = sdiv i32 %40, 340
%42 = sub nsw i32 6, %41
store i32 %42, i32* %3
br label %44
43: ; preds = %34
store i32 0, i32* %3
br label %44
44: ; preds = %43, %37
br label %45
45: ; preds = %44, %28
br label %46
46: ; preds = %45, %19
br label %47
47: ; preds = %46, %10
br label %48
48: ; preds = %47, %6
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @battery_level_in_percent(i32 %0) {
%2 = icmp sgt i32 %0, 2999
br i1 %2, label %38, label %3
3: ; preds = %1
%4 = icmp sgt i32 %0, 2900
br i1 %4, label %5, label %12
5: ; preds = %3
%6 = trunc i32 %0 to i16
%7 = sub nuw nsw i16 3000, %6
%8 = mul nuw nsw i16 %7, 58
%9 = udiv i16 %8, 100
%10 = zext i16 %9 to i32
%11 = sub nuw nsw i32 100, %10
br label %38
12: ; preds = %3
%13 = icmp sgt i32 %0, 2740
br i1 %13, label %14, label %21
14: ; preds = %12
%15 = trunc i32 %0 to i16
%16 = sub nuw nsw i16 2900, %15
%17 = mul nuw nsw i16 %16, 24
%18 = udiv i16 %17, 160
%19 = zext i16 %18 to i32
%20 = sub nuw nsw i32 42, %19
br label %38
21: ; preds = %12
%22 = icmp sgt i32 %0, 2440
br i1 %22, label %23, label %29
23: ; preds = %21
%24 = trunc i32 %0 to i16
%25 = sub nuw nsw i16 2740, %24
%26 = udiv i16 %25, 25
%27 = zext i16 %26 to i32
%28 = sub nuw nsw i32 18, %27
br label %38
29: ; preds = %21
%30 = icmp sgt i32 %0, 2100
br i1 %30, label %31, label %38
31: ; preds = %29
%32 = trunc i32 %0 to i16
%33 = sub nuw nsw i16 2440, %32
%34 = mul nuw nsw i16 %33, 6
%35 = udiv i16 %34, 340
%36 = zext i16 %35 to i32
%37 = sub nuw nsw i32 6, %36
br label %38
38: ; preds = %29, %1, %5, %23, %31, %14
%39 = phi i32 [ %11, %5 ], [ %20, %14 ], [ %28, %23 ], [ %37, %31 ], [ 100, %1 ], [ 0, %29 ]
ret i32 %39
}