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

@spritelights = external dso_local global ptr, align 8
@validcount = external global i32, align 4
@extralight = external global i32, align 4
@scalelight = external global [16 x [48 x ptr]], align 16

; 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_ProjectSprite(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_AddSprites(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.sector_t, ptr %6, i32 0, i32 11
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = load i32, ptr @validcount, align 4, !tbaa !15
  %10 = icmp eq i32 %8, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 1, ptr %5, align 4
  br label %50

12:                                               ; preds = %1
  %13 = load i32, ptr @validcount, align 4, !tbaa !15
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.sector_t, ptr %14, i32 0, i32 11
  store i32 %13, ptr %15, align 8, !tbaa !9
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.sector_t, ptr %16, i32 0, i32 4
  %18 = load i16, ptr %17, align 4, !tbaa !16
  %19 = sext i16 %18 to i32
  %20 = ashr i32 %19, 4
  %21 = load i32, ptr @extralight, align 4, !tbaa !15
  %22 = add nsw i32 %20, %21
  store i32 %22, ptr %4, align 4, !tbaa !15
  %23 = load i32, ptr %4, align 4, !tbaa !15
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %12
  store ptr @scalelight, ptr @spritelights, align 8, !tbaa !5
  br label %36

26:                                               ; preds = %12
  %27 = load i32, ptr %4, align 4, !tbaa !15
  %28 = icmp sge i32 %27, 16
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  store ptr getelementptr inbounds ([16 x [48 x ptr]], ptr @scalelight, i64 0, i64 15), ptr @spritelights, align 8, !tbaa !5
  br label %35

30:                                               ; preds = %26
  %31 = load i32, ptr %4, align 4, !tbaa !15
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [16 x [48 x ptr]], ptr @scalelight, i64 0, i64 %32
  %34 = getelementptr inbounds [48 x ptr], ptr %33, i64 0, i64 0
  store ptr %34, ptr @spritelights, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %30, %29
  br label %36

36:                                               ; preds = %35, %25
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.sector_t, ptr %37, i32 0, i32 12
  %39 = load ptr, ptr %38, align 8, !tbaa !17
  store ptr %39, ptr %3, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %45, %36
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %49

43:                                               ; preds = %40
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  call void @R_ProjectSprite(ptr noundef %44)
  br label %45

45:                                               ; preds = %43
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mobj_s, ptr %46, i32 0, i32 4
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  store ptr %48, ptr %3, align 8, !tbaa !5
  br label %40, !llvm.loop !21

49:                                               ; preds = %40
  store i32 0, ptr %5, align 4
  br label %50

50:                                               ; preds = %49, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %51 = load i32, ptr %5, align 4
  switch i32 %51, label %53 [
    i32 0, label %52
    i32 1, label %52
  ]

52:                                               ; preds = %50, %50
  ret void

53:                                               ; preds = %50
  unreachable
}

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 = { 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, !11, i64 88}
!10 = !{!"", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 10, !12, i64 12, !12, i64 14, !12, i64 16, !11, i64 20, !6, i64 24, !7, i64 32, !13, i64 48, !11, i64 88, !6, i64 96, !6, i64 104, !11, i64 112, !6, i64 120}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"", !14, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!14 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !12, i64 12}
!17 = !{!10, !6, i64 96}
!18 = !{!19, !6, i64 40}
!19 = !{!"mobj_s", !14, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !6, i64 40, !6, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !11, i64 96, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !6, i64 136, !11, i64 144, !6, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !6, i64 176, !11, i64 184, !11, i64 188, !6, i64 192, !11, i64 200, !20, i64 204, !6, i64 216}
!20 = !{!"", !12, i64 0, !12, i64 2, !12, i64 4, !12, i64 6, !12, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@spritelights = external dso_local local_unnamed_addr global ptr, align 8
@validcount = external local_unnamed_addr global i32, align 4
@extralight = external local_unnamed_addr global i32, align 4
@scalelight = external global [16 x [48 x ptr]], align 16

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

; Function Attrs: nounwind uwtable
define dso_local void @R_AddSprites(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 88
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = load i32, ptr @validcount, align 4, !tbaa !14
  %5 = icmp eq i32 %3, %4
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %1
  store i32 %4, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  %8 = load i16, ptr %7, align 4, !tbaa !15
  %9 = ashr i16 %8, 4
  %10 = sext i16 %9 to i32
  %11 = load i32, ptr @extralight, align 4, !tbaa !14
  %12 = add nsw i32 %11, %10
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %19, label %14

14:                                               ; preds = %6
  %15 = icmp ugt i32 %12, 15
  br i1 %15, label %19, label %16

16:                                               ; preds = %14
  %17 = zext nneg i32 %12 to i64
  %18 = getelementptr inbounds [16 x [48 x ptr]], ptr @scalelight, i64 0, i64 %17
  br label %19

19:                                               ; preds = %14, %6, %16
  %.sink = phi ptr [ %18, %16 ], [ @scalelight, %6 ], [ getelementptr inbounds (i8, ptr @scalelight, i64 5760), %14 ]
  store ptr %.sink, ptr @spritelights, align 8, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %0, i64 96
  %.011 = load ptr, ptr %20, align 8, !tbaa !16
  %.not12 = icmp eq ptr %.011, null
  br i1 %.not12, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %19, %.lr.ph
  %.013 = phi ptr [ %.0, %.lr.ph ], [ %.011, %19 ]
  tail call void @R_ProjectSprite(ptr noundef nonnull %.013)
  %21 = getelementptr inbounds i8, ptr %.013, i64 40
  %.0 = load ptr, ptr %21, align 8, !tbaa !16
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !17

.loopexit:                                        ; preds = %.lr.ph, %19, %1
  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" }

!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, !7, i64 88}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 10, !10, i64 12, !10, i64 14, !10, i64 16, !7, i64 20, !11, i64 24, !8, i64 32, !12, i64 48, !7, i64 88, !11, i64 96, !11, i64 104, !7, i64 112, !11, i64 120}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"", !13, i64 0, !7, i64 24, !7, i64 28, !7, i64 32}
!13 = !{!"thinker_s", !11, i64 0, !11, i64 8, !8, i64 16}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !10, i64 12}
!16 = !{!11, !11, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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