; 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_gen_public(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_make_params(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 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 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %26, label %20

20:                                               ; preds = %6
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %22, i32 0, i32 6
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %20, %6
  store i32 -20352, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %74

27:                                               ; preds = %20
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %12, align 8, !tbaa !5
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = call i32 @mbedtls_ecdh_gen_public(ptr noundef %29, ptr noundef %31, ptr noundef %33, ptr noundef %34, ptr noundef %35)
  store i32 %36, ptr %14, align 4, !tbaa !17
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %40

38:                                               ; preds = %27
  %39 = load i32, ptr %14, align 4, !tbaa !17
  store i32 %39, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %74

40:                                               ; preds = %27
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = load i64, ptr %11, align 8, !tbaa !9
  %45 = call i32 @mbedtls_ecp_tls_write_group(ptr noundef %42, ptr noundef %15, ptr noundef %43, i64 noundef %44)
  store i32 %45, ptr %14, align 4, !tbaa !17
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %49

47:                                               ; preds = %40
  %48 = load i32, ptr %14, align 4, !tbaa !17
  store i32 %48, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %74

49:                                               ; preds = %40
  %50 = load i64, ptr %15, align 8, !tbaa !9
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %51, i64 %50
  store ptr %52, ptr %10, align 8, !tbaa !5
  %53 = load i64, ptr %15, align 8, !tbaa !9
  %54 = load i64, ptr %11, align 8, !tbaa !9
  %55 = sub i64 %54, %53
  store i64 %55, ptr %11, align 8, !tbaa !9
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_ecdh_context, ptr %60, i32 0, i32 5
  %62 = load i32, ptr %61, align 8, !tbaa !18
  %63 = load ptr, ptr %10, align 8, !tbaa !5
  %64 = load i64, ptr %11, align 8, !tbaa !9
  %65 = call i32 @mbedtls_ecp_tls_write_point(ptr noundef %57, ptr noundef %59, i32 noundef %62, ptr noundef %16, ptr noundef %63, i64 noundef %64)
  store i32 %65, ptr %14, align 4, !tbaa !17
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %69

67:                                               ; preds = %49
  %68 = load i32, ptr %14, align 4, !tbaa !17
  store i32 %68, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %74

69:                                               ; preds = %49
  %70 = load i64, ptr %15, align 8, !tbaa !9
  %71 = load i64, ptr %16, align 8, !tbaa !9
  %72 = add i64 %70, %71
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %72, ptr %73, align 8, !tbaa !9
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %74

74:                                               ; preds = %69, %67, %47, %38, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %75 = load i32, ptr %7, align 4
  ret i32 %75
}

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

declare i32 @mbedtls_ecp_tls_write_point(ptr noundef, ptr noundef, i32 noundef, 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, !10, i64 176}
!12 = !{!"", !13, i64 0, !15, i64 248, !16, i64 272, !16, i64 344, !15, i64 416, !14, i64 440, !16, i64 448, !16, i64 520, !15, i64 592}
!13 = !{!"", !14, i64 0, !15, i64 8, !15, i64 32, !15, i64 56, !16, i64 80, !15, i64 152, !10, i64 176, !10, i64 184, !14, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !10, i64 240}
!14 = !{!"int", !7, i64 0}
!15 = !{!"", !14, i64 0, !10, i64 8, !6, i64 16}
!16 = !{!"", !15, i64 0, !15, i64 24, !15, i64 48}
!17 = !{!14, !14, i64 0}
!18 = !{!12, !14, i64 440}


; 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_gen_public(ptr noundef, ptr noundef, ptr 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

; Function Attrs: mustprogress 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_make_params(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 = alloca i64, align 8
  %8 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #3
  %9 = icmp eq ptr %0, null
  br i1 %9, label %31, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 176
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %31, label %14

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %0, i64 248
  %16 = getelementptr inbounds i8, ptr %0, i64 272
  %17 = tail call i32 @mbedtls_ecdh_gen_public(ptr noundef nonnull %0, ptr noundef nonnull %15, ptr noundef nonnull %16, ptr noundef %4, ptr noundef %5)
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %18, label %31

18:                                               ; preds = %14
  %19 = call i32 @mbedtls_ecp_tls_write_group(ptr noundef nonnull %0, ptr noundef nonnull %7, ptr noundef %2, i64 noundef %3) #3
  %.not25 = icmp eq i32 %19, 0
  br i1 %.not25, label %20, label %31

20:                                               ; preds = %18
  %21 = load i64, ptr %7, align 8, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %2, i64 %21
  %23 = sub i64 %3, %21
  %24 = getelementptr inbounds i8, ptr %0, i64 440
  %25 = load i32, ptr %24, align 8, !tbaa !16
  %26 = call i32 @mbedtls_ecp_tls_write_point(ptr noundef nonnull %0, ptr noundef nonnull %16, i32 noundef %25, ptr noundef nonnull %8, ptr noundef %22, i64 noundef %23) #3
  %.not26 = icmp eq i32 %26, 0
  br i1 %.not26, label %27, label %31

27:                                               ; preds = %20
  %28 = load i64, ptr %7, align 8, !tbaa !15
  %29 = load i64, ptr %8, align 8, !tbaa !15
  %30 = add i64 %29, %28
  store i64 %30, ptr %1, align 8, !tbaa !15
  br label %31

31:                                               ; preds = %20, %18, %14, %6, %10, %27
  %.0 = phi i32 [ 0, %27 ], [ -20352, %10 ], [ -20352, %6 ], [ %17, %14 ], [ %19, %18 ], [ %26, %20 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #3
  ret i32 %.0
}

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

declare i32 @mbedtls_ecp_tls_write_point(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, i64 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, !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}
!16 = !{!6, !8, i64 440}


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