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

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

@bmaporgx = external global i32, align 4
@bmaporgy = external global i32, align 4
@bombspot = external dso_local global ptr, align 8
@bombdamage = external dso_local global i32, align 4
@bombsource = external dso_local 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_BlockThingsIterator(i32 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @PIT_RadiusAttack(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @P_RadiusAttack(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = add nsw i32 %14, 2097152
  %16 = shl i32 %15, 16
  store i32 %16, ptr %13, align 4, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mobj_s, ptr %17, i32 0, i32 2
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = load i32, ptr %13, align 4, !tbaa !9
  %21 = add nsw i32 %19, %20
  %22 = load i32, ptr @bmaporgy, align 4, !tbaa !9
  %23 = sub nsw i32 %21, %22
  %24 = ashr i32 %23, 23
  store i32 %24, ptr %12, align 4, !tbaa !9
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mobj_s, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = load i32, ptr %13, align 4, !tbaa !9
  %29 = sub nsw i32 %27, %28
  %30 = load i32, ptr @bmaporgy, align 4, !tbaa !9
  %31 = sub nsw i32 %29, %30
  %32 = ashr i32 %31, 23
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mobj_s, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = load i32, ptr %13, align 4, !tbaa !9
  %37 = add nsw i32 %35, %36
  %38 = load i32, ptr @bmaporgx, align 4, !tbaa !9
  %39 = sub nsw i32 %37, %38
  %40 = ashr i32 %39, 23
  store i32 %40, ptr %10, align 4, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mobj_s, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = load i32, ptr %13, align 4, !tbaa !9
  %45 = sub nsw i32 %43, %44
  %46 = load i32, ptr @bmaporgx, align 4, !tbaa !9
  %47 = sub nsw i32 %45, %46
  %48 = ashr i32 %47, 23
  store i32 %48, ptr %9, align 4, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %49, ptr @bombspot, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %50, ptr @bombsource, align 8, !tbaa !5
  %51 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %51, ptr @bombdamage, align 4, !tbaa !9
  %52 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %52, ptr %8, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %71, %3
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = load i32, ptr %12, align 4, !tbaa !9
  %56 = icmp sle i32 %54, %55
  br i1 %56, label %57, label %74

57:                                               ; preds = %53
  %58 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %58, ptr %7, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %67, %57
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = icmp sle i32 %60, %61
  br i1 %62, label %63, label %70

63:                                               ; preds = %59
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = call i32 @P_BlockThingsIterator(i32 noundef %64, i32 noundef %65, ptr noundef @PIT_RadiusAttack)
  br label %67

67:                                               ; preds = %63
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %7, align 4, !tbaa !9
  br label %59, !llvm.loop !17

70:                                               ; preds = %59
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %8, align 4, !tbaa !9
  br label %53, !llvm.loop !19

74:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

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 28}
!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, !10, i64 24}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

@bmaporgx = external local_unnamed_addr global i32, align 4
@bmaporgy = external local_unnamed_addr global i32, align 4
@bombspot = external dso_local local_unnamed_addr global ptr, align 8
@bombdamage = external dso_local local_unnamed_addr global i32, align 4
@bombsource = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @PIT_RadiusAttack(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @P_RadiusAttack(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = shl i32 %2, 16
  %5 = getelementptr inbounds i8, ptr %0, i64 28
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = add nsw i32 %6, %4
  %8 = load i32, ptr @bmaporgy, align 4, !tbaa !14
  %9 = sub i32 %7, %8
  %10 = ashr i32 %9, 23
  %11 = add i32 %4, %8
  %12 = sub i32 %6, %11
  %13 = ashr i32 %12, 23
  %14 = getelementptr inbounds i8, ptr %0, i64 24
  %15 = load i32, ptr %14, align 8, !tbaa !15
  %16 = add nsw i32 %15, %4
  %17 = load i32, ptr @bmaporgx, align 4, !tbaa !14
  %18 = sub i32 %16, %17
  %19 = ashr i32 %18, 23
  %20 = add i32 %4, %17
  %21 = sub i32 %15, %20
  %22 = ashr i32 %21, 23
  store ptr %0, ptr @bombspot, align 8, !tbaa !16
  store ptr %1, ptr @bombsource, align 8, !tbaa !16
  store i32 %2, ptr @bombdamage, align 4, !tbaa !14
  %.not29 = icmp sgt i32 %13, %10
  %.not2227 = icmp sgt i32 %22, %19
  %or.cond = select i1 %.not29, i1 true, i1 %.not2227
  br i1 %or.cond, label %._crit_edge31, label %.preheader

.preheader:                                       ; preds = %3, %._crit_edge
  %.02130 = phi i32 [ %26, %._crit_edge ], [ %13, %3 ]
  br label %23

23:                                               ; preds = %.preheader, %23
  %.028 = phi i32 [ %22, %.preheader ], [ %25, %23 ]
  %24 = tail call i32 @P_BlockThingsIterator(i32 noundef %.028, i32 noundef %.02130, ptr noundef nonnull @PIT_RadiusAttack) #2
  %25 = add nsw i32 %.028, 1
  %exitcond.not = icmp eq i32 %.028, %19
  br i1 %exitcond.not, label %._crit_edge, label %23, !llvm.loop !17

._crit_edge:                                      ; preds = %23
  %26 = add nsw i32 %.02130, 1
  %exitcond34.not = icmp eq i32 %.02130, %10
  br i1 %exitcond34.not, label %._crit_edge31, label %.preheader, !llvm.loop !19

._crit_edge31:                                    ; preds = %._crit_edge, %3
  ret void
}

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 28}
!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 = !{!11, !11, i64 0}
!15 = !{!6, !11, i64 24}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass