; LLVM IR from aap/librw :: src/base.ll (ZN2rw6Matrix8optimizeEPNS0_9ToleranceE)

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

%"struct.rw::Matrix::Tolerance" = type { float, float, float }
%"struct.rw::Matrix" = type { %"struct.rw::V3d", i32, %"struct.rw::V3d", i32, %"struct.rw::V3d", i32, %"struct.rw::V3d", i32 }
%"struct.rw::V3d" = type { float, float, float }

@_ZN2rwL22matrixDefaultToleranceE = external hidden global %"struct.rw::Matrix::Tolerance", align 4

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw6Matrix8optimizeEPNS0_9ToleranceE(ptr noundef nonnull align 4 dereferenceable(64) %0, ptr noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store ptr @_ZN2rwL22matrixDefaultToleranceE, ptr %4, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %11, %2
  %13 = call noundef float @_ZN2rw6Matrix11normalErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %8)
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %"struct.rw::Matrix::Tolerance", ptr %14, i32 0, i32 0
  %16 = load float, ptr %15, align 4, !tbaa !9
  %17 = fcmp ole float %13, %16
  %18 = zext i1 %17 to i32
  store i32 %18, ptr %5, align 4, !tbaa !12
  %19 = call noundef float @_ZN2rw6Matrix15orthogonalErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %8)
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.rw::Matrix::Tolerance", ptr %20, i32 0, i32 1
  %22 = load float, ptr %21, align 4, !tbaa !14
  %23 = fcmp ole float %19, %22
  %24 = zext i1 %23 to i32
  store i32 %24, ptr %6, align 4, !tbaa !12
  %25 = load i32, ptr %5, align 4, !tbaa !12
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %36

27:                                               ; preds = %12
  %28 = load i32, ptr %6, align 4, !tbaa !12
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %36

30:                                               ; preds = %27
  %31 = call noundef float @_ZN2rw6Matrix13identityErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %8)
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.rw::Matrix::Tolerance", ptr %32, i32 0, i32 2
  %34 = load float, ptr %33, align 4, !tbaa !15
  %35 = fcmp ole float %31, %34
  br label %36

36:                                               ; preds = %30, %27, %12
  %37 = phi i1 [ false, %27 ], [ false, %12 ], [ %35, %30 ]
  %38 = zext i1 %37 to i32
  store i32 %38, ptr %7, align 4, !tbaa !12
  %39 = load i32, ptr %5, align 4, !tbaa !12
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %45

41:                                               ; preds = %36
  %42 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !16
  %44 = or i32 %43, 1
  store i32 %44, ptr %42, align 4, !tbaa !16
  br label %49

45:                                               ; preds = %36
  %46 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !16
  %48 = and i32 %47, -2
  store i32 %48, ptr %46, align 4, !tbaa !16
  br label %49

49:                                               ; preds = %45, %41
  %50 = load i32, ptr %6, align 4, !tbaa !12
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %56

52:                                               ; preds = %49
  %53 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !16
  %55 = or i32 %54, 2
  store i32 %55, ptr %53, align 4, !tbaa !16
  br label %60

56:                                               ; preds = %49
  %57 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !16
  %59 = and i32 %58, -3
  store i32 %59, ptr %57, align 4, !tbaa !16
  br label %60

60:                                               ; preds = %56, %52
  %61 = load i32, ptr %7, align 4, !tbaa !12
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %67

63:                                               ; preds = %60
  %64 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %65 = load i32, ptr %64, align 4, !tbaa !16
  %66 = or i32 %65, 131072
  store i32 %66, ptr %64, align 4, !tbaa !16
  br label %71

67:                                               ; preds = %60
  %68 = getelementptr inbounds %"struct.rw::Matrix", ptr %8, i32 0, i32 1
  %69 = load i32, ptr %68, align 4, !tbaa !16
  %70 = and i32 %69, -131073
  store i32 %70, ptr %68, align 4, !tbaa !16
  br label %71

71:                                               ; preds = %67, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix11normalErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix15orthogonalErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix13identityErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTSN2rw6Matrix9ToleranceE", !11, i64 0, !11, i64 4, !11, i64 8}
!11 = !{!"float", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !11, i64 4}
!15 = !{!10, !11, i64 8}
!16 = !{!17, !13, i64 12}
!17 = !{!"_ZTSN2rw6MatrixE", !18, i64 0, !13, i64 12, !18, i64 16, !13, i64 28, !18, i64 32, !13, i64 44, !18, i64 48, !13, i64 60}
!18 = !{!"_ZTSN2rw3V3dE", !11, i64 0, !11, i64 4, !11, i64 8}


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

%"struct.rw::Matrix::Tolerance" = type { float, float, float }

@_ZN2rwL22matrixDefaultToleranceE = external hidden local_unnamed_addr global %"struct.rw::Matrix::Tolerance", align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw6Matrix8optimizeEPNS0_9ToleranceE(ptr noundef nonnull align 4 dereferenceable(64) %0, ptr noundef readonly %1) local_unnamed_addr #0 align 2 {
  %3 = icmp eq ptr %1, null
  %spec.store.select = select i1 %3, ptr @_ZN2rwL22matrixDefaultToleranceE, ptr %1
  %4 = tail call noundef float @_ZN2rw6Matrix11normalErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %0)
  %5 = load float, ptr %spec.store.select, align 4, !tbaa !5
  %6 = fcmp ole float %4, %5
  %7 = tail call noundef float @_ZN2rw6Matrix15orthogonalErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %0)
  %8 = getelementptr inbounds i8, ptr %spec.store.select, i64 4
  %9 = load float, ptr %8, align 4, !tbaa !10
  %10 = fcmp ole float %7, %9
  %or.cond = select i1 %6, i1 %10, i1 false
  br i1 %or.cond, label %11, label %17

11:                                               ; preds = %2
  %12 = tail call noundef float @_ZN2rw6Matrix13identityErrorEv(ptr noundef nonnull align 4 dereferenceable(64) %0)
  %13 = getelementptr inbounds i8, ptr %spec.store.select, i64 8
  %14 = load float, ptr %13, align 4, !tbaa !11
  %15 = fcmp ole float %12, %14
  %16 = select i1 %15, i32 131072, i32 0
  br label %17

17:                                               ; preds = %11, %2
  %masksel11 = phi i32 [ 0, %2 ], [ %16, %11 ]
  %18 = getelementptr inbounds i8, ptr %0, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = and i32 %19, -131076
  %masksel = zext i1 %6 to i32
  %21 = or disjoint i32 %20, %masksel
  %masksel10 = select i1 %10, i32 2, i32 0
  %22 = or disjoint i32 %21, %masksel10
  %23 = getelementptr inbounds i8, ptr %0, i64 12
  %.sink = or disjoint i32 %22, %masksel11
  store i32 %.sink, ptr %23, align 4, !tbaa !12
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix11normalErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix15orthogonalErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef float @_ZN2rw6Matrix13identityErrorEv(ptr noundef nonnull align 4 dereferenceable(64)) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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" }

!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, !7, i64 0}
!6 = !{!"_ZTSN2rw6Matrix9ToleranceE", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"float", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 8}
!12 = !{!13, !15, i64 12}
!13 = !{!"_ZTSN2rw6MatrixE", !14, i64 0, !15, i64 12, !14, i64 16, !15, i64 28, !14, i64 32, !15, i64 44, !14, i64 48, !15, i64 60}
!14 = !{!"_ZTSN2rw3V3dE", !7, i64 0, !7, i64 4, !7, i64 8}
!15 = !{!"int", !8, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass