; 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.mapsidedef_t = type { i16, i16, [8 x i8], [8 x i8], [8 x i8], i16 }
%struct.side_t = type { i32, i32, i16, i16, i16, ptr }
%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 }

@sides = external dso_local global ptr, align 8
@sectors = external dso_local global ptr, align 8
@numsides = external dso_local global i32, align 4

; 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_LoadSideDefs(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, 30
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr @numsides, align 4, !tbaa !5
  %12 = load i32, ptr @numsides, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul i64 %13, 24
  %15 = trunc i64 %14 to i32
  %16 = call ptr @Z_Malloc(i32 noundef %15, i32 noundef 50, ptr noundef null)
  store ptr %16, ptr @sides, align 8, !tbaa !9
  %17 = load ptr, ptr @sides, align 8, !tbaa !9
  %18 = load i32, ptr @numsides, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = mul i64 %19, 24
  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 @sides, 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 = %73, %1
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = load i32, ptr @numsides, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %80

29:                                               ; preds = %25
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.mapsidedef_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.side_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.mapsidedef_t, ptr %37, i32 0, i32 1
  %39 = load i16, ptr %38, align 2, !tbaa !16
  %40 = sext i16 %39 to i32
  %41 = shl i32 %40, 16
  %42 = load ptr, ptr %6, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.side_t, ptr %42, i32 0, i32 1
  store i32 %41, ptr %43, align 4, !tbaa !17
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.mapsidedef_t, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds [8 x i8], ptr %45, i64 0, i64 0
  %47 = call i32 @R_TextureNumForName(ptr noundef %46)
  %48 = trunc i32 %47 to i16
  %49 = load ptr, ptr %6, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.side_t, ptr %49, i32 0, i32 2
  store i16 %48, ptr %50, align 8, !tbaa !18
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.mapsidedef_t, ptr %51, i32 0, i32 3
  %53 = getelementptr inbounds [8 x i8], ptr %52, i64 0, i64 0
  %54 = call i32 @R_TextureNumForName(ptr noundef %53)
  %55 = trunc i32 %54 to i16
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.side_t, ptr %56, i32 0, i32 3
  store i16 %55, ptr %57, align 2, !tbaa !19
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.mapsidedef_t, ptr %58, i32 0, i32 4
  %60 = getelementptr inbounds [8 x i8], ptr %59, i64 0, i64 0
  %61 = call i32 @R_TextureNumForName(ptr noundef %60)
  %62 = trunc i32 %61 to i16
  %63 = load ptr, ptr %6, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.side_t, ptr %63, i32 0, i32 4
  store i16 %62, ptr %64, align 4, !tbaa !20
  %65 = load ptr, ptr @sectors, align 8, !tbaa !9
  %66 = load ptr, ptr %5, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.mapsidedef_t, ptr %66, i32 0, i32 5
  %68 = load i16, ptr %67, align 2, !tbaa !21
  %69 = sext i16 %68 to i64
  %70 = getelementptr inbounds %struct.sector_t, ptr %65, i64 %69
  %71 = load ptr, ptr %6, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.side_t, ptr %71, i32 0, i32 5
  store ptr %70, ptr %72, align 8, !tbaa !22
  br label %73

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

80:                                               ; preds = %25
  %81 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %81)
  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_TextureNumForName(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, !7, i64 20, !13, i64 28}
!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, !10, i64 16}
!16 = !{!12, !13, i64 2}
!17 = !{!15, !6, i64 4}
!18 = !{!15, !13, i64 8}
!19 = !{!15, !13, i64 10}
!20 = !{!15, !13, i64 12}
!21 = !{!12, !13, i64 28}
!22 = !{!15, !10, i64 16}
!23 = distinct !{!23, !24}
!24 = !{!"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"

%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 }

@sides = external dso_local local_unnamed_addr global ptr, align 8
@sectors = external dso_local local_unnamed_addr global ptr, align 8
@numsides = external dso_local local_unnamed_addr global i32, align 4

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_LoadSideDefs(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, 30
  %5 = trunc i64 %4 to i32
  store i32 %5, ptr @numsides, align 4, !tbaa !5
  %6 = mul i32 %5, 24
  %7 = tail call ptr @Z_Malloc(i32 noundef %6, i32 noundef 50, ptr noundef null) #3
  store ptr %7, ptr @sides, align 8, !tbaa !9
  %8 = load i32, ptr @numsides, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = mul nsw i64 %9, 24
  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 @numsides, 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 @sides, align 8, !tbaa !9
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.023 = phi ptr [ %38, %.lr.ph ], [ %14, %.lr.ph.preheader ]
  %.01922 = phi ptr [ %37, %.lr.ph ], [ %11, %.lr.ph.preheader ]
  %.02021 = phi i32 [ %36, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %15 = load <2 x i16>, ptr %.01922, 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 %.023, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.01922, i64 4
  %19 = tail call i32 @R_TextureNumForName(ptr noundef nonnull %18) #3
  %20 = trunc i32 %19 to i16
  %21 = getelementptr inbounds i8, ptr %.023, i64 8
  store i16 %20, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %.01922, i64 12
  %23 = tail call i32 @R_TextureNumForName(ptr noundef nonnull %22) #3
  %24 = trunc i32 %23 to i16
  %25 = getelementptr inbounds i8, ptr %.023, i64 10
  store i16 %24, ptr %25, align 2, !tbaa !15
  %26 = getelementptr inbounds i8, ptr %.01922, i64 20
  %27 = tail call i32 @R_TextureNumForName(ptr noundef nonnull %26) #3
  %28 = trunc i32 %27 to i16
  %29 = getelementptr inbounds i8, ptr %.023, i64 12
  store i16 %28, ptr %29, align 4, !tbaa !16
  %30 = load ptr, ptr @sectors, align 8, !tbaa !9
  %31 = getelementptr inbounds i8, ptr %.01922, i64 28
  %32 = load i16, ptr %31, align 2, !tbaa !17
  %33 = sext i16 %32 to i64
  %34 = getelementptr inbounds %struct.sector_t, ptr %30, i64 %33
  %35 = getelementptr inbounds i8, ptr %.023, i64 16
  store ptr %34, ptr %35, align 8, !tbaa !19
  %36 = add nuw nsw i32 %.02021, 1
  %37 = getelementptr inbounds i8, ptr %.01922, i64 30
  %38 = getelementptr inbounds i8, ptr %.023, i64 24
  %39 = load i32, ptr @numsides, align 4, !tbaa !5
  %40 = icmp slt i32 %36, %39
  br i1 %40, label %.lr.ph, label %._crit_edge, !llvm.loop !20

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

declare i32 @R_TextureNumForName(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, !10, i64 16}
!15 = !{!14, !12, i64 10}
!16 = !{!14, !12, i64 12}
!17 = !{!18, !12, i64 28}
!18 = !{!"", !12, i64 0, !12, i64 2, !7, i64 4, !7, i64 12, !7, i64 20, !12, i64 28}
!19 = !{!14, !10, i64 16}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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