; LLVM IR from ARMmbed/mbed-crypto :: library (psa_crypto)

; 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.psa_global_data_t = type { ptr, ptr, %struct.mbedtls_entropy_context, %struct.mbedtls_ctr_drbg_context, i8 }
%struct.mbedtls_entropy_context = type { i32, %struct.mbedtls_sha512_context, i32, [20 x %struct.mbedtls_entropy_source_state] }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_entropy_source_state = type { ptr, ptr, i64, i64, i32 }
%struct.mbedtls_ctr_drbg_context = type { [16 x i8], i32, i32, i64, i32, %struct.mbedtls_aes_context, ptr, ptr }
%struct.mbedtls_aes_context = type { i32, ptr, [68 x i32] }

@global_data = external hidden global %struct.psa_global_data_t, 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden i32 @mbedtls_to_psa_error(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_generate_random(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 -110, ptr %6, align 4, !tbaa !11
  %8 = load i8, ptr getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 4), align 8
  %9 = and i8 %8, 1
  %10 = zext i8 %9 to i32
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 -137, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %36

13:                                               ; preds = %2
  br label %14

14:                                               ; preds = %25, %13
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = icmp ugt i64 %15, 1024
  br i1 %16, label %17, label %30

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = call i32 @mbedtls_ctr_drbg_random(ptr noundef getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 3), ptr noundef %18, i64 noundef 1024)
  store i32 %19, ptr %6, align 4, !tbaa !11
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %25

22:                                               ; preds = %17
  %23 = load i32, ptr %6, align 4, !tbaa !11
  %24 = call i32 @mbedtls_to_psa_error(i32 noundef %23)
  store i32 %24, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %36

25:                                               ; preds = %17
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 1024
  store ptr %27, ptr %4, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !9
  %29 = sub i64 %28, 1024
  store i64 %29, ptr %5, align 8, !tbaa !9
  br label %14, !llvm.loop !13

30:                                               ; preds = %14
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = call i32 @mbedtls_ctr_drbg_random(ptr noundef getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 3), ptr noundef %31, i64 noundef %32)
  store i32 %33, ptr %6, align 4, !tbaa !11
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = call i32 @mbedtls_to_psa_error(i32 noundef %34)
  store i32 %35, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %36

36:                                               ; preds = %30, %22, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %37 = load i32, ptr %3, align 4
  ret i32 %37
}

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 = { "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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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"

%struct.psa_global_data_t = type { ptr, ptr, %struct.mbedtls_entropy_context, %struct.mbedtls_ctr_drbg_context, i8 }
%struct.mbedtls_entropy_context = type { i32, %struct.mbedtls_sha512_context, i32, [20 x %struct.mbedtls_entropy_source_state] }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_entropy_source_state = type { ptr, ptr, i64, i64, i32 }
%struct.mbedtls_ctr_drbg_context = type { [16 x i8], i32, i32, i64, i32, %struct.mbedtls_aes_context, ptr, ptr }
%struct.mbedtls_aes_context = type { i32, ptr, [68 x i32] }

@global_data = external hidden global %struct.psa_global_data_t, align 8

; Function Attrs: nounwind uwtable
declare hidden i32 @mbedtls_to_psa_error(i32 noundef) local_unnamed_addr #0

declare i32 @mbedtls_ctr_drbg_random(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_generate_random(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @global_data, i64 1392), align 8
  %4 = and i8 %3, 1
  %5 = icmp eq i8 %4, 0
  br i1 %5, label %14, label %.preheader

.preheader:                                       ; preds = %2
  %6 = icmp ugt i64 %1, 1024
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %8
  %.015 = phi i64 [ %10, %8 ], [ %1, %.preheader ]
  %.0914 = phi ptr [ %9, %8 ], [ %0, %.preheader ]
  %7 = tail call i32 @mbedtls_ctr_drbg_random(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 1048), ptr noundef %.0914, i64 noundef 1024) #2
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %.sink.split

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.0914, i64 1024
  %10 = add i64 %.015, -1024
  %11 = icmp ugt i64 %10, 1024
  br i1 %11, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %8, %.preheader
  %.09.lcssa = phi ptr [ %0, %.preheader ], [ %9, %8 ]
  %.0.lcssa = phi i64 [ %1, %.preheader ], [ %10, %8 ]
  %12 = tail call i32 @mbedtls_ctr_drbg_random(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 1048), ptr noundef %.09.lcssa, i64 noundef %.0.lcssa) #2
  br label %.sink.split

.sink.split:                                      ; preds = %.lr.ph, %._crit_edge
  %.sink = phi i32 [ %12, %._crit_edge ], [ %7, %.lr.ph ]
  %13 = tail call i32 @mbedtls_to_psa_error(i32 noundef %.sink)
  br label %14

14:                                               ; preds = %.sink.split, %2
  %.010 = phi i32 [ -137, %2 ], [ %13, %.sink.split ]
  ret i32 %.010
}

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


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass