; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto/tools (xpubaddrgen)

; 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"

%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@SECP256K1_NAME = external constant [0 x i8], align 1
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @process_job(i32 noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.HDNode, align 8
  %12 = alloca %struct.HDNode, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca [36 x i8], align 16
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 152, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 152, ptr %12) #4
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = icmp ugt i32 %16, 1
  br i1 %17, label %26, label %18

18:                                               ; preds = %5
  %19 = load i32, ptr %10, align 4, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = icmp ule i32 %19, %20
  br i1 %21, label %26, label %22

22:                                               ; preds = %18
  %23 = load ptr, ptr %7, align 8, !tbaa !9
  %24 = call i32 @hdnode_deserialize_public(ptr noundef %23, i32 noundef 76067358, ptr noundef @SECP256K1_NAME, ptr noundef %11, ptr noundef null)
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %29

26:                                               ; preds = %22, %18, %5
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %27)
  store i32 1, ptr %13, align 4
  br label %51

29:                                               ; preds = %22
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = call i32 @hdnode_public_ckd(ptr noundef %11, i32 noundef %30)
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr %15) #4
  %32 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %32, ptr %14, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %47, %29
  %34 = load i32, ptr %14, align 4, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !5
  %36 = icmp ult i32 %34, %35
  br i1 %36, label %37, label %50

37:                                               ; preds = %33
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %12, ptr align 8 %11, i64 152, i1 false)
  %38 = load i32, ptr %14, align 4, !tbaa !5
  %39 = call i32 @hdnode_public_ckd(ptr noundef %12, i32 noundef %38)
  %40 = getelementptr inbounds %struct.HDNode, ptr %12, i32 0, i32 5
  %41 = getelementptr inbounds [33 x i8], ptr %40, i64 0, i64 0
  %42 = getelementptr inbounds [36 x i8], ptr %15, i64 0, i64 0
  call void @ecdsa_get_address(ptr noundef %41, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef %42, i32 noundef 36)
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = load i32, ptr %14, align 4, !tbaa !5
  %45 = getelementptr inbounds [36 x i8], ptr %15, i64 0, i64 0
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %43, i32 noundef %44, ptr noundef %45)
  br label %47

47:                                               ; preds = %37
  %48 = load i32, ptr %14, align 4, !tbaa !5
  %49 = add i32 %48, 1
  store i32 %49, ptr %14, align 4, !tbaa !5
  br label %33, !llvm.loop !11

50:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 36, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  store i32 0, ptr %13, align 4
  br label %51

51:                                               ; preds = %50, %26
  call void @llvm.lifetime.end.p0(i64 152, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 152, ptr %11) #4
  %52 = load i32, ptr %13, align 4
  switch i32 %52, label %54 [
    i32 0, label %53
    i32 1, label %53
  ]

53:                                               ; preds = %51, %51
  ret void

54:                                               ; preds = %51
  unreachable
}

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

declare i32 @hdnode_deserialize_public(ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @printf(ptr noundef, ...) #2

declare i32 @hdnode_public_ckd(ptr noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

declare void @ecdsa_get_address(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) #2

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

attributes #0 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@SECP256K1_NAME = external constant [0 x i8], align 1
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @process_job(i32 noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca %struct.HDNode, align 8
  %7 = alloca %struct.HDNode, align 8
  %8 = alloca [36 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %7) #5
  %9 = icmp ult i32 %2, 2
  %.not = icmp ugt i32 %4, %3
  %or.cond = and i1 %9, %.not
  br i1 %or.cond, label %10, label %12

10:                                               ; preds = %5
  %11 = call i32 @hdnode_deserialize_public(ptr noundef %1, i32 noundef 76067358, ptr noundef nonnull @SECP256K1_NAME, ptr noundef nonnull %6, ptr noundef null) #5
  %.not15 = icmp eq i32 %11, 0
  br i1 %.not15, label %.lr.ph, label %12

12:                                               ; preds = %10, %5
  %13 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %0)
  br label %20

.lr.ph:                                           ; preds = %10
  %14 = call i32 @hdnode_public_ckd(ptr noundef nonnull %6, i32 noundef %2) #5
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %8) #5
  %15 = getelementptr inbounds i8, ptr %7, i64 104
  br label %16

16:                                               ; preds = %.lr.ph, %16
  %.016 = phi i32 [ %3, %.lr.ph ], [ %19, %16 ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(152) %7, ptr noundef nonnull align 8 dereferenceable(152) %6, i64 152, i1 false)
  %17 = call i32 @hdnode_public_ckd(ptr noundef nonnull %7, i32 noundef %.016) #5
  call void @ecdsa_get_address(ptr noundef nonnull %15, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef nonnull %8, i32 noundef 36) #5
  %18 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %0, i32 noundef %.016, ptr noundef nonnull %8)
  %19 = add i32 %.016, 1
  %exitcond.not = icmp eq i32 %19, %4
  br i1 %exitcond.not, label %._crit_edge, label %16, !llvm.loop !5

._crit_edge:                                      ; preds = %16
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %8) #5
  br label %20

20:                                               ; preds = %._crit_edge, %12
  call void @llvm.lifetime.end.p0(i64 152, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 152, ptr nonnull %6) #5
  ret void
}

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

declare i32 @hdnode_deserialize_public(ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

declare i32 @hdnode_public_ckd(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

declare void @ecdsa_get_address(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

attributes #0 = { 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 #1 = { mustprogress 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 = { nofree 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,SCCPPass,SROAPass,SimplifyCFGPass