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

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

@frontsector = external dso_local global ptr, align 8
@numsubsectors = external global i32, align 4
@.str = external hidden unnamed_addr constant [35 x i8], align 1
@sscount = external global i32, align 4
@subsectors = external global ptr, align 8
@segs = external global ptr, align 8
@viewz = external global i32, align 4
@floorplane = external global ptr, align 8
@skyflatnum = external global i32, align 4
@ceilingplane = external 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

; 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
declare dso_local void @R_AddLine(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_Subsector(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %8 = icmp sge i32 %6, %7
  br i1 %8, label %9, label %12

9:                                                ; preds = %1
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  call void (ptr, ...) @I_Error(ptr noundef @.str, i32 noundef %10, i32 noundef %11) #5
  unreachable

12:                                               ; preds = %1
  %13 = load i32, ptr @sscount, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @sscount, align 4, !tbaa !5
  %15 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %struct.subsector_s, ptr %15, i64 %17
  store ptr %18, ptr %5, align 8, !tbaa !9
  %19 = load ptr, ptr %5, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.subsector_s, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  store ptr %21, ptr @frontsector, align 8, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.subsector_s, ptr %22, i32 0, i32 1
  %24 = load i16, ptr %23, align 8, !tbaa !14
  %25 = sext i16 %24 to i32
  store i32 %25, ptr %3, align 4, !tbaa !5
  %26 = load ptr, ptr @segs, align 8, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.subsector_s, ptr %27, i32 0, i32 2
  %29 = load i16, ptr %28, align 2, !tbaa !15
  %30 = sext i16 %29 to i64
  %31 = getelementptr inbounds %struct.seg_t, ptr %26, i64 %30
  store ptr %31, ptr %4, align 8, !tbaa !9
  %32 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.sector_t, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !16
  %35 = load i32, ptr @viewz, align 4, !tbaa !5
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %37, label %50

37:                                               ; preds = %12
  %38 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.sector_t, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !16
  %41 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.sector_t, ptr %41, i32 0, i32 2
  %43 = load i16, ptr %42, align 8, !tbaa !20
  %44 = sext i16 %43 to i32
  %45 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.sector_t, ptr %45, i32 0, i32 4
  %47 = load i16, ptr %46, align 4, !tbaa !21
  %48 = sext i16 %47 to i32
  %49 = call ptr @R_FindPlane(i32 noundef %40, i32 noundef %44, i32 noundef %48)
  store ptr %49, ptr @floorplane, align 8, !tbaa !9
  br label %51

50:                                               ; preds = %12
  store ptr null, ptr @floorplane, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %50, %37
  %52 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.sector_t, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !22
  %55 = load i32, ptr @viewz, align 4, !tbaa !5
  %56 = icmp sgt i32 %54, %55
  br i1 %56, label %64, label %57

57:                                               ; preds = %51
  %58 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.sector_t, ptr %58, i32 0, i32 3
  %60 = load i16, ptr %59, align 2, !tbaa !23
  %61 = sext i16 %60 to i32
  %62 = load i32, ptr @skyflatnum, align 4, !tbaa !5
  %63 = icmp eq i32 %61, %62
  br i1 %63, label %64, label %77

64:                                               ; preds = %57, %51
  %65 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.sector_t, ptr %65, i32 0, i32 1
  %67 = load i32, ptr %66, align 4, !tbaa !22
  %68 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.sector_t, ptr %68, i32 0, i32 3
  %70 = load i16, ptr %69, align 2, !tbaa !23
  %71 = sext i16 %70 to i32
  %72 = load ptr, ptr @frontsector, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.sector_t, ptr %72, i32 0, i32 4
  %74 = load i16, ptr %73, align 4, !tbaa !21
  %75 = sext i16 %74 to i32
  %76 = call ptr @R_FindPlane(i32 noundef %67, i32 noundef %71, i32 noundef %75)
  store ptr %76, ptr @ceilingplane, align 8, !tbaa !9
  br label %78

77:                                               ; preds = %57
  store ptr null, ptr @ceilingplane, align 8, !tbaa !9
  br label %78

78:                                               ; preds = %77, %64
  %79 = load ptr, ptr @frontsector, align 8, !tbaa !9
  call void @R_AddSprites(ptr noundef %79)
  br label %80

80:                                               ; preds = %84, %78
  %81 = load i32, ptr %3, align 4, !tbaa !5
  %82 = add nsw i32 %81, -1
  store i32 %82, ptr %3, align 4, !tbaa !5
  %83 = icmp ne i32 %81, 0
  br i1 %83, label %84, label %88

84:                                               ; preds = %80
  %85 = load ptr, ptr %4, align 8, !tbaa !9
  call void @R_AddLine(ptr noundef %85)
  %86 = load ptr, ptr %4, align 8, !tbaa !9
  %87 = getelementptr inbounds %struct.seg_t, ptr %86, i32 1
  store ptr %87, ptr %4, align 8, !tbaa !9
  br label %80, !llvm.loop !24

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

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

declare ptr @R_FindPlane(i32 noundef, i32 noundef, i32 noundef) #3

declare void @R_AddSprites(ptr noundef) #3

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 = { 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 #3 = { "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 }
attributes #5 = { 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 = !{!"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, !10, i64 0}
!12 = !{!"subsector_s", !10, i64 0, !13, i64 8, !13, i64 10}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!12, !13, i64 10}
!16 = !{!17, !6, i64 0}
!17 = !{!"", !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, !18, i64 48, !6, i64 88, !10, i64 96, !10, i64 104, !6, i64 112, !10, i64 120}
!18 = !{!"", !19, i64 0, !6, i64 24, !6, i64 28, !6, i64 32}
!19 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!20 = !{!17, !13, i64 8}
!21 = !{!17, !13, i64 12}
!22 = !{!17, !6, i64 4}
!23 = !{!17, !13, i64 10}
!24 = distinct !{!24, !25}
!25 = !{!"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.subsector_s = type { ptr, i16, i16 }
%struct.seg_t = type { ptr, ptr, i32, i32, ptr, ptr, ptr, ptr }

@frontsector = external dso_local local_unnamed_addr global ptr, align 8
@numsubsectors = external local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [35 x i8], align 1
@sscount = external local_unnamed_addr global i32, align 4
@subsectors = external local_unnamed_addr global ptr, align 8
@segs = external local_unnamed_addr global ptr, align 8
@viewz = external local_unnamed_addr global i32, align 4
@floorplane = external local_unnamed_addr global ptr, align 8
@skyflatnum = external local_unnamed_addr global i32, align 4
@ceilingplane = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @R_AddLine(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @R_Subsector(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @numsubsectors, align 4, !tbaa !5
  %.not = icmp sgt i32 %2, %0
  br i1 %.not, label %4, label %3

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

4:                                                ; preds = %1
  %5 = load i32, ptr @sscount, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  store i32 %6, ptr @sscount, align 4, !tbaa !5
  %7 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %8 = sext i32 %0 to i64
  %9 = getelementptr inbounds %struct.subsector_s, ptr %7, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  store ptr %10, ptr @frontsector, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load i16, ptr %11, align 8, !tbaa !14
  %13 = sext i16 %12 to i32
  %14 = load ptr, ptr @segs, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %9, i64 10
  %16 = load i16, ptr %15, align 2, !tbaa !15
  %17 = sext i16 %16 to i64
  %18 = getelementptr inbounds %struct.seg_t, ptr %14, i64 %17
  %19 = load i32, ptr %10, align 8, !tbaa !16
  %20 = load i32, ptr @viewz, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %30

22:                                               ; preds = %4
  %23 = getelementptr inbounds i8, ptr %10, i64 8
  %24 = load i16, ptr %23, align 8, !tbaa !20
  %25 = sext i16 %24 to i32
  %26 = getelementptr inbounds i8, ptr %10, i64 12
  %27 = load i16, ptr %26, align 4, !tbaa !21
  %28 = sext i16 %27 to i32
  %29 = tail call ptr @R_FindPlane(i32 noundef %19, i32 noundef %25, i32 noundef %28) #4
  %.pre = load ptr, ptr @frontsector, align 8, !tbaa !9
  %.pre17 = load i32, ptr @viewz, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %4, %22
  %31 = phi i32 [ %.pre17, %22 ], [ %20, %4 ]
  %32 = phi ptr [ %.pre, %22 ], [ %10, %4 ]
  %storemerge = phi ptr [ %29, %22 ], [ null, %4 ]
  store ptr %storemerge, ptr @floorplane, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i64 4
  %34 = load i32, ptr %33, align 4, !tbaa !22
  %35 = icmp sgt i32 %34, %31
  %.phi.trans.insert = getelementptr inbounds i8, ptr %32, i64 10
  %.pre19 = load i16, ptr %.phi.trans.insert, align 2, !tbaa !23
  %.pre21 = sext i16 %.pre19 to i32
  %36 = load i32, ptr @skyflatnum, align 4
  %37 = icmp eq i32 %36, %.pre21
  %or.cond = select i1 %35, i1 true, i1 %37
  br i1 %or.cond, label %._crit_edge18, label %42

._crit_edge18:                                    ; preds = %30
  %38 = getelementptr inbounds i8, ptr %32, i64 12
  %39 = load i16, ptr %38, align 4, !tbaa !21
  %40 = sext i16 %39 to i32
  %41 = tail call ptr @R_FindPlane(i32 noundef %34, i32 noundef %.pre21, i32 noundef %40) #4
  %.pre20 = load ptr, ptr @frontsector, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %30, %._crit_edge18
  %43 = phi ptr [ %.pre20, %._crit_edge18 ], [ %32, %30 ]
  %storemerge12 = phi ptr [ %41, %._crit_edge18 ], [ null, %30 ]
  store ptr %storemerge12, ptr @ceilingplane, align 8, !tbaa !9
  tail call void @R_AddSprites(ptr noundef %43) #4
  %.not1314 = icmp eq i16 %12, 0
  br i1 %.not1314, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %42, %.lr.ph
  %.016 = phi i32 [ %44, %.lr.ph ], [ %13, %42 ]
  %.0815 = phi ptr [ %45, %.lr.ph ], [ %18, %42 ]
  %44 = add nsw i32 %.016, -1
  tail call void @R_AddLine(ptr noundef %.0815)
  %45 = getelementptr inbounds i8, ptr %.0815, i64 56
  %.not13 = icmp eq i32 %44, 0
  br i1 %.not13, label %._crit_edge, label %.lr.ph, !llvm.loop !24

._crit_edge:                                      ; preds = %.lr.ph, %42
  ret void
}

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

declare ptr @R_FindPlane(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

declare void @R_AddSprites(ptr noundef) local_unnamed_addr #2

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


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