; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (am_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.mpoint_t = type { 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 }

@m_w = external hidden global i32, align 4
@m_x = external hidden global i32, align 4
@m_h = external hidden global i32, align 4
@m_y = external hidden global i32, align 4
@scale_ftom = external hidden global i32, align 4
@m_x2 = external hidden global i32, align 4
@m_y2 = external hidden global i32, align 4
@plr = external hidden global ptr, align 8
@scale_mtof = external hidden global i32, align 4
@f_oldloc = external hidden global %struct.mpoint_t, align 4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_doFollowPlayer() #1 {
  %1 = load i32, ptr @f_oldloc, align 4, !tbaa !5
  %2 = load ptr, ptr @plr, align 8, !tbaa !10
  %3 = getelementptr inbounds %struct.player_s, ptr %2, i32 0, i32 0
  %4 = load ptr, ptr %3, align 8, !tbaa !12
  %5 = getelementptr inbounds %struct.mobj_s, ptr %4, i32 0, i32 1
  %6 = load i32, ptr %5, align 8, !tbaa !16
  %7 = icmp ne i32 %1, %6
  br i1 %7, label %16, label %8

8:                                                ; preds = %0
  %9 = load i32, ptr getelementptr inbounds (%struct.mpoint_t, ptr @f_oldloc, i32 0, i32 1), align 4, !tbaa !20
  %10 = load ptr, ptr @plr, align 8, !tbaa !10
  %11 = getelementptr inbounds %struct.player_s, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = getelementptr inbounds %struct.mobj_s, ptr %12, i32 0, i32 2
  %14 = load i32, ptr %13, align 4, !tbaa !21
  %15 = icmp ne i32 %9, %14
  br i1 %15, label %16, label %61

16:                                               ; preds = %8, %0
  %17 = load ptr, ptr @plr, align 8, !tbaa !10
  %18 = getelementptr inbounds %struct.player_s, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = getelementptr inbounds %struct.mobj_s, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 8, !tbaa !16
  %22 = load i32, ptr @scale_mtof, align 4, !tbaa !22
  %23 = call i32 @FixedMul(i32 noundef %21, i32 noundef %22)
  %24 = ashr i32 %23, 16
  %25 = shl i32 %24, 16
  %26 = load i32, ptr @scale_ftom, align 4, !tbaa !22
  %27 = call i32 @FixedMul(i32 noundef %25, i32 noundef %26)
  %28 = load i32, ptr @m_w, align 4, !tbaa !22
  %29 = sdiv i32 %28, 2
  %30 = sub nsw i32 %27, %29
  store i32 %30, ptr @m_x, align 4, !tbaa !22
  %31 = load ptr, ptr @plr, align 8, !tbaa !10
  %32 = getelementptr inbounds %struct.player_s, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  %34 = getelementptr inbounds %struct.mobj_s, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !21
  %36 = load i32, ptr @scale_mtof, align 4, !tbaa !22
  %37 = call i32 @FixedMul(i32 noundef %35, i32 noundef %36)
  %38 = ashr i32 %37, 16
  %39 = shl i32 %38, 16
  %40 = load i32, ptr @scale_ftom, align 4, !tbaa !22
  %41 = call i32 @FixedMul(i32 noundef %39, i32 noundef %40)
  %42 = load i32, ptr @m_h, align 4, !tbaa !22
  %43 = sdiv i32 %42, 2
  %44 = sub nsw i32 %41, %43
  store i32 %44, ptr @m_y, align 4, !tbaa !22
  %45 = load i32, ptr @m_x, align 4, !tbaa !22
  %46 = load i32, ptr @m_w, align 4, !tbaa !22
  %47 = add nsw i32 %45, %46
  store i32 %47, ptr @m_x2, align 4, !tbaa !22
  %48 = load i32, ptr @m_y, align 4, !tbaa !22
  %49 = load i32, ptr @m_h, align 4, !tbaa !22
  %50 = add nsw i32 %48, %49
  store i32 %50, ptr @m_y2, align 4, !tbaa !22
  %51 = load ptr, ptr @plr, align 8, !tbaa !10
  %52 = getelementptr inbounds %struct.player_s, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !12
  %54 = getelementptr inbounds %struct.mobj_s, ptr %53, i32 0, i32 1
  %55 = load i32, ptr %54, align 8, !tbaa !16
  store i32 %55, ptr @f_oldloc, align 4, !tbaa !5
  %56 = load ptr, ptr @plr, align 8, !tbaa !10
  %57 = getelementptr inbounds %struct.player_s, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !12
  %59 = getelementptr inbounds %struct.mobj_s, ptr %58, i32 0, i32 2
  %60 = load i32, ptr %59, align 4, !tbaa !21
  store i32 %60, ptr getelementptr inbounds (%struct.mpoint_t, ptr @f_oldloc, i32 0, i32 1), align 4, !tbaa !20
  br label %61

61:                                               ; preds = %16, %8
  ret void
}

attributes #0 = { inlinehint 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 = { 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" }

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


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

@m_w = external hidden local_unnamed_addr global i32, align 4
@m_x = external hidden local_unnamed_addr global i32, align 4
@m_h = external hidden local_unnamed_addr global i32, align 4
@m_y = external hidden local_unnamed_addr global i32, align 4
@scale_ftom = external hidden local_unnamed_addr global i32, align 4
@m_x2 = external hidden local_unnamed_addr global i32, align 4
@m_y2 = external hidden local_unnamed_addr global i32, align 4
@plr = external hidden local_unnamed_addr global ptr, align 8
@scale_mtof = external hidden local_unnamed_addr global i32, align 4
@f_oldloc = external hidden local_unnamed_addr global %struct.mpoint_t, align 4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_doFollowPlayer() local_unnamed_addr #1 {
  %1 = load i32, ptr @f_oldloc, align 4, !tbaa !5
  %2 = load ptr, ptr @plr, align 8, !tbaa !10
  %3 = load ptr, ptr %2, align 8, !tbaa !12
  %4 = getelementptr inbounds i8, ptr %3, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !16
  %.not = icmp eq i32 %1, %5
  br i1 %.not, label %6, label %10

6:                                                ; preds = %0
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @f_oldloc, i64 4), align 4, !tbaa !20
  %8 = getelementptr inbounds i8, ptr %3, i64 28
  %9 = load i32, ptr %8, align 4, !tbaa !21
  %.not1 = icmp eq i32 %7, %9
  br i1 %.not1, label %37, label %10

10:                                               ; preds = %6, %0
  %11 = load i32, ptr @scale_mtof, align 4, !tbaa !22
  %12 = tail call i32 @FixedMul(i32 noundef %5, i32 noundef %11)
  %13 = and i32 %12, -65536
  %14 = load i32, ptr @scale_ftom, align 4, !tbaa !22
  %15 = tail call i32 @FixedMul(i32 noundef %13, i32 noundef %14)
  %16 = load i32, ptr @m_w, align 4, !tbaa !22
  %.neg = sdiv i32 %16, -2
  %17 = add i32 %.neg, %15
  store i32 %17, ptr @m_x, align 4, !tbaa !22
  %18 = load ptr, ptr @plr, align 8, !tbaa !10
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = getelementptr inbounds i8, ptr %19, i64 28
  %21 = load i32, ptr %20, align 4, !tbaa !21
  %22 = load i32, ptr @scale_mtof, align 4, !tbaa !22
  %23 = tail call i32 @FixedMul(i32 noundef %21, i32 noundef %22)
  %24 = and i32 %23, -65536
  %25 = load i32, ptr @scale_ftom, align 4, !tbaa !22
  %26 = tail call i32 @FixedMul(i32 noundef %24, i32 noundef %25)
  %27 = load i32, ptr @m_h, align 4, !tbaa !22
  %.neg2 = sdiv i32 %27, -2
  %28 = add i32 %.neg2, %26
  store i32 %28, ptr @m_y, align 4, !tbaa !22
  %29 = load i32, ptr @m_x, align 4, !tbaa !22
  %30 = load i32, ptr @m_w, align 4, !tbaa !22
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr @m_x2, align 4, !tbaa !22
  %32 = add nsw i32 %28, %27
  store i32 %32, ptr @m_y2, align 4, !tbaa !22
  %33 = load ptr, ptr @plr, align 8, !tbaa !10
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = getelementptr inbounds i8, ptr %34, i64 24
  %36 = load <2 x i32>, ptr %35, align 8, !tbaa !22
  store <2 x i32> %36, ptr @f_oldloc, align 4, !tbaa !22
  br label %37

37:                                               ; preds = %10, %6
  ret void
}

attributes #0 = { inlinehint 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 = { 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" }

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


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