; LLVM IR from drohmer/cgp :: library/third_party/src/jpeg/jpge.ll (ZN4jpge12jpeg_encoder19compute_quant_tableEPiPs)

; 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.jpge::jpeg_encoder" = type <{ ptr, %"struct.jpge::params", i8, [3 x i8], [3 x i8], i8, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [16 x ptr], i8, [3 x i8], [64 x i32], [64 x i16], [2 x [64 x i32]], [4 x [256 x i32]], [4 x [256 x i8]], [4 x [17 x i8]], [4 x [256 x i8]], [4 x [256 x i32]], [3 x i32], [2048 x i8], [4 x i8], ptr, i32, i32, i32, i8, i8, [2 x i8] }>
%"struct.jpge::params" = type <{ i32, i32, i8, i8, [2 x i8] }>

; 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 @_ZN4jpge12jpeg_encoder19compute_quant_tableEPiPs(ptr noundef nonnull align 8 dereferenceable(13494) %0, ptr noundef %1, ptr noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %10, i32 0, i32 1
  %12 = getelementptr inbounds %"struct.jpge::params", ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = icmp slt i32 %13, 50
  br i1 %14, label %15, label %20

15:                                               ; preds = %3
  %16 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %10, i32 0, i32 1
  %17 = getelementptr inbounds %"struct.jpge::params", ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !9
  %19 = sdiv i32 5000, %18
  store i32 %19, ptr %7, align 4, !tbaa !15
  br label %26

20:                                               ; preds = %3
  %21 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %10, i32 0, i32 1
  %22 = getelementptr inbounds %"struct.jpge::params", ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !9
  %24 = mul nsw i32 %23, 2
  %25 = sub nsw i32 200, %24
  store i32 %25, ptr %7, align 4, !tbaa !15
  br label %26

26:                                               ; preds = %20, %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !15
  br label %27

27:                                               ; preds = %64, %26
  %28 = load i32, ptr %8, align 4, !tbaa !15
  %29 = icmp slt i32 %28, 64
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %67

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds i16, ptr %32, i32 1
  store ptr %33, ptr %6, align 8, !tbaa !5
  %34 = load i16, ptr %32, align 2, !tbaa !16
  %35 = sext i16 %34 to i32
  store i32 %35, ptr %9, align 4, !tbaa !15
  %36 = load i32, ptr %9, align 4, !tbaa !15
  %37 = load i32, ptr %7, align 4, !tbaa !15
  %38 = mul nsw i32 %36, %37
  %39 = sext i32 %38 to i64
  %40 = add nsw i64 %39, 50
  %41 = sdiv i64 %40, 100
  %42 = trunc i64 %41 to i32
  store i32 %42, ptr %9, align 4, !tbaa !15
  %43 = load i32, ptr %9, align 4, !tbaa !15
  %44 = icmp sgt i32 %43, 1
  br i1 %44, label %45, label %47

45:                                               ; preds = %31
  %46 = load i32, ptr %9, align 4, !tbaa !15
  br label %48

47:                                               ; preds = %31
  br label %48

48:                                               ; preds = %47, %45
  %49 = phi i32 [ %46, %45 ], [ 1, %47 ]
  %50 = icmp slt i32 %49, 255
  br i1 %50, label %51, label %59

51:                                               ; preds = %48
  %52 = load i32, ptr %9, align 4, !tbaa !15
  %53 = icmp sgt i32 %52, 1
  br i1 %53, label %54, label %56

54:                                               ; preds = %51
  %55 = load i32, ptr %9, align 4, !tbaa !15
  br label %57

56:                                               ; preds = %51
  br label %57

57:                                               ; preds = %56, %54
  %58 = phi i32 [ %55, %54 ], [ 1, %56 ]
  br label %60

59:                                               ; preds = %48
  br label %60

60:                                               ; preds = %59, %57
  %61 = phi i32 [ %58, %57 ], [ 255, %59 ]
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds i32, ptr %62, i32 1
  store ptr %63, ptr %5, align 8, !tbaa !5
  store i32 %61, ptr %62, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %64

64:                                               ; preds = %60
  %65 = load i32, ptr %8, align 4, !tbaa !15
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %8, align 4, !tbaa !15
  br label %27, !llvm.loop !18

67:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #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, !12, i64 8}
!10 = !{!"_ZTSN4jpge12jpeg_encoderE", !6, i64 0, !11, i64 8, !7, i64 20, !7, i64 21, !7, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !7, i64 72, !7, i64 200, !7, i64 204, !7, i64 460, !7, i64 588, !7, i64 1100, !7, i64 5196, !7, i64 6220, !7, i64 6288, !7, i64 7312, !7, i64 11408, !7, i64 11420, !6, i64 13472, !12, i64 13480, !12, i64 13484, !12, i64 13488, !7, i64 13492, !14, i64 13493}
!11 = !{!"_ZTSN4jpge6paramsE", !12, i64 0, !13, i64 4, !14, i64 8, !14, i64 9}
!12 = !{!"int", !7, i64 0}
!13 = !{!"_ZTSN4jpge13subsampling_tE", !7, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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 @_ZN4jpge12jpeg_encoder19compute_quant_tableEPiPs(ptr nocapture noundef nonnull readonly align 8 dereferenceable(13494) %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp slt i32 %5, 50
  br i1 %6, label %7, label %9

7:                                                ; preds = %3
  %8 = sdiv i32 5000, %5
  br label %12

9:                                                ; preds = %3
  %10 = shl nuw nsw i32 %5, 1
  %11 = sub nsw i32 200, %10
  br label %12

12:                                               ; preds = %9, %7
  %.011 = phi i32 [ %8, %7 ], [ %11, %9 ]
  br label %14

13:                                               ; preds = %14
  ret void

14:                                               ; preds = %12, %14
  %.016 = phi ptr [ %1, %12 ], [ %25, %14 ]
  %.01015 = phi i32 [ 0, %12 ], [ %26, %14 ]
  %.01214 = phi ptr [ %2, %12 ], [ %15, %14 ]
  %15 = getelementptr inbounds i8, ptr %.01214, i64 2
  %16 = load i16, ptr %.01214, align 2, !tbaa !14
  %17 = sext i16 %16 to i32
  %18 = mul nsw i32 %.011, %17
  %19 = sext i32 %18 to i64
  %20 = add nsw i64 %19, 50
  %21 = sdiv i64 %20, 100
  %22 = trunc nsw i64 %21 to i32
  %23 = tail call i32 @llvm.smax.i32(i32 %22, i32 1)
  %24 = tail call i32 @llvm.umin.i32(i32 %23, i32 255)
  %25 = getelementptr inbounds i8, ptr %.016, i64 4
  store i32 %24, ptr %.016, align 4, !tbaa !16
  %26 = add nuw nsw i32 %.01015, 1
  %exitcond.not = icmp eq i32 %26, 64
  br i1 %exitcond.not, label %13, label %14, !llvm.loop !17
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, i64 8}
!6 = !{!"_ZTSN4jpge12jpeg_encoderE", !7, i64 0, !10, i64 8, !8, i64 20, !8, i64 21, !8, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !8, i64 72, !8, i64 200, !8, i64 204, !8, i64 460, !8, i64 588, !8, i64 1100, !8, i64 5196, !8, i64 6220, !8, i64 6288, !8, i64 7312, !8, i64 11408, !8, i64 11420, !7, i64 13472, !11, i64 13480, !11, i64 13484, !11, i64 13488, !8, i64 13492, !13, i64 13493}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN4jpge6paramsE", !11, i64 0, !12, i64 4, !13, i64 8, !13, i64 9}
!11 = !{!"int", !8, i64 0}
!12 = !{!"_ZTSN4jpge13subsampling_tE", !8, i64 0}
!13 = !{!"bool", !8, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !8, i64 0}
!16 = !{!11, !11, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass