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

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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ecp_group_copy(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ecp_mul(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecp_check_pub_priv(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.mbedtls_ecp_point, align 8
  %8 = alloca %struct.mbedtls_ecp_group, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 248, ptr %8) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %10, i32 0, i32 0
  %12 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %52, label %15

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %16, i32 0, i32 0
  %18 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !9
  %24 = icmp ne i32 %19, %23
  br i1 %24, label %52, label %25

25:                                               ; preds = %15
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %26, i32 0, i32 2
  %28 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %29, i32 0, i32 2
  %31 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %30, i32 0, i32 0
  %32 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %28, ptr noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %52, label %34

34:                                               ; preds = %25
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %35, i32 0, i32 2
  %37 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %38, i32 0, i32 2
  %40 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %39, i32 0, i32 1
  %41 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %37, ptr noundef %40)
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %52, label %43

43:                                               ; preds = %34
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %47, i32 0, i32 2
  %49 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %48, i32 0, i32 2
  %50 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %46, ptr noundef %49)
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %53

52:                                               ; preds = %43, %34, %25, %15, %2
  store i32 -20352, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %93

53:                                               ; preds = %43
  call void @mbedtls_ecp_point_init(ptr noundef %7)
  call void @mbedtls_ecp_group_init(ptr noundef %8)
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %54, i32 0, i32 0
  %56 = call i32 @mbedtls_ecp_group_copy(ptr noundef %8, ptr noundef %55)
  br label %57

57:                                               ; preds = %53
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %61, i32 0, i32 4
  %63 = call i32 @mbedtls_ecp_mul(ptr noundef %8, ptr noundef %7, ptr noundef %59, ptr noundef %62, ptr noundef null, ptr noundef null)
  store i32 %63, ptr %6, align 4, !tbaa !16
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %57
  br label %91

66:                                               ; preds = %57
  br label %67

67:                                               ; preds = %66
  br label %68

68:                                               ; preds = %67
  %69 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %7, i32 0, i32 0
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %70, i32 0, i32 2
  %72 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %71, i32 0, i32 0
  %73 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %69, ptr noundef %72)
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %89, label %75

75:                                               ; preds = %68
  %76 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %7, i32 0, i32 1
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %77, i32 0, i32 2
  %79 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %78, i32 0, i32 1
  %80 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %76, ptr noundef %79)
  %81 = icmp ne i32 %80, 0
  br i1 %81, label %89, label %82

82:                                               ; preds = %75
  %83 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %7, i32 0, i32 2
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.mbedtls_ecp_keypair, ptr %84, i32 0, i32 2
  %86 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %85, i32 0, i32 2
  %87 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %83, ptr noundef %86)
  %88 = icmp ne i32 %87, 0
  br i1 %88, label %89, label %90

89:                                               ; preds = %82, %75, %68
  store i32 -20352, ptr %6, align 4, !tbaa !16
  br label %91

90:                                               ; preds = %82
  br label %91

91:                                               ; preds = %90, %89, %65
  call void @mbedtls_ecp_point_free(ptr noundef %7)
  call void @mbedtls_ecp_group_free(ptr noundef %8)
  %92 = load i32, ptr %6, align 4, !tbaa !16
  store i32 %92, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %93

93:                                               ; preds = %91, %52
  call void @llvm.lifetime.end.p0(i64 248, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %94 = load i32, ptr %3, align 4
  ret i32 %94
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !12, i64 0}
!10 = !{!"", !11, i64 0, !13, i64 248, !15, i64 272}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 32, !13, i64 56, !15, i64 80, !13, i64 152, !14, i64 176, !14, i64 184, !12, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !14, i64 240}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !12, i64 0, !14, i64 8, !6, i64 16}
!14 = !{!"long", !7, i64 0}
!15 = !{!"", !13, i64 0, !13, i64 24, !13, i64 48}
!16 = !{!12, !12, i64 0}


; 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_ecp_point = type { %struct.mbedtls_mpi, %struct.mbedtls_mpi, %struct.mbedtls_mpi }
%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 }

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_ecp_point_init(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_ecp_group_init(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_ecp_point_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_ecp_group_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ecp_group_copy(ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ecp_mul(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecp_check_pub_priv(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca %struct.mbedtls_ecp_point, align 8
  %4 = alloca %struct.mbedtls_ecp_group, align 8
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 248, ptr nonnull %4) #3
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %36, label %7

7:                                                ; preds = %2
  %8 = load i32, ptr %1, align 8, !tbaa !5
  %.not = icmp eq i32 %5, %8
  br i1 %.not, label %9, label %36

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %0, i64 272
  %11 = getelementptr inbounds i8, ptr %1, i64 272
  %12 = tail call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %10, ptr noundef nonnull %11) #3
  %.not19 = icmp eq i32 %12, 0
  br i1 %.not19, label %13, label %36

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %0, i64 296
  %15 = getelementptr inbounds i8, ptr %1, i64 296
  %16 = tail call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %14, ptr noundef nonnull %15) #3
  %.not20 = icmp eq i32 %16, 0
  br i1 %.not20, label %17, label %36

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 320
  %19 = getelementptr inbounds i8, ptr %1, i64 320
  %20 = tail call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %18, ptr noundef nonnull %19) #3
  %.not21 = icmp eq i32 %20, 0
  br i1 %.not21, label %21, label %36

21:                                               ; preds = %17
  call void @mbedtls_ecp_point_init(ptr noundef nonnull %3)
  call void @mbedtls_ecp_group_init(ptr noundef nonnull %4)
  %22 = call i32 @mbedtls_ecp_group_copy(ptr noundef nonnull %4, ptr noundef nonnull %1)
  %23 = getelementptr inbounds i8, ptr %1, i64 248
  %24 = getelementptr inbounds i8, ptr %1, i64 80
  %25 = call i32 @mbedtls_ecp_mul(ptr noundef nonnull %4, ptr noundef nonnull %3, ptr noundef nonnull %23, ptr noundef nonnull %24, ptr noundef null, ptr noundef null)
  %.not22 = icmp eq i32 %25, 0
  br i1 %.not22, label %26, label %35

26:                                               ; preds = %21
  %27 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %3, ptr noundef nonnull %11) #3
  %.not23 = icmp eq i32 %27, 0
  br i1 %.not23, label %28, label %34

28:                                               ; preds = %26
  %29 = getelementptr inbounds i8, ptr %3, i64 24
  %30 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %29, ptr noundef nonnull %15) #3
  %.not24 = icmp eq i32 %30, 0
  br i1 %.not24, label %31, label %34

31:                                               ; preds = %28
  %32 = getelementptr inbounds i8, ptr %3, i64 48
  %33 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %32, ptr noundef nonnull %19) #3
  %.not25 = icmp eq i32 %33, 0
  br i1 %.not25, label %35, label %34

34:                                               ; preds = %31, %28, %26
  br label %35

35:                                               ; preds = %31, %21, %34
  %.0 = phi i32 [ %25, %21 ], [ -20352, %34 ], [ 0, %31 ]
  call void @mbedtls_ecp_point_free(ptr noundef nonnull %3)
  call void @mbedtls_ecp_group_free(ptr noundef nonnull %4)
  br label %36

36:                                               ; preds = %2, %7, %9, %13, %17, %35
  %.016 = phi i32 [ %.0, %35 ], [ -20352, %17 ], [ -20352, %13 ], [ -20352, %9 ], [ -20352, %7 ], [ -20352, %2 ]
  call void @llvm.lifetime.end.p0(i64 248, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %3) #3
  ret i32 %.016
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !11, i64 248, !14, i64 272}
!7 = !{!"", !8, i64 0, !11, i64 8, !11, i64 32, !11, i64 56, !14, i64 80, !11, i64 152, !12, i64 176, !12, i64 184, !8, i64 192, !13, i64 200, !13, i64 208, !13, i64 216, !13, i64 224, !13, i64 232, !12, i64 240}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"", !8, i64 0, !12, i64 8, !13, i64 16}
!12 = !{!"long", !9, i64 0}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!"", !11, i64 0, !11, i64 24, !11, i64 48}


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