; 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
@.str.10 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [42 x i8], align 1

; 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: noreturn
declare void @I_Error(ptr noundef, ...) #1

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

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

6:                                                ; preds = %50, %0
  %7 = load ptr, ptr %1, align 8, !tbaa !5
  call void @Z_CheckBlockIntegrity(ptr noundef %7)
  %8 = load ptr, ptr %1, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.memblock_s, ptr %8, i32 0, i32 5
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.memzone_t, ptr %11, i32 0, i32 1
  %13 = icmp eq ptr %10, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %6
  br label %54

15:                                               ; preds = %6
  %16 = load ptr, ptr %1, align 8, !tbaa !5
  %17 = load ptr, ptr %1, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.memblock_s, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %16, i64 %20
  %22 = load ptr, ptr %1, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.memblock_s, ptr %22, i32 0, i32 5
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = icmp ne ptr %21, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %15
  call void (ptr, ...) @I_Error(ptr noundef @.str.10) #4
  unreachable

27:                                               ; preds = %15
  %28 = load ptr, ptr %1, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.memblock_s, ptr %28, i32 0, i32 5
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct.memblock_s, ptr %30, i32 0, i32 6
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  %34 = icmp ne ptr %32, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %27
  call void (ptr, ...) @I_Error(ptr noundef @.str.11) #4
  unreachable

36:                                               ; preds = %27
  %37 = load ptr, ptr %1, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.memblock_s, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = icmp ne ptr %39, null
  br i1 %40, label %49, label %41

41:                                               ; preds = %36
  %42 = load ptr, ptr %1, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.memblock_s, ptr %42, i32 0, i32 5
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  %45 = getelementptr inbounds %struct.memblock_s, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = icmp ne ptr %46, null
  br i1 %47, label %49, label %48

48:                                               ; preds = %41
  call void (ptr, ...) @I_Error(ptr noundef @.str.12) #4
  unreachable

49:                                               ; preds = %41, %36
  br label %50

50:                                               ; preds = %49
  %51 = load ptr, ptr %1, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.memblock_s, ptr %51, i32 0, i32 5
  %53 = load ptr, ptr %52, align 8, !tbaa !13
  store ptr %53, ptr %1, align 8, !tbaa !5
  br label %6

54:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn "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 }
attributes #4 = { noreturn }

!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, !11, i64 4}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !6, i64 8}


; 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
@.str.10 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [42 x i8], align 1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @Z_CheckHeap() local_unnamed_addr #1 {
  %1 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 32
  %.014 = load ptr, ptr %2, align 8, !tbaa !9
  tail call void @Z_CheckBlockIntegrity(ptr noundef %.014)
  %3 = getelementptr inbounds i8, ptr %.014, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = icmp eq ptr %4, %6
  br i1 %7, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %24
  %.0 = phi ptr [ %26, %24 ], [ %4, %0 ]
  %.015 = phi ptr [ %.0, %24 ], [ %.014, %0 ]
  %8 = getelementptr inbounds i8, ptr %.015, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !12
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %.015, i64 %10
  %.not = icmp eq ptr %11, %.0
  br i1 %.not, label %13, label %12

12:                                               ; preds = %.lr.ph
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.10) #2
  unreachable

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.0, i64 32
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %.not11 = icmp eq ptr %15, %.015
  br i1 %.not11, label %17, label %16

16:                                               ; preds = %13
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.11) #2
  unreachable

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %.015, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %.not12 = icmp eq ptr %19, null
  br i1 %.not12, label %20, label %24

20:                                               ; preds = %17
  %21 = getelementptr inbounds i8, ptr %.0, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %.not13 = icmp eq ptr %22, null
  br i1 %.not13, label %23, label %24

23:                                               ; preds = %20
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.12) #2
  unreachable

24:                                               ; preds = %17, %20
  tail call void @Z_CheckBlockIntegrity(ptr noundef nonnull %.0)
  %25 = getelementptr inbounds i8, ptr %.0, i64 24
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  %27 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  %29 = icmp eq ptr %26, %28
  br i1 %29, label %._crit_edge, label %.lr.ph

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

attributes #0 = { noreturn "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 = { noreturn 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, !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, !11, i64 4}
!13 = !{!10, !6, i64 32}
!14 = !{!10, !6, i64 8}


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