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

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

@.str.19 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [76 x i8], align 1

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw10malloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) #1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw8Material6addRefEv(ptr noundef nonnull align 8 dereferenceable(40)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN2rw12MaterialList14appendMaterialEPNS_8MaterialE(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1) #3 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %10 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %14 = icmp sge i32 %11, %13
  br i1 %14, label %15, label %43

15:                                               ; preds = %2
  %16 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 2
  %17 = load i32, ptr %16, align 4, !tbaa !12
  %18 = add nsw i32 %17, 20
  store i32 %18, ptr %7, align 4, !tbaa !13
  %19 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %29

22:                                               ; preds = %15
  %23 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = load i32, ptr %7, align 4, !tbaa !13
  %26 = sext i32 %25 to i64
  %27 = mul i64 %26, 8
  %28 = call noundef ptr @_ZN2rw11realloc_LOCEPvmjPKc(ptr noundef %24, i64 noundef %27, i32 noundef 196615, ptr noundef @.str.19)
  store ptr %28, ptr %6, align 8, !tbaa !5
  br label %34

29:                                               ; preds = %15
  %30 = load i32, ptr %7, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = mul i64 %31, 8
  %33 = call noundef ptr @_ZN2rw10malloc_LOCEmjPKc(i64 noundef %32, i32 noundef 196615, ptr noundef @.str.20)
  store ptr %33, ptr %6, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %29, %22
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = icmp eq ptr %35, null
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

38:                                               ; preds = %34
  %39 = load i32, ptr %7, align 4, !tbaa !13
  %40 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 2
  store i32 %39, ptr %40, align 4, !tbaa !12
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 0
  store ptr %41, ptr %42, align 8, !tbaa !14
  br label %43

43:                                               ; preds = %38, %2
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 1
  %48 = load i32, ptr %47, align 8, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %47, align 8, !tbaa !9
  %50 = sext i32 %48 to i64
  %51 = getelementptr inbounds ptr, ptr %46, i64 %50
  store ptr %44, ptr %51, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN2rw8Material6addRefEv(ptr noundef nonnull align 8 dereferenceable(40) %52)
  %53 = getelementptr inbounds %"struct.rw::MaterialList", ptr %9, i32 0, i32 1
  %54 = load i32, ptr %53, align 8, !tbaa !9
  %55 = sub nsw i32 %54, 1
  store i32 %55, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %56

56:                                               ; preds = %43, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %57 = load i32, ptr %3, align 4
  ret i32 %57
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw11realloc_LOCEPvmjPKc(ptr noundef, i64 noundef, i32 noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 #4 = { 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 = !{!10, !11, i64 12}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, 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"

@.str.19 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [76 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw10malloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw8Material6addRefEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -2147483648, 2147483647) i32 @_ZN2rw12MaterialList14appendMaterialEPNS_8MaterialE(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %.not = icmp slt i32 %4, %6
  %.pre = load ptr, ptr %0, align 8, !tbaa !12
  br i1 %.not, label %18, label %7

7:                                                ; preds = %2
  %8 = add nsw i32 %6, 20
  %.not11 = icmp eq ptr %.pre, null
  %9 = sext i32 %8 to i64
  %10 = shl nsw i64 %9, 3
  br i1 %.not11, label %13, label %11

11:                                               ; preds = %7
  %12 = tail call noundef ptr @_ZN2rw11realloc_LOCEPvmjPKc(ptr noundef nonnull %.pre, i64 noundef %10, i32 noundef 196615, ptr noundef nonnull @.str.19)
  br label %15

13:                                               ; preds = %7
  %14 = tail call noundef ptr @_ZN2rw10malloc_LOCEmjPKc(i64 noundef %10, i32 noundef 196615, ptr noundef nonnull @.str.20)
  br label %15

15:                                               ; preds = %13, %11
  %.08 = phi ptr [ %12, %11 ], [ %14, %13 ]
  %16 = icmp eq ptr %.08, null
  br i1 %16, label %26, label %17

17:                                               ; preds = %15
  store i32 %8, ptr %5, align 4, !tbaa !11
  store ptr %.08, ptr %0, align 8, !tbaa !12
  %.pre12 = load i32, ptr %3, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %17, %2
  %19 = phi i32 [ %.pre12, %17 ], [ %4, %2 ]
  %20 = phi ptr [ %.08, %17 ], [ %.pre, %2 ]
  %21 = add nsw i32 %19, 1
  store i32 %21, ptr %3, align 8, !tbaa !5
  %22 = sext i32 %19 to i64
  %23 = getelementptr inbounds ptr, ptr %20, i64 %22
  store ptr %1, ptr %23, align 8, !tbaa !13
  tail call void @_ZN2rw8Material6addRefEv(ptr noundef nonnull align 8 dereferenceable(40) %1)
  %24 = load i32, ptr %3, align 8, !tbaa !5
  %25 = add nsw i32 %24, -1
  br label %26

26:                                               ; preds = %15, %18
  %.0 = phi i32 [ %25, %18 ], [ -1, %15 ]
  ret i32 %.0
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw11realloc_LOCEPvmjPKc(ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

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

!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 = !{!6, !10, i64 12}
!12 = !{!6, !7, i64 0}
!13 = !{!7, !7, i64 0}


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