; 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.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 }

@linetarget = external global ptr, align 8

; 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

declare i32 @P_Random() #1

declare i32 @P_AimLineAttack(ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @A_BFGSpray(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %63, %1
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 40
  br i1 %9, label %10, label %66

10:                                               ; preds = %7
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mobj_s, ptr %11, i32 0, i32 6
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = sub i32 %13, 536870912
  %15 = load i32, ptr %3, align 4, !tbaa !9
  %16 = mul nsw i32 26843545, %15
  %17 = add i32 %14, %16
  store i32 %17, ptr %6, align 4, !tbaa !9
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 28
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = call i32 @P_AimLineAttack(ptr noundef %20, i32 noundef %21, i32 noundef 67108864)
  %23 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %26, label %25

25:                                               ; preds = %10
  br label %63

26:                                               ; preds = %10
  %27 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 8, !tbaa !17
  %30 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mobj_s, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !18
  %33 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mobj_s, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 8, !tbaa !19
  %36 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mobj_s, ptr %36, i32 0, i32 15
  %38 = load i32, ptr %37, align 4, !tbaa !20
  %39 = ashr i32 %38, 2
  %40 = add nsw i32 %35, %39
  %41 = call ptr @P_SpawnMobj(i32 noundef %29, i32 noundef %32, i32 noundef %40, i32 noundef 42)
  store i32 0, ptr %5, align 4, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %51, %26
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = icmp slt i32 %43, 15
  br i1 %44, label %45, label %54

45:                                               ; preds = %42
  %46 = call i32 @P_Random()
  %47 = and i32 %46, 7
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = add nsw i32 %49, %48
  store i32 %50, ptr %5, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %45
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %4, align 4, !tbaa !9
  br label %42, !llvm.loop !21

54:                                               ; preds = %42
  %55 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mobj_s, ptr %56, i32 0, i32 28
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.mobj_s, ptr %59, i32 0, i32 28
  %61 = load ptr, ptr %60, align 8, !tbaa !16
  %62 = load i32, ptr %5, align 4, !tbaa !9
  call void @P_DamageMobj(ptr noundef %55, ptr noundef %58, ptr noundef %61, i32 noundef %62)
  br label %63

63:                                               ; preds = %54, %25
  %64 = load i32, ptr %3, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %3, align 4, !tbaa !9
  br label %7, !llvm.loop !23

66:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

declare ptr @P_SpawnMobj(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

declare void @P_DamageMobj(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 56}
!12 = !{!"mobj_s", !13, i64 0, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !10, i64 60, !10, i64 64, !6, i64 72, !6, i64 80, !6, 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, !6, i64 136, !10, i64 144, !6, i64 152, !10, i64 160, !10, i64 164, !10, i64 168, !10, i64 172, !6, i64 176, !10, i64 184, !10, i64 188, !6, i64 192, !10, i64 200, !14, i64 204, !6, i64 216}
!13 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!14 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4, !15, i64 6, !15, i64 8}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 176}
!17 = !{!12, !10, i64 24}
!18 = !{!12, !10, i64 28}
!19 = !{!12, !10, i64 32}
!20 = !{!12, !10, i64 108}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; 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"

@linetarget = external local_unnamed_addr global ptr, align 8

declare i32 @P_Random() local_unnamed_addr #0

declare i32 @P_AimLineAttack(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @A_BFGSpray(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 56
  %3 = getelementptr inbounds i8, ptr %0, i64 176
  br label %4

4:                                                ; preds = %1, %56
  %.016 = phi i32 [ 0, %1 ], [ %57, %56 ]
  %5 = load i32, ptr %2, align 8, !tbaa !5
  %6 = mul nuw nsw i32 %.016, 26843545
  %7 = add nsw i32 %6, -536870912
  %8 = add i32 %7, %5
  %9 = load ptr, ptr %3, align 8, !tbaa !14
  %10 = tail call i32 @P_AimLineAttack(ptr noundef %9, i32 noundef %8, i32 noundef 67108864) #2
  %11 = load ptr, ptr @linetarget, align 8, !tbaa !15
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %56, label %12

12:                                               ; preds = %4
  %13 = getelementptr inbounds i8, ptr %11, i64 24
  %14 = load i32, ptr %13, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %11, i64 28
  %16 = load i32, ptr %15, align 4, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %11, i64 32
  %18 = load i32, ptr %17, align 8, !tbaa !18
  %19 = getelementptr inbounds i8, ptr %11, i64 108
  %20 = load i32, ptr %19, align 4, !tbaa !19
  %21 = ashr i32 %20, 2
  %22 = add nsw i32 %21, %18
  %23 = tail call ptr @P_SpawnMobj(i32 noundef %14, i32 noundef %16, i32 noundef %22, i32 noundef 42) #2
  %24 = tail call i32 @P_Random() #2
  %25 = and i32 %24, 7
  %26 = tail call i32 @P_Random() #2
  %27 = and i32 %26, 7
  %28 = tail call i32 @P_Random() #2
  %29 = and i32 %28, 7
  %30 = tail call i32 @P_Random() #2
  %31 = and i32 %30, 7
  %32 = tail call i32 @P_Random() #2
  %33 = and i32 %32, 7
  %34 = tail call i32 @P_Random() #2
  %35 = and i32 %34, 7
  %36 = tail call i32 @P_Random() #2
  %37 = and i32 %36, 7
  %38 = tail call i32 @P_Random() #2
  %39 = and i32 %38, 7
  %40 = tail call i32 @P_Random() #2
  %41 = and i32 %40, 7
  %42 = tail call i32 @P_Random() #2
  %43 = and i32 %42, 7
  %44 = tail call i32 @P_Random() #2
  %45 = and i32 %44, 7
  %46 = tail call i32 @P_Random() #2
  %47 = and i32 %46, 7
  %48 = tail call i32 @P_Random() #2
  %49 = and i32 %48, 7
  %50 = tail call i32 @P_Random() #2
  %51 = and i32 %50, 7
  %52 = tail call i32 @P_Random() #2
  %53 = and i32 %52, 7
  %op.rdx = add nuw nsw i32 %25, 15
  %op.rdx17 = add nuw nsw i32 %27, %29
  %op.rdx18 = add nuw nsw i32 %31, %33
  %op.rdx19 = add nuw nsw i32 %35, %37
  %op.rdx20 = add nuw nsw i32 %39, %41
  %op.rdx21 = add nuw nsw i32 %43, %45
  %op.rdx22 = add nuw nsw i32 %47, %49
  %op.rdx23 = add nuw nsw i32 %51, %53
  %op.rdx24 = add nuw nsw i32 %op.rdx, %op.rdx17
  %op.rdx25 = add nuw nsw i32 %op.rdx18, %op.rdx19
  %op.rdx26 = add nuw nsw i32 %op.rdx20, %op.rdx21
  %op.rdx27 = add nuw nsw i32 %op.rdx22, %op.rdx23
  %op.rdx28 = add nuw nsw i32 %op.rdx24, %op.rdx25
  %op.rdx29 = add nuw nsw i32 %op.rdx26, %op.rdx27
  %op.rdx30 = add nuw nsw i32 %op.rdx28, %op.rdx29
  %54 = load ptr, ptr @linetarget, align 8, !tbaa !15
  %55 = load ptr, ptr %3, align 8, !tbaa !14
  tail call void @P_DamageMobj(ptr noundef %54, ptr noundef %55, ptr noundef %55, i32 noundef %op.rdx30) #2
  br label %56

56:                                               ; preds = %4, %12
  %57 = add nuw nsw i32 %.016, 1
  %exitcond.not = icmp eq i32 %57, 40
  br i1 %exitcond.not, label %58, label %4, !llvm.loop !20

58:                                               ; preds = %56
  ret void
}

declare ptr @P_SpawnMobj(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @P_DamageMobj(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

attributes #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 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, !11, i64 56}
!6 = !{!"mobj_s", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !8, i64 40, !8, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !8, i64 72, !8, i64 80, !8, 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, !8, i64 136, !11, i64 144, !8, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !8, i64 176, !11, i64 184, !11, i64 188, !8, i64 192, !11, i64 200, !12, i64 204, !8, i64 216}
!7 = !{!"thinker_s", !8, i64 0, !8, i64 8, !9, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !9, i64 0}
!14 = !{!6, !8, i64 176}
!15 = !{!8, !8, i64 0}
!16 = !{!6, !11, i64 24}
!17 = !{!6, !11, i64 28}
!18 = !{!6, !11, i64 32}
!19 = !{!6, !11, i64 108}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass