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

@plyr = external hidden global ptr, align 8
@ST_calcPainOffset.lastcalc = external hidden global i32, align 4
@ST_calcPainOffset.oldhealth = 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 i32 @ST_calcPainOffset() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  %2 = load ptr, ptr @plyr, align 8, !tbaa !5
  %3 = getelementptr inbounds %struct.player_s, ptr %2, i32 0, i32 7
  %4 = load i32, ptr %3, align 4, !tbaa !9
  %5 = icmp sgt i32 %4, 100
  br i1 %5, label %6, label %7

6:                                                ; preds = %0
  br label %11

7:                                                ; preds = %0
  %8 = load ptr, ptr @plyr, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.player_s, ptr %8, i32 0, i32 7
  %10 = load i32, ptr %9, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %7, %6
  %12 = phi i32 [ 100, %6 ], [ %10, %7 ]
  store i32 %12, ptr %1, align 4, !tbaa !14
  %13 = load i32, ptr %1, align 4, !tbaa !14
  %14 = load i32, ptr @ST_calcPainOffset.oldhealth, align 4, !tbaa !14
  %15 = icmp ne i32 %13, %14
  br i1 %15, label %16, label %23

16:                                               ; preds = %11
  %17 = load i32, ptr %1, align 4, !tbaa !14
  %18 = sub nsw i32 100, %17
  %19 = mul nsw i32 %18, 5
  %20 = sdiv i32 %19, 101
  %21 = mul nsw i32 8, %20
  store i32 %21, ptr @ST_calcPainOffset.lastcalc, align 4, !tbaa !14
  %22 = load i32, ptr %1, align 4, !tbaa !14
  store i32 %22, ptr @ST_calcPainOffset.oldhealth, align 4, !tbaa !14
  br label %23

23:                                               ; preds = %16, %11
  %24 = load i32, ptr @ST_calcPainOffset.lastcalc, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i32 %24
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 36}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !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}


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

@plyr = external hidden local_unnamed_addr global ptr, align 8
@ST_calcPainOffset.lastcalc = external hidden local_unnamed_addr global i32, align 4
@ST_calcPainOffset.oldhealth = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local i32 @ST_calcPainOffset() local_unnamed_addr #0 {
  %1 = load ptr, ptr @plyr, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 36
  %3 = load i32, ptr %2, align 4, !tbaa !9
  %spec.select = tail call i32 @llvm.smin.i32(i32 %3, i32 100)
  %4 = load i32, ptr @ST_calcPainOffset.oldhealth, align 4, !tbaa !14
  %.not = icmp eq i32 %spec.select, %4
  br i1 %.not, label %._crit_edge, label %5

._crit_edge:                                      ; preds = %0
  %.pre = load i32, ptr @ST_calcPainOffset.lastcalc, align 4, !tbaa !14
  br label %10

5:                                                ; preds = %0
  %6 = sub nsw i32 100, %spec.select
  %7 = mul nsw i32 %6, 5
  %8 = udiv i32 %7, 101
  %9 = shl nuw nsw i32 %8, 3
  store i32 %9, ptr @ST_calcPainOffset.lastcalc, align 4, !tbaa !14
  store i32 %spec.select, ptr @ST_calcPainOffset.oldhealth, align 4, !tbaa !14
  br label %10

10:                                               ; preds = %._crit_edge, %5
  %11 = phi i32 [ %.pre, %._crit_edge ], [ %9, %5 ]
  ret i32 %11
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: read, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 36}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !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,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass