; LLVM IR from machinefi/psa-crypto-esp32 :: crypto (ecc_dh)

; 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.uECC_Curve_t = type { i8, i8, i16, [8 x i32], [8 x i32], [16 x i32], [8 x i32], ptr, ptr, ptr }

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

declare void @uECC_vli_nativeToBytes(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_set_secure(ptr noundef, i8 noundef zeroext, i32 noundef) #2

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

declare ptr @uECC_get_rng() #1

; Function Attrs: nounwind uwtable
define dso_local i32 @uECC_shared_secret(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca [16 x i32], align 16
  %10 = alloca [8 x i32], align 16
  %11 = alloca [8 x i32], align 16
  %12 = alloca [2 x ptr], align 16
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #4
  %18 = getelementptr inbounds [8 x i32], ptr %10, i64 0, i64 0
  store ptr %18, ptr %12, align 8, !tbaa !5
  %19 = getelementptr inbounds ptr, ptr %12, i64 1
  %20 = getelementptr inbounds [8 x i32], ptr %11, i64 0, i64 0
  store ptr %20, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #4
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.uECC_Curve_t, ptr %21, i32 0, i32 0
  %23 = load i8, ptr %22, align 8, !tbaa !9
  store i8 %23, ptr %15, align 1, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.uECC_Curve_t, ptr %24, i32 0, i32 1
  %26 = load i8, ptr %25, align 1, !tbaa !13
  store i8 %26, ptr %16, align 1, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %27 = getelementptr inbounds [8 x i32], ptr %10, i64 0, i64 0
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.uECC_Curve_t, ptr %29, i32 0, i32 2
  %31 = load i16, ptr %30, align 2, !tbaa !14
  %32 = sext i16 %31 to i32
  %33 = add nsw i32 %32, 7
  %34 = sdiv i32 %33, 8
  call void @uECC_vli_bytesToNative(ptr noundef %27, ptr noundef %28, i32 noundef %34)
  %35 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i8, ptr %16, align 1, !tbaa !12
  %38 = sext i8 %37 to i32
  call void @uECC_vli_bytesToNative(ptr noundef %35, ptr noundef %36, i32 noundef %38)
  %39 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  %40 = load i8, ptr %15, align 1, !tbaa !12
  %41 = sext i8 %40 to i32
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %39, i64 %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i8, ptr %16, align 1, !tbaa !12
  %46 = sext i8 %45 to i32
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %44, i64 %47
  %49 = load i8, ptr %16, align 1, !tbaa !12
  %50 = sext i8 %49 to i32
  call void @uECC_vli_bytesToNative(ptr noundef %43, ptr noundef %48, i32 noundef %50)
  %51 = getelementptr inbounds [8 x i32], ptr %10, i64 0, i64 0
  %52 = getelementptr inbounds [8 x i32], ptr %10, i64 0, i64 0
  %53 = getelementptr inbounds [8 x i32], ptr %11, i64 0, i64 0
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = call i32 @regularize_k(ptr noundef %51, ptr noundef %52, ptr noundef %53, ptr noundef %54)
  store i32 %55, ptr %14, align 4, !tbaa !15
  %56 = call ptr @uECC_get_rng()
  %57 = icmp ne ptr %56, null
  br i1 %57, label %58, label %75

58:                                               ; preds = %4
  %59 = load i32, ptr %14, align 4, !tbaa !15
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds [2 x ptr], ptr %12, i64 0, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.uECC_Curve_t, ptr %63, i32 0, i32 3
  %65 = getelementptr inbounds [8 x i32], ptr %64, i64 0, i64 0
  %66 = load i8, ptr %15, align 1, !tbaa !12
  %67 = call i32 @uECC_generate_random_int(ptr noundef %62, ptr noundef %65, i8 noundef signext %66)
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %70, label %69

69:                                               ; preds = %58
  store i32 0, ptr %17, align 4, !tbaa !15
  br label %103

70:                                               ; preds = %58
  %71 = load i32, ptr %14, align 4, !tbaa !15
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds [2 x ptr], ptr %12, i64 0, i64 %72
  %74 = load ptr, ptr %73, align 8, !tbaa !5
  store ptr %74, ptr %13, align 8, !tbaa !5
  br label %75

75:                                               ; preds = %70, %4
  %76 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  %77 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  %78 = load i32, ptr %14, align 4, !tbaa !15
  %79 = icmp ne i32 %78, 0
  %80 = xor i1 %79, true
  %81 = zext i1 %80 to i32
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [2 x ptr], ptr %12, i64 0, i64 %82
  %84 = load ptr, ptr %83, align 8, !tbaa !5
  %85 = load ptr, ptr %13, align 8, !tbaa !5
  %86 = load ptr, ptr %8, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.uECC_Curve_t, ptr %86, i32 0, i32 2
  %88 = load i16, ptr %87, align 2, !tbaa !14
  %89 = sext i16 %88 to i32
  %90 = add nsw i32 %89, 1
  %91 = trunc i32 %90 to i16
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  call void @EccPoint_mult(ptr noundef %76, ptr noundef %77, ptr noundef %84, ptr noundef %85, i16 noundef signext %91, ptr noundef %92)
  %93 = load ptr, ptr %7, align 8, !tbaa !5
  %94 = load i8, ptr %16, align 1, !tbaa !12
  %95 = sext i8 %94 to i32
  %96 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  call void @uECC_vli_nativeToBytes(ptr noundef %93, i32 noundef %95, ptr noundef %96)
  %97 = getelementptr inbounds [16 x i32], ptr %9, i64 0, i64 0
  %98 = load ptr, ptr %8, align 8, !tbaa !5
  %99 = call i32 @EccPoint_isZero(ptr noundef %97, ptr noundef %98)
  %100 = icmp ne i32 %99, 0
  %101 = xor i1 %100, true
  %102 = zext i1 %101 to i32
  store i32 %102, ptr %17, align 4, !tbaa !15
  br label %103

103:                                              ; preds = %75, %69
  %104 = getelementptr inbounds [2 x ptr], ptr %12, i64 0, i64 0
  call void @_set_secure(ptr noundef %104, i8 noundef zeroext 0, i32 noundef 16)
  %105 = getelementptr inbounds [8 x i32], ptr %11, i64 0, i64 0
  call void @_set_secure(ptr noundef %105, i8 noundef zeroext 0, i32 noundef 32)
  %106 = getelementptr inbounds [8 x i32], ptr %10, i64 0, i64 0
  call void @_set_secure(ptr noundef %106, i8 noundef zeroext 0, i32 noundef 32)
  %107 = load i32, ptr %17, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %9) #4
  ret i32 %107
}

declare void @uECC_vli_bytesToNative(ptr noundef, ptr noundef, i32 noundef) #1

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

declare i32 @uECC_generate_random_int(ptr noundef, ptr noundef, i8 noundef signext) #1

declare void @EccPoint_mult(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i16 noundef signext, ptr noundef) #1

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 #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, !7, i64 0}
!10 = !{!"uECC_Curve_t", !7, i64 0, !7, i64 1, !11, i64 2, !7, i64 4, !7, i64 36, !7, i64 68, !7, i64 132, !6, i64 168, !6, i64 176, !6, i64 184}
!11 = !{!"short", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!10, !7, i64 1}
!14 = !{!10, !11, i64 2}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !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"

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

declare void @uECC_vli_nativeToBytes(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_set_secure(ptr noundef, i8 noundef zeroext, 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) #0

declare ptr @uECC_get_rng() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @uECC_shared_secret(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #3 {
  %5 = alloca [16 x i32], align 16
  %6 = alloca [8 x i32], align 16
  %7 = alloca [8 x i32], align 16
  %8 = alloca [2 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %8) #4
  store ptr %6, ptr %8, align 16, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr %7, ptr %9, align 8, !tbaa !5
  %10 = load i8, ptr %3, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %3, i64 1
  %12 = load i8, ptr %11, align 1, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %3, i64 2
  %14 = load i16, ptr %13, align 2, !tbaa !13
  %15 = sext i16 %14 to i32
  %16 = add nsw i32 %15, 7
  %17 = sdiv i32 %16, 8
  call void @uECC_vli_bytesToNative(ptr noundef nonnull %6, ptr noundef %1, i32 noundef %17) #4
  %18 = sext i8 %12 to i32
  call void @uECC_vli_bytesToNative(ptr noundef nonnull %5, ptr noundef %0, i32 noundef %18) #4
  %19 = sext i8 %10 to i64
  %20 = getelementptr inbounds i32, ptr %5, i64 %19
  %21 = sext i8 %12 to i64
  %22 = getelementptr inbounds i8, ptr %0, i64 %21
  call void @uECC_vli_bytesToNative(ptr noundef nonnull %20, ptr noundef %22, i32 noundef %18) #4
  %23 = call i32 @regularize_k(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %7, ptr noundef nonnull %3) #4
  %24 = call ptr @uECC_get_rng() #4
  %.not = icmp eq ptr %24, null
  br i1 %.not, label %31, label %25

25:                                               ; preds = %4
  %26 = zext i32 %23 to i64
  %27 = getelementptr inbounds [2 x ptr], ptr %8, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %3, i64 4
  %30 = call i32 @uECC_generate_random_int(ptr noundef %28, ptr noundef nonnull %29, i8 noundef signext %10) #4
  %.not23 = icmp eq i32 %30, 0
  br i1 %.not23, label %39, label %31

31:                                               ; preds = %25, %4
  %.022 = phi ptr [ null, %4 ], [ %28, %25 ]
  %.not24 = icmp eq i32 %23, 0
  %32 = zext i1 %.not24 to i64
  %33 = getelementptr inbounds [2 x ptr], ptr %8, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = load i16, ptr %13, align 2, !tbaa !13
  %36 = add i16 %35, 1
  call void @EccPoint_mult(ptr noundef nonnull %5, ptr noundef nonnull %5, ptr noundef %34, ptr noundef %.022, i16 noundef signext %36, ptr noundef nonnull %3) #4
  call void @uECC_vli_nativeToBytes(ptr noundef %2, i32 noundef %18, ptr noundef nonnull %5) #4
  %37 = call i32 @EccPoint_isZero(ptr noundef nonnull %5, ptr noundef nonnull %3) #4
  %.not25 = icmp eq i32 %37, 0
  %38 = zext i1 %.not25 to i32
  br label %39

39:                                               ; preds = %25, %31
  %.0 = phi i32 [ %38, %31 ], [ 0, %25 ]
  call void @_set_secure(ptr noundef nonnull %8, i8 noundef zeroext 0, i32 noundef 16)
  call void @_set_secure(ptr noundef nonnull %7, i8 noundef zeroext 0, i32 noundef 32)
  call void @_set_secure(ptr noundef nonnull %6, i8 noundef zeroext 0, i32 noundef 32)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #4
  ret i32 %.0
}

declare void @uECC_vli_bytesToNative(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

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

declare i32 @uECC_generate_random_int(ptr noundef, ptr noundef, i8 noundef signext) local_unnamed_addr #1

declare void @EccPoint_mult(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i16 noundef signext, ptr noundef) local_unnamed_addr #1

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 #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, !7, i64 0}
!10 = !{!"uECC_Curve_t", !7, i64 0, !7, i64 1, !11, i64 2, !7, i64 4, !7, i64 36, !7, i64 68, !7, i64 132, !6, i64 168, !6, i64 176, !6, i64 184}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !7, i64 1}
!13 = !{!10, !11, i64 2}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass