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

; 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 uwtable
declare dso_local void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4jpge12jpeg_encoder8put_bitsEjj(ptr noundef nonnull align 8 dereferenceable(13494) %0, i32 noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 34
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = add i32 %12, %10
  store i32 %13, ptr %11, align 8, !tbaa !11
  %14 = sub i32 24, %13
  %15 = shl i32 %9, %14
  %16 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 33
  %17 = load i32, ptr %16, align 4, !tbaa !16
  %18 = or i32 %17, %15
  store i32 %18, ptr %16, align 4, !tbaa !16
  br label %19

19:                                               ; preds = %51, %3
  %20 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 34
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = icmp uge i32 %21, 8
  br i1 %22, label %23, label %58

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %24 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 33
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = lshr i32 %25, 16
  %27 = and i32 %26, 255
  %28 = trunc i32 %27 to i8
  store i8 %28, ptr %7, align 1, !tbaa !17
  %29 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 31
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  %31 = getelementptr inbounds i8, ptr %30, i32 1
  store ptr %31, ptr %29, align 8, !tbaa !18
  store i8 %28, ptr %30, align 1, !tbaa !17
  %32 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 32
  %33 = load i32, ptr %32, align 8, !tbaa !19
  %34 = add i32 %33, -1
  store i32 %34, ptr %32, align 8, !tbaa !19
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %23
  call void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494) %8)
  br label %37

37:                                               ; preds = %36, %23
  %38 = load i8, ptr %7, align 1, !tbaa !17
  %39 = zext i8 %38 to i32
  %40 = icmp eq i32 %39, 255
  br i1 %40, label %41, label %51

41:                                               ; preds = %37
  %42 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 31
  %43 = load ptr, ptr %42, align 8, !tbaa !18
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %42, align 8, !tbaa !18
  store i8 0, ptr %43, align 1, !tbaa !17
  %45 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 32
  %46 = load i32, ptr %45, align 8, !tbaa !19
  %47 = add i32 %46, -1
  store i32 %47, ptr %45, align 8, !tbaa !19
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %41
  call void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494) %8)
  br label %50

50:                                               ; preds = %49, %41
  br label %51

51:                                               ; preds = %50, %37
  %52 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 33
  %53 = load i32, ptr %52, align 4, !tbaa !16
  %54 = shl i32 %53, 8
  store i32 %54, ptr %52, align 4, !tbaa !16
  %55 = getelementptr inbounds %"class.jpge::jpeg_encoder", ptr %8, i32 0, i32 34
  %56 = load i32, ptr %55, align 8, !tbaa !11
  %57 = sub i32 %56, 8
  store i32 %57, ptr %55, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  br label %19, !llvm.loop !20

58:                                               ; preds = %19
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!12, !10, i64 13488}
!12 = !{!"_ZTSN4jpge12jpeg_encoderE", !6, i64 0, !13, i64 8, !7, i64 20, !7, i64 21, !7, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, 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, !10, i64 13480, !10, i64 13484, !10, i64 13488, !7, i64 13492, !15, i64 13493}
!13 = !{!"_ZTSN4jpge6paramsE", !10, i64 0, !14, i64 4, !15, i64 8, !15, i64 9}
!14 = !{!"_ZTSN4jpge13subsampling_tE", !7, i64 0}
!15 = !{!"bool", !7, i64 0}
!16 = !{!12, !10, i64 13484}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !6, i64 13472}
!19 = !{!12, !10, i64 13480}
!20 = distinct !{!20, !21}
!21 = !{!"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 uwtable
declare dso_local void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4jpge12jpeg_encoder8put_bitsEjj(ptr noundef nonnull align 8 dereferenceable(13494) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 13488
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = add i32 %5, %2
  store i32 %6, ptr %4, align 8, !tbaa !5
  %7 = sub i32 24, %6
  %8 = shl i32 %1, %7
  %9 = getelementptr inbounds i8, ptr %0, i64 13484
  %10 = load i32, ptr %9, align 4, !tbaa !14
  %11 = or i32 %8, %10
  store i32 %11, ptr %9, align 4, !tbaa !14
  %12 = icmp ugt i32 %6, 7
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %13 = getelementptr inbounds i8, ptr %0, i64 13472
  %14 = getelementptr inbounds i8, ptr %0, i64 13480
  br label %15

15:                                               ; preds = %.lr.ph, %35
  %16 = phi i32 [ %11, %.lr.ph ], [ %37, %35 ]
  %17 = lshr i32 %16, 16
  %18 = trunc i32 %17 to i8
  %19 = load ptr, ptr %13, align 8, !tbaa !15
  %20 = getelementptr inbounds i8, ptr %19, i64 1
  store ptr %20, ptr %13, align 8, !tbaa !15
  store i8 %18, ptr %19, align 1, !tbaa !16
  %21 = load i32, ptr %14, align 8, !tbaa !17
  %22 = add i32 %21, -1
  store i32 %22, ptr %14, align 8, !tbaa !17
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %15
  tail call void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494) %0)
  br label %25

25:                                               ; preds = %24, %15
  %26 = and i32 %16, 16711680
  %27 = icmp eq i32 %26, 16711680
  br i1 %27, label %28, label %35

28:                                               ; preds = %25
  %29 = load ptr, ptr %13, align 8, !tbaa !15
  %30 = getelementptr inbounds i8, ptr %29, i64 1
  store ptr %30, ptr %13, align 8, !tbaa !15
  store i8 0, ptr %29, align 1, !tbaa !16
  %31 = load i32, ptr %14, align 8, !tbaa !17
  %32 = add i32 %31, -1
  store i32 %32, ptr %14, align 8, !tbaa !17
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %28
  tail call void @_ZN4jpge12jpeg_encoder19flush_output_bufferEv(ptr noundef nonnull align 8 dereferenceable(13494) %0)
  br label %35

35:                                               ; preds = %28, %34, %25
  %36 = load i32, ptr %9, align 4, !tbaa !14
  %37 = shl i32 %36, 8
  store i32 %37, ptr %9, align 4, !tbaa !14
  %38 = load i32, ptr %4, align 8, !tbaa !5
  %39 = add i32 %38, -8
  store i32 %39, ptr %4, align 8, !tbaa !5
  %40 = icmp ugt i32 %39, 7
  br i1 %40, label %15, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %35, %3
  ret void
}

attributes #0 = { mustprogress 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, !11, i64 13488}
!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 = !{!6, !11, i64 13484}
!15 = !{!6, !7, i64 13472}
!16 = !{!8, !8, i64 0}
!17 = !{!6, !11, i64 13480}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass