; 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.curve_info = type { ptr, ptr, i32, i32, i32, i32 }
%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@ed25519_info = external dso_local constant %struct.curve_info, align 8
@ed25519_sha3_info = external dso_local constant %struct.curve_info, align 8
@ed25519_keccak_info = external dso_local constant %struct.curve_info, align 8
@curve25519_info = external dso_local constant %struct.curve_info, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @hdnode_fill_public_key(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_sign(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6) #0 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  store ptr %0, ptr %9, align 8, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !5
  store i32 %2, ptr %11, align 4, !tbaa !9
  store i32 %3, ptr %12, align 4, !tbaa !9
  store ptr %4, ptr %13, align 8, !tbaa !5
  store ptr %5, ptr %14, align 8, !tbaa !5
  store ptr %6, ptr %15, align 8, !tbaa !5
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.HDNode, ptr %16, i32 0, i32 6
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %struct.curve_info, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %38

22:                                               ; preds = %7
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.HDNode, ptr %23, i32 0, i32 6
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = getelementptr inbounds %struct.curve_info, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = load i32, ptr %12, align 4, !tbaa !9
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.HDNode, ptr %29, i32 0, i32 3
  %31 = getelementptr inbounds [32 x i8], ptr %30, i64 0, i64 0
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = load ptr, ptr %13, align 8, !tbaa !5
  %35 = load ptr, ptr %14, align 8, !tbaa !5
  %36 = load ptr, ptr %15, align 8, !tbaa !5
  %37 = call i32 @ecdsa_sign(ptr noundef %27, i32 noundef %28, ptr noundef %31, ptr noundef %32, i32 noundef %33, ptr noundef %34, ptr noundef %35, ptr noundef %36)
  store i32 %37, ptr %8, align 4
  br label %99

38:                                               ; preds = %7
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.HDNode, ptr %39, i32 0, i32 6
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = icmp eq ptr %41, @curve25519_info
  br i1 %42, label %43, label %44

43:                                               ; preds = %38
  store i32 1, ptr %8, align 4
  br label %99

44:                                               ; preds = %38
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  call void @hdnode_fill_public_key(ptr noundef %45)
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.HDNode, ptr %46, i32 0, i32 6
  %48 = load ptr, ptr %47, align 8, !tbaa !11
  %49 = icmp eq ptr %48, @ed25519_info
  br i1 %49, label %50, label %62

50:                                               ; preds = %44
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = load i32, ptr %11, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.HDNode, ptr %54, i32 0, i32 3
  %56 = getelementptr inbounds [32 x i8], ptr %55, i64 0, i64 0
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.HDNode, ptr %57, i32 0, i32 5
  %59 = getelementptr inbounds [33 x i8], ptr %58, i64 0, i64 0
  %60 = getelementptr inbounds i8, ptr %59, i64 1
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  call void @ed25519_sign(ptr noundef %51, i64 noundef %53, ptr noundef %56, ptr noundef %60, ptr noundef %61)
  br label %98

62:                                               ; preds = %44
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.HDNode, ptr %63, i32 0, i32 6
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = icmp eq ptr %65, @ed25519_sha3_info
  br i1 %66, label %67, label %79

67:                                               ; preds = %62
  %68 = load ptr, ptr %10, align 8, !tbaa !5
  %69 = load i32, ptr %11, align 4, !tbaa !9
  %70 = zext i32 %69 to i64
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.HDNode, ptr %71, i32 0, i32 3
  %73 = getelementptr inbounds [32 x i8], ptr %72, i64 0, i64 0
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.HDNode, ptr %74, i32 0, i32 5
  %76 = getelementptr inbounds [33 x i8], ptr %75, i64 0, i64 0
  %77 = getelementptr inbounds i8, ptr %76, i64 1
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  call void @ed25519_sign_sha3(ptr noundef %68, i64 noundef %70, ptr noundef %73, ptr noundef %77, ptr noundef %78)
  br label %97

79:                                               ; preds = %62
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.HDNode, ptr %80, i32 0, i32 6
  %82 = load ptr, ptr %81, align 8, !tbaa !11
  %83 = icmp eq ptr %82, @ed25519_keccak_info
  br i1 %83, label %84, label %96

84:                                               ; preds = %79
  %85 = load ptr, ptr %10, align 8, !tbaa !5
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = zext i32 %86 to i64
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.HDNode, ptr %88, i32 0, i32 3
  %90 = getelementptr inbounds [32 x i8], ptr %89, i64 0, i64 0
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.HDNode, ptr %91, i32 0, i32 5
  %93 = getelementptr inbounds [33 x i8], ptr %92, i64 0, i64 0
  %94 = getelementptr inbounds i8, ptr %93, i64 1
  %95 = load ptr, ptr %13, align 8, !tbaa !5
  call void @ed25519_sign_keccak(ptr noundef %85, i64 noundef %87, ptr noundef %90, ptr noundef %94, ptr noundef %95)
  br label %96

96:                                               ; preds = %84, %79
  br label %97

97:                                               ; preds = %96, %67
  br label %98

98:                                               ; preds = %97, %50
  store i32 0, ptr %8, align 4
  br label %99

99:                                               ; preds = %98, %43, %22
  %100 = load i32, ptr %8, align 4
  ret i32 %100
}

declare i32 @ecdsa_sign(ptr noundef, i32 noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare void @ed25519_sign(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare void @ed25519_sign_sha3(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare void @ed25519_sign_keccak(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) #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 = { "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 = !{!"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 = !{!12, !6, i64 144}
!12 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 104, !6, i64 144}
!13 = !{!14, !6, i64 8}
!14 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}


; 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.curve_info = type { ptr, ptr, i32, i32, i32, i32 }

@ed25519_info = external dso_local constant %struct.curve_info, align 8
@ed25519_sha3_info = external dso_local constant %struct.curve_info, align 8
@ed25519_keccak_info = external dso_local constant %struct.curve_info, align 8
@curve25519_info = external dso_local constant %struct.curve_info, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @hdnode_fill_public_key(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_sign(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6) local_unnamed_addr #0 {
  %8 = getelementptr inbounds i8, ptr %0, i64 144
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %15, label %12

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  %14 = tail call i32 @ecdsa_sign(ptr noundef nonnull %11, i32 noundef %3, ptr noundef nonnull %13, ptr noundef %1, i32 noundef %2, ptr noundef %4, ptr noundef %5, ptr noundef %6) #2
  br label %36

15:                                               ; preds = %7
  %16 = icmp eq ptr %9, @curve25519_info
  br i1 %16, label %36, label %17

17:                                               ; preds = %15
  tail call void @hdnode_fill_public_key(ptr noundef nonnull %0)
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp eq ptr %18, @ed25519_info
  br i1 %19, label %20, label %24

20:                                               ; preds = %17
  %21 = zext i32 %2 to i64
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  %23 = getelementptr inbounds i8, ptr %0, i64 105
  tail call void @ed25519_sign(ptr noundef %1, i64 noundef %21, ptr noundef nonnull %22, ptr noundef nonnull %23, ptr noundef %4) #2
  br label %36

24:                                               ; preds = %17
  %25 = icmp eq ptr %18, @ed25519_sha3_info
  br i1 %25, label %26, label %30

26:                                               ; preds = %24
  %27 = zext i32 %2 to i64
  %28 = getelementptr inbounds i8, ptr %0, i64 40
  %29 = getelementptr inbounds i8, ptr %0, i64 105
  tail call void @ed25519_sign_sha3(ptr noundef %1, i64 noundef %27, ptr noundef nonnull %28, ptr noundef nonnull %29, ptr noundef %4) #2
  br label %36

30:                                               ; preds = %24
  %31 = icmp eq ptr %18, @ed25519_keccak_info
  br i1 %31, label %32, label %36

32:                                               ; preds = %30
  %33 = zext i32 %2 to i64
  %34 = getelementptr inbounds i8, ptr %0, i64 40
  %35 = getelementptr inbounds i8, ptr %0, i64 105
  tail call void @ed25519_sign_keccak(ptr noundef %1, i64 noundef %33, ptr noundef nonnull %34, ptr noundef nonnull %35, ptr noundef %4) #2
  br label %36

36:                                               ; preds = %20, %30, %32, %26, %15, %12
  %.0 = phi i32 [ %14, %12 ], [ 1, %15 ], [ 0, %26 ], [ 0, %32 ], [ 0, %30 ], [ 0, %20 ]
  ret i32 %.0
}

declare i32 @ecdsa_sign(ptr noundef, i32 noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @ed25519_sign(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @ed25519_sign_sha3(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @ed25519_sign_keccak(ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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 = { "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, !10, i64 144}
!6 = !{!"", !7, i64 0, !7, i64 4, !8, i64 8, !8, i64 40, !8, i64 72, !8, i64 104, !10, i64 144}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass