; LLVM IR from aap/librw :: src/geoplg.ll (ZN2rw10MeshHeader12setupIndicesEv)

; 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::Mesh" = type { ptr, i32, ptr }
%"struct.rw::MeshHeader" = type { i32, i16, i16, i32, i32 }

; 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 ptr @_ZN2rw10MeshHeader9getMeshesEv(ptr noundef nonnull align 4 dereferenceable(16)) #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 uwtable
define dso_local void @_ZN2rw10MeshHeader12setupIndicesEv(ptr noundef nonnull align 4 dereferenceable(16) %0) #2 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %8 = call noundef ptr @_ZN2rw10MeshHeader9getMeshesEv(ptr noundef nonnull align 4 dereferenceable(16) %7)
  store ptr %8, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds %"struct.rw::Mesh", ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %11, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  store i32 1, ptr %6, align 4
  br label %38

15:                                               ; preds = %1
  store i32 0, ptr %3, align 4, !tbaa !12
  br label %16

16:                                               ; preds = %34, %15
  %17 = load i32, ptr %3, align 4, !tbaa !12
  %18 = getelementptr inbounds %"struct.rw::MeshHeader", ptr %7, i32 0, i32 1
  %19 = load i16, ptr %18, align 4, !tbaa !13
  %20 = zext i16 %19 to i32
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %37

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.rw::Mesh", ptr %24, i32 0, i32 0
  store ptr %23, ptr %25, align 8, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %"struct.rw::Mesh", ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = zext i32 %28 to i64
  %31 = getelementptr inbounds i16, ptr %29, i64 %30
  store ptr %31, ptr %4, align 8, !tbaa !5
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.rw::Mesh", ptr %32, i32 1
  store ptr %33, ptr %5, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %22
  %35 = load i32, ptr %3, align 4, !tbaa !12
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !12
  br label %16, !llvm.loop !17

37:                                               ; preds = %16
  store i32 0, ptr %6, align 4
  br label %38

38:                                               ; preds = %37, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  %39 = load i32, ptr %6, align 4
  switch i32 %39, label %41 [
    i32 0, label %40
    i32 1, label %40
  ]

40:                                               ; preds = %38, %38
  ret void

41:                                               ; preds = %38
  unreachable
}

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 = { 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 #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, !6, i64 0}
!10 = !{!"_ZTSN2rw4MeshE", !6, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !15, i64 4}
!14 = !{!"_ZTSN2rw10MeshHeaderE", !11, i64 0, !15, i64 4, !15, i64 6, !11, i64 8, !11, i64 12}
!15 = !{!"short", !7, i64 0}
!16 = !{!10, !11, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"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"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN2rw10MeshHeader9getMeshesEv(ptr noundef nonnull align 4 dereferenceable(16)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN2rw10MeshHeader12setupIndicesEv(ptr noundef nonnull align 4 dereferenceable(16) %0) local_unnamed_addr #0 align 2 {
  %2 = tail call noundef ptr @_ZN2rw10MeshHeader9getMeshesEv(ptr noundef nonnull align 4 dereferenceable(16) %0)
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i16, ptr %5, align 4, !tbaa !11
  %7 = zext i16 %6 to i32
  %.not = icmp eq i16 %6, 0
  br i1 %.not, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i32 %7, 7
  %8 = icmp ult i16 %6, 8
  br i1 %8, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %7, 65528
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.014 = phi ptr [ %2, %.lr.ph.preheader.new ], [ %48, %.lr.ph ]
  %.0913 = phi ptr [ %3, %.lr.ph.preheader.new ], [ %47, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  store ptr %.0913, ptr %.014, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.014, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = zext i32 %10 to i64
  %12 = getelementptr inbounds i16, ptr %.0913, i64 %11
  %13 = getelementptr inbounds i8, ptr %.014, i64 24
  store ptr %12, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.014, i64 32
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds i16, ptr %12, i64 %16
  %18 = getelementptr inbounds i8, ptr %.014, i64 48
  store ptr %17, ptr %18, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.014, i64 56
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds i16, ptr %17, i64 %21
  %23 = getelementptr inbounds i8, ptr %.014, i64 72
  store ptr %22, ptr %23, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.014, i64 80
  %25 = load i32, ptr %24, align 8, !tbaa !14
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds i16, ptr %22, i64 %26
  %28 = getelementptr inbounds i8, ptr %.014, i64 96
  store ptr %27, ptr %28, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %.014, i64 104
  %30 = load i32, ptr %29, align 8, !tbaa !14
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds i16, ptr %27, i64 %31
  %33 = getelementptr inbounds i8, ptr %.014, i64 120
  store ptr %32, ptr %33, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %.014, i64 128
  %35 = load i32, ptr %34, align 8, !tbaa !14
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i16, ptr %32, i64 %36
  %38 = getelementptr inbounds i8, ptr %.014, i64 144
  store ptr %37, ptr %38, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %.014, i64 152
  %40 = load i32, ptr %39, align 8, !tbaa !14
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds i16, ptr %37, i64 %41
  %43 = getelementptr inbounds i8, ptr %.014, i64 168
  store ptr %42, ptr %43, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %.014, i64 176
  %45 = load i32, ptr %44, align 8, !tbaa !14
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds i16, ptr %42, i64 %46
  %48 = getelementptr inbounds i8, ptr %.014, i64 192
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !15

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.014.unr = phi ptr [ %2, %.lr.ph.preheader ], [ %48, %.lr.ph ]
  %.0913.unr = phi ptr [ %3, %.lr.ph.preheader ], [ %47, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %.014.epil = phi ptr [ %53, %.lr.ph.epil ], [ %.014.unr, %.loopexit.loopexit.unr-lcssa ]
  %.0913.epil = phi ptr [ %52, %.lr.ph.epil ], [ %.0913.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  store ptr %.0913.epil, ptr %.014.epil, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %.014.epil, i64 8
  %50 = load i32, ptr %49, align 8, !tbaa !14
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds i16, ptr %.0913.epil, i64 %51
  %53 = getelementptr inbounds i8, ptr %.014.epil, i64 24
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.lr.ph.epil, !llvm.loop !17

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader, %1
  ret void
}

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

!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 = !{!"_ZTSN2rw4MeshE", !7, i64 0, !10, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !13, i64 4}
!12 = !{!"_ZTSN2rw10MeshHeaderE", !10, i64 0, !13, i64 4, !13, i64 6, !10, i64 8, !10, i64 12}
!13 = !{!"short", !8, i64 0}
!14 = !{!6, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass