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

@finecosine = external global ptr, align 8
@finesine = external global [10240 x i32], align 16
@attackrange = external dso_local global i32, align 4
@shootz = external dso_local global i32, align 4
@shootthing = external dso_local global ptr, align 8
@aimslope = external dso_local global i32, align 4
@la_damage = external dso_local global i32, align 4

; 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_PathTraverse(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @P_LineAttack(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #2 {
  %6 = alloca ptr, align 8
  %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
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = lshr i32 %13, 19
  store i32 %14, ptr %7, align 4, !tbaa !9
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %15, ptr @shootthing, align 8, !tbaa !5
  %16 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %16, ptr @la_damage, align 4, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mobj_s, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = ashr i32 %20, 16
  %22 = load ptr, ptr @finecosine, align 8, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = mul nsw i32 %21, %26
  %28 = add nsw i32 %19, %27
  store i32 %28, ptr %11, align 4, !tbaa !9
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mobj_s, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !16
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = ashr i32 %32, 16
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = zext i32 %34 to i64
  %36 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = mul nsw i32 %33, %37
  %39 = add nsw i32 %31, %38
  store i32 %39, ptr %12, align 4, !tbaa !9
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.mobj_s, ptr %40, i32 0, i32 3
  %42 = load i32, ptr %41, align 8, !tbaa !17
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mobj_s, ptr %43, i32 0, i32 15
  %45 = load i32, ptr %44, align 4, !tbaa !18
  %46 = ashr i32 %45, 1
  %47 = add nsw i32 %42, %46
  %48 = add nsw i32 %47, 524288
  store i32 %48, ptr @shootz, align 4, !tbaa !9
  %49 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %49, ptr @attackrange, align 4, !tbaa !9
  %50 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %50, ptr @aimslope, align 4, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mobj_s, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mobj_s, ptr %54, i32 0, i32 2
  %56 = load i32, ptr %55, align 4, !tbaa !16
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = call i32 @P_PathTraverse(i32 noundef %53, i32 noundef %56, i32 noundef %57, i32 noundef %58, i32 noundef 3, ptr noundef @PTR_ShootTraverse)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #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 24}
!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 28}
!17 = !{!12, !10, i64 32}
!18 = !{!12, !10, i64 108}


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

@finecosine = external local_unnamed_addr global ptr, align 8
@finesine = external local_unnamed_addr global [10240 x i32], align 16
@attackrange = external dso_local local_unnamed_addr global i32, align 4
@shootz = external dso_local local_unnamed_addr global i32, align 4
@shootthing = external dso_local local_unnamed_addr global ptr, align 8
@aimslope = external dso_local local_unnamed_addr global i32, align 4
@la_damage = external dso_local local_unnamed_addr global i32, align 4

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_LineAttack(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = lshr i32 %1, 19
  store ptr %0, ptr @shootthing, align 8, !tbaa !5
  store i32 %4, ptr @la_damage, align 4, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load i32, ptr %7, align 8, !tbaa !11
  %9 = ashr i32 %2, 16
  %10 = load ptr, ptr @finecosine, align 8, !tbaa !5
  %11 = zext nneg i32 %6 to i64
  %12 = getelementptr inbounds i32, ptr %10, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = mul nsw i32 %13, %9
  %15 = add nsw i32 %14, %8
  %16 = getelementptr inbounds i8, ptr %0, i64 28
  %17 = load i32, ptr %16, align 4, !tbaa !16
  %18 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %11
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = mul nsw i32 %19, %9
  %21 = add nsw i32 %20, %17
  %22 = getelementptr inbounds i8, ptr %0, i64 32
  %23 = load i32, ptr %22, align 8, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %0, i64 108
  %25 = load i32, ptr %24, align 4, !tbaa !18
  %26 = ashr i32 %25, 1
  %27 = add i32 %23, 524288
  %28 = add i32 %27, %26
  store i32 %28, ptr @shootz, align 4, !tbaa !9
  store i32 %2, ptr @attackrange, align 4, !tbaa !9
  store i32 %3, ptr @aimslope, align 4, !tbaa !9
  %29 = tail call i32 @P_PathTraverse(i32 noundef %8, i32 noundef %17, i32 noundef %15, i32 noundef %21, i32 noundef 3, ptr noundef nonnull @PTR_ShootTraverse) #2
  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, !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 24}
!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 28}
!17 = !{!12, !10, i64 32}
!18 = !{!12, !10, i64 108}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,TailCallElimPass