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

; 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

declare i32 @mbedtls_mpi_cmp_int(ptr noundef, i64 noundef) #1

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

19:                                               ; preds = %6
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = icmp ne i32 %20, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %19
  store i32 -20352, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

23:                                               ; preds = %19, %6
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %24, i32 0, i32 2
  %26 = call i32 @mbedtls_mpi_cmp_int(ptr noundef %25, i64 noundef 0)
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %36

28:                                               ; preds = %23
  %29 = load i64, ptr %13, align 8, !tbaa !11
  %30 = icmp ult i64 %29, 1
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  store i32 -20224, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

32:                                               ; preds = %28
  %33 = load ptr, ptr %12, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i64 0
  store i8 0, ptr %34, align 1, !tbaa !13
  %35 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 1, ptr %35, align 8, !tbaa !11
  store i32 0, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

36:                                               ; preds = %23
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mbedtls_ecp_group, ptr %37, i32 0, i32 1
  %39 = call i64 @mbedtls_mpi_size(ptr noundef %38)
  store i64 %39, ptr %15, align 8, !tbaa !11
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %81

42:                                               ; preds = %36
  %43 = load i64, ptr %15, align 8, !tbaa !11
  %44 = mul i64 2, %43
  %45 = add i64 %44, 1
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 %45, ptr %46, align 8, !tbaa !11
  %47 = load i64, ptr %13, align 8, !tbaa !11
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = icmp ult i64 %47, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %42
  store i32 -20224, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

52:                                               ; preds = %42
  %53 = load ptr, ptr %12, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 0
  store i8 4, ptr %54, align 1, !tbaa !13
  br label %55

55:                                               ; preds = %52
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %12, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i64 1
  %60 = load i64, ptr %15, align 8, !tbaa !11
  %61 = call i32 @mbedtls_mpi_write_binary(ptr noundef %57, ptr noundef %59, i64 noundef %60)
  store i32 %61, ptr %14, align 4, !tbaa !9
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %64

63:                                               ; preds = %55
  br label %115

64:                                               ; preds = %55
  br label %65

65:                                               ; preds = %64
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %68, i32 0, i32 1
  %70 = load ptr, ptr %12, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i64 1
  %72 = load i64, ptr %15, align 8, !tbaa !11
  %73 = getelementptr inbounds i8, ptr %71, i64 %72
  %74 = load i64, ptr %15, align 8, !tbaa !11
  %75 = call i32 @mbedtls_mpi_write_binary(ptr noundef %69, ptr noundef %73, i64 noundef %74)
  store i32 %75, ptr %14, align 4, !tbaa !9
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %78

77:                                               ; preds = %67
  br label %115

78:                                               ; preds = %67
  br label %79

79:                                               ; preds = %78
  br label %80

80:                                               ; preds = %79
  br label %114

81:                                               ; preds = %36
  %82 = load i32, ptr %10, align 4, !tbaa !9
  %83 = icmp eq i32 %82, 1
  br i1 %83, label %84, label %113

84:                                               ; preds = %81
  %85 = load i64, ptr %15, align 8, !tbaa !11
  %86 = add i64 %85, 1
  %87 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 %86, ptr %87, align 8, !tbaa !11
  %88 = load i64, ptr %13, align 8, !tbaa !11
  %89 = load ptr, ptr %11, align 8, !tbaa !5
  %90 = load i64, ptr %89, align 8, !tbaa !11
  %91 = icmp ult i64 %88, %90
  br i1 %91, label %92, label %93

92:                                               ; preds = %84
  store i32 -20224, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

93:                                               ; preds = %84
  %94 = load ptr, ptr %9, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %94, i32 0, i32 1
  %96 = call i32 @mbedtls_mpi_get_bit(ptr noundef %95, i64 noundef 0)
  %97 = add nsw i32 2, %96
  %98 = trunc i32 %97 to i8
  %99 = load ptr, ptr %12, align 8, !tbaa !5
  %100 = getelementptr inbounds i8, ptr %99, i64 0
  store i8 %98, ptr %100, align 1, !tbaa !13
  br label %101

101:                                              ; preds = %93
  %102 = load ptr, ptr %9, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.mbedtls_ecp_point, ptr %102, i32 0, i32 0
  %104 = load ptr, ptr %12, align 8, !tbaa !5
  %105 = getelementptr inbounds i8, ptr %104, i64 1
  %106 = load i64, ptr %15, align 8, !tbaa !11
  %107 = call i32 @mbedtls_mpi_write_binary(ptr noundef %103, ptr noundef %105, i64 noundef %106)
  store i32 %107, ptr %14, align 4, !tbaa !9
  %108 = icmp ne i32 %107, 0
  br i1 %108, label %109, label %110

109:                                              ; preds = %101
  br label %115

110:                                              ; preds = %101
  br label %111

111:                                              ; preds = %110
  br label %112

112:                                              ; preds = %111
  br label %113

113:                                              ; preds = %112, %81
  br label %114

114:                                              ; preds = %113, %80
  br label %115

115:                                              ; preds = %114, %109, %77, %63
  %116 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %116, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %117

117:                                              ; preds = %115, %92, %51, %32, %31, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %118 = load i32, ptr %7, align 4
  ret i32 %118
}

declare i64 @mbedtls_mpi_size(ptr noundef) #1

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

declare i32 @mbedtls_mpi_get_bit(ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, 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"

declare i32 @mbedtls_mpi_cmp_int(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecp_point_write_binary(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3, ptr noundef %4, i64 noundef %5) local_unnamed_addr #1 {
  %or.cond = icmp ugt i32 %2, 1
  br i1 %or.cond, label %37, label %7

7:                                                ; preds = %6
  %8 = getelementptr inbounds i8, ptr %1, i64 48
  %9 = tail call i32 @mbedtls_mpi_cmp_int(ptr noundef nonnull %8, i64 noundef 0) #2
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %7
  %12 = icmp eq i64 %5, 0
  br i1 %12, label %37, label %13

13:                                               ; preds = %11
  store i8 0, ptr %4, align 1, !tbaa !5
  store i64 1, ptr %3, align 8, !tbaa !8
  br label %37

14:                                               ; preds = %7
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = tail call i64 @mbedtls_mpi_size(ptr noundef nonnull %15) #2
  %switch = icmp eq i32 %2, 0
  br i1 %switch, label %17, label %27

17:                                               ; preds = %14
  %18 = shl i64 %16, 1
  %19 = or disjoint i64 %18, 1
  store i64 %19, ptr %3, align 8, !tbaa !8
  %.not = icmp ult i64 %18, %5
  br i1 %.not, label %20, label %37

20:                                               ; preds = %17
  store i8 4, ptr %4, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %4, i64 1
  %22 = tail call i32 @mbedtls_mpi_write_binary(ptr noundef %1, ptr noundef nonnull %21, i64 noundef %16) #2
  %.not35 = icmp eq i32 %22, 0
  br i1 %.not35, label %23, label %37

23:                                               ; preds = %20
  %24 = getelementptr inbounds i8, ptr %1, i64 24
  %25 = getelementptr inbounds i8, ptr %21, i64 %16
  %26 = tail call i32 @mbedtls_mpi_write_binary(ptr noundef nonnull %24, ptr noundef nonnull %25, i64 noundef %16) #2
  br label %37

27:                                               ; preds = %14
  %28 = add i64 %16, 1
  store i64 %28, ptr %3, align 8, !tbaa !8
  %29 = icmp ugt i64 %28, %5
  br i1 %29, label %37, label %30

30:                                               ; preds = %27
  %31 = getelementptr inbounds i8, ptr %1, i64 24
  %32 = tail call i32 @mbedtls_mpi_get_bit(ptr noundef nonnull %31, i64 noundef 0) #2
  %33 = trunc i32 %32 to i8
  %34 = add i8 %33, 2
  store i8 %34, ptr %4, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %4, i64 1
  %36 = tail call i32 @mbedtls_mpi_write_binary(ptr noundef %1, ptr noundef nonnull %35, i64 noundef %16) #2
  br label %37

37:                                               ; preds = %20, %23, %30, %27, %17, %11, %6, %13
  %.0 = phi i32 [ 0, %13 ], [ -20352, %6 ], [ -20224, %11 ], [ -20224, %17 ], [ -20224, %27 ], [ %22, %20 ], [ %26, %23 ], [ %36, %30 ]
  ret i32 %.0
}

declare i64 @mbedtls_mpi_size(ptr noundef) local_unnamed_addr #0

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

declare i32 @mbedtls_mpi_get_bit(ptr noundef, i64 noundef) local_unnamed_addr #0

attributes #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 = { 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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}


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