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

%struct.musicinfo_t = type { ptr, i32, ptr, i32 }
%struct.memblock_s = type { i32, i32, ptr, i32, i32, ptr, ptr, i32 }

@mus_paused = external hidden global i32, align 4
@mus_playing = external hidden global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [108 x i8], align 1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) #0

declare void @I_ResumeSong(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @S_StopMusic() #2 {
  %1 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %2 = icmp ne ptr %1, null
  br i1 %2, label %3, label %31

3:                                                ; preds = %0
  %4 = load i32, ptr @mus_paused, align 4, !tbaa !9
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %6, label %10

6:                                                ; preds = %3
  %7 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.musicinfo_t, ptr %7, i32 0, i32 3
  %9 = load i32, ptr %8, align 8, !tbaa !11
  call void @I_ResumeSong(i32 noundef %9)
  br label %10

10:                                               ; preds = %6, %3
  %11 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.musicinfo_t, ptr %11, i32 0, i32 3
  %13 = load i32, ptr %12, align 8, !tbaa !11
  call void @I_StopSong(i32 noundef %13)
  %14 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.musicinfo_t, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !11
  call void @I_UnRegisterSong(i32 noundef %16)
  %17 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.musicinfo_t, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %19, i64 -48
  %21 = getelementptr inbounds %struct.memblock_s, ptr %20, i32 0, i32 4
  %22 = load i32, ptr %21, align 4, !tbaa !14
  %23 = icmp ne i32 %22, 1919505
  br i1 %23, label %24, label %25

24:                                               ; preds = %10
  call void (ptr, ...) @I_Error(ptr noundef @.str.6, i32 noundef 518) #3
  unreachable

25:                                               ; preds = %10
  %26 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.musicinfo_t, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  call void @Z_ChangeTag2(ptr noundef %28, i32 noundef 101)
  %29 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.musicinfo_t, ptr %29, i32 0, i32 2
  store ptr null, ptr %30, align 8, !tbaa !13
  store ptr null, ptr @mus_playing, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %25, %0
  ret void
}

declare void @I_StopSong(i32 noundef) #1

declare void @I_UnRegisterSong(i32 noundef) #1

declare void @Z_ChangeTag2(ptr noundef, i32 noundef) #1

attributes #0 = { noreturn "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 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 #3 = { noreturn }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 16}
!14 = !{!15, !10, i64 20}
!15 = !{!"memblock_s", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !10, i64 40}


; 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
@mus_playing = external hidden local_unnamed_addr global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [108 x i8], align 1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #0

declare void @I_ResumeSong(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @S_StopMusic() local_unnamed_addr #2 {
  %1 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %23, label %2

2:                                                ; preds = %0
  %3 = load i32, ptr @mus_paused, align 4, !tbaa !9
  %.not1 = icmp eq i32 %3, 0
  br i1 %.not1, label %7, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %1, i64 24
  %6 = load i32, ptr %5, align 8, !tbaa !11
  tail call void @I_ResumeSong(i32 noundef %6) #3
  %.pre = load ptr, ptr @mus_playing, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %4, %2
  %8 = phi ptr [ %.pre, %4 ], [ %1, %2 ]
  %9 = getelementptr inbounds i8, ptr %8, i64 24
  %10 = load i32, ptr %9, align 8, !tbaa !11
  tail call void @I_StopSong(i32 noundef %10) #3
  %11 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 24
  %13 = load i32, ptr %12, align 8, !tbaa !11
  tail call void @I_UnRegisterSong(i32 noundef %13) #3
  %14 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %16, i64 -28
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %.not2 = icmp eq i32 %18, 1919505
  br i1 %.not2, label %20, label %19

19:                                               ; preds = %7
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.6, i32 noundef 518) #4
  unreachable

20:                                               ; preds = %7
  tail call void @Z_ChangeTag2(ptr noundef nonnull %16, i32 noundef 101) #3
  %21 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  store ptr null, ptr %22, align 8, !tbaa !13
  store ptr null, ptr @mus_playing, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %20, %0
  ret void
}

declare void @I_StopSong(i32 noundef) local_unnamed_addr #1

declare void @I_UnRegisterSong(i32 noundef) local_unnamed_addr #1

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

attributes #0 = { noreturn "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 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 #3 = { nounwind }
attributes #4 = { noreturn 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 16}
!14 = !{!15, !10, i64 20}
!15 = !{!"memblock_s", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !10, i64 40}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,TailCallElimPass