; 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.channel_t = type { ptr, ptr, i32 }
%struct.sfxinfo_struct = type { ptr, i32, i32, ptr, i32, i32, ptr, i32, i32 }
%struct.musicinfo_t = type { ptr, i32, ptr, i32 }

@numChannels = external dso_local global i32, align 4
@channels = external hidden global ptr, align 8
@mus_paused = external hidden global i32, align 4
@snd_SfxVolume = external dso_local global i32, align 4
@mus_playing = 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
declare dso_local i32 @S_AdjustSoundParams(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @S_UpdateSounds(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %11, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %95, %1
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = load i32, ptr @numChannels, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %98

16:                                               ; preds = %12
  %17 = load ptr, ptr @channels, align 8, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %struct.channel_t, ptr %17, i64 %19
  store ptr %20, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.channel_t, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  store ptr %23, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.channel_t, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %94

28:                                               ; preds = %16
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.channel_t, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !13
  %32 = call i32 @I_SoundIsPlaying(i32 noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %91

34:                                               ; preds = %28
  %35 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !9
  store i32 %35, ptr %5, align 4, !tbaa !9
  store i32 128, ptr %7, align 4, !tbaa !9
  store i32 128, ptr %6, align 4, !tbaa !9
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.sfxinfo_struct, ptr %36, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !14
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %61

40:                                               ; preds = %34
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.sfxinfo_struct, ptr %41, i32 0, i32 4
  %43 = load i32, ptr %42, align 8, !tbaa !16
  store i32 %43, ptr %7, align 4, !tbaa !9
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.sfxinfo_struct, ptr %44, i32 0, i32 5
  %46 = load i32, ptr %45, align 4, !tbaa !17
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = add nsw i32 %47, %46
  store i32 %48, ptr %5, align 4, !tbaa !9
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = icmp slt i32 %49, 1
  br i1 %50, label %51, label %53

51:                                               ; preds = %40
  %52 = load i32, ptr %4, align 4, !tbaa !9
  call void @S_StopChannel(i32 noundef %52)
  br label %95

53:                                               ; preds = %40
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !9
  %56 = icmp sgt i32 %54, %55
  br i1 %56, label %57, label %59

57:                                               ; preds = %53
  %58 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !9
  store i32 %58, ptr %5, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %57, %53
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60, %34
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.channel_t, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !18
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %90

66:                                               ; preds = %61
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.channel_t, ptr %68, i32 0, i32 1
  %70 = load ptr, ptr %69, align 8, !tbaa !18
  %71 = icmp ne ptr %67, %70
  br i1 %71, label %72, label %90

72:                                               ; preds = %66
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.channel_t, ptr %74, i32 0, i32 1
  %76 = load ptr, ptr %75, align 8, !tbaa !18
  %77 = call i32 @S_AdjustSoundParams(ptr noundef %73, ptr noundef %76, ptr noundef %5, ptr noundef %6, ptr noundef %7)
  store i32 %77, ptr %3, align 4, !tbaa !9
  %78 = load i32, ptr %3, align 4, !tbaa !9
  %79 = icmp ne i32 %78, 0
  br i1 %79, label %82, label %80

80:                                               ; preds = %72
  %81 = load i32, ptr %4, align 4, !tbaa !9
  call void @S_StopChannel(i32 noundef %81)
  br label %89

82:                                               ; preds = %72
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.channel_t, ptr %83, i32 0, i32 2
  %85 = load i32, ptr %84, align 8, !tbaa !13
  %86 = load i32, ptr %5, align 4, !tbaa !9
  %87 = load i32, ptr %6, align 4, !tbaa !9
  %88 = load i32, ptr %7, align 4, !tbaa !9
  call void @I_UpdateSoundParams(i32 noundef %85, i32 noundef %86, i32 noundef %87, i32 noundef %88)
  br label %89

89:                                               ; preds = %82, %80
  br label %90

90:                                               ; preds = %89, %66, %61
  br label %93

91:                                               ; preds = %28
  %92 = load i32, ptr %4, align 4, !tbaa !9
  call void @S_StopChannel(i32 noundef %92)
  br label %93

93:                                               ; preds = %91, %90
  br label %94

94:                                               ; preds = %93, %16
  br label %95

95:                                               ; preds = %94, %51
  %96 = load i32, ptr %4, align 4, !tbaa !9
  %97 = add nsw i32 %96, 1
  store i32 %97, ptr %4, align 4, !tbaa !9
  br label %12, !llvm.loop !19

98:                                               ; preds = %12
  %99 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %100 = icmp ne ptr %99, null
  br i1 %100, label %101, label %116

101:                                              ; preds = %98
  %102 = load ptr, ptr @mus_playing, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.musicinfo_t, ptr %102, i32 0, i32 3
  %104 = load i32, ptr %103, align 8, !tbaa !21
  %105 = call i32 @I_QrySongPlaying(i32 noundef %104)
  %106 = icmp ne i32 %105, 0
  %107 = xor i1 %106, true
  %108 = zext i1 %107 to i32
  %109 = load i32, ptr @mus_paused, align 4, !tbaa !9
  %110 = icmp ne i32 %109, 0
  %111 = xor i1 %110, true
  %112 = zext i1 %111 to i32
  %113 = and i32 %108, %112
  %114 = icmp ne i32 %113, 0
  br i1 %114, label %115, label %116

115:                                              ; preds = %101
  call void @S_StopMusic()
  br label %116

116:                                              ; preds = %115, %101, %98
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

declare i32 @I_SoundIsPlaying(i32 noundef) #2

declare void @I_UpdateSoundParams(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #2

declare i32 @I_QrySongPlaying(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @S_StopMusic() #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 = { 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, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 16}
!14 = !{!15, !6, i64 16}
!15 = !{!"sfxinfo_struct", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !10, i64 24, !10, i64 28, !6, i64 32, !10, i64 40, !10, i64 44}
!16 = !{!15, !10, i64 24}
!17 = !{!15, !10, i64 28}
!18 = !{!12, !6, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!22, !10, i64 24}
!22 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}


; 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.channel_t = type { ptr, ptr, i32 }

@numChannels = external dso_local local_unnamed_addr global i32, align 4
@channels = external hidden local_unnamed_addr global ptr, align 8
@mus_paused = external hidden local_unnamed_addr global i32, align 4
@snd_SfxVolume = external dso_local local_unnamed_addr global i32, align 4
@mus_playing = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @S_AdjustSoundParams(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @S_UpdateSounds(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %5 = load i32, ptr @numChannels, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %44
  %indvars.iv = phi i64 [ %indvars.iv.next, %44 ], [ 0, %1 ]
  %7 = load ptr, ptr @channels, align 8, !tbaa !9
  %8 = getelementptr inbounds %struct.channel_t, ptr %7, i64 %indvars.iv
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %.not27 = icmp eq ptr %9, null
  br i1 %.not27, label %44, label %10

10:                                               ; preds = %.lr.ph
  %11 = getelementptr inbounds i8, ptr %8, i64 16
  %12 = load i32, ptr %11, align 8, !tbaa !13
  %13 = call i32 @I_SoundIsPlaying(i32 noundef %12) #3
  %.not28 = icmp eq i32 %13, 0
  br i1 %.not28, label %42, label %14

14:                                               ; preds = %10
  %15 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !5
  store i32 %15, ptr %2, align 4, !tbaa !5
  store i32 128, ptr %4, align 4, !tbaa !5
  store i32 128, ptr %3, align 4, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %9, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %.not29 = icmp eq ptr %17, null
  br i1 %.not29, label %30, label %18

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %9, i64 24
  %20 = load i32, ptr %19, align 8, !tbaa !16
  store i32 %20, ptr %4, align 4, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %9, i64 28
  %22 = load i32, ptr %21, align 4, !tbaa !17
  %23 = add nsw i32 %22, %15
  store i32 %23, ptr %2, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 1
  br i1 %24, label %25, label %27

25:                                               ; preds = %18
  %26 = trunc nuw nsw i64 %indvars.iv to i32
  call void @S_StopChannel(i32 noundef %26)
  br label %44

27:                                               ; preds = %18
  %28 = icmp sgt i32 %22, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %27
  store i32 %15, ptr %2, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %29, %27, %14
  %31 = getelementptr inbounds i8, ptr %8, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !18
  %.not30 = icmp eq ptr %32, null
  %.not31 = icmp eq ptr %32, %0
  %or.cond = or i1 %.not30, %.not31
  br i1 %or.cond, label %44, label %33

33:                                               ; preds = %30
  %34 = call i32 @S_AdjustSoundParams(ptr noundef %0, ptr noundef nonnull %32, ptr noundef nonnull %2, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %.not32 = icmp eq i32 %34, 0
  br i1 %.not32, label %35, label %37

35:                                               ; preds = %33
  %36 = trunc nuw nsw i64 %indvars.iv to i32
  call void @S_StopChannel(i32 noundef %36)
  br label %44

37:                                               ; preds = %33
  %38 = load i32, ptr %11, align 8, !tbaa !13
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = load i32, ptr %4, align 4, !tbaa !5
  call void @I_UpdateSoundParams(i32 noundef %38, i32 noundef %39, i32 noundef %40, i32 noundef %41) #3
  br label %44

42:                                               ; preds = %10
  %43 = trunc nuw nsw i64 %indvars.iv to i32
  call void @S_StopChannel(i32 noundef %43)
  br label %44

44:                                               ; preds = %.lr.ph, %30, %37, %35, %42, %25
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %45 = load i32, ptr @numChannels, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = icmp slt i64 %indvars.iv.next, %46
  br i1 %47, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %44, %1
  %48 = load ptr, ptr @mus_playing, align 8, !tbaa !9
  %.not = icmp eq ptr %48, null
  br i1 %.not, label %57, label %49

49:                                               ; preds = %._crit_edge
  %50 = getelementptr inbounds i8, ptr %48, i64 24
  %51 = load i32, ptr %50, align 8, !tbaa !21
  %52 = call i32 @I_QrySongPlaying(i32 noundef %51) #3
  %53 = load i32, ptr @mus_paused, align 4, !tbaa !5
  %54 = or i32 %53, %52
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %49
  call void @S_StopMusic()
  br label %57

57:                                               ; preds = %56, %49, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  ret void
}

declare i32 @I_SoundIsPlaying(i32 noundef) local_unnamed_addr #2

declare void @I_UpdateSoundParams(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

declare i32 @I_QrySongPlaying(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @S_StopMusic() local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 16}
!14 = !{!15, !10, i64 16}
!15 = !{!"sfxinfo_struct", !10, i64 0, !6, i64 8, !6, i64 12, !10, i64 16, !6, i64 24, !6, i64 28, !10, i64 32, !6, i64 40, !6, i64 44}
!16 = !{!15, !6, i64 24}
!17 = !{!15, !6, i64 28}
!18 = !{!12, !10, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!22, !6, i64 24}
!22 = !{!"", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass