; 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.wbplayerstruct_t = type { i32, i32, i32, i32, i32, [4 x i32], i32 }

@playeringame = external global [4 x i32], align 16
@plrs = external hidden global ptr, 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 i32 @WI_fragSum(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %31, %1
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 4
  br i1 %7, label %8, label %34

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

14:                                               ; preds = %8
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp ne i32 %15, %16
  br i1 %17, label %18, label %30

18:                                               ; preds = %14
  %19 = load ptr, ptr @plrs, align 8, !tbaa !9
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %19, i64 %21
  %23 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %22, i32 0, i32 5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4 x i32], ptr %23, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = add nsw i32 %28, %27
  store i32 %29, ptr %4, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %18, %14, %8
  br label %31

31:                                               ; preds = %30
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %3, align 4, !tbaa !5
  br label %5, !llvm.loop !11

34:                                               ; preds = %5
  %35 = load ptr, ptr @plrs, align 8, !tbaa !9
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %38, i32 0, i32 5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [4 x i32], ptr %39, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = sub nsw i32 %44, %43
  store i32 %45, ptr %4, align 4, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %46
}

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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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.wbplayerstruct_t = type { i32, i32, i32, i32, i32, [4 x i32], i32 }

@playeringame = external local_unnamed_addr global [4 x i32], align 16
@plrs = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @WI_fragSum(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @plrs, align 8
  %3 = sext i32 %0 to i64
  %4 = load i32, ptr @playeringame, align 16, !tbaa !5
  %.not = icmp eq i32 %4, 0
  %.not14 = icmp eq i32 %0, 0
  %or.cond = or i1 %.not14, %.not
  br i1 %or.cond, label %8, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %2, i64 %3, i32 5, i64 0
  %7 = load i32, ptr %6, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %1, %5
  %.1 = phi i32 [ %7, %5 ], [ 0, %1 ]
  %9 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 4), align 4, !tbaa !5
  %.not.1 = icmp eq i32 %9, 0
  %.not14.1 = icmp eq i32 %0, 1
  %or.cond.1 = or i1 %.not14.1, %.not.1
  br i1 %or.cond.1, label %14, label %10

10:                                               ; preds = %8
  %11 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %2, i64 %3, i32 5, i64 1
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, %.1
  br label %14

14:                                               ; preds = %10, %8
  %.1.1 = phi i32 [ %13, %10 ], [ %.1, %8 ]
  %15 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 8), align 8, !tbaa !5
  %.not.2 = icmp eq i32 %15, 0
  %.not14.2 = icmp eq i32 %0, 2
  %or.cond.2 = or i1 %.not14.2, %.not.2
  br i1 %or.cond.2, label %20, label %16

16:                                               ; preds = %14
  %17 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %2, i64 %3, i32 5, i64 2
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %.1.1
  br label %20

20:                                               ; preds = %16, %14
  %.1.2 = phi i32 [ %19, %16 ], [ %.1.1, %14 ]
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @playeringame, i64 12), align 4, !tbaa !5
  %.not.3 = icmp eq i32 %21, 0
  %.not14.3 = icmp eq i32 %0, 3
  %or.cond.3 = or i1 %.not14.3, %.not.3
  br i1 %or.cond.3, label %26, label %22

22:                                               ; preds = %20
  %23 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %2, i64 %3, i32 5, i64 3
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, %.1.2
  br label %26

26:                                               ; preds = %22, %20
  %.1.3 = phi i32 [ %25, %22 ], [ %.1.2, %20 ]
  %27 = getelementptr inbounds %struct.wbplayerstruct_t, ptr %2, i64 %3, i32 5, i64 %3
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sub nsw i32 %.1.3, %28
  ret i32 %29
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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" }

!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,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass