; LLVM IR from cryspen/libcrux :: sys/hacl/c/src (Hacl_FFDHE)

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

@.str = external hidden unnamed_addr constant [70 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [77 x i8], align 1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @ffdhe_len(i8 noundef zeroext) #0

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

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @ffdhe_compute_exp(i8 noundef zeroext, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #4

; Function Attrs: nounwind uwtable
define dso_local i64 @Hacl_FFDHE_ffdhe_shared_secret_precomp(i8 noundef zeroext %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #6 {
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %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 i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i64, align 8
  store i8 %0, ptr %6, align 1, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !8
  store ptr %2, ptr %8, align 8, !tbaa !8
  store ptr %3, ptr %9, align 8, !tbaa !8
  store ptr %4, ptr %10, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #7
  %18 = load i8, ptr %6, align 1, !tbaa !5
  %19 = call i32 @ffdhe_len(i8 noundef zeroext %18)
  store i32 %19, ptr %11, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #7
  %20 = load i32, ptr %11, align 4, !tbaa !10
  %21 = sub i32 %20, 1
  %22 = udiv i32 %21, 8
  %23 = add i32 %22, 1
  store i32 %23, ptr %12, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #7
  %24 = load ptr, ptr %7, align 8, !tbaa !8
  store ptr %24, ptr %13, align 8, !tbaa !8
  br label %25

25:                                               ; preds = %5
  %26 = load i32, ptr %12, align 4, !tbaa !10
  %27 = zext i32 %26 to i64
  %28 = icmp ugt i64 %27, 2305843009213693951
  br i1 %28, label %29, label %31

29:                                               ; preds = %25
  %30 = call i32 (ptr, ...) @printf(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 317)
  call void @exit(i32 noundef 253) #8
  unreachable

31:                                               ; preds = %25
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %12, align 4, !tbaa !10
  %35 = zext i32 %34 to i64
  %36 = call ptr @llvm.stacksave.p0()
  store ptr %36, ptr %14, align 8
  %37 = alloca i64, i64 %35, align 16
  store i64 %35, ptr %15, align 8
  %38 = load i32, ptr %12, align 4, !tbaa !10
  %39 = zext i32 %38 to i64
  %40 = mul i64 %39, 8
  call void @llvm.memset.p0.i64(ptr align 16 %37, i8 0, i64 %40, i1 false)
  br label %41

41:                                               ; preds = %33
  %42 = load i32, ptr %12, align 4, !tbaa !10
  %43 = zext i32 %42 to i64
  %44 = icmp ugt i64 %43, 2305843009213693951
  br i1 %44, label %45, label %47

45:                                               ; preds = %41
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 320)
  call void @exit(i32 noundef 253) #8
  unreachable

47:                                               ; preds = %41
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %12, align 4, !tbaa !10
  %51 = zext i32 %50 to i64
  %52 = alloca i64, i64 %51, align 16
  store i64 %51, ptr %16, align 8
  %53 = load i32, ptr %12, align 4, !tbaa !10
  %54 = zext i32 %53 to i64
  %55 = mul i64 %54, 8
  call void @llvm.memset.p0.i64(ptr align 16 %52, i8 0, i64 %55, i1 false)
  %56 = load i32, ptr %11, align 4, !tbaa !10
  %57 = load ptr, ptr %8, align 8, !tbaa !8
  call void @Hacl_Bignum_Convert_bn_from_bytes_be_uint64(i32 noundef %56, ptr noundef %57, ptr noundef %37)
  %58 = load i32, ptr %11, align 4, !tbaa !10
  %59 = load ptr, ptr %9, align 8, !tbaa !8
  call void @Hacl_Bignum_Convert_bn_from_bytes_be_uint64(i32 noundef %58, ptr noundef %59, ptr noundef %52)
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #7
  %60 = load i8, ptr %6, align 1, !tbaa !5
  %61 = load ptr, ptr %13, align 8, !tbaa !8
  %62 = call i64 @ffdhe_check_pk(i8 noundef zeroext %60, ptr noundef %52, ptr noundef %61)
  store i64 %62, ptr %17, align 8, !tbaa !12
  %63 = load i64, ptr %17, align 8, !tbaa !12
  %64 = icmp eq i64 %63, -1
  br i1 %64, label %65, label %69

65:                                               ; preds = %49
  %66 = load i8, ptr %6, align 1, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !8
  %68 = load ptr, ptr %10, align 8, !tbaa !8
  call void @ffdhe_compute_exp(i8 noundef zeroext %66, ptr noundef %67, ptr noundef %37, ptr noundef %52, ptr noundef %68)
  br label %69

69:                                               ; preds = %65, %49
  %70 = load i64, ptr %17, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #7
  %71 = load ptr, ptr %14, align 8
  call void @llvm.stackrestore.p0(ptr %71)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #7
  ret i64 %70
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @ffdhe_check_pk(i8 noundef zeroext, ptr noundef, ptr noundef) #0

attributes #0 = { 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 #1 = { nocallback nofree nosync 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 = { noreturn nounwind "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 = { nocallback nofree nosync nounwind willreturn }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { 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 #7 = { nounwind }
attributes #8 = { noreturn 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !6, 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: inlinehint nounwind uwtable
declare hidden i32 @ffdhe_len(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @ffdhe_compute_exp(i8 noundef zeroext, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @Hacl_FFDHE_ffdhe_shared_secret_precomp(i8 noundef zeroext %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #2 {
  %6 = tail call i32 @ffdhe_len(i8 noundef zeroext %0)
  %7 = add i32 %6, -1
  %8 = lshr i32 %7, 3
  %9 = add nuw nsw i32 %8, 1
  %10 = zext nneg i32 %9 to i64
  %11 = alloca i64, i64 %10, align 16
  %12 = shl nuw nsw i64 %10, 3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1) %11, i8 0, i64 %12, i1 false)
  %13 = alloca i64, i64 %10, align 16
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1) %13, i8 0, i64 %12, i1 false)
  call void @Hacl_Bignum_Convert_bn_from_bytes_be_uint64(i32 noundef %6, ptr noundef %2, ptr noundef nonnull %11)
  call void @Hacl_Bignum_Convert_bn_from_bytes_be_uint64(i32 noundef %6, ptr noundef %3, ptr noundef nonnull %13)
  %14 = call i64 @ffdhe_check_pk(i8 noundef zeroext %0, ptr noundef nonnull %13, ptr noundef %1)
  %15 = icmp eq i64 %14, -1
  br i1 %15, label %16, label %17

16:                                               ; preds = %5
  call void @ffdhe_compute_exp(i8 noundef zeroext %0, ptr noundef %1, ptr noundef nonnull %11, ptr noundef nonnull %13, ptr noundef %4)
  br label %17

17:                                               ; preds = %16, %5
  ret i64 %14
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @ffdhe_check_pk(i8 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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" }

!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)"}


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