; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (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: nounwind uwtable
define dso_local i32 @mbedtls_ctr_drbg_reseed(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca [384 x i8], align 16
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 384, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %11, i32 0, i32 3
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = icmp ugt i64 %13, 384
  br i1 %14, label %22, label %15

15:                                               ; preds = %3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %17, i32 0, i32 3
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = sub i64 384, %19
  %21 = icmp ugt i64 %16, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %15, %3
  store i32 -56, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

23:                                               ; preds = %15
  %24 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %24, i8 0, i64 384, i1 false)
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %25, i32 0, i32 6
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %28, i32 0, i32 7
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %32, i32 0, i32 3
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = call i32 %27(ptr noundef %30, ptr noundef %31, i64 noundef %34)
  %36 = icmp ne i32 0, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %23
  store i32 -52, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

38:                                               ; preds = %23
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %39, i32 0, i32 3
  %41 = load i64, ptr %40, align 8, !tbaa !11
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = add i64 %42, %41
  store i64 %43, ptr %9, align 8, !tbaa !9
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = icmp ne ptr %44, null
  br i1 %45, label %46, label %58

46:                                               ; preds = %38
  %47 = load i64, ptr %7, align 8, !tbaa !9
  %48 = icmp ne i64 %47, 0
  br i1 %48, label %49, label %58

49:                                               ; preds = %46
  %50 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %51 = load i64, ptr %9, align 8, !tbaa !9
  %52 = getelementptr inbounds i8, ptr %50, i64 %51
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 1 %53, i64 %54, i1 false)
  %55 = load i64, ptr %7, align 8, !tbaa !9
  %56 = load i64, ptr %9, align 8, !tbaa !9
  %57 = add i64 %56, %55
  store i64 %57, ptr %9, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %49, %46, %38
  %59 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %60 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %61 = load i64, ptr %9, align 8, !tbaa !9
  %62 = call i32 @block_cipher_df(ptr noundef %59, ptr noundef %60, i64 noundef %61)
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %65 = call i32 @ctr_drbg_update_internal(ptr noundef %63, ptr noundef %64)
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.mbedtls_ctr_drbg_context, ptr %66, i32 0, i32 1
  store i32 1, ptr %67, align 8, !tbaa !17
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %68

68:                                               ; preds = %58, %37, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 384, ptr %8) #4
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

; 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 @block_cipher_df(ptr noundef, ptr noundef, i64 noundef) #2

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

; 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

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 = { 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, !10, i64 24}
!12 = !{!"", !7, i64 0, !13, i64 16, !13, i64 20, !10, i64 24, !13, i64 32, !14, i64 40, !6, i64 328, !6, i64 336}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !6, i64 8, !7, i64 16}
!15 = !{!12, !6, i64 328}
!16 = !{!12, !6, i64 336}
!17 = !{!12, !13, i64 16}


; 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: nounwind uwtable
define dso_local range(i32 -56, 1) i32 @mbedtls_ctr_drbg_reseed(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [384 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 384, ptr nonnull %4) #4
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = icmp ugt i64 %6, 384
  %8 = sub nuw nsw i64 384, %6
  %9 = icmp ult i64 %8, %2
  %or.cond24 = select i1 %7, i1 true, i1 %9
  br i1 %or.cond24, label %27, label %10

10:                                               ; preds = %3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(384) %4, i8 0, i64 384, i1 false)
  %11 = getelementptr inbounds i8, ptr %0, i64 328
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %0, i64 336
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = call i32 %12(ptr noundef %14, ptr noundef nonnull %4, i64 noundef %6) #4
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %27

16:                                               ; preds = %10
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = icmp ne ptr %1, null
  %19 = icmp ne i64 %2, 0
  %or.cond = and i1 %18, %19
  br i1 %or.cond, label %20, label %23

20:                                               ; preds = %16
  %21 = getelementptr inbounds i8, ptr %4, i64 %17
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %21, ptr nonnull align 1 %1, i64 %2, i1 false)
  %22 = add i64 %17, %2
  br label %23

23:                                               ; preds = %20, %16
  %.0 = phi i64 [ %22, %20 ], [ %17, %16 ]
  %24 = call i32 @block_cipher_df(ptr noundef nonnull %4, ptr noundef nonnull %4, i64 noundef %.0)
  %25 = call i32 @ctr_drbg_update_internal(ptr noundef nonnull %0, ptr noundef nonnull %4)
  %26 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 1, ptr %26, align 8, !tbaa !15
  br label %27

27:                                               ; preds = %10, %3, %23
  %.019 = phi i32 [ 0, %23 ], [ -56, %3 ], [ -52, %10 ]
  call void @llvm.lifetime.end.p0(i64 384, ptr nonnull %4) #4
  ret i32 %.019
}

; 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 @block_cipher_df(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

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

; 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) #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 = { mustprogress 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, !10, i64 24}
!6 = !{!"", !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 = !{!"", !9, i64 0, !12, i64 8, !7, i64 16}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!6, !12, i64 328}
!14 = !{!6, !12, i64 336}
!15 = !{!6, !9, i64 16}


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