; 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.player_s = type { ptr, i32, %struct.ticcmd_t, i32, i32, i32, i32, i32, i32, i32, [6 x i32], [6 x i32], i32, [4 x i32], i32, i32, [9 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, i32, i32, [2 x %struct.pspdef_t], i32 }
%struct.ticcmd_t = type { i8, i8, i16, i16, i8, i8 }
%struct.pspdef_t = type { ptr, i32, i32, i32 }
%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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.mapthing_t = type { i16, i16, i16, i16, i16 }
%struct.subsector_s = type { ptr, 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 }

@negonearray = external dso_local global [320 x i16], align 16
@mfloorclip = external dso_local global ptr, align 8
@mceilingclip = external dso_local global ptr, align 8
@spritelights = external dso_local global ptr, align 8
@extralight = external global i32, align 4
@scalelight = external global [16 x [48 x ptr]], align 16
@viewplayer = external global ptr, align 8
@screenheightarray = external dso_local global [320 x i16], 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_DrawPSprite(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawPlayerSprites() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %4 = load ptr, ptr @viewplayer, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.player_s, ptr %4, i32 0, i32 0
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds %struct.mobj_s, ptr %6, i32 0, i32 11
  %8 = load ptr, ptr %7, align 8, !tbaa !14
  %9 = getelementptr inbounds %struct.subsector_s, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !18
  %11 = getelementptr inbounds %struct.sector_t, ptr %10, i32 0, i32 4
  %12 = load i16, ptr %11, align 4, !tbaa !20
  %13 = sext i16 %12 to i32
  %14 = ashr i32 %13, 4
  %15 = load i32, ptr @extralight, align 4, !tbaa !23
  %16 = add nsw i32 %14, %15
  store i32 %16, ptr %2, align 4, !tbaa !23
  %17 = load i32, ptr %2, align 4, !tbaa !23
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %0
  store ptr @scalelight, ptr @spritelights, align 8, !tbaa !5
  br label %30

20:                                               ; preds = %0
  %21 = load i32, ptr %2, align 4, !tbaa !23
  %22 = icmp sge i32 %21, 16
  br i1 %22, label %23, label %24

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

24:                                               ; preds = %20
  %25 = load i32, ptr %2, align 4, !tbaa !23
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [16 x [48 x ptr]], ptr @scalelight, i64 0, i64 %26
  %28 = getelementptr inbounds [48 x ptr], ptr %27, i64 0, i64 0
  store ptr %28, ptr @spritelights, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %24, %23
  br label %30

30:                                               ; preds = %29, %19
  store ptr @screenheightarray, ptr @mfloorclip, align 8, !tbaa !5
  store ptr @negonearray, ptr @mceilingclip, align 8, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !23
  %31 = load ptr, ptr @viewplayer, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.player_s, ptr %31, i32 0, i32 33
  %33 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %32, i64 0, i64 0
  store ptr %33, ptr %3, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %45, %30
  %35 = load i32, ptr %1, align 4, !tbaa !23
  %36 = icmp slt i32 %35, 2
  br i1 %36, label %37, label %50

37:                                               ; preds = %34
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.pspdef_t, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !24
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %44

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

44:                                               ; preds = %42, %37
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %1, align 4, !tbaa !23
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %1, align 4, !tbaa !23
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.pspdef_t, ptr %48, i32 1
  store ptr %49, ptr %3, align 8, !tbaa !5
  br label %34, !llvm.loop !26

50:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  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 = { 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 0}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 88}
!15 = !{!"mobj_s", !16, 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, !17, i64 204, !6, i64 216}
!16 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!17 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!18 = !{!19, !6, i64 0}
!19 = !{!"subsector_s", !6, i64 0, !13, i64 8, !13, i64 10}
!20 = !{!21, !13, i64 12}
!21 = !{!"", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !11, i64 20, !6, i64 24, !7, i64 32, !22, i64 48, !11, i64 88, !6, i64 96, !6, i64 104, !11, i64 112, !6, i64 120}
!22 = !{!"", !16, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!23 = !{!11, !11, i64 0}
!24 = !{!25, !6, i64 0}
!25 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16}
!26 = distinct !{!26, !27}
!27 = !{!"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"

@negonearray = external dso_local global [320 x i16], align 16
@mfloorclip = external dso_local local_unnamed_addr global ptr, align 8
@mceilingclip = external dso_local local_unnamed_addr global ptr, align 8
@spritelights = external dso_local local_unnamed_addr global ptr, align 8
@extralight = external local_unnamed_addr global i32, align 4
@scalelight = external global [16 x [48 x ptr]], align 16
@viewplayer = external local_unnamed_addr global ptr, align 8
@screenheightarray = external dso_local global [320 x i16], align 16

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

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawPlayerSprites() local_unnamed_addr #0 {
  %1 = load ptr, ptr @viewplayer, align 8, !tbaa !5
  %2 = load ptr, ptr %1, align 8, !tbaa !9
  %3 = getelementptr inbounds i8, ptr %2, i64 88
  %4 = load ptr, ptr %3, align 8, !tbaa !14
  %5 = load ptr, ptr %4, align 8, !tbaa !18
  %6 = getelementptr inbounds i8, ptr %5, i64 12
  %7 = load i16, ptr %6, align 4, !tbaa !20
  %8 = ashr i16 %7, 4
  %9 = sext i16 %8 to i32
  %10 = load i32, ptr @extralight, align 4, !tbaa !23
  %11 = add nsw i32 %10, %9
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %18, label %13

13:                                               ; preds = %0
  %14 = icmp ugt i32 %11, 15
  br i1 %14, label %18, label %15

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

18:                                               ; preds = %13, %0, %15
  %.sink = phi ptr [ %17, %15 ], [ @scalelight, %0 ], [ getelementptr inbounds (i8, ptr @scalelight, i64 5760), %13 ]
  store ptr %.sink, ptr @spritelights, align 8, !tbaa !5
  store ptr @screenheightarray, ptr @mfloorclip, align 8, !tbaa !5
  store ptr @negonearray, ptr @mceilingclip, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %1, i64 264
  %20 = load ptr, ptr %19, align 8, !tbaa !24
  %.not = icmp eq ptr %20, null
  br i1 %.not, label %22, label %21

21:                                               ; preds = %18
  tail call void @R_DrawPSprite(ptr noundef nonnull %19)
  br label %22

22:                                               ; preds = %18, %21
  %23 = getelementptr inbounds i8, ptr %1, i64 288
  %24 = load ptr, ptr %23, align 8, !tbaa !24
  %.not.1 = icmp eq ptr %24, null
  br i1 %.not.1, label %26, label %25

25:                                               ; preds = %22
  tail call void @R_DrawPSprite(ptr noundef nonnull %23)
  br label %26

26:                                               ; preds = %25, %22
  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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 88}
!15 = !{!"mobj_s", !16, 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, !17, i64 204, !6, i64 216}
!16 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!17 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!18 = !{!19, !6, i64 0}
!19 = !{!"subsector_s", !6, i64 0, !13, i64 8, !13, i64 10}
!20 = !{!21, !13, i64 12}
!21 = !{!"", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !11, i64 20, !6, i64 24, !7, i64 32, !22, i64 48, !11, i64 88, !6, i64 96, !6, i64 104, !11, i64 112, !6, i64 120}
!22 = !{!"", !16, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!23 = !{!11, !11, i64 0}
!24 = !{!25, !6, i64 0}
!25 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass