; LLVM IR from tensorflow/tflite-micro :: tensorflow/lite/kernels/internal (portable_tensor_utils)

; 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"

; 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: mustprogress nounwind uwtable
define dso_local void @_ZN6tflite12tensor_utils23UnpackDenseInt4IntoInt8EPKaiPa(ptr noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %47, %3
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = sdiv i32 %13, 2
  %15 = icmp slt i32 %12, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %50

17:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %18, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  store i8 %22, ptr %8, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  %23 = load i8, ptr %8, align 1, !tbaa !11
  %24 = sext i8 %23 to i32
  %25 = shl i32 %24, 4
  %26 = trunc i32 %25 to i8
  %27 = sext i8 %26 to i32
  %28 = ashr i32 %27, 4
  %29 = trunc i32 %28 to i8
  store i8 %29, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  %30 = load i8, ptr %8, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = ashr i32 %31, 4
  %33 = trunc i32 %32 to i8
  store i8 %33, ptr %10, align 1, !tbaa !11
  %34 = load i8, ptr %9, align 1, !tbaa !11
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = mul nsw i32 2, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %35, i64 %38
  store i8 %34, ptr %39, align 1, !tbaa !11
  %40 = load i8, ptr %10, align 1, !tbaa !11
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = mul nsw i32 2, %42
  %44 = add nsw i32 %43, 1
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %41, i64 %45
  store i8 %40, ptr %46, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  br label %47

47:                                               ; preds = %17
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %11, !llvm.loop !12

50:                                               ; preds = %16
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = srem i32 %51, 2
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %72

54:                                               ; preds = %50
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load i32, ptr %5, align 4, !tbaa !9
  %57 = sdiv i32 %56, 2
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %55, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !11
  %61 = sext i8 %60 to i32
  %62 = shl i32 %61, 4
  %63 = trunc i32 %62 to i8
  %64 = sext i8 %63 to i32
  %65 = ashr i32 %64, 4
  %66 = trunc i32 %65 to i8
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = load i32, ptr %5, align 4, !tbaa !9
  %69 = sub nsw i32 %68, 1
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i8, ptr %67, i64 %70
  store i8 %66, ptr %71, align 1, !tbaa !11
  br label %72

72:                                               ; preds = %54, %50
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_ZN6tflite12tensor_utils23UnpackDenseInt4IntoInt8EPKaiPa(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = sdiv i32 %1, 2
  %5 = icmp sgt i32 %1, 1
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %4 to i64
  %min.iters.check = icmp ult i32 %4, 16
  br i1 %min.iters.check, label %.lr.ph.preheader23, label %vector.memcheck

.lr.ph.preheader23:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader23
  %6 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.ph
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = shl i8 %7, 4
  %9 = ashr exact i8 %8, 4
  %10 = ashr i8 %7, 4
  %11 = shl nuw nsw i64 %indvars.iv.ph, 1
  %12 = getelementptr inbounds i8, ptr %2, i64 %11
  store i8 %9, ptr %12, align 1, !tbaa !5
  %13 = or disjoint i64 %11, 1
  %14 = getelementptr inbounds i8, ptr %2, i64 %13
  store i8 %10, ptr %14, align 1, !tbaa !5
  %indvars.iv.next.prol = or disjoint i64 %indvars.iv.ph, 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader23
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph.preheader23 ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %15 = add nsw i64 %wide.trip.count, -1
  %16 = icmp eq i64 %indvars.iv.ph, %15
  br i1 %16, label %._crit_edge, label %.lr.ph

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %17 = shl nuw nsw i64 %wide.trip.count, 1
  %scevgep = getelementptr i8, ptr %2, i64 %17
  %scevgep20 = getelementptr i8, ptr %0, i64 %wide.trip.count
  %bound0 = icmp ugt ptr %scevgep20, %2
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader23, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %wide.trip.count, 1073741808
  %invariant.gep = getelementptr i8, ptr %2, i64 -1
  %invariant.gep24 = getelementptr i8, ptr %2, i64 -1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %18 = getelementptr inbounds i8, ptr %0, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %wide.load = load <8 x i8>, ptr %18, align 1, !tbaa !5, !alias.scope !8
  %wide.load21 = load <8 x i8>, ptr %19, align 1, !tbaa !5, !alias.scope !8
  %20 = shl <8 x i8> %wide.load, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
  %21 = shl <8 x i8> %wide.load21, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
  %22 = shl nuw nsw i64 %index, 1
  %23 = shl i64 %index, 1
  %24 = or disjoint i64 %22, 1
  %25 = or disjoint i64 %23, 17
  %gep = getelementptr i8, ptr %invariant.gep, i64 %24
  %gep25 = getelementptr i8, ptr %invariant.gep24, i64 %25
  %26 = shufflevector <8 x i8> %20, <8 x i8> %wide.load, <16 x i32> <i32 0, i32 8, i32 1, i32 9, i32 2, i32 10, i32 3, i32 11, i32 4, i32 12, i32 5, i32 13, i32 6, i32 14, i32 7, i32 15>
  %interleaved.vec = ashr <16 x i8> %26, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
  store <16 x i8> %interleaved.vec, ptr %gep, align 1, !tbaa !5
  %27 = shufflevector <8 x i8> %21, <8 x i8> %wide.load21, <16 x i32> <i32 0, i32 8, i32 1, i32 9, i32 2, i32 10, i32 3, i32 11, i32 4, i32 12, i32 5, i32 13, i32 6, i32 14, i32 7, i32 15>
  %interleaved.vec22 = ashr <16 x i8> %27, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
  store <16 x i8> %interleaved.vec22, ptr %gep25, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader23

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %3
  %29 = and i32 %1, 1
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %57, label %48

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %30 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = shl i8 %31, 4
  %33 = ashr exact i8 %32, 4
  %34 = ashr i8 %31, 4
  %35 = shl nuw nsw i64 %indvars.iv, 1
  %36 = getelementptr inbounds i8, ptr %2, i64 %35
  store i8 %33, ptr %36, align 1, !tbaa !5
  %37 = or disjoint i64 %35, 1
  %38 = getelementptr inbounds i8, ptr %2, i64 %37
  store i8 %34, ptr %38, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %39 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %40 = load i8, ptr %39, align 1, !tbaa !5
  %41 = shl i8 %40, 4
  %42 = ashr exact i8 %41, 4
  %43 = ashr i8 %40, 4
  %44 = shl nuw nsw i64 %indvars.iv.next, 1
  %45 = getelementptr inbounds i8, ptr %2, i64 %44
  store i8 %42, ptr %45, align 1, !tbaa !5
  %46 = or disjoint i64 %44, 1
  %47 = getelementptr inbounds i8, ptr %2, i64 %46
  store i8 %43, ptr %47, align 1, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count
  br i1 %exitcond.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !15

48:                                               ; preds = %._crit_edge
  %49 = sext i32 %4 to i64
  %50 = getelementptr inbounds i8, ptr %0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !5
  %52 = shl i8 %51, 4
  %53 = ashr exact i8 %52, 4
  %54 = sext i32 %1 to i64
  %55 = getelementptr i8, ptr %2, i64 %54
  %56 = getelementptr i8, ptr %55, i64 -1
  store i8 %53, ptr %56, align 1, !tbaa !5
  br label %57

57:                                               ; preds = %48, %._crit_edge
  ret void
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9}
!9 = distinct !{!9, !10}
!10 = distinct !{!10, !"LVerDomain"}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !13}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,VectorCombinePass