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

; 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_mpi = type { i32, i64, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_init(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_free(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
declare dso_local i32 @mbedtls_mpi_copy(ptr noundef, ptr noundef) #0

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_gcd(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca %struct.mbedtls_mpi, align 8
  %11 = alloca %struct.mbedtls_mpi, align 8
  %12 = alloca %struct.mbedtls_mpi, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 24, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 24, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 24, ptr %12) #2
  call void @mbedtls_mpi_init(ptr noundef %10)
  call void @mbedtls_mpi_init(ptr noundef %11)
  call void @mbedtls_mpi_init(ptr noundef %12)
  br label %13

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = call i32 @mbedtls_mpi_copy(ptr noundef %11, ptr noundef %14)
  store i32 %15, ptr %7, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  br label %125

18:                                               ; preds = %13
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19
  br label %21

21:                                               ; preds = %20
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = call i32 @mbedtls_mpi_copy(ptr noundef %12, ptr noundef %22)
  store i32 %23, ptr %7, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  br label %125

26:                                               ; preds = %21
  br label %27

27:                                               ; preds = %26
  br label %28

28:                                               ; preds = %27
  %29 = call i64 @mbedtls_mpi_lsb(ptr noundef %11)
  store i64 %29, ptr %8, align 8, !tbaa !11
  %30 = call i64 @mbedtls_mpi_lsb(ptr noundef %12)
  store i64 %30, ptr %9, align 8, !tbaa !11
  %31 = load i64, ptr %9, align 8, !tbaa !11
  %32 = load i64, ptr %8, align 8, !tbaa !11
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %34, label %36

34:                                               ; preds = %28
  %35 = load i64, ptr %9, align 8, !tbaa !11
  store i64 %35, ptr %8, align 8, !tbaa !11
  br label %36

36:                                               ; preds = %34, %28
  br label %37

37:                                               ; preds = %36
  %38 = load i64, ptr %8, align 8, !tbaa !11
  %39 = call i32 @mbedtls_mpi_shift_r(ptr noundef %11, i64 noundef %38)
  store i32 %39, ptr %7, align 4, !tbaa !9
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %37
  br label %125

42:                                               ; preds = %37
  br label %43

43:                                               ; preds = %42
  br label %44

44:                                               ; preds = %43
  br label %45

45:                                               ; preds = %44
  %46 = load i64, ptr %8, align 8, !tbaa !11
  %47 = call i32 @mbedtls_mpi_shift_r(ptr noundef %12, i64 noundef %46)
  store i32 %47, ptr %7, align 4, !tbaa !9
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %45
  br label %125

50:                                               ; preds = %45
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  %53 = getelementptr inbounds %struct.mbedtls_mpi, ptr %12, i32 0, i32 0
  store i32 1, ptr %53, align 8, !tbaa !13
  %54 = getelementptr inbounds %struct.mbedtls_mpi, ptr %11, i32 0, i32 0
  store i32 1, ptr %54, align 8, !tbaa !13
  br label %55

55:                                               ; preds = %107, %52
  %56 = call i32 @mbedtls_mpi_cmp_int(ptr noundef %11, i64 noundef 0)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %108

58:                                               ; preds = %55
  br label %59

59:                                               ; preds = %58
  %60 = call i64 @mbedtls_mpi_lsb(ptr noundef %11)
  %61 = call i32 @mbedtls_mpi_shift_r(ptr noundef %11, i64 noundef %60)
  store i32 %61, ptr %7, align 4, !tbaa !9
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %64

63:                                               ; preds = %59
  br label %125

64:                                               ; preds = %59
  br label %65

65:                                               ; preds = %64
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  %68 = call i64 @mbedtls_mpi_lsb(ptr noundef %12)
  %69 = call i32 @mbedtls_mpi_shift_r(ptr noundef %12, i64 noundef %68)
  store i32 %69, ptr %7, align 4, !tbaa !9
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %67
  br label %125

72:                                               ; preds = %67
  br label %73

73:                                               ; preds = %72
  br label %74

74:                                               ; preds = %73
  %75 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef %11, ptr noundef %12)
  %76 = icmp sge i32 %75, 0
  br i1 %76, label %77, label %92

77:                                               ; preds = %74
  br label %78

78:                                               ; preds = %77
  %79 = call i32 @mbedtls_mpi_sub_abs(ptr noundef %11, ptr noundef %11, ptr noundef %12)
  store i32 %79, ptr %7, align 4, !tbaa !9
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %78
  br label %125

82:                                               ; preds = %78
  br label %83

83:                                               ; preds = %82
  br label %84

84:                                               ; preds = %83
  br label %85

85:                                               ; preds = %84
  %86 = call i32 @mbedtls_mpi_shift_r(ptr noundef %11, i64 noundef 1)
  store i32 %86, ptr %7, align 4, !tbaa !9
  %87 = icmp ne i32 %86, 0
  br i1 %87, label %88, label %89

88:                                               ; preds = %85
  br label %125

89:                                               ; preds = %85
  br label %90

90:                                               ; preds = %89
  br label %91

91:                                               ; preds = %90
  br label %107

92:                                               ; preds = %74
  br label %93

93:                                               ; preds = %92
  %94 = call i32 @mbedtls_mpi_sub_abs(ptr noundef %12, ptr noundef %12, ptr noundef %11)
  store i32 %94, ptr %7, align 4, !tbaa !9
  %95 = icmp ne i32 %94, 0
  br i1 %95, label %96, label %97

96:                                               ; preds = %93
  br label %125

97:                                               ; preds = %93
  br label %98

98:                                               ; preds = %97
  br label %99

99:                                               ; preds = %98
  br label %100

100:                                              ; preds = %99
  %101 = call i32 @mbedtls_mpi_shift_r(ptr noundef %12, i64 noundef 1)
  store i32 %101, ptr %7, align 4, !tbaa !9
  %102 = icmp ne i32 %101, 0
  br i1 %102, label %103, label %104

103:                                              ; preds = %100
  br label %125

104:                                              ; preds = %100
  br label %105

105:                                              ; preds = %104
  br label %106

106:                                              ; preds = %105
  br label %107

107:                                              ; preds = %106, %91
  br label %55, !llvm.loop !15

108:                                              ; preds = %55
  br label %109

109:                                              ; preds = %108
  %110 = load i64, ptr %8, align 8, !tbaa !11
  %111 = call i32 @mbedtls_mpi_shift_l(ptr noundef %12, i64 noundef %110)
  store i32 %111, ptr %7, align 4, !tbaa !9
  %112 = icmp ne i32 %111, 0
  br i1 %112, label %113, label %114

113:                                              ; preds = %109
  br label %125

114:                                              ; preds = %109
  br label %115

115:                                              ; preds = %114
  br label %116

116:                                              ; preds = %115
  br label %117

117:                                              ; preds = %116
  %118 = load ptr, ptr %4, align 8, !tbaa !5
  %119 = call i32 @mbedtls_mpi_copy(ptr noundef %118, ptr noundef %12)
  store i32 %119, ptr %7, align 4, !tbaa !9
  %120 = icmp ne i32 %119, 0
  br i1 %120, label %121, label %122

121:                                              ; preds = %117
  br label %125

122:                                              ; preds = %117
  br label %123

123:                                              ; preds = %122
  br label %124

124:                                              ; preds = %123
  br label %125

125:                                              ; preds = %124, %121, %113, %103, %96, %88, %81, %71, %63, %49, %41, %25, %17
  call void @mbedtls_mpi_free(ptr noundef %10)
  call void @mbedtls_mpi_free(ptr noundef %11)
  call void @mbedtls_mpi_free(ptr noundef %12)
  %126 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 24, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %126
}

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 = { 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 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !12, i64 8, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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_mpi = type { i32, i64, ptr }

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

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_mpi_free(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
declare dso_local i32 @mbedtls_mpi_copy(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_gcd(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.mbedtls_mpi, align 8
  %5 = alloca %struct.mbedtls_mpi, align 8
  %6 = alloca %struct.mbedtls_mpi, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %6) #3
  call void @mbedtls_mpi_init(ptr noundef nonnull %4)
  call void @mbedtls_mpi_init(ptr noundef nonnull %5)
  call void @mbedtls_mpi_init(ptr noundef nonnull %6)
  %7 = call i32 @mbedtls_mpi_copy(ptr noundef nonnull %5, ptr noundef %1)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %.loopexit

8:                                                ; preds = %3
  %9 = call i32 @mbedtls_mpi_copy(ptr noundef nonnull %6, ptr noundef %2)
  %.not23 = icmp eq i32 %9, 0
  br i1 %.not23, label %10, label %.loopexit

10:                                               ; preds = %8
  %11 = call i64 @mbedtls_mpi_lsb(ptr noundef nonnull %5)
  %12 = call i64 @mbedtls_mpi_lsb(ptr noundef nonnull %6)
  %spec.select = call i64 @llvm.umin.i64(i64 %12, i64 %11)
  %13 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %5, i64 noundef %spec.select)
  %.not24 = icmp eq i32 %13, 0
  br i1 %.not24, label %14, label %.loopexit

14:                                               ; preds = %10
  %15 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %6, i64 noundef %spec.select)
  %.not25 = icmp eq i32 %15, 0
  br i1 %.not25, label %16, label %.loopexit

16:                                               ; preds = %14
  store i32 1, ptr %6, align 8, !tbaa !5
  store i32 1, ptr %5, align 8, !tbaa !5
  %17 = call i32 @mbedtls_mpi_cmp_int(ptr noundef nonnull %5, i64 noundef 0)
  %.not2634 = icmp eq i32 %17, 0
  br i1 %.not2634, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %16, %34
  %18 = call i64 @mbedtls_mpi_lsb(ptr noundef nonnull %5)
  %19 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %5, i64 noundef %18)
  %.not28 = icmp eq i32 %19, 0
  br i1 %.not28, label %20, label %.loopexit

20:                                               ; preds = %.lr.ph
  %21 = call i64 @mbedtls_mpi_lsb(ptr noundef nonnull %6)
  %22 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %6, i64 noundef %21)
  %.not29 = icmp eq i32 %22, 0
  br i1 %.not29, label %23, label %.loopexit

23:                                               ; preds = %20
  %24 = call i32 @mbedtls_mpi_cmp_mpi(ptr noundef nonnull %5, ptr noundef nonnull %6)
  %25 = icmp sgt i32 %24, -1
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = call i32 @mbedtls_mpi_sub_abs(ptr noundef nonnull %5, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %.not32 = icmp eq i32 %27, 0
  br i1 %.not32, label %28, label %.loopexit

28:                                               ; preds = %26
  %29 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %5, i64 noundef 1)
  %.not33 = icmp eq i32 %29, 0
  br i1 %.not33, label %34, label %.loopexit

30:                                               ; preds = %23
  %31 = call i32 @mbedtls_mpi_sub_abs(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %5)
  %.not30 = icmp eq i32 %31, 0
  br i1 %.not30, label %32, label %.loopexit

32:                                               ; preds = %30
  %33 = call i32 @mbedtls_mpi_shift_r(ptr noundef nonnull %6, i64 noundef 1)
  %.not31 = icmp eq i32 %33, 0
  br i1 %.not31, label %34, label %.loopexit

34:                                               ; preds = %32, %28
  %35 = call i32 @mbedtls_mpi_cmp_int(ptr noundef nonnull %5, i64 noundef 0)
  %.not26 = icmp eq i32 %35, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %34, %16
  %36 = call i32 @mbedtls_mpi_shift_l(ptr noundef nonnull %6, i64 noundef %spec.select)
  %.not27 = icmp eq i32 %36, 0
  br i1 %.not27, label %37, label %.loopexit

37:                                               ; preds = %._crit_edge
  %38 = call i32 @mbedtls_mpi_copy(ptr noundef %0, ptr noundef nonnull %6)
  br label %.loopexit

.loopexit:                                        ; preds = %32, %30, %28, %26, %20, %.lr.ph, %37, %._crit_edge, %14, %10, %8, %3
  %.0 = phi i32 [ %7, %3 ], [ %9, %8 ], [ %13, %10 ], [ %15, %14 ], [ %36, %._crit_edge ], [ %38, %37 ], [ %33, %32 ], [ %31, %30 ], [ %29, %28 ], [ %27, %26 ], [ %22, %20 ], [ %19, %.lr.ph ]
  call void @mbedtls_mpi_free(ptr noundef nonnull %4)
  call void @mbedtls_mpi_free(ptr noundef nonnull %5)
  call void @mbedtls_mpi_free(ptr noundef nonnull %6)
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #3
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass