; 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 }
%struct.ZSTD_frameHeader = type { i64, i64, i32, i32, i32, i32, i32, i32, i32 }

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @ZSTD_isError(i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @ZSTD_getFrameHeader(ptr noundef, ptr noundef, i64 noundef) #0

; 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) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_decompressionMargin(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.ZSTD_frameSizeInfo, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca %struct.ZSTD_frameHeader, align 8
  %12 = alloca i64, align 8
  %13 = 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 !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %105, %2
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = icmp ugt i64 %15, 0
  br i1 %16, label %17, label %106

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

24:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = call i64 @ZSTD_getFrameHeader(ptr noundef %11, ptr noundef %25, i64 noundef %26)
  store i64 %27, ptr %12, align 8, !tbaa !9
  %28 = load i64, ptr %12, align 8, !tbaa !9
  %29 = call i32 @ERR_isError(i64 noundef %28)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %45

31:                                               ; preds = %24
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  br label %34

34:                                               ; preds = %33
  br label %35

35:                                               ; preds = %34
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36
  br label %38

38:                                               ; preds = %37
  br label %39

39:                                               ; preds = %38
  br label %40

40:                                               ; preds = %39
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  %44 = load i64, ptr %12, align 8, !tbaa !9
  store i64 %44, ptr %3, align 8
  store i32 1, ptr %13, align 4
  br label %46

45:                                               ; preds = %24
  store i32 0, ptr %13, align 4
  br label %46

46:                                               ; preds = %45, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  %47 = load i32, ptr %13, align 4
  switch i32 %47, label %103 [
    i32 0, label %48
  ]

48:                                               ; preds = %46
  br label %49

49:                                               ; preds = %48
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %9, align 8, !tbaa !9
  %52 = call i32 @ZSTD_isError(i64 noundef %51)
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %57, label %54

54:                                               ; preds = %50
  %55 = load i64, ptr %10, align 8, !tbaa !17
  %56 = icmp eq i64 %55, -2
  br i1 %56, label %57, label %58

57:                                               ; preds = %54, %50
  store i64 -20, ptr %3, align 8
  store i32 1, ptr %13, align 4
  br label %103

58:                                               ; preds = %54
  %59 = getelementptr inbounds %struct.ZSTD_frameHeader, ptr %11, i32 0, i32 3
  %60 = load i32, ptr %59, align 4, !tbaa !18
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %92

62:                                               ; preds = %58
  %63 = getelementptr inbounds %struct.ZSTD_frameHeader, ptr %11, i32 0, i32 4
  %64 = load i32, ptr %63, align 8, !tbaa !20
  %65 = zext i32 %64 to i64
  %66 = load i64, ptr %6, align 8, !tbaa !9
  %67 = add i64 %66, %65
  store i64 %67, ptr %6, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.ZSTD_frameHeader, ptr %11, i32 0, i32 6
  %69 = load i32, ptr %68, align 8, !tbaa !21
  %70 = icmp ne i32 %69, 0
  %71 = zext i1 %70 to i64
  %72 = select i1 %70, i32 4, i32 0
  %73 = sext i32 %72 to i64
  %74 = load i64, ptr %6, align 8, !tbaa !9
  %75 = add i64 %74, %73
  store i64 %75, ptr %6, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.ZSTD_frameSizeInfo, ptr %8, i32 0, i32 0
  %77 = load i64, ptr %76, align 8, !tbaa !22
  %78 = mul i64 3, %77
  %79 = load i64, ptr %6, align 8, !tbaa !9
  %80 = add i64 %79, %78
  store i64 %80, ptr %6, align 8, !tbaa !9
  %81 = load i32, ptr %7, align 4, !tbaa !11
  %82 = getelementptr inbounds %struct.ZSTD_frameHeader, ptr %11, i32 0, i32 2
  %83 = load i32, ptr %82, align 8, !tbaa !23
  %84 = icmp ugt i32 %81, %83
  br i1 %84, label %85, label %87

85:                                               ; preds = %62
  %86 = load i32, ptr %7, align 4, !tbaa !11
  br label %90

87:                                               ; preds = %62
  %88 = getelementptr inbounds %struct.ZSTD_frameHeader, ptr %11, i32 0, i32 2
  %89 = load i32, ptr %88, align 8, !tbaa !23
  br label %90

90:                                               ; preds = %87, %85
  %91 = phi i32 [ %86, %85 ], [ %89, %87 ]
  store i32 %91, ptr %7, align 4, !tbaa !11
  br label %96

92:                                               ; preds = %58
  %93 = load i64, ptr %9, align 8, !tbaa !9
  %94 = load i64, ptr %6, align 8, !tbaa !9
  %95 = add i64 %94, %93
  store i64 %95, ptr %6, align 8, !tbaa !9
  br label %96

96:                                               ; preds = %92, %90
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  %98 = load i64, ptr %9, align 8, !tbaa !9
  %99 = getelementptr inbounds i8, ptr %97, i64 %98
  store ptr %99, ptr %4, align 8, !tbaa !5
  %100 = load i64, ptr %9, align 8, !tbaa !9
  %101 = load i64, ptr %5, align 8, !tbaa !9
  %102 = sub i64 %101, %100
  store i64 %102, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %13, align 4
  br label %103

103:                                              ; preds = %96, %57, %46
  call void @llvm.lifetime.end.p0(i64 48, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %8) #2
  %104 = load i32, ptr %13, align 4
  switch i32 %104, label %112 [
    i32 0, label %105
  ]

105:                                              ; preds = %103
  br label %14, !llvm.loop !24

106:                                              ; preds = %14
  %107 = load i32, ptr %7, align 4, !tbaa !11
  %108 = zext i32 %107 to i64
  %109 = load i64, ptr %6, align 8, !tbaa !9
  %110 = add i64 %109, %108
  store i64 %110, ptr %6, align 8, !tbaa !9
  %111 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %111, ptr %3, align 8
  store i32 1, ptr %13, align 4
  br label %112

112:                                              ; preds = %106, %103
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %113 = load i64, ptr %3, align 8
  ret i64 %113
}

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !10, i64 0, !10, i64 8, !15, i64 16}
!15 = !{!"long long", !7, i64 0}
!16 = !{!14, !15, i64 16}
!17 = !{!15, !15, i64 0}
!18 = !{!19, !12, i64 20}
!19 = !{!"", !15, i64 0, !15, i64 8, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40}
!20 = !{!19, !12, i64 24}
!21 = !{!19, !12, i64 32}
!22 = !{!14, !10, i64 0}
!23 = !{!19, !12, i64 16}
!24 = distinct !{!24, !25}
!25 = !{!"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 }
%struct.ZSTD_frameHeader = type { i64, i64, i32, i32, i32, i32, i32, i32, i32 }

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @ZSTD_isError(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @ZSTD_getFrameHeader(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; 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 #0

; Function Attrs: nounwind uwtable
define dso_local i64 @ZSTD_decompressionMargin(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.ZSTD_frameSizeInfo, align 8
  %4 = alloca %struct.ZSTD_frameHeader, align 8
  %.not49 = icmp eq i64 %1, 0
  br i1 %.not49, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %5 = getelementptr inbounds i8, ptr %3, i64 8
  %6 = getelementptr inbounds i8, ptr %3, i64 16
  %7 = getelementptr inbounds i8, ptr %4, i64 20
  %8 = getelementptr inbounds i8, ptr %4, i64 24
  %9 = getelementptr inbounds i8, ptr %4, i64 32
  %10 = getelementptr inbounds i8, ptr %4, i64 16
  br label %11

11:                                               ; preds = %.lr.ph, %37
  %.02953 = phi ptr [ %0, %.lr.ph ], [ %38, %37 ]
  %.03152 = phi i64 [ %1, %.lr.ph ], [ %39, %37 ]
  %.03351 = phi i32 [ 0, %.lr.ph ], [ %.134, %37 ]
  %.03650 = phi i64 [ 0, %.lr.ph ], [ %.137, %37 ]
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #3
  call void @ZSTD_findFrameSizeInfo(ptr dead_on_unwind nonnull writable sret(%struct.ZSTD_frameSizeInfo) align 8 %3, ptr noundef %.02953, i64 noundef %.03152, i32 noundef 0)
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = load i64, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %4) #3
  %14 = call i64 @ZSTD_getFrameHeader(ptr noundef nonnull %4, ptr noundef %.02953, i64 noundef %.03152)
  %15 = call i32 @ERR_isError(i64 noundef %14)
  %.not39 = icmp eq i32 %15, 0
  br i1 %.not39, label %16, label %.thread

16:                                               ; preds = %11
  %17 = call i32 @ZSTD_isError(i64 noundef %12)
  %18 = icmp ne i32 %17, 0
  %19 = icmp eq i64 %13, -2
  %or.cond = select i1 %18, i1 true, i1 %19
  br i1 %or.cond, label %.thread, label %20

20:                                               ; preds = %16
  %21 = load i32, ptr %7, align 4, !tbaa !12
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %35

23:                                               ; preds = %20
  %24 = load i32, ptr %8, align 8, !tbaa !15
  %25 = zext i32 %24 to i64
  %26 = add i64 %.03650, %25
  %27 = load i32, ptr %9, align 8, !tbaa !16
  %.not40 = icmp eq i32 %27, 0
  %28 = select i1 %.not40, i64 0, i64 4
  %29 = add i64 %26, %28
  %30 = load i64, ptr %3, align 8, !tbaa !17
  %31 = mul i64 %30, 3
  %32 = add i64 %29, %31
  %33 = load i32, ptr %10, align 8
  %34 = call i32 @llvm.umax.i32(i32 %.03351, i32 %33)
  br label %37

35:                                               ; preds = %20
  %36 = add i64 %12, %.03650
  br label %37

.thread:                                          ; preds = %11, %16
  %.2.ph = phi i64 [ -20, %16 ], [ %14, %11 ]
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #3
  br label %._crit_edge

37:                                               ; preds = %23, %35
  %.137 = phi i64 [ %32, %23 ], [ %36, %35 ]
  %.134 = phi i32 [ %34, %23 ], [ %.03351, %35 ]
  %38 = getelementptr inbounds i8, ptr %.02953, i64 %12
  %39 = sub i64 %.03152, %12
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #3
  %.not = icmp eq i64 %39, 0
  br i1 %.not, label %._crit_edge.loopexit, label %11, !llvm.loop !18

._crit_edge.loopexit:                             ; preds = %37
  %40 = zext i32 %.134 to i64
  %41 = add i64 %.137, %40
  br label %._crit_edge

._crit_edge:                                      ; preds = %2, %._crit_edge.loopexit, %.thread
  %.3 = phi i64 [ %.2.ph, %.thread ], [ 0, %2 ], [ %41, %._crit_edge.loopexit ]
  ret i64 %.3
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #2

attributes #0 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !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 = !{!13, !14, i64 20}
!13 = !{!"", !10, i64 0, !10, i64 8, !14, i64 16, !14, i64 20, !14, i64 24, !14, i64 28, !14, i64 32, !14, i64 36, !14, i64 40}
!14 = !{!"int", !8, i64 0}
!15 = !{!13, !14, i64 24}
!16 = !{!13, !14, i64 32}
!17 = !{!6, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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