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

; 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.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%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.mapthing_t = type { i16, i16, i16, i16, i16 }

@save_p = external dso_local global ptr, align 8
@players = external global [4 x %struct.player_s], align 16
@states = external global [967 x %struct.state_t], align 16
@thinkercap = external global %struct.thinker_s, 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @P_ArchiveThinkers() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %3 = load ptr, ptr getelementptr inbounds (%struct.thinker_s, ptr @thinkercap, i32 0, i32 1), align 8, !tbaa !5
  store ptr %3, ptr %1, align 8, !tbaa !10
  br label %4

4:                                                ; preds = %53, %0
  %5 = load ptr, ptr %1, align 8, !tbaa !10
  %6 = icmp ne ptr %5, @thinkercap
  br i1 %6, label %7, label %57

7:                                                ; preds = %4
  %8 = load ptr, ptr %1, align 8, !tbaa !10
  %9 = getelementptr inbounds %struct.thinker_s, ptr %8, i32 0, i32 2
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = icmp eq ptr %10, @P_MobjThinker
  br i1 %11, label %12, label %52

12:                                               ; preds = %7
  %13 = load ptr, ptr @save_p, align 8, !tbaa !10
  %14 = getelementptr inbounds i8, ptr %13, i32 1
  store ptr %14, ptr @save_p, align 8, !tbaa !10
  store i8 1, ptr %13, align 1, !tbaa !11
  %15 = load ptr, ptr @save_p, align 8, !tbaa !10
  %16 = ptrtoint ptr %15 to i64
  %17 = and i64 %16, 3
  %18 = sub i64 4, %17
  %19 = and i64 %18, 3
  %20 = load ptr, ptr @save_p, align 8, !tbaa !10
  %21 = getelementptr inbounds i8, ptr %20, i64 %19
  store ptr %21, ptr @save_p, align 8, !tbaa !10
  %22 = load ptr, ptr @save_p, align 8, !tbaa !10
  store ptr %22, ptr %2, align 8, !tbaa !10
  %23 = load ptr, ptr %2, align 8, !tbaa !10
  %24 = load ptr, ptr %1, align 8, !tbaa !10
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %23, ptr align 8 %24, i64 224, i1 false)
  %25 = load ptr, ptr @save_p, align 8, !tbaa !10
  %26 = getelementptr inbounds i8, ptr %25, i64 224
  store ptr %26, ptr @save_p, align 8, !tbaa !10
  %27 = load ptr, ptr %2, align 8, !tbaa !10
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 23
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  %30 = ptrtoint ptr %29 to i64
  %31 = sub i64 %30, ptrtoint (ptr @states to i64)
  %32 = sdiv exact i64 %31, 56
  %33 = inttoptr i64 %32 to ptr
  %34 = load ptr, ptr %2, align 8, !tbaa !10
  %35 = getelementptr inbounds %struct.mobj_s, ptr %34, i32 0, i32 23
  store ptr %33, ptr %35, align 8, !tbaa !12
  %36 = load ptr, ptr %2, align 8, !tbaa !10
  %37 = getelementptr inbounds %struct.mobj_s, ptr %36, i32 0, i32 31
  %38 = load ptr, ptr %37, align 8, !tbaa !17
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %51

40:                                               ; preds = %12
  %41 = load ptr, ptr %2, align 8, !tbaa !10
  %42 = getelementptr inbounds %struct.mobj_s, ptr %41, i32 0, i32 31
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = ptrtoint ptr %43 to i64
  %45 = sub i64 %44, ptrtoint (ptr @players to i64)
  %46 = sdiv exact i64 %45, 320
  %47 = add nsw i64 %46, 1
  %48 = inttoptr i64 %47 to ptr
  %49 = load ptr, ptr %2, align 8, !tbaa !10
  %50 = getelementptr inbounds %struct.mobj_s, ptr %49, i32 0, i32 31
  store ptr %48, ptr %50, align 8, !tbaa !17
  br label %51

51:                                               ; preds = %40, %12
  br label %53

52:                                               ; preds = %7
  br label %53

53:                                               ; preds = %52, %51
  %54 = load ptr, ptr %1, align 8, !tbaa !10
  %55 = getelementptr inbounds %struct.thinker_s, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  store ptr %56, ptr %1, align 8, !tbaa !10
  br label %4, !llvm.loop !18

57:                                               ; preds = %4
  %58 = load ptr, ptr @save_p, align 8, !tbaa !10
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr @save_p, align 8, !tbaa !10
  store i8 0, ptr %58, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

declare void @P_MobjThinker(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 #4 = { 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, !7, i64 8}
!6 = !{!"thinker_s", !7, i64 0, !7, i64 8, !8, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!13, !7, i64 152}
!13 = !{!"mobj_s", !6, i64 0, !14, i64 24, !14, i64 28, !14, i64 32, !7, i64 40, !7, i64 48, !14, i64 56, !14, i64 60, !14, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !14, i64 96, !14, i64 100, !14, i64 104, !14, i64 108, !14, i64 112, !14, i64 116, !14, i64 120, !14, i64 124, !14, i64 128, !7, i64 136, !14, i64 144, !7, i64 152, !14, i64 160, !14, i64 164, !14, i64 168, !14, i64 172, !7, i64 176, !14, i64 184, !14, i64 188, !7, i64 192, !14, i64 200, !15, i64 204, !7, i64 216}
!14 = !{!"int", !8, i64 0}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !16, i64 8}
!16 = !{!"short", !8, i64 0}
!17 = !{!13, !7, i64 192}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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.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.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }

@save_p = external dso_local local_unnamed_addr global ptr, align 8
@players = external global [4 x %struct.player_s], align 16
@states = external global [967 x %struct.state_t], align 16
@thinkercap = external global %struct.thinker_s, align 8

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @P_ArchiveThinkers() local_unnamed_addr #1 {
  %.012 = load ptr, ptr getelementptr inbounds (i8, ptr @thinkercap, i64 8), align 8, !tbaa !5
  %.not13 = icmp eq ptr %.012, @thinkercap
  br i1 %.not13, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %28
  %.014 = phi ptr [ %.0, %28 ], [ %.012, %0 ]
  %1 = getelementptr inbounds i8, ptr %.014, i64 16
  %2 = load ptr, ptr %1, align 8, !tbaa !10
  %3 = icmp eq ptr %2, @P_MobjThinker
  br i1 %3, label %4, label %28

4:                                                ; preds = %.lr.ph
  %5 = load ptr, ptr @save_p, align 8, !tbaa !11
  %6 = getelementptr inbounds i8, ptr %5, i64 1
  store ptr %6, ptr @save_p, align 8, !tbaa !11
  store i8 1, ptr %5, align 1, !tbaa !10
  %7 = load ptr, ptr @save_p, align 8, !tbaa !11
  %8 = ptrtoint ptr %7 to i64
  %9 = sub i64 0, %8
  %10 = and i64 %9, 3
  %11 = getelementptr inbounds i8, ptr %7, i64 %10
  store ptr %11, ptr @save_p, align 8, !tbaa !11
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(224) %11, ptr noundef nonnull align 8 dereferenceable(224) %.014, i64 224, i1 false)
  %12 = load ptr, ptr @save_p, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %12, i64 224
  store ptr %13, ptr @save_p, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %11, i64 152
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  %16 = ptrtoint ptr %15 to i64
  %17 = sub i64 %16, ptrtoint (ptr @states to i64)
  %18 = sdiv exact i64 %17, 56
  %19 = inttoptr i64 %18 to ptr
  store ptr %19, ptr %14, align 8, !tbaa !12
  %20 = getelementptr inbounds i8, ptr %11, i64 192
  %21 = load ptr, ptr %20, align 8, !tbaa !17
  %.not11 = icmp eq ptr %21, null
  br i1 %.not11, label %28, label %22

22:                                               ; preds = %4
  %23 = ptrtoint ptr %21 to i64
  %24 = sub i64 %23, ptrtoint (ptr @players to i64)
  %25 = sdiv exact i64 %24, 320
  %26 = add nsw i64 %25, 1
  %27 = inttoptr i64 %26 to ptr
  store ptr %27, ptr %20, align 8, !tbaa !17
  br label %28

28:                                               ; preds = %.lr.ph, %4, %22
  %29 = getelementptr inbounds i8, ptr %.014, i64 8
  %.0 = load ptr, ptr %29, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, @thinkercap
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %28, %0
  %30 = load ptr, ptr @save_p, align 8, !tbaa !11
  %31 = getelementptr inbounds i8, ptr %30, i64 1
  store ptr %31, ptr @save_p, align 8, !tbaa !11
  store i8 0, ptr %30, align 1, !tbaa !10
  ret void
}

declare void @P_MobjThinker(ptr noundef) #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = { "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 8}
!6 = !{!"thinker_s", !7, i64 0, !7, i64 8, !8, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 152}
!13 = !{!"mobj_s", !6, i64 0, !14, i64 24, !14, i64 28, !14, i64 32, !7, i64 40, !7, i64 48, !14, i64 56, !14, i64 60, !14, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !14, i64 96, !14, i64 100, !14, i64 104, !14, i64 108, !14, i64 112, !14, i64 116, !14, i64 120, !14, i64 124, !14, i64 128, !7, i64 136, !14, i64 144, !7, i64 152, !14, i64 160, !14, i64 164, !14, i64 168, !14, i64 172, !7, i64 176, !14, i64 184, !14, i64 188, !7, i64 192, !14, i64 200, !15, i64 204, !7, i64 216}
!14 = !{!"int", !8, i64 0}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !16, i64 8}
!16 = !{!"short", !8, i64 0}
!17 = !{!13, !7, i64 192}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass