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

; 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 [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @default_CSPRNG(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %16, label %13

13:                                               ; preds = %2
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp ule i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %13, %2
  store i32 0, ptr %3, align 4
  br label %59

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %18 = call i32 (ptr, i32, ...) @open(ptr noundef @.str, i32 noundef 524288)
  store i32 %18, ptr %6, align 4, !tbaa !9
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %27

21:                                               ; preds = %17
  %22 = call i32 (ptr, i32, ...) @open(ptr noundef @.str.1, i32 noundef 524288)
  store i32 %22, ptr %6, align 4, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = icmp eq i32 %23, -1
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %58

26:                                               ; preds = %21
  br label %27

27:                                               ; preds = %26, %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %28, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = zext i32 %29 to i64
  store i64 %30, ptr %9, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %53, %27
  %32 = load i64, ptr %9, align 8, !tbaa !11
  %33 = icmp ugt i64 %32, 0
  br i1 %33, label %34, label %54

34:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = load i64, ptr %9, align 8, !tbaa !11
  %38 = call i64 @read(i32 noundef %35, ptr noundef %36, i64 noundef %37)
  store i64 %38, ptr %10, align 8, !tbaa !11
  %39 = load i64, ptr %10, align 8, !tbaa !11
  %40 = icmp sle i64 %39, 0
  br i1 %40, label %41, label %44

41:                                               ; preds = %34
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = call i32 @close(i32 noundef %42)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %51

44:                                               ; preds = %34
  %45 = load i64, ptr %10, align 8, !tbaa !11
  %46 = load i64, ptr %9, align 8, !tbaa !11
  %47 = sub i64 %46, %45
  store i64 %47, ptr %9, align 8, !tbaa !11
  %48 = load i64, ptr %10, align 8, !tbaa !11
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %49, i64 %48
  store ptr %50, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %7, align 4
  br label %51

51:                                               ; preds = %44, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %52 = load i32, ptr %7, align 4
  switch i32 %52, label %57 [
    i32 0, label %53
  ]

53:                                               ; preds = %51
  br label %31, !llvm.loop !13

54:                                               ; preds = %31
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = call i32 @close(i32 noundef %55)
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %57

57:                                               ; preds = %54, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %58

58:                                               ; preds = %57, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %59

59:                                               ; preds = %58, %16
  %60 = load i32, ptr %3, align 4
  ret i32 %60
}

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

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

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #2

declare i32 @close(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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @default_CSPRNG(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq i32 %1, 0
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %18, label %5

5:                                                ; preds = %2
  %6 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str, i32 noundef 524288) #3
  %7 = icmp eq i32 %6, -1
  br i1 %7, label %8, label %.lr.ph.preheader

8:                                                ; preds = %5
  %9 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str.1, i32 noundef 524288) #3
  %10 = icmp eq i32 %9, -1
  br i1 %10, label %18, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5, %8
  %.024 = phi i32 [ %9, %8 ], [ %6, %5 ]
  %11 = zext i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %14
  %.01934 = phi i64 [ %15, %14 ], [ %11, %.lr.ph.preheader ]
  %.02133 = phi ptr [ %16, %14 ], [ %0, %.lr.ph.preheader ]
  %12 = tail call i64 @read(i32 noundef %.024, ptr noundef %.02133, i64 noundef %.01934) #3
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %14, label %.sink.split

14:                                               ; preds = %.lr.ph
  %15 = sub i64 %.01934, %12
  %16 = getelementptr inbounds i8, ptr %.02133, i64 %12
  %.not = icmp eq i64 %15, 0
  br i1 %.not, label %.sink.split, label %.lr.ph, !llvm.loop !5

.sink.split:                                      ; preds = %14, %.lr.ph
  %.4.ph = phi i32 [ 0, %.lr.ph ], [ 1, %14 ]
  %17 = tail call i32 @close(i32 noundef %.024) #3
  br label %18

18:                                               ; preds = %.sink.split, %8, %2
  %.4 = phi i32 [ 0, %2 ], [ 0, %8 ], [ %.4.ph, %.sink.split ]
  ret i32 %.4
}

; Function Attrs: nofree
declare noundef i32 @open(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #1

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #1

declare i32 @close(i32 noundef) local_unnamed_addr #2

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 = { nofree "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 = { "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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass