; 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.MAFBlock = type { ptr, i16, i16, i32, i32, [0 x i8] }
%struct.MAFInfo = type { ptr, ptr, ptr, i32, i32, i32 }

@morn_alloc_free_ref_flag2 = external dso_local global [16 x i16], 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
define dso_local void @m_Free(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  br label %119

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 -16
  store ptr %14, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds i64, ptr %15, i64 0
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %12
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %20) #3
  store i32 1, ptr %4, align 4
  br label %117

21:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds i64, ptr %22, i64 0
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = inttoptr i64 %24 to ptr
  store ptr %25, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MAFBlock, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  store ptr %28, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds i64, ptr %29, i64 1
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = trunc i64 %31 to i32
  store i32 %32, ptr %7, align 4, !tbaa !15
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.MAFInfo, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = icmp ne ptr %33, %36
  br i1 %37, label %38, label %104

38:                                               ; preds = %21
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MAFInfo, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = icmp ne ptr %39, %42
  br i1 %43, label %44, label %104

44:                                               ; preds = %38
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.MAFBlock, ptr %45, i32 0, i32 3
  %47 = load volatile i32, ptr %46, align 4, !tbaa !19
  %48 = icmp eq i32 %47, 15
  br i1 %48, label %49, label %69

49:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.MAFBlock, ptr %50, i32 0, i32 4
  %52 = load i32, ptr %51, align 8, !tbaa !20
  store i32 %52, ptr %8, align 4, !tbaa !15
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.MAFInfo, ptr %53, i32 0, i32 3
  %55 = load i32, ptr %54, align 8, !tbaa !21
  %56 = sub nsw i32 %55, 1
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.MAFInfo, ptr %57, i32 0, i32 3
  store i32 %56, ptr %58, align 8, !tbaa !21
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.MAFInfo, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !22
  %62 = load i32, ptr %8, align 4, !tbaa !15
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds ptr, ptr %61, i64 %63
  store ptr null, ptr %64, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !15
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.MAFInfo, ptr %66, i32 0, i32 5
  store i32 %65, ptr %67, align 8, !tbaa !23
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  call void @m_Free(ptr noundef %68)
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %116

69:                                               ; preds = %44
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.MAFBlock, ptr %70, i32 0, i32 3
  %72 = load volatile i32, ptr %71, align 4, !tbaa !19
  %73 = icmp sgt i32 %72, 0
  br i1 %73, label %74, label %102

74:                                               ; preds = %69
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.MAFInfo, ptr %75, i32 0, i32 2
  %77 = load ptr, ptr %76, align 8, !tbaa !18
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.MAFInfo, ptr %78, i32 0, i32 1
  %80 = load ptr, ptr %79, align 8, !tbaa !16
  %81 = icmp eq ptr %77, %80
  br i1 %81, label %82, label %86

82:                                               ; preds = %74
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.MAFInfo, ptr %84, i32 0, i32 2
  store ptr %83, ptr %85, align 8, !tbaa !18
  br label %101

86:                                               ; preds = %74
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.MAFBlock, ptr %87, i32 0, i32 3
  %89 = load volatile i32, ptr %88, align 4, !tbaa !19
  %90 = load ptr, ptr %6, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.MAFInfo, ptr %90, i32 0, i32 2
  %92 = load ptr, ptr %91, align 8, !tbaa !18
  %93 = getelementptr inbounds %struct.MAFBlock, ptr %92, i32 0, i32 3
  %94 = load volatile i32, ptr %93, align 4, !tbaa !19
  %95 = icmp sgt i32 %89, %94
  br i1 %95, label %96, label %100

96:                                               ; preds = %86
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.MAFInfo, ptr %98, i32 0, i32 2
  store ptr %97, ptr %99, align 8, !tbaa !18
  br label %100

100:                                              ; preds = %96, %86
  br label %101

101:                                              ; preds = %100, %82
  br label %102

102:                                              ; preds = %101, %69
  br label %103

103:                                              ; preds = %102
  br label %104

104:                                              ; preds = %103, %38, %21
  %105 = load ptr, ptr %5, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.MAFBlock, ptr %105, i32 0, i32 2
  %107 = load i32, ptr %7, align 4, !tbaa !15
  %108 = sext i32 %107 to i64
  %109 = getelementptr inbounds [16 x i16], ptr @morn_alloc_free_ref_flag2, i64 0, i64 %108
  %110 = load i16, ptr %109, align 2, !tbaa !24
  %111 = atomicrmw and ptr %106, i16 %110 seq_cst, align 2
  %112 = load ptr, ptr %5, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.MAFBlock, ptr %112, i32 0, i32 3
  %114 = atomicrmw add ptr %113, i32 1 seq_cst, align 4
  %115 = add i32 %114, 1
  store i32 0, ptr %4, align 4
  br label %116

116:                                              ; preds = %104, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %117

117:                                              ; preds = %116, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %118 = load i32, ptr %4, align 4
  switch i32 %118, label %120 [
    i32 0, label %119
    i32 1, label %119
  ]

119:                                              ; preds = %117, %117, %11
  ret void

120:                                              ; preds = %117
  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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"MAFBlock", !6, i64 0, !13, i64 8, !13, i64 10, !14, i64 12, !14, i64 16, !7, i64 20}
!13 = !{!"short", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"MAFInfo", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !14, i64 28, !14, i64 32}
!18 = !{!17, !6, i64 16}
!19 = !{!12, !14, i64 12}
!20 = !{!12, !14, i64 16}
!21 = !{!17, !14, i64 24}
!22 = !{!17, !6, i64 0}
!23 = !{!17, !14, i64 32}
!24 = !{!13, !13, 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"

@morn_alloc_free_ref_flag2 = external dso_local local_unnamed_addr global [16 x i16], 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
define dso_local void @m_Free(ptr noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 -16
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.lr.ph._crit_edge, label %.lr.ph89

.lr.ph._crit_edge:                                ; preds = %tailrecurse, %.lr.ph.preheader
  %.tr48.lcssa = phi ptr [ %0, %.lr.ph.preheader ], [ %8, %tailrecurse ]
  %6 = getelementptr inbounds i8, ptr %.tr48.lcssa, i64 -16
  tail call void @free(ptr noundef nonnull %6) #2
  br label %.loopexit

.lr.ph89:                                         ; preds = %.lr.ph.preheader, %tailrecurse
  %7 = phi i64 [ %31, %tailrecurse ], [ %4, %.lr.ph.preheader ]
  %.tr4888 = phi ptr [ %8, %tailrecurse ], [ %0, %.lr.ph.preheader ]
  %8 = inttoptr i64 %7 to ptr
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %.tr4888, i64 -8
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %9, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %.not = icmp eq ptr %13, %8
  br i1 %.not, label %.loopexit34, label %14

14:                                               ; preds = %.lr.ph89
  %15 = getelementptr inbounds i8, ptr %9, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %.not33 = icmp eq ptr %16, %8
  br i1 %.not33, label %.loopexit34, label %17

17:                                               ; preds = %14
  %18 = getelementptr inbounds i8, ptr %8, i64 12
  %19 = load volatile i32, ptr %18, align 4, !tbaa !17
  %20 = icmp eq i32 %19, 15
  br i1 %20, label %tailrecurse, label %33

tailrecurse:                                      ; preds = %17
  %21 = getelementptr inbounds i8, ptr %8, i64 16
  %22 = load i32, ptr %21, align 8, !tbaa !18
  %23 = getelementptr inbounds i8, ptr %9, i64 24
  %24 = load i32, ptr %23, align 8, !tbaa !19
  %25 = add nsw i32 %24, -1
  store i32 %25, ptr %23, align 8, !tbaa !19
  %26 = load ptr, ptr %9, align 8, !tbaa !20
  %27 = sext i32 %22 to i64
  %28 = getelementptr inbounds ptr, ptr %26, i64 %27
  store ptr null, ptr %28, align 8, !tbaa !21
  %29 = getelementptr inbounds i8, ptr %9, i64 32
  store i32 %22, ptr %29, align 8, !tbaa !22
  %30 = getelementptr inbounds i8, ptr %8, i64 -16
  %31 = load i64, ptr %30, align 8, !tbaa !5
  %32 = icmp eq i64 %31, 0
  br i1 %32, label %.lr.ph._crit_edge, label %.lr.ph89

33:                                               ; preds = %17
  %34 = getelementptr inbounds i8, ptr %9, i64 16
  %35 = getelementptr inbounds i8, ptr %8, i64 12
  %36 = load volatile i32, ptr %35, align 4, !tbaa !17
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %38, label %.loopexit34

38:                                               ; preds = %33
  %39 = icmp eq ptr %16, %13
  br i1 %39, label %.loopexit34.sink.split, label %40

40:                                               ; preds = %38
  %41 = load volatile i32, ptr %35, align 4, !tbaa !17
  %42 = getelementptr inbounds i8, ptr %16, i64 12
  %43 = load volatile i32, ptr %42, align 4, !tbaa !17
  %44 = icmp sgt i32 %41, %43
  br i1 %44, label %.loopexit34.sink.split, label %.loopexit34

.loopexit34.sink.split:                           ; preds = %40, %38
  store ptr %8, ptr %34, align 8, !tbaa !16
  br label %.loopexit34

.loopexit34:                                      ; preds = %14, %.lr.ph89, %.loopexit34.sink.split, %40, %33
  %45 = getelementptr inbounds i8, ptr %8, i64 10
  %sext = shl i64 %11, 32
  %46 = ashr exact i64 %sext, 32
  %47 = getelementptr inbounds [16 x i16], ptr @morn_alloc_free_ref_flag2, i64 0, i64 %46
  %48 = load i16, ptr %47, align 2, !tbaa !23
  %49 = atomicrmw and ptr %45, i16 %48 seq_cst, align 2
  %50 = getelementptr inbounds i8, ptr %8, i64 12
  %51 = atomicrmw add ptr %50, i32 1 seq_cst, align 4
  br label %.loopexit

.loopexit:                                        ; preds = %1, %.lr.ph._crit_edge, %.loopexit34
  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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"MAFBlock", !11, i64 0, !12, i64 8, !12, i64 10, !13, i64 12, !13, i64 16, !7, i64 20}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !11, i64 8}
!15 = !{!"MAFInfo", !11, i64 0, !11, i64 8, !11, i64 16, !13, i64 24, !13, i64 28, !13, i64 32}
!16 = !{!15, !11, i64 16}
!17 = !{!10, !13, i64 12}
!18 = !{!10, !13, i64 16}
!19 = !{!15, !13, i64 24}
!20 = !{!15, !11, i64 0}
!21 = !{!11, !11, i64 0}
!22 = !{!15, !13, i64 32}
!23 = !{!12, !12, i64 0}


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