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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_ecdh_compute_shared(ptr noundef, ptr noundef, ptr noundef, ptr 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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecdh_calc_secret(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = 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 i64 %3, ptr %11, align 8, !tbaa !9
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %6
  store i32 -20352, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %63

19:                                               ; preds = %6
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %22, i32 0, i32 4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %12, align 8, !tbaa !5
  %29 = load ptr, ptr %13, align 8, !tbaa !5
  %30 = call i32 @mbedtls_ecdh_compute_shared(ptr noundef %21, ptr noundef %23, ptr noundef %25, ptr noundef %27, ptr noundef %28, ptr noundef %29)
  store i32 %30, ptr %14, align 4, !tbaa !11
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %34

32:                                               ; preds = %19
  %33 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %33, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %63

34:                                               ; preds = %19
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %35, i32 0, i32 4
  %37 = call i64 @mbedtls_mpi_size(ptr noundef %36)
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = icmp ugt i64 %37, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %34
  store i32 -20352, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %63

41:                                               ; preds = %34
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %43, i32 0, i32 6
  %45 = load i64, ptr %44, align 8, !tbaa !13
  %46 = udiv i64 %45, 8
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %48, i32 0, i32 6
  %50 = load i64, ptr %49, align 8, !tbaa !13
  %51 = urem i64 %50, 8
  %52 = icmp ne i64 %51, 0
  %53 = zext i1 %52 to i32
  %54 = sext i32 %53 to i64
  %55 = add i64 %46, %54
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %55, ptr %56, align 8, !tbaa !9
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %57, i32 0, i32 4
  %59 = load ptr, ptr %10, align 8, !tbaa !5
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = load i64, ptr %60, align 8, !tbaa !9
  %62 = call i32 @mbedtls_mpi_write_binary(ptr noundef %58, ptr noundef %59, i64 noundef %61)
  store i32 %62, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %63

63:                                               ; preds = %41, %40, %32, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %64 = load i32, ptr %7, align 4
  ret i32 %64
}

declare i64 @mbedtls_mpi_size(ptr noundef) #2

declare i32 @mbedtls_mpi_write_binary(ptr noundef, ptr noundef, i64 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 176}
!14 = !{!"", !15, i64 0, !16, i64 248, !17, i64 272, !17, i64 344, !16, i64 416, !12, i64 440, !17, i64 448, !17, i64 520, !16, i64 592}
!15 = !{!"", !12, i64 0, !16, i64 8, !16, i64 32, !16, i64 56, !17, i64 80, !16, 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}
!16 = !{!"", !12, i64 0, !10, i64 8, !6, i64 16}
!17 = !{!"", !16, i64 0, !16, i64 24, !16, 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"

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecdh_calc_secret(ptr noundef %0, ptr nocapture noundef writeonly %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = icmp eq ptr %0, null
  br i1 %7, label %25, label %8

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %0, i64 416
  %10 = getelementptr inbounds i8, ptr %0, i64 344
  %11 = getelementptr inbounds i8, ptr %0, i64 248
  %12 = tail call i32 @mbedtls_ecdh_compute_shared(ptr noundef nonnull %0, ptr noundef nonnull %9, ptr noundef nonnull %10, ptr noundef nonnull %11, ptr noundef %4, ptr noundef %5)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %25

13:                                               ; preds = %8
  %14 = tail call i64 @mbedtls_mpi_size(ptr noundef nonnull %9) #2
  %15 = icmp ugt i64 %14, %3
  br i1 %15, label %25, label %16

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %0, i64 176
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = lshr i64 %18, 3
  %20 = and i64 %18, 7
  %21 = icmp ne i64 %20, 0
  %22 = zext i1 %21 to i64
  %23 = add nuw nsw i64 %19, %22
  store i64 %23, ptr %1, align 8, !tbaa !15
  %24 = tail call i32 @mbedtls_mpi_write_binary(ptr noundef nonnull %9, ptr noundef %2, i64 noundef %23) #2
  br label %25

25:                                               ; preds = %13, %8, %6, %16
  %.0 = phi i32 [ %24, %16 ], [ -20352, %6 ], [ %12, %8 ], [ -20352, %13 ]
  ret i32 %.0
}

declare i64 @mbedtls_mpi_size(ptr noundef) local_unnamed_addr #1

declare i32 @mbedtls_mpi_write_binary(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #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 = { "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 }

!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 176}
!6 = !{!"", !7, i64 0, !11, i64 248, !14, i64 272, !14, i64 344, !11, i64 416, !8, i64 440, !14, i64 448, !14, i64 520, !11, i64 592}
!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}
!15 = !{!12, !12, i64 0}


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