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

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

@paused = external global i32, align 4
@netgame = external global i32, align 4
@menuactive = external global i32, align 4
@demoplayback = external global i32, align 4
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external global i32, align 4
@playeringame = external global [4 x i32], align 16
@leveltime = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @P_RunThinkers() #0

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local void @P_Ticker() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %3 = load i32, ptr @paused, align 4, !tbaa !5
  %4 = icmp ne i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %0
  store i32 1, ptr %2, align 4
  br label %44

6:                                                ; preds = %0
  %7 = load i32, ptr @netgame, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %23, label %9

9:                                                ; preds = %6
  %10 = load i32, ptr @menuactive, align 4, !tbaa !5
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %23

12:                                               ; preds = %9
  %13 = load i32, ptr @demoplayback, align 4, !tbaa !5
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %23, label %15

15:                                               ; preds = %12
  %16 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.player_s, ptr %18, i32 0, i32 3
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = icmp ne i32 %20, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %15
  store i32 1, ptr %2, align 4
  br label %44

23:                                               ; preds = %15, %12, %9, %6
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %38, %23
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 4
  br i1 %26, label %27, label %41

27:                                               ; preds = %24
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [4 x i32], ptr @playeringame, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %37

33:                                               ; preds = %27
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %35
  call void @P_PlayerThink(ptr noundef %36)
  br label %37

37:                                               ; preds = %33, %27
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %1, align 4, !tbaa !5
  br label %24, !llvm.loop !14

41:                                               ; preds = %24
  call void @P_RunThinkers()
  call void @P_UpdateSpecials()
  call void @P_RespawnSpecials()
  %42 = load i32, ptr @leveltime, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr @leveltime, align 4, !tbaa !5
  store i32 0, ptr %2, align 4
  br label %44

44:                                               ; preds = %41, %22, %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %45 = load i32, ptr %2, align 4
  switch i32 %45, label %47 [
    i32 0, label %46
    i32 1, label %46
  ]

46:                                               ; preds = %44, %44
  ret void

47:                                               ; preds = %44
  unreachable
}

declare void @P_PlayerThink(ptr noundef) #2

declare void @P_UpdateSpecials() #2

declare void @P_RespawnSpecials() #2

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 = { "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, !6, i64 20}
!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 = distinct !{!14, !15}
!15 = !{!"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 }

@paused = external local_unnamed_addr global i32, align 4
@netgame = external local_unnamed_addr global i32, align 4
@menuactive = external local_unnamed_addr global i32, align 4
@demoplayback = external local_unnamed_addr global i32, align 4
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external local_unnamed_addr global i32, align 4
@playeringame = external local_unnamed_addr global [4 x i32], align 16
@leveltime = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @P_RunThinkers() local_unnamed_addr #0

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

2:                                                ; preds = %0
  %3 = load i32, ptr @netgame, align 4, !tbaa !5
  %4 = icmp ne i32 %3, 0
  %5 = load i32, ptr @menuactive, align 4
  %6 = icmp eq i32 %5, 0
  %or.cond.not9 = select i1 %4, i1 true, i1 %6
  %7 = load i32, ptr @demoplayback, align 4
  %8 = icmp ne i32 %7, 0
  %or.cond3 = select i1 %or.cond.not9, i1 true, i1 %8
  br i1 %or.cond3, label %14, label %9

9:                                                ; preds = %2
  %10 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %11, i32 3
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %.not10 = icmp eq i32 %13, 1
  br i1 %.not10, label %14, label %29

14:                                               ; preds = %9, %2
  %15 = load i32, ptr @playeringame, align 16, !tbaa !5
  %.not11 = icmp eq i32 %15, 0
  br i1 %.not11, label %17, label %16

16:                                               ; preds = %14
  tail call void @P_PlayerThink(ptr noundef nonnull @players) #2
  br label %17

17:                                               ; preds = %14, %16
  %18 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 4), align 4, !tbaa !5
  %.not11.1 = icmp eq i32 %18, 0
  br i1 %.not11.1, label %20, label %19

19:                                               ; preds = %17
  tail call void @P_PlayerThink(ptr noundef nonnull getelementptr inbounds (i8, ptr @players, i64 320)) #2
  br label %20

20:                                               ; preds = %19, %17
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 8), align 8, !tbaa !5
  %.not11.2 = icmp eq i32 %21, 0
  br i1 %.not11.2, label %23, label %22

22:                                               ; preds = %20
  tail call void @P_PlayerThink(ptr noundef nonnull getelementptr inbounds (i8, ptr @players, i64 640)) #2
  br label %23

23:                                               ; preds = %22, %20
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 12), align 4, !tbaa !5
  %.not11.3 = icmp eq i32 %24, 0
  br i1 %.not11.3, label %26, label %25

25:                                               ; preds = %23
  tail call void @P_PlayerThink(ptr noundef nonnull getelementptr inbounds (i8, ptr @players, i64 960)) #2
  br label %26

26:                                               ; preds = %25, %23
  tail call void @P_RunThinkers()
  tail call void @P_UpdateSpecials() #2
  tail call void @P_RespawnSpecials() #2
  %27 = load i32, ptr @leveltime, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr @leveltime, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %9, %0, %26
  ret void
}

declare void @P_PlayerThink(ptr noundef) local_unnamed_addr #1

declare void @P_UpdateSpecials() local_unnamed_addr #1

declare void @P_RespawnSpecials() 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, !6, i64 20}
!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}


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