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

; 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.vissprite_s = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32 }
%struct.drawseg_s = type { ptr, i32, i32, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr }

@vissprites = external dso_local global [128 x %struct.vissprite_s], align 16
@vissprite_p = external dso_local global ptr, align 8
@vsprsortedhead = external dso_local global %struct.vissprite_s, align 8
@ds_p = external global ptr, align 8
@drawsegs = external global [256 x %struct.drawseg_s], align 16
@viewangleoffset = external 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

; 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_DrawPlayerSprites() #1

; Function Attrs: nounwind uwtable
declare dso_local void @R_SortVisSprites() #1

; Function Attrs: nounwind uwtable
declare dso_local void @R_DrawSprite(ptr noundef) #1

declare void @R_RenderMaskedSegRange(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawMasked() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @R_SortVisSprites()
  %3 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %4 = icmp ugt ptr %3, @vissprites
  br i1 %4, label %5, label %17

5:                                                ; preds = %0
  %6 = load ptr, ptr getelementptr inbounds (%struct.vissprite_s, ptr @vsprsortedhead, i32 0, i32 1), align 8, !tbaa !9
  store ptr %6, ptr %1, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %12, %5
  %8 = load ptr, ptr %1, align 8, !tbaa !5
  %9 = icmp ne ptr %8, @vsprsortedhead
  br i1 %9, label %10, label %16

10:                                               ; preds = %7
  %11 = load ptr, ptr %1, align 8, !tbaa !5
  call void @R_DrawSprite(ptr noundef %11)
  br label %12

12:                                               ; preds = %10
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.vissprite_s, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %1, align 8, !tbaa !5
  br label %7, !llvm.loop !12

16:                                               ; preds = %7
  br label %17

17:                                               ; preds = %16, %0
  %18 = load ptr, ptr @ds_p, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.drawseg_s, ptr %18, i64 -1
  store ptr %19, ptr %2, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %37, %17
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = icmp uge ptr %21, @drawsegs
  br i1 %22, label %23, label %40

23:                                               ; preds = %20
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.drawseg_s, ptr %24, i32 0, i32 11
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %36

28:                                               ; preds = %23
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.drawseg_s, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 8, !tbaa !16
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.drawseg_s, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !17
  call void @R_RenderMaskedSegRange(ptr noundef %29, i32 noundef %32, i32 noundef %35)
  br label %36

36:                                               ; preds = %28, %23
  br label %37

37:                                               ; preds = %36
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.drawseg_s, ptr %38, i32 -1
  store ptr %39, ptr %2, align 8, !tbaa !5
  br label %20, !llvm.loop !18

40:                                               ; preds = %20
  %41 = load i32, ptr @viewangleoffset, align 4, !tbaa !19
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %44, label %43

43:                                               ; preds = %40
  call void @R_DrawPlayerSprites()
  br label %44

44:                                               ; preds = %43, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

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 = { 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, !6, i64 8}
!10 = !{!"vissprite_s", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !6, i64 64, !11, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 56}
!15 = !{!"drawseg_s", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !6, i64 40, !6, i64 48, !6, i64 56}
!16 = !{!15, !11, i64 8}
!17 = !{!15, !11, i64 12}
!18 = distinct !{!18, !13}
!19 = !{!11, !11, 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"

%struct.vissprite_s = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32 }
%struct.drawseg_s = type { ptr, i32, i32, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr }

@vissprites = external dso_local global [128 x %struct.vissprite_s], align 16
@vissprite_p = external dso_local local_unnamed_addr global ptr, align 8
@vsprsortedhead = external dso_local global %struct.vissprite_s, align 8
@ds_p = external local_unnamed_addr global ptr, align 8
@drawsegs = external global [256 x %struct.drawseg_s], align 16
@viewangleoffset = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @R_DrawPlayerSprites() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @R_SortVisSprites() local_unnamed_addr #0

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

declare void @R_RenderMaskedSegRange(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawMasked() local_unnamed_addr #0 {
  tail call void @R_SortVisSprites()
  %1 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %2 = icmp ule ptr %1, @vissprites
  %.0913 = load ptr, ptr getelementptr inbounds (i8, ptr @vsprsortedhead, i64 8), align 8
  %.not14 = icmp eq ptr %.0913, @vsprsortedhead
  %or.cond = select i1 %2, i1 true, i1 %.not14
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.0915 = phi ptr [ %.09, %.lr.ph ], [ %.0913, %0 ]
  tail call void @R_DrawSprite(ptr noundef %.0915)
  %3 = getelementptr inbounds i8, ptr %.0915, i64 8
  %.09 = load ptr, ptr %3, align 8, !tbaa !9
  %.not = icmp eq ptr %.09, @vsprsortedhead
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !12

.loopexit:                                        ; preds = %.lr.ph, %0
  %4 = load ptr, ptr @ds_p, align 8, !tbaa !5
  %.016 = getelementptr inbounds i8, ptr %4, i64 -64
  %.not1017 = icmp ult ptr %.016, @drawsegs
  br i1 %.not1017, label %._crit_edge, label %.lr.ph20

.lr.ph20:                                         ; preds = %.loopexit, %12
  %.019 = phi ptr [ %.0, %12 ], [ %.016, %.loopexit ]
  %.pn18 = phi ptr [ %.019, %12 ], [ %4, %.loopexit ]
  %5 = getelementptr inbounds i8, ptr %.pn18, i64 -8
  %6 = load ptr, ptr %5, align 8, !tbaa !14
  %.not12 = icmp eq ptr %6, null
  br i1 %.not12, label %12, label %7

7:                                                ; preds = %.lr.ph20
  %8 = getelementptr inbounds i8, ptr %.pn18, i64 -56
  %9 = load i32, ptr %8, align 8, !tbaa !16
  %10 = getelementptr inbounds i8, ptr %.pn18, i64 -52
  %11 = load i32, ptr %10, align 4, !tbaa !17
  tail call void @R_RenderMaskedSegRange(ptr noundef nonnull %.019, i32 noundef %9, i32 noundef %11) #2
  br label %12

12:                                               ; preds = %.lr.ph20, %7
  %.0 = getelementptr inbounds i8, ptr %.019, i64 -64
  %.not10 = icmp ult ptr %.0, @drawsegs
  br i1 %.not10, label %._crit_edge, label %.lr.ph20, !llvm.loop !18

._crit_edge:                                      ; preds = %12, %.loopexit
  %13 = load i32, ptr @viewangleoffset, align 4, !tbaa !19
  %.not11 = icmp eq i32 %13, 0
  br i1 %.not11, label %14, label %15

14:                                               ; preds = %._crit_edge
  tail call void @R_DrawPlayerSprites()
  br label %15

15:                                               ; preds = %14, %._crit_edge
  ret void
}

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 = { "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 = { 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, !6, i64 8}
!10 = !{!"vissprite_s", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !6, i64 64, !11, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 56}
!15 = !{!"drawseg_s", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !6, i64 40, !6, i64 48, !6, i64 56}
!16 = !{!15, !11, i64 8}
!17 = !{!15, !11, i64 12}
!18 = distinct !{!18, !13}
!19 = !{!11, !11, i64 0}


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