; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (st_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.st_number_t = type { i32, i32, i32, i32, ptr, ptr, ptr, i32 }
%struct.st_percent_t = type { %struct.st_number_t, ptr }
%struct.st_binicon_t = type { i32, i32, i32, ptr, ptr, ptr, i32 }
%struct.st_multicon_t = type { i32, i32, i32, ptr, ptr, ptr, i32 }

@st_statusbaron = external hidden global i32, align 4
@w_ready = external hidden global %struct.st_number_t, align 8
@deathmatch = external global i32, align 4
@st_armson = external hidden global i32, align 4
@st_fragson = external hidden global i32, align 4
@w_ammo = external hidden global [4 x %struct.st_number_t], align 16
@w_maxammo = external hidden global [4 x %struct.st_number_t], align 16
@w_health = external hidden global %struct.st_percent_t, align 8
@w_armor = external hidden global %struct.st_percent_t, align 8
@w_armsbg = external hidden global %struct.st_binicon_t, align 8
@w_arms = external hidden global [6 x %struct.st_multicon_t], align 16
@w_faces = external hidden global %struct.st_multicon_t, align 8
@w_keyboxes = external hidden global [3 x %struct.st_multicon_t], align 16
@w_frags = external hidden global %struct.st_number_t, align 8

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

6:                                                ; preds = %1
  %7 = load i32, ptr @deathmatch, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  %9 = xor i1 %8, true
  br label %10

10:                                               ; preds = %6, %1
  %11 = phi i1 [ false, %1 ], [ %9, %6 ]
  %12 = zext i1 %11 to i32
  store i32 %12, ptr @st_armson, align 4, !tbaa !5
  %13 = load i32, ptr @deathmatch, align 4, !tbaa !5
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %10
  %16 = load i32, ptr @st_statusbaron, align 4, !tbaa !5
  %17 = icmp ne i32 %16, 0
  br label %18

18:                                               ; preds = %15, %10
  %19 = phi i1 [ false, %10 ], [ %17, %15 ]
  %20 = zext i1 %19 to i32
  store i32 %20, ptr @st_fragson, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateNum(ptr noundef @w_ready, i32 noundef %21)
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %34, %18
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 4
  br i1 %24, label %25, label %37

25:                                               ; preds = %22
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [4 x %struct.st_number_t], ptr @w_ammo, i64 0, i64 %27
  %29 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateNum(ptr noundef %28, i32 noundef %29)
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [4 x %struct.st_number_t], ptr @w_maxammo, i64 0, i64 %31
  %33 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateNum(ptr noundef %32, i32 noundef %33)
  br label %34

34:                                               ; preds = %25
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !5
  br label %22, !llvm.loop !9

37:                                               ; preds = %22
  %38 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updatePercent(ptr noundef @w_health, i32 noundef %38)
  %39 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updatePercent(ptr noundef @w_armor, i32 noundef %39)
  %40 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateBinIcon(ptr noundef @w_armsbg, i32 noundef %40)
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %49, %37
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = icmp slt i32 %42, 6
  br i1 %43, label %44, label %52

44:                                               ; preds = %41
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [6 x %struct.st_multicon_t], ptr @w_arms, i64 0, i64 %46
  %48 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateMultIcon(ptr noundef %47, i32 noundef %48)
  br label %49

49:                                               ; preds = %44
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %3, align 4, !tbaa !5
  br label %41, !llvm.loop !11

52:                                               ; preds = %41
  %53 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateMultIcon(ptr noundef @w_faces, i32 noundef %53)
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %62, %52
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = icmp slt i32 %55, 3
  br i1 %56, label %57, label %65

57:                                               ; preds = %54
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [3 x %struct.st_multicon_t], ptr @w_keyboxes, i64 0, i64 %59
  %61 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateMultIcon(ptr noundef %60, i32 noundef %61)
  br label %62

62:                                               ; preds = %57
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %3, align 4, !tbaa !5
  br label %54, !llvm.loop !12

65:                                               ; preds = %54
  %66 = load i32, ptr %2, align 4, !tbaa !5
  call void @STlib_updateNum(ptr noundef @w_frags, i32 noundef %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

declare void @STlib_updateNum(ptr noundef, i32 noundef) #2

declare void @STlib_updatePercent(ptr noundef, i32 noundef) #2

declare void @STlib_updateBinIcon(ptr noundef, i32 noundef) #2

declare void @STlib_updateMultIcon(ptr noundef, i32 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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.st_number_t = type { i32, i32, i32, i32, ptr, ptr, ptr, i32 }
%struct.st_percent_t = type { %struct.st_number_t, ptr }
%struct.st_binicon_t = type { i32, i32, i32, ptr, ptr, ptr, i32 }
%struct.st_multicon_t = type { i32, i32, i32, ptr, ptr, ptr, i32 }

@st_statusbaron = external hidden local_unnamed_addr global i32, align 4
@w_ready = external hidden global %struct.st_number_t, align 8
@deathmatch = external local_unnamed_addr global i32, align 4
@st_armson = external hidden local_unnamed_addr global i32, align 4
@st_fragson = external hidden local_unnamed_addr global i32, align 4
@w_ammo = external hidden global [4 x %struct.st_number_t], align 16
@w_maxammo = external hidden global [4 x %struct.st_number_t], align 16
@w_health = external hidden global %struct.st_percent_t, align 8
@w_armor = external hidden global %struct.st_percent_t, align 8
@w_armsbg = external hidden global %struct.st_binicon_t, align 8
@w_arms = external hidden global [6 x %struct.st_multicon_t], align 16
@w_faces = external hidden global %struct.st_multicon_t, align 8
@w_keyboxes = external hidden global [3 x %struct.st_multicon_t], align 16
@w_frags = external hidden global %struct.st_number_t, align 8

; Function Attrs: nounwind uwtable
define dso_local void @ST_drawWidgets(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @st_statusbaron, align 4
  %3 = icmp ne i32 %2, 0
  %4 = load i32, ptr @deathmatch, align 4
  %.not = icmp eq i32 %4, 0
  %5 = select i1 %3, i1 %.not, i1 false
  %6 = zext i1 %5 to i32
  store i32 %6, ptr @st_armson, align 4, !tbaa !5
  %7 = icmp ne i32 %4, 0
  %8 = select i1 %7, i1 %3, i1 false
  %9 = zext i1 %8 to i32
  store i32 %9, ptr @st_fragson, align 4, !tbaa !5
  tail call void @STlib_updateNum(ptr noundef nonnull @w_ready, i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull @w_ammo, i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull @w_maxammo, i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_ammo, i64 48), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_maxammo, i64 48), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_ammo, i64 96), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_maxammo, i64 96), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_ammo, i64 144), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_maxammo, i64 144), i32 noundef %0) #2
  tail call void @STlib_updatePercent(ptr noundef nonnull @w_health, i32 noundef %0) #2
  tail call void @STlib_updatePercent(ptr noundef nonnull @w_armor, i32 noundef %0) #2
  tail call void @STlib_updateBinIcon(ptr noundef nonnull @w_armsbg, i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull @w_arms, i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_arms, i64 48), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_arms, i64 96), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_arms, i64 144), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_arms, i64 192), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_arms, i64 240), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull @w_faces, i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull @w_keyboxes, i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_keyboxes, i64 48), i32 noundef %0) #2
  tail call void @STlib_updateMultIcon(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_keyboxes, i64 96), i32 noundef %0) #2
  tail call void @STlib_updateNum(ptr noundef nonnull @w_frags, i32 noundef %0) #2
  ret void
}

declare void @STlib_updateNum(ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @STlib_updatePercent(ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @STlib_updateBinIcon(ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @STlib_updateMultIcon(ptr noundef, i32 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"}


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