; LLVM IR from TobiasKozel/VAE :: external/portaudio/qa/loopback/src (audio_analyzer)

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

@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

; 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

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @PaQa_SaveRecordingToWaveFile(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.WAV_Writer_s, align 8
  %7 = alloca i32, align 4
  %8 = alloca [200 x i16], align 16
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca double, align 8
  %15 = alloca i32, align 4
  %16 = 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 16, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 400, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.PaQaRecording_s, ptr %17, i32 0, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !11
  store i32 %19, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.PaQaRecording_s, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = getelementptr inbounds float, ptr %22, i64 0
  store ptr %23, ptr %11, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.PaQaRecording_s, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = call i64 @Audio_WAV_OpenWriter(ptr noundef %6, ptr noundef %24, i32 noundef %27, i32 noundef 1)
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %7, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %2
  br label %96

33:                                               ; preds = %2
  br label %34

34:                                               ; preds = %88, %33
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %89

37:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = icmp sgt i32 %38, 200
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  br label %43

41:                                               ; preds = %37
  %42 = load i32, ptr %10, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %41, %40
  %44 = phi i32 [ 200, %40 ], [ %42, %41 ]
  store i32 %44, ptr %13, align 4, !tbaa !9
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %71, %43
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = load i32, ptr %13, align 4, !tbaa !9
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %49, label %74

49:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %50 = load ptr, ptr %11, align 8, !tbaa !5
  %51 = getelementptr inbounds float, ptr %50, i32 1
  store ptr %51, ptr %11, align 8, !tbaa !5
  %52 = load float, ptr %50, align 4, !tbaa !15
  %53 = fpext float %52 to double
  store double %53, ptr %14, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %54 = load double, ptr %14, align 8, !tbaa !17
  %55 = fmul double %54, 3.276800e+04
  %56 = fptosi double %55 to i32
  store i32 %56, ptr %15, align 4, !tbaa !9
  %57 = load i32, ptr %15, align 4, !tbaa !9
  %58 = icmp sgt i32 %57, 32767
  br i1 %58, label %59, label %60

59:                                               ; preds = %49
  store i32 32767, ptr %15, align 4, !tbaa !9
  br label %65

60:                                               ; preds = %49
  %61 = load i32, ptr %15, align 4, !tbaa !9
  %62 = icmp slt i32 %61, -32768
  br i1 %62, label %63, label %64

63:                                               ; preds = %60
  store i32 -32768, ptr %15, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %63, %60
  br label %65

65:                                               ; preds = %64, %59
  %66 = load i32, ptr %15, align 4, !tbaa !9
  %67 = trunc i32 %66 to i16
  %68 = load i32, ptr %12, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [200 x i16], ptr %8, i64 0, i64 %69
  store i16 %67, ptr %70, align 2, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  br label %71

71:                                               ; preds = %65
  %72 = load i32, ptr %12, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %12, align 4, !tbaa !9
  br label %45, !llvm.loop !21

74:                                               ; preds = %45
  %75 = getelementptr inbounds [200 x i16], ptr %8, i64 0, i64 0
  %76 = load i32, ptr %13, align 4, !tbaa !9
  %77 = call i64 @Audio_WAV_WriteShorts(ptr noundef %6, ptr noundef %75, i32 noundef %76)
  %78 = trunc i64 %77 to i32
  store i32 %78, ptr %7, align 4, !tbaa !9
  %79 = load i32, ptr %7, align 4, !tbaa !9
  %80 = icmp slt i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %74
  store i32 2, ptr %16, align 4
  br label %86

82:                                               ; preds = %74
  %83 = load i32, ptr %13, align 4, !tbaa !9
  %84 = load i32, ptr %10, align 4, !tbaa !9
  %85 = sub nsw i32 %84, %83
  store i32 %85, ptr %10, align 4, !tbaa !9
  store i32 0, ptr %16, align 4
  br label %86

86:                                               ; preds = %82, %81
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %87 = load i32, ptr %16, align 4
  switch i32 %87, label %100 [
    i32 0, label %88
    i32 2, label %96
  ]

88:                                               ; preds = %86
  br label %34, !llvm.loop !23

89:                                               ; preds = %34
  %90 = call i64 @Audio_WAV_CloseWriter(ptr noundef %6)
  %91 = trunc i64 %90 to i32
  store i32 %91, ptr %7, align 4, !tbaa !9
  %92 = load i32, ptr %7, align 4, !tbaa !9
  %93 = icmp slt i32 %92, 0
  br i1 %93, label %94, label %95

94:                                               ; preds = %89
  br label %96

95:                                               ; preds = %89
  store i32 0, ptr %3, align 4
  store i32 1, ptr %16, align 4
  br label %100

96:                                               ; preds = %94, %86, %32
  %97 = load i32, ptr %7, align 4, !tbaa !9
  %98 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %97)
  %99 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %99, ptr %3, align 4
  store i32 1, ptr %16, align 4
  br label %100

100:                                              ; preds = %96, %95, %86
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 400, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #3
  %101 = load i32, ptr %3, align 4
  ret i32 %101
}

declare i64 @Audio_WAV_OpenWriter(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #1

declare i64 @Audio_WAV_WriteShorts(ptr noundef, ptr noundef, i32 noundef) #1

declare i64 @Audio_WAV_CloseWriter(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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 16}
!12 = !{!"PaQaRecording_s", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 20}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !10, i64 20}
!15 = !{!16, !16, i64 0}
!16 = !{!"float", !7, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"double", !7, i64 0}
!19 = !{!20, !20, i64 0}
!20 = !{!"short", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


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

@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

; 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: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @PaQa_SaveRecordingToWaveFile(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.WAV_Writer_s, align 8
  %4 = alloca [200 x i16], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 400, ptr nonnull %4) #5
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %0, i64 20
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = call i64 @Audio_WAV_OpenWriter(ptr noundef nonnull %3, ptr noundef %1, i32 noundef %10, i32 noundef 1) #5
  %12 = trunc i64 %11 to i32
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2, %.loopexit43
  %.030 = phi i32 [ %36, %.loopexit43 ], [ %6, %2 ]
  %.029 = phi ptr [ %.lcssa, %.loopexit43 ], [ %8, %2 ]
  %14 = icmp sgt i32 %.030, 0
  br i1 %14, label %15, label %37

15:                                               ; preds = %.preheader
  %16 = call i32 @llvm.umin.i32(i32 %.030, i32 200)
  %wide.trip.count = zext nneg i32 %16 to i64
  %min.iters.check = icmp ult i32 %.030, 4
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %15
  %n.vec = and i64 %wide.trip.count, 252
  %17 = shl nuw nsw i64 %n.vec, 2
  %ind.end = getelementptr i8, ptr %.029, i64 %17
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %.029, i64 %offset.idx
  %wide.load = load <4 x float>, ptr %next.gep, align 4, !tbaa !13
  %18 = fpext <4 x float> %wide.load to <4 x double>
  %19 = fmul <4 x double> %18, <double 3.276800e+04, double 3.276800e+04, double 3.276800e+04, double 3.276800e+04>
  %20 = fptosi <4 x double> %19 to <4 x i32>
  %21 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %20, <4 x i32> <i32 -32768, i32 -32768, i32 -32768, i32 -32768>)
  %22 = call <4 x i32> @llvm.smin.v4i32(<4 x i32> %21, <4 x i32> <i32 32767, i32 32767, i32 32767, i32 32767>)
  %23 = trunc nsw <4 x i32> %22 to <4 x i16>
  %24 = getelementptr inbounds [200 x i16], ptr %4, i64 0, i64 %index
  store <4 x i16> %23, ptr %24, align 8, !tbaa !15
  %index.next = add nuw i64 %index, 4
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.loopexit43, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %15
  %indvars.iv.ph = phi i64 [ 0, %15 ], [ %n.vec, %middle.block ]
  %.136.ph = phi ptr [ %.029, %15 ], [ %ind.end, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.136 = phi ptr [ %26, %scalar.ph ], [ %.136.ph, %scalar.ph.preheader ]
  %26 = getelementptr inbounds i8, ptr %.136, i64 4
  %27 = load float, ptr %.136, align 4, !tbaa !13
  %28 = fpext float %27 to double
  %29 = fmul double %28, 3.276800e+04
  %30 = fptosi double %29 to i32
  %spec.store.select = call i32 @llvm.smax.i32(i32 %30, i32 -32768)
  %.025 = call i32 @llvm.smin.i32(i32 %spec.store.select, i32 32767)
  %31 = trunc nsw i32 %.025 to i16
  %32 = getelementptr inbounds [200 x i16], ptr %4, i64 0, i64 %indvars.iv
  store i16 %31, ptr %32, align 2, !tbaa !15
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit43, label %scalar.ph, !llvm.loop !21

.loopexit43:                                      ; preds = %scalar.ph, %middle.block
  %.lcssa = phi ptr [ %ind.end, %middle.block ], [ %26, %scalar.ph ]
  %33 = call i64 @Audio_WAV_WriteShorts(ptr noundef nonnull %3, ptr noundef nonnull %4, i32 noundef %16) #5
  %34 = trunc i64 %33 to i32
  %35 = icmp slt i32 %34, 0
  %36 = sub nsw i32 %.030, %16
  br i1 %35, label %.loopexit, label %.preheader

37:                                               ; preds = %.preheader
  %38 = call i64 @Audio_WAV_CloseWriter(ptr noundef nonnull %3) #5
  %39 = trunc i64 %38 to i32
  %40 = icmp slt i32 %39, 0
  br i1 %40, label %.loopexit, label %42

.loopexit:                                        ; preds = %.loopexit43, %37, %2
  %.027 = phi i32 [ %12, %2 ], [ %39, %37 ], [ %34, %.loopexit43 ]
  %41 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %.027)
  br label %42

42:                                               ; preds = %37, %.loopexit
  %.026 = phi i32 [ %.027, %.loopexit ], [ 0, %37 ]
  call void @llvm.lifetime.end.p0(i64 400, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #5
  ret i32 %.026
}

declare i64 @Audio_WAV_OpenWriter(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

declare i64 @Audio_WAV_WriteShorts(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

declare i64 @Audio_WAV_CloseWriter(ptr noundef) local_unnamed_addr #3

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

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

; 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 <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smin.v4i32(<4 x i32>, <4 x i32>) #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { "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, !7, i64 16}
!6 = !{!"PaQaRecording_s", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 20}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !7, i64 20}
!13 = !{!14, !14, i64 0}
!14 = !{!"float", !8, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"short", !8, i64 0}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !20, !19}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass