; LLVM IR from glaretechnologies/substrata :: opus/opus-1.3.1/celt (entenc)

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

; 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 void @ec_enc_bits(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.ec_ctx, ptr %9, i32 0, i32 3
  %11 = load i32, ptr %10, align 8, !tbaa !11
  store i32 %11, ptr %7, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ec_ctx, ptr %12, i32 0, i32 4
  %14 = load i32, ptr %13, align 4, !tbaa !13
  store i32 %14, ptr %8, align 4, !tbaa !9
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = add i32 %15, %16
  %18 = icmp ugt i32 %17, 32
  br i1 %18, label %19, label %37

19:                                               ; preds = %3
  br label %20

20:                                               ; preds = %33, %19
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = and i32 %22, 255
  %24 = call i32 @ec_write_byte_at_end(ptr noundef %21, i32 noundef %23)
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.ec_ctx, ptr %25, i32 0, i32 11
  %27 = load i32, ptr %26, align 8, !tbaa !14
  %28 = or i32 %27, %24
  store i32 %28, ptr %26, align 8, !tbaa !14
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = lshr i32 %29, 8
  store i32 %30, ptr %7, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = sub nsw i32 %31, 8
  store i32 %32, ptr %8, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %20
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = icmp sge i32 %34, 8
  br i1 %35, label %20, label %36, !llvm.loop !15

36:                                               ; preds = %33
  br label %37

37:                                               ; preds = %36, %3
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = shl i32 %38, %39
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = or i32 %41, %40
  store i32 %42, ptr %7, align 4, !tbaa !9
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = add i32 %44, %43
  store i32 %45, ptr %8, align 4, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.ec_ctx, ptr %47, i32 0, i32 3
  store i32 %46, ptr %48, align 8, !tbaa !11
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ec_ctx, ptr %50, i32 0, i32 4
  store i32 %49, ptr %51, align 4, !tbaa !13
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ec_ctx, ptr %53, i32 0, i32 5
  %55 = load i32, ptr %54, align 8, !tbaa !17
  %56 = add i32 %55, %52
  store i32 %56, ptr %54, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ec_write_byte_at_end(ptr noundef, 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, !10, i64 16}
!12 = !{!"ec_ctx", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48}
!13 = !{!12, !10, i64 20}
!14 = !{!12, !10, i64 48}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !10, i64 24}


; 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: nounwind uwtable
define dso_local void @ec_enc_bits(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 20
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = add i32 %7, %2
  %9 = icmp ugt i32 %8, 32
  br i1 %9, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %3
  %10 = getelementptr inbounds i8, ptr %0, i64 48
  br label %11

11:                                               ; preds = %.preheader, %11
  %.020 = phi i32 [ %16, %11 ], [ %5, %.preheader ]
  %.0 = phi i32 [ %17, %11 ], [ %7, %.preheader ]
  %12 = and i32 %.020, 255
  %13 = tail call i32 @ec_write_byte_at_end(ptr noundef nonnull %0, i32 noundef %12)
  %14 = load i32, ptr %10, align 8, !tbaa !12
  %15 = or i32 %14, %13
  store i32 %15, ptr %10, align 8, !tbaa !12
  %16 = lshr i32 %.020, 8
  %17 = add nsw i32 %.0, -8
  %18 = icmp sgt i32 %.0, 15
  br i1 %18, label %11, label %.loopexit.loopexit, !llvm.loop !13

.loopexit.loopexit:                               ; preds = %11
  %.pre = add i32 %17, %2
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %3
  %.pre-phi = phi i32 [ %.pre, %.loopexit.loopexit ], [ %8, %3 ]
  %.121 = phi i32 [ %16, %.loopexit.loopexit ], [ %5, %3 ]
  %.1 = phi i32 [ %17, %.loopexit.loopexit ], [ %7, %3 ]
  %19 = shl i32 %1, %.1
  %20 = or i32 %19, %.121
  store i32 %20, ptr %4, align 8, !tbaa !5
  store i32 %.pre-phi, ptr %6, align 4, !tbaa !11
  %21 = getelementptr inbounds i8, ptr %0, i64 24
  %22 = load i32, ptr %21, align 8, !tbaa !15
  %23 = add i32 %22, %2
  store i32 %23, ptr %21, align 8, !tbaa !15
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ec_write_byte_at_end(ptr noundef, 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, !10, i64 16}
!6 = !{!"ec_ctx", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 20}
!12 = !{!6, !10, i64 48}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!6, !10, i64 24}


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