; LLVM IR from fhanau/Efficient-Compression-Tool :: src/zopfli (deflate)

; 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: mustprogress uwtable
define dso_local i64 @CalculateTreeSize(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i8 %2, ptr %8, align 1, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  call void @_ZL34PatchDistanceCodesForBuggyDecodersPj(ptr noundef %14)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !10
  %15 = load i8, ptr %8, align 1, !tbaa !9
  %16 = icmp ne i8 %15, 0
  br i1 %16, label %17, label %83

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !12
  br label %18

18:                                               ; preds = %78, %17
  %19 = load i32, ptr %11, align 4, !tbaa !12
  %20 = load i8, ptr %8, align 1, !tbaa !9
  %21 = zext i8 %20 to i32
  %22 = icmp eq i32 %21, 2
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 32, i32 10
  %25 = icmp ult i32 %19, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %81

27:                                               ; preds = %18
  %28 = load i32, ptr %11, align 4, !tbaa !12
  %29 = and i32 %28, 1
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %40, label %31

31:                                               ; preds = %27
  %32 = load i32, ptr %11, align 4, !tbaa !12
  %33 = and i32 %32, 8
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %39, label %35

35:                                               ; preds = %31
  %36 = load i32, ptr %11, align 4, !tbaa !12
  %37 = and i32 %36, 16
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %35, %31
  br label %78

40:                                               ; preds = %35, %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i32, ptr %11, align 4, !tbaa !12
  %44 = and i32 %43, 1
  %45 = load i32, ptr %11, align 4, !tbaa !12
  %46 = and i32 %45, 2
  %47 = load i32, ptr %11, align 4, !tbaa !12
  %48 = and i32 %47, 4
  %49 = load i32, ptr %11, align 4, !tbaa !12
  %50 = and i32 %49, 8
  %51 = load i32, ptr %11, align 4, !tbaa !12
  %52 = and i32 %51, 16
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %63, label %54

54:                                               ; preds = %40
  %55 = load i8, ptr %8, align 1, !tbaa !9
  %56 = zext i8 %55 to i32
  %57 = icmp eq i32 %56, 1
  br i1 %57, label %58, label %61

58:                                               ; preds = %54
  %59 = load i32, ptr %11, align 4, !tbaa !12
  %60 = icmp eq i32 %59, 9
  br label %61

61:                                               ; preds = %58, %54
  %62 = phi i1 [ false, %54 ], [ %60, %58 ]
  br label %63

63:                                               ; preds = %61, %40
  %64 = phi i1 [ true, %40 ], [ %62, %61 ]
  %65 = zext i1 %64 to i32
  %66 = call noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef %41, ptr noundef %42, i32 noundef %44, i32 noundef %46, i32 noundef %48, i32 noundef %50, i32 noundef %65, ptr noundef null, ptr noundef null, ptr noundef null)
  store i64 %66, ptr %12, align 8, !tbaa !10
  %67 = load i64, ptr %10, align 8, !tbaa !10
  %68 = icmp eq i64 %67, 0
  br i1 %68, label %73, label %69

69:                                               ; preds = %63
  %70 = load i64, ptr %12, align 8, !tbaa !10
  %71 = load i64, ptr %10, align 8, !tbaa !10
  %72 = icmp ult i64 %70, %71
  br i1 %72, label %73, label %77

73:                                               ; preds = %69, %63
  %74 = load i64, ptr %12, align 8, !tbaa !10
  store i64 %74, ptr %10, align 8, !tbaa !10
  %75 = load i32, ptr %11, align 4, !tbaa !12
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %75, ptr %76, align 4, !tbaa !12
  br label %77

77:                                               ; preds = %73, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %78

78:                                               ; preds = %77, %39
  %79 = load i32, ptr %11, align 4, !tbaa !12
  %80 = add i32 %79, 1
  store i32 %80, ptr %11, align 4, !tbaa !12
  br label %18, !llvm.loop !14

81:                                               ; preds = %26
  %82 = load i64, ptr %10, align 8, !tbaa !10
  store i64 %82, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %88

83:                                               ; preds = %4
  %84 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 7, ptr %84, align 4, !tbaa !12
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = call noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef %85, ptr noundef %86, i32 noundef 1, i32 noundef 1, i32 noundef 1, i32 noundef 0, i32 noundef 0, ptr noundef null, ptr noundef null, ptr noundef null)
  store i64 %87, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %88

88:                                               ; preds = %83, %81
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %89 = load i64, ptr %5, align 8
  ret i64 %89
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress uwtable
declare hidden noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

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" }
attributes #1 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 i64 @CalculateTreeSize(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  tail call void @_ZL34PatchDistanceCodesForBuggyDecodersPj(ptr noundef %1)
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %27, label %.preheader

.preheader:                                       ; preds = %4
  %5 = icmp eq i8 %2, 2
  %6 = select i1 %5, i32 32, i32 10
  %7 = icmp eq i8 %2, 1
  br label %8

8:                                                ; preds = %.preheader, %25
  %.02737 = phi i32 [ 0, %.preheader ], [ %26, %25 ]
  %.02836 = phi i64 [ 0, %.preheader ], [ %.2, %25 ]
  %9 = and i32 %.02737, 1
  %.not30 = icmp ne i32 %9, 0
  %10 = and i32 %.02737, 24
  %or.cond = icmp eq i32 %10, 0
  %or.cond35 = or i1 %.not30, %or.cond
  br i1 %or.cond35, label %11, label %25

11:                                               ; preds = %8
  %12 = and i32 %.02737, 2
  %13 = and i32 %.02737, 4
  %14 = and i32 %.02737, 8
  %15 = and i32 %.02737, 16
  %.not33 = icmp eq i32 %15, 0
  br i1 %.not33, label %16, label %19

16:                                               ; preds = %11
  %17 = icmp eq i32 %.02737, 9
  %18 = and i1 %7, %17
  br label %19

19:                                               ; preds = %16, %11
  %20 = phi i1 [ true, %11 ], [ %18, %16 ]
  %21 = zext i1 %20 to i32
  %22 = tail call noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef %0, ptr noundef %1, i32 noundef %9, i32 noundef %12, i32 noundef %13, i32 noundef %14, i32 noundef %21, ptr noundef null, ptr noundef null, ptr noundef null)
  %23 = add i64 %.02836, -1
  %or.cond34.not = icmp ult i64 %23, %22
  br i1 %or.cond34.not, label %25, label %24

24:                                               ; preds = %19
  store i32 %.02737, ptr %3, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %8, %24, %19
  %.2 = phi i64 [ %22, %24 ], [ %.02836, %19 ], [ %.02836, %8 ]
  %26 = add nuw nsw i32 %.02737, 1
  %exitcond.not = icmp eq i32 %26, %6
  br i1 %exitcond.not, label %.loopexit, label %8, !llvm.loop !9

27:                                               ; preds = %4
  store i32 7, ptr %3, align 4, !tbaa !5
  %28 = tail call noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef %0, ptr noundef %1, i32 noundef 1, i32 noundef 1, i32 noundef 1, i32 noundef 0, i32 noundef 0, ptr noundef null, ptr noundef null, ptr noundef null)
  br label %.loopexit

.loopexit:                                        ; preds = %25, %27
  %.0 = phi i64 [ %28, %27 ], [ %.2, %25 ]
  ret i64 %.0
}

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

; Function Attrs: mustprogress uwtable
declare hidden noundef i64 @_ZL10EncodeTreePKjS0_iiiiiPhS1_Pm(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass