; LLVM IR from indygreg/python-zstandard :: zstd (zstd)

; 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_frameSizeInfo = type { i64, i64, 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: nounwind uwtable
declare dso_local i32 @ZSTD_isError(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_decompressBound(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca %struct.ZSTD_frameSizeInfo, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %40, %2
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = icmp ugt i64 %12, 0
  br i1 %13, label %14, label %41

14:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %5, align 8, !tbaa !9
  call void @ZSTD_findFrameSizeInfo(ptr dead_on_unwind writable sret(%struct.ZSTD_frameSizeInfo) align 8 %7, ptr noundef %15, i64 noundef %16, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %17 = getelementptr inbounds %struct.ZSTD_frameSizeInfo, ptr %7, i32 0, i32 1
  %18 = load i64, ptr %17, align 8, !tbaa !13
  store i64 %18, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %19 = getelementptr inbounds %struct.ZSTD_frameSizeInfo, ptr %7, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !15
  store i64 %20, ptr %9, align 8, !tbaa !11
  %21 = load i64, ptr %8, align 8, !tbaa !9
  %22 = call i32 @ZSTD_isError(i64 noundef %21)
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %27, label %24

24:                                               ; preds = %14
  %25 = load i64, ptr %9, align 8, !tbaa !11
  %26 = icmp eq i64 %25, -2
  br i1 %26, label %27, label %28

27:                                               ; preds = %24, %14
  store i64 -2, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %38

28:                                               ; preds = %24
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i64, ptr %8, align 8, !tbaa !9
  %31 = getelementptr inbounds i8, ptr %29, i64 %30
  store ptr %31, ptr %4, align 8, !tbaa !5
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = sub i64 %33, %32
  store i64 %34, ptr %5, align 8, !tbaa !9
  %35 = load i64, ptr %9, align 8, !tbaa !11
  %36 = load i64, ptr %6, align 8, !tbaa !11
  %37 = add i64 %36, %35
  store i64 %37, ptr %6, align 8, !tbaa !11
  store i32 0, ptr %10, align 4
  br label %38

38:                                               ; preds = %28, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #2
  %39 = load i32, ptr %10, align 4
  switch i32 %39, label %43 [
    i32 0, label %40
  ]

40:                                               ; preds = %38
  br label %11, !llvm.loop !16

41:                                               ; preds = %11
  %42 = load i64, ptr %6, align 8, !tbaa !11
  store i64 %42, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %43

43:                                               ; preds = %41, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %44 = load i64, ptr %3, align 8
  ret i64 %44
}

; Function Attrs: nounwind uwtable
declare hidden void @ZSTD_findFrameSizeInfo(ptr dead_on_unwind noalias writable sret(%struct.ZSTD_frameSizeInfo) align 8, ptr noundef, i64 noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { 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, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !10, i64 0, !10, i64 8, !12, i64 16}
!15 = !{!14, !12, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.ZSTD_frameSizeInfo = type { i64, 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: nounwind uwtable
declare dso_local i32 @ZSTD_isError(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_decompressBound(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = alloca %struct.ZSTD_frameSizeInfo, align 8
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = getelementptr inbounds i8, ptr %3, i64 16
  br label %6

6:                                                ; preds = %7, %2
  %.019 = phi i64 [ %1, %2 ], [ %14, %7 ]
  %.017 = phi i64 [ 0, %2 ], [ %15, %7 ]
  %.015 = phi ptr [ %0, %2 ], [ %13, %7 ]
  %.not = icmp eq i64 %.019, 0
  br i1 %.not, label %16, label %7

7:                                                ; preds = %6
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #2
  call void @ZSTD_findFrameSizeInfo(ptr dead_on_unwind nonnull writable sret(%struct.ZSTD_frameSizeInfo) align 8 %3, ptr noundef %.015, i64 noundef %.019, i32 noundef 0)
  %8 = load i64, ptr %4, align 8, !tbaa !5
  %9 = load i64, ptr %5, align 8, !tbaa !11
  %10 = call i32 @ZSTD_isError(i64 noundef %8)
  %11 = icmp eq i32 %10, 0
  %12 = icmp ne i64 %9, -2
  %or.cond.not = select i1 %11, i1 %12, i1 false
  %13 = getelementptr inbounds i8, ptr %.015, i64 %8
  %14 = sub i64 %.019, %8
  %15 = add i64 %9, %.017
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #2
  br i1 %or.cond.not, label %6, label %16, !llvm.loop !12

16:                                               ; preds = %6, %7
  %.2 = phi i64 [ -2, %7 ], [ %.017, %6 ]
  ret i64 %.2
}

; Function Attrs: nounwind uwtable
declare hidden void @ZSTD_findFrameSizeInfo(ptr dead_on_unwind noalias writable sret(%struct.ZSTD_frameSizeInfo) align 8, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { 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, !7, i64 8}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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