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

@mainzone = external dso_local global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [39 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
define dso_local void @Z_Free(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 -48
  store ptr %6, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  call void @Z_CheckBlockIntegrity(ptr noundef %7)
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.memblock_s, ptr %8, i32 0, i32 4
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 1919505
  br i1 %11, label %12, label %13

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

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.memblock_s, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !12
  %17 = icmp ugt ptr %16, inttoptr (i64 256 to ptr)
  br i1 %17, label %18, label %22

18:                                               ; preds = %13
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.memblock_s, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  store ptr null, ptr %21, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %18, %13
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.memblock_s, ptr %23, i32 0, i32 2
  store ptr null, ptr %24, align 8, !tbaa !12
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.memblock_s, ptr %25, i32 0, i32 3
  store i32 0, ptr %26, align 8, !tbaa !13
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.memblock_s, ptr %27, i32 0, i32 4
  store i32 0, ptr %28, align 4, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.memblock_s, ptr %29, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  store ptr %31, ptr %4, align 8, !tbaa !5
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.memblock_s, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = icmp ne ptr %34, null
  br i1 %35, label %65, label %36

36:                                               ; preds = %22
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.memblock_s, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !15
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.memblock_s, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = add nsw i32 %42, %39
  store i32 %43, ptr %41, align 4, !tbaa !15
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.memblock_s, ptr %44, i32 0, i32 5
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.memblock_s, ptr %47, i32 0, i32 5
  store ptr %46, ptr %48, align 8, !tbaa !16
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.memblock_s, ptr %50, i32 0, i32 5
  %52 = load ptr, ptr %51, align 8, !tbaa !16
  %53 = getelementptr inbounds %struct.memblock_s, ptr %52, i32 0, i32 6
  store ptr %49, ptr %53, align 8, !tbaa !14
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.memzone_t, ptr %55, i32 0, i32 2
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = icmp eq ptr %54, %57
  br i1 %58, label %59, label %63

59:                                               ; preds = %36
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.memzone_t, ptr %61, i32 0, i32 2
  store ptr %60, ptr %62, align 8, !tbaa !17
  br label %63

63:                                               ; preds = %59, %36
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %64, ptr %3, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %63, %22
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.memblock_s, ptr %66, i32 0, i32 5
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  store ptr %68, ptr %4, align 8, !tbaa !5
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.memblock_s, ptr %69, i32 0, i32 2
  %71 = load ptr, ptr %70, align 8, !tbaa !12
  %72 = icmp ne ptr %71, null
  br i1 %72, label %101, label %73

73:                                               ; preds = %65
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.memblock_s, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !15
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.memblock_s, ptr %77, i32 0, i32 1
  %79 = load i32, ptr %78, align 4, !tbaa !15
  %80 = add nsw i32 %79, %76
  store i32 %80, ptr %78, align 4, !tbaa !15
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.memblock_s, ptr %81, i32 0, i32 5
  %83 = load ptr, ptr %82, align 8, !tbaa !16
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.memblock_s, ptr %84, i32 0, i32 5
  store ptr %83, ptr %85, align 8, !tbaa !16
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.memblock_s, ptr %87, i32 0, i32 5
  %89 = load ptr, ptr %88, align 8, !tbaa !16
  %90 = getelementptr inbounds %struct.memblock_s, ptr %89, i32 0, i32 6
  store ptr %86, ptr %90, align 8, !tbaa !14
  %91 = load ptr, ptr %4, align 8, !tbaa !5
  %92 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.memzone_t, ptr %92, i32 0, i32 2
  %94 = load ptr, ptr %93, align 8, !tbaa !17
  %95 = icmp eq ptr %91, %94
  br i1 %95, label %96, label %100

96:                                               ; preds = %73
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.memzone_t, ptr %98, i32 0, i32 2
  store ptr %97, ptr %99, align 8, !tbaa !17
  br label %100

100:                                              ; preds = %96, %73
  br label %101

101:                                              ; preds = %100, %65
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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

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, !11, i64 20}
!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, !6, i64 32}
!15 = !{!10, !11, i64 4}
!16 = !{!10, !6, i64 24}
!17 = !{!18, !6, i64 56}
!18 = !{!"", !11, i64 0, !10, i64 8, !6, i64 56}


; 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.1 = external hidden unnamed_addr constant [39 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @Z_Free(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 -48
  tail call void @Z_CheckBlockIntegrity(ptr noundef nonnull %2)
  %3 = getelementptr inbounds i8, ptr %0, i64 -28
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %.not = icmp eq i32 %4, 1919505
  br i1 %.not, label %6, label %5

5:                                                ; preds = %1
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.1) #3
  unreachable

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 -40
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = icmp ugt ptr %8, inttoptr (i64 256 to ptr)
  br i1 %9, label %10, label %11

10:                                               ; preds = %6
  store ptr null, ptr %8, align 8, !tbaa !12
  br label %11

11:                                               ; preds = %10, %6
  %12 = getelementptr inbounds i8, ptr %0, i64 -16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %7, i8 0, i64 16, i1 false)
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %.not30 = icmp eq ptr %15, null
  br i1 %.not30, label %16, label %._crit_edge

._crit_edge:                                      ; preds = %11
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 -24
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !14
  br label %31

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 -44
  %18 = load i32, ptr %17, align 4, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %13, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !15
  %21 = add nsw i32 %20, %18
  store i32 %21, ptr %19, align 4, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %0, i64 -24
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %13, i64 24
  store ptr %23, ptr %24, align 8, !tbaa !14
  %25 = getelementptr inbounds i8, ptr %23, i64 32
  store ptr %13, ptr %25, align 8, !tbaa !13
  %26 = load ptr, ptr @mainzone, align 8, !tbaa !12
  %27 = getelementptr inbounds i8, ptr %26, i64 56
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  %29 = icmp eq ptr %2, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %16
  store ptr %13, ptr %27, align 8, !tbaa !16
  br label %31

31:                                               ; preds = %._crit_edge, %16, %30
  %32 = phi ptr [ %.pre, %._crit_edge ], [ %23, %30 ], [ %23, %16 ]
  %.0 = phi ptr [ %2, %._crit_edge ], [ %13, %30 ], [ %13, %16 ]
  %33 = getelementptr inbounds i8, ptr %32, i64 8
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  %.not31 = icmp eq ptr %34, null
  br i1 %.not31, label %35, label %50

35:                                               ; preds = %31
  %36 = getelementptr inbounds i8, ptr %.0, i64 24
  %37 = getelementptr inbounds i8, ptr %32, i64 4
  %38 = load i32, ptr %37, align 4, !tbaa !15
  %39 = getelementptr inbounds i8, ptr %.0, i64 4
  %40 = load i32, ptr %39, align 4, !tbaa !15
  %41 = add nsw i32 %40, %38
  store i32 %41, ptr %39, align 4, !tbaa !15
  %42 = getelementptr inbounds i8, ptr %32, i64 24
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  store ptr %43, ptr %36, align 8, !tbaa !14
  %44 = getelementptr inbounds i8, ptr %43, i64 32
  store ptr %.0, ptr %44, align 8, !tbaa !13
  %45 = load ptr, ptr @mainzone, align 8, !tbaa !12
  %46 = getelementptr inbounds i8, ptr %45, i64 56
  %47 = load ptr, ptr %46, align 8, !tbaa !16
  %48 = icmp eq ptr %32, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %35
  store ptr %.0, ptr %46, align 8, !tbaa !16
  br label %50

50:                                               ; preds = %35, %49, %31
  ret void
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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, !7, i64 20}
!6 = !{!"memblock_s", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 32, !7, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 32}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !7, i64 4}
!16 = !{!17, !10, i64 56}
!17 = !{!"", !7, i64 0, !6, i64 8, !10, i64 56}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass