; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (wi_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.wbstartstruct_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [4 x %struct.wbplayerstruct_t] }
%struct.wbplayerstruct_t = type { i32, i32, i32, i32, i32, [4 x i32], i32 }
%struct.anim_t = type { i32, i32, i32, %struct.point_t, i32, i32, [3 x ptr], i32, i32, i32, i32 }
%struct.point_t = type { i32, i32 }

@wbs = external hidden global ptr, align 8
@gamemode = external global i32, align 4
@NUMANIMS = external hidden global [4 x i32], align 16
@anims = external hidden global [4 x ptr], align 16
@bcnt = 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 @WI_initAnimatedBack() #1 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %4 = load i32, ptr @gamemode, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 2
  br i1 %5, label %6, label %7

6:                                                ; preds = %0
  store i32 1, ptr %3, align 4
  br label %87

7:                                                ; preds = %0
  %8 = load ptr, ptr @wbs, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.wbstartstruct_t, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = icmp sgt i32 %10, 2
  br i1 %11, label %12, label %13

12:                                               ; preds = %7
  store i32 1, ptr %3, align 4
  br label %87

13:                                               ; preds = %7
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %83, %13
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = load ptr, ptr @wbs, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.wbstartstruct_t, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x i32], ptr @NUMANIMS, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp slt i32 %15, %21
  br i1 %22, label %23, label %86

23:                                               ; preds = %14
  %24 = load ptr, ptr @wbs, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.wbstartstruct_t, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [4 x ptr], ptr @anims, i64 0, i64 %27
  %29 = load ptr, ptr %28, align 8, !tbaa !9
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds %struct.anim_t, ptr %29, i64 %31
  store ptr %32, ptr %2, align 8, !tbaa !9
  %33 = load ptr, ptr %2, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.anim_t, ptr %33, i32 0, i32 9
  store i32 -1, ptr %34, align 8, !tbaa !13
  %35 = load ptr, ptr %2, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.anim_t, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !16
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %50

39:                                               ; preds = %23
  %40 = load i32, ptr @bcnt, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  %42 = call i32 @M_Random()
  %43 = load ptr, ptr %2, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.anim_t, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !17
  %46 = srem i32 %42, %45
  %47 = add nsw i32 %41, %46
  %48 = load ptr, ptr %2, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.anim_t, ptr %48, i32 0, i32 7
  store i32 %47, ptr %49, align 8, !tbaa !18
  br label %82

50:                                               ; preds = %23
  %51 = load ptr, ptr %2, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.anim_t, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !16
  %54 = icmp eq i32 %53, 1
  br i1 %54, label %55, label %70

55:                                               ; preds = %50
  %56 = load i32, ptr @bcnt, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = load ptr, ptr %2, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.anim_t, ptr %58, i32 0, i32 5
  %60 = load i32, ptr %59, align 8, !tbaa !19
  %61 = add nsw i32 %57, %60
  %62 = call i32 @M_Random()
  %63 = load ptr, ptr %2, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.anim_t, ptr %63, i32 0, i32 4
  %65 = load i32, ptr %64, align 4, !tbaa !20
  %66 = srem i32 %62, %65
  %67 = add nsw i32 %61, %66
  %68 = load ptr, ptr %2, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.anim_t, ptr %68, i32 0, i32 7
  store i32 %67, ptr %69, align 8, !tbaa !18
  br label %81

70:                                               ; preds = %50
  %71 = load ptr, ptr %2, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.anim_t, ptr %71, i32 0, i32 0
  %73 = load i32, ptr %72, align 8, !tbaa !16
  %74 = icmp eq i32 %73, 2
  br i1 %74, label %75, label %80

75:                                               ; preds = %70
  %76 = load i32, ptr @bcnt, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  %78 = load ptr, ptr %2, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.anim_t, ptr %78, i32 0, i32 7
  store i32 %77, ptr %79, align 8, !tbaa !18
  br label %80

80:                                               ; preds = %75, %70
  br label %81

81:                                               ; preds = %80, %55
  br label %82

82:                                               ; preds = %81, %39
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %1, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %1, align 4, !tbaa !5
  br label %14, !llvm.loop !21

86:                                               ; preds = %14
  store i32 0, ptr %3, align 4
  br label %87

87:                                               ; preds = %86, %12, %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %88 = load i32, ptr %3, align 4
  switch i32 %88, label %90 [
    i32 0, label %89
    i32 1, label %89
  ]

