; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/edge-impulse-sdk/tensorflow/lite/kernels/internal (quantization_util)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%union.anon.0 = type { double }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZN6tflite26DoubleFromFractionAndShiftEli(i64 noundef %0, i32 noundef %1) #1 {
  %3 = alloca double, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca %union.anon.0, align 8
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = call noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() #2
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %28

15:                                               ; preds = %2
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = call noundef double @_ZNSt14numeric_limitsIdE9quiet_NaNEv() #2
  store double %19, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %92

20:                                               ; preds = %15
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = icmp sgt i64 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %20
  %24 = call noundef double @_ZNSt14numeric_limitsIdE8infinityEv() #2
  store double %24, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %92

25:                                               ; preds = %20
  %26 = call noundef double @_ZNSt14numeric_limitsIdE8infinityEv() #2
  %27 = fneg double %26
  store double %27, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %92

28:                                               ; preds = %2
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = icmp eq i64 %29, 0
  br i1 %30, label %31, label %33

31:                                               ; preds = %28
  store i64 0, ptr %6, align 8, !tbaa !11
  %32 = load double, ptr %6, align 8, !tbaa !11
  store double %32, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %92

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = icmp slt i64 %34, 0
  %36 = zext i1 %35 to i8
  store i8 %36, ptr %8, align 1, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %37 = load i8, ptr %8, align 1, !tbaa !12, !range !14, !noundef !15
  %38 = trunc i8 %37 to i1
  br i1 %38, label %39, label %42

39:                                               ; preds = %33
  %40 = load i64, ptr %4, align 8, !tbaa !5
  %41 = sub nsw i64 0, %40
  br label %44

42:                                               ; preds = %33
  %43 = load i64, ptr %4, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %42, %39
  %45 = phi i64 [ %41, %39 ], [ %43, %42 ]
  store i64 %45, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = sub nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  store i64 %48, ptr %10, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %52, %44
  %50 = load i64, ptr %9, align 8, !tbaa !5
  %51 = icmp slt i64 %50, 1073741824
  br i1 %51, label %52, label %57

52:                                               ; preds = %49
  %53 = load i64, ptr %9, align 8, !tbaa !5
  %54 = mul nsw i64 %53, 2
  store i64 %54, ptr %9, align 8, !tbaa !5
  %55 = load i64, ptr %10, align 8, !tbaa !5
  %56 = sub nsw i64 %55, 1
  store i64 %56, ptr %10, align 8, !tbaa !5
  br label %49, !llvm.loop !16

57:                                               ; preds = %49
  br label %58

58:                                               ; preds = %61, %57
  %59 = load i64, ptr %9, align 8, !tbaa !5
  %60 = icmp sgt i64 %59, 2147483648
  br i1 %60, label %61, label %66

61:                                               ; preds = %58
  %62 = load i64, ptr %9, align 8, !tbaa !5
  %63 = sdiv i64 %62, 2
  store i64 %63, ptr %9, align 8, !tbaa !5
  %64 = load i64, ptr %10, align 8, !tbaa !5
  %65 = add nsw i64 %64, 1
  store i64 %65, ptr %10, align 8, !tbaa !5
  br label %58, !llvm.loop !18

66:                                               ; preds = %58
  %67 = load i64, ptr %9, align 8, !tbaa !5
  %68 = sub nsw i64 %67, 1073741824
  store i64 %68, ptr %9, align 8, !tbaa !5
  %69 = load i64, ptr %10, align 8, !tbaa !5
  %70 = icmp slt i64 %69, -1022
  br i1 %70, label %71, label %72

71:                                               ; preds = %66
  store i64 -1023, ptr %10, align 8, !tbaa !5
  br label %77

72:                                               ; preds = %66
  %73 = load i64, ptr %10, align 8, !tbaa !5
  %74 = icmp sgt i64 %73, 1022
  br i1 %74, label %75, label %76

75:                                               ; preds = %72
  store i64 1023, ptr %10, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %75, %72
  br label %77

77:                                               ; preds = %76, %71
  %78 = load i64, ptr %10, align 8, !tbaa !5
  %79 = add nsw i64 %78, 1023
  store i64 %79, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %80 = load i8, ptr %8, align 1, !tbaa !12, !range !14, !noundef !15
  %81 = trunc i8 %80 to i1
  %82 = zext i1 %81 to i64
  %83 = select i1 %81, i64 -9223372036854775808, i64 0
  store i64 %83, ptr %11, align 8, !tbaa !5
  %84 = load i64, ptr %11, align 8, !tbaa !5
  %85 = load i64, ptr %10, align 8, !tbaa !5
  %86 = shl i64 %85, 52
  %87 = or i64 %84, %86
  %88 = load i64, ptr %9, align 8, !tbaa !5
  %89 = shl i64 %88, 22
  %90 = or i64 %87, %89
  store i64 %90, ptr %6, align 8, !tbaa !11
  %91 = load double, ptr %6, align 8, !tbaa !11
  store double %91, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  br label %92

92:                                               ; preds = %77, %31, %25, %23, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %93 = load double, ptr %3, align 8
  ret double %93
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZNSt14numeric_limitsIdE9quiet_NaNEv() #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZNSt14numeric_limitsIdE8infinityEv() #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZN6tflite26DoubleFromFractionAndShiftEli(i64 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() #2
  %4 = icmp eq i32 %3, %1
  %5 = icmp eq i64 %0, 0
  br i1 %4, label %6, label %14

6:                                                ; preds = %2
  br i1 %5, label %7, label %9

7:                                                ; preds = %6
  %8 = tail call noundef double @_ZNSt14numeric_limitsIdE9quiet_NaNEv() #2
  br label %38

9:                                                ; preds = %6
  %10 = icmp sgt i64 %0, 0
  %11 = tail call noundef double @_ZNSt14numeric_limitsIdE8infinityEv() #2
  br i1 %10, label %38, label %12

12:                                               ; preds = %9
  %13 = fneg double %11
  br label %38

14:                                               ; preds = %2
  br i1 %5, label %38, label %15

15:                                               ; preds = %14
  %16 = tail call i64 @llvm.abs.i64(i64 %0, i1 true)
  %17 = add nsw i32 %1, -1
  %18 = sext i32 %17 to i64
  %19 = icmp ult i64 %16, 1073741824
  br i1 %19, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %15
  %.025.lcssa = phi i64 [ %16, %15 ], [ %21, %.lr.ph ]
  %.024.lcssa = phi i64 [ %18, %15 ], [ %22, %.lr.ph ]
  %20 = icmp ugt i64 %.025.lcssa, 2147483648
  br i1 %20, label %.lr.ph35, label %._crit_edge

.lr.ph:                                           ; preds = %15, %.lr.ph
  %.02431 = phi i64 [ %22, %.lr.ph ], [ %18, %15 ]
  %.02530 = phi i64 [ %21, %.lr.ph ], [ %16, %15 ]
  %21 = shl nuw nsw i64 %.02530, 1
  %22 = add nsw i64 %.02431, -1
  %23 = icmp ult i64 %.02530, 536870912
  br i1 %23, label %.lr.ph, label %.preheader, !llvm.loop !5

.lr.ph35:                                         ; preds = %.preheader, %.lr.ph35
  %.134 = phi i64 [ %25, %.lr.ph35 ], [ %.024.lcssa, %.preheader ]
  %.12633 = phi i64 [ %24, %.lr.ph35 ], [ %.025.lcssa, %.preheader ]
  %24 = lshr i64 %.12633, 1
  %25 = add nsw i64 %.134, 1
  %26 = icmp ugt i64 %.12633, 4294967297
  br i1 %26, label %.lr.ph35, label %._crit_edge, !llvm.loop !7

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader
  %.126.lcssa = phi i64 [ %.025.lcssa, %.preheader ], [ %24, %.lr.ph35 ]
  %.1.lcssa = phi i64 [ %.024.lcssa, %.preheader ], [ %25, %.lr.ph35 ]
  %27 = icmp slt i64 %.1.lcssa, -1022
  br i1 %27, label %31, label %28

28:                                               ; preds = %._crit_edge
  %spec.store.select = tail call i64 @llvm.smin.i64(i64 %.1.lcssa, i64 1023)
  %29 = shl nsw i64 %spec.store.select, 52
  %30 = add i64 %29, 4607182418800017408
  br label %31

31:                                               ; preds = %._crit_edge, %28
  %.2 = phi i64 [ %30, %28 ], [ 0, %._crit_edge ]
  %32 = and i64 %0, -9223372036854775808
  %33 = shl nuw nsw i64 %.126.lcssa, 22
  %34 = add nsw i64 %33, -4503599627370496
  %35 = or i64 %34, %32
  %36 = or i64 %35, %.2
  %37 = bitcast i64 %36 to double
  br label %38

38:                                               ; preds = %9, %14, %31, %12, %7
  %.0 = phi double [ %8, %7 ], [ %13, %12 ], [ %37, %31 ], [ 0.000000e+00, %14 ], [ %11, %9 ]
  ret double %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZNSt14numeric_limitsIdE9quiet_NaNEv() local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZNSt14numeric_limitsIdE8infinityEv() local_unnamed_addr #0 align 2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #1

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass