; LLVM IR from Simple-XX/SimpleKernel :: src/libcxx/src/cxxabi.ll_ (cxa_finalize)

; 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.atexit_func_entry_t = type { ptr, ptr, ptr }

@__atexit_funcs = external dso_local global [128 x %struct.atexit_func_entry_t], align 16
@__atexit_func_count = external dso_local global i32, 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 @__cxa_finalize(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %5 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  store i32 %5, ptr %3, align 4, !tbaa !9
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %33, label %8

8:                                                ; preds = %1
  br label %9

9:                                                ; preds = %31, %8
  %10 = load i32, ptr %3, align 4, !tbaa !9
  %11 = add i32 %10, -1
  store i32 %11, ptr %3, align 4, !tbaa !9
  %12 = icmp ne i32 %10, 0
  br i1 %12, label %13, label %32

13:                                               ; preds = %9
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %31

20:                                               ; preds = %13
  %21 = load i32, ptr %3, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  call void %25(ptr noundef %30)
  br label %31

31:                                               ; preds = %20, %13
  br label %9, !llvm.loop !14

32:                                               ; preds = %9
  store i32 1, ptr %4, align 4
  br label %63

33:                                               ; preds = %1
  br label %34

34:                                               ; preds = %61, %33
  %35 = load i32, ptr %3, align 4, !tbaa !9
  %36 = add i32 %35, -1
  store i32 %36, ptr %3, align 4, !tbaa !9
  %37 = icmp ne i32 %35, 0
  br i1 %37, label %38, label %62

38:                                               ; preds = %34
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = icmp eq ptr %43, %44
  br i1 %45, label %46, label %61

46:                                               ; preds = %38
  %47 = load i32, ptr %3, align 4, !tbaa !9
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  %52 = load i32, ptr %3, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  %54 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !13
  call void %51(ptr noundef %56)
  %57 = load i32, ptr %3, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %59, i32 0, i32 0
  store ptr null, ptr %60, align 8, !tbaa !11
  br label %61

61:                                               ; preds = %46, %38
  br label %34, !llvm.loop !16

62:                                               ; preds = %34
  store i32 0, ptr %4, align 4
  br label %63

63:                                               ; preds = %62, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  %64 = load i32, ptr %4, align 4
  switch i32 %64, label %66 [
    i32 0, label %65
    i32 1, label %65
  ]

65:                                               ; preds = %63, %63
  ret void

66:                                               ; preds = %63
  unreachable
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS19atexit_func_entry_t", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!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"

%struct.atexit_func_entry_t = type { ptr, ptr, ptr }

@__atexit_funcs = external dso_local local_unnamed_addr global [128 x %struct.atexit_func_entry_t], align 16
@__atexit_func_count = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @__cxa_finalize(ptr noundef readnone %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @__atexit_func_count, align 4, !tbaa !5
  %.not = icmp eq ptr %0, null
  %.not1218 = icmp eq i32 %2, 0
  br i1 %.not, label %.preheader, label %.preheader15

.preheader15:                                     ; preds = %1
  br i1 %.not1218, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader15
  %3 = zext i32 %2 to i64
  br label %.lr.ph

.preheader:                                       ; preds = %1
  br i1 %.not1218, label %.loopexit, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %.preheader
  %4 = zext i32 %2 to i64
  br label %.lr.ph19

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %11
  %indvars.iv23 = phi i64 [ %4, %.lr.ph19.preheader ], [ %5, %11 ]
  %5 = add nsw i64 %indvars.iv23, -1
  %6 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %5
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %.not13 = icmp eq ptr %7, null
  br i1 %.not13, label %11, label %8

8:                                                ; preds = %.lr.ph19
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  tail call void %7(ptr noundef %10)
  br label %11

11:                                               ; preds = %8, %.lr.ph19
  %.not12.wide = icmp eq i64 %5, 0
  br i1 %.not12.wide, label %.loopexit, label %.lr.ph19, !llvm.loop !13

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %indvars.iv = phi i64 [ %3, %.lr.ph.preheader ], [ %12, %19 ]
  %12 = add nsw i64 %indvars.iv, -1
  %13 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %12
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = icmp eq ptr %14, %0
  br i1 %15, label %16, label %19

16:                                               ; preds = %.lr.ph
  %17 = getelementptr inbounds i8, ptr %13, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  tail call void %14(ptr noundef %18)
  store ptr null, ptr %13, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %16, %.lr.ph
  %.not14.wide = icmp eq i64 %12, 0
  br i1 %.not14.wide, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %19, %11, %.preheader15, %.preheader
  ret void
}

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


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