89:                                               ; preds = %87, %87
  ret void

90:                                               ; preds = %87
  unreachable
}

declare i32 @M_Random() #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 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !7, i64 40}
!13 = !{!14, !6, i64 64}
!14 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !15, i64 12, !6, i64 20, !6, i64 24, !7, i64 32, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!15 = !{!"", !6, i64 0, !6, i64 4}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !6, i64 4}
!18 = !{!14, !6, i64 56}
!19 = !{!14, !6, i64 24}
!20 = !{!14, !6, i64 20}
!21 = distinct !{!21, !22}
!22 = !{!"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.anim_t = type { i32, i32, i32, %struct.point_t, i32, i32, [3 x ptr], i32, i32, i32, i32 }
%struct.point_t = type { i32, i32 }

@wbs = external hidden local_unnamed_addr global ptr, align 8
@gamemode = external local_unnamed_addr global i32, align 4
@NUMANIMS = external hidden local_unnamed_addr global [4 x i32], align 16
@anims = external hidden local_unnamed_addr global [4 x ptr], align 16
@bcnt = external hidden local_unnamed_addr global i32, align 4

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

3:                                                ; preds = %0
  %4 = load ptr, ptr @wbs, align 8, !tbaa !9
  %5 = load i32, ptr %4, align 4, !tbaa !11
  %6 = icmp sgt i32 %5, 2
  br i1 %6, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %7 = sext i32 %5 to i64
  %8 = getelementptr inbounds [4 x i32], ptr @NUMANIMS, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %40
  %indvars.iv = phi i64 [ %indvars.iv.next, %40 ], [ 0, %.preheader ]
  %11 = phi i64 [ %43, %40 ], [ %7, %.preheader ]
  %12 = getelementptr inbounds [4 x ptr], ptr @anims, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.anim_t, ptr %13, i64 %indvars.iv
  %15 = getelementptr inbounds i8, ptr %14, i64 64
  store i32 -1, ptr %15, align 8, !tbaa !13
  %16 = load i32, ptr %14, align 8, !tbaa !16
  switch i32 %16, label %40 [
    i32 0, label %17
    i32 1, label %25
    i32 2, label %36
  ]

17:                                               ; preds = %.lr.ph
  %18 = load i32, ptr @bcnt, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  %20 = tail call i32 @M_Random() #2
  %21 = getelementptr inbounds i8, ptr %14, i64 4
  %22 = load i32, ptr %21, align 4, !tbaa !17
  %23 = srem i32 %20, %22
  %24 = add nsw i32 %19, %23
  br label %.sink.split

25:                                               ; preds = %.lr.ph
  %26 = load i32, ptr @bcnt, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  %28 = getelementptr inbounds i8, ptr %14, i64 24
  %29 = load i32, ptr %28, align 8, !tbaa !18
  %30 = add nsw i32 %27, %29
  %31 = tail call i32 @M_Random() #2
  %32 = getelementptr inbounds i8, ptr %14, i64 20
  %33 = load i32, ptr %32, align 4, !tbaa !19
  %34 = srem i32 %31, %33
  %35 = add nsw i32 %30, %34
  br label %.sink.split

36:                                               ; preds = %.lr.ph
  %37 = load i32, ptr @bcnt, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  br label %.sink.split

.sink.split:                                      ; preds = %25, %36, %17
  %.sink = phi i32 [ %24, %17 ], [ %38, %36 ], [ %35, %25 ]
  %39 = getelementptr inbounds i8, ptr %14, i64 56
  store i32 %.sink, ptr %39, align 8, !tbaa !20
  br label %40

40:                                               ; preds = %.sink.split, %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %41 = load ptr, ptr @wbs, align 8, !tbaa !9
  %42 = load i32, ptr %41, align 4, !tbaa !11
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [4 x i32], ptr @NUMANIMS, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = icmp slt i64 %indvars.iv.next, %46
  br i1 %47, label %.lr.ph, label %.loopexit, !llvm.loop !21

.loopexit:                                        ; preds = %40, %.preheader, %3, %0
  ret void
}

declare i32 @M_Random() 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !7, i64 40}
!13 = !{!14, !6, i64 64}
!14 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !15, i64 12, !6, i64 20, !6, i64 24, !7, i64 32, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!15 = !{!"", !6, i64 0, !6, i64 4}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !6, i64 4}
!18 = !{!14, !6, i64 24}
!19 = !{!14, !6, i64 20}
!20 = !{!14, !6, i64 56}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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