; 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 = external hidden unnamed_addr constant [50 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: nounwind uwtable
define dso_local void @Z_Init() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  %3 = call ptr @I_ZoneBase(ptr noundef %2)
  store ptr %3, ptr @mainzone, align 8, !tbaa !5
  %4 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %8

6:                                                ; preds = %0
  %7 = load i32, ptr %2, align 4, !tbaa !9
  call void (ptr, ...) @I_Error(ptr noundef @.str, i32 noundef %7) #6
  unreachable

8:                                                ; preds = %0
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.memzone_t, ptr %10, i32 0, i32 0
  store i32 %9, ptr %11, align 8, !tbaa !11
  %12 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  call void @llvm.memset.p0.i64(ptr align 8 %12, i8 85, i64 %14, i1 false)
  %15 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 64
  store ptr %16, ptr %1, align 8, !tbaa !5
  %17 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.memzone_t, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds %struct.memblock_s, ptr %18, i32 0, i32 6
  store ptr %16, ptr %19, align 8, !tbaa !14
  %20 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.memzone_t, ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds %struct.memblock_s, ptr %21, i32 0, i32 5
  store ptr %16, ptr %22, align 8, !tbaa !15
  %23 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %24 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.memzone_t, ptr %24, i32 0, i32 1
  %26 = getelementptr inbounds %struct.memblock_s, ptr %25, i32 0, i32 2
  store ptr %23, ptr %26, align 8, !tbaa !16
  %27 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.memzone_t, ptr %27, i32 0, i32 1
  %29 = getelementptr inbounds %struct.memblock_s, ptr %28, i32 0, i32 3
  store i32 1, ptr %29, align 8, !tbaa !17
  %30 = load ptr, ptr %1, align 8, !tbaa !5
  %31 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.memzone_t, ptr %31, i32 0, i32 2
  store ptr %30, ptr %32, align 8, !tbaa !18
  %33 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.memzone_t, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %1, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.memblock_s, ptr %35, i32 0, i32 5
  store ptr %34, ptr %36, align 8, !tbaa !19
  %37 = load ptr, ptr %1, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.memblock_s, ptr %37, i32 0, i32 6
  store ptr %34, ptr %38, align 8, !tbaa !20
  %39 = load ptr, ptr %1, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.memblock_s, ptr %39, i32 0, i32 2
  store ptr null, ptr %40, align 8, !tbaa !21
  %41 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.memzone_t, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !11
  %44 = sext i32 %43 to i64
  %45 = sub i64 %44, 64
  %46 = trunc i64 %45 to i32
  %47 = load ptr, ptr %1, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.memblock_s, ptr %47, i32 0, i32 1
  store i32 %46, ptr %48, align 4, !tbaa !22
  %49 = load ptr, ptr %1, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.memblock_s, ptr %49, i32 0, i32 7
  store i32 -1073627409, ptr %50, align 8, !tbaa !23
  %51 = load ptr, ptr %1, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.memblock_s, ptr %51, i32 0, i32 0
  store i32 -1073627409, ptr %52, align 8, !tbaa !24
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  ret void
}

declare ptr @I_ZoneBase(ptr noundef) #2

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

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

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 = { "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 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !13, i64 8, !6, i64 56}
!13 = !{!"memblock_s", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !10, i64 40}
!14 = !{!12, !6, i64 40}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !6, i64 16}
!17 = !{!12, !10, i64 24}
!18 = !{!12, !6, i64 56}
!19 = !{!13, !6, i64 24}
!20 = !{!13, !6, i64 32}
!21 = !{!13, !6, i64 8}
!22 = !{!13, !10, i64 4}
!23 = !{!13, !10, i64 40}
!24 = !{!13, !10, i64 0}


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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @Z_Init() local_unnamed_addr #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %2 = call ptr @I_ZoneBase(ptr noundef nonnull %1) #5
  store ptr %2, ptr @mainzone, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  %4 = load i32, ptr %1, align 4, !tbaa !9
  br i1 %3, label %5, label %6

5:                                                ; preds = %0
  call void (ptr, ...) @I_Error(ptr noundef nonnull @.str, i32 noundef %4) #6
  unreachable

6:                                                ; preds = %0
  store i32 %4, ptr %2, align 8, !tbaa !11
  %7 = sext i32 %4 to i64
  call void @llvm.memset.p0.i64(ptr nonnull align 8 %2, i8 85, i64 %7, i1 false)
  %8 = load ptr, ptr @mainzone, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 64
  %10 = getelementptr inbounds i8, ptr %8, i64 40
  store ptr %9, ptr %10, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %8, i64 32
  store ptr %9, ptr %11, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %8, i64 16
  store ptr %8, ptr %12, align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %8, i64 24
  store i32 1, ptr %13, align 8, !tbaa !17
  %14 = getelementptr inbounds i8, ptr %8, i64 56
  store ptr %9, ptr %14, align 8, !tbaa !18
  %15 = getelementptr inbounds i8, ptr %8, i64 8
  %16 = getelementptr inbounds i8, ptr %8, i64 88
  store ptr %15, ptr %16, align 8, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %8, i64 96
  store ptr %15, ptr %17, align 8, !tbaa !20
  %18 = getelementptr inbounds i8, ptr %8, i64 72
  store ptr null, ptr %18, align 8, !tbaa !21
  %19 = load i32, ptr %8, align 8, !tbaa !11
  %20 = add i32 %19, -64
  %21 = getelementptr inbounds i8, ptr %8, i64 68
  store i32 %20, ptr %21, align 4, !tbaa !22
  %22 = getelementptr inbounds i8, ptr %8, i64 104
  store i32 -1073627409, ptr %22, align 8, !tbaa !23
  store i32 -1073627409, ptr %9, align 8, !tbaa !24
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret void
}

declare ptr @I_ZoneBase(ptr noundef) local_unnamed_addr #2

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

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

attributes #0 = { mustprogress 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 = { "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 = { 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !13, i64 8, !6, i64 56}
!13 = !{!"memblock_s", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !10, i64 40}
!14 = !{!12, !6, i64 40}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !6, i64 16}
!17 = !{!12, !10, i64 24}
!18 = !{!12, !6, i64 56}
!19 = !{!13, !6, i64 24}
!20 = !{!13, !6, i64 32}
!21 = !{!13, !6, i64 8}
!22 = !{!13, !10, i64 4}
!23 = !{!13, !10, i64 40}
!24 = !{!13, !10, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass