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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ctr_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i64 noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca [32 x i8], align 16
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 -110, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 32, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %16 = getelementptr inbounds [32 x i8], ptr %13, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %16, i8 0, i64 32, i1 false)
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %17, i32 0, i32 5
  call void @mbedtls_aes_init(ptr noundef %18)
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %20, i32 0, i32 6
  store ptr %19, ptr %21, align 8, !tbaa !13
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %23, i32 0, i32 7
  store ptr %22, ptr %24, align 8, !tbaa !16
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %25, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %5
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %30, i32 0, i32 3
  store i64 48, ptr %31, align 8, !tbaa !17
  br label %32

32:                                               ; preds = %29, %5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 8, !tbaa !18
  %36 = icmp sge i32 %35, 0
  br i1 %36, label %37, label %42

37:                                               ; preds = %32
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 8, !tbaa !18
  %41 = sext i32 %40 to i64
  br label %47

42:                                               ; preds = %32
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %43, i32 0, i32 3
  %45 = load i64, ptr %44, align 8, !tbaa !17
  %46 = call i64 @good_nonce_len(i64 noundef %45)
  br label %47

47:                                               ; preds = %42, %37
  %48 = phi i64 [ %41, %37 ], [ %46, %42 ]
  store i64 %48, ptr %14, align 8, !tbaa !9
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %49, i32 0, i32 4
  store i32 10000, ptr %50, align 8, !tbaa !19
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %51, i32 0, i32 5
  %53 = getelementptr inbounds [32 x i8], ptr %13, i64 0, i64 0
  %54 = call i32 @mbedtls_aes_setkey_enc(ptr noundef %52, ptr noundef %53, i32 noundef 256)
  store i32 %54, ptr %12, align 4, !tbaa !11
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %58

56:                                               ; preds = %47
  %57 = load i32, ptr %12, align 4, !tbaa !11
  store i32 %57, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %68

58:                                               ; preds = %47
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = load i64, ptr %11, align 8, !tbaa !9
  %62 = load i64, ptr %14, align 8, !tbaa !9
  %63 = call i32 @mbedtls_ctr_drbg_reseed_internal(ptr noundef %59, ptr noundef %60, i64 noundef %61, i64 noundef %62)
  store i32 %63, ptr %12, align 4, !tbaa !11
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %58
  %66 = load i32, ptr %12, align 4, !tbaa !11
  store i32 %66, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %68

67:                                               ; preds = %58
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %68

68:                                               ; preds = %67, %65, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %69 = load i32, ptr %6, align 4
  ret i32 %69
}

declare void @mbedtls_aes_init(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i64 @good_nonce_len(i64 noundef) #2

declare i32 @mbedtls_aes_setkey_enc(ptr noundef, ptr noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #3 = { "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 = { 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 = !{!14, !6, i64 328}
!14 = !{!"mbedtls_ctr_drbg_context", !7, i64 0, !12, i64 16, !12, i64 20, !10, i64 24, !12, i64 32, !15, i64 40, !6, i64 328, !6, i64 336}
!15 = !{!"mbedtls_aes_context", !12, i64 0, !6, i64 8, !7, i64 16}
!16 = !{!14, !6, i64 336}
!17 = !{!14, !10, i64 24}
!18 = !{!14, !12, i64 16}
!19 = !{!14, !12, i64 32}


; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ctr_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %6, i8 0, i64 32, i1 false)
  %7 = getelementptr inbounds i8, ptr %0, i64 40
  tail call void @mbedtls_aes_init(ptr noundef nonnull %7) #4
  %8 = getelementptr inbounds i8, ptr %0, i64 328
  store ptr %1, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 336
  store ptr %2, ptr %9, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = load i64, ptr %10, align 8, !tbaa !14
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %5
  store i64 48, ptr %10, align 8, !tbaa !14
  br label %14

14:                                               ; preds = %13, %5
  %15 = phi i64 [ 48, %13 ], [ %11, %5 ]
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = load i32, ptr %16, align 8, !tbaa !15
  %18 = icmp sgt i32 %17, -1
  br i1 %18, label %19, label %21

19:                                               ; preds = %14
  %20 = zext nneg i32 %17 to i64
  br label %23

21:                                               ; preds = %14
  %22 = tail call i64 @good_nonce_len(i64 noundef %15)
  br label %23

23:                                               ; preds = %21, %19
  %24 = phi i64 [ %20, %19 ], [ %22, %21 ]
  %25 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 10000, ptr %25, align 8, !tbaa !16
  %26 = call i32 @mbedtls_aes_setkey_enc(ptr noundef nonnull %7, ptr noundef nonnull %6, i32 noundef 256) #4
  %.not = icmp eq i32 %26, 0
  br i1 %.not, label %27, label %29

27:                                               ; preds = %23
  %28 = call i32 @mbedtls_ctr_drbg_reseed_internal(ptr noundef nonnull %0, ptr noundef %3, i64 noundef %4, i64 noundef %24)
  br label %29

29:                                               ; preds = %27, %23
  %.0 = phi i32 [ %26, %23 ], [ %28, %27 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #4
  ret i32 %.0
}

declare void @mbedtls_aes_init(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i64 @good_nonce_len(i64 noundef) local_unnamed_addr #2

declare i32 @mbedtls_aes_setkey_enc(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #3 = { "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 = { 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, !12, i64 328}
!6 = !{!"mbedtls_ctr_drbg_context", !7, i64 0, !9, i64 16, !9, i64 20, !10, i64 24, !9, i64 32, !11, i64 40, !12, i64 328, !12, i64 336}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!"mbedtls_aes_context", !9, i64 0, !12, i64 8, !7, i64 16}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!6, !12, i64 336}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !9, i64 16}
!16 = !{!6, !9, i64 32}


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