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

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

@st_statusbaron = external hidden global i32, align 4
@st_gamestate = external hidden global i32, align 4
@st_firsttime = external hidden global i32, align 4
@plyr = external hidden global ptr, align 8
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external global i32, align 4
@st_faceindex = external hidden global i32, align 4
@oldweaponsowned = external hidden global [9 x i32], align 16
@st_oldhealth = external hidden global i32, align 4
@keyboxes = external hidden global [3 x i32], align 4
@st_oldchat = external hidden global i32, align 4
@st_chat = external hidden global i32, align 4
@st_clock = external hidden global i32, align 4
@st_palette = external hidden global i32, align 4
@st_chatstate = external hidden global i32, align 4
@st_cursoron = external hidden global i32, align 4

; 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

; Function Attrs: nounwind uwtable
define dso_local void @ST_initData() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 1, ptr @st_firsttime, align 4, !tbaa !5
  %2 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %3
  store ptr %4, ptr @plyr, align 8, !tbaa !9
  store i32 0, ptr @st_clock, align 4, !tbaa !5
  store i32 0, ptr @st_chatstate, align 4, !tbaa !5
  store i32 1, ptr @st_gamestate, align 4, !tbaa !5
  store i32 1, ptr @st_statusbaron, align 4, !tbaa !5
  store i32 0, ptr @st_chat, align 4, !tbaa !5
  store i32 0, ptr @st_oldchat, align 4, !tbaa !5
  store i32 0, ptr @st_cursoron, align 4, !tbaa !5
  store i32 0, ptr @st_faceindex, align 4, !tbaa !5
  store i32 -1, ptr @st_palette, align 4, !tbaa !5
  store i32 -1, ptr @st_oldhealth, align 4, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %18, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 9
  br i1 %7, label %8, label %21

8:                                                ; preds = %5
  %9 = load ptr, ptr @plyr, align 8, !tbaa !9
  %10 = getelementptr inbounds %struct.player_s, ptr %9, i32 0, i32 16
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [9 x i32], ptr %10, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [9 x i32], ptr @oldweaponsowned, i64 0, i64 %16
  store i32 %14, ptr %17, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %8
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !11

21:                                               ; preds = %5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %29, %21
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 3
  br i1 %24, label %25, label %32

25:                                               ; preds = %22
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [3 x i32], ptr @keyboxes, i64 0, i64 %27
  store i32 -1, ptr %28, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %25
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %1, align 4, !tbaa !5
  br label %22, !llvm.loop !13

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

declare void @STlib_init() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@st_statusbaron = external hidden local_unnamed_addr global i32, align 4
@st_gamestate = external hidden local_unnamed_addr global i32, align 4
@st_firsttime = external hidden local_unnamed_addr global i32, align 4
@plyr = external hidden local_unnamed_addr global ptr, align 8
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external local_unnamed_addr global i32, align 4
@st_faceindex = external hidden local_unnamed_addr global i32, align 4
@oldweaponsowned = external hidden local_unnamed_addr global [9 x i32], align 16
@st_oldhealth = external hidden local_unnamed_addr global i32, align 4
@keyboxes = external hidden local_unnamed_addr global [3 x i32], align 4
@st_oldchat = external hidden local_unnamed_addr global i32, align 4
@st_chat = external hidden local_unnamed_addr global i32, align 4
@st_clock = external hidden local_unnamed_addr global i32, align 4
@st_palette = external hidden local_unnamed_addr global i32, align 4
@st_chatstate = external hidden local_unnamed_addr global i32, align 4
@st_cursoron = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @ST_initData() local_unnamed_addr #0 {
.preheader.preheader:
  store i32 1, ptr @st_firsttime, align 4, !tbaa !5
  %0 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %1 = sext i32 %0 to i64
  %2 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %1
  store ptr %2, ptr @plyr, align 8, !tbaa !9
  store i32 0, ptr @st_clock, align 4, !tbaa !5
  store i32 0, ptr @st_chatstate, align 4, !tbaa !5
  store i32 1, ptr @st_gamestate, align 4, !tbaa !5
  store i32 1, ptr @st_statusbaron, align 4, !tbaa !5
  store i32 0, ptr @st_chat, align 4, !tbaa !5
  store i32 0, ptr @st_oldchat, align 4, !tbaa !5
  store i32 0, ptr @st_cursoron, align 4, !tbaa !5
  store i32 0, ptr @st_faceindex, align 4, !tbaa !5
  store i32 -1, ptr @st_palette, align 4, !tbaa !5
  store i32 -1, ptr @st_oldhealth, align 4, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 124
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(36) @oldweaponsowned, ptr noundef nonnull align 4 dereferenceable(36) %3, i64 36, i1 false), !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) @keyboxes, i8 -1, i64 12, i1 false), !tbaa !5
  tail call void @STlib_init() #4
  ret void
}

declare void @STlib_init() local_unnamed_addr #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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass