; 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
@bottomslope = external global i32, align 4
@topslope = external global i32, align 4
@shootthing = external dso_local global ptr, align 8
@aimslope = external dso_local global i32, align 4
@linetarget = 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_PathTraverse(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

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

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

58:                                               ; preds = %3
  %59 = load i32, ptr @aimslope, align 4, !tbaa !9
  store i32 %59, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %61

60:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %61

61:                                               ; preds = %60, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %62 = load i32, ptr %4, align 4
  ret i32 %62
}

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
@bottomslope = external local_unnamed_addr global i32, align 4
@topslope = external 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
@linetarget = external dso_local local_unnamed_addr global ptr, align 8

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_AimTraverse(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @P_AimLineAttack(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = lshr i32 %1, 19
  store ptr %0, ptr @shootthing, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i32, ptr %5, align 8, !tbaa !9
  %7 = ashr i32 %2, 16
  %8 = load ptr, ptr @finecosine, align 8, !tbaa !5
  %9 = zext nneg i32 %4 to i64
  %10 = getelementptr inbounds i32, ptr %8, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !15
  %12 = mul nsw i32 %11, %7
  %13 = add nsw i32 %12, %6
  %14 = getelementptr inbounds i8, ptr %0, i64 28
  %15 = load i32, ptr %14, align 4, !tbaa !16
  %16 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %9
  %17 = load i32, ptr %16, align 4, !tbaa !15
  %18 = mul nsw i32 %17, %7
  %19 = add nsw i32 %18, %15
  %20 = getelementptr inbounds i8, ptr %0, i64 32
  %21 = load i32, ptr %20, align 8, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %0, i64 108
  %23 = load i32, ptr %22, align 4, !tbaa !18
  %24 = ashr i32 %23, 1
  %25 = add i32 %21, 524288
  %26 = add i32 %25, %24
  store i32 %26, ptr @shootz, align 4, !tbaa !15
  store i32 40960, ptr @topslope, align 4, !tbaa !15
  store i32 -40960, ptr @bottomslope, align 4, !tbaa !15
  store i32 %2, ptr @attackrange, align 4, !tbaa !15
  store ptr null, ptr @linetarget, align 8, !tbaa !5
  %27 = tail call i32 @P_PathTraverse(i32 noundef %6, i32 noundef %15, i32 noundef %13, i32 noundef %19, i32 noundef 3, ptr noundef nonnull @PTR_AimTraverse) #2
  %28 = load ptr, ptr @linetarget, align 8, !tbaa !5
  %.not = icmp eq ptr %28, null
  %29 = load i32, ptr @aimslope, align 4
  %.0 = select i1 %.not, i32 0, i32 %29
  ret i32 %.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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 24}
!10 = !{!"mobj_s", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !12, i64 96, !12, i64 100, !12, i64 104, !12, i64 108, !12, i64 112, !12, i64 116, !12, i64 120, !12, i64 124, !12, i64 128, !6, i64 136, !12, i64 144, !6, i64 152, !12, i64 160, !12, i64 164, !12, i64 168, !12, i64 172, !6, i64 176, !12, i64 184, !12, i64 188, !6, i64 192, !12, i64 200, !13, i64 204, !6, i64 216}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !12, i64 28}
!17 = !{!10, !12, i64 32}
!18 = !{!10, !12, i64 108}


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