; LLVM IR from zsiciarz/aquila :: aquila/source (WaveFileHandler)

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

; 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: mustprogress uwtable
define dso_local void @_ZN6Aquila15WaveFileHandler10encode8bitERKNS_12SignalSourceEPsm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %53, %3
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %56

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %7, align 8, !tbaa !9
  %20 = mul i64 2, %19
  %21 = load ptr, ptr %18, align 8, !tbaa !11
  %22 = getelementptr inbounds ptr, ptr %21, i64 6
  %23 = load ptr, ptr %22, align 8
  %24 = call noundef double %23(ptr noundef nonnull align 8 dereferenceable(40) %18, i64 noundef %20)
  %25 = fadd double %24, 1.280000e+02
  %26 = fptoui double %25 to i8
  store i8 %26, ptr %8, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = mul i64 2, %28
  %30 = add i64 %29, 1
  %31 = load ptr, ptr %27, align 8, !tbaa !11
  %32 = getelementptr inbounds ptr, ptr %31, i64 6
  %33 = load ptr, ptr %32, align 8
  %34 = call noundef double %33(ptr noundef nonnull align 8 dereferenceable(40) %27, i64 noundef %30)
  %35 = fadd double %34, 1.280000e+02
  %36 = fptoui double %35 to i8
  store i8 %36, ptr %9, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %37 = load i8, ptr %8, align 1, !tbaa !13
  %38 = zext i8 %37 to i16
  store i16 %38, ptr %10, align 2, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  %39 = load i8, ptr %9, align 1, !tbaa !13
  %40 = zext i8 %39 to i16
  store i16 %40, ptr %11, align 2, !tbaa !14
  %41 = load i16, ptr %10, align 2, !tbaa !14
  %42 = sext i16 %41 to i32
  %43 = shl i32 %42, 8
  %44 = and i32 %43, 65280
  %45 = load i16, ptr %11, align 2, !tbaa !14
  %46 = sext i16 %45 to i32
  %47 = and i32 %46, 255
  %48 = or i32 %44, %47
  %49 = trunc i32 %48 to i16
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = getelementptr inbounds i16, ptr %50, i64 %51
  store i16 %49, ptr %52, align 2, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  br label %53

53:                                               ; preds = %17
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = add i64 %54, 1
  store i64 %55, ptr %7, align 8, !tbaa !9
  br label %12, !llvm.loop !16

56:                                               ; preds = %16
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6Aquila15WaveFileHandler10encode8bitERKNS_12SignalSourceEPsm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %3
  ret void

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.012 = phi i64 [ %23, %.lr.ph ], [ 0, %3 ]
  %4 = shl i64 %.012, 1
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 48
  %7 = load ptr, ptr %6, align 8
  %8 = tail call noundef double %7(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %4)
  %9 = fadd double %8, 1.280000e+02
  %10 = fptoui double %9 to i8
  %11 = or disjoint i64 %4, 1
  %12 = load ptr, ptr %0, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 48
  %14 = load ptr, ptr %13, align 8
  %15 = tail call noundef double %14(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %11)
  %16 = fadd double %15, 1.280000e+02
  %17 = fptoui double %16 to i8
  %18 = zext i8 %10 to i16
  %19 = shl nuw i16 %18, 8
  %20 = zext i8 %17 to i16
  %21 = or disjoint i16 %19, %20
  %22 = getelementptr inbounds i16, ptr %1, i64 %.012
  store i16 %21, ptr %22, align 2, !tbaa !8
  %23 = add nuw i64 %.012, 1
  %exitcond.not = icmp eq i64 %23, %2
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11
}

attributes #0 = { mustprogress 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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"short", !10, i64 0}
!10 = !{!"omnipotent char", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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