; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (ecdsa)

; 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 }
%struct.mbedtls_mpi = type { i32, i64, ptr }
%struct.mbedtls_ecp_group = type { i32, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_ecp_point, %struct.mbedtls_mpi, i64, i64, i32, ptr, ptr, ptr, ptr, ptr, i64 }
%struct.mbedtls_ecp_point = type { %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi }

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

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

declare void @mbedtls_mpi_init(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @derive_mpi(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #0

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

declare void @mbedtls_mpi_free(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecdsa_sign_det(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5, i32 noundef %6) #0 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca %struct.mbedtls_hmac_drbg_context, align 8
  %18 = alloca [132 x i8], align 16
  %19 = alloca i64, align 8
  %20 = alloca ptr, align 8
  %21 = alloca %struct.mbedtls_mpi, align 8
  %22 = alloca i32, align 4
  store ptr %0, ptr %9, align 8, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !5
  store ptr %2, ptr %11, align 8, !tbaa !5
  store ptr %3, ptr %12, align 8, !tbaa !5
  store ptr %4, ptr %13, align 8, !tbaa !5
  store i64 %5, ptr %14, align 8, !tbaa !9
  store i32 %6, ptr %15, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 128, ptr %17) #3
  call void @llvm.lifetime.start.p0(i64 132, ptr %18) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #3
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %23, i32 0, i32 7
  %25 = load i64, ptr %24, align 8, !tbaa !13
  %26 = add i64 %25, 7
  %27 = udiv i64 %26, 8
  store i64 %27, ptr %19, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr %21) #3
  %28 = load i32, ptr %15, align 4, !tbaa !11
  %29 = call ptr @mbedtls_md_info_from_type(i32 noundef %28)
  store ptr %29, ptr %20, align 8, !tbaa !5
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %32

31:                                               ; preds = %7
  store i32 -20352, ptr %8, align 4
  store i32 1, ptr %22, align 4
  br label %78

32:                                               ; preds = %7
  call void @mbedtls_mpi_init(ptr noundef %21)
  call void @mbedtls_hmac_drbg_init(ptr noundef %17)
  br label %33

33:                                               ; preds = %32
  %34 = load ptr, ptr %12, align 8, !tbaa !5
  %35 = getelementptr inbounds [132 x i8], ptr %18, i64 0, i64 0
  %36 = load i64, ptr %19, align 8, !tbaa !9
  %37 = call i32 @mbedtls_mpi_write_binary(ptr noundef %34, ptr noundef %35, i64 noundef %36)
  store i32 %37, ptr %16, align 4, !tbaa !11
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %33
  br label %76

40:                                               ; preds = %33
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = load ptr, ptr %13, align 8, !tbaa !5
  %46 = load i64, ptr %14, align 8, !tbaa !9
  %47 = call i32 @derive_mpi(ptr noundef %44, ptr noundef %21, ptr noundef %45, i64 noundef %46)
  store i32 %47, ptr %16, align 4, !tbaa !11
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %43
  br label %76

50:                                               ; preds = %43
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  br label %53

53:                                               ; preds = %52
  %54 = getelementptr inbounds [132 x i8], ptr %18, i64 0, i64 0
  %55 = load i64, ptr %19, align 8, !tbaa !9
  %56 = getelementptr inbounds i8, ptr %54, i64 %55
  %57 = load i64, ptr %19, align 8, !tbaa !9
  %58 = call i32 @mbedtls_mpi_write_binary(ptr noundef %21, ptr noundef %56, i64 noundef %57)
  store i32 %58, ptr %16, align 4, !tbaa !11
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %53
  br label %76

61:                                               ; preds = %53
  br label %62

62:                                               ; preds = %61
  br label %63

63:                                               ; preds = %62
  %64 = load ptr, ptr %20, align 8, !tbaa !5
  %65 = getelementptr inbounds [132 x i8], ptr %18, i64 0, i64 0
  %66 = load i64, ptr %19, align 8, !tbaa !9
  %67 = mul i64 2, %66
  %68 = call i32 @mbedtls_hmac_drbg_seed_buf(ptr noundef %17, ptr noundef %64, ptr noundef %65, i64 noundef %67)
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  %70 = load ptr, ptr %10, align 8, !tbaa !5
  %71 = load ptr, ptr %11, align 8, !tbaa !5
  %72 = load ptr, ptr %12, align 8, !tbaa !5
  %73 = load ptr, ptr %13, align 8, !tbaa !5
  %74 = load i64, ptr %14, align 8, !tbaa !9
  %75 = call i32 @mbedtls_ecdsa_sign(ptr noundef %69, ptr noundef %70, ptr noundef %71, ptr noundef %72, ptr noundef %73, i64 noundef %74, ptr noundef @mbedtls_hmac_drbg_random, ptr noundef %17)
  store i32 %75, ptr %16, align 4, !tbaa !11
  br label %76

76:                                               ; preds = %63, %60, %49, %39
  call void @mbedtls_hmac_drbg_free(ptr noundef %17)
  call void @mbedtls_mpi_free(ptr noundef %21)
  %77 = load i32, ptr %16, align 4, !tbaa !11
  store i32 %77, ptr %8, align 4
  store i32 1, ptr %22, align 4
  br label %78

78:                                               ; preds = %76, %31
  call void @llvm.lifetime.end.p0(i64 24, ptr %21) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #3
  call void @llvm.lifetime.end.p0(i64 132, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  %79 = load i32, ptr %8, align 4
  ret i32 %79
}

declare ptr @mbedtls_md_info_from_type(i32 noundef) #2

declare void @mbedtls_hmac_drbg_init(ptr noundef) #2

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

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

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

declare void @mbedtls_hmac_drbg_free(ptr noundef) #2

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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 184}
!14 = !{!"", !12, i64 0, !15, i64 8, !15, i64 32, !15, i64 56, !16, i64 80, !15, i64 152, !10, i64 176, !10, i64 184, !12, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !10, i64 240}
!15 = !{!"", !12, i64 0, !10, i64 8, !6, i64 16}
!16 = !{!"", !15, i64 0, !15, i64 24, !15, i64 48}


; 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"

%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 }
%struct.mbedtls_mpi = type { i32, i64, ptr }

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

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

declare void @mbedtls_mpi_init(ptr noundef) local_unnamed_addr #2

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

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

declare void @mbedtls_mpi_free(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecdsa_sign_det(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5, i32 noundef %6) local_unnamed_addr #0 {
  %8 = alloca %struct.mbedtls_hmac_drbg_context, align 8
  %9 = alloca [132 x i8], align 16
  %10 = alloca %struct.mbedtls_mpi, align 8
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %8) #3
  call void @llvm.lifetime.start.p0(i64 132, ptr nonnull %9) #3
  %11 = getelementptr inbounds i8, ptr %0, i64 184
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = add i64 %12, 7
  %14 = lshr i64 %13, 3
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %10) #3
  %15 = tail call ptr @mbedtls_md_info_from_type(i32 noundef %6) #3
  %16 = icmp eq ptr %15, null
  br i1 %16, label %29, label %17

17:                                               ; preds = %7
  call void @mbedtls_mpi_init(ptr noundef nonnull %10) #3
  call void @mbedtls_hmac_drbg_init(ptr noundef nonnull %8) #3
  %18 = call i32 @mbedtls_mpi_write_binary(ptr noundef %3, ptr noundef nonnull %9, i64 noundef %14) #3
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %19, label %28

19:                                               ; preds = %17
  %20 = call i32 @derive_mpi(ptr noundef nonnull %0, ptr noundef nonnull %10, ptr noundef %4, i64 noundef %5)
  %.not23 = icmp eq i32 %20, 0
  br i1 %.not23, label %21, label %28

21:                                               ; preds = %19
  %22 = getelementptr inbounds i8, ptr %9, i64 %14
  %23 = call i32 @mbedtls_mpi_write_binary(ptr noundef nonnull %10, ptr noundef nonnull %22, i64 noundef %14) #3
  %.not24 = icmp eq i32 %23, 0
  br i1 %.not24, label %24, label %28

24:                                               ; preds = %21
  %25 = shl nuw nsw i64 %14, 1
  %26 = call i32 @mbedtls_hmac_drbg_seed_buf(ptr noundef nonnull %8, ptr noundef nonnull %15, ptr noundef nonnull %9, i64 noundef %25) #3
  %27 = call i32 @mbedtls_ecdsa_sign(ptr noundef nonnull %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef nonnull @mbedtls_hmac_drbg_random, ptr noundef nonnull %8)
  br label %28

28:                                               ; preds = %21, %19, %17, %24
  %.018 = phi i32 [ %18, %17 ], [ %20, %19 ], [ %23, %21 ], [ %27, %24 ]
  call void @mbedtls_hmac_drbg_free(ptr noundef nonnull %8) #3
  call void @mbedtls_mpi_free(ptr noundef nonnull %10) #3
  br label %29

29:                                               ; preds = %7, %28
  %.0 = phi i32 [ %.018, %28 ], [ -20352, %7 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %10) #3
  call void @llvm.lifetime.end.p0(i64 132, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %8) #3
  ret i32 %.0
}

declare ptr @mbedtls_md_info_from_type(i32 noundef) local_unnamed_addr #2

declare void @mbedtls_hmac_drbg_init(ptr noundef) local_unnamed_addr #2

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

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

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

declare void @mbedtls_hmac_drbg_free(ptr noundef) local_unnamed_addr #2

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, !11, i64 184}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 32, !10, i64 56, !13, i64 80, !10, i64 152, !11, i64 176, !11, i64 184, !7, i64 192, !12, i64 200, !12, i64 208, !12, i64 216, !12, i64 224, !12, i64 232, !11, i64 240}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !7, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"long", !8, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!"", !10, i64 0, !10, i64 24, !10, i64 48}


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