; 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 = 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 i64 @_ZN6tflite12IntegerFrExpEdPi(double noundef %0, ptr noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca double, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %union.anon, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  store double %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %11 = load double, ptr %4, align 8, !tbaa !5
  store double %11, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %12 = load i64, ptr %6, align 8, !tbaa !11
  store i64 %12, ptr %7, align 8, !tbaa !12
  %13 = load i64, ptr %7, align 8, !tbaa !12
  %14 = and i64 %13, 9223372036854775807
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %18

16:                                               ; preds = %2
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %17, align 4, !tbaa !14
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %65

18:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %19 = load i64, ptr %7, align 8, !tbaa !12
  %20 = and i64 %19, 9218868437227405312
  %21 = lshr i64 %20, 52
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %9, align 4, !tbaa !14
  %23 = load i32, ptr %9, align 4, !tbaa !14
  %24 = icmp eq i32 %23, 2047
  br i1 %24, label %25, label %40

25:                                               ; preds = %18
  %26 = call noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() #2
  %27 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 %26, ptr %27, align 4, !tbaa !14
  %28 = load i64, ptr %7, align 8, !tbaa !12
  %29 = and i64 %28, 4503599623176192
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %25
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %64

32:                                               ; preds = %25
  %33 = load i64, ptr %7, align 8, !tbaa !12
  %34 = and i64 %33, -9223372036854775808
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %32
  %37 = call noundef i64 @_ZNSt14numeric_limitsIlE3minEv() #2
  store i64 %37, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %64

38:                                               ; preds = %32
  %39 = call noundef i64 @_ZNSt14numeric_limitsIlE3maxEv() #2
  store i64 %39, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %64

40:                                               ; preds = %18
  %41 = load i32, ptr %9, align 4, !tbaa !14
  %42 = sub i32 %41, 1023
  %43 = add i32 %42, 1
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 %43, ptr %44, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %45 = load i64, ptr %7, align 8, !tbaa !12
  %46 = and i64 %45, 4503599623176192
  %47 = lshr i64 %46, 22
  %48 = add i64 1073741824, %47
  store i64 %48, ptr %10, align 8, !tbaa !12
  %49 = load i64, ptr %7, align 8, !tbaa !12
  %50 = and i64 %49, 4194303
  %51 = icmp ugt i64 %50, 2097152
  br i1 %51, label %52, label %55

52:                                               ; preds = %40
  %53 = load i64, ptr %10, align 8, !tbaa !12
  %54 = add nsw i64 %53, 1
  store i64 %54, ptr %10, align 8, !tbaa !12
  br label %55

55:                                               ; preds = %52, %40
  %56 = load i64, ptr %7, align 8, !tbaa !12
  %57 = and i64 %56, -9223372036854775808
  %58 = icmp ne i64 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %55
  %60 = load i64, ptr %10, align 8, !tbaa !12
  %61 = mul nsw i64 %60, -1
  store i64 %61, ptr %10, align 8, !tbaa !12
  br label %62

62:                                               ; preds = %59, %55
  %63 = load i64, ptr %10, align 8, !tbaa !12
  store i64 %63, ptr %3, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %64

64:                                               ; preds = %62, %38, %36, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %65

65:                                               ; preds = %64, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %66 = load i64, ptr %3, align 8
  ret i64 %66
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNSt14numeric_limitsIlE3maxEv() #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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


; 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 i64 @_ZN6tflite12IntegerFrExpEdPi(double noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = bitcast double %0 to i64
  %4 = fcmp oeq double %0, 0.000000e+00
  br i1 %4, label %5, label %6

5:                                                ; preds = %2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %28

6:                                                ; preds = %2
  %7 = lshr i64 %3, 52
  %8 = trunc nuw nsw i64 %7 to i32
  %9 = and i32 %8, 2047
  %10 = icmp eq i32 %9, 2047
  br i1 %10, label %11, label %19

11:                                               ; preds = %6
  %12 = tail call noundef i32 @_ZNSt14numeric_limitsIiE3maxEv() #1
  store i32 %12, ptr %1, align 4, !tbaa !5
  %13 = and i64 %3, 4503599623176192
  %.not19 = icmp eq i64 %13, 0
  br i1 %.not19, label %14, label %28

14:                                               ; preds = %11
  %.not20 = icmp sgt i64 %3, -1
  br i1 %.not20, label %17, label %15

15:                                               ; preds = %14
  %16 = tail call noundef i64 @_ZNSt14numeric_limitsIlE3minEv() #1
  br label %28

17:                                               ; preds = %14
  %18 = tail call noundef i64 @_ZNSt14numeric_limitsIlE3maxEv() #1
  br label %28

19:                                               ; preds = %6
  %20 = add nsw i32 %9, -1022
  store i32 %20, ptr %1, align 4, !tbaa !5
  %21 = lshr i64 %3, 22
  %22 = and i64 %21, 1073741823
  %23 = or disjoint i64 %22, 1073741824
  %24 = and i64 %3, 4194303
  %25 = icmp ugt i64 %24, 2097152
  %26 = add nuw nsw i64 %22, 1073741825
  %spec.select = select i1 %25, i64 %26, i64 %23
  %27 = sub nsw i64 0, %spec.select
  %.not21 = icmp slt i64 %3, 0
  %.1 = select i1 %.not21, i64 %27, i64 %spec.select
  br label %28

28:                                               ; preds = %15, %17, %19, %11, %5
  %.117 = phi i64 [ 0, %5 ], [ %16, %15 ], [ %18, %17 ], [ %.1, %19 ], [ 0, %11 ]
  ret i64 %.117
}

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

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

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass