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

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

@gameaction = external global i32, align 4
@finaletext = external dso_local global ptr, align 8
@gamemode = external global i32, align 4
@gameepisode = external global i32, align 4
@gamemap = external global i32, align 4
@finalestage = external dso_local global i32, align 4
@finalecount = external dso_local global i32, align 4
@players = external global [4 x %struct.player_s], align 16
@wipegamestate = external global i32, align 4

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

5:                                                ; preds = %0
  %6 = load i32, ptr @finalecount, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 50
  br i1 %7, label %8, label %35

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

9:                                                ; preds = %22, %8
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 4
  br i1 %11, label %12, label %25

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

20:                                               ; preds = %12
  br label %25

21:                                               ; preds = %12
  br label %22

22:                                               ; preds = %21
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %1, align 4, !tbaa !5
  br label %9, !llvm.loop !14

25:                                               ; preds = %20, %9
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = icmp slt i32 %26, 4
  br i1 %27, label %28, label %34

28:                                               ; preds = %25
  %29 = load i32, ptr @gamemap, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 30
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  call void @F_StartCast()
  br label %33

32:                                               ; preds = %28
  store i32 8, ptr @gameaction, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %32, %31
  br label %34

34:                                               ; preds = %33, %25
  br label %35

35:                                               ; preds = %34, %5, %0
  %36 = load i32, ptr @finalecount, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr @finalecount, align 4, !tbaa !5
  %38 = load i32, ptr @finalestage, align 4, !tbaa !5
  %39 = icmp eq i32 %38, 2
  br i1 %39, label %40, label %41

40:                                               ; preds = %35
  call void @F_CastTicker()
  store i32 1, ptr %2, align 4
  br label %62

41:                                               ; preds = %35
  %42 = load i32, ptr @gamemode, align 4, !tbaa !5
  %43 = icmp eq i32 %42, 2
  br i1 %43, label %44, label %45

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

45:                                               ; preds = %41
  %46 = load i32, ptr @finalestage, align 4, !tbaa !5
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %61, label %48

48:                                               ; preds = %45
  %49 = load i32, ptr @finalecount, align 4, !tbaa !5
  %50 = load ptr, ptr @finaletext, align 8, !tbaa !16
  %51 = call i64 @strlen(ptr noundef %50) #5
  %52 = trunc i64 %51 to i32
  %53 = mul nsw i32 %52, 3
  %54 = add nsw i32 %53, 250
  %55 = icmp sgt i32 %49, %54
  br i1 %55, label %56, label %61

56:                                               ; preds = %48
  store i32 0, ptr @finalecount, align 4, !tbaa !5
  store i32 1, ptr @finalestage, align 4, !tbaa !5
  store i32 -1, ptr @wipegamestate, align 4, !tbaa !5
  %57 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %58 = icmp eq i32 %57, 3
  br i1 %58, label %59, label %60

59:                                               ; preds = %56
  call void @S_StartMusic(i32 noundef 30)
  br label %60

60:                                               ; preds = %59, %56
  br label %61

61:                                               ; preds = %60, %48, %45
  store i32 0, ptr %2, align 4
  br label %62

62:                                               ; preds = %61, %44, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  %63 = load i32, ptr %2, align 4
  switch i32 %63, label %65 [
    i32 0, label %64
    i32 1, label %64
  ]

64:                                               ; preds = %62, %62
  ret void

65:                                               ; preds = %62
  unreachable
}

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

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

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

declare void @S_StartMusic(i32 noundef) #3

; 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 = { nounwind willreturn memory(read) "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 }
attributes #5 = { nounwind willreturn memory(read) }

!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, !7, i64 19}
!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"}
!16 = !{!11, !11, i64 0}


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

@gameaction = external local_unnamed_addr global i32, align 4
@finaletext = external dso_local local_unnamed_addr global ptr, align 8
@gamemode = external local_unnamed_addr global i32, align 4
@gameepisode = external local_unnamed_addr global i32, align 4
@gamemap = external local_unnamed_addr global i32, align 4
@finalestage = external dso_local local_unnamed_addr global i32, align 4
@finalecount = external dso_local local_unnamed_addr global i32, align 4
@players = external local_unnamed_addr global [4 x %struct.player_s], align 16
@wipegamestate = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @F_Ticker() local_unnamed_addr #0 {
  %1 = load i32, ptr @gamemode, align 4, !tbaa !5
  %2 = icmp eq i32 %1, 2
  %3 = load i32, ptr @finalecount, align 4
  %4 = icmp sgt i32 %3, 50
  %or.cond = select i1 %2, i1 %4, i1 false
  br i1 %or.cond, label %.preheader.preheader, label %.critedge

.preheader.preheader:                             ; preds = %0
  %5 = load i8, ptr getelementptr inbounds (i8, ptr @players, i64 19), align 1, !tbaa !9
  %.not = icmp eq i8 %5, 0
  %6 = load i8, ptr getelementptr inbounds (i8, ptr @players, i64 339), align 1
  %.not.1 = icmp eq i8 %6, 0
  %or.cond11 = select i1 %.not, i1 %.not.1, i1 false
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @players, i64 659), align 1
  %.not.2 = icmp eq i8 %7, 0
  %or.cond12 = select i1 %or.cond11, i1 %.not.2, i1 false
  %8 = load i8, ptr getelementptr inbounds (i8, ptr @players, i64 979), align 1
  %.not.3 = icmp eq i8 %8, 0
  %or.cond13 = select i1 %or.cond12, i1 %.not.3, i1 false
  br i1 %or.cond13, label %.critedge, label %9

9:                                                ; preds = %.preheader.preheader
  %10 = load i32, ptr @gamemap, align 4, !tbaa !5
  %11 = icmp eq i32 %10, 30
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @F_StartCast()
  %.pre = load i32, ptr @finalecount, align 4, !tbaa !5
  br label %.critedge

13:                                               ; preds = %9
  store i32 8, ptr @gameaction, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.preheader.preheader, %13, %12, %0
  %14 = phi i32 [ %3, %13 ], [ %.pre, %12 ], [ %3, %0 ], [ %3, %.preheader.preheader ]
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr @finalecount, align 4, !tbaa !5
  %16 = load i32, ptr @finalestage, align 4, !tbaa !5
  %17 = icmp eq i32 %16, 2
  br i1 %17, label %18, label %19

18:                                               ; preds = %.critedge
  tail call void @F_CastTicker()
  br label %32

19:                                               ; preds = %.critedge
  %20 = load i32, ptr @gamemode, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 2
  %.not6 = icmp eq i32 %16, 0
  %or.cond8 = and i1 %.not6, %21
  br i1 %or.cond8, label %22, label %32

22:                                               ; preds = %19
  %23 = load ptr, ptr @finaletext, align 8, !tbaa !14
  %24 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %23) #3
  %25 = trunc i64 %24 to i32
  %26 = mul nsw i32 %25, 3
  %27 = add nsw i32 %26, 250
  %.not7 = icmp slt i32 %14, %27
  br i1 %.not7, label %32, label %28

28:                                               ; preds = %22
  store i32 0, ptr @finalecount, align 4, !tbaa !5
  store i32 1, ptr @finalestage, align 4, !tbaa !5
  store i32 -1, ptr @wipegamestate, align 4, !tbaa !5
  %29 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 3
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  tail call void @S_StartMusic(i32 noundef 30) #4
  br label %32

32:                                               ; preds = %22, %31, %28, %19, %18
  ret void
}

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

declare void @S_StartMusic(i32 noundef) local_unnamed_addr #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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }
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, !7, i64 19}
!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 = !{!11, !11, i64 0}


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