; LLVM IR from heroseh/cgm :: cgm (cgm_3d_ray_vs_mesh)

; 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.CgmTriangle3d = type { %struct.anon.9 }
%struct.anon.9 = type { %union.CgmVec3, %union.CgmVec3, %union.CgmVec3 }
%union.CgmVec3 = type { %struct.anon.0 }
%struct.anon.0 = type { float, float, float }
%struct.CgmMesh = type { ptr, %union.CgmVec3, i32 }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fabs.f32(float) #2

; Function Attrs: nounwind uwtable
declare dso_local void @CgmTriangle3d_offset(ptr noundef, <2 x float>, float) #3

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @cgm_3d_ray_vs_triangle(ptr noundef, float noundef, ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext) #3

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @cgm_3d_ray_vs_mesh(ptr noundef %0, float noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #3 {
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca float, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca %union.CgmTriangle3d, align 4
  %13 = alloca %union.CgmVec3, align 4
  %14 = alloca float, align 4
  %15 = alloca %union.CgmVec3, align 4
  %16 = alloca float, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca { <2 x float>, float }, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store float %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 36, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 12, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store float 0x7FF0000000000000, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 12, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  store i32 0, ptr %17, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %59, %5
  %21 = load i32, ptr %17, align 4, !tbaa !11
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.CgmMesh, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %25 = icmp ult i32 %21, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  store i32 2, ptr %18, align 4
  br label %62

27:                                               ; preds = %20
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.CgmMesh, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load i32, ptr %17, align 4, !tbaa !11
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds %union.CgmTriangle3d, ptr %30, i64 %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %12, ptr align 4 %33, i64 36, i1 false), !tbaa.struct !16
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.CgmMesh, ptr %34, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %19, ptr align 8 %35, i64 12, i1 false)
  %36 = getelementptr inbounds { <2 x float>, float }, ptr %19, i32 0, i32 0
  %37 = load <2 x float>, ptr %36, align 8
  %38 = getelementptr inbounds { <2 x float>, float }, ptr %19, i32 0, i32 1
  %39 = load float, ptr %38, align 8
  call void @CgmTriangle3d_offset(ptr noundef %12, <2 x float> %37, float %39)
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load float, ptr %8, align 4, !tbaa !9
  %42 = call zeroext i8 @cgm_3d_ray_vs_triangle(ptr noundef %40, float noundef %41, ptr noundef %12, ptr noundef %15, ptr noundef %16, i8 noundef zeroext 1)
  %43 = icmp ne i8 %42, 0
  br i1 %43, label %44, label %58

44:                                               ; preds = %27
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %51, label %47

47:                                               ; preds = %44
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = icmp ne ptr %48, null
  br i1 %49, label %51, label %50

50:                                               ; preds = %47
  store i8 1, ptr %6, align 1
  store i32 1, ptr %18, align 4
  br label %62

51:                                               ; preds = %47, %44
  %52 = load float, ptr %16, align 4, !tbaa !9
  %53 = load float, ptr %14, align 4, !tbaa !9
  %54 = fcmp olt float %52, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %51
  %56 = load float, ptr %16, align 4, !tbaa !9
  store float %56, ptr %14, align 4, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %15, i64 12, i1 false), !tbaa.struct !18
  br label %57

57:                                               ; preds = %55, %51
  br label %58

58:                                               ; preds = %57, %27
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %17, align 4, !tbaa !11
  %61 = add i32 %60, 1
  store i32 %61, ptr %17, align 4, !tbaa !11
  br label %20, !llvm.loop !19

62:                                               ; preds = %50, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  %63 = load i32, ptr %18, align 4
  switch i32 %63, label %86 [
    i32 2, label %64
  ]

64:                                               ; preds = %62
  %65 = load float, ptr %14, align 4, !tbaa !9
  %66 = call float @llvm.fabs.f32(float %65) #5
  %67 = fcmp oeq float %66, 0x7FF0000000000000
  %68 = bitcast float %65 to i32
  %69 = icmp slt i32 %68, 0
  %70 = select i1 %69, i32 -1, i32 1
  %71 = select i1 %67, i32 %70, i32 0
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %64
  store i8 0, ptr %6, align 1
  store i32 1, ptr %18, align 4
  br label %86

74:                                               ; preds = %64
  %75 = load ptr, ptr %10, align 8, !tbaa !5
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %79

77:                                               ; preds = %74
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %78, ptr align 4 %13, i64 12, i1 false), !tbaa.struct !18
  br label %79

79:                                               ; preds = %77, %74
  %80 = load ptr, ptr %11, align 8, !tbaa !5
  %81 = icmp ne ptr %80, null
  br i1 %81, label %82, label %85

82:                                               ; preds = %79
  %83 = load float, ptr %16, align 4, !tbaa !9
  %84 = load ptr, ptr %11, align 8, !tbaa !5
  store float %83, ptr %84, align 4, !tbaa !9
  br label %85

85:                                               ; preds = %82, %79
  store i8 1, ptr %6, align 1
  store i32 1, ptr %18, align 4
  br label %86

86:                                               ; preds = %85, %73, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 12, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 12, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %12) #4
  %87 = load i8, ptr %6, align 1
  ret i8 %87
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind uwtable "min-legal-vector-width"="64" "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 #4 = { nounwind }
attributes #5 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 20}
!14 = !{!"CgmMesh", !6, i64 0, !7, i64 8, !12, i64 20}
!15 = !{!14, !6, i64 0}
!16 = !{i64 0, i64 36, !17}
!17 = !{!7, !7, i64 0}
!18 = !{i64 0, i64 12, !17}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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

%union.CgmTriangle3d = type { %struct.anon.9 }
%struct.anon.9 = type { %union.CgmVec3, %union.CgmVec3, %union.CgmVec3 }
%union.CgmVec3 = type { %struct.anon.0 }
%struct.anon.0 = type { float, float, float }

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fabs.f32(float) #2

; Function Attrs: nounwind uwtable
declare dso_local void @CgmTriangle3d_offset(ptr noundef, <2 x float>, float) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @cgm_3d_ray_vs_triangle(ptr noundef, float noundef, ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @cgm_3d_ray_vs_mesh(ptr noundef %0, float noundef %1, ptr nocapture noundef readonly %2, ptr noundef writeonly %3, ptr noundef writeonly %4) local_unnamed_addr #3 {
  %6 = alloca %union.CgmTriangle3d, align 4
  %.sroa.06 = alloca %struct.anon.0, align 8
  %7 = alloca %union.CgmVec3, align 4
  %8 = alloca float, align 4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %.sroa.06)
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #4
  %9 = getelementptr inbounds i8, ptr %2, i64 20
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not2529.not = icmp eq i32 %10, 0
  br i1 %.not2529.not, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %11 = getelementptr inbounds i8, ptr %2, i64 8
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %2, i64 16
  %12 = icmp ne ptr %3, null
  %13 = icmp ne ptr %4, null
  %or.cond = or i1 %12, %13
  br i1 %or.cond, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %21
  %indvars.iv34 = phi i64 [ %indvars.iv.next35, %21 ], [ 0, %.lr.ph ]
  %.01931.us = phi float [ %.120.us, %21 ], [ 0x7FF0000000000000, %.lr.ph ]
  %14 = load ptr, ptr %2, align 8, !tbaa !11
  %15 = getelementptr inbounds %union.CgmTriangle3d, ptr %14, i64 %indvars.iv34
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %6, ptr noundef nonnull align 4 dereferenceable(36) %15, i64 36, i1 false), !tbaa.struct !12
  %.sroa.0.0.copyload.us = load <2 x float>, ptr %11, align 8
  %.sroa.2.0.copyload.us = load float, ptr %.sroa.2.0..sroa_idx, align 8
  call void @CgmTriangle3d_offset(ptr noundef nonnull %6, <2 x float> %.sroa.0.0.copyload.us, float %.sroa.2.0.copyload.us)
  %16 = call zeroext i8 @cgm_3d_ray_vs_triangle(ptr noundef %0, float noundef %1, ptr noundef nonnull %6, ptr noundef nonnull %7, ptr noundef nonnull %8, i8 noundef zeroext 1)
  %.not.us = icmp eq i8 %16, 0
  br i1 %.not.us, label %21, label %17

