; 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 dso_local void @Z_Free(ptr noundef) #1

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

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

11:                                               ; preds = %42, %2
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = load ptr, ptr @mainzone, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.memzone_t, ptr %13, i32 0, i32 1
  %15 = icmp ne ptr %12, %14
  br i1 %15, label %16, label %44

16:                                               ; preds = %11
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  call void @Z_CheckBlockIntegrity(ptr noundef %17)
  %18 = load ptr, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.memblock_s, ptr %18, i32 0, i32 5
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  store ptr %20, ptr %6, align 8, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.memblock_s, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = icmp ne ptr %23, null
  br i1 %24, label %26, label %25

25:                                               ; preds = %16
  br label %42

26:                                               ; preds = %16
  %27 = load ptr, ptr %5, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.memblock_s, ptr %27, i32 0, i32 3
  %29 = load i32, ptr %28, align 8, !tbaa !16
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = icmp sge i32 %29, %30
  br i1 %31, label %32, label %41

32:                                               ; preds = %26
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.memblock_s, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = icmp sle i32 %35, %36
  br i1 %37, label %38, label %41

38:                                               ; preds = %32
  %39 = load ptr, ptr %5, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %39, i64 48
  call void @Z_Free(ptr noundef %40)
  br label %41

41:                                               ; preds = %38, %32, %26
  br label %42

42:                                               ; preds = %41, %25
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %43, ptr %5, align 8, !tbaa !9
  br label %11, !llvm.loop !17

44:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  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 = !{!"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 24}
!15 = !{!13, !10, i64 8}
!16 = !{!13, !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"

@mainzone = external dso_local local_unnamed_addr global ptr, align 8

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

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

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

.lr.ph:                                           ; preds = %2, %16
  %.014 = phi ptr [ %8, %16 ], [ %5, %2 ]
  tail call void @Z_CheckBlockIntegrity(ptr noundef %.014)
  %7 = getelementptr inbounds i8, ptr %.014, i64 24
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %.014, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !14
  %.not10 = icmp eq ptr %10, null
  br i1 %.not10, label %16, label %11

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.014, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !15
  %.not11 = icmp slt i32 %13, %0
  %.not12 = icmp sgt i32 %13, %1
  %or.cond = or i1 %.not11, %.not12
  br i1 %or.cond, label %16, label %14

14:                                               ; preds = %11
  %15 = getelementptr inbounds i8, ptr %.014, i64 48
  tail call void @Z_Free(ptr noundef nonnull %15)
  br label %16

16:                                               ; preds = %11, %14, %.lr.ph
  %17 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %.not = icmp eq ptr %8, %18
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %16, %2
  ret void
}

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


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