; LLVM IR from trustwallet/wallet-core :: src/interface (TWString+Hex)

; 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: mustprogress uwtable
define dso_local ptr @TWStringCreateWithHexData(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #6
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = call i64 @TWDataSize(ptr noundef %7)
  %9 = mul i64 %8, 2
  store i64 %9, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %10 = load i64, ptr %3, align 8, !tbaa !9
  %11 = add i64 %10, 1
  %12 = call noalias ptr @malloc(i64 noundef %11) #7
  store ptr %12, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i64, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %13, i64 %14
  store i8 0, ptr %15, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %40, %1
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = call i64 @TWDataSize(ptr noundef %18)
  %20 = icmp ult i64 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %43

22:                                               ; preds = %16
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !9
  %25 = call zeroext i8 @TWDataGet(ptr noundef %23, i64 noundef %24)
  %26 = call noundef signext i8 @_ZL9high_charh(i8 noundef zeroext %25)
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !9
  %29 = mul i64 2, %28
  %30 = getelementptr inbounds i8, ptr %27, i64 %29
  store i8 %26, ptr %30, align 1, !tbaa !11
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = call zeroext i8 @TWDataGet(ptr noundef %31, i64 noundef %32)
  %34 = call noundef signext i8 @_ZL8low_charh(i8 noundef zeroext %33)
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i64, ptr %5, align 8, !tbaa !9
  %37 = mul i64 2, %36
  %38 = add i64 %37, 1
  %39 = getelementptr inbounds i8, ptr %35, i64 %38
  store i8 %34, ptr %39, align 1, !tbaa !11
  br label %40

40:                                               ; preds = %22
  %41 = load i64, ptr %5, align 8, !tbaa !9
  %42 = add i64 %41, 1
  store i64 %42, ptr %5, align 8, !tbaa !9
  br label %16, !llvm.loop !12

43:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = call ptr @TWStringCreateWithUTF8Bytes(ptr noundef %44)
  store ptr %45, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %46) #6
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #6
  ret ptr %47
}

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

declare i64 @TWDataSize(ptr noundef) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef signext i8 @_ZL9high_charh(i8 noundef zeroext) #4

declare zeroext i8 @TWDataGet(ptr noundef, i64 noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef signext i8 @_ZL8low_charh(i8 noundef zeroext) #4

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

declare ptr @TWStringCreateWithUTF8Bytes(ptr noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #5

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 allocsize(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 #4 = { inlinehint 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 #5 = { nounwind "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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 = !{!"long", !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 uwtable
define dso_local ptr @TWStringCreateWithHexData(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i64 @TWDataSize(ptr noundef %0)
  %3 = shl i64 %2, 1
  %4 = or disjoint i64 %3, 1
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #5
  %6 = getelementptr inbounds i8, ptr %5, i64 %3
  store i8 0, ptr %6, align 1, !tbaa !5
  %7 = tail call i64 @TWDataSize(ptr noundef %0)
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  %8 = tail call ptr @TWStringCreateWithUTF8Bytes(ptr noundef nonnull %5)
  tail call void @free(ptr noundef nonnull %5) #6
  ret ptr %8

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.017 = phi i64 [ %17, %.lr.ph ], [ 0, %1 ]
  %9 = tail call zeroext i8 @TWDataGet(ptr noundef %0, i64 noundef %.017)
  %10 = tail call noundef signext i8 @_ZL9high_charh(i8 noundef zeroext %9)
  %11 = shl i64 %.017, 1
  %12 = getelementptr inbounds i8, ptr %5, i64 %11
  store i8 %10, ptr %12, align 1, !tbaa !5
  %13 = tail call zeroext i8 @TWDataGet(ptr noundef %0, i64 noundef %.017)
  %14 = tail call noundef signext i8 @_ZL8low_charh(i8 noundef zeroext %13)
  %15 = or disjoint i64 %11, 1
  %16 = getelementptr inbounds i8, ptr %5, i64 %15
  store i8 %14, ptr %16, align 1, !tbaa !5
  %17 = add nuw i64 %.017, 1
  %18 = tail call i64 @TWDataSize(ptr noundef %0)
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %.lr.ph, label %._crit_edge, !llvm.loop !8
}

declare i64 @TWDataSize(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef signext i8 @_ZL9high_charh(i8 noundef zeroext) local_unnamed_addr #3

declare zeroext i8 @TWDataGet(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef signext i8 @_ZL8low_charh(i8 noundef zeroext) local_unnamed_addr #3

declare ptr @TWStringCreateWithUTF8Bytes(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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" }
attributes #1 = { "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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { inlinehint 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 #4 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #5 = { nounwind allocsize(0) }
attributes #6 = { 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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass