; LLVM IR from asllop/AppOS :: core/mem (mem_internal)

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

@blocks = external hidden global [1 x %struct.BlockStruct], align 16
@numBlocks = external hidden global i8, align 1
@.str = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @mem_init() #0 {
  %1 = alloca i8, align 1
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  call void (...) @setup_mem()
  %6 = call i32 @scan_blocks(ptr noundef @blocks)
  %7 = trunc i32 %6 to i8
  store i8 %7, ptr @numBlocks, align 1, !tbaa !5
  %8 = zext i8 %7 to i32
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %47

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

11:                                               ; preds = %43, %10
  %12 = load i8, ptr %1, align 1, !tbaa !5
  %13 = zext i8 %12 to i32
  %14 = load i8, ptr @numBlocks, align 1, !tbaa !5
  %15 = zext i8 %14 to i32
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %11
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #3
  br label %46

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %19 = load i8, ptr %1, align 1, !tbaa !5
  %20 = zext i8 %19 to i64
  %21 = getelementptr inbounds [1 x %struct.BlockStruct], ptr @blocks, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.BlockStruct, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !8
  %24 = udiv i64 %23, 256
  store i64 %24, ptr %3, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %25 = load i8, ptr %1, align 1, !tbaa !5
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [1 x %struct.BlockStruct], ptr @blocks, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.BlockStruct, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 16, !tbaa !13
  store ptr %29, ptr %4, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !12
  br label %30

30:                                               ; preds = %39, %18
  %31 = load i64, ptr %5, align 8, !tbaa !12
  %32 = load i64, ptr %3, align 8, !tbaa !12
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %30
  %36 = load ptr, ptr %4, align 8, !tbaa !14
  store i16 0, ptr %36, align 2, !tbaa !15
  %37 = load ptr, ptr %4, align 8, !tbaa !14
  %38 = getelementptr inbounds i8, ptr %37, i64 256
  store ptr %38, ptr %4, align 8, !tbaa !14
  br label %39

39:                                               ; preds = %35
  %40 = load i64, ptr %5, align 8, !tbaa !12
  %41 = add i64 %40, 1
  store i64 %41, ptr %5, align 8, !tbaa !12
  br label %30, !llvm.loop !17

42:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %43

43:                                               ; preds = %42
  %44 = load i8, ptr %1, align 1, !tbaa !5
  %45 = add i8 %44, 1
  store i8 %45, ptr %1, align 1, !tbaa !5
  br label %11, !llvm.loop !19

46:                                               ; preds = %17
  br label %48

47:                                               ; preds = %0
  call void @core_fatal(ptr noundef @.str)
  br label %48

48:                                               ; preds = %47, %46
  ret void
}

declare void @setup_mem(...) #1

declare i32 @scan_blocks(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

declare void @core_fatal(ptr noundef) #1

attributes #0 = { 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 #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 = { nocallback nofree nosync 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 8}
!9 = !{!"BlockStruct", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!9, !10, i64 0}
!14 = !{!10, !10, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"short", !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

@blocks = external hidden global [1 x %struct.BlockStruct], align 16
@numBlocks = external hidden local_unnamed_addr global i8, align 1
@.str = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @mem_init() local_unnamed_addr #0 {
  tail call void (...) @setup_mem() #2
  %1 = tail call i32 @scan_blocks(ptr noundef nonnull @blocks) #2
  %2 = trunc i32 %1 to i8
  store i8 %2, ptr @numBlocks, align 1, !tbaa !5
  %3 = and i32 %1, 255
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %24, label %.preheader

.preheader:                                       ; preds = %0
  %.not14 = icmp eq i8 %2, 0
  br i1 %.not14, label %.loopexit, label %.lr.ph13

.lr.ph13:                                         ; preds = %.preheader, %._crit_edge
  %4 = phi i8 [ %13, %._crit_edge ], [ %2, %.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 0, %.preheader ]
  %5 = getelementptr inbounds [1 x %struct.BlockStruct], ptr @blocks, i64 0, i64 %indvars.iv
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !8
  %8 = lshr i64 %7, 8
  %.not15 = icmp ult i64 %7, 256
  br i1 %.not15, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph13
  %9 = load ptr, ptr %5, align 16, !tbaa !12
  %10 = add nsw i64 %8, -1
  %xtraiter = and i64 %8, 7
  %11 = icmp ult i64 %10, 7
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %8, 72057594037927928
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.0810.unr = phi ptr [ %9, %.lr.ph.preheader ], [ %23, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.0810.epil = phi ptr [ %12, %.lr.ph.epil ], [ %.0810.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  store i16 0, ptr %.0810.epil, align 2, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %.0810.epil, i64 256
  %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 %._crit_edge.loopexit, label %.lr.ph.epil, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa
  %.pre = load i8, ptr @numBlocks, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph13
  %13 = phi i8 [ %.pre, %._crit_edge.loopexit ], [ %4, %.lr.ph13 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = zext i8 %13 to i64
  %15 = icmp ult i64 %indvars.iv.next, %14
  br i1 %15, label %.lr.ph13, label %.loopexit, !llvm.loop !17

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.0810 = phi ptr [ %9, %.lr.ph.preheader.new ], [ %23, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  store i16 0, ptr %.0810, align 2, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %.0810, i64 256
  store i16 0, ptr %16, align 2, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %.0810, i64 512
  store i16 0, ptr %17, align 2, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %.0810, i64 768
  store i16 0, ptr %18, align 2, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %.0810, i64 1024
  store i16 0, ptr %19, align 2, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %.0810, i64 1280
  store i16 0, ptr %20, align 2, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %.0810, i64 1536
  store i16 0, ptr %21, align 2, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %.0810, i64 1792
  store i16 0, ptr %22, align 2, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %.0810, i64 2048
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !19

24:                                               ; preds = %0
  tail call void @core_fatal(ptr noundef nonnull @.str) #2
  br label %.loopexit

.loopexit:                                        ; preds = %._crit_edge, %.preheader, %24
  ret void
}

declare void @setup_mem(...) local_unnamed_addr #1

declare i32 @scan_blocks(ptr noundef) local_unnamed_addr #1

declare void @core_fatal(ptr noundef) local_unnamed_addr #1

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 8}
!9 = !{!"BlockStruct", !10, i64 0, !11, i64 8}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!9, !10, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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