; LLVM IR from catid/Zpng :: zstd (zstd_compress)

; 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.ZSTD_inBuffer_s = type { ptr, i64, i64 }
%struct.ZSTD_outBuffer_s = type { ptr, i64, i64 }
%struct.ZSTD_CCtx_s = type { i32, i32, i32, %struct.ZSTD_CCtx_params_s, %struct.ZSTD_CCtx_params_s, i32, ptr, i64, i64, i64, i64, i64, %struct.XXH64_state_s, %struct.ZSTD_customMem, i64, %struct.seqStore_t, %struct.ldmState_t, ptr, i64, %struct.rawSeqStore_t, %struct.ZSTD_blockState_t, ptr, ptr, i64, i64, i64, i64, ptr, i64, i64, i64, i32, i32, ptr, ptr, %struct.ZSTD_prefixDict_s }
%struct.ZSTD_CCtx_params_s = type { i32, %struct.ZSTD_compressionParameters, %struct.ZSTD_frameParameters, i32, i32, i32, i32, i32, i32, %struct.ldmParams_t, %struct.ZSTD_customMem }
%struct.ZSTD_compressionParameters = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.ZSTD_frameParameters = type { i32, i32, i32 }
%struct.ldmParams_t = type { i32, i32, i32, i32, i32, i32 }
%struct.XXH64_state_s = type { i64, i64, i64, i64, i64, [4 x i64], i32, [2 x i32] }
%struct.ZSTD_customMem = type { ptr, ptr, ptr }
%struct.seqStore_t = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32 }
%struct.ldmState_t = type { %struct.ZSTD_window_t, ptr, ptr, i64 }
%struct.ZSTD_window_t = type { ptr, ptr, ptr, i32, i32 }
%struct.rawSeqStore_t = type { ptr, i64, i64, i64 }
%struct.ZSTD_blockState_t = type { ptr, ptr, %struct.ZSTD_matchState_t }
%struct.ZSTD_matchState_t = type { %struct.ZSTD_window_t, i32, i32, i32, i32, ptr, ptr, ptr, %struct.optState_t }
%struct.optState_t = type { ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.ZSTD_prefixDict_s = type { ptr, i64, i32 }

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ERR_isError(i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @ZSTD_compressStream_generic(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_endStream(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.ZSTD_inBuffer_s, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  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 24, ptr %6) #3
  call void @llvm.memset.p0.i64(ptr align 8 %6, i8 0, i64 24, i1 false)
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ZSTD_outBuffer_s, ptr %12, i32 0, i32 2
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.ZSTD_outBuffer_s, ptr %15, i32 0, i32 1
  %17 = load i64, ptr %16, align 8, !tbaa !12
  %18 = icmp ugt i64 %14, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %2
  store i64 -1, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %67

20:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i64 @ZSTD_compressStream_generic(ptr noundef %21, ptr noundef %22, ptr noundef %6, i32 noundef 2)
  store i64 %23, ptr %8, align 8, !tbaa !13
  %24 = load i64, ptr %8, align 8, !tbaa !13
  %25 = call i32 @ERR_isError(i64 noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %29

27:                                               ; preds = %20
  %28 = load i64, ptr %8, align 8, !tbaa !13
  store i64 %28, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %30

29:                                               ; preds = %20
  store i32 0, ptr %7, align 4
  br label %30

30:                                               ; preds = %29, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %31 = load i32, ptr %7, align 4
  switch i32 %31, label %67 [
    i32 0, label %32
  ]

32:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ZSTD_CCtx_s, ptr %33, i32 0, i32 32
  %35 = load i32, ptr %34, align 4, !tbaa !14
  %36 = icmp ne i32 %35, 0
  %37 = zext i1 %36 to i64
  %38 = select i1 %36, i32 0, i32 3
  %39 = sext i32 %38 to i64
  store i64 %39, ptr %9, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ZSTD_CCtx_s, ptr %40, i32 0, i32 32
  %42 = load i32, ptr %41, align 4, !tbaa !14
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %32
  br label %52

45:                                               ; preds = %32
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.ZSTD_CCtx_s, ptr %46, i32 0, i32 4
  %48 = getelementptr inbounds %struct.ZSTD_CCtx_params_s, ptr %47, i32 0, i32 2
  %49 = getelementptr inbounds %struct.ZSTD_frameParameters, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !32
  %51 = mul i32 %50, 4
  br label %52

52:                                               ; preds = %45, %44
  %53 = phi i32 [ 0, %44 ], [ %51, %45 ]
  %54 = zext i32 %53 to i64
  store i64 %54, ptr %10, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ZSTD_CCtx_s, ptr %55, i32 0, i32 29
  %57 = load i64, ptr %56, align 8, !tbaa !33
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.ZSTD_CCtx_s, ptr %58, i32 0, i32 30
  %60 = load i64, ptr %59, align 8, !tbaa !34
  %61 = sub i64 %57, %60
  %62 = load i64, ptr %9, align 8, !tbaa !13
  %63 = add i64 %61, %62
  %64 = load i64, ptr %10, align 8, !tbaa !13
  %65 = add i64 %63, %64
  store i64 %65, ptr %11, align 8, !tbaa !13
  %66 = load i64, ptr %11, align 8, !tbaa !13
  store i64 %66, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %67

67:                                               ; preds = %52, %30, %19
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #3
  %68 = load i64, ptr %3, align 8
  ret i64 %68
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !11, i64 16}
!10 = !{!"ZSTD_outBuffer_s", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !16, i64 860}
!15 = !{!"ZSTD_CCtx_s", !16, i64 0, !16, i64 4, !16, i64 8, !17, i64 16, !17, i64 136, !16, i64 256, !6, i64 264, !11, i64 272, !11, i64 280, !22, i64 288, !22, i64 296, !22, i64 304, !23, i64 312, !21, i64 400, !11, i64 424, !24, i64 432, !25, i64 496, !6, i64 552, !11, i64 560, !27, i64 568, !28, i64 600, !6, i64 776, !6, i64 784, !11, i64 792, !11, i64 800, !11, i64 808, !11, i64 816, !6, i64 824, !11, i64 832, !11, i64 840, !11, i64 848, !16, i64 856, !16, i64 860, !6, i64 864, !6, i64 872, !31, i64 880}
!16 = !{!"int", !7, i64 0}
!17 = !{!"ZSTD_CCtx_params_s", !16, i64 0, !18, i64 4, !19, i64 32, !16, i64 44, !16, i64 48, !16, i64 52, !16, i64 56, !16, i64 60, !16, i64 64, !20, i64 68, !21, i64 96}
!18 = !{!"", !16, i64 0, !16, i64 4, !16, i64 8, !16, i64 12, !16, i64 16, !16, i64 20, !16, i64 24}
!19 = !{!"", !16, i64 0, !16, i64 4, !16, i64 8}
!20 = !{!"", !16, i64 0, !16, i64 4, !16, i64 8, !16, i64 12, !16, i64 16, !16, i64 20}
!21 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!22 = !{!"long long", !7, i64 0}
!23 = !{!"XXH64_state_s", !22, i64 0, !22, i64 8, !22, i64 16, !22, i64 24, !22, i64 32, !7, i64 40, !16, i64 72, !7, i64 76}
!24 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !16, i64 56, !16, i64 60}
!25 = !{!"", !26, i64 0, !6, i64 32, !6, i64 40, !11, i64 48}
!26 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !16, i64 24, !16, i64 28}
!27 = !{!"", !6, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!28 = !{!"", !6, i64 0, !6, i64 8, !29, i64 16}
!29 = !{!"", !26, i64 0, !16, i64 32, !16, i64 36, !16, i64 40, !16, i64 44, !6, i64 48, !6, i64 56, !6, i64 64, !30, i64 72}
!30 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !16, i64 48, !16, i64 52, !16, i64 56, !16, i64 60, !16, i64 64, !16, i64 68, !16, i64 72, !16, i64 76, !16, i64 80}
!31 = !{!"ZSTD_prefixDict_s", !6, i64 0, !11, i64 8, !16, i64 16}
!32 = !{!15, !16, i64 172}
!33 = !{!15, !11, i64 840}
!34 = !{!15, !11, i64 848}


; 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.ZSTD_inBuffer_s = type { ptr, i64, i64 }

; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ERR_isError(i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @ZSTD_compressStream_generic(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_endStream(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.ZSTD_inBuffer_s, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %3, i8 0, i64 24, i1 false)
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !11
  %8 = icmp ugt i64 %5, %7
  br i1 %8, label %30, label %9

9:                                                ; preds = %2
  %10 = call i64 @ZSTD_compressStream_generic(ptr noundef %0, ptr noundef nonnull %1, ptr noundef nonnull %3, i32 noundef 2)
  %11 = call i32 @ERR_isError(i64 noundef %10)
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %12, label %30

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %0, i64 860
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %.not16 = icmp eq i32 %14, 0
  %15 = select i1 %.not16, i64 3, i64 0
  br i1 %.not16, label %16, label %21

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %0, i64 172
  %18 = load i32, ptr %17, align 4, !tbaa !30
  %19 = shl i32 %18, 2
  %20 = zext i32 %19 to i64
  br label %21

21:                                               ; preds = %12, %16
  %22 = phi i64 [ %20, %16 ], [ 0, %12 ]
  %23 = getelementptr inbounds i8, ptr %0, i64 840
  %24 = load i64, ptr %23, align 8, !tbaa !31
  %25 = getelementptr inbounds i8, ptr %0, i64 848
  %26 = load i64, ptr %25, align 8, !tbaa !32
  %27 = add nuw nsw i64 %22, %15
  %28 = add i64 %27, %24
  %29 = sub i64 %28, %26
  br label %30

30:                                               ; preds = %2, %9, %21
  %.1 = phi i64 [ %29, %21 ], [ %10, %9 ], [ -1, %2 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #3
  ret i64 %.1
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !10, i64 16}
!6 = !{!"ZSTD_outBuffer_s", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !14, i64 860}
!13 = !{!"ZSTD_CCtx_s", !14, i64 0, !14, i64 4, !14, i64 8, !15, i64 16, !15, i64 136, !14, i64 256, !7, i64 264, !10, i64 272, !10, i64 280, !20, i64 288, !20, i64 296, !20, i64 304, !21, i64 312, !19, i64 400, !10, i64 424, !22, i64 432, !23, i64 496, !7, i64 552, !10, i64 560, !25, i64 568, !26, i64 600, !7, i64 776, !7, i64 784, !10, i64 792, !10, i64 800, !10, i64 808, !10, i64 816, !7, i64 824, !10, i64 832, !10, i64 840, !10, i64 848, !14, i64 856, !14, i64 860, !7, i64 864, !7, i64 872, !29, i64 880}
!14 = !{!"int", !8, i64 0}
!15 = !{!"ZSTD_CCtx_params_s", !14, i64 0, !16, i64 4, !17, i64 32, !14, i64 44, !14, i64 48, !14, i64 52, !14, i64 56, !14, i64 60, !14, i64 64, !18, i64 68, !19, i64 96}
!16 = !{!"", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20, !14, i64 24}
!17 = !{!"", !14, i64 0, !14, i64 4, !14, i64 8}
!18 = !{!"", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20}
!19 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16}
!20 = !{!"long long", !8, i64 0}
!21 = !{!"XXH64_state_s", !20, i64 0, !20, i64 8, !20, i64 16, !20, i64 24, !20, i64 32, !8, i64 40, !14, i64 72, !8, i64 76}
!22 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !14, i64 56, !14, i64 60}
!23 = !{!"", !24, i64 0, !7, i64 32, !7, i64 40, !10, i64 48}
!24 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !14, i64 24, !14, i64 28}
!25 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!26 = !{!"", !7, i64 0, !7, i64 8, !27, i64 16}
!27 = !{!"", !24, i64 0, !14, i64 32, !14, i64 36, !14, i64 40, !14, i64 44, !7, i64 48, !7, i64 56, !7, i64 64, !28, i64 72}
!28 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !14, i64 48, !14, i64 52, !14, i64 56, !14, i64 60, !14, i64 64, !14, i64 68, !14, i64 72, !14, i64 76, !14, i64 80}
!29 = !{!"ZSTD_prefixDict_s", !7, i64 0, !10, i64 8, !14, i64 16}
!30 = !{!13, !14, i64 172}
!31 = !{!13, !10, i64 840}
!32 = !{!13, !10, i64 848}


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