; LLVM IR from jingweizhanghuai/Morn :: src/util (morn_memory)

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

@morn_alloc_free_info_num = external hidden global i32, align 4
@morn_alloc_free_info_list = external hidden global [512 x ptr], align 16

; 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: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @m_Free(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @endMAF() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %83, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr @morn_alloc_free_info_num, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  store i32 2, ptr %2, align 4
  br label %86

10:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [512 x ptr], ptr @morn_alloc_free_info_list, i64 0, i64 %12
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  store ptr %14, ptr %3, align 8, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %10
  store i32 1, ptr %2, align 4
  br label %80

18:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %75, %18
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = icmp slt i32 %20, 32
  br i1 %21, label %23, label %22

22:                                               ; preds = %19
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %78

23:                                               ; preds = %19
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds %struct.MAFInfo, ptr %24, i64 %26
  %28 = getelementptr inbounds %struct.MAFInfo, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = load ptr, ptr %3, align 8, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.MAFInfo, ptr %30, i64 %32
  %34 = getelementptr inbounds %struct.MAFInfo, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = icmp ne ptr %29, %35
  br i1 %36, label %37, label %44

37:                                               ; preds = %23
  %38 = load ptr, ptr %3, align 8, !tbaa !9
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %struct.MAFInfo, ptr %38, i64 %40
  %42 = getelementptr inbounds %struct.MAFInfo, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  call void @m_Free(ptr noundef %43)
  br label %44

44:                                               ; preds = %37, %23
  %45 = load ptr, ptr %3, align 8, !tbaa !9
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.MAFInfo, ptr %45, i64 %47
  %49 = getelementptr inbounds %struct.MAFInfo, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !13
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %59

52:                                               ; preds = %44
  %53 = load ptr, ptr %3, align 8, !tbaa !9
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds %struct.MAFInfo, ptr %53, i64 %55
  %57 = getelementptr inbounds %struct.MAFInfo, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  call void @m_Free(ptr noundef %58)
  br label %59

59:                                               ; preds = %52, %44
  %60 = load ptr, ptr %3, align 8, !tbaa !9
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds %struct.MAFInfo, ptr %60, i64 %62
  %64 = getelementptr inbounds %struct.MAFInfo, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %74

67:                                               ; preds = %59
  %68 = load ptr, ptr %3, align 8, !tbaa !9
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds %struct.MAFInfo, ptr %68, i64 %70
  %72 = getelementptr inbounds %struct.MAFInfo, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  call void @free(ptr noundef %73) #3
  br label %74

74:                                               ; preds = %67, %59
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %4, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !15

78:                                               ; preds = %22
  %79 = load ptr, ptr %3, align 8, !tbaa !9
  call void @free(ptr noundef %79) #3
  store i32 0, ptr %2, align 4
  br label %80

80:                                               ; preds = %78, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %81 = load i32, ptr %2, align 4
  switch i32 %81, label %86 [
    i32 0, label %82
  ]

82:                                               ; preds = %80
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %1, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !17

86:                                               ; preds = %80, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %87 = load i32, ptr %2, align 4
  switch i32 %87, label %90 [
    i32 2, label %88
    i32 1, label %89
  ]

88:                                               ; preds = %86
  store i32 0, ptr @morn_alloc_free_info_num, align 4, !tbaa !5
  br label %89

89:                                               ; preds = %88, %86
  ret void

90:                                               ; preds = %86
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"MAFInfo", !10, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 28, !6, i64 32}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@morn_alloc_free_info_num = external hidden local_unnamed_addr global i32, align 4
@morn_alloc_free_info_list = external hidden local_unnamed_addr global [512 x ptr], align 16

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @m_Free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @endMAF() local_unnamed_addr #1 {
  %1 = load i32, ptr @morn_alloc_free_info_num, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %18
  %indvars.iv37 = phi i64 [ %indvars.iv.next38, %18 ], [ 0, %0 ]
  %3 = getelementptr inbounds [512 x ptr], ptr @morn_alloc_free_info_list, i64 0, i64 %indvars.iv37
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %.not30 = icmp eq ptr %4, null
  br i1 %.not30, label %.critedge, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %.lr.ph ]
  %5 = getelementptr inbounds %struct.MAFInfo, ptr %4, i64 %indvars.iv
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %5, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  %.not = icmp eq ptr %7, %9
  br i1 %.not, label %11, label %10

10:                                               ; preds = %.preheader
  tail call void @m_Free(ptr noundef %7)
  %.pr = load ptr, ptr %8, align 8, !tbaa !13
  br label %11

11:                                               ; preds = %10, %.preheader
  %12 = phi ptr [ %.pr, %10 ], [ %9, %.preheader ]
  %.not28 = icmp eq ptr %12, null
  br i1 %.not28, label %14, label %13

13:                                               ; preds = %11
  tail call void @m_Free(ptr noundef nonnull %12)
  br label %14

14:                                               ; preds = %13, %11
  %15 = load ptr, ptr %5, align 8, !tbaa !14
  %.not29 = icmp eq ptr %15, null
  br i1 %.not29, label %17, label %16

16:                                               ; preds = %14
  tail call void @free(ptr noundef nonnull %15) #2
  br label %17

17:                                               ; preds = %14, %16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 32
  br i1 %exitcond.not, label %18, label %.preheader, !llvm.loop !15

18:                                               ; preds = %17
  tail call void @free(ptr noundef nonnull %4) #2
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %19 = load i32, ptr @morn_alloc_free_info_num, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = icmp slt i64 %indvars.iv.next38, %20
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %18, %0
  store i32 0, ptr @morn_alloc_free_info_num, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.lr.ph, %._crit_edge
  ret void
}

attributes #0 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"MAFInfo", !10, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 28, !6, i64 32}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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