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

; 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.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%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 }

@states = external global [967 x %struct.state_t], align 16

; Function Attrs: nounwind uwtable
define dso_local void @P_SetPsprite(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.player_s, ptr %9, i32 0, i32 33
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %10, i64 0, i64 %12
  store ptr %13, ptr %7, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %75, %3
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %20, label %17

17:                                               ; preds = %14
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.pspdef_t, ptr %18, i32 0, i32 0
  store ptr null, ptr %19, align 8, !tbaa !11
  br label %81

20:                                               ; preds = %14
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %22
  store ptr %23, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.pspdef_t, ptr %25, i32 0, i32 0
  store ptr %24, ptr %26, align 8, !tbaa !11
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.state_t, ptr %27, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = trunc i64 %29 to i32
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.pspdef_t, ptr %31, i32 0, i32 1
  store i32 %30, ptr %32, align 8, !tbaa !16
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.state_t, ptr %33, i32 0, i32 5
  %35 = load i64, ptr %34, align 8, !tbaa !17
  %36 = icmp ne i64 %35, 0
  br i1 %36, label %37, label %52

37:                                               ; preds = %20
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.state_t, ptr %38, i32 0, i32 5
  %40 = load i64, ptr %39, align 8, !tbaa !17
  %41 = shl i64 %40, 16
  %42 = trunc i64 %41 to i32
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.pspdef_t, ptr %43, i32 0, i32 2
  store i32 %42, ptr %44, align 4, !tbaa !18
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.state_t, ptr %45, i32 0, i32 6
  %47 = load i64, ptr %46, align 8, !tbaa !19
  %48 = shl i64 %47, 16
  %49 = trunc i64 %48 to i32
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.pspdef_t, ptr %50, i32 0, i32 3
  store i32 %49, ptr %51, align 8, !tbaa !20
  br label %52

52:                                               ; preds = %37, %20
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.state_t, ptr %53, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !21
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %69

57:                                               ; preds = %52
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.state_t, ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !21
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  call void %60(ptr noundef %61, ptr noundef %62)
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.pspdef_t, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = icmp ne ptr %65, null
  br i1 %66, label %68, label %67

67:                                               ; preds = %57
  br label %81

68:                                               ; preds = %57
  br label %69

69:                                               ; preds = %68, %52
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.pspdef_t, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  %73 = getelementptr inbounds %struct.state_t, ptr %72, i32 0, i32 4
  %74 = load i32, ptr %73, align 8, !tbaa !22
  store i32 %74, ptr %6, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %69
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.pspdef_t, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %77, align 8, !tbaa !16
  %79 = icmp ne i32 %78, 0
  %80 = xor i1 %79, true
  br i1 %80, label %14, label %81, !llvm.loop !23

81:                                               ; preds = %75, %67, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!13 = !{!14, !15, i64 16}
!14 = !{!"", !10, i64 0, !15, i64 8, !15, i64 16, !7, i64 24, !10, i64 32, !15, i64 40, !15, i64 48}
!15 = !{!"long", !7, i64 0}
!16 = !{!12, !10, i64 8}
!17 = !{!14, !15, i64 40}
!18 = !{!12, !10, i64 12}
!19 = !{!14, !15, i64 48}
!20 = !{!12, !10, i64 16}
!21 = !{!7, !7, i64 0}
!22 = !{!14, !10, i64 32}
!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.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%struct.pspdef_t = type { ptr, i32, i32, i32 }

@states = external global [967 x %struct.state_t], align 16

; Function Attrs: nounwind uwtable
define dso_local void @P_SetPsprite(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 264
  %5 = sext i32 %1 to i64
  %6 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %4, i64 0, i64 %5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = getelementptr inbounds i8, ptr %6, i64 12
  %9 = getelementptr inbounds i8, ptr %6, i64 16
  br label %10

10:                                               ; preds = %30, %3
  %.0 = phi i32 [ %2, %3 ], [ %34, %30 ]
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %11, label %12

11:                                               ; preds = %10
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %.loopexit

12:                                               ; preds = %10
  %13 = zext i32 %.0 to i64
  %14 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %13
  store ptr %14, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %7, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %14, i64 40
  %19 = load i64, ptr %18, align 8, !tbaa !15
  %.not22 = icmp eq i64 %19, 0
  br i1 %.not22, label %25, label %20

20:                                               ; preds = %12
  %.tr = trunc i64 %19 to i32
  %21 = shl i32 %.tr, 16
  store i32 %21, ptr %8, align 4, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %14, i64 48
  %23 = load i64, ptr %22, align 8, !tbaa !17
  %.tr23 = trunc i64 %23 to i32
  %24 = shl i32 %.tr23, 16
  store i32 %24, ptr %9, align 8, !tbaa !18
  br label %25

25:                                               ; preds = %20, %12
  %26 = getelementptr inbounds i8, ptr %14, i64 24
  %27 = load ptr, ptr %26, align 8, !tbaa !19
  %.not24 = icmp eq ptr %27, null
  br i1 %.not24, label %30, label %28

28:                                               ; preds = %25
  tail call void %27(ptr noundef nonnull %0, ptr noundef nonnull %6) #1
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %.not25 = icmp eq ptr %29, null
  br i1 %.not25, label %.loopexit, label %._crit_edge

._crit_edge:                                      ; preds = %28
  %.pre = load i32, ptr %7, align 8, !tbaa !14
  br label %30

30:                                               ; preds = %._crit_edge, %25
  %31 = phi i32 [ %.pre, %._crit_edge ], [ %17, %25 ]
  %32 = phi ptr [ %29, %._crit_edge ], [ %14, %25 ]
  %33 = getelementptr inbounds i8, ptr %32, i64 32
  %34 = load i32, ptr %33, align 8, !tbaa !20
  %.not26 = icmp eq i32 %31, 0
  br i1 %.not26, label %10, label %.loopexit, !llvm.loop !21

.loopexit:                                        ; preds = %28, %30, %11
  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 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !13, i64 16}
!12 = !{!"", !10, i64 0, !13, i64 8, !13, i64 16, !8, i64 24, !10, i64 32, !13, i64 40, !13, i64 48}
!13 = !{!"long", !8, i64 0}
!14 = !{!6, !10, i64 8}
!15 = !{!12, !13, i64 40}
!16 = !{!6, !10, i64 12}
!17 = !{!12, !13, i64 48}
!18 = !{!6, !10, i64 16}
!19 = !{!8, !8, i64 0}
!20 = !{!12, !10, i64 32}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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