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

; 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.tc_hmac_prng_struct = type { %struct.tc_hmac_state_struct, [32 x i8], [32 x i8], i32 }
%struct.tc_hmac_state_struct = type { %struct.tc_sha256_state_struct, [128 x i8] }
%struct.tc_sha256_state_struct = type { [8 x i32], i64, [64 x i8], i64 }

; Function Attrs: nounwind uwtable
declare hidden void @update(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @tc_hmac_prng_generate(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %21, label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %21, label %15

15:                                               ; preds = %12
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %21, label %18

18:                                               ; preds = %15
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp ugt i32 %19, 524288
  br i1 %20, label %21, label %22

21:                                               ; preds = %18, %15, %12, %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %87

22:                                               ; preds = %18
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %23, i32 0, i32 3
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %22
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %87

28:                                               ; preds = %22
  br label %29

29:                                               ; preds = %28
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %30, i32 0, i32 3
  %32 = load i32, ptr %31, align 8, !tbaa !11
  %33 = add i32 %32, -1
  store i32 %33, ptr %31, align 8, !tbaa !11
  br label %34

34:                                               ; preds = %83, %29
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %85

37:                                               ; preds = %34
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %40, i32 0, i32 1
  %42 = getelementptr inbounds [32 x i8], ptr %41, i64 0, i64 0
  %43 = call i32 @tc_hmac_set_key(ptr noundef %39, ptr noundef %42, i32 noundef 32)
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %44, i32 0, i32 0
  %46 = call i32 @tc_hmac_init(ptr noundef %45)
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %49, i32 0, i32 2
  %51 = getelementptr inbounds [32 x i8], ptr %50, i64 0, i64 0
  %52 = call i32 @tc_hmac_update(ptr noundef %48, ptr noundef %51, i32 noundef 32)
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %53, i32 0, i32 2
  %55 = getelementptr inbounds [32 x i8], ptr %54, i64 0, i64 0
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %56, i32 0, i32 0
  %58 = call i32 @tc_hmac_final(ptr noundef %55, i32 noundef 32, ptr noundef %57)
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = icmp ugt i32 32, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %37
  %62 = load i32, ptr %6, align 4, !tbaa !9
  br label %64

63:                                               ; preds = %37
  br label %64

64:                                               ; preds = %63, %61
  %65 = phi i32 [ %62, %61 ], [ 32, %63 ]
  store i32 %65, ptr %8, align 4, !tbaa !9
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !9
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.tc_hmac_prng_struct, ptr %68, i32 0, i32 2
  %70 = getelementptr inbounds [32 x i8], ptr %69, i64 0, i64 0
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = call i32 @_copy(ptr noundef %66, i32 noundef %67, ptr noundef %70, i32 noundef %71)
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = zext i32 %73 to i64
  %76 = getelementptr inbounds i8, ptr %74, i64 %75
  store ptr %76, ptr %5, align 8, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !9
  %78 = icmp ugt i32 %77, 32
  br i1 %78, label %79, label %82

79:                                               ; preds = %64
  %80 = load i32, ptr %6, align 4, !tbaa !9
  %81 = sub i32 %80, 32
  br label %83

82:                                               ; preds = %64
  br label %83

83:                                               ; preds = %82, %79
  %84 = phi i32 [ %81, %79 ], [ 0, %82 ]
  store i32 %84, ptr %6, align 4, !tbaa !9
  br label %34, !llvm.loop !16

85:                                               ; preds = %34
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  call void @update(ptr noundef %86, ptr noundef null, i32 noundef 0, ptr noundef null, i32 noundef 0)
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %87

87:                                               ; preds = %85, %27, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %88 = load i32, ptr %4, align 4
  ret i32 %88
}

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

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

declare i32 @tc_hmac_init(ptr noundef) #2

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

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

declare i32 @_copy(ptr noundef, i32 noundef, ptr noundef, 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, !10, i64 304}
!12 = !{!"tc_hmac_prng_struct", !13, i64 0, !7, i64 240, !7, i64 272, !10, i64 304}
!13 = !{!"tc_hmac_state_struct", !14, i64 0, !7, i64 112}
!14 = !{!"tc_sha256_state_struct", !7, i64 0, !15, i64 32, !7, i64 40, !15, i64 104}
!15 = !{!"long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

; Function Attrs: nounwind uwtable
declare hidden void @update(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @tc_hmac_prng_generate(ptr noundef %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %2, null
  %or.cond = or i1 %4, %5
  %6 = add i32 %1, -524289
  %7 = icmp ult i32 %6, -524288
  %or.cond5 = or i1 %7, %or.cond
  br i1 %or.cond5, label %25, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %2, i64 304
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %25, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %12 = add i32 %10, -1
  store i32 %12, ptr %9, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %2, i64 240
  %14 = getelementptr inbounds i8, ptr %2, i64 272
  br label %15

15:                                               ; preds = %.lr.ph, %15
  %.03038 = phi ptr [ %0, %.lr.ph ], [ %23, %15 ]
  %.03137 = phi i32 [ %1, %.lr.ph ], [ %24, %15 ]
  %16 = tail call i32 @tc_hmac_set_key(ptr noundef %2, ptr noundef nonnull %13, i32 noundef 32) #3
  %17 = tail call i32 @tc_hmac_init(ptr noundef %2) #3
  %18 = tail call i32 @tc_hmac_update(ptr noundef %2, ptr noundef nonnull %14, i32 noundef 32) #3
  %19 = tail call i32 @tc_hmac_final(ptr noundef nonnull %14, i32 noundef 32, ptr noundef %2) #3
  %20 = tail call i32 @llvm.umin.i32(i32 %.03137, i32 32)
  %21 = tail call i32 @_copy(ptr noundef %.03038, i32 noundef %20, ptr noundef nonnull %14, i32 noundef %20) #3
  %22 = zext nneg i32 %20 to i64
  %23 = getelementptr inbounds i8, ptr %.03038, i64 %22
  %24 = add i32 %.03137, -32
  %.not = icmp ult i32 %.03137, 33
  br i1 %.not, label %._crit_edge, label %15, !llvm.loop !13

._crit_edge:                                      ; preds = %15
  tail call void @update(ptr noundef %2, ptr noundef null, i32 noundef 0, ptr noundef null, i32 noundef 0)
  br label %25

25:                                               ; preds = %8, %3, %._crit_edge
  %.0 = phi i32 [ 1, %._crit_edge ], [ 0, %3 ], [ -1, %8 ]
  ret i32 %.0
}

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

declare i32 @tc_hmac_init(ptr noundef) local_unnamed_addr #1

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !12, i64 304}
!6 = !{!"tc_hmac_prng_struct", !7, i64 0, !9, i64 240, !9, i64 272, !12, i64 304}
!7 = !{!"tc_hmac_state_struct", !8, i64 0, !9, i64 112}
!8 = !{!"tc_sha256_state_struct", !9, i64 0, !11, i64 32, !9, i64 40, !11, i64 104}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"int", !9, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass