; 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: 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_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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) #3 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store ptr %3, ptr %11, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  store i64 %5, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = call i32 @mbedtls_md_setup(ptr noundef %19, ptr noundef %20, i32 noundef 1)
  store i32 %21, ptr %14, align 4, !tbaa !11
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %6
  %24 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %24, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %75

25:                                               ; preds = %6
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = call zeroext i8 @mbedtls_md_get_size(ptr noundef %26)
  %28 = zext i8 %27 to i64
  store i64 %28, ptr %16, align 8, !tbaa !9
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %31, i32 0, i32 1
  %33 = getelementptr inbounds [64 x i8], ptr %32, i64 0, i64 0
  %34 = load i64, ptr %16, align 8, !tbaa !9
  %35 = call i32 @mbedtls_md_hmac_starts(ptr noundef %30, ptr noundef %33, i64 noundef %34)
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds [64 x i8], ptr %37, i64 0, i64 0
  %39 = load i64, ptr %16, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 8 %38, i8 1, i64 %39, i1 false)
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %41, i32 0, i32 6
  store ptr %40, ptr %42, align 8, !tbaa !13
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %44, i32 0, i32 7
  store ptr %43, ptr %45, align 8, !tbaa !16
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %46, i32 0, i32 5
  store i32 10000, ptr %47, align 4, !tbaa !17
  %48 = load i64, ptr %16, align 8, !tbaa !9
  %49 = icmp ule i64 %48, 20
  br i1 %49, label %50, label %51

50:                                               ; preds = %25
  br label %56

51:                                               ; preds = %25
  %52 = load i64, ptr %16, align 8, !tbaa !9
  %53 = icmp ule i64 %52, 28
  %54 = zext i1 %53 to i64
  %55 = select i1 %53, i32 24, i32 32
  br label %56

56:                                               ; preds = %51, %50
  %57 = phi i32 [ 16, %50 ], [ %55, %51 ]
  %58 = sext i32 %57 to i64
  store i64 %58, ptr %15, align 8, !tbaa !9
  %59 = load i64, ptr %15, align 8, !tbaa !9
  %60 = mul i64 %59, 3
  %61 = udiv i64 %60, 2
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %62, i32 0, i32 3
  store i64 %61, ptr %63, align 8, !tbaa !18
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = load ptr, ptr %12, align 8, !tbaa !5
  %66 = load i64, ptr %13, align 8, !tbaa !9
  %67 = call i32 @mbedtls_hmac_drbg_reseed(ptr noundef %64, ptr noundef %65, i64 noundef %66)
  store i32 %67, ptr %14, align 4, !tbaa !11
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %71

69:                                               ; preds = %56
  %70 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %70, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %75

71:                                               ; preds = %56
  %72 = load i64, ptr %15, align 8, !tbaa !9
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mbedtls_hmac_drbg_context, ptr %73, i32 0, i32 3
  store i64 %72, ptr %74, align 8, !tbaa !18
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %75

75:                                               ; preds = %71, %69, %23
  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 4, ptr %14) #4
  %76 = load i32, ptr %7, align 4
  ret i32 %76
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 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 #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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 112}
!14 = !{!"", !15, i64 0, !7, i64 24, !12, i64 88, !10, i64 96, !12, i64 104, !12, i64 108, !6, i64 112, !6, i64 120}
!15 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!14, !6, i64 120}
!17 = !{!14, !12, i64 108}
!18 = !{!14, !10, i64 96}


; 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

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

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

declare i32 @mbedtls_md_setup(ptr noundef, ptr noundef, i32 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 #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_hmac_drbg_seed(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) local_unnamed_addr #2 {
  %7 = tail call i32 @mbedtls_md_setup(ptr noundef %0, ptr noundef %1, i32 noundef 1) #3
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %25

8:                                                ; preds = %6
  %9 = tail call zeroext i8 @mbedtls_md_get_size(ptr noundef %1) #3
  %10 = zext i8 %9 to i64
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = tail call i32 @mbedtls_md_hmac_starts(ptr noundef %0, ptr noundef nonnull %11, i64 noundef %10) #3
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %11, i8 1, i64 %10, i1 false)
  %13 = getelementptr inbounds i8, ptr %0, i64 112
  store ptr %2, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %0, i64 120
  store ptr %3, ptr %14, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %0, i64 108
  store i32 10000, ptr %15, align 4, !tbaa !14
  %16 = icmp ult i8 %9, 21
  %17 = icmp ult i8 %9, 29
  %18 = select i1 %17, i64 24, i64 32
  %19 = select i1 %16, i64 16, i64 %18
  %20 = lshr exact i64 %19, 1
  %21 = add nuw nsw i64 %20, %19
  %22 = getelementptr inbounds i8, ptr %0, i64 96
  store i64 %21, ptr %22, align 8, !tbaa !15
  %23 = tail call i32 @mbedtls_hmac_drbg_reseed(ptr noundef %0, ptr noundef %4, i64 noundef %5)
  %.not27 = icmp eq i32 %23, 0
  br i1 %.not27, label %24, label %25

24:                                               ; preds = %8
  store i64 %19, ptr %22, align 8, !tbaa !15
  br label %25

25:                                               ; preds = %8, %6, %24
  %.0 = phi i32 [ 0, %24 ], [ %7, %6 ], [ %23, %8 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 = { 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 112}
!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 120}
!14 = !{!6, !11, i64 108}
!15 = !{!6, !12, i64 96}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass