; LLVM IR from itchio/go-brotli :: enc (encode)

; 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.BrotliEncoderStateStruct = type { %struct.BrotliEncoderParams, %struct.MemoryManager, ptr, i64, %struct.RingBuffer, i64, ptr, i64, i64, i64, i64, i64, [16 x i32], [4 x i32], i8, i8, i8, i8, i64, ptr, [1024 x i32], ptr, i64, [128 x i8], [128 x i16], [512 x i8], i64, ptr, ptr, ptr, i64, i64, %union.anon, i32, i32, i32, i32 }
%struct.BrotliEncoderParams = type { i32, i32, i32, i32, i64, i32, %struct.BrotliHasherParams }
%struct.BrotliHasherParams = type { i32, i32, i32, i32, i32 }
%struct.MemoryManager = type { ptr, ptr, ptr }
%struct.RingBuffer = type { i32, i32, i32, i32, i32, i32, ptr, ptr }
%union.anon = type { [2 x i64] }

; 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: alwaysinline nounwind uwtable
declare hidden i64 @brotli_min_size_t(i64 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @CheckFlushComplete(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @BrotliEncoderTakeOutput(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %7, i32 0, i32 30
  %9 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %9, ptr %5, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %10, i32 0, i32 29
  %12 = load ptr, ptr %11, align 8, !tbaa !18
  store ptr %12, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i64, ptr %13, align 8, !tbaa !17
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %23

16:                                               ; preds = %2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %19, i32 0, i32 30
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = call i64 @brotli_min_size_t(i64 noundef %18, i64 noundef %21)
  store i64 %22, ptr %5, align 8, !tbaa !17
  br label %23

23:                                               ; preds = %16, %2
  %24 = load i64, ptr %5, align 8, !tbaa !17
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %45

26:                                               ; preds = %23
  %27 = load i64, ptr %5, align 8, !tbaa !17
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %28, i32 0, i32 29
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  %31 = getelementptr inbounds i8, ptr %30, i64 %27
  store ptr %31, ptr %29, align 8, !tbaa !18
  %32 = load i64, ptr %5, align 8, !tbaa !17
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %33, i32 0, i32 30
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = sub i64 %35, %32
  store i64 %36, ptr %34, align 8, !tbaa !9
  %37 = load i64, ptr %5, align 8, !tbaa !17
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.BrotliEncoderStateStruct, ptr %38, i32 0, i32 31
  %40 = load i64, ptr %39, align 8, !tbaa !19
  %41 = add i64 %40, %37
  store i64 %41, ptr %39, align 8, !tbaa !19
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  call void @CheckFlushComplete(ptr noundef %42)
  %43 = load i64, ptr %5, align 8, !tbaa !17
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %43, ptr %44, align 8, !tbaa !17
  br label %47

45:                                               ; preds = %23
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 0, ptr %46, align 8, !tbaa !17
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %45, %26
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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, !13, i64 5328}
!10 = !{!"BrotliEncoderStateStruct", !11, i64 0, !15, i64 48, !6, i64 72, !13, i64 80, !16, i64 88, !13, i64 128, !6, i64 136, !13, i64 144, !13, i64 152, !13, i64 160, !13, i64 168, !13, i64 176, !7, i64 184, !7, i64 248, !7, i64 264, !7, i64 265, !7, i64 266, !7, i64 267, !13, i64 272, !6, i64 280, !7, i64 288, !6, i64 4384, !13, i64 4392, !7, i64 4400, !7, i64 4528, !7, i64 4784, !13, i64 5296, !6, i64 5304, !6, i64 5312, !6, i64 5320, !13, i64 5328, !13, i64 5336, !7, i64 5344, !12, i64 5360, !12, i64 5364, !12, i64 5368, !12, i64 5372}
!11 = !{!"BrotliEncoderParams", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !13, i64 16, !12, i64 24, !14, i64 28}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"BrotliHasherParams", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16}
!15 = !{!"MemoryManager", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!"RingBuffer", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !6, i64 24, !6, i64 32}
!17 = !{!13, !13, i64 0}
!18 = !{!10, !6, i64 5320}
!19 = !{!10, !13, i64 5336}


; 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: alwaysinline nounwind uwtable
declare hidden i64 @brotli_min_size_t(i64 noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @BrotliEncoderTakeOutput(ptr noundef %0, ptr nocapture noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 5328
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 5320
  %6 = load ptr, ptr %5, align 8, !tbaa !16
  %7 = load i64, ptr %1, align 8, !tbaa !17
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %10, label %8

8:                                                ; preds = %2
  %9 = tail call i64 @brotli_min_size_t(i64 noundef %7, i64 noundef %4)
  br label %10

10:                                               ; preds = %8, %2
  %.016 = phi i64 [ %9, %8 ], [ %4, %2 ]
  %.not18 = icmp eq i64 %.016, 0
  br i1 %.not18, label %19, label %11

11:                                               ; preds = %10
  %12 = load ptr, ptr %5, align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %12, i64 %.016
  store ptr %13, ptr %5, align 8, !tbaa !16
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = sub i64 %14, %.016
  store i64 %15, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %0, i64 5336
  %17 = load i64, ptr %16, align 8, !tbaa !18
  %18 = add i64 %17, %.016
  store i64 %18, ptr %16, align 8, !tbaa !18
  tail call void @CheckFlushComplete(ptr noundef nonnull %0)
  br label %19

19:                                               ; preds = %10, %11
  %.0 = phi ptr [ %6, %11 ], [ null, %10 ]
  store i64 %.016, ptr %1, align 8, !tbaa !17
  ret ptr %.0
}

attributes #0 = { alwaysinline 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 #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" }

!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, !11, i64 5328}
!6 = !{!"BrotliEncoderStateStruct", !7, i64 0, !13, i64 48, !14, i64 72, !11, i64 80, !15, i64 88, !11, i64 128, !14, i64 136, !11, i64 144, !11, i64 152, !11, i64 160, !11, i64 168, !11, i64 176, !9, i64 184, !9, i64 248, !9, i64 264, !9, i64 265, !9, i64 266, !9, i64 267, !11, i64 272, !14, i64 280, !9, i64 288, !14, i64 4384, !11, i64 4392, !9, i64 4400, !9, i64 4528, !9, i64 4784, !11, i64 5296, !14, i64 5304, !14, i64 5312, !14, i64 5320, !11, i64 5328, !11, i64 5336, !9, i64 5344, !8, i64 5360, !8, i64 5364, !8, i64 5368, !8, i64 5372}
!7 = !{!"BrotliEncoderParams", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !11, i64 16, !8, i64 24, !12, i64 28}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"BrotliHasherParams", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !8, i64 16}
!13 = !{!"MemoryManager", !14, i64 0, !14, i64 8, !14, i64 16}
!14 = !{!"any pointer", !9, i64 0}
!15 = !{!"RingBuffer", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !8, i64 16, !8, i64 20, !14, i64 24, !14, i64 32}
!16 = !{!6, !14, i64 5320}
!17 = !{!11, !11, i64 0}
!18 = !{!6, !11, i64 5336}


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