; LLVM IR from yinqiwen/ardb :: src/common/buffer/buffer_helper.ll (ZN4ardb12BufferHelper14WriteVarUInt32ERNS_6BufferEj)

; 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: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4ardb12BufferHelper14WriteVarUInt32ERNS_6BufferEj(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [5 x i8], align 1
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 5, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %10, %2
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp ugt i32 %8, 127
  br i1 %9, label %10, label %23

10:                                               ; preds = %7
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = trunc i32 %11 to i8
  %13 = zext i8 %12 to i32
  %14 = and i32 %13, 127
  %15 = or i32 %14, 128
  %16 = trunc i32 %15 to i8
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = add i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !9
  %19 = zext i32 %17 to i64
  %20 = getelementptr inbounds [5 x i8], ptr %5, i64 0, i64 %19
  store i8 %16, ptr %20, align 1, !tbaa !11
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = lshr i32 %21, 7
  store i32 %22, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !12

23:                                               ; preds = %7
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = trunc i32 %24 to i8
  %26 = zext i8 %25 to i32
  %27 = and i32 %26, 127
  %28 = trunc i32 %27 to i8
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = add i32 %29, 1
  store i32 %30, ptr %6, align 4, !tbaa !9
  %31 = zext i32 %29 to i64
  %32 = getelementptr inbounds [5 x i8], ptr %5, i64 0, i64 %31
  store i8 %28, ptr %32, align 1, !tbaa !11
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds [5 x i8], ptr %5, i64 0, i64 0
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = call noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33) %34, ptr noundef %35, i64 noundef %37)
  %39 = icmp eq i32 %33, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 5, ptr %5) #3
  ret i1 %39
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4ardb12BufferHelper14WriteVarUInt32ERNS_6BufferEj(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1) local_unnamed_addr #2 align 2 {
  %3 = alloca [5 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 5, ptr nonnull %3) #3
  %4 = icmp ugt i32 %1, 127
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %2 ]
  %.089 = phi i32 [ %8, %.lr.ph ], [ %1, %2 ]
  %5 = trunc i32 %.089 to i8
  %6 = or i8 %5, -128
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %7 = getelementptr inbounds [5 x i8], ptr %3, i64 0, i64 %indvars.iv
  store i8 %6, ptr %7, align 1, !tbaa !5
  %8 = lshr i32 %.089, 7
  %9 = icmp ugt i32 %.089, 16383
  br i1 %9, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !8

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %10 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %.08.lcssa = phi i32 [ %1, %2 ], [ %8, %._crit_edge.loopexit ]
  %.0.lcssa = phi i32 [ 0, %2 ], [ %10, %._crit_edge.loopexit ]
  %11 = trunc nuw nsw i32 %.08.lcssa to i8
  %12 = add i32 %.0.lcssa, 1
  %13 = zext i32 %.0.lcssa to i64
  %14 = getelementptr inbounds [5 x i8], ptr %3, i64 0, i64 %13
  store i8 %11, ptr %14, align 1, !tbaa !5
  %15 = zext i32 %12 to i64
  %16 = call noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33) %0, ptr noundef nonnull %3, i64 noundef %15)
  %17 = icmp eq i32 %12, %16
  call void @llvm.lifetime.end.p0(i64 5, ptr nonnull %3) #3
  ret i1 %17
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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