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

; 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.HandleList = type { %struct.MList, i32, i32, i32, i32 }
%struct.MList = type { i32, ptr }
%struct.MHandle = type { i32, i32, ptr, ptr }
%struct.HandleMemory = type { i32, i32, i32, i32, ptr, i32, i32, ptr, i32 }

@.str = external hidden unnamed_addr constant [84 x i8], align 1
@__FUNCTION__.MemoryDefrag = external hidden unnamed_addr constant [13 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [22 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

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr 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: nounwind uwtable
declare dso_local void @mMemoryBlockRelease(ptr noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local void @MemoryDefrag(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.HandleList, ptr %7, i64 -1
  %9 = getelementptr inbounds %struct.HandleList, ptr %8, i32 0, i32 0
  %10 = getelementptr inbounds %struct.MList, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds ptr, ptr %11, i64 0
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.MHandle, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  store ptr %15, ptr %3, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.HandleMemory, ptr %17, i32 0, i32 6
  %19 = load i32, ptr %18, align 4, !tbaa !15
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.MList, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !17
  %23 = icmp ne i32 %19, %22
  br i1 %23, label %29, label %24

24:                                               ; preds = %16
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.HandleMemory, ptr %25, i32 0, i32 8
  %27 = load i32, ptr %26, align 8, !tbaa !18
  %28 = icmp slt i32 %27, 0
  br label %29

29:                                               ; preds = %24, %16
  %30 = phi i1 [ true, %16 ], [ %28, %24 ]
  %31 = zext i1 %30 to i32
  store i32 %31, ptr %4, align 4, !tbaa !19
  %32 = load i32, ptr %4, align 4, !tbaa !19
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %29
  %35 = load i32, ptr %4, align 4, !tbaa !19
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %35, i32 noundef -1, ptr noundef @.str, i32 noundef 635, ptr noundef @__FUNCTION__.MemoryDefrag, ptr noundef @.str.6)
  br label %36

36:                                               ; preds = %34, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %37

37:                                               ; preds = %36
  br label %38

38:                                               ; preds = %37
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.HandleMemory, ptr %39, i32 0, i32 8
  store i32 0, ptr %40, align 8, !tbaa !18
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.HandleMemory, ptr %41, i32 0, i32 7
  %43 = load ptr, ptr %42, align 8, !tbaa !20
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.HandleMemory, ptr %44, i32 0, i32 3
  %46 = load i32, ptr %45, align 4, !tbaa !21
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %43, i64 %47
  store i32 1, ptr %48, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !19
  br label %49

49:                                               ; preds = %100, %38
  %50 = load i32, ptr %6, align 4, !tbaa !19
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MList, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !17
  %54 = icmp slt i32 %50, %53
  br i1 %54, label %56, label %55

55:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %103

56:                                               ; preds = %49
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.HandleMemory, ptr %57, i32 0, i32 7
  %59 = load ptr, ptr %58, align 8, !tbaa !20
  %60 = load i32, ptr %6, align 4, !tbaa !19
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !19
  %64 = icmp eq i32 %63, 1
  br i1 %64, label %65, label %91

65:                                               ; preds = %56
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.MList, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !22
  %69 = load i32, ptr %6, align 4, !tbaa !19
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds ptr, ptr %68, i64 %70
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.MList, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !22
  %76 = load i32, ptr %5, align 4, !tbaa !19
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds ptr, ptr %75, i64 %77
  store ptr %72, ptr %78, align 8, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !19
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.HandleMemory, ptr %80, i32 0, i32 3
  %82 = load i32, ptr %81, align 4, !tbaa !21
  %83 = icmp eq i32 %79, %82
  br i1 %83, label %84, label %88

84:                                               ; preds = %65
  %85 = load i32, ptr %5, align 4, !tbaa !19
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.HandleMemory, ptr %86, i32 0, i32 3
  store i32 %85, ptr %87, align 4, !tbaa !21
  br label %88

88:                                               ; preds = %84, %65
  %89 = load i32, ptr %5, align 4, !tbaa !19
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %5, align 4, !tbaa !19
  br label %99

91:                                               ; preds = %56
  %92 = load ptr, ptr %2, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.MList, ptr %92, i32 0, i32 1
  %94 = load ptr, ptr %93, align 8, !tbaa !22
  %95 = load i32, ptr %6, align 4, !tbaa !19
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds ptr, ptr %94, i64 %96
  %98 = load ptr, ptr %97, align 8, !tbaa !5
  call void @mMemoryBlockRelease(ptr noundef %98)
  br label %99

99:                                               ; preds = %91, %88
  br label %100

100:                                              ; preds = %99
  %101 = load i32, ptr %6, align 4, !tbaa !19
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %6, align 4, !tbaa !19
  br label %49, !llvm.loop !23

103:                                              ; preds = %55
  %104 = load i32, ptr %5, align 4, !tbaa !19
  %105 = load ptr, ptr %2, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.MList, ptr %105, i32 0, i32 0
  store i32 %104, ptr %106, align 8, !tbaa !17
  %107 = load ptr, ptr %2, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.MList, ptr %107, i32 0, i32 1
  %109 = load ptr, ptr %108, align 8, !tbaa !22
  %110 = load i32, ptr %5, align 4, !tbaa !19
  %111 = sext i32 %110 to i64
  %112 = getelementptr inbounds ptr, ptr %109, i64 %111
  %113 = load ptr, ptr %3, align 8, !tbaa !5
  %114 = getelementptr inbounds %struct.HandleMemory, ptr %113, i32 0, i32 2
  %115 = load i32, ptr %114, align 8, !tbaa !25
  %116 = load i32, ptr %5, align 4, !tbaa !19
  %117 = sub nsw i32 %115, %116
  %118 = sext i32 %117 to i64
  %119 = mul i64 %118, 8
  call void @llvm.memset.p0.i64(ptr align 8 %112, i8 0, i64 %119, i1 false)
  %120 = load ptr, ptr %3, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.HandleMemory, ptr %120, i32 0, i32 7
  %122 = load ptr, ptr %121, align 8, !tbaa !20
  %123 = load i32, ptr %5, align 4, !tbaa !19
  %124 = sext i32 %123 to i64
  %125 = mul i64 %124, 4
  call void @llvm.memset.p0.i64(ptr align 4 %122, i8 0, i64 %125, i1 false)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"HandleList", !11, i64 0, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28}
!11 = !{!"MList", !12, i64 0, !6, i64 8}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"MHandle", !12, i64 0, !12, i64 4, !6, i64 8, !6, i64 16}
!15 = !{!16, !12, i64 28}
!16 = !{!"HandleMemory", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !6, i64 16, !12, i64 24, !12, i64 28, !6, i64 32, !12, i64 40}
!17 = !{!11, !12, i64 0}
!18 = !{!16, !12, i64 40}
!19 = !{!12, !12, i64 0}
!20 = !{!16, !6, i64 32}
!21 = !{!16, !12, i64 12}
!22 = !{!11, !6, i64 8}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!16, !12, i64 8}


; 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 = external hidden unnamed_addr constant [84 x i8], align 1
@__FUNCTION__.MemoryDefrag = external hidden unnamed_addr constant [13 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [22 x i8], align 1

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) local_unnamed_addr #0

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @MemoryDefrag(ptr nocapture noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 -24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !12
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %6, i64 28
  %8 = load i32, ptr %7, align 4, !tbaa !15
  %9 = load i32, ptr %0, align 8, !tbaa !17
  %.not = icmp eq i32 %8, %9
  br i1 %.not, label %10, label %.thread

10:                                               ; preds = %1
  %11 = getelementptr inbounds i8, ptr %6, i64 40
  %12 = load i32, ptr %11, align 8, !tbaa !18
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %.thread, label %14

.thread:                                          ; preds = %1, %10
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef 1, i32 noundef -1, ptr noundef nonnull @.str, i32 noundef 635, ptr noundef nonnull @__FUNCTION__.MemoryDefrag, ptr noundef nonnull @.str.6) #3
  br label %14

14:                                               ; preds = %.thread, %10
  %15 = getelementptr inbounds i8, ptr %6, i64 40
  store i32 0, ptr %15, align 8, !tbaa !18
  %16 = getelementptr inbounds i8, ptr %6, i64 32
  %17 = load ptr, ptr %16, align 8, !tbaa !19
  %18 = getelementptr inbounds i8, ptr %6, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !20
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %17, i64 %20
  store i32 1, ptr %21, align 4, !tbaa !21
  %22 = load i32, ptr %0, align 8, !tbaa !17
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %14
  %24 = getelementptr inbounds i8, ptr %0, i64 8
  br label %36

._crit_edge:                                      ; preds = %55, %14
  %.032.lcssa = phi i32 [ 0, %14 ], [ %.1, %55 ]
  store i32 %.032.lcssa, ptr %0, align 8, !tbaa !17
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !22
  %27 = sext i32 %.032.lcssa to i64
  %28 = getelementptr inbounds ptr, ptr %26, i64 %27
  %29 = getelementptr inbounds i8, ptr %6, i64 8
  %30 = load i32, ptr %29, align 8, !tbaa !23
  %31 = sub nsw i32 %30, %.032.lcssa
  %32 = sext i32 %31 to i64
  %33 = shl nsw i64 %32, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %28, i8 0, i64 %33, i1 false)
  %34 = load ptr, ptr %16, align 8, !tbaa !19
  %35 = shl nsw i64 %27, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %34, i8 0, i64 %35, i1 false)
  ret void

36:                                               ; preds = %.lr.ph, %55
  %37 = phi i32 [ %22, %.lr.ph ], [ %56, %55 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %55 ]
  %.03233 = phi i32 [ 0, %.lr.ph ], [ %.1, %55 ]
  %38 = load ptr, ptr %16, align 8, !tbaa !19
  %39 = getelementptr inbounds i32, ptr %38, i64 %indvars.iv
  %40 = load i32, ptr %39, align 4, !tbaa !21
  %41 = icmp eq i32 %40, 1
  %42 = load ptr, ptr %24, align 8, !tbaa !22
  %43 = getelementptr inbounds ptr, ptr %42, i64 %indvars.iv
  %44 = load ptr, ptr %43, align 8, !tbaa !12
  br i1 %41, label %45, label %54

45:                                               ; preds = %36
  %46 = sext i32 %.03233 to i64
  %47 = getelementptr inbounds ptr, ptr %42, i64 %46
  store ptr %44, ptr %47, align 8, !tbaa !12
  %48 = load i32, ptr %18, align 4, !tbaa !20
  %49 = zext i32 %48 to i64
  %50 = icmp eq i64 %indvars.iv, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %45
  store i32 %.03233, ptr %18, align 4, !tbaa !20
  br label %52

52:                                               ; preds = %51, %45
  %53 = add nsw i32 %.03233, 1
  br label %55

54:                                               ; preds = %36
  tail call void @mMemoryBlockRelease(ptr noundef %44)
  %.pre = load i32, ptr %0, align 8, !tbaa !17
  br label %55

55:                                               ; preds = %52, %54
  %56 = phi i32 [ %37, %52 ], [ %.pre, %54 ]
  %.1 = phi i32 [ %53, %52 ], [ %.03233, %54 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %57 = sext i32 %56 to i64
  %58 = icmp slt i64 %indvars.iv.next, %57
  br i1 %58, label %36, label %._crit_edge, !llvm.loop !24
}

attributes #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 = { 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 nocallback nofree nounwind willreturn memory(argmem: write) }
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, !11, i64 8}
!6 = !{!"HandleList", !7, i64 0, !8, i64 16, !8, i64 20, !8, i64 24, !8, i64 28}
!7 = !{!"MList", !8, i64 0, !11, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 16}
!14 = !{!"MHandle", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16}
!15 = !{!16, !8, i64 28}
!16 = !{!"HandleMemory", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !11, i64 16, !8, i64 24, !8, i64 28, !11, i64 32, !8, i64 40}
!17 = !{!7, !8, i64 0}
!18 = !{!16, !8, i64 40}
!19 = !{!16, !11, i64 32}
!20 = !{!16, !8, i64 12}
!21 = !{!8, !8, i64 0}
!22 = !{!7, !11, i64 8}
!23 = !{!16, !8, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


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