; LLVM IR from facebookresearch/habitat-sim :: src/deps/basis-universal/encoder/jpgd.ll (ZN4jpgd12jpeg_decoder10word_clearEPvtj)

; 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 @_ZN4jpgd12jpeg_decoder10word_clearEPvtj(ptr noundef nonnull align 8 dereferenceable(13523) %0, ptr noundef %1, i16 noundef zeroext %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i8, align 1
  %11 = alloca i8, align 1
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i16 %2, ptr %7, align 2, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  %12 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %13, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  %14 = load i16, ptr %7, align 2, !tbaa !9
  %15 = zext i16 %14 to i32
  %16 = and i32 %15, 255
  %17 = trunc i32 %16 to i8
  store i8 %17, ptr %10, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  %18 = load i16, ptr %7, align 2, !tbaa !9
  %19 = zext i16 %18 to i32
  %20 = ashr i32 %19, 8
  %21 = and i32 %20, 255
  %22 = trunc i32 %21 to i8
  store i8 %22, ptr %11, align 1, !tbaa !13
  br label %23

23:                                               ; preds = %26, %4
  %24 = load i32, ptr %8, align 4, !tbaa !11
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %37

26:                                               ; preds = %23
  %27 = load i8, ptr %10, align 1, !tbaa !13
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i64 0
  store i8 %27, ptr %29, align 1, !tbaa !13
  %30 = load i8, ptr %11, align 1, !tbaa !13
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 1
  store i8 %30, ptr %32, align 1, !tbaa !13
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i64 2
  store ptr %34, ptr %9, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !11
  %36 = add i32 %35, -1
  store i32 %36, ptr %8, align 4, !tbaa !11
  br label %23, !llvm.loop !14

37:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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(write, inaccessiblemem: none) uwtable
define dso_local void @_ZN4jpgd12jpeg_decoder10word_clearEPvtj(ptr nocapture noundef nonnull readnone align 8 dereferenceable(13523) %0, ptr nocapture noundef writeonly %1, i16 noundef zeroext %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %5 = trunc i16 %2 to i8
  %6 = lshr i16 %2, 8
  %7 = trunc nuw i16 %6 to i8
  %.not11 = icmp eq i32 %3, 0
  br i1 %.not11, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %8 = zext i32 %3 to i64
  %min.iters.check = icmp ult i32 %3, 16
  br i1 %min.iters.check, label %.lr.ph.preheader20, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %8, 4294967280
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end = sub i32 %3, %.cast
  %9 = shl nuw nsw i64 %n.vec, 1
  %ind.end14 = getelementptr i8, ptr %1, i64 %9
  %broadcast.splatinsert = insertelement <8 x i8> poison, i8 %5, i64 0
  %broadcast.splatinsert17 = insertelement <8 x i8> poison, i8 %7, i64 0
  %interleaved.vec = shufflevector <8 x i8> %broadcast.splatinsert, <8 x i8> %broadcast.splatinsert17, <16 x i32> <i32 0, i32 8, i32 0, i32 8, i32 0, i32 8, i32 0, i32 8, i32 0, i32 8, i32 0, i32 8, i32 0, i32 8, i32 0, i32 8>
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %10 = or disjoint i64 %offset.idx, 16
  %next.gep = getelementptr i8, ptr %1, i64 %offset.idx
  %next.gep16 = getelementptr i8, ptr %1, i64 %10
  store <16 x i8> %interleaved.vec, ptr %next.gep, align 1, !tbaa !5
  store <16 x i8> %interleaved.vec, ptr %next.gep16, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %11 = icmp eq i64 %index.next, %n.vec
  br i1 %11, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %8
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader20

.lr.ph.preheader20:                               ; preds = %middle.block, %.lr.ph.preheader
  %.013.ph = phi i32 [ %3, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01012.ph = phi ptr [ %1, %.lr.ph.preheader ], [ %ind.end14, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader20, %.lr.ph
  %.013 = phi i32 [ %14, %.lr.ph ], [ %.013.ph, %.lr.ph.preheader20 ]
  %.01012 = phi ptr [ %13, %.lr.ph ], [ %.01012.ph, %.lr.ph.preheader20 ]
  store i8 %5, ptr %.01012, align 1, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %.01012, i64 1
  store i8 %7, ptr %12, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.01012, i64 2
  %14 = add i32 %.013, -1
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %4
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(write, 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" }

!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 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !11, !10}


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