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

@numChannels = external dso_local global i32, align 4
@channels = 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 @S_getChannel(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %38, %2
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = load i32, ptr @numChannels, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %41

13:                                               ; preds = %9
  %14 = load ptr, ptr @channels, align 8, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds %struct.channel_t, ptr %14, i64 %16
  %18 = getelementptr inbounds %struct.channel_t, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = icmp ne ptr %19, null
  br i1 %20, label %22, label %21

21:                                               ; preds = %13
  br label %41

22:                                               ; preds = %13
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %36

25:                                               ; preds = %22
  %26 = load ptr, ptr @channels, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds %struct.channel_t, ptr %26, i64 %28
  %30 = getelementptr inbounds %struct.channel_t, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = icmp eq ptr %31, %32
  br i1 %33, label %34, label %36

34:                                               ; preds = %25
  %35 = load i32, ptr %6, align 4, !tbaa !9
  call void @S_StopChannel(i32 noundef %35)
  br label %41

36:                                               ; preds = %25, %22
  br label %37

37:                                               ; preds = %36
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %6, align 4, !tbaa !9
  br label %9, !llvm.loop !14

41:                                               ; preds = %34, %21, %9
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = load i32, ptr @numChannels, align 4, !tbaa !9
  %44 = icmp eq i32 %42, %43
  br i1 %44, label %45, label %76

45:                                               ; preds = %41
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %65, %45
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr @numChannels, align 4, !tbaa !9
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %68

50:                                               ; preds = %46
  %51 = load ptr, ptr @channels, align 8, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds %struct.channel_t, ptr %51, i64 %53
  %55 = getelementptr inbounds %struct.channel_t, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = getelementptr inbounds %struct.sfxinfo_struct, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %57, align 4, !tbaa !16
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.sfxinfo_struct, ptr %59, i32 0, i32 2
  %61 = load i32, ptr %60, align 4, !tbaa !16
  %62 = icmp sge i32 %58, %61
  br i1 %62, label %63, label %64

63:                                               ; preds = %50
  br label %68

64:                                               ; preds = %50
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %6, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %6, align 4, !tbaa !9
  br label %46, !llvm.loop !18

68:                                               ; preds = %63, %46
  %69 = load i32, ptr %6, align 4, !tbaa !9
  %70 = load i32, ptr @numChannels, align 4, !tbaa !9
  %71 = icmp eq i32 %69, %70
  br i1 %71, label %72, label %73

72:                                               ; preds = %68
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %88

73:                                               ; preds = %68
  %74 = load i32, ptr %6, align 4, !tbaa !9
  call void @S_StopChannel(i32 noundef %74)
  br label %75

75:                                               ; preds = %73
  br label %76

76:                                               ; preds = %75, %41
  %77 = load ptr, ptr @channels, align 8, !tbaa !5
  %78 = load i32, ptr %6, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds %struct.channel_t, ptr %77, i64 %79
  store ptr %80, ptr %7, align 8, !tbaa !5
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.channel_t, ptr %82, i32 0, i32 0
  store ptr %81, ptr %83, align 8, !tbaa !11
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.channel_t, ptr %85, i32 0, i32 1
  store ptr %84, ptr %86, align 8, !tbaa !13
  %87 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %87, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %88

88:                                               ; preds = %76, %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %89 = load i32, ptr %3, align 4
  ret i32 %89
}

; Function Attrs: nounwind uwtable
declare dso_local void @S_StopChannel(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 = { 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, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !10, i64 12}
!17 = !{!"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}
!18 = distinct !{!18, !15}


; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @S_getChannel(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @numChannels, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2
  %5 = load ptr, ptr @channels, align 8, !tbaa !9
  %.not22 = icmp eq ptr %0, null
  %wide.trip.count42 = zext nneg i32 %3 to i64
  br i1 %.not22, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %8
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %8 ], [ 0, %.lr.ph ]
  %6 = getelementptr inbounds %struct.channel_t, ptr %5, i64 %indvars.iv39
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %.not.us = icmp eq ptr %7, null
  br i1 %.not.us, label %.loopexit.loopexit, label %8

8:                                                ; preds = %.lr.ph.split.us
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next40, %wide.trip.count42
  br i1 %exitcond43.not, label %.preheader, label %.lr.ph.split.us, !llvm.loop !13

.lr.ph.split:                                     ; preds = %.lr.ph, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %.lr.ph ]
  %9 = getelementptr inbounds %struct.channel_t, ptr %5, i64 %indvars.iv
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %.loopexit.loopexit51, label %11

11:                                               ; preds = %.lr.ph.split
  %12 = getelementptr inbounds i8, ptr %9, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  %14 = icmp eq ptr %13, %0
  br i1 %14, label %15, label %17

15:                                               ; preds = %11
  %16 = trunc nuw nsw i64 %indvars.iv to i32
  tail call void @S_StopChannel(i32 noundef %16)
  %.pre = load i32, ptr @numChannels, align 4, !tbaa !5
  br label %.loopexit

17:                                               ; preds = %11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count42
  br i1 %exitcond.not, label %.preheader, label %.lr.ph.split, !llvm.loop !13

.loopexit.loopexit:                               ; preds = %.lr.ph.split.us
  %18 = trunc nuw nsw i64 %indvars.iv39 to i32
  br label %.loopexit

.loopexit.loopexit51:                             ; preds = %.lr.ph.split
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit51, %.loopexit.loopexit, %2, %15
  %20 = phi i32 [ %.pre, %15 ], [ %3, %2 ], [ %3, %.loopexit.loopexit ], [ %3, %.loopexit.loopexit51 ]
  %.02025 = phi i32 [ %16, %15 ], [ 0, %2 ], [ %18, %.loopexit.loopexit ], [ %19, %.loopexit.loopexit51 ]
  %21 = icmp eq i32 %.02025, %20
  br i1 %21, label %.preheader, label %36

.preheader:                                       ; preds = %17, %8, %.loopexit
  %22 = phi i32 [ %20, %.loopexit ], [ %3, %8 ], [ %3, %17 ]
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %.lr.ph31, label %._crit_edge

.lr.ph31:                                         ; preds = %.preheader
  %24 = load ptr, ptr @channels, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %1, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !16
  %wide.trip.count47 = zext nneg i32 %22 to i64
  br label %27

27:                                               ; preds = %.lr.ph31, %32
  %indvars.iv44 = phi i64 [ 0, %.lr.ph31 ], [ %indvars.iv.next45, %32 ]
  %28 = getelementptr inbounds %struct.channel_t, ptr %24, i64 %indvars.iv44
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %29, i64 12
  %31 = load i32, ptr %30, align 4, !tbaa !16
  %.not23 = icmp slt i32 %31, %26
  br i1 %.not23, label %32, label %._crit_edge.loopexit

32:                                               ; preds = %27
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count47
  br i1 %exitcond48.not, label %._crit_edge.thread, label %27, !llvm.loop !18

._crit_edge.loopexit:                             ; preds = %27
  %33 = trunc nuw nsw i64 %indvars.iv44 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.1.lcssa = phi i32 [ 0, %.preheader ], [ %33, %._crit_edge.loopexit ]
  %34 = icmp eq i32 %.1.lcssa, %22
  br i1 %34, label %._crit_edge.thread, label %35

35:                                               ; preds = %._crit_edge
  tail call void @S_StopChannel(i32 noundef %.1.lcssa)
  br label %36

36:                                               ; preds = %35, %.loopexit
  %.2 = phi i32 [ %.1.lcssa, %35 ], [ %.02025, %.loopexit ]
  %37 = load ptr, ptr @channels, align 8, !tbaa !9
  %38 = zext nneg i32 %.2 to i64
  %39 = getelementptr inbounds %struct.channel_t, ptr %37, i64 %38
  store ptr %1, ptr %39, align 8, !tbaa !11
  %40 = getelementptr inbounds i8, ptr %39, i64 8
  store ptr %0, ptr %40, align 8, !tbaa !15
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %32, %._crit_edge, %36
  %.0 = phi i32 [ %.2, %36 ], [ -1, %._crit_edge ], [ -1, %32 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local void @S_StopChannel(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" }

!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 = !{!12, !10, i64 8}
!16 = !{!17, !6, i64 12}
!17 = !{!"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}
!18 = distinct !{!18, !14}


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