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

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

%"class.jpgd::jpeg_decoder" = type <{ [1 x %struct.__jmp_buf_tag], i32, [4 x i8], ptr, i32, i32, ptr, i32, [8 x i8], [4 x i8], [8 x ptr], [8 x ptr], [4 x ptr], i32, i32, [4 x i32], [4 x i32], [4 x i32], [4 x i32], [4 x i32], [4 x i32], i32, [4 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [10 x i32], i32, i32, i32, i32, i32, i32, [4 x i8], [8 x ptr], [4 x ptr], [4 x ptr], i32, [4 x i32], [4 x i8], ptr, i32, i32, [64 x i8], [8320 x i8], [64 x i8], i32, i32, i32, i32, i32, i32, i32, i32, [4 x i32], ptr, [10 x i32], ptr, ptr, [256 x i32], [256 x i32], [256 x i32], [256 x i32], ptr, ptr, i32, i32, i8, i8, i8, [5 x i8] }>
%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
%struct.__sigset_t = type { [16 x 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: mustprogress nounwind uwtable
define dso_local void @_ZN4jpgd12jpeg_decoder12gray_convertEv(ptr noundef nonnull align 8 dereferenceable(13523) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %8 = getelementptr inbounds %"class.jpgd::jpeg_decoder", ptr %7, i32 0, i32 30
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds %"class.jpgd::jpeg_decoder", ptr %7, i32 0, i32 37
  %11 = load i32, ptr %10, align 8, !tbaa !14
  %12 = sub nsw i32 %9, %11
  store i32 %12, ptr %3, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %13 = getelementptr inbounds %"class.jpgd::jpeg_decoder", ptr %7, i32 0, i32 72
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  store ptr %14, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %15 = getelementptr inbounds %"class.jpgd::jpeg_decoder", ptr %7, i32 0, i32 66
  %16 = load ptr, ptr %15, align 8, !tbaa !17
  %17 = load i32, ptr %3, align 4, !tbaa !15
  %18 = mul nsw i32 %17, 8
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %16, i64 %19
  store ptr %20, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %21 = getelementptr inbounds %"class.jpgd::jpeg_decoder", ptr %7, i32 0, i32 60
  %22 = load i32, ptr %21, align 4, !tbaa !18
  store i32 %22, ptr %6, align 4, !tbaa !15
  br label %23

23:                                               ; preds = %40, %1
  %24 = load i32, ptr %6, align 4, !tbaa !15
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %43

27:                                               ; preds = %23
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %28, align 4, !tbaa !15
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %29, ptr %30, align 4, !tbaa !15
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 4
  %33 = load i32, ptr %32, align 4, !tbaa !15
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 4
  store i32 %33, ptr %35, align 4, !tbaa !15
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 64
  store ptr %37, ptr %5, align 8, !tbaa !5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 8
  store ptr %39, ptr %4, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %27
  %41 = load i32, ptr %6, align 4, !tbaa !15
  %42 = add nsw i32 %41, -1
  store i32 %42, ptr %6, align 4, !tbaa !15
  br label %23, !llvm.loop !19

43:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #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, !11, i64 584}
!10 = !{!"_ZTSN4jpgd12jpeg_decoderE", !7, i64 0, !11, i64 200, !6, i64 208, !11, i64 216, !11, i64 220, !6, i64 224, !11, i64 232, !7, i64 236, !7, i64 248, !7, i64 312, !7, i64 376, !11, i64 408, !11, i64 412, !7, i64 416, !7, i64 432, !7, i64 448, !7, i64 464, !7, i64 480, !7, i64 496, !11, i64 512, !7, i64 516, !7, i64 532, !7, i64 548, !11, i64 564, !11, i64 568, !11, i64 572, !11, i64 576, !11, i64 580, !11, i64 584, !11, i64 588, !11, i64 592, !11, i64 596, !11, i64 600, !7, i64 604, !11, i64 644, !11, i64 648, !11, i64 652, !11, i64 656, !11, i64 660, !11, i64 664, !7, i64 672, !7, i64 736, !7, i64 768, !11, i64 800, !7, i64 804, !6, i64 824, !11, i64 832, !11, i64 836, !7, i64 840, !7, i64 904, !7, i64 9224, !11, i64 9288, !11, i64 9292, !11, i64 9296, !11, i64 9300, !11, i64 9304, !11, i64 9308, !11, i64 9312, !11, i64 9316, !7, i64 9320, !6, i64 9336, !7, i64 9344, !6, i64 9384, !6, i64 9392, !7, i64 9400, !7, i64 10424, !7, i64 11448, !7, i64 12472, !6, i64 13496, !6, i64 13504, !12, i64 13512, !11, i64 13516, !13, i64 13520, !13, i64 13521, !13, i64 13522}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_ZTSN4jpgd11jpgd_statusE", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!10, !11, i64 648}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !6, i64 13496}
!17 = !{!10, !6, i64 9384}
!18 = !{!10, !11, i64 9308}
!19 = distinct !{!19, !20}
!20 = !{!"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(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN4jpgd12jpeg_decoder12gray_convertEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(13523) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 9308
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 9384
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %0, i64 584
  %8 = load i32, ptr %7, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %0, i64 648
  %10 = load i32, ptr %9, align 8, !tbaa !15
  %11 = sub nsw i32 %8, %10
  %12 = shl nsw i32 %11, 3
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %6, i64 %13
  %15 = getelementptr inbounds i8, ptr %0, i64 13496
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %xtraiter = and i32 %3, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.013.prol = phi i32 [ %23, %.lr.ph.prol ], [ %3, %.lr.ph.preheader ]
  %.0912.prol = phi ptr [ %21, %.lr.ph.prol ], [ %14, %.lr.ph.preheader ]
  %.01011.prol = phi ptr [ %22, %.lr.ph.prol ], [ %16, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %17 = load i32, ptr %.0912.prol, align 4, !tbaa !17
  store i32 %17, ptr %.01011.prol, align 4, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %.0912.prol, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %.01011.prol, i64 4
  store i32 %19, ptr %20, align 4, !tbaa !17
  %21 = getelementptr inbounds i8, ptr %.0912.prol, i64 64
  %22 = getelementptr inbounds i8, ptr %.01011.prol, i64 8
  %23 = add nsw i32 %.013.prol, -1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !18

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.013.unr = phi i32 [ %3, %.lr.ph.preheader ], [ %23, %.lr.ph.prol ]
  %.0912.unr = phi ptr [ %14, %.lr.ph.preheader ], [ %21, %.lr.ph.prol ]
  %.01011.unr = phi ptr [ %16, %.lr.ph.preheader ], [ %22, %.lr.ph.prol ]
  %24 = icmp ult i32 %3, 4
  br i1 %24, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %1
  ret void

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.013 = phi i32 [ %49, %.lr.ph ], [ %.013.unr, %.lr.ph.prol.loopexit ]
  %.0912 = phi ptr [ %47, %.lr.ph ], [ %.0912.unr, %.lr.ph.prol.loopexit ]
  %.01011 = phi ptr [ %48, %.lr.ph ], [ %.01011.unr, %.lr.ph.prol.loopexit ]
  %25 = load i32, ptr %.0912, align 4, !tbaa !17
  store i32 %25, ptr %.01011, align 4, !tbaa !17
  %26 = getelementptr inbounds i8, ptr %.0912, i64 4
  %27 = load i32, ptr %26, align 4, !tbaa !17
  %28 = getelementptr inbounds i8, ptr %.01011, i64 4
  store i32 %27, ptr %28, align 4, !tbaa !17
  %29 = getelementptr inbounds i8, ptr %.0912, i64 64
  %30 = getelementptr inbounds i8, ptr %.01011, i64 8
  %31 = load i32, ptr %29, align 4, !tbaa !17
  store i32 %31, ptr %30, align 4, !tbaa !17
  %32 = getelementptr inbounds i8, ptr %.0912, i64 68
  %33 = load i32, ptr %32, align 4, !tbaa !17
  %34 = getelementptr inbounds i8, ptr %.01011, i64 12
  store i32 %33, ptr %34, align 4, !tbaa !17
  %35 = getelementptr inbounds i8, ptr %.0912, i64 128
  %36 = getelementptr inbounds i8, ptr %.01011, i64 16
  %37 = load i32, ptr %35, align 4, !tbaa !17
  store i32 %37, ptr %36, align 4, !tbaa !17
  %38 = getelementptr inbounds i8, ptr %.0912, i64 132
  %39 = load i32, ptr %38, align 4, !tbaa !17
  %40 = getelementptr inbounds i8, ptr %.01011, i64 20
  store i32 %39, ptr %40, align 4, !tbaa !17
  %41 = getelementptr inbounds i8, ptr %.0912, i64 192
  %42 = getelementptr inbounds i8, ptr %.01011, i64 24
  %43 = load i32, ptr %41, align 4, !tbaa !17
  store i32 %43, ptr %42, align 4, !tbaa !17
  %44 = getelementptr inbounds i8, ptr %.0912, i64 196
  %45 = load i32, ptr %44, align 4, !tbaa !17
  %46 = getelementptr inbounds i8, ptr %.01011, i64 28
  store i32 %45, ptr %46, align 4, !tbaa !17
  %47 = getelementptr inbounds i8, ptr %.0912, i64 256
  %48 = getelementptr inbounds i8, ptr %.01011, i64 32
  %49 = add nsw i32 %.013, -4
  %50 = add i32 %.013, -5
  %51 = icmp ult i32 %50, -2
  br i1 %51, label %.lr.ph, label %._crit_edge, !llvm.loop !20
}

attributes #0 = { mustprogress 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" }

!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 9308}
!6 = !{!"_ZTSN4jpgd12jpeg_decoderE", !7, i64 0, !9, i64 200, !10, i64 208, !9, i64 216, !9, i64 220, !10, i64 224, !9, i64 232, !7, i64 236, !7, i64 248, !7, i64 312, !7, i64 376, !9, i64 408, !9, i64 412, !7, i64 416, !7, i64 432, !7, i64 448, !7, i64 464, !7, i64 480, !7, i64 496, !9, i64 512, !7, i64 516, !7, i64 532, !7, i64 548, !9, i64 564, !9, i64 568, !9, i64 572, !9, i64 576, !9, i64 580, !9, i64 584, !9, i64 588, !9, i64 592, !9, i64 596, !9, i64 600, !7, i64 604, !9, i64 644, !9, i64 648, !9, i64 652, !9, i64 656, !9, i64 660, !9, i64 664, !7, i64 672, !7, i64 736, !7, i64 768, !9, i64 800, !7, i64 804, !10, i64 824, !9, i64 832, !9, i64 836, !7, i64 840, !7, i64 904, !7, i64 9224, !9, i64 9288, !9, i64 9292, !9, i64 9296, !9, i64 9300, !9, i64 9304, !9, i64 9308, !9, i64 9312, !9, i64 9316, !7, i64 9320, !10, i64 9336, !7, i64 9344, !10, i64 9384, !10, i64 9392, !7, i64 9400, !7, i64 10424, !7, i64 11448, !7, i64 12472, !10, i64 13496, !10, i64 13504, !11, i64 13512, !9, i64 13516, !12, i64 13520, !12, i64 13521, !12, i64 13522}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"_ZTSN4jpgd11jpgd_statusE", !7, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!6, !10, i64 9384}
!14 = !{!6, !9, i64 584}
!15 = !{!6, !9, i64 648}
!16 = !{!6, !10, i64 13496}
!17 = !{!9, !9, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.unroll.disable"}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass