; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto/monero (serialize)

; 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: nounwind uwtable
define dso_local i32 @xmr_write_varint(ptr noundef %0, i64 noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %8

8:                                                ; preds = %18, %3
  %9 = load i64, ptr %6, align 8, !tbaa !9
  %10 = icmp uge i64 %9, 128
  br i1 %10, label %11, label %16

11:                                               ; preds = %8
  %12 = load i32, ptr %7, align 4, !tbaa !11
  %13 = zext i32 %12 to i64
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = icmp ult i64 %13, %14
  br label %16

16:                                               ; preds = %11, %8
  %17 = phi i1 [ false, %8 ], [ %15, %11 ]
  br i1 %17, label %18, label %30

18:                                               ; preds = %16
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = and i64 %19, 127
  %21 = or i64 %20, 128
  %22 = trunc i64 %21 to i8
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 %22, ptr %23, align 1, !tbaa !13
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %4, align 8, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !11
  %27 = add i32 %26, 1
  store i32 %27, ptr %7, align 4, !tbaa !11
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = lshr i64 %28, 7
  store i64 %29, ptr %6, align 8, !tbaa !9
  br label %8, !llvm.loop !14

30:                                               ; preds = %16
  %31 = load i32, ptr %7, align 4, !tbaa !11
  %32 = zext i32 %31 to i64
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %35, label %41

35:                                               ; preds = %30
  %36 = load i64, ptr %6, align 8, !tbaa !9
  %37 = trunc i64 %36 to i8
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 %37, ptr %38, align 1, !tbaa !13
  %39 = load i32, ptr %7, align 4, !tbaa !11
  %40 = add i32 %39, 1
  store i32 %40, ptr %7, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %35, %30
  %42 = load i32, ptr %7, align 4, !tbaa !11
  %43 = zext i32 %42 to i64
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = icmp ule i64 %43, %44
  br i1 %45, label %46, label %48

46:                                               ; preds = %41
  %47 = load i32, ptr %7, align 4, !tbaa !11
  br label %49

48:                                               ; preds = %41
  br label %49

49:                                               ; preds = %48, %46
  %50 = phi i32 [ %47, %46 ], [ -1, %48 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %50
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local i32 @xmr_write_varint(ptr nocapture noundef writeonly %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ugt i64 %2, 127
  %5 = icmp ne i64 %1, 0
  %6 = and i1 %4, %5
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %3 ]
  %.01518 = phi i64 [ %10, %.lr.ph ], [ %2, %3 ]
  %.01617 = phi ptr [ %9, %.lr.ph ], [ %0, %3 ]
  %7 = trunc i64 %.01518 to i8
  %8 = or i8 %7, -128
  store i8 %8, ptr %.01617, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.01617, i64 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = lshr i64 %.01518, 7
  %11 = icmp ugt i64 %.01518, 16383
  %12 = icmp ult i64 %indvars.iv.next, %1
  %13 = select i1 %11, i1 %12, i1 false
  br i1 %13, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !8

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %14 = trunc nuw nsw i64 %indvars.iv.next to i32
  br i1 %12, label %15, label %18

._crit_edge:                                      ; preds = %3
  br i1 %5, label %15, label %18

15:                                               ; preds = %._crit_edge.loopexit, %._crit_edge
  %.0.lcssa30 = phi i32 [ %14, %._crit_edge.loopexit ], [ 0, %._crit_edge ]
  %.015.lcssa29 = phi i64 [ %10, %._crit_edge.loopexit ], [ %2, %._crit_edge ]
  %.016.lcssa28 = phi ptr [ %9, %._crit_edge.loopexit ], [ %0, %._crit_edge ]
  %16 = trunc i64 %.015.lcssa29 to i8
  store i8 %16, ptr %.016.lcssa28, align 1, !tbaa !5
  %17 = add i32 %.0.lcssa30, 1
  br label %18

18:                                               ; preds = %._crit_edge.loopexit, %15, %._crit_edge
  %.1 = phi i32 [ %17, %15 ], [ 0, %._crit_edge ], [ %14, %._crit_edge.loopexit ]
  %19 = zext i32 %.1 to i64
  %.not = icmp ugt i64 %19, %1
  %20 = select i1 %.not, i32 -1, i32 %.1
  ret i32 %20
}

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


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