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

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@numChannels = external dso_local global i32, align 4
@channels = external hidden global ptr, align 8
@mus_paused = external hidden global i32, align 4
@S_sfx = external global [0 x %struct.sfxinfo_struct], align 8

; Function Attrs: nounwind uwtable
define dso_local void @S_Init(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %6 = load ptr, ptr @stderr, align 8, !tbaa !9
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef @.str, i32 noundef %7) #4
  %9 = load i32, ptr %3, align 4, !tbaa !5
  call void @S_SetSfxVolume(i32 noundef %9)
  %10 = load i32, ptr %4, align 4, !tbaa !5
  call void @S_SetMusicVolume(i32 noundef %10)
  %11 = load i32, ptr @numChannels, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = mul i64 %12, 24
  %14 = trunc i64 %13 to i32
  %15 = call ptr @Z_Malloc(i32 noundef %14, i32 noundef 1, ptr noundef null)
  store ptr %15, ptr @channels, align 8, !tbaa !9
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %26, %2
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr @numChannels, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %29

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

26:                                               ; preds = %20
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %5, align 4, !tbaa !5
  br label %16, !llvm.loop !13

29:                                               ; preds = %16
  store i32 0, ptr @mus_paused, align 4, !tbaa !5
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %42, %29
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = icmp slt i32 %31, 109
  br i1 %32, label %33, label %45

33:                                               ; preds = %30
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.sfxinfo_struct, ptr %36, i32 0, i32 7
  store i32 -1, ptr %37, align 8, !tbaa !15
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.sfxinfo_struct, ptr %40, i32 0, i32 8
  store i32 -1, ptr %41, align 4, !tbaa !17
  br label %42

42:                                               ; preds = %33
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !5
  br label %30, !llvm.loop !18

45:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

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

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

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) #3

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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 = { "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 #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"}
!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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !6, i64 40}
!16 = !{!"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}
!17 = !{!16, !6, i64 44}
!18 = distinct !{!18, !14}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@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
@S_sfx = external local_unnamed_addr global [0 x %struct.sfxinfo_struct], align 8

; Function Attrs: nounwind uwtable
define dso_local void @S_Init(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @stderr, align 8, !tbaa !5
  %4 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %3, ptr noundef nonnull @.str, i32 noundef %0) #3
  tail call void @S_SetSfxVolume(i32 noundef %0)
  tail call void @S_SetMusicVolume(i32 noundef %1)
  %5 = load i32, ptr @numChannels, align 4, !tbaa !9
  %6 = mul i32 %5, 24
  %7 = tail call ptr @Z_Malloc(i32 noundef %6, i32 noundef 1, ptr noundef null) #4
  store ptr %7, ptr @channels, align 8, !tbaa !5
  %8 = load i32, ptr @numChannels, align 4, !tbaa !9
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %8 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %10 = icmp ult i32 %8, 4
  br i1 %10, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %11 = load ptr, ptr @channels, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.channel_t, ptr %11, i64 %indvars.iv
  store ptr null, ptr %12, align 8, !tbaa !11
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %13 = load ptr, ptr @channels, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.channel_t, ptr %13, i64 %indvars.iv.next
  store ptr null, ptr %14, align 8, !tbaa !11
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %15 = load ptr, ptr @channels, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.channel_t, ptr %15, i64 %indvars.iv.next.1
  store ptr null, ptr %16, align 8, !tbaa !11
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %17 = load ptr, ptr @channels, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.channel_t, ptr %17, i64 %indvars.iv.next.2
  store ptr null, ptr %18, align 8, !tbaa !11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !13

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %19 = load ptr, ptr @channels, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.channel_t, ptr %19, i64 %indvars.iv.epil
  store ptr null, ptr %20, align 8, !tbaa !11
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !15

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %2
  store i32 0, ptr @mus_paused, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %21, %._crit_edge
  %indvars.iv13 = phi i64 [ 1, %._crit_edge ], [ %indvars.iv.next14.5, %21 ]
  %22 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv13
  %23 = getelementptr inbounds i8, ptr %22, i64 40
  store i32 -1, ptr %23, align 8, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %22, i64 44
  store i32 -1, ptr %24, align 4, !tbaa !19
  %indvars.iv.next14 = add nuw nsw i64 %indvars.iv13, 1
  %25 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv.next14
  %26 = getelementptr inbounds i8, ptr %25, i64 40
  store i32 -1, ptr %26, align 8, !tbaa !17
  %27 = getelementptr inbounds i8, ptr %25, i64 44
  store i32 -1, ptr %27, align 4, !tbaa !19
  %indvars.iv.next14.1 = add nuw nsw i64 %indvars.iv13, 2
  %28 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv.next14.1
  %29 = getelementptr inbounds i8, ptr %28, i64 40
  store i32 -1, ptr %29, align 8, !tbaa !17
  %30 = getelementptr inbounds i8, ptr %28, i64 44
  store i32 -1, ptr %30, align 4, !tbaa !19
  %indvars.iv.next14.2 = add nuw nsw i64 %indvars.iv13, 3
  %31 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv.next14.2
  %32 = getelementptr inbounds i8, ptr %31, i64 40
  store i32 -1, ptr %32, align 8, !tbaa !17
  %33 = getelementptr inbounds i8, ptr %31, i64 44
  store i32 -1, ptr %33, align 4, !tbaa !19
  %indvars.iv.next14.3 = add nuw nsw i64 %indvars.iv13, 4
  %34 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv.next14.3
  %35 = getelementptr inbounds i8, ptr %34, i64 40
  store i32 -1, ptr %35, align 8, !tbaa !17
  %36 = getelementptr inbounds i8, ptr %34, i64 44
  store i32 -1, ptr %36, align 4, !tbaa !19
  %indvars.iv.next14.4 = add nuw nsw i64 %indvars.iv13, 5
  %37 = getelementptr inbounds [0 x %struct.sfxinfo_struct], ptr @S_sfx, i64 0, i64 %indvars.iv.next14.4
  %38 = getelementptr inbounds i8, ptr %37, i64 40
  store i32 -1, ptr %38, align 8, !tbaa !17
  %39 = getelementptr inbounds i8, ptr %37, i64 44
  store i32 -1, ptr %39, align 4, !tbaa !19
  %indvars.iv.next14.5 = add nuw nsw i64 %indvars.iv13, 6
  %exitcond16.not.5 = icmp eq i64 %indvars.iv.next14.5, 109
  br i1 %exitcond16.not.5, label %40, label %21, !llvm.loop !20

40:                                               ; preds = %21
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

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

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

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 = { nofree nounwind "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 = { cold nounwind }
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 = !{!"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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = !{!18, !10, i64 40}
!18 = !{!"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}
!19 = !{!18, !10, i64 44}
!20 = distinct !{!20, !14}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass