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

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @mpi_get_digit(ptr noundef, i32 noundef, i8 noundef signext) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_read_file(ptr noundef %0, i32 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca [2484 x i8], align 16
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 2484, ptr %11) #5
  %13 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %13, i8 0, i64 2484, i1 false)
  %14 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 0
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = call ptr @fgets(ptr noundef %14, i32 noundef 2483, ptr noundef %15)
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store i32 -2, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %78

19:                                               ; preds = %3
  %20 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 0
  %21 = call i64 @strlen(ptr noundef %20) #6
  store i64 %21, ptr %9, align 8, !tbaa !11
  %22 = load i64, ptr %9, align 8, !tbaa !11
  %23 = icmp eq i64 %22, 2482
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  store i32 -8, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %78

25:                                               ; preds = %19
  %26 = load i64, ptr %9, align 8, !tbaa !11
  %27 = icmp ugt i64 %26, 0
  br i1 %27, label %28, label %40

28:                                               ; preds = %25
  %29 = load i64, ptr %9, align 8, !tbaa !11
  %30 = sub i64 %29, 1
  %31 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = sext i8 %32 to i32
  %34 = icmp eq i32 %33, 10
  br i1 %34, label %35, label %40

35:                                               ; preds = %28
  %36 = load i64, ptr %9, align 8, !tbaa !11
  %37 = add i64 %36, -1
  store i64 %37, ptr %9, align 8, !tbaa !11
  %38 = load i64, ptr %9, align 8, !tbaa !11
  %39 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 %38
  store i8 0, ptr %39, align 1, !tbaa !13
  br label %40

40:                                               ; preds = %35, %28, %25
  %41 = load i64, ptr %9, align 8, !tbaa !11
  %42 = icmp ugt i64 %41, 0
  br i1 %42, label %43, label %55

43:                                               ; preds = %40
  %44 = load i64, ptr %9, align 8, !tbaa !11
  %45 = sub i64 %44, 1
  %46 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !13
  %48 = sext i8 %47 to i32
  %49 = icmp eq i32 %48, 13
  br i1 %49, label %50, label %55

50:                                               ; preds = %43
  %51 = load i64, ptr %9, align 8, !tbaa !11
  %52 = add i64 %51, -1
  store i64 %52, ptr %9, align 8, !tbaa !11
  %53 = load i64, ptr %9, align 8, !tbaa !11
  %54 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 %53
  store i8 0, ptr %54, align 1, !tbaa !13
  br label %55

55:                                               ; preds = %50, %43, %40
  %56 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 0
  %57 = load i64, ptr %9, align 8, !tbaa !11
  %58 = getelementptr inbounds i8, ptr %56, i64 %57
  store ptr %58, ptr %10, align 8, !tbaa !5
  br label %59

59:                                               ; preds = %71, %55
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %60, i32 -1
  store ptr %61, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds [2484 x i8], ptr %11, i64 0, i64 0
  %63 = icmp ugt ptr %60, %62
  br i1 %63, label %64, label %72

64:                                               ; preds = %59
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = load i8, ptr %66, align 1, !tbaa !13
  %68 = call i32 @mpi_get_digit(ptr noundef %8, i32 noundef %65, i8 noundef signext %67)
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %71

70:                                               ; preds = %64
  br label %72

71:                                               ; preds = %64
  br label %59, !llvm.loop !14

72:                                               ; preds = %70, %59
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = load ptr, ptr %10, align 8, !tbaa !5
  %76 = getelementptr inbounds i8, ptr %75, i64 1
  %77 = call i32 @mbedtls_mpi_read_string(ptr noundef %73, i32 noundef %74, ptr noundef %76)
  store i32 %77, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %78

78:                                               ; preds = %72, %24, %18
  call void @llvm.lifetime.end.p0(i64 2484, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %79 = load i32, ptr %4, align 4
  ret i32 %79
}

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 willreturn memory(read) "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 #4 = { "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i32 @mpi_get_digit(ptr noundef, i32 noundef, i8 noundef signext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_mpi_read_file(ptr noundef %0, i32 noundef %1, ptr nocapture noundef %2) local_unnamed_addr #2 {
  %4 = alloca i64, align 8
  %5 = alloca [2484 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 2484, ptr nonnull %5) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(2484) %5, i8 0, i64 2484, i1 false)
  %6 = call ptr @fgets(ptr noundef nonnull %5, i32 noundef 2483, ptr noundef %2)
  %7 = icmp eq ptr %6, null
  br i1 %7, label %29, label %8

8:                                                ; preds = %3
  %9 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %5) #6
  switch i64 %9, label %10 [
    i64 2482, label %29
    i64 0, label %.thread
  ]

10:                                               ; preds = %8
  %11 = add i64 %9, -1
  %12 = getelementptr inbounds [2484 x i8], ptr %5, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = icmp eq i8 %13, 10
  br i1 %14, label %15, label %.thread24

15:                                               ; preds = %10
  store i8 0, ptr %12, align 1, !tbaa !5
  %.not20 = icmp eq i64 %11, 0
  br i1 %.not20, label %.thread, label %..thread24_crit_edge

..thread24_crit_edge:                             ; preds = %15
  %.pre = add i64 %9, -2
  %.phi.trans.insert = getelementptr inbounds [2484 x i8], ptr %5, i64 0, i64 %.pre
  %.pre28 = load i8, ptr %.phi.trans.insert, align 1, !tbaa !5
  br label %.thread24

.thread24:                                        ; preds = %..thread24_crit_edge, %10
  %16 = phi i8 [ %.pre28, %..thread24_crit_edge ], [ %13, %10 ]
  %.pre-phi = phi i64 [ %.pre, %..thread24_crit_edge ], [ %11, %10 ]
  %.01727 = phi i64 [ %11, %..thread24_crit_edge ], [ %9, %10 ]
  %17 = icmp eq i8 %16, 13
  br i1 %17, label %18, label %.thread

18:                                               ; preds = %.thread24
  %19 = getelementptr inbounds [2484 x i8], ptr %5, i64 0, i64 %.pre-phi
  store i8 0, ptr %19, align 1, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %8, %18, %.thread24, %15
  %.1 = phi i64 [ %.pre-phi, %18 ], [ %.01727, %.thread24 ], [ 0, %15 ], [ %9, %8 ]
  %20 = getelementptr inbounds i8, ptr %5, i64 %.1
  br label %21

21:                                               ; preds = %23, %.thread
  %.0 = phi ptr [ %20, %.thread ], [ %24, %23 ]
  %22 = icmp ugt ptr %.0, %5
  br i1 %22, label %23, label %27

23:                                               ; preds = %21
  %24 = getelementptr inbounds i8, ptr %.0, i64 -1
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = call i32 @mpi_get_digit(ptr noundef nonnull %4, i32 noundef %1, i8 noundef signext %25)
  %.not21 = icmp eq i32 %26, 0
  br i1 %.not21, label %21, label %27, !llvm.loop !8

27:                                               ; preds = %23, %21
  %28 = call i32 @mbedtls_mpi_read_string(ptr noundef %0, i32 noundef %1, ptr noundef nonnull %.0)
  br label %29

29:                                               ; preds = %8, %3, %27
  %.018 = phi i32 [ %28, %27 ], [ -2, %3 ], [ -8, %8 ]
  call void @llvm.lifetime.end.p0(i64 2484, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  ret i32 %.018
}

; Function Attrs: nofree nounwind
declare noundef ptr @fgets(ptr noundef, i32 noundef, ptr nocapture noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 #4 = { nofree nounwind "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass