; LLVM IR from ARMmbed/mbed-crypto :: library (hmac_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_hmac_drbg_context = type { %struct.mbedtls_md_context_t, [64 x i8], i32, i64, i32, i32, ptr, ptr }
%struct.mbedtls_md_context_t = type { ptr, ptr, ptr }

; 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

declare zeroext i8 @mbedtls_md_get_size(ptr noundef) #2

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

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

declare i32 @mbedtls_md_setup(ptr noundef, ptr noundef, i32 noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) #3 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store ptr %3, ptr %11, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  store i64 %5, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 -110, ptr %14, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  %20 = call i32 @mbedtls_md_setup(ptr noundef %18, ptr noundef %19, i32 noundef 1)
  store i32 %20, ptr %14, align 4, !tbaa !11
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %6
  %23 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %23, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %78

24:                                               ; preds = %6
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = call zeroext i8 @mbedtls_md_get_size(ptr noundef %25)
  %27 = zext i8 %26 to i64
  store i64 %27, ptr %15, align 8, !tbaa !9
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %30, i32 0, i32 1
  %32 = getelementptr inbounds [64 x i8], ptr %31, i64 0, i64 0
  %33 = load i64, ptr %15, align 8, !tbaa !9
  %34 = call i32 @mbedtls_md_hmac_starts(ptr noundef %29, ptr noundef %32, i64 noundef %33)
  store i32 %34, ptr %14, align 4, !tbaa !11
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %24
  %37 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %37, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %78

38:                                               ; preds = %24
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %39, i32 0, i32 1
  %41 = getelementptr inbounds [64 x i8], ptr %40, i64 0, i64 0
  %42 = load i64, ptr %15, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 8 %41, i8 1, i64 %42, i1 false)
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %44, i32 0, i32 6
  store ptr %43, ptr %45, align 8, !tbaa !13
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %47, i32 0, i32 7
  store ptr %46, ptr %48, align 8, !tbaa !16
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %49, i32 0, i32 5
  store i32 10000, ptr %50, align 4, !tbaa !17
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %51, i32 0, i32 3
  %53 = load i64, ptr %52, align 8, !tbaa !18
  %54 = icmp eq i64 %53, 0
  br i1 %54, label %55, label %69

55:                                               ; preds = %38
  %56 = load i64, ptr %15, align 8, !tbaa !9
  %57 = icmp ule i64 %56, 20
  br i1 %57, label %58, label %59

58:                                               ; preds = %55
  br label %64

59:                                               ; preds = %55
  %60 = load i64, ptr %15, align 8, !tbaa !9
  %61 = icmp ule i64 %60, 28
  %62 = zext i1 %61 to i64
  %63 = select i1 %61, i32 24, i32 32
  br label %64

64:                                               ; preds = %59, %58
  %65 = phi i32 [ 16, %58 ], [ %63, %59 ]
  %66 = sext i32 %65 to i64
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %67, i32 0, i32 3
  store i64 %66, ptr %68, align 8, !tbaa !18
  br label %69

69:                                               ; preds = %64, %38
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  %72 = load i64, ptr %13, align 8, !tbaa !9
  %73 = call i32 @hmac_drbg_reseed_core(ptr noundef %70, ptr noundef %71, i64 noundef %72, i32 noundef 1)
  store i32 %73, ptr %14, align 4, !tbaa !11
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %75, label %77

75:                                               ; preds = %69
  %76 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %76, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %78

77:                                               ; preds = %69
  store i32 0, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %78

78:                                               ; preds = %77, %75, %36, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  %79 = load i32, ptr %7, align 4
  ret i32 %79
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 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 #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 112}
!14 = !{!"mbedtls_hmac_drbg_context", !15, i64 0, !7, i64 24, !12, i64 88, !10, i64 96, !12, i64 104, !12, i64 108, !6, i64 112, !6, i64 120}
!15 = !{!"mbedtls_md_context_t", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!14, !6, i64 120}
!17 = !{!14, !12, i64 108}
!18 = !{!14, !10, i64 96}


; 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

declare zeroext i8 @mbedtls_md_get_size(ptr noundef) local_unnamed_addr #1

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

declare i32 @mbedtls_md_setup(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) local_unnamed_addr #2 {
  %7 = tail call i32 @mbedtls_md_setup(ptr noundef %0, ptr noundef %1, i32 noundef 1) #3
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %27

8:                                                ; preds = %6
  %9 = tail call zeroext i8 @mbedtls_md_get_size(ptr noundef %1) #3
  %10 = zext i8 %9 to i64
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = tail call i32 @mbedtls_md_hmac_starts(ptr noundef %0, ptr noundef nonnull %11, i64 noundef %10) #3
  %.not27 = icmp eq i32 %12, 0
  br i1 %.not27, label %13, label %27

13:                                               ; preds = %8
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %11, i8 1, i64 %10, i1 false)
  %14 = getelementptr inbounds i8, ptr %0, i64 112
  store ptr %2, ptr %14, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %0, i64 120
  store ptr %3, ptr %15, align 8, !tbaa !13
  %16 = getelementptr inbounds i8, ptr %0, i64 108
  store i32 10000, ptr %16, align 4, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %0, i64 96
  %18 = load i64, ptr %17, align 8, !tbaa !15
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %25

20:                                               ; preds = %13
  %21 = icmp ult i8 %9, 21
  %22 = icmp ult i8 %9, 29
  %23 = select i1 %22, i64 24, i64 32
  %24 = select i1 %21, i64 16, i64 %23
  store i64 %24, ptr %17, align 8, !tbaa !15
  br label %25

25:                                               ; preds = %20, %13
  %26 = tail call i32 @hmac_drbg_reseed_core(ptr noundef nonnull %0, ptr noundef %4, i64 noundef %5, i32 noundef 1)
  br label %27

27:                                               ; preds = %25, %8, %6
  %.0 = phi i32 [ %7, %6 ], [ %12, %8 ], [ %26, %25 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 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, !8, i64 112}
!6 = !{!"mbedtls_hmac_drbg_context", !7, i64 0, !9, i64 24, !11, i64 88, !12, i64 96, !11, i64 104, !11, i64 108, !8, i64 112, !8, i64 120}
!7 = !{!"mbedtls_md_context_t", !8, i64 0, !8, i64 8, !8, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"long", !9, i64 0}
!13 = !{!6, !8, i64 120}
!14 = !{!6, !11, i64 108}
!15 = !{!6, !12, i64 96}


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