; LLVM IR from sp-nitech/SPTK :: src/compression/uniform_quantization.ll (ZNK4sptk19UniformQuantization3RunEdPi)

; 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"

%"class.sptk::UniformQuantization" = type { ptr, double, i32, i32, i8, i32, double }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZNK4sptk19UniformQuantization3RunEdPi(ptr noundef nonnull align 8 dereferenceable(40) %0, double noundef %1, ptr noundef %2) #0 align 2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store double %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8
  %11 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 4
  %12 = load i8, ptr %11, align 8, !tbaa !11, !range !16, !noundef !17
  %13 = trunc i8 %12 to i1
  br i1 %13, label %14, label %17

14:                                               ; preds = %3
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = icmp eq ptr null, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %14, %3
  store i1 false, ptr %4, align 1
  br label %65

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %19 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 3
  %20 = load i32, ptr %19, align 4, !tbaa !18
  switch i32 %20, label %46 [
    i32 0, label %21
    i32 1, label %33
  ]

21:                                               ; preds = %18
  %22 = load double, ptr %6, align 8, !tbaa !9
  %23 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 6
  %24 = load double, ptr %23, align 8, !tbaa !19
  %25 = fmul double %22, %24
  %26 = call double @llvm.floor.f64(double %25)
  %27 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 5
  %28 = load i32, ptr %27, align 4, !tbaa !20
  %29 = sdiv i32 %28, 2
  %30 = sitofp i32 %29 to double
  %31 = fadd double %26, %30
  %32 = fptosi double %31 to i32
  store i32 %32, ptr %8, align 4, !tbaa !21
  br label %47

33:                                               ; preds = %18
  %34 = load double, ptr %6, align 8, !tbaa !9
  %35 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 6
  %36 = load double, ptr %35, align 8, !tbaa !19
  %37 = fmul double %34, %36
  %38 = call double @llvm.round.f64(double %37)
  %39 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 5
  %40 = load i32, ptr %39, align 4, !tbaa !20
  %41 = sub nsw i32 %40, 1
  %42 = sdiv i32 %41, 2
  %43 = sitofp i32 %42 to double
  %44 = fadd double %38, %43
  %45 = fptosi double %44 to i32
  store i32 %45, ptr %8, align 4, !tbaa !21
  br label %47

46:                                               ; preds = %18
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %64

47:                                               ; preds = %33, %21
  %48 = load i32, ptr %8, align 4, !tbaa !21
  %49 = icmp slt i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %47
  store i32 0, ptr %8, align 4, !tbaa !21
  br label %61

51:                                               ; preds = %47
  %52 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 5
  %53 = load i32, ptr %52, align 4, !tbaa !20
  %54 = load i32, ptr %8, align 4, !tbaa !21
  %55 = icmp sle i32 %53, %54
  br i1 %55, label %56, label %60

56:                                               ; preds = %51
  %57 = getelementptr inbounds %"class.sptk::UniformQuantization", ptr %10, i32 0, i32 5
  %58 = load i32, ptr %57, align 4, !tbaa !20
  %59 = sub nsw i32 %58, 1
  store i32 %59, ptr %8, align 4, !tbaa !21
  br label %60

60:                                               ; preds = %56, %51
  br label %61

61:                                               ; preds = %60, %50
  %62 = load i32, ptr %8, align 4, !tbaa !21
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %62, ptr %63, align 4, !tbaa !21
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %64

64:                                               ; preds = %61, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %65

65:                                               ; preds = %64, %17
  %66 = load i1, ptr %4, align 1
  ret i1 %66
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.floor.f64(double) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.round.f64(double) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !15, i64 24}
!12 = !{!"_ZTSN4sptk19UniformQuantizationE", !10, i64 8, !13, i64 16, !14, i64 20, !15, i64 24, !13, i64 28, !10, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTSN4sptk19UniformQuantization16QuantizationTypeE", !7, i64 0}
!15 = !{!"bool", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!12, !14, i64 20}
!19 = !{!12, !10, i64 32}
!20 = !{!12, !13, i64 28}
!21 = !{!13, !13, 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 nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local noundef zeroext i1 @_ZNK4sptk19UniformQuantization3RunEdPi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(40) %0, double noundef %1, ptr noundef writeonly %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i8, ptr %4, align 8, !tbaa !5, !range !13, !noundef !14
  %6 = trunc nuw i8 %5 to i1
  %7 = icmp ne ptr %2, null
  %or.cond.not = and i1 %7, %6
  br i1 %or.cond.not, label %8, label %35

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 20
  %10 = load i32, ptr %9, align 4, !tbaa !15
  switch i32 %10, label %35 [
    i32 0, label %11
    i32 1, label %21
  ]

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 32
  %13 = load double, ptr %12, align 8, !tbaa !16
  %14 = fmul double %13, %1
  %15 = tail call double @llvm.floor.f64(double %14)
  %16 = getelementptr inbounds i8, ptr %0, i64 28
  %17 = load i32, ptr %16, align 4
  %18 = sdiv i32 %17, 2
  %19 = sitofp i32 %18 to double
  %20 = fadd double %15, %19
  %.pre = add nsw i32 %17, -1
  br label %32

21:                                               ; preds = %8
  %22 = getelementptr inbounds i8, ptr %0, i64 32
  %23 = load double, ptr %22, align 8, !tbaa !16
  %24 = fmul double %23, %1
  %25 = tail call double @llvm.round.f64(double %24)
  %26 = getelementptr inbounds i8, ptr %0, i64 28
  %27 = load i32, ptr %26, align 4
  %28 = add nsw i32 %27, -1
  %29 = sdiv i32 %28, 2
  %30 = sitofp i32 %29 to double
  %31 = fadd double %25, %30
  br label %32

32:                                               ; preds = %21, %11
  %.pre-phi = phi i32 [ %28, %21 ], [ %.pre, %11 ]
  %33 = phi i32 [ %27, %21 ], [ %17, %11 ]
  %.0.in = phi double [ %31, %21 ], [ %20, %11 ]
  %.0 = fptosi double %.0.in to i32
  %34 = icmp slt i32 %.0, 0
  %.not = icmp sgt i32 %33, %.0
  %spec.select = select i1 %.not, i32 %.0, i32 %.pre-phi
  %.1 = select i1 %34, i32 0, i32 %spec.select
  store i32 %.1, ptr %2, align 4, !tbaa !17
  br label %35

35:                                               ; preds = %32, %8, %3
  %.110 = phi i1 [ false, %3 ], [ true, %32 ], [ false, %8 ]
  ret i1 %.110
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.floor.f64(double) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.round.f64(double) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !12, i64 24}
!6 = !{!"_ZTSN4sptk19UniformQuantizationE", !7, i64 8, !10, i64 16, !11, i64 20, !12, i64 24, !10, i64 28, !7, i64 32}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_ZTSN4sptk19UniformQuantization16QuantizationTypeE", !8, i64 0}
!12 = !{!"bool", !8, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !11, i64 20}
!16 = !{!6, !7, i64 32}
!17 = !{!10, !10, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass