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

; Function Attrs: nounwind uwtable
define dso_local i32 @uECC_make_key(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [4 x i64], align 16
  %9 = alloca [8 x i64], align 16
  %10 = alloca i64, align 8
  %11 = 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
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 64, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %70, %3
  %13 = load i64, ptr %10, align 8, !tbaa !9
  %14 = icmp ult i64 %13, 64
  br i1 %14, label %15, label %73

15:                                               ; preds = %12
  %16 = getelementptr inbounds [4 x i64], ptr %8, i64 0, i64 0
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.uECC_Curve_t, ptr %17, i32 0, i32 4
  %19 = getelementptr inbounds [4 x i64], ptr %18, i64 0, i64 0
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.uECC_Curve_t, ptr %20, i32 0, i32 2
  %22 = load i16, ptr %21, align 2, !tbaa !11
  %23 = sext i16 %22 to i32
  %24 = add nsw i32 %23, 63
  %25 = sdiv i32 %24, 64
  %26 = trunc i32 %25 to i8
  %27 = call i32 @uECC_generate_random_int(ptr noundef %16, ptr noundef %19, i8 noundef signext %26)
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %30, label %29

29:                                               ; preds = %15
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %74

30:                                               ; preds = %15
  %31 = getelementptr inbounds [8 x i64], ptr %9, i64 0, i64 0
  %32 = getelementptr inbounds [4 x i64], ptr %8, i64 0, i64 0
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = call i64 @EccPoint_compute_public_key(ptr noundef %31, ptr noundef %32, ptr noundef %33)
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %69

36:                                               ; preds = %30
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.uECC_Curve_t, ptr %38, i32 0, i32 2
  %40 = load i16, ptr %39, align 2, !tbaa !11
  %41 = sext i16 %40 to i32
  %42 = add nsw i32 %41, 7
  %43 = sdiv i32 %42, 8
  %44 = getelementptr inbounds [4 x i64], ptr %8, i64 0, i64 0
  call void @uECC_vli_nativeToBytes(ptr noundef %37, i32 noundef %43, ptr noundef %44)
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.uECC_Curve_t, ptr %46, i32 0, i32 1
  %48 = load i8, ptr %47, align 1, !tbaa !14
  %49 = sext i8 %48 to i32
  %50 = getelementptr inbounds [8 x i64], ptr %9, i64 0, i64 0
  call void @uECC_vli_nativeToBytes(ptr noundef %45, i32 noundef %49, ptr noundef %50)
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.uECC_Curve_t, ptr %52, i32 0, i32 1
  %54 = load i8, ptr %53, align 1, !tbaa !14
  %55 = sext i8 %54 to i32
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %51, i64 %56
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.uECC_Curve_t, ptr %58, i32 0, i32 1
  %60 = load i8, ptr %59, align 1, !tbaa !14
  %61 = sext i8 %60 to i32
  %62 = getelementptr inbounds [8 x i64], ptr %9, i64 0, i64 0
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.uECC_Curve_t, ptr %63, i32 0, i32 0
  %65 = load i8, ptr %64, align 8, !tbaa !15
  %66 = sext i8 %65 to i32
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i64, ptr %62, i64 %67
  call void @uECC_vli_nativeToBytes(ptr noundef %57, i32 noundef %61, ptr noundef %68)
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %74

69:                                               ; preds = %30
  br label %70

70:                                               ; preds = %69
  %71 = load i64, ptr %10, align 8, !tbaa !9
  %72 = add i64 %71, 1
  store i64 %72, ptr %10, align 8, !tbaa !9
  br label %12, !llvm.loop !16

73:                                               ; preds = %12
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %74

74:                                               ; preds = %73, %36, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #2
  %75 = load i32, ptr %4, align 4
  ret i32 %75
}

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

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

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

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

; 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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 2}
!12 = !{!"uECC_Curve_t", !7, i64 0, !7, i64 1, !13, 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}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !7, i64 1}
!15 = !{!12, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @uECC_make_key(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca [4 x i64], align 16
  %5 = alloca [8 x i64], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #2
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #2
  %6 = getelementptr inbounds i8, ptr %2, i64 40
  %7 = getelementptr inbounds i8, ptr %2, i64 2
  br label %10

8:                                                ; preds = %17
  %9 = add nuw nsw i64 %.015, 1
  %exitcond.not = icmp eq i64 %9, 64
  br i1 %exitcond.not, label %.loopexit, label %10, !llvm.loop !5

10:                                               ; preds = %3, %8
  %.015 = phi i64 [ 0, %3 ], [ %9, %8 ]
  %11 = load i16, ptr %7, align 2, !tbaa !7
  %12 = sext i16 %11 to i32
  %13 = add nsw i32 %12, 63
  %14 = sdiv i32 %13, 64
  %15 = trunc i32 %14 to i8
  %16 = call i32 @uECC_generate_random_int(ptr noundef nonnull %4, ptr noundef nonnull %6, i8 noundef signext %15)
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %.loopexit, label %17

17:                                               ; preds = %10
  %18 = call i64 @EccPoint_compute_public_key(ptr noundef nonnull %5, ptr noundef nonnull %4, ptr noundef nonnull %2)
  %.not14 = icmp eq i64 %18, 0
  br i1 %.not14, label %8, label %19

19:                                               ; preds = %17
  %20 = load i16, ptr %7, align 2, !tbaa !7
  %21 = sext i16 %20 to i32
  %22 = add nsw i32 %21, 7
  %23 = sdiv i32 %22, 8
  call void @uECC_vli_nativeToBytes(ptr noundef %1, i32 noundef %23, ptr noundef nonnull %4)
  %24 = getelementptr inbounds i8, ptr %2, i64 1
  %25 = load i8, ptr %24, align 1, !tbaa !13
  %26 = sext i8 %25 to i32
  call void @uECC_vli_nativeToBytes(ptr noundef %0, i32 noundef %26, ptr noundef nonnull %5)
  %27 = load i8, ptr %24, align 1, !tbaa !13
  %28 = sext i8 %27 to i32
  %29 = sext i8 %27 to i64
  %30 = getelementptr inbounds i8, ptr %0, i64 %29
  %31 = load i8, ptr %2, align 8, !tbaa !14
  %32 = sext i8 %31 to i64
  %33 = getelementptr inbounds i64, ptr %5, i64 %32
  call void @uECC_vli_nativeToBytes(ptr noundef %30, i32 noundef %28, ptr noundef nonnull %33)
  br label %.loopexit

.loopexit:                                        ; preds = %8, %10, %19
  %.013 = phi i32 [ 1, %19 ], [ 0, %10 ], [ 0, %8 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #2
  ret i32 %.013
}

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

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

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

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

; 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 = { 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"}
!7 = !{!8, !11, i64 2}
!8 = !{!"uECC_Curve_t", !9, i64 0, !9, i64 1, !11, i64 2, !9, i64 8, !9, i64 40, !9, i64 72, !9, i64 136, !12, i64 168, !12, i64 176, !12, i64 184, !12, i64 192}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"short", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!8, !9, i64 1}
!14 = !{!8, !9, i64 0}


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