; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (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: nounwind uwtable
declare dso_local void @mbedtls_hmac_drbg_update(ptr noundef, ptr noundef, i64 noundef) #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_reset(ptr noundef) #2

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

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

; 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 i32 @mbedtls_hmac_drbg_reseed(ptr noundef, ptr noundef, i64 noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_random_with_add(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  %18 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  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
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %19, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %20 = load ptr, ptr %13, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.mbedtls_md_context_t, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = call zeroext i8 @mbedtls_md_get_size(ptr noundef %23)
  %25 = zext i8 %24 to i64
  store i64 %25, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %26 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %26, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %27, ptr %16, align 8, !tbaa !5
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = icmp ugt i64 %28, 1024
  br i1 %29, label %30, label %31

30:                                               ; preds = %5
  store i32 -3, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %121

31:                                               ; preds = %5
  %32 = load i64, ptr %11, align 8, !tbaa !9
  %33 = icmp ugt i64 %32, 256
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  store i32 -5, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %121

35:                                               ; preds = %31
  %36 = load ptr, ptr %13, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %36, i32 0, i32 6
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %62

40:                                               ; preds = %35
  %41 = load ptr, ptr %13, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %41, i32 0, i32 4
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = icmp eq i32 %43, 1
  br i1 %44, label %53, label %45

45:                                               ; preds = %40
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %46, i32 0, i32 2
  %48 = load i32, ptr %47, align 8, !tbaa !17
  %49 = load ptr, ptr %13, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %49, i32 0, i32 5
  %51 = load i32, ptr %50, align 4, !tbaa !18
  %52 = icmp sgt i32 %48, %51
  br i1 %52, label %53, label %62

53:                                               ; preds = %45, %40
  %54 = load ptr, ptr %13, align 8, !tbaa !5
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = load i64, ptr %11, align 8, !tbaa !9
  %57 = call i32 @mbedtls_hmac_drbg_reseed(ptr noundef %54, ptr noundef %55, i64 noundef %56)
  store i32 %57, ptr %12, align 4, !tbaa !19
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %61

59:                                               ; preds = %53
  %60 = load i32, ptr %12, align 4, !tbaa !19
  store i32 %60, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %121

61:                                               ; preds = %53
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %61, %45, %35
  %63 = load ptr, ptr %10, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %72

65:                                               ; preds = %62
  %66 = load i64, ptr %11, align 8, !tbaa !9
  %67 = icmp ne i64 %66, 0
  br i1 %67, label %68, label %72

68:                                               ; preds = %65
  %69 = load ptr, ptr %13, align 8, !tbaa !5
  %70 = load ptr, ptr %10, align 8, !tbaa !5
  %71 = load i64, ptr %11, align 8, !tbaa !9
  call void @mbedtls_hmac_drbg_update(ptr noundef %69, ptr noundef %70, i64 noundef %71)
  br label %72

72:                                               ; preds = %68, %65, %62
  br label %73

73:                                               ; preds = %84, %72
  %74 = load i64, ptr %15, align 8, !tbaa !9
  %75 = icmp ne i64 %74, 0
  br i1 %75, label %76, label %113

76:                                               ; preds = %73
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #4
  %77 = load i64, ptr %15, align 8, !tbaa !9
  %78 = load i64, ptr %14, align 8, !tbaa !9
  %79 = icmp ugt i64 %77, %78
  br i1 %79, label %80, label %82

80:                                               ; preds = %76
  %81 = load i64, ptr %14, align 8, !tbaa !9
  br label %84

82:                                               ; preds = %76
  %83 = load i64, ptr %15, align 8, !tbaa !9
  br label %84

84:                                               ; preds = %82, %80
  %85 = phi i64 [ %81, %80 ], [ %83, %82 ]
  store i64 %85, ptr %18, align 8, !tbaa !9
  %86 = load ptr, ptr %13, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %86, i32 0, i32 0
  %88 = call i32 @mbedtls_md_hmac_reset(ptr noundef %87)
  %89 = load ptr, ptr %13, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %89, i32 0, i32 0
  %91 = load ptr, ptr %13, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %91, i32 0, i32 1
  %93 = getelementptr inbounds [64 x i8], ptr %92, i64 0, i64 0
  %94 = load i64, ptr %14, align 8, !tbaa !9
  %95 = call i32 @mbedtls_md_hmac_update(ptr noundef %90, ptr noundef %93, i64 noundef %94)
  %96 = load ptr, ptr %13, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %96, i32 0, i32 0
  %98 = load ptr, ptr %13, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %98, i32 0, i32 1
  %100 = getelementptr inbounds [64 x i8], ptr %99, i64 0, i64 0
  %101 = call i32 @mbedtls_md_hmac_finish(ptr noundef %97, ptr noundef %100)
  %102 = load ptr, ptr %16, align 8, !tbaa !5
  %103 = load ptr, ptr %13, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %103, i32 0, i32 1
  %105 = getelementptr inbounds [64 x i8], ptr %104, i64 0, i64 0
  %106 = load i64, ptr %18, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %102, ptr align 8 %105, i64 %106, i1 false)
  %107 = load i64, ptr %18, align 8, !tbaa !9
  %108 = load ptr, ptr %16, align 8, !tbaa !5
  %109 = getelementptr inbounds i8, ptr %108, i64 %107
  store ptr %109, ptr %16, align 8, !tbaa !5
  %110 = load i64, ptr %18, align 8, !tbaa !9
  %111 = load i64, ptr %15, align 8, !tbaa !9
  %112 = sub i64 %111, %110
  store i64 %112, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #4
  br label %73, !llvm.loop !20

113:                                              ; preds = %73
  %114 = load ptr, ptr %13, align 8, !tbaa !5
  %115 = load ptr, ptr %10, align 8, !tbaa !5
  %116 = load i64, ptr %11, align 8, !tbaa !9
  call void @mbedtls_hmac_drbg_update(ptr noundef %114, ptr noundef %115, i64 noundef %116)
  %117 = load ptr, ptr %13, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %117, i32 0, i32 2
  %119 = load i32, ptr %118, align 8, !tbaa !17
  %120 = add nsw i32 %119, 1
  store i32 %120, ptr %118, align 8, !tbaa !17
  store i32 0, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %121

121:                                              ; preds = %113, %59, %34, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %122 = load i32, ptr %6, align 4
  ret i32 %122
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!12 = !{!"", !13, i64 0, !7, i64 24, !14, i64 88, !10, i64 96, !14, i64 104, !14, i64 108, !6, i64 112, !6, i64 120}
!13 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 112}
!16 = !{!12, !14, i64 104}
!17 = !{!12, !14, i64 88}
!18 = !{!12, !14, i64 108}
!19 = !{!14, !14, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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 dso_local void @mbedtls_hmac_drbg_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

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

declare i32 @mbedtls_md_hmac_reset(ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_hmac_drbg_reseed(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_random_with_add(ptr noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = tail call zeroext i8 @mbedtls_md_get_size(ptr noundef %6) #4
  %8 = zext i8 %7 to i64
  %9 = icmp ugt i64 %2, 1024
  br i1 %9, label %42, label %10

10:                                               ; preds = %5
  %11 = icmp ugt i64 %4, 256
  br i1 %11, label %42, label %12

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %0, i64 112
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %27, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 104
  %17 = load i32, ptr %16, align 8, !tbaa !14
  %18 = icmp eq i32 %17, 1
  br i1 %18, label %25, label %19

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %0, i64 88
  %21 = load i32, ptr %20, align 8, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %0, i64 108
  %23 = load i32, ptr %22, align 4, !tbaa !16
  %24 = icmp sgt i32 %21, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %19, %15
  %26 = tail call i32 @mbedtls_hmac_drbg_reseed(ptr noundef nonnull %0, ptr noundef %3, i64 noundef %4)
  %.not49 = icmp eq i32 %26, 0
  br i1 %.not49, label %.thread, label %42

27:                                               ; preds = %19, %12
  %28 = icmp ne ptr %3, null
  %29 = icmp ne i64 %4, 0
  %or.cond = and i1 %28, %29
  br i1 %or.cond, label %30, label %.thread

30:                                               ; preds = %27
  tail call void @mbedtls_hmac_drbg_update(ptr noundef nonnull %0, ptr noundef nonnull %3, i64 noundef %4)
  br label %.thread

.thread:                                          ; preds = %25, %30, %27
  %.04453 = phi i64 [ %4, %30 ], [ %4, %27 ], [ 0, %25 ]
  %.not5054 = icmp eq i64 %2, 0
  br i1 %.not5054, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.thread
  %31 = getelementptr inbounds i8, ptr %0, i64 24
  br label %32

32:                                               ; preds = %.lr.ph, %32
  %.04256 = phi ptr [ %1, %.lr.ph ], [ %37, %32 ]
  %.04355 = phi i64 [ %2, %.lr.ph ], [ %38, %32 ]
  %33 = tail call i64 @llvm.umin.i64(i64 %.04355, i64 %8)
  %34 = tail call i32 @mbedtls_md_hmac_reset(ptr noundef nonnull %0) #4
  %35 = tail call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %31, i64 noundef %8) #4
  %36 = tail call i32 @mbedtls_md_hmac_finish(ptr noundef nonnull %0, ptr noundef nonnull %31) #4
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.04256, ptr nonnull align 8 %31, i64 %33, i1 false)
  %37 = getelementptr inbounds i8, ptr %.04256, i64 %33
  %38 = sub i64 %.04355, %33
  %.not50 = icmp eq i64 %38, 0
  br i1 %.not50, label %._crit_edge, label %32, !llvm.loop !17

._crit_edge:                                      ; preds = %32, %.thread
  tail call void @mbedtls_hmac_drbg_update(ptr noundef nonnull %0, ptr noundef %3, i64 noundef %.04453)
  %39 = getelementptr inbounds i8, ptr %0, i64 88
  %40 = load i32, ptr %39, align 8, !tbaa !15
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %39, align 8, !tbaa !15
  br label %42

42:                                               ; preds = %25, %10, %5, %._crit_edge
  %.0 = phi i32 [ 0, %._crit_edge ], [ -3, %5 ], [ -5, %10 ], [ %26, %25 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !8, i64 0}
!6 = !{!"", !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 = !{!"", !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 112}
!14 = !{!6, !11, i64 104}
!15 = !{!6, !11, i64 88}
!16 = !{!6, !11, i64 108}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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