; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (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._HMAC_DRBG_CTX = type { [8 x i32], [8 x i32], [16 x i32] }

@sha256_initial_hash_value = external constant [8 x i32], align 16

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

26:                                               ; preds = %35, %5
  %27 = load i64, ptr %12, align 8, !tbaa !9
  %28 = icmp ult i64 %27, 15
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %38

30:                                               ; preds = %26
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct._HMAC_DRBG_CTX, ptr %31, i32 0, i32 2
  %33 = load i64, ptr %12, align 8, !tbaa !9
  %34 = getelementptr inbounds [16 x i32], ptr %32, i64 0, i64 %33
  store i32 0, ptr %34, align 4, !tbaa !11
  br label %35

35:                                               ; preds = %30
  %36 = load i64, ptr %12, align 8, !tbaa !9
  %37 = add i64 %36, 1
  store i64 %37, ptr %12, align 8, !tbaa !9
  br label %26, !llvm.loop !13

38:                                               ; preds = %29
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._HMAC_DRBG_CTX, ptr %39, i32 0, i32 2
  %41 = getelementptr inbounds [16 x i32], ptr %40, i64 0, i64 8
  store i32 -2147483648, ptr %41, align 4, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct._HMAC_DRBG_CTX, ptr %42, i32 0, i32 2
  %44 = getelementptr inbounds [16 x i32], ptr %43, i64 0, i64 15
  store i32 768, ptr %44, align 4, !tbaa !11
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = load i64, ptr %10, align 8, !tbaa !9
  call void @hmac_drbg_reseed(ptr noundef %45, ptr noundef %46, i64 noundef %47, ptr noundef %48, i64 noundef %49)
  %50 = getelementptr inbounds [16 x i32], ptr %11, i64 0, i64 0
  call void @memzero(ptr noundef %50, i64 noundef 64)
  call void @llvm.lifetime.end.p0(i64 64, ptr %11) #4
  ret void
}

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

declare void @sha256_Transform(ptr noundef, ptr noundef, ptr noundef) #3

; 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 dso_local void @hmac_drbg_reseed(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) #0

declare void @memzero(ptr noundef, i64 noundef) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = 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"

@sha256_initial_hash_value = external constant [8 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @hmac_drbg_init(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = alloca [16 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %6, i8 54, i64 64, i1 false)
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  call void @sha256_Transform(ptr noundef nonnull @sha256_initial_hash_value, ptr noundef nonnull %6, ptr noundef nonnull %7) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %6, i8 92, i64 64, i1 false)
  call void @sha256_Transform(ptr noundef nonnull @sha256_initial_hash_value, ptr noundef nonnull %6, ptr noundef %0) #4
  %8 = getelementptr inbounds i8, ptr %0, i64 64
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(32) %8, i8 1, i64 32, i1 false)
  %scevgep = getelementptr i8, ptr %0, i64 100
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %scevgep, i8 0, i64 24, i1 false), !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 96
  store i32 -2147483648, ptr %9, align 4, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 124
  store i32 768, ptr %10, align 4, !tbaa !5
  call void @hmac_drbg_reseed(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4)
  call void @memzero(ptr noundef nonnull %6, i64 noundef 64) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %6) #4
  ret void
}

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

declare void @sha256_Transform(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; 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 dso_local void @hmac_drbg_reseed(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare void @memzero(ptr noundef, i64 noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,SROAPass,SimplifyCFGPass