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

; 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 }

; 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 uwtable
define dso_local void @mHandleReset(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %12, label %8

8:                                                ; preds = %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = ptrtoint ptr %9 to i64
  %11 = icmp eq i64 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %8, %1
  br label %39

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.HandleList, ptr %14, i64 -1
  store ptr %15, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %35, %13
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.HandleList, ptr %18, i32 0, i32 0
  %20 = getelementptr inbounds %struct.MList, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = icmp slt i32 %17, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %38

24:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.HandleList, ptr %25, i32 0, i32 0
  %27 = getelementptr inbounds %struct.MList, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds ptr, ptr %28, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !5
  store ptr %32, ptr %5, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MHandle, ptr %33, i32 0, i32 1
  store volatile i32 0, ptr %34, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %35

35:                                               ; preds = %24
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %4, align 4, !tbaa !9
  br label %16, !llvm.loop !17

38:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %39

39:                                               ; preds = %38, %12
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !10, i64 0}
!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 = !{!12, !6, i64 8}
!15 = !{!16, !10, i64 4}
!16 = !{!"MHandle", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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"

; Function Attrs: nofree norecurse nounwind uwtable
define dso_local void @mHandleReset(ptr noundef %0) local_unnamed_addr #0 {
  %magicptr = ptrtoint ptr %0 to i64
  switch i64 %magicptr, label %2 [
    i64 -1, label %.loopexit
    i64 0, label %.loopexit
  ]

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 -32
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %.fr = freeze i32 %4
  %5 = icmp sgt i32 %.fr, 1
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 -24
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %wide.trip.count = zext i32 %.fr to i64
  %8 = add nsw i64 %wide.trip.count, -1
  %9 = add nsw i64 %wide.trip.count, -2
  %xtraiter = and i64 %8, 7
  %10 = icmp ult i64 %9, 7
  br i1 %10, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %8, -8
  %invariant.gep = getelementptr inbounds i8, ptr %7, i64 8
  %invariant.gep13 = getelementptr inbounds i8, ptr %7, i64 16
  %invariant.gep15 = getelementptr inbounds i8, ptr %7, i64 24
  %invariant.gep17 = getelementptr inbounds i8, ptr %7, i64 32
  %invariant.gep19 = getelementptr inbounds i8, ptr %7, i64 40
  %invariant.gep21 = getelementptr inbounds i8, ptr %7, i64 48
  %invariant.gep23 = getelementptr inbounds i8, ptr %7, i64 56
  br label %11

11:                                               ; preds = %11, %.lr.ph.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.new ], [ %indvars.iv.next.7, %11 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.7, %11 ]
  %12 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  store volatile i32 0, ptr %14, align 4, !tbaa !14
  %gep = getelementptr inbounds ptr, ptr %invariant.gep, i64 %indvars.iv
  %15 = load ptr, ptr %gep, align 8, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %15, i64 4
  store volatile i32 0, ptr %16, align 4, !tbaa !14
  %gep14 = getelementptr inbounds ptr, ptr %invariant.gep13, i64 %indvars.iv
  %17 = load ptr, ptr %gep14, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %17, i64 4
  store volatile i32 0, ptr %18, align 4, !tbaa !14
  %gep16 = getelementptr inbounds ptr, ptr %invariant.gep15, i64 %indvars.iv
  %19 = load ptr, ptr %gep16, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %19, i64 4
  store volatile i32 0, ptr %20, align 4, !tbaa !14
  %gep18 = getelementptr inbounds ptr, ptr %invariant.gep17, i64 %indvars.iv
  %21 = load ptr, ptr %gep18, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %21, i64 4
  store volatile i32 0, ptr %22, align 4, !tbaa !14
  %gep20 = getelementptr inbounds ptr, ptr %invariant.gep19, i64 %indvars.iv
  %23 = load ptr, ptr %gep20, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %23, i64 4
  store volatile i32 0, ptr %24, align 4, !tbaa !14
  %gep22 = getelementptr inbounds ptr, ptr %invariant.gep21, i64 %indvars.iv
  %25 = load ptr, ptr %gep22, align 8, !tbaa !13
  %26 = getelementptr inbounds i8, ptr %25, i64 4
  store volatile i32 0, ptr %26, align 4, !tbaa !14
  %gep24 = getelementptr inbounds ptr, ptr %invariant.gep23, i64 %indvars.iv
  %27 = load ptr, ptr %gep24, align 8, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %27, i64 4
  store volatile i32 0, ptr %28, align 4, !tbaa !14
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.loopexit.loopexit.unr-lcssa, label %11, !llvm.loop !16

.loopexit.loopexit.unr-lcssa:                     ; preds = %11, %.lr.ph
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next.7, %11 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.epil.preheader

.epil.preheader:                                  ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %29 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv.epil
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %30, i64 4
  store volatile i32 0, ptr %31, align 4, !tbaa !14
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.epil.preheader, !llvm.loop !18

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader, %2, %1, %1
  ret void
}

attributes #0 = { nofree norecurse 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" }

!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, !8, i64 0}
!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 = !{!6, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !8, i64 4}
!15 = !{!"MHandle", !8, i64 0, !8, i64 4, !11, i64 8, !11, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.unroll.disable"}


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