17:                                               ; preds = %.lr.ph.split.us
  %18 = load float, ptr %8, align 4, !tbaa !14
  %19 = fcmp olt float %18, %.01931.us
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(12) %.sroa.06, ptr noundef nonnull align 4 dereferenceable(12) %7, i64 12, i1 false), !tbaa.struct !16
  br label %21

21:                                               ; preds = %20, %17, %.lr.ph.split.us
  %.120.us = phi float [ %18, %20 ], [ %.01931.us, %17 ], [ %.01931.us, %.lr.ph.split.us ]
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = zext i32 %22 to i64
  %.not25.us = icmp ult i64 %indvars.iv.next35, %23
  br i1 %.not25.us, label %.lr.ph.split.us, label %.critedge, !llvm.loop !17

.lr.ph.split:                                     ; preds = %.lr.ph, %27
  %indvars.iv = phi i64 [ %indvars.iv.next, %27 ], [ 0, %.lr.ph ]
  %24 = load ptr, ptr %2, align 8, !tbaa !11
  %25 = getelementptr inbounds %union.CgmTriangle3d, ptr %24, i64 %indvars.iv
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %6, ptr noundef nonnull align 4 dereferenceable(36) %25, i64 36, i1 false), !tbaa.struct !12
  %.sroa.0.0.copyload = load <2 x float>, ptr %11, align 8
  %.sroa.2.0.copyload = load float, ptr %.sroa.2.0..sroa_idx, align 8
  call void @CgmTriangle3d_offset(ptr noundef nonnull %6, <2 x float> %.sroa.0.0.copyload, float %.sroa.2.0.copyload)
  %26 = call zeroext i8 @cgm_3d_ray_vs_triangle(ptr noundef %0, float noundef %1, ptr noundef nonnull %6, ptr noundef nonnull %7, ptr noundef nonnull %8, i8 noundef zeroext 1)
  %.not = icmp eq i8 %26, 0
  br i1 %.not, label %27, label %.loopexit

27:                                               ; preds = %.lr.ph.split
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = zext i32 %28 to i64
  %.not25 = icmp ult i64 %indvars.iv.next, %29
  br i1 %.not25, label %.lr.ph.split, label %.critedge, !llvm.loop !17

.critedge:                                        ; preds = %27, %21, %5
  %.019.lcssa = phi float [ 0x7FF0000000000000, %5 ], [ %.120.us, %21 ], [ 0x7FF0000000000000, %27 ]
  %30 = call float @llvm.fabs.f32(float %.019.lcssa) #5
  %31 = fcmp oeq float %30, 0x7FF0000000000000
  br i1 %31, label %.loopexit, label %32

32:                                               ; preds = %.critedge
  %.not26 = icmp eq ptr %3, null
  br i1 %.not26, label %34, label %33

33:                                               ; preds = %32
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) %3, ptr noundef nonnull align 8 dereferenceable(12) %.sroa.06, i64 12, i1 false), !tbaa.struct !16
  br label %34

34:                                               ; preds = %33, %32
  %.not27 = icmp eq ptr %4, null
  br i1 %.not27, label %.loopexit, label %35

35:                                               ; preds = %34
  %36 = load float, ptr %8, align 4, !tbaa !14
  store float %36, ptr %4, align 4, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.split, %34, %35, %.critedge
  %.1 = phi i8 [ 0, %.critedge ], [ 1, %35 ], [ 1, %34 ], [ 1, %.lr.ph.split ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %.sroa.06)
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %6) #4
  ret i8 %.1
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind uwtable "min-legal-vector-width"="64" "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 #4 = { nounwind }
attributes #5 = { 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, !10, i64 20}
!6 = !{!"CgmMesh", !7, i64 0, !8, i64 8, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{i64 0, i64 36, !13}
!13 = !{!8, !8, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"float", !8, i64 0}
!16 = !{i64 0, i64 12, !13}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass