; LLVM IR from intel/cryptography-primitives :: sources/ippcp/gfpec (pcpgfp_rand)

; 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._gsModEngine = type { ptr, i32, i32, i32, i32, i32, ptr, ptr, ptr, i64, ptr, ptr, ptr, ptr, i32, i32, ptr }
%struct._gsModMethod = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @cpGFpRand(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  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 4, ptr %9) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._gsModEngine, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !9
  store i32 %16, ptr %9, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._gsModEngine, ptr %17, i32 0, i32 2
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = add nsw i32 %19, 128
  store i32 %20, ptr %10, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %21 = load i32, ptr %10, align 4, !tbaa !13
  %22 = add nsw i32 %21, 64
  %23 = sub nsw i32 %22, 1
  %24 = sdiv i32 %23, 64
  store i32 %24, ptr %11, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = call ptr @gsModPoolAlloc(ptr noundef %25, i32 noundef 2)
  store ptr %26, ptr %13, align 8, !tbaa !5
  %27 = load ptr, ptr %13, align 8, !tbaa !5
  %28 = load i32, ptr %11, align 4, !tbaa !13
  %29 = call ptr @cpGFpElementPad(ptr noundef %27, i32 noundef %28, i64 noundef 0)
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load ptr, ptr %13, align 8, !tbaa !5
  %32 = load i32, ptr %10, align 4, !tbaa !13
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = call i32 %30(ptr noundef %31, i32 noundef %32, ptr noundef %33)
  %35 = icmp ne i32 0, %34
  %36 = zext i1 %35 to i32
  store i32 %36, ptr %12, align 4, !tbaa !13
  %37 = load i32, ptr %12, align 4, !tbaa !13
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %64, label %39

39:                                               ; preds = %4
  %40 = load ptr, ptr %13, align 8, !tbaa !5
  %41 = load i32, ptr %11, align 4, !tbaa !13
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct._gsModEngine, ptr %42, i32 0, i32 8
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load i32, ptr %9, align 4, !tbaa !13
  %46 = call i32 @cpMod_BNU(ptr noundef %40, i32 noundef %41, ptr noundef %44, i32 noundef %45)
  store i32 %46, ptr %11, align 4, !tbaa !13
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  %48 = load i32, ptr %11, align 4, !tbaa !13
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i64, ptr %47, i64 %49
  %51 = load i32, ptr %9, align 4, !tbaa !13
  %52 = load i32, ptr %11, align 4, !tbaa !13
  %53 = sub nsw i32 %51, %52
  %54 = call ptr @cpGFpElementPad(ptr noundef %50, i32 noundef %53, i64 noundef 0)
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._gsModEngine, ptr %55, i32 0, i32 6
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = getelementptr inbounds %struct._gsModMethod, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !17
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = call ptr %59(ptr noundef %60, ptr noundef %61, ptr noundef %62)
  br label %64

64:                                               ; preds = %39, %4
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  call void @gsModPoolFree(ptr noundef %65, i32 noundef 2)
  %66 = load i32, ptr %12, align 4, !tbaa !13
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %64
  br label %71

69:                                               ; preds = %64
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %69, %68
  %72 = phi ptr [ null, %68 ], [ %70, %69 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret ptr %72
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @gsModPoolAlloc(ptr noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @cpGFpElementPad(ptr noundef, i32 noundef, i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @cpMod_BNU(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #2

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

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

!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, !11, i64 16}
!10 = !{!"_gsModEngine", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !11, i64 96, !11, i64 100, !6, i64 104}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 12}
!15 = !{!10, !6, i64 48}
!16 = !{!10, !6, i64 32}
!17 = !{!18, !6, i64 0}
!18 = !{!"_gsModMethod", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80}


; 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 noundef ptr @cpGFpRand(ptr noundef %0, ptr noundef %1, ptr nocapture noundef readonly %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %1, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %1, i64 12
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %9 = add nsw i32 %8, 128
  %10 = add nsw i32 %8, 191
  %11 = sdiv i32 %10, 64
  %12 = tail call ptr @gsModPoolAlloc(ptr noundef %1, i32 noundef 2)
  %13 = tail call ptr @cpGFpElementPad(ptr noundef %12, i32 noundef %11, i64 noundef 0)
  %14 = tail call i32 %2(ptr noundef %12, i32 noundef %9, ptr noundef %3) #2
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %15, label %27

15:                                               ; preds = %4
  %16 = getelementptr inbounds i8, ptr %1, i64 48
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = tail call i32 @cpMod_BNU(ptr noundef %12, i32 noundef %11, ptr noundef %17, i32 noundef %6)
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i64, ptr %12, i64 %19
  %21 = sub nsw i32 %6, %18
  %22 = tail call ptr @cpGFpElementPad(ptr noundef %20, i32 noundef %21, i64 noundef 0)
  %23 = getelementptr inbounds i8, ptr %1, i64 32
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = tail call ptr %25(ptr noundef %0, ptr noundef %12, ptr noundef nonnull %1) #2
  br label %27

27:                                               ; preds = %15, %4
  %28 = phi ptr [ %0, %15 ], [ null, %4 ]
  tail call void @gsModPoolFree(ptr noundef nonnull %1, i32 noundef 2)
  ret ptr %28
}

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @gsModPoolFree(ptr noundef, i32 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 = { 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 #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 16}
!6 = !{!"_gsModEngine", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !11, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !10, i64 96, !10, i64 100, !7, i64 104}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{!6, !7, i64 48}
!14 = !{!6, !7, i64 32}
!15 = !{!16, !7, i64 0}
!16 = !{!"_gsModMethod", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80}


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