; LLVM IR from aap/librw :: src/geometry.ll (ZN2rw12MaterialList13streamGetSizeEv)

; 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::MaterialList" = type { ptr, 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: 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 noundef i32 @_ZN2rw12MaterialList13streamGetSizeEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %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) #2
  %8 = getelementptr inbounds %"struct.rw::MaterialList", ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = mul nsw i32 %9, 4
  %11 = add nsw i32 16, %10
  store i32 %11, ptr %3, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !12
  br label %12

12:                                               ; preds = %58, %1
  %13 = load i32, ptr %4, align 4, !tbaa !12
  %14 = getelementptr inbounds %"struct.rw::MaterialList", ptr %7, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %12
  store i32 2, ptr %5, align 4
  br label %61

18:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %19 = load i32, ptr %4, align 4, !tbaa !12
  %20 = sub nsw i32 %19, 1
  store i32 %20, ptr %6, align 4, !tbaa !12
  br label %21

21:                                               ; preds = %41, %18
  %22 = load i32, ptr %6, align 4, !tbaa !12
  %23 = icmp sge i32 %22, 0
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  store i32 5, ptr %5, align 4
  br label %44

25:                                               ; preds = %21
  %26 = getelementptr inbounds %"struct.rw::MaterialList", ptr %7, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = load i32, ptr %4, align 4, !tbaa !12
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds ptr, ptr %27, i64 %29
  %31 = load ptr, ptr %30, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.rw::MaterialList", ptr %7, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load i32, ptr %6, align 4, !tbaa !12
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds ptr, ptr %33, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = icmp eq ptr %31, %37
  br i1 %38, label %39, label %40

39:                                               ; preds = %25
  store i32 8, ptr %5, align 4
  br label %44

40:                                               ; preds = %25
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %6, align 4, !tbaa !12
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %6, align 4, !tbaa !12
  br label %21, !llvm.loop !14

44:                                               ; preds = %39, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %45 = load i32, ptr %5, align 4
  switch i32 %45, label %61 [
    i32 5, label %46
    i32 8, label %57
  ]

46:                                               ; preds = %44
  %47 = getelementptr inbounds %"struct.rw::MaterialList", ptr %7, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = load i32, ptr %4, align 4, !tbaa !12
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds ptr, ptr %48, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  %53 = call noundef i32 @_ZN2rw8Material13streamGetSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %52)
  %54 = add i32 12, %53
  %55 = load i32, ptr %3, align 4, !tbaa !12
  %56 = add i32 %55, %54
  store i32 %56, ptr %3, align 4, !tbaa !12
  br label %57

57:                                               ; preds = %46, %44
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %4, align 4, !tbaa !12
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %4, align 4, !tbaa !12
  br label %12, !llvm.loop !16

61:                                               ; preds = %44, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %3, align 4, !tbaa !12
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %63
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN2rw8Material13streamGetSizeEv(ptr noundef nonnull align 8 dereferenceable(40)) #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 8}
!10 = !{!"_ZTSN2rw12MaterialListE", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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 uwtable
define dso_local noundef i32 @_ZN2rw12MaterialList13streamGetSizeEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = shl nsw i32 %3, 2
  %5 = add nsw i32 %4, 16
  %6 = icmp sgt i32 %3, 0
  br i1 %6, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %1, %.loopexit
  %7 = phi i32 [ %23, %.loopexit ], [ %3, %1 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 0, %1 ]
  %.01315 = phi i32 [ %.1, %.loopexit ], [ %5, %1 ]
  %8 = load ptr, ptr %0, align 8
  %9 = getelementptr inbounds ptr, ptr %8, i64 %indvars.iv
  br label %10

10:                                               ; preds = %.preheader, %12
  %indvars.iv17 = phi i64 [ %indvars.iv, %.preheader ], [ %13, %12 ]
  %11 = icmp sgt i64 %indvars.iv17, 0
  br i1 %11, label %12, label %18

12:                                               ; preds = %10
  %13 = add nsw i64 %indvars.iv17, -1
  %14 = load ptr, ptr %9, align 8, !tbaa !11
  %15 = getelementptr inbounds ptr, ptr %8, i64 %13
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = icmp eq ptr %14, %16
  br i1 %17, label %.loopexit, label %10, !llvm.loop !12

18:                                               ; preds = %10
  %19 = load ptr, ptr %9, align 8, !tbaa !11
  %20 = tail call noundef i32 @_ZN2rw8Material13streamGetSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %19)
  %21 = add i32 %.01315, 12
  %22 = add i32 %21, %20
  %.pre = load i32, ptr %2, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %12, %18
  %23 = phi i32 [ %.pre, %18 ], [ %7, %12 ]
  %.1 = phi i32 [ %22, %18 ], [ %.01315, %12 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = sext i32 %23 to i64
  %25 = icmp slt i64 %indvars.iv.next, %24
  br i1 %25, label %.preheader, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.loopexit, %1
  %.013.lcssa = phi i32 [ %5, %1 ], [ %.1, %.loopexit ]
  ret i32 %.013.lcssa
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN2rw8Material13streamGetSizeEv(ptr noundef nonnull align 8 dereferenceable(40)) 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, !10, i64 8}
!6 = !{!"_ZTSN2rw12MaterialListE", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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