; LLVM IR from vergecurrency/verge :: src/crypto/lz4 (lz4)

; 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.LZ4_Data_Structure = type { [4096 x i32], ptr, ptr, ptr }

; 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
define dso_local ptr @LZ4_slideInputBuffer(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %7, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %8, i32 0, i32 3
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %13, i64 65536
  %15 = ptrtoint ptr %10 to i64
  %16 = ptrtoint ptr %14 to i64
  %17 = sub i64 %15, %16
  store i64 %17, ptr %4, align 8, !tbaa !12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = load i64, ptr %4, align 8, !tbaa !12
  %22 = sub i64 0, %21
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = icmp ugt ptr %23, %26
  br i1 %27, label %39, label %28

28:                                               ; preds = %1
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = ptrtoint ptr %31 to i64
  %36 = ptrtoint ptr %34 to i64
  %37 = sub i64 %35, %36
  %38 = icmp ugt i64 %37, 3758096384
  br i1 %38, label %39, label %102

39:                                               ; preds = %28, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds i8, ptr %42, i64 -65536
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = ptrtoint ptr %43 to i64
  %48 = ptrtoint ptr %46 to i64
  %49 = sub i64 %47, %48
  store i64 %49, ptr %5, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !15
  br label %50

50:                                               ; preds = %80, %39
  %51 = load i32, ptr %6, align 4, !tbaa !15
  %52 = icmp slt i32 %51, 4096
  br i1 %52, label %53, label %83

53:                                               ; preds = %50
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %6, align 4, !tbaa !15
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [4096 x i32], ptr %55, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !15
  %60 = zext i32 %59 to i64
  %61 = load i64, ptr %5, align 8, !tbaa !12
  %62 = icmp ult i64 %60, %61
  br i1 %62, label %63, label %69

63:                                               ; preds = %53
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %6, align 4, !tbaa !15
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [4096 x i32], ptr %65, i64 0, i64 %67
  store i32 0, ptr %68, align 4, !tbaa !15
  br label %79

69:                                               ; preds = %53
  %70 = load i64, ptr %5, align 8, !tbaa !12
  %71 = trunc i64 %70 to i32
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %6, align 4, !tbaa !15
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [4096 x i32], ptr %73, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !15
  %78 = sub i32 %77, %71
  store i32 %78, ptr %76, align 4, !tbaa !15
  br label %79

79:                                               ; preds = %69, %63
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %6, align 4, !tbaa !15
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %6, align 4, !tbaa !15
  br label %50, !llvm.loop !17

83:                                               ; preds = %50
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %84, i32 0, i32 1
  %86 = load ptr, ptr %85, align 8, !tbaa !11
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %87, i32 0, i32 3
  %89 = load ptr, ptr %88, align 8, !tbaa !9
  %90 = getelementptr inbounds i8, ptr %89, i64 -65536
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %86, ptr align 1 %90, i64 65536, i1 false)
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %91, i32 0, i32 1
  %93 = load ptr, ptr %92, align 8, !tbaa !11
  %94 = load ptr, ptr %3, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %94, i32 0, i32 2
  store ptr %93, ptr %95, align 8, !tbaa !14
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %96, i32 0, i32 2
  %98 = load ptr, ptr %97, align 8, !tbaa !14
  %99 = getelementptr inbounds i8, ptr %98, i64 65536
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %100, i32 0, i32 3
  store ptr %99, ptr %101, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %122

102:                                              ; preds = %28
  %103 = load ptr, ptr %3, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %103, i32 0, i32 1
  %105 = load ptr, ptr %104, align 8, !tbaa !11
  %106 = load ptr, ptr %3, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %106, i32 0, i32 3
  %108 = load ptr, ptr %107, align 8, !tbaa !9
  %109 = getelementptr inbounds i8, ptr %108, i64 -65536
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %105, ptr align 1 %109, i64 65536, i1 false)
  %110 = load i64, ptr %4, align 8, !tbaa !12
  %111 = load ptr, ptr %3, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %111, i32 0, i32 3
  %113 = load ptr, ptr %112, align 8, !tbaa !9
  %114 = sub i64 0, %110
  %115 = getelementptr inbounds i8, ptr %113, i64 %114
  store ptr %115, ptr %112, align 8, !tbaa !9
  %116 = load i64, ptr %4, align 8, !tbaa !12
  %117 = load ptr, ptr %3, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %117, i32 0, i32 2
  %119 = load ptr, ptr %118, align 8, !tbaa !14
  %120 = sub i64 0, %116
  %121 = getelementptr inbounds i8, ptr %119, i64 %120
  store ptr %121, ptr %118, align 8, !tbaa !14
  br label %122

122:                                              ; preds = %102, %83
  %123 = load ptr, ptr %3, align 8, !tbaa !5
  %124 = getelementptr inbounds %struct.LZ4_Data_Structure, ptr %123, i32 0, i32 3
  %125 = load ptr, ptr %124, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr %125
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 16400}
!10 = !{!"", !7, i64 0, !6, i64 16384, !6, i64 16392, !6, i64 16400}
!11 = !{!10, !6, i64 16384}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!10, !6, i64 16392}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @LZ4_slideInputBuffer(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16400
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 16384
  %5 = load ptr, ptr %4, align 8, !tbaa !10
  %6 = getelementptr inbounds i8, ptr %5, i64 65536
  %7 = ptrtoint ptr %3 to i64
  %8 = ptrtoint ptr %6 to i64
  %.neg = sub i64 %8, %7
  %9 = getelementptr inbounds i8, ptr %0, i64 16392
  %10 = icmp sgt i64 %.neg, 0
  %.pre = load ptr, ptr %9, align 8, !tbaa !11
  %.pre36 = ptrtoint ptr %.pre to i64
  %11 = sub i64 %7, %.pre36
  %12 = icmp ugt i64 %11, 3758096384
  %or.cond = select i1 %10, i1 true, i1 %12
  %13 = getelementptr inbounds i8, ptr %3, i64 -65536
  br i1 %or.cond, label %vector.ph, label %25

vector.ph:                                        ; preds = %1
  %14 = ptrtoint ptr %13 to i64
  %15 = sub i64 %14, %.pre36
  %16 = trunc nuw i64 %15 to i32
  %broadcast.splatinsert = insertelement <4 x i64> poison, i64 %15, i64 0
  %broadcast.splat = shufflevector <4 x i64> %broadcast.splatinsert, <4 x i64> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert38 = insertelement <4 x i32> poison, i32 %16, i64 0
  %broadcast.splat39 = shufflevector <4 x i32> %broadcast.splatinsert38, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %17 = getelementptr inbounds [4096 x i32], ptr %0, i64 0, i64 %index
  %wide.load = load <4 x i32>, ptr %17, align 4, !tbaa !12
  %18 = zext <4 x i32> %wide.load to <4 x i64>
  %19 = icmp ugt <4 x i64> %broadcast.splat, %18
  %20 = sub <4 x i32> %wide.load, %broadcast.splat39
  %21 = select <4 x i1> %19, <4 x i32> zeroinitializer, <4 x i32> %20
  store <4 x i32> %21, ptr %17, align 4, !tbaa !12
  %index.next = add nuw i64 %index, 4
  %22 = icmp eq i64 %index.next, 4096
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(65536) %5, ptr noundef nonnull align 1 dereferenceable(65536) %13, i64 65536, i1 false)
  %23 = load ptr, ptr %4, align 8, !tbaa !10
  store ptr %23, ptr %9, align 8, !tbaa !11
  %24 = getelementptr inbounds i8, ptr %23, i64 65536
  store ptr %24, ptr %2, align 8, !tbaa !5
  br label %30

25:                                               ; preds = %1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(65536) %5, ptr noundef nonnull align 1 dereferenceable(65536) %13, i64 65536, i1 false)
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 %.neg
  store ptr %27, ptr %2, align 8, !tbaa !5
  %28 = load ptr, ptr %9, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %28, i64 %.neg
  store ptr %29, ptr %9, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %25, %middle.block
  %31 = phi ptr [ %27, %25 ], [ %24, %middle.block ]
  ret ptr %31
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: readwrite) }

!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, !9, i64 16400}
!6 = !{!"", !7, i64 0, !9, i64 16384, !9, i64 16392, !9, i64 16400}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!6, !9, i64 16384}
!11 = !{!6, !9, i64 16392}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass