; LLVM IR from IlyaGrebnov/libbsc :: libbsc/coder/qlfc (qlfc)

; 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
declare dso_local noundef i32 @_Z24bsc_qlfc_adaptive_encodePKhPhS1_iiP21QlfcStatisticalModel1(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare ptr @bsc_malloc(i64 noundef) #2

declare void @bsc_free(ptr noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local i32 @bsc_qlfc_adaptive_encode_block(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %14 = call ptr @bsc_malloc(i64 noundef 3578964)
  store ptr %14, ptr %10, align 8, !tbaa !5
  %15 = load ptr, ptr %10, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %40

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = mul i64 %19, 1
  %21 = call ptr @bsc_malloc(i64 noundef %20)
  store ptr %21, ptr %11, align 8, !tbaa !5
  %22 = load ptr, ptr %11, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %35

24:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load ptr, ptr %11, align 8, !tbaa !5
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = call noundef i32 @_Z24bsc_qlfc_adaptive_encodePKhPhS1_iiP21QlfcStatisticalModel1(ptr noundef %25, ptr noundef %26, ptr noundef %27, i32 noundef %28, i32 noundef %29, ptr noundef %30)
  store i32 %31, ptr %12, align 4, !tbaa !9
  %32 = load ptr, ptr %11, align 8, !tbaa !5
  call void @bsc_free(ptr noundef %32)
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bsc_free(ptr noundef %33)
  %34 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %34, ptr %5, align 4
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %36

35:                                               ; preds = %17
  store i32 0, ptr %13, align 4
  br label %36

36:                                               ; preds = %35, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  %37 = load i32, ptr %13, align 4
  switch i32 %37, label %41 [
    i32 0, label %38
  ]

38:                                               ; preds = %36
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  call void @bsc_free(ptr noundef %39)
  br label %40

40:                                               ; preds = %38, %4
  store i32 0, ptr %13, align 4
  br label %41

41:                                               ; preds = %40, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %42 = load i32, ptr %13, align 4
  switch i32 %42, label %46 [
    i32 0, label %43
    i32 1, label %44
  ]

43:                                               ; preds = %41
  store i32 -2, ptr %5, align 4
  br label %44

44:                                               ; preds = %43, %41
  %45 = load i32, ptr %5, align 4
  ret i32 %45

46:                                               ; preds = %41
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress uwtable "min-legal-vector-width"="128" "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 = { "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 = { 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 #4 = { 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 = !{!"int", !7, 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"

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z24bsc_qlfc_adaptive_encodePKhPhS1_iiP21QlfcStatisticalModel1(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

declare ptr @bsc_malloc(i64 noundef) local_unnamed_addr #1

declare void @bsc_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @bsc_qlfc_adaptive_encode_block(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #2 {
  %5 = tail call ptr @bsc_malloc(i64 noundef 3578964)
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %11, label %6

6:                                                ; preds = %4
  %7 = sext i32 %2 to i64
  %8 = tail call ptr @bsc_malloc(i64 noundef %7)
  %.not19 = icmp eq ptr %8, null
  br i1 %.not19, label %.sink.split, label %9

9:                                                ; preds = %6
  %10 = tail call noundef i32 @_Z24bsc_qlfc_adaptive_encodePKhPhS1_iiP21QlfcStatisticalModel1(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %8, i32 noundef %2, i32 noundef %3, ptr noundef nonnull %5)
  tail call void @bsc_free(ptr noundef nonnull %8)
  br label %.sink.split

.sink.split:                                      ; preds = %6, %9
  %.ph = phi i32 [ %10, %9 ], [ -2, %6 ]
  tail call void @bsc_free(ptr noundef nonnull %5)
  br label %11

11:                                               ; preds = %.sink.split, %4
  %12 = phi i32 [ -2, %4 ], [ %.ph, %.sink.split ]
  ret i32 %12
}

attributes #0 = { mustprogress uwtable "min-legal-vector-width"="128" "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 = { "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 = { 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)"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass