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

@state = external hidden global i32, align 4
@acceleratestage = external hidden global i32, align 4
@playeringame = external global [4 x i32], align 16
@cnt_pause = external hidden global i32, align 4
@ng_state = external hidden global i32, align 4
@cnt_frags = external hidden global [4 x i32], align 16
@cnt_secret = external hidden global [4 x i32], align 16
@cnt_items = external hidden global [4 x i32], align 16
@cnt_kills = external hidden global [4 x i32], align 16
@dofrags = 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
declare dso_local void @WI_initAnimatedBack() #1

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

; Function Attrs: nounwind uwtable
define dso_local void @WI_initNetgameStats() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr @state, align 4, !tbaa !5
  store i32 0, ptr @acceleratestage, align 4, !tbaa !5
  store i32 1, ptr @ng_state, align 4, !tbaa !5
  store i32 35, ptr @cnt_pause, align 4, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %29, %0
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = icmp slt i32 %3, 4
  br i1 %4, label %5, label %32

5:                                                ; preds = %2
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [4 x i32], ptr @playeringame, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %12, label %11

11:                                               ; preds = %5
  br label %29

12:                                               ; preds = %5
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [4 x i32], ptr @cnt_frags, i64 0, i64 %14
  store i32 0, ptr %15, align 4, !tbaa !5
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4 x i32], ptr @cnt_secret, i64 0, i64 %17
  store i32 0, ptr %18, align 4, !tbaa !5
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [4 x i32], ptr @cnt_items, i64 0, i64 %20
  store i32 0, ptr %21, align 4, !tbaa !5
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [4 x i32], ptr @cnt_kills, i64 0, i64 %23
  store i32 0, ptr %24, align 4, !tbaa !5
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = call i32 @WI_fragSum(i32 noundef %25)
  %27 = load i32, ptr @dofrags, align 4, !tbaa !5
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr @dofrags, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %12, %11
  %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 %2, !llvm.loop !9

32:                                               ; preds = %2
  %33 = load i32, ptr @dofrags, align 4, !tbaa !5
  %34 = icmp ne i32 %33, 0
  %35 = xor i1 %34, true
  %36 = xor i1 %35, true
  %37 = zext i1 %36 to i32
  store i32 %37, ptr @dofrags, align 4, !tbaa !5
  call void @WI_initAnimatedBack()
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret void
}

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 = { 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 = distinct !{!9, !10}
!10 = !{!"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"

@state = external hidden local_unnamed_addr global i32, align 4
@acceleratestage = external hidden local_unnamed_addr global i32, align 4
@playeringame = external local_unnamed_addr global [4 x i32], align 16
@cnt_pause = external hidden local_unnamed_addr global i32, align 4
@ng_state = external hidden local_unnamed_addr global i32, align 4
@cnt_frags = external hidden local_unnamed_addr global [4 x i32], align 16
@cnt_secret = external hidden local_unnamed_addr global [4 x i32], align 16
@cnt_items = external hidden local_unnamed_addr global [4 x i32], align 16
@cnt_kills = external hidden local_unnamed_addr global [4 x i32], align 16
@dofrags = external hidden local_unnamed_addr global i32, align 4

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

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

; Function Attrs: nounwind uwtable
define dso_local void @WI_initNetgameStats() local_unnamed_addr #0 {
  store i32 0, ptr @state, align 4, !tbaa !5
  store i32 0, ptr @acceleratestage, align 4, !tbaa !5
  store i32 1, ptr @ng_state, align 4, !tbaa !5
  store i32 35, ptr @cnt_pause, align 4, !tbaa !5
  %1 = load i32, ptr @playeringame, align 16, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %6, label %2

2:                                                ; preds = %0
  store i32 0, ptr @cnt_frags, align 16, !tbaa !5
  store i32 0, ptr @cnt_secret, align 16, !tbaa !5
  store i32 0, ptr @cnt_items, align 16, !tbaa !5
  store i32 0, ptr @cnt_kills, align 16, !tbaa !5
  %3 = tail call i32 @WI_fragSum(i32 noundef 0)
  %4 = load i32, ptr @dofrags, align 4, !tbaa !5
  %5 = add nsw i32 %4, %3
  store i32 %5, ptr @dofrags, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %0, %2
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 4), align 4, !tbaa !5
  %.not.1 = icmp eq i32 %7, 0
  br i1 %.not.1, label %12, label %8

8:                                                ; preds = %6
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_frags, i64 4), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_secret, i64 4), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_items, i64 4), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_kills, i64 4), align 4, !tbaa !5
  %9 = tail call i32 @WI_fragSum(i32 noundef 1)
  %10 = load i32, ptr @dofrags, align 4, !tbaa !5
  %11 = add nsw i32 %10, %9
  store i32 %11, ptr @dofrags, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %8, %6
  %13 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 8), align 8, !tbaa !5
  %.not.2 = icmp eq i32 %13, 0
  br i1 %.not.2, label %18, label %14

14:                                               ; preds = %12
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_frags, i64 8), align 8, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_secret, i64 8), align 8, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_items, i64 8), align 8, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_kills, i64 8), align 8, !tbaa !5
  %15 = tail call i32 @WI_fragSum(i32 noundef 2)
  %16 = load i32, ptr @dofrags, align 4, !tbaa !5
  %17 = add nsw i32 %16, %15
  store i32 %17, ptr @dofrags, align 4, !tbaa !5
  br label %18

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

._crit_edge:                                      ; preds = %18
  %.pre = load i32, ptr @dofrags, align 4, !tbaa !5
  br label %24

20:                                               ; preds = %18
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_frags, i64 12), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_secret, i64 12), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_items, i64 12), align 4, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @cnt_kills, i64 12), align 4, !tbaa !5
  %21 = tail call i32 @WI_fragSum(i32 noundef 3)
  %22 = load i32, ptr @dofrags, align 4, !tbaa !5
  %23 = add nsw i32 %22, %21
  br label %24

24:                                               ; preds = %._crit_edge, %20
  %25 = phi i32 [ %.pre, %._crit_edge ], [ %23, %20 ]
  %26 = icmp ne i32 %25, 0
  %27 = zext i1 %26 to i32
  store i32 %27, ptr @dofrags, align 4, !tbaa !5
  tail call void @WI_initAnimatedBack()
  ret void
}

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

!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"}


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