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

; 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.mapthing_t = type { i16, i16, i16, i16, i16 }
%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 }

@players = external dso_local global [4 x %struct.player_s], align 16
@gameaction = external dso_local global i32, align 4
@deathmatch = external dso_local global i32, align 4
@netgame = external dso_local global i32, align 4
@playerstarts = external global [4 x %struct.mapthing_t], align 16

; 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 @G_DoReborn(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %5 = load i32, ptr @netgame, align 4, !tbaa !5
  %6 = icmp ne i32 %5, 0
  br i1 %6, label %8, label %7

7:                                                ; preds = %1
  store i32 1, ptr @gameaction, align 4, !tbaa !5
  br label %67

8:                                                ; preds = %1
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.player_s, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 16, !tbaa !9
  %14 = getelementptr inbounds %struct.mobj_s, ptr %13, i32 0, i32 31
  store ptr null, ptr %14, align 8, !tbaa !14
  %15 = load i32, ptr @deathmatch, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %8
  %18 = load i32, ptr %2, align 4, !tbaa !5
  call void @G_DeathMatchSpawnPlayer(i32 noundef %18)
  store i32 1, ptr %4, align 4
  br label %68

19:                                               ; preds = %8
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %22
  %24 = call i32 @G_CheckSpot(i32 noundef %20, ptr noundef %23)
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %30

26:                                               ; preds = %19
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %28
  call void @P_SpawnPlayer(ptr noundef %29)
  store i32 1, ptr %4, align 4
  br label %68

30:                                               ; preds = %19
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %31

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

34:                                               ; preds = %31
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %37
  %39 = call i32 @G_CheckSpot(i32 noundef %35, ptr noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %59

41:                                               ; preds = %34
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  %44 = trunc i32 %43 to i16
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.mapthing_t, ptr %47, i32 0, i32 3
  store i16 %44, ptr %48, align 2, !tbaa !18
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %50
  call void @P_SpawnPlayer(ptr noundef %51)
  %52 = load i32, ptr %3, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  %54 = trunc i32 %53 to i16
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.mapthing_t, ptr %57, i32 0, i32 3
  store i16 %54, ptr %58, align 2, !tbaa !18
  store i32 1, ptr %4, align 4
  br label %68

59:                                               ; preds = %34
  br label %60

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

63:                                               ; preds = %31
  %64 = load i32, ptr %2, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %65
  call void @P_SpawnPlayer(ptr noundef %66)
  br label %67

67:                                               ; preds = %63, %7
  store i32 0, ptr %4, align 4
  br label %68

68:                                               ; preds = %67, %41, %26, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  %69 = load i32, ptr %4, align 4
  switch i32 %69, label %71 [
    i32 0, label %70
    i32 1, label %70
  ]

70:                                               ; preds = %68, %68
  ret void

71:                                               ; preds = %68
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @G_CheckSpot(i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @G_DeathMatchSpawnPlayer(i32 noundef) #1

declare void @P_SpawnPlayer(ptr noundef) #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, !11, i64 0}
!10 = !{!"player_s", !11, i64 0, !6, i64 8, !12, 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, !11, i64 224, !6, i64 232, !6, i64 236, !11, i64 240, !6, i64 248, !6, i64 252, !6, i64 256, !7, i64 264, !6, i64 312}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !11, i64 192}
!15 = !{!"mobj_s", !16, i64 0, !6, i64 24, !6, i64 28, !6, i64 32, !11, i64 40, !11, i64 48, !6, i64 56, !6, i64 60, !6, i64 64, !11, i64 72, !11, i64 80, !11, 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, !11, i64 136, !6, i64 144, !11, i64 152, !6, i64 160, !6, i64 164, !6, i64 168, !6, i64 172, !11, i64 176, !6, i64 184, !6, i64 188, !11, i64 192, !6, i64 200, !17, i64 204, !11, i64 216}
!16 = !{!"thinker_s", !11, i64 0, !11, i64 8, !7, i64 16}
!17 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!18 = !{!17, !13, i64 6}
!19 = distinct !{!19, !20}
!20 = !{!"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.mapthing_t = type { i16, i16, i16, i16, i16 }

@players = external dso_local local_unnamed_addr global [4 x %struct.player_s], align 16
@gameaction = external dso_local local_unnamed_addr global i32, align 4
@deathmatch = external dso_local local_unnamed_addr global i32, align 4
@netgame = external dso_local local_unnamed_addr global i32, align 4
@playerstarts = external global [4 x %struct.mapthing_t], align 16

; Function Attrs: nounwind uwtable
define dso_local void @G_DoReborn(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @netgame, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %4

3:                                                ; preds = %1
  store i32 1, ptr @gameaction, align 4, !tbaa !5
  br label %24

4:                                                ; preds = %1
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %5
  %7 = load ptr, ptr %6, align 16, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 192
  store ptr null, ptr %8, align 8, !tbaa !14
  %9 = load i32, ptr @deathmatch, align 4, !tbaa !5
  %.not15 = icmp eq i32 %9, 0
  br i1 %.not15, label %11, label %10

10:                                               ; preds = %4
  tail call void @G_DeathMatchSpawnPlayer(i32 noundef %0)
  br label %24

11:                                               ; preds = %4
  %12 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %5
  %13 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %12)
  %.not16 = icmp eq i32 %13, 0
  br i1 %.not16, label %.preheader.preheader, label %15

.preheader.preheader:                             ; preds = %11
  %14 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull @playerstarts)
  %.not17 = icmp eq i32 %14, 0
  br i1 %.not17, label %.preheader.1, label %16

15:                                               ; preds = %11
  tail call void @P_SpawnPlayer(ptr noundef nonnull %12) #2
  br label %24

16:                                               ; preds = %.preheader.3, %.preheader.2, %.preheader.1, %.preheader.preheader
  %.019.lcssa.wide = phi i16 [ 1, %.preheader.preheader ], [ 2, %.preheader.1 ], [ 3, %.preheader.2 ], [ 4, %.preheader.3 ]
  %.lcssa = phi ptr [ @playerstarts, %.preheader.preheader ], [ getelementptr inbounds (i8, ptr @playerstarts, i64 10), %.preheader.1 ], [ getelementptr inbounds (i8, ptr @playerstarts, i64 20), %.preheader.2 ], [ getelementptr inbounds (i8, ptr @playerstarts, i64 30), %.preheader.3 ]
  %17 = trunc i32 %0 to i16
  %18 = add i16 %17, 1
  %19 = getelementptr inbounds i8, ptr %.lcssa, i64 6
  store i16 %18, ptr %19, align 2, !tbaa !18
  tail call void @P_SpawnPlayer(ptr noundef nonnull %.lcssa) #2
  store i16 %.019.lcssa.wide, ptr %19, align 2, !tbaa !18
  br label %24

.preheader.1:                                     ; preds = %.preheader.preheader
  %20 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @playerstarts, i64 10))
  %.not17.1 = icmp eq i32 %20, 0
  br i1 %.not17.1, label %.preheader.2, label %16

.preheader.2:                                     ; preds = %.preheader.1
  %21 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @playerstarts, i64 20))
  %.not17.2 = icmp eq i32 %21, 0
  br i1 %.not17.2, label %.preheader.3, label %16

.preheader.3:                                     ; preds = %.preheader.2
  %22 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @playerstarts, i64 30))
  %.not17.3 = icmp eq i32 %22, 0
  br i1 %.not17.3, label %23, label %16

23:                                               ; preds = %.preheader.3
  tail call void @P_SpawnPlayer(ptr noundef nonnull %12) #2
  br label %24

24:                                               ; preds = %3, %23, %16, %15, %10
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @G_CheckSpot(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @G_DeathMatchSpawnPlayer(i32 noundef) local_unnamed_addr #0

declare void @P_SpawnPlayer(ptr noundef) local_unnamed_addr #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 = { "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, !11, i64 0}
!10 = !{!"player_s", !11, i64 0, !6, i64 8, !12, 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, !11, i64 224, !6, i64 232, !6, i64 236, !11, i64 240, !6, i64 248, !6, i64 252, !6, i64 256, !7, i64 264, !6, i64 312}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !11, i64 192}
!15 = !{!"mobj_s", !16, i64 0, !6, i64 24, !6, i64 28, !6, i64 32, !11, i64 40, !11, i64 48, !6, i64 56, !6, i64 60, !6, i64 64, !11, i64 72, !11, i64 80, !11, 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, !11, i64 136, !6, i64 144, !11, i64 152, !6, i64 160, !6, i64 164, !6, i64 168, !6, i64 172, !11, i64 176, !6, i64 184, !6, i64 188, !11, i64 192, !6, i64 200, !17, i64 204, !11, i64 216}
!16 = !{!"thinker_s", !11, i64 0, !11, i64 8, !7, i64 16}
!17 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!18 = !{!17, !13, i64 6}


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