; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (z_zone)

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

@mainzone = external dso_local global ptr, align 8

; 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
declare hidden void @Z_CheckBlockIntegrity(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @Z_FreeMemory() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  %3 = load ptr, ptr @mainzone, align 8, !tbaa !9
  %4 = getelementptr inbounds %struct.memzone_t, ptr %3, i32 0, i32 1
  %5 = getelementptr inbounds %struct.memblock_s, ptr %4, i32 0, i32 5
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  store ptr %6, ptr %1, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %30, %0
  %8 = load ptr, ptr %1, align 8, !tbaa !9
  %9 = load ptr, ptr @mainzone, align 8, !tbaa !9
  %10 = getelementptr inbounds %struct.memzone_t, ptr %9, i32 0, i32 1
  %11 = icmp ne ptr %8, %10
  br i1 %11, label %12, label %34

12:                                               ; preds = %7
  %13 = load ptr, ptr %1, align 8, !tbaa !9
  call void @Z_CheckBlockIntegrity(ptr noundef %13)
  %14 = load ptr, ptr %1, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.memblock_s, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %23

18:                                               ; preds = %12
  %19 = load ptr, ptr %1, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.memblock_s, ptr %19, i32 0, i32 3
  %21 = load i32, ptr %20, align 8, !tbaa !15
  %22 = icmp sge i32 %21, 100
  br i1 %22, label %23, label %29

23:                                               ; preds = %18, %12
  %24 = load ptr, ptr %1, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.memblock_s, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !16
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr %2, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %23, %18
  br label %30

30:                                               ; preds = %29
  %31 = load ptr, ptr %1, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.memblock_s, ptr %31, i32 0, i32 5
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  store ptr %33, ptr %1, align 8, !tbaa !9
  br label %7, !llvm.loop !18

34:                                               ; preds = %7
  %35 = load i32, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i32 %35
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 32}
!12 = !{!"", !6, i64 0, !13, i64 8, !10, i64 56}
!13 = !{!"memblock_s", !6, i64 0, !6, i64 4, !10, i64 8, !6, i64 16, !6, i64 20, !10, i64 24, !10, i64 32, !6, i64 40}
!14 = !{!13, !10, i64 8}
!15 = !{!13, !6, i64 16}
!16 = !{!13, !6, i64 4}
!17 = !{!13, !10, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@mainzone = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare hidden void @Z_CheckBlockIntegrity(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @Z_FreeMemory() local_unnamed_addr #0 {
  %1 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 32
  %.079 = load ptr, ptr %2, align 8, !tbaa !9
  %3 = getelementptr inbounds i8, ptr %1, i64 8
  %.not10 = icmp eq ptr %.079, %3
  br i1 %.not10, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %14
  %.0712 = phi ptr [ %.07, %14 ], [ %.079, %0 ]
  %.011 = phi i32 [ %.1, %14 ], [ 0, %0 ]
  tail call void @Z_CheckBlockIntegrity(ptr noundef %.0712)
  %4 = getelementptr inbounds i8, ptr %.0712, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  %.not8 = icmp eq ptr %5, null
  br i1 %.not8, label %10, label %6

6:                                                ; preds = %.lr.ph
  %7 = getelementptr inbounds i8, ptr %.0712, i64 16
  %8 = load i32, ptr %7, align 8, !tbaa !13
  %9 = icmp sgt i32 %8, 99
  br i1 %9, label %10, label %14

10:                                               ; preds = %6, %.lr.ph
  %11 = getelementptr inbounds i8, ptr %.0712, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = add nsw i32 %12, %.011
  br label %14

14:                                               ; preds = %6, %10
  %.1 = phi i32 [ %13, %10 ], [ %.011, %6 ]
  %15 = getelementptr inbounds i8, ptr %.0712, i64 24
  %.07 = load ptr, ptr %15, align 8, !tbaa !9
  %16 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  %.not = icmp eq ptr %.07, %17
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %14, %0
  %.0.lcssa = phi i32 [ 0, %0 ], [ %.1, %14 ]
  ret i32 %.0.lcssa
}

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

!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 24}
!10 = !{!"memblock_s", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16, !11, i64 20, !6, i64 24, !6, i64 32, !11, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !11, i64 16}
!14 = !{!10, !11, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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