; LLVM IR from square/subzero :: core/trezor-crypto (bip32)

; 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.bignum256 = type { [9 x i32] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }
%struct.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @memzero(ptr noundef, i64 noundef) #2

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

declare void @bn_read_be(ptr noundef, ptr noundef) #2

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

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

declare void @bn_write_be(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_public_ckd_cp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) #3 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca [37 x i8], align 16
  %15 = alloca [64 x i8], align 16
  %16 = alloca %struct.bignum256, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !9
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 37, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr %16) #4
  %18 = load i32, ptr %11, align 4, !tbaa !9
  %19 = and i32 %18, -2147483648
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %75

22:                                               ; preds = %6
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.curve_point, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds %struct.bignum256, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds [9 x i32], ptr %25, i64 0, i64 0
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = and i32 %27, 1
  %29 = or i32 2, %28
  %30 = trunc i32 %29 to i8
  %31 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  store i8 %30, ptr %31, align 16, !tbaa !11
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.curve_point, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %35 = getelementptr inbounds i8, ptr %34, i64 1
  call void @bn_write_be(ptr noundef %33, ptr noundef %35)
  %36 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %37 = getelementptr inbounds i8, ptr %36, i64 33
  %38 = load i32, ptr %11, align 4, !tbaa !9
  call void @write_be(ptr noundef %37, i32 noundef %38)
  br label %39

39:                                               ; preds = %69, %22
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %43 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @hmac_sha512(ptr noundef %41, i32 noundef 32, ptr noundef %42, i32 noundef 37, ptr noundef %43)
  %44 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @bn_read_be(ptr noundef %44, ptr noundef %16)
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ecdsa_curve, ptr %45, i32 0, i32 2
  %47 = call i32 @bn_is_less(ptr noundef %16, ptr noundef %46)
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %69

49:                                               ; preds = %40
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  call void @scalar_multiply(ptr noundef %50, ptr noundef %16, ptr noundef %51)
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = load ptr, ptr %12, align 8, !tbaa !5
  call void @point_add(ptr noundef %52, ptr noundef %53, ptr noundef %54)
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  %56 = call i32 @point_is_infinity(ptr noundef %55)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %68, label %58

58:                                               ; preds = %49
  %59 = load ptr, ptr %13, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %65

61:                                               ; preds = %58
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  %64 = getelementptr inbounds i8, ptr %63, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %62, ptr align 1 %64, i64 32, i1 false)
  br label %65

65:                                               ; preds = %61, %58
  %66 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  call void @memzero(ptr noundef %66, i64 noundef 37)
  %67 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @memzero(ptr noundef %67, i64 noundef 64)
  call void @memzero(ptr noundef %16, i64 noundef 36)
  store i32 1, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %75

68:                                               ; preds = %49
  br label %69

69:                                               ; preds = %68, %40
  %70 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  store i8 1, ptr %70, align 16, !tbaa !11
  %71 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %72 = getelementptr inbounds i8, ptr %71, i64 1
  %73 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  %74 = getelementptr inbounds i8, ptr %73, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %72, ptr align 1 %74, i64 32, i1 false)
  br label %39

75:                                               ; preds = %65, %21
  call void @llvm.lifetime.end.p0(i64 36, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 37, ptr %14) #4
  %76 = load i32, ptr %7, align 4
  ret i32 %76
}

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

declare void @scalar_multiply(ptr noundef, ptr noundef, ptr noundef) #2

declare void @point_add(ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @point_is_infinity(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}


; 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.bignum256 = type { [9 x i32] }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @memzero(ptr noundef, i64 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) #0

declare void @bn_read_be(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

declare void @bn_write_be(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_public_ckd_cp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef writeonly %5) local_unnamed_addr #3 {
  %7 = alloca [37 x i8], align 16
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 37, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %8) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #4
  %.not = icmp sgt i32 %3, -1
  br i1 %.not, label %10, label %28

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %1, i64 36
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = trunc i32 %12 to i8
  %14 = and i8 %13, 1
  %15 = or disjoint i8 %14, 2
  store i8 %15, ptr %7, align 16, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %7, i64 1
  call void @bn_write_be(ptr noundef %1, ptr noundef nonnull %16) #4
  %17 = getelementptr inbounds i8, ptr %7, i64 33
  call void @write_be(ptr noundef nonnull %17, i32 noundef %3) #4
  %18 = getelementptr inbounds i8, ptr %0, i64 108
  %19 = getelementptr inbounds i8, ptr %8, i64 32
  br label %20

20:                                               ; preds = %27, %10
  call void @hmac_sha512(ptr noundef %2, i32 noundef 32, ptr noundef nonnull %7, i32 noundef 37, ptr noundef nonnull %8) #4
  call void @bn_read_be(ptr noundef nonnull %8, ptr noundef nonnull %9) #4
  %21 = call i32 @bn_is_less(ptr noundef nonnull %9, ptr noundef nonnull %18) #4
  %.not15 = icmp eq i32 %21, 0
  br i1 %.not15, label %27, label %22

22:                                               ; preds = %20
  call void @scalar_multiply(ptr noundef %0, ptr noundef nonnull %9, ptr noundef %4) #4
  call void @point_add(ptr noundef %0, ptr noundef %1, ptr noundef %4) #4
  %23 = call i32 @point_is_infinity(ptr noundef %4) #4
  %.not16 = icmp eq i32 %23, 0
  br i1 %.not16, label %24, label %27

24:                                               ; preds = %22
  %.not17 = icmp eq ptr %5, null
  br i1 %.not17, label %26, label %25

25:                                               ; preds = %24
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %5, ptr noundef nonnull align 16 dereferenceable(32) %19, i64 32, i1 false)
  br label %26

26:                                               ; preds = %25, %24
  call void @memzero(ptr noundef nonnull %7, i64 noundef 37) #4
  call void @memzero(ptr noundef nonnull %8, i64 noundef 64) #4
  call void @memzero(ptr noundef nonnull %9, i64 noundef 36) #4
  br label %28

27:                                               ; preds = %22, %20
  store i8 1, ptr %7, align 16, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %16, ptr noundef nonnull align 16 dereferenceable(32) %19, i64 32, i1 false)
  br label %20

28:                                               ; preds = %6, %26
  %.0 = phi i32 [ 1, %26 ], [ 0, %6 ]
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 37, ptr nonnull %7) #4
  ret i32 %.0
}

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

declare void @scalar_multiply(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @point_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @point_is_infinity(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree 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 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 = { 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 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass