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

@playeringame = external global [4 x i32], align 16
@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

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

4:                                                ; preds = %64, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 4
  br i1 %6, label %7, label %67

7:                                                ; preds = %4
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [4 x i32], ptr @playeringame, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %7
  br label %64

14:                                               ; preds = %7
  %15 = load ptr, ptr @save_p, align 8, !tbaa !9
  %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 !9
  %21 = getelementptr inbounds i8, ptr %20, i64 %19
  store ptr %21, ptr @save_p, align 8, !tbaa !9
  %22 = load ptr, ptr @save_p, align 8, !tbaa !9
  store ptr %22, ptr %3, align 8, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !9
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %25
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %23, ptr align 16 %26, i64 320, i1 false)
  %27 = load ptr, ptr @save_p, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %27, i64 320
  store ptr %28, ptr @save_p, align 8, !tbaa !9
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %60, %14
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = icmp slt i32 %30, 2
  br i1 %31, label %32, label %63

32:                                               ; preds = %29
  %33 = load ptr, ptr %3, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.player_s, ptr %33, i32 0, i32 33
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %34, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.pspdef_t, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %59

41:                                               ; preds = %32
  %42 = load ptr, ptr %3, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.player_s, ptr %42, i32 0, i32 33
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %43, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.pspdef_t, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !11
  %49 = ptrtoint ptr %48 to i64
  %50 = sub i64 %49, ptrtoint (ptr @states to i64)
  %51 = sdiv exact i64 %50, 56
  %52 = inttoptr i64 %51 to ptr
  %53 = load ptr, ptr %3, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.player_s, ptr %53, i32 0, i32 33
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2 x %struct.pspdef_t], ptr %54, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.pspdef_t, ptr %57, i32 0, i32 0
  store ptr %52, ptr %58, align 8, !tbaa !11
  br label %59

59:                                               ; preds = %41, %32
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %2, align 4, !tbaa !5
  br label %29, !llvm.loop !13

63:                                               ; preds = %29
  br label %64

64:                                               ; preds = %63, %13
  %65 = load i32, ptr %1, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !15

67:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

; 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) #2

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!"", !10, i64 0, !6, i64 8, !6, i64 12, !6, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @P_ArchivePlayers() local_unnamed_addr #0 {
  %1 = load i32, ptr @playeringame, align 16, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %0
  %3 = load ptr, ptr @save_p, align 8, !tbaa !9
  %4 = ptrtoint ptr %3 to i64
  %5 = sub i64 0, %4
  %6 = and i64 %5, 3
  %7 = getelementptr inbounds i8, ptr %3, i64 %6
  store ptr %7, ptr @save_p, align 8, !tbaa !9
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(320) %7, ptr noundef nonnull align 16 dereferenceable(320) @players, i64 320, i1 false)
  %8 = load ptr, ptr @save_p, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 320
  store ptr %9, ptr @save_p, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %7, i64 264
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %.not14 = icmp eq ptr %11, null
  br i1 %.not14, label %17, label %12

12:                                               ; preds = %2
  %13 = ptrtoint ptr %11 to i64
  %14 = sub i64 %13, ptrtoint (ptr @states to i64)
  %15 = sdiv exact i64 %14, 56
  %16 = inttoptr i64 %15 to ptr
  store ptr %16, ptr %10, align 8, !tbaa !11
  br label %17

17:                                               ; preds = %2, %12
  %18 = getelementptr inbounds i8, ptr %7, i64 288
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %.not14.1 = icmp eq ptr %19, null
  br i1 %.not14.1, label %.loopexit, label %20

20:                                               ; preds = %17
  %21 = ptrtoint ptr %19 to i64
  %22 = sub i64 %21, ptrtoint (ptr @states to i64)
  %23 = sdiv exact i64 %22, 56
  %24 = inttoptr i64 %23 to ptr
  store ptr %24, ptr %18, align 8, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %17, %20, %0
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 4), align 4, !tbaa !5
  %.not.1 = icmp eq i32 %25, 0
  br i1 %.not.1, label %.loopexit.1, label %26

26:                                               ; preds = %.loopexit
  %27 = load ptr, ptr @save_p, align 8, !tbaa !9
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 0, %28
  %30 = and i64 %29, 3
  %31 = getelementptr inbounds i8, ptr %27, i64 %30
  store ptr %31, ptr @save_p, align 8, !tbaa !9
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(320) %31, ptr noundef nonnull align 16 dereferenceable(320) getelementptr inbounds (i8, ptr @players, i64 320), i64 320, i1 false)
  %32 = load ptr, ptr @save_p, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i64 320
  store ptr %33, ptr @save_p, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %31, i64 264
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %.not14.119 = icmp eq ptr %35, null
  br i1 %.not14.119, label %41, label %36

36:                                               ; preds = %26
  %37 = ptrtoint ptr %35 to i64
  %38 = sub i64 %37, ptrtoint (ptr @states to i64)
  %39 = sdiv exact i64 %38, 56
  %40 = inttoptr i64 %39 to ptr
  store ptr %40, ptr %34, align 8, !tbaa !11
  br label %41

41:                                               ; preds = %36, %26
  %42 = getelementptr inbounds i8, ptr %31, i64 288
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  %.not14.1.1 = icmp eq ptr %43, null
  br i1 %.not14.1.1, label %.loopexit.1, label %44

44:                                               ; preds = %41
  %45 = ptrtoint ptr %43 to i64
  %46 = sub i64 %45, ptrtoint (ptr @states to i64)
  %47 = sdiv exact i64 %46, 56
  %48 = inttoptr i64 %47 to ptr
  store ptr %48, ptr %42, align 8, !tbaa !11
  br label %.loopexit.1

.loopexit.1:                                      ; preds = %41, %44, %.loopexit
  %49 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 8), align 8, !tbaa !5
  %.not.2 = icmp eq i32 %49, 0
  br i1 %.not.2, label %.loopexit.2, label %50

50:                                               ; preds = %.loopexit.1
  %51 = load ptr, ptr @save_p, align 8, !tbaa !9
  %52 = ptrtoint ptr %51 to i64
  %53 = sub i64 0, %52
  %54 = and i64 %53, 3
  %55 = getelementptr inbounds i8, ptr %51, i64 %54
  store ptr %55, ptr @save_p, align 8, !tbaa !9
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(320) %55, ptr noundef nonnull align 16 dereferenceable(320) getelementptr inbounds (i8, ptr @players, i64 640), i64 320, i1 false)
  %56 = load ptr, ptr @save_p, align 8, !tbaa !9
  %57 = getelementptr inbounds i8, ptr %56, i64 320
  store ptr %57, ptr @save_p, align 8, !tbaa !9
  %58 = getelementptr inbounds i8, ptr %55, i64 264
  %59 = load ptr, ptr %58, align 8, !tbaa !11
  %.not14.2 = icmp eq ptr %59, null
  br i1 %.not14.2, label %65, label %60

60:                                               ; preds = %50
  %61 = ptrtoint ptr %59 to i64
  %62 = sub i64 %61, ptrtoint (ptr @states to i64)
  %63 = sdiv exact i64 %62, 56
  %64 = inttoptr i64 %63 to ptr
  store ptr %64, ptr %58, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %60, %50
  %66 = getelementptr inbounds i8, ptr %55, i64 288
  %67 = load ptr, ptr %66, align 8, !tbaa !11
  %.not14.1.2 = icmp eq ptr %67, null
  br i1 %.not14.1.2, label %.loopexit.2, label %68

68:                                               ; preds = %65
  %69 = ptrtoint ptr %67 to i64
  %70 = sub i64 %69, ptrtoint (ptr @states to i64)
  %71 = sdiv exact i64 %70, 56
  %72 = inttoptr i64 %71 to ptr
  store ptr %72, ptr %66, align 8, !tbaa !11
  br label %.loopexit.2

.loopexit.2:                                      ; preds = %65, %68, %.loopexit.1
  %73 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 12), align 4, !tbaa !5
  %.not.3 = icmp eq i32 %73, 0
  br i1 %.not.3, label %.loopexit.3, label %74

74:                                               ; preds = %.loopexit.2
  %75 = load ptr, ptr @save_p, align 8, !tbaa !9
  %76 = ptrtoint ptr %75 to i64
  %77 = sub i64 0, %76
  %78 = and i64 %77, 3
  %79 = getelementptr inbounds i8, ptr %75, i64 %78
  store ptr %79, ptr @save_p, align 8, !tbaa !9
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(320) %79, ptr noundef nonnull align 16 dereferenceable(320) getelementptr inbounds (i8, ptr @players, i64 960), i64 320, i1 false)
  %80 = load ptr, ptr @save_p, align 8, !tbaa !9
  %81 = getelementptr inbounds i8, ptr %80, i64 320
  store ptr %81, ptr @save_p, align 8, !tbaa !9
  %82 = getelementptr inbounds i8, ptr %79, i64 264
  %83 = load ptr, ptr %82, align 8, !tbaa !11
  %.not14.3 = icmp eq ptr %83, null
  br i1 %.not14.3, label %89, label %84

84:                                               ; preds = %74
  %85 = ptrtoint ptr %83 to i64
  %86 = sub i64 %85, ptrtoint (ptr @states to i64)
  %87 = sdiv exact i64 %86, 56
  %88 = inttoptr i64 %87 to ptr
  store ptr %88, ptr %82, align 8, !tbaa !11
  br label %89

89:                                               ; preds = %84, %74
  %90 = getelementptr inbounds i8, ptr %79, i64 288
  %91 = load ptr, ptr %90, align 8, !tbaa !11
  %.not14.1.3 = icmp eq ptr %91, null
  br i1 %.not14.1.3, label %.loopexit.3, label %92

92:                                               ; preds = %89
  %93 = ptrtoint ptr %91 to i64
  %94 = sub i64 %93, ptrtoint (ptr @states to i64)
  %95 = sdiv exact i64 %94, 56
  %96 = inttoptr i64 %95 to ptr
  store ptr %96, ptr %90, align 8, !tbaa !11
  br label %.loopexit.3

.loopexit.3:                                      ; preds = %89, %92, %.loopexit.2
  ret void
}

; 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) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{!"", !10, i64 0, !6, i64 8, !6, i64 12, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass