; 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.weaponinfo_t = type { i32, i32, i32, i32, i32, i32 }
%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 }

@weaponinfo = external global [9 x %struct.weaponinfo_t], align 16
@bulletslope = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @P_SetPsprite(ptr noundef, i32 noundef, i32 noundef) #0

; 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

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

declare i32 @P_SetMobjState(ptr noundef, i32 noundef) #2

declare i32 @P_Random() #2

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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_FireShotgun2(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.player_s, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  call void @S_StartSound(ptr noundef %11, i32 noundef 4)
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.player_s, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = call i32 @P_SetMobjState(ptr noundef %14, i32 noundef 155)
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.player_s, ptr %16, i32 0, i32 17
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.player_s, ptr %18, i32 0, i32 14
  %20 = load i32, ptr %19, align 4, !tbaa !14
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.weaponinfo_t, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 8, !tbaa !15
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds [4 x i32], ptr %17, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !17
  %28 = sub nsw i32 %27, 2
  store i32 %28, ptr %26, align 4, !tbaa !17
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.player_s, ptr %30, i32 0, i32 14
  %32 = load i32, ptr %31, align 4, !tbaa !14
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.weaponinfo_t, ptr %34, i32 0, i32 5
  %36 = load i32, ptr %35, align 4, !tbaa !18
  call void @P_SetPsprite(ptr noundef %29, i32 noundef 1, i32 noundef %36)
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.player_s, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  call void @P_BulletSlope(ptr noundef %39)
  store i32 0, ptr %5, align 4, !tbaa !17
  br label %40

40:                                               ; preds = %70, %2
  %41 = load i32, ptr %5, align 4, !tbaa !17
  %42 = icmp slt i32 %41, 20
  br i1 %42, label %43, label %73

43:                                               ; preds = %40
  %44 = call i32 @P_Random()
  %45 = srem i32 %44, 3
  %46 = add nsw i32 %45, 1
  %47 = mul nsw i32 5, %46
  store i32 %47, ptr %7, align 4, !tbaa !17
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.player_s, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.mobj_s, ptr %50, i32 0, i32 6
  %52 = load i32, ptr %51, align 8, !tbaa !19
  store i32 %52, ptr %6, align 4, !tbaa !17
  %53 = call i32 @P_Random()
  %54 = call i32 @P_Random()
  %55 = sub nsw i32 %53, %54
  %56 = shl i32 %55, 19
  %57 = load i32, ptr %6, align 4, !tbaa !17
  %58 = add i32 %57, %56
  store i32 %58, ptr %6, align 4, !tbaa !17
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.player_s, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !9
  %62 = load i32, ptr %6, align 4, !tbaa !17
  %63 = load i32, ptr @bulletslope, align 4, !tbaa !17
  %64 = call i32 @P_Random()
  %65 = call i32 @P_Random()
  %66 = sub nsw i32 %64, %65
  %67 = shl i32 %66, 5
  %68 = add nsw i32 %63, %67
  %69 = load i32, ptr %7, align 4, !tbaa !17
  call void @P_LineAttack(ptr noundef %61, i32 noundef %62, i32 noundef 134217728, i32 noundef %68, i32 noundef %69)
  br label %70

70:                                               ; preds = %43
  %71 = load i32, ptr %5, align 4, !tbaa !17
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %5, align 4, !tbaa !17
  br label %40, !llvm.loop !23

73:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!10, !11, i64 116}
!15 = !{!16, !11, i64 0}
!16 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20}
!17 = !{!11, !11, i64 0}
!18 = !{!16, !11, i64 20}
!19 = !{!20, !11, i64 56}
!20 = !{!"mobj_s", !21, 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, !22, i64 204, !6, i64 216}
!21 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!22 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!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.weaponinfo_t = type { i32, i32, i32, i32, i32, i32 }

@weaponinfo = external local_unnamed_addr global [9 x %struct.weaponinfo_t], align 16
@bulletslope = external dso_local local_unnamed_addr global i32, align 4

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

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

declare i32 @P_SetMobjState(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @P_Random() local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_FireShotgun2(ptr noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  tail call void @S_StartSound(ptr noundef %3, i32 noundef 4) #2
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = tail call i32 @P_SetMobjState(ptr noundef %4, i32 noundef 155) #2
  %6 = getelementptr inbounds i8, ptr %0, i64 160
  %7 = getelementptr inbounds i8, ptr %0, i64 116
  %8 = load i32, ptr %7, align 4, !tbaa !13
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %9
  %11 = load i32, ptr %10, align 8, !tbaa !14
  %12 = zext i32 %11 to i64
  %13 = getelementptr inbounds [4 x i32], ptr %6, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !16
  %15 = add nsw i32 %14, -2
  store i32 %15, ptr %13, align 4, !tbaa !16
  %16 = getelementptr inbounds [9 x %struct.weaponinfo_t], ptr @weaponinfo, i64 0, i64 %9, i32 5
  %17 = load i32, ptr %16, align 4, !tbaa !17
  tail call void @P_SetPsprite(ptr noundef nonnull %0, i32 noundef 1, i32 noundef %17)
  %18 = load ptr, ptr %0, align 8, !tbaa !5
  tail call void @P_BulletSlope(ptr noundef %18)
  br label %19

19:                                               ; preds = %2, %19
  %.014 = phi i32 [ 0, %2 ], [ %39, %19 ]
  %20 = tail call i32 @P_Random() #2
  %21 = srem i32 %20, 3
  %22 = mul nsw i32 %21, 5
  %23 = add nsw i32 %22, 5
  %24 = load ptr, ptr %0, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 56
  %26 = load i32, ptr %25, align 8, !tbaa !18
  %27 = tail call i32 @P_Random() #2
  %28 = tail call i32 @P_Random() #2
  %29 = sub nsw i32 %27, %28
  %30 = shl i32 %29, 19
  %31 = add i32 %30, %26
  %32 = load ptr, ptr %0, align 8, !tbaa !5
  %33 = load i32, ptr @bulletslope, align 4, !tbaa !16
  %34 = tail call i32 @P_Random() #2
  %35 = tail call i32 @P_Random() #2
  %36 = sub nsw i32 %34, %35
  %37 = shl i32 %36, 5
  %38 = add nsw i32 %37, %33
  tail call void @P_LineAttack(ptr noundef %32, i32 noundef %31, i32 noundef 134217728, i32 noundef %38, i32 noundef %23) #2
  %39 = add nuw nsw i32 %.014, 1
  %exitcond.not = icmp eq i32 %39, 20
  br i1 %exitcond.not, label %40, label %19, !llvm.loop !22

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass