; 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
define dso_local void @mbedtls_hmac_drbg_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  %9 = alloca [1 x i8], align 1
  %10 = alloca [64 x i8], align 16
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds %struct.mbedtls_md_context_t, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = call zeroext i8 @mbedtls_md_get_size(ptr noundef %14)
  %16 = zext i8 %15 to i64
  store i64 %16, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %22

19:                                               ; preds = %3
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = icmp ne i64 %20, 0
  br label %22

22:                                               ; preds = %19, %3
  %23 = phi i1 [ false, %3 ], [ %21, %19 ]
  %24 = zext i1 %23 to i64
  %25 = select i1 %23, i32 2, i32 1
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %8, align 1, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 64, ptr %10) #3
  %27 = getelementptr inbounds [1 x i8], ptr %9, i64 0, i64 0
  store i8 0, ptr %27, align 1, !tbaa !15
  br label %28

28:                                               ; preds = %82, %22
  %29 = getelementptr inbounds [1 x i8], ptr %9, i64 0, i64 0
  %30 = load i8, ptr %29, align 1, !tbaa !15
  %31 = zext i8 %30 to i32
  %32 = load i8, ptr %8, align 1, !tbaa !15
  %33 = zext i8 %32 to i32
  %34 = icmp slt i32 %31, %33
  br i1 %34, label %35, label %86

35:                                               ; preds = %28
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %36, i32 0, i32 0
  %38 = call i32 @mbedtls_md_hmac_reset(ptr noundef %37)
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %41, i32 0, i32 1
  %43 = getelementptr inbounds [64 x i8], ptr %42, i64 0, i64 0
  %44 = load i64, ptr %7, align 8, !tbaa !9
  %45 = call i32 @mbedtls_md_hmac_update(ptr noundef %40, ptr noundef %43, i64 noundef %44)
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds [1 x i8], ptr %9, i64 0, i64 0
  %49 = call i32 @mbedtls_md_hmac_update(ptr noundef %47, ptr noundef %48, i64 noundef 1)
  %50 = load i8, ptr %8, align 1, !tbaa !15
  %51 = zext i8 %50 to i32
  %52 = icmp eq i32 %51, 2
  br i1 %52, label %53, label %59

53:                                               ; preds = %35
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load i64, ptr %6, align 8, !tbaa !9
  %58 = call i32 @mbedtls_md_hmac_update(ptr noundef %55, ptr noundef %56, i64 noundef %57)
  br label %59

59:                                               ; preds = %53, %35
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  %63 = call i32 @mbedtls_md_hmac_finish(ptr noundef %61, ptr noundef %62)
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  %67 = load i64, ptr %7, align 8, !tbaa !9
  %68 = call i32 @mbedtls_md_hmac_starts(ptr noundef %65, ptr noundef %66, i64 noundef %67)
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %71, i32 0, i32 1
  %73 = getelementptr inbounds [64 x i8], ptr %72, i64 0, i64 0
  %74 = load i64, ptr %7, align 8, !tbaa !9
  %75 = call i32 @mbedtls_md_hmac_update(ptr noundef %70, ptr noundef %73, i64 noundef %74)
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %78, i32 0, i32 1
  %80 = getelementptr inbounds [64 x i8], ptr %79, i64 0, i64 0
  %81 = call i32 @mbedtls_md_hmac_finish(ptr noundef %77, ptr noundef %80)
  br label %82

82:                                               ; preds = %59
  %83 = getelementptr inbounds [1 x i8], ptr %9, i64 0, i64 0
  %84 = load i8, ptr %83, align 1, !tbaa !15
  %85 = add i8 %84, 1
  store i8 %85, ptr %83, align 1, !tbaa !15
  br label %28, !llvm.loop !16

86:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

; 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

declare i32 @mbedtls_md_hmac_starts(ptr noundef, ptr noundef, i64 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 = !{!"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 = !{!7, !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
define dso_local void @mbedtls_hmac_drbg_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca [1 x i8], align 1
  %5 = alloca [64 x i8], align 16
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = tail call zeroext i8 @mbedtls_md_get_size(ptr noundef %6) #3
  %8 = zext i8 %7 to i64
  %9 = icmp ne ptr %1, null
  %10 = icmp ne i64 %2, 0
  %11 = and i1 %9, %10
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #3
  store i8 0, ptr %4, align 1, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  br i1 %11, label %.split.us, label %.split

.split.us:                                        ; preds = %3, %.split.us
  %13 = call i32 @mbedtls_md_hmac_reset(ptr noundef nonnull %0) #3
  %14 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %12, i64 noundef %8) #3
  %15 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %4, i64 noundef 1) #3
  %16 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2) #3
  %17 = call i32 @mbedtls_md_hmac_finish(ptr noundef nonnull %0, ptr noundef nonnull %5) #3
  %18 = call i32 @mbedtls_md_hmac_starts(ptr noundef nonnull %0, ptr noundef nonnull %5, i64 noundef %8) #3
  %19 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %12, i64 noundef %8) #3
  %20 = call i32 @mbedtls_md_hmac_finish(ptr noundef nonnull %0, ptr noundef nonnull %12) #3
  %21 = load i8, ptr %4, align 1, !tbaa !13
  %22 = add i8 %21, 1
  store i8 %22, ptr %4, align 1, !tbaa !13
  %23 = icmp ult i8 %22, 2
  br i1 %23, label %.split.us, label %.split21.us, !llvm.loop !14

.split:                                           ; preds = %3, %.split
  %24 = call i32 @mbedtls_md_hmac_reset(ptr noundef nonnull %0) #3
  %25 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %12, i64 noundef %8) #3
  %26 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %4, i64 noundef 1) #3
  %27 = call i32 @mbedtls_md_hmac_finish(ptr noundef nonnull %0, ptr noundef nonnull %5) #3
  %28 = call i32 @mbedtls_md_hmac_starts(ptr noundef nonnull %0, ptr noundef nonnull %5, i64 noundef %8) #3
  %29 = call i32 @mbedtls_md_hmac_update(ptr noundef nonnull %0, ptr noundef nonnull %12, i64 noundef %8) #3
  %30 = call i32 @mbedtls_md_hmac_finish(ptr noundef nonnull %0, ptr noundef nonnull %12) #3
  %31 = load i8, ptr %4, align 1, !tbaa !13
  %32 = add i8 %31, 1
  store i8 %32, ptr %4, align 1, !tbaa !13
  %33 = icmp eq i8 %32, 0
  br i1 %33, label %.split, label %.split21.us, !llvm.loop !14

.split21.us:                                      ; preds = %.split, %.split.us
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %4) #3
  ret void
}

; Function Attrs: mustprogress 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) local_unnamed_addr #2

declare i32 @mbedtls_md_hmac_reset(ptr noundef) local_unnamed_addr #2

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

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

declare i32 @mbedtls_md_hmac_starts(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress 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 = { mustprogress 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, !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 = !{!9, !9, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass