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

; 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.mapsubsector_t = type { i16, i16 }
%struct.subsector_s = type { ptr, i16, i16 }

@numsubsectors = external dso_local global i32, align 4
@subsectors = 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

declare i32 @W_LumpLength(i32 noundef) #1

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) #1

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) #1

declare void @Z_Free(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadSubsectors(i32 noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = call i32 @W_LumpLength(i32 noundef %7)
  %9 = sext i32 %8 to i64
  %10 = udiv i64 %9, 4
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr @numsubsectors, align 4, !tbaa !5
  %12 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul i64 %13, 16
  %15 = trunc i64 %14 to i32
  %16 = call ptr @Z_Malloc(i32 noundef %15, i32 noundef 50, ptr noundef null)
  store ptr %16, ptr @subsectors, align 8, !tbaa !9
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = call ptr @W_CacheLumpNum(i32 noundef %17, i32 noundef 1)
  store ptr %18, ptr %3, align 8, !tbaa !9
  %19 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %21 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = mul i64 %22, 16
  call void @llvm.memset.p0.i64(ptr align 8 %20, i8 0, i64 %23, i1 false)
  %24 = load ptr, ptr @subsectors, align 8, !tbaa !9
  store ptr %24, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %40, %1
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %47

29:                                               ; preds = %25
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.mapsubsector_t, ptr %30, i32 0, i32 0
  %32 = load i16, ptr %31, align 2, !tbaa !11
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.subsector_s, ptr %33, i32 0, i32 1
  store i16 %32, ptr %34, align 8, !tbaa !14
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.mapsubsector_t, ptr %35, i32 0, i32 1
  %37 = load i16, ptr %36, align 2, !tbaa !16
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.subsector_s, ptr %38, i32 0, i32 2
  store i16 %37, ptr %39, align 2, !tbaa !17
  br label %40

40:                                               ; preds = %29
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %4, align 4, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.subsector_s, ptr %43, i32 1
  store ptr %44, ptr %6, align 8, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.mapsubsector_t, ptr %45, i32 1
  store ptr %46, ptr %5, align 8, !tbaa !9
  br label %25, !llvm.loop !18

47:                                               ; preds = %25
  %48 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %48)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #4 = { 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, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"subsector_s", !10, i64 0, !13, i64 8, !13, i64 10}
!16 = !{!12, !13, i64 2}
!17 = !{!15, !13, i64 10}
!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"

@numsubsectors = external dso_local local_unnamed_addr global i32, align 4
@subsectors = external dso_local local_unnamed_addr global ptr, align 8

declare i32 @W_LumpLength(i32 noundef) local_unnamed_addr #0

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @Z_Free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadSubsectors(i32 noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @W_LumpLength(i32 noundef %0) #3
  %3 = ashr i32 %2, 2
  store i32 %3, ptr @numsubsectors, align 4, !tbaa !5
  %4 = shl i32 %3, 4
  %5 = tail call ptr @Z_Malloc(i32 noundef %4, i32 noundef 50, ptr noundef null) #3
  store ptr %5, ptr @subsectors, align 8, !tbaa !9
  %6 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 1) #3
  %7 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %8 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = shl nsw i64 %9, 4
  tail call void @llvm.memset.p0.i64(ptr align 8 %7, i8 0, i64 %10, i1 false)
  %11 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %13 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %xtraiter = and i32 %11, 7
  %14 = icmp ult i32 %11, 8
  br i1 %14, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %11, 2147483640
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.015 = phi ptr [ %13, %.lr.ph.preheader.new ], [ %38, %.lr.ph ]
  %.01114 = phi ptr [ %6, %.lr.ph.preheader.new ], [ %39, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %15 = getelementptr inbounds i8, ptr %.015, i64 8
  %16 = load <2 x i16>, ptr %.01114, align 2, !tbaa !11
  store <2 x i16> %16, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %.01114, i64 4
  %18 = getelementptr inbounds i8, ptr %.015, i64 24
  %19 = load <2 x i16>, ptr %17, align 2, !tbaa !11
  store <2 x i16> %19, ptr %18, align 8, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %.01114, i64 8
  %21 = getelementptr inbounds i8, ptr %.015, i64 40
  %22 = load <2 x i16>, ptr %20, align 2, !tbaa !11
  store <2 x i16> %22, ptr %21, align 8, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %.01114, i64 12
  %24 = getelementptr inbounds i8, ptr %.015, i64 56
  %25 = load <2 x i16>, ptr %23, align 2, !tbaa !11
  store <2 x i16> %25, ptr %24, align 8, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %.01114, i64 16
  %27 = getelementptr inbounds i8, ptr %.015, i64 72
  %28 = load <2 x i16>, ptr %26, align 2, !tbaa !11
  store <2 x i16> %28, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %.01114, i64 20
  %30 = getelementptr inbounds i8, ptr %.015, i64 88
  %31 = load <2 x i16>, ptr %29, align 2, !tbaa !11
  store <2 x i16> %31, ptr %30, align 8, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %.01114, i64 24
  %33 = getelementptr inbounds i8, ptr %.015, i64 104
  %34 = load <2 x i16>, ptr %32, align 2, !tbaa !11
  store <2 x i16> %34, ptr %33, align 8, !tbaa !11
  %35 = getelementptr inbounds i8, ptr %.01114, i64 28
  %36 = getelementptr inbounds i8, ptr %.015, i64 120
  %37 = load <2 x i16>, ptr %35, align 2, !tbaa !11
  store <2 x i16> %37, ptr %36, align 8, !tbaa !11
  %38 = getelementptr inbounds i8, ptr %.015, i64 128
  %39 = getelementptr inbounds i8, ptr %.01114, i64 32
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !13

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.015.unr = phi ptr [ %13, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %.01114.unr = phi ptr [ %6, %.lr.ph.preheader ], [ %39, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.015.epil = phi ptr [ %42, %.lr.ph.epil ], [ %.015.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01114.epil = phi ptr [ %43, %.lr.ph.epil ], [ %.01114.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %40 = getelementptr inbounds i8, ptr %.015.epil, i64 8
  %41 = load <2 x i16>, ptr %.01114.epil, align 2, !tbaa !11
  store <2 x i16> %41, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds i8, ptr %.015.epil, i64 16
  %43 = getelementptr inbounds i8, ptr %.01114.epil, i64 4
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !15

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %1
  tail call void @Z_Free(ptr noundef %6) #3
  ret void
}

attributes #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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 }

!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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass