; 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.mobj_s = type { %struct.thinker_s, i32, i32, i32, ptr, ptr, i32, i32, i32, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32, ptr, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, %struct.mapthing_t, ptr }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.mapthing_t = type { i16, i16, i16, i16, i16 }

@gamemap = external global i32, align 4
@snd_SfxVolume = external dso_local global i32, align 4
@finesine = external global [10240 x i32], align 16

; 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_AdjustSoundParams(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %17 = load ptr, ptr %11, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mobj_s, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 8, !tbaa !9
  %24 = sub nsw i32 %20, %23
  %25 = call i32 @llvm.abs.i32(i32 %24, i1 true)
  store i32 %25, ptr %13, align 4, !tbaa !15
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mobj_s, ptr %26, i32 0, i32 2
  %28 = load i32, ptr %27, align 4, !tbaa !16
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mobj_s, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !16
  %32 = sub nsw i32 %28, %31
  %33 = call i32 @llvm.abs.i32(i32 %32, i1 true)
  store i32 %33, ptr %14, align 4, !tbaa !15
  %34 = load i32, ptr %13, align 4, !tbaa !15
  %35 = load i32, ptr %14, align 4, !tbaa !15
  %36 = add nsw i32 %34, %35
  %37 = load i32, ptr %13, align 4, !tbaa !15
  %38 = load i32, ptr %14, align 4, !tbaa !15
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %40, label %42

40:                                               ; preds = %5
  %41 = load i32, ptr %13, align 4, !tbaa !15
  br label %44

42:                                               ; preds = %5
  %43 = load i32, ptr %14, align 4, !tbaa !15
  br label %44

44:                                               ; preds = %42, %40
  %45 = phi i32 [ %41, %40 ], [ %43, %42 ]
  %46 = ashr i32 %45, 1
  %47 = sub nsw i32 %36, %46
  store i32 %47, ptr %12, align 4, !tbaa !15
  %48 = load i32, ptr @gamemap, align 4, !tbaa !15
  %49 = icmp ne i32 %48, 8
  br i1 %49, label %50, label %54

50:                                               ; preds = %44
  %51 = load i32, ptr %12, align 4, !tbaa !15
  %52 = icmp sgt i32 %51, 78643200
  br i1 %52, label %53, label %54

53:                                               ; preds = %50
  store i32 0, ptr %6, align 4
  store i32 1, ptr %16, align 4
  br label %133

54:                                               ; preds = %50, %44
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mobj_s, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 8, !tbaa !9
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mobj_s, ptr %58, i32 0, i32 2
  %60 = load i32, ptr %59, align 4, !tbaa !16
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mobj_s, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 8, !tbaa !9
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mobj_s, ptr %64, i32 0, i32 2
  %66 = load i32, ptr %65, align 4, !tbaa !16
  %67 = call i32 @R_PointToAngle2(i32 noundef %57, i32 noundef %60, i32 noundef %63, i32 noundef %66)
  store i32 %67, ptr %15, align 4, !tbaa !15
  %68 = load i32, ptr %15, align 4, !tbaa !15
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.mobj_s, ptr %69, i32 0, i32 6
  %71 = load i32, ptr %70, align 8, !tbaa !17
  %72 = icmp ugt i32 %68, %71
  br i1 %72, label %73, label %79

73:                                               ; preds = %54
  %74 = load i32, ptr %15, align 4, !tbaa !15
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.mobj_s, ptr %75, i32 0, i32 6
  %77 = load i32, ptr %76, align 8, !tbaa !17
  %78 = sub i32 %74, %77
  store i32 %78, ptr %15, align 4, !tbaa !15
  br label %86

79:                                               ; preds = %54
  %80 = load i32, ptr %15, align 4, !tbaa !15
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.mobj_s, ptr %81, i32 0, i32 6
  %83 = load i32, ptr %82, align 8, !tbaa !17
  %84 = sub i32 -1, %83
  %85 = add i32 %80, %84
  store i32 %85, ptr %15, align 4, !tbaa !15
  br label %86

86:                                               ; preds = %79, %73
  %87 = load i32, ptr %15, align 4, !tbaa !15
  %88 = lshr i32 %87, 19
  store i32 %88, ptr %15, align 4, !tbaa !15
  %89 = load i32, ptr %15, align 4, !tbaa !15
  %90 = zext i32 %89 to i64
  %91 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %90
  %92 = load i32, ptr %91, align 4, !tbaa !15
  %93 = call i32 @FixedMul(i32 noundef 6291456, i32 noundef %92)
  %94 = ashr i32 %93, 16
  %95 = sub nsw i32 128, %94
  %96 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 %95, ptr %96, align 4, !tbaa !15
  %97 = load i32, ptr %12, align 4, !tbaa !15
  %98 = icmp slt i32 %97, 10485760
  br i1 %98, label %99, label %102

99:                                               ; preds = %86
  %100 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !15
  %101 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %100, ptr %101, align 4, !tbaa !15
  br label %128

102:                                              ; preds = %86
  %103 = load i32, ptr @gamemap, align 4, !tbaa !15
  %104 = icmp eq i32 %103, 8
  br i1 %104, label %105, label %119

105:                                              ; preds = %102
  %106 = load i32, ptr %12, align 4, !tbaa !15
  %107 = icmp sgt i32 %106, 78643200
  br i1 %107, label %108, label %109

108:                                              ; preds = %105
  store i32 78643200, ptr %12, align 4, !tbaa !15
  br label %109

109:                                              ; preds = %108, %105
  %110 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !15
  %111 = sub nsw i32 %110, 15
  %112 = load i32, ptr %12, align 4, !tbaa !15
  %113 = sub nsw i32 78643200, %112
  %114 = ashr i32 %113, 16
  %115 = mul nsw i32 %111, %114
  %116 = sdiv i32 %115, 1040
  %117 = add nsw i32 15, %116
  %118 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %117, ptr %118, align 4, !tbaa !15
  br label %127

119:                                              ; preds = %102
  %120 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !15
  %121 = load i32, ptr %12, align 4, !tbaa !15
  %122 = sub nsw i32 78643200, %121
  %123 = ashr i32 %122, 16
  %124 = mul nsw i32 %120, %123
  %125 = sdiv i32 %124, 1040
  %126 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %125, ptr %126, align 4, !tbaa !15
  br label %127

127:                                              ; preds = %119, %109
  br label %128

128:                                              ; preds = %127, %99
  %129 = load ptr, ptr %9, align 8, !tbaa !5
  %130 = load i32, ptr %129, align 4, !tbaa !15
  %131 = icmp sgt i32 %130, 0
  %132 = zext i1 %131 to i32
  store i32 %132, ptr %6, align 4
  store i32 1, ptr %16, align 4
  br label %133

133:                                              ; preds = %128, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  %134 = load i32, ptr %6, align 4
  ret i32 %134
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

declare i32 @R_PointToAngle2(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) #4

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { inlinehint 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 #5 = { 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, !12, i64 24}
!10 = !{!"mobj_s", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !12, i64 96, !12, i64 100, !12, i64 104, !12, i64 108, !12, i64 112, !12, i64 116, !12, i64 120, !12, i64 124, !12, i64 128, !6, i64 136, !12, i64 144, !6, i64 152, !12, i64 160, !12, i64 164, !12, i64 168, !12, i64 172, !6, i64 176, !12, i64 184, !12, i64 188, !6, i64 192, !12, i64 200, !13, i64 204, !6, i64 216}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!10, !12, i64 28}
!17 = !{!10, !12, i64 56}


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

@gamemap = external local_unnamed_addr global i32, align 4
@snd_SfxVolume = external dso_local local_unnamed_addr global i32, align 4
@finesine = external local_unnamed_addr global [10240 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @S_AdjustSoundParams(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef writeonly %3, ptr nocapture noundef readnone %4) local_unnamed_addr #0 {
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %1, i64 24
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %10 = sub nsw i32 %7, %9
  %11 = tail call i32 @llvm.abs.i32(i32 %10, i1 true)
  %12 = getelementptr inbounds i8, ptr %0, i64 28
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %1, i64 28
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = sub nsw i32 %13, %15
  %17 = tail call i32 @llvm.abs.i32(i32 %16, i1 true)
  %18 = add nuw nsw i32 %17, %11
  %19 = tail call i32 @llvm.umin.i32(i32 %11, i32 %17)
  %20 = lshr i32 %19, 1
  %21 = sub nsw i32 %18, %20
  %22 = load i32, ptr @gamemap, align 4, !tbaa !15
  %23 = icmp ne i32 %22, 8
  %24 = icmp sgt i32 %21, 78643200
  %or.cond = select i1 %23, i1 %24, i1 false
  br i1 %or.cond, label %61, label %25

25:                                               ; preds = %5
  %26 = tail call i32 @R_PointToAngle2(i32 noundef %7, i32 noundef %13, i32 noundef %9, i32 noundef %15) #5
  %27 = getelementptr inbounds i8, ptr %0, i64 56
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = icmp ugt i32 %26, %28
  %30 = xor i32 %28, -1
  %31 = sub i32 0, %28
  %.0.p = select i1 %29, i32 %31, i32 %30
  %.0 = add i32 %.0.p, %26
  %32 = lshr i32 %.0, 19
  %33 = zext nneg i32 %32 to i64
  %34 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !15
  %36 = tail call i32 @FixedMul(i32 noundef 6291456, i32 noundef %35)
  %37 = ashr i32 %36, 16
  %38 = sub nsw i32 128, %37
  store i32 %38, ptr %3, align 4, !tbaa !15
  %39 = icmp slt i32 %21, 10485760
  br i1 %39, label %40, label %42

40:                                               ; preds = %25
  %41 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !15
  br label %58

42:                                               ; preds = %25
  %43 = load i32, ptr @gamemap, align 4, !tbaa !15
  %44 = icmp eq i32 %43, 8
  %45 = load i32, ptr @snd_SfxVolume, align 4, !tbaa !15
  br i1 %44, label %46, label %53

46:                                               ; preds = %42
  %47 = add nsw i32 %45, -15
  %48 = tail call i32 @llvm.usub.sat.i32(i32 78643200, i32 %21)
  %49 = lshr i32 %48, 16
  %50 = mul nsw i32 %47, %49
  %51 = sdiv i32 %50, 1040
  %52 = add nsw i32 %51, 15
  br label %58

53:                                               ; preds = %42
  %54 = sub nsw i32 78643200, %21
  %55 = ashr i32 %54, 16
  %56 = mul nsw i32 %45, %55
  %57 = sdiv i32 %56, 1040
  br label %58

58:                                               ; preds = %46, %53, %40
  %.sink = phi i32 [ %52, %46 ], [ %57, %53 ], [ %41, %40 ]
  store i32 %.sink, ptr %2, align 4, !tbaa !15
  %59 = icmp sgt i32 %.sink, 0
  %60 = zext i1 %59 to i32
  br label %61

61:                                               ; preds = %5, %58
  %.034 = phi i32 [ %60, %58 ], [ 0, %5 ]
  ret i32 %.034
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

declare i32 @R_PointToAngle2(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.usub.sat.i32(i32, i32) #4

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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { inlinehint 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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, !11, i64 24}
!6 = !{!"mobj_s", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !8, i64 40, !8, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !8, i64 72, !8, i64 80, !8, i64 88, !11, i64 96, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !8, i64 136, !11, i64 144, !8, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !8, i64 176, !11, i64 184, !11, i64 188, !8, i64 192, !11, i64 200, !12, i64 204, !8, i64 216}
!7 = !{!"thinker_s", !8, i64 0, !8, i64 8, !9, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !9, i64 0}
!14 = !{!6, !11, i64 28}
!15 = !{!11, !11, i64 0}
!16 = !{!6, !11, i64 56}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass