; LLVM IR from itchio/go-brotli :: dec (decode)

; 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.BrotliDecoderStateStruct = type { i32, i32, %struct.BrotliBitReader, ptr, ptr, ptr, %union.anon, i32, i32, i32, i32, i32, i32, i32, [4 x i32], i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.HuffmanTreeGroup, %struct.HuffmanTreeGroup, %struct.HuffmanTreeGroup, ptr, ptr, i32, i32, i32, i32, [3 x i32], [3 x i32], [6 x i32], i32, i32, i32, i32, ptr, ptr, i8, i32, i32, i32, i32, i64, i64, i32, i32, i32, [32 x %struct.HuffmanCode], ptr, [720 x i16], [32 x i32], [18 x i8], [16 x i16], i32, ptr, i32, i32, i32, [646 x %struct.HuffmanCode], i32, [65 x i32], i32, i32, i32, i32, i32, i32, i32, i16, i32, i32, i32, ptr, ptr, ptr, [8 x i32] }
%struct.BrotliBitReader = type { i64, i32, ptr, i64 }
%union.anon = type { i64 }
%struct.HuffmanTreeGroup = type { ptr, ptr, i16, i16 }
%struct.HuffmanCode = type { i8, i16 }

; 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: noinline nounwind uwtable
declare hidden i32 @SaveErrorCode(ptr noundef, i32 noundef) #1

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

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

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

14:                                               ; preds = %2
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %15, align 8, !tbaa !9
  br label %18

17:                                               ; preds = %2
  br label %18

18:                                               ; preds = %17, %14
  %19 = phi i64 [ %16, %14 ], [ 16777216, %17 ]
  store i64 %19, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %20 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %20, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BrotliDecoderStateStruct, ptr %21, i32 0, i32 17
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = icmp eq ptr %23, null
  br i1 %24, label %30, label %25

25:                                               ; preds = %18
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.BrotliDecoderStateStruct, ptr %26, i32 0, i32 15
  %28 = load i32, ptr %27, align 4, !tbaa !17
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %32

30:                                               ; preds = %25, %18
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 0, ptr %31, align 8, !tbaa !9
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %57

32:                                               ; preds = %25
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  call void @WrapRingBuffer(ptr noundef %33)
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = call i32 @WriteRingBuffer(ptr noundef %34, ptr noundef %7, ptr noundef %6, ptr noundef null, i32 noundef 1)
  store i32 %35, ptr %9, align 4, !tbaa !18
  %36 = load i32, ptr %9, align 4, !tbaa !18
  %37 = icmp eq i32 %36, 1
  br i1 %37, label %41, label %38

38:                                               ; preds = %32
  %39 = load i32, ptr %9, align 4, !tbaa !18
  %40 = icmp eq i32 %39, 3
  br i1 %40, label %41, label %46

41:                                               ; preds = %38, %32
  %42 = load i64, ptr %8, align 8, !tbaa !9
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = sub i64 %42, %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %44, ptr %45, align 8, !tbaa !9
  br label %55

46:                                               ; preds = %38
  %47 = load i32, ptr %9, align 4, !tbaa !18
  %48 = icmp slt i32 %47, 0
  br i1 %48, label %49, label %53

49:                                               ; preds = %46
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load i32, ptr %9, align 4, !tbaa !18
  %52 = call i32 @SaveErrorCode(ptr noundef %50, i32 noundef %51)
  br label %53

53:                                               ; preds = %49, %46
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 0, ptr %54, align 8, !tbaa !9
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %53, %41
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %56, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %57

57:                                               ; preds = %55, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %58 = load ptr, ptr %3, align 8
  ret ptr %58
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noinline 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 128}
!12 = !{!"BrotliDecoderStateStruct", !13, i64 0, !13, i64 4, !14, i64 8, !6, i64 40, !6, i64 48, !6, i64 56, !7, i64 64, !13, i64 72, !13, i64 76, !13, i64 80, !13, i64 84, !13, i64 88, !13, i64 92, !13, i64 96, !7, i64 100, !13, i64 116, !13, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !15, i64 184, !15, i64 208, !15, i64 232, !6, i64 256, !6, i64 264, !13, i64 272, !13, i64 276, !13, i64 280, !13, i64 284, !7, i64 288, !7, i64 300, !7, i64 312, !13, i64 336, !13, i64 340, !13, i64 344, !13, i64 348, !6, i64 352, !6, i64 360, !7, i64 368, !13, i64 372, !13, i64 376, !13, i64 380, !13, i64 384, !10, i64 392, !10, i64 400, !13, i64 408, !13, i64 412, !13, i64 416, !7, i64 420, !6, i64 552, !7, i64 560, !7, i64 2000, !7, i64 2128, !7, i64 2146, !13, i64 2180, !6, i64 2184, !13, i64 2192, !13, i64 2196, !13, i64 2200, !7, i64 2204, !13, i64 4788, !7, i64 4792, !13, i64 5052, !13, i64 5056, !13, i64 5060, !13, i64 5064, !13, i64 5068, !13, i64 5072, !13, i64 5076, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5084, !13, i64 5088, !13, i64 5092, !6, i64 5096, !6, i64 5104, !6, i64 5112, !7, i64 5120}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !10, i64 0, !13, i64 8, !6, i64 16, !10, i64 24}
!15 = !{!"", !6, i64 0, !6, i64 8, !16, i64 16, !16, i64 18}
!16 = !{!"short", !7, i64 0}
!17 = !{!12, !13, i64 116}
!18 = !{!13, !13, 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 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: noinline nounwind uwtable
declare hidden i32 @SaveErrorCode(ptr noundef, i32 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @BrotliDecoderTakeOutput(ptr noundef %0, ptr nocapture noundef %1) local_unnamed_addr #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  store ptr null, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %5 = load i64, ptr %1, align 8, !tbaa !9
  %.not = icmp eq i64 %5, 0
  %spec.select = select i1 %.not, i64 16777216, i64 %5
  store i64 %spec.select, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %0, i64 128
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = icmp eq ptr %7, null
  br i1 %8, label %13, label %9

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 116
  %11 = load i32, ptr %10, align 4, !tbaa !17
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %9, %2
  store i64 0, ptr %1, align 8, !tbaa !9
  br label %25

14:                                               ; preds = %9
  tail call void @WrapRingBuffer(ptr noundef nonnull %0)
  %15 = call i32 @WriteRingBuffer(ptr noundef nonnull %0, ptr noundef nonnull %4, ptr noundef nonnull %3, ptr noundef null, i32 noundef 1)
  %16 = and i32 %15, -3
  %or.cond = icmp eq i32 %16, 1
  br i1 %or.cond, label %17, label %20

17:                                               ; preds = %14
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = sub i64 %spec.select, %18
  store i64 %19, ptr %1, align 8, !tbaa !9
  %.pre = load ptr, ptr %3, align 8, !tbaa !5
  br label %25

20:                                               ; preds = %14
  %21 = icmp slt i32 %15, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %20
  %23 = call i32 @SaveErrorCode(ptr noundef nonnull %0, i32 noundef %15)
  br label %24

24:                                               ; preds = %22, %20
  store i64 0, ptr %1, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %17, %24, %13
  %.0 = phi ptr [ null, %13 ], [ null, %24 ], [ %.pre, %17 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret ptr %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noinline 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 128}
!12 = !{!"BrotliDecoderStateStruct", !13, i64 0, !13, i64 4, !14, i64 8, !6, i64 40, !6, i64 48, !6, i64 56, !7, i64 64, !13, i64 72, !13, i64 76, !13, i64 80, !13, i64 84, !13, i64 88, !13, i64 92, !13, i64 96, !7, i64 100, !13, i64 116, !13, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !15, i64 184, !15, i64 208, !15, i64 232, !6, i64 256, !6, i64 264, !13, i64 272, !13, i64 276, !13, i64 280, !13, i64 284, !7, i64 288, !7, i64 300, !7, i64 312, !13, i64 336, !13, i64 340, !13, i64 344, !13, i64 348, !6, i64 352, !6, i64 360, !7, i64 368, !13, i64 372, !13, i64 376, !13, i64 380, !13, i64 384, !10, i64 392, !10, i64 400, !13, i64 408, !13, i64 412, !13, i64 416, !7, i64 420, !6, i64 552, !7, i64 560, !7, i64 2000, !7, i64 2128, !7, i64 2146, !13, i64 2180, !6, i64 2184, !13, i64 2192, !13, i64 2196, !13, i64 2200, !7, i64 2204, !13, i64 4788, !7, i64 4792, !13, i64 5052, !13, i64 5056, !13, i64 5060, !13, i64 5064, !13, i64 5068, !13, i64 5072, !13, i64 5076, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5080, !13, i64 5084, !13, i64 5088, !13, i64 5092, !6, i64 5096, !6, i64 5104, !6, i64 5112, !7, i64 5120}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !10, i64 0, !13, i64 8, !6, i64 16, !10, i64 24}
!15 = !{!"", !6, i64 0, !6, i64 8, !16, i64 16, !16, i64 18}
!16 = !{!"short", !7, i64 0}
!17 = !{!12, !13, i64 116}


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