; 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.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
@f_w = 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
@old_m_x = external hidden global i32, align 4
@old_m_y = external hidden global i32, align 4
@old_m_w = external hidden global i32, align 4
@old_m_h = external hidden global i32, align 4
@followplayer = external hidden global i32, align 4
@plr = external hidden global ptr, align 8
@scale_mtof = external hidden global i32, align 4

declare i32 @FixedDiv(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_restoreScaleAndLoc() #1 {
  %1 = load i32, ptr @old_m_w, align 4, !tbaa !5
  store i32 %1, ptr @m_w, align 4, !tbaa !5
  %2 = load i32, ptr @old_m_h, align 4, !tbaa !5
  store i32 %2, ptr @m_h, align 4, !tbaa !5
  %3 = load i32, ptr @followplayer, align 4, !tbaa !5
  %4 = icmp ne i32 %3, 0
  br i1 %4, label %8, label %5

5:                                                ; preds = %0
  %6 = load i32, ptr @old_m_x, align 4, !tbaa !5
  store i32 %6, ptr @m_x, align 4, !tbaa !5
  %7 = load i32, ptr @old_m_y, align 4, !tbaa !5
  store i32 %7, ptr @m_y, align 4, !tbaa !5
  br label %25

8:                                                ; preds = %0
  %9 = load ptr, ptr @plr, align 8, !tbaa !9
  %10 = getelementptr inbounds %struct.player_s, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds %struct.mobj_s, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !15
  %14 = load i32, ptr @m_w, align 4, !tbaa !5
  %15 = sdiv i32 %14, 2
  %16 = sub nsw i32 %13, %15
  store i32 %16, ptr @m_x, align 4, !tbaa !5
  %17 = load ptr, ptr @plr, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.player_s, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = getelementptr inbounds %struct.mobj_s, ptr %19, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !19
  %22 = load i32, ptr @m_h, align 4, !tbaa !5
  %23 = sdiv i32 %22, 2
  %24 = sub nsw i32 %21, %23
  store i32 %24, ptr @m_y, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %8, %5
  %26 = load i32, ptr @m_x, align 4, !tbaa !5
  %27 = load i32, ptr @m_w, align 4, !tbaa !5
  %28 = add nsw i32 %26, %27
  store i32 %28, ptr @m_x2, align 4, !tbaa !5
  %29 = load i32, ptr @m_y, align 4, !tbaa !5
  %30 = load i32, ptr @m_h, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  store i32 %31, ptr @m_y2, align 4, !tbaa !5
  %32 = load i32, ptr @f_w, align 4, !tbaa !5
  %33 = shl i32 %32, 16
  %34 = load i32, ptr @m_w, align 4, !tbaa !5
  %35 = call i32 @FixedDiv(i32 noundef %33, i32 noundef %34)
  store i32 %35, ptr @scale_mtof, align 4, !tbaa !5
  %36 = load i32, ptr @scale_mtof, align 4, !tbaa !5
  %37 = call i32 @FixedDiv(i32 noundef 65536, i32 noundef %36)
  store i32 %37, ptr @scale_ftom, align 4, !tbaa !5
  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" }

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


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

@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
@f_w = 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
@old_m_x = external hidden local_unnamed_addr global i32, align 4
@old_m_y = external hidden local_unnamed_addr global i32, align 4
@old_m_w = external hidden local_unnamed_addr global i32, align 4
@old_m_h = external hidden local_unnamed_addr global i32, align 4
@followplayer = 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

declare i32 @FixedDiv(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_restoreScaleAndLoc() local_unnamed_addr #1 {
  %1 = load i32, ptr @old_m_w, align 4, !tbaa !5
  store i32 %1, ptr @m_w, align 4, !tbaa !5
  %2 = load i32, ptr @old_m_h, align 4, !tbaa !5
  store i32 %2, ptr @m_h, align 4, !tbaa !5
  %3 = load i32, ptr @followplayer, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %7

4:                                                ; preds = %0
  %5 = load i32, ptr @old_m_x, align 4, !tbaa !5
  %6 = load i32, ptr @old_m_y, align 4, !tbaa !5
  br label %16

7:                                                ; preds = %0
  %8 = load ptr, ptr @plr, align 8, !tbaa !9
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load i32, ptr %10, align 8, !tbaa !15
  %.neg = sdiv i32 %1, -2
  %12 = add i32 %11, %.neg
  %13 = getelementptr inbounds i8, ptr %9, i64 28
  %14 = load i32, ptr %13, align 4, !tbaa !19
  %.neg1 = sdiv i32 %2, -2
  %15 = add i32 %14, %.neg1
  br label %16

16:                                               ; preds = %7, %4
  %.sink = phi i32 [ %5, %4 ], [ %12, %7 ]
  %storemerge = phi i32 [ %6, %4 ], [ %15, %7 ]
  store i32 %.sink, ptr @m_x, align 4
  store i32 %storemerge, ptr @m_y, align 4, !tbaa !5
  %17 = add nsw i32 %.sink, %1
  store i32 %17, ptr @m_x2, align 4, !tbaa !5
  %18 = add nsw i32 %storemerge, %2
  store i32 %18, ptr @m_y2, align 4, !tbaa !5
  %19 = load i32, ptr @f_w, align 4, !tbaa !5
  %20 = shl i32 %19, 16
  %21 = tail call i32 @FixedDiv(i32 noundef %20, i32 noundef %1) #2
  store i32 %21, ptr @scale_mtof, align 4, !tbaa !5
  %22 = tail call i32 @FixedDiv(i32 noundef 65536, i32 noundef %21) #2
  store i32 %22, ptr @scale_ftom, align 4, !tbaa !5
  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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"player_s", !10, i64 0, !6, i64 8, !13, i64 12, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !7, i64 48, !7, i64 72, !6, i64 96, !7, i64 100, !6, i64 116, !6, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !6, i64 192, !6, i64 196, !6, i64 200, !6, i64 204, !6, i64 208, !6, i64 212, !6, i64 216, !10, i64 224, !6, i64 232, !6, i64 236, !10, i64 240, !6, i64 248, !6, i64 252, !6, i64 256, !7, i64 264, !6, i64 312}
!13 = !{!"", !7, i64 0, !7, i64 1, !14, i64 2, !14, i64 4, !7, i64 6, !7, i64 7}
!14 = !{!"short", !7, i64 0}
!15 = !{!16, !6, i64 24}
!16 = !{!"mobj_s", !17, i64 0, !6, i64 24, !6, i64 28, !6, i64 32, !10, i64 40, !10, i64 48, !6, i64 56, !6, i64 60, !6, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !6, i64 96, !6, i64 100, !6, i64 104, !6, i64 108, !6, i64 112, !6, i64 116, !6, i64 120, !6, i64 124, !6, i64 128, !10, i64 136, !6, i64 144, !10, i64 152, !6, i64 160, !6, i64 164, !6, i64 168, !6, i64 172, !10, i64 176, !6, i64 184, !6, i64 188, !10, i64 192, !6, i64 200, !18, i64 204, !10, i64 216}
!17 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!18 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!19 = !{!16, !6, i64 28}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,MergedLoadStoreMotionPass,ReassociatePass,TailCallElimPass