; 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.mapsector_t = type { i16, i16, [8 x i8], [8 x i8], i16, i16, i16 }
%struct.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@numsectors = external dso_local global i32, align 4
@sectors = 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_LoadSectors(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, 26
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr @numsectors, align 4, !tbaa !5
  %12 = load i32, ptr @numsectors, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul i64 %13, 128
  %15 = trunc i64 %14 to i32
  %16 = call ptr @Z_Malloc(i32 noundef %15, i32 noundef 50, ptr noundef null)
  store ptr %16, ptr @sectors, align 8, !tbaa !9
  %17 = load ptr, ptr @sectors, align 8, !tbaa !9
  %18 = load i32, ptr @numsectors, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = mul i64 %19, 128
  call void @llvm.memset.p0.i64(ptr align 8 %17, i8 0, i64 %20, i1 false)
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = call ptr @W_CacheLumpNum(i32 noundef %21, i32 noundef 1)
  store ptr %22, ptr %3, align 8, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %23, ptr %5, align 8, !tbaa !9
  %24 = load ptr, ptr @sectors, 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 = %75, %1
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = load i32, ptr @numsectors, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %82

29:                                               ; preds = %25
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.mapsector_t, ptr %30, i32 0, i32 0
  %32 = load i16, ptr %31, align 2, !tbaa !11
  %33 = sext i16 %32 to i32
  %34 = shl i32 %33, 16
  %35 = load ptr, ptr %6, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.sector_t, ptr %35, i32 0, i32 0
  store i32 %34, ptr %36, align 8, !tbaa !14
  %37 = load ptr, ptr %5, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.mapsector_t, ptr %37, i32 0, i32 1
  %39 = load i16, ptr %38, align 2, !tbaa !18
  %40 = sext i16 %39 to i32
  %41 = shl i32 %40, 16
  %42 = load ptr, ptr %6, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.sector_t, ptr %42, i32 0, i32 1
  store i32 %41, ptr %43, align 4, !tbaa !19
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.mapsector_t, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds [8 x i8], ptr %45, i64 0, i64 0
  %47 = call i32 @R_FlatNumForName(ptr noundef %46)
  %48 = trunc i32 %47 to i16
  %49 = load ptr, ptr %6, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.sector_t, ptr %49, i32 0, i32 2
  store i16 %48, ptr %50, align 8, !tbaa !20
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.mapsector_t, ptr %51, i32 0, i32 3
  %53 = getelementptr inbounds [8 x i8], ptr %52, i64 0, i64 0
  %54 = call i32 @R_FlatNumForName(ptr noundef %53)
  %55 = trunc i32 %54 to i16
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.sector_t, ptr %56, i32 0, i32 3
  store i16 %55, ptr %57, align 2, !tbaa !21
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.mapsector_t, ptr %58, i32 0, i32 4
  %60 = load i16, ptr %59, align 2, !tbaa !22
  %61 = load ptr, ptr %6, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.sector_t, ptr %61, i32 0, i32 4
  store i16 %60, ptr %62, align 4, !tbaa !23
  %63 = load ptr, ptr %5, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.mapsector_t, ptr %63, i32 0, i32 5
  %65 = load i16, ptr %64, align 2, !tbaa !24
  %66 = load ptr, ptr %6, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.sector_t, ptr %66, i32 0, i32 5
  store i16 %65, ptr %67, align 2, !tbaa !25
  %68 = load ptr, ptr %5, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.mapsector_t, ptr %68, i32 0, i32 6
  %70 = load i16, ptr %69, align 2, !tbaa !26
  %71 = load ptr, ptr %6, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.sector_t, ptr %71, i32 0, i32 6
  store i16 %70, ptr %72, align 8, !tbaa !27
  %73 = load ptr, ptr %6, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.sector_t, ptr %73, i32 0, i32 12
  store ptr null, ptr %74, align 8, !tbaa !28
  br label %75

75:                                               ; preds = %29
  %76 = load i32, ptr %4, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %4, align 4, !tbaa !5
  %78 = load ptr, ptr %6, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.sector_t, ptr %78, i32 1
  store ptr %79, ptr %6, align 8, !tbaa !9
  %80 = load ptr, ptr %5, align 8, !tbaa !9
  %81 = getelementptr inbounds %struct.mapsector_t, ptr %80, i32 1
  store ptr %81, ptr %5, align 8, !tbaa !9
  br label %25, !llvm.loop !29

82:                                               ; preds = %25
  %83 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %83)
  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
}

declare i32 @R_FlatNumForName(ptr noundef) #1

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, !7, i64 4, !7, i64 12, !13, i64 20, !13, i64 22, !13, i64 24}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !6, i64 20, !10, i64 24, !7, i64 32, !16, i64 48, !6, i64 88, !10, i64 96, !10, i64 104, !6, i64 112, !10, i64 120}
!16 = !{!"", !17, i64 0, !6, i64 24, !6, i64 28, !6, i64 32}
!17 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!18 = !{!12, !13, i64 2}
!19 = !{!15, !6, i64 4}
!20 = !{!15, !13, i64 8}
!21 = !{!15, !13, i64 10}
!22 = !{!12, !13, i64 20}
!23 = !{!15, !13, i64 12}
!24 = !{!12, !13, i64 22}
!25 = !{!15, !13, i64 14}
!26 = !{!12, !13, i64 24}
!27 = !{!15, !13, i64 16}
!28 = !{!15, !10, i64 96}
!29 = distinct !{!29, !30}
!30 = !{!"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"

@numsectors = external dso_local local_unnamed_addr global i32, align 4
@sectors = 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_LoadSectors(i32 noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @W_LumpLength(i32 noundef %0) #3
  %3 = sext i32 %2 to i64
  %4 = udiv i64 %3, 26
  %5 = trunc i64 %4 to i32
  store i32 %5, ptr @numsectors, align 4, !tbaa !5
  %6 = shl i32 %5, 7
  %7 = tail call ptr @Z_Malloc(i32 noundef %6, i32 noundef 50, ptr noundef null) #3
  store ptr %7, ptr @sectors, align 8, !tbaa !9
  %8 = load i32, ptr @numsectors, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = shl nsw i64 %9, 7
  tail call void @llvm.memset.p0.i64(ptr align 8 %7, i8 0, i64 %10, i1 false)
  %11 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 1) #3
  %12 = load i32, ptr @numsectors, align 4, !tbaa !5
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %14 = load ptr, ptr @sectors, align 8, !tbaa !9
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.026 = phi ptr [ %34, %.lr.ph ], [ %14, %.lr.ph.preheader ]
  %.02225 = phi ptr [ %35, %.lr.ph ], [ %11, %.lr.ph.preheader ]
  %.02324 = phi i32 [ %33, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %15 = load <2 x i16>, ptr %.02225, align 2, !tbaa !11
  %16 = sext <2 x i16> %15 to <2 x i32>
  %17 = shl nsw <2 x i32> %16, <i32 16, i32 16>
  store <2 x i32> %17, ptr %.026, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.02225, i64 4
  %19 = tail call i32 @R_FlatNumForName(ptr noundef nonnull %18) #3
  %20 = trunc i32 %19 to i16
  %21 = getelementptr inbounds i8, ptr %.026, i64 8
  store i16 %20, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %.02225, i64 12
  %23 = tail call i32 @R_FlatNumForName(ptr noundef nonnull %22) #3
  %24 = trunc i32 %23 to i16
  %25 = getelementptr inbounds i8, ptr %.026, i64 10
  store i16 %24, ptr %25, align 2, !tbaa !17
  %26 = getelementptr inbounds i8, ptr %.02225, i64 20
  %27 = getelementptr inbounds i8, ptr %.026, i64 12
  %28 = load <2 x i16>, ptr %26, align 2, !tbaa !11
  store <2 x i16> %28, ptr %27, align 4, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %.02225, i64 24
  %30 = load i16, ptr %29, align 2, !tbaa !18
  %31 = getelementptr inbounds i8, ptr %.026, i64 16
  store i16 %30, ptr %31, align 8, !tbaa !20
  %32 = getelementptr inbounds i8, ptr %.026, i64 96
  store ptr null, ptr %32, align 8, !tbaa !21
  %33 = add nuw nsw i32 %.02324, 1
  %34 = getelementptr inbounds i8, ptr %.026, i64 128
  %35 = getelementptr inbounds i8, ptr %.02225, i64 26
  %36 = load i32, ptr @numsectors, align 4, !tbaa !5
  %37 = icmp slt i32 %33, %36
  br i1 %37, label %.lr.ph, label %._crit_edge, !llvm.loop !22

._crit_edge:                                      ; preds = %.lr.ph, %1
  tail call void @Z_Free(ptr noundef %11) #3
  ret void
}

declare i32 @R_FlatNumForName(ptr noundef) local_unnamed_addr #0

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 = !{!14, !12, i64 8}
!14 = !{!"", !6, i64 0, !6, i64 4, !12, i64 8, !12, i64 10, !12, i64 12, !12, i64 14, !12, i64 16, !6, i64 20, !10, i64 24, !7, i64 32, !15, i64 48, !6, i64 88, !10, i64 96, !10, i64 104, !6, i64 112, !10, i64 120}
!15 = !{!"", !16, i64 0, !6, i64 24, !6, i64 28, !6, i64 32}
!16 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!17 = !{!14, !12, i64 10}
!18 = !{!19, !12, i64 24}
!19 = !{!"", !12, i64 0, !12, i64 2, !7, i64 4, !7, i64 12, !12, i64 20, !12, i64 22, !12, i64 24}
!20 = !{!14, !12, i64 16}
!21 = !{!14, !10, i64 96}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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