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

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_hmac_drbg_update(ptr noundef, ptr noundef, i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_reseed(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %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
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, 256
  br i1 %12, label %20, label %13

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

20:                                               ; preds = %13, %3
  store i32 -5, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %60

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

35:                                               ; preds = %21
  store i32 -9, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %60

36:                                               ; preds = %21
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %37, i32 0, i32 3
  %39 = load i64, ptr %38, align 8, !tbaa !11
  store i64 %39, ptr %9, align 8, !tbaa !9
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %54

42:                                               ; preds = %36
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %54

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

54:                                               ; preds = %45, %42, %36
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds [384 x i8], ptr %8, i64 0, i64 0
  %57 = load i64, ptr %9, align 8, !tbaa !9
  call void @mbedtls_hmac_drbg_update(ptr noundef %55, ptr noundef %56, i64 noundef %57)
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %58, i32 0, i32 2
  store i32 1, ptr %59, align 8, !tbaa !17
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %60

60:                                               ; preds = %54, %35, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 384, ptr %8) #4
  %61 = load i32, ptr %4, align 4
  ret i32 %61
}

; 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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 96}
!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, !6, i64 120}
!17 = !{!12, !14, i64 88}


; 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: nounwind uwtable
declare dso_local void @mbedtls_hmac_drbg_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -9, 1) i32 @mbedtls_hmac_drbg_reseed(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = alloca [384 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 384, ptr nonnull %4) #4
  %5 = icmp ugt i64 %2, 256
  br i1 %5, label %26, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 96
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = add i64 %8, %2
  %10 = icmp ugt i64 %9, 384
  br i1 %10, label %26, label %11

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

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

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

24:                                               ; preds = %21, %17
  %.0 = phi i64 [ %23, %21 ], [ %18, %17 ]
  call void @mbedtls_hmac_drbg_update(ptr noundef nonnull %0, ptr noundef nonnull %4, i64 noundef %.0)
  %25 = getelementptr inbounds i8, ptr %0, i64 88
  store i32 1, ptr %25, align 8, !tbaa !15
  br label %26

26:                                               ; preds = %11, %3, %6, %24
  %.018 = phi i32 [ 0, %24 ], [ -5, %6 ], [ -5, %3 ], [ -9, %11 ]
  call void @llvm.lifetime.end.p0(i64 384, ptr nonnull %4) #4
  ret i32 %.018
}

; 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 = { 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 #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !12, i64 96}
!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, !8, i64 120}
!15 = !{!6, !11, i64 88}


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