; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (s_sound)

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

@mus_paused = external hidden global i32, align 4
@gamemode = external global i32, align 4
@gamemap = external global i32, align 4
@__const.S_Start.spmus = external hidden unnamed_addr constant [9 x i32], align 16
@gameepisode = external global i32, align 4
@nextcleanup = 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 @S_Start() #1 {
  %1 = alloca i32, align 4
  %2 = alloca [9 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void (...) @I_StopAllSounds()
  store i32 0, ptr @mus_paused, align 4, !tbaa !5
  %3 = load i32, ptr @gamemode, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 2
  br i1 %4, label %5, label %9

5:                                                ; preds = %0
  %6 = load i32, ptr @gamemap, align 4, !tbaa !5
  %7 = add nsw i32 33, %6
  %8 = sub nsw i32 %7, 1
  store i32 %8, ptr %1, align 4, !tbaa !5
  br label %27

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 36, ptr %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %2, ptr align 16 @__const.S_Start.spmus, i64 36, i1 false)
  %10 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 4
  br i1 %11, label %12, label %20

12:                                               ; preds = %9
  %13 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %14 = sub nsw i32 %13, 1
  %15 = mul nsw i32 %14, 9
  %16 = add nsw i32 1, %15
  %17 = load i32, ptr @gamemap, align 4, !tbaa !5
  %18 = add nsw i32 %16, %17
  %19 = sub nsw i32 %18, 1
  store i32 %19, ptr %1, align 4, !tbaa !5
  br label %26

20:                                               ; preds = %9
  %21 = load i32, ptr @gamemap, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [9 x i32], ptr %2, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %25, ptr %1, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %20, %12
  call void @llvm.lifetime.end.p0(i64 36, ptr %2) #4
  br label %27

27:                                               ; preds = %26, %5
  %28 = load i32, ptr %1, align 4, !tbaa !5
  call void @S_ChangeMusic(i32 noundef %28, i32 noundef 1)
  store i32 15, ptr @nextcleanup, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

declare void @I_StopAllSounds(...) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
declare dso_local void @S_ChangeMusic(i32 noundef, i32 noundef) #1

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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"}


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

@mus_paused = external hidden local_unnamed_addr global i32, align 4
@gamemode = external local_unnamed_addr global i32, align 4
@gamemap = external local_unnamed_addr global i32, align 4
@__const.S_Start.spmus = external hidden unnamed_addr constant [9 x i32], align 16
@gameepisode = external local_unnamed_addr global i32, align 4
@nextcleanup = external hidden local_unnamed_addr global i32, align 4

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

3:                                                ; preds = %0
  %4 = load i32, ptr @gamemap, align 4, !tbaa !5
  %5 = add nsw i32 %4, 32
  br label %20

6:                                                ; preds = %0
  %7 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 4
  br i1 %8, label %9, label %14

9:                                                ; preds = %6
  %10 = mul i32 %7, 9
  %11 = load i32, ptr @gamemap, align 4, !tbaa !5
  %12 = add i32 %10, -9
  %13 = add i32 %12, %11
  br label %20

14:                                               ; preds = %6
  %15 = load i32, ptr @gamemap, align 4, !tbaa !5
  %16 = add nsw i32 %15, -1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [9 x i32], ptr @__const.S_Start.spmus, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %9, %14, %3
  %.1 = phi i32 [ %5, %3 ], [ %13, %9 ], [ %19, %14 ]
  tail call void @S_ChangeMusic(i32 noundef %.1, i32 noundef 1)
  store i32 15, ptr @nextcleanup, align 4, !tbaa !5
  ret void
}

declare void @I_StopAllSounds(...) local_unnamed_addr #1

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

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,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass