; LLVM IR from vteromero/VTEnc :: encode (vtenc_encode64)

; 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.encctx64 = type { ptr, i64, i32, i64, %struct.enc_stack, %struct.bswriter }
%struct.enc_stack = type { [64 x %struct.enc_bit_cluster], i64 }
%struct.enc_bit_cluster = type { i64, i64, i32 }
%struct.bswriter = type { i64, i32, ptr, ptr, ptr }
%struct.vtenc = type { %struct.vtenc_enc_params, i64 }
%struct.vtenc_enc_params = type { i32, i32, i64 }

@BITS_SIZE_MASK = external hidden constant [65 x i64], 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 @vtenc_encode64(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  %14 = alloca %struct.encctx64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.vtenc, ptr %16, i32 0, i32 0
  %18 = getelementptr inbounds %struct.vtenc_enc_params, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = icmp ne i32 %19, 0
  %21 = zext i1 %20 to i64
  %22 = load i64, ptr getelementptr inbounds ([65 x i64], ptr @BITS_SIZE_MASK, i64 0, i64 56), align 16, !tbaa !9
  %23 = load i64, ptr getelementptr inbounds ([65 x i64], ptr @BITS_SIZE_MASK, i64 0, i64 56), align 16, !tbaa !9
  %24 = select i1 %20, i64 %22, i64 %23
  store i64 %24, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1616, ptr %14) #3
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.vtenc, ptr %25, i32 0, i32 1
  store i64 0, ptr %26, align 8, !tbaa !15
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = load i64, ptr %13, align 8, !tbaa !9
  %29 = icmp ugt i64 %27, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %5
  store i32 -2, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %47

31:                                               ; preds = %5
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = load ptr, ptr %10, align 8, !tbaa !5
  %36 = load i64, ptr %11, align 8, !tbaa !9
  %37 = call i32 @encctx_init64(ptr noundef %14, ptr noundef %32, ptr noundef %33, i64 noundef %34, ptr noundef %35, i64 noundef %36)
  store i32 %37, ptr %12, align 4, !tbaa !16
  %38 = load i32, ptr %12, align 4, !tbaa !16
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %42

40:                                               ; preds = %31
  %41 = load i32, ptr %12, align 4, !tbaa !16
  store i32 %41, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %47

42:                                               ; preds = %31
  call void @encode_bit_cluster_tree64(ptr noundef %14)
  %43 = call i64 @encctx_close64(ptr noundef %14)
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.vtenc, ptr %44, i32 0, i32 1
  store i64 %43, ptr %45, align 8, !tbaa !15
  %46 = load i32, ptr %12, align 4, !tbaa !16
  store i32 %46, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %47

47:                                               ; preds = %42, %40, %30
  call void @llvm.lifetime.end.p0(i64 1616, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %48 = load i32, ptr %6, align 4
  ret i32 %48
}

; Function Attrs: nounwind uwtable
declare hidden i32 @encctx_init64(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @encode_bit_cluster_tree64(ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @encctx_close64(ptr noundef) #2

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 = { 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 #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 = !{!"long", !7, i64 0}
!11 = !{!12, !14, i64 0}
!12 = !{!"vtenc", !13, i64 0, !10, i64 16}
!13 = !{!"vtenc_enc_params", !14, i64 0, !14, i64 4, !10, i64 8}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !10, i64 16}
!16 = !{!14, !14, i64 0}


; 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.encctx64 = type { ptr, i64, i32, i64, %struct.enc_stack, %struct.bswriter }
%struct.enc_stack = type { [64 x %struct.enc_bit_cluster], i64 }
%struct.enc_bit_cluster = type { i64, i64, i32 }
%struct.bswriter = type { i64, i32, ptr, ptr, ptr }

@BITS_SIZE_MASK = external hidden local_unnamed_addr constant [65 x i64], align 16

; 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: nounwind uwtable
define dso_local i32 @vtenc_encode64(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #1 {
  %6 = alloca %struct.encctx64, align 8
  %7 = load i64, ptr getelementptr inbounds (i8, ptr @BITS_SIZE_MASK, i64 448), align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1616, ptr nonnull %6) #3
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 0, ptr %8, align 8, !tbaa !9
  %9 = icmp ult i64 %7, %2
  br i1 %9, label %14, label %10

10:                                               ; preds = %5
  %11 = call i32 @encctx_init64(ptr noundef nonnull %6, ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4)
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %12, label %14

12:                                               ; preds = %10
  call void @encode_bit_cluster_tree64(ptr noundef nonnull %6)
  %13 = call i64 @encctx_close64(ptr noundef nonnull %6)
  store i64 %13, ptr %8, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %10, %5, %12
  %.0 = phi i32 [ 0, %12 ], [ -2, %5 ], [ %11, %10 ]
  call void @llvm.lifetime.end.p0(i64 1616, ptr nonnull %6) #3
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @encctx_init64(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @encode_bit_cluster_tree64(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @encctx_close64(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"vtenc", !11, i64 0, !6, i64 16}
!11 = !{!"vtenc_enc_params", !12, i64 0, !12, i64 4, !6, i64 8}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass