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

@.str = external hidden unnamed_addr constant [84 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [15 x i8], align 1
@__FUNCTION__.mMemoryAppend = external hidden unnamed_addr constant [14 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 ptr @mMemoryBlockCreate(i32 noundef, i32 noundef) #2

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @mMemoryAppend(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %19, label %15

15:                                               ; preds = %12
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = ptrtoint ptr %16 to i64
  %18 = icmp eq i64 %17, -1
  br label %19

19:                                               ; preds = %15, %12
  %20 = phi i1 [ true, %12 ], [ %18, %15 ]
  %21 = zext i1 %20 to i32
  store i32 %21, ptr %5, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %19
  %25 = load i32, ptr %5, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %25, i32 noundef -1, ptr noundef @.str, i32 noundef 504, ptr noundef @__FUNCTION__.mMemoryAppend, ptr noundef @.str.4)
  br label %26

26:                                               ; preds = %24, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %27

27:                                               ; preds = %26
  br label %28

28:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = icmp sle i32 %29, 0
  %31 = zext i1 %30 to i32
  store i32 %31, ptr %6, align 4, !tbaa !9
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %28
  %35 = load i32, ptr %6, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %35, i32 noundef -1, ptr noundef @.str, i32 noundef 505, ptr noundef @__FUNCTION__.mMemoryAppend, ptr noundef @.str.4)
  br label %36

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

37:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.HandleList, ptr %38, i64 -1
  %40 = getelementptr inbounds %struct.HandleList, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.MList, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = getelementptr inbounds ptr, ptr %42, i64 0
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  store ptr %44, ptr %7, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.MHandle, ptr %46, i32 0, i32 0
  %48 = load volatile i32, ptr %47, align 8, !tbaa !14
  %49 = icmp ne i32 %48, 628699680
  %50 = zext i1 %49 to i32
  store i32 %50, ptr %8, align 4, !tbaa !9
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %45
  %54 = load i32, ptr %8, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %54, i32 noundef -1, ptr noundef @.str, i32 noundef 508, ptr noundef @__FUNCTION__.mMemoryAppend, ptr noundef @.str.5)
  br label %55

55:                                               ; preds = %53, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %56

56:                                               ; preds = %55
  br label %57

57:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.MHandle, ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !16
  store ptr %60, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.MList, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !17
  store i32 %63, ptr %10, align 4, !tbaa !9
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = load i32, ptr %10, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  call void @mMemoryRedefine(ptr noundef %64, i32 noundef %66, i32 noundef -1, i32 noundef -1)
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.MList, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !18
  %70 = getelementptr inbounds ptr, ptr %69, i64 1
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.MList, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !18
  %74 = load i32, ptr %10, align 4, !tbaa !9
  %75 = sext i32 %74 to i64
  %76 = mul i64 %75, 8
  call void @llvm.memmove.p0.p0.i64(ptr align 8 %70, ptr align 8 %73, i64 %76, i1 false)
  %77 = load i32, ptr %4, align 4, !tbaa !9
  %78 = add nsw i32 %77, 15
  %79 = ashr i32 %78, 4
  %80 = shl i32 %79, 4
  store i32 %80, ptr %4, align 4, !tbaa !9
  %81 = load i32, ptr %4, align 4, !tbaa !9
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.HandleMemory, ptr %82, i32 0, i32 1
  %84 = load i32, ptr %83, align 4, !tbaa !19
  %85 = call ptr @mMemoryBlockCreate(i32 noundef %81, i32 noundef %84)
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.MList, ptr %86, i32 0, i32 1
  %88 = load ptr, ptr %87, align 8, !tbaa !18
  %89 = getelementptr inbounds ptr, ptr %88, i64 0
  store ptr %85, ptr %89, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %90

90:                                               ; preds = %105, %57
  %91 = load i32, ptr %11, align 4, !tbaa !9
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.MList, ptr %92, i32 0, i32 0
  %94 = load i32, ptr %93, align 8, !tbaa !17
  %95 = icmp slt i32 %91, %94
  br i1 %95, label %97, label %96

96:                                               ; preds = %90
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %108

97:                                               ; preds = %90
  %98 = load ptr, ptr %3, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.MList, ptr %98, i32 0, i32 1
  %100 = load ptr, ptr %99, align 8, !tbaa !18
  %101 = load i32, ptr %11, align 4, !tbaa !9
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds ptr, ptr %100, i64 %102
  %104 = load ptr, ptr %103, align 8, !tbaa !5
  call void @mMemoryBlockRead(ptr noundef %104)
  br label %105

105:                                              ; preds = %97
  %106 = load i32, ptr %11, align 4, !tbaa !9
  %107 = add nsw i32 %106, 1
  store i32 %107, ptr %11, align 4, !tbaa !9
  br label %90, !llvm.loop !21

108:                                              ; preds = %96
  %109 = load ptr, ptr %9, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.HandleMemory, ptr %109, i32 0, i32 0
  store i32 0, ptr %110, align 8, !tbaa !23
  %111 = load ptr, ptr %3, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.MList, ptr %111, i32 0, i32 1
  %113 = load ptr, ptr %112, align 8, !tbaa !18
  %114 = getelementptr inbounds ptr, ptr %113, i64 0
  %115 = load ptr, ptr %114, align 8, !tbaa !5
  %116 = getelementptr inbounds %struct.MMemoryBlock, ptr %115, i32 0, i32 2
  %117 = load ptr, ptr %116, align 8, !tbaa !24
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %117
}

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

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: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"HandleList", !13, i64 0, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}
!13 = !{!"MList", !10, i64 0, !6, i64 8}
!14 = !{!15, !10, i64 0}
!15 = !{!"MHandle", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16}
!16 = !{!15, !6, i64 16}
!17 = !{!13, !10, i64 0}
!18 = !{!13, !6, i64 8}
!19 = !{!20, !10, i64 4}
!20 = !{!"HandleMemory", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !6, i64 16, !10, i64 24, !10, i64 28, !6, i64 32, !10, i64 40}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!20, !10, i64 0}
!24 = !{!25, !6, i64 8}
!25 = !{!"MMemoryBlock", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}


; 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
@.str.4 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [15 x i8], align 1
@__FUNCTION__.mMemoryAppend = external hidden unnamed_addr constant [14 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 ptr @mMemoryBlockCreate(i32 noundef, i32 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @mMemoryAppend(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %magicptr = ptrtoint ptr %0 to i64
  switch i64 %magicptr, label %8 [
    i64 -1, label %3
    i64 0, label %3
  ]

3:                                                ; preds = %2, %2
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %0, inttoptr (i64 -1 to ptr)
  %6 = or i1 %4, %5
  %7 = zext i1 %6 to i32
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %7, i32 noundef -1, ptr noundef nonnull @.str, i32 noundef 504, ptr noundef nonnull @__FUNCTION__.mMemoryAppend, ptr noundef nonnull @.str.4) #3
  br label %8

8:                                                ; preds = %2, %3
  %9 = icmp slt i32 %1, 1
  br i1 %9, label %10, label %11

10:                                               ; preds = %8
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef 1, i32 noundef -1, ptr noundef nonnull @.str, i32 noundef 505, ptr noundef nonnull @__FUNCTION__.mMemoryAppend, ptr noundef nonnull @.str.4) #3
  br label %11

11:                                               ; preds = %10, %8
  %12 = getelementptr inbounds i8, ptr %0, i64 -24
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = load volatile i32, ptr %14, align 8, !tbaa !13
  %.not = icmp eq i32 %15, 628699680
  br i1 %.not, label %17, label %16

16:                                               ; preds = %11
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef 1, i32 noundef -1, ptr noundef nonnull @.str, i32 noundef 508, ptr noundef nonnull @__FUNCTION__.mMemoryAppend, ptr noundef nonnull @.str.5) #3
  br label %17

17:                                               ; preds = %16, %11
  %18 = getelementptr inbounds i8, ptr %14, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = load i32, ptr %0, align 8, !tbaa !16
  %21 = add nsw i32 %20, 1
  tail call void @mMemoryRedefine(ptr noundef nonnull %0, i32 noundef %21, i32 noundef -1, i32 noundef -1)
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = sext i32 %20 to i64
  %26 = shl nsw i64 %25, 3
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 8 %24, ptr align 8 %23, i64 %26, i1 false)
  %27 = add nsw i32 %1, 15
  %28 = and i32 %27, -16
  %29 = getelementptr inbounds i8, ptr %19, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !18
  %31 = tail call ptr @mMemoryBlockCreate(i32 noundef %28, i32 noundef %30)
  %32 = load ptr, ptr %22, align 8, !tbaa !17
  store ptr %31, ptr %32, align 8, !tbaa !12
  %33 = load i32, ptr %0, align 8, !tbaa !16
  %34 = icmp sgt i32 %33, 1
  br i1 %34, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %17
  store i32 0, ptr %19, align 8, !tbaa !20
  %35 = load ptr, ptr %22, align 8, !tbaa !17
  %36 = load ptr, ptr %35, align 8, !tbaa !12
  %37 = getelementptr inbounds i8, ptr %36, i64 8
  %38 = load ptr, ptr %37, align 8, !tbaa !21
  ret ptr %38

.lr.ph:                                           ; preds = %17, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %17 ]
  %39 = load ptr, ptr %22, align 8, !tbaa !17
  %40 = getelementptr inbounds ptr, ptr %39, i64 %indvars.iv
  %41 = load ptr, ptr %40, align 8, !tbaa !12
  tail call void @mMemoryBlockRead(ptr noundef %41)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %42 = load i32, ptr %0, align 8, !tbaa !16
  %43 = sext i32 %42 to i64
  %44 = icmp slt i64 %indvars.iv.next, %43
  br i1 %44, label %.lr.ph, label %._crit_edge, !llvm.loop !23
}

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

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: readwrite) }
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, !8, i64 0}
!14 = !{!"MHandle", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16}
!15 = !{!14, !11, i64 16}
!16 = !{!7, !8, i64 0}
!17 = !{!7, !11, i64 8}
!18 = !{!19, !8, i64 4}
!19 = !{!"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}
!20 = !{!19, !8, i64 0}
!21 = !{!22, !11, i64 8}
!22 = !{!"MMemoryBlock", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16, !11, i64 24, !8, i64 32}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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