; LLVM IR from ermig1979/Simd :: src/Simd/SimdBaseImageSaveJpeg.ll (ZN4Simd4Base21JpegCalcBitsTableInitEv)

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

@_ZN4Simd4Base17JpegCalcBitsTableE = external dso_local global [4096 x [2 x i16]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN4Simd4Base21JpegCalcBitsTableInitEv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 4096, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %56, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 2048
  store i32 %13, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %11
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sub nsw i32 0, %17
  br label %21

19:                                               ; preds = %11
  %20 = load i32, ptr %3, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %19, %16
  %22 = phi i32 [ %18, %16 ], [ %20, %19 ]
  store i32 %22, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %21
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  br label %30

28:                                               ; preds = %21
  %29 = load i32, ptr %3, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %28, %25
  %31 = phi i32 [ %27, %25 ], [ %29, %28 ]
  store i32 %31, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %36, %30
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = ashr i32 %33, 1
  store i32 %34, ptr %4, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %32
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !5
  br label %32, !llvm.loop !9

39:                                               ; preds = %32
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = shl i32 1, %41
  %43 = sub nsw i32 %42, 1
  %44 = and i32 %40, %43
  %45 = trunc i32 %44 to i16
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [4096 x [2 x i16]], ptr @_ZN4Simd4Base17JpegCalcBitsTableE, i64 0, i64 %47
  %49 = getelementptr inbounds [2 x i16], ptr %48, i64 0, i64 0
  store i16 %45, ptr %49, align 4, !tbaa !11
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = trunc i32 %50 to i16
  %52 = load i32, ptr %1, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [4096 x [2 x i16]], ptr @_ZN4Simd4Base17JpegCalcBitsTableE, i64 0, i64 %53
  %55 = getelementptr inbounds [2 x i16], ptr %54, i64 0, i64 1
  store i16 %51, ptr %55, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %56

56:                                               ; preds = %39
  %57 = load i32, ptr %1, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !13

59:                                               ; preds = %10
  ret i1 true
}

; 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

attributes #0 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !10}


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

@_ZN4Simd4Base17JpegCalcBitsTableE = external dso_local local_unnamed_addr global [4096 x [2 x i16]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_ZN4Simd4Base21JpegCalcBitsTableInitEv() local_unnamed_addr #0 {
  br label %2

1:                                                ; preds = %._crit_edge
  ret i1 true

2:                                                ; preds = %0, %._crit_edge
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %._crit_edge ]
  %indvars24 = trunc i64 %indvars.iv to i32
  %3 = add nsw i32 %indvars24, -2048
  %4 = icmp ult i64 %indvars.iv, 2048
  %5 = trunc i64 %indvars.iv to i32
  %6 = sub i32 2048, %5
  %7 = select i1 %4, i32 %6, i32 %3
  %8 = trunc i64 %indvars.iv to i32
  %9 = add i32 %8, 63487
  %10 = select i1 %4, i32 %9, i32 %3
  %.not20 = icmp ult i32 %7, 2
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.022 = phi i32 [ %12, %.lr.ph ], [ 1, %2 ]
  %.01721 = phi i32 [ %11, %.lr.ph ], [ %7, %2 ]
  %11 = ashr i32 %.01721, 1
  %12 = add nuw nsw i32 %.022, 1
  %.not = icmp ult i32 %11, 2
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.0.lcssa = phi i32 [ 1, %2 ], [ %12, %.lr.ph ]
  %notmask = shl nsw i32 -1, %.0.lcssa
  %13 = xor i32 %notmask, -1
  %14 = and i32 %10, %13
  %15 = trunc i32 %14 to i16
  %16 = getelementptr inbounds [4096 x [2 x i16]], ptr @_ZN4Simd4Base17JpegCalcBitsTableE, i64 0, i64 %indvars.iv
  store i16 %15, ptr %16, align 4, !tbaa !7
  %17 = trunc i32 %.0.lcssa to i16
  %18 = getelementptr inbounds i8, ptr %16, i64 2
  store i16 %17, ptr %18, align 2, !tbaa !7
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 4096
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(write, argmem: none, 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = distinct !{!11, !6}


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