#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 scalar_t__ uint32_t;
typedef int uint16_t;

/* Variables and functions */
uint16_t VL53L0X_encode_timeout(uint32_t timeout_macro_clks) {
  uint16_t encoded_timeout = 0;
  uint32_t ls_byte = 0;
  uint16_t ms_byte = 0;
  if (timeout_macro_clks > 0) {
    ls_byte = timeout_macro_clks - 1;
    while ((ls_byte & 0xFFFFFF00) > 0) {
      ls_byte = ls_byte >> 1;
      ms_byte++;
    }
    encoded_timeout = (ms_byte << 8) + (uint16_t)(ls_byte & 0x000000FF);
  }
  return encoded_timeout;
}


x86 assembly (clang, -O0):
<VL53L0X_encode_timeout>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    117d <VL53L0X_encode_timeout+0x7d>
mov    -0x8(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
movabs $0xffffff00,%rax
and    -0x18(%rbp),%rax
cmp    $0x0,%rax
jle    1167 <VL53L0X_encode_timeout+0x67>
mov    -0x18(%rbp),%rax
sar    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1135 <VL53L0X_encode_timeout+0x35>
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
mov    -0x18(%rbp),%rcx
and    $0xff,%rcx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<VL53L0X_encode_timeout>:
test   %rdi,%rdi
jle    1138 <VL53L0X_encode_timeout+0x38>
add    $0xffffffffffffffff,%rdi
xor    %ecx,%ecx
test   $0xffffff00,%edi
je     1131 <VL53L0X_encode_timeout+0x31>
movabs $0x1fffffe00,%rax
mov    %rdi,%rdx
sar    %rdi
add    $0x100,%ecx
test   %rax,%rdx
mov    %rdi,%rdx
jne    1120 <VL53L0X_encode_timeout+0x20>
movzbl %dil,%eax
or     %ecx,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @VL53L0X_encode_timeout(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %3
store i64 0, i64* %4
store i32 0, i32* %5
%6 = load i64, i64* %2
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %1
%9 = load i64, i64* %2
%10 = sub nsw i64 %9, 1
store i64 %10, i64* %4
br label %11
11: ; preds = %15, %8
%12 = load i64, i64* %4
%13 = and i64 %12, 4294967040
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i64, i64* %4
%17 = ashr i64 %16, 1
store i64 %17, i64* %4
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
br label %11
20: ; preds = %11
%21 = load i32, i32* %5
%22 = shl i32 %21, 8
%23 = load i64, i64* %4
%24 = and i64 %23, 255
%25 = trunc i64 %24 to i32
%26 = add nsw i32 %22, %25
store i32 %26, i32* %3
br label %27
27: ; preds = %20, %1
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @VL53L0X_encode_timeout(i64 %0) {
%2 = icmp sgt i64 %0, 0
br i1 %2, label %3, label %22
3: ; preds = %1
%4 = add nsw i64 %0, -1
%5 = and i64 %4, 4294967040
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %3, %7
%8 = phi i32 [ %11, %7 ], [ 0, %3 ]
%9 = phi i64 [ %10, %7 ], [ %4, %3 ]
%10 = ashr i64 %9, 1
%11 = add nuw nsw i32 %8, 1
%12 = and i64 %9, 8589934080
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %7
14: ; preds = %7
%15 = shl i32 %11, 8
br label %16
16: ; preds = %14, %3
%17 = phi i64 [ %4, %3 ], [ %10, %14 ]
%18 = phi i32 [ 0, %3 ], [ %15, %14 ]
%19 = trunc i64 %17 to i32
%20 = and i32 %19, 255
%21 = or i32 %18, %20
br label %22
22: ; preds = %16, %1
%23 = phi i32 [ %21, %16 ], [ 0, %1 ]
ret i32 %23
}