; LLVM IR from IOsonata/IOsonata :: micro-ecc (uECC)

; 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, [4 x i64], [4 x i64], [8 x i64], [4 x i64], ptr, ptr, ptr, ptr }

@g_rng_function = external hidden global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare hidden i32 @uECC_generate_random_int(ptr noundef, ptr noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare hidden void @uECC_vli_nativeToBytes(ptr noundef, i32 noundef, ptr noundef) #1

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

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

59:                                               ; preds = %4
  %60 = load i64, ptr %15, align 8, !tbaa !15
  %61 = getelementptr inbounds [2 x ptr], ptr %13, i64 0, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.uECC_Curve_t, ptr %63, i32 0, i32 3
  %65 = getelementptr inbounds [4 x i64], ptr %64, i64 0, i64 0
  %66 = load i8, ptr %16, 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 = %59
  store i32 0, ptr %5, align 4
  store i32 1, ptr %18, align 4
  br label %107

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

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

107:                                              ; preds = %74, %69
  call void @llvm.lifetime.end.p0(i64 1, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #2
  %108 = load i32, ptr %5, align 4
  ret i32 %108
}

; Function Attrs: nounwind uwtable
declare hidden void @uECC_vli_bytesToNative(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @regularize_k(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @EccPoint_mult(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i16 noundef signext, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @uECC_vli_isZero(ptr noundef, i8 noundef signext) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 8, !7, i64 40, !7, i64 72, !7, i64 136, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192}
!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 = !{!"long", !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"

@g_rng_function = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare hidden i32 @uECC_generate_random_int(ptr noundef, ptr noundef, i8 noundef signext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @uECC_vli_nativeToBytes(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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

; 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 #1 {
  %5 = alloca [8 x i64], align 16
  %6 = alloca [4 x i64], align 16
  %7 = alloca [4 x i64], align 16
  %8 = alloca [2 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #2
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #2
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #2
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %8) #2
  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)
  %18 = sext i8 %12 to i32
  call void @uECC_vli_bytesToNative(ptr noundef nonnull %5, ptr noundef %0, i32 noundef %18)
  %19 = sext i8 %10 to i64
  %20 = getelementptr inbounds i64, 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)
  %23 = call i64 @regularize_k(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %7, ptr noundef nonnull %3)
  %24 = load ptr, ptr @g_rng_function, align 8, !tbaa !5
  %.not = icmp eq ptr %24, null
  br i1 %.not, label %30, label %25

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

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

40:                                               ; preds = %25, %30
  %.0 = phi i32 [ %39, %30 ], [ 0, %25 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %8) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #2
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @uECC_vli_bytesToNative(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i64 @regularize_k(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @EccPoint_mult(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i16 noundef signext, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i64 @uECC_vli_isZero(ptr noundef, i8 noundef signext) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 8, !7, i64 40, !7, i64 72, !7, i64 136, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !7, i64 1}
!13 = !{!10, !11, i64 2}